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 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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_clamp(exec_list *instructions,
  166. ir_variable **declarations,
  167. const glsl_type *type)
  168. {
  169. ir_dereference *const x = new ir_dereference(declarations[0]);
  170. ir_dereference *const minval = new ir_dereference(declarations[1]);
  171. ir_dereference *const maxval = new ir_dereference(declarations[2]);
  172. ir_rvalue *result;
  173. result = new ir_expression(ir_binop_min, type, x, maxval);
  174. result = new ir_expression(ir_binop_max, type, result, minval);
  175. ir_instruction *inst = new ir_return(result);
  176. instructions->push_tail(inst);
  177. }
  178. static void
  179. generate_mix_vec(exec_list *instructions,
  180. ir_variable **declarations,
  181. const glsl_type *type)
  182. {
  183. ir_dereference *const x = new ir_dereference(declarations[0]);
  184. ir_dereference *const y = new ir_dereference(declarations[1]);
  185. ir_dereference *const a = new ir_dereference(declarations[2]);
  186. ir_rvalue *result, *temp;
  187. temp = new ir_expression(ir_binop_sub, type, new ir_constant(1.0f), a);
  188. result = new ir_expression(ir_binop_mul, type, x, temp);
  189. temp = new ir_expression(ir_binop_mul, type, y, a);
  190. result = new ir_expression(ir_binop_add, type, result, temp);
  191. ir_instruction *inst = new ir_return(result);
  192. instructions->push_tail(inst);
  193. }
  194. static void
  195. generate_normalize(exec_list *instructions,
  196. ir_variable **declarations,
  197. const glsl_type *type)
  198. {
  199. ir_dereference *const arg = new ir_dereference(declarations[0]);
  200. ir_rvalue *temp;
  201. ir_rvalue *result;
  202. temp = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg);
  203. temp = new ir_expression(ir_unop_rsq, glsl_type::float_type, temp, NULL);
  204. result = new ir_expression(ir_binop_mul, type, arg, temp);
  205. ir_instruction *inst = new ir_return(result);
  206. instructions->push_tail(inst);
  207. }
  208. static void
  209. generate_pow(exec_list *instructions,
  210. ir_variable **declarations,
  211. const glsl_type *type)
  212. {
  213. generate_binop(instructions, declarations, type, ir_binop_pow);
  214. }
  215. void
  216. generate_function_instance(ir_function *f,
  217. const char *name,
  218. exec_list *instructions,
  219. int n_args,
  220. void (*generate)(exec_list *instructions,
  221. ir_variable **declarations,
  222. const glsl_type *type),
  223. const glsl_type *ret_type,
  224. const glsl_type *type)
  225. {
  226. ir_variable *declarations[16];
  227. ir_function_signature *const sig = new ir_function_signature(ret_type);
  228. f->add_signature(sig);
  229. ir_label *const label = new ir_label(name, sig);
  230. instructions->push_tail(label);
  231. sig->definition = label;
  232. static const char *arg_names[] = {
  233. "arg0",
  234. "arg1",
  235. "arg2"
  236. };
  237. int i;
  238. for (i = 0; i < n_args; i++) {
  239. ir_variable *var = new ir_variable(type, arg_names[i]);
  240. var->mode = ir_var_in;
  241. sig->parameters.push_tail(var);
  242. declarations[i] = var;
  243. }
  244. generate(&sig->body, declarations, type);
  245. }
  246. void
  247. make_gentype_function(glsl_symbol_table *symtab, exec_list *instructions,
  248. const char *name,
  249. int n_args,
  250. void (*generate)(exec_list *instructions,
  251. ir_variable **declarations,
  252. const glsl_type *type))
  253. {
  254. ir_function *const f = new ir_function(name);
  255. const glsl_type *float_type = glsl_type::float_type;
  256. const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
  257. const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
  258. const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
  259. bool added = symtab->add_function(name, f);
  260. assert(added);
  261. generate_function_instance(f, name, instructions, n_args, generate,
  262. float_type, float_type);
  263. generate_function_instance(f, name, instructions, n_args, generate,
  264. vec2_type, vec2_type);
  265. generate_function_instance(f, name, instructions, n_args, generate,
  266. vec3_type, vec3_type);
  267. generate_function_instance(f, name, instructions, n_args, generate,
  268. vec4_type, vec4_type);
  269. }
  270. static void
  271. generate_vec_compare(exec_list *instructions,
  272. ir_variable **declarations,
  273. const glsl_type *type,
  274. enum ir_expression_operation op)
  275. {
  276. ir_dereference *const x = new ir_dereference(declarations[0]);
  277. ir_dereference *const y = new ir_dereference(declarations[1]);
  278. ir_variable *temp;
  279. const glsl_type *return_type;
  280. int i;
  281. return_type = glsl_type::get_instance(GLSL_TYPE_BOOL,
  282. type->vector_elements, 1);
  283. temp = new ir_variable(return_type, "temp");
  284. for (i = 0; i < type->vector_elements; i++) {
  285. ir_assignment *assign;
  286. ir_expression *compare;
  287. compare = new ir_expression(op,
  288. glsl_type::get_instance(type->base_type,
  289. 1, 1),
  290. new ir_swizzle(x, i, 0, 0, 0, 1),
  291. new ir_swizzle(y, i, 0, 0, 0, 1));
  292. assign = new ir_assignment(new ir_swizzle(new ir_dereference(temp),
  293. i, 0, 0, 0, 1),
  294. compare, NULL);
  295. instructions->push_tail(assign);
  296. }
  297. ir_instruction *inst = new ir_return(new ir_dereference(temp));
  298. instructions->push_tail(inst);
  299. }
  300. static void
  301. generate_lessThan(exec_list *instructions,
  302. ir_variable **declarations,
  303. const glsl_type *type)
  304. {
  305. generate_vec_compare(instructions, declarations, type, ir_binop_less);
  306. }
  307. static void
  308. generate_lessThanEqual(exec_list *instructions,
  309. ir_variable **declarations,
  310. const glsl_type *type)
  311. {
  312. generate_vec_compare(instructions, declarations, type, ir_binop_lequal);
  313. }
  314. static void
  315. generate_greaterThan(exec_list *instructions,
  316. ir_variable **declarations,
  317. const glsl_type *type)
  318. {
  319. generate_vec_compare(instructions, declarations, type, ir_binop_greater);
  320. }
  321. static void
  322. generate_greaterThanEqual(exec_list *instructions,
  323. ir_variable **declarations,
  324. const glsl_type *type)
  325. {
  326. generate_vec_compare(instructions, declarations, type, ir_binop_gequal);
  327. }
  328. static void
  329. generate_equal(exec_list *instructions,
  330. ir_variable **declarations,
  331. const glsl_type *type)
  332. {
  333. generate_vec_compare(instructions, declarations, type, ir_binop_equal);
  334. }
  335. static void
  336. generate_notEqual(exec_list *instructions,
  337. ir_variable **declarations,
  338. const glsl_type *type)
  339. {
  340. generate_vec_compare(instructions, declarations, type, ir_binop_nequal);
  341. }
  342. static void
  343. generate_vec_compare_function(glsl_symbol_table *symtab,
  344. exec_list *instructions,
  345. const char *name,
  346. void (*generate)(exec_list *instructions,
  347. ir_variable **declarations,
  348. const glsl_type *type),
  349. bool do_bool)
  350. {
  351. ir_function *const f = new ir_function(name);
  352. const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
  353. const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
  354. const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
  355. const glsl_type *ivec2_type = glsl_type::get_instance(GLSL_TYPE_INT, 2, 1);
  356. const glsl_type *ivec3_type = glsl_type::get_instance(GLSL_TYPE_INT, 3, 1);
  357. const glsl_type *ivec4_type = glsl_type::get_instance(GLSL_TYPE_INT, 4, 1);
  358. const glsl_type *uvec2_type = glsl_type::get_instance(GLSL_TYPE_UINT, 2, 1);
  359. const glsl_type *uvec3_type = glsl_type::get_instance(GLSL_TYPE_UINT, 3, 1);
  360. const glsl_type *uvec4_type = glsl_type::get_instance(GLSL_TYPE_UINT, 4, 1);
  361. const glsl_type *bvec2_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 2, 1);
  362. const glsl_type *bvec3_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 3, 1);
  363. const glsl_type *bvec4_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 4, 1);
  364. bool added = symtab->add_function(name, f);
  365. assert(added);
  366. generate_function_instance(f, name, instructions, 2, generate,
  367. bvec2_type, vec2_type);
  368. generate_function_instance(f, name, instructions, 2, generate,
  369. bvec3_type, vec3_type);
  370. generate_function_instance(f, name, instructions, 2, generate,
  371. bvec4_type, vec4_type);
  372. generate_function_instance(f, name, instructions, 2, generate,
  373. bvec2_type, ivec2_type);
  374. generate_function_instance(f, name, instructions, 2, generate,
  375. bvec3_type, ivec3_type);
  376. generate_function_instance(f, name, instructions, 2, generate,
  377. bvec4_type, ivec4_type);
  378. generate_function_instance(f, name, instructions, 2, generate,
  379. bvec2_type, uvec2_type);
  380. generate_function_instance(f, name, instructions, 2, generate,
  381. bvec3_type, uvec3_type);
  382. generate_function_instance(f, name, instructions, 2, generate,
  383. bvec4_type, uvec4_type);
  384. if (do_bool) {
  385. generate_function_instance(f, name, instructions, 2, generate,
  386. bvec2_type, bvec2_type);
  387. generate_function_instance(f, name, instructions, 2, generate,
  388. bvec3_type, bvec3_type);
  389. generate_function_instance(f, name, instructions, 2, generate,
  390. bvec4_type, bvec4_type);
  391. }
  392. }
  393. static void
  394. generate_length(exec_list *instructions,
  395. ir_variable **declarations,
  396. const glsl_type *type)
  397. {
  398. ir_dereference *const arg = new ir_dereference(declarations[0]);
  399. ir_rvalue *result, *temp;
  400. (void)type;
  401. /* FINISHME: implement the abs(arg) variant for length(float f) */
  402. temp = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg);
  403. result = new ir_expression(ir_unop_sqrt, glsl_type::float_type, temp, NULL);
  404. ir_instruction *inst = new ir_return(result);
  405. instructions->push_tail(inst);
  406. }
  407. void
  408. generate_length_functions(glsl_symbol_table *symtab, exec_list *instructions)
  409. {
  410. const char *name = "length";
  411. ir_function *const f = new ir_function(name);
  412. const glsl_type *float_type = glsl_type::float_type;
  413. const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
  414. const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
  415. const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
  416. bool added = symtab->add_function(name, f);
  417. assert(added);
  418. generate_function_instance(f, name, instructions, 1, generate_length,
  419. float_type, float_type);
  420. generate_function_instance(f, name, instructions, 1, generate_length,
  421. float_type, vec2_type);
  422. generate_function_instance(f, name, instructions, 1, generate_length,
  423. float_type, vec3_type);
  424. generate_function_instance(f, name, instructions, 1, generate_length,
  425. float_type, vec4_type);
  426. }
  427. static void
  428. generate_dot(exec_list *instructions,
  429. ir_variable **declarations,
  430. const glsl_type *type)
  431. {
  432. ir_dereference *const arg0 = new ir_dereference(declarations[0]);
  433. ir_dereference *const arg1 = new ir_dereference(declarations[1]);
  434. ir_rvalue *result;
  435. (void)type;
  436. result = new ir_expression(ir_binop_dot, glsl_type::float_type, arg0, arg1);
  437. ir_instruction *inst = new ir_return(result);
  438. instructions->push_tail(inst);
  439. }
  440. void
  441. generate_dot_functions(glsl_symbol_table *symtab, exec_list *instructions)
  442. {
  443. const char *name = "dot";
  444. ir_function *const f = new ir_function(name);
  445. const glsl_type *float_type = glsl_type::float_type;
  446. const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
  447. const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
  448. const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
  449. bool added = symtab->add_function(name, f);
  450. assert(added);
  451. generate_function_instance(f, name, instructions, 2, generate_dot,
  452. float_type, float_type);
  453. generate_function_instance(f, name, instructions, 2, generate_dot,
  454. float_type, vec2_type);
  455. generate_function_instance(f, name, instructions, 2, generate_dot,
  456. float_type, vec3_type);
  457. generate_function_instance(f, name, instructions, 2, generate_dot,
  458. float_type, vec4_type);
  459. }
  460. static void
  461. generate_any_bvec2(exec_list *instructions,
  462. ir_variable **declarations,
  463. const glsl_type *type)
  464. {
  465. ir_dereference *const arg0 = new ir_dereference(declarations[0]);
  466. ir_rvalue *result;
  467. (void)type;
  468. result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type,
  469. new ir_swizzle(arg0, 0, 0, 0, 0, 1),
  470. new ir_swizzle(arg0, 1, 0, 0, 0, 1));
  471. ir_instruction *inst = new ir_return(result);
  472. instructions->push_tail(inst);
  473. }
  474. static void
  475. generate_any_bvec3(exec_list *instructions,
  476. ir_variable **declarations,
  477. const glsl_type *type)
  478. {
  479. ir_dereference *const arg0 = new ir_dereference(declarations[0]);
  480. ir_rvalue *result;
  481. (void)type;
  482. result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type,
  483. new ir_swizzle(arg0, 0, 0, 0, 0, 1),
  484. new ir_swizzle(arg0, 1, 0, 0, 0, 1));
  485. result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type,
  486. result,
  487. new ir_swizzle(arg0, 2, 0, 0, 0, 1));
  488. ir_instruction *inst = new ir_return(result);
  489. instructions->push_tail(inst);
  490. }
  491. static void
  492. generate_any_bvec4(exec_list *instructions,
  493. ir_variable **declarations,
  494. const glsl_type *type)
  495. {
  496. ir_dereference *const arg0 = new ir_dereference(declarations[0]);
  497. ir_rvalue *result;
  498. (void)type;
  499. result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type,
  500. new ir_swizzle(arg0, 0, 0, 0, 0, 1),
  501. new ir_swizzle(arg0, 1, 0, 0, 0, 1));
  502. result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type,
  503. result,
  504. new ir_swizzle(arg0, 2, 0, 0, 0, 1));
  505. result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type,
  506. result,
  507. new ir_swizzle(arg0, 3, 0, 0, 0, 1));
  508. ir_instruction *inst = new ir_return(result);
  509. instructions->push_tail(inst);
  510. }
  511. static void
  512. generate_all_bvec2(exec_list *instructions,
  513. ir_variable **declarations,
  514. const glsl_type *type)
  515. {
  516. ir_dereference *const arg0 = new ir_dereference(declarations[0]);
  517. ir_rvalue *result;
  518. (void)type;
  519. result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type,
  520. new ir_swizzle(arg0, 0, 0, 0, 0, 1),
  521. new ir_swizzle(arg0, 1, 0, 0, 0, 1));
  522. ir_instruction *inst = new ir_return(result);
  523. instructions->push_tail(inst);
  524. }
  525. static void
  526. generate_all_bvec3(exec_list *instructions,
  527. ir_variable **declarations,
  528. const glsl_type *type)
  529. {
  530. ir_dereference *const arg0 = new ir_dereference(declarations[0]);
  531. ir_rvalue *result;
  532. (void)type;
  533. result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type,
  534. new ir_swizzle(arg0, 0, 0, 0, 0, 1),
  535. new ir_swizzle(arg0, 1, 0, 0, 0, 1));
  536. result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type,
  537. result,
  538. new ir_swizzle(arg0, 2, 0, 0, 0, 1));
  539. ir_instruction *inst = new ir_return(result);
  540. instructions->push_tail(inst);
  541. }
  542. static void
  543. generate_all_bvec4(exec_list *instructions,
  544. ir_variable **declarations,
  545. const glsl_type *type)
  546. {
  547. ir_dereference *const arg0 = new ir_dereference(declarations[0]);
  548. ir_rvalue *result;
  549. (void)type;
  550. result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type,
  551. new ir_swizzle(arg0, 0, 0, 0, 0, 1),
  552. new ir_swizzle(arg0, 1, 0, 0, 0, 1));
  553. result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type,
  554. result,
  555. new ir_swizzle(arg0, 2, 0, 0, 0, 1));
  556. result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type,
  557. result,
  558. new ir_swizzle(arg0, 3, 0, 0, 0, 1));
  559. ir_instruction *inst = new ir_return(result);
  560. instructions->push_tail(inst);
  561. }
  562. static void
  563. generate_not(exec_list *instructions,
  564. ir_variable **declarations,
  565. const glsl_type *type)
  566. {
  567. ir_dereference *const arg0 = new ir_dereference(declarations[0]);
  568. ir_rvalue *result;
  569. result = new ir_expression(ir_unop_logic_not, type, arg0, NULL);
  570. ir_instruction *inst = new ir_return(result);
  571. instructions->push_tail(inst);
  572. }
  573. void
  574. generate_any_functions(glsl_symbol_table *symtab, exec_list *instructions)
  575. {
  576. const char *name = "any";
  577. ir_function *const f = new ir_function(name);
  578. const glsl_type *bvec2_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 2, 1);
  579. const glsl_type *bvec3_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 3, 1);
  580. const glsl_type *bvec4_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 4, 1);
  581. bool added = symtab->add_function(name, f);
  582. assert(added);
  583. generate_function_instance(f, name, instructions, 1, generate_any_bvec2,
  584. glsl_type::bool_type, bvec2_type);
  585. generate_function_instance(f, name, instructions, 1, generate_any_bvec3,
  586. glsl_type::bool_type, bvec3_type);
  587. generate_function_instance(f, name, instructions, 1, generate_any_bvec4,
  588. glsl_type::bool_type, bvec4_type);
  589. }
  590. void
  591. generate_all_functions(glsl_symbol_table *symtab, exec_list *instructions)
  592. {
  593. const char *name = "all";
  594. ir_function *const f = new ir_function(name);
  595. const glsl_type *bvec2_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 2, 1);
  596. const glsl_type *bvec3_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 3, 1);
  597. const glsl_type *bvec4_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 4, 1);
  598. bool added = symtab->add_function(name, f);
  599. assert(added);
  600. generate_function_instance(f, name, instructions, 1, generate_all_bvec2,
  601. glsl_type::bool_type, bvec2_type);
  602. generate_function_instance(f, name, instructions, 1, generate_all_bvec3,
  603. glsl_type::bool_type, bvec3_type);
  604. generate_function_instance(f, name, instructions, 1, generate_all_bvec4,
  605. glsl_type::bool_type, bvec4_type);
  606. }
  607. void
  608. generate_not_functions(glsl_symbol_table *symtab, exec_list *instructions)
  609. {
  610. const char *name = "not";
  611. ir_function *const f = new ir_function(name);
  612. const glsl_type *bvec2_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 2, 1);
  613. const glsl_type *bvec3_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 3, 1);
  614. const glsl_type *bvec4_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 4, 1);
  615. bool added = symtab->add_function(name, f);
  616. assert(added);
  617. generate_function_instance(f, name, instructions, 1, generate_not,
  618. bvec2_type, bvec2_type);
  619. generate_function_instance(f, name, instructions, 1, generate_not,
  620. bvec3_type, bvec3_type);
  621. generate_function_instance(f, name, instructions, 1, generate_not,
  622. bvec4_type, bvec4_type);
  623. }
  624. void
  625. generate_110_functions(glsl_symbol_table *symtab, exec_list *instructions)
  626. {
  627. make_gentype_function(symtab, instructions, "radians", 1, generate_radians);
  628. make_gentype_function(symtab, instructions, "degrees", 1, generate_degrees);
  629. /* FINISHME: sin() */
  630. /* FINISHME: cos() */
  631. /* FINISHME: tan() */
  632. /* FINISHME: asin() */
  633. /* FINISHME: acos() */
  634. /* FINISHME: atan(y,x) */
  635. /* FINISHME: atan(y/x) */
  636. make_gentype_function(symtab, instructions, "pow", 2, generate_pow);
  637. make_gentype_function(symtab, instructions, "exp", 1, generate_exp);
  638. make_gentype_function(symtab, instructions, "log", 1, generate_log);
  639. make_gentype_function(symtab, instructions, "exp2", 1, generate_exp2);
  640. make_gentype_function(symtab, instructions, "log2", 1, generate_log2);
  641. make_gentype_function(symtab, instructions, "sqrt", 1, generate_sqrt);
  642. make_gentype_function(symtab, instructions, "inversesqrt", 1, generate_rsq);
  643. make_gentype_function(symtab, instructions, "abs", 1, generate_abs);
  644. /* FINISHME: sign() */
  645. make_gentype_function(symtab, instructions, "floor", 1, generate_floor);
  646. make_gentype_function(symtab, instructions, "ceil", 1, generate_ceil);
  647. /* FINISHME: fract() */
  648. /* FINISHME: mod(x, float y) */
  649. make_gentype_function(symtab, instructions, "mod", 2, generate_mod);
  650. make_gentype_function(symtab, instructions, "min", 2, generate_min);
  651. /* FINISHME: min(x, float y) */
  652. make_gentype_function(symtab, instructions, "max", 2, generate_max);
  653. /* FINISHME: max(x, float y) */
  654. make_gentype_function(symtab, instructions, "clamp", 3, generate_clamp);
  655. /* FINISHME: clamp() */
  656. make_gentype_function(symtab, instructions, "mix", 3, generate_mix_vec);
  657. /* FINISHME: mix() */
  658. /* FINISHME: step() */
  659. /* FINISHME: step() */
  660. /* FINISHME: smoothstep() */
  661. /* FINISHME: smoothstep() */
  662. /* FINISHME: floor() */
  663. /* FINISHME: step() */
  664. generate_length_functions(symtab, instructions);
  665. /* FINISHME: distance() */
  666. generate_dot_functions(symtab, instructions);
  667. /* FINISHME: cross() */
  668. make_gentype_function(symtab, instructions, "normalize", 1,
  669. generate_normalize);
  670. /* FINISHME: normalize() */
  671. /* FINISHME: ftransform() */
  672. /* FINISHME: faceforward() */
  673. /* FINISHME: reflect() */
  674. /* FINISHME: refract() */
  675. /* FINISHME: matrixCompMult() */
  676. generate_vec_compare_function(symtab, instructions,
  677. "lessThan", generate_lessThan, false);
  678. generate_vec_compare_function(symtab, instructions,
  679. "lessThanEqual", generate_lessThanEqual,
  680. false);
  681. generate_vec_compare_function(symtab, instructions,
  682. "greaterThan", generate_greaterThan, false);
  683. generate_vec_compare_function(symtab, instructions,
  684. "greaterThanEqual", generate_greaterThanEqual,
  685. false);
  686. generate_vec_compare_function(symtab, instructions,
  687. "equal", generate_equal, false);
  688. generate_vec_compare_function(symtab, instructions,
  689. "notEqual", generate_notEqual, false);
  690. generate_any_functions(symtab, instructions);
  691. generate_all_functions(symtab, instructions);
  692. generate_not_functions(symtab, instructions);
  693. /* FINISHME: texture*() */
  694. /* FINISHME: shadow*() */
  695. /* FINISHME: dFd[xy]() */
  696. /* FINISHME: fwidth() */
  697. }
  698. void
  699. _mesa_glsl_initialize_functions(exec_list *instructions,
  700. struct _mesa_glsl_parse_state *state)
  701. {
  702. generate_110_functions(state->symbols, instructions);
  703. }