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 | |
Kenneth Graunke | 79fed37 | 2010-07-09 11:53:56 -0700 | [diff] [blame^] | 41 | #define min(x,y) (x) < (y) ? (x) : (y) |
| 42 | #define max(x,y) (x) > (y) ? (x) : (y) |
| 43 | |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Visitor class for evaluating constant expressions |
| 46 | */ |
| 47 | class ir_constant_visitor : public ir_visitor { |
| 48 | public: |
| 49 | ir_constant_visitor() |
| 50 | : value(NULL) |
| 51 | { |
| 52 | /* empty */ |
| 53 | } |
| 54 | |
| 55 | virtual ~ir_constant_visitor() |
| 56 | { |
| 57 | /* empty */ |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * \name Visit methods |
| 62 | * |
| 63 | * As typical for the visitor pattern, there must be one \c visit method for |
| 64 | * each concrete subclass of \c ir_instruction. Virtual base classes within |
| 65 | * the hierarchy should not have \c visit methods. |
| 66 | */ |
| 67 | /*@{*/ |
| 68 | virtual void visit(ir_variable *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 69 | virtual void visit(ir_function_signature *); |
| 70 | virtual void visit(ir_function *); |
| 71 | virtual void visit(ir_expression *); |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 72 | virtual void visit(ir_texture *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 73 | virtual void visit(ir_swizzle *); |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 74 | virtual void visit(ir_dereference_variable *); |
| 75 | virtual void visit(ir_dereference_array *); |
| 76 | virtual void visit(ir_dereference_record *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 77 | virtual void visit(ir_assignment *); |
| 78 | virtual void visit(ir_constant *); |
| 79 | virtual void visit(ir_call *); |
| 80 | virtual void visit(ir_return *); |
Kenneth Graunke | 16efab1 | 2010-06-30 10:47:34 -0700 | [diff] [blame] | 81 | virtual void visit(ir_discard *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 82 | virtual void visit(ir_if *); |
Ian Romanick | fad607a | 2010-04-05 16:16:07 -0700 | [diff] [blame] | 83 | virtual void visit(ir_loop *); |
Ian Romanick | f8e31e0 | 2010-04-05 16:28:15 -0700 | [diff] [blame] | 84 | virtual void visit(ir_loop_jump *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 85 | /*@}*/ |
| 86 | |
| 87 | /** |
| 88 | * Value of the constant expression. |
| 89 | * |
| 90 | * \note |
| 91 | * This field will be \c NULL if the expression is not constant valued. |
| 92 | */ |
| 93 | /* FINIHSME: This cannot hold values for constant arrays or structures. */ |
| 94 | ir_constant *value; |
| 95 | }; |
| 96 | |
| 97 | |
| 98 | ir_constant * |
| 99 | ir_instruction::constant_expression_value() |
| 100 | { |
| 101 | ir_constant_visitor visitor; |
| 102 | |
| 103 | this->accept(& visitor); |
| 104 | return visitor.value; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | void |
| 109 | ir_constant_visitor::visit(ir_variable *ir) |
| 110 | { |
| 111 | (void) ir; |
| 112 | value = NULL; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | void |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 117 | ir_constant_visitor::visit(ir_function_signature *ir) |
| 118 | { |
| 119 | (void) ir; |
| 120 | value = NULL; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | void |
| 125 | ir_constant_visitor::visit(ir_function *ir) |
| 126 | { |
| 127 | (void) ir; |
| 128 | value = NULL; |
| 129 | } |
| 130 | |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 131 | void |
| 132 | ir_constant_visitor::visit(ir_expression *ir) |
| 133 | { |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 134 | value = NULL; |
Kenneth Graunke | 6bc432e | 2010-07-02 17:12:23 -0700 | [diff] [blame] | 135 | ir_constant *op[2] = { NULL, NULL }; |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 136 | ir_constant_data data; |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame] | 137 | |
Kenneth Graunke | c63a1db | 2010-07-05 22:33:35 -0700 | [diff] [blame] | 138 | memset(&data, 0, sizeof(data)); |
| 139 | |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 140 | for (unsigned operand = 0; operand < ir->get_num_operands(); operand++) { |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 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 | |
Kenneth Graunke | 6fc983b | 2010-07-06 02:39:57 -0700 | [diff] [blame] | 146 | if (op[1] != NULL) |
| 147 | assert(op[0]->type->base_type == op[1]->type->base_type); |
| 148 | |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 149 | bool op0_scalar = op[0]->type->is_scalar(); |
| 150 | bool op1_scalar = op[1] != NULL && op[1]->type->is_scalar(); |
| 151 | |
| 152 | /* When iterating over a vector or matrix's components, we want to increase |
| 153 | * the loop counter. However, for scalars, we want to stay at 0. |
| 154 | */ |
Kenneth Graunke | acf88f2 | 2010-07-07 12:08:23 -0700 | [diff] [blame] | 155 | unsigned c0_inc = op0_scalar ? 0 : 1; |
| 156 | unsigned c1_inc = op1_scalar ? 0 : 1; |
Eric Anholt | 570dc0d | 2010-07-07 09:07:09 -0700 | [diff] [blame] | 157 | unsigned components; |
| 158 | if (op1_scalar || !op[1]) { |
| 159 | components = op[0]->type->components(); |
| 160 | } else { |
| 161 | components = op[1]->type->components(); |
| 162 | } |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 163 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 164 | switch (ir->operation) { |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 165 | case ir_unop_logic_not: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 166 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 167 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 168 | data.b[c] = !op[0]->value.b[c]; |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 169 | break; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 170 | |
| 171 | case ir_unop_f2i: |
| 172 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 173 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 174 | data.i[c] = op[0]->value.f[c]; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 175 | } |
| 176 | break; |
| 177 | case ir_unop_i2f: |
| 178 | assert(op[0]->type->base_type == GLSL_TYPE_UINT || |
| 179 | op[0]->type->base_type == GLSL_TYPE_INT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 180 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 181 | if (op[0]->type->base_type == GLSL_TYPE_INT) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 182 | data.f[c] = op[0]->value.i[c]; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 183 | else |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 184 | data.f[c] = op[0]->value.u[c]; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 185 | } |
| 186 | break; |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 187 | case ir_unop_b2f: |
| 188 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 189 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 190 | data.f[c] = op[0]->value.b[c] ? 1.0 : 0.0; |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 191 | } |
| 192 | break; |
| 193 | case ir_unop_f2b: |
| 194 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 195 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 196 | data.b[c] = bool(op[0]->value.f[c]); |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 197 | } |
| 198 | break; |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 199 | case ir_unop_b2i: |
| 200 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 201 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 202 | data.u[c] = op[0]->value.b[c] ? 1 : 0; |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 203 | } |
| 204 | break; |
| 205 | case ir_unop_i2b: |
| 206 | assert(op[0]->type->is_integer()); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 207 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 208 | data.b[c] = bool(op[0]->value.u[c]); |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 209 | } |
| 210 | break; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 211 | |
Kenneth Graunke | 323d909 | 2010-07-08 23:21:36 -0700 | [diff] [blame] | 212 | case ir_unop_trunc: |
| 213 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 214 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
| 215 | data.f[c] = truncf(op[0]->value.f[c]); |
| 216 | } |
| 217 | break; |
| 218 | |
Kenneth Graunke | c1ee30a | 2010-07-08 23:22:36 -0700 | [diff] [blame] | 219 | case ir_unop_ceil: |
| 220 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 221 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
| 222 | data.f[c] = ceilf(op[0]->value.f[c]); |
| 223 | } |
| 224 | break; |
| 225 | |
Kenneth Graunke | 0747204 | 2010-07-08 23:23:23 -0700 | [diff] [blame] | 226 | case ir_unop_floor: |
| 227 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 228 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
| 229 | data.f[c] = floorf(op[0]->value.f[c]); |
| 230 | } |
| 231 | break; |
| 232 | |
Eric Anholt | d925c91 | 2010-07-01 10:37:11 -0700 | [diff] [blame] | 233 | case ir_unop_fract: |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 234 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Eric Anholt | d925c91 | 2010-07-01 10:37:11 -0700 | [diff] [blame] | 235 | switch (ir->type->base_type) { |
| 236 | case GLSL_TYPE_UINT: |
| 237 | data.u[c] = 0; |
| 238 | break; |
| 239 | case GLSL_TYPE_INT: |
| 240 | data.i[c] = 0; |
| 241 | break; |
| 242 | case GLSL_TYPE_FLOAT: |
| 243 | data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]); |
| 244 | break; |
| 245 | default: |
| 246 | assert(0); |
| 247 | } |
| 248 | } |
| 249 | break; |
| 250 | |
Kenneth Graunke | 908afd1 | 2010-07-08 23:28:50 -0700 | [diff] [blame] | 251 | case ir_unop_sin: |
| 252 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 253 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
| 254 | data.f[c] = sinf(op[0]->value.f[c]); |
| 255 | } |
| 256 | break; |
| 257 | |
Kenneth Graunke | 3fab376 | 2010-07-08 23:29:37 -0700 | [diff] [blame] | 258 | case ir_unop_cos: |
| 259 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 260 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
| 261 | data.f[c] = cosf(op[0]->value.f[c]); |
| 262 | } |
| 263 | break; |
| 264 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 265 | case ir_unop_neg: |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 266 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 267 | switch (ir->type->base_type) { |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 268 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 269 | data.u[c] = -op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 270 | break; |
| 271 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 272 | data.i[c] = -op[0]->value.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 273 | break; |
| 274 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 275 | data.f[c] = -op[0]->value.f[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 276 | break; |
| 277 | default: |
| 278 | assert(0); |
| 279 | } |
| 280 | } |
| 281 | break; |
| 282 | |
| 283 | case ir_unop_abs: |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 284 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 285 | switch (ir->type->base_type) { |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 286 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 287 | data.u[c] = op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 288 | break; |
| 289 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 290 | data.i[c] = op[0]->value.i[c]; |
| 291 | if (data.i[c] < 0) |
| 292 | data.i[c] = -data.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 293 | break; |
| 294 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 295 | data.f[c] = fabs(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 296 | break; |
| 297 | default: |
| 298 | assert(0); |
| 299 | } |
| 300 | } |
| 301 | break; |
| 302 | |
Kenneth Graunke | 14b7b26 | 2010-07-08 23:11:14 -0700 | [diff] [blame] | 303 | case ir_unop_sign: |
| 304 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
| 305 | switch (ir->type->base_type) { |
| 306 | case GLSL_TYPE_UINT: |
| 307 | data.u[c] = op[0]->value.i[c] > 0; |
| 308 | break; |
| 309 | case GLSL_TYPE_INT: |
| 310 | data.i[c] = (op[0]->value.i[c] > 0) - (op[0]->value.i[c] < 0); |
| 311 | break; |
| 312 | case GLSL_TYPE_FLOAT: |
| 313 | data.f[c] = float((op[0]->value.f[c] > 0)-(op[0]->value.f[c] < 0)); |
| 314 | break; |
| 315 | default: |
| 316 | assert(0); |
| 317 | } |
| 318 | } |
| 319 | break; |
| 320 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 321 | case ir_unop_rcp: |
| 322 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 323 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 324 | switch (ir->type->base_type) { |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 325 | case GLSL_TYPE_UINT: |
| 326 | if (op[0]->value.u[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 327 | data.u[c] = 1 / op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 328 | break; |
| 329 | case GLSL_TYPE_INT: |
| 330 | if (op[0]->value.i[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 331 | data.i[c] = 1 / op[0]->value.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 332 | break; |
| 333 | case GLSL_TYPE_FLOAT: |
| 334 | if (op[0]->value.f[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 335 | data.f[c] = 1.0 / op[0]->value.f[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 336 | break; |
| 337 | default: |
| 338 | assert(0); |
| 339 | } |
| 340 | } |
| 341 | break; |
| 342 | |
| 343 | case ir_unop_rsq: |
| 344 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 345 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 346 | data.f[c] = 1.0 / sqrtf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 347 | } |
| 348 | break; |
| 349 | |
| 350 | case ir_unop_sqrt: |
| 351 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 352 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 353 | data.f[c] = sqrtf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 354 | } |
| 355 | break; |
| 356 | |
| 357 | case ir_unop_exp: |
| 358 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 359 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 360 | data.f[c] = expf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 361 | } |
| 362 | break; |
| 363 | |
Kenneth Graunke | aca01ed | 2010-07-08 23:14:32 -0700 | [diff] [blame] | 364 | case ir_unop_exp2: |
| 365 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 366 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
| 367 | data.f[c] = exp2f(op[0]->value.f[c]); |
| 368 | } |
| 369 | break; |
| 370 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 371 | case ir_unop_log: |
| 372 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 373 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 374 | data.f[c] = logf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 375 | } |
| 376 | break; |
| 377 | |
Kenneth Graunke | cb63929 | 2010-07-08 23:18:09 -0700 | [diff] [blame] | 378 | case ir_unop_log2: |
| 379 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 380 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
| 381 | data.f[c] = log2f(op[0]->value.f[c]); |
| 382 | } |
| 383 | break; |
| 384 | |
Kenneth Graunke | d6a32d4 | 2010-06-09 15:22:35 -0700 | [diff] [blame] | 385 | case ir_unop_dFdx: |
| 386 | case ir_unop_dFdy: |
| 387 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 388 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 389 | data.f[c] = 0.0; |
Kenneth Graunke | d6a32d4 | 2010-06-09 15:22:35 -0700 | [diff] [blame] | 390 | } |
| 391 | break; |
| 392 | |
Kenneth Graunke | 891a064 | 2010-07-08 23:35:09 -0700 | [diff] [blame] | 393 | case ir_binop_pow: |
| 394 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 395 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
| 396 | data.f[c] = powf(op[0]->value.f[c], op[1]->value.f[c]); |
| 397 | } |
| 398 | break; |
| 399 | |
Kenneth Graunke | 3f4a0b8 | 2010-07-05 21:15:32 -0700 | [diff] [blame] | 400 | case ir_binop_dot: |
| 401 | assert(op[0]->type->is_vector() && op[1]->type->is_vector()); |
| 402 | data.f[0] = 0; |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 403 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | 3f4a0b8 | 2010-07-05 21:15:32 -0700 | [diff] [blame] | 404 | switch (ir->operands[0]->type->base_type) { |
| 405 | case GLSL_TYPE_UINT: |
| 406 | data.u[0] += op[0]->value.u[c] * op[1]->value.u[c]; |
| 407 | break; |
| 408 | case GLSL_TYPE_INT: |
| 409 | data.i[0] += op[0]->value.i[c] * op[1]->value.i[c]; |
| 410 | break; |
| 411 | case GLSL_TYPE_FLOAT: |
| 412 | data.f[0] += op[0]->value.f[c] * op[1]->value.f[c]; |
| 413 | break; |
| 414 | default: |
| 415 | assert(0); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | break; |
Kenneth Graunke | 79fed37 | 2010-07-09 11:53:56 -0700 | [diff] [blame^] | 420 | case ir_binop_min: |
| 421 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 422 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 423 | c < components; |
| 424 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 425 | |
| 426 | switch (ir->operands[0]->type->base_type) { |
| 427 | case GLSL_TYPE_UINT: |
| 428 | data.u[c] = min(op[0]->value.u[c0], op[1]->value.u[c1]); |
| 429 | break; |
| 430 | case GLSL_TYPE_INT: |
| 431 | data.i[c] = min(op[0]->value.i[c0], op[1]->value.i[c1]); |
| 432 | break; |
| 433 | case GLSL_TYPE_FLOAT: |
| 434 | data.f[c] = min(op[0]->value.f[c0], op[1]->value.f[c1]); |
| 435 | break; |
| 436 | default: |
| 437 | assert(0); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | break; |
| 442 | case ir_binop_max: |
| 443 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 444 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 445 | c < components; |
| 446 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 447 | |
| 448 | switch (ir->operands[0]->type->base_type) { |
| 449 | case GLSL_TYPE_UINT: |
| 450 | data.u[c] = max(op[0]->value.u[c0], op[1]->value.u[c1]); |
| 451 | break; |
| 452 | case GLSL_TYPE_INT: |
| 453 | data.i[c] = max(op[0]->value.i[c0], op[1]->value.i[c1]); |
| 454 | break; |
| 455 | case GLSL_TYPE_FLOAT: |
| 456 | data.f[c] = max(op[0]->value.f[c0], op[1]->value.f[c1]); |
| 457 | break; |
| 458 | default: |
| 459 | assert(0); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 464 | case ir_binop_add: |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 465 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 466 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 467 | c < components; |
| 468 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 469 | |
| 470 | switch (ir->operands[0]->type->base_type) { |
| 471 | case GLSL_TYPE_UINT: |
| 472 | data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1]; |
| 473 | break; |
| 474 | case GLSL_TYPE_INT: |
| 475 | data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1]; |
| 476 | break; |
| 477 | case GLSL_TYPE_FLOAT: |
| 478 | data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1]; |
| 479 | break; |
| 480 | default: |
| 481 | assert(0); |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 482 | } |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 483 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 484 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 485 | break; |
| 486 | case ir_binop_sub: |
Kenneth Graunke | 97b44f0 | 2010-07-06 02:53:29 -0700 | [diff] [blame] | 487 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 488 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 489 | c < components; |
| 490 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 491 | |
| 492 | switch (ir->operands[0]->type->base_type) { |
| 493 | case GLSL_TYPE_UINT: |
| 494 | data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1]; |
| 495 | break; |
| 496 | case GLSL_TYPE_INT: |
| 497 | data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1]; |
| 498 | break; |
| 499 | case GLSL_TYPE_FLOAT: |
| 500 | data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1]; |
| 501 | break; |
| 502 | default: |
| 503 | assert(0); |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 504 | } |
Kenneth Graunke | 97b44f0 | 2010-07-06 02:53:29 -0700 | [diff] [blame] | 505 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 506 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 507 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 508 | case ir_binop_mul: |
Kenneth Graunke | cf80a4d | 2010-07-05 23:19:56 -0700 | [diff] [blame] | 509 | /* Check for equal types, or unequal types involving scalars */ |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 510 | if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix()) |
| 511 | || op0_scalar || op1_scalar) { |
| 512 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 513 | c < components; |
| 514 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 515 | |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 516 | switch (ir->operands[0]->type->base_type) { |
| 517 | case GLSL_TYPE_UINT: |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 518 | 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] | 519 | break; |
| 520 | case GLSL_TYPE_INT: |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 521 | 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] | 522 | break; |
| 523 | case GLSL_TYPE_FLOAT: |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 524 | 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] | 525 | break; |
| 526 | default: |
| 527 | assert(0); |
| 528 | } |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 529 | } |
Kenneth Graunke | cf80a4d | 2010-07-05 23:19:56 -0700 | [diff] [blame] | 530 | } else { |
| 531 | assert(op[0]->type->is_matrix() || op[1]->type->is_matrix()); |
| 532 | |
| 533 | /* Multiply an N-by-M matrix with an M-by-P matrix. Since either |
| 534 | * matrix can be a GLSL vector, either N or P can be 1. |
| 535 | * |
| 536 | * For vec*mat, the vector is treated as a row vector. This |
| 537 | * means the vector is a 1-row x M-column matrix. |
| 538 | * |
| 539 | * For mat*vec, the vector is treated as a column vector. Since |
| 540 | * matrix_columns is 1 for vectors, this just works. |
| 541 | */ |
| 542 | const unsigned n = op[0]->type->is_vector() |
| 543 | ? 1 : op[0]->type->vector_elements; |
| 544 | const unsigned m = op[1]->type->vector_elements; |
| 545 | const unsigned p = op[1]->type->matrix_columns; |
| 546 | for (unsigned j = 0; j < p; j++) { |
| 547 | for (unsigned i = 0; i < n; i++) { |
| 548 | for (unsigned k = 0; k < m; k++) { |
| 549 | data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j]; |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 554 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 555 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 556 | case ir_binop_div: |
Kenneth Graunke | dad35eb | 2010-07-06 02:56:36 -0700 | [diff] [blame] | 557 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 558 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 559 | c < components; |
| 560 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 561 | |
| 562 | switch (ir->operands[0]->type->base_type) { |
| 563 | case GLSL_TYPE_UINT: |
| 564 | data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1]; |
| 565 | break; |
| 566 | case GLSL_TYPE_INT: |
| 567 | data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1]; |
| 568 | break; |
| 569 | case GLSL_TYPE_FLOAT: |
| 570 | data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1]; |
| 571 | break; |
| 572 | default: |
| 573 | assert(0); |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 574 | } |
Kenneth Graunke | dad35eb | 2010-07-06 02:56:36 -0700 | [diff] [blame] | 575 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 576 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 577 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 578 | case ir_binop_logic_and: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 579 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 580 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 581 | 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] | 582 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 583 | case ir_binop_logic_xor: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 584 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 585 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 586 | 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] | 587 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 588 | case ir_binop_logic_or: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 589 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 590 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 591 | 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] | 592 | break; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 593 | |
| 594 | case ir_binop_less: |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 595 | switch (ir->operands[0]->type->base_type) { |
| 596 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 597 | 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] | 598 | break; |
| 599 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 600 | 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] | 601 | break; |
| 602 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 603 | 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] | 604 | break; |
| 605 | default: |
| 606 | assert(0); |
| 607 | } |
| 608 | break; |
| 609 | case ir_binop_greater: |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 610 | switch (ir->operands[0]->type->base_type) { |
| 611 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 612 | 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] | 613 | break; |
| 614 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 615 | 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] | 616 | break; |
| 617 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 618 | 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] | 619 | break; |
| 620 | default: |
| 621 | assert(0); |
| 622 | } |
| 623 | break; |
| 624 | case ir_binop_lequal: |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 625 | switch (ir->operands[0]->type->base_type) { |
| 626 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 627 | 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] | 628 | break; |
| 629 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 630 | 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] | 631 | break; |
| 632 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 633 | 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] | 634 | break; |
| 635 | default: |
| 636 | assert(0); |
| 637 | } |
| 638 | break; |
| 639 | case ir_binop_gequal: |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 640 | switch (ir->operands[0]->type->base_type) { |
| 641 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 642 | 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] | 643 | break; |
| 644 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 645 | 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] | 646 | break; |
| 647 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 648 | 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] | 649 | break; |
| 650 | default: |
| 651 | assert(0); |
| 652 | } |
| 653 | break; |
| 654 | |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 655 | case ir_binop_equal: |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame] | 656 | data.b[0] = true; |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 657 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame] | 658 | switch (ir->operands[0]->type->base_type) { |
| 659 | case GLSL_TYPE_UINT: |
| 660 | data.b[0] = data.b[0] && op[0]->value.u[c] == op[1]->value.u[c]; |
| 661 | break; |
| 662 | case GLSL_TYPE_INT: |
| 663 | data.b[0] = data.b[0] && op[0]->value.i[c] == op[1]->value.i[c]; |
| 664 | break; |
| 665 | case GLSL_TYPE_FLOAT: |
| 666 | data.b[0] = data.b[0] && op[0]->value.f[c] == op[1]->value.f[c]; |
| 667 | break; |
| 668 | case GLSL_TYPE_BOOL: |
| 669 | data.b[0] = data.b[0] && op[0]->value.b[c] == op[1]->value.b[c]; |
| 670 | break; |
| 671 | default: |
| 672 | assert(0); |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | break; |
| 676 | case ir_binop_nequal: |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame] | 677 | data.b[0] = false; |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 678 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame] | 679 | switch (ir->operands[0]->type->base_type) { |
| 680 | case GLSL_TYPE_UINT: |
| 681 | data.b[0] = data.b[0] || op[0]->value.u[c] != op[1]->value.u[c]; |
| 682 | break; |
| 683 | case GLSL_TYPE_INT: |
| 684 | data.b[0] = data.b[0] || op[0]->value.i[c] != op[1]->value.i[c]; |
| 685 | break; |
| 686 | case GLSL_TYPE_FLOAT: |
| 687 | data.b[0] = data.b[0] || op[0]->value.f[c] != op[1]->value.f[c]; |
| 688 | break; |
| 689 | case GLSL_TYPE_BOOL: |
| 690 | data.b[0] = data.b[0] || op[0]->value.b[c] != op[1]->value.b[c]; |
| 691 | break; |
| 692 | default: |
| 693 | assert(0); |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | break; |
| 697 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 698 | default: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 699 | /* FINISHME: Should handle all expression types. */ |
| 700 | return; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 701 | } |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 702 | |
Eric Anholt | 6b01b50 | 2010-06-24 15:18:39 -0700 | [diff] [blame] | 703 | void *ctx = talloc_parent(ir); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 704 | this->value = new(ctx) ir_constant(ir->type, &data); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | |
| 708 | void |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 709 | ir_constant_visitor::visit(ir_texture *ir) |
| 710 | { |
| 711 | // FINISHME: Do stuff with texture lookups |
| 712 | (void) ir; |
| 713 | value = NULL; |
| 714 | } |
| 715 | |
| 716 | |
| 717 | void |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 718 | ir_constant_visitor::visit(ir_swizzle *ir) |
| 719 | { |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 720 | ir_constant *v = ir->val->constant_expression_value(); |
| 721 | |
| 722 | this->value = NULL; |
| 723 | |
| 724 | if (v != NULL) { |
Ian Romanick | 0bb70a3 | 2010-06-11 15:49:49 -0700 | [diff] [blame] | 725 | ir_constant_data data; |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 726 | |
| 727 | const unsigned swiz_idx[4] = { |
| 728 | ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w |
| 729 | }; |
| 730 | |
| 731 | for (unsigned i = 0; i < ir->mask.num_components; i++) { |
| 732 | switch (v->type->base_type) { |
| 733 | case GLSL_TYPE_UINT: |
| 734 | case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break; |
| 735 | case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break; |
| 736 | case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break; |
| 737 | default: assert(!"Should not get here."); break; |
| 738 | } |
| 739 | } |
| 740 | |
Eric Anholt | 6b01b50 | 2010-06-24 15:18:39 -0700 | [diff] [blame] | 741 | void *ctx = talloc_parent(ir); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 742 | this->value = new(ctx) ir_constant(ir->type, &data); |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 743 | } |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | |
| 747 | void |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 748 | ir_constant_visitor::visit(ir_dereference_variable *ir) |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 749 | { |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 750 | value = NULL; |
Eric Anholt | 326c676 | 2010-04-06 10:30:54 -0700 | [diff] [blame] | 751 | |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 752 | ir_variable *var = ir->variable_referenced(); |
| 753 | if (var && var->constant_value) |
Ian Romanick | ca088cc | 2010-07-06 17:41:02 -0700 | [diff] [blame] | 754 | value = var->constant_value->clone(NULL); |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | |
| 758 | void |
| 759 | ir_constant_visitor::visit(ir_dereference_array *ir) |
| 760 | { |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 761 | void *ctx = talloc_parent(ir); |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 762 | ir_constant *array = ir->array->constant_expression_value(); |
| 763 | ir_constant *idx = ir->array_index->constant_expression_value(); |
| 764 | |
| 765 | this->value = NULL; |
| 766 | |
| 767 | if ((array != NULL) && (idx != NULL)) { |
| 768 | if (array->type->is_matrix()) { |
| 769 | /* Array access of a matrix results in a vector. |
| 770 | */ |
| 771 | const unsigned column = idx->value.u[0]; |
| 772 | |
| 773 | const glsl_type *const column_type = array->type->column_type(); |
| 774 | |
| 775 | /* Offset in the constant matrix to the first element of the column |
| 776 | * to be extracted. |
| 777 | */ |
| 778 | const unsigned mat_idx = column * column_type->vector_elements; |
| 779 | |
Ian Romanick | 0bb70a3 | 2010-06-11 15:49:49 -0700 | [diff] [blame] | 780 | ir_constant_data data; |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 781 | |
| 782 | switch (column_type->base_type) { |
| 783 | case GLSL_TYPE_UINT: |
| 784 | case GLSL_TYPE_INT: |
| 785 | for (unsigned i = 0; i < column_type->vector_elements; i++) |
| 786 | data.u[i] = array->value.u[mat_idx + i]; |
| 787 | |
| 788 | break; |
| 789 | |
| 790 | case GLSL_TYPE_FLOAT: |
| 791 | for (unsigned i = 0; i < column_type->vector_elements; i++) |
| 792 | data.f[i] = array->value.f[mat_idx + i]; |
| 793 | |
| 794 | break; |
| 795 | |
| 796 | default: |
| 797 | assert(!"Should not get here."); |
| 798 | break; |
| 799 | } |
| 800 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 801 | this->value = new(ctx) ir_constant(column_type, &data); |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 802 | } else if (array->type->is_vector()) { |
| 803 | const unsigned component = idx->value.u[0]; |
| 804 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 805 | this->value = new(ctx) ir_constant(array, component); |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 806 | } else { |
| 807 | /* FINISHME: Handle access of constant arrays. */ |
| 808 | } |
| 809 | } |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | |
| 813 | void |
| 814 | ir_constant_visitor::visit(ir_dereference_record *ir) |
| 815 | { |
Ian Romanick | 253dede | 2010-06-09 17:30:19 -0700 | [diff] [blame] | 816 | ir_constant *v = ir->record->constant_expression_value(); |
| 817 | |
| 818 | this->value = (v != NULL) ? v->get_record_field(ir->field) : NULL; |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | |
| 822 | void |
| 823 | ir_constant_visitor::visit(ir_assignment *ir) |
| 824 | { |
| 825 | (void) ir; |
| 826 | value = NULL; |
| 827 | } |
| 828 | |
| 829 | |
| 830 | void |
| 831 | ir_constant_visitor::visit(ir_constant *ir) |
| 832 | { |
| 833 | value = ir; |
| 834 | } |
| 835 | |
| 836 | |
| 837 | void |
| 838 | ir_constant_visitor::visit(ir_call *ir) |
| 839 | { |
| 840 | (void) ir; |
| 841 | value = NULL; |
| 842 | } |
| 843 | |
| 844 | |
| 845 | void |
| 846 | ir_constant_visitor::visit(ir_return *ir) |
| 847 | { |
| 848 | (void) ir; |
| 849 | value = NULL; |
| 850 | } |
| 851 | |
| 852 | |
| 853 | void |
Kenneth Graunke | 16efab1 | 2010-06-30 10:47:34 -0700 | [diff] [blame] | 854 | ir_constant_visitor::visit(ir_discard *ir) |
| 855 | { |
| 856 | (void) ir; |
| 857 | value = NULL; |
| 858 | } |
| 859 | |
| 860 | |
| 861 | void |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 862 | ir_constant_visitor::visit(ir_if *ir) |
| 863 | { |
| 864 | (void) ir; |
| 865 | value = NULL; |
| 866 | } |
Ian Romanick | fad607a | 2010-04-05 16:16:07 -0700 | [diff] [blame] | 867 | |
| 868 | |
| 869 | void |
| 870 | ir_constant_visitor::visit(ir_loop *ir) |
| 871 | { |
| 872 | (void) ir; |
| 873 | value = NULL; |
| 874 | } |
Ian Romanick | f8e31e0 | 2010-04-05 16:28:15 -0700 | [diff] [blame] | 875 | |
| 876 | |
| 877 | void |
| 878 | ir_constant_visitor::visit(ir_loop_jump *ir) |
| 879 | { |
| 880 | (void) ir; |
| 881 | value = NULL; |
| 882 | } |