Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * \file ir_constant_expression.cpp |
| 26 | * Evaluate and process constant valued expressions |
| 27 | * |
| 28 | * In GLSL, constant valued expressions are used in several places. These |
| 29 | * must be processed and evaluated very early in the compilation process. |
| 30 | * |
| 31 | * * Sizes of arrays |
| 32 | * * Initializers for uniforms |
| 33 | * * Initializers for \c const variables |
| 34 | */ |
| 35 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 36 | #include <math.h> |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 37 | #include "ir.h" |
| 38 | #include "ir_visitor.h" |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 39 | #include "glsl_types.h" |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 40 | |
Kenneth Graunke | 79fed37 | 2010-07-09 11:53:56 -0700 | [diff] [blame] | 41 | #define min(x,y) (x) < (y) ? (x) : (y) |
| 42 | #define max(x,y) (x) > (y) ? (x) : (y) |
| 43 | |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 44 | ir_constant * |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 45 | ir_expression::constant_expression_value() |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 46 | { |
Kenneth Graunke | 6bc432e | 2010-07-02 17:12:23 -0700 | [diff] [blame] | 47 | ir_constant *op[2] = { NULL, NULL }; |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 48 | ir_constant_data data; |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame] | 49 | |
Kenneth Graunke | c63a1db | 2010-07-05 22:33:35 -0700 | [diff] [blame] | 50 | memset(&data, 0, sizeof(data)); |
| 51 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 52 | for (unsigned operand = 0; operand < this->get_num_operands(); operand++) { |
| 53 | op[operand] = this->operands[operand]->constant_expression_value(); |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 54 | if (!op[operand]) |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 55 | return NULL; |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame] | 56 | } |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 57 | |
Kenneth Graunke | 6fc983b | 2010-07-06 02:39:57 -0700 | [diff] [blame] | 58 | if (op[1] != NULL) |
| 59 | assert(op[0]->type->base_type == op[1]->type->base_type); |
| 60 | |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 61 | bool op0_scalar = op[0]->type->is_scalar(); |
| 62 | bool op1_scalar = op[1] != NULL && op[1]->type->is_scalar(); |
| 63 | |
| 64 | /* When iterating over a vector or matrix's components, we want to increase |
| 65 | * the loop counter. However, for scalars, we want to stay at 0. |
| 66 | */ |
Kenneth Graunke | acf88f2 | 2010-07-07 12:08:23 -0700 | [diff] [blame] | 67 | unsigned c0_inc = op0_scalar ? 0 : 1; |
| 68 | unsigned c1_inc = op1_scalar ? 0 : 1; |
Eric Anholt | 570dc0d | 2010-07-07 09:07:09 -0700 | [diff] [blame] | 69 | unsigned components; |
| 70 | if (op1_scalar || !op[1]) { |
| 71 | components = op[0]->type->components(); |
| 72 | } else { |
| 73 | components = op[1]->type->components(); |
| 74 | } |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 75 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 76 | switch (this->operation) { |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 77 | case ir_unop_logic_not: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 78 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 79 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 80 | data.b[c] = !op[0]->value.b[c]; |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 81 | break; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 82 | |
| 83 | case ir_unop_f2i: |
| 84 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 85 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 86 | data.i[c] = op[0]->value.f[c]; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 87 | } |
| 88 | break; |
| 89 | case ir_unop_i2f: |
| 90 | assert(op[0]->type->base_type == GLSL_TYPE_UINT || |
| 91 | op[0]->type->base_type == GLSL_TYPE_INT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 92 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 93 | if (op[0]->type->base_type == GLSL_TYPE_INT) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 94 | data.f[c] = op[0]->value.i[c]; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 95 | else |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 96 | data.f[c] = op[0]->value.u[c]; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 97 | } |
| 98 | break; |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 99 | case ir_unop_b2f: |
| 100 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 101 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 102 | data.f[c] = op[0]->value.b[c] ? 1.0 : 0.0; |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 103 | } |
| 104 | break; |
| 105 | case ir_unop_f2b: |
| 106 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 107 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 108 | data.b[c] = bool(op[0]->value.f[c]); |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 109 | } |
| 110 | break; |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 111 | case ir_unop_b2i: |
| 112 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 113 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 114 | data.u[c] = op[0]->value.b[c] ? 1 : 0; |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 115 | } |
| 116 | break; |
| 117 | case ir_unop_i2b: |
| 118 | assert(op[0]->type->is_integer()); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 119 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 120 | data.b[c] = bool(op[0]->value.u[c]); |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 121 | } |
| 122 | break; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 123 | |
Kenneth Graunke | 323d909 | 2010-07-08 23:21:36 -0700 | [diff] [blame] | 124 | case ir_unop_trunc: |
| 125 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 126 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | 323d909 | 2010-07-08 23:21:36 -0700 | [diff] [blame] | 127 | data.f[c] = truncf(op[0]->value.f[c]); |
| 128 | } |
| 129 | break; |
| 130 | |
Kenneth Graunke | c1ee30a | 2010-07-08 23:22:36 -0700 | [diff] [blame] | 131 | case ir_unop_ceil: |
| 132 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 133 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | c1ee30a | 2010-07-08 23:22:36 -0700 | [diff] [blame] | 134 | data.f[c] = ceilf(op[0]->value.f[c]); |
| 135 | } |
| 136 | break; |
| 137 | |
Kenneth Graunke | 0747204 | 2010-07-08 23:23:23 -0700 | [diff] [blame] | 138 | case ir_unop_floor: |
| 139 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 140 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | 0747204 | 2010-07-08 23:23:23 -0700 | [diff] [blame] | 141 | data.f[c] = floorf(op[0]->value.f[c]); |
| 142 | } |
| 143 | break; |
| 144 | |
Eric Anholt | d925c91 | 2010-07-01 10:37:11 -0700 | [diff] [blame] | 145 | case ir_unop_fract: |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 146 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 147 | switch (this->type->base_type) { |
Eric Anholt | d925c91 | 2010-07-01 10:37:11 -0700 | [diff] [blame] | 148 | case GLSL_TYPE_UINT: |
| 149 | data.u[c] = 0; |
| 150 | break; |
| 151 | case GLSL_TYPE_INT: |
| 152 | data.i[c] = 0; |
| 153 | break; |
| 154 | case GLSL_TYPE_FLOAT: |
| 155 | data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]); |
| 156 | break; |
| 157 | default: |
| 158 | assert(0); |
| 159 | } |
| 160 | } |
| 161 | break; |
| 162 | |
Kenneth Graunke | 908afd1 | 2010-07-08 23:28:50 -0700 | [diff] [blame] | 163 | case ir_unop_sin: |
| 164 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 165 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | 908afd1 | 2010-07-08 23:28:50 -0700 | [diff] [blame] | 166 | data.f[c] = sinf(op[0]->value.f[c]); |
| 167 | } |
| 168 | break; |
| 169 | |
Kenneth Graunke | 3fab376 | 2010-07-08 23:29:37 -0700 | [diff] [blame] | 170 | case ir_unop_cos: |
| 171 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 172 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | 3fab376 | 2010-07-08 23:29:37 -0700 | [diff] [blame] | 173 | data.f[c] = cosf(op[0]->value.f[c]); |
| 174 | } |
| 175 | break; |
| 176 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 177 | case ir_unop_neg: |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 178 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 179 | switch (this->type->base_type) { |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 180 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 181 | data.u[c] = -op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 182 | break; |
| 183 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 184 | data.i[c] = -op[0]->value.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 185 | break; |
| 186 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 187 | data.f[c] = -op[0]->value.f[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 188 | break; |
| 189 | default: |
| 190 | assert(0); |
| 191 | } |
| 192 | } |
| 193 | break; |
| 194 | |
| 195 | case ir_unop_abs: |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 196 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 197 | switch (this->type->base_type) { |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 198 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 199 | data.u[c] = op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 200 | break; |
| 201 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 202 | data.i[c] = op[0]->value.i[c]; |
| 203 | if (data.i[c] < 0) |
| 204 | data.i[c] = -data.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 205 | break; |
| 206 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 207 | data.f[c] = fabs(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 208 | break; |
| 209 | default: |
| 210 | assert(0); |
| 211 | } |
| 212 | } |
| 213 | break; |
| 214 | |
Kenneth Graunke | 14b7b26 | 2010-07-08 23:11:14 -0700 | [diff] [blame] | 215 | case ir_unop_sign: |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 216 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 217 | switch (this->type->base_type) { |
Kenneth Graunke | 14b7b26 | 2010-07-08 23:11:14 -0700 | [diff] [blame] | 218 | case GLSL_TYPE_UINT: |
| 219 | data.u[c] = op[0]->value.i[c] > 0; |
| 220 | break; |
| 221 | case GLSL_TYPE_INT: |
| 222 | data.i[c] = (op[0]->value.i[c] > 0) - (op[0]->value.i[c] < 0); |
| 223 | break; |
| 224 | case GLSL_TYPE_FLOAT: |
| 225 | data.f[c] = float((op[0]->value.f[c] > 0)-(op[0]->value.f[c] < 0)); |
| 226 | break; |
| 227 | default: |
| 228 | assert(0); |
| 229 | } |
| 230 | } |
| 231 | break; |
| 232 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 233 | case ir_unop_rcp: |
| 234 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 235 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 236 | switch (this->type->base_type) { |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 237 | case GLSL_TYPE_UINT: |
| 238 | if (op[0]->value.u[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 239 | data.u[c] = 1 / op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 240 | break; |
| 241 | case GLSL_TYPE_INT: |
| 242 | if (op[0]->value.i[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 243 | data.i[c] = 1 / op[0]->value.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 244 | break; |
| 245 | case GLSL_TYPE_FLOAT: |
| 246 | if (op[0]->value.f[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 247 | data.f[c] = 1.0 / op[0]->value.f[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 248 | break; |
| 249 | default: |
| 250 | assert(0); |
| 251 | } |
| 252 | } |
| 253 | break; |
| 254 | |
| 255 | case ir_unop_rsq: |
| 256 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 257 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 258 | data.f[c] = 1.0 / sqrtf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 259 | } |
| 260 | break; |
| 261 | |
| 262 | case ir_unop_sqrt: |
| 263 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 264 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 265 | data.f[c] = sqrtf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 266 | } |
| 267 | break; |
| 268 | |
| 269 | case ir_unop_exp: |
| 270 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 271 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 272 | data.f[c] = expf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 273 | } |
| 274 | break; |
| 275 | |
Kenneth Graunke | aca01ed | 2010-07-08 23:14:32 -0700 | [diff] [blame] | 276 | case ir_unop_exp2: |
| 277 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 278 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | aca01ed | 2010-07-08 23:14:32 -0700 | [diff] [blame] | 279 | data.f[c] = exp2f(op[0]->value.f[c]); |
| 280 | } |
| 281 | break; |
| 282 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 283 | case ir_unop_log: |
| 284 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 285 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 286 | data.f[c] = logf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 287 | } |
| 288 | break; |
| 289 | |
Kenneth Graunke | cb63929 | 2010-07-08 23:18:09 -0700 | [diff] [blame] | 290 | case ir_unop_log2: |
| 291 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 292 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | cb63929 | 2010-07-08 23:18:09 -0700 | [diff] [blame] | 293 | data.f[c] = log2f(op[0]->value.f[c]); |
| 294 | } |
| 295 | break; |
| 296 | |
Kenneth Graunke | d6a32d4 | 2010-06-09 15:22:35 -0700 | [diff] [blame] | 297 | case ir_unop_dFdx: |
| 298 | case ir_unop_dFdy: |
| 299 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 300 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 301 | data.f[c] = 0.0; |
Kenneth Graunke | d6a32d4 | 2010-06-09 15:22:35 -0700 | [diff] [blame] | 302 | } |
| 303 | break; |
| 304 | |
Kenneth Graunke | 891a064 | 2010-07-08 23:35:09 -0700 | [diff] [blame] | 305 | case ir_binop_pow: |
| 306 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 307 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | 891a064 | 2010-07-08 23:35:09 -0700 | [diff] [blame] | 308 | data.f[c] = powf(op[0]->value.f[c], op[1]->value.f[c]); |
| 309 | } |
| 310 | break; |
| 311 | |
Kenneth Graunke | 3f4a0b8 | 2010-07-05 21:15:32 -0700 | [diff] [blame] | 312 | case ir_binop_dot: |
| 313 | assert(op[0]->type->is_vector() && op[1]->type->is_vector()); |
| 314 | data.f[0] = 0; |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 315 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 316 | switch (op[0]->type->base_type) { |
Kenneth Graunke | 3f4a0b8 | 2010-07-05 21:15:32 -0700 | [diff] [blame] | 317 | case GLSL_TYPE_UINT: |
| 318 | data.u[0] += op[0]->value.u[c] * op[1]->value.u[c]; |
| 319 | break; |
| 320 | case GLSL_TYPE_INT: |
| 321 | data.i[0] += op[0]->value.i[c] * op[1]->value.i[c]; |
| 322 | break; |
| 323 | case GLSL_TYPE_FLOAT: |
| 324 | data.f[0] += op[0]->value.f[c] * op[1]->value.f[c]; |
| 325 | break; |
| 326 | default: |
| 327 | assert(0); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | break; |
Kenneth Graunke | 79fed37 | 2010-07-09 11:53:56 -0700 | [diff] [blame] | 332 | case ir_binop_min: |
| 333 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 334 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 335 | c < components; |
| 336 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 337 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 338 | switch (op[0]->type->base_type) { |
Kenneth Graunke | 79fed37 | 2010-07-09 11:53:56 -0700 | [diff] [blame] | 339 | case GLSL_TYPE_UINT: |
| 340 | data.u[c] = min(op[0]->value.u[c0], op[1]->value.u[c1]); |
| 341 | break; |
| 342 | case GLSL_TYPE_INT: |
| 343 | data.i[c] = min(op[0]->value.i[c0], op[1]->value.i[c1]); |
| 344 | break; |
| 345 | case GLSL_TYPE_FLOAT: |
| 346 | data.f[c] = min(op[0]->value.f[c0], op[1]->value.f[c1]); |
| 347 | break; |
| 348 | default: |
| 349 | assert(0); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | break; |
| 354 | case ir_binop_max: |
| 355 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 356 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 357 | c < components; |
| 358 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 359 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 360 | switch (op[0]->type->base_type) { |
Kenneth Graunke | 79fed37 | 2010-07-09 11:53:56 -0700 | [diff] [blame] | 361 | case GLSL_TYPE_UINT: |
| 362 | data.u[c] = max(op[0]->value.u[c0], op[1]->value.u[c1]); |
| 363 | break; |
| 364 | case GLSL_TYPE_INT: |
| 365 | data.i[c] = max(op[0]->value.i[c0], op[1]->value.i[c1]); |
| 366 | break; |
| 367 | case GLSL_TYPE_FLOAT: |
| 368 | data.f[c] = max(op[0]->value.f[c0], op[1]->value.f[c1]); |
| 369 | break; |
| 370 | default: |
| 371 | assert(0); |
| 372 | } |
| 373 | } |
Kenneth Graunke | 79fed37 | 2010-07-09 11:53:56 -0700 | [diff] [blame] | 374 | break; |
Eric Anholt | 9be7f63 | 2010-07-13 15:37:57 -0700 | [diff] [blame] | 375 | |
| 376 | case ir_binop_cross: |
| 377 | assert(op[0]->type == glsl_type::vec3_type); |
| 378 | assert(op[1]->type == glsl_type::vec3_type); |
| 379 | data.f[0] = (op[0]->value.f[1] * op[1]->value.f[2] - |
| 380 | op[1]->value.f[1] * op[0]->value.f[2]); |
| 381 | data.f[1] = (op[0]->value.f[2] * op[1]->value.f[0] - |
| 382 | op[1]->value.f[2] * op[0]->value.f[0]); |
| 383 | data.f[2] = (op[0]->value.f[0] * op[1]->value.f[1] - |
| 384 | op[1]->value.f[0] * op[0]->value.f[1]); |
| 385 | break; |
| 386 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 387 | case ir_binop_add: |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 388 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 389 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 390 | c < components; |
| 391 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 392 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 393 | switch (op[0]->type->base_type) { |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 394 | case GLSL_TYPE_UINT: |
| 395 | data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1]; |
| 396 | break; |
| 397 | case GLSL_TYPE_INT: |
| 398 | data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1]; |
| 399 | break; |
| 400 | case GLSL_TYPE_FLOAT: |
| 401 | data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1]; |
| 402 | break; |
| 403 | default: |
| 404 | assert(0); |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 405 | } |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 406 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 407 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 408 | break; |
| 409 | case ir_binop_sub: |
Kenneth Graunke | 97b44f0 | 2010-07-06 02:53:29 -0700 | [diff] [blame] | 410 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 411 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 412 | c < components; |
| 413 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 414 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 415 | switch (op[0]->type->base_type) { |
Kenneth Graunke | 97b44f0 | 2010-07-06 02:53:29 -0700 | [diff] [blame] | 416 | case GLSL_TYPE_UINT: |
| 417 | data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1]; |
| 418 | break; |
| 419 | case GLSL_TYPE_INT: |
| 420 | data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1]; |
| 421 | break; |
| 422 | case GLSL_TYPE_FLOAT: |
| 423 | data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1]; |
| 424 | break; |
| 425 | default: |
| 426 | assert(0); |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 427 | } |
Kenneth Graunke | 97b44f0 | 2010-07-06 02:53:29 -0700 | [diff] [blame] | 428 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 429 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 430 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 431 | case ir_binop_mul: |
Kenneth Graunke | cf80a4d | 2010-07-05 23:19:56 -0700 | [diff] [blame] | 432 | /* Check for equal types, or unequal types involving scalars */ |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 433 | if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix()) |
| 434 | || op0_scalar || op1_scalar) { |
| 435 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 436 | c < components; |
| 437 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 438 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 439 | switch (op[0]->type->base_type) { |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 440 | case GLSL_TYPE_UINT: |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 441 | 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] | 442 | break; |
| 443 | case GLSL_TYPE_INT: |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 444 | 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] | 445 | break; |
| 446 | case GLSL_TYPE_FLOAT: |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 447 | 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] | 448 | break; |
| 449 | default: |
| 450 | assert(0); |
| 451 | } |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 452 | } |
Kenneth Graunke | cf80a4d | 2010-07-05 23:19:56 -0700 | [diff] [blame] | 453 | } else { |
| 454 | assert(op[0]->type->is_matrix() || op[1]->type->is_matrix()); |
| 455 | |
| 456 | /* Multiply an N-by-M matrix with an M-by-P matrix. Since either |
| 457 | * matrix can be a GLSL vector, either N or P can be 1. |
| 458 | * |
| 459 | * For vec*mat, the vector is treated as a row vector. This |
| 460 | * means the vector is a 1-row x M-column matrix. |
| 461 | * |
| 462 | * For mat*vec, the vector is treated as a column vector. Since |
| 463 | * matrix_columns is 1 for vectors, this just works. |
| 464 | */ |
| 465 | const unsigned n = op[0]->type->is_vector() |
| 466 | ? 1 : op[0]->type->vector_elements; |
| 467 | const unsigned m = op[1]->type->vector_elements; |
| 468 | const unsigned p = op[1]->type->matrix_columns; |
| 469 | for (unsigned j = 0; j < p; j++) { |
| 470 | for (unsigned i = 0; i < n; i++) { |
| 471 | for (unsigned k = 0; k < m; k++) { |
| 472 | data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j]; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 477 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 478 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 479 | case ir_binop_div: |
Kenneth Graunke | dad35eb | 2010-07-06 02:56:36 -0700 | [diff] [blame] | 480 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 481 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 482 | c < components; |
| 483 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 484 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 485 | switch (op[0]->type->base_type) { |
Kenneth Graunke | dad35eb | 2010-07-06 02:56:36 -0700 | [diff] [blame] | 486 | case GLSL_TYPE_UINT: |
| 487 | data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1]; |
| 488 | break; |
| 489 | case GLSL_TYPE_INT: |
| 490 | data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1]; |
| 491 | break; |
| 492 | case GLSL_TYPE_FLOAT: |
| 493 | data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1]; |
| 494 | break; |
| 495 | default: |
| 496 | assert(0); |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 497 | } |
Kenneth Graunke | dad35eb | 2010-07-06 02:56:36 -0700 | [diff] [blame] | 498 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 499 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 500 | break; |
Kenneth Graunke | ce5ae5f | 2010-07-14 11:28:40 -0700 | [diff] [blame] | 501 | case ir_binop_mod: |
| 502 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 503 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 504 | c < components; |
| 505 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 506 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 507 | switch (op[0]->type->base_type) { |
Kenneth Graunke | ce5ae5f | 2010-07-14 11:28:40 -0700 | [diff] [blame] | 508 | case GLSL_TYPE_UINT: |
| 509 | data.u[c] = op[0]->value.u[c0] % op[1]->value.u[c1]; |
| 510 | break; |
| 511 | case GLSL_TYPE_INT: |
| 512 | data.i[c] = op[0]->value.i[c0] % op[1]->value.i[c1]; |
| 513 | break; |
| 514 | case GLSL_TYPE_FLOAT: |
| 515 | /* We don't use fmod because it rounds toward zero; GLSL specifies |
| 516 | * the use of floor. |
| 517 | */ |
| 518 | data.f[c] = (op[0]->value.f[c0] - op[1]->value.f[c1]) |
| 519 | * floorf(op[0]->value.f[c0] / op[1]->value.f[c1]); |
| 520 | break; |
| 521 | default: |
| 522 | assert(0); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | break; |
| 527 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 528 | case ir_binop_logic_and: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 529 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 530 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 531 | 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] | 532 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 533 | case ir_binop_logic_xor: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 534 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 535 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 536 | 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] | 537 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 538 | case ir_binop_logic_or: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 539 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 540 | for (unsigned c = 0; c < op[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 541 | 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] | 542 | break; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 543 | |
| 544 | case ir_binop_less: |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 545 | switch (op[0]->type->base_type) { |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 546 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 547 | 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] | 548 | break; |
| 549 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 550 | 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] | 551 | break; |
| 552 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 553 | 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] | 554 | break; |
| 555 | default: |
| 556 | assert(0); |
| 557 | } |
| 558 | break; |
| 559 | case ir_binop_greater: |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 560 | switch (op[0]->type->base_type) { |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 561 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 562 | 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] | 563 | break; |
| 564 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 565 | 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] | 566 | break; |
| 567 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 568 | 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] | 569 | break; |
| 570 | default: |
| 571 | assert(0); |
| 572 | } |
| 573 | break; |
| 574 | case ir_binop_lequal: |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 575 | switch (op[0]->type->base_type) { |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 576 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 577 | 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] | 578 | break; |
| 579 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 580 | 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] | 581 | break; |
| 582 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 583 | 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] | 584 | break; |
| 585 | default: |
| 586 | assert(0); |
| 587 | } |
| 588 | break; |
| 589 | case ir_binop_gequal: |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 590 | switch (op[0]->type->base_type) { |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 591 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 592 | 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] | 593 | break; |
| 594 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 595 | 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] | 596 | break; |
| 597 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 598 | 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] | 599 | break; |
| 600 | default: |
| 601 | assert(0); |
| 602 | } |
| 603 | break; |
| 604 | |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 605 | case ir_binop_equal: |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame] | 606 | data.b[0] = true; |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 607 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 608 | switch (op[0]->type->base_type) { |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame] | 609 | case GLSL_TYPE_UINT: |
| 610 | data.b[0] = data.b[0] && op[0]->value.u[c] == op[1]->value.u[c]; |
| 611 | break; |
| 612 | case GLSL_TYPE_INT: |
| 613 | data.b[0] = data.b[0] && op[0]->value.i[c] == op[1]->value.i[c]; |
| 614 | break; |
| 615 | case GLSL_TYPE_FLOAT: |
| 616 | data.b[0] = data.b[0] && op[0]->value.f[c] == op[1]->value.f[c]; |
| 617 | break; |
| 618 | case GLSL_TYPE_BOOL: |
| 619 | data.b[0] = data.b[0] && op[0]->value.b[c] == op[1]->value.b[c]; |
| 620 | break; |
| 621 | default: |
| 622 | assert(0); |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | break; |
| 626 | case ir_binop_nequal: |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame] | 627 | data.b[0] = false; |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 628 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
| 629 | switch (op[0]->type->base_type) { |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame] | 630 | case GLSL_TYPE_UINT: |
| 631 | data.b[0] = data.b[0] || op[0]->value.u[c] != op[1]->value.u[c]; |
| 632 | break; |
| 633 | case GLSL_TYPE_INT: |
| 634 | data.b[0] = data.b[0] || op[0]->value.i[c] != op[1]->value.i[c]; |
| 635 | break; |
| 636 | case GLSL_TYPE_FLOAT: |
| 637 | data.b[0] = data.b[0] || op[0]->value.f[c] != op[1]->value.f[c]; |
| 638 | break; |
| 639 | case GLSL_TYPE_BOOL: |
| 640 | data.b[0] = data.b[0] || op[0]->value.b[c] != op[1]->value.b[c]; |
| 641 | break; |
| 642 | default: |
| 643 | assert(0); |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | break; |
| 647 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 648 | default: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 649 | /* FINISHME: Should handle all expression types. */ |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 650 | return NULL; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 651 | } |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 652 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 653 | void *ctx = talloc_parent(this); |
| 654 | return new(ctx) ir_constant(this->type, &data); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 658 | ir_constant * |
| 659 | ir_texture::constant_expression_value() |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 660 | { |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 661 | /* texture lookups aren't constant expressions */ |
| 662 | return NULL; |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 666 | ir_constant * |
| 667 | ir_swizzle::constant_expression_value() |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 668 | { |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 669 | ir_constant *v = this->val->constant_expression_value(); |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 670 | |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 671 | if (v != NULL) { |
Ian Romanick | 0bb70a3 | 2010-06-11 15:49:49 -0700 | [diff] [blame] | 672 | ir_constant_data data; |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 673 | |
| 674 | const unsigned swiz_idx[4] = { |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 675 | this->mask.x, this->mask.y, this->mask.z, this->mask.w |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 676 | }; |
| 677 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 678 | for (unsigned i = 0; i < this->mask.num_components; i++) { |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 679 | switch (v->type->base_type) { |
| 680 | case GLSL_TYPE_UINT: |
| 681 | case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break; |
| 682 | case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break; |
| 683 | case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break; |
| 684 | default: assert(!"Should not get here."); break; |
| 685 | } |
| 686 | } |
| 687 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 688 | void *ctx = talloc_parent(this); |
| 689 | return new(ctx) ir_constant(this->type, &data); |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 690 | } |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 691 | return NULL; |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 695 | ir_constant * |
| 696 | ir_dereference_variable::constant_expression_value() |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 697 | { |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 698 | ir_variable *var = this->variable_referenced(); |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 699 | if (var && var->constant_value) |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 700 | return var->constant_value->clone(NULL); |
| 701 | return NULL; |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 705 | ir_constant * |
| 706 | ir_dereference_array::constant_expression_value() |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 707 | { |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 708 | void *ctx = talloc_parent(this); |
| 709 | ir_constant *array = this->array->constant_expression_value(); |
| 710 | ir_constant *idx = this->array_index->constant_expression_value(); |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 711 | |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 712 | if ((array != NULL) && (idx != NULL)) { |
| 713 | if (array->type->is_matrix()) { |
| 714 | /* Array access of a matrix results in a vector. |
| 715 | */ |
| 716 | const unsigned column = idx->value.u[0]; |
| 717 | |
| 718 | const glsl_type *const column_type = array->type->column_type(); |
| 719 | |
| 720 | /* Offset in the constant matrix to the first element of the column |
| 721 | * to be extracted. |
| 722 | */ |
| 723 | const unsigned mat_idx = column * column_type->vector_elements; |
| 724 | |
Ian Romanick | 0bb70a3 | 2010-06-11 15:49:49 -0700 | [diff] [blame] | 725 | ir_constant_data data; |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 726 | |
| 727 | switch (column_type->base_type) { |
| 728 | case GLSL_TYPE_UINT: |
| 729 | case GLSL_TYPE_INT: |
| 730 | for (unsigned i = 0; i < column_type->vector_elements; i++) |
| 731 | data.u[i] = array->value.u[mat_idx + i]; |
| 732 | |
| 733 | break; |
| 734 | |
| 735 | case GLSL_TYPE_FLOAT: |
| 736 | for (unsigned i = 0; i < column_type->vector_elements; i++) |
| 737 | data.f[i] = array->value.f[mat_idx + i]; |
| 738 | |
| 739 | break; |
| 740 | |
| 741 | default: |
| 742 | assert(!"Should not get here."); |
| 743 | break; |
| 744 | } |
| 745 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 746 | return new(ctx) ir_constant(column_type, &data); |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 747 | } else if (array->type->is_vector()) { |
| 748 | const unsigned component = idx->value.u[0]; |
| 749 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 750 | return new(ctx) ir_constant(array, component); |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 751 | } else { |
| 752 | /* FINISHME: Handle access of constant arrays. */ |
| 753 | } |
| 754 | } |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 755 | return NULL; |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 759 | ir_constant * |
| 760 | ir_dereference_record::constant_expression_value() |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 761 | { |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 762 | ir_constant *v = this->record->constant_expression_value(); |
Ian Romanick | 253dede | 2010-06-09 17:30:19 -0700 | [diff] [blame] | 763 | |
Kenneth Graunke | 98f32a1 | 2010-07-15 10:20:51 -0700 | [diff] [blame^] | 764 | return (v != NULL) ? v->get_record_field(this->field) : NULL; |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 768 | ir_constant * |
| 769 | ir_assignment::constant_expression_value() |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 770 | { |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 771 | /* FINISHME: Handle CEs involving assignment (return RHS) */ |
| 772 | return NULL; |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 776 | ir_constant * |
| 777 | ir_constant::constant_expression_value() |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 778 | { |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 779 | return this; |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 783 | ir_constant * |
| 784 | ir_call::constant_expression_value() |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 785 | { |
Kenneth Graunke | fb2ffd2 | 2010-07-15 10:09:09 -0700 | [diff] [blame] | 786 | /* FINISHME: Handle CEs involving builtin function calls. */ |
| 787 | return NULL; |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 788 | } |
| 789 | |