blob: edb33dd8f350ef6f0377f0a2c88c9de23817dc6d [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>
25#include "glsl_symbol_table.h"
26#include "glsl_parser_extras.h"
27#include "glsl_types.h"
28#include "ir.h"
29
30static void
Eric Anholt2eec73f2010-03-27 12:25:20 -070031generate_unop(exec_list *instructions,
32 ir_variable **declarations,
33 const glsl_type *type,
34 enum ir_expression_operation op)
Eric Anholtc22c4002010-03-26 18:20:30 -070035{
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 Anholt2eec73f2010-03-27 12:25:20 -070040 result = new ir_expression(op, type, arg, NULL);
Eric Anholtc22c4002010-03-26 18:20:30 -070041
42 ir_instruction *inst = new ir_assignment(retval, result, NULL);
43 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{
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
63static void
Eric Anholt2eec73f2010-03-27 12:25:20 -070064generate_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
71static void
72generate_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
79static void
Eric Anholt01665262010-03-27 13:56:35 -070080generate_exp2(exec_list *instructions,
81 ir_variable **declarations,
82 const glsl_type *type)
83{
84 generate_unop(instructions, declarations, type, ir_unop_exp2);
85}
86
87static void
88generate_log2(exec_list *instructions,
89 ir_variable **declarations,
90 const glsl_type *type)
91{
92 generate_unop(instructions, declarations, type, ir_unop_log2);
93}
94
95static void
Eric Anholt2eec73f2010-03-27 12:25:20 -070096generate_rsq(exec_list *instructions,
97 ir_variable **declarations,
98 const glsl_type *type)
99{
100 generate_unop(instructions, declarations, type, ir_unop_rsq);
101}
102
103static void
Eric Anholt44d68fd2010-03-27 13:01:51 -0700104generate_sqrt(exec_list *instructions,
105 ir_variable **declarations,
106 const glsl_type *type)
107{
108 generate_unop(instructions, declarations, type, ir_unop_sqrt);
109}
110
111static void
Eric Anholt2eec73f2010-03-27 12:25:20 -0700112generate_abs(exec_list *instructions,
113 ir_variable **declarations,
114 const glsl_type *type)
115{
116 generate_unop(instructions, declarations, type, ir_unop_abs);
117}
118
119static void
120generate_ceil(exec_list *instructions,
121 ir_variable **declarations,
122 const glsl_type *type)
123{
124 generate_unop(instructions, declarations, type, ir_unop_ceil);
125}
126
127static void
128generate_floor(exec_list *instructions,
129 ir_variable **declarations,
130 const glsl_type *type)
131{
132 generate_unop(instructions, declarations, type, ir_unop_floor);
133}
134
Eric Anholtbfe380a2010-03-27 12:43:13 -0700135static void
136generate_mod(exec_list *instructions,
137 ir_variable **declarations,
138 const glsl_type *type)
139{
140 generate_binop(instructions, declarations, type, ir_binop_mod);
141}
142
143static void
144generate_min(exec_list *instructions,
145 ir_variable **declarations,
146 const glsl_type *type)
147{
148 generate_binop(instructions, declarations, type, ir_binop_min);
149}
150
151static void
152generate_max(exec_list *instructions,
153 ir_variable **declarations,
154 const glsl_type *type)
155{
156 generate_binop(instructions, declarations, type, ir_binop_max);
157}
158
Eric Anholtddd2e832010-03-27 12:59:42 -0700159
160static void
161generate_pow(exec_list *instructions,
162 ir_variable **declarations,
163 const glsl_type *type)
164{
165 generate_binop(instructions, declarations, type, ir_binop_pow);
166}
167
Eric Anholtc22c4002010-03-26 18:20:30 -0700168void
169generate_function_instance(ir_function *f,
170 const char *name,
171 exec_list *instructions,
Eric Anholtbfe380a2010-03-27 12:43:13 -0700172 int n_args,
Eric Anholtc22c4002010-03-26 18:20:30 -0700173 void (*generate)(exec_list *instructions,
174 ir_variable **declarations,
175 const glsl_type *type),
Eric Anholt53afc362010-03-27 13:55:04 -0700176 const glsl_type *ret_type,
Eric Anholtc22c4002010-03-26 18:20:30 -0700177 const glsl_type *type)
178{
179 ir_variable *declarations[17];
180
181 ir_function_signature *const sig = new ir_function_signature(type);
182 f->signatures.push_tail(sig);
183
184 ir_label *const label = new ir_label(name);
185 instructions->push_tail(label);
186 sig->definition = label;
Eric Anholtbfe380a2010-03-27 12:43:13 -0700187 static const char *arg_names[] = {
188 "arg0",
189 "arg1"
190 };
191 int i;
Eric Anholtc22c4002010-03-26 18:20:30 -0700192
Eric Anholtbfe380a2010-03-27 12:43:13 -0700193 for (i = 0; i < n_args; i++) {
194 ir_variable *var = new ir_variable(type, arg_names[i]);
Eric Anholtc22c4002010-03-26 18:20:30 -0700195
Eric Anholtbfe380a2010-03-27 12:43:13 -0700196 var->mode = ir_var_in;
197 sig->parameters.push_tail(var);
Eric Anholtc22c4002010-03-26 18:20:30 -0700198
Eric Anholtbfe380a2010-03-27 12:43:13 -0700199 var = new ir_variable(type, arg_names[i]);
Eric Anholtc22c4002010-03-26 18:20:30 -0700200
Eric Anholtbfe380a2010-03-27 12:43:13 -0700201 declarations[i] = var;
202 }
Eric Anholtc22c4002010-03-26 18:20:30 -0700203
Eric Anholt53afc362010-03-27 13:55:04 -0700204 ir_variable *retval = new ir_variable(ret_type, "__retval");
Eric Anholtc22c4002010-03-26 18:20:30 -0700205 instructions->push_tail(retval);
206
207 declarations[16] = retval;
208
209 generate(instructions, declarations, type);
210}
211
212void
213make_gentype_function(glsl_symbol_table *symtab, exec_list *instructions,
214 const char *name,
Eric Anholtbfe380a2010-03-27 12:43:13 -0700215 int n_args,
Eric Anholtc22c4002010-03-26 18:20:30 -0700216 void (*generate)(exec_list *instructions,
217 ir_variable **declarations,
218 const glsl_type *type))
219{
220 ir_function *const f = new ir_function(name);
Eric Anholt53afc362010-03-27 13:55:04 -0700221 const glsl_type *float_type = glsl_type::float_type;
Eric Anholtc22c4002010-03-26 18:20:30 -0700222 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
223 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
224 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
225
226 bool added = symtab->add_function(name, f);
227 assert(added);
228
Eric Anholtbfe380a2010-03-27 12:43:13 -0700229 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700230 float_type, float_type);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700231 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700232 vec2_type, vec2_type);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700233 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700234 vec3_type, vec3_type);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700235 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700236 vec4_type, vec4_type);
237}
238
239static void
240generate_length(exec_list *instructions,
241 ir_variable **declarations,
242 const glsl_type *type)
243{
244 ir_dereference *const retval = new ir_dereference(declarations[16]);
245 ir_dereference *const arg = new ir_dereference(declarations[0]);
246 ir_rvalue *result, *temp;
247
248 (void)type;
249
250 /* FINISHME: implement the abs(arg) variant for length(float f) */
251
252 temp = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg);
253 result = new ir_expression(ir_unop_sqrt, glsl_type::float_type, temp, NULL);
254
255 ir_instruction *inst = new ir_assignment(retval, result, NULL);
256 instructions->push_tail(inst);
257}
258
259void
260generate_length_functions(glsl_symbol_table *symtab, exec_list *instructions)
261{
262 const char *name = "length";
263 ir_function *const f = new ir_function(name);
264 const glsl_type *float_type = glsl_type::float_type;
265 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
266 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
267 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
268
269 bool added = symtab->add_function(name, f);
270 assert(added);
271
272 generate_function_instance(f, name, instructions, 1, generate_length,
273 float_type, float_type);
274 generate_function_instance(f, name, instructions, 1, generate_length,
275 float_type, vec2_type);
276 generate_function_instance(f, name, instructions, 1, generate_length,
277 float_type, vec3_type);
278 generate_function_instance(f, name, instructions, 1, generate_length,
279 float_type, vec4_type);
Eric Anholtc22c4002010-03-26 18:20:30 -0700280}
281
Eric Anholt76a91e12010-03-27 14:04:43 -0700282static void
283generate_dot(exec_list *instructions,
284 ir_variable **declarations,
285 const glsl_type *type)
286{
287 ir_dereference *const retval = new ir_dereference(declarations[16]);
288 ir_dereference *const arg = new ir_dereference(declarations[0]);
289 ir_rvalue *result;
290
291 (void)type;
292
293 result = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg);
294
295 ir_instruction *inst = new ir_assignment(retval, result, NULL);
296 instructions->push_tail(inst);
297}
298
299void
300generate_dot_functions(glsl_symbol_table *symtab, exec_list *instructions)
301{
302 const char *name = "dot";
303 ir_function *const f = new ir_function(name);
304 const glsl_type *float_type = glsl_type::float_type;
305 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
306 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
307 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
308
309 bool added = symtab->add_function(name, f);
310 assert(added);
311
312 generate_function_instance(f, name, instructions, 1, generate_dot,
313 float_type, float_type);
314 generate_function_instance(f, name, instructions, 1, generate_dot,
315 float_type, vec2_type);
316 generate_function_instance(f, name, instructions, 1, generate_dot,
317 float_type, vec3_type);
318 generate_function_instance(f, name, instructions, 1, generate_dot,
319 float_type, vec4_type);
320}
321
Eric Anholtc22c4002010-03-26 18:20:30 -0700322void
323generate_110_functions(glsl_symbol_table *symtab, exec_list *instructions)
324{
325 /* FINISHME: radians() */
326 /* FINISHME: degrees() */
327 /* FINISHME: sin() */
328 /* FINISHME: cos() */
329 /* FINISHME: tan() */
330 /* FINISHME: asin() */
331 /* FINISHME: acos() */
332 /* FINISHME: atan(y,x) */
333 /* FINISHME: atan(y/x) */
Eric Anholtddd2e832010-03-27 12:59:42 -0700334 make_gentype_function(symtab, instructions, "pow", 2, generate_pow);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700335 make_gentype_function(symtab, instructions, "exp", 1, generate_exp);
336 make_gentype_function(symtab, instructions, "log", 1, generate_log);
Eric Anholt01665262010-03-27 13:56:35 -0700337 make_gentype_function(symtab, instructions, "exp2", 1, generate_exp2);
338 make_gentype_function(symtab, instructions, "log2", 1, generate_log2);
Eric Anholt44d68fd2010-03-27 13:01:51 -0700339 make_gentype_function(symtab, instructions, "sqrt", 1, generate_sqrt);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700340 make_gentype_function(symtab, instructions, "inversesqrt", 1, generate_rsq);
341 make_gentype_function(symtab, instructions, "abs", 1, generate_abs);
Eric Anholtc22c4002010-03-26 18:20:30 -0700342 /* FINISHME: sign() */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700343 make_gentype_function(symtab, instructions, "floor", 1, generate_floor);
344 make_gentype_function(symtab, instructions, "ceil", 1, generate_ceil);
Eric Anholtc22c4002010-03-26 18:20:30 -0700345 /* FINISHME: fract() */
346 /* FINISHME: mod(x, float y) */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700347 make_gentype_function(symtab, instructions, "mod", 2, generate_mod);
348 make_gentype_function(symtab, instructions, "min", 2, generate_min);
349 /* FINISHME: min(x, float y) */
350 make_gentype_function(symtab, instructions, "max", 2, generate_max);
351 /* FINISHME: max(x, float y) */
Eric Anholtc22c4002010-03-26 18:20:30 -0700352 /* FINISHME: clamp() */
353 /* FINISHME: clamp() */
354 /* FINISHME: mix() */
355 /* FINISHME: mix() */
356 /* FINISHME: step() */
357 /* FINISHME: step() */
358 /* FINISHME: smoothstep() */
359 /* FINISHME: smoothstep() */
360 /* FINISHME: floor() */
361 /* FINISHME: step() */
Eric Anholt53afc362010-03-27 13:55:04 -0700362 generate_length_functions(symtab, instructions);
Eric Anholtc22c4002010-03-26 18:20:30 -0700363 /* FINISHME: distance() */
Eric Anholt76a91e12010-03-27 14:04:43 -0700364 generate_dot_functions(symtab, instructions);
Eric Anholtc22c4002010-03-26 18:20:30 -0700365 /* FINISHME: cross() */
366 /* FINISHME: normalize() */
367 /* FINISHME: ftransform() */
368 /* FINISHME: faceforward() */
369 /* FINISHME: reflect() */
370 /* FINISHME: refract() */
371 /* FINISHME: matrixCompMult() */
372 /* FINISHME: lessThan() */
373 /* FINISHME: lessThanEqual() */
374 /* FINISHME: greaterThan() */
375 /* FINISHME: greaterThanEqual() */
376 /* FINISHME: equal() */
377 /* FINISHME: notEqual() */
378 /* FINISHME: any() */
379 /* FINISHME: all() */
380 /* FINISHME: not() */
381 /* FINISHME: texture*() */
382 /* FINISHME: shadow*() */
383 /* FINISHME: dFd[xy]() */
384 /* FINISHME: fwidth() */
385}
386
387void
388_mesa_glsl_initialize_functions(exec_list *instructions,
389 struct _mesa_glsl_parse_state *state)
390{
391 generate_110_functions(state->symbols, instructions);
392}