glsl/ir: remove TABs in ir_constant_expression.cpp
[mesa.git] / src / compiler / 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 "compiler/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_d2f:
652 assert(op[0]->type->base_type == GLSL_TYPE_DOUBLE);
653 for (unsigned c = 0; c < op[0]->type->components(); c++) {
654 data.f[c] = op[0]->value.d[c];
655 }
656 break;
657 case ir_unop_f2d:
658 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
659 for (unsigned c = 0; c < op[0]->type->components(); c++) {
660 data.d[c] = op[0]->value.f[c];
661 }
662 break;
663 case ir_unop_d2i:
664 assert(op[0]->type->base_type == GLSL_TYPE_DOUBLE);
665 for (unsigned c = 0; c < op[0]->type->components(); c++) {
666 data.i[c] = op[0]->value.d[c];
667 }
668 break;
669 case ir_unop_i2d:
670 assert(op[0]->type->base_type == GLSL_TYPE_INT);
671 for (unsigned c = 0; c < op[0]->type->components(); c++) {
672 data.d[c] = op[0]->value.i[c];
673 }
674 break;
675 case ir_unop_d2u:
676 assert(op[0]->type->base_type == GLSL_TYPE_DOUBLE);
677 for (unsigned c = 0; c < op[0]->type->components(); c++) {
678 data.u[c] = op[0]->value.d[c];
679 }
680 break;
681 case ir_unop_u2d:
682 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
683 for (unsigned c = 0; c < op[0]->type->components(); c++) {
684 data.d[c] = op[0]->value.u[c];
685 }
686 break;
687 case ir_unop_d2b:
688 assert(op[0]->type->base_type == GLSL_TYPE_DOUBLE);
689 for (unsigned c = 0; c < op[0]->type->components(); c++) {
690 data.b[c] = op[0]->value.d[c] != 0.0;
691 }
692 break;
693 case ir_unop_trunc:
694 for (unsigned c = 0; c < op[0]->type->components(); c++) {
695 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
696 data.d[c] = trunc(op[0]->value.d[c]);
697 else
698 data.f[c] = truncf(op[0]->value.f[c]);
699 }
700 break;
701
702 case ir_unop_round_even:
703 for (unsigned c = 0; c < op[0]->type->components(); c++) {
704 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
705 data.d[c] = _mesa_roundeven(op[0]->value.d[c]);
706 else
707 data.f[c] = _mesa_roundevenf(op[0]->value.f[c]);
708 }
709 break;
710
711 case ir_unop_ceil:
712 for (unsigned c = 0; c < op[0]->type->components(); c++) {
713 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
714 data.d[c] = ceil(op[0]->value.d[c]);
715 else
716 data.f[c] = ceilf(op[0]->value.f[c]);
717 }
718 break;
719
720 case ir_unop_floor:
721 for (unsigned c = 0; c < op[0]->type->components(); c++) {
722 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
723 data.d[c] = floor(op[0]->value.d[c]);
724 else
725 data.f[c] = floorf(op[0]->value.f[c]);
726 }
727 break;
728
729 case ir_unop_fract:
730 for (unsigned c = 0; c < op[0]->type->components(); c++) {
731 switch (this->type->base_type) {
732 case GLSL_TYPE_UINT:
733 data.u[c] = 0;
734 break;
735 case GLSL_TYPE_INT:
736 data.i[c] = 0;
737 break;
738 case GLSL_TYPE_FLOAT:
739 data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]);
740 break;
741 case GLSL_TYPE_DOUBLE:
742 data.d[c] = op[0]->value.d[c] - floor(op[0]->value.d[c]);
743 break;
744 default:
745 assert(0);
746 }
747 }
748 break;
749
750 case ir_unop_sin:
751 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
752 for (unsigned c = 0; c < op[0]->type->components(); c++) {
753 data.f[c] = sinf(op[0]->value.f[c]);
754 }
755 break;
756
757 case ir_unop_cos:
758 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
759 for (unsigned c = 0; c < op[0]->type->components(); c++) {
760 data.f[c] = cosf(op[0]->value.f[c]);
761 }
762 break;
763
764 case ir_unop_neg:
765 for (unsigned c = 0; c < op[0]->type->components(); c++) {
766 switch (this->type->base_type) {
767 case GLSL_TYPE_UINT:
768 data.u[c] = -((int) op[0]->value.u[c]);
769 break;
770 case GLSL_TYPE_INT:
771 data.i[c] = -op[0]->value.i[c];
772 break;
773 case GLSL_TYPE_FLOAT:
774 data.f[c] = -op[0]->value.f[c];
775 break;
776 case GLSL_TYPE_DOUBLE:
777 data.d[c] = -op[0]->value.d[c];
778 break;
779 default:
780 assert(0);
781 }
782 }
783 break;
784
785 case ir_unop_abs:
786 for (unsigned c = 0; c < op[0]->type->components(); c++) {
787 switch (this->type->base_type) {
788 case GLSL_TYPE_UINT:
789 data.u[c] = op[0]->value.u[c];
790 break;
791 case GLSL_TYPE_INT:
792 data.i[c] = op[0]->value.i[c];
793 if (data.i[c] < 0)
794 data.i[c] = -data.i[c];
795 break;
796 case GLSL_TYPE_FLOAT:
797 data.f[c] = fabs(op[0]->value.f[c]);
798 break;
799 case GLSL_TYPE_DOUBLE:
800 data.d[c] = fabs(op[0]->value.d[c]);
801 break;
802 default:
803 assert(0);
804 }
805 }
806 break;
807
808 case ir_unop_sign:
809 for (unsigned c = 0; c < op[0]->type->components(); c++) {
810 switch (this->type->base_type) {
811 case GLSL_TYPE_UINT:
812 data.u[c] = op[0]->value.i[c] > 0;
813 break;
814 case GLSL_TYPE_INT:
815 data.i[c] = (op[0]->value.i[c] > 0) - (op[0]->value.i[c] < 0);
816 break;
817 case GLSL_TYPE_FLOAT:
818 data.f[c] = float((op[0]->value.f[c] > 0)-(op[0]->value.f[c] < 0));
819 break;
820 case GLSL_TYPE_DOUBLE:
821 data.d[c] = double((op[0]->value.d[c] > 0)-(op[0]->value.d[c] < 0));
822 break;
823 default:
824 assert(0);
825 }
826 }
827 break;
828
829 case ir_unop_rcp:
830 for (unsigned c = 0; c < op[0]->type->components(); c++) {
831 switch (this->type->base_type) {
832 case GLSL_TYPE_UINT:
833 if (op[0]->value.u[c] != 0.0)
834 data.u[c] = 1 / op[0]->value.u[c];
835 break;
836 case GLSL_TYPE_INT:
837 if (op[0]->value.i[c] != 0.0)
838 data.i[c] = 1 / op[0]->value.i[c];
839 break;
840 case GLSL_TYPE_FLOAT:
841 if (op[0]->value.f[c] != 0.0)
842 data.f[c] = 1.0F / op[0]->value.f[c];
843 break;
844 case GLSL_TYPE_DOUBLE:
845 if (op[0]->value.d[c] != 0.0)
846 data.d[c] = 1.0 / op[0]->value.d[c];
847 break;
848 default:
849 assert(0);
850 }
851 }
852 break;
853
854 case ir_unop_rsq:
855 for (unsigned c = 0; c < op[0]->type->components(); c++) {
856 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
857 data.d[c] = 1.0 / sqrt(op[0]->value.d[c]);
858 else
859 data.f[c] = 1.0F / sqrtf(op[0]->value.f[c]);
860 }
861 break;
862
863 case ir_unop_sqrt:
864 for (unsigned c = 0; c < op[0]->type->components(); c++) {
865 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
866 data.d[c] = sqrt(op[0]->value.d[c]);
867 else
868 data.f[c] = sqrtf(op[0]->value.f[c]);
869 }
870 break;
871
872 case ir_unop_exp:
873 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
874 for (unsigned c = 0; c < op[0]->type->components(); c++) {
875 data.f[c] = expf(op[0]->value.f[c]);
876 }
877 break;
878
879 case ir_unop_exp2:
880 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
881 for (unsigned c = 0; c < op[0]->type->components(); c++) {
882 data.f[c] = exp2f(op[0]->value.f[c]);
883 }
884 break;
885
886 case ir_unop_log:
887 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
888 for (unsigned c = 0; c < op[0]->type->components(); c++) {
889 data.f[c] = logf(op[0]->value.f[c]);
890 }
891 break;
892
893 case ir_unop_log2:
894 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
895 for (unsigned c = 0; c < op[0]->type->components(); c++) {
896 data.f[c] = log2f(op[0]->value.f[c]);
897 }
898 break;
899
900 case ir_unop_dFdx:
901 case ir_unop_dFdx_coarse:
902 case ir_unop_dFdx_fine:
903 case ir_unop_dFdy:
904 case ir_unop_dFdy_coarse:
905 case ir_unop_dFdy_fine:
906 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
907 for (unsigned c = 0; c < op[0]->type->components(); c++) {
908 data.f[c] = 0.0;
909 }
910 break;
911
912 case ir_unop_pack_snorm_2x16:
913 assert(op[0]->type == glsl_type::vec2_type);
914 data.u[0] = pack_2x16(pack_snorm_1x16,
915 op[0]->value.f[0],
916 op[0]->value.f[1]);
917 break;
918 case ir_unop_pack_snorm_4x8:
919 assert(op[0]->type == glsl_type::vec4_type);
920 data.u[0] = pack_4x8(pack_snorm_1x8,
921 op[0]->value.f[0],
922 op[0]->value.f[1],
923 op[0]->value.f[2],
924 op[0]->value.f[3]);
925 break;
926 case ir_unop_unpack_snorm_2x16:
927 assert(op[0]->type == glsl_type::uint_type);
928 unpack_2x16(unpack_snorm_1x16,
929 op[0]->value.u[0],
930 &data.f[0], &data.f[1]);
931 break;
932 case ir_unop_unpack_snorm_4x8:
933 assert(op[0]->type == glsl_type::uint_type);
934 unpack_4x8(unpack_snorm_1x8,
935 op[0]->value.u[0],
936 &data.f[0], &data.f[1], &data.f[2], &data.f[3]);
937 break;
938 case ir_unop_pack_unorm_2x16:
939 assert(op[0]->type == glsl_type::vec2_type);
940 data.u[0] = pack_2x16(pack_unorm_1x16,
941 op[0]->value.f[0],
942 op[0]->value.f[1]);
943 break;
944 case ir_unop_pack_unorm_4x8:
945 assert(op[0]->type == glsl_type::vec4_type);
946 data.u[0] = pack_4x8(pack_unorm_1x8,
947 op[0]->value.f[0],
948 op[0]->value.f[1],
949 op[0]->value.f[2],
950 op[0]->value.f[3]);
951 break;
952 case ir_unop_unpack_unorm_2x16:
953 assert(op[0]->type == glsl_type::uint_type);
954 unpack_2x16(unpack_unorm_1x16,
955 op[0]->value.u[0],
956 &data.f[0], &data.f[1]);
957 break;
958 case ir_unop_unpack_unorm_4x8:
959 assert(op[0]->type == glsl_type::uint_type);
960 unpack_4x8(unpack_unorm_1x8,
961 op[0]->value.u[0],
962 &data.f[0], &data.f[1], &data.f[2], &data.f[3]);
963 break;
964 case ir_unop_pack_half_2x16:
965 assert(op[0]->type == glsl_type::vec2_type);
966 data.u[0] = pack_2x16(pack_half_1x16,
967 op[0]->value.f[0],
968 op[0]->value.f[1]);
969 break;
970 case ir_unop_unpack_half_2x16:
971 assert(op[0]->type == glsl_type::uint_type);
972 unpack_2x16(unpack_half_1x16,
973 op[0]->value.u[0],
974 &data.f[0], &data.f[1]);
975 break;
976 case ir_binop_pow:
977 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
978 for (unsigned c = 0; c < op[0]->type->components(); c++) {
979 data.f[c] = powf(op[0]->value.f[c], op[1]->value.f[c]);
980 }
981 break;
982
983 case ir_binop_dot:
984 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
985 data.d[0] = dot_d(op[0], op[1]);
986 else
987 data.f[0] = dot_f(op[0], op[1]);
988 break;
989
990 case ir_binop_min:
991 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
992 for (unsigned c = 0, c0 = 0, c1 = 0;
993 c < components;
994 c0 += c0_inc, c1 += c1_inc, c++) {
995
996 switch (op[0]->type->base_type) {
997 case GLSL_TYPE_UINT:
998 data.u[c] = MIN2(op[0]->value.u[c0], op[1]->value.u[c1]);
999 break;
1000 case GLSL_TYPE_INT:
1001 data.i[c] = MIN2(op[0]->value.i[c0], op[1]->value.i[c1]);
1002 break;
1003 case GLSL_TYPE_FLOAT:
1004 data.f[c] = MIN2(op[0]->value.f[c0], op[1]->value.f[c1]);
1005 break;
1006 case GLSL_TYPE_DOUBLE:
1007 data.d[c] = MIN2(op[0]->value.d[c0], op[1]->value.d[c1]);
1008 break;
1009 default:
1010 assert(0);
1011 }
1012 }
1013
1014 break;
1015 case ir_binop_max:
1016 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1017 for (unsigned c = 0, c0 = 0, c1 = 0;
1018 c < components;
1019 c0 += c0_inc, c1 += c1_inc, c++) {
1020
1021 switch (op[0]->type->base_type) {
1022 case GLSL_TYPE_UINT:
1023 data.u[c] = MAX2(op[0]->value.u[c0], op[1]->value.u[c1]);
1024 break;
1025 case GLSL_TYPE_INT:
1026 data.i[c] = MAX2(op[0]->value.i[c0], op[1]->value.i[c1]);
1027 break;
1028 case GLSL_TYPE_FLOAT:
1029 data.f[c] = MAX2(op[0]->value.f[c0], op[1]->value.f[c1]);
1030 break;
1031 case GLSL_TYPE_DOUBLE:
1032 data.d[c] = MAX2(op[0]->value.d[c0], op[1]->value.d[c1]);
1033 break;
1034 default:
1035 assert(0);
1036 }
1037 }
1038 break;
1039
1040 case ir_binop_add:
1041 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1042 for (unsigned c = 0, c0 = 0, c1 = 0;
1043 c < components;
1044 c0 += c0_inc, c1 += c1_inc, c++) {
1045
1046 switch (op[0]->type->base_type) {
1047 case GLSL_TYPE_UINT:
1048 data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1];
1049 break;
1050 case GLSL_TYPE_INT:
1051 data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1];
1052 break;
1053 case GLSL_TYPE_FLOAT:
1054 data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1];
1055 break;
1056 case GLSL_TYPE_DOUBLE:
1057 data.d[c] = op[0]->value.d[c0] + op[1]->value.d[c1];
1058 break;
1059 default:
1060 assert(0);
1061 }
1062 }
1063
1064 break;
1065 case ir_binop_sub:
1066 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1067 for (unsigned c = 0, c0 = 0, c1 = 0;
1068 c < components;
1069 c0 += c0_inc, c1 += c1_inc, c++) {
1070
1071 switch (op[0]->type->base_type) {
1072 case GLSL_TYPE_UINT:
1073 data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1];
1074 break;
1075 case GLSL_TYPE_INT:
1076 data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1];
1077 break;
1078 case GLSL_TYPE_FLOAT:
1079 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1];
1080 break;
1081 case GLSL_TYPE_DOUBLE:
1082 data.d[c] = op[0]->value.d[c0] - op[1]->value.d[c1];
1083 break;
1084 default:
1085 assert(0);
1086 }
1087 }
1088
1089 break;
1090 case ir_binop_mul:
1091 /* Check for equal types, or unequal types involving scalars */
1092 if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix())
1093 || op0_scalar || op1_scalar) {
1094 for (unsigned c = 0, c0 = 0, c1 = 0;
1095 c < components;
1096 c0 += c0_inc, c1 += c1_inc, c++) {
1097
1098 switch (op[0]->type->base_type) {
1099 case GLSL_TYPE_UINT:
1100 data.u[c] = op[0]->value.u[c0] * op[1]->value.u[c1];
1101 break;
1102 case GLSL_TYPE_INT:
1103 data.i[c] = op[0]->value.i[c0] * op[1]->value.i[c1];
1104 break;
1105 case GLSL_TYPE_FLOAT:
1106 data.f[c] = op[0]->value.f[c0] * op[1]->value.f[c1];
1107 break;
1108 case GLSL_TYPE_DOUBLE:
1109 data.d[c] = op[0]->value.d[c0] * op[1]->value.d[c1];
1110 break;
1111 default:
1112 assert(0);
1113 }
1114 }
1115 } else {
1116 assert(op[0]->type->is_matrix() || op[1]->type->is_matrix());
1117
1118 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
1119 * matrix can be a GLSL vector, either N or P can be 1.
1120 *
1121 * For vec*mat, the vector is treated as a row vector. This
1122 * means the vector is a 1-row x M-column matrix.
1123 *
1124 * For mat*vec, the vector is treated as a column vector. Since
1125 * matrix_columns is 1 for vectors, this just works.
1126 */
1127 const unsigned n = op[0]->type->is_vector()
1128 ? 1 : op[0]->type->vector_elements;
1129 const unsigned m = op[1]->type->vector_elements;
1130 const unsigned p = op[1]->type->matrix_columns;
1131 for (unsigned j = 0; j < p; j++) {
1132 for (unsigned i = 0; i < n; i++) {
1133 for (unsigned k = 0; k < m; k++) {
1134 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
1135 data.d[i+n*j] += op[0]->value.d[i+n*k]*op[1]->value.d[k+m*j];
1136 else
1137 data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j];
1138 }
1139 }
1140 }
1141 }
1142
1143 break;
1144 case ir_binop_div:
1145 /* FINISHME: Emit warning when division-by-zero is detected. */
1146 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1147 for (unsigned c = 0, c0 = 0, c1 = 0;
1148 c < components;
1149 c0 += c0_inc, c1 += c1_inc, c++) {
1150
1151 switch (op[0]->type->base_type) {
1152 case GLSL_TYPE_UINT:
1153 if (op[1]->value.u[c1] == 0) {
1154 data.u[c] = 0;
1155 } else {
1156 data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1];
1157 }
1158 break;
1159 case GLSL_TYPE_INT:
1160 if (op[1]->value.i[c1] == 0) {
1161 data.i[c] = 0;
1162 } else {
1163 data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1];
1164 }
1165 break;
1166 case GLSL_TYPE_FLOAT:
1167 data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1];
1168 break;
1169 case GLSL_TYPE_DOUBLE:
1170 data.d[c] = op[0]->value.d[c0] / op[1]->value.d[c1];
1171 break;
1172 default:
1173 assert(0);
1174 }
1175 }
1176
1177 break;
1178 case ir_binop_mod:
1179 /* FINISHME: Emit warning when division-by-zero is detected. */
1180 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1181 for (unsigned c = 0, c0 = 0, c1 = 0;
1182 c < components;
1183 c0 += c0_inc, c1 += c1_inc, c++) {
1184
1185 switch (op[0]->type->base_type) {
1186 case GLSL_TYPE_UINT:
1187 if (op[1]->value.u[c1] == 0) {
1188 data.u[c] = 0;
1189 } else {
1190 data.u[c] = op[0]->value.u[c0] % op[1]->value.u[c1];
1191 }
1192 break;
1193 case GLSL_TYPE_INT:
1194 if (op[1]->value.i[c1] == 0) {
1195 data.i[c] = 0;
1196 } else {
1197 data.i[c] = op[0]->value.i[c0] % op[1]->value.i[c1];
1198 }
1199 break;
1200 case GLSL_TYPE_FLOAT:
1201 /* We don't use fmod because it rounds toward zero; GLSL specifies
1202 * the use of floor.
1203 */
1204 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1]
1205 * floorf(op[0]->value.f[c0] / op[1]->value.f[c1]);
1206 break;
1207 case GLSL_TYPE_DOUBLE:
1208 /* We don't use fmod because it rounds toward zero; GLSL specifies
1209 * the use of floor.
1210 */
1211 data.d[c] = op[0]->value.d[c0] - op[1]->value.d[c1]
1212 * floor(op[0]->value.d[c0] / op[1]->value.d[c1]);
1213 break;
1214 default:
1215 assert(0);
1216 }
1217 }
1218
1219 break;
1220
1221 case ir_binop_logic_and:
1222 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
1223 for (unsigned c = 0; c < op[0]->type->components(); c++)
1224 data.b[c] = op[0]->value.b[c] && op[1]->value.b[c];
1225 break;
1226 case ir_binop_logic_xor:
1227 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
1228 for (unsigned c = 0; c < op[0]->type->components(); c++)
1229 data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
1230 break;
1231 case ir_binop_logic_or:
1232 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
1233 for (unsigned c = 0; c < op[0]->type->components(); c++)
1234 data.b[c] = op[0]->value.b[c] || op[1]->value.b[c];
1235 break;
1236
1237 case ir_binop_less:
1238 assert(op[0]->type == op[1]->type);
1239 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1240 switch (op[0]->type->base_type) {
1241 case GLSL_TYPE_UINT:
1242 data.b[c] = op[0]->value.u[c] < op[1]->value.u[c];
1243 break;
1244 case GLSL_TYPE_INT:
1245 data.b[c] = op[0]->value.i[c] < op[1]->value.i[c];
1246 break;
1247 case GLSL_TYPE_FLOAT:
1248 data.b[c] = op[0]->value.f[c] < op[1]->value.f[c];
1249 break;
1250 case GLSL_TYPE_DOUBLE:
1251 data.b[c] = op[0]->value.d[c] < op[1]->value.d[c];
1252 break;
1253 default:
1254 assert(0);
1255 }
1256 }
1257 break;
1258 case ir_binop_greater:
1259 assert(op[0]->type == op[1]->type);
1260 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1261 switch (op[0]->type->base_type) {
1262 case GLSL_TYPE_UINT:
1263 data.b[c] = op[0]->value.u[c] > op[1]->value.u[c];
1264 break;
1265 case GLSL_TYPE_INT:
1266 data.b[c] = op[0]->value.i[c] > op[1]->value.i[c];
1267 break;
1268 case GLSL_TYPE_FLOAT:
1269 data.b[c] = op[0]->value.f[c] > op[1]->value.f[c];
1270 break;
1271 case GLSL_TYPE_DOUBLE:
1272 data.b[c] = op[0]->value.d[c] > op[1]->value.d[c];
1273 break;
1274 default:
1275 assert(0);
1276 }
1277 }
1278 break;
1279 case ir_binop_lequal:
1280 assert(op[0]->type == op[1]->type);
1281 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1282 switch (op[0]->type->base_type) {
1283 case GLSL_TYPE_UINT:
1284 data.b[c] = op[0]->value.u[c] <= op[1]->value.u[c];
1285 break;
1286 case GLSL_TYPE_INT:
1287 data.b[c] = op[0]->value.i[c] <= op[1]->value.i[c];
1288 break;
1289 case GLSL_TYPE_FLOAT:
1290 data.b[c] = op[0]->value.f[c] <= op[1]->value.f[c];
1291 break;
1292 case GLSL_TYPE_DOUBLE:
1293 data.b[c] = op[0]->value.d[c] <= op[1]->value.d[c];
1294 break;
1295 default:
1296 assert(0);
1297 }
1298 }
1299 break;
1300 case ir_binop_gequal:
1301 assert(op[0]->type == op[1]->type);
1302 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1303 switch (op[0]->type->base_type) {
1304 case GLSL_TYPE_UINT:
1305 data.b[c] = op[0]->value.u[c] >= op[1]->value.u[c];
1306 break;
1307 case GLSL_TYPE_INT:
1308 data.b[c] = op[0]->value.i[c] >= op[1]->value.i[c];
1309 break;
1310 case GLSL_TYPE_FLOAT:
1311 data.b[c] = op[0]->value.f[c] >= op[1]->value.f[c];
1312 break;
1313 case GLSL_TYPE_DOUBLE:
1314 data.b[c] = op[0]->value.d[c] >= op[1]->value.d[c];
1315 break;
1316 default:
1317 assert(0);
1318 }
1319 }
1320 break;
1321 case ir_binop_equal:
1322 assert(op[0]->type == op[1]->type);
1323 for (unsigned c = 0; c < components; c++) {
1324 switch (op[0]->type->base_type) {
1325 case GLSL_TYPE_UINT:
1326 data.b[c] = op[0]->value.u[c] == op[1]->value.u[c];
1327 break;
1328 case GLSL_TYPE_INT:
1329 data.b[c] = op[0]->value.i[c] == op[1]->value.i[c];
1330 break;
1331 case GLSL_TYPE_FLOAT:
1332 data.b[c] = op[0]->value.f[c] == op[1]->value.f[c];
1333 break;
1334 case GLSL_TYPE_BOOL:
1335 data.b[c] = op[0]->value.b[c] == op[1]->value.b[c];
1336 break;
1337 case GLSL_TYPE_DOUBLE:
1338 data.b[c] = op[0]->value.d[c] == op[1]->value.d[c];
1339 break;
1340 default:
1341 assert(0);
1342 }
1343 }
1344 break;
1345 case ir_binop_nequal:
1346 assert(op[0]->type == op[1]->type);
1347 for (unsigned c = 0; c < components; c++) {
1348 switch (op[0]->type->base_type) {
1349 case GLSL_TYPE_UINT:
1350 data.b[c] = op[0]->value.u[c] != op[1]->value.u[c];
1351 break;
1352 case GLSL_TYPE_INT:
1353 data.b[c] = op[0]->value.i[c] != op[1]->value.i[c];
1354 break;
1355 case GLSL_TYPE_FLOAT:
1356 data.b[c] = op[0]->value.f[c] != op[1]->value.f[c];
1357 break;
1358 case GLSL_TYPE_BOOL:
1359 data.b[c] = op[0]->value.b[c] != op[1]->value.b[c];
1360 break;
1361 case GLSL_TYPE_DOUBLE:
1362 data.b[c] = op[0]->value.d[c] != op[1]->value.d[c];
1363 break;
1364 default:
1365 assert(0);
1366 }
1367 }
1368 break;
1369 case ir_binop_all_equal:
1370 data.b[0] = op[0]->has_value(op[1]);
1371 break;
1372 case ir_binop_any_nequal:
1373 data.b[0] = !op[0]->has_value(op[1]);
1374 break;
1375
1376 case ir_binop_lshift:
1377 for (unsigned c = 0, c0 = 0, c1 = 0;
1378 c < components;
1379 c0 += c0_inc, c1 += c1_inc, c++) {
1380
1381 if (op[0]->type->base_type == GLSL_TYPE_INT &&
1382 op[1]->type->base_type == GLSL_TYPE_INT) {
1383 data.i[c] = op[0]->value.i[c0] << op[1]->value.i[c1];
1384
1385 } else if (op[0]->type->base_type == GLSL_TYPE_INT &&
1386 op[1]->type->base_type == GLSL_TYPE_UINT) {
1387 data.i[c] = op[0]->value.i[c0] << op[1]->value.u[c1];
1388
1389 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1390 op[1]->type->base_type == GLSL_TYPE_INT) {
1391 data.u[c] = op[0]->value.u[c0] << op[1]->value.i[c1];
1392
1393 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1394 op[1]->type->base_type == GLSL_TYPE_UINT) {
1395 data.u[c] = op[0]->value.u[c0] << op[1]->value.u[c1];
1396 }
1397 }
1398 break;
1399
1400 case ir_binop_rshift:
1401 for (unsigned c = 0, c0 = 0, c1 = 0;
1402 c < components;
1403 c0 += c0_inc, c1 += c1_inc, c++) {
1404
1405 if (op[0]->type->base_type == GLSL_TYPE_INT &&
1406 op[1]->type->base_type == GLSL_TYPE_INT) {
1407 data.i[c] = op[0]->value.i[c0] >> op[1]->value.i[c1];
1408
1409 } else if (op[0]->type->base_type == GLSL_TYPE_INT &&
1410 op[1]->type->base_type == GLSL_TYPE_UINT) {
1411 data.i[c] = op[0]->value.i[c0] >> op[1]->value.u[c1];
1412
1413 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1414 op[1]->type->base_type == GLSL_TYPE_INT) {
1415 data.u[c] = op[0]->value.u[c0] >> op[1]->value.i[c1];
1416
1417 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1418 op[1]->type->base_type == GLSL_TYPE_UINT) {
1419 data.u[c] = op[0]->value.u[c0] >> op[1]->value.u[c1];
1420 }
1421 }
1422 break;
1423
1424 case ir_binop_bit_and:
1425 for (unsigned c = 0, c0 = 0, c1 = 0;
1426 c < components;
1427 c0 += c0_inc, c1 += c1_inc, c++) {
1428
1429 switch (op[0]->type->base_type) {
1430 case GLSL_TYPE_INT:
1431 data.i[c] = op[0]->value.i[c0] & op[1]->value.i[c1];
1432 break;
1433 case GLSL_TYPE_UINT:
1434 data.u[c] = op[0]->value.u[c0] & op[1]->value.u[c1];
1435 break;
1436 default:
1437 assert(0);
1438 }
1439 }
1440 break;
1441
1442 case ir_binop_bit_or:
1443 for (unsigned c = 0, c0 = 0, c1 = 0;
1444 c < components;
1445 c0 += c0_inc, c1 += c1_inc, c++) {
1446
1447 switch (op[0]->type->base_type) {
1448 case GLSL_TYPE_INT:
1449 data.i[c] = op[0]->value.i[c0] | op[1]->value.i[c1];
1450 break;
1451 case GLSL_TYPE_UINT:
1452 data.u[c] = op[0]->value.u[c0] | op[1]->value.u[c1];
1453 break;
1454 default:
1455 assert(0);
1456 }
1457 }
1458 break;
1459
1460 case ir_binop_vector_extract: {
1461 const int c = CLAMP(op[1]->value.i[0], 0,
1462 (int) op[0]->type->vector_elements - 1);
1463
1464 switch (op[0]->type->base_type) {
1465 case GLSL_TYPE_UINT:
1466 data.u[0] = op[0]->value.u[c];
1467 break;
1468 case GLSL_TYPE_INT:
1469 data.i[0] = op[0]->value.i[c];
1470 break;
1471 case GLSL_TYPE_FLOAT:
1472 data.f[0] = op[0]->value.f[c];
1473 break;
1474 case GLSL_TYPE_DOUBLE:
1475 data.d[0] = op[0]->value.d[c];
1476 break;
1477 case GLSL_TYPE_BOOL:
1478 data.b[0] = op[0]->value.b[c];
1479 break;
1480 default:
1481 assert(0);
1482 }
1483 break;
1484 }
1485
1486 case ir_binop_bit_xor:
1487 for (unsigned c = 0, c0 = 0, c1 = 0;
1488 c < components;
1489 c0 += c0_inc, c1 += c1_inc, c++) {
1490
1491 switch (op[0]->type->base_type) {
1492 case GLSL_TYPE_INT:
1493 data.i[c] = op[0]->value.i[c0] ^ op[1]->value.i[c1];
1494 break;
1495 case GLSL_TYPE_UINT:
1496 data.u[c] = op[0]->value.u[c0] ^ op[1]->value.u[c1];
1497 break;
1498 default:
1499 assert(0);
1500 }
1501 }
1502 break;
1503
1504 case ir_unop_bitfield_reverse:
1505 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
1506 for (unsigned c = 0; c < components; c++) {
1507 unsigned int v = op[0]->value.u[c]; // input bits to be reversed
1508 unsigned int r = v; // r will be reversed bits of v; first get LSB of v
1509 int s = sizeof(v) * CHAR_BIT - 1; // extra shift needed at end
1510
1511 for (v >>= 1; v; v >>= 1) {
1512 r <<= 1;
1513 r |= v & 1;
1514 s--;
1515 }
1516 r <<= s; // shift when v's highest bits are zero
1517
1518 data.u[c] = r;
1519 }
1520 break;
1521
1522 case ir_unop_bit_count:
1523 for (unsigned c = 0; c < components; c++) {
1524 unsigned count = 0;
1525 unsigned v = op[0]->value.u[c];
1526
1527 for (; v; count++) {
1528 v &= v - 1;
1529 }
1530 data.u[c] = count;
1531 }
1532 break;
1533
1534 case ir_unop_find_msb:
1535 for (unsigned c = 0; c < components; c++) {
1536 int v = op[0]->value.i[c];
1537
1538 if (v == 0 || (op[0]->type->base_type == GLSL_TYPE_INT && v == -1))
1539 data.i[c] = -1;
1540 else {
1541 int count = 0;
1542 unsigned top_bit = op[0]->type->base_type == GLSL_TYPE_UINT
1543 ? 0 : v & (1u << 31);
1544
1545 while (((v & (1u << 31)) == top_bit) && count != 32) {
1546 count++;
1547 v <<= 1;
1548 }
1549
1550 data.i[c] = 31 - count;
1551 }
1552 }
1553 break;
1554
1555 case ir_unop_find_lsb:
1556 for (unsigned c = 0; c < components; c++) {
1557 if (op[0]->value.i[c] == 0)
1558 data.i[c] = -1;
1559 else {
1560 unsigned pos = 0;
1561 unsigned v = op[0]->value.u[c];
1562
1563 for (; !(v & 1); v >>= 1) {
1564 pos++;
1565 }
1566 data.u[c] = pos;
1567 }
1568 }
1569 break;
1570
1571 case ir_unop_saturate:
1572 for (unsigned c = 0; c < components; c++) {
1573 data.f[c] = CLAMP(op[0]->value.f[c], 0.0f, 1.0f);
1574 }
1575 break;
1576 case ir_unop_pack_double_2x32: {
1577 /* XXX needs to be checked on big-endian */
1578 uint64_t temp;
1579 temp = (uint64_t)op[0]->value.u[0] | ((uint64_t)op[0]->value.u[1] << 32);
1580 data.d[0] = *(double *)&temp;
1581
1582 break;
1583 }
1584 case ir_unop_unpack_double_2x32:
1585 /* XXX needs to be checked on big-endian */
1586 data.u[0] = *(uint32_t *)&op[0]->value.d[0];
1587 data.u[1] = *((uint32_t *)&op[0]->value.d[0] + 1);
1588 break;
1589
1590 case ir_triop_bitfield_extract: {
1591 for (unsigned c = 0; c < components; c++) {
1592 int offset = op[1]->value.i[c];
1593 int bits = op[2]->value.i[c];
1594
1595 if (bits == 0)
1596 data.u[c] = 0;
1597 else if (offset < 0 || bits < 0)
1598 data.u[c] = 0; /* Undefined, per spec. */
1599 else if (offset + bits > 32)
1600 data.u[c] = 0; /* Undefined, per spec. */
1601 else {
1602 if (op[0]->type->base_type == GLSL_TYPE_INT) {
1603 /* int so that the right shift will sign-extend. */
1604 int value = op[0]->value.i[c];
1605 value <<= 32 - bits - offset;
1606 value >>= 32 - bits;
1607 data.i[c] = value;
1608 } else {
1609 unsigned value = op[0]->value.u[c];
1610 value <<= 32 - bits - offset;
1611 value >>= 32 - bits;
1612 data.u[c] = value;
1613 }
1614 }
1615 }
1616 break;
1617 }
1618
1619 case ir_binop_ldexp:
1620 for (unsigned c = 0; c < components; c++) {
1621 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE) {
1622 data.d[c] = ldexp(op[0]->value.d[c], op[1]->value.i[c]);
1623 /* Flush subnormal values to zero. */
1624 if (!isnormal(data.d[c]))
1625 data.d[c] = copysign(0.0, op[0]->value.d[c]);
1626 } else {
1627 data.f[c] = ldexpf(op[0]->value.f[c], op[1]->value.i[c]);
1628 /* Flush subnormal values to zero. */
1629 if (!isnormal(data.f[c]))
1630 data.f[c] = copysignf(0.0f, op[0]->value.f[c]);
1631 }
1632 }
1633 break;
1634
1635 case ir_triop_fma:
1636 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT ||
1637 op[0]->type->base_type == GLSL_TYPE_DOUBLE);
1638 assert(op[1]->type->base_type == GLSL_TYPE_FLOAT ||
1639 op[1]->type->base_type == GLSL_TYPE_DOUBLE);
1640 assert(op[2]->type->base_type == GLSL_TYPE_FLOAT ||
1641 op[2]->type->base_type == GLSL_TYPE_DOUBLE);
1642
1643 for (unsigned c = 0; c < components; c++) {
1644 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
1645 data.d[c] = op[0]->value.d[c] * op[1]->value.d[c]
1646 + op[2]->value.d[c];
1647 else
1648 data.f[c] = op[0]->value.f[c] * op[1]->value.f[c]
1649 + op[2]->value.f[c];
1650 }
1651 break;
1652
1653 case ir_triop_lrp: {
1654 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT ||
1655 op[0]->type->base_type == GLSL_TYPE_DOUBLE);
1656 assert(op[1]->type->base_type == GLSL_TYPE_FLOAT ||
1657 op[1]->type->base_type == GLSL_TYPE_DOUBLE);
1658 assert(op[2]->type->base_type == GLSL_TYPE_FLOAT ||
1659 op[2]->type->base_type == GLSL_TYPE_DOUBLE);
1660
1661 unsigned c2_inc = op[2]->type->is_scalar() ? 0 : 1;
1662 for (unsigned c = 0, c2 = 0; c < components; c2 += c2_inc, c++) {
1663 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
1664 data.d[c] = op[0]->value.d[c] * (1.0 - op[2]->value.d[c2]) +
1665 (op[1]->value.d[c] * op[2]->value.d[c2]);
1666 else
1667 data.f[c] = op[0]->value.f[c] * (1.0f - op[2]->value.f[c2]) +
1668 (op[1]->value.f[c] * op[2]->value.f[c2]);
1669 }
1670 break;
1671 }
1672
1673 case ir_triop_csel:
1674 for (unsigned c = 0; c < components; c++) {
1675 if (op[1]->type->base_type == GLSL_TYPE_DOUBLE)
1676 data.d[c] = op[0]->value.b[c] ? op[1]->value.d[c]
1677 : op[2]->value.d[c];
1678 else
1679 data.u[c] = op[0]->value.b[c] ? op[1]->value.u[c]
1680 : op[2]->value.u[c];
1681 }
1682 break;
1683
1684 case ir_triop_vector_insert: {
1685 const unsigned idx = op[2]->value.u[0];
1686
1687 memcpy(&data, &op[0]->value, sizeof(data));
1688
1689 switch (this->type->base_type) {
1690 case GLSL_TYPE_INT:
1691 data.i[idx] = op[1]->value.i[0];
1692 break;
1693 case GLSL_TYPE_UINT:
1694 data.u[idx] = op[1]->value.u[0];
1695 break;
1696 case GLSL_TYPE_FLOAT:
1697 data.f[idx] = op[1]->value.f[0];
1698 break;
1699 case GLSL_TYPE_BOOL:
1700 data.b[idx] = op[1]->value.b[0];
1701 break;
1702 case GLSL_TYPE_DOUBLE:
1703 data.d[idx] = op[1]->value.d[0];
1704 break;
1705 default:
1706 assert(!"Should not get here.");
1707 break;
1708 }
1709 break;
1710 }
1711
1712 case ir_quadop_bitfield_insert: {
1713 for (unsigned c = 0; c < components; c++) {
1714 int offset = op[2]->value.i[c];
1715 int bits = op[3]->value.i[c];
1716
1717 if (bits == 0)
1718 data.u[c] = op[0]->value.u[c];
1719 else if (offset < 0 || bits < 0)
1720 data.u[c] = 0; /* Undefined, per spec. */
1721 else if (offset + bits > 32)
1722 data.u[c] = 0; /* Undefined, per spec. */
1723 else {
1724 unsigned insert_mask = ((1ull << bits) - 1) << offset;
1725
1726 unsigned insert = op[1]->value.u[c];
1727 insert <<= offset;
1728 insert &= insert_mask;
1729
1730 unsigned base = op[0]->value.u[c];
1731 base &= ~insert_mask;
1732
1733 data.u[c] = base | insert;
1734 }
1735 }
1736 break;
1737 }
1738
1739 case ir_quadop_vector:
1740 for (unsigned c = 0; c < this->type->vector_elements; c++) {
1741 switch (this->type->base_type) {
1742 case GLSL_TYPE_INT:
1743 data.i[c] = op[c]->value.i[0];
1744 break;
1745 case GLSL_TYPE_UINT:
1746 data.u[c] = op[c]->value.u[0];
1747 break;
1748 case GLSL_TYPE_FLOAT:
1749 data.f[c] = op[c]->value.f[0];
1750 break;
1751 case GLSL_TYPE_DOUBLE:
1752 data.d[c] = op[c]->value.d[0];
1753 break;
1754 default:
1755 assert(0);
1756 }
1757 }
1758 break;
1759
1760 default:
1761 /* FINISHME: Should handle all expression types. */
1762 return NULL;
1763 }
1764
1765 return new(ctx) ir_constant(this->type, &data);
1766 }
1767
1768
1769 ir_constant *
1770 ir_texture::constant_expression_value(struct hash_table *)
1771 {
1772 /* texture lookups aren't constant expressions */
1773 return NULL;
1774 }
1775
1776
1777 ir_constant *
1778 ir_swizzle::constant_expression_value(struct hash_table *variable_context)
1779 {
1780 ir_constant *v = this->val->constant_expression_value(variable_context);
1781
1782 if (v != NULL) {
1783 ir_constant_data data = { { 0 } };
1784
1785 const unsigned swiz_idx[4] = {
1786 this->mask.x, this->mask.y, this->mask.z, this->mask.w
1787 };
1788
1789 for (unsigned i = 0; i < this->mask.num_components; i++) {
1790 switch (v->type->base_type) {
1791 case GLSL_TYPE_UINT:
1792 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
1793 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
1794 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
1795 case GLSL_TYPE_DOUBLE:data.d[i] = v->value.d[swiz_idx[i]]; break;
1796 default: assert(!"Should not get here."); break;
1797 }
1798 }
1799
1800 void *ctx = ralloc_parent(this);
1801 return new(ctx) ir_constant(this->type, &data);
1802 }
1803 return NULL;
1804 }
1805
1806
1807 ir_constant *
1808 ir_dereference_variable::constant_expression_value(struct hash_table *variable_context)
1809 {
1810 assert(var);
1811
1812 /* Give priority to the context hashtable, if it exists */
1813 if (variable_context) {
1814 ir_constant *value = (ir_constant *)hash_table_find(variable_context, var);
1815 if(value)
1816 return value;
1817 }
1818
1819 /* The constant_value of a uniform variable is its initializer,
1820 * not the lifetime constant value of the uniform.
1821 */
1822 if (var->data.mode == ir_var_uniform)
1823 return NULL;
1824
1825 if (!var->constant_value)
1826 return NULL;
1827
1828 return var->constant_value->clone(ralloc_parent(var), NULL);
1829 }
1830
1831
1832 ir_constant *
1833 ir_dereference_array::constant_expression_value(struct hash_table *variable_context)
1834 {
1835 ir_constant *array = this->array->constant_expression_value(variable_context);
1836 ir_constant *idx = this->array_index->constant_expression_value(variable_context);
1837
1838 if ((array != NULL) && (idx != NULL)) {
1839 void *ctx = ralloc_parent(this);
1840 if (array->type->is_matrix()) {
1841 /* Array access of a matrix results in a vector.
1842 */
1843 const unsigned column = idx->value.u[0];
1844
1845 const glsl_type *const column_type = array->type->column_type();
1846
1847 /* Offset in the constant matrix to the first element of the column
1848 * to be extracted.
1849 */
1850 const unsigned mat_idx = column * column_type->vector_elements;
1851
1852 ir_constant_data data = { { 0 } };
1853
1854 switch (column_type->base_type) {
1855 case GLSL_TYPE_UINT:
1856 case GLSL_TYPE_INT:
1857 for (unsigned i = 0; i < column_type->vector_elements; i++)
1858 data.u[i] = array->value.u[mat_idx + i];
1859
1860 break;
1861
1862 case GLSL_TYPE_FLOAT:
1863 for (unsigned i = 0; i < column_type->vector_elements; i++)
1864 data.f[i] = array->value.f[mat_idx + i];
1865
1866 break;
1867
1868 case GLSL_TYPE_DOUBLE:
1869 for (unsigned i = 0; i < column_type->vector_elements; i++)
1870 data.d[i] = array->value.d[mat_idx + i];
1871
1872 break;
1873
1874 default:
1875 assert(!"Should not get here.");
1876 break;
1877 }
1878
1879 return new(ctx) ir_constant(column_type, &data);
1880 } else if (array->type->is_vector()) {
1881 const unsigned component = idx->value.u[0];
1882
1883 return new(ctx) ir_constant(array, component);
1884 } else {
1885 const unsigned index = idx->value.u[0];
1886 return array->get_array_element(index)->clone(ctx, NULL);
1887 }
1888 }
1889 return NULL;
1890 }
1891
1892
1893 ir_constant *
1894 ir_dereference_record::constant_expression_value(struct hash_table *)
1895 {
1896 ir_constant *v = this->record->constant_expression_value();
1897
1898 return (v != NULL) ? v->get_record_field(this->field) : NULL;
1899 }
1900
1901
1902 ir_constant *
1903 ir_assignment::constant_expression_value(struct hash_table *)
1904 {
1905 /* FINISHME: Handle CEs involving assignment (return RHS) */
1906 return NULL;
1907 }
1908
1909
1910 ir_constant *
1911 ir_constant::constant_expression_value(struct hash_table *)
1912 {
1913 return this;
1914 }
1915
1916
1917 ir_constant *
1918 ir_call::constant_expression_value(struct hash_table *variable_context)
1919 {
1920 return this->callee->constant_expression_value(&this->actual_parameters, variable_context);
1921 }
1922
1923
1924 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list &body,
1925 struct hash_table *variable_context,
1926 ir_constant **result)
1927 {
1928 foreach_in_list(ir_instruction, inst, &body) {
1929 switch(inst->ir_type) {
1930
1931 /* (declare () type symbol) */
1932 case ir_type_variable: {
1933 ir_variable *var = inst->as_variable();
1934 hash_table_insert(variable_context, ir_constant::zero(this, var->type), var);
1935 break;
1936 }
1937
1938 /* (assign [condition] (write-mask) (ref) (value)) */
1939 case ir_type_assignment: {
1940 ir_assignment *asg = inst->as_assignment();
1941 if (asg->condition) {
1942 ir_constant *cond = asg->condition->constant_expression_value(variable_context);
1943 if (!cond)
1944 return false;
1945 if (!cond->get_bool_component(0))
1946 break;
1947 }
1948
1949 ir_constant *store = NULL;
1950 int offset = 0;
1951
1952 if (!constant_referenced(asg->lhs, variable_context, store, offset))
1953 return false;
1954
1955 ir_constant *value = asg->rhs->constant_expression_value(variable_context);
1956
1957 if (!value)
1958 return false;
1959
1960 store->copy_masked_offset(value, offset, asg->write_mask);
1961 break;
1962 }
1963
1964 /* (return (expression)) */
1965 case ir_type_return:
1966 assert (result);
1967 *result = inst->as_return()->value->constant_expression_value(variable_context);
1968 return *result != NULL;
1969
1970 /* (call name (ref) (params))*/
1971 case ir_type_call: {
1972 ir_call *call = inst->as_call();
1973
1974 /* Just say no to void functions in constant expressions. We
1975 * don't need them at that point.
1976 */
1977
1978 if (!call->return_deref)
1979 return false;
1980
1981 ir_constant *store = NULL;
1982 int offset = 0;
1983
1984 if (!constant_referenced(call->return_deref, variable_context,
1985 store, offset))
1986 return false;
1987
1988 ir_constant *value = call->constant_expression_value(variable_context);
1989
1990 if(!value)
1991 return false;
1992
1993 store->copy_offset(value, offset);
1994 break;
1995 }
1996
1997 /* (if condition (then-instructions) (else-instructions)) */
1998 case ir_type_if: {
1999 ir_if *iif = inst->as_if();
2000
2001 ir_constant *cond = iif->condition->constant_expression_value(variable_context);
2002 if (!cond || !cond->type->is_boolean())
2003 return false;
2004
2005 exec_list &branch = cond->get_bool_component(0) ? iif->then_instructions : iif->else_instructions;
2006
2007 *result = NULL;
2008 if (!constant_expression_evaluate_expression_list(branch, variable_context, result))
2009 return false;
2010
2011 /* If there was a return in the branch chosen, drop out now. */
2012 if (*result)
2013 return true;
2014
2015 break;
2016 }
2017
2018 /* Every other expression type, we drop out. */
2019 default:
2020 return false;
2021 }
2022 }
2023
2024 /* Reaching the end of the block is not an error condition */
2025 if (result)
2026 *result = NULL;
2027
2028 return true;
2029 }
2030
2031 ir_constant *
2032 ir_function_signature::constant_expression_value(exec_list *actual_parameters, struct hash_table *variable_context)
2033 {
2034 const glsl_type *type = this->return_type;
2035 if (type == glsl_type::void_type)
2036 return NULL;
2037
2038 /* From the GLSL 1.20 spec, page 23:
2039 * "Function calls to user-defined functions (non-built-in functions)
2040 * cannot be used to form constant expressions."
2041 */
2042 if (!this->is_builtin())
2043 return NULL;
2044
2045 /*
2046 * Of the builtin functions, only the texture lookups and the noise
2047 * ones must not be used in constant expressions. They all include
2048 * specific opcodes so they don't need to be special-cased at this
2049 * point.
2050 */
2051
2052 /* Initialize the table of dereferencable names with the function
2053 * parameters. Verify their const-ness on the way.
2054 *
2055 * We expect the correctness of the number of parameters to have
2056 * been checked earlier.
2057 */
2058 hash_table *deref_hash = hash_table_ctor(8, hash_table_pointer_hash,
2059 hash_table_pointer_compare);
2060
2061 /* If "origin" is non-NULL, then the function body is there. So we
2062 * have to use the variable objects from the object with the body,
2063 * but the parameter instanciation on the current object.
2064 */
2065 const exec_node *parameter_info = origin ? origin->parameters.head : parameters.head;
2066
2067 foreach_in_list(ir_rvalue, n, actual_parameters) {
2068 ir_constant *constant = n->constant_expression_value(variable_context);
2069 if (constant == NULL) {
2070 hash_table_dtor(deref_hash);
2071 return NULL;
2072 }
2073
2074
2075 ir_variable *var = (ir_variable *)parameter_info;
2076 hash_table_insert(deref_hash, constant, var);
2077
2078 parameter_info = parameter_info->next;
2079 }
2080
2081 ir_constant *result = NULL;
2082
2083 /* Now run the builtin function until something non-constant
2084 * happens or we get the result.
2085 */
2086 if (constant_expression_evaluate_expression_list(origin ? origin->body : body, deref_hash, &result) && result)
2087 result = result->clone(ralloc_parent(this), NULL);
2088
2089 hash_table_dtor(deref_hash);
2090
2091 return result;
2092 }