Implement dot() builtin.
[mesa.git] / builtin_function.cpp
1 /*
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
30 static void
31 generate_unop(exec_list *instructions,
32 ir_variable **declarations,
33 const glsl_type *type,
34 enum ir_expression_operation op)
35 {
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
40 result = new ir_expression(op, type, arg, NULL);
41
42 ir_instruction *inst = new ir_assignment(retval, result, NULL);
43 instructions->push_tail(inst);
44 }
45
46 static void
47 generate_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
63 static void
64 generate_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
71 static void
72 generate_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
79 static void
80 generate_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
87 static void
88 generate_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
95 static void
96 generate_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
103 static void
104 generate_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
111 static void
112 generate_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
119 static void
120 generate_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
127 static void
128 generate_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
135 static void
136 generate_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
143 static void
144 generate_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
151 static void
152 generate_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
159
160 static void
161 generate_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
168 void
169 generate_function_instance(ir_function *f,
170 const char *name,
171 exec_list *instructions,
172 int n_args,
173 void (*generate)(exec_list *instructions,
174 ir_variable **declarations,
175 const glsl_type *type),
176 const glsl_type *ret_type,
177 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;
187 static const char *arg_names[] = {
188 "arg0",
189 "arg1"
190 };
191 int i;
192
193 for (i = 0; i < n_args; i++) {
194 ir_variable *var = new ir_variable(type, arg_names[i]);
195
196 var->mode = ir_var_in;
197 sig->parameters.push_tail(var);
198
199 var = new ir_variable(type, arg_names[i]);
200
201 declarations[i] = var;
202 }
203
204 ir_variable *retval = new ir_variable(ret_type, "__retval");
205 instructions->push_tail(retval);
206
207 declarations[16] = retval;
208
209 generate(instructions, declarations, type);
210 }
211
212 void
213 make_gentype_function(glsl_symbol_table *symtab, exec_list *instructions,
214 const char *name,
215 int n_args,
216 void (*generate)(exec_list *instructions,
217 ir_variable **declarations,
218 const glsl_type *type))
219 {
220 ir_function *const f = new ir_function(name);
221 const glsl_type *float_type = glsl_type::float_type;
222 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
229 generate_function_instance(f, name, instructions, n_args, generate,
230 float_type, float_type);
231 generate_function_instance(f, name, instructions, n_args, generate,
232 vec2_type, vec2_type);
233 generate_function_instance(f, name, instructions, n_args, generate,
234 vec3_type, vec3_type);
235 generate_function_instance(f, name, instructions, n_args, generate,
236 vec4_type, vec4_type);
237 }
238
239 static void
240 generate_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
259 void
260 generate_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);
280 }
281
282 static void
283 generate_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
299 void
300 generate_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
322 void
323 generate_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) */
334 make_gentype_function(symtab, instructions, "pow", 2, generate_pow);
335 make_gentype_function(symtab, instructions, "exp", 1, generate_exp);
336 make_gentype_function(symtab, instructions, "log", 1, generate_log);
337 make_gentype_function(symtab, instructions, "exp2", 1, generate_exp2);
338 make_gentype_function(symtab, instructions, "log2", 1, generate_log2);
339 make_gentype_function(symtab, instructions, "sqrt", 1, generate_sqrt);
340 make_gentype_function(symtab, instructions, "inversesqrt", 1, generate_rsq);
341 make_gentype_function(symtab, instructions, "abs", 1, generate_abs);
342 /* FINISHME: sign() */
343 make_gentype_function(symtab, instructions, "floor", 1, generate_floor);
344 make_gentype_function(symtab, instructions, "ceil", 1, generate_ceil);
345 /* FINISHME: fract() */
346 /* FINISHME: mod(x, float y) */
347 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) */
352 /* 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() */
362 generate_length_functions(symtab, instructions);
363 /* FINISHME: distance() */
364 generate_dot_functions(symtab, instructions);
365 /* 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
387 void
388 _mesa_glsl_initialize_functions(exec_list *instructions,
389 struct _mesa_glsl_parse_state *state)
390 {
391 generate_110_functions(state->symbols, instructions);
392 }