Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ir_constant_expression.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. * \file ir_constant_expression.cpp
  25. * Evaluate and process constant valued expressions
  26. *
  27. * In GLSL, constant valued expressions are used in several places. These
  28. * must be processed and evaluated very early in the compilation process.
  29. *
  30. * * Sizes of arrays
  31. * * Initializers for uniforms
  32. * * Initializers for \c const variables
  33. */
  34. #define NULL 0
  35. #include <math.h>
  36. #include "ir.h"
  37. #include "ir_visitor.h"
  38. #include "glsl_types.h"
  39. /**
  40. * Visitor class for evaluating constant expressions
  41. */
  42. class ir_constant_visitor : public ir_visitor {
  43. public:
  44. ir_constant_visitor()
  45. : value(NULL)
  46. {
  47. /* empty */
  48. }
  49. virtual ~ir_constant_visitor()
  50. {
  51. /* empty */
  52. }
  53. /**
  54. * \name Visit methods
  55. *
  56. * As typical for the visitor pattern, there must be one \c visit method for
  57. * each concrete subclass of \c ir_instruction. Virtual base classes within
  58. * the hierarchy should not have \c visit methods.
  59. */
  60. /*@{*/
  61. virtual void visit(ir_variable *);
  62. virtual void visit(ir_function_signature *);
  63. virtual void visit(ir_function *);
  64. virtual void visit(ir_expression *);
  65. virtual void visit(ir_texture *);
  66. virtual void visit(ir_swizzle *);
  67. virtual void visit(ir_dereference_variable *);
  68. virtual void visit(ir_dereference_array *);
  69. virtual void visit(ir_dereference_record *);
  70. virtual void visit(ir_assignment *);
  71. virtual void visit(ir_constant *);
  72. virtual void visit(ir_call *);
  73. virtual void visit(ir_return *);
  74. virtual void visit(ir_if *);
  75. virtual void visit(ir_loop *);
  76. virtual void visit(ir_loop_jump *);
  77. /*@}*/
  78. /**
  79. * Value of the constant expression.
  80. *
  81. * \note
  82. * This field will be \c NULL if the expression is not constant valued.
  83. */
  84. /* FINIHSME: This cannot hold values for constant arrays or structures. */
  85. ir_constant *value;
  86. };
  87. ir_constant *
  88. ir_instruction::constant_expression_value()
  89. {
  90. ir_constant_visitor visitor;
  91. this->accept(& visitor);
  92. return visitor.value;
  93. }
  94. void
  95. ir_constant_visitor::visit(ir_variable *ir)
  96. {
  97. (void) ir;
  98. value = NULL;
  99. }
  100. void
  101. ir_constant_visitor::visit(ir_function_signature *ir)
  102. {
  103. (void) ir;
  104. value = NULL;
  105. }
  106. void
  107. ir_constant_visitor::visit(ir_function *ir)
  108. {
  109. (void) ir;
  110. value = NULL;
  111. }
  112. void
  113. ir_constant_visitor::visit(ir_expression *ir)
  114. {
  115. value = NULL;
  116. ir_constant *op[2];
  117. unsigned int operand, c;
  118. unsigned u[16];
  119. int i[16];
  120. float f[16];
  121. bool b[16];
  122. const glsl_type *type = NULL;
  123. for (operand = 0; operand < ir->get_num_operands(); operand++) {
  124. op[operand] = ir->operands[operand]->constant_expression_value();
  125. if (!op[operand])
  126. return;
  127. }
  128. switch (ir->operation) {
  129. case ir_unop_logic_not:
  130. type = ir->operands[0]->type;
  131. assert(type->base_type == GLSL_TYPE_BOOL);
  132. for (c = 0; c < ir->operands[0]->type->components(); c++)
  133. b[c] = !op[0]->value.b[c];
  134. break;
  135. case ir_unop_f2i:
  136. assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  137. type = ir->type;
  138. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  139. i[c] = op[0]->value.f[c];
  140. }
  141. break;
  142. case ir_unop_i2f:
  143. assert(op[0]->type->base_type == GLSL_TYPE_UINT ||
  144. op[0]->type->base_type == GLSL_TYPE_INT);
  145. type = ir->type;
  146. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  147. if (op[0]->type->base_type == GLSL_TYPE_INT)
  148. f[c] = op[0]->value.i[c];
  149. else
  150. f[c] = op[0]->value.u[c];
  151. }
  152. break;
  153. case ir_unop_b2f:
  154. assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
  155. type = ir->type;
  156. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  157. f[c] = op[0]->value.b[c] ? 1.0 : 0.0;
  158. }
  159. break;
  160. case ir_unop_f2b:
  161. assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  162. type = ir->type;
  163. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  164. b[c] = bool(op[0]->value.f[c]);
  165. }
  166. break;
  167. case ir_unop_neg:
  168. type = ir->type;
  169. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  170. switch (type->base_type) {
  171. case GLSL_TYPE_UINT:
  172. u[c] = -op[0]->value.u[c];
  173. break;
  174. case GLSL_TYPE_INT:
  175. i[c] = -op[0]->value.i[c];
  176. break;
  177. case GLSL_TYPE_FLOAT:
  178. f[c] = -op[0]->value.f[c];
  179. break;
  180. default:
  181. assert(0);
  182. }
  183. }
  184. break;
  185. case ir_unop_abs:
  186. assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  187. type = ir->type;
  188. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  189. switch (type->base_type) {
  190. case GLSL_TYPE_UINT:
  191. u[c] = op[0]->value.u[c];
  192. break;
  193. case GLSL_TYPE_INT:
  194. i[c] = op[0]->value.i[c];
  195. if (i[c] < 0)
  196. i[c] = -i[c];
  197. break;
  198. case GLSL_TYPE_FLOAT:
  199. f[c] = fabs(op[0]->value.f[c]);
  200. break;
  201. default:
  202. assert(0);
  203. }
  204. }
  205. break;
  206. case ir_unop_rcp:
  207. assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  208. type = ir->type;
  209. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  210. switch (type->base_type) {
  211. case GLSL_TYPE_UINT:
  212. if (op[0]->value.u[c] != 0.0)
  213. u[c] = 1 / op[0]->value.u[c];
  214. break;
  215. case GLSL_TYPE_INT:
  216. if (op[0]->value.i[c] != 0.0)
  217. i[c] = 1 / op[0]->value.i[c];
  218. break;
  219. case GLSL_TYPE_FLOAT:
  220. if (op[0]->value.f[c] != 0.0)
  221. f[c] = 1.0 / op[0]->value.f[c];
  222. break;
  223. default:
  224. assert(0);
  225. }
  226. }
  227. break;
  228. case ir_unop_rsq:
  229. assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  230. type = ir->type;
  231. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  232. f[c] = 1.0 / sqrtf(op[0]->value.f[c]);
  233. }
  234. break;
  235. case ir_unop_sqrt:
  236. assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  237. type = ir->type;
  238. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  239. f[c] = sqrtf(op[0]->value.f[c]);
  240. }
  241. break;
  242. case ir_unop_exp:
  243. assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  244. type = ir->type;
  245. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  246. f[c] = expf(op[0]->value.f[c]);
  247. }
  248. break;
  249. case ir_unop_log:
  250. assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  251. type = ir->type;
  252. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  253. f[c] = logf(op[0]->value.f[c]);
  254. }
  255. break;
  256. case ir_unop_dFdx:
  257. case ir_unop_dFdy:
  258. assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  259. type = ir->type;
  260. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  261. f[c] = 0.0;
  262. }
  263. break;
  264. case ir_binop_add:
  265. if (ir->operands[0]->type == ir->operands[1]->type) {
  266. type = ir->operands[0]->type;
  267. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  268. switch (ir->operands[0]->type->base_type) {
  269. case GLSL_TYPE_UINT:
  270. u[c] = op[0]->value.u[c] + op[1]->value.u[c];
  271. break;
  272. case GLSL_TYPE_INT:
  273. i[c] = op[0]->value.i[c] + op[1]->value.i[c];
  274. break;
  275. case GLSL_TYPE_FLOAT:
  276. f[c] = op[0]->value.f[c] + op[1]->value.f[c];
  277. break;
  278. default:
  279. assert(0);
  280. }
  281. }
  282. }
  283. break;
  284. case ir_binop_sub:
  285. if (ir->operands[0]->type == ir->operands[1]->type) {
  286. type = ir->operands[0]->type;
  287. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  288. switch (ir->operands[0]->type->base_type) {
  289. case GLSL_TYPE_UINT:
  290. u[c] = op[0]->value.u[c] - op[1]->value.u[c];
  291. break;
  292. case GLSL_TYPE_INT:
  293. i[c] = op[0]->value.i[c] - op[1]->value.i[c];
  294. break;
  295. case GLSL_TYPE_FLOAT:
  296. f[c] = op[0]->value.f[c] - op[1]->value.f[c];
  297. break;
  298. default:
  299. assert(0);
  300. }
  301. }
  302. }
  303. break;
  304. case ir_binop_mul:
  305. if (ir->operands[0]->type == ir->operands[1]->type &&
  306. !ir->operands[0]->type->is_matrix()) {
  307. type = ir->operands[0]->type;
  308. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  309. switch (ir->operands[0]->type->base_type) {
  310. case GLSL_TYPE_UINT:
  311. u[c] = op[0]->value.u[c] * op[1]->value.u[c];
  312. break;
  313. case GLSL_TYPE_INT:
  314. i[c] = op[0]->value.i[c] * op[1]->value.i[c];
  315. break;
  316. case GLSL_TYPE_FLOAT:
  317. f[c] = op[0]->value.f[c] * op[1]->value.f[c];
  318. break;
  319. default:
  320. assert(0);
  321. }
  322. }
  323. }
  324. break;
  325. case ir_binop_div:
  326. if (ir->operands[0]->type == ir->operands[1]->type) {
  327. type = ir->operands[0]->type;
  328. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  329. switch (ir->operands[0]->type->base_type) {
  330. case GLSL_TYPE_UINT:
  331. u[c] = op[0]->value.u[c] / op[1]->value.u[c];
  332. break;
  333. case GLSL_TYPE_INT:
  334. i[c] = op[0]->value.i[c] / op[1]->value.i[c];
  335. break;
  336. case GLSL_TYPE_FLOAT:
  337. f[c] = op[0]->value.f[c] / op[1]->value.f[c];
  338. break;
  339. default:
  340. assert(0);
  341. }
  342. }
  343. }
  344. break;
  345. case ir_binop_logic_and:
  346. type = ir->operands[0]->type;
  347. assert(type->base_type == GLSL_TYPE_BOOL);
  348. for (c = 0; c < ir->operands[0]->type->components(); c++)
  349. b[c] = op[0]->value.b[c] && op[1]->value.b[c];
  350. break;
  351. case ir_binop_logic_xor:
  352. type = ir->operands[0]->type;
  353. assert(type->base_type == GLSL_TYPE_BOOL);
  354. for (c = 0; c < ir->operands[0]->type->components(); c++)
  355. b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
  356. break;
  357. case ir_binop_logic_or:
  358. type = ir->operands[0]->type;
  359. assert(type->base_type == GLSL_TYPE_BOOL);
  360. for (c = 0; c < ir->operands[0]->type->components(); c++)
  361. b[c] = op[0]->value.b[c] || op[1]->value.b[c];
  362. break;
  363. case ir_binop_less:
  364. type = glsl_type::bool_type;
  365. switch (ir->operands[0]->type->base_type) {
  366. case GLSL_TYPE_UINT:
  367. b[0] = op[0]->value.u[0] < op[1]->value.u[0];
  368. break;
  369. case GLSL_TYPE_INT:
  370. b[0] = op[0]->value.i[0] < op[1]->value.i[0];
  371. break;
  372. case GLSL_TYPE_FLOAT:
  373. b[0] = op[0]->value.f[0] < op[1]->value.f[0];
  374. break;
  375. default:
  376. assert(0);
  377. }
  378. break;
  379. case ir_binop_greater:
  380. type = glsl_type::bool_type;
  381. switch (ir->operands[0]->type->base_type) {
  382. case GLSL_TYPE_UINT:
  383. b[0] = op[0]->value.u[0] > op[1]->value.u[0];
  384. break;
  385. case GLSL_TYPE_INT:
  386. b[0] = op[0]->value.i[0] > op[1]->value.i[0];
  387. break;
  388. case GLSL_TYPE_FLOAT:
  389. b[0] = op[0]->value.f[0] > op[1]->value.f[0];
  390. break;
  391. default:
  392. assert(0);
  393. }
  394. break;
  395. case ir_binop_lequal:
  396. type = glsl_type::bool_type;
  397. switch (ir->operands[0]->type->base_type) {
  398. case GLSL_TYPE_UINT:
  399. b[0] = op[0]->value.u[0] <= op[1]->value.u[0];
  400. break;
  401. case GLSL_TYPE_INT:
  402. b[0] = op[0]->value.i[0] <= op[1]->value.i[0];
  403. break;
  404. case GLSL_TYPE_FLOAT:
  405. b[0] = op[0]->value.f[0] <= op[1]->value.f[0];
  406. break;
  407. default:
  408. assert(0);
  409. }
  410. break;
  411. case ir_binop_gequal:
  412. type = glsl_type::bool_type;
  413. switch (ir->operands[0]->type->base_type) {
  414. case GLSL_TYPE_UINT:
  415. b[0] = op[0]->value.u[0] >= op[1]->value.u[0];
  416. break;
  417. case GLSL_TYPE_INT:
  418. b[0] = op[0]->value.i[0] >= op[1]->value.i[0];
  419. break;
  420. case GLSL_TYPE_FLOAT:
  421. b[0] = op[0]->value.f[0] >= op[1]->value.f[0];
  422. break;
  423. default:
  424. assert(0);
  425. }
  426. break;
  427. case ir_binop_equal:
  428. if (ir->operands[0]->type == ir->operands[1]->type) {
  429. type = glsl_type::bool_type;
  430. b[0] = true;
  431. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  432. switch (ir->operands[0]->type->base_type) {
  433. case GLSL_TYPE_UINT:
  434. b[0] = b[0] && op[0]->value.u[c] == op[1]->value.u[c];
  435. break;
  436. case GLSL_TYPE_INT:
  437. b[0] = b[0] && op[0]->value.i[c] == op[1]->value.i[c];
  438. break;
  439. case GLSL_TYPE_FLOAT:
  440. b[0] = b[0] && op[0]->value.f[c] == op[1]->value.f[c];
  441. break;
  442. case GLSL_TYPE_BOOL:
  443. b[0] = b[0] && op[0]->value.b[c] == op[1]->value.b[c];
  444. break;
  445. default:
  446. assert(0);
  447. }
  448. }
  449. }
  450. break;
  451. case ir_binop_nequal:
  452. if (ir->operands[0]->type == ir->operands[1]->type) {
  453. type = glsl_type::bool_type;
  454. b[0] = false;
  455. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  456. switch (ir->operands[0]->type->base_type) {
  457. case GLSL_TYPE_UINT:
  458. b[0] = b[0] || op[0]->value.u[c] != op[1]->value.u[c];
  459. break;
  460. case GLSL_TYPE_INT:
  461. b[0] = b[0] || op[0]->value.i[c] != op[1]->value.i[c];
  462. break;
  463. case GLSL_TYPE_FLOAT:
  464. b[0] = b[0] || op[0]->value.f[c] != op[1]->value.f[c];
  465. break;
  466. case GLSL_TYPE_BOOL:
  467. b[0] = b[0] || op[0]->value.b[c] != op[1]->value.b[c];
  468. break;
  469. default:
  470. assert(0);
  471. }
  472. }
  473. }
  474. break;
  475. default:
  476. break;
  477. }
  478. if (type) {
  479. switch (type->base_type) {
  480. case GLSL_TYPE_UINT:
  481. value = new ir_constant(type, u);
  482. break;
  483. case GLSL_TYPE_INT:
  484. value = new ir_constant(type, i);
  485. break;
  486. case GLSL_TYPE_FLOAT:
  487. value = new ir_constant(type, f);
  488. break;
  489. case GLSL_TYPE_BOOL:
  490. value = new ir_constant(type, b);
  491. break;
  492. }
  493. }
  494. }
  495. void
  496. ir_constant_visitor::visit(ir_texture *ir)
  497. {
  498. // FINISHME: Do stuff with texture lookups
  499. (void) ir;
  500. value = NULL;
  501. }
  502. void
  503. ir_constant_visitor::visit(ir_swizzle *ir)
  504. {
  505. (void) ir;
  506. value = NULL;
  507. }
  508. void
  509. ir_constant_visitor::visit(ir_dereference_variable *ir)
  510. {
  511. value = NULL;
  512. ir_variable *var = ir->variable_referenced();
  513. if (var && var->constant_value)
  514. value = var->constant_value->clone();
  515. }
  516. void
  517. ir_constant_visitor::visit(ir_dereference_array *ir)
  518. {
  519. ir_constant *array = ir->array->constant_expression_value();
  520. ir_constant *idx = ir->array_index->constant_expression_value();
  521. this->value = NULL;
  522. if ((array != NULL) && (idx != NULL)) {
  523. if (array->type->is_matrix()) {
  524. /* Array access of a matrix results in a vector.
  525. */
  526. const unsigned column = idx->value.u[0];
  527. const glsl_type *const column_type = array->type->column_type();
  528. /* Offset in the constant matrix to the first element of the column
  529. * to be extracted.
  530. */
  531. const unsigned mat_idx = column * column_type->vector_elements;
  532. union {
  533. unsigned u[4];
  534. float f[4];
  535. } data;
  536. switch (column_type->base_type) {
  537. case GLSL_TYPE_UINT:
  538. case GLSL_TYPE_INT:
  539. for (unsigned i = 0; i < column_type->vector_elements; i++)
  540. data.u[i] = array->value.u[mat_idx + i];
  541. break;
  542. case GLSL_TYPE_FLOAT:
  543. for (unsigned i = 0; i < column_type->vector_elements; i++)
  544. data.f[i] = array->value.f[mat_idx + i];
  545. break;
  546. default:
  547. assert(!"Should not get here.");
  548. break;
  549. }
  550. this->value = new ir_constant(column_type, &data);
  551. } else if (array->type->is_vector()) {
  552. const unsigned component = idx->value.u[0];
  553. this->value = new ir_constant(array, component);
  554. } else {
  555. /* FINISHME: Handle access of constant arrays. */
  556. }
  557. }
  558. }
  559. void
  560. ir_constant_visitor::visit(ir_dereference_record *ir)
  561. {
  562. ir_constant *v = ir->record->constant_expression_value();
  563. this->value = (v != NULL) ? v->get_record_field(ir->field) : NULL;
  564. }
  565. void
  566. ir_constant_visitor::visit(ir_assignment *ir)
  567. {
  568. (void) ir;
  569. value = NULL;
  570. }
  571. void
  572. ir_constant_visitor::visit(ir_constant *ir)
  573. {
  574. value = ir;
  575. }
  576. void
  577. ir_constant_visitor::visit(ir_call *ir)
  578. {
  579. (void) ir;
  580. value = NULL;
  581. }
  582. void
  583. ir_constant_visitor::visit(ir_return *ir)
  584. {
  585. (void) ir;
  586. value = NULL;
  587. }
  588. void
  589. ir_constant_visitor::visit(ir_if *ir)
  590. {
  591. (void) ir;
  592. value = NULL;
  593. }
  594. void
  595. ir_constant_visitor::visit(ir_loop *ir)
  596. {
  597. (void) ir;
  598. value = NULL;
  599. }
  600. void
  601. ir_constant_visitor::visit(ir_loop_jump *ir)
  602. {
  603. (void) ir;
  604. value = NULL;
  605. }