Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 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 | #define NULL 0 |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 37 | #include <math.h> |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 38 | #include "ir.h" |
| 39 | #include "ir_visitor.h" |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 40 | #include "glsl_types.h" |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Visitor class for evaluating constant expressions |
| 44 | */ |
| 45 | class ir_constant_visitor : public ir_visitor { |
| 46 | public: |
| 47 | ir_constant_visitor() |
| 48 | : value(NULL) |
| 49 | { |
| 50 | /* empty */ |
| 51 | } |
| 52 | |
| 53 | virtual ~ir_constant_visitor() |
| 54 | { |
| 55 | /* empty */ |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * \name Visit methods |
| 60 | * |
| 61 | * As typical for the visitor pattern, there must be one \c visit method for |
| 62 | * each concrete subclass of \c ir_instruction. Virtual base classes within |
| 63 | * the hierarchy should not have \c visit methods. |
| 64 | */ |
| 65 | /*@{*/ |
| 66 | virtual void visit(ir_variable *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 67 | virtual void visit(ir_function_signature *); |
| 68 | virtual void visit(ir_function *); |
| 69 | virtual void visit(ir_expression *); |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 70 | virtual void visit(ir_texture *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 71 | virtual void visit(ir_swizzle *); |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 72 | virtual void visit(ir_dereference_variable *); |
| 73 | virtual void visit(ir_dereference_array *); |
| 74 | virtual void visit(ir_dereference_record *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 75 | virtual void visit(ir_assignment *); |
| 76 | virtual void visit(ir_constant *); |
| 77 | virtual void visit(ir_call *); |
| 78 | virtual void visit(ir_return *); |
| 79 | virtual void visit(ir_if *); |
Ian Romanick | fad607a | 2010-04-05 16:16:07 -0700 | [diff] [blame] | 80 | virtual void visit(ir_loop *); |
Ian Romanick | f8e31e0 | 2010-04-05 16:28:15 -0700 | [diff] [blame] | 81 | virtual void visit(ir_loop_jump *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 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 |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 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 | |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 128 | void |
| 129 | ir_constant_visitor::visit(ir_expression *ir) |
| 130 | { |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 131 | value = NULL; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 132 | ir_constant *op[2]; |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 133 | unsigned int operand, c; |
| 134 | unsigned u[16]; |
| 135 | int i[16]; |
| 136 | float f[16]; |
| 137 | bool b[16]; |
| 138 | const glsl_type *type = NULL; |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame] | 139 | |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 140 | for (operand = 0; operand < ir->get_num_operands(); operand++) { |
| 141 | op[operand] = ir->operands[operand]->constant_expression_value(); |
| 142 | if (!op[operand]) |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame] | 143 | return; |
| 144 | } |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 145 | |
| 146 | switch (ir->operation) { |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 147 | case ir_unop_logic_not: |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 148 | type = ir->operands[0]->type; |
| 149 | assert(type->base_type == GLSL_TYPE_BOOL); |
| 150 | for (c = 0; c < ir->operands[0]->type->components(); c++) |
| 151 | b[c] = !op[0]->value.b[c]; |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 152 | break; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 153 | |
| 154 | case ir_unop_f2i: |
| 155 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 156 | type = ir->type; |
| 157 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 158 | i[c] = op[0]->value.f[c]; |
| 159 | } |
| 160 | break; |
| 161 | case ir_unop_i2f: |
| 162 | assert(op[0]->type->base_type == GLSL_TYPE_UINT || |
| 163 | op[0]->type->base_type == GLSL_TYPE_INT); |
| 164 | type = ir->type; |
| 165 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 166 | if (op[0]->type->base_type == GLSL_TYPE_INT) |
| 167 | f[c] = op[0]->value.i[c]; |
| 168 | else |
| 169 | f[c] = op[0]->value.u[c]; |
| 170 | } |
| 171 | break; |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 172 | case ir_unop_b2f: |
| 173 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
| 174 | type = ir->type; |
| 175 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 176 | f[c] = op[0]->value.b[c] ? 1.0 : 0.0; |
| 177 | } |
| 178 | break; |
| 179 | case ir_unop_f2b: |
| 180 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 181 | type = ir->type; |
| 182 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 183 | b[c] = bool(op[0]->value.f[c]); |
| 184 | } |
| 185 | break; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 186 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 187 | case ir_unop_neg: |
| 188 | type = ir->type; |
| 189 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 190 | switch (type->base_type) { |
| 191 | case GLSL_TYPE_UINT: |
| 192 | u[c] = -op[0]->value.u[c]; |
| 193 | break; |
| 194 | case GLSL_TYPE_INT: |
| 195 | i[c] = -op[0]->value.i[c]; |
| 196 | break; |
| 197 | case GLSL_TYPE_FLOAT: |
| 198 | f[c] = -op[0]->value.f[c]; |
| 199 | break; |
| 200 | default: |
| 201 | assert(0); |
| 202 | } |
| 203 | } |
| 204 | break; |
| 205 | |
| 206 | case ir_unop_abs: |
| 207 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 208 | type = ir->type; |
| 209 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 210 | switch (type->base_type) { |
| 211 | case GLSL_TYPE_UINT: |
| 212 | u[c] = op[0]->value.u[c]; |
| 213 | break; |
| 214 | case GLSL_TYPE_INT: |
| 215 | i[c] = op[0]->value.i[c]; |
| 216 | if (i[c] < 0) |
| 217 | i[c] = -i[c]; |
| 218 | break; |
| 219 | case GLSL_TYPE_FLOAT: |
| 220 | f[c] = fabs(op[0]->value.f[c]); |
| 221 | break; |
| 222 | default: |
| 223 | assert(0); |
| 224 | } |
| 225 | } |
| 226 | break; |
| 227 | |
| 228 | case ir_unop_rcp: |
| 229 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 230 | type = ir->type; |
| 231 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 232 | switch (type->base_type) { |
| 233 | case GLSL_TYPE_UINT: |
| 234 | if (op[0]->value.u[c] != 0.0) |
| 235 | u[c] = 1 / op[0]->value.u[c]; |
| 236 | break; |
| 237 | case GLSL_TYPE_INT: |
| 238 | if (op[0]->value.i[c] != 0.0) |
| 239 | i[c] = 1 / op[0]->value.i[c]; |
| 240 | break; |
| 241 | case GLSL_TYPE_FLOAT: |
| 242 | if (op[0]->value.f[c] != 0.0) |
| 243 | f[c] = 1.0 / op[0]->value.f[c]; |
| 244 | break; |
| 245 | default: |
| 246 | assert(0); |
| 247 | } |
| 248 | } |
| 249 | break; |
| 250 | |
| 251 | case ir_unop_rsq: |
| 252 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 253 | type = ir->type; |
| 254 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 255 | f[c] = 1.0 / sqrtf(op[0]->value.f[c]); |
| 256 | } |
| 257 | break; |
| 258 | |
| 259 | case ir_unop_sqrt: |
| 260 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 261 | type = ir->type; |
| 262 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 263 | f[c] = sqrtf(op[0]->value.f[c]); |
| 264 | } |
| 265 | break; |
| 266 | |
| 267 | case ir_unop_exp: |
| 268 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 269 | type = ir->type; |
| 270 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 271 | f[c] = expf(op[0]->value.f[c]); |
| 272 | } |
| 273 | break; |
| 274 | |
| 275 | case ir_unop_log: |
| 276 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 277 | type = ir->type; |
| 278 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 279 | f[c] = logf(op[0]->value.f[c]); |
| 280 | } |
| 281 | break; |
| 282 | |
Kenneth Graunke | d6a32d4 | 2010-06-09 15:22:35 -0700 | [diff] [blame] | 283 | case ir_unop_dFdx: |
| 284 | case ir_unop_dFdy: |
| 285 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 286 | type = ir->type; |
| 287 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 288 | f[c] = 0.0; |
| 289 | } |
| 290 | break; |
| 291 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 292 | case ir_binop_add: |
| 293 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 294 | type = ir->operands[0]->type; |
| 295 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 296 | switch (ir->operands[0]->type->base_type) { |
| 297 | case GLSL_TYPE_UINT: |
| 298 | u[c] = op[0]->value.u[c] + op[1]->value.u[c]; |
| 299 | break; |
| 300 | case GLSL_TYPE_INT: |
| 301 | i[c] = op[0]->value.i[c] + op[1]->value.i[c]; |
| 302 | break; |
| 303 | case GLSL_TYPE_FLOAT: |
| 304 | f[c] = op[0]->value.f[c] + op[1]->value.f[c]; |
| 305 | break; |
| 306 | default: |
| 307 | assert(0); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | break; |
| 312 | case ir_binop_sub: |
| 313 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 314 | type = ir->operands[0]->type; |
| 315 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 316 | switch (ir->operands[0]->type->base_type) { |
| 317 | case GLSL_TYPE_UINT: |
| 318 | u[c] = op[0]->value.u[c] - op[1]->value.u[c]; |
| 319 | break; |
| 320 | case GLSL_TYPE_INT: |
| 321 | i[c] = op[0]->value.i[c] - op[1]->value.i[c]; |
| 322 | break; |
| 323 | case GLSL_TYPE_FLOAT: |
| 324 | f[c] = op[0]->value.f[c] - op[1]->value.f[c]; |
| 325 | break; |
| 326 | default: |
| 327 | assert(0); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 332 | case ir_binop_mul: |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 333 | if (ir->operands[0]->type == ir->operands[1]->type && |
| 334 | !ir->operands[0]->type->is_matrix()) { |
| 335 | type = ir->operands[0]->type; |
| 336 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 337 | switch (ir->operands[0]->type->base_type) { |
| 338 | case GLSL_TYPE_UINT: |
| 339 | u[c] = op[0]->value.u[c] * op[1]->value.u[c]; |
| 340 | break; |
| 341 | case GLSL_TYPE_INT: |
| 342 | i[c] = op[0]->value.i[c] * op[1]->value.i[c]; |
| 343 | break; |
| 344 | case GLSL_TYPE_FLOAT: |
| 345 | f[c] = op[0]->value.f[c] * op[1]->value.f[c]; |
| 346 | break; |
| 347 | default: |
| 348 | assert(0); |
| 349 | } |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 353 | case ir_binop_div: |
| 354 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 355 | type = ir->operands[0]->type; |
| 356 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 357 | switch (ir->operands[0]->type->base_type) { |
| 358 | case GLSL_TYPE_UINT: |
| 359 | u[c] = op[0]->value.u[c] / op[1]->value.u[c]; |
| 360 | break; |
| 361 | case GLSL_TYPE_INT: |
| 362 | i[c] = op[0]->value.i[c] / op[1]->value.i[c]; |
| 363 | break; |
| 364 | case GLSL_TYPE_FLOAT: |
| 365 | f[c] = op[0]->value.f[c] / op[1]->value.f[c]; |
| 366 | break; |
| 367 | default: |
| 368 | assert(0); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 373 | case ir_binop_logic_and: |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 374 | type = ir->operands[0]->type; |
| 375 | assert(type->base_type == GLSL_TYPE_BOOL); |
| 376 | for (c = 0; c < ir->operands[0]->type->components(); c++) |
| 377 | b[c] = op[0]->value.b[c] && op[1]->value.b[c]; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 378 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 379 | case ir_binop_logic_xor: |
| 380 | type = ir->operands[0]->type; |
| 381 | assert(type->base_type == GLSL_TYPE_BOOL); |
| 382 | for (c = 0; c < ir->operands[0]->type->components(); c++) |
| 383 | b[c] = op[0]->value.b[c] ^ op[1]->value.b[c]; |
| 384 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 385 | case ir_binop_logic_or: |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 386 | type = ir->operands[0]->type; |
| 387 | assert(type->base_type == GLSL_TYPE_BOOL); |
| 388 | for (c = 0; c < ir->operands[0]->type->components(); c++) |
| 389 | b[c] = op[0]->value.b[c] || op[1]->value.b[c]; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 390 | break; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 391 | |
| 392 | case ir_binop_less: |
| 393 | type = glsl_type::bool_type; |
| 394 | switch (ir->operands[0]->type->base_type) { |
| 395 | case GLSL_TYPE_UINT: |
| 396 | b[0] = op[0]->value.u[0] < op[1]->value.u[0]; |
| 397 | break; |
| 398 | case GLSL_TYPE_INT: |
| 399 | b[0] = op[0]->value.i[0] < op[1]->value.i[0]; |
| 400 | break; |
| 401 | case GLSL_TYPE_FLOAT: |
| 402 | b[0] = op[0]->value.f[0] < op[1]->value.f[0]; |
| 403 | break; |
| 404 | default: |
| 405 | assert(0); |
| 406 | } |
| 407 | break; |
| 408 | case ir_binop_greater: |
| 409 | type = glsl_type::bool_type; |
| 410 | switch (ir->operands[0]->type->base_type) { |
| 411 | case GLSL_TYPE_UINT: |
| 412 | b[0] = op[0]->value.u[0] > op[1]->value.u[0]; |
| 413 | break; |
| 414 | case GLSL_TYPE_INT: |
| 415 | b[0] = op[0]->value.i[0] > op[1]->value.i[0]; |
| 416 | break; |
| 417 | case GLSL_TYPE_FLOAT: |
| 418 | b[0] = op[0]->value.f[0] > op[1]->value.f[0]; |
| 419 | break; |
| 420 | default: |
| 421 | assert(0); |
| 422 | } |
| 423 | break; |
| 424 | case ir_binop_lequal: |
| 425 | type = glsl_type::bool_type; |
| 426 | switch (ir->operands[0]->type->base_type) { |
| 427 | case GLSL_TYPE_UINT: |
| 428 | b[0] = op[0]->value.u[0] <= op[1]->value.u[0]; |
| 429 | break; |
| 430 | case GLSL_TYPE_INT: |
| 431 | b[0] = op[0]->value.i[0] <= op[1]->value.i[0]; |
| 432 | break; |
| 433 | case GLSL_TYPE_FLOAT: |
| 434 | b[0] = op[0]->value.f[0] <= op[1]->value.f[0]; |
| 435 | break; |
| 436 | default: |
| 437 | assert(0); |
| 438 | } |
| 439 | break; |
| 440 | case ir_binop_gequal: |
| 441 | type = glsl_type::bool_type; |
| 442 | switch (ir->operands[0]->type->base_type) { |
| 443 | case GLSL_TYPE_UINT: |
| 444 | b[0] = op[0]->value.u[0] >= op[1]->value.u[0]; |
| 445 | break; |
| 446 | case GLSL_TYPE_INT: |
| 447 | b[0] = op[0]->value.i[0] >= op[1]->value.i[0]; |
| 448 | break; |
| 449 | case GLSL_TYPE_FLOAT: |
| 450 | b[0] = op[0]->value.f[0] >= op[1]->value.f[0]; |
| 451 | break; |
| 452 | default: |
| 453 | assert(0); |
| 454 | } |
| 455 | break; |
| 456 | |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 457 | case ir_binop_equal: |
| 458 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 459 | type = glsl_type::bool_type; |
| 460 | b[0] = true; |
| 461 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 462 | switch (ir->operands[0]->type->base_type) { |
| 463 | case GLSL_TYPE_UINT: |
| 464 | b[0] = b[0] && op[0]->value.u[c] == op[1]->value.u[c]; |
| 465 | break; |
| 466 | case GLSL_TYPE_INT: |
| 467 | b[0] = b[0] && op[0]->value.i[c] == op[1]->value.i[c]; |
| 468 | break; |
| 469 | case GLSL_TYPE_FLOAT: |
| 470 | b[0] = b[0] && op[0]->value.f[c] == op[1]->value.f[c]; |
| 471 | break; |
Ian Romanick | 77cce64 | 2010-04-07 16:48:42 -0700 | [diff] [blame] | 472 | case GLSL_TYPE_BOOL: |
| 473 | b[0] = b[0] && op[0]->value.b[c] == op[1]->value.b[c]; |
| 474 | break; |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 475 | default: |
| 476 | assert(0); |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | break; |
| 481 | case ir_binop_nequal: |
| 482 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 483 | type = glsl_type::bool_type; |
| 484 | b[0] = false; |
| 485 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 486 | switch (ir->operands[0]->type->base_type) { |
| 487 | case GLSL_TYPE_UINT: |
| 488 | b[0] = b[0] || op[0]->value.u[c] != op[1]->value.u[c]; |
| 489 | break; |
| 490 | case GLSL_TYPE_INT: |
| 491 | b[0] = b[0] || op[0]->value.i[c] != op[1]->value.i[c]; |
| 492 | break; |
| 493 | case GLSL_TYPE_FLOAT: |
| 494 | b[0] = b[0] || op[0]->value.f[c] != op[1]->value.f[c]; |
| 495 | break; |
Ian Romanick | 77cce64 | 2010-04-07 16:48:42 -0700 | [diff] [blame] | 496 | case GLSL_TYPE_BOOL: |
| 497 | b[0] = b[0] || op[0]->value.b[c] != op[1]->value.b[c]; |
| 498 | break; |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 499 | default: |
| 500 | assert(0); |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | break; |
| 505 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 506 | default: |
| 507 | break; |
| 508 | } |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 509 | |
| 510 | if (type) { |
| 511 | switch (type->base_type) { |
| 512 | case GLSL_TYPE_UINT: |
| 513 | value = new ir_constant(type, u); |
| 514 | break; |
| 515 | case GLSL_TYPE_INT: |
| 516 | value = new ir_constant(type, i); |
| 517 | break; |
| 518 | case GLSL_TYPE_FLOAT: |
| 519 | value = new ir_constant(type, f); |
| 520 | break; |
| 521 | case GLSL_TYPE_BOOL: |
| 522 | value = new ir_constant(type, b); |
| 523 | break; |
| 524 | } |
| 525 | } |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | |
| 529 | void |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 530 | ir_constant_visitor::visit(ir_texture *ir) |
| 531 | { |
| 532 | // FINISHME: Do stuff with texture lookups |
| 533 | (void) ir; |
| 534 | value = NULL; |
| 535 | } |
| 536 | |
| 537 | |
| 538 | void |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 539 | ir_constant_visitor::visit(ir_swizzle *ir) |
| 540 | { |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame^] | 541 | ir_constant *v = ir->val->constant_expression_value(); |
| 542 | |
| 543 | this->value = NULL; |
| 544 | |
| 545 | if (v != NULL) { |
| 546 | union { |
| 547 | float f[4]; |
| 548 | unsigned u[4]; |
| 549 | bool b[4]; |
| 550 | } data; |
| 551 | |
| 552 | const unsigned swiz_idx[4] = { |
| 553 | ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w |
| 554 | }; |
| 555 | |
| 556 | for (unsigned i = 0; i < ir->mask.num_components; i++) { |
| 557 | switch (v->type->base_type) { |
| 558 | case GLSL_TYPE_UINT: |
| 559 | case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break; |
| 560 | case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break; |
| 561 | case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break; |
| 562 | default: assert(!"Should not get here."); break; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | this->value = new ir_constant(ir->type, &data); |
| 567 | } |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | |
| 571 | void |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 572 | ir_constant_visitor::visit(ir_dereference_variable *ir) |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 573 | { |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 574 | value = NULL; |
Eric Anholt | 326c676 | 2010-04-06 10:30:54 -0700 | [diff] [blame] | 575 | |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 576 | ir_variable *var = ir->variable_referenced(); |
| 577 | if (var && var->constant_value) |
Ian Romanick | d4b33ed | 2010-06-09 17:19:10 -0700 | [diff] [blame] | 578 | value = var->constant_value->clone(); |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | |
| 582 | void |
| 583 | ir_constant_visitor::visit(ir_dereference_array *ir) |
| 584 | { |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 585 | ir_constant *array = ir->array->constant_expression_value(); |
| 586 | ir_constant *idx = ir->array_index->constant_expression_value(); |
| 587 | |
| 588 | this->value = NULL; |
| 589 | |
| 590 | if ((array != NULL) && (idx != NULL)) { |
| 591 | if (array->type->is_matrix()) { |
| 592 | /* Array access of a matrix results in a vector. |
| 593 | */ |
| 594 | const unsigned column = idx->value.u[0]; |
| 595 | |
| 596 | const glsl_type *const column_type = array->type->column_type(); |
| 597 | |
| 598 | /* Offset in the constant matrix to the first element of the column |
| 599 | * to be extracted. |
| 600 | */ |
| 601 | const unsigned mat_idx = column * column_type->vector_elements; |
| 602 | |
| 603 | union { |
| 604 | unsigned u[4]; |
| 605 | float f[4]; |
| 606 | } data; |
| 607 | |
| 608 | switch (column_type->base_type) { |
| 609 | case GLSL_TYPE_UINT: |
| 610 | case GLSL_TYPE_INT: |
| 611 | for (unsigned i = 0; i < column_type->vector_elements; i++) |
| 612 | data.u[i] = array->value.u[mat_idx + i]; |
| 613 | |
| 614 | break; |
| 615 | |
| 616 | case GLSL_TYPE_FLOAT: |
| 617 | for (unsigned i = 0; i < column_type->vector_elements; i++) |
| 618 | data.f[i] = array->value.f[mat_idx + i]; |
| 619 | |
| 620 | break; |
| 621 | |
| 622 | default: |
| 623 | assert(!"Should not get here."); |
| 624 | break; |
| 625 | } |
| 626 | |
| 627 | this->value = new ir_constant(column_type, &data); |
| 628 | } else if (array->type->is_vector()) { |
| 629 | const unsigned component = idx->value.u[0]; |
| 630 | |
| 631 | this->value = new ir_constant(array, component); |
| 632 | } else { |
| 633 | /* FINISHME: Handle access of constant arrays. */ |
| 634 | } |
| 635 | } |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | |
| 639 | void |
| 640 | ir_constant_visitor::visit(ir_dereference_record *ir) |
| 641 | { |
Ian Romanick | 253dede | 2010-06-09 17:30:19 -0700 | [diff] [blame] | 642 | ir_constant *v = ir->record->constant_expression_value(); |
| 643 | |
| 644 | this->value = (v != NULL) ? v->get_record_field(ir->field) : NULL; |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | |
| 648 | void |
| 649 | ir_constant_visitor::visit(ir_assignment *ir) |
| 650 | { |
| 651 | (void) ir; |
| 652 | value = NULL; |
| 653 | } |
| 654 | |
| 655 | |
| 656 | void |
| 657 | ir_constant_visitor::visit(ir_constant *ir) |
| 658 | { |
| 659 | value = ir; |
| 660 | } |
| 661 | |
| 662 | |
| 663 | void |
| 664 | ir_constant_visitor::visit(ir_call *ir) |
| 665 | { |
| 666 | (void) ir; |
| 667 | value = NULL; |
| 668 | } |
| 669 | |
| 670 | |
| 671 | void |
| 672 | ir_constant_visitor::visit(ir_return *ir) |
| 673 | { |
| 674 | (void) ir; |
| 675 | value = NULL; |
| 676 | } |
| 677 | |
| 678 | |
| 679 | void |
| 680 | ir_constant_visitor::visit(ir_if *ir) |
| 681 | { |
| 682 | (void) ir; |
| 683 | value = NULL; |
| 684 | } |
Ian Romanick | fad607a | 2010-04-05 16:16:07 -0700 | [diff] [blame] | 685 | |
| 686 | |
| 687 | void |
| 688 | ir_constant_visitor::visit(ir_loop *ir) |
| 689 | { |
| 690 | (void) ir; |
| 691 | value = NULL; |
| 692 | } |
Ian Romanick | f8e31e0 | 2010-04-05 16:28:15 -0700 | [diff] [blame] | 693 | |
| 694 | |
| 695 | void |
| 696 | ir_constant_visitor::visit(ir_loop_jump *ir) |
| 697 | { |
| 698 | (void) ir; |
| 699 | value = NULL; |
| 700 | } |