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