2 * Copyright © 2010 Intel Corporation
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:
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
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.
26 #include "glsl_symbol_table.h"
27 #include "glsl_parser_extras.h"
28 #include "glsl_types.h"
32 generate_unop(exec_list
*instructions
,
33 ir_variable
**declarations
,
34 const glsl_type
*type
,
35 enum ir_expression_operation op
)
37 ir_dereference
*const retval
= new ir_dereference(declarations
[16]);
38 ir_dereference
*const arg
= new ir_dereference(declarations
[0]);
41 result
= new ir_expression(op
, type
, arg
, NULL
);
43 ir_instruction
*inst
= new ir_assignment(retval
, result
, NULL
);
44 instructions
->push_tail(inst
);
48 generate_binop(exec_list
*instructions
,
49 ir_variable
**declarations
,
50 const glsl_type
*type
,
51 enum ir_expression_operation op
)
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]);
58 result
= new ir_expression(op
, type
, arg1
, arg2
);
60 ir_instruction
*inst
= new ir_assignment(retval
, result
, NULL
);
61 instructions
->push_tail(inst
);
65 generate_radians(exec_list
*instructions
,
66 ir_variable
**declarations
,
67 const glsl_type
*type
)
69 ir_dereference
*const retval
= new ir_dereference(declarations
[16]);
70 ir_dereference
*const arg
= new ir_dereference(declarations
[0]);
73 result
= new ir_expression(ir_binop_mul
, type
,
75 new ir_constant((float)(M_PI
/ 180.0)));
77 ir_instruction
*inst
= new ir_assignment(retval
, result
, NULL
);
78 instructions
->push_tail(inst
);
82 generate_degrees(exec_list
*instructions
,
83 ir_variable
**declarations
,
84 const glsl_type
*type
)
86 ir_dereference
*const retval
= new ir_dereference(declarations
[16]);
87 ir_dereference
*const arg
= new ir_dereference(declarations
[0]);
90 result
= new ir_expression(ir_binop_mul
, type
,
92 new ir_constant((float)(180.0 / M_PI
)));
94 ir_instruction
*inst
= new ir_assignment(retval
, result
, NULL
);
95 instructions
->push_tail(inst
);
99 generate_exp(exec_list
*instructions
,
100 ir_variable
**declarations
,
101 const glsl_type
*type
)
103 generate_unop(instructions
, declarations
, type
, ir_unop_exp
);
107 generate_log(exec_list
*instructions
,
108 ir_variable
**declarations
,
109 const glsl_type
*type
)
111 generate_unop(instructions
, declarations
, type
, ir_unop_log
);
115 generate_exp2(exec_list
*instructions
,
116 ir_variable
**declarations
,
117 const glsl_type
*type
)
119 generate_unop(instructions
, declarations
, type
, ir_unop_exp2
);
123 generate_log2(exec_list
*instructions
,
124 ir_variable
**declarations
,
125 const glsl_type
*type
)
127 generate_unop(instructions
, declarations
, type
, ir_unop_log2
);
131 generate_rsq(exec_list
*instructions
,
132 ir_variable
**declarations
,
133 const glsl_type
*type
)
135 generate_unop(instructions
, declarations
, type
, ir_unop_rsq
);
139 generate_sqrt(exec_list
*instructions
,
140 ir_variable
**declarations
,
141 const glsl_type
*type
)
143 generate_unop(instructions
, declarations
, type
, ir_unop_sqrt
);
147 generate_abs(exec_list
*instructions
,
148 ir_variable
**declarations
,
149 const glsl_type
*type
)
151 generate_unop(instructions
, declarations
, type
, ir_unop_abs
);
155 generate_ceil(exec_list
*instructions
,
156 ir_variable
**declarations
,
157 const glsl_type
*type
)
159 generate_unop(instructions
, declarations
, type
, ir_unop_ceil
);
163 generate_floor(exec_list
*instructions
,
164 ir_variable
**declarations
,
165 const glsl_type
*type
)
167 generate_unop(instructions
, declarations
, type
, ir_unop_floor
);
171 generate_mod(exec_list
*instructions
,
172 ir_variable
**declarations
,
173 const glsl_type
*type
)
175 generate_binop(instructions
, declarations
, type
, ir_binop_mod
);
179 generate_min(exec_list
*instructions
,
180 ir_variable
**declarations
,
181 const glsl_type
*type
)
183 generate_binop(instructions
, declarations
, type
, ir_binop_min
);
187 generate_max(exec_list
*instructions
,
188 ir_variable
**declarations
,
189 const glsl_type
*type
)
191 generate_binop(instructions
, declarations
, type
, ir_binop_max
);
196 generate_pow(exec_list
*instructions
,
197 ir_variable
**declarations
,
198 const glsl_type
*type
)
200 generate_binop(instructions
, declarations
, type
, ir_binop_pow
);
204 generate_function_instance(ir_function
*f
,
206 exec_list
*instructions
,
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
)
214 ir_variable
*declarations
[17];
216 ir_function_signature
*const sig
= new ir_function_signature(type
);
217 f
->signatures
.push_tail(sig
);
219 ir_label
*const label
= new ir_label(name
);
220 instructions
->push_tail(label
);
221 sig
->definition
= label
;
222 static const char *arg_names
[] = {
228 for (i
= 0; i
< n_args
; i
++) {
229 ir_variable
*var
= new ir_variable(type
, arg_names
[i
]);
231 var
= new ir_variable(type
, arg_names
[i
]);
232 var
->mode
= ir_var_in
;
233 sig
->parameters
.push_tail(var
);
235 var
= new ir_variable(type
, arg_names
[i
]);
236 var
->mode
= ir_var_in
;
237 instructions
->push_tail(var
);
238 declarations
[i
] = var
;
241 ir_variable
*retval
= new ir_variable(ret_type
, "__retval");
242 instructions
->push_tail(retval
);
244 declarations
[16] = retval
;
246 generate(instructions
, declarations
, type
);
250 make_gentype_function(glsl_symbol_table
*symtab
, exec_list
*instructions
,
253 void (*generate
)(exec_list
*instructions
,
254 ir_variable
**declarations
,
255 const glsl_type
*type
))
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);
263 bool added
= symtab
->add_function(name
, f
);
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
);
277 generate_length(exec_list
*instructions
,
278 ir_variable
**declarations
,
279 const glsl_type
*type
)
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
;
287 /* FINISHME: implement the abs(arg) variant for length(float f) */
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
);
292 ir_instruction
*inst
= new ir_assignment(retval
, result
, NULL
);
293 instructions
->push_tail(inst
);
297 generate_length_functions(glsl_symbol_table
*symtab
, exec_list
*instructions
)
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);
306 bool added
= symtab
->add_function(name
, f
);
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
);
320 generate_dot(exec_list
*instructions
,
321 ir_variable
**declarations
,
322 const glsl_type
*type
)
324 ir_dereference
*const retval
= new ir_dereference(declarations
[16]);
325 ir_dereference
*const arg
= new ir_dereference(declarations
[0]);
330 result
= new ir_expression(ir_binop_dot
, glsl_type::float_type
, arg
, arg
);
332 ir_instruction
*inst
= new ir_assignment(retval
, result
, NULL
);
333 instructions
->push_tail(inst
);
337 generate_dot_functions(glsl_symbol_table
*symtab
, exec_list
*instructions
)
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);
346 bool added
= symtab
->add_function(name
, f
);
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
);
360 generate_110_functions(glsl_symbol_table
*symtab
, exec_list
*instructions
)
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() */
425 _mesa_glsl_initialize_functions(exec_list
*instructions
,
426 struct _mesa_glsl_parse_state
*state
)
428 generate_110_functions(state
->symbols
, instructions
);