2 * Copyright © 2010 Intel Corporation
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:
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
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.
25 * \file ir_constant_expression.cpp
26 * Evaluate and process constant valued expressions
28 * In GLSL, constant valued expressions are used in several places. These
29 * must be processed and evaluated very early in the compilation process.
32 * * Initializers for uniforms
33 * * Initializers for \c const variables
37 #include "main/core.h" /* for MAX2, MIN2, CLAMP */
39 #include "ir_visitor.h"
40 #include "glsl_types.h"
41 #include "program/hash_table.h"
43 /* Using C99 rounding functions for roundToEven() implementation is
44 * difficult, because round(), rint, and nearbyint() are affected by
45 * fesetenv(), which the application may have done for its own
46 * purposes. Mesa's IROUND macro is close to what we want, but it
47 * rounds away from 0 on n + 0.5.
50 round_to_even(float val
)
52 int rounded
= IROUND(val
);
54 if (val
- floor(val
) == 0.5) {
56 rounded
+= val
> 0 ? -1 : 1;
63 dot(ir_constant
*op0
, ir_constant
*op1
)
65 assert(op0
->type
->is_float() && op1
->type
->is_float());
68 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
69 result
+= op0
->value
.f
[c
] * op1
->value
.f
[c
];
75 ir_rvalue::constant_expression_value(struct hash_table
*variable_context
)
77 assert(this->type
->is_error());
82 ir_expression::constant_expression_value(struct hash_table
*variable_context
)
84 if (this->type
->is_error())
87 ir_constant
*op
[Elements(this->operands
)] = { NULL
, };
88 ir_constant_data data
;
90 memset(&data
, 0, sizeof(data
));
92 for (unsigned operand
= 0; operand
< this->get_num_operands(); operand
++) {
93 op
[operand
] = this->operands
[operand
]->constant_expression_value(variable_context
);
99 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
||
100 this->operation
== ir_binop_lshift
||
101 this->operation
== ir_binop_rshift
);
103 bool op0_scalar
= op
[0]->type
->is_scalar();
104 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
106 /* When iterating over a vector or matrix's components, we want to increase
107 * the loop counter. However, for scalars, we want to stay at 0.
109 unsigned c0_inc
= op0_scalar
? 0 : 1;
110 unsigned c1_inc
= op1_scalar
? 0 : 1;
112 if (op1_scalar
|| !op
[1]) {
113 components
= op
[0]->type
->components();
115 components
= op
[1]->type
->components();
118 void *ctx
= ralloc_parent(this);
120 /* Handle array operations here, rather than below. */
121 if (op
[0]->type
->is_array()) {
122 assert(op
[1] != NULL
&& op
[1]->type
->is_array());
123 switch (this->operation
) {
124 case ir_binop_all_equal
:
125 return new(ctx
) ir_constant(op
[0]->has_value(op
[1]));
126 case ir_binop_any_nequal
:
127 return new(ctx
) ir_constant(!op
[0]->has_value(op
[1]));
134 switch (this->operation
) {
135 case ir_unop_bit_not
:
136 switch (op
[0]->type
->base_type
) {
138 for (unsigned c
= 0; c
< components
; c
++)
139 data
.i
[c
] = ~ op
[0]->value
.i
[c
];
142 for (unsigned c
= 0; c
< components
; c
++)
143 data
.u
[c
] = ~ op
[0]->value
.u
[c
];
150 case ir_unop_logic_not
:
151 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
152 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
153 data
.b
[c
] = !op
[0]->value
.b
[c
];
157 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
158 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
159 data
.i
[c
] = (int) op
[0]->value
.f
[c
];
163 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
164 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
165 data
.f
[c
] = (float) op
[0]->value
.i
[c
];
169 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
170 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
171 data
.f
[c
] = (float) op
[0]->value
.u
[c
];
175 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
176 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
177 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0F
: 0.0F
;
181 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
182 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
183 data
.b
[c
] = op
[0]->value
.f
[c
] != 0.0F
? true : false;
187 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
188 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
189 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
193 assert(op
[0]->type
->is_integer());
194 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
195 data
.b
[c
] = op
[0]->value
.u
[c
] ? true : false;
199 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
200 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
201 data
.i
[c
] = op
[0]->value
.u
[c
];
205 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
206 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
207 data
.u
[c
] = op
[0]->value
.i
[c
];
211 assert(op
[0]->type
->is_boolean());
213 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
214 if (op
[0]->value
.b
[c
])
220 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
221 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
222 data
.f
[c
] = truncf(op
[0]->value
.f
[c
]);
226 case ir_unop_round_even
:
227 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
228 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
229 data
.f
[c
] = round_to_even(op
[0]->value
.f
[c
]);
234 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
235 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
236 data
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
241 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
242 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
243 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
248 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
249 switch (this->type
->base_type
) {
256 case GLSL_TYPE_FLOAT
:
257 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
266 case ir_unop_sin_reduced
:
267 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
268 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
269 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
274 case ir_unop_cos_reduced
:
275 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
276 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
277 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
282 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
283 switch (this->type
->base_type
) {
285 data
.u
[c
] = -((int) op
[0]->value
.u
[c
]);
288 data
.i
[c
] = -op
[0]->value
.i
[c
];
290 case GLSL_TYPE_FLOAT
:
291 data
.f
[c
] = -op
[0]->value
.f
[c
];
300 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
301 switch (this->type
->base_type
) {
303 data
.u
[c
] = op
[0]->value
.u
[c
];
306 data
.i
[c
] = op
[0]->value
.i
[c
];
308 data
.i
[c
] = -data
.i
[c
];
310 case GLSL_TYPE_FLOAT
:
311 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
320 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
321 switch (this->type
->base_type
) {
323 data
.u
[c
] = op
[0]->value
.i
[c
] > 0;
326 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
328 case GLSL_TYPE_FLOAT
:
329 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
338 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
339 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
340 switch (this->type
->base_type
) {
342 if (op
[0]->value
.u
[c
] != 0.0)
343 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
346 if (op
[0]->value
.i
[c
] != 0.0)
347 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
349 case GLSL_TYPE_FLOAT
:
350 if (op
[0]->value
.f
[c
] != 0.0)
351 data
.f
[c
] = 1.0F
/ op
[0]->value
.f
[c
];
360 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
361 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
362 data
.f
[c
] = 1.0F
/ sqrtf(op
[0]->value
.f
[c
]);
367 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
368 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
369 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
374 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
375 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
376 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
381 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
382 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
383 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
388 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
389 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
390 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
395 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
396 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
397 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
403 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
404 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
410 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
411 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
412 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
417 data
.f
[0] = dot(op
[0], op
[1]);
421 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
422 for (unsigned c
= 0, c0
= 0, c1
= 0;
424 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
426 switch (op
[0]->type
->base_type
) {
428 data
.u
[c
] = MIN2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
431 data
.i
[c
] = MIN2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
433 case GLSL_TYPE_FLOAT
:
434 data
.f
[c
] = MIN2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
443 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
444 for (unsigned c
= 0, c0
= 0, c1
= 0;
446 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
448 switch (op
[0]->type
->base_type
) {
450 data
.u
[c
] = MAX2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
453 data
.i
[c
] = MAX2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
455 case GLSL_TYPE_FLOAT
:
456 data
.f
[c
] = MAX2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
465 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
466 for (unsigned c
= 0, c0
= 0, c1
= 0;
468 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
470 switch (op
[0]->type
->base_type
) {
472 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
475 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
477 case GLSL_TYPE_FLOAT
:
478 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
487 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
488 for (unsigned c
= 0, c0
= 0, c1
= 0;
490 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
492 switch (op
[0]->type
->base_type
) {
494 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
497 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
499 case GLSL_TYPE_FLOAT
:
500 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
509 /* Check for equal types, or unequal types involving scalars */
510 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
511 || op0_scalar
|| op1_scalar
) {
512 for (unsigned c
= 0, c0
= 0, c1
= 0;
514 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
516 switch (op
[0]->type
->base_type
) {
518 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
521 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
523 case GLSL_TYPE_FLOAT
:
524 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
531 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
533 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
534 * matrix can be a GLSL vector, either N or P can be 1.
536 * For vec*mat, the vector is treated as a row vector. This
537 * means the vector is a 1-row x M-column matrix.
539 * For mat*vec, the vector is treated as a column vector. Since
540 * matrix_columns is 1 for vectors, this just works.
542 const unsigned n
= op
[0]->type
->is_vector()
543 ? 1 : op
[0]->type
->vector_elements
;
544 const unsigned m
= op
[1]->type
->vector_elements
;
545 const unsigned p
= op
[1]->type
->matrix_columns
;
546 for (unsigned j
= 0; j
< p
; j
++) {
547 for (unsigned i
= 0; i
< n
; i
++) {
548 for (unsigned k
= 0; k
< m
; k
++) {
549 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
557 /* FINISHME: Emit warning when division-by-zero is detected. */
558 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
559 for (unsigned c
= 0, c0
= 0, c1
= 0;
561 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
563 switch (op
[0]->type
->base_type
) {
565 if (op
[1]->value
.u
[c1
] == 0) {
568 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
572 if (op
[1]->value
.i
[c1
] == 0) {
575 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
578 case GLSL_TYPE_FLOAT
:
579 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
588 /* FINISHME: Emit warning when division-by-zero is detected. */
589 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
590 for (unsigned c
= 0, c0
= 0, c1
= 0;
592 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
594 switch (op
[0]->type
->base_type
) {
596 if (op
[1]->value
.u
[c1
] == 0) {
599 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
603 if (op
[1]->value
.i
[c1
] == 0) {
606 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
609 case GLSL_TYPE_FLOAT
:
610 /* We don't use fmod because it rounds toward zero; GLSL specifies
613 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
]
614 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
623 case ir_binop_logic_and
:
624 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
625 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
626 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
628 case ir_binop_logic_xor
:
629 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
630 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
631 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
633 case ir_binop_logic_or
:
634 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
635 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
636 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
640 assert(op
[0]->type
== op
[1]->type
);
641 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
642 switch (op
[0]->type
->base_type
) {
644 data
.b
[c
] = op
[0]->value
.u
[c
] < op
[1]->value
.u
[c
];
647 data
.b
[c
] = op
[0]->value
.i
[c
] < op
[1]->value
.i
[c
];
649 case GLSL_TYPE_FLOAT
:
650 data
.b
[c
] = op
[0]->value
.f
[c
] < op
[1]->value
.f
[c
];
657 case ir_binop_greater
:
658 assert(op
[0]->type
== op
[1]->type
);
659 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
660 switch (op
[0]->type
->base_type
) {
662 data
.b
[c
] = op
[0]->value
.u
[c
] > op
[1]->value
.u
[c
];
665 data
.b
[c
] = op
[0]->value
.i
[c
] > op
[1]->value
.i
[c
];
667 case GLSL_TYPE_FLOAT
:
668 data
.b
[c
] = op
[0]->value
.f
[c
] > op
[1]->value
.f
[c
];
675 case ir_binop_lequal
:
676 assert(op
[0]->type
== op
[1]->type
);
677 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
678 switch (op
[0]->type
->base_type
) {
680 data
.b
[c
] = op
[0]->value
.u
[c
] <= op
[1]->value
.u
[c
];
683 data
.b
[c
] = op
[0]->value
.i
[c
] <= op
[1]->value
.i
[c
];
685 case GLSL_TYPE_FLOAT
:
686 data
.b
[c
] = op
[0]->value
.f
[c
] <= op
[1]->value
.f
[c
];
693 case ir_binop_gequal
:
694 assert(op
[0]->type
== op
[1]->type
);
695 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
696 switch (op
[0]->type
->base_type
) {
698 data
.b
[c
] = op
[0]->value
.u
[c
] >= op
[1]->value
.u
[c
];
701 data
.b
[c
] = op
[0]->value
.i
[c
] >= op
[1]->value
.i
[c
];
703 case GLSL_TYPE_FLOAT
:
704 data
.b
[c
] = op
[0]->value
.f
[c
] >= op
[1]->value
.f
[c
];
712 assert(op
[0]->type
== op
[1]->type
);
713 for (unsigned c
= 0; c
< components
; c
++) {
714 switch (op
[0]->type
->base_type
) {
716 data
.b
[c
] = op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
719 data
.b
[c
] = op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
721 case GLSL_TYPE_FLOAT
:
722 data
.b
[c
] = op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
725 data
.b
[c
] = op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
732 case ir_binop_nequal
:
733 assert(op
[0]->type
== op
[1]->type
);
734 for (unsigned c
= 0; c
< components
; c
++) {
735 switch (op
[0]->type
->base_type
) {
737 data
.b
[c
] = op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
740 data
.b
[c
] = op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
742 case GLSL_TYPE_FLOAT
:
743 data
.b
[c
] = op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
746 data
.b
[c
] = op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
753 case ir_binop_all_equal
:
754 data
.b
[0] = op
[0]->has_value(op
[1]);
756 case ir_binop_any_nequal
:
757 data
.b
[0] = !op
[0]->has_value(op
[1]);
760 case ir_binop_lshift
:
761 for (unsigned c
= 0, c0
= 0, c1
= 0;
763 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
765 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
766 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
767 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.i
[c1
];
769 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
770 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
771 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.u
[c1
];
773 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
774 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
775 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.i
[c1
];
777 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
778 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
779 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.u
[c1
];
784 case ir_binop_rshift
:
785 for (unsigned c
= 0, c0
= 0, c1
= 0;
787 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
789 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
790 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
791 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.i
[c1
];
793 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
794 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
795 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.u
[c1
];
797 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
798 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
799 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.i
[c1
];
801 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
802 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
803 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.u
[c1
];
808 case ir_binop_bit_and
:
809 for (unsigned c
= 0, c0
= 0, c1
= 0;
811 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
813 switch (op
[0]->type
->base_type
) {
815 data
.i
[c
] = op
[0]->value
.i
[c0
] & op
[1]->value
.i
[c1
];
818 data
.u
[c
] = op
[0]->value
.u
[c0
] & op
[1]->value
.u
[c1
];
826 case ir_binop_bit_or
:
827 for (unsigned c
= 0, c0
= 0, c1
= 0;
829 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
831 switch (op
[0]->type
->base_type
) {
833 data
.i
[c
] = op
[0]->value
.i
[c0
] | op
[1]->value
.i
[c1
];
836 data
.u
[c
] = op
[0]->value
.u
[c0
] | op
[1]->value
.u
[c1
];
844 case ir_binop_bit_xor
:
845 for (unsigned c
= 0, c0
= 0, c1
= 0;
847 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
849 switch (op
[0]->type
->base_type
) {
851 data
.i
[c
] = op
[0]->value
.i
[c0
] ^ op
[1]->value
.i
[c1
];
854 data
.u
[c
] = op
[0]->value
.u
[c0
] ^ op
[1]->value
.u
[c1
];
862 case ir_quadop_vector
:
863 for (unsigned c
= 0; c
< this->type
->vector_elements
; c
++) {
864 switch (this->type
->base_type
) {
866 data
.i
[c
] = op
[c
]->value
.i
[0];
869 data
.u
[c
] = op
[c
]->value
.u
[0];
871 case GLSL_TYPE_FLOAT
:
872 data
.f
[c
] = op
[c
]->value
.f
[0];
881 /* FINISHME: Should handle all expression types. */
885 return new(ctx
) ir_constant(this->type
, &data
);
890 ir_texture::constant_expression_value(struct hash_table
*variable_context
)
892 /* texture lookups aren't constant expressions */
898 ir_swizzle::constant_expression_value(struct hash_table
*variable_context
)
900 ir_constant
*v
= this->val
->constant_expression_value(variable_context
);
903 ir_constant_data data
= { { 0 } };
905 const unsigned swiz_idx
[4] = {
906 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
909 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
910 switch (v
->type
->base_type
) {
912 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
913 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
914 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
915 default: assert(!"Should not get here."); break;
919 void *ctx
= ralloc_parent(this);
920 return new(ctx
) ir_constant(this->type
, &data
);
927 ir_dereference_variable::constant_referenced(struct hash_table
*variable_context
,
928 ir_constant
*&store
, int &offset
) const
930 if (variable_context
) {
931 store
= (ir_constant
*)hash_table_find(variable_context
, var
);
940 ir_dereference_variable::constant_expression_value(struct hash_table
*variable_context
)
942 /* This may occur during compile and var->type is glsl_type::error_type */
946 /* Give priority to the context hashtable, if it exists */
947 if (variable_context
) {
948 ir_constant
*value
= (ir_constant
*)hash_table_find(variable_context
, var
);
953 /* The constant_value of a uniform variable is its initializer,
954 * not the lifetime constant value of the uniform.
956 if (var
->mode
== ir_var_uniform
)
959 if (!var
->constant_value
)
962 return var
->constant_value
->clone(ralloc_parent(var
), NULL
);
967 ir_dereference_array::constant_referenced(struct hash_table
*variable_context
,
968 ir_constant
*&store
, int &offset
) const
970 ir_constant
*index_c
= array_index
->constant_expression_value(variable_context
);
972 if (!index_c
|| !index_c
->type
->is_scalar() || !index_c
->type
->is_integer()) {
978 int index
= index_c
->type
->base_type
== GLSL_TYPE_INT
?
979 index_c
->get_int_component(0) :
980 index_c
->get_uint_component(0);
982 ir_constant
*substore
;
984 const ir_dereference
*deref
= array
->as_dereference();
991 deref
->constant_referenced(variable_context
, substore
, suboffset
);
999 const glsl_type
*vt
= substore
->type
;
1000 if (vt
->is_array()) {
1001 store
= substore
->get_array_element(index
);
1005 if (vt
->is_matrix()) {
1007 offset
= index
* vt
->vector_elements
;
1010 if (vt
->is_vector()) {
1012 offset
= suboffset
+ index
;
1021 ir_dereference_array::constant_expression_value(struct hash_table
*variable_context
)
1023 ir_constant
*array
= this->array
->constant_expression_value(variable_context
);
1024 ir_constant
*idx
= this->array_index
->constant_expression_value(variable_context
);
1026 if ((array
!= NULL
) && (idx
!= NULL
)) {
1027 void *ctx
= ralloc_parent(this);
1028 if (array
->type
->is_matrix()) {
1029 /* Array access of a matrix results in a vector.
1031 const unsigned column
= idx
->value
.u
[0];
1033 const glsl_type
*const column_type
= array
->type
->column_type();
1035 /* Offset in the constant matrix to the first element of the column
1038 const unsigned mat_idx
= column
* column_type
->vector_elements
;
1040 ir_constant_data data
= { { 0 } };
1042 switch (column_type
->base_type
) {
1043 case GLSL_TYPE_UINT
:
1045 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1046 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
1050 case GLSL_TYPE_FLOAT
:
1051 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1052 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
1057 assert(!"Should not get here.");
1061 return new(ctx
) ir_constant(column_type
, &data
);
1062 } else if (array
->type
->is_vector()) {
1063 const unsigned component
= idx
->value
.u
[0];
1065 return new(ctx
) ir_constant(array
, component
);
1067 const unsigned index
= idx
->value
.u
[0];
1068 return array
->get_array_element(index
)->clone(ctx
, NULL
);
1076 ir_dereference_record::constant_referenced(struct hash_table
*variable_context
,
1077 ir_constant
*&store
, int &offset
) const
1079 ir_constant
*substore
;
1081 const ir_dereference
*deref
= record
->as_dereference();
1088 deref
->constant_referenced(variable_context
, substore
, suboffset
);
1096 store
= substore
->get_record_field(field
);
1101 ir_dereference_record::constant_expression_value(struct hash_table
*variable_context
)
1103 ir_constant
*v
= this->record
->constant_expression_value();
1105 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
1110 ir_assignment::constant_expression_value(struct hash_table
*variable_context
)
1112 /* FINISHME: Handle CEs involving assignment (return RHS) */
1118 ir_constant::constant_expression_value(struct hash_table
*variable_context
)
1125 ir_call::constant_expression_value(struct hash_table
*variable_context
)
1127 return this->callee
->constant_expression_value(&this->actual_parameters
, variable_context
);
1131 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1132 struct hash_table
*variable_context
,
1133 ir_constant
**result
)
1135 foreach_list(n
, &body
) {
1136 ir_instruction
*inst
= (ir_instruction
*)n
;
1137 switch(inst
->ir_type
) {
1139 /* (declare () type symbol) */
1140 case ir_type_variable
: {
1141 ir_variable
*var
= inst
->as_variable();
1142 hash_table_insert(variable_context
, ir_constant::zero(this, var
->type
), var
);
1146 /* (assign [condition] (write-mask) (ref) (value)) */
1147 case ir_type_assignment
: {
1148 ir_assignment
*asg
= inst
->as_assignment();
1149 if (asg
->condition
) {
1150 ir_constant
*cond
= asg
->condition
->constant_expression_value(variable_context
);
1153 if (!cond
->get_bool_component(0))
1157 ir_constant
*store
= NULL
;
1159 asg
->lhs
->constant_referenced(variable_context
, store
, offset
);
1164 ir_constant
*value
= asg
->rhs
->constant_expression_value(variable_context
);
1169 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
1173 /* (return (expression)) */
1174 case ir_type_return
:
1176 *result
= inst
->as_return()->value
->constant_expression_value(variable_context
);
1177 return *result
!= NULL
;
1179 /* (call name (ref) (params))*/
1180 case ir_type_call
: {
1181 ir_call
*call
= inst
->as_call();
1183 /* Just say no to void functions in constant expressions. We
1184 * don't need them at that point.
1187 if (!call
->return_deref
)
1190 ir_constant
*store
= NULL
;
1192 call
->return_deref
->constant_referenced(variable_context
, store
, offset
);
1197 ir_constant
*value
= call
->constant_expression_value(variable_context
);
1202 store
->copy_offset(value
, offset
);
1206 /* (if condition (then-instructions) (else-instructions)) */
1208 ir_if
*iif
= inst
->as_if();
1210 ir_constant
*cond
= iif
->condition
->constant_expression_value(variable_context
);
1211 if (!cond
|| !cond
->type
->is_boolean())
1214 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
1217 if (!constant_expression_evaluate_expression_list(branch
, variable_context
, result
))
1220 /* If there was a return in the branch chosen, drop out now. */
1227 /* Every other expression type, we drop out. */
1233 /* Reaching the end of the block is not an error condition */
1241 ir_function_signature::constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
)
1243 const glsl_type
*type
= this->return_type
;
1244 if (type
== glsl_type::void_type
)
1247 /* From the GLSL 1.20 spec, page 23:
1248 * "Function calls to user-defined functions (non-built-in functions)
1249 * cannot be used to form constant expressions."
1251 if (!this->is_builtin
)
1255 * Of the builtin functions, only the texture lookups and the noise
1256 * ones must not be used in constant expressions. They all include
1257 * specific opcodes so they don't need to be special-cased at this
1261 /* Initialize the table of dereferencable names with the function
1262 * parameters. Verify their const-ness on the way.
1264 * We expect the correctness of the number of parameters to have
1265 * been checked earlier.
1267 hash_table
*deref_hash
= hash_table_ctor(8, hash_table_pointer_hash
,
1268 hash_table_pointer_compare
);
1270 /* If "origin" is non-NULL, then the function body is there. So we
1271 * have to use the variable objects from the object with the body,
1272 * but the parameter instanciation on the current object.
1274 const exec_node
*parameter_info
= origin
? origin
->parameters
.head
: parameters
.head
;
1276 foreach_list(n
, actual_parameters
) {
1277 ir_constant
*constant
= ((ir_rvalue
*) n
)->constant_expression_value(variable_context
);
1278 if (constant
== NULL
)
1281 ir_variable
*var
= (ir_variable
*)parameter_info
;
1282 hash_table_insert(deref_hash
, constant
, var
);
1284 parameter_info
= parameter_info
->next
;
1287 ir_constant
*result
= NULL
;
1289 /* Now run the builtin function until something non-constant
1290 * happens or we get the result.
1292 if (constant_expression_evaluate_expression_list(origin
? origin
->body
: body
, deref_hash
, &result
) && result
)
1293 result
= result
->clone(ralloc_parent(this), NULL
);
1295 hash_table_dtor(deref_hash
);