Add some variable declaration qualifier tests
[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 <math.h>
26 #include "glsl_symbol_table.h"
27 #include "glsl_parser_extras.h"
28 #include "glsl_types.h"
29 #include "ir.h"
30
31 static void
32 generate_unop(exec_list *instructions,
33 ir_variable **declarations,
34 const glsl_type *type,
35 enum ir_expression_operation op)
36 {
37 ir_dereference *const retval = new ir_dereference(declarations[16]);
38 ir_dereference *const arg = new ir_dereference(declarations[0]);
39 ir_rvalue *result;
40
41 result = new ir_expression(op, type, arg, NULL);
42
43 ir_instruction *inst = new ir_assignment(retval, result, NULL);
44 instructions->push_tail(inst);
45 }
46
47 static void
48 generate_binop(exec_list *instructions,
49 ir_variable **declarations,
50 const glsl_type *type,
51 enum ir_expression_operation op)
52 {
53 ir_dereference *const retval = new ir_dereference(declarations[16]);
54 ir_dereference *const arg1 = new ir_dereference(declarations[0]);
55 ir_dereference *const arg2 = new ir_dereference(declarations[1]);
56 ir_rvalue *result;
57
58 result = new ir_expression(op, type, arg1, arg2);
59
60 ir_instruction *inst = new ir_assignment(retval, result, NULL);
61 instructions->push_tail(inst);
62 }
63
64 static void
65 generate_radians(exec_list *instructions,
66 ir_variable **declarations,
67 const glsl_type *type)
68 {
69 ir_dereference *const retval = new ir_dereference(declarations[16]);
70 ir_dereference *const arg = new ir_dereference(declarations[0]);
71 ir_rvalue *result;
72
73 result = new ir_expression(ir_binop_mul, type,
74 arg,
75 new ir_constant((float)(M_PI / 180.0)));
76
77 ir_instruction *inst = new ir_assignment(retval, result, NULL);
78 instructions->push_tail(inst);
79 }
80
81 static void
82 generate_degrees(exec_list *instructions,
83 ir_variable **declarations,
84 const glsl_type *type)
85 {
86 ir_dereference *const retval = new ir_dereference(declarations[16]);
87 ir_dereference *const arg = new ir_dereference(declarations[0]);
88 ir_rvalue *result;
89
90 result = new ir_expression(ir_binop_mul, type,
91 arg,
92 new ir_constant((float)(180.0 / M_PI)));
93
94 ir_instruction *inst = new ir_assignment(retval, result, NULL);
95 instructions->push_tail(inst);
96 }
97
98 static void
99 generate_exp(exec_list *instructions,
100 ir_variable **declarations,
101 const glsl_type *type)
102 {
103 generate_unop(instructions, declarations, type, ir_unop_exp);
104 }
105
106 static void
107 generate_log(exec_list *instructions,
108 ir_variable **declarations,
109 const glsl_type *type)
110 {
111 generate_unop(instructions, declarations, type, ir_unop_log);
112 }
113
114 static void
115 generate_exp2(exec_list *instructions,
116 ir_variable **declarations,
117 const glsl_type *type)
118 {
119 generate_unop(instructions, declarations, type, ir_unop_exp2);
120 }
121
122 static void
123 generate_log2(exec_list *instructions,
124 ir_variable **declarations,
125 const glsl_type *type)
126 {
127 generate_unop(instructions, declarations, type, ir_unop_log2);
128 }
129
130 static void
131 generate_rsq(exec_list *instructions,
132 ir_variable **declarations,
133 const glsl_type *type)
134 {
135 generate_unop(instructions, declarations, type, ir_unop_rsq);
136 }
137
138 static void
139 generate_sqrt(exec_list *instructions,
140 ir_variable **declarations,
141 const glsl_type *type)
142 {
143 generate_unop(instructions, declarations, type, ir_unop_sqrt);
144 }
145
146 static void
147 generate_abs(exec_list *instructions,
148 ir_variable **declarations,
149 const glsl_type *type)
150 {
151 generate_unop(instructions, declarations, type, ir_unop_abs);
152 }
153
154 static void
155 generate_ceil(exec_list *instructions,
156 ir_variable **declarations,
157 const glsl_type *type)
158 {
159 generate_unop(instructions, declarations, type, ir_unop_ceil);
160 }
161
162 static void
163 generate_floor(exec_list *instructions,
164 ir_variable **declarations,
165 const glsl_type *type)
166 {
167 generate_unop(instructions, declarations, type, ir_unop_floor);
168 }
169
170 static void
171 generate_mod(exec_list *instructions,
172 ir_variable **declarations,
173 const glsl_type *type)
174 {
175 generate_binop(instructions, declarations, type, ir_binop_mod);
176 }
177
178 static void
179 generate_min(exec_list *instructions,
180 ir_variable **declarations,
181 const glsl_type *type)
182 {
183 generate_binop(instructions, declarations, type, ir_binop_min);
184 }
185
186 static void
187 generate_max(exec_list *instructions,
188 ir_variable **declarations,
189 const glsl_type *type)
190 {
191 generate_binop(instructions, declarations, type, ir_binop_max);
192 }
193
194
195 static void
196 generate_pow(exec_list *instructions,
197 ir_variable **declarations,
198 const glsl_type *type)
199 {
200 generate_binop(instructions, declarations, type, ir_binop_pow);
201 }
202
203 void
204 generate_function_instance(ir_function *f,
205 const char *name,
206 exec_list *instructions,
207 int n_args,
208 void (*generate)(exec_list *instructions,
209 ir_variable **declarations,
210 const glsl_type *type),
211 const glsl_type *ret_type,
212 const glsl_type *type)
213 {
214 ir_variable *declarations[17];
215
216 ir_function_signature *const sig = new ir_function_signature(type);
217 f->signatures.push_tail(sig);
218
219 ir_label *const label = new ir_label(name);
220 instructions->push_tail(label);
221 sig->definition = label;
222 static const char *arg_names[] = {
223 "arg0",
224 "arg1"
225 };
226 int i;
227
228 for (i = 0; i < n_args; i++) {
229 ir_variable *var = new ir_variable(type, arg_names[i]);
230
231 var = new ir_variable(type, arg_names[i]);
232 var->mode = ir_var_in;
233 sig->parameters.push_tail(var);
234
235 var = new ir_variable(type, arg_names[i]);
236 var->mode = ir_var_in;
237 instructions->push_tail(var);
238 declarations[i] = var;
239 }
240
241 ir_variable *retval = new ir_variable(ret_type, "__retval");
242 instructions->push_tail(retval);
243
244 declarations[16] = retval;
245
246 generate(instructions, declarations, type);
247 }
248
249 void
250 make_gentype_function(glsl_symbol_table *symtab, exec_list *instructions,
251 const char *name,
252 int n_args,
253 void (*generate)(exec_list *instructions,
254 ir_variable **declarations,
255 const glsl_type *type))
256 {
257 ir_function *const f = new ir_function(name);
258 const glsl_type *float_type = glsl_type::float_type;
259 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
260 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
261 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
262
263 bool added = symtab->add_function(name, f);
264 assert(added);
265
266 generate_function_instance(f, name, instructions, n_args, generate,
267 float_type, float_type);
268 generate_function_instance(f, name, instructions, n_args, generate,
269 vec2_type, vec2_type);
270 generate_function_instance(f, name, instructions, n_args, generate,
271 vec3_type, vec3_type);
272 generate_function_instance(f, name, instructions, n_args, generate,
273 vec4_type, vec4_type);
274 }
275
276 static void
277 generate_length(exec_list *instructions,
278 ir_variable **declarations,
279 const glsl_type *type)
280 {
281 ir_dereference *const retval = new ir_dereference(declarations[16]);
282 ir_dereference *const arg = new ir_dereference(declarations[0]);
283 ir_rvalue *result, *temp;
284
285 (void)type;
286
287 /* FINISHME: implement the abs(arg) variant for length(float f) */
288
289 temp = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg);
290 result = new ir_expression(ir_unop_sqrt, glsl_type::float_type, temp, NULL);
291
292 ir_instruction *inst = new ir_assignment(retval, result, NULL);
293 instructions->push_tail(inst);
294 }
295
296 void
297 generate_length_functions(glsl_symbol_table *symtab, exec_list *instructions)
298 {
299 const char *name = "length";
300 ir_function *const f = new ir_function(name);
301 const glsl_type *float_type = glsl_type::float_type;
302 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
303 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
304 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
305
306 bool added = symtab->add_function(name, f);
307 assert(added);
308
309 generate_function_instance(f, name, instructions, 1, generate_length,
310 float_type, float_type);
311 generate_function_instance(f, name, instructions, 1, generate_length,
312 float_type, vec2_type);
313 generate_function_instance(f, name, instructions, 1, generate_length,
314 float_type, vec3_type);
315 generate_function_instance(f, name, instructions, 1, generate_length,
316 float_type, vec4_type);
317 }
318
319 static void
320 generate_dot(exec_list *instructions,
321 ir_variable **declarations,
322 const glsl_type *type)
323 {
324 ir_dereference *const retval = new ir_dereference(declarations[16]);
325 ir_dereference *const arg = new ir_dereference(declarations[0]);
326 ir_rvalue *result;
327
328 (void)type;
329
330 result = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg);
331
332 ir_instruction *inst = new ir_assignment(retval, result, NULL);
333 instructions->push_tail(inst);
334 }
335
336 void
337 generate_dot_functions(glsl_symbol_table *symtab, exec_list *instructions)
338 {
339 const char *name = "dot";
340 ir_function *const f = new ir_function(name);
341 const glsl_type *float_type = glsl_type::float_type;
342 const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
343 const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
344 const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
345
346 bool added = symtab->add_function(name, f);
347 assert(added);
348
349 generate_function_instance(f, name, instructions, 1, generate_dot,
350 float_type, float_type);
351 generate_function_instance(f, name, instructions, 1, generate_dot,
352 float_type, vec2_type);
353 generate_function_instance(f, name, instructions, 1, generate_dot,
354 float_type, vec3_type);
355 generate_function_instance(f, name, instructions, 1, generate_dot,
356 float_type, vec4_type);
357 }
358
359 void
360 generate_110_functions(glsl_symbol_table *symtab, exec_list *instructions)
361 {
362 make_gentype_function(symtab, instructions, "radians", 1, generate_radians);
363 make_gentype_function(symtab, instructions, "degrees", 1, generate_degrees);
364 /* FINISHME: sin() */
365 /* FINISHME: cos() */
366 /* FINISHME: tan() */
367 /* FINISHME: asin() */
368 /* FINISHME: acos() */
369 /* FINISHME: atan(y,x) */
370 /* FINISHME: atan(y/x) */
371 make_gentype_function(symtab, instructions, "pow", 2, generate_pow);
372 make_gentype_function(symtab, instructions, "exp", 1, generate_exp);
373 make_gentype_function(symtab, instructions, "log", 1, generate_log);
374 make_gentype_function(symtab, instructions, "exp2", 1, generate_exp2);
375 make_gentype_function(symtab, instructions, "log2", 1, generate_log2);
376 make_gentype_function(symtab, instructions, "sqrt", 1, generate_sqrt);
377 make_gentype_function(symtab, instructions, "inversesqrt", 1, generate_rsq);
378 make_gentype_function(symtab, instructions, "abs", 1, generate_abs);
379 /* FINISHME: sign() */
380 make_gentype_function(symtab, instructions, "floor", 1, generate_floor);
381 make_gentype_function(symtab, instructions, "ceil", 1, generate_ceil);
382 /* FINISHME: fract() */
383 /* FINISHME: mod(x, float y) */
384 make_gentype_function(symtab, instructions, "mod", 2, generate_mod);
385 make_gentype_function(symtab, instructions, "min", 2, generate_min);
386 /* FINISHME: min(x, float y) */
387 make_gentype_function(symtab, instructions, "max", 2, generate_max);
388 /* FINISHME: max(x, float y) */
389 /* FINISHME: clamp() */
390 /* FINISHME: clamp() */
391 /* FINISHME: mix() */
392 /* FINISHME: mix() */
393 /* FINISHME: step() */
394 /* FINISHME: step() */
395 /* FINISHME: smoothstep() */
396 /* FINISHME: smoothstep() */
397 /* FINISHME: floor() */
398 /* FINISHME: step() */
399 generate_length_functions(symtab, instructions);
400 /* FINISHME: distance() */
401 generate_dot_functions(symtab, instructions);
402 /* FINISHME: cross() */
403 /* FINISHME: normalize() */
404 /* FINISHME: ftransform() */
405 /* FINISHME: faceforward() */
406 /* FINISHME: reflect() */
407 /* FINISHME: refract() */
408 /* FINISHME: matrixCompMult() */
409 /* FINISHME: lessThan() */
410 /* FINISHME: lessThanEqual() */
411 /* FINISHME: greaterThan() */
412 /* FINISHME: greaterThanEqual() */
413 /* FINISHME: equal() */
414 /* FINISHME: notEqual() */
415 /* FINISHME: any() */
416 /* FINISHME: all() */
417 /* FINISHME: not() */
418 /* FINISHME: texture*() */
419 /* FINISHME: shadow*() */
420 /* FINISHME: dFd[xy]() */
421 /* FINISHME: fwidth() */
422 }
423
424 void
425 _mesa_glsl_initialize_functions(exec_list *instructions,
426 struct _mesa_glsl_parse_state *state)
427 {
428 generate_110_functions(state->symbols, instructions);
429 }