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
];
74 /* This method is the only one supported by gcc. Unions in particular
75 * are iffy, and read-through-converted-pointer is killed by strict
76 * aliasing. OTOH, the compiler sees through the memcpy, so the
77 * resulting asm is reasonable.
80 bitcast_u2f(unsigned int u
)
82 assert(sizeof(float) == sizeof(unsigned int));
84 memcpy(&f
, &u
, sizeof(f
));
91 assert(sizeof(float) == sizeof(unsigned int));
93 memcpy(&u
, &f
, sizeof(f
));
98 ir_rvalue::constant_expression_value(struct hash_table
*variable_context
)
100 assert(this->type
->is_error());
105 ir_expression::constant_expression_value(struct hash_table
*variable_context
)
107 if (this->type
->is_error())
110 ir_constant
*op
[Elements(this->operands
)] = { NULL
, };
111 ir_constant_data data
;
113 memset(&data
, 0, sizeof(data
));
115 for (unsigned operand
= 0; operand
< this->get_num_operands(); operand
++) {
116 op
[operand
] = this->operands
[operand
]->constant_expression_value(variable_context
);
122 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
||
123 this->operation
== ir_binop_lshift
||
124 this->operation
== ir_binop_rshift
);
126 bool op0_scalar
= op
[0]->type
->is_scalar();
127 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
129 /* When iterating over a vector or matrix's components, we want to increase
130 * the loop counter. However, for scalars, we want to stay at 0.
132 unsigned c0_inc
= op0_scalar
? 0 : 1;
133 unsigned c1_inc
= op1_scalar
? 0 : 1;
135 if (op1_scalar
|| !op
[1]) {
136 components
= op
[0]->type
->components();
138 components
= op
[1]->type
->components();
141 void *ctx
= ralloc_parent(this);
143 /* Handle array operations here, rather than below. */
144 if (op
[0]->type
->is_array()) {
145 assert(op
[1] != NULL
&& op
[1]->type
->is_array());
146 switch (this->operation
) {
147 case ir_binop_all_equal
:
148 return new(ctx
) ir_constant(op
[0]->has_value(op
[1]));
149 case ir_binop_any_nequal
:
150 return new(ctx
) ir_constant(!op
[0]->has_value(op
[1]));
157 switch (this->operation
) {
158 case ir_unop_bit_not
:
159 switch (op
[0]->type
->base_type
) {
161 for (unsigned c
= 0; c
< components
; c
++)
162 data
.i
[c
] = ~ op
[0]->value
.i
[c
];
165 for (unsigned c
= 0; c
< components
; c
++)
166 data
.u
[c
] = ~ op
[0]->value
.u
[c
];
173 case ir_unop_logic_not
:
174 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
175 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
176 data
.b
[c
] = !op
[0]->value
.b
[c
];
180 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
181 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
182 data
.i
[c
] = (int) op
[0]->value
.f
[c
];
186 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
187 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
188 data
.f
[c
] = (float) op
[0]->value
.i
[c
];
192 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
193 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
194 data
.f
[c
] = (float) op
[0]->value
.u
[c
];
198 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
199 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
200 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0F
: 0.0F
;
204 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
205 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
206 data
.b
[c
] = op
[0]->value
.f
[c
] != 0.0F
? true : false;
210 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
211 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
212 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
216 assert(op
[0]->type
->is_integer());
217 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
218 data
.b
[c
] = op
[0]->value
.u
[c
] ? true : false;
222 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
223 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
224 data
.i
[c
] = op
[0]->value
.u
[c
];
228 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
229 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
230 data
.u
[c
] = op
[0]->value
.i
[c
];
233 case ir_unop_bitcast_i2f
:
234 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
235 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
236 data
.f
[c
] = bitcast_u2f(op
[0]->value
.i
[c
]);
239 case ir_unop_bitcast_f2i
:
240 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
241 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
242 data
.i
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
245 case ir_unop_bitcast_u2f
:
246 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
247 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
248 data
.f
[c
] = bitcast_u2f(op
[0]->value
.u
[c
]);
251 case ir_unop_bitcast_f2u
:
252 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
253 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
254 data
.u
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
258 assert(op
[0]->type
->is_boolean());
260 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
261 if (op
[0]->value
.b
[c
])
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
] = truncf(op
[0]->value
.f
[c
]);
273 case ir_unop_round_even
:
274 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
275 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
276 data
.f
[c
] = round_to_even(op
[0]->value
.f
[c
]);
281 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
282 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
283 data
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
288 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
289 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
290 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
295 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
296 switch (this->type
->base_type
) {
303 case GLSL_TYPE_FLOAT
:
304 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
313 case ir_unop_sin_reduced
:
314 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
315 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
316 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
321 case ir_unop_cos_reduced
:
322 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
323 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
324 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
329 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
330 switch (this->type
->base_type
) {
332 data
.u
[c
] = -((int) op
[0]->value
.u
[c
]);
335 data
.i
[c
] = -op
[0]->value
.i
[c
];
337 case GLSL_TYPE_FLOAT
:
338 data
.f
[c
] = -op
[0]->value
.f
[c
];
347 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
348 switch (this->type
->base_type
) {
350 data
.u
[c
] = op
[0]->value
.u
[c
];
353 data
.i
[c
] = op
[0]->value
.i
[c
];
355 data
.i
[c
] = -data
.i
[c
];
357 case GLSL_TYPE_FLOAT
:
358 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
367 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
368 switch (this->type
->base_type
) {
370 data
.u
[c
] = op
[0]->value
.i
[c
] > 0;
373 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
375 case GLSL_TYPE_FLOAT
:
376 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
385 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
386 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
387 switch (this->type
->base_type
) {
389 if (op
[0]->value
.u
[c
] != 0.0)
390 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
393 if (op
[0]->value
.i
[c
] != 0.0)
394 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
396 case GLSL_TYPE_FLOAT
:
397 if (op
[0]->value
.f
[c
] != 0.0)
398 data
.f
[c
] = 1.0F
/ op
[0]->value
.f
[c
];
407 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
408 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
409 data
.f
[c
] = 1.0F
/ sqrtf(op
[0]->value
.f
[c
]);
414 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
415 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
416 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
421 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
422 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
423 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
428 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
429 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
430 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
435 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
436 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
437 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
442 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
443 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
444 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
450 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
451 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
457 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
458 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
459 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
464 data
.f
[0] = dot(op
[0], op
[1]);
468 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
469 for (unsigned c
= 0, c0
= 0, c1
= 0;
471 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
473 switch (op
[0]->type
->base_type
) {
475 data
.u
[c
] = MIN2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
478 data
.i
[c
] = MIN2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
480 case GLSL_TYPE_FLOAT
:
481 data
.f
[c
] = MIN2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
490 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
491 for (unsigned c
= 0, c0
= 0, c1
= 0;
493 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
495 switch (op
[0]->type
->base_type
) {
497 data
.u
[c
] = MAX2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
500 data
.i
[c
] = MAX2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
502 case GLSL_TYPE_FLOAT
:
503 data
.f
[c
] = MAX2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
512 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
513 for (unsigned c
= 0, c0
= 0, c1
= 0;
515 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
517 switch (op
[0]->type
->base_type
) {
519 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
522 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
524 case GLSL_TYPE_FLOAT
:
525 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
534 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
535 for (unsigned c
= 0, c0
= 0, c1
= 0;
537 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
539 switch (op
[0]->type
->base_type
) {
541 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
544 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
546 case GLSL_TYPE_FLOAT
:
547 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
556 /* Check for equal types, or unequal types involving scalars */
557 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
558 || 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 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
568 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
570 case GLSL_TYPE_FLOAT
:
571 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
578 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
580 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
581 * matrix can be a GLSL vector, either N or P can be 1.
583 * For vec*mat, the vector is treated as a row vector. This
584 * means the vector is a 1-row x M-column matrix.
586 * For mat*vec, the vector is treated as a column vector. Since
587 * matrix_columns is 1 for vectors, this just works.
589 const unsigned n
= op
[0]->type
->is_vector()
590 ? 1 : op
[0]->type
->vector_elements
;
591 const unsigned m
= op
[1]->type
->vector_elements
;
592 const unsigned p
= op
[1]->type
->matrix_columns
;
593 for (unsigned j
= 0; j
< p
; j
++) {
594 for (unsigned i
= 0; i
< n
; i
++) {
595 for (unsigned k
= 0; k
< m
; k
++) {
596 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
604 /* FINISHME: Emit warning when division-by-zero is detected. */
605 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
606 for (unsigned c
= 0, c0
= 0, c1
= 0;
608 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
610 switch (op
[0]->type
->base_type
) {
612 if (op
[1]->value
.u
[c1
] == 0) {
615 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
619 if (op
[1]->value
.i
[c1
] == 0) {
622 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
625 case GLSL_TYPE_FLOAT
:
626 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
635 /* FINISHME: Emit warning when division-by-zero is detected. */
636 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
637 for (unsigned c
= 0, c0
= 0, c1
= 0;
639 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
641 switch (op
[0]->type
->base_type
) {
643 if (op
[1]->value
.u
[c1
] == 0) {
646 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
650 if (op
[1]->value
.i
[c1
] == 0) {
653 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
656 case GLSL_TYPE_FLOAT
:
657 /* We don't use fmod because it rounds toward zero; GLSL specifies
660 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
]
661 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
670 case ir_binop_logic_and
:
671 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
672 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
673 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
675 case ir_binop_logic_xor
:
676 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
677 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
678 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
680 case ir_binop_logic_or
:
681 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
682 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
683 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
687 assert(op
[0]->type
== op
[1]->type
);
688 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
689 switch (op
[0]->type
->base_type
) {
691 data
.b
[c
] = op
[0]->value
.u
[c
] < op
[1]->value
.u
[c
];
694 data
.b
[c
] = op
[0]->value
.i
[c
] < op
[1]->value
.i
[c
];
696 case GLSL_TYPE_FLOAT
:
697 data
.b
[c
] = op
[0]->value
.f
[c
] < op
[1]->value
.f
[c
];
704 case ir_binop_greater
:
705 assert(op
[0]->type
== op
[1]->type
);
706 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
707 switch (op
[0]->type
->base_type
) {
709 data
.b
[c
] = op
[0]->value
.u
[c
] > op
[1]->value
.u
[c
];
712 data
.b
[c
] = op
[0]->value
.i
[c
] > op
[1]->value
.i
[c
];
714 case GLSL_TYPE_FLOAT
:
715 data
.b
[c
] = op
[0]->value
.f
[c
] > op
[1]->value
.f
[c
];
722 case ir_binop_lequal
:
723 assert(op
[0]->type
== op
[1]->type
);
724 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
725 switch (op
[0]->type
->base_type
) {
727 data
.b
[c
] = op
[0]->value
.u
[c
] <= op
[1]->value
.u
[c
];
730 data
.b
[c
] = op
[0]->value
.i
[c
] <= op
[1]->value
.i
[c
];
732 case GLSL_TYPE_FLOAT
:
733 data
.b
[c
] = op
[0]->value
.f
[c
] <= op
[1]->value
.f
[c
];
740 case ir_binop_gequal
:
741 assert(op
[0]->type
== op
[1]->type
);
742 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
743 switch (op
[0]->type
->base_type
) {
745 data
.b
[c
] = op
[0]->value
.u
[c
] >= op
[1]->value
.u
[c
];
748 data
.b
[c
] = op
[0]->value
.i
[c
] >= op
[1]->value
.i
[c
];
750 case GLSL_TYPE_FLOAT
:
751 data
.b
[c
] = op
[0]->value
.f
[c
] >= op
[1]->value
.f
[c
];
759 assert(op
[0]->type
== op
[1]->type
);
760 for (unsigned c
= 0; c
< components
; c
++) {
761 switch (op
[0]->type
->base_type
) {
763 data
.b
[c
] = op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
766 data
.b
[c
] = op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
768 case GLSL_TYPE_FLOAT
:
769 data
.b
[c
] = op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
772 data
.b
[c
] = op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
779 case ir_binop_nequal
:
780 assert(op
[0]->type
== op
[1]->type
);
781 for (unsigned c
= 0; c
< components
; c
++) {
782 switch (op
[0]->type
->base_type
) {
784 data
.b
[c
] = op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
787 data
.b
[c
] = op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
789 case GLSL_TYPE_FLOAT
:
790 data
.b
[c
] = op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
793 data
.b
[c
] = op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
800 case ir_binop_all_equal
:
801 data
.b
[0] = op
[0]->has_value(op
[1]);
803 case ir_binop_any_nequal
:
804 data
.b
[0] = !op
[0]->has_value(op
[1]);
807 case ir_binop_lshift
:
808 for (unsigned c
= 0, c0
= 0, c1
= 0;
810 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
812 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
813 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
814 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.i
[c1
];
816 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
817 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
818 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.u
[c1
];
820 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
821 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
822 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.i
[c1
];
824 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
825 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
826 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.u
[c1
];
831 case ir_binop_rshift
:
832 for (unsigned c
= 0, c0
= 0, c1
= 0;
834 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
836 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
837 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
838 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.i
[c1
];
840 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
841 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
842 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.u
[c1
];
844 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
845 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
846 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.i
[c1
];
848 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
849 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
850 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.u
[c1
];
855 case ir_binop_bit_and
:
856 for (unsigned c
= 0, c0
= 0, c1
= 0;
858 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
860 switch (op
[0]->type
->base_type
) {
862 data
.i
[c
] = op
[0]->value
.i
[c0
] & op
[1]->value
.i
[c1
];
865 data
.u
[c
] = op
[0]->value
.u
[c0
] & op
[1]->value
.u
[c1
];
873 case ir_binop_bit_or
:
874 for (unsigned c
= 0, c0
= 0, c1
= 0;
876 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
878 switch (op
[0]->type
->base_type
) {
880 data
.i
[c
] = op
[0]->value
.i
[c0
] | op
[1]->value
.i
[c1
];
883 data
.u
[c
] = op
[0]->value
.u
[c0
] | op
[1]->value
.u
[c1
];
891 case ir_binop_bit_xor
:
892 for (unsigned c
= 0, c0
= 0, c1
= 0;
894 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
896 switch (op
[0]->type
->base_type
) {
898 data
.i
[c
] = op
[0]->value
.i
[c0
] ^ op
[1]->value
.i
[c1
];
901 data
.u
[c
] = op
[0]->value
.u
[c0
] ^ op
[1]->value
.u
[c1
];
909 case ir_quadop_vector
:
910 for (unsigned c
= 0; c
< this->type
->vector_elements
; c
++) {
911 switch (this->type
->base_type
) {
913 data
.i
[c
] = op
[c
]->value
.i
[0];
916 data
.u
[c
] = op
[c
]->value
.u
[0];
918 case GLSL_TYPE_FLOAT
:
919 data
.f
[c
] = op
[c
]->value
.f
[0];
928 /* FINISHME: Should handle all expression types. */
932 return new(ctx
) ir_constant(this->type
, &data
);
937 ir_texture::constant_expression_value(struct hash_table
*variable_context
)
939 /* texture lookups aren't constant expressions */
945 ir_swizzle::constant_expression_value(struct hash_table
*variable_context
)
947 ir_constant
*v
= this->val
->constant_expression_value(variable_context
);
950 ir_constant_data data
= { { 0 } };
952 const unsigned swiz_idx
[4] = {
953 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
956 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
957 switch (v
->type
->base_type
) {
959 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
960 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
961 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
962 default: assert(!"Should not get here."); break;
966 void *ctx
= ralloc_parent(this);
967 return new(ctx
) ir_constant(this->type
, &data
);
974 ir_dereference_variable::constant_referenced(struct hash_table
*variable_context
,
975 ir_constant
*&store
, int &offset
) const
977 if (variable_context
) {
978 store
= (ir_constant
*)hash_table_find(variable_context
, var
);
987 ir_dereference_variable::constant_expression_value(struct hash_table
*variable_context
)
989 /* This may occur during compile and var->type is glsl_type::error_type */
993 /* Give priority to the context hashtable, if it exists */
994 if (variable_context
) {
995 ir_constant
*value
= (ir_constant
*)hash_table_find(variable_context
, var
);
1000 /* The constant_value of a uniform variable is its initializer,
1001 * not the lifetime constant value of the uniform.
1003 if (var
->mode
== ir_var_uniform
)
1006 if (!var
->constant_value
)
1009 return var
->constant_value
->clone(ralloc_parent(var
), NULL
);
1014 ir_dereference_array::constant_referenced(struct hash_table
*variable_context
,
1015 ir_constant
*&store
, int &offset
) const
1017 ir_constant
*index_c
= array_index
->constant_expression_value(variable_context
);
1019 if (!index_c
|| !index_c
->type
->is_scalar() || !index_c
->type
->is_integer()) {
1025 int index
= index_c
->type
->base_type
== GLSL_TYPE_INT
?
1026 index_c
->get_int_component(0) :
1027 index_c
->get_uint_component(0);
1029 ir_constant
*substore
;
1031 const ir_dereference
*deref
= array
->as_dereference();
1038 deref
->constant_referenced(variable_context
, substore
, suboffset
);
1046 const glsl_type
*vt
= substore
->type
;
1047 if (vt
->is_array()) {
1048 store
= substore
->get_array_element(index
);
1052 if (vt
->is_matrix()) {
1054 offset
= index
* vt
->vector_elements
;
1057 if (vt
->is_vector()) {
1059 offset
= suboffset
+ index
;
1068 ir_dereference_array::constant_expression_value(struct hash_table
*variable_context
)
1070 ir_constant
*array
= this->array
->constant_expression_value(variable_context
);
1071 ir_constant
*idx
= this->array_index
->constant_expression_value(variable_context
);
1073 if ((array
!= NULL
) && (idx
!= NULL
)) {
1074 void *ctx
= ralloc_parent(this);
1075 if (array
->type
->is_matrix()) {
1076 /* Array access of a matrix results in a vector.
1078 const unsigned column
= idx
->value
.u
[0];
1080 const glsl_type
*const column_type
= array
->type
->column_type();
1082 /* Offset in the constant matrix to the first element of the column
1085 const unsigned mat_idx
= column
* column_type
->vector_elements
;
1087 ir_constant_data data
= { { 0 } };
1089 switch (column_type
->base_type
) {
1090 case GLSL_TYPE_UINT
:
1092 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1093 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
1097 case GLSL_TYPE_FLOAT
:
1098 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1099 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
1104 assert(!"Should not get here.");
1108 return new(ctx
) ir_constant(column_type
, &data
);
1109 } else if (array
->type
->is_vector()) {
1110 const unsigned component
= idx
->value
.u
[0];
1112 return new(ctx
) ir_constant(array
, component
);
1114 const unsigned index
= idx
->value
.u
[0];
1115 return array
->get_array_element(index
)->clone(ctx
, NULL
);
1123 ir_dereference_record::constant_referenced(struct hash_table
*variable_context
,
1124 ir_constant
*&store
, int &offset
) const
1126 ir_constant
*substore
;
1128 const ir_dereference
*deref
= record
->as_dereference();
1135 deref
->constant_referenced(variable_context
, substore
, suboffset
);
1143 store
= substore
->get_record_field(field
);
1148 ir_dereference_record::constant_expression_value(struct hash_table
*variable_context
)
1150 ir_constant
*v
= this->record
->constant_expression_value();
1152 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
1157 ir_assignment::constant_expression_value(struct hash_table
*variable_context
)
1159 /* FINISHME: Handle CEs involving assignment (return RHS) */
1165 ir_constant::constant_expression_value(struct hash_table
*variable_context
)
1172 ir_call::constant_expression_value(struct hash_table
*variable_context
)
1174 return this->callee
->constant_expression_value(&this->actual_parameters
, variable_context
);
1178 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1179 struct hash_table
*variable_context
,
1180 ir_constant
**result
)
1182 foreach_list(n
, &body
) {
1183 ir_instruction
*inst
= (ir_instruction
*)n
;
1184 switch(inst
->ir_type
) {
1186 /* (declare () type symbol) */
1187 case ir_type_variable
: {
1188 ir_variable
*var
= inst
->as_variable();
1189 hash_table_insert(variable_context
, ir_constant::zero(this, var
->type
), var
);
1193 /* (assign [condition] (write-mask) (ref) (value)) */
1194 case ir_type_assignment
: {
1195 ir_assignment
*asg
= inst
->as_assignment();
1196 if (asg
->condition
) {
1197 ir_constant
*cond
= asg
->condition
->constant_expression_value(variable_context
);
1200 if (!cond
->get_bool_component(0))
1204 ir_constant
*store
= NULL
;
1206 asg
->lhs
->constant_referenced(variable_context
, store
, offset
);
1211 ir_constant
*value
= asg
->rhs
->constant_expression_value(variable_context
);
1216 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
1220 /* (return (expression)) */
1221 case ir_type_return
:
1223 *result
= inst
->as_return()->value
->constant_expression_value(variable_context
);
1224 return *result
!= NULL
;
1226 /* (call name (ref) (params))*/
1227 case ir_type_call
: {
1228 ir_call
*call
= inst
->as_call();
1230 /* Just say no to void functions in constant expressions. We
1231 * don't need them at that point.
1234 if (!call
->return_deref
)
1237 ir_constant
*store
= NULL
;
1239 call
->return_deref
->constant_referenced(variable_context
, store
, offset
);
1244 ir_constant
*value
= call
->constant_expression_value(variable_context
);
1249 store
->copy_offset(value
, offset
);
1253 /* (if condition (then-instructions) (else-instructions)) */
1255 ir_if
*iif
= inst
->as_if();
1257 ir_constant
*cond
= iif
->condition
->constant_expression_value(variable_context
);
1258 if (!cond
|| !cond
->type
->is_boolean())
1261 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
1264 if (!constant_expression_evaluate_expression_list(branch
, variable_context
, result
))
1267 /* If there was a return in the branch chosen, drop out now. */
1274 /* Every other expression type, we drop out. */
1280 /* Reaching the end of the block is not an error condition */
1288 ir_function_signature::constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
)
1290 const glsl_type
*type
= this->return_type
;
1291 if (type
== glsl_type::void_type
)
1294 /* From the GLSL 1.20 spec, page 23:
1295 * "Function calls to user-defined functions (non-built-in functions)
1296 * cannot be used to form constant expressions."
1298 if (!this->is_builtin
)
1302 * Of the builtin functions, only the texture lookups and the noise
1303 * ones must not be used in constant expressions. They all include
1304 * specific opcodes so they don't need to be special-cased at this
1308 /* Initialize the table of dereferencable names with the function
1309 * parameters. Verify their const-ness on the way.
1311 * We expect the correctness of the number of parameters to have
1312 * been checked earlier.
1314 hash_table
*deref_hash
= hash_table_ctor(8, hash_table_pointer_hash
,
1315 hash_table_pointer_compare
);
1317 /* If "origin" is non-NULL, then the function body is there. So we
1318 * have to use the variable objects from the object with the body,
1319 * but the parameter instanciation on the current object.
1321 const exec_node
*parameter_info
= origin
? origin
->parameters
.head
: parameters
.head
;
1323 foreach_list(n
, actual_parameters
) {
1324 ir_constant
*constant
= ((ir_rvalue
*) n
)->constant_expression_value(variable_context
);
1325 if (constant
== NULL
) {
1326 hash_table_dtor(deref_hash
);
1331 ir_variable
*var
= (ir_variable
*)parameter_info
;
1332 hash_table_insert(deref_hash
, constant
, var
);
1334 parameter_info
= parameter_info
->next
;
1337 ir_constant
*result
= NULL
;
1339 /* Now run the builtin function until something non-constant
1340 * happens or we get the result.
1342 if (constant_expression_evaluate_expression_list(origin
? origin
->body
: body
, deref_hash
, &result
) && result
)
1343 result
= result
->clone(ralloc_parent(this), NULL
);
1345 hash_table_dtor(deref_hash
);