blob: 871bce43b7a3a0fe68cf5ed1d9e572f85dfbcb36 [file] [log] [blame]
Ian Romanick1cf43a42010-03-30 16:56:50 -07001/*
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
Eric Anholt43ad37a2010-05-12 14:42:21 -070036#include <math.h>
Ian Romanick1cf43a42010-03-30 16:56:50 -070037#include "ir.h"
38#include "ir_visitor.h"
Eric Anholta576f9d2010-03-31 16:25:12 -100039#include "glsl_types.h"
Ian Romanick1cf43a42010-03-30 16:56:50 -070040
41/**
42 * Visitor class for evaluating constant expressions
43 */
44class ir_constant_visitor : public ir_visitor {
45public:
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 *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070066 virtual void visit(ir_function_signature *);
67 virtual void visit(ir_function *);
68 virtual void visit(ir_expression *);
Kenneth Graunke26d74cd2010-05-26 17:42:03 -070069 virtual void visit(ir_texture *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070070 virtual void visit(ir_swizzle *);
Ian Romanickc7b10462010-05-19 13:20:12 +020071 virtual void visit(ir_dereference_variable *);
72 virtual void visit(ir_dereference_array *);
73 virtual void visit(ir_dereference_record *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070074 virtual void visit(ir_assignment *);
75 virtual void visit(ir_constant *);
76 virtual void visit(ir_call *);
77 virtual void visit(ir_return *);
Kenneth Graunke16efab12010-06-30 10:47:34 -070078 virtual void visit(ir_discard *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070079 virtual void visit(ir_if *);
Ian Romanickfad607a2010-04-05 16:16:07 -070080 virtual void visit(ir_loop *);
Ian Romanickf8e31e02010-04-05 16:28:15 -070081 virtual void visit(ir_loop_jump *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070082 /*@}*/
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
95ir_constant *
96ir_instruction::constant_expression_value()
97{
98 ir_constant_visitor visitor;
99
100 this->accept(& visitor);
101 return visitor.value;
102}
103
104
105void
106ir_constant_visitor::visit(ir_variable *ir)
107{
108 (void) ir;
109 value = NULL;
110}
111
112
113void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700114ir_constant_visitor::visit(ir_function_signature *ir)
115{
116 (void) ir;
117 value = NULL;
118}
119
120
121void
122ir_constant_visitor::visit(ir_function *ir)
123{
124 (void) ir;
125 value = NULL;
126}
127
Ian Romanick1cf43a42010-03-30 16:56:50 -0700128void
129ir_constant_visitor::visit(ir_expression *ir)
130{
Ian Romanick1cf43a42010-03-30 16:56:50 -0700131 value = NULL;
Kenneth Graunke6bc432e2010-07-02 17:12:23 -0700132 ir_constant *op[2] = { NULL, NULL };
Ian Romanick4daaab62010-06-11 16:08:47 -0700133 ir_constant_data data;
Eric Anholt160d0922010-04-01 18:07:08 -1000134
Kenneth Graunkec63a1db2010-07-05 22:33:35 -0700135 memset(&data, 0, sizeof(data));
136
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700137 for (unsigned operand = 0; operand < ir->get_num_operands(); operand++) {
Eric Anholtd98da972010-04-01 18:25:11 -1000138 op[operand] = ir->operands[operand]->constant_expression_value();
139 if (!op[operand])
Eric Anholt160d0922010-04-01 18:07:08 -1000140 return;
141 }
Eric Anholta576f9d2010-03-31 16:25:12 -1000142
Kenneth Graunke6fc983b2010-07-06 02:39:57 -0700143 if (op[1] != NULL)
144 assert(op[0]->type->base_type == op[1]->type->base_type);
145
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700146 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 */
Kenneth Graunkeacf88f22010-07-07 12:08:23 -0700152 unsigned c0_inc = op0_scalar ? 0 : 1;
153 unsigned c1_inc = op1_scalar ? 0 : 1;
Eric Anholt570dc0d2010-07-07 09:07:09 -0700154 unsigned components;
155 if (op1_scalar || !op[1]) {
156 components = op[0]->type->components();
157 } else {
158 components = op[1]->type->components();
159 }
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700160
Eric Anholta576f9d2010-03-31 16:25:12 -1000161 switch (ir->operation) {
Eric Anholt528bb852010-03-31 21:09:02 -1000162 case ir_unop_logic_not:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700163 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700164 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700165 data.b[c] = !op[0]->value.b[c];
Eric Anholt528bb852010-03-31 21:09:02 -1000166 break;
Eric Anholtaf186412010-04-06 10:53:57 -0700167
168 case ir_unop_f2i:
169 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700170 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700171 data.i[c] = op[0]->value.f[c];
Eric Anholtaf186412010-04-06 10:53:57 -0700172 }
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);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700177 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Eric Anholtaf186412010-04-06 10:53:57 -0700178 if (op[0]->type->base_type == GLSL_TYPE_INT)
Ian Romanick4daaab62010-06-11 16:08:47 -0700179 data.f[c] = op[0]->value.i[c];
Eric Anholtaf186412010-04-06 10:53:57 -0700180 else
Ian Romanick4daaab62010-06-11 16:08:47 -0700181 data.f[c] = op[0]->value.u[c];
Eric Anholtaf186412010-04-06 10:53:57 -0700182 }
183 break;
Ian Romanick7dc2b712010-06-07 15:10:14 -0700184 case ir_unop_b2f:
185 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700186 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700187 data.f[c] = op[0]->value.b[c] ? 1.0 : 0.0;
Ian Romanick7dc2b712010-06-07 15:10:14 -0700188 }
189 break;
190 case ir_unop_f2b:
191 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700192 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700193 data.b[c] = bool(op[0]->value.f[c]);
Ian Romanick7dc2b712010-06-07 15:10:14 -0700194 }
195 break;
Ian Romanick39d6dd32010-06-11 13:46:30 -0700196 case ir_unop_b2i:
197 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700198 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700199 data.u[c] = op[0]->value.b[c] ? 1 : 0;
Ian Romanick39d6dd32010-06-11 13:46:30 -0700200 }
201 break;
202 case ir_unop_i2b:
203 assert(op[0]->type->is_integer());
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700204 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700205 data.b[c] = bool(op[0]->value.u[c]);
Ian Romanick39d6dd32010-06-11 13:46:30 -0700206 }
207 break;
Eric Anholtaf186412010-04-06 10:53:57 -0700208
Eric Anholtd925c912010-07-01 10:37:11 -0700209 case ir_unop_fract:
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700210 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Eric Anholtd925c912010-07-01 10:37:11 -0700211 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
Eric Anholt43ad37a2010-05-12 14:42:21 -0700227 case ir_unop_neg:
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700228 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700229 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700230 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700231 data.u[c] = -op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700232 break;
233 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700234 data.i[c] = -op[0]->value.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700235 break;
236 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700237 data.f[c] = -op[0]->value.f[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700238 break;
239 default:
240 assert(0);
241 }
242 }
243 break;
244
245 case ir_unop_abs:
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700246 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700247 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700248 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700249 data.u[c] = op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700250 break;
251 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700252 data.i[c] = op[0]->value.i[c];
253 if (data.i[c] < 0)
254 data.i[c] = -data.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700255 break;
256 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700257 data.f[c] = fabs(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700258 break;
259 default:
260 assert(0);
261 }
262 }
263 break;
264
Kenneth Graunke14b7b262010-07-08 23:11:14 -0700265 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
Eric Anholt43ad37a2010-05-12 14:42:21 -0700283 case ir_unop_rcp:
284 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700285 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700286 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700287 case GLSL_TYPE_UINT:
288 if (op[0]->value.u[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700289 data.u[c] = 1 / op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700290 break;
291 case GLSL_TYPE_INT:
292 if (op[0]->value.i[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700293 data.i[c] = 1 / op[0]->value.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700294 break;
295 case GLSL_TYPE_FLOAT:
296 if (op[0]->value.f[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700297 data.f[c] = 1.0 / op[0]->value.f[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700298 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);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700307 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700308 data.f[c] = 1.0 / sqrtf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700309 }
310 break;
311
312 case ir_unop_sqrt:
313 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700314 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700315 data.f[c] = sqrtf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700316 }
317 break;
318
319 case ir_unop_exp:
320 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700321 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700322 data.f[c] = expf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700323 }
324 break;
325
Kenneth Graunkeaca01ed2010-07-08 23:14:32 -0700326 case ir_unop_exp2:
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] = exp2f(op[0]->value.f[c]);
330 }
331 break;
332
Eric Anholt43ad37a2010-05-12 14:42:21 -0700333 case ir_unop_log:
334 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700335 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700336 data.f[c] = logf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700337 }
338 break;
339
Kenneth Graunked6a32d42010-06-09 15:22:35 -0700340 case ir_unop_dFdx:
341 case ir_unop_dFdy:
342 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700343 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700344 data.f[c] = 0.0;
Kenneth Graunked6a32d42010-06-09 15:22:35 -0700345 }
346 break;
347
Kenneth Graunke3f4a0b82010-07-05 21:15:32 -0700348 case ir_binop_dot:
349 assert(op[0]->type->is_vector() && op[1]->type->is_vector());
350 data.f[0] = 0;
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700351 for (unsigned c = 0; c < op[0]->type->components(); c++) {
Kenneth Graunke3f4a0b82010-07-05 21:15:32 -0700352 switch (ir->operands[0]->type->base_type) {
353 case GLSL_TYPE_UINT:
354 data.u[0] += op[0]->value.u[c] * op[1]->value.u[c];
355 break;
356 case GLSL_TYPE_INT:
357 data.i[0] += op[0]->value.i[c] * op[1]->value.i[c];
358 break;
359 case GLSL_TYPE_FLOAT:
360 data.f[0] += op[0]->value.f[c] * op[1]->value.f[c];
361 break;
362 default:
363 assert(0);
364 }
365 }
366
367 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000368 case ir_binop_add:
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700369 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
370 for (unsigned c = 0, c0 = 0, c1 = 0;
371 c < components;
372 c0 += c0_inc, c1 += c1_inc, c++) {
373
374 switch (ir->operands[0]->type->base_type) {
375 case GLSL_TYPE_UINT:
376 data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1];
377 break;
378 case GLSL_TYPE_INT:
379 data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1];
380 break;
381 case GLSL_TYPE_FLOAT:
382 data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1];
383 break;
384 default:
385 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000386 }
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700387 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700388
Eric Anholtd251b922010-04-01 18:35:42 -1000389 break;
390 case ir_binop_sub:
Kenneth Graunke97b44f02010-07-06 02:53:29 -0700391 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
392 for (unsigned c = 0, c0 = 0, c1 = 0;
393 c < components;
394 c0 += c0_inc, c1 += c1_inc, c++) {
395
396 switch (ir->operands[0]->type->base_type) {
397 case GLSL_TYPE_UINT:
398 data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1];
399 break;
400 case GLSL_TYPE_INT:
401 data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1];
402 break;
403 case GLSL_TYPE_FLOAT:
404 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1];
405 break;
406 default:
407 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000408 }
Kenneth Graunke97b44f02010-07-06 02:53:29 -0700409 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700410
Eric Anholtd251b922010-04-01 18:35:42 -1000411 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000412 case ir_binop_mul:
Kenneth Graunkecf80a4d2010-07-05 23:19:56 -0700413 /* Check for equal types, or unequal types involving scalars */
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700414 if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix())
415 || op0_scalar || op1_scalar) {
416 for (unsigned c = 0, c0 = 0, c1 = 0;
417 c < components;
418 c0 += c0_inc, c1 += c1_inc, c++) {
419
Eric Anholtd98da972010-04-01 18:25:11 -1000420 switch (ir->operands[0]->type->base_type) {
421 case GLSL_TYPE_UINT:
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700422 data.u[c] = op[0]->value.u[c0] * op[1]->value.u[c1];
Eric Anholtd98da972010-04-01 18:25:11 -1000423 break;
424 case GLSL_TYPE_INT:
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700425 data.i[c] = op[0]->value.i[c0] * op[1]->value.i[c1];
Eric Anholtd98da972010-04-01 18:25:11 -1000426 break;
427 case GLSL_TYPE_FLOAT:
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700428 data.f[c] = op[0]->value.f[c0] * op[1]->value.f[c1];
Eric Anholtd98da972010-04-01 18:25:11 -1000429 break;
430 default:
431 assert(0);
432 }
Eric Anholta576f9d2010-03-31 16:25:12 -1000433 }
Kenneth Graunkecf80a4d2010-07-05 23:19:56 -0700434 } else {
435 assert(op[0]->type->is_matrix() || op[1]->type->is_matrix());
436
437 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
438 * matrix can be a GLSL vector, either N or P can be 1.
439 *
440 * For vec*mat, the vector is treated as a row vector. This
441 * means the vector is a 1-row x M-column matrix.
442 *
443 * For mat*vec, the vector is treated as a column vector. Since
444 * matrix_columns is 1 for vectors, this just works.
445 */
446 const unsigned n = op[0]->type->is_vector()
447 ? 1 : op[0]->type->vector_elements;
448 const unsigned m = op[1]->type->vector_elements;
449 const unsigned p = op[1]->type->matrix_columns;
450 for (unsigned j = 0; j < p; j++) {
451 for (unsigned i = 0; i < n; i++) {
452 for (unsigned k = 0; k < m; k++) {
453 data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j];
454 }
455 }
456 }
457 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700458
Eric Anholta576f9d2010-03-31 16:25:12 -1000459 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000460 case ir_binop_div:
Kenneth Graunkedad35eb2010-07-06 02:56:36 -0700461 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
462 for (unsigned c = 0, c0 = 0, c1 = 0;
463 c < components;
464 c0 += c0_inc, c1 += c1_inc, c++) {
465
466 switch (ir->operands[0]->type->base_type) {
467 case GLSL_TYPE_UINT:
468 data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1];
469 break;
470 case GLSL_TYPE_INT:
471 data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1];
472 break;
473 case GLSL_TYPE_FLOAT:
474 data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1];
475 break;
476 default:
477 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000478 }
Kenneth Graunkedad35eb2010-07-06 02:56:36 -0700479 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700480
Eric Anholtd251b922010-04-01 18:35:42 -1000481 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000482 case ir_binop_logic_and:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700483 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700484 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700485 data.b[c] = op[0]->value.b[c] && op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000486 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000487 case ir_binop_logic_xor:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700488 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700489 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700490 data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
Eric Anholtd251b922010-04-01 18:35:42 -1000491 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000492 case ir_binop_logic_or:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700493 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700494 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700495 data.b[c] = op[0]->value.b[c] || op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000496 break;
Eric Anholt85171c22010-04-06 09:55:45 -0700497
498 case ir_binop_less:
Eric Anholt85171c22010-04-06 09:55:45 -0700499 switch (ir->operands[0]->type->base_type) {
500 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700501 data.b[0] = op[0]->value.u[0] < op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700502 break;
503 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700504 data.b[0] = op[0]->value.i[0] < op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700505 break;
506 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700507 data.b[0] = op[0]->value.f[0] < op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700508 break;
509 default:
510 assert(0);
511 }
512 break;
513 case ir_binop_greater:
Eric Anholt85171c22010-04-06 09:55:45 -0700514 switch (ir->operands[0]->type->base_type) {
515 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700516 data.b[0] = op[0]->value.u[0] > op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700517 break;
518 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700519 data.b[0] = op[0]->value.i[0] > op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700520 break;
521 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700522 data.b[0] = op[0]->value.f[0] > op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700523 break;
524 default:
525 assert(0);
526 }
527 break;
528 case ir_binop_lequal:
Eric Anholt85171c22010-04-06 09:55:45 -0700529 switch (ir->operands[0]->type->base_type) {
530 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700531 data.b[0] = op[0]->value.u[0] <= op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700532 break;
533 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700534 data.b[0] = op[0]->value.i[0] <= op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700535 break;
536 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700537 data.b[0] = op[0]->value.f[0] <= op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700538 break;
539 default:
540 assert(0);
541 }
542 break;
543 case ir_binop_gequal:
Eric Anholt85171c22010-04-06 09:55:45 -0700544 switch (ir->operands[0]->type->base_type) {
545 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700546 data.b[0] = op[0]->value.u[0] >= op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700547 break;
548 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700549 data.b[0] = op[0]->value.i[0] >= op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700550 break;
551 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700552 data.b[0] = op[0]->value.f[0] >= op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700553 break;
554 default:
555 assert(0);
556 }
557 break;
558
Eric Anholtec1949e2010-04-06 10:02:27 -0700559 case ir_binop_equal:
Ian Romanick083d75a2010-06-11 16:20:43 -0700560 data.b[0] = true;
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700561 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick083d75a2010-06-11 16:20:43 -0700562 switch (ir->operands[0]->type->base_type) {
563 case GLSL_TYPE_UINT:
564 data.b[0] = data.b[0] && op[0]->value.u[c] == op[1]->value.u[c];
565 break;
566 case GLSL_TYPE_INT:
567 data.b[0] = data.b[0] && op[0]->value.i[c] == op[1]->value.i[c];
568 break;
569 case GLSL_TYPE_FLOAT:
570 data.b[0] = data.b[0] && op[0]->value.f[c] == op[1]->value.f[c];
571 break;
572 case GLSL_TYPE_BOOL:
573 data.b[0] = data.b[0] && op[0]->value.b[c] == op[1]->value.b[c];
574 break;
575 default:
576 assert(0);
Eric Anholtec1949e2010-04-06 10:02:27 -0700577 }
578 }
579 break;
580 case ir_binop_nequal:
Ian Romanick083d75a2010-06-11 16:20:43 -0700581 data.b[0] = false;
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700582 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick083d75a2010-06-11 16:20:43 -0700583 switch (ir->operands[0]->type->base_type) {
584 case GLSL_TYPE_UINT:
585 data.b[0] = data.b[0] || op[0]->value.u[c] != op[1]->value.u[c];
586 break;
587 case GLSL_TYPE_INT:
588 data.b[0] = data.b[0] || op[0]->value.i[c] != op[1]->value.i[c];
589 break;
590 case GLSL_TYPE_FLOAT:
591 data.b[0] = data.b[0] || op[0]->value.f[c] != op[1]->value.f[c];
592 break;
593 case GLSL_TYPE_BOOL:
594 data.b[0] = data.b[0] || op[0]->value.b[c] != op[1]->value.b[c];
595 break;
596 default:
597 assert(0);
Eric Anholtec1949e2010-04-06 10:02:27 -0700598 }
599 }
600 break;
601
Eric Anholta576f9d2010-03-31 16:25:12 -1000602 default:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700603 /* FINISHME: Should handle all expression types. */
604 return;
Eric Anholta576f9d2010-03-31 16:25:12 -1000605 }
Eric Anholtd98da972010-04-01 18:25:11 -1000606
Eric Anholt6b01b502010-06-24 15:18:39 -0700607 void *ctx = talloc_parent(ir);
Carl Worth1660a292010-06-23 18:11:51 -0700608 this->value = new(ctx) ir_constant(ir->type, &data);
Ian Romanick1cf43a42010-03-30 16:56:50 -0700609}
610
611
612void
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700613ir_constant_visitor::visit(ir_texture *ir)
614{
615 // FINISHME: Do stuff with texture lookups
616 (void) ir;
617 value = NULL;
618}
619
620
621void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700622ir_constant_visitor::visit(ir_swizzle *ir)
623{
Ian Romanickc2ba6192010-06-11 12:30:28 -0700624 ir_constant *v = ir->val->constant_expression_value();
625
626 this->value = NULL;
627
628 if (v != NULL) {
Ian Romanick0bb70a32010-06-11 15:49:49 -0700629 ir_constant_data data;
Ian Romanickc2ba6192010-06-11 12:30:28 -0700630
631 const unsigned swiz_idx[4] = {
632 ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w
633 };
634
635 for (unsigned i = 0; i < ir->mask.num_components; i++) {
636 switch (v->type->base_type) {
637 case GLSL_TYPE_UINT:
638 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
639 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
640 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
641 default: assert(!"Should not get here."); break;
642 }
643 }
644
Eric Anholt6b01b502010-06-24 15:18:39 -0700645 void *ctx = talloc_parent(ir);
Carl Worth1660a292010-06-23 18:11:51 -0700646 this->value = new(ctx) ir_constant(ir->type, &data);
Ian Romanickc2ba6192010-06-11 12:30:28 -0700647 }
Ian Romanick1cf43a42010-03-30 16:56:50 -0700648}
649
650
651void
Ian Romanickc7b10462010-05-19 13:20:12 +0200652ir_constant_visitor::visit(ir_dereference_variable *ir)
Ian Romanick1cf43a42010-03-30 16:56:50 -0700653{
Ian Romanick1cf43a42010-03-30 16:56:50 -0700654 value = NULL;
Eric Anholt326c6762010-04-06 10:30:54 -0700655
Ian Romanickc7b10462010-05-19 13:20:12 +0200656 ir_variable *var = ir->variable_referenced();
657 if (var && var->constant_value)
Ian Romanickca088cc2010-07-06 17:41:02 -0700658 value = var->constant_value->clone(NULL);
Ian Romanickc7b10462010-05-19 13:20:12 +0200659}
660
661
662void
663ir_constant_visitor::visit(ir_dereference_array *ir)
664{
Carl Worth1660a292010-06-23 18:11:51 -0700665 void *ctx = talloc_parent(ir);
Ian Romanick9b92af92010-06-11 12:20:12 -0700666 ir_constant *array = ir->array->constant_expression_value();
667 ir_constant *idx = ir->array_index->constant_expression_value();
668
669 this->value = NULL;
670
671 if ((array != NULL) && (idx != NULL)) {
672 if (array->type->is_matrix()) {
673 /* Array access of a matrix results in a vector.
674 */
675 const unsigned column = idx->value.u[0];
676
677 const glsl_type *const column_type = array->type->column_type();
678
679 /* Offset in the constant matrix to the first element of the column
680 * to be extracted.
681 */
682 const unsigned mat_idx = column * column_type->vector_elements;
683
Ian Romanick0bb70a32010-06-11 15:49:49 -0700684 ir_constant_data data;
Ian Romanick9b92af92010-06-11 12:20:12 -0700685
686 switch (column_type->base_type) {
687 case GLSL_TYPE_UINT:
688 case GLSL_TYPE_INT:
689 for (unsigned i = 0; i < column_type->vector_elements; i++)
690 data.u[i] = array->value.u[mat_idx + i];
691
692 break;
693
694 case GLSL_TYPE_FLOAT:
695 for (unsigned i = 0; i < column_type->vector_elements; i++)
696 data.f[i] = array->value.f[mat_idx + i];
697
698 break;
699
700 default:
701 assert(!"Should not get here.");
702 break;
703 }
704
Carl Worth1660a292010-06-23 18:11:51 -0700705 this->value = new(ctx) ir_constant(column_type, &data);
Ian Romanick9b92af92010-06-11 12:20:12 -0700706 } else if (array->type->is_vector()) {
707 const unsigned component = idx->value.u[0];
708
Carl Worth1660a292010-06-23 18:11:51 -0700709 this->value = new(ctx) ir_constant(array, component);
Ian Romanick9b92af92010-06-11 12:20:12 -0700710 } else {
711 /* FINISHME: Handle access of constant arrays. */
712 }
713 }
Ian Romanickc7b10462010-05-19 13:20:12 +0200714}
715
716
717void
718ir_constant_visitor::visit(ir_dereference_record *ir)
719{
Ian Romanick253dede2010-06-09 17:30:19 -0700720 ir_constant *v = ir->record->constant_expression_value();
721
722 this->value = (v != NULL) ? v->get_record_field(ir->field) : NULL;
Ian Romanick1cf43a42010-03-30 16:56:50 -0700723}
724
725
726void
727ir_constant_visitor::visit(ir_assignment *ir)
728{
729 (void) ir;
730 value = NULL;
731}
732
733
734void
735ir_constant_visitor::visit(ir_constant *ir)
736{
737 value = ir;
738}
739
740
741void
742ir_constant_visitor::visit(ir_call *ir)
743{
744 (void) ir;
745 value = NULL;
746}
747
748
749void
750ir_constant_visitor::visit(ir_return *ir)
751{
752 (void) ir;
753 value = NULL;
754}
755
756
757void
Kenneth Graunke16efab12010-06-30 10:47:34 -0700758ir_constant_visitor::visit(ir_discard *ir)
759{
760 (void) ir;
761 value = NULL;
762}
763
764
765void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700766ir_constant_visitor::visit(ir_if *ir)
767{
768 (void) ir;
769 value = NULL;
770}
Ian Romanickfad607a2010-04-05 16:16:07 -0700771
772
773void
774ir_constant_visitor::visit(ir_loop *ir)
775{
776 (void) ir;
777 value = NULL;
778}
Ian Romanickf8e31e02010-04-05 16:28:15 -0700779
780
781void
782ir_constant_visitor::visit(ir_loop_jump *ir)
783{
784 (void) ir;
785 value = NULL;
786}