blob: ec1b54a2dd54d7ed2610ad286225c0e24bd368c3 [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
88generate_abs(exec_list *instructions,
89 ir_variable **declarations,
90 const glsl_type *type)
91{
92 generate_unop(instructions, declarations, type, ir_unop_abs);
93}
94
95static void
96generate_ceil(exec_list *instructions,
97 ir_variable **declarations,
98 const glsl_type *type)
99{
100 generate_unop(instructions, declarations, type, ir_unop_ceil);
101}
102
103static void
104generate_floor(exec_list *instructions,
105 ir_variable **declarations,
106 const glsl_type *type)
107{
108 generate_unop(instructions, declarations, type, ir_unop_floor);
109}
110
Eric Anholtbfe380a2010-03-27 12:43:13 -0700111static void
112generate_mod(exec_list *instructions,
113 ir_variable **declarations,
114 const glsl_type *type)
115{
116 generate_binop(instructions, declarations, type, ir_binop_mod);
117}
118
119static void
120generate_min(exec_list *instructions,
121 ir_variable **declarations,
122 const glsl_type *type)
123{
124 generate_binop(instructions, declarations, type, ir_binop_min);
125}
126
127static void
128generate_max(exec_list *instructions,
129 ir_variable **declarations,
130 const glsl_type *type)
131{
132 generate_binop(instructions, declarations, type, ir_binop_max);
133}
134
Eric Anholtddd2e832010-03-27 12:59:42 -0700135
136static void
137generate_pow(exec_list *instructions,
138 ir_variable **declarations,
139 const glsl_type *type)
140{
141 generate_binop(instructions, declarations, type, ir_binop_pow);
142}
143
Eric Anholtc22c4002010-03-26 18:20:30 -0700144void
145generate_function_instance(ir_function *f,
146 const char *name,
147 exec_list *instructions,
Eric Anholtbfe380a2010-03-27 12:43:13 -0700148 int n_args,
Eric Anholtc22c4002010-03-26 18:20:30 -0700149 void (*generate)(exec_list *instructions,
150 ir_variable **declarations,
151 const glsl_type *type),
152 const glsl_type *type)
153{
154 ir_variable *declarations[17];
155
156 ir_function_signature *const sig = new ir_function_signature(type);
157 f->signatures.push_tail(sig);
158
159 ir_label *const label = new ir_label(name);
160 instructions->push_tail(label);
161 sig->definition = label;
Eric Anholtbfe380a2010-03-27 12:43:13 -0700162 static const char *arg_names[] = {
163 "arg0",
164 "arg1"
165 };
166 int i;
Eric Anholtc22c4002010-03-26 18:20:30 -0700167
Eric Anholtbfe380a2010-03-27 12:43:13 -0700168 for (i = 0; i < n_args; i++) {
169 ir_variable *var = new ir_variable(type, arg_names[i]);
Eric Anholtc22c4002010-03-26 18:20:30 -0700170
Eric Anholtbfe380a2010-03-27 12:43:13 -0700171 var->mode = ir_var_in;
172 sig->parameters.push_tail(var);
Eric Anholtc22c4002010-03-26 18:20:30 -0700173
Eric Anholtbfe380a2010-03-27 12:43:13 -0700174 var = new ir_variable(type, arg_names[i]);
Eric Anholtc22c4002010-03-26 18:20:30 -0700175
Eric Anholtbfe380a2010-03-27 12:43:13 -0700176 declarations[i] = var;
177 }
Eric Anholtc22c4002010-03-26 18:20:30 -0700178
179 ir_variable *retval = new ir_variable(type, "__retval");
180 instructions->push_tail(retval);
181
182 declarations[16] = retval;
183
184 generate(instructions, declarations, type);
185}
186
187void
188make_gentype_function(glsl_symbol_table *symtab, exec_list *instructions,
189 const char *name,
Eric Anholtbfe380a2010-03-27 12:43:13 -0700190 int n_args,
Eric Anholtc22c4002010-03-26 18:20:30 -0700191 void (*generate)(exec_list *instructions,
192 ir_variable **declarations,
193 const glsl_type *type))
194{
195 ir_function *const f = new ir_function(name);
196 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
197 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
198 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
199
200 bool added = symtab->add_function(name, f);
201 assert(added);
202
Eric Anholtbfe380a2010-03-27 12:43:13 -0700203 generate_function_instance(f, name, instructions, n_args, generate,
204 glsl_type::float_type);
205 generate_function_instance(f, name, instructions, n_args, generate,
206 vec2_type);
207 generate_function_instance(f, name, instructions, n_args, generate,
208 vec3_type);
209 generate_function_instance(f, name, instructions, n_args, generate,
210 vec4_type);
Eric Anholtc22c4002010-03-26 18:20:30 -0700211}
212
213void
214generate_110_functions(glsl_symbol_table *symtab, exec_list *instructions)
215{
216 /* FINISHME: radians() */
217 /* FINISHME: degrees() */
218 /* FINISHME: sin() */
219 /* FINISHME: cos() */
220 /* FINISHME: tan() */
221 /* FINISHME: asin() */
222 /* FINISHME: acos() */
223 /* FINISHME: atan(y,x) */
224 /* FINISHME: atan(y/x) */
Eric Anholtddd2e832010-03-27 12:59:42 -0700225 make_gentype_function(symtab, instructions, "pow", 2, generate_pow);
Eric Anholtbfe380a2010-03-27 12:43:13 -0700226 make_gentype_function(symtab, instructions, "exp", 1, generate_exp);
227 make_gentype_function(symtab, instructions, "log", 1, generate_log);
Eric Anholtc22c4002010-03-26 18:20:30 -0700228 /* FINISHME: exp2() */
229 /* FINISHME: log2() */
230 /* FINISHME: sqrt() */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700231 make_gentype_function(symtab, instructions, "inversesqrt", 1, generate_rsq);
232 make_gentype_function(symtab, instructions, "abs", 1, generate_abs);
Eric Anholtc22c4002010-03-26 18:20:30 -0700233 /* FINISHME: sign() */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700234 make_gentype_function(symtab, instructions, "floor", 1, generate_floor);
235 make_gentype_function(symtab, instructions, "ceil", 1, generate_ceil);
Eric Anholtc22c4002010-03-26 18:20:30 -0700236 /* FINISHME: fract() */
237 /* FINISHME: mod(x, float y) */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700238 make_gentype_function(symtab, instructions, "mod", 2, generate_mod);
239 make_gentype_function(symtab, instructions, "min", 2, generate_min);
240 /* FINISHME: min(x, float y) */
241 make_gentype_function(symtab, instructions, "max", 2, generate_max);
242 /* FINISHME: max(x, float y) */
Eric Anholtc22c4002010-03-26 18:20:30 -0700243 /* FINISHME: clamp() */
244 /* FINISHME: clamp() */
245 /* FINISHME: mix() */
246 /* FINISHME: mix() */
247 /* FINISHME: step() */
248 /* FINISHME: step() */
249 /* FINISHME: smoothstep() */
250 /* FINISHME: smoothstep() */
251 /* FINISHME: floor() */
252 /* FINISHME: step() */
253 /* FINISHME: length() */
254 /* FINISHME: distance() */
255 /* FINISHME: dot() */
256 /* FINISHME: cross() */
257 /* FINISHME: normalize() */
258 /* FINISHME: ftransform() */
259 /* FINISHME: faceforward() */
260 /* FINISHME: reflect() */
261 /* FINISHME: refract() */
262 /* FINISHME: matrixCompMult() */
263 /* FINISHME: lessThan() */
264 /* FINISHME: lessThanEqual() */
265 /* FINISHME: greaterThan() */
266 /* FINISHME: greaterThanEqual() */
267 /* FINISHME: equal() */
268 /* FINISHME: notEqual() */
269 /* FINISHME: any() */
270 /* FINISHME: all() */
271 /* FINISHME: not() */
272 /* FINISHME: texture*() */
273 /* FINISHME: shadow*() */
274 /* FINISHME: dFd[xy]() */
275 /* FINISHME: fwidth() */
276}
277
278void
279_mesa_glsl_initialize_functions(exec_list *instructions,
280 struct _mesa_glsl_parse_state *state)
281{
282 generate_110_functions(state->symbols, instructions);
283}