Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

builtin_function.cpp 13KB

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