Eric Anholt | c22c400 | 2010-03-26 18:20:30 -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 | #include <stdlib.h> |
Eric Anholt | d1e3195 | 2010-03-28 01:55:38 -0700 | [diff] [blame] | 25 | #include <math.h> |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 26 | #include "glsl_symbol_table.h" |
| 27 | #include "glsl_parser_extras.h" |
| 28 | #include "glsl_types.h" |
| 29 | #include "ir.h" |
| 30 | |
| 31 | static void |
Eric Anholt | 2eec73f | 2010-03-27 12:25:20 -0700 | [diff] [blame] | 32 | generate_unop(exec_list *instructions, |
| 33 | ir_variable **declarations, |
| 34 | const glsl_type *type, |
| 35 | enum ir_expression_operation op) |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 36 | { |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 37 | ir_dereference *const arg = new ir_dereference(declarations[0]); |
| 38 | ir_rvalue *result; |
| 39 | |
Eric Anholt | 2eec73f | 2010-03-27 12:25:20 -0700 | [diff] [blame] | 40 | result = new ir_expression(op, type, arg, NULL); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 41 | |
Eric Anholt | 7e78e07 | 2010-04-07 13:34:15 -0700 | [diff] [blame] | 42 | ir_instruction *inst = new ir_return(result); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 43 | instructions->push_tail(inst); |
| 44 | } |
| 45 | |
Eric Anholt | 2eec73f | 2010-03-27 12:25:20 -0700 | [diff] [blame] | 46 | static void |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 47 | generate_binop(exec_list *instructions, |
| 48 | ir_variable **declarations, |
| 49 | const glsl_type *type, |
| 50 | enum ir_expression_operation op) |
| 51 | { |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 52 | ir_dereference *const arg1 = new ir_dereference(declarations[0]); |
| 53 | ir_dereference *const arg2 = new ir_dereference(declarations[1]); |
| 54 | ir_rvalue *result; |
| 55 | |
| 56 | result = new ir_expression(op, type, arg1, arg2); |
| 57 | |
Eric Anholt | 7e78e07 | 2010-04-07 13:34:15 -0700 | [diff] [blame] | 58 | ir_instruction *inst = new ir_return(result); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 59 | instructions->push_tail(inst); |
| 60 | } |
| 61 | |
| 62 | static void |
Eric Anholt | d1e3195 | 2010-03-28 01:55:38 -0700 | [diff] [blame] | 63 | generate_radians(exec_list *instructions, |
| 64 | ir_variable **declarations, |
| 65 | const glsl_type *type) |
| 66 | { |
Eric Anholt | d1e3195 | 2010-03-28 01:55:38 -0700 | [diff] [blame] | 67 | ir_dereference *const arg = new ir_dereference(declarations[0]); |
| 68 | ir_rvalue *result; |
| 69 | |
| 70 | result = new ir_expression(ir_binop_mul, type, |
| 71 | arg, |
| 72 | new ir_constant((float)(M_PI / 180.0))); |
| 73 | |
Eric Anholt | 7e78e07 | 2010-04-07 13:34:15 -0700 | [diff] [blame] | 74 | ir_instruction *inst = new ir_return(result); |
Eric Anholt | d1e3195 | 2010-03-28 01:55:38 -0700 | [diff] [blame] | 75 | instructions->push_tail(inst); |
| 76 | } |
| 77 | |
| 78 | static void |
| 79 | generate_degrees(exec_list *instructions, |
| 80 | ir_variable **declarations, |
| 81 | const glsl_type *type) |
| 82 | { |
Eric Anholt | d1e3195 | 2010-03-28 01:55:38 -0700 | [diff] [blame] | 83 | ir_dereference *const arg = new ir_dereference(declarations[0]); |
| 84 | ir_rvalue *result; |
| 85 | |
| 86 | result = new ir_expression(ir_binop_mul, type, |
| 87 | arg, |
| 88 | new ir_constant((float)(180.0 / M_PI))); |
| 89 | |
Eric Anholt | 7e78e07 | 2010-04-07 13:34:15 -0700 | [diff] [blame] | 90 | ir_instruction *inst = new ir_return(result); |
Eric Anholt | d1e3195 | 2010-03-28 01:55:38 -0700 | [diff] [blame] | 91 | instructions->push_tail(inst); |
| 92 | } |
| 93 | |
| 94 | static void |
Eric Anholt | 2eec73f | 2010-03-27 12:25:20 -0700 | [diff] [blame] | 95 | generate_exp(exec_list *instructions, |
| 96 | ir_variable **declarations, |
| 97 | const glsl_type *type) |
| 98 | { |
| 99 | generate_unop(instructions, declarations, type, ir_unop_exp); |
| 100 | } |
| 101 | |
| 102 | static void |
| 103 | generate_log(exec_list *instructions, |
| 104 | ir_variable **declarations, |
| 105 | const glsl_type *type) |
| 106 | { |
| 107 | generate_unop(instructions, declarations, type, ir_unop_log); |
| 108 | } |
| 109 | |
| 110 | static void |
Eric Anholt | 0166526 | 2010-03-27 13:56:35 -0700 | [diff] [blame] | 111 | generate_exp2(exec_list *instructions, |
| 112 | ir_variable **declarations, |
| 113 | const glsl_type *type) |
| 114 | { |
| 115 | generate_unop(instructions, declarations, type, ir_unop_exp2); |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | generate_log2(exec_list *instructions, |
| 120 | ir_variable **declarations, |
| 121 | const glsl_type *type) |
| 122 | { |
| 123 | generate_unop(instructions, declarations, type, ir_unop_log2); |
| 124 | } |
| 125 | |
| 126 | static void |
Eric Anholt | 2eec73f | 2010-03-27 12:25:20 -0700 | [diff] [blame] | 127 | generate_rsq(exec_list *instructions, |
| 128 | ir_variable **declarations, |
| 129 | const glsl_type *type) |
| 130 | { |
| 131 | generate_unop(instructions, declarations, type, ir_unop_rsq); |
| 132 | } |
| 133 | |
| 134 | static void |
Eric Anholt | 44d68fd | 2010-03-27 13:01:51 -0700 | [diff] [blame] | 135 | generate_sqrt(exec_list *instructions, |
| 136 | ir_variable **declarations, |
| 137 | const glsl_type *type) |
| 138 | { |
| 139 | generate_unop(instructions, declarations, type, ir_unop_sqrt); |
| 140 | } |
| 141 | |
| 142 | static void |
Eric Anholt | 2eec73f | 2010-03-27 12:25:20 -0700 | [diff] [blame] | 143 | generate_abs(exec_list *instructions, |
| 144 | ir_variable **declarations, |
| 145 | const glsl_type *type) |
| 146 | { |
| 147 | generate_unop(instructions, declarations, type, ir_unop_abs); |
| 148 | } |
| 149 | |
| 150 | static void |
| 151 | generate_ceil(exec_list *instructions, |
| 152 | ir_variable **declarations, |
| 153 | const glsl_type *type) |
| 154 | { |
| 155 | generate_unop(instructions, declarations, type, ir_unop_ceil); |
| 156 | } |
| 157 | |
| 158 | static void |
| 159 | generate_floor(exec_list *instructions, |
| 160 | ir_variable **declarations, |
| 161 | const glsl_type *type) |
| 162 | { |
| 163 | generate_unop(instructions, declarations, type, ir_unop_floor); |
| 164 | } |
| 165 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 166 | static void |
| 167 | generate_mod(exec_list *instructions, |
| 168 | ir_variable **declarations, |
| 169 | const glsl_type *type) |
| 170 | { |
| 171 | generate_binop(instructions, declarations, type, ir_binop_mod); |
| 172 | } |
| 173 | |
| 174 | static void |
| 175 | generate_min(exec_list *instructions, |
| 176 | ir_variable **declarations, |
| 177 | const glsl_type *type) |
| 178 | { |
| 179 | generate_binop(instructions, declarations, type, ir_binop_min); |
| 180 | } |
| 181 | |
| 182 | static void |
| 183 | generate_max(exec_list *instructions, |
| 184 | ir_variable **declarations, |
| 185 | const glsl_type *type) |
| 186 | { |
| 187 | generate_binop(instructions, declarations, type, ir_binop_max); |
| 188 | } |
| 189 | |
Eric Anholt | cc49cea | 2010-04-08 15:10:37 -0700 | [diff] [blame^] | 190 | static void |
| 191 | generate_mix_vec(exec_list *instructions, |
| 192 | ir_variable **declarations, |
| 193 | const glsl_type *type) |
| 194 | { |
| 195 | ir_dereference *const x = new ir_dereference(declarations[0]); |
| 196 | ir_dereference *const y = new ir_dereference(declarations[1]); |
| 197 | ir_dereference *const a = new ir_dereference(declarations[2]); |
| 198 | ir_rvalue *result, *temp; |
| 199 | |
| 200 | temp = new ir_expression(ir_binop_sub, type, new ir_constant(1.0f), a); |
| 201 | result = new ir_expression(ir_binop_mul, type, x, temp); |
| 202 | |
| 203 | temp = new ir_expression(ir_binop_mul, type, y, a); |
| 204 | result = new ir_expression(ir_binop_add, type, result, temp); |
| 205 | |
| 206 | ir_instruction *inst = new ir_return(result); |
| 207 | instructions->push_tail(inst); |
| 208 | } |
| 209 | |
Eric Anholt | ddd2e83 | 2010-03-27 12:59:42 -0700 | [diff] [blame] | 210 | |
| 211 | static void |
Eric Anholt | 9257592 | 2010-04-08 14:34:38 -0700 | [diff] [blame] | 212 | generate_normalize(exec_list *instructions, |
| 213 | ir_variable **declarations, |
| 214 | const glsl_type *type) |
| 215 | { |
| 216 | ir_dereference *const arg = new ir_dereference(declarations[0]); |
| 217 | ir_rvalue *temp; |
| 218 | ir_rvalue *result; |
| 219 | |
| 220 | temp = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg); |
| 221 | temp = new ir_expression(ir_unop_rsq, glsl_type::float_type, temp, NULL); |
| 222 | result = new ir_expression(ir_binop_mul, type, arg, temp); |
| 223 | |
| 224 | ir_instruction *inst = new ir_return(result); |
| 225 | instructions->push_tail(inst); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | static void |
Eric Anholt | ddd2e83 | 2010-03-27 12:59:42 -0700 | [diff] [blame] | 230 | generate_pow(exec_list *instructions, |
| 231 | ir_variable **declarations, |
| 232 | const glsl_type *type) |
| 233 | { |
| 234 | generate_binop(instructions, declarations, type, ir_binop_pow); |
| 235 | } |
| 236 | |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 237 | void |
| 238 | generate_function_instance(ir_function *f, |
| 239 | const char *name, |
| 240 | exec_list *instructions, |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 241 | int n_args, |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 242 | void (*generate)(exec_list *instructions, |
| 243 | ir_variable **declarations, |
| 244 | const glsl_type *type), |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 245 | const glsl_type *ret_type, |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 246 | const glsl_type *type) |
| 247 | { |
Eric Anholt | 7e78e07 | 2010-04-07 13:34:15 -0700 | [diff] [blame] | 248 | ir_variable *declarations[16]; |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 249 | |
Eric Anholt | 7e78e07 | 2010-04-07 13:34:15 -0700 | [diff] [blame] | 250 | ir_function_signature *const sig = new ir_function_signature(ret_type); |
Ian Romanick | 6a15d5b5 | 2010-03-31 16:37:10 -0700 | [diff] [blame] | 251 | f->add_signature(sig); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 252 | |
Eric Anholt | 894ea97 | 2010-04-07 13:19:11 -0700 | [diff] [blame] | 253 | ir_label *const label = new ir_label(name, sig); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 254 | instructions->push_tail(label); |
| 255 | sig->definition = label; |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 256 | static const char *arg_names[] = { |
| 257 | "arg0", |
Eric Anholt | cc49cea | 2010-04-08 15:10:37 -0700 | [diff] [blame^] | 258 | "arg1", |
| 259 | "arg2" |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 260 | }; |
| 261 | int i; |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 262 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 263 | for (i = 0; i < n_args; i++) { |
| 264 | ir_variable *var = new ir_variable(type, arg_names[i]); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 265 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 266 | var->mode = ir_var_in; |
| 267 | sig->parameters.push_tail(var); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 268 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 269 | declarations[i] = var; |
| 270 | } |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 271 | |
Eric Anholt | 894ea97 | 2010-04-07 13:19:11 -0700 | [diff] [blame] | 272 | generate(&sig->body, declarations, type); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void |
| 276 | make_gentype_function(glsl_symbol_table *symtab, exec_list *instructions, |
| 277 | const char *name, |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 278 | int n_args, |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 279 | void (*generate)(exec_list *instructions, |
| 280 | ir_variable **declarations, |
| 281 | const glsl_type *type)) |
| 282 | { |
| 283 | ir_function *const f = new ir_function(name); |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 284 | const glsl_type *float_type = glsl_type::float_type; |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 285 | const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1); |
| 286 | const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1); |
| 287 | const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1); |
| 288 | |
| 289 | bool added = symtab->add_function(name, f); |
| 290 | assert(added); |
| 291 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 292 | generate_function_instance(f, name, instructions, n_args, generate, |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 293 | float_type, float_type); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 294 | generate_function_instance(f, name, instructions, n_args, generate, |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 295 | vec2_type, vec2_type); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 296 | generate_function_instance(f, name, instructions, n_args, generate, |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 297 | vec3_type, vec3_type); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 298 | generate_function_instance(f, name, instructions, n_args, generate, |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 299 | vec4_type, vec4_type); |
| 300 | } |
| 301 | |
| 302 | static void |
| 303 | generate_length(exec_list *instructions, |
| 304 | ir_variable **declarations, |
| 305 | const glsl_type *type) |
| 306 | { |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 307 | ir_dereference *const arg = new ir_dereference(declarations[0]); |
| 308 | ir_rvalue *result, *temp; |
| 309 | |
| 310 | (void)type; |
| 311 | |
| 312 | /* FINISHME: implement the abs(arg) variant for length(float f) */ |
| 313 | |
| 314 | temp = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg); |
| 315 | result = new ir_expression(ir_unop_sqrt, glsl_type::float_type, temp, NULL); |
| 316 | |
Eric Anholt | 7e78e07 | 2010-04-07 13:34:15 -0700 | [diff] [blame] | 317 | ir_instruction *inst = new ir_return(result); |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 318 | instructions->push_tail(inst); |
| 319 | } |
| 320 | |
| 321 | void |
| 322 | generate_length_functions(glsl_symbol_table *symtab, exec_list *instructions) |
| 323 | { |
| 324 | const char *name = "length"; |
| 325 | ir_function *const f = new ir_function(name); |
| 326 | const glsl_type *float_type = glsl_type::float_type; |
| 327 | const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1); |
| 328 | const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1); |
| 329 | const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1); |
| 330 | |
| 331 | bool added = symtab->add_function(name, f); |
| 332 | assert(added); |
| 333 | |
| 334 | generate_function_instance(f, name, instructions, 1, generate_length, |
| 335 | float_type, float_type); |
| 336 | generate_function_instance(f, name, instructions, 1, generate_length, |
| 337 | float_type, vec2_type); |
| 338 | generate_function_instance(f, name, instructions, 1, generate_length, |
| 339 | float_type, vec3_type); |
| 340 | generate_function_instance(f, name, instructions, 1, generate_length, |
| 341 | float_type, vec4_type); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 344 | static void |
| 345 | generate_dot(exec_list *instructions, |
| 346 | ir_variable **declarations, |
| 347 | const glsl_type *type) |
| 348 | { |
Eric Anholt | 6173312 | 2010-04-07 15:18:37 -0700 | [diff] [blame] | 349 | ir_dereference *const arg0 = new ir_dereference(declarations[0]); |
| 350 | ir_dereference *const arg1 = new ir_dereference(declarations[1]); |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 351 | ir_rvalue *result; |
| 352 | |
| 353 | (void)type; |
| 354 | |
Eric Anholt | 6173312 | 2010-04-07 15:18:37 -0700 | [diff] [blame] | 355 | result = new ir_expression(ir_binop_dot, glsl_type::float_type, arg0, arg1); |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 356 | |
Eric Anholt | 7e78e07 | 2010-04-07 13:34:15 -0700 | [diff] [blame] | 357 | ir_instruction *inst = new ir_return(result); |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 358 | instructions->push_tail(inst); |
| 359 | } |
| 360 | |
| 361 | void |
| 362 | generate_dot_functions(glsl_symbol_table *symtab, exec_list *instructions) |
| 363 | { |
| 364 | const char *name = "dot"; |
| 365 | ir_function *const f = new ir_function(name); |
| 366 | const glsl_type *float_type = glsl_type::float_type; |
| 367 | const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1); |
| 368 | const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1); |
| 369 | const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1); |
| 370 | |
| 371 | bool added = symtab->add_function(name, f); |
| 372 | assert(added); |
| 373 | |
Eric Anholt | 6173312 | 2010-04-07 15:18:37 -0700 | [diff] [blame] | 374 | generate_function_instance(f, name, instructions, 2, generate_dot, |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 375 | float_type, float_type); |
Eric Anholt | 6173312 | 2010-04-07 15:18:37 -0700 | [diff] [blame] | 376 | generate_function_instance(f, name, instructions, 2, generate_dot, |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 377 | float_type, vec2_type); |
Eric Anholt | 6173312 | 2010-04-07 15:18:37 -0700 | [diff] [blame] | 378 | generate_function_instance(f, name, instructions, 2, generate_dot, |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 379 | float_type, vec3_type); |
Eric Anholt | 6173312 | 2010-04-07 15:18:37 -0700 | [diff] [blame] | 380 | generate_function_instance(f, name, instructions, 2, generate_dot, |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 381 | float_type, vec4_type); |
| 382 | } |
| 383 | |
Eric Anholt | feeb43b | 2010-04-08 15:02:59 -0700 | [diff] [blame] | 384 | static void |
| 385 | generate_any_bvec2(exec_list *instructions, |
| 386 | ir_variable **declarations, |
| 387 | const glsl_type *type) |
| 388 | { |
| 389 | ir_dereference *const arg0 = new ir_dereference(declarations[0]); |
| 390 | ir_rvalue *result; |
| 391 | |
| 392 | (void)type; |
| 393 | |
| 394 | result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type, |
| 395 | new ir_swizzle(arg0, 0, 0, 0, 0, 1), |
| 396 | new ir_swizzle(arg0, 1, 0, 0, 0, 1)); |
| 397 | |
| 398 | ir_instruction *inst = new ir_return(result); |
| 399 | instructions->push_tail(inst); |
| 400 | } |
| 401 | |
| 402 | static void |
| 403 | generate_any_bvec3(exec_list *instructions, |
| 404 | ir_variable **declarations, |
| 405 | const glsl_type *type) |
| 406 | { |
| 407 | ir_dereference *const arg0 = new ir_dereference(declarations[0]); |
| 408 | ir_rvalue *result; |
| 409 | |
| 410 | (void)type; |
| 411 | |
| 412 | result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type, |
| 413 | new ir_swizzle(arg0, 0, 0, 0, 0, 1), |
| 414 | new ir_swizzle(arg0, 1, 0, 0, 0, 1)); |
| 415 | result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type, |
| 416 | result, |
| 417 | new ir_swizzle(arg0, 2, 0, 0, 0, 1)); |
| 418 | |
| 419 | ir_instruction *inst = new ir_return(result); |
| 420 | instructions->push_tail(inst); |
| 421 | } |
| 422 | |
| 423 | static void |
| 424 | generate_any_bvec4(exec_list *instructions, |
| 425 | ir_variable **declarations, |
| 426 | const glsl_type *type) |
| 427 | { |
| 428 | ir_dereference *const arg0 = new ir_dereference(declarations[0]); |
| 429 | ir_rvalue *result; |
| 430 | |
| 431 | (void)type; |
| 432 | |
| 433 | result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type, |
| 434 | new ir_swizzle(arg0, 0, 0, 0, 0, 1), |
| 435 | new ir_swizzle(arg0, 1, 0, 0, 0, 1)); |
| 436 | result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type, |
| 437 | result, |
| 438 | new ir_swizzle(arg0, 2, 0, 0, 0, 1)); |
| 439 | result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type, |
| 440 | result, |
| 441 | new ir_swizzle(arg0, 3, 0, 0, 0, 1)); |
| 442 | |
| 443 | ir_instruction *inst = new ir_return(result); |
| 444 | instructions->push_tail(inst); |
| 445 | } |
| 446 | |
| 447 | static void |
| 448 | generate_all_bvec2(exec_list *instructions, |
| 449 | ir_variable **declarations, |
| 450 | const glsl_type *type) |
| 451 | { |
| 452 | ir_dereference *const arg0 = new ir_dereference(declarations[0]); |
| 453 | ir_rvalue *result; |
| 454 | |
| 455 | (void)type; |
| 456 | |
| 457 | result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type, |
| 458 | new ir_swizzle(arg0, 0, 0, 0, 0, 1), |
| 459 | new ir_swizzle(arg0, 1, 0, 0, 0, 1)); |
| 460 | |
| 461 | ir_instruction *inst = new ir_return(result); |
| 462 | instructions->push_tail(inst); |
| 463 | } |
| 464 | |
| 465 | static void |
| 466 | generate_all_bvec3(exec_list *instructions, |
| 467 | ir_variable **declarations, |
| 468 | const glsl_type *type) |
| 469 | { |
| 470 | ir_dereference *const arg0 = new ir_dereference(declarations[0]); |
| 471 | ir_rvalue *result; |
| 472 | |
| 473 | (void)type; |
| 474 | |
| 475 | result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type, |
| 476 | new ir_swizzle(arg0, 0, 0, 0, 0, 1), |
| 477 | new ir_swizzle(arg0, 1, 0, 0, 0, 1)); |
| 478 | result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type, |
| 479 | result, |
| 480 | new ir_swizzle(arg0, 2, 0, 0, 0, 1)); |
| 481 | |
| 482 | ir_instruction *inst = new ir_return(result); |
| 483 | instructions->push_tail(inst); |
| 484 | } |
| 485 | |
| 486 | static void |
| 487 | generate_all_bvec4(exec_list *instructions, |
| 488 | ir_variable **declarations, |
| 489 | const glsl_type *type) |
| 490 | { |
| 491 | ir_dereference *const arg0 = new ir_dereference(declarations[0]); |
| 492 | ir_rvalue *result; |
| 493 | |
| 494 | (void)type; |
| 495 | |
| 496 | result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type, |
| 497 | new ir_swizzle(arg0, 0, 0, 0, 0, 1), |
| 498 | new ir_swizzle(arg0, 1, 0, 0, 0, 1)); |
| 499 | result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type, |
| 500 | result, |
| 501 | new ir_swizzle(arg0, 2, 0, 0, 0, 1)); |
| 502 | result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type, |
| 503 | result, |
| 504 | new ir_swizzle(arg0, 3, 0, 0, 0, 1)); |
| 505 | |
| 506 | ir_instruction *inst = new ir_return(result); |
| 507 | instructions->push_tail(inst); |
| 508 | } |
| 509 | |
| 510 | static void |
| 511 | generate_not(exec_list *instructions, |
| 512 | ir_variable **declarations, |
| 513 | const glsl_type *type) |
| 514 | { |
| 515 | ir_dereference *const arg0 = new ir_dereference(declarations[0]); |
| 516 | ir_rvalue *result; |
| 517 | |
| 518 | result = new ir_expression(ir_unop_logic_not, type, arg0, NULL); |
| 519 | |
| 520 | ir_instruction *inst = new ir_return(result); |
| 521 | instructions->push_tail(inst); |
| 522 | } |
| 523 | |
| 524 | void |
| 525 | generate_any_functions(glsl_symbol_table *symtab, exec_list *instructions) |
| 526 | { |
| 527 | const char *name = "any"; |
| 528 | ir_function *const f = new ir_function(name); |
| 529 | const glsl_type *bvec2_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 2, 1); |
| 530 | const glsl_type *bvec3_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 3, 1); |
| 531 | const glsl_type *bvec4_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 4, 1); |
| 532 | |
| 533 | bool added = symtab->add_function(name, f); |
| 534 | assert(added); |
| 535 | |
| 536 | generate_function_instance(f, name, instructions, 1, generate_any_bvec2, |
| 537 | glsl_type::bool_type, bvec2_type); |
| 538 | generate_function_instance(f, name, instructions, 1, generate_any_bvec3, |
| 539 | glsl_type::bool_type, bvec3_type); |
| 540 | generate_function_instance(f, name, instructions, 1, generate_any_bvec4, |
| 541 | glsl_type::bool_type, bvec4_type); |
| 542 | } |
| 543 | |
| 544 | void |
| 545 | generate_all_functions(glsl_symbol_table *symtab, exec_list *instructions) |
| 546 | { |
| 547 | const char *name = "all"; |
| 548 | ir_function *const f = new ir_function(name); |
| 549 | const glsl_type *bvec2_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 2, 1); |
| 550 | const glsl_type *bvec3_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 3, 1); |
| 551 | const glsl_type *bvec4_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 4, 1); |
| 552 | |
| 553 | bool added = symtab->add_function(name, f); |
| 554 | assert(added); |
| 555 | |
| 556 | generate_function_instance(f, name, instructions, 1, generate_all_bvec2, |
| 557 | glsl_type::bool_type, bvec2_type); |
| 558 | generate_function_instance(f, name, instructions, 1, generate_all_bvec3, |
| 559 | glsl_type::bool_type, bvec3_type); |
| 560 | generate_function_instance(f, name, instructions, 1, generate_all_bvec4, |
| 561 | glsl_type::bool_type, bvec4_type); |
| 562 | } |
| 563 | |
| 564 | void |
| 565 | generate_not_functions(glsl_symbol_table *symtab, exec_list *instructions) |
| 566 | { |
| 567 | const char *name = "not"; |
| 568 | ir_function *const f = new ir_function(name); |
| 569 | const glsl_type *bvec2_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 2, 1); |
| 570 | const glsl_type *bvec3_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 3, 1); |
| 571 | const glsl_type *bvec4_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 4, 1); |
| 572 | |
| 573 | bool added = symtab->add_function(name, f); |
| 574 | assert(added); |
| 575 | |
| 576 | generate_function_instance(f, name, instructions, 1, generate_not, |
| 577 | bvec2_type, bvec2_type); |
| 578 | generate_function_instance(f, name, instructions, 1, generate_not, |
| 579 | bvec3_type, bvec3_type); |
| 580 | generate_function_instance(f, name, instructions, 1, generate_not, |
| 581 | bvec4_type, bvec4_type); |
| 582 | } |
| 583 | |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 584 | void |
| 585 | generate_110_functions(glsl_symbol_table *symtab, exec_list *instructions) |
| 586 | { |
Eric Anholt | d1e3195 | 2010-03-28 01:55:38 -0700 | [diff] [blame] | 587 | make_gentype_function(symtab, instructions, "radians", 1, generate_radians); |
| 588 | make_gentype_function(symtab, instructions, "degrees", 1, generate_degrees); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 589 | /* FINISHME: sin() */ |
| 590 | /* FINISHME: cos() */ |
| 591 | /* FINISHME: tan() */ |
| 592 | /* FINISHME: asin() */ |
| 593 | /* FINISHME: acos() */ |
| 594 | /* FINISHME: atan(y,x) */ |
| 595 | /* FINISHME: atan(y/x) */ |
Eric Anholt | ddd2e83 | 2010-03-27 12:59:42 -0700 | [diff] [blame] | 596 | make_gentype_function(symtab, instructions, "pow", 2, generate_pow); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 597 | make_gentype_function(symtab, instructions, "exp", 1, generate_exp); |
| 598 | make_gentype_function(symtab, instructions, "log", 1, generate_log); |
Eric Anholt | 0166526 | 2010-03-27 13:56:35 -0700 | [diff] [blame] | 599 | make_gentype_function(symtab, instructions, "exp2", 1, generate_exp2); |
| 600 | make_gentype_function(symtab, instructions, "log2", 1, generate_log2); |
Eric Anholt | 44d68fd | 2010-03-27 13:01:51 -0700 | [diff] [blame] | 601 | make_gentype_function(symtab, instructions, "sqrt", 1, generate_sqrt); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 602 | make_gentype_function(symtab, instructions, "inversesqrt", 1, generate_rsq); |
| 603 | make_gentype_function(symtab, instructions, "abs", 1, generate_abs); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 604 | /* FINISHME: sign() */ |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 605 | make_gentype_function(symtab, instructions, "floor", 1, generate_floor); |
| 606 | make_gentype_function(symtab, instructions, "ceil", 1, generate_ceil); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 607 | /* FINISHME: fract() */ |
| 608 | /* FINISHME: mod(x, float y) */ |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 609 | make_gentype_function(symtab, instructions, "mod", 2, generate_mod); |
| 610 | make_gentype_function(symtab, instructions, "min", 2, generate_min); |
| 611 | /* FINISHME: min(x, float y) */ |
| 612 | make_gentype_function(symtab, instructions, "max", 2, generate_max); |
| 613 | /* FINISHME: max(x, float y) */ |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 614 | /* FINISHME: clamp() */ |
| 615 | /* FINISHME: clamp() */ |
Eric Anholt | cc49cea | 2010-04-08 15:10:37 -0700 | [diff] [blame^] | 616 | make_gentype_function(symtab, instructions, "mix", 3, generate_mix_vec); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 617 | /* FINISHME: mix() */ |
| 618 | /* FINISHME: step() */ |
| 619 | /* FINISHME: step() */ |
| 620 | /* FINISHME: smoothstep() */ |
| 621 | /* FINISHME: smoothstep() */ |
| 622 | /* FINISHME: floor() */ |
| 623 | /* FINISHME: step() */ |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 624 | generate_length_functions(symtab, instructions); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 625 | /* FINISHME: distance() */ |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 626 | generate_dot_functions(symtab, instructions); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 627 | /* FINISHME: cross() */ |
Eric Anholt | 9257592 | 2010-04-08 14:34:38 -0700 | [diff] [blame] | 628 | make_gentype_function(symtab, instructions, "normalize", 1, |
| 629 | generate_normalize); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 630 | /* FINISHME: normalize() */ |
| 631 | /* FINISHME: ftransform() */ |
| 632 | /* FINISHME: faceforward() */ |
| 633 | /* FINISHME: reflect() */ |
| 634 | /* FINISHME: refract() */ |
| 635 | /* FINISHME: matrixCompMult() */ |
| 636 | /* FINISHME: lessThan() */ |
| 637 | /* FINISHME: lessThanEqual() */ |
| 638 | /* FINISHME: greaterThan() */ |
| 639 | /* FINISHME: greaterThanEqual() */ |
| 640 | /* FINISHME: equal() */ |
| 641 | /* FINISHME: notEqual() */ |
Eric Anholt | feeb43b | 2010-04-08 15:02:59 -0700 | [diff] [blame] | 642 | generate_any_functions(symtab, instructions); |
| 643 | generate_all_functions(symtab, instructions); |
| 644 | generate_not_functions(symtab, instructions); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 645 | /* FINISHME: texture*() */ |
| 646 | /* FINISHME: shadow*() */ |
| 647 | /* FINISHME: dFd[xy]() */ |
| 648 | /* FINISHME: fwidth() */ |
| 649 | } |
| 650 | |
| 651 | void |
| 652 | _mesa_glsl_initialize_functions(exec_list *instructions, |
| 653 | struct _mesa_glsl_parse_state *state) |
| 654 | { |
| 655 | generate_110_functions(state->symbols, instructions); |
| 656 | } |