blob: 58e3abfceb77f98f9a583e34ad5ea3fe9154e87a [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 Anholt3cb43582010-03-28 00:36:06 -0700196 var = new ir_variable(type, arg_names[i]);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700197 var->mode = ir_var_in;
198 sig->parameters.push_tail(var);
Eric Anholtc22c4002010-03-26 18:20:30 -0700199
Eric Anholtbfe380a2010-03-27 12:43:13 -0700200 var = new ir_variable(type, arg_names[i]);
Eric Anholt3cb43582010-03-28 00:36:06 -0700201 var->mode = ir_var_in;
202 instructions->push_tail(var);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700203 declarations[i] = var;
204 }
Eric Anholtc22c4002010-03-26 18:20:30 -0700205
Eric Anholt53afc362010-03-27 13:55:04 -0700206 ir_variable *retval = new ir_variable(ret_type, "__retval");
Eric Anholtc22c4002010-03-26 18:20:30 -0700207 instructions->push_tail(retval);
208
209 declarations[16] = retval;
210
211 generate(instructions, declarations, type);
212}
213
214void
215make_gentype_function(glsl_symbol_table *symtab, exec_list *instructions,
216 const char *name,
Eric Anholtbfe380a2010-03-27 12:43:13 -0700217 int n_args,
Eric Anholtc22c4002010-03-26 18:20:30 -0700218 void (*generate)(exec_list *instructions,
219 ir_variable **declarations,
220 const glsl_type *type))
221{
222 ir_function *const f = new ir_function(name);
Eric Anholt53afc362010-03-27 13:55:04 -0700223 const glsl_type *float_type = glsl_type::float_type;
Eric Anholtc22c4002010-03-26 18:20:30 -0700224 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
225 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
226 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
227
228 bool added = symtab->add_function(name, f);
229 assert(added);
230
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 float_type, float_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 vec2_type, vec2_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 vec3_type, vec3_type);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700237 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700238 vec4_type, vec4_type);
239}
240
241static void
242generate_length(exec_list *instructions,
243 ir_variable **declarations,
244 const glsl_type *type)
245{
246 ir_dereference *const retval = new ir_dereference(declarations[16]);
247 ir_dereference *const arg = new ir_dereference(declarations[0]);
248 ir_rvalue *result, *temp;
249
250 (void)type;
251
252 /* FINISHME: implement the abs(arg) variant for length(float f) */
253
254 temp = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg);
255 result = new ir_expression(ir_unop_sqrt, glsl_type::float_type, temp, NULL);
256
257 ir_instruction *inst = new ir_assignment(retval, result, NULL);
258 instructions->push_tail(inst);
259}
260
261void
262generate_length_functions(glsl_symbol_table *symtab, exec_list *instructions)
263{
264 const char *name = "length";
265 ir_function *const f = new ir_function(name);
266 const glsl_type *float_type = glsl_type::float_type;
267 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
268 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
269 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
270
271 bool added = symtab->add_function(name, f);
272 assert(added);
273
274 generate_function_instance(f, name, instructions, 1, generate_length,
275 float_type, float_type);
276 generate_function_instance(f, name, instructions, 1, generate_length,
277 float_type, vec2_type);
278 generate_function_instance(f, name, instructions, 1, generate_length,
279 float_type, vec3_type);
280 generate_function_instance(f, name, instructions, 1, generate_length,
281 float_type, vec4_type);
Eric Anholtc22c4002010-03-26 18:20:30 -0700282}
283
Eric Anholt76a91e12010-03-27 14:04:43 -0700284static void
285generate_dot(exec_list *instructions,
286 ir_variable **declarations,
287 const glsl_type *type)
288{
289 ir_dereference *const retval = new ir_dereference(declarations[16]);
290 ir_dereference *const arg = new ir_dereference(declarations[0]);
291 ir_rvalue *result;
292
293 (void)type;
294
295 result = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg);
296
297 ir_instruction *inst = new ir_assignment(retval, result, NULL);
298 instructions->push_tail(inst);
299}
300
301void
302generate_dot_functions(glsl_symbol_table *symtab, exec_list *instructions)
303{
304 const char *name = "dot";
305 ir_function *const f = new ir_function(name);
306 const glsl_type *float_type = glsl_type::float_type;
307 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
308 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
309 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
310
311 bool added = symtab->add_function(name, f);
312 assert(added);
313
314 generate_function_instance(f, name, instructions, 1, generate_dot,
315 float_type, float_type);
316 generate_function_instance(f, name, instructions, 1, generate_dot,
317 float_type, vec2_type);
318 generate_function_instance(f, name, instructions, 1, generate_dot,
319 float_type, vec3_type);
320 generate_function_instance(f, name, instructions, 1, generate_dot,
321 float_type, vec4_type);
322}
323
Eric Anholtc22c4002010-03-26 18:20:30 -0700324void
325generate_110_functions(glsl_symbol_table *symtab, exec_list *instructions)
326{
327 /* FINISHME: radians() */
328 /* FINISHME: degrees() */
329 /* FINISHME: sin() */
330 /* FINISHME: cos() */
331 /* FINISHME: tan() */
332 /* FINISHME: asin() */
333 /* FINISHME: acos() */
334 /* FINISHME: atan(y,x) */
335 /* FINISHME: atan(y/x) */
Eric Anholtddd2e832010-03-27 12:59:42 -0700336 make_gentype_function(symtab, instructions, "pow", 2, generate_pow);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700337 make_gentype_function(symtab, instructions, "exp", 1, generate_exp);
338 make_gentype_function(symtab, instructions, "log", 1, generate_log);
Eric Anholt01665262010-03-27 13:56:35 -0700339 make_gentype_function(symtab, instructions, "exp2", 1, generate_exp2);
340 make_gentype_function(symtab, instructions, "log2", 1, generate_log2);
Eric Anholt44d68fd2010-03-27 13:01:51 -0700341 make_gentype_function(symtab, instructions, "sqrt", 1, generate_sqrt);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700342 make_gentype_function(symtab, instructions, "inversesqrt", 1, generate_rsq);
343 make_gentype_function(symtab, instructions, "abs", 1, generate_abs);
Eric Anholtc22c4002010-03-26 18:20:30 -0700344 /* FINISHME: sign() */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700345 make_gentype_function(symtab, instructions, "floor", 1, generate_floor);
346 make_gentype_function(symtab, instructions, "ceil", 1, generate_ceil);
Eric Anholtc22c4002010-03-26 18:20:30 -0700347 /* FINISHME: fract() */
348 /* FINISHME: mod(x, float y) */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700349 make_gentype_function(symtab, instructions, "mod", 2, generate_mod);
350 make_gentype_function(symtab, instructions, "min", 2, generate_min);
351 /* FINISHME: min(x, float y) */
352 make_gentype_function(symtab, instructions, "max", 2, generate_max);
353 /* FINISHME: max(x, float y) */
Eric Anholtc22c4002010-03-26 18:20:30 -0700354 /* FINISHME: clamp() */
355 /* FINISHME: clamp() */
356 /* FINISHME: mix() */
357 /* FINISHME: mix() */
358 /* FINISHME: step() */
359 /* FINISHME: step() */
360 /* FINISHME: smoothstep() */
361 /* FINISHME: smoothstep() */
362 /* FINISHME: floor() */
363 /* FINISHME: step() */
Eric Anholt53afc362010-03-27 13:55:04 -0700364 generate_length_functions(symtab, instructions);
Eric Anholtc22c4002010-03-26 18:20:30 -0700365 /* FINISHME: distance() */
Eric Anholt76a91e12010-03-27 14:04:43 -0700366 generate_dot_functions(symtab, instructions);
Eric Anholtc22c4002010-03-26 18:20:30 -0700367 /* FINISHME: cross() */
368 /* FINISHME: normalize() */
369 /* FINISHME: ftransform() */
370 /* FINISHME: faceforward() */
371 /* FINISHME: reflect() */
372 /* FINISHME: refract() */
373 /* FINISHME: matrixCompMult() */
374 /* FINISHME: lessThan() */
375 /* FINISHME: lessThanEqual() */
376 /* FINISHME: greaterThan() */
377 /* FINISHME: greaterThanEqual() */
378 /* FINISHME: equal() */
379 /* FINISHME: notEqual() */
380 /* FINISHME: any() */
381 /* FINISHME: all() */
382 /* FINISHME: not() */
383 /* FINISHME: texture*() */
384 /* FINISHME: shadow*() */
385 /* FINISHME: dFd[xy]() */
386 /* FINISHME: fwidth() */
387}
388
389void
390_mesa_glsl_initialize_functions(exec_list *instructions,
391 struct _mesa_glsl_parse_state *state)
392{
393 generate_110_functions(state->symbols, instructions);
394}