ir_constant_expression: Add support for ir_unop_floor.
[mesa.git] / src / glsl / ir_constant_expression.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file ir_constant_expression.cpp
26 * Evaluate and process constant valued expressions
27 *
28 * In GLSL, constant valued expressions are used in several places. These
29 * must be processed and evaluated very early in the compilation process.
30 *
31 * * Sizes of arrays
32 * * Initializers for uniforms
33 * * Initializers for \c const variables
34 */
35
36 #include <math.h>
37 #include "ir.h"
38 #include "ir_visitor.h"
39 #include "glsl_types.h"
40
41 /**
42 * Visitor class for evaluating constant expressions
43 */
44 class ir_constant_visitor : public ir_visitor {
45 public:
46 ir_constant_visitor()
47 : value(NULL)
48 {
49 /* empty */
50 }
51
52 virtual ~ir_constant_visitor()
53 {
54 /* empty */
55 }
56
57 /**
58 * \name Visit methods
59 *
60 * As typical for the visitor pattern, there must be one \c visit method for
61 * each concrete subclass of \c ir_instruction. Virtual base classes within
62 * the hierarchy should not have \c visit methods.
63 */
64 /*@{*/
65 virtual void visit(ir_variable *);
66 virtual void visit(ir_function_signature *);
67 virtual void visit(ir_function *);
68 virtual void visit(ir_expression *);
69 virtual void visit(ir_texture *);
70 virtual void visit(ir_swizzle *);
71 virtual void visit(ir_dereference_variable *);
72 virtual void visit(ir_dereference_array *);
73 virtual void visit(ir_dereference_record *);
74 virtual void visit(ir_assignment *);
75 virtual void visit(ir_constant *);
76 virtual void visit(ir_call *);
77 virtual void visit(ir_return *);
78 virtual void visit(ir_discard *);
79 virtual void visit(ir_if *);
80 virtual void visit(ir_loop *);
81 virtual void visit(ir_loop_jump *);
82 /*@}*/
83
84 /**
85 * Value of the constant expression.
86 *
87 * \note
88 * This field will be \c NULL if the expression is not constant valued.
89 */
90 /* FINIHSME: This cannot hold values for constant arrays or structures. */
91 ir_constant *value;
92 };
93
94
95 ir_constant *
96 ir_instruction::constant_expression_value()
97 {
98 ir_constant_visitor visitor;
99
100 this->accept(& visitor);
101 return visitor.value;
102 }
103
104
105 void
106 ir_constant_visitor::visit(ir_variable *ir)
107 {
108 (void) ir;
109 value = NULL;
110 }
111
112
113 void
114 ir_constant_visitor::visit(ir_function_signature *ir)
115 {
116 (void) ir;
117 value = NULL;
118 }
119
120
121 void
122 ir_constant_visitor::visit(ir_function *ir)
123 {
124 (void) ir;
125 value = NULL;
126 }
127
128 void
129 ir_constant_visitor::visit(ir_expression *ir)
130 {
131 value = NULL;
132 ir_constant *op[2] = { NULL, NULL };
133 ir_constant_data data;
134
135 memset(&data, 0, sizeof(data));
136
137 for (unsigned operand = 0; operand < ir->get_num_operands(); operand++) {
138 op[operand] = ir->operands[operand]->constant_expression_value();
139 if (!op[operand])
140 return;
141 }
142
143 if (op[1] != NULL)
144 assert(op[0]->type->base_type == op[1]->type->base_type);
145
146 bool op0_scalar = op[0]->type->is_scalar();
147 bool op1_scalar = op[1] != NULL && op[1]->type->is_scalar();
148
149 /* When iterating over a vector or matrix's components, we want to increase
150 * the loop counter. However, for scalars, we want to stay at 0.
151 */
152 unsigned c0_inc = op0_scalar ? 0 : 1;
153 unsigned c1_inc = op1_scalar ? 0 : 1;
154 unsigned components;
155 if (op1_scalar || !op[1]) {
156 components = op[0]->type->components();
157 } else {
158 components = op[1]->type->components();
159 }
160
161 switch (ir->operation) {
162 case ir_unop_logic_not:
163 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
164 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
165 data.b[c] = !op[0]->value.b[c];
166 break;
167
168 case ir_unop_f2i:
169 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
170 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
171 data.i[c] = op[0]->value.f[c];
172 }
173 break;
174 case ir_unop_i2f:
175 assert(op[0]->type->base_type == GLSL_TYPE_UINT ||
176 op[0]->type->base_type == GLSL_TYPE_INT);
177 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
178 if (op[0]->type->base_type == GLSL_TYPE_INT)
179 data.f[c] = op[0]->value.i[c];
180 else
181 data.f[c] = op[0]->value.u[c];
182 }
183 break;
184 case ir_unop_b2f:
185 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
186 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
187 data.f[c] = op[0]->value.b[c] ? 1.0 : 0.0;
188 }
189 break;
190 case ir_unop_f2b:
191 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
192 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
193 data.b[c] = bool(op[0]->value.f[c]);
194 }
195 break;
196 case ir_unop_b2i:
197 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
198 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
199 data.u[c] = op[0]->value.b[c] ? 1 : 0;
200 }
201 break;
202 case ir_unop_i2b:
203 assert(op[0]->type->is_integer());
204 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
205 data.b[c] = bool(op[0]->value.u[c]);
206 }
207 break;
208
209 case ir_unop_trunc:
210 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
211 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
212 data.f[c] = truncf(op[0]->value.f[c]);
213 }
214 break;
215
216 case ir_unop_ceil:
217 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
218 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
219 data.f[c] = ceilf(op[0]->value.f[c]);
220 }
221 break;
222
223 case ir_unop_floor:
224 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
225 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
226 data.f[c] = floorf(op[0]->value.f[c]);
227 }
228 break;
229
230 case ir_unop_fract:
231 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
232 switch (ir->type->base_type) {
233 case GLSL_TYPE_UINT:
234 data.u[c] = 0;
235 break;
236 case GLSL_TYPE_INT:
237 data.i[c] = 0;
238 break;
239 case GLSL_TYPE_FLOAT:
240 data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]);
241 break;
242 default:
243 assert(0);
244 }
245 }
246 break;
247
248 case ir_unop_neg:
249 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
250 switch (ir->type->base_type) {
251 case GLSL_TYPE_UINT:
252 data.u[c] = -op[0]->value.u[c];
253 break;
254 case GLSL_TYPE_INT:
255 data.i[c] = -op[0]->value.i[c];
256 break;
257 case GLSL_TYPE_FLOAT:
258 data.f[c] = -op[0]->value.f[c];
259 break;
260 default:
261 assert(0);
262 }
263 }
264 break;
265
266 case ir_unop_abs:
267 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
268 switch (ir->type->base_type) {
269 case GLSL_TYPE_UINT:
270 data.u[c] = op[0]->value.u[c];
271 break;
272 case GLSL_TYPE_INT:
273 data.i[c] = op[0]->value.i[c];
274 if (data.i[c] < 0)
275 data.i[c] = -data.i[c];
276 break;
277 case GLSL_TYPE_FLOAT:
278 data.f[c] = fabs(op[0]->value.f[c]);
279 break;
280 default:
281 assert(0);
282 }
283 }
284 break;
285
286 case ir_unop_sign:
287 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
288 switch (ir->type->base_type) {
289 case GLSL_TYPE_UINT:
290 data.u[c] = op[0]->value.i[c] > 0;
291 break;
292 case GLSL_TYPE_INT:
293 data.i[c] = (op[0]->value.i[c] > 0) - (op[0]->value.i[c] < 0);
294 break;
295 case GLSL_TYPE_FLOAT:
296 data.f[c] = float((op[0]->value.f[c] > 0)-(op[0]->value.f[c] < 0));
297 break;
298 default:
299 assert(0);
300 }
301 }
302 break;
303
304 case ir_unop_rcp:
305 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
306 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
307 switch (ir->type->base_type) {
308 case GLSL_TYPE_UINT:
309 if (op[0]->value.u[c] != 0.0)
310 data.u[c] = 1 / op[0]->value.u[c];
311 break;
312 case GLSL_TYPE_INT:
313 if (op[0]->value.i[c] != 0.0)
314 data.i[c] = 1 / op[0]->value.i[c];
315 break;
316 case GLSL_TYPE_FLOAT:
317 if (op[0]->value.f[c] != 0.0)
318 data.f[c] = 1.0 / op[0]->value.f[c];
319 break;
320 default:
321 assert(0);
322 }
323 }
324 break;
325
326 case ir_unop_rsq:
327 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
328 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
329 data.f[c] = 1.0 / sqrtf(op[0]->value.f[c]);
330 }
331 break;
332
333 case ir_unop_sqrt:
334 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
335 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
336 data.f[c] = sqrtf(op[0]->value.f[c]);
337 }
338 break;
339
340 case ir_unop_exp:
341 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
342 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
343 data.f[c] = expf(op[0]->value.f[c]);
344 }
345 break;
346
347 case ir_unop_exp2:
348 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
349 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
350 data.f[c] = exp2f(op[0]->value.f[c]);
351 }
352 break;
353
354 case ir_unop_log:
355 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
356 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
357 data.f[c] = logf(op[0]->value.f[c]);
358 }
359 break;
360
361 case ir_unop_log2:
362 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
363 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
364 data.f[c] = log2f(op[0]->value.f[c]);
365 }
366 break;
367
368 case ir_unop_dFdx:
369 case ir_unop_dFdy:
370 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
371 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
372 data.f[c] = 0.0;
373 }
374 break;
375
376 case ir_binop_dot:
377 assert(op[0]->type->is_vector() && op[1]->type->is_vector());
378 data.f[0] = 0;
379 for (unsigned c = 0; c < op[0]->type->components(); c++) {
380 switch (ir->operands[0]->type->base_type) {
381 case GLSL_TYPE_UINT:
382 data.u[0] += op[0]->value.u[c] * op[1]->value.u[c];
383 break;
384 case GLSL_TYPE_INT:
385 data.i[0] += op[0]->value.i[c] * op[1]->value.i[c];
386 break;
387 case GLSL_TYPE_FLOAT:
388 data.f[0] += op[0]->value.f[c] * op[1]->value.f[c];
389 break;
390 default:
391 assert(0);
392 }
393 }
394
395 break;
396 case ir_binop_add:
397 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
398 for (unsigned c = 0, c0 = 0, c1 = 0;
399 c < components;
400 c0 += c0_inc, c1 += c1_inc, c++) {
401
402 switch (ir->operands[0]->type->base_type) {
403 case GLSL_TYPE_UINT:
404 data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1];
405 break;
406 case GLSL_TYPE_INT:
407 data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1];
408 break;
409 case GLSL_TYPE_FLOAT:
410 data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1];
411 break;
412 default:
413 assert(0);
414 }
415 }
416
417 break;
418 case ir_binop_sub:
419 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
420 for (unsigned c = 0, c0 = 0, c1 = 0;
421 c < components;
422 c0 += c0_inc, c1 += c1_inc, c++) {
423
424 switch (ir->operands[0]->type->base_type) {
425 case GLSL_TYPE_UINT:
426 data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1];
427 break;
428 case GLSL_TYPE_INT:
429 data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1];
430 break;
431 case GLSL_TYPE_FLOAT:
432 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1];
433 break;
434 default:
435 assert(0);
436 }
437 }
438
439 break;
440 case ir_binop_mul:
441 /* Check for equal types, or unequal types involving scalars */
442 if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix())
443 || op0_scalar || op1_scalar) {
444 for (unsigned c = 0, c0 = 0, c1 = 0;
445 c < components;
446 c0 += c0_inc, c1 += c1_inc, c++) {
447
448 switch (ir->operands[0]->type->base_type) {
449 case GLSL_TYPE_UINT:
450 data.u[c] = op[0]->value.u[c0] * op[1]->value.u[c1];
451 break;
452 case GLSL_TYPE_INT:
453 data.i[c] = op[0]->value.i[c0] * op[1]->value.i[c1];
454 break;
455 case GLSL_TYPE_FLOAT:
456 data.f[c] = op[0]->value.f[c0] * op[1]->value.f[c1];
457 break;
458 default:
459 assert(0);
460 }
461 }
462 } else {
463 assert(op[0]->type->is_matrix() || op[1]->type->is_matrix());
464
465 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
466 * matrix can be a GLSL vector, either N or P can be 1.
467 *
468 * For vec*mat, the vector is treated as a row vector. This
469 * means the vector is a 1-row x M-column matrix.
470 *
471 * For mat*vec, the vector is treated as a column vector. Since
472 * matrix_columns is 1 for vectors, this just works.
473 */
474 const unsigned n = op[0]->type->is_vector()
475 ? 1 : op[0]->type->vector_elements;
476 const unsigned m = op[1]->type->vector_elements;
477 const unsigned p = op[1]->type->matrix_columns;
478 for (unsigned j = 0; j < p; j++) {
479 for (unsigned i = 0; i < n; i++) {
480 for (unsigned k = 0; k < m; k++) {
481 data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j];
482 }
483 }
484 }
485 }
486
487 break;
488 case ir_binop_div:
489 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
490 for (unsigned c = 0, c0 = 0, c1 = 0;
491 c < components;
492 c0 += c0_inc, c1 += c1_inc, c++) {
493
494 switch (ir->operands[0]->type->base_type) {
495 case GLSL_TYPE_UINT:
496 data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1];
497 break;
498 case GLSL_TYPE_INT:
499 data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1];
500 break;
501 case GLSL_TYPE_FLOAT:
502 data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1];
503 break;
504 default:
505 assert(0);
506 }
507 }
508
509 break;
510 case ir_binop_logic_and:
511 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
512 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
513 data.b[c] = op[0]->value.b[c] && op[1]->value.b[c];
514 break;
515 case ir_binop_logic_xor:
516 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
517 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
518 data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
519 break;
520 case ir_binop_logic_or:
521 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
522 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
523 data.b[c] = op[0]->value.b[c] || op[1]->value.b[c];
524 break;
525
526 case ir_binop_less:
527 switch (ir->operands[0]->type->base_type) {
528 case GLSL_TYPE_UINT:
529 data.b[0] = op[0]->value.u[0] < op[1]->value.u[0];
530 break;
531 case GLSL_TYPE_INT:
532 data.b[0] = op[0]->value.i[0] < op[1]->value.i[0];
533 break;
534 case GLSL_TYPE_FLOAT:
535 data.b[0] = op[0]->value.f[0] < op[1]->value.f[0];
536 break;
537 default:
538 assert(0);
539 }
540 break;
541 case ir_binop_greater:
542 switch (ir->operands[0]->type->base_type) {
543 case GLSL_TYPE_UINT:
544 data.b[0] = op[0]->value.u[0] > op[1]->value.u[0];
545 break;
546 case GLSL_TYPE_INT:
547 data.b[0] = op[0]->value.i[0] > op[1]->value.i[0];
548 break;
549 case GLSL_TYPE_FLOAT:
550 data.b[0] = op[0]->value.f[0] > op[1]->value.f[0];
551 break;
552 default:
553 assert(0);
554 }
555 break;
556 case ir_binop_lequal:
557 switch (ir->operands[0]->type->base_type) {
558 case GLSL_TYPE_UINT:
559 data.b[0] = op[0]->value.u[0] <= op[1]->value.u[0];
560 break;
561 case GLSL_TYPE_INT:
562 data.b[0] = op[0]->value.i[0] <= op[1]->value.i[0];
563 break;
564 case GLSL_TYPE_FLOAT:
565 data.b[0] = op[0]->value.f[0] <= op[1]->value.f[0];
566 break;
567 default:
568 assert(0);
569 }
570 break;
571 case ir_binop_gequal:
572 switch (ir->operands[0]->type->base_type) {
573 case GLSL_TYPE_UINT:
574 data.b[0] = op[0]->value.u[0] >= op[1]->value.u[0];
575 break;
576 case GLSL_TYPE_INT:
577 data.b[0] = op[0]->value.i[0] >= op[1]->value.i[0];
578 break;
579 case GLSL_TYPE_FLOAT:
580 data.b[0] = op[0]->value.f[0] >= op[1]->value.f[0];
581 break;
582 default:
583 assert(0);
584 }
585 break;
586
587 case ir_binop_equal:
588 data.b[0] = true;
589 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
590 switch (ir->operands[0]->type->base_type) {
591 case GLSL_TYPE_UINT:
592 data.b[0] = data.b[0] && op[0]->value.u[c] == op[1]->value.u[c];
593 break;
594 case GLSL_TYPE_INT:
595 data.b[0] = data.b[0] && op[0]->value.i[c] == op[1]->value.i[c];
596 break;
597 case GLSL_TYPE_FLOAT:
598 data.b[0] = data.b[0] && op[0]->value.f[c] == op[1]->value.f[c];
599 break;
600 case GLSL_TYPE_BOOL:
601 data.b[0] = data.b[0] && op[0]->value.b[c] == op[1]->value.b[c];
602 break;
603 default:
604 assert(0);
605 }
606 }
607 break;
608 case ir_binop_nequal:
609 data.b[0] = false;
610 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
611 switch (ir->operands[0]->type->base_type) {
612 case GLSL_TYPE_UINT:
613 data.b[0] = data.b[0] || op[0]->value.u[c] != op[1]->value.u[c];
614 break;
615 case GLSL_TYPE_INT:
616 data.b[0] = data.b[0] || op[0]->value.i[c] != op[1]->value.i[c];
617 break;
618 case GLSL_TYPE_FLOAT:
619 data.b[0] = data.b[0] || op[0]->value.f[c] != op[1]->value.f[c];
620 break;
621 case GLSL_TYPE_BOOL:
622 data.b[0] = data.b[0] || op[0]->value.b[c] != op[1]->value.b[c];
623 break;
624 default:
625 assert(0);
626 }
627 }
628 break;
629
630 default:
631 /* FINISHME: Should handle all expression types. */
632 return;
633 }
634
635 void *ctx = talloc_parent(ir);
636 this->value = new(ctx) ir_constant(ir->type, &data);
637 }
638
639
640 void
641 ir_constant_visitor::visit(ir_texture *ir)
642 {
643 // FINISHME: Do stuff with texture lookups
644 (void) ir;
645 value = NULL;
646 }
647
648
649 void
650 ir_constant_visitor::visit(ir_swizzle *ir)
651 {
652 ir_constant *v = ir->val->constant_expression_value();
653
654 this->value = NULL;
655
656 if (v != NULL) {
657 ir_constant_data data;
658
659 const unsigned swiz_idx[4] = {
660 ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w
661 };
662
663 for (unsigned i = 0; i < ir->mask.num_components; i++) {
664 switch (v->type->base_type) {
665 case GLSL_TYPE_UINT:
666 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
667 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
668 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
669 default: assert(!"Should not get here."); break;
670 }
671 }
672
673 void *ctx = talloc_parent(ir);
674 this->value = new(ctx) ir_constant(ir->type, &data);
675 }
676 }
677
678
679 void
680 ir_constant_visitor::visit(ir_dereference_variable *ir)
681 {
682 value = NULL;
683
684 ir_variable *var = ir->variable_referenced();
685 if (var && var->constant_value)
686 value = var->constant_value->clone(NULL);
687 }
688
689
690 void
691 ir_constant_visitor::visit(ir_dereference_array *ir)
692 {
693 void *ctx = talloc_parent(ir);
694 ir_constant *array = ir->array->constant_expression_value();
695 ir_constant *idx = ir->array_index->constant_expression_value();
696
697 this->value = NULL;
698
699 if ((array != NULL) && (idx != NULL)) {
700 if (array->type->is_matrix()) {
701 /* Array access of a matrix results in a vector.
702 */
703 const unsigned column = idx->value.u[0];
704
705 const glsl_type *const column_type = array->type->column_type();
706
707 /* Offset in the constant matrix to the first element of the column
708 * to be extracted.
709 */
710 const unsigned mat_idx = column * column_type->vector_elements;
711
712 ir_constant_data data;
713
714 switch (column_type->base_type) {
715 case GLSL_TYPE_UINT:
716 case GLSL_TYPE_INT:
717 for (unsigned i = 0; i < column_type->vector_elements; i++)
718 data.u[i] = array->value.u[mat_idx + i];
719
720 break;
721
722 case GLSL_TYPE_FLOAT:
723 for (unsigned i = 0; i < column_type->vector_elements; i++)
724 data.f[i] = array->value.f[mat_idx + i];
725
726 break;
727
728 default:
729 assert(!"Should not get here.");
730 break;
731 }
732
733 this->value = new(ctx) ir_constant(column_type, &data);
734 } else if (array->type->is_vector()) {
735 const unsigned component = idx->value.u[0];
736
737 this->value = new(ctx) ir_constant(array, component);
738 } else {
739 /* FINISHME: Handle access of constant arrays. */
740 }
741 }
742 }
743
744
745 void
746 ir_constant_visitor::visit(ir_dereference_record *ir)
747 {
748 ir_constant *v = ir->record->constant_expression_value();
749
750 this->value = (v != NULL) ? v->get_record_field(ir->field) : NULL;
751 }
752
753
754 void
755 ir_constant_visitor::visit(ir_assignment *ir)
756 {
757 (void) ir;
758 value = NULL;
759 }
760
761
762 void
763 ir_constant_visitor::visit(ir_constant *ir)
764 {
765 value = ir;
766 }
767
768
769 void
770 ir_constant_visitor::visit(ir_call *ir)
771 {
772 (void) ir;
773 value = NULL;
774 }
775
776
777 void
778 ir_constant_visitor::visit(ir_return *ir)
779 {
780 (void) ir;
781 value = NULL;
782 }
783
784
785 void
786 ir_constant_visitor::visit(ir_discard *ir)
787 {
788 (void) ir;
789 value = NULL;
790 }
791
792
793 void
794 ir_constant_visitor::visit(ir_if *ir)
795 {
796 (void) ir;
797 value = NULL;
798 }
799
800
801 void
802 ir_constant_visitor::visit(ir_loop *ir)
803 {
804 (void) ir;
805 value = NULL;
806 }
807
808
809 void
810 ir_constant_visitor::visit(ir_loop_jump *ir)
811 {
812 (void) ir;
813 value = NULL;
814 }