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 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 36 | #include <math.h> |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 37 | #include "ir.h" |
| 38 | #include "ir_visitor.h" |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 39 | #include "glsl_types.h" |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 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 *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 66 | virtual void visit(ir_function_signature *); |
| 67 | virtual void visit(ir_function *); |
| 68 | virtual void visit(ir_expression *); |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 69 | virtual void visit(ir_texture *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 70 | virtual void visit(ir_swizzle *); |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 71 | virtual void visit(ir_dereference_variable *); |
| 72 | virtual void visit(ir_dereference_array *); |
| 73 | virtual void visit(ir_dereference_record *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 74 | virtual void visit(ir_assignment *); |
| 75 | virtual void visit(ir_constant *); |
| 76 | virtual void visit(ir_call *); |
| 77 | virtual void visit(ir_return *); |
Kenneth Graunke | 16efab1 | 2010-06-30 10:47:34 -0700 | [diff] [blame] | 78 | virtual void visit(ir_discard *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 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; |
Kenneth Graunke | 6bc432e | 2010-07-02 17:12:23 -0700 | [diff] [blame] | 132 | ir_constant *op[2] = { NULL, NULL }; |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 133 | ir_constant_data data; |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame] | 134 | |
Kenneth Graunke | c63a1db | 2010-07-05 22:33:35 -0700 | [diff] [blame] | 135 | memset(&data, 0, sizeof(data)); |
| 136 | |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 137 | for (unsigned operand = 0; operand < ir->get_num_operands(); operand++) { |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 138 | op[operand] = ir->operands[operand]->constant_expression_value(); |
| 139 | if (!op[operand]) |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame] | 140 | return; |
| 141 | } |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 142 | |
Kenneth Graunke | 6fc983b | 2010-07-06 02:39:57 -0700 | [diff] [blame] | 143 | if (op[1] != NULL) |
| 144 | assert(op[0]->type->base_type == op[1]->type->base_type); |
| 145 | |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 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 | */ |
Kenneth Graunke | acf88f2 | 2010-07-07 12:08:23 -0700 | [diff] [blame] | 152 | unsigned c0_inc = op0_scalar ? 0 : 1; |
| 153 | unsigned c1_inc = op1_scalar ? 0 : 1; |
Eric Anholt | 570dc0d | 2010-07-07 09:07:09 -0700 | [diff] [blame] | 154 | unsigned components; |
| 155 | if (op1_scalar || !op[1]) { |
| 156 | components = op[0]->type->components(); |
| 157 | } else { |
| 158 | components = op[1]->type->components(); |
| 159 | } |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 160 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 161 | switch (ir->operation) { |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 162 | case ir_unop_logic_not: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 163 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 164 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 165 | data.b[c] = !op[0]->value.b[c]; |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 166 | break; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 167 | |
| 168 | case ir_unop_f2i: |
| 169 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 170 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 171 | data.i[c] = op[0]->value.f[c]; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 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); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 177 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 178 | if (op[0]->type->base_type == GLSL_TYPE_INT) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 179 | data.f[c] = op[0]->value.i[c]; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 180 | else |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 181 | data.f[c] = op[0]->value.u[c]; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 182 | } |
| 183 | break; |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 184 | case ir_unop_b2f: |
| 185 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 186 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 187 | data.f[c] = op[0]->value.b[c] ? 1.0 : 0.0; |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 188 | } |
| 189 | break; |
| 190 | case ir_unop_f2b: |
| 191 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 192 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 193 | data.b[c] = bool(op[0]->value.f[c]); |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 194 | } |
| 195 | break; |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 196 | case ir_unop_b2i: |
| 197 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 198 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 199 | data.u[c] = op[0]->value.b[c] ? 1 : 0; |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 200 | } |
| 201 | break; |
| 202 | case ir_unop_i2b: |
| 203 | assert(op[0]->type->is_integer()); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 204 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 205 | data.b[c] = bool(op[0]->value.u[c]); |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 206 | } |
| 207 | break; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 208 | |
Kenneth Graunke | 323d909 | 2010-07-08 23:21:36 -0700 | [diff] [blame] | 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 | |
Kenneth Graunke | c1ee30a | 2010-07-08 23:22:36 -0700 | [diff] [blame^] | 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 | |
Eric Anholt | d925c91 | 2010-07-01 10:37:11 -0700 | [diff] [blame] | 223 | case ir_unop_fract: |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 224 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Eric Anholt | d925c91 | 2010-07-01 10:37:11 -0700 | [diff] [blame] | 225 | switch (ir->type->base_type) { |
| 226 | case GLSL_TYPE_UINT: |
| 227 | data.u[c] = 0; |
| 228 | break; |
| 229 | case GLSL_TYPE_INT: |
| 230 | data.i[c] = 0; |
| 231 | break; |
| 232 | case GLSL_TYPE_FLOAT: |
| 233 | data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]); |
| 234 | break; |
| 235 | default: |
| 236 | assert(0); |
| 237 | } |
| 238 | } |
| 239 | break; |
| 240 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 241 | case ir_unop_neg: |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 242 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 243 | switch (ir->type->base_type) { |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 244 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 245 | data.u[c] = -op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 246 | break; |
| 247 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 248 | data.i[c] = -op[0]->value.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 249 | break; |
| 250 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 251 | data.f[c] = -op[0]->value.f[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 252 | break; |
| 253 | default: |
| 254 | assert(0); |
| 255 | } |
| 256 | } |
| 257 | break; |
| 258 | |
| 259 | case ir_unop_abs: |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 260 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 261 | switch (ir->type->base_type) { |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 262 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 263 | data.u[c] = op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 264 | break; |
| 265 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 266 | data.i[c] = op[0]->value.i[c]; |
| 267 | if (data.i[c] < 0) |
| 268 | data.i[c] = -data.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 269 | break; |
| 270 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 271 | data.f[c] = fabs(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 272 | break; |
| 273 | default: |
| 274 | assert(0); |
| 275 | } |
| 276 | } |
| 277 | break; |
| 278 | |
Kenneth Graunke | 14b7b26 | 2010-07-08 23:11:14 -0700 | [diff] [blame] | 279 | case ir_unop_sign: |
| 280 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
| 281 | switch (ir->type->base_type) { |
| 282 | case GLSL_TYPE_UINT: |
| 283 | data.u[c] = op[0]->value.i[c] > 0; |
| 284 | break; |
| 285 | case GLSL_TYPE_INT: |
| 286 | data.i[c] = (op[0]->value.i[c] > 0) - (op[0]->value.i[c] < 0); |
| 287 | break; |
| 288 | case GLSL_TYPE_FLOAT: |
| 289 | data.f[c] = float((op[0]->value.f[c] > 0)-(op[0]->value.f[c] < 0)); |
| 290 | break; |
| 291 | default: |
| 292 | assert(0); |
| 293 | } |
| 294 | } |
| 295 | break; |
| 296 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 297 | case ir_unop_rcp: |
| 298 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 299 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 300 | switch (ir->type->base_type) { |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 301 | case GLSL_TYPE_UINT: |
| 302 | if (op[0]->value.u[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 303 | data.u[c] = 1 / op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 304 | break; |
| 305 | case GLSL_TYPE_INT: |
| 306 | if (op[0]->value.i[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 307 | data.i[c] = 1 / op[0]->value.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 308 | break; |
| 309 | case GLSL_TYPE_FLOAT: |
| 310 | if (op[0]->value.f[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 311 | data.f[c] = 1.0 / op[0]->value.f[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 312 | break; |
| 313 | default: |
| 314 | assert(0); |
| 315 | } |
| 316 | } |
| 317 | break; |
| 318 | |
| 319 | case ir_unop_rsq: |
| 320 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 321 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 322 | data.f[c] = 1.0 / sqrtf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 323 | } |
| 324 | break; |
| 325 | |
| 326 | case ir_unop_sqrt: |
| 327 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 328 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 329 | data.f[c] = sqrtf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 330 | } |
| 331 | break; |
| 332 | |
| 333 | case ir_unop_exp: |
| 334 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 335 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 336 | data.f[c] = expf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 337 | } |
| 338 | break; |
| 339 | |
Kenneth Graunke | aca01ed | 2010-07-08 23:14:32 -0700 | [diff] [blame] | 340 | case ir_unop_exp2: |
| 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] = exp2f(op[0]->value.f[c]); |
| 344 | } |
| 345 | break; |
| 346 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 347 | case ir_unop_log: |
| 348 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 349 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 350 | data.f[c] = logf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 351 | } |
| 352 | break; |
| 353 | |
Kenneth Graunke | cb63929 | 2010-07-08 23:18:09 -0700 | [diff] [blame] | 354 | case ir_unop_log2: |
| 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] = log2f(op[0]->value.f[c]); |
| 358 | } |
| 359 | break; |
| 360 | |
Kenneth Graunke | d6a32d4 | 2010-06-09 15:22:35 -0700 | [diff] [blame] | 361 | case ir_unop_dFdx: |
| 362 | case ir_unop_dFdy: |
| 363 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 364 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 365 | data.f[c] = 0.0; |
Kenneth Graunke | d6a32d4 | 2010-06-09 15:22:35 -0700 | [diff] [blame] | 366 | } |
| 367 | break; |
| 368 | |
Kenneth Graunke | 3f4a0b8 | 2010-07-05 21:15:32 -0700 | [diff] [blame] | 369 | case ir_binop_dot: |
| 370 | assert(op[0]->type->is_vector() && op[1]->type->is_vector()); |
| 371 | data.f[0] = 0; |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 372 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | 3f4a0b8 | 2010-07-05 21:15:32 -0700 | [diff] [blame] | 373 | switch (ir->operands[0]->type->base_type) { |
| 374 | case GLSL_TYPE_UINT: |
| 375 | data.u[0] += op[0]->value.u[c] * op[1]->value.u[c]; |
| 376 | break; |
| 377 | case GLSL_TYPE_INT: |
| 378 | data.i[0] += op[0]->value.i[c] * op[1]->value.i[c]; |
| 379 | break; |
| 380 | case GLSL_TYPE_FLOAT: |
| 381 | data.f[0] += op[0]->value.f[c] * op[1]->value.f[c]; |
| 382 | break; |
| 383 | default: |
| 384 | assert(0); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 389 | case ir_binop_add: |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 390 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 391 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 392 | c < components; |
| 393 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 394 | |
| 395 | switch (ir->operands[0]->type->base_type) { |
| 396 | case GLSL_TYPE_UINT: |
| 397 | data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1]; |
| 398 | break; |
| 399 | case GLSL_TYPE_INT: |
| 400 | data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1]; |
| 401 | break; |
| 402 | case GLSL_TYPE_FLOAT: |
| 403 | data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1]; |
| 404 | break; |
| 405 | default: |
| 406 | assert(0); |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 407 | } |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 408 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 409 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 410 | break; |
| 411 | case ir_binop_sub: |
Kenneth Graunke | 97b44f0 | 2010-07-06 02:53:29 -0700 | [diff] [blame] | 412 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 413 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 414 | c < components; |
| 415 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 416 | |
| 417 | switch (ir->operands[0]->type->base_type) { |
| 418 | case GLSL_TYPE_UINT: |
| 419 | data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1]; |
| 420 | break; |
| 421 | case GLSL_TYPE_INT: |
| 422 | data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1]; |
| 423 | break; |
| 424 | case GLSL_TYPE_FLOAT: |
| 425 | data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1]; |
| 426 | break; |
| 427 | default: |
| 428 | assert(0); |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 429 | } |
Kenneth Graunke | 97b44f0 | 2010-07-06 02:53:29 -0700 | [diff] [blame] | 430 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 431 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 432 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 433 | case ir_binop_mul: |
Kenneth Graunke | cf80a4d | 2010-07-05 23:19:56 -0700 | [diff] [blame] | 434 | /* Check for equal types, or unequal types involving scalars */ |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 435 | if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix()) |
| 436 | || op0_scalar || op1_scalar) { |
| 437 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 438 | c < components; |
| 439 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 440 | |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 441 | switch (ir->operands[0]->type->base_type) { |
| 442 | case GLSL_TYPE_UINT: |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 443 | data.u[c] = op[0]->value.u[c0] * op[1]->value.u[c1]; |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 444 | break; |
| 445 | case GLSL_TYPE_INT: |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 446 | data.i[c] = op[0]->value.i[c0] * op[1]->value.i[c1]; |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 447 | break; |
| 448 | case GLSL_TYPE_FLOAT: |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 449 | data.f[c] = op[0]->value.f[c0] * op[1]->value.f[c1]; |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 450 | break; |
| 451 | default: |
| 452 | assert(0); |
| 453 | } |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 454 | } |
Kenneth Graunke | cf80a4d | 2010-07-05 23:19:56 -0700 | [diff] [blame] | 455 | } else { |
| 456 | assert(op[0]->type->is_matrix() || op[1]->type->is_matrix()); |
| 457 | |
| 458 | /* Multiply an N-by-M matrix with an M-by-P matrix. Since either |
| 459 | * matrix can be a GLSL vector, either N or P can be 1. |
| 460 | * |
| 461 | * For vec*mat, the vector is treated as a row vector. This |
| 462 | * means the vector is a 1-row x M-column matrix. |
| 463 | * |
| 464 | * For mat*vec, the vector is treated as a column vector. Since |
| 465 | * matrix_columns is 1 for vectors, this just works. |
| 466 | */ |
| 467 | const unsigned n = op[0]->type->is_vector() |
| 468 | ? 1 : op[0]->type->vector_elements; |
| 469 | const unsigned m = op[1]->type->vector_elements; |
| 470 | const unsigned p = op[1]->type->matrix_columns; |
| 471 | for (unsigned j = 0; j < p; j++) { |
| 472 | for (unsigned i = 0; i < n; i++) { |
| 473 | for (unsigned k = 0; k < m; k++) { |
| 474 | data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j]; |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 479 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 480 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 481 | case ir_binop_div: |
Kenneth Graunke | dad35eb | 2010-07-06 02:56:36 -0700 | [diff] [blame] | 482 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 483 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 484 | c < components; |
| 485 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 486 | |
| 487 | switch (ir->operands[0]->type->base_type) { |
| 488 | case GLSL_TYPE_UINT: |
| 489 | data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1]; |
| 490 | break; |
| 491 | case GLSL_TYPE_INT: |
| 492 | data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1]; |
| 493 | break; |
| 494 | case GLSL_TYPE_FLOAT: |
| 495 | data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1]; |
| 496 | break; |
| 497 | default: |
| 498 | assert(0); |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 499 | } |
Kenneth Graunke | dad35eb | 2010-07-06 02:56:36 -0700 | [diff] [blame] | 500 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 501 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 502 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 503 | case ir_binop_logic_and: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 504 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 505 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 506 | data.b[c] = op[0]->value.b[c] && op[1]->value.b[c]; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 507 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 508 | case ir_binop_logic_xor: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 509 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 510 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 511 | data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c]; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 512 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 513 | case ir_binop_logic_or: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 514 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 515 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 516 | data.b[c] = op[0]->value.b[c] || op[1]->value.b[c]; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 517 | break; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 518 | |
| 519 | case ir_binop_less: |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 520 | switch (ir->operands[0]->type->base_type) { |
| 521 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 522 | data.b[0] = op[0]->value.u[0] < op[1]->value.u[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 523 | break; |
| 524 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 525 | data.b[0] = op[0]->value.i[0] < op[1]->value.i[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 526 | break; |
| 527 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 528 | data.b[0] = op[0]->value.f[0] < op[1]->value.f[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 529 | break; |
| 530 | default: |
| 531 | assert(0); |
| 532 | } |
| 533 | break; |
| 534 | case ir_binop_greater: |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 535 | switch (ir->operands[0]->type->base_type) { |
| 536 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 537 | data.b[0] = op[0]->value.u[0] > op[1]->value.u[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 538 | break; |
| 539 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 540 | data.b[0] = op[0]->value.i[0] > op[1]->value.i[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 541 | break; |
| 542 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 543 | data.b[0] = op[0]->value.f[0] > op[1]->value.f[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 544 | break; |
| 545 | default: |
| 546 | assert(0); |
| 547 | } |
| 548 | break; |
| 549 | case ir_binop_lequal: |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 550 | switch (ir->operands[0]->type->base_type) { |
| 551 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 552 | data.b[0] = op[0]->value.u[0] <= op[1]->value.u[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 553 | break; |
| 554 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 555 | data.b[0] = op[0]->value.i[0] <= op[1]->value.i[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 556 | break; |
| 557 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 558 | data.b[0] = op[0]->value.f[0] <= op[1]->value.f[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 559 | break; |
| 560 | default: |
| 561 | assert(0); |
| 562 | } |
| 563 | break; |
| 564 | case ir_binop_gequal: |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 565 | switch (ir->operands[0]->type->base_type) { |
| 566 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 567 | data.b[0] = op[0]->value.u[0] >= op[1]->value.u[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 568 | break; |
| 569 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 570 | data.b[0] = op[0]->value.i[0] >= op[1]->value.i[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 571 | break; |
| 572 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 573 | data.b[0] = op[0]->value.f[0] >= op[1]->value.f[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 574 | break; |
| 575 | default: |
| 576 | assert(0); |
| 577 | } |
| 578 | break; |
| 579 | |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 580 | case ir_binop_equal: |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame] | 581 | data.b[0] = true; |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 582 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame] | 583 | 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 Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | break; |
| 601 | case ir_binop_nequal: |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame] | 602 | data.b[0] = false; |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 603 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame] | 604 | switch (ir->operands[0]->type->base_type) { |
| 605 | case GLSL_TYPE_UINT: |
| 606 | data.b[0] = data.b[0] || op[0]->value.u[c] != op[1]->value.u[c]; |
| 607 | break; |
| 608 | case GLSL_TYPE_INT: |
| 609 | data.b[0] = data.b[0] || op[0]->value.i[c] != op[1]->value.i[c]; |
| 610 | break; |
| 611 | case GLSL_TYPE_FLOAT: |
| 612 | data.b[0] = data.b[0] || op[0]->value.f[c] != op[1]->value.f[c]; |
| 613 | break; |
| 614 | case GLSL_TYPE_BOOL: |
| 615 | data.b[0] = data.b[0] || op[0]->value.b[c] != op[1]->value.b[c]; |
| 616 | break; |
| 617 | default: |
| 618 | assert(0); |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | break; |
| 622 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 623 | default: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 624 | /* FINISHME: Should handle all expression types. */ |
| 625 | return; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 626 | } |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 627 | |
Eric Anholt | 6b01b50 | 2010-06-24 15:18:39 -0700 | [diff] [blame] | 628 | void *ctx = talloc_parent(ir); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 629 | this->value = new(ctx) ir_constant(ir->type, &data); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | |
| 633 | void |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 634 | ir_constant_visitor::visit(ir_texture *ir) |
| 635 | { |
| 636 | // FINISHME: Do stuff with texture lookups |
| 637 | (void) ir; |
| 638 | value = NULL; |
| 639 | } |
| 640 | |
| 641 | |
| 642 | void |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 643 | ir_constant_visitor::visit(ir_swizzle *ir) |
| 644 | { |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 645 | ir_constant *v = ir->val->constant_expression_value(); |
| 646 | |
| 647 | this->value = NULL; |
| 648 | |
| 649 | if (v != NULL) { |
Ian Romanick | 0bb70a3 | 2010-06-11 15:49:49 -0700 | [diff] [blame] | 650 | ir_constant_data data; |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 651 | |
| 652 | const unsigned swiz_idx[4] = { |
| 653 | ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w |
| 654 | }; |
| 655 | |
| 656 | for (unsigned i = 0; i < ir->mask.num_components; i++) { |
| 657 | switch (v->type->base_type) { |
| 658 | case GLSL_TYPE_UINT: |
| 659 | case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break; |
| 660 | case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break; |
| 661 | case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break; |
| 662 | default: assert(!"Should not get here."); break; |
| 663 | } |
| 664 | } |
| 665 | |
Eric Anholt | 6b01b50 | 2010-06-24 15:18:39 -0700 | [diff] [blame] | 666 | void *ctx = talloc_parent(ir); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 667 | this->value = new(ctx) ir_constant(ir->type, &data); |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 668 | } |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | |
| 672 | void |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 673 | ir_constant_visitor::visit(ir_dereference_variable *ir) |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 674 | { |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 675 | value = NULL; |
Eric Anholt | 326c676 | 2010-04-06 10:30:54 -0700 | [diff] [blame] | 676 | |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 677 | ir_variable *var = ir->variable_referenced(); |
| 678 | if (var && var->constant_value) |
Ian Romanick | ca088cc | 2010-07-06 17:41:02 -0700 | [diff] [blame] | 679 | value = var->constant_value->clone(NULL); |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | |
| 683 | void |
| 684 | ir_constant_visitor::visit(ir_dereference_array *ir) |
| 685 | { |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 686 | void *ctx = talloc_parent(ir); |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 687 | ir_constant *array = ir->array->constant_expression_value(); |
| 688 | ir_constant *idx = ir->array_index->constant_expression_value(); |
| 689 | |
| 690 | this->value = NULL; |
| 691 | |
| 692 | if ((array != NULL) && (idx != NULL)) { |
| 693 | if (array->type->is_matrix()) { |
| 694 | /* Array access of a matrix results in a vector. |
| 695 | */ |
| 696 | const unsigned column = idx->value.u[0]; |
| 697 | |
| 698 | const glsl_type *const column_type = array->type->column_type(); |
| 699 | |
| 700 | /* Offset in the constant matrix to the first element of the column |
| 701 | * to be extracted. |
| 702 | */ |
| 703 | const unsigned mat_idx = column * column_type->vector_elements; |
| 704 | |
Ian Romanick | 0bb70a3 | 2010-06-11 15:49:49 -0700 | [diff] [blame] | 705 | ir_constant_data data; |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 706 | |
| 707 | switch (column_type->base_type) { |
| 708 | case GLSL_TYPE_UINT: |
| 709 | case GLSL_TYPE_INT: |
| 710 | for (unsigned i = 0; i < column_type->vector_elements; i++) |
| 711 | data.u[i] = array->value.u[mat_idx + i]; |
| 712 | |
| 713 | break; |
| 714 | |
| 715 | case GLSL_TYPE_FLOAT: |
| 716 | for (unsigned i = 0; i < column_type->vector_elements; i++) |
| 717 | data.f[i] = array->value.f[mat_idx + i]; |
| 718 | |
| 719 | break; |
| 720 | |
| 721 | default: |
| 722 | assert(!"Should not get here."); |
| 723 | break; |
| 724 | } |
| 725 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 726 | this->value = new(ctx) ir_constant(column_type, &data); |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 727 | } else if (array->type->is_vector()) { |
| 728 | const unsigned component = idx->value.u[0]; |
| 729 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 730 | this->value = new(ctx) ir_constant(array, component); |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 731 | } else { |
| 732 | /* FINISHME: Handle access of constant arrays. */ |
| 733 | } |
| 734 | } |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | |
| 738 | void |
| 739 | ir_constant_visitor::visit(ir_dereference_record *ir) |
| 740 | { |
Ian Romanick | 253dede | 2010-06-09 17:30:19 -0700 | [diff] [blame] | 741 | ir_constant *v = ir->record->constant_expression_value(); |
| 742 | |
| 743 | this->value = (v != NULL) ? v->get_record_field(ir->field) : NULL; |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | |
| 747 | void |
| 748 | ir_constant_visitor::visit(ir_assignment *ir) |
| 749 | { |
| 750 | (void) ir; |
| 751 | value = NULL; |
| 752 | } |
| 753 | |
| 754 | |
| 755 | void |
| 756 | ir_constant_visitor::visit(ir_constant *ir) |
| 757 | { |
| 758 | value = ir; |
| 759 | } |
| 760 | |
| 761 | |
| 762 | void |
| 763 | ir_constant_visitor::visit(ir_call *ir) |
| 764 | { |
| 765 | (void) ir; |
| 766 | value = NULL; |
| 767 | } |
| 768 | |
| 769 | |
| 770 | void |
| 771 | ir_constant_visitor::visit(ir_return *ir) |
| 772 | { |
| 773 | (void) ir; |
| 774 | value = NULL; |
| 775 | } |
| 776 | |
| 777 | |
| 778 | void |
Kenneth Graunke | 16efab1 | 2010-06-30 10:47:34 -0700 | [diff] [blame] | 779 | ir_constant_visitor::visit(ir_discard *ir) |
| 780 | { |
| 781 | (void) ir; |
| 782 | value = NULL; |
| 783 | } |
| 784 | |
| 785 | |
| 786 | void |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 787 | ir_constant_visitor::visit(ir_if *ir) |
| 788 | { |
| 789 | (void) ir; |
| 790 | value = NULL; |
| 791 | } |
Ian Romanick | fad607a | 2010-04-05 16:16:07 -0700 | [diff] [blame] | 792 | |
| 793 | |
| 794 | void |
| 795 | ir_constant_visitor::visit(ir_loop *ir) |
| 796 | { |
| 797 | (void) ir; |
| 798 | value = NULL; |
| 799 | } |
Ian Romanick | f8e31e0 | 2010-04-05 16:28:15 -0700 | [diff] [blame] | 800 | |
| 801 | |
| 802 | void |
| 803 | ir_constant_visitor::visit(ir_loop_jump *ir) |
| 804 | { |
| 805 | (void) ir; |
| 806 | value = NULL; |
| 807 | } |