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_FLOAT
);
187 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
188 data
.i
[c
] = (unsigned) op
[0]->value
.f
[c
];
192 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
193 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
194 data
.f
[c
] = (float) op
[0]->value
.i
[c
];
198 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
199 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
200 data
.f
[c
] = (float) op
[0]->value
.u
[c
];
204 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
205 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
206 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0F
: 0.0F
;
210 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
211 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
212 data
.b
[c
] = op
[0]->value
.f
[c
] != 0.0F
? true : false;
216 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
217 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
218 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
222 assert(op
[0]->type
->is_integer());
223 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
224 data
.b
[c
] = op
[0]->value
.u
[c
] ? true : false;
228 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
229 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
230 data
.i
[c
] = op
[0]->value
.u
[c
];
234 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
235 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
236 data
.u
[c
] = op
[0]->value
.i
[c
];
239 case ir_unop_bitcast_i2f
:
240 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
241 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
242 data
.f
[c
] = bitcast_u2f(op
[0]->value
.i
[c
]);
245 case ir_unop_bitcast_f2i
:
246 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
247 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
248 data
.i
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
251 case ir_unop_bitcast_u2f
:
252 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
253 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
254 data
.f
[c
] = bitcast_u2f(op
[0]->value
.u
[c
]);
257 case ir_unop_bitcast_f2u
:
258 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
259 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
260 data
.u
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
264 assert(op
[0]->type
->is_boolean());
266 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
267 if (op
[0]->value
.b
[c
])
273 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
274 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
275 data
.f
[c
] = truncf(op
[0]->value
.f
[c
]);
279 case ir_unop_round_even
:
280 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
281 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
282 data
.f
[c
] = round_to_even(op
[0]->value
.f
[c
]);
287 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
288 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
289 data
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
294 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
295 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
296 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
301 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
302 switch (this->type
->base_type
) {
309 case GLSL_TYPE_FLOAT
:
310 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
319 case ir_unop_sin_reduced
:
320 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
321 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
322 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
327 case ir_unop_cos_reduced
:
328 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
329 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
330 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
335 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
336 switch (this->type
->base_type
) {
338 data
.u
[c
] = -((int) op
[0]->value
.u
[c
]);
341 data
.i
[c
] = -op
[0]->value
.i
[c
];
343 case GLSL_TYPE_FLOAT
:
344 data
.f
[c
] = -op
[0]->value
.f
[c
];
353 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
354 switch (this->type
->base_type
) {
356 data
.u
[c
] = op
[0]->value
.u
[c
];
359 data
.i
[c
] = op
[0]->value
.i
[c
];
361 data
.i
[c
] = -data
.i
[c
];
363 case GLSL_TYPE_FLOAT
:
364 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
373 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
374 switch (this->type
->base_type
) {
376 data
.u
[c
] = op
[0]->value
.i
[c
] > 0;
379 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
381 case GLSL_TYPE_FLOAT
:
382 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
391 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
392 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
393 switch (this->type
->base_type
) {
395 if (op
[0]->value
.u
[c
] != 0.0)
396 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
399 if (op
[0]->value
.i
[c
] != 0.0)
400 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
402 case GLSL_TYPE_FLOAT
:
403 if (op
[0]->value
.f
[c
] != 0.0)
404 data
.f
[c
] = 1.0F
/ op
[0]->value
.f
[c
];
413 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
414 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
415 data
.f
[c
] = 1.0F
/ sqrtf(op
[0]->value
.f
[c
]);
420 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
421 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
422 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
427 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
428 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
429 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
434 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
435 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
436 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
441 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
442 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
443 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
448 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
449 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
450 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
456 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
457 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
463 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
464 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
465 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
470 data
.f
[0] = dot(op
[0], op
[1]);
474 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
475 for (unsigned c
= 0, c0
= 0, c1
= 0;
477 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
479 switch (op
[0]->type
->base_type
) {
481 data
.u
[c
] = MIN2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
484 data
.i
[c
] = MIN2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
486 case GLSL_TYPE_FLOAT
:
487 data
.f
[c
] = MIN2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
496 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
497 for (unsigned c
= 0, c0
= 0, c1
= 0;
499 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
501 switch (op
[0]->type
->base_type
) {
503 data
.u
[c
] = MAX2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
506 data
.i
[c
] = MAX2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
508 case GLSL_TYPE_FLOAT
:
509 data
.f
[c
] = MAX2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
518 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
519 for (unsigned c
= 0, c0
= 0, c1
= 0;
521 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
523 switch (op
[0]->type
->base_type
) {
525 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
528 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
530 case GLSL_TYPE_FLOAT
:
531 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
540 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
541 for (unsigned c
= 0, c0
= 0, c1
= 0;
543 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
545 switch (op
[0]->type
->base_type
) {
547 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
550 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
552 case GLSL_TYPE_FLOAT
:
553 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
562 /* Check for equal types, or unequal types involving scalars */
563 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
564 || op0_scalar
|| op1_scalar
) {
565 for (unsigned c
= 0, c0
= 0, c1
= 0;
567 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
569 switch (op
[0]->type
->base_type
) {
571 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
574 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
576 case GLSL_TYPE_FLOAT
:
577 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
584 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
586 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
587 * matrix can be a GLSL vector, either N or P can be 1.
589 * For vec*mat, the vector is treated as a row vector. This
590 * means the vector is a 1-row x M-column matrix.
592 * For mat*vec, the vector is treated as a column vector. Since
593 * matrix_columns is 1 for vectors, this just works.
595 const unsigned n
= op
[0]->type
->is_vector()
596 ? 1 : op
[0]->type
->vector_elements
;
597 const unsigned m
= op
[1]->type
->vector_elements
;
598 const unsigned p
= op
[1]->type
->matrix_columns
;
599 for (unsigned j
= 0; j
< p
; j
++) {
600 for (unsigned i
= 0; i
< n
; i
++) {
601 for (unsigned k
= 0; k
< m
; k
++) {
602 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
610 /* FINISHME: Emit warning when division-by-zero is detected. */
611 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
612 for (unsigned c
= 0, c0
= 0, c1
= 0;
614 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
616 switch (op
[0]->type
->base_type
) {
618 if (op
[1]->value
.u
[c1
] == 0) {
621 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
625 if (op
[1]->value
.i
[c1
] == 0) {
628 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
631 case GLSL_TYPE_FLOAT
:
632 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
641 /* FINISHME: Emit warning when division-by-zero is detected. */
642 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
643 for (unsigned c
= 0, c0
= 0, c1
= 0;
645 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
647 switch (op
[0]->type
->base_type
) {
649 if (op
[1]->value
.u
[c1
] == 0) {
652 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
656 if (op
[1]->value
.i
[c1
] == 0) {
659 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
662 case GLSL_TYPE_FLOAT
:
663 /* We don't use fmod because it rounds toward zero; GLSL specifies
666 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
]
667 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
676 case ir_binop_logic_and
:
677 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
678 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
679 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
681 case ir_binop_logic_xor
:
682 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
683 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
684 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
686 case ir_binop_logic_or
:
687 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
688 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
689 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
693 assert(op
[0]->type
== op
[1]->type
);
694 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
695 switch (op
[0]->type
->base_type
) {
697 data
.b
[c
] = op
[0]->value
.u
[c
] < op
[1]->value
.u
[c
];
700 data
.b
[c
] = op
[0]->value
.i
[c
] < op
[1]->value
.i
[c
];
702 case GLSL_TYPE_FLOAT
:
703 data
.b
[c
] = op
[0]->value
.f
[c
] < op
[1]->value
.f
[c
];
710 case ir_binop_greater
:
711 assert(op
[0]->type
== op
[1]->type
);
712 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
713 switch (op
[0]->type
->base_type
) {
715 data
.b
[c
] = op
[0]->value
.u
[c
] > op
[1]->value
.u
[c
];
718 data
.b
[c
] = op
[0]->value
.i
[c
] > op
[1]->value
.i
[c
];
720 case GLSL_TYPE_FLOAT
:
721 data
.b
[c
] = op
[0]->value
.f
[c
] > op
[1]->value
.f
[c
];
728 case ir_binop_lequal
:
729 assert(op
[0]->type
== op
[1]->type
);
730 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
731 switch (op
[0]->type
->base_type
) {
733 data
.b
[c
] = op
[0]->value
.u
[c
] <= op
[1]->value
.u
[c
];
736 data
.b
[c
] = op
[0]->value
.i
[c
] <= op
[1]->value
.i
[c
];
738 case GLSL_TYPE_FLOAT
:
739 data
.b
[c
] = op
[0]->value
.f
[c
] <= op
[1]->value
.f
[c
];
746 case ir_binop_gequal
:
747 assert(op
[0]->type
== op
[1]->type
);
748 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
749 switch (op
[0]->type
->base_type
) {
751 data
.b
[c
] = op
[0]->value
.u
[c
] >= op
[1]->value
.u
[c
];
754 data
.b
[c
] = op
[0]->value
.i
[c
] >= op
[1]->value
.i
[c
];
756 case GLSL_TYPE_FLOAT
:
757 data
.b
[c
] = op
[0]->value
.f
[c
] >= op
[1]->value
.f
[c
];
765 assert(op
[0]->type
== op
[1]->type
);
766 for (unsigned c
= 0; c
< components
; c
++) {
767 switch (op
[0]->type
->base_type
) {
769 data
.b
[c
] = op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
772 data
.b
[c
] = op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
774 case GLSL_TYPE_FLOAT
:
775 data
.b
[c
] = op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
778 data
.b
[c
] = op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
785 case ir_binop_nequal
:
786 assert(op
[0]->type
== op
[1]->type
);
787 for (unsigned c
= 0; c
< components
; c
++) {
788 switch (op
[0]->type
->base_type
) {
790 data
.b
[c
] = op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
793 data
.b
[c
] = op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
795 case GLSL_TYPE_FLOAT
:
796 data
.b
[c
] = op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
799 data
.b
[c
] = op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
806 case ir_binop_all_equal
:
807 data
.b
[0] = op
[0]->has_value(op
[1]);
809 case ir_binop_any_nequal
:
810 data
.b
[0] = !op
[0]->has_value(op
[1]);
813 case ir_binop_lshift
:
814 for (unsigned c
= 0, c0
= 0, c1
= 0;
816 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
818 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
819 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
820 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.i
[c1
];
822 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
823 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
824 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.u
[c1
];
826 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
827 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
828 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.i
[c1
];
830 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
831 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
832 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.u
[c1
];
837 case ir_binop_rshift
:
838 for (unsigned c
= 0, c0
= 0, c1
= 0;
840 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
842 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
843 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
844 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.i
[c1
];
846 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
847 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
848 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.u
[c1
];
850 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
851 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
852 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.i
[c1
];
854 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
855 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
856 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.u
[c1
];
861 case ir_binop_bit_and
:
862 for (unsigned c
= 0, c0
= 0, c1
= 0;
864 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
866 switch (op
[0]->type
->base_type
) {
868 data
.i
[c
] = op
[0]->value
.i
[c0
] & op
[1]->value
.i
[c1
];
871 data
.u
[c
] = op
[0]->value
.u
[c0
] & op
[1]->value
.u
[c1
];
879 case ir_binop_bit_or
:
880 for (unsigned c
= 0, c0
= 0, c1
= 0;
882 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
884 switch (op
[0]->type
->base_type
) {
886 data
.i
[c
] = op
[0]->value
.i
[c0
] | op
[1]->value
.i
[c1
];
889 data
.u
[c
] = op
[0]->value
.u
[c0
] | op
[1]->value
.u
[c1
];
897 case ir_binop_bit_xor
:
898 for (unsigned c
= 0, c0
= 0, c1
= 0;
900 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
902 switch (op
[0]->type
->base_type
) {
904 data
.i
[c
] = op
[0]->value
.i
[c0
] ^ op
[1]->value
.i
[c1
];
907 data
.u
[c
] = op
[0]->value
.u
[c0
] ^ op
[1]->value
.u
[c1
];
915 case ir_quadop_vector
:
916 for (unsigned c
= 0; c
< this->type
->vector_elements
; c
++) {
917 switch (this->type
->base_type
) {
919 data
.i
[c
] = op
[c
]->value
.i
[0];
922 data
.u
[c
] = op
[c
]->value
.u
[0];
924 case GLSL_TYPE_FLOAT
:
925 data
.f
[c
] = op
[c
]->value
.f
[0];
934 /* FINISHME: Should handle all expression types. */
938 return new(ctx
) ir_constant(this->type
, &data
);
943 ir_texture::constant_expression_value(struct hash_table
*variable_context
)
945 /* texture lookups aren't constant expressions */
951 ir_swizzle::constant_expression_value(struct hash_table
*variable_context
)
953 ir_constant
*v
= this->val
->constant_expression_value(variable_context
);
956 ir_constant_data data
= { { 0 } };
958 const unsigned swiz_idx
[4] = {
959 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
962 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
963 switch (v
->type
->base_type
) {
965 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
966 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
967 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
968 default: assert(!"Should not get here."); break;
972 void *ctx
= ralloc_parent(this);
973 return new(ctx
) ir_constant(this->type
, &data
);
980 ir_dereference_variable::constant_referenced(struct hash_table
*variable_context
,
981 ir_constant
*&store
, int &offset
) const
983 if (variable_context
) {
984 store
= (ir_constant
*)hash_table_find(variable_context
, var
);
993 ir_dereference_variable::constant_expression_value(struct hash_table
*variable_context
)
995 /* This may occur during compile and var->type is glsl_type::error_type */
999 /* Give priority to the context hashtable, if it exists */
1000 if (variable_context
) {
1001 ir_constant
*value
= (ir_constant
*)hash_table_find(variable_context
, var
);
1006 /* The constant_value of a uniform variable is its initializer,
1007 * not the lifetime constant value of the uniform.
1009 if (var
->mode
== ir_var_uniform
)
1012 if (!var
->constant_value
)
1015 return var
->constant_value
->clone(ralloc_parent(var
), NULL
);
1020 ir_dereference_array::constant_referenced(struct hash_table
*variable_context
,
1021 ir_constant
*&store
, int &offset
) const
1023 ir_constant
*index_c
= array_index
->constant_expression_value(variable_context
);
1025 if (!index_c
|| !index_c
->type
->is_scalar() || !index_c
->type
->is_integer()) {
1031 int index
= index_c
->type
->base_type
== GLSL_TYPE_INT
?
1032 index_c
->get_int_component(0) :
1033 index_c
->get_uint_component(0);
1035 ir_constant
*substore
;
1037 const ir_dereference
*deref
= array
->as_dereference();
1044 deref
->constant_referenced(variable_context
, substore
, suboffset
);
1052 const glsl_type
*vt
= substore
->type
;
1053 if (vt
->is_array()) {
1054 store
= substore
->get_array_element(index
);
1058 if (vt
->is_matrix()) {
1060 offset
= index
* vt
->vector_elements
;
1063 if (vt
->is_vector()) {
1065 offset
= suboffset
+ index
;
1074 ir_dereference_array::constant_expression_value(struct hash_table
*variable_context
)
1076 ir_constant
*array
= this->array
->constant_expression_value(variable_context
);
1077 ir_constant
*idx
= this->array_index
->constant_expression_value(variable_context
);
1079 if ((array
!= NULL
) && (idx
!= NULL
)) {
1080 void *ctx
= ralloc_parent(this);
1081 if (array
->type
->is_matrix()) {
1082 /* Array access of a matrix results in a vector.
1084 const unsigned column
= idx
->value
.u
[0];
1086 const glsl_type
*const column_type
= array
->type
->column_type();
1088 /* Offset in the constant matrix to the first element of the column
1091 const unsigned mat_idx
= column
* column_type
->vector_elements
;
1093 ir_constant_data data
= { { 0 } };
1095 switch (column_type
->base_type
) {
1096 case GLSL_TYPE_UINT
:
1098 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1099 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
1103 case GLSL_TYPE_FLOAT
:
1104 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1105 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
1110 assert(!"Should not get here.");
1114 return new(ctx
) ir_constant(column_type
, &data
);
1115 } else if (array
->type
->is_vector()) {
1116 const unsigned component
= idx
->value
.u
[0];
1118 return new(ctx
) ir_constant(array
, component
);
1120 const unsigned index
= idx
->value
.u
[0];
1121 return array
->get_array_element(index
)->clone(ctx
, NULL
);
1129 ir_dereference_record::constant_referenced(struct hash_table
*variable_context
,
1130 ir_constant
*&store
, int &offset
) const
1132 ir_constant
*substore
;
1134 const ir_dereference
*deref
= record
->as_dereference();
1141 deref
->constant_referenced(variable_context
, substore
, suboffset
);
1149 store
= substore
->get_record_field(field
);
1154 ir_dereference_record::constant_expression_value(struct hash_table
*variable_context
)
1156 ir_constant
*v
= this->record
->constant_expression_value();
1158 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
1163 ir_assignment::constant_expression_value(struct hash_table
*variable_context
)
1165 /* FINISHME: Handle CEs involving assignment (return RHS) */
1171 ir_constant::constant_expression_value(struct hash_table
*variable_context
)
1178 ir_call::constant_expression_value(struct hash_table
*variable_context
)
1180 return this->callee
->constant_expression_value(&this->actual_parameters
, variable_context
);
1184 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1185 struct hash_table
*variable_context
,
1186 ir_constant
**result
)
1188 foreach_list(n
, &body
) {
1189 ir_instruction
*inst
= (ir_instruction
*)n
;
1190 switch(inst
->ir_type
) {
1192 /* (declare () type symbol) */
1193 case ir_type_variable
: {
1194 ir_variable
*var
= inst
->as_variable();
1195 hash_table_insert(variable_context
, ir_constant::zero(this, var
->type
), var
);
1199 /* (assign [condition] (write-mask) (ref) (value)) */
1200 case ir_type_assignment
: {
1201 ir_assignment
*asg
= inst
->as_assignment();
1202 if (asg
->condition
) {
1203 ir_constant
*cond
= asg
->condition
->constant_expression_value(variable_context
);
1206 if (!cond
->get_bool_component(0))
1210 ir_constant
*store
= NULL
;
1212 asg
->lhs
->constant_referenced(variable_context
, store
, offset
);
1217 ir_constant
*value
= asg
->rhs
->constant_expression_value(variable_context
);
1222 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
1226 /* (return (expression)) */
1227 case ir_type_return
:
1229 *result
= inst
->as_return()->value
->constant_expression_value(variable_context
);
1230 return *result
!= NULL
;
1232 /* (call name (ref) (params))*/
1233 case ir_type_call
: {
1234 ir_call
*call
= inst
->as_call();
1236 /* Just say no to void functions in constant expressions. We
1237 * don't need them at that point.
1240 if (!call
->return_deref
)
1243 ir_constant
*store
= NULL
;
1245 call
->return_deref
->constant_referenced(variable_context
, store
, offset
);
1250 ir_constant
*value
= call
->constant_expression_value(variable_context
);
1255 store
->copy_offset(value
, offset
);
1259 /* (if condition (then-instructions) (else-instructions)) */
1261 ir_if
*iif
= inst
->as_if();
1263 ir_constant
*cond
= iif
->condition
->constant_expression_value(variable_context
);
1264 if (!cond
|| !cond
->type
->is_boolean())
1267 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
1270 if (!constant_expression_evaluate_expression_list(branch
, variable_context
, result
))
1273 /* If there was a return in the branch chosen, drop out now. */
1280 /* Every other expression type, we drop out. */
1286 /* Reaching the end of the block is not an error condition */
1294 ir_function_signature::constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
)
1296 const glsl_type
*type
= this->return_type
;
1297 if (type
== glsl_type::void_type
)
1300 /* From the GLSL 1.20 spec, page 23:
1301 * "Function calls to user-defined functions (non-built-in functions)
1302 * cannot be used to form constant expressions."
1304 if (!this->is_builtin
)
1308 * Of the builtin functions, only the texture lookups and the noise
1309 * ones must not be used in constant expressions. They all include
1310 * specific opcodes so they don't need to be special-cased at this
1314 /* Initialize the table of dereferencable names with the function
1315 * parameters. Verify their const-ness on the way.
1317 * We expect the correctness of the number of parameters to have
1318 * been checked earlier.
1320 hash_table
*deref_hash
= hash_table_ctor(8, hash_table_pointer_hash
,
1321 hash_table_pointer_compare
);
1323 /* If "origin" is non-NULL, then the function body is there. So we
1324 * have to use the variable objects from the object with the body,
1325 * but the parameter instanciation on the current object.
1327 const exec_node
*parameter_info
= origin
? origin
->parameters
.head
: parameters
.head
;
1329 foreach_list(n
, actual_parameters
) {
1330 ir_constant
*constant
= ((ir_rvalue
*) n
)->constant_expression_value(variable_context
);
1331 if (constant
== NULL
) {
1332 hash_table_dtor(deref_hash
);
1337 ir_variable
*var
= (ir_variable
*)parameter_info
;
1338 hash_table_insert(deref_hash
, constant
, var
);
1340 parameter_info
= parameter_info
->next
;
1343 ir_constant
*result
= NULL
;
1345 /* Now run the builtin function until something non-constant
1346 * happens or we get the result.
1348 if (constant_expression_evaluate_expression_list(origin
? origin
->body
: body
, deref_hash
, &result
) && result
)
1349 result
= result
->clone(ralloc_parent(this), NULL
);
1351 hash_table_dtor(deref_hash
);