blob: e59b314a91d68a298eb36a429d51f05407ed9fd2 [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 Anholtc22c4002010-03-26 18:20:30 -0700135void
136generate_function_instance(ir_function *f,
137 const char *name,
138 exec_list *instructions,
Eric Anholtbfe380a2010-03-27 12:43:13 -0700139 int n_args,
Eric Anholtc22c4002010-03-26 18:20:30 -0700140 void (*generate)(exec_list *instructions,
141 ir_variable **declarations,
142 const glsl_type *type),
143 const glsl_type *type)
144{
145 ir_variable *declarations[17];
146
147 ir_function_signature *const sig = new ir_function_signature(type);
148 f->signatures.push_tail(sig);
149
150 ir_label *const label = new ir_label(name);
151 instructions->push_tail(label);
152 sig->definition = label;
Eric Anholtbfe380a2010-03-27 12:43:13 -0700153 static const char *arg_names[] = {
154 "arg0",
155 "arg1"
156 };
157 int i;
Eric Anholtc22c4002010-03-26 18:20:30 -0700158
Eric Anholtbfe380a2010-03-27 12:43:13 -0700159 for (i = 0; i < n_args; i++) {
160 ir_variable *var = new ir_variable(type, arg_names[i]);
Eric Anholtc22c4002010-03-26 18:20:30 -0700161
Eric Anholtbfe380a2010-03-27 12:43:13 -0700162 var->mode = ir_var_in;
163 sig->parameters.push_tail(var);
Eric Anholtc22c4002010-03-26 18:20:30 -0700164
Eric Anholtbfe380a2010-03-27 12:43:13 -0700165 var = new ir_variable(type, arg_names[i]);
Eric Anholtc22c4002010-03-26 18:20:30 -0700166
Eric Anholtbfe380a2010-03-27 12:43:13 -0700167 declarations[i] = var;
168 }
Eric Anholtc22c4002010-03-26 18:20:30 -0700169
170 ir_variable *retval = new ir_variable(type, "__retval");
171 instructions->push_tail(retval);
172
173 declarations[16] = retval;
174
175 generate(instructions, declarations, type);
176}
177
178void
179make_gentype_function(glsl_symbol_table *symtab, exec_list *instructions,
180 const char *name,
Eric Anholtbfe380a2010-03-27 12:43:13 -0700181 int n_args,
Eric Anholtc22c4002010-03-26 18:20:30 -0700182 void (*generate)(exec_list *instructions,
183 ir_variable **declarations,
184 const glsl_type *type))
185{
186 ir_function *const f = new ir_function(name);
187 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
188 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
189 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
190
191 bool added = symtab->add_function(name, f);
192 assert(added);
193
Eric Anholtbfe380a2010-03-27 12:43:13 -0700194 generate_function_instance(f, name, instructions, n_args, generate,
195 glsl_type::float_type);
196 generate_function_instance(f, name, instructions, n_args, generate,
197 vec2_type);
198 generate_function_instance(f, name, instructions, n_args, generate,
199 vec3_type);
200 generate_function_instance(f, name, instructions, n_args, generate,
201 vec4_type);
Eric Anholtc22c4002010-03-26 18:20:30 -0700202}
203
204void
205generate_110_functions(glsl_symbol_table *symtab, exec_list *instructions)
206{
207 /* FINISHME: radians() */
208 /* FINISHME: degrees() */
209 /* FINISHME: sin() */
210 /* FINISHME: cos() */
211 /* FINISHME: tan() */
212 /* FINISHME: asin() */
213 /* FINISHME: acos() */
214 /* FINISHME: atan(y,x) */
215 /* FINISHME: atan(y/x) */
216 /* FINISHME: pow() */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700217 make_gentype_function(symtab, instructions, "exp", 1, generate_exp);
218 make_gentype_function(symtab, instructions, "log", 1, generate_log);
Eric Anholtc22c4002010-03-26 18:20:30 -0700219 /* FINISHME: exp2() */
220 /* FINISHME: log2() */
221 /* FINISHME: sqrt() */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700222 make_gentype_function(symtab, instructions, "inversesqrt", 1, generate_rsq);
223 make_gentype_function(symtab, instructions, "abs", 1, generate_abs);
Eric Anholtc22c4002010-03-26 18:20:30 -0700224 /* FINISHME: sign() */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700225 make_gentype_function(symtab, instructions, "floor", 1, generate_floor);
226 make_gentype_function(symtab, instructions, "ceil", 1, generate_ceil);
Eric Anholtc22c4002010-03-26 18:20:30 -0700227 /* FINISHME: fract() */
228 /* FINISHME: mod(x, float y) */
Eric Anholtbfe380a2010-03-27 12:43:13 -0700229 make_gentype_function(symtab, instructions, "mod", 2, generate_mod);
230 make_gentype_function(symtab, instructions, "min", 2, generate_min);
231 /* FINISHME: min(x, float y) */
232 make_gentype_function(symtab, instructions, "max", 2, generate_max);
233 /* FINISHME: max(x, float y) */
Eric Anholtc22c4002010-03-26 18:20:30 -0700234 /* FINISHME: clamp() */
235 /* FINISHME: clamp() */
236 /* FINISHME: mix() */
237 /* FINISHME: mix() */
238 /* FINISHME: step() */
239 /* FINISHME: step() */
240 /* FINISHME: smoothstep() */
241 /* FINISHME: smoothstep() */
242 /* FINISHME: floor() */
243 /* FINISHME: step() */
244 /* FINISHME: length() */
245 /* FINISHME: distance() */
246 /* FINISHME: dot() */
247 /* FINISHME: cross() */
248 /* FINISHME: normalize() */
249 /* FINISHME: ftransform() */
250 /* FINISHME: faceforward() */
251 /* FINISHME: reflect() */
252 /* FINISHME: refract() */
253 /* FINISHME: matrixCompMult() */
254 /* FINISHME: lessThan() */
255 /* FINISHME: lessThanEqual() */
256 /* FINISHME: greaterThan() */
257 /* FINISHME: greaterThanEqual() */
258 /* FINISHME: equal() */
259 /* FINISHME: notEqual() */
260 /* FINISHME: any() */
261 /* FINISHME: all() */
262 /* FINISHME: not() */
263 /* FINISHME: texture*() */
264 /* FINISHME: shadow*() */
265 /* FINISHME: dFd[xy]() */
266 /* FINISHME: fwidth() */
267}
268
269void
270_mesa_glsl_initialize_functions(exec_list *instructions,
271 struct _mesa_glsl_parse_state *state)
272{
273 generate_110_functions(state->symbols, instructions);
274}