ir_constant_expression: Add support for ir_unop_sign.
[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_fract:
210 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
211 switch (ir->type->base_type) {
212 case GLSL_TYPE_UINT:
213 data.u[c] = 0;
214 break;
215 case GLSL_TYPE_INT:
216 data.i[c] = 0;
217 break;
218 case GLSL_TYPE_FLOAT:
219 data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]);
220 break;
221 default:
222 assert(0);
223 }
224 }
225 break;
226
227 case ir_unop_neg:
228 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
229 switch (ir->type->base_type) {
230 case GLSL_TYPE_UINT:
231 data.u[c] = -op[0]->value.u[c];
232 break;
233 case GLSL_TYPE_INT:
234 data.i[c] = -op[0]->value.i[c];
235 break;
236 case GLSL_TYPE_FLOAT:
237 data.f[c] = -op[0]->value.f[c];
238 break;
239 default:
240 assert(0);
241 }
242 }
243 break;
244
245 case ir_unop_abs:
246 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
247 switch (ir->type->base_type) {
248 case GLSL_TYPE_UINT:
249 data.u[c] = op[0]->value.u[c];
250 break;
251 case GLSL_TYPE_INT:
252 data.i[c] = op[0]->value.i[c];
253 if (data.i[c] < 0)
254 data.i[c] = -data.i[c];
255 break;
256 case GLSL_TYPE_FLOAT:
257 data.f[c] = fabs(op[0]->value.f[c]);
258 break;
259 default:
260 assert(0);
261 }
262 }
263 break;
264
265 case ir_unop_sign:
266 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
267 switch (ir->type->base_type) {
268 case GLSL_TYPE_UINT:
269 data.u[c] = op[0]->value.i[c] > 0;
270 break;
271 case GLSL_TYPE_INT:
272 data.i[c] = (op[0]->value.i[c] > 0) - (op[0]->value.i[c] < 0);
273 break;
274 case GLSL_TYPE_FLOAT:
275 data.f[c] = float((op[0]->value.f[c] > 0)-(op[0]->value.f[c] < 0));
276 break;
277 default:
278 assert(0);
279 }
280 }
281 break;
282
283 case ir_unop_rcp:
284 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
285 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
286 switch (ir->type->base_type) {
287 case GLSL_TYPE_UINT:
288 if (op[0]->value.u[c] != 0.0)
289 data.u[c] = 1 / op[0]->value.u[c];
290 break;
291 case GLSL_TYPE_INT:
292 if (op[0]->value.i[c] != 0.0)
293 data.i[c] = 1 / op[0]->value.i[c];
294 break;
295 case GLSL_TYPE_FLOAT:
296 if (op[0]->value.f[c] != 0.0)
297 data.f[c] = 1.0 / op[0]->value.f[c];
298 break;
299 default:
300 assert(0);
301 }
302 }
303 break;
304
305 case ir_unop_rsq:
306 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
307 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
308 data.f[c] = 1.0 / sqrtf(op[0]->value.f[c]);
309 }
310 break;
311
312 case ir_unop_sqrt:
313 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
314 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
315 data.f[c] = sqrtf(op[0]->value.f[c]);
316 }
317 break;
318
319 case ir_unop_exp:
320 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
321 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
322 data.f[c] = expf(op[0]->value.f[c]);
323 }
324 break;
325
326 case ir_unop_log:
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] = logf(op[0]->value.f[c]);
330 }
331 break;
332
333 case ir_unop_dFdx:
334 case ir_unop_dFdy:
335 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
336 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
337 data.f[c] = 0.0;
338 }
339 break;
340
341 case ir_binop_dot:
342 assert(op[0]->type->is_vector() && op[1]->type->is_vector());
343 data.f[0] = 0;
344 for (unsigned c = 0; c < op[0]->type->components(); c++) {
345 switch (ir->operands[0]->type->base_type) {
346 case GLSL_TYPE_UINT:
347 data.u[0] += op[0]->value.u[c] * op[1]->value.u[c];
348 break;
349 case GLSL_TYPE_INT:
350 data.i[0] += op[0]->value.i[c] * op[1]->value.i[c];
351 break;
352 case GLSL_TYPE_FLOAT:
353 data.f[0] += op[0]->value.f[c] * op[1]->value.f[c];
354 break;
355 default:
356 assert(0);
357 }
358 }
359
360 break;
361 case ir_binop_add:
362 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
363 for (unsigned c = 0, c0 = 0, c1 = 0;
364 c < components;
365 c0 += c0_inc, c1 += c1_inc, c++) {
366
367 switch (ir->operands[0]->type->base_type) {
368 case GLSL_TYPE_UINT:
369 data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1];
370 break;
371 case GLSL_TYPE_INT:
372 data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1];
373 break;
374 case GLSL_TYPE_FLOAT:
375 data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1];
376 break;
377 default:
378 assert(0);
379 }
380 }
381
382 break;
383 case ir_binop_sub:
384 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
385 for (unsigned c = 0, c0 = 0, c1 = 0;
386 c < components;
387 c0 += c0_inc, c1 += c1_inc, c++) {
388
389 switch (ir->operands[0]->type->base_type) {
390 case GLSL_TYPE_UINT:
391 data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1];
392 break;
393 case GLSL_TYPE_INT:
394 data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1];
395 break;
396 case GLSL_TYPE_FLOAT:
397 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1];
398 break;
399 default:
400 assert(0);
401 }
402 }
403
404 break;
405 case ir_binop_mul:
406 /* Check for equal types, or unequal types involving scalars */
407 if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix())
408 || op0_scalar || op1_scalar) {
409 for (unsigned c = 0, c0 = 0, c1 = 0;
410 c < components;
411 c0 += c0_inc, c1 += c1_inc, c++) {
412
413 switch (ir->operands[0]->type->base_type) {
414 case GLSL_TYPE_UINT:
415 data.u[c] = op[0]->value.u[c0] * op[1]->value.u[c1];
416 break;
417 case GLSL_TYPE_INT:
418 data.i[c] = op[0]->value.i[c0] * op[1]->value.i[c1];
419 break;
420 case GLSL_TYPE_FLOAT:
421 data.f[c] = op[0]->value.f[c0] * op[1]->value.f[c1];
422 break;
423 default:
424 assert(0);
425 }
426 }
427 } else {
428 assert(op[0]->type->is_matrix() || op[1]->type->is_matrix());
429
430 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
431 * matrix can be a GLSL vector, either N or P can be 1.
432 *
433 * For vec*mat, the vector is treated as a row vector. This
434 * means the vector is a 1-row x M-column matrix.
435 *
436 * For mat*vec, the vector is treated as a column vector. Since
437 * matrix_columns is 1 for vectors, this just works.
438 */
439 const unsigned n = op[0]->type->is_vector()
440 ? 1 : op[0]->type->vector_elements;
441 const unsigned m = op[1]->type->vector_elements;
442 const unsigned p = op[1]->type->matrix_columns;
443 for (unsigned j = 0; j < p; j++) {
444 for (unsigned i = 0; i < n; i++) {
445 for (unsigned k = 0; k < m; k++) {
446 data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j];
447 }
448 }
449 }
450 }
451
452 break;
453 case ir_binop_div:
454 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
455 for (unsigned c = 0, c0 = 0, c1 = 0;
456 c < components;
457 c0 += c0_inc, c1 += c1_inc, c++) {
458
459 switch (ir->operands[0]->type->base_type) {
460 case GLSL_TYPE_UINT:
461 data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1];
462 break;
463 case GLSL_TYPE_INT:
464 data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1];
465 break;
466 case GLSL_TYPE_FLOAT:
467 data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1];
468 break;
469 default:
470 assert(0);
471 }
472 }
473
474 break;
475 case ir_binop_logic_and:
476 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
477 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
478 data.b[c] = op[0]->value.b[c] && op[1]->value.b[c];
479 break;
480 case ir_binop_logic_xor:
481 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
482 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
483 data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
484 break;
485 case ir_binop_logic_or:
486 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
487 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
488 data.b[c] = op[0]->value.b[c] || op[1]->value.b[c];
489 break;
490
491 case ir_binop_less:
492 switch (ir->operands[0]->type->base_type) {
493 case GLSL_TYPE_UINT:
494 data.b[0] = op[0]->value.u[0] < op[1]->value.u[0];
495 break;
496 case GLSL_TYPE_INT:
497 data.b[0] = op[0]->value.i[0] < op[1]->value.i[0];
498 break;
499 case GLSL_TYPE_FLOAT:
500 data.b[0] = op[0]->value.f[0] < op[1]->value.f[0];
501 break;
502 default:
503 assert(0);
504 }
505 break;
506 case ir_binop_greater:
507 switch (ir->operands[0]->type->base_type) {
508 case GLSL_TYPE_UINT:
509 data.b[0] = op[0]->value.u[0] > op[1]->value.u[0];
510 break;
511 case GLSL_TYPE_INT:
512 data.b[0] = op[0]->value.i[0] > op[1]->value.i[0];
513 break;
514 case GLSL_TYPE_FLOAT:
515 data.b[0] = op[0]->value.f[0] > op[1]->value.f[0];
516 break;
517 default:
518 assert(0);
519 }
520 break;
521 case ir_binop_lequal:
522 switch (ir->operands[0]->type->base_type) {
523 case GLSL_TYPE_UINT:
524 data.b[0] = op[0]->value.u[0] <= op[1]->value.u[0];
525 break;
526 case GLSL_TYPE_INT:
527 data.b[0] = op[0]->value.i[0] <= op[1]->value.i[0];
528 break;
529 case GLSL_TYPE_FLOAT:
530 data.b[0] = op[0]->value.f[0] <= op[1]->value.f[0];
531 break;
532 default:
533 assert(0);
534 }
535 break;
536 case ir_binop_gequal:
537 switch (ir->operands[0]->type->base_type) {
538 case GLSL_TYPE_UINT:
539 data.b[0] = op[0]->value.u[0] >= op[1]->value.u[0];
540 break;
541 case GLSL_TYPE_INT:
542 data.b[0] = op[0]->value.i[0] >= op[1]->value.i[0];
543 break;
544 case GLSL_TYPE_FLOAT:
545 data.b[0] = op[0]->value.f[0] >= op[1]->value.f[0];
546 break;
547 default:
548 assert(0);
549 }
550 break;
551
552 case ir_binop_equal:
553 data.b[0] = true;
554 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
555 switch (ir->operands[0]->type->base_type) {
556 case GLSL_TYPE_UINT:
557 data.b[0] = data.b[0] && op[0]->value.u[c] == op[1]->value.u[c];
558 break;
559 case GLSL_TYPE_INT:
560 data.b[0] = data.b[0] && op[0]->value.i[c] == op[1]->value.i[c];
561 break;
562 case GLSL_TYPE_FLOAT:
563 data.b[0] = data.b[0] && op[0]->value.f[c] == op[1]->value.f[c];
564 break;
565 case GLSL_TYPE_BOOL:
566 data.b[0] = data.b[0] && op[0]->value.b[c] == op[1]->value.b[c];
567 break;
568 default:
569 assert(0);
570 }
571 }
572 break;
573 case ir_binop_nequal:
574 data.b[0] = false;
575 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
576 switch (ir->operands[0]->type->base_type) {
577 case GLSL_TYPE_UINT:
578 data.b[0] = data.b[0] || op[0]->value.u[c] != op[1]->value.u[c];
579 break;
580 case GLSL_TYPE_INT:
581 data.b[0] = data.b[0] || op[0]->value.i[c] != op[1]->value.i[c];
582 break;
583 case GLSL_TYPE_FLOAT:
584 data.b[0] = data.b[0] || op[0]->value.f[c] != op[1]->value.f[c];
585 break;
586 case GLSL_TYPE_BOOL:
587 data.b[0] = data.b[0] || op[0]->value.b[c] != op[1]->value.b[c];
588 break;
589 default:
590 assert(0);
591 }
592 }
593 break;
594
595 default:
596 /* FINISHME: Should handle all expression types. */
597 return;
598 }
599
600 void *ctx = talloc_parent(ir);
601 this->value = new(ctx) ir_constant(ir->type, &data);
602 }
603
604
605 void
606 ir_constant_visitor::visit(ir_texture *ir)
607 {
608 // FINISHME: Do stuff with texture lookups
609 (void) ir;
610 value = NULL;
611 }
612
613
614 void
615 ir_constant_visitor::visit(ir_swizzle *ir)
616 {
617 ir_constant *v = ir->val->constant_expression_value();
618
619 this->value = NULL;
620
621 if (v != NULL) {
622 ir_constant_data data;
623
624 const unsigned swiz_idx[4] = {
625 ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w
626 };
627
628 for (unsigned i = 0; i < ir->mask.num_components; i++) {
629 switch (v->type->base_type) {
630 case GLSL_TYPE_UINT:
631 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
632 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
633 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
634 default: assert(!"Should not get here."); break;
635 }
636 }
637
638 void *ctx = talloc_parent(ir);
639 this->value = new(ctx) ir_constant(ir->type, &data);
640 }
641 }
642
643
644 void
645 ir_constant_visitor::visit(ir_dereference_variable *ir)
646 {
647 value = NULL;
648
649 ir_variable *var = ir->variable_referenced();
650 if (var && var->constant_value)
651 value = var->constant_value->clone(NULL);
652 }
653
654
655 void
656 ir_constant_visitor::visit(ir_dereference_array *ir)
657 {
658 void *ctx = talloc_parent(ir);
659 ir_constant *array = ir->array->constant_expression_value();
660 ir_constant *idx = ir->array_index->constant_expression_value();
661
662 this->value = NULL;
663
664 if ((array != NULL) && (idx != NULL)) {
665 if (array->type->is_matrix()) {
666 /* Array access of a matrix results in a vector.
667 */
668 const unsigned column = idx->value.u[0];
669
670 const glsl_type *const column_type = array->type->column_type();
671
672 /* Offset in the constant matrix to the first element of the column
673 * to be extracted.
674 */
675 const unsigned mat_idx = column * column_type->vector_elements;
676
677 ir_constant_data data;
678
679 switch (column_type->base_type) {
680 case GLSL_TYPE_UINT:
681 case GLSL_TYPE_INT:
682 for (unsigned i = 0; i < column_type->vector_elements; i++)
683 data.u[i] = array->value.u[mat_idx + i];
684
685 break;
686
687 case GLSL_TYPE_FLOAT:
688 for (unsigned i = 0; i < column_type->vector_elements; i++)
689 data.f[i] = array->value.f[mat_idx + i];
690
691 break;
692
693 default:
694 assert(!"Should not get here.");
695 break;
696 }
697
698 this->value = new(ctx) ir_constant(column_type, &data);
699 } else if (array->type->is_vector()) {
700 const unsigned component = idx->value.u[0];
701
702 this->value = new(ctx) ir_constant(array, component);
703 } else {
704 /* FINISHME: Handle access of constant arrays. */
705 }
706 }
707 }
708
709
710 void
711 ir_constant_visitor::visit(ir_dereference_record *ir)
712 {
713 ir_constant *v = ir->record->constant_expression_value();
714
715 this->value = (v != NULL) ? v->get_record_field(ir->field) : NULL;
716 }
717
718
719 void
720 ir_constant_visitor::visit(ir_assignment *ir)
721 {
722 (void) ir;
723 value = NULL;
724 }
725
726
727 void
728 ir_constant_visitor::visit(ir_constant *ir)
729 {
730 value = ir;
731 }
732
733
734 void
735 ir_constant_visitor::visit(ir_call *ir)
736 {
737 (void) ir;
738 value = NULL;
739 }
740
741
742 void
743 ir_constant_visitor::visit(ir_return *ir)
744 {
745 (void) ir;
746 value = NULL;
747 }
748
749
750 void
751 ir_constant_visitor::visit(ir_discard *ir)
752 {
753 (void) ir;
754 value = NULL;
755 }
756
757
758 void
759 ir_constant_visitor::visit(ir_if *ir)
760 {
761 (void) ir;
762 value = NULL;
763 }
764
765
766 void
767 ir_constant_visitor::visit(ir_loop *ir)
768 {
769 (void) ir;
770 value = NULL;
771 }
772
773
774 void
775 ir_constant_visitor::visit(ir_loop_jump *ir)
776 {
777 (void) ir;
778 value = NULL;
779 }