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.

ir.h 25KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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 <cstdio>
  28. #include <cstdlib>
  29. #include "list.h"
  30. #include "ir_visitor.h"
  31. #include "ir_hierarchical_visitor.h"
  32. struct ir_program {
  33. void *bong_hits;
  34. };
  35. /**
  36. * Base class of all IR instructions
  37. */
  38. class ir_instruction : public exec_node {
  39. public:
  40. const struct glsl_type *type;
  41. class ir_constant *constant_expression_value();
  42. /** ir_print_visitor helper for debugging. */
  43. void print(void);
  44. virtual void accept(ir_visitor *) = 0;
  45. virtual ir_visitor_status accept(ir_hierarchical_visitor *) = 0;
  46. /**
  47. * \name IR instruction downcast functions
  48. *
  49. * These functions either cast the object to a derived class or return
  50. * \c NULL if the object's type does not match the specified derived class.
  51. * Additional downcast functions will be added as needed.
  52. */
  53. /*@{*/
  54. virtual class ir_variable * as_variable() { return NULL; }
  55. virtual class ir_function * as_function() { return NULL; }
  56. virtual class ir_dereference * as_dereference() { return NULL; }
  57. virtual class ir_dereference_array * as_dereference_array() { return NULL; }
  58. virtual class ir_rvalue * as_rvalue() { return NULL; }
  59. virtual class ir_loop * as_loop() { return NULL; }
  60. virtual class ir_assignment * as_assignment() { return NULL; }
  61. virtual class ir_call * as_call() { return NULL; }
  62. virtual class ir_return * as_return() { return NULL; }
  63. virtual class ir_if * as_if() { return NULL; }
  64. virtual class ir_swizzle * as_swizzle() { return NULL; }
  65. virtual class ir_constant * as_constant() { return NULL; }
  66. /*@}*/
  67. protected:
  68. ir_instruction()
  69. {
  70. /* empty */
  71. }
  72. };
  73. class ir_rvalue : public ir_instruction {
  74. public:
  75. virtual ir_rvalue * as_rvalue()
  76. {
  77. return this;
  78. }
  79. virtual bool is_lvalue()
  80. {
  81. return false;
  82. }
  83. /**
  84. * Get the variable that is ultimately referenced by an r-value
  85. */
  86. virtual ir_variable *variable_referenced()
  87. {
  88. return NULL;
  89. }
  90. /**
  91. * If an r-value is a reference to a whole variable, get that variable
  92. *
  93. * \return
  94. * Pointer to a variable that is completely dereferenced by the r-value. If
  95. * the r-value is not a dereference or the dereference does not access the
  96. * entire variable (i.e., it's just one array element, struct field), \c NULL
  97. * is returned.
  98. */
  99. virtual ir_variable *whole_variable_referenced()
  100. {
  101. return NULL;
  102. }
  103. protected:
  104. ir_rvalue()
  105. {
  106. /* empty */
  107. }
  108. };
  109. enum ir_variable_mode {
  110. ir_var_auto = 0,
  111. ir_var_uniform,
  112. ir_var_in,
  113. ir_var_out,
  114. ir_var_inout
  115. };
  116. enum ir_varaible_interpolation {
  117. ir_var_smooth = 0,
  118. ir_var_flat,
  119. ir_var_noperspective
  120. };
  121. class ir_variable : public ir_instruction {
  122. public:
  123. ir_variable(const struct glsl_type *, const char *);
  124. virtual ir_variable *as_variable()
  125. {
  126. return this;
  127. }
  128. virtual void accept(ir_visitor *v)
  129. {
  130. v->visit(this);
  131. }
  132. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  133. /**
  134. * Duplicate an IR variable
  135. *
  136. * \note
  137. * This will probably be made \c virtual and moved to the base class
  138. * eventually.
  139. */
  140. ir_variable *clone() const
  141. {
  142. ir_variable *var = new ir_variable(type, name);
  143. var->max_array_access = this->max_array_access;
  144. var->read_only = this->read_only;
  145. var->centroid = this->centroid;
  146. var->invariant = this->invariant;
  147. var->mode = this->mode;
  148. var->interpolation = this->interpolation;
  149. return var;
  150. }
  151. const char *name;
  152. /**
  153. * Highest element accessed with a constant expression array index
  154. *
  155. * Not used for non-array variables.
  156. */
  157. unsigned max_array_access;
  158. unsigned read_only:1;
  159. unsigned centroid:1;
  160. unsigned invariant:1;
  161. /** If the variable is initialized outside of the scope of the shader */
  162. unsigned shader_in:1;
  163. /**
  164. * If the variable value is later used outside of the scope of the shader.
  165. */
  166. unsigned shader_out:1;
  167. unsigned mode:3;
  168. unsigned interpolation:2;
  169. /**
  170. * Flag that the whole array is assignable
  171. *
  172. * In GLSL 1.20 and later whole arrays are assignable (and comparable for
  173. * equality). This flag enables this behavior.
  174. */
  175. unsigned array_lvalue:1;
  176. /**
  177. * Emit a warning if this variable is accessed.
  178. */
  179. const char *warn_extension;
  180. /**
  181. * Value assigned in the initializer of a variable declared "const"
  182. */
  183. ir_constant *constant_value;
  184. };
  185. /*@{*/
  186. /**
  187. * The representation of a function instance; may be the full definition or
  188. * simply a prototype.
  189. */
  190. class ir_function_signature : public ir_instruction {
  191. /* An ir_function_signature will be part of the list of signatures in
  192. * an ir_function.
  193. */
  194. public:
  195. ir_function_signature(const glsl_type *return_type);
  196. virtual void accept(ir_visitor *v)
  197. {
  198. v->visit(this);
  199. }
  200. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  201. /**
  202. * Get the name of the function for which this is a signature
  203. */
  204. const char *function_name() const;
  205. /**
  206. * Check whether the qualifiers match between this signature's parameters
  207. * and the supplied parameter list. If not, returns the name of the first
  208. * parameter with mismatched qualifiers (for use in error messages).
  209. */
  210. const char *qualifiers_match(exec_list *params);
  211. /**
  212. * Replace the current parameter list with the given one. This is useful
  213. * if the current information came from a prototype, and either has invalid
  214. * or missing parameter names.
  215. */
  216. void replace_parameters(exec_list *new_params);
  217. /**
  218. * Function return type.
  219. *
  220. * \note This discards the optional precision qualifier.
  221. */
  222. const struct glsl_type *return_type;
  223. /**
  224. * List of ir_variable of function parameters.
  225. *
  226. * This represents the storage. The paramaters passed in a particular
  227. * call will be in ir_call::actual_paramaters.
  228. */
  229. struct exec_list parameters;
  230. /** Whether or not this function has a body (which may be empty). */
  231. unsigned is_defined:1;
  232. /** Body of instructions in the function. */
  233. struct exec_list body;
  234. private:
  235. /** Function of which this signature is one overload. */
  236. class ir_function *function;
  237. friend class ir_function;
  238. };
  239. /**
  240. * Header for tracking multiple overloaded functions with the same name.
  241. * Contains a list of ir_function_signatures representing each of the
  242. * actual functions.
  243. */
  244. class ir_function : public ir_instruction {
  245. public:
  246. ir_function(const char *name);
  247. virtual ir_function *as_function()
  248. {
  249. return this;
  250. }
  251. virtual void accept(ir_visitor *v)
  252. {
  253. v->visit(this);
  254. }
  255. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  256. void add_signature(ir_function_signature *sig)
  257. {
  258. sig->function = this;
  259. signatures.push_tail(sig);
  260. }
  261. /**
  262. * Get an iterator for the set of function signatures
  263. */
  264. exec_list_iterator iterator()
  265. {
  266. return signatures.iterator();
  267. }
  268. /**
  269. * Find a signature that matches a set of actual parameters, taking implicit
  270. * conversions into account.
  271. */
  272. const ir_function_signature *matching_signature(exec_list *actual_param);
  273. /**
  274. * Find a signature that exactly matches a set of actual parameters without
  275. * any implicit type conversions.
  276. */
  277. ir_function_signature *exact_matching_signature(exec_list *actual_ps);
  278. /**
  279. * Name of the function.
  280. */
  281. const char *name;
  282. private:
  283. /**
  284. * List of ir_function_signature for each overloaded function with this name.
  285. */
  286. struct exec_list signatures;
  287. };
  288. inline const char *ir_function_signature::function_name() const
  289. {
  290. return function->name;
  291. }
  292. /*@}*/
  293. /**
  294. * IR instruction representing high-level if-statements
  295. */
  296. class ir_if : public ir_instruction {
  297. public:
  298. ir_if(ir_rvalue *condition)
  299. : condition(condition)
  300. {
  301. /* empty */
  302. }
  303. virtual ir_if *as_if()
  304. {
  305. return this;
  306. }
  307. virtual void accept(ir_visitor *v)
  308. {
  309. v->visit(this);
  310. }
  311. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  312. ir_rvalue *condition;
  313. /** List of ir_instruction for the body of the then branch */
  314. exec_list then_instructions;
  315. /** List of ir_instruction for the body of the else branch */
  316. exec_list else_instructions;
  317. };
  318. /**
  319. * IR instruction representing a high-level loop structure.
  320. */
  321. class ir_loop : public ir_instruction {
  322. public:
  323. ir_loop() : from(NULL), to(NULL), increment(NULL), counter(NULL)
  324. {
  325. /* empty */
  326. }
  327. virtual void accept(ir_visitor *v)
  328. {
  329. v->visit(this);
  330. }
  331. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  332. virtual ir_loop *as_loop()
  333. {
  334. return this;
  335. }
  336. /**
  337. * Get an iterator for the instructions of the loop body
  338. */
  339. exec_list_iterator iterator()
  340. {
  341. return body_instructions.iterator();
  342. }
  343. /** List of ir_instruction that make up the body of the loop. */
  344. exec_list body_instructions;
  345. /**
  346. * \name Loop counter and controls
  347. */
  348. /*@{*/
  349. ir_rvalue *from;
  350. ir_rvalue *to;
  351. ir_rvalue *increment;
  352. ir_variable *counter;
  353. /*@}*/
  354. };
  355. class ir_assignment : public ir_rvalue {
  356. public:
  357. ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
  358. virtual void accept(ir_visitor *v)
  359. {
  360. v->visit(this);
  361. }
  362. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  363. virtual ir_assignment * as_assignment()
  364. {
  365. return this;
  366. }
  367. /**
  368. * Left-hand side of the assignment.
  369. */
  370. ir_rvalue *lhs;
  371. /**
  372. * Value being assigned
  373. */
  374. ir_rvalue *rhs;
  375. /**
  376. * Optional condition for the assignment.
  377. */
  378. ir_rvalue *condition;
  379. };
  380. /* Update ir_expression::num_operands() and operator_strs when
  381. * updating this list.
  382. */
  383. enum ir_expression_operation {
  384. ir_unop_bit_not,
  385. ir_unop_logic_not,
  386. ir_unop_neg,
  387. ir_unop_abs,
  388. ir_unop_sign,
  389. ir_unop_rcp,
  390. ir_unop_rsq,
  391. ir_unop_sqrt,
  392. ir_unop_exp,
  393. ir_unop_log,
  394. ir_unop_exp2,
  395. ir_unop_log2,
  396. ir_unop_f2i, /**< Float-to-integer conversion. */
  397. ir_unop_i2f, /**< Integer-to-float conversion. */
  398. ir_unop_f2b, /**< Float-to-boolean conversion */
  399. ir_unop_b2f, /**< Boolean-to-float conversion */
  400. ir_unop_i2b, /**< int-to-boolean conversion */
  401. ir_unop_b2i, /**< Boolean-to-int conversion */
  402. ir_unop_u2f, /**< Unsigned-to-float conversion. */
  403. /**
  404. * \name Unary floating-point rounding operations.
  405. */
  406. /*@{*/
  407. ir_unop_trunc,
  408. ir_unop_ceil,
  409. ir_unop_floor,
  410. /*@}*/
  411. /**
  412. * \name Trigonometric operations.
  413. */
  414. /*@{*/
  415. ir_unop_sin,
  416. ir_unop_cos,
  417. /*@}*/
  418. /**
  419. * \name Partial derivatives.
  420. */
  421. /*@{*/
  422. ir_unop_dFdx,
  423. ir_unop_dFdy,
  424. /*@}*/
  425. ir_binop_add,
  426. ir_binop_sub,
  427. ir_binop_mul,
  428. ir_binop_div,
  429. ir_binop_mod,
  430. /**
  431. * \name Binary comparison operators
  432. */
  433. /*@{*/
  434. ir_binop_less,
  435. ir_binop_greater,
  436. ir_binop_lequal,
  437. ir_binop_gequal,
  438. ir_binop_equal,
  439. ir_binop_nequal,
  440. /*@}*/
  441. /**
  442. * \name Bit-wise binary operations.
  443. */
  444. /*@{*/
  445. ir_binop_lshift,
  446. ir_binop_rshift,
  447. ir_binop_bit_and,
  448. ir_binop_bit_xor,
  449. ir_binop_bit_or,
  450. /*@}*/
  451. ir_binop_logic_and,
  452. ir_binop_logic_xor,
  453. ir_binop_logic_or,
  454. ir_binop_dot,
  455. ir_binop_min,
  456. ir_binop_max,
  457. ir_binop_pow
  458. };
  459. class ir_expression : public ir_rvalue {
  460. public:
  461. ir_expression(int op, const struct glsl_type *type,
  462. ir_rvalue *, ir_rvalue *);
  463. static unsigned int get_num_operands(ir_expression_operation);
  464. unsigned int get_num_operands()
  465. {
  466. return get_num_operands(operation);
  467. }
  468. /**
  469. * Return a string representing this expression's operator.
  470. */
  471. const char *operator_string();
  472. /**
  473. * Do a reverse-lookup to translate the given string into an operator.
  474. */
  475. static ir_expression_operation get_operator(const char *);
  476. virtual void accept(ir_visitor *v)
  477. {
  478. v->visit(this);
  479. }
  480. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  481. ir_expression *clone();
  482. ir_expression_operation operation;
  483. ir_rvalue *operands[2];
  484. };
  485. /**
  486. * IR instruction representing a function call
  487. */
  488. class ir_call : public ir_rvalue {
  489. public:
  490. ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
  491. : callee(callee)
  492. {
  493. assert(callee->return_type != NULL);
  494. type = callee->return_type;
  495. actual_parameters->move_nodes_to(& this->actual_parameters);
  496. }
  497. virtual ir_call *as_call()
  498. {
  499. return this;
  500. }
  501. virtual void accept(ir_visitor *v)
  502. {
  503. v->visit(this);
  504. }
  505. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  506. /**
  507. * Get a generic ir_call object when an error occurs
  508. */
  509. static ir_call *get_error_instruction();
  510. /**
  511. * Get an iterator for the set of acutal parameters
  512. */
  513. exec_list_iterator iterator()
  514. {
  515. return actual_parameters.iterator();
  516. }
  517. /**
  518. * Get the name of the function being called.
  519. */
  520. const char *callee_name() const
  521. {
  522. return callee->function_name();
  523. }
  524. const ir_function_signature *get_callee()
  525. {
  526. return callee;
  527. }
  528. /**
  529. * Generates an inline version of the function before @ir,
  530. * returning the return value of the function.
  531. */
  532. ir_rvalue *generate_inline(ir_instruction *ir);
  533. private:
  534. ir_call()
  535. : callee(NULL)
  536. {
  537. /* empty */
  538. }
  539. const ir_function_signature *callee;
  540. /* List of ir_rvalue of paramaters passed in this call. */
  541. exec_list actual_parameters;
  542. };
  543. /**
  544. * \name Jump-like IR instructions.
  545. *
  546. * These include \c break, \c continue, \c return, and \c discard.
  547. */
  548. /*@{*/
  549. class ir_jump : public ir_instruction {
  550. protected:
  551. ir_jump()
  552. {
  553. /* empty */
  554. }
  555. };
  556. class ir_return : public ir_jump {
  557. public:
  558. ir_return()
  559. : value(NULL)
  560. {
  561. /* empty */
  562. }
  563. ir_return(ir_rvalue *value)
  564. : value(value)
  565. {
  566. /* empty */
  567. }
  568. virtual ir_return *as_return()
  569. {
  570. return this;
  571. }
  572. ir_rvalue *get_value() const
  573. {
  574. return value;
  575. }
  576. virtual void accept(ir_visitor *v)
  577. {
  578. v->visit(this);
  579. }
  580. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  581. ir_rvalue *value;
  582. };
  583. /**
  584. * Jump instructions used inside loops
  585. *
  586. * These include \c break and \c continue. The \c break within a loop is
  587. * different from the \c break within a switch-statement.
  588. *
  589. * \sa ir_switch_jump
  590. */
  591. class ir_loop_jump : public ir_jump {
  592. public:
  593. enum jump_mode {
  594. jump_break,
  595. jump_continue
  596. };
  597. ir_loop_jump(ir_loop *loop, jump_mode mode)
  598. : loop(loop), mode(mode)
  599. {
  600. /* empty */
  601. }
  602. virtual void accept(ir_visitor *v)
  603. {
  604. v->visit(this);
  605. }
  606. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  607. bool is_break() const
  608. {
  609. return mode == jump_break;
  610. }
  611. bool is_continue() const
  612. {
  613. return mode == jump_continue;
  614. }
  615. private:
  616. /** Loop containing this break instruction. */
  617. ir_loop *loop;
  618. /** Mode selector for the jump instruction. */
  619. enum jump_mode mode;
  620. };
  621. /*@}*/
  622. /**
  623. * Texture sampling opcodes used in ir_texture
  624. */
  625. enum ir_texture_opcode {
  626. ir_tex, /* Regular texture look-up */
  627. ir_txb, /* Texture look-up with LOD bias */
  628. ir_txl, /* Texture look-up with explicit LOD */
  629. ir_txd, /* Texture look-up with partial derivatvies */
  630. ir_txf /* Texel fetch with explicit LOD */
  631. };
  632. /**
  633. * IR instruction to sample a texture
  634. *
  635. * The specific form of the IR instruction depends on the \c mode value
  636. * selected from \c ir_texture_opcodes. In the printed IR, these will
  637. * appear as:
  638. *
  639. * Texel offset
  640. * | Projection divisor
  641. * | | Shadow comparitor
  642. * | | |
  643. * v v v
  644. * (tex (sampler) (coordinate) (0 0 0) (1) ( ))
  645. * (txb (sampler) (coordinate) (0 0 0) (1) ( ) (bias))
  646. * (txl (sampler) (coordinate) (0 0 0) (1) ( ) (lod))
  647. * (txd (sampler) (coordinate) (0 0 0) (1) ( ) (dPdx dPdy))
  648. * (txf (sampler) (coordinate) (0 0 0) (lod))
  649. */
  650. class ir_texture : public ir_rvalue {
  651. public:
  652. ir_texture(enum ir_texture_opcode op)
  653. : op(op), projector(NULL), shadow_comparitor(NULL)
  654. {
  655. /* empty */
  656. }
  657. virtual void accept(ir_visitor *v)
  658. {
  659. v->visit(this);
  660. }
  661. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  662. /**
  663. * Return a string representing the ir_texture_opcode.
  664. */
  665. const char *opcode_string();
  666. /** Set the sampler and infer the type. */
  667. void set_sampler(ir_dereference *sampler);
  668. /**
  669. * Do a reverse-lookup to translate a string into an ir_texture_opcode.
  670. */
  671. static ir_texture_opcode get_opcode(const char *);
  672. enum ir_texture_opcode op;
  673. /** Sampler to use for the texture access. */
  674. ir_dereference *sampler;
  675. /** Texture coordinate to sample */
  676. ir_rvalue *coordinate;
  677. /**
  678. * Value used for projective divide.
  679. *
  680. * If there is no projective divide (the common case), this will be
  681. * \c NULL. Optimization passes should check for this to point to a constant
  682. * of 1.0 and replace that with \c NULL.
  683. */
  684. ir_rvalue *projector;
  685. /**
  686. * Coordinate used for comparison on shadow look-ups.
  687. *
  688. * If there is no shadow comparison, this will be \c NULL. For the
  689. * \c ir_txf opcode, this *must* be \c NULL.
  690. */
  691. ir_rvalue *shadow_comparitor;
  692. /** Explicit texel offsets. */
  693. signed char offsets[3];
  694. union {
  695. ir_rvalue *lod; /**< Floating point LOD */
  696. ir_rvalue *bias; /**< Floating point LOD bias */
  697. struct {
  698. ir_rvalue *dPdx; /**< Partial derivative of coordinate wrt X */
  699. ir_rvalue *dPdy; /**< Partial derivative of coordinate wrt Y */
  700. } grad;
  701. } lod_info;
  702. };
  703. struct ir_swizzle_mask {
  704. unsigned x:2;
  705. unsigned y:2;
  706. unsigned z:2;
  707. unsigned w:2;
  708. /**
  709. * Number of components in the swizzle.
  710. */
  711. unsigned num_components:3;
  712. /**
  713. * Does the swizzle contain duplicate components?
  714. *
  715. * L-value swizzles cannot contain duplicate components.
  716. */
  717. unsigned has_duplicates:1;
  718. };
  719. class ir_swizzle : public ir_rvalue {
  720. public:
  721. ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
  722. unsigned count);
  723. ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
  724. virtual ir_swizzle *as_swizzle()
  725. {
  726. return this;
  727. }
  728. ir_swizzle *clone()
  729. {
  730. return new ir_swizzle(this->val, this->mask);
  731. }
  732. /**
  733. * Construct an ir_swizzle from the textual representation. Can fail.
  734. */
  735. static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
  736. virtual void accept(ir_visitor *v)
  737. {
  738. v->visit(this);
  739. }
  740. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  741. bool is_lvalue()
  742. {
  743. return val->is_lvalue() && !mask.has_duplicates;
  744. }
  745. /**
  746. * Get the variable that is ultimately referenced by an r-value
  747. */
  748. virtual ir_variable *variable_referenced();
  749. ir_rvalue *val;
  750. ir_swizzle_mask mask;
  751. };
  752. class ir_dereference : public ir_rvalue {
  753. public:
  754. virtual ir_dereference *as_dereference()
  755. {
  756. return this;
  757. }
  758. bool is_lvalue();
  759. /**
  760. * Get the variable that is ultimately referenced by an r-value
  761. */
  762. virtual ir_variable *variable_referenced() = 0;
  763. };
  764. class ir_dereference_variable : public ir_dereference {
  765. public:
  766. ir_dereference_variable(ir_variable *var);
  767. /**
  768. * Get the variable that is ultimately referenced by an r-value
  769. */
  770. virtual ir_variable *variable_referenced()
  771. {
  772. return this->var;
  773. }
  774. virtual ir_variable *whole_variable_referenced()
  775. {
  776. /* ir_dereference_variable objects always dereference the entire
  777. * variable. However, if this dereference is dereferenced by anything
  778. * else, the complete deferefernce chain is not a whole-variable
  779. * dereference. This method should only be called on the top most
  780. * ir_rvalue in a dereference chain.
  781. */
  782. return this->var;
  783. }
  784. virtual void accept(ir_visitor *v)
  785. {
  786. v->visit(this);
  787. }
  788. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  789. /**
  790. * Object being dereferenced.
  791. */
  792. ir_variable *var;
  793. };
  794. class ir_dereference_array : public ir_dereference {
  795. public:
  796. ir_dereference_array(ir_rvalue *value, ir_rvalue *array_index);
  797. ir_dereference_array(ir_variable *var, ir_rvalue *array_index);
  798. virtual ir_dereference_array *as_dereference_array()
  799. {
  800. return this;
  801. }
  802. /**
  803. * Get the variable that is ultimately referenced by an r-value
  804. */
  805. virtual ir_variable *variable_referenced()
  806. {
  807. return this->array->variable_referenced();
  808. }
  809. virtual void accept(ir_visitor *v)
  810. {
  811. v->visit(this);
  812. }
  813. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  814. ir_rvalue *array;
  815. ir_rvalue *array_index;
  816. private:
  817. void set_array(ir_rvalue *value);
  818. };
  819. class ir_dereference_record : public ir_dereference {
  820. public:
  821. ir_dereference_record(ir_rvalue *value, const char *field);
  822. ir_dereference_record(ir_variable *var, const char *field);
  823. /**
  824. * Get the variable that is ultimately referenced by an r-value
  825. */
  826. virtual ir_variable *variable_referenced()
  827. {
  828. return this->record->variable_referenced();
  829. }
  830. virtual void accept(ir_visitor *v)
  831. {
  832. v->visit(this);
  833. }
  834. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  835. ir_rvalue *record;
  836. const char *field;
  837. };
  838. /**
  839. * Data stored in an ir_constant
  840. */
  841. union ir_constant_data {
  842. unsigned u[16];
  843. int i[16];
  844. float f[16];
  845. bool b[16];
  846. };
  847. class ir_constant : public ir_rvalue {
  848. public:
  849. ir_constant(const struct glsl_type *type, const ir_constant_data *data);
  850. ir_constant(bool b);
  851. ir_constant(unsigned int u);
  852. ir_constant(int i);
  853. ir_constant(float f);
  854. /**
  855. * Construct an ir_constant from a list of ir_constant values
  856. */
  857. ir_constant(const struct glsl_type *type, exec_list *values);
  858. /**
  859. * Construct an ir_constant from a scalar component of another ir_constant
  860. *
  861. * The new \c ir_constant inherits the type of the component from the
  862. * source constant.
  863. *
  864. * \note
  865. * In the case of a matrix constant, the new constant is a scalar, \b not
  866. * a vector.
  867. */
  868. ir_constant(const ir_constant *c, unsigned i);
  869. virtual ir_constant *as_constant()
  870. {
  871. return this;
  872. }
  873. virtual void accept(ir_visitor *v)
  874. {
  875. v->visit(this);
  876. }
  877. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  878. ir_constant *clone();
  879. /**
  880. * Get a particular component of a constant as a specific type
  881. *
  882. * This is useful, for example, to get a value from an integer constant
  883. * as a float or bool. This appears frequently when constructors are
  884. * called with all constant parameters.
  885. */
  886. /*@{*/
  887. bool get_bool_component(unsigned i) const;
  888. float get_float_component(unsigned i) const;
  889. int get_int_component(unsigned i) const;
  890. unsigned get_uint_component(unsigned i) const;
  891. /*@}*/
  892. ir_constant *get_record_field(const char *name);
  893. /**
  894. * Value of the constant.
  895. *
  896. * The field used to back the values supplied by the constant is determined
  897. * by the type associated with the \c ir_instruction. Constants may be
  898. * scalars, vectors, or matrices.
  899. */
  900. union ir_constant_data value;
  901. exec_list components;
  902. private:
  903. /**
  904. * Parameterless constructor only used by the clone method
  905. */
  906. ir_constant(void);
  907. };
  908. void
  909. visit_exec_list(exec_list *list, ir_visitor *visitor);
  910. extern void
  911. _mesa_glsl_initialize_variables(exec_list *instructions,
  912. struct _mesa_glsl_parse_state *state);
  913. extern void
  914. _mesa_glsl_initialize_functions(exec_list *instructions,
  915. struct _mesa_glsl_parse_state *state);
  916. #endif /* IR_H */