blob: f8ec38c90b557cacadbd4e32fca07f4692f936e7 [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{
37 ir_dereference *const retval = new ir_dereference(declarations[16]);
38 ir_dereference *const arg = new ir_dereference(declarations[0]);
39 ir_rvalue *result;
40
Eric Anholt2eec73f2010-03-27 12:25:20 -070041 result = new ir_expression(op, type, arg, NULL);
Eric Anholtc22c4002010-03-26 18:20:30 -070042
43 ir_instruction *inst = new ir_assignment(retval, result, NULL);
44 instructions->push_tail(inst);
45}
46
Eric Anholt2eec73f2010-03-27 12:25:20 -070047static void
Eric Anholtbfe380a2010-03-27 12:43:13 -070048generate_binop(exec_list *instructions,
49 ir_variable **declarations,
50 const glsl_type *type,
51 enum ir_expression_operation op)
52{
53 ir_dereference *const retval = new ir_dereference(declarations[16]);
54 ir_dereference *const arg1 = new ir_dereference(declarations[0]);
55 ir_dereference *const arg2 = new ir_dereference(declarations[1]);
56 ir_rvalue *result;
57
58 result = new ir_expression(op, type, arg1, arg2);
59
60 ir_instruction *inst = new ir_assignment(retval, result, NULL);
61 instructions->push_tail(inst);
62}
63
64static void
Eric Anholtd1e31952010-03-28 01:55:38 -070065generate_radians(exec_list *instructions,
66 ir_variable **declarations,
67 const glsl_type *type)
68{
69 ir_dereference *const retval = new ir_dereference(declarations[16]);
70 ir_dereference *const arg = new ir_dereference(declarations[0]);
71 ir_rvalue *result;
72
73 result = new ir_expression(ir_binop_mul, type,
74 arg,
75 new ir_constant((float)(M_PI / 180.0)));
76
77 ir_instruction *inst = new ir_assignment(retval, result, NULL);
78 instructions->push_tail(inst);
79}
80
81static void
82generate_degrees(exec_list *instructions,
83 ir_variable **declarations,
84 const glsl_type *type)
85{
86 ir_dereference *const retval = new ir_dereference(declarations[16]);
87 ir_dereference *const arg = new ir_dereference(declarations[0]);
88 ir_rvalue *result;
89
90 result = new ir_expression(ir_binop_mul, type,
91 arg,
92 new ir_constant((float)(180.0 / M_PI)));
93
94 ir_instruction *inst = new ir_assignment(retval, result, NULL);
95 instructions->push_tail(inst);
96}
97
98static void
Eric Anholt2eec73f2010-03-27 12:25:20 -070099generate_exp(exec_list *instructions,
100 ir_variable **declarations,
101 const glsl_type *type)
102{
103 generate_unop(instructions, declarations, type, ir_unop_exp);
104}
105
106static void
107generate_log(exec_list *instructions,
108 ir_variable **declarations,
109 const glsl_type *type)
110{
111 generate_unop(instructions, declarations, type, ir_unop_log);
112}
113
114static void
Eric Anholt01665262010-03-27 13:56:35 -0700115generate_exp2(exec_list *instructions,
116 ir_variable **declarations,
117 const glsl_type *type)
118{
119 generate_unop(instructions, declarations, type, ir_unop_exp2);
120}
121
122static void
123generate_log2(exec_list *instructions,
124 ir_variable **declarations,
125 const glsl_type *type)
126{
127 generate_unop(instructions, declarations, type, ir_unop_log2);
128}
129
130static void
Eric Anholt2eec73f2010-03-27 12:25:20 -0700131generate_rsq(exec_list *instructions,
132 ir_variable **declarations,
133 const glsl_type *type)
134{
135 generate_unop(instructions, declarations, type, ir_unop_rsq);
136}
137
138static void
Eric Anholt44d68fd2010-03-27 13:01:51 -0700139generate_sqrt(exec_list *instructions,
140 ir_variable **declarations,
141 const glsl_type *type)
142{
143 generate_unop(instructions, declarations, type, ir_unop_sqrt);
144}
145
146static void
Eric Anholt2eec73f2010-03-27 12:25:20 -0700147generate_abs(exec_list *instructions,
148 ir_variable **declarations,
149 const glsl_type *type)
150{
151 generate_unop(instructions, declarations, type, ir_unop_abs);
152}
153
154static void
155generate_ceil(exec_list *instructions,
156 ir_variable **declarations,
157 const glsl_type *type)
158{
159 generate_unop(instructions, declarations, type, ir_unop_ceil);
160}
161
162static void
163generate_floor(exec_list *instructions,
164 ir_variable **declarations,
165 const glsl_type *type)
166{
167 generate_unop(instructions, declarations, type, ir_unop_floor);
168}
169
Eric Anholtbfe380a2010-03-27 12:43:13 -0700170static void
171generate_mod(exec_list *instructions,
172 ir_variable **declarations,
173 const glsl_type *type)
174{
175 generate_binop(instructions, declarations, type, ir_binop_mod);
176}
177
178static void
179generate_min(exec_list *instructions,
180 ir_variable **declarations,
181 const glsl_type *type)
182{
183 generate_binop(instructions, declarations, type, ir_binop_min);
184}
185
186static void
187generate_max(exec_list *instructions,
188 ir_variable **declarations,
189 const glsl_type *type)
190{
191 generate_binop(instructions, declarations, type, ir_binop_max);
192}
193
Eric Anholtddd2e832010-03-27 12:59:42 -0700194
195static void
196generate_pow(exec_list *instructions,
197 ir_variable **declarations,
198 const glsl_type *type)
199{
200 generate_binop(instructions, declarations, type, ir_binop_pow);
201}
202
Eric Anholtc22c4002010-03-26 18:20:30 -0700203void
204generate_function_instance(ir_function *f,
205 const char *name,
206 exec_list *instructions,
Eric Anholtbfe380a2010-03-27 12:43:13 -0700207 int n_args,
Eric Anholtc22c4002010-03-26 18:20:30 -0700208 void (*generate)(exec_list *instructions,
209 ir_variable **declarations,
210 const glsl_type *type),
Eric Anholt53afc362010-03-27 13:55:04 -0700211 const glsl_type *ret_type,
Eric Anholtc22c4002010-03-26 18:20:30 -0700212 const glsl_type *type)
213{
214 ir_variable *declarations[17];
215
216 ir_function_signature *const sig = new ir_function_signature(type);
Ian Romanick6a15d5b52010-03-31 16:37:10 -0700217 f->add_signature(sig);
Eric Anholtc22c4002010-03-26 18:20:30 -0700218
Eric Anholt894ea972010-04-07 13:19:11 -0700219 ir_label *const label = new ir_label(name, sig);
Eric Anholtc22c4002010-03-26 18:20:30 -0700220 instructions->push_tail(label);
221 sig->definition = label;
Eric Anholtbfe380a2010-03-27 12:43:13 -0700222 static const char *arg_names[] = {
223 "arg0",
224 "arg1"
225 };
226 int i;
Eric Anholtc22c4002010-03-26 18:20:30 -0700227
Eric Anholtbfe380a2010-03-27 12:43:13 -0700228 for (i = 0; i < n_args; i++) {
229 ir_variable *var = new ir_variable(type, arg_names[i]);
Eric Anholtc22c4002010-03-26 18:20:30 -0700230
Eric Anholt3cb43582010-03-28 00:36:06 -0700231 var = new ir_variable(type, arg_names[i]);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700232 var->mode = ir_var_in;
233 sig->parameters.push_tail(var);
Eric Anholtc22c4002010-03-26 18:20:30 -0700234
Eric Anholtbfe380a2010-03-27 12:43:13 -0700235 var = new ir_variable(type, arg_names[i]);
Eric Anholt3cb43582010-03-28 00:36:06 -0700236 var->mode = ir_var_in;
Eric Anholt894ea972010-04-07 13:19:11 -0700237 sig->body.push_tail(var);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700238 declarations[i] = var;
239 }
Eric Anholtc22c4002010-03-26 18:20:30 -0700240
Eric Anholt53afc362010-03-27 13:55:04 -0700241 ir_variable *retval = new ir_variable(ret_type, "__retval");
Eric Anholt894ea972010-04-07 13:19:11 -0700242 sig->body.push_tail(retval);
Eric Anholtc22c4002010-03-26 18:20:30 -0700243
244 declarations[16] = retval;
245
Eric Anholt894ea972010-04-07 13:19:11 -0700246 generate(&sig->body, declarations, type);
Eric Anholtc22c4002010-03-26 18:20:30 -0700247}
248
249void
250make_gentype_function(glsl_symbol_table *symtab, exec_list *instructions,
251 const char *name,
Eric Anholtbfe380a2010-03-27 12:43:13 -0700252 int n_args,
Eric Anholtc22c4002010-03-26 18:20:30 -0700253 void (*generate)(exec_list *instructions,
254 ir_variable **declarations,
255 const glsl_type *type))
256{
257 ir_function *const f = new ir_function(name);
Eric Anholt53afc362010-03-27 13:55:04 -0700258 const glsl_type *float_type = glsl_type::float_type;
Eric Anholtc22c4002010-03-26 18:20:30 -0700259 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
260 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
261 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
262
263 bool added = symtab->add_function(name, f);
264 assert(added);
265
Eric Anholtbfe380a2010-03-27 12:43:13 -0700266 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700267 float_type, float_type);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700268 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700269 vec2_type, vec2_type);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700270 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700271 vec3_type, vec3_type);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700272 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700273 vec4_type, vec4_type);
274}
275
276static void
277generate_length(exec_list *instructions,
278 ir_variable **declarations,
279 const glsl_type *type)
280{
281 ir_dereference *const retval = new ir_dereference(declarations[16]);
282 ir_dereference *const arg = new ir_dereference(declarations[0]);
283 ir_rvalue *result, *temp;
284
285 (void)type;
286
287 /* FINISHME: implement the abs(arg) variant for length(float f) */
288
289 temp = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg);
290 result = new ir_expression(ir_unop_sqrt, glsl_type::float_type, temp, NULL);
291
292 ir_instruction *inst = new ir_assignment(retval, result, NULL);
293 instructions->push_tail(inst);
294}
295
296void
297generate_length_functions(glsl_symbol_table *symtab, exec_list *instructions)
298{
299 const char *name = "length";
300 ir_function *const f = new ir_function(name);
301 const glsl_type *float_type = glsl_type::float_type;
302 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
303 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
304 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
305
306 bool added = symtab->add_function(name, f);
307 assert(added);
308
309 generate_function_instance(f, name, instructions, 1, generate_length,
310 float_type, float_type);
311 generate_function_instance(f, name, instructions, 1, generate_length,
312 float_type, vec2_type);
313 generate_function_instance(f, name, instructions, 1, generate_length,
314 float_type, vec3_type);
315 generate_function_instance(f, name, instructions, 1, generate_length,
316 float_type, vec4_type);
Eric Anholtc22c4002010-03-26 18:20:30 -0700317}
318
Eric Anholt76a91e12010-03-27 14:04:43 -0700319static void
320generate_dot(exec_list *instructions,
321 ir_variable **declarations,
322 const glsl_type *type)
323{
324 ir_dereference *const retval = new ir_dereference(declarations[16]);
325 ir_dereference *const arg = new ir_dereference(declarations[0]);
326 ir_rvalue *result;
327
328 (void)type;
329
330 result = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg);
331
332 ir_instruction *inst = new ir_assignment(retval, result, NULL);
333 instructions->push_tail(inst);
334}
335
336void
337generate_dot_functions(glsl_symbol_table *symtab, exec_list *instructions)
338{
339 const char *name = "dot";
340 ir_function *const f = new ir_function(name);
341 const glsl_type *float_type = glsl_type::float_type;
342 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
343 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
344 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
345
346 bool added = symtab->add_function(name, f);
347 assert(added);
348
349 generate_function_instance(f, name, instructions, 1, generate_dot,
350 float_type, float_type);
351 generate_function_instance(f, name, instructions, 1, generate_dot,
352 float_type, vec2_type);
353 generate_function_instance(f, name, instructions, 1, generate_dot,
354 float_type, vec3_type);
355 generate_function_instance(f, name, instructions, 1, generate_dot,
356 float_type, vec4_type);
357}
358
Eric Anholtc22c4002010-03-26 18:20:30 -0700359void
360generate_110_functions(glsl_symbol_table *symtab, exec_list *instructions)
361{
Eric Anholtd1e31952010-03-28 01:55:38 -0700362 make_gentype_function(symtab, instructions, "radians", 1, generate_radians);
363 make_gentype_function(symtab, instructions, "degrees", 1, generate_degrees);
Eric Anholtc22c4002010-03-26 18:20:30 -0700364 /* FINISHME: sin() */
365 /* FINISHME: cos() */
366 /* FINISHME: tan() */
367 /* FINISHME: asin() */
368 /* FINISHME: acos() */
369 /* FINISHME: atan(y,x) */
370 /* FINISHME: atan(y/x) */
Eric Anholtddd2e832010-03-27 12:59:42 -0700371 make_gentype_function(symtab, instructions, "pow", 2, generate_pow);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700372 make_gentype_function(symtab, instructions, "exp", 1, generate_exp);
373 make_gentype_function(symtab, instructions, "log", 1, generate_log);
Eric Anholt01665262010-03-27 13:56:35 -0700374 make_gentype_function(symtab, instructions, "exp2", 1, generate_exp2);
375 make_gentype_function(symtab, instructions, "log2", 1, generate_log2);
Eric Anholt44d68fd2010-03-27 13:01:51 -0700376 make_gentype_function(symtab, instructions, "sqrt", 1, generate_sqrt);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700377 make_gentype_function(symtab, instructions, "inversesqrt", 1, generate_rsq);
378 make_gentype_function(symtab, instructions, "abs", 1, generate_abs);
Eric Anholtc22c4002010-03-26 18:20:30 -0700379 /* FINISHME: sign() */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700380 make_gentype_function(symtab, instructions, "floor", 1, generate_floor);
381 make_gentype_function(symtab, instructions, "ceil", 1, generate_ceil);
Eric Anholtc22c4002010-03-26 18:20:30 -0700382 /* FINISHME: fract() */
383 /* FINISHME: mod(x, float y) */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700384 make_gentype_function(symtab, instructions, "mod", 2, generate_mod);
385 make_gentype_function(symtab, instructions, "min", 2, generate_min);
386 /* FINISHME: min(x, float y) */
387 make_gentype_function(symtab, instructions, "max", 2, generate_max);
388 /* FINISHME: max(x, float y) */
Eric Anholtc22c4002010-03-26 18:20:30 -0700389 /* FINISHME: clamp() */
390 /* FINISHME: clamp() */
391 /* FINISHME: mix() */
392 /* FINISHME: mix() */
393 /* FINISHME: step() */
394 /* FINISHME: step() */
395 /* FINISHME: smoothstep() */
396 /* FINISHME: smoothstep() */
397 /* FINISHME: floor() */
398 /* FINISHME: step() */
Eric Anholt53afc362010-03-27 13:55:04 -0700399 generate_length_functions(symtab, instructions);
Eric Anholtc22c4002010-03-26 18:20:30 -0700400 /* FINISHME: distance() */
Eric Anholt76a91e12010-03-27 14:04:43 -0700401 generate_dot_functions(symtab, instructions);
Eric Anholtc22c4002010-03-26 18:20:30 -0700402 /* FINISHME: cross() */
403 /* FINISHME: normalize() */
404 /* FINISHME: ftransform() */
405 /* FINISHME: faceforward() */
406 /* FINISHME: reflect() */
407 /* FINISHME: refract() */
408 /* FINISHME: matrixCompMult() */
409 /* FINISHME: lessThan() */
410 /* FINISHME: lessThanEqual() */
411 /* FINISHME: greaterThan() */
412 /* FINISHME: greaterThanEqual() */
413 /* FINISHME: equal() */
414 /* FINISHME: notEqual() */
415 /* FINISHME: any() */
416 /* FINISHME: all() */
417 /* FINISHME: not() */
418 /* FINISHME: texture*() */
419 /* FINISHME: shadow*() */
420 /* FINISHME: dFd[xy]() */
421 /* FINISHME: fwidth() */
422}
423
424void
425_mesa_glsl_initialize_functions(exec_list *instructions,
426 struct _mesa_glsl_parse_state *state)
427{
428 generate_110_functions(state->symbols, instructions);
429}