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> |
| 25 | #include "glsl_symbol_table.h" |
| 26 | #include "glsl_parser_extras.h" |
| 27 | #include "glsl_types.h" |
| 28 | #include "ir.h" |
| 29 | |
| 30 | static void |
Eric Anholt | 2eec73f | 2010-03-27 12:25:20 -0700 | [diff] [blame] | 31 | generate_unop(exec_list *instructions, |
| 32 | ir_variable **declarations, |
| 33 | const glsl_type *type, |
| 34 | enum ir_expression_operation op) |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 35 | { |
| 36 | ir_dereference *const retval = new ir_dereference(declarations[16]); |
| 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 | |
| 42 | ir_instruction *inst = new ir_assignment(retval, result, NULL); |
| 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 | { |
| 52 | ir_dereference *const retval = new ir_dereference(declarations[16]); |
| 53 | ir_dereference *const arg1 = new ir_dereference(declarations[0]); |
| 54 | ir_dereference *const arg2 = new ir_dereference(declarations[1]); |
| 55 | ir_rvalue *result; |
| 56 | |
| 57 | result = new ir_expression(op, type, arg1, arg2); |
| 58 | |
| 59 | ir_instruction *inst = new ir_assignment(retval, result, NULL); |
| 60 | instructions->push_tail(inst); |
| 61 | } |
| 62 | |
| 63 | static void |
Eric Anholt | 2eec73f | 2010-03-27 12:25:20 -0700 | [diff] [blame] | 64 | generate_exp(exec_list *instructions, |
| 65 | ir_variable **declarations, |
| 66 | const glsl_type *type) |
| 67 | { |
| 68 | generate_unop(instructions, declarations, type, ir_unop_exp); |
| 69 | } |
| 70 | |
| 71 | static void |
| 72 | generate_log(exec_list *instructions, |
| 73 | ir_variable **declarations, |
| 74 | const glsl_type *type) |
| 75 | { |
| 76 | generate_unop(instructions, declarations, type, ir_unop_log); |
| 77 | } |
| 78 | |
| 79 | static void |
| 80 | generate_rsq(exec_list *instructions, |
| 81 | ir_variable **declarations, |
| 82 | const glsl_type *type) |
| 83 | { |
| 84 | generate_unop(instructions, declarations, type, ir_unop_rsq); |
| 85 | } |
| 86 | |
| 87 | static void |
Eric Anholt | 44d68fd | 2010-03-27 13:01:51 -0700 | [diff] [blame] | 88 | generate_sqrt(exec_list *instructions, |
| 89 | ir_variable **declarations, |
| 90 | const glsl_type *type) |
| 91 | { |
| 92 | generate_unop(instructions, declarations, type, ir_unop_sqrt); |
| 93 | } |
| 94 | |
| 95 | static void |
Eric Anholt | 2eec73f | 2010-03-27 12:25:20 -0700 | [diff] [blame] | 96 | generate_abs(exec_list *instructions, |
| 97 | ir_variable **declarations, |
| 98 | const glsl_type *type) |
| 99 | { |
| 100 | generate_unop(instructions, declarations, type, ir_unop_abs); |
| 101 | } |
| 102 | |
| 103 | static void |
| 104 | generate_ceil(exec_list *instructions, |
| 105 | ir_variable **declarations, |
| 106 | const glsl_type *type) |
| 107 | { |
| 108 | generate_unop(instructions, declarations, type, ir_unop_ceil); |
| 109 | } |
| 110 | |
| 111 | static void |
| 112 | generate_floor(exec_list *instructions, |
| 113 | ir_variable **declarations, |
| 114 | const glsl_type *type) |
| 115 | { |
| 116 | generate_unop(instructions, declarations, type, ir_unop_floor); |
| 117 | } |
| 118 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 119 | static void |
| 120 | generate_mod(exec_list *instructions, |
| 121 | ir_variable **declarations, |
| 122 | const glsl_type *type) |
| 123 | { |
| 124 | generate_binop(instructions, declarations, type, ir_binop_mod); |
| 125 | } |
| 126 | |
| 127 | static void |
| 128 | generate_min(exec_list *instructions, |
| 129 | ir_variable **declarations, |
| 130 | const glsl_type *type) |
| 131 | { |
| 132 | generate_binop(instructions, declarations, type, ir_binop_min); |
| 133 | } |
| 134 | |
| 135 | static void |
| 136 | generate_max(exec_list *instructions, |
| 137 | ir_variable **declarations, |
| 138 | const glsl_type *type) |
| 139 | { |
| 140 | generate_binop(instructions, declarations, type, ir_binop_max); |
| 141 | } |
| 142 | |
Eric Anholt | ddd2e83 | 2010-03-27 12:59:42 -0700 | [diff] [blame] | 143 | |
| 144 | static void |
| 145 | generate_pow(exec_list *instructions, |
| 146 | ir_variable **declarations, |
| 147 | const glsl_type *type) |
| 148 | { |
| 149 | generate_binop(instructions, declarations, type, ir_binop_pow); |
| 150 | } |
| 151 | |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 152 | void |
| 153 | generate_function_instance(ir_function *f, |
| 154 | const char *name, |
| 155 | exec_list *instructions, |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 156 | int n_args, |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 157 | void (*generate)(exec_list *instructions, |
| 158 | ir_variable **declarations, |
| 159 | const glsl_type *type), |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame^] | 160 | const glsl_type *ret_type, |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 161 | const glsl_type *type) |
| 162 | { |
| 163 | ir_variable *declarations[17]; |
| 164 | |
| 165 | ir_function_signature *const sig = new ir_function_signature(type); |
| 166 | f->signatures.push_tail(sig); |
| 167 | |
| 168 | ir_label *const label = new ir_label(name); |
| 169 | instructions->push_tail(label); |
| 170 | sig->definition = label; |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 171 | static const char *arg_names[] = { |
| 172 | "arg0", |
| 173 | "arg1" |
| 174 | }; |
| 175 | int i; |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 176 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 177 | for (i = 0; i < n_args; i++) { |
| 178 | ir_variable *var = new ir_variable(type, arg_names[i]); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 179 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 180 | var->mode = ir_var_in; |
| 181 | sig->parameters.push_tail(var); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 182 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 183 | var = new ir_variable(type, arg_names[i]); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 184 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 185 | declarations[i] = var; |
| 186 | } |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 187 | |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame^] | 188 | ir_variable *retval = new ir_variable(ret_type, "__retval"); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 189 | instructions->push_tail(retval); |
| 190 | |
| 191 | declarations[16] = retval; |
| 192 | |
| 193 | generate(instructions, declarations, type); |
| 194 | } |
| 195 | |
| 196 | void |
| 197 | make_gentype_function(glsl_symbol_table *symtab, exec_list *instructions, |
| 198 | const char *name, |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 199 | int n_args, |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 200 | void (*generate)(exec_list *instructions, |
| 201 | ir_variable **declarations, |
| 202 | const glsl_type *type)) |
| 203 | { |
| 204 | ir_function *const f = new ir_function(name); |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame^] | 205 | const glsl_type *float_type = glsl_type::float_type; |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 206 | const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1); |
| 207 | const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1); |
| 208 | const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1); |
| 209 | |
| 210 | bool added = symtab->add_function(name, f); |
| 211 | assert(added); |
| 212 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 213 | generate_function_instance(f, name, instructions, n_args, generate, |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame^] | 214 | float_type, float_type); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 215 | generate_function_instance(f, name, instructions, n_args, generate, |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame^] | 216 | vec2_type, vec2_type); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 217 | generate_function_instance(f, name, instructions, n_args, generate, |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame^] | 218 | vec3_type, vec3_type); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 219 | generate_function_instance(f, name, instructions, n_args, generate, |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame^] | 220 | vec4_type, vec4_type); |
| 221 | } |
| 222 | |
| 223 | static void |
| 224 | generate_length(exec_list *instructions, |
| 225 | ir_variable **declarations, |
| 226 | const glsl_type *type) |
| 227 | { |
| 228 | ir_dereference *const retval = new ir_dereference(declarations[16]); |
| 229 | ir_dereference *const arg = new ir_dereference(declarations[0]); |
| 230 | ir_rvalue *result, *temp; |
| 231 | |
| 232 | (void)type; |
| 233 | |
| 234 | /* FINISHME: implement the abs(arg) variant for length(float f) */ |
| 235 | |
| 236 | temp = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg); |
| 237 | result = new ir_expression(ir_unop_sqrt, glsl_type::float_type, temp, NULL); |
| 238 | |
| 239 | ir_instruction *inst = new ir_assignment(retval, result, NULL); |
| 240 | instructions->push_tail(inst); |
| 241 | } |
| 242 | |
| 243 | void |
| 244 | generate_length_functions(glsl_symbol_table *symtab, exec_list *instructions) |
| 245 | { |
| 246 | const char *name = "length"; |
| 247 | ir_function *const f = new ir_function(name); |
| 248 | const glsl_type *float_type = glsl_type::float_type; |
| 249 | const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1); |
| 250 | const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1); |
| 251 | const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1); |
| 252 | |
| 253 | bool added = symtab->add_function(name, f); |
| 254 | assert(added); |
| 255 | |
| 256 | generate_function_instance(f, name, instructions, 1, generate_length, |
| 257 | float_type, float_type); |
| 258 | generate_function_instance(f, name, instructions, 1, generate_length, |
| 259 | float_type, vec2_type); |
| 260 | generate_function_instance(f, name, instructions, 1, generate_length, |
| 261 | float_type, vec3_type); |
| 262 | generate_function_instance(f, name, instructions, 1, generate_length, |
| 263 | float_type, vec4_type); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void |
| 267 | generate_110_functions(glsl_symbol_table *symtab, exec_list *instructions) |
| 268 | { |
| 269 | /* FINISHME: radians() */ |
| 270 | /* FINISHME: degrees() */ |
| 271 | /* FINISHME: sin() */ |
| 272 | /* FINISHME: cos() */ |
| 273 | /* FINISHME: tan() */ |
| 274 | /* FINISHME: asin() */ |
| 275 | /* FINISHME: acos() */ |
| 276 | /* FINISHME: atan(y,x) */ |
| 277 | /* FINISHME: atan(y/x) */ |
Eric Anholt | ddd2e83 | 2010-03-27 12:59:42 -0700 | [diff] [blame] | 278 | make_gentype_function(symtab, instructions, "pow", 2, generate_pow); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 279 | make_gentype_function(symtab, instructions, "exp", 1, generate_exp); |
| 280 | make_gentype_function(symtab, instructions, "log", 1, generate_log); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 281 | /* FINISHME: exp2() */ |
| 282 | /* FINISHME: log2() */ |
Eric Anholt | 44d68fd | 2010-03-27 13:01:51 -0700 | [diff] [blame] | 283 | make_gentype_function(symtab, instructions, "sqrt", 1, generate_sqrt); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 284 | make_gentype_function(symtab, instructions, "inversesqrt", 1, generate_rsq); |
| 285 | make_gentype_function(symtab, instructions, "abs", 1, generate_abs); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 286 | /* FINISHME: sign() */ |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 287 | make_gentype_function(symtab, instructions, "floor", 1, generate_floor); |
| 288 | make_gentype_function(symtab, instructions, "ceil", 1, generate_ceil); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 289 | /* FINISHME: fract() */ |
| 290 | /* FINISHME: mod(x, float y) */ |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 291 | make_gentype_function(symtab, instructions, "mod", 2, generate_mod); |
| 292 | make_gentype_function(symtab, instructions, "min", 2, generate_min); |
| 293 | /* FINISHME: min(x, float y) */ |
| 294 | make_gentype_function(symtab, instructions, "max", 2, generate_max); |
| 295 | /* FINISHME: max(x, float y) */ |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 296 | /* FINISHME: clamp() */ |
| 297 | /* FINISHME: clamp() */ |
| 298 | /* FINISHME: mix() */ |
| 299 | /* FINISHME: mix() */ |
| 300 | /* FINISHME: step() */ |
| 301 | /* FINISHME: step() */ |
| 302 | /* FINISHME: smoothstep() */ |
| 303 | /* FINISHME: smoothstep() */ |
| 304 | /* FINISHME: floor() */ |
| 305 | /* FINISHME: step() */ |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame^] | 306 | generate_length_functions(symtab, instructions); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 307 | /* FINISHME: distance() */ |
| 308 | /* FINISHME: dot() */ |
| 309 | /* FINISHME: cross() */ |
| 310 | /* FINISHME: normalize() */ |
| 311 | /* FINISHME: ftransform() */ |
| 312 | /* FINISHME: faceforward() */ |
| 313 | /* FINISHME: reflect() */ |
| 314 | /* FINISHME: refract() */ |
| 315 | /* FINISHME: matrixCompMult() */ |
| 316 | /* FINISHME: lessThan() */ |
| 317 | /* FINISHME: lessThanEqual() */ |
| 318 | /* FINISHME: greaterThan() */ |
| 319 | /* FINISHME: greaterThanEqual() */ |
| 320 | /* FINISHME: equal() */ |
| 321 | /* FINISHME: notEqual() */ |
| 322 | /* FINISHME: any() */ |
| 323 | /* FINISHME: all() */ |
| 324 | /* FINISHME: not() */ |
| 325 | /* FINISHME: texture*() */ |
| 326 | /* FINISHME: shadow*() */ |
| 327 | /* FINISHME: dFd[xy]() */ |
| 328 | /* FINISHME: fwidth() */ |
| 329 | } |
| 330 | |
| 331 | void |
| 332 | _mesa_glsl_initialize_functions(exec_list *instructions, |
| 333 | struct _mesa_glsl_parse_state *state) |
| 334 | { |
| 335 | generate_110_functions(state->symbols, instructions); |
| 336 | } |