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> |
Kenneth Graunke | f914915 | 2010-07-21 21:56:13 -0700 | [diff] [blame] | 37 | #include "main/macros.h" |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 38 | #include "ir.h" |
| 39 | #include "ir_visitor.h" |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 40 | #include "glsl_types.h" |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 41 | |
Kenneth Graunke | ffcec13 | 2010-07-21 20:09:21 -0700 | [diff] [blame] | 42 | static float |
| 43 | dot(ir_constant *op0, ir_constant *op1) |
| 44 | { |
| 45 | assert(op0->type->is_float() && op1->type->is_float()); |
| 46 | |
| 47 | float result = 0; |
| 48 | for (unsigned c = 0; c < op0->type->components(); c++) |
| 49 | result += op0->value.f[c] * op1->value.f[c]; |
| 50 | |
| 51 | return result; |
| 52 | } |
| 53 | |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 54 | ir_constant * |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 55 | ir_expression::constant_expression_value() |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 56 | { |
Kenneth Graunke | 6bc432e | 2010-07-02 17:12:23 -0700 | [diff] [blame] | 57 | ir_constant *op[2] = { NULL, NULL }; |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 58 | ir_constant_data data; |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame] | 59 | |
Kenneth Graunke | c63a1db | 2010-07-05 22:33:35 -0700 | [diff] [blame] | 60 | memset(&data, 0, sizeof(data)); |
| 61 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 62 | for (unsigned operand = 0; operand < this->get_num_operands(); operand++) { |
| 63 | op[operand] = this->operands[operand]->constant_expression_value(); |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 64 | if (!op[operand]) |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 65 | return NULL; |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame] | 66 | } |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 67 | |
Kenneth Graunke | 6fc983b | 2010-07-06 02:39:57 -0700 | [diff] [blame] | 68 | if (op[1] != NULL) |
| 69 | assert(op[0]->type->base_type == op[1]->type->base_type); |
| 70 | |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 71 | bool op0_scalar = op[0]->type->is_scalar(); |
| 72 | bool op1_scalar = op[1] != NULL && op[1]->type->is_scalar(); |
| 73 | |
| 74 | /* When iterating over a vector or matrix's components, we want to increase |
| 75 | * the loop counter. However, for scalars, we want to stay at 0. |
| 76 | */ |
Kenneth Graunke | acf88f2 | 2010-07-07 12:08:23 -0700 | [diff] [blame] | 77 | unsigned c0_inc = op0_scalar ? 0 : 1; |
| 78 | unsigned c1_inc = op1_scalar ? 0 : 1; |
Eric Anholt | 570dc0d | 2010-07-07 09:07:09 -0700 | [diff] [blame] | 79 | unsigned components; |
| 80 | if (op1_scalar || !op[1]) { |
| 81 | components = op[0]->type->components(); |
| 82 | } else { |
| 83 | components = op[1]->type->components(); |
| 84 | } |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 85 | |
Kenneth Graunke | 9a6d40f | 2010-07-20 03:08:32 -0700 | [diff] [blame] | 86 | void *ctx = talloc_parent(this); |
| 87 | |
| 88 | /* Handle array operations here, rather than below. */ |
| 89 | if (op[0]->type->is_array()) { |
| 90 | assert(op[1] != NULL && op[1]->type->is_array()); |
| 91 | switch (this->operation) { |
| 92 | case ir_binop_equal: |
| 93 | return new(ctx) ir_constant(op[0]->has_value(op[1])); |
| 94 | case ir_binop_nequal: |
| 95 | return new(ctx) ir_constant(!op[0]->has_value(op[1])); |
| 96 | default: |
| 97 | break; |
| 98 | } |
| 99 | return NULL; |
| 100 | } |
| 101 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 102 | switch (this->operation) { |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 103 | case ir_unop_logic_not: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 104 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 105 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 106 | data.b[c] = !op[0]->value.b[c]; |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 107 | break; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 108 | |
| 109 | case ir_unop_f2i: |
| 110 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 111 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 112 | data.i[c] = op[0]->value.f[c]; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 113 | } |
| 114 | break; |
| 115 | case ir_unop_i2f: |
Kenneth Graunke | 46d6b8d | 2010-07-20 13:01:56 -0700 | [diff] [blame] | 116 | assert(op[0]->type->base_type == GLSL_TYPE_INT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 117 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | 46d6b8d | 2010-07-20 13:01:56 -0700 | [diff] [blame] | 118 | data.f[c] = op[0]->value.i[c]; |
| 119 | } |
| 120 | break; |
| 121 | case ir_unop_u2f: |
| 122 | assert(op[0]->type->base_type == GLSL_TYPE_UINT); |
| 123 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 124 | data.f[c] = op[0]->value.u[c]; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 125 | } |
| 126 | break; |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 127 | case ir_unop_b2f: |
| 128 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 129 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 130 | data.f[c] = op[0]->value.b[c] ? 1.0 : 0.0; |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 131 | } |
| 132 | break; |
| 133 | case ir_unop_f2b: |
| 134 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 135 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 136 | data.b[c] = bool(op[0]->value.f[c]); |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 137 | } |
| 138 | break; |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 139 | case ir_unop_b2i: |
| 140 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 141 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 142 | data.u[c] = op[0]->value.b[c] ? 1 : 0; |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 143 | } |
| 144 | break; |
| 145 | case ir_unop_i2b: |
| 146 | assert(op[0]->type->is_integer()); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 147 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 148 | data.b[c] = bool(op[0]->value.u[c]); |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 149 | } |
| 150 | break; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 151 | |
Eric Anholt | 5e9ac94 | 2010-08-23 12:21:33 -0700 | [diff] [blame] | 152 | case ir_unop_any: |
| 153 | assert(op[0]->type->is_boolean()); |
| 154 | data.b[0] = false; |
| 155 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 156 | if (op[0]->value.b[c]) |
| 157 | data.b[0] = true; |
| 158 | } |
| 159 | break; |
| 160 | |
Kenneth Graunke | 323d909 | 2010-07-08 23:21:36 -0700 | [diff] [blame] | 161 | case ir_unop_trunc: |
| 162 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 163 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | 323d909 | 2010-07-08 23:21:36 -0700 | [diff] [blame] | 164 | data.f[c] = truncf(op[0]->value.f[c]); |
| 165 | } |
| 166 | break; |
| 167 | |
Kenneth Graunke | c1ee30a | 2010-07-08 23:22:36 -0700 | [diff] [blame] | 168 | case ir_unop_ceil: |
| 169 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 170 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | c1ee30a | 2010-07-08 23:22:36 -0700 | [diff] [blame] | 171 | data.f[c] = ceilf(op[0]->value.f[c]); |
| 172 | } |
| 173 | break; |
| 174 | |
Kenneth Graunke | 0747204 | 2010-07-08 23:23:23 -0700 | [diff] [blame] | 175 | case ir_unop_floor: |
| 176 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 177 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | 0747204 | 2010-07-08 23:23:23 -0700 | [diff] [blame] | 178 | data.f[c] = floorf(op[0]->value.f[c]); |
| 179 | } |
| 180 | break; |
| 181 | |
Eric Anholt | d925c91 | 2010-07-01 10:37:11 -0700 | [diff] [blame] | 182 | case ir_unop_fract: |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 183 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 184 | switch (this->type->base_type) { |
Eric Anholt | d925c91 | 2010-07-01 10:37:11 -0700 | [diff] [blame] | 185 | case GLSL_TYPE_UINT: |
| 186 | data.u[c] = 0; |
| 187 | break; |
| 188 | case GLSL_TYPE_INT: |
| 189 | data.i[c] = 0; |
| 190 | break; |
| 191 | case GLSL_TYPE_FLOAT: |
| 192 | data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]); |
| 193 | break; |
| 194 | default: |
| 195 | assert(0); |
| 196 | } |
| 197 | } |
| 198 | break; |
| 199 | |
Kenneth Graunke | 908afd1 | 2010-07-08 23:28:50 -0700 | [diff] [blame] | 200 | case ir_unop_sin: |
| 201 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 202 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | 908afd1 | 2010-07-08 23:28:50 -0700 | [diff] [blame] | 203 | data.f[c] = sinf(op[0]->value.f[c]); |
| 204 | } |
| 205 | break; |
| 206 | |
Kenneth Graunke | 3fab376 | 2010-07-08 23:29:37 -0700 | [diff] [blame] | 207 | case ir_unop_cos: |
| 208 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 209 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | 3fab376 | 2010-07-08 23:29:37 -0700 | [diff] [blame] | 210 | data.f[c] = cosf(op[0]->value.f[c]); |
| 211 | } |
| 212 | break; |
| 213 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 214 | case ir_unop_neg: |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 215 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 216 | switch (this->type->base_type) { |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 217 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 218 | data.u[c] = -op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 219 | break; |
| 220 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 221 | data.i[c] = -op[0]->value.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 222 | break; |
| 223 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 224 | data.f[c] = -op[0]->value.f[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 225 | break; |
| 226 | default: |
| 227 | assert(0); |
| 228 | } |
| 229 | } |
| 230 | break; |
| 231 | |
| 232 | case ir_unop_abs: |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 233 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 234 | switch (this->type->base_type) { |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 235 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 236 | data.u[c] = op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 237 | break; |
| 238 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 239 | data.i[c] = op[0]->value.i[c]; |
| 240 | if (data.i[c] < 0) |
| 241 | data.i[c] = -data.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 242 | break; |
| 243 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 244 | data.f[c] = fabs(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 245 | break; |
| 246 | default: |
| 247 | assert(0); |
| 248 | } |
| 249 | } |
| 250 | break; |
| 251 | |
Kenneth Graunke | 14b7b26 | 2010-07-08 23:11:14 -0700 | [diff] [blame] | 252 | case ir_unop_sign: |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 253 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 254 | switch (this->type->base_type) { |
Kenneth Graunke | 14b7b26 | 2010-07-08 23:11:14 -0700 | [diff] [blame] | 255 | case GLSL_TYPE_UINT: |
| 256 | data.u[c] = op[0]->value.i[c] > 0; |
| 257 | break; |
| 258 | case GLSL_TYPE_INT: |
| 259 | data.i[c] = (op[0]->value.i[c] > 0) - (op[0]->value.i[c] < 0); |
| 260 | break; |
| 261 | case GLSL_TYPE_FLOAT: |
| 262 | data.f[c] = float((op[0]->value.f[c] > 0)-(op[0]->value.f[c] < 0)); |
| 263 | break; |
| 264 | default: |
| 265 | assert(0); |
| 266 | } |
| 267 | } |
| 268 | break; |
| 269 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 270 | case ir_unop_rcp: |
| 271 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 272 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 273 | switch (this->type->base_type) { |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 274 | case GLSL_TYPE_UINT: |
| 275 | if (op[0]->value.u[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 276 | data.u[c] = 1 / op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 277 | break; |
| 278 | case GLSL_TYPE_INT: |
| 279 | if (op[0]->value.i[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 280 | data.i[c] = 1 / op[0]->value.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 281 | break; |
| 282 | case GLSL_TYPE_FLOAT: |
| 283 | if (op[0]->value.f[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 284 | data.f[c] = 1.0 / op[0]->value.f[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 285 | break; |
| 286 | default: |
| 287 | assert(0); |
| 288 | } |
| 289 | } |
| 290 | break; |
| 291 | |
| 292 | case ir_unop_rsq: |
| 293 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 294 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 295 | data.f[c] = 1.0 / sqrtf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 296 | } |
| 297 | break; |
| 298 | |
| 299 | case ir_unop_sqrt: |
| 300 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 301 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 302 | data.f[c] = sqrtf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 303 | } |
| 304 | break; |
| 305 | |
| 306 | case ir_unop_exp: |
| 307 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 308 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 309 | data.f[c] = expf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 310 | } |
| 311 | break; |
| 312 | |
Kenneth Graunke | aca01ed | 2010-07-08 23:14:32 -0700 | [diff] [blame] | 313 | case ir_unop_exp2: |
| 314 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 315 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | aca01ed | 2010-07-08 23:14:32 -0700 | [diff] [blame] | 316 | data.f[c] = exp2f(op[0]->value.f[c]); |
| 317 | } |
| 318 | break; |
| 319 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 320 | case ir_unop_log: |
| 321 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 322 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 323 | data.f[c] = logf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 324 | } |
| 325 | break; |
| 326 | |
Kenneth Graunke | cb63929 | 2010-07-08 23:18:09 -0700 | [diff] [blame] | 327 | case ir_unop_log2: |
| 328 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 329 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | cb63929 | 2010-07-08 23:18:09 -0700 | [diff] [blame] | 330 | data.f[c] = log2f(op[0]->value.f[c]); |
| 331 | } |
| 332 | break; |
| 333 | |
Kenneth Graunke | d6a32d4 | 2010-06-09 15:22:35 -0700 | [diff] [blame] | 334 | case ir_unop_dFdx: |
| 335 | case ir_unop_dFdy: |
| 336 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 337 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 338 | data.f[c] = 0.0; |
Kenneth Graunke | d6a32d4 | 2010-06-09 15:22:35 -0700 | [diff] [blame] | 339 | } |
| 340 | break; |
| 341 | |
Kenneth Graunke | 891a064 | 2010-07-08 23:35:09 -0700 | [diff] [blame] | 342 | case ir_binop_pow: |
| 343 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 344 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | 891a064 | 2010-07-08 23:35:09 -0700 | [diff] [blame] | 345 | data.f[c] = powf(op[0]->value.f[c], op[1]->value.f[c]); |
| 346 | } |
| 347 | break; |
| 348 | |
Kenneth Graunke | 3f4a0b8 | 2010-07-05 21:15:32 -0700 | [diff] [blame] | 349 | case ir_binop_dot: |
Kenneth Graunke | ffcec13 | 2010-07-21 20:09:21 -0700 | [diff] [blame] | 350 | data.f[0] = dot(op[0], op[1]); |
Kenneth Graunke | 3f4a0b8 | 2010-07-05 21:15:32 -0700 | [diff] [blame] | 351 | break; |
Kenneth Graunke | ffcec13 | 2010-07-21 20:09:21 -0700 | [diff] [blame] | 352 | |
Kenneth Graunke | 79fed37 | 2010-07-09 11:53:56 -0700 | [diff] [blame] | 353 | case ir_binop_min: |
| 354 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 355 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 356 | c < components; |
| 357 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 358 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 359 | switch (op[0]->type->base_type) { |
Kenneth Graunke | 79fed37 | 2010-07-09 11:53:56 -0700 | [diff] [blame] | 360 | case GLSL_TYPE_UINT: |
Kenneth Graunke | f914915 | 2010-07-21 21:56:13 -0700 | [diff] [blame] | 361 | data.u[c] = MIN2(op[0]->value.u[c0], op[1]->value.u[c1]); |
Kenneth Graunke | 79fed37 | 2010-07-09 11:53:56 -0700 | [diff] [blame] | 362 | break; |
| 363 | case GLSL_TYPE_INT: |
Kenneth Graunke | f914915 | 2010-07-21 21:56:13 -0700 | [diff] [blame] | 364 | data.i[c] = MIN2(op[0]->value.i[c0], op[1]->value.i[c1]); |
Kenneth Graunke | 79fed37 | 2010-07-09 11:53:56 -0700 | [diff] [blame] | 365 | break; |
| 366 | case GLSL_TYPE_FLOAT: |
Kenneth Graunke | f914915 | 2010-07-21 21:56:13 -0700 | [diff] [blame] | 367 | data.f[c] = MIN2(op[0]->value.f[c0], op[1]->value.f[c1]); |
Kenneth Graunke | 79fed37 | 2010-07-09 11:53:56 -0700 | [diff] [blame] | 368 | break; |
| 369 | default: |
| 370 | assert(0); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | break; |
| 375 | case ir_binop_max: |
| 376 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 377 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 378 | c < components; |
| 379 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 380 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 381 | switch (op[0]->type->base_type) { |
Kenneth Graunke | 79fed37 | 2010-07-09 11:53:56 -0700 | [diff] [blame] | 382 | case GLSL_TYPE_UINT: |
Kenneth Graunke | f914915 | 2010-07-21 21:56:13 -0700 | [diff] [blame] | 383 | data.u[c] = MAX2(op[0]->value.u[c0], op[1]->value.u[c1]); |
Kenneth Graunke | 79fed37 | 2010-07-09 11:53:56 -0700 | [diff] [blame] | 384 | break; |
| 385 | case GLSL_TYPE_INT: |
Kenneth Graunke | f914915 | 2010-07-21 21:56:13 -0700 | [diff] [blame] | 386 | data.i[c] = MAX2(op[0]->value.i[c0], op[1]->value.i[c1]); |
Kenneth Graunke | 79fed37 | 2010-07-09 11:53:56 -0700 | [diff] [blame] | 387 | break; |
| 388 | case GLSL_TYPE_FLOAT: |
Kenneth Graunke | f914915 | 2010-07-21 21:56:13 -0700 | [diff] [blame] | 389 | data.f[c] = MAX2(op[0]->value.f[c0], op[1]->value.f[c1]); |
Kenneth Graunke | 79fed37 | 2010-07-09 11:53:56 -0700 | [diff] [blame] | 390 | break; |
| 391 | default: |
| 392 | assert(0); |
| 393 | } |
| 394 | } |
Kenneth Graunke | 79fed37 | 2010-07-09 11:53:56 -0700 | [diff] [blame] | 395 | break; |
Eric Anholt | 9be7f63 | 2010-07-13 15:37:57 -0700 | [diff] [blame] | 396 | |
| 397 | case ir_binop_cross: |
| 398 | assert(op[0]->type == glsl_type::vec3_type); |
| 399 | assert(op[1]->type == glsl_type::vec3_type); |
| 400 | data.f[0] = (op[0]->value.f[1] * op[1]->value.f[2] - |
| 401 | op[1]->value.f[1] * op[0]->value.f[2]); |
| 402 | data.f[1] = (op[0]->value.f[2] * op[1]->value.f[0] - |
| 403 | op[1]->value.f[2] * op[0]->value.f[0]); |
| 404 | data.f[2] = (op[0]->value.f[0] * op[1]->value.f[1] - |
| 405 | op[1]->value.f[0] * op[0]->value.f[1]); |
| 406 | break; |
| 407 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 408 | case ir_binop_add: |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 409 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 410 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 411 | c < components; |
| 412 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 413 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 414 | switch (op[0]->type->base_type) { |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 415 | case GLSL_TYPE_UINT: |
| 416 | data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1]; |
| 417 | break; |
| 418 | case GLSL_TYPE_INT: |
| 419 | data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1]; |
| 420 | break; |
| 421 | case GLSL_TYPE_FLOAT: |
| 422 | data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1]; |
| 423 | break; |
| 424 | default: |
| 425 | assert(0); |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 426 | } |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 427 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 428 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 429 | break; |
| 430 | case ir_binop_sub: |
Kenneth Graunke | 97b44f0 | 2010-07-06 02:53:29 -0700 | [diff] [blame] | 431 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 432 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 433 | c < components; |
| 434 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 435 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 436 | switch (op[0]->type->base_type) { |
Kenneth Graunke | 97b44f0 | 2010-07-06 02:53:29 -0700 | [diff] [blame] | 437 | case GLSL_TYPE_UINT: |
| 438 | data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1]; |
| 439 | break; |
| 440 | case GLSL_TYPE_INT: |
| 441 | data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1]; |
| 442 | break; |
| 443 | case GLSL_TYPE_FLOAT: |
| 444 | data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1]; |
| 445 | break; |
| 446 | default: |
| 447 | assert(0); |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 448 | } |
Kenneth Graunke | 97b44f0 | 2010-07-06 02:53:29 -0700 | [diff] [blame] | 449 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 450 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 451 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 452 | case ir_binop_mul: |
Kenneth Graunke | cf80a4d | 2010-07-05 23:19:56 -0700 | [diff] [blame] | 453 | /* Check for equal types, or unequal types involving scalars */ |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 454 | if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix()) |
| 455 | || op0_scalar || op1_scalar) { |
| 456 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 457 | c < components; |
| 458 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 459 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 460 | switch (op[0]->type->base_type) { |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 461 | case GLSL_TYPE_UINT: |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 462 | 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] | 463 | break; |
| 464 | case GLSL_TYPE_INT: |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 465 | 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] | 466 | break; |
| 467 | case GLSL_TYPE_FLOAT: |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 468 | 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] | 469 | break; |
| 470 | default: |
| 471 | assert(0); |
| 472 | } |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 473 | } |
Kenneth Graunke | cf80a4d | 2010-07-05 23:19:56 -0700 | [diff] [blame] | 474 | } else { |
| 475 | assert(op[0]->type->is_matrix() || op[1]->type->is_matrix()); |
| 476 | |
| 477 | /* Multiply an N-by-M matrix with an M-by-P matrix. Since either |
| 478 | * matrix can be a GLSL vector, either N or P can be 1. |
| 479 | * |
| 480 | * For vec*mat, the vector is treated as a row vector. This |
| 481 | * means the vector is a 1-row x M-column matrix. |
| 482 | * |
| 483 | * For mat*vec, the vector is treated as a column vector. Since |
| 484 | * matrix_columns is 1 for vectors, this just works. |
| 485 | */ |
| 486 | const unsigned n = op[0]->type->is_vector() |
| 487 | ? 1 : op[0]->type->vector_elements; |
| 488 | const unsigned m = op[1]->type->vector_elements; |
| 489 | const unsigned p = op[1]->type->matrix_columns; |
| 490 | for (unsigned j = 0; j < p; j++) { |
| 491 | for (unsigned i = 0; i < n; i++) { |
| 492 | for (unsigned k = 0; k < m; k++) { |
| 493 | data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j]; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 498 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 499 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 500 | case ir_binop_div: |
Kenneth Graunke | dad35eb | 2010-07-06 02:56:36 -0700 | [diff] [blame] | 501 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 502 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 503 | c < components; |
| 504 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 505 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 506 | switch (op[0]->type->base_type) { |
Kenneth Graunke | dad35eb | 2010-07-06 02:56:36 -0700 | [diff] [blame] | 507 | case GLSL_TYPE_UINT: |
| 508 | data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1]; |
| 509 | break; |
| 510 | case GLSL_TYPE_INT: |
| 511 | data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1]; |
| 512 | break; |
| 513 | case GLSL_TYPE_FLOAT: |
| 514 | data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1]; |
| 515 | break; |
| 516 | default: |
| 517 | assert(0); |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 518 | } |
Kenneth Graunke | dad35eb | 2010-07-06 02:56:36 -0700 | [diff] [blame] | 519 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 520 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 521 | break; |
Kenneth Graunke | ce5ae5f | 2010-07-14 11:28:40 -0700 | [diff] [blame] | 522 | case ir_binop_mod: |
| 523 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 524 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 525 | c < components; |
| 526 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 527 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 528 | switch (op[0]->type->base_type) { |
Kenneth Graunke | ce5ae5f | 2010-07-14 11:28:40 -0700 | [diff] [blame] | 529 | case GLSL_TYPE_UINT: |
| 530 | data.u[c] = op[0]->value.u[c0] % op[1]->value.u[c1]; |
| 531 | break; |
| 532 | case GLSL_TYPE_INT: |
| 533 | data.i[c] = op[0]->value.i[c0] % op[1]->value.i[c1]; |
| 534 | break; |
| 535 | case GLSL_TYPE_FLOAT: |
| 536 | /* We don't use fmod because it rounds toward zero; GLSL specifies |
| 537 | * the use of floor. |
| 538 | */ |
Kenneth Graunke | 3e882ec | 2010-07-22 17:44:34 -0700 | [diff] [blame] | 539 | data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1] |
Kenneth Graunke | ce5ae5f | 2010-07-14 11:28:40 -0700 | [diff] [blame] | 540 | * floorf(op[0]->value.f[c0] / op[1]->value.f[c1]); |
| 541 | break; |
| 542 | default: |
| 543 | assert(0); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | break; |
| 548 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 549 | case ir_binop_logic_and: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 550 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 551 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 552 | 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] | 553 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 554 | case ir_binop_logic_xor: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 555 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 556 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 557 | 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] | 558 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 559 | case ir_binop_logic_or: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 560 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 561 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 562 | 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] | 563 | break; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 564 | |
| 565 | case ir_binop_less: |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 566 | switch (op[0]->type->base_type) { |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 567 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 568 | 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] | 569 | break; |
| 570 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 571 | 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] | 572 | break; |
| 573 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 574 | 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] | 575 | break; |
| 576 | default: |
| 577 | assert(0); |
| 578 | } |
| 579 | break; |
| 580 | case ir_binop_greater: |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 581 | switch (op[0]->type->base_type) { |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 582 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 583 | 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] | 584 | break; |
| 585 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 586 | 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] | 587 | break; |
| 588 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 589 | 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] | 590 | break; |
| 591 | default: |
| 592 | assert(0); |
| 593 | } |
| 594 | break; |
| 595 | case ir_binop_lequal: |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 596 | switch (op[0]->type->base_type) { |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 597 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 598 | 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] | 599 | break; |
| 600 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 601 | 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] | 602 | break; |
| 603 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 604 | 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] | 605 | break; |
| 606 | default: |
| 607 | assert(0); |
| 608 | } |
| 609 | break; |
| 610 | case ir_binop_gequal: |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 611 | switch (op[0]->type->base_type) { |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 612 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 613 | 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] | 614 | break; |
| 615 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 616 | 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] | 617 | break; |
| 618 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 619 | 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] | 620 | break; |
| 621 | default: |
| 622 | assert(0); |
| 623 | } |
| 624 | break; |
| 625 | |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 626 | case ir_binop_equal: |
Kenneth Graunke | 3163f87 | 2010-07-20 03:01:54 -0700 | [diff] [blame] | 627 | data.b[0] = op[0]->has_value(op[1]); |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 628 | break; |
| 629 | case ir_binop_nequal: |
Kenneth Graunke | 3163f87 | 2010-07-20 03:01:54 -0700 | [diff] [blame] | 630 | data.b[0] = !op[0]->has_value(op[1]); |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 631 | break; |
| 632 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 633 | default: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 634 | /* FINISHME: Should handle all expression types. */ |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 635 | return NULL; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 636 | } |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 637 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 638 | return new(ctx) ir_constant(this->type, &data); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 642 | ir_constant * |
| 643 | ir_texture::constant_expression_value() |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 644 | { |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 645 | /* texture lookups aren't constant expressions */ |
| 646 | return NULL; |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 650 | ir_constant * |
| 651 | ir_swizzle::constant_expression_value() |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 652 | { |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 653 | ir_constant *v = this->val->constant_expression_value(); |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 654 | |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 655 | if (v != NULL) { |
Ian Romanick | 0bb70a3 | 2010-06-11 15:49:49 -0700 | [diff] [blame] | 656 | ir_constant_data data; |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 657 | |
| 658 | const unsigned swiz_idx[4] = { |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 659 | this->mask.x, this->mask.y, this->mask.z, this->mask.w |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 660 | }; |
| 661 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 662 | for (unsigned i = 0; i < this->mask.num_components; i++) { |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 663 | switch (v->type->base_type) { |
| 664 | case GLSL_TYPE_UINT: |
| 665 | case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break; |
| 666 | case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break; |
| 667 | case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break; |
| 668 | default: assert(!"Should not get here."); break; |
| 669 | } |
| 670 | } |
| 671 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 672 | void *ctx = talloc_parent(this); |
| 673 | return new(ctx) ir_constant(this->type, &data); |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 674 | } |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 675 | return NULL; |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 679 | ir_constant * |
| 680 | ir_dereference_variable::constant_expression_value() |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 681 | { |
Eric Anholt | 54f583a | 2010-07-27 12:10:50 -0700 | [diff] [blame] | 682 | /* This may occur during compile and var->type is glsl_type::error_type */ |
| 683 | if (!var) |
| 684 | return NULL; |
| 685 | |
Eric Anholt | 5704ed2 | 2010-08-02 15:31:28 -0700 | [diff] [blame] | 686 | /* The constant_value of a uniform variable is its initializer, |
| 687 | * not the lifetime constant value of the uniform. |
| 688 | */ |
| 689 | if (var->mode == ir_var_uniform) |
| 690 | return NULL; |
| 691 | |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 692 | if (!var->constant_value) |
| 693 | return NULL; |
| 694 | |
| 695 | return var->constant_value->clone(talloc_parent(var), NULL); |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 699 | ir_constant * |
| 700 | ir_dereference_array::constant_expression_value() |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 701 | { |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 702 | ir_constant *array = this->array->constant_expression_value(); |
| 703 | ir_constant *idx = this->array_index->constant_expression_value(); |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 704 | |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 705 | if ((array != NULL) && (idx != NULL)) { |
Eric Anholt | 952d0f8 | 2010-08-04 12:57:58 -0700 | [diff] [blame] | 706 | void *ctx = talloc_parent(this); |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 707 | if (array->type->is_matrix()) { |
| 708 | /* Array access of a matrix results in a vector. |
| 709 | */ |
| 710 | const unsigned column = idx->value.u[0]; |
| 711 | |
| 712 | const glsl_type *const column_type = array->type->column_type(); |
| 713 | |
| 714 | /* Offset in the constant matrix to the first element of the column |
| 715 | * to be extracted. |
| 716 | */ |
| 717 | const unsigned mat_idx = column * column_type->vector_elements; |
| 718 | |
Ian Romanick | 0bb70a3 | 2010-06-11 15:49:49 -0700 | [diff] [blame] | 719 | ir_constant_data data; |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 720 | |
| 721 | switch (column_type->base_type) { |
| 722 | case GLSL_TYPE_UINT: |
| 723 | case GLSL_TYPE_INT: |
| 724 | for (unsigned i = 0; i < column_type->vector_elements; i++) |
| 725 | data.u[i] = array->value.u[mat_idx + i]; |
| 726 | |
| 727 | break; |
| 728 | |
| 729 | case GLSL_TYPE_FLOAT: |
| 730 | for (unsigned i = 0; i < column_type->vector_elements; i++) |
| 731 | data.f[i] = array->value.f[mat_idx + i]; |
| 732 | |
| 733 | break; |
| 734 | |
| 735 | default: |
| 736 | assert(!"Should not get here."); |
| 737 | break; |
| 738 | } |
| 739 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 740 | return new(ctx) ir_constant(column_type, &data); |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 741 | } else if (array->type->is_vector()) { |
| 742 | const unsigned component = idx->value.u[0]; |
| 743 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 744 | return new(ctx) ir_constant(array, component); |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 745 | } else { |
Kenneth Graunke | a096fa7 | 2010-07-20 01:31:29 -0700 | [diff] [blame] | 746 | const unsigned index = idx->value.u[0]; |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 747 | return array->get_array_element(index)->clone(ctx, NULL); |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 748 | } |
| 749 | } |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 750 | return NULL; |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 754 | ir_constant * |
| 755 | ir_dereference_record::constant_expression_value() |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 756 | { |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 757 | ir_constant *v = this->record->constant_expression_value(); |
Ian Romanick | 253dede | 2010-06-09 17:30:19 -0700 | [diff] [blame] | 758 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame] | 759 | return (v != NULL) ? v->get_record_field(this->field) : NULL; |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 763 | ir_constant * |
| 764 | ir_assignment::constant_expression_value() |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 765 | { |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 766 | /* FINISHME: Handle CEs involving assignment (return RHS) */ |
| 767 | return NULL; |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 771 | ir_constant * |
| 772 | ir_constant::constant_expression_value() |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 773 | { |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 774 | return this; |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 778 | ir_constant * |
| 779 | ir_call::constant_expression_value() |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 780 | { |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 781 | if (this->type == glsl_type::error_type) |
| 782 | return NULL; |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 783 | |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 784 | /* From the GLSL 1.20 spec, page 23: |
| 785 | * "Function calls to user-defined functions (non-built-in functions) |
| 786 | * cannot be used to form constant expressions." |
| 787 | */ |
| 788 | if (!this->callee->is_built_in) |
| 789 | return NULL; |
| 790 | |
| 791 | unsigned num_parameters = 0; |
| 792 | |
| 793 | /* Check if all parameters are constant */ |
| 794 | ir_constant *op[3]; |
| 795 | foreach_list(n, &this->actual_parameters) { |
| 796 | ir_constant *constant = ((ir_rvalue *) n)->constant_expression_value(); |
| 797 | if (constant == NULL) |
| 798 | return NULL; |
| 799 | |
| 800 | op[num_parameters] = constant; |
| 801 | |
| 802 | assert(num_parameters < 3); |
| 803 | num_parameters++; |
| 804 | } |
| 805 | |
| 806 | /* Individual cases below can either: |
| 807 | * - Assign "expr" a new ir_expression to evaluate (for basic opcodes) |
| 808 | * - Fill "data" with appopriate constant data |
| 809 | * - Return an ir_constant directly. |
| 810 | */ |
| 811 | void *mem_ctx = talloc_parent(this); |
| 812 | ir_expression *expr = NULL; |
| 813 | |
| 814 | ir_constant_data data; |
| 815 | memset(&data, 0, sizeof(data)); |
| 816 | |
| 817 | const char *callee = this->callee_name(); |
| 818 | if (strcmp(callee, "abs") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 819 | expr = new(mem_ctx) ir_expression(ir_unop_abs, type, op[0], NULL); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 820 | } else if (strcmp(callee, "all") == 0) { |
Kenneth Graunke | aca7e95 | 2010-07-21 17:34:32 -0700 | [diff] [blame] | 821 | assert(op[0]->type->is_boolean()); |
| 822 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 823 | if (!op[0]->value.b[c]) |
| 824 | return new(mem_ctx) ir_constant(false); |
| 825 | } |
| 826 | return new(mem_ctx) ir_constant(true); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 827 | } else if (strcmp(callee, "any") == 0) { |
Kenneth Graunke | d6792a7 | 2010-07-21 17:35:52 -0700 | [diff] [blame] | 828 | assert(op[0]->type->is_boolean()); |
| 829 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 830 | if (op[0]->value.b[c]) |
| 831 | return new(mem_ctx) ir_constant(true); |
| 832 | } |
| 833 | return new(mem_ctx) ir_constant(false); |
Kenneth Graunke | f6ea9df | 2010-07-21 17:39:47 -0700 | [diff] [blame] | 834 | } else if (strcmp(callee, "acos") == 0) { |
| 835 | assert(op[0]->type->is_float()); |
| 836 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
| 837 | data.f[c] = acosf(op[0]->value.f[c]); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 838 | } else if (strcmp(callee, "asin") == 0) { |
Kenneth Graunke | 3b6c29b | 2010-07-21 17:38:38 -0700 | [diff] [blame] | 839 | assert(op[0]->type->is_float()); |
| 840 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
| 841 | data.f[c] = asinf(op[0]->value.f[c]); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 842 | } else if (strcmp(callee, "atan") == 0) { |
Kenneth Graunke | 13f8758 | 2010-07-21 17:42:23 -0700 | [diff] [blame] | 843 | assert(op[0]->type->is_float()); |
| 844 | if (num_parameters == 2) { |
| 845 | assert(op[1]->type->is_float()); |
| 846 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
| 847 | data.f[c] = atan2f(op[0]->value.f[c], op[1]->value.f[c]); |
| 848 | } else { |
| 849 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
| 850 | data.f[c] = atanf(op[0]->value.f[c]); |
| 851 | } |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 852 | } else if (strcmp(callee, "dFdx") == 0 || strcmp(callee, "dFdy") == 0) { |
Kenneth Graunke | 38cb1b2 | 2010-07-21 16:57:10 -0700 | [diff] [blame] | 853 | return ir_constant::zero(mem_ctx, this->type); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 854 | } else if (strcmp(callee, "ceil") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 855 | expr = new(mem_ctx) ir_expression(ir_unop_ceil, type, op[0], NULL); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 856 | } else if (strcmp(callee, "clamp") == 0) { |
Kenneth Graunke | a4ca1cf | 2010-07-21 22:23:17 -0700 | [diff] [blame] | 857 | assert(num_parameters == 3); |
| 858 | unsigned c1_inc = op[1]->type->is_scalar() ? 0 : 1; |
| 859 | unsigned c2_inc = op[2]->type->is_scalar() ? 0 : 1; |
| 860 | for (unsigned c = 0, c1 = 0, c2 = 0; |
| 861 | c < op[0]->type->components(); |
| 862 | c1 += c1_inc, c2 += c2_inc, c++) { |
| 863 | |
| 864 | switch (op[0]->type->base_type) { |
| 865 | case GLSL_TYPE_UINT: |
| 866 | data.u[c] = CLAMP(op[0]->value.u[c], op[1]->value.u[c1], |
| 867 | op[2]->value.u[c2]); |
| 868 | break; |
| 869 | case GLSL_TYPE_INT: |
| 870 | data.i[c] = CLAMP(op[0]->value.i[c], op[1]->value.i[c1], |
Eric Anholt | c8babd5 | 2010-08-02 17:49:01 -0700 | [diff] [blame] | 871 | op[2]->value.i[c2]); |
Kenneth Graunke | a4ca1cf | 2010-07-21 22:23:17 -0700 | [diff] [blame] | 872 | break; |
| 873 | case GLSL_TYPE_FLOAT: |
| 874 | data.f[c] = CLAMP(op[0]->value.f[c], op[1]->value.f[c1], |
| 875 | op[2]->value.f[c2]); |
| 876 | break; |
| 877 | default: |
| 878 | assert(!"Should not get here."); |
| 879 | } |
| 880 | } |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 881 | } else if (strcmp(callee, "cos") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 882 | expr = new(mem_ctx) ir_expression(ir_unop_cos, type, op[0], NULL); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 883 | } else if (strcmp(callee, "cosh") == 0) { |
Kenneth Graunke | ba41783 | 2010-07-21 17:47:55 -0700 | [diff] [blame] | 884 | assert(op[0]->type->is_float()); |
| 885 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
| 886 | data.f[c] = coshf(op[0]->value.f[c]); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 887 | } else if (strcmp(callee, "cross") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 888 | expr = new(mem_ctx) ir_expression(ir_binop_cross, type, op[0], op[1]); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 889 | } else if (strcmp(callee, "degrees") == 0) { |
Kenneth Graunke | 7bcaa38 | 2010-07-21 17:59:00 -0700 | [diff] [blame] | 890 | assert(op[0]->type->is_float()); |
| 891 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
| 892 | data.f[c] = 180.0/M_PI * op[0]->value.f[c]; |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 893 | } else if (strcmp(callee, "distance") == 0) { |
Kenneth Graunke | 2eaf316 | 2010-07-21 18:01:22 -0700 | [diff] [blame] | 894 | assert(op[0]->type->is_float() && op[1]->type->is_float()); |
| 895 | float length_squared = 0.0; |
| 896 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 897 | float t = op[0]->value.f[c] - op[1]->value.f[c]; |
| 898 | length_squared += t * t; |
| 899 | } |
| 900 | return new(mem_ctx) ir_constant(sqrtf(length_squared)); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 901 | } else if (strcmp(callee, "dot") == 0) { |
Kenneth Graunke | a7650af | 2010-07-21 20:12:43 -0700 | [diff] [blame] | 902 | return new(mem_ctx) ir_constant(dot(op[0], op[1])); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 903 | } else if (strcmp(callee, "equal") == 0) { |
Kenneth Graunke | 0b6ef6e | 2010-07-21 18:04:22 -0700 | [diff] [blame] | 904 | assert(op[0]->type->is_vector() && op[1] && op[1]->type->is_vector()); |
| 905 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 906 | switch (op[0]->type->base_type) { |
| 907 | case GLSL_TYPE_UINT: |
| 908 | data.b[c] = op[0]->value.u[c] == op[1]->value.u[c]; |
| 909 | break; |
| 910 | case GLSL_TYPE_INT: |
| 911 | data.b[c] = op[0]->value.i[c] == op[1]->value.i[c]; |
| 912 | break; |
| 913 | case GLSL_TYPE_FLOAT: |
| 914 | data.b[c] = op[0]->value.f[c] == op[1]->value.f[c]; |
| 915 | break; |
Kenneth Graunke | d12cb77 | 2010-08-18 12:06:25 -0700 | [diff] [blame] | 916 | case GLSL_TYPE_BOOL: |
| 917 | data.b[c] = op[0]->value.b[c] == op[1]->value.b[c]; |
| 918 | break; |
Kenneth Graunke | 0b6ef6e | 2010-07-21 18:04:22 -0700 | [diff] [blame] | 919 | default: |
| 920 | assert(!"Should not get here."); |
| 921 | } |
| 922 | } |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 923 | } else if (strcmp(callee, "exp") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 924 | expr = new(mem_ctx) ir_expression(ir_unop_exp, type, op[0], NULL); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 925 | } else if (strcmp(callee, "exp2") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 926 | expr = new(mem_ctx) ir_expression(ir_unop_exp2, type, op[0], NULL); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 927 | } else if (strcmp(callee, "faceforward") == 0) { |
Kenneth Graunke | 3d5c2f0 | 2010-07-21 21:46:16 -0700 | [diff] [blame] | 928 | if (dot(op[2], op[1]) < 0) |
| 929 | return op[0]; |
| 930 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
| 931 | data.f[c] = -op[0]->value.f[c]; |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 932 | } else if (strcmp(callee, "floor") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 933 | expr = new(mem_ctx) ir_expression(ir_unop_floor, type, op[0], NULL); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 934 | } else if (strcmp(callee, "fract") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 935 | expr = new(mem_ctx) ir_expression(ir_unop_fract, type, op[0], NULL); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 936 | } else if (strcmp(callee, "fwidth") == 0) { |
Kenneth Graunke | 38cb1b2 | 2010-07-21 16:57:10 -0700 | [diff] [blame] | 937 | return ir_constant::zero(mem_ctx, this->type); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 938 | } else if (strcmp(callee, "greaterThan") == 0) { |
Kenneth Graunke | 7f042b9 | 2010-07-21 18:07:43 -0700 | [diff] [blame] | 939 | assert(op[0]->type->is_vector() && op[1] && op[1]->type->is_vector()); |
| 940 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 941 | switch (op[0]->type->base_type) { |
| 942 | case GLSL_TYPE_UINT: |
| 943 | data.b[c] = op[0]->value.u[c] > op[1]->value.u[c]; |
| 944 | break; |
| 945 | case GLSL_TYPE_INT: |
| 946 | data.b[c] = op[0]->value.i[c] > op[1]->value.i[c]; |
| 947 | break; |
| 948 | case GLSL_TYPE_FLOAT: |
| 949 | data.b[c] = op[0]->value.f[c] > op[1]->value.f[c]; |
| 950 | break; |
| 951 | default: |
| 952 | assert(!"Should not get here."); |
| 953 | } |
| 954 | } |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 955 | } else if (strcmp(callee, "greaterThanEqual") == 0) { |
Kenneth Graunke | 5578273 | 2010-07-21 18:08:05 -0700 | [diff] [blame] | 956 | assert(op[0]->type->is_vector() && op[1] && op[1]->type->is_vector()); |
| 957 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 958 | switch (op[0]->type->base_type) { |
| 959 | case GLSL_TYPE_UINT: |
| 960 | data.b[c] = op[0]->value.u[c] >= op[1]->value.u[c]; |
| 961 | break; |
| 962 | case GLSL_TYPE_INT: |
| 963 | data.b[c] = op[0]->value.i[c] >= op[1]->value.i[c]; |
| 964 | break; |
| 965 | case GLSL_TYPE_FLOAT: |
| 966 | data.b[c] = op[0]->value.f[c] >= op[1]->value.f[c]; |
| 967 | break; |
| 968 | default: |
| 969 | assert(!"Should not get here."); |
| 970 | } |
| 971 | } |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 972 | } else if (strcmp(callee, "inversesqrt") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 973 | expr = new(mem_ctx) ir_expression(ir_unop_rsq, type, op[0], NULL); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 974 | } else if (strcmp(callee, "length") == 0) { |
Kenneth Graunke | 5489ad0 | 2010-07-21 18:26:00 -0700 | [diff] [blame] | 975 | return new(mem_ctx) ir_constant(sqrtf(dot(op[0], op[0]))); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 976 | } else if (strcmp(callee, "lessThan") == 0) { |
Kenneth Graunke | 6d897f0 | 2010-07-21 18:06:18 -0700 | [diff] [blame] | 977 | assert(op[0]->type->is_vector() && op[1] && op[1]->type->is_vector()); |
| 978 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 979 | switch (op[0]->type->base_type) { |
| 980 | case GLSL_TYPE_UINT: |
| 981 | data.b[c] = op[0]->value.u[c] < op[1]->value.u[c]; |
| 982 | break; |
| 983 | case GLSL_TYPE_INT: |
| 984 | data.b[c] = op[0]->value.i[c] < op[1]->value.i[c]; |
| 985 | break; |
| 986 | case GLSL_TYPE_FLOAT: |
| 987 | data.b[c] = op[0]->value.f[c] < op[1]->value.f[c]; |
| 988 | break; |
| 989 | default: |
| 990 | assert(!"Should not get here."); |
| 991 | } |
| 992 | } |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 993 | } else if (strcmp(callee, "lessThanEqual") == 0) { |
Kenneth Graunke | 319f494 | 2010-07-21 18:07:09 -0700 | [diff] [blame] | 994 | assert(op[0]->type->is_vector() && op[1] && op[1]->type->is_vector()); |
| 995 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 996 | switch (op[0]->type->base_type) { |
| 997 | case GLSL_TYPE_UINT: |
| 998 | data.b[c] = op[0]->value.u[c] <= op[1]->value.u[c]; |
| 999 | break; |
| 1000 | case GLSL_TYPE_INT: |
| 1001 | data.b[c] = op[0]->value.i[c] <= op[1]->value.i[c]; |
| 1002 | break; |
| 1003 | case GLSL_TYPE_FLOAT: |
| 1004 | data.b[c] = op[0]->value.f[c] <= op[1]->value.f[c]; |
| 1005 | break; |
| 1006 | default: |
| 1007 | assert(!"Should not get here."); |
| 1008 | } |
| 1009 | } |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1010 | } else if (strcmp(callee, "log") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 1011 | expr = new(mem_ctx) ir_expression(ir_unop_log, type, op[0], NULL); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1012 | } else if (strcmp(callee, "log2") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 1013 | expr = new(mem_ctx) ir_expression(ir_unop_log2, type, op[0], NULL); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1014 | } else if (strcmp(callee, "matrixCompMult") == 0) { |
Kenneth Graunke | 8fe5f30 | 2010-07-21 20:03:55 -0700 | [diff] [blame] | 1015 | assert(op[0]->type->is_float() && op[1]->type->is_float()); |
| 1016 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
| 1017 | data.f[c] = op[0]->value.f[c] * op[1]->value.f[c]; |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1018 | } else if (strcmp(callee, "max") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 1019 | expr = new(mem_ctx) ir_expression(ir_binop_max, type, op[0], op[1]); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1020 | } else if (strcmp(callee, "min") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 1021 | expr = new(mem_ctx) ir_expression(ir_binop_min, type, op[0], op[1]); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1022 | } else if (strcmp(callee, "mix") == 0) { |
Kenneth Graunke | 5d255e2 | 2010-07-23 12:36:50 -0700 | [diff] [blame] | 1023 | assert(op[0]->type->is_float() && op[1]->type->is_float()); |
| 1024 | if (op[2]->type->is_float()) { |
| 1025 | unsigned c2_inc = op[2]->type->is_scalar() ? 0 : 1; |
| 1026 | unsigned components = op[0]->type->components(); |
| 1027 | for (unsigned c = 0, c2 = 0; c < components; c2 += c2_inc, c++) { |
| 1028 | data.f[c] = op[0]->value.f[c] * (1 - op[2]->value.f[c2]) + |
| 1029 | op[1]->value.f[c] * op[2]->value.f[c2]; |
| 1030 | } |
| 1031 | } else { |
| 1032 | assert(op[2]->type->is_boolean()); |
| 1033 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
| 1034 | data.f[c] = op[op[2]->value.b[c] ? 1 : 0]->value.f[c]; |
| 1035 | } |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1036 | } else if (strcmp(callee, "mod") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 1037 | expr = new(mem_ctx) ir_expression(ir_binop_mod, type, op[0], op[1]); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1038 | } else if (strcmp(callee, "normalize") == 0) { |
Kenneth Graunke | 53f306d | 2010-07-21 20:19:32 -0700 | [diff] [blame] | 1039 | assert(op[0]->type->is_float()); |
| 1040 | float length = sqrtf(dot(op[0], op[0])); |
| 1041 | |
| 1042 | if (length == 0) |
| 1043 | return ir_constant::zero(mem_ctx, this->type); |
| 1044 | |
| 1045 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
| 1046 | data.f[c] = op[0]->value.f[c] / length; |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1047 | } else if (strcmp(callee, "not") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 1048 | expr = new(mem_ctx) ir_expression(ir_unop_logic_not, type, op[0], NULL); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1049 | } else if (strcmp(callee, "notEqual") == 0) { |
Kenneth Graunke | 48a69ba | 2010-07-21 18:05:36 -0700 | [diff] [blame] | 1050 | assert(op[0]->type->is_vector() && op[1] && op[1]->type->is_vector()); |
| 1051 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 1052 | switch (op[0]->type->base_type) { |
| 1053 | case GLSL_TYPE_UINT: |
| 1054 | data.b[c] = op[0]->value.u[c] != op[1]->value.u[c]; |
| 1055 | break; |
| 1056 | case GLSL_TYPE_INT: |
| 1057 | data.b[c] = op[0]->value.i[c] != op[1]->value.i[c]; |
| 1058 | break; |
| 1059 | case GLSL_TYPE_FLOAT: |
| 1060 | data.b[c] = op[0]->value.f[c] != op[1]->value.f[c]; |
| 1061 | break; |
Kenneth Graunke | d12cb77 | 2010-08-18 12:06:25 -0700 | [diff] [blame] | 1062 | case GLSL_TYPE_BOOL: |
| 1063 | data.b[c] = op[0]->value.b[c] != op[1]->value.b[c]; |
| 1064 | break; |
Kenneth Graunke | 48a69ba | 2010-07-21 18:05:36 -0700 | [diff] [blame] | 1065 | default: |
| 1066 | assert(!"Should not get here."); |
| 1067 | } |
| 1068 | } |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1069 | } else if (strcmp(callee, "outerProduct") == 0) { |
Kenneth Graunke | 7ddee6a | 2010-07-23 13:24:09 -0700 | [diff] [blame] | 1070 | assert(op[0]->type->is_vector() && op[1]->type->is_vector()); |
| 1071 | const unsigned m = op[0]->type->vector_elements; |
| 1072 | const unsigned n = op[1]->type->vector_elements; |
| 1073 | for (unsigned j = 0; j < n; j++) { |
| 1074 | for (unsigned i = 0; i < m; i++) { |
| 1075 | data.f[i+m*j] = op[0]->value.f[i] * op[1]->value.f[j]; |
| 1076 | } |
| 1077 | } |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1078 | } else if (strcmp(callee, "pow") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 1079 | expr = new(mem_ctx) ir_expression(ir_binop_pow, type, op[0], op[1]); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1080 | } else if (strcmp(callee, "radians") == 0) { |
Kenneth Graunke | 0afe349 | 2010-07-21 17:58:03 -0700 | [diff] [blame] | 1081 | assert(op[0]->type->is_float()); |
| 1082 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
| 1083 | data.f[c] = M_PI/180.0 * op[0]->value.f[c]; |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1084 | } else if (strcmp(callee, "reflect") == 0) { |
Kenneth Graunke | d60b2b0 | 2010-07-21 20:24:29 -0700 | [diff] [blame] | 1085 | assert(op[0]->type->is_float()); |
| 1086 | float dot_NI = dot(op[1], op[0]); |
| 1087 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
| 1088 | data.f[c] = op[0]->value.f[c] - 2 * dot_NI * op[1]->value.f[c]; |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1089 | } else if (strcmp(callee, "refract") == 0) { |
Kenneth Graunke | 04b3643 | 2010-07-21 20:33:42 -0700 | [diff] [blame] | 1090 | const float eta = op[2]->value.f[0]; |
| 1091 | const float dot_NI = dot(op[1], op[0]); |
| 1092 | const float k = 1.0 - eta * eta * (1.0 - dot_NI * dot_NI); |
| 1093 | if (k < 0.0) { |
| 1094 | return ir_constant::zero(mem_ctx, this->type); |
| 1095 | } else { |
| 1096 | for (unsigned c = 0; c < type->components(); c++) { |
| 1097 | data.f[c] = eta * op[0]->value.f[c] - (eta * dot_NI + sqrtf(k)) |
| 1098 | * op[1]->value.f[c]; |
| 1099 | } |
| 1100 | } |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1101 | } else if (strcmp(callee, "sign") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 1102 | expr = new(mem_ctx) ir_expression(ir_unop_sign, type, op[0], NULL); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1103 | } else if (strcmp(callee, "sin") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 1104 | expr = new(mem_ctx) ir_expression(ir_unop_sin, type, op[0], NULL); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1105 | } else if (strcmp(callee, "sinh") == 0) { |
Kenneth Graunke | 5d551da | 2010-07-21 17:49:56 -0700 | [diff] [blame] | 1106 | assert(op[0]->type->is_float()); |
| 1107 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
| 1108 | data.f[c] = sinhf(op[0]->value.f[c]); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1109 | } else if (strcmp(callee, "smoothstep") == 0) { |
Kenneth Graunke | 546f3a2 | 2010-07-21 22:37:50 -0700 | [diff] [blame] | 1110 | assert(num_parameters == 3); |
| 1111 | assert(op[1]->type == op[0]->type); |
| 1112 | unsigned edge_inc = op[0]->type->is_scalar() ? 0 : 1; |
| 1113 | for (unsigned c = 0, e = 0; c < type->components(); e += edge_inc, c++) { |
| 1114 | const float edge0 = op[0]->value.f[e]; |
| 1115 | const float edge1 = op[1]->value.f[e]; |
| 1116 | if (edge0 == edge1) { |
| 1117 | data.f[c] = 0.0; /* Avoid a crash - results are undefined anyway */ |
| 1118 | } else { |
| 1119 | const float numerator = op[2]->value.f[c] - edge0; |
| 1120 | const float denominator = edge1 - edge0; |
| 1121 | const float t = CLAMP(numerator/denominator, 0, 1); |
| 1122 | data.f[c] = t * t * (3 - 2 * t); |
| 1123 | } |
| 1124 | } |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1125 | } else if (strcmp(callee, "sqrt") == 0) { |
Kenneth Graunke | 8b1680a | 2010-07-21 16:57:53 -0700 | [diff] [blame] | 1126 | expr = new(mem_ctx) ir_expression(ir_unop_sqrt, type, op[0], NULL); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1127 | } else if (strcmp(callee, "step") == 0) { |
Kenneth Graunke | ff58b7c | 2010-07-21 21:53:40 -0700 | [diff] [blame] | 1128 | assert(op[0]->type->is_float() && op[1]->type->is_float()); |
| 1129 | /* op[0] (edge) may be either a scalar or a vector */ |
| 1130 | const unsigned c0_inc = op[0]->type->is_scalar() ? 0 : 1; |
| 1131 | for (unsigned c = 0, c0 = 0; c < type->components(); c0 += c0_inc, c++) |
| 1132 | data.f[c] = (op[1]->value.f[c] < op[0]->value.f[c0]) ? 0.0 : 1.0; |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1133 | } else if (strcmp(callee, "tan") == 0) { |
Kenneth Graunke | 9c9f8b2 | 2010-07-21 17:50:50 -0700 | [diff] [blame] | 1134 | assert(op[0]->type->is_float()); |
| 1135 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
| 1136 | data.f[c] = tanf(op[0]->value.f[c]); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1137 | } else if (strcmp(callee, "tanh") == 0) { |
Kenneth Graunke | 20970f7 | 2010-07-21 17:51:23 -0700 | [diff] [blame] | 1138 | assert(op[0]->type->is_float()); |
| 1139 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
| 1140 | data.f[c] = tanhf(op[0]->value.f[c]); |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1141 | } else if (strcmp(callee, "transpose") == 0) { |
Kenneth Graunke | b09ae5d | 2010-07-22 15:34:49 -0700 | [diff] [blame] | 1142 | assert(op[0]->type->is_matrix()); |
| 1143 | const unsigned n = op[0]->type->vector_elements; |
| 1144 | const unsigned m = op[0]->type->matrix_columns; |
| 1145 | for (unsigned j = 0; j < m; j++) { |
| 1146 | for (unsigned i = 0; i < n; i++) { |
| 1147 | data.f[m*i+j] += op[0]->value.f[i+n*j]; |
| 1148 | } |
| 1149 | } |
Kenneth Graunke | bafd89f | 2010-07-21 16:55:46 -0700 | [diff] [blame] | 1150 | } else { |
| 1151 | /* Unsupported builtin - some are not allowed in constant expressions. */ |
| 1152 | return NULL; |
| 1153 | } |
| 1154 | |
| 1155 | if (expr != NULL) |
| 1156 | return expr->constant_expression_value(); |
| 1157 | |
| 1158 | return new(mem_ctx) ir_constant(this->type, &data); |
| 1159 | } |