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.
25 #include "glsl_symbol_table.h"
26 #include "glsl_parser_extras.h"
27 #include "glsl_types.h"
31 generate_unop(exec_list
*instructions
,
32 ir_variable
**declarations
,
33 const glsl_type
*type
,
34 enum ir_expression_operation op
)
36 ir_dereference
*const retval
= new ir_dereference(declarations
[16]);
37 ir_dereference
*const arg
= new ir_dereference(declarations
[0]);
40 result
= new ir_expression(op
, type
, arg
, NULL
);
42 ir_instruction
*inst
= new ir_assignment(retval
, result
, NULL
);
43 instructions
->push_tail(inst
);
47 generate_binop(exec_list
*instructions
,
48 ir_variable
**declarations
,
49 const glsl_type
*type
,
50 enum ir_expression_operation op
)
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]);
57 result
= new ir_expression(op
, type
, arg1
, arg2
);
59 ir_instruction
*inst
= new ir_assignment(retval
, result
, NULL
);
60 instructions
->push_tail(inst
);
64 generate_exp(exec_list
*instructions
,
65 ir_variable
**declarations
,
66 const glsl_type
*type
)
68 generate_unop(instructions
, declarations
, type
, ir_unop_exp
);
72 generate_log(exec_list
*instructions
,
73 ir_variable
**declarations
,
74 const glsl_type
*type
)
76 generate_unop(instructions
, declarations
, type
, ir_unop_log
);
80 generate_exp2(exec_list
*instructions
,
81 ir_variable
**declarations
,
82 const glsl_type
*type
)
84 generate_unop(instructions
, declarations
, type
, ir_unop_exp2
);
88 generate_log2(exec_list
*instructions
,
89 ir_variable
**declarations
,
90 const glsl_type
*type
)
92 generate_unop(instructions
, declarations
, type
, ir_unop_log2
);
96 generate_rsq(exec_list
*instructions
,
97 ir_variable
**declarations
,
98 const glsl_type
*type
)
100 generate_unop(instructions
, declarations
, type
, ir_unop_rsq
);
104 generate_sqrt(exec_list
*instructions
,
105 ir_variable
**declarations
,
106 const glsl_type
*type
)
108 generate_unop(instructions
, declarations
, type
, ir_unop_sqrt
);
112 generate_abs(exec_list
*instructions
,
113 ir_variable
**declarations
,
114 const glsl_type
*type
)
116 generate_unop(instructions
, declarations
, type
, ir_unop_abs
);
120 generate_ceil(exec_list
*instructions
,
121 ir_variable
**declarations
,
122 const glsl_type
*type
)
124 generate_unop(instructions
, declarations
, type
, ir_unop_ceil
);
128 generate_floor(exec_list
*instructions
,
129 ir_variable
**declarations
,
130 const glsl_type
*type
)
132 generate_unop(instructions
, declarations
, type
, ir_unop_floor
);
136 generate_mod(exec_list
*instructions
,
137 ir_variable
**declarations
,
138 const glsl_type
*type
)
140 generate_binop(instructions
, declarations
, type
, ir_binop_mod
);
144 generate_min(exec_list
*instructions
,
145 ir_variable
**declarations
,
146 const glsl_type
*type
)
148 generate_binop(instructions
, declarations
, type
, ir_binop_min
);
152 generate_max(exec_list
*instructions
,
153 ir_variable
**declarations
,
154 const glsl_type
*type
)
156 generate_binop(instructions
, declarations
, type
, ir_binop_max
);
161 generate_pow(exec_list
*instructions
,
162 ir_variable
**declarations
,
163 const glsl_type
*type
)
165 generate_binop(instructions
, declarations
, type
, ir_binop_pow
);
169 generate_function_instance(ir_function
*f
,
171 exec_list
*instructions
,
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
)
179 ir_variable
*declarations
[17];
181 ir_function_signature
*const sig
= new ir_function_signature(type
);
182 f
->signatures
.push_tail(sig
);
184 ir_label
*const label
= new ir_label(name
);
185 instructions
->push_tail(label
);
186 sig
->definition
= label
;
187 static const char *arg_names
[] = {
193 for (i
= 0; i
< n_args
; i
++) {
194 ir_variable
*var
= new ir_variable(type
, arg_names
[i
]);
196 var
->mode
= ir_var_in
;
197 sig
->parameters
.push_tail(var
);
199 var
= new ir_variable(type
, arg_names
[i
]);
201 declarations
[i
] = var
;
204 ir_variable
*retval
= new ir_variable(ret_type
, "__retval");
205 instructions
->push_tail(retval
);
207 declarations
[16] = retval
;
209 generate(instructions
, declarations
, type
);
213 make_gentype_function(glsl_symbol_table
*symtab
, exec_list
*instructions
,
216 void (*generate
)(exec_list
*instructions
,
217 ir_variable
**declarations
,
218 const glsl_type
*type
))
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);
226 bool added
= symtab
->add_function(name
, f
);
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
);
240 generate_length(exec_list
*instructions
,
241 ir_variable
**declarations
,
242 const glsl_type
*type
)
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
;
250 /* FINISHME: implement the abs(arg) variant for length(float f) */
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
);
255 ir_instruction
*inst
= new ir_assignment(retval
, result
, NULL
);
256 instructions
->push_tail(inst
);
260 generate_length_functions(glsl_symbol_table
*symtab
, exec_list
*instructions
)
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);
269 bool added
= symtab
->add_function(name
, f
);
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
);
283 generate_dot(exec_list
*instructions
,
284 ir_variable
**declarations
,
285 const glsl_type
*type
)
287 ir_dereference
*const retval
= new ir_dereference(declarations
[16]);
288 ir_dereference
*const arg
= new ir_dereference(declarations
[0]);
293 result
= new ir_expression(ir_binop_dot
, glsl_type::float_type
, arg
, arg
);
295 ir_instruction
*inst
= new ir_assignment(retval
, result
, NULL
);
296 instructions
->push_tail(inst
);
300 generate_dot_functions(glsl_symbol_table
*symtab
, exec_list
*instructions
)
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);
309 bool added
= symtab
->add_function(name
, f
);
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
);
323 generate_110_functions(glsl_symbol_table
*symtab
, exec_list
*instructions
)
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() */
388 _mesa_glsl_initialize_functions(exec_list
*instructions
,
389 struct _mesa_glsl_parse_state
*state
)
391 generate_110_functions(state
->symbols
, instructions
);