blob: 7cd08d772f0a5e24dc60931f5d1cd4b79c548e4c [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
80generate_rsq(exec_list *instructions,
81 ir_variable **declarations,
82 const glsl_type *type)
83{
84 generate_unop(instructions, declarations, type, ir_unop_rsq);
85}
86
87static void
Eric Anholt44d68fd2010-03-27 13:01:51 -070088generate_sqrt(exec_list *instructions,
89 ir_variable **declarations,
90 const glsl_type *type)
91{
92 generate_unop(instructions, declarations, type, ir_unop_sqrt);
93}
94
95static void
Eric Anholt2eec73f2010-03-27 12:25:20 -070096generate_abs(exec_list *instructions,
97 ir_variable **declarations,
98 const glsl_type *type)
99{
100 generate_unop(instructions, declarations, type, ir_unop_abs);
101}
102
103static void
104generate_ceil(exec_list *instructions,
105 ir_variable **declarations,
106 const glsl_type *type)
107{
108 generate_unop(instructions, declarations, type, ir_unop_ceil);
109}
110
111static void
112generate_floor(exec_list *instructions,
113 ir_variable **declarations,
114 const glsl_type *type)
115{
116 generate_unop(instructions, declarations, type, ir_unop_floor);
117}
118
Eric Anholtbfe380a2010-03-27 12:43:13 -0700119static void
120generate_mod(exec_list *instructions,
121 ir_variable **declarations,
122 const glsl_type *type)
123{
124 generate_binop(instructions, declarations, type, ir_binop_mod);
125}
126
127static void
128generate_min(exec_list *instructions,
129 ir_variable **declarations,
130 const glsl_type *type)
131{
132 generate_binop(instructions, declarations, type, ir_binop_min);
133}
134
135static void
136generate_max(exec_list *instructions,
137 ir_variable **declarations,
138 const glsl_type *type)
139{
140 generate_binop(instructions, declarations, type, ir_binop_max);
141}
142
Eric Anholtddd2e832010-03-27 12:59:42 -0700143
144static void
145generate_pow(exec_list *instructions,
146 ir_variable **declarations,
147 const glsl_type *type)
148{
149 generate_binop(instructions, declarations, type, ir_binop_pow);
150}
151
Eric Anholtc22c4002010-03-26 18:20:30 -0700152void
153generate_function_instance(ir_function *f,
154 const char *name,
155 exec_list *instructions,
Eric Anholtbfe380a2010-03-27 12:43:13 -0700156 int n_args,
Eric Anholtc22c4002010-03-26 18:20:30 -0700157 void (*generate)(exec_list *instructions,
158 ir_variable **declarations,
159 const glsl_type *type),
Eric Anholt53afc362010-03-27 13:55:04 -0700160 const glsl_type *ret_type,
Eric Anholtc22c4002010-03-26 18:20:30 -0700161 const glsl_type *type)
162{
163 ir_variable *declarations[17];
164
165 ir_function_signature *const sig = new ir_function_signature(type);
166 f->signatures.push_tail(sig);
167
168 ir_label *const label = new ir_label(name);
169 instructions->push_tail(label);
170 sig->definition = label;
Eric Anholtbfe380a2010-03-27 12:43:13 -0700171 static const char *arg_names[] = {
172 "arg0",
173 "arg1"
174 };
175 int i;
Eric Anholtc22c4002010-03-26 18:20:30 -0700176
Eric Anholtbfe380a2010-03-27 12:43:13 -0700177 for (i = 0; i < n_args; i++) {
178 ir_variable *var = new ir_variable(type, arg_names[i]);
Eric Anholtc22c4002010-03-26 18:20:30 -0700179
Eric Anholtbfe380a2010-03-27 12:43:13 -0700180 var->mode = ir_var_in;
181 sig->parameters.push_tail(var);
Eric Anholtc22c4002010-03-26 18:20:30 -0700182
Eric Anholtbfe380a2010-03-27 12:43:13 -0700183 var = new ir_variable(type, arg_names[i]);
Eric Anholtc22c4002010-03-26 18:20:30 -0700184
Eric Anholtbfe380a2010-03-27 12:43:13 -0700185 declarations[i] = var;
186 }
Eric Anholtc22c4002010-03-26 18:20:30 -0700187
Eric Anholt53afc362010-03-27 13:55:04 -0700188 ir_variable *retval = new ir_variable(ret_type, "__retval");
Eric Anholtc22c4002010-03-26 18:20:30 -0700189 instructions->push_tail(retval);
190
191 declarations[16] = retval;
192
193 generate(instructions, declarations, type);
194}
195
196void
197make_gentype_function(glsl_symbol_table *symtab, exec_list *instructions,
198 const char *name,
Eric Anholtbfe380a2010-03-27 12:43:13 -0700199 int n_args,
Eric Anholtc22c4002010-03-26 18:20:30 -0700200 void (*generate)(exec_list *instructions,
201 ir_variable **declarations,
202 const glsl_type *type))
203{
204 ir_function *const f = new ir_function(name);
Eric Anholt53afc362010-03-27 13:55:04 -0700205 const glsl_type *float_type = glsl_type::float_type;
Eric Anholtc22c4002010-03-26 18:20:30 -0700206 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
207 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
208 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
209
210 bool added = symtab->add_function(name, f);
211 assert(added);
212
Eric Anholtbfe380a2010-03-27 12:43:13 -0700213 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700214 float_type, float_type);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700215 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700216 vec2_type, vec2_type);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700217 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700218 vec3_type, vec3_type);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700219 generate_function_instance(f, name, instructions, n_args, generate,
Eric Anholt53afc362010-03-27 13:55:04 -0700220 vec4_type, vec4_type);
221}
222
223static void
224generate_length(exec_list *instructions,
225 ir_variable **declarations,
226 const glsl_type *type)
227{
228 ir_dereference *const retval = new ir_dereference(declarations[16]);
229 ir_dereference *const arg = new ir_dereference(declarations[0]);
230 ir_rvalue *result, *temp;
231
232 (void)type;
233
234 /* FINISHME: implement the abs(arg) variant for length(float f) */
235
236 temp = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg);
237 result = new ir_expression(ir_unop_sqrt, glsl_type::float_type, temp, NULL);
238
239 ir_instruction *inst = new ir_assignment(retval, result, NULL);
240 instructions->push_tail(inst);
241}
242
243void
244generate_length_functions(glsl_symbol_table *symtab, exec_list *instructions)
245{
246 const char *name = "length";
247 ir_function *const f = new ir_function(name);
248 const glsl_type *float_type = glsl_type::float_type;
249 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
250 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
251 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
252
253 bool added = symtab->add_function(name, f);
254 assert(added);
255
256 generate_function_instance(f, name, instructions, 1, generate_length,
257 float_type, float_type);
258 generate_function_instance(f, name, instructions, 1, generate_length,
259 float_type, vec2_type);
260 generate_function_instance(f, name, instructions, 1, generate_length,
261 float_type, vec3_type);
262 generate_function_instance(f, name, instructions, 1, generate_length,
263 float_type, vec4_type);
Eric Anholtc22c4002010-03-26 18:20:30 -0700264}
265
266void
267generate_110_functions(glsl_symbol_table *symtab, exec_list *instructions)
268{
269 /* FINISHME: radians() */
270 /* FINISHME: degrees() */
271 /* FINISHME: sin() */
272 /* FINISHME: cos() */
273 /* FINISHME: tan() */
274 /* FINISHME: asin() */
275 /* FINISHME: acos() */
276 /* FINISHME: atan(y,x) */
277 /* FINISHME: atan(y/x) */
Eric Anholtddd2e832010-03-27 12:59:42 -0700278 make_gentype_function(symtab, instructions, "pow", 2, generate_pow);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700279 make_gentype_function(symtab, instructions, "exp", 1, generate_exp);
280 make_gentype_function(symtab, instructions, "log", 1, generate_log);
Eric Anholtc22c4002010-03-26 18:20:30 -0700281 /* FINISHME: exp2() */
282 /* FINISHME: log2() */
Eric Anholt44d68fd2010-03-27 13:01:51 -0700283 make_gentype_function(symtab, instructions, "sqrt", 1, generate_sqrt);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700284 make_gentype_function(symtab, instructions, "inversesqrt", 1, generate_rsq);
285 make_gentype_function(symtab, instructions, "abs", 1, generate_abs);
Eric Anholtc22c4002010-03-26 18:20:30 -0700286 /* FINISHME: sign() */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700287 make_gentype_function(symtab, instructions, "floor", 1, generate_floor);
288 make_gentype_function(symtab, instructions, "ceil", 1, generate_ceil);
Eric Anholtc22c4002010-03-26 18:20:30 -0700289 /* FINISHME: fract() */
290 /* FINISHME: mod(x, float y) */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700291 make_gentype_function(symtab, instructions, "mod", 2, generate_mod);
292 make_gentype_function(symtab, instructions, "min", 2, generate_min);
293 /* FINISHME: min(x, float y) */
294 make_gentype_function(symtab, instructions, "max", 2, generate_max);
295 /* FINISHME: max(x, float y) */
Eric Anholtc22c4002010-03-26 18:20:30 -0700296 /* FINISHME: clamp() */
297 /* FINISHME: clamp() */
298 /* FINISHME: mix() */
299 /* FINISHME: mix() */
300 /* FINISHME: step() */
301 /* FINISHME: step() */
302 /* FINISHME: smoothstep() */
303 /* FINISHME: smoothstep() */
304 /* FINISHME: floor() */
305 /* FINISHME: step() */
Eric Anholt53afc362010-03-27 13:55:04 -0700306 generate_length_functions(symtab, instructions);
Eric Anholtc22c4002010-03-26 18:20:30 -0700307 /* FINISHME: distance() */
308 /* FINISHME: dot() */
309 /* FINISHME: cross() */
310 /* FINISHME: normalize() */
311 /* FINISHME: ftransform() */
312 /* FINISHME: faceforward() */
313 /* FINISHME: reflect() */
314 /* FINISHME: refract() */
315 /* FINISHME: matrixCompMult() */
316 /* FINISHME: lessThan() */
317 /* FINISHME: lessThanEqual() */
318 /* FINISHME: greaterThan() */
319 /* FINISHME: greaterThanEqual() */
320 /* FINISHME: equal() */
321 /* FINISHME: notEqual() */
322 /* FINISHME: any() */
323 /* FINISHME: all() */
324 /* FINISHME: not() */
325 /* FINISHME: texture*() */
326 /* FINISHME: shadow*() */
327 /* FINISHME: dFd[xy]() */
328 /* FINISHME: fwidth() */
329}
330
331void
332_mesa_glsl_initialize_functions(exec_list *instructions,
333 struct _mesa_glsl_parse_state *state)
334{
335 generate_110_functions(state->symbols, instructions);
336}