Clone of mesa.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /* -*- c++ -*- */
  2. /*
  3. * Copyright © 2010 Intel Corporation
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #pragma once
  25. #ifndef IR_H
  26. #define IR_H
  27. #include "list.h"
  28. #include "ir_visitor.h"
  29. struct ir_program {
  30. void *bong_hits;
  31. };
  32. /**
  33. * Base class of all IR instructions
  34. */
  35. class ir_instruction : public exec_node {
  36. public:
  37. const struct glsl_type *type;
  38. virtual void accept(ir_visitor *) = 0;
  39. /**
  40. * \name IR instruction downcast functions
  41. *
  42. * These functions either cast the object to a derived class or return
  43. * \c NULL if the object's type does not match the specified derived class.
  44. * Additional downcast functions will be added as needed.
  45. */
  46. /*@{*/
  47. virtual class ir_variable * as_variable() { return NULL; }
  48. virtual class ir_dereference * as_dereference() { return NULL; }
  49. virtual class ir_rvalue * as_rvalue() { return NULL; }
  50. /*@}*/
  51. protected:
  52. ir_instruction()
  53. {
  54. /* empty */
  55. }
  56. };
  57. class ir_rvalue : public ir_instruction {
  58. public:
  59. virtual ir_rvalue * as_rvalue()
  60. {
  61. return this;
  62. }
  63. virtual bool is_lvalue()
  64. {
  65. return false;
  66. }
  67. protected:
  68. ir_rvalue() : ir_instruction() { }
  69. };
  70. enum ir_variable_mode {
  71. ir_var_auto = 0,
  72. ir_var_uniform,
  73. ir_var_in,
  74. ir_var_out,
  75. ir_var_inout
  76. };
  77. enum ir_varaible_interpolation {
  78. ir_var_smooth = 0,
  79. ir_var_flat,
  80. ir_var_noperspective
  81. };
  82. class ir_variable : public ir_instruction {
  83. public:
  84. ir_variable(const struct glsl_type *, const char *);
  85. virtual ir_variable *as_variable()
  86. {
  87. return this;
  88. }
  89. virtual void accept(ir_visitor *v)
  90. {
  91. v->visit(this);
  92. }
  93. const char *name;
  94. unsigned read_only:1;
  95. unsigned centroid:1;
  96. unsigned invariant:1;
  97. unsigned mode:3;
  98. unsigned interpolation:2;
  99. };
  100. class ir_label : public ir_instruction {
  101. public:
  102. ir_label(const char *label);
  103. virtual void accept(ir_visitor *v)
  104. {
  105. v->visit(this);
  106. }
  107. const char *label;
  108. };
  109. /*@{*/
  110. class ir_function_signature : public ir_instruction {
  111. public:
  112. ir_function_signature(const glsl_type *return_type);
  113. virtual void accept(ir_visitor *v)
  114. {
  115. v->visit(this);
  116. }
  117. /**
  118. * Function return type.
  119. *
  120. * \note This discards the optional precision qualifier.
  121. */
  122. const struct glsl_type *return_type;
  123. /**
  124. * List of function parameters stored as ir_variable objects.
  125. */
  126. struct exec_list parameters;
  127. /**
  128. * Pointer to the label that begins the function definition.
  129. */
  130. ir_label *definition;
  131. };
  132. /**
  133. * Header for tracking functions in the symbol table
  134. */
  135. class ir_function : public ir_instruction {
  136. public:
  137. ir_function(const char *name);
  138. virtual void accept(ir_visitor *v)
  139. {
  140. v->visit(this);
  141. }
  142. /**
  143. * Find a signature that matches a set of actual parameters.
  144. */
  145. const ir_function_signature *matching_signature(exec_list *actual_param);
  146. /**
  147. * Name of the function.
  148. */
  149. const char *name;
  150. /**
  151. * Set of overloaded functions with this name.
  152. */
  153. struct exec_list signatures;
  154. };
  155. /*@}*/
  156. class ir_assignment : public ir_rvalue {
  157. public:
  158. ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
  159. virtual void accept(ir_visitor *v)
  160. {
  161. v->visit(this);
  162. }
  163. /**
  164. * Left-hand side of the assignment.
  165. */
  166. ir_rvalue *lhs;
  167. /**
  168. * Value being assigned
  169. */
  170. ir_rvalue *rhs;
  171. /**
  172. * Optional condition for the assignment.
  173. */
  174. ir_rvalue *condition;
  175. };
  176. /* Update ir_print_visitor.cpp when updating this list. */
  177. enum ir_expression_operation {
  178. ir_unop_bit_not,
  179. ir_unop_logic_not,
  180. ir_unop_neg,
  181. ir_unop_abs,
  182. ir_unop_rcp,
  183. ir_unop_rsq,
  184. ir_unop_exp,
  185. ir_unop_log,
  186. ir_unop_f2i, /**< Float-to-integer conversion. */
  187. ir_unop_i2f, /**< Integer-to-float conversion. */
  188. ir_unop_u2f, /**< Unsigned-to-float conversion. */
  189. /**
  190. * \name Unary floating-point rounding operations.
  191. */
  192. /*@{*/
  193. ir_unop_trunc,
  194. ir_unop_ceil,
  195. ir_unop_floor,
  196. /*@}*/
  197. ir_binop_add,
  198. ir_binop_sub,
  199. ir_binop_mul,
  200. ir_binop_div,
  201. ir_binop_mod,
  202. /**
  203. * \name Binary comparison operators
  204. */
  205. /*@{*/
  206. ir_binop_less,
  207. ir_binop_greater,
  208. ir_binop_lequal,
  209. ir_binop_gequal,
  210. ir_binop_equal,
  211. ir_binop_nequal,
  212. /*@}*/
  213. /**
  214. * \name Bit-wise binary operations.
  215. */
  216. /*@{*/
  217. ir_binop_lshift,
  218. ir_binop_rshift,
  219. ir_binop_bit_and,
  220. ir_binop_bit_xor,
  221. ir_binop_bit_or,
  222. /*@}*/
  223. ir_binop_logic_and,
  224. ir_binop_logic_xor,
  225. ir_binop_logic_or,
  226. ir_binop_logic_not,
  227. ir_binop_dot,
  228. ir_binop_min,
  229. ir_binop_max,
  230. ir_binop_pow
  231. };
  232. class ir_expression : public ir_rvalue {
  233. public:
  234. ir_expression(int op, const struct glsl_type *type,
  235. ir_rvalue *, ir_rvalue *);
  236. virtual void accept(ir_visitor *v)
  237. {
  238. v->visit(this);
  239. }
  240. ir_expression_operation operation;
  241. ir_rvalue *operands[2];
  242. };
  243. /**
  244. * IR instruction representing a function call
  245. */
  246. class ir_call : public ir_rvalue {
  247. public:
  248. ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
  249. : ir_rvalue(), callee(callee)
  250. {
  251. assert(callee->return_type != NULL);
  252. type = callee->return_type;
  253. actual_parameters->move_nodes_to(& this->actual_parameters);
  254. }
  255. virtual void accept(ir_visitor *v)
  256. {
  257. v->visit(this);
  258. }
  259. /**
  260. * Get a generic ir_call object when an error occurs
  261. */
  262. static ir_call *get_error_instruction();
  263. /**
  264. * Get an iterator for the set of acutal parameters
  265. */
  266. exec_list_iterator iterator()
  267. {
  268. return actual_parameters.iterator();
  269. }
  270. /**
  271. * Get the name of the function being called.
  272. */
  273. const char *callee_name() const
  274. {
  275. /* FINISHME: This only works for functions that have definitions. */
  276. return callee->definition->label;
  277. }
  278. private:
  279. ir_call()
  280. : ir_rvalue(), callee(NULL)
  281. {
  282. /* empty */
  283. }
  284. const ir_function_signature *callee;
  285. exec_list actual_parameters;
  286. };
  287. /**
  288. * \name Jump-like IR instructions.
  289. *
  290. * These include \c break, \c continue, \c return, and \c discard.
  291. */
  292. /*@{*/
  293. class ir_jump : public ir_instruction {
  294. protected:
  295. ir_jump()
  296. : ir_instruction()
  297. {
  298. /* empty */
  299. }
  300. };
  301. class ir_return : public ir_jump {
  302. public:
  303. ir_return()
  304. : value(NULL)
  305. {
  306. /* empty */
  307. }
  308. ir_return(ir_rvalue *value)
  309. : value(value)
  310. {
  311. /* empty */
  312. }
  313. ir_rvalue *get_value() const
  314. {
  315. return value;
  316. }
  317. virtual void accept(ir_visitor *v)
  318. {
  319. v->visit(this);
  320. }
  321. private:
  322. ir_rvalue *value;
  323. };
  324. /*@}*/
  325. struct ir_swizzle_mask {
  326. unsigned x:2;
  327. unsigned y:2;
  328. unsigned z:2;
  329. unsigned w:2;
  330. /**
  331. * Number of components in the swizzle.
  332. */
  333. unsigned num_components:3;
  334. /**
  335. * Does the swizzle contain duplicate components?
  336. *
  337. * L-value swizzles cannot contain duplicate components.
  338. */
  339. unsigned has_duplicates:1;
  340. };
  341. class ir_swizzle : public ir_rvalue {
  342. public:
  343. ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
  344. unsigned count);
  345. /**
  346. * Construct an ir_swizzle from the textual representation. Can fail.
  347. */
  348. static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
  349. virtual void accept(ir_visitor *v)
  350. {
  351. v->visit(this);
  352. }
  353. bool is_lvalue()
  354. {
  355. return val->is_lvalue();
  356. }
  357. ir_rvalue *val;
  358. ir_swizzle_mask mask;
  359. };
  360. class ir_dereference : public ir_rvalue {
  361. public:
  362. ir_dereference(struct ir_instruction *);
  363. ir_dereference(ir_instruction *variable, ir_rvalue *array_index);
  364. virtual ir_dereference *as_dereference()
  365. {
  366. return this;
  367. }
  368. virtual void accept(ir_visitor *v)
  369. {
  370. v->visit(this);
  371. }
  372. bool is_lvalue()
  373. {
  374. ir_variable *as_var;
  375. if (var == NULL)
  376. return NULL;
  377. as_var = var->as_variable();
  378. if (as_var == NULL)
  379. return NULL;
  380. return !as_var->read_only;
  381. }
  382. enum {
  383. ir_reference_variable,
  384. ir_reference_array,
  385. ir_reference_record
  386. } mode;
  387. /**
  388. * Object being dereferenced.
  389. *
  390. * Must be either an \c ir_variable or an \c ir_rvalue.
  391. */
  392. ir_instruction *var;
  393. union {
  394. ir_rvalue *array_index;
  395. const char *field;
  396. } selector;
  397. };
  398. class ir_constant : public ir_rvalue {
  399. public:
  400. ir_constant(const struct glsl_type *type, const void *data);
  401. ir_constant(bool b);
  402. ir_constant(unsigned int u);
  403. ir_constant(int i);
  404. ir_constant(float f);
  405. virtual void accept(ir_visitor *v)
  406. {
  407. v->visit(this);
  408. }
  409. /**
  410. * Value of the constant.
  411. *
  412. * The field used to back the values supplied by the constant is determined
  413. * by the type associated with the \c ir_instruction. Constants may be
  414. * scalars, vectors, or matrices.
  415. */
  416. union {
  417. unsigned u[16];
  418. int i[16];
  419. float f[16];
  420. bool b[16];
  421. } value;
  422. };
  423. extern void
  424. _mesa_glsl_initialize_variables(exec_list *instructions,
  425. struct _mesa_glsl_parse_state *state);
  426. extern void
  427. _mesa_glsl_initialize_functions(exec_list *instructions,
  428. struct _mesa_glsl_parse_state *state);
  429. #endif /* IR_H */