blob: ec6fd1b11a3e7902c3925c5e70d29d816d3e8b49 [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 Anholt3cb43582010-03-28 00:36:06 -0700227 var = new ir_variable(type, arg_names[i]);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700228 var->mode = ir_var_in;
229 sig->parameters.push_tail(var);
Eric Anholtc22c4002010-03-26 18:20:30 -0700230
Eric Anholtbfe380a2010-03-27 12:43:13 -0700231 var = new ir_variable(type, arg_names[i]);
Eric Anholt3cb43582010-03-28 00:36:06 -0700232 var->mode = ir_var_in;
Eric Anholt894ea972010-04-07 13:19:11 -0700233 sig->body.push_tail(var);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700234 declarations[i] = var;
235 }
Eric Anholtc22c4002010-03-26 18:20:30 -0700236
Eric Anholt894ea972010-04-07 13:19:11 -0700237 generate(&sig->body, declarations, type);
Eric Anholtc22c4002010-03-26 18:20:30 -0700238}
239
240void
241make_gentype_function(glsl_symbol_table *symtab, exec_list *instructions,
242 const char *name,
Eric Anholtbfe380a2010-03-27 12:43:13 -0700243 int n_args,
Eric Anholtc22c4002010-03-26 18:20:30 -0700244 void (*generate)(exec_list *instructions,
245 ir_variable **declarations,
246 const glsl_type *type))
247{
248 ir_function *const f = new ir_function(name);
Eric Anholt53afc362010-03-27 13:55:04 -0700249 const glsl_type *float_type = glsl_type::float_type;
Eric Anholtc22c4002010-03-26 18:20:30 -0700250 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
251 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
252 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
253
254 bool added = symtab->add_function(name, f);
255 assert(added);
256
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 float_type, float_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 vec2_type, vec2_type);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700261 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700262 vec3_type, vec3_type);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700263 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700264 vec4_type, vec4_type);
265}
266
267static void
268generate_length(exec_list *instructions,
269 ir_variable **declarations,
270 const glsl_type *type)
271{
Eric Anholt53afc362010-03-27 13:55:04 -0700272 ir_dereference *const arg = new ir_dereference(declarations[0]);
273 ir_rvalue *result, *temp;
274
275 (void)type;
276
277 /* FINISHME: implement the abs(arg) variant for length(float f) */
278
279 temp = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg);
280 result = new ir_expression(ir_unop_sqrt, glsl_type::float_type, temp, NULL);
281
Eric Anholt7e78e072010-04-07 13:34:15 -0700282 ir_instruction *inst = new ir_return(result);
Eric Anholt53afc362010-03-27 13:55:04 -0700283 instructions->push_tail(inst);
284}
285
286void
287generate_length_functions(glsl_symbol_table *symtab, exec_list *instructions)
288{
289 const char *name = "length";
290 ir_function *const f = new ir_function(name);
291 const glsl_type *float_type = glsl_type::float_type;
292 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
293 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
294 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
295
296 bool added = symtab->add_function(name, f);
297 assert(added);
298
299 generate_function_instance(f, name, instructions, 1, generate_length,
300 float_type, float_type);
301 generate_function_instance(f, name, instructions, 1, generate_length,
302 float_type, vec2_type);
303 generate_function_instance(f, name, instructions, 1, generate_length,
304 float_type, vec3_type);
305 generate_function_instance(f, name, instructions, 1, generate_length,
306 float_type, vec4_type);
Eric Anholtc22c4002010-03-26 18:20:30 -0700307}
308
Eric Anholt76a91e12010-03-27 14:04:43 -0700309static void
310generate_dot(exec_list *instructions,
311 ir_variable **declarations,
312 const glsl_type *type)
313{
Eric Anholt76a91e12010-03-27 14:04:43 -0700314 ir_dereference *const arg = new ir_dereference(declarations[0]);
315 ir_rvalue *result;
316
317 (void)type;
318
319 result = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg);
320
Eric Anholt7e78e072010-04-07 13:34:15 -0700321 ir_instruction *inst = new ir_return(result);
Eric Anholt76a91e12010-03-27 14:04:43 -0700322 instructions->push_tail(inst);
323}
324
325void
326generate_dot_functions(glsl_symbol_table *symtab, exec_list *instructions)
327{
328 const char *name = "dot";
329 ir_function *const f = new ir_function(name);
330 const glsl_type *float_type = glsl_type::float_type;
331 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
332 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
333 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
334
335 bool added = symtab->add_function(name, f);
336 assert(added);
337
338 generate_function_instance(f, name, instructions, 1, generate_dot,
339 float_type, float_type);
340 generate_function_instance(f, name, instructions, 1, generate_dot,
341 float_type, vec2_type);
342 generate_function_instance(f, name, instructions, 1, generate_dot,
343 float_type, vec3_type);
344 generate_function_instance(f, name, instructions, 1, generate_dot,
345 float_type, vec4_type);
346}
347
Eric Anholtc22c4002010-03-26 18:20:30 -0700348void
349generate_110_functions(glsl_symbol_table *symtab, exec_list *instructions)
350{
Eric Anholtd1e31952010-03-28 01:55:38 -0700351 make_gentype_function(symtab, instructions, "radians", 1, generate_radians);
352 make_gentype_function(symtab, instructions, "degrees", 1, generate_degrees);
Eric Anholtc22c4002010-03-26 18:20:30 -0700353 /* FINISHME: sin() */
354 /* FINISHME: cos() */
355 /* FINISHME: tan() */
356 /* FINISHME: asin() */
357 /* FINISHME: acos() */
358 /* FINISHME: atan(y,x) */
359 /* FINISHME: atan(y/x) */
Eric Anholtddd2e832010-03-27 12:59:42 -0700360 make_gentype_function(symtab, instructions, "pow", 2, generate_pow);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700361 make_gentype_function(symtab, instructions, "exp", 1, generate_exp);
362 make_gentype_function(symtab, instructions, "log", 1, generate_log);
Eric Anholt01665262010-03-27 13:56:35 -0700363 make_gentype_function(symtab, instructions, "exp2", 1, generate_exp2);
364 make_gentype_function(symtab, instructions, "log2", 1, generate_log2);
Eric Anholt44d68fd2010-03-27 13:01:51 -0700365 make_gentype_function(symtab, instructions, "sqrt", 1, generate_sqrt);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700366 make_gentype_function(symtab, instructions, "inversesqrt", 1, generate_rsq);
367 make_gentype_function(symtab, instructions, "abs", 1, generate_abs);
Eric Anholtc22c4002010-03-26 18:20:30 -0700368 /* FINISHME: sign() */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700369 make_gentype_function(symtab, instructions, "floor", 1, generate_floor);
370 make_gentype_function(symtab, instructions, "ceil", 1, generate_ceil);
Eric Anholtc22c4002010-03-26 18:20:30 -0700371 /* FINISHME: fract() */
372 /* FINISHME: mod(x, float y) */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700373 make_gentype_function(symtab, instructions, "mod", 2, generate_mod);
374 make_gentype_function(symtab, instructions, "min", 2, generate_min);
375 /* FINISHME: min(x, float y) */
376 make_gentype_function(symtab, instructions, "max", 2, generate_max);
377 /* FINISHME: max(x, float y) */
Eric Anholtc22c4002010-03-26 18:20:30 -0700378 /* FINISHME: clamp() */
379 /* FINISHME: clamp() */
380 /* FINISHME: mix() */
381 /* FINISHME: mix() */
382 /* FINISHME: step() */
383 /* FINISHME: step() */
384 /* FINISHME: smoothstep() */
385 /* FINISHME: smoothstep() */
386 /* FINISHME: floor() */
387 /* FINISHME: step() */
Eric Anholt53afc362010-03-27 13:55:04 -0700388 generate_length_functions(symtab, instructions);
Eric Anholtc22c4002010-03-26 18:20:30 -0700389 /* FINISHME: distance() */
Eric Anholt76a91e12010-03-27 14:04:43 -0700390 generate_dot_functions(symtab, instructions);
Eric Anholtc22c4002010-03-26 18:20:30 -0700391 /* FINISHME: cross() */
392 /* FINISHME: normalize() */
393 /* FINISHME: ftransform() */
394 /* FINISHME: faceforward() */
395 /* FINISHME: reflect() */
396 /* FINISHME: refract() */
397 /* FINISHME: matrixCompMult() */
398 /* FINISHME: lessThan() */
399 /* FINISHME: lessThanEqual() */
400 /* FINISHME: greaterThan() */
401 /* FINISHME: greaterThanEqual() */
402 /* FINISHME: equal() */
403 /* FINISHME: notEqual() */
404 /* FINISHME: any() */
405 /* FINISHME: all() */
406 /* FINISHME: not() */
407 /* FINISHME: texture*() */
408 /* FINISHME: shadow*() */
409 /* FINISHME: dFd[xy]() */
410 /* FINISHME: fwidth() */
411}
412
413void
414_mesa_glsl_initialize_functions(exec_list *instructions,
415 struct _mesa_glsl_parse_state *state)
416{
417 generate_110_functions(state->symbols, instructions);
418}