blob: e537141e3f0e8975da493e868c9a634a90dced22 [file] [log] [blame]
Eric Anholtc22c4002010-03-26 18:20:30 -07001/*
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 Anholtd1e31952010-03-28 01:55:38 -070025#include <math.h>
Eric Anholtc22c4002010-03-26 18:20:30 -070026#include "glsl_symbol_table.h"
27#include "glsl_parser_extras.h"
28#include "glsl_types.h"
29#include "ir.h"
30
31static void
Eric Anholt2eec73f2010-03-27 12:25:20 -070032generate_unop(exec_list *instructions,
33 ir_variable **declarations,
34 const glsl_type *type,
35 enum ir_expression_operation op)
Eric Anholtc22c4002010-03-26 18:20:30 -070036{
Eric Anholtc22c4002010-03-26 18:20:30 -070037 ir_dereference *const arg = new ir_dereference(declarations[0]);
38 ir_rvalue *result;
39
Eric Anholt2eec73f2010-03-27 12:25:20 -070040 result = new ir_expression(op, type, arg, NULL);
Eric Anholtc22c4002010-03-26 18:20:30 -070041
Eric Anholt7e78e072010-04-07 13:34:15 -070042 ir_instruction *inst = new ir_return(result);
Eric Anholtc22c4002010-03-26 18:20:30 -070043 instructions->push_tail(inst);
44}
45
Eric Anholt2eec73f2010-03-27 12:25:20 -070046static void
Eric Anholtbfe380a2010-03-27 12:43:13 -070047generate_binop(exec_list *instructions,
48 ir_variable **declarations,
49 const glsl_type *type,
50 enum ir_expression_operation op)
51{
Eric Anholtbfe380a2010-03-27 12:43:13 -070052 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 Anholt7e78e072010-04-07 13:34:15 -070058 ir_instruction *inst = new ir_return(result);
Eric Anholtbfe380a2010-03-27 12:43:13 -070059 instructions->push_tail(inst);
60}
61
62static void
Eric Anholtd1e31952010-03-28 01:55:38 -070063generate_radians(exec_list *instructions,
64 ir_variable **declarations,
65 const glsl_type *type)
66{
Eric Anholtd1e31952010-03-28 01:55:38 -070067 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 Anholt7e78e072010-04-07 13:34:15 -070074 ir_instruction *inst = new ir_return(result);
Eric Anholtd1e31952010-03-28 01:55:38 -070075 instructions->push_tail(inst);
76}
77
78static void
79generate_degrees(exec_list *instructions,
80 ir_variable **declarations,
81 const glsl_type *type)
82{
Eric Anholtd1e31952010-03-28 01:55:38 -070083 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 Anholt7e78e072010-04-07 13:34:15 -070090 ir_instruction *inst = new ir_return(result);
Eric Anholtd1e31952010-03-28 01:55:38 -070091 instructions->push_tail(inst);
92}
93
94static void
Eric Anholt2eec73f2010-03-27 12:25:20 -070095generate_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
102static void
103generate_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
110static void
Eric Anholt01665262010-03-27 13:56:35 -0700111generate_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
118static void
119generate_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
126static void
Eric Anholt2eec73f2010-03-27 12:25:20 -0700127generate_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
134static void
Eric Anholt44d68fd2010-03-27 13:01:51 -0700135generate_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
142static void
Eric Anholt2eec73f2010-03-27 12:25:20 -0700143generate_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
150static void
151generate_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
158static void
159generate_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 Anholtbfe380a2010-03-27 12:43:13 -0700166static void
167generate_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
174static void
175generate_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
182static void
183generate_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 Anholtddd2e832010-03-27 12:59:42 -0700190
191static void
192generate_pow(exec_list *instructions,
193 ir_variable **declarations,
194 const glsl_type *type)
195{
196 generate_binop(instructions, declarations, type, ir_binop_pow);
197}
198
Eric Anholtc22c4002010-03-26 18:20:30 -0700199void
200generate_function_instance(ir_function *f,
201 const char *name,
202 exec_list *instructions,
Eric Anholtbfe380a2010-03-27 12:43:13 -0700203 int n_args,
Eric Anholtc22c4002010-03-26 18:20:30 -0700204 void (*generate)(exec_list *instructions,
205 ir_variable **declarations,
206 const glsl_type *type),
Eric Anholt53afc362010-03-27 13:55:04 -0700207 const glsl_type *ret_type,
Eric Anholtc22c4002010-03-26 18:20:30 -0700208 const glsl_type *type)
209{
Eric Anholt7e78e072010-04-07 13:34:15 -0700210 ir_variable *declarations[16];
Eric Anholtc22c4002010-03-26 18:20:30 -0700211
Eric Anholt7e78e072010-04-07 13:34:15 -0700212 ir_function_signature *const sig = new ir_function_signature(ret_type);
Ian Romanick6a15d5b52010-03-31 16:37:10 -0700213 f->add_signature(sig);
Eric Anholtc22c4002010-03-26 18:20:30 -0700214
Eric Anholt894ea972010-04-07 13:19:11 -0700215 ir_label *const label = new ir_label(name, sig);
Eric Anholtc22c4002010-03-26 18:20:30 -0700216 instructions->push_tail(label);
217 sig->definition = label;
Eric Anholtbfe380a2010-03-27 12:43:13 -0700218 static const char *arg_names[] = {
219 "arg0",
220 "arg1"
221 };
222 int i;
Eric Anholtc22c4002010-03-26 18:20:30 -0700223
Eric Anholtbfe380a2010-03-27 12:43:13 -0700224 for (i = 0; i < n_args; i++) {
225 ir_variable *var = new ir_variable(type, arg_names[i]);
Eric Anholtc22c4002010-03-26 18:20:30 -0700226
Eric Anholtbfe380a2010-03-27 12:43:13 -0700227 var->mode = ir_var_in;
228 sig->parameters.push_tail(var);
Eric Anholtc22c4002010-03-26 18:20:30 -0700229
Eric Anholtbfe380a2010-03-27 12:43:13 -0700230 declarations[i] = var;
231 }
Eric Anholtc22c4002010-03-26 18:20:30 -0700232
Eric Anholt894ea972010-04-07 13:19:11 -0700233 generate(&sig->body, declarations, type);
Eric Anholtc22c4002010-03-26 18:20:30 -0700234}
235
236void
237make_gentype_function(glsl_symbol_table *symtab, exec_list *instructions,
238 const char *name,
Eric Anholtbfe380a2010-03-27 12:43:13 -0700239 int n_args,
Eric Anholtc22c4002010-03-26 18:20:30 -0700240 void (*generate)(exec_list *instructions,
241 ir_variable **declarations,
242 const glsl_type *type))
243{
244 ir_function *const f = new ir_function(name);
Eric Anholt53afc362010-03-27 13:55:04 -0700245 const glsl_type *float_type = glsl_type::float_type;
Eric Anholtc22c4002010-03-26 18:20:30 -0700246 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
247 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
248 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
249
250 bool added = symtab->add_function(name, f);
251 assert(added);
252
Eric Anholtbfe380a2010-03-27 12:43:13 -0700253 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700254 float_type, float_type);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700255 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700256 vec2_type, vec2_type);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700257 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700258 vec3_type, vec3_type);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700259 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700260 vec4_type, vec4_type);
261}
262
263static void
264generate_length(exec_list *instructions,
265 ir_variable **declarations,
266 const glsl_type *type)
267{
Eric Anholt53afc362010-03-27 13:55:04 -0700268 ir_dereference *const arg = new ir_dereference(declarations[0]);
269 ir_rvalue *result, *temp;
270
271 (void)type;
272
273 /* FINISHME: implement the abs(arg) variant for length(float f) */
274
275 temp = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg);
276 result = new ir_expression(ir_unop_sqrt, glsl_type::float_type, temp, NULL);
277
Eric Anholt7e78e072010-04-07 13:34:15 -0700278 ir_instruction *inst = new ir_return(result);
Eric Anholt53afc362010-03-27 13:55:04 -0700279 instructions->push_tail(inst);
280}
281
282void
283generate_length_functions(glsl_symbol_table *symtab, exec_list *instructions)
284{
285 const char *name = "length";
286 ir_function *const f = new ir_function(name);
287 const glsl_type *float_type = glsl_type::float_type;
288 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
289 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
290 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
291
292 bool added = symtab->add_function(name, f);
293 assert(added);
294
295 generate_function_instance(f, name, instructions, 1, generate_length,
296 float_type, float_type);
297 generate_function_instance(f, name, instructions, 1, generate_length,
298 float_type, vec2_type);
299 generate_function_instance(f, name, instructions, 1, generate_length,
300 float_type, vec3_type);
301 generate_function_instance(f, name, instructions, 1, generate_length,
302 float_type, vec4_type);
Eric Anholtc22c4002010-03-26 18:20:30 -0700303}
304
Eric Anholt76a91e12010-03-27 14:04:43 -0700305static void
306generate_dot(exec_list *instructions,
307 ir_variable **declarations,
308 const glsl_type *type)
309{
Eric Anholt61733122010-04-07 15:18:37 -0700310 ir_dereference *const arg0 = new ir_dereference(declarations[0]);
311 ir_dereference *const arg1 = new ir_dereference(declarations[1]);
Eric Anholt76a91e12010-03-27 14:04:43 -0700312 ir_rvalue *result;
313
314 (void)type;
315
Eric Anholt61733122010-04-07 15:18:37 -0700316 result = new ir_expression(ir_binop_dot, glsl_type::float_type, arg0, arg1);
Eric Anholt76a91e12010-03-27 14:04:43 -0700317
Eric Anholt7e78e072010-04-07 13:34:15 -0700318 ir_instruction *inst = new ir_return(result);
Eric Anholt76a91e12010-03-27 14:04:43 -0700319 instructions->push_tail(inst);
320}
321
322void
323generate_dot_functions(glsl_symbol_table *symtab, exec_list *instructions)
324{
325 const char *name = "dot";
326 ir_function *const f = new ir_function(name);
327 const glsl_type *float_type = glsl_type::float_type;
328 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
329 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
330 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
331
332 bool added = symtab->add_function(name, f);
333 assert(added);
334
Eric Anholt61733122010-04-07 15:18:37 -0700335 generate_function_instance(f, name, instructions, 2, generate_dot,
Eric Anholt76a91e12010-03-27 14:04:43 -0700336 float_type, float_type);
Eric Anholt61733122010-04-07 15:18:37 -0700337 generate_function_instance(f, name, instructions, 2, generate_dot,
Eric Anholt76a91e12010-03-27 14:04:43 -0700338 float_type, vec2_type);
Eric Anholt61733122010-04-07 15:18:37 -0700339 generate_function_instance(f, name, instructions, 2, generate_dot,
Eric Anholt76a91e12010-03-27 14:04:43 -0700340 float_type, vec3_type);
Eric Anholt61733122010-04-07 15:18:37 -0700341 generate_function_instance(f, name, instructions, 2, generate_dot,
Eric Anholt76a91e12010-03-27 14:04:43 -0700342 float_type, vec4_type);
343}
344
Eric Anholtc22c4002010-03-26 18:20:30 -0700345void
346generate_110_functions(glsl_symbol_table *symtab, exec_list *instructions)
347{
Eric Anholtd1e31952010-03-28 01:55:38 -0700348 make_gentype_function(symtab, instructions, "radians", 1, generate_radians);
349 make_gentype_function(symtab, instructions, "degrees", 1, generate_degrees);
Eric Anholtc22c4002010-03-26 18:20:30 -0700350 /* FINISHME: sin() */
351 /* FINISHME: cos() */
352 /* FINISHME: tan() */
353 /* FINISHME: asin() */
354 /* FINISHME: acos() */
355 /* FINISHME: atan(y,x) */
356 /* FINISHME: atan(y/x) */
Eric Anholtddd2e832010-03-27 12:59:42 -0700357 make_gentype_function(symtab, instructions, "pow", 2, generate_pow);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700358 make_gentype_function(symtab, instructions, "exp", 1, generate_exp);
359 make_gentype_function(symtab, instructions, "log", 1, generate_log);
Eric Anholt01665262010-03-27 13:56:35 -0700360 make_gentype_function(symtab, instructions, "exp2", 1, generate_exp2);
361 make_gentype_function(symtab, instructions, "log2", 1, generate_log2);
Eric Anholt44d68fd2010-03-27 13:01:51 -0700362 make_gentype_function(symtab, instructions, "sqrt", 1, generate_sqrt);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700363 make_gentype_function(symtab, instructions, "inversesqrt", 1, generate_rsq);
364 make_gentype_function(symtab, instructions, "abs", 1, generate_abs);
Eric Anholtc22c4002010-03-26 18:20:30 -0700365 /* FINISHME: sign() */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700366 make_gentype_function(symtab, instructions, "floor", 1, generate_floor);
367 make_gentype_function(symtab, instructions, "ceil", 1, generate_ceil);
Eric Anholtc22c4002010-03-26 18:20:30 -0700368 /* FINISHME: fract() */
369 /* FINISHME: mod(x, float y) */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700370 make_gentype_function(symtab, instructions, "mod", 2, generate_mod);
371 make_gentype_function(symtab, instructions, "min", 2, generate_min);
372 /* FINISHME: min(x, float y) */
373 make_gentype_function(symtab, instructions, "max", 2, generate_max);
374 /* FINISHME: max(x, float y) */
Eric Anholtc22c4002010-03-26 18:20:30 -0700375 /* FINISHME: clamp() */
376 /* FINISHME: clamp() */
377 /* FINISHME: mix() */
378 /* FINISHME: mix() */
379 /* FINISHME: step() */
380 /* FINISHME: step() */
381 /* FINISHME: smoothstep() */
382 /* FINISHME: smoothstep() */
383 /* FINISHME: floor() */
384 /* FINISHME: step() */
Eric Anholt53afc362010-03-27 13:55:04 -0700385 generate_length_functions(symtab, instructions);
Eric Anholtc22c4002010-03-26 18:20:30 -0700386 /* FINISHME: distance() */
Eric Anholt76a91e12010-03-27 14:04:43 -0700387 generate_dot_functions(symtab, instructions);
Eric Anholtc22c4002010-03-26 18:20:30 -0700388 /* FINISHME: cross() */
389 /* FINISHME: normalize() */
390 /* FINISHME: ftransform() */
391 /* FINISHME: faceforward() */
392 /* FINISHME: reflect() */
393 /* FINISHME: refract() */
394 /* FINISHME: matrixCompMult() */
395 /* FINISHME: lessThan() */
396 /* FINISHME: lessThanEqual() */
397 /* FINISHME: greaterThan() */
398 /* FINISHME: greaterThanEqual() */
399 /* FINISHME: equal() */
400 /* FINISHME: notEqual() */
401 /* FINISHME: any() */
402 /* FINISHME: all() */
403 /* FINISHME: not() */
404 /* FINISHME: texture*() */
405 /* FINISHME: shadow*() */
406 /* FINISHME: dFd[xy]() */
407 /* FINISHME: fwidth() */
408}
409
410void
411_mesa_glsl_initialize_functions(exec_list *instructions,
412 struct _mesa_glsl_parse_state *state)
413{
414 generate_110_functions(state->symbols, instructions);
415}