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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551
  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 <stdio.h>
  28. #include <stdlib.h>
  29. #include "util/ralloc.h"
  30. #include "glsl_types.h"
  31. #include "list.h"
  32. #include "ir_visitor.h"
  33. #include "ir_hierarchical_visitor.h"
  34. #include "main/mtypes.h"
  35. #ifdef __cplusplus
  36. /**
  37. * \defgroup IR Intermediate representation nodes
  38. *
  39. * @{
  40. */
  41. /**
  42. * Class tags
  43. *
  44. * Each concrete class derived from \c ir_instruction has a value in this
  45. * enumerant. The value for the type is stored in \c ir_instruction::ir_type
  46. * by the constructor. While using type tags is not very C++, it is extremely
  47. * convenient. For example, during debugging you can simply inspect
  48. * \c ir_instruction::ir_type to find out the actual type of the object.
  49. *
  50. * In addition, it is possible to use a switch-statement based on \c
  51. * \c ir_instruction::ir_type to select different behavior for different object
  52. * types. For functions that have only slight differences for several object
  53. * types, this allows writing very straightforward, readable code.
  54. */
  55. enum ir_node_type {
  56. ir_type_dereference_array,
  57. ir_type_dereference_record,
  58. ir_type_dereference_variable,
  59. ir_type_constant,
  60. ir_type_expression,
  61. ir_type_swizzle,
  62. ir_type_texture,
  63. ir_type_variable,
  64. ir_type_assignment,
  65. ir_type_call,
  66. ir_type_function,
  67. ir_type_function_signature,
  68. ir_type_if,
  69. ir_type_loop,
  70. ir_type_loop_jump,
  71. ir_type_return,
  72. ir_type_discard,
  73. ir_type_emit_vertex,
  74. ir_type_end_primitive,
  75. ir_type_barrier,
  76. ir_type_max, /**< maximum ir_type enum number, for validation */
  77. ir_type_unset = ir_type_max
  78. };
  79. /**
  80. * Base class of all IR instructions
  81. */
  82. class ir_instruction : public exec_node {
  83. public:
  84. enum ir_node_type ir_type;
  85. /**
  86. * GCC 4.7+ and clang warn when deleting an ir_instruction unless
  87. * there's a virtual destructor present. Because we almost
  88. * universally use ralloc for our memory management of
  89. * ir_instructions, the destructor doesn't need to do any work.
  90. */
  91. virtual ~ir_instruction()
  92. {
  93. }
  94. /** ir_print_visitor helper for debugging. */
  95. void print(void) const;
  96. void fprint(FILE *f) const;
  97. virtual void accept(ir_visitor *) = 0;
  98. virtual ir_visitor_status accept(ir_hierarchical_visitor *) = 0;
  99. virtual ir_instruction *clone(void *mem_ctx,
  100. struct hash_table *ht) const = 0;
  101. bool is_rvalue() const
  102. {
  103. return ir_type == ir_type_dereference_array ||
  104. ir_type == ir_type_dereference_record ||
  105. ir_type == ir_type_dereference_variable ||
  106. ir_type == ir_type_constant ||
  107. ir_type == ir_type_expression ||
  108. ir_type == ir_type_swizzle ||
  109. ir_type == ir_type_texture;
  110. }
  111. bool is_dereference() const
  112. {
  113. return ir_type == ir_type_dereference_array ||
  114. ir_type == ir_type_dereference_record ||
  115. ir_type == ir_type_dereference_variable;
  116. }
  117. bool is_jump() const
  118. {
  119. return ir_type == ir_type_loop_jump ||
  120. ir_type == ir_type_return ||
  121. ir_type == ir_type_discard;
  122. }
  123. /**
  124. * \name IR instruction downcast functions
  125. *
  126. * These functions either cast the object to a derived class or return
  127. * \c NULL if the object's type does not match the specified derived class.
  128. * Additional downcast functions will be added as needed.
  129. */
  130. /*@{*/
  131. #define AS_BASE(TYPE) \
  132. class ir_##TYPE *as_##TYPE() \
  133. { \
  134. assume(this != NULL); \
  135. return is_##TYPE() ? (ir_##TYPE *) this : NULL; \
  136. } \
  137. const class ir_##TYPE *as_##TYPE() const \
  138. { \
  139. assume(this != NULL); \
  140. return is_##TYPE() ? (ir_##TYPE *) this : NULL; \
  141. }
  142. AS_BASE(rvalue)
  143. AS_BASE(dereference)
  144. AS_BASE(jump)
  145. #undef AS_BASE
  146. #define AS_CHILD(TYPE) \
  147. class ir_##TYPE * as_##TYPE() \
  148. { \
  149. assume(this != NULL); \
  150. return ir_type == ir_type_##TYPE ? (ir_##TYPE *) this : NULL; \
  151. } \
  152. const class ir_##TYPE * as_##TYPE() const \
  153. { \
  154. assume(this != NULL); \
  155. return ir_type == ir_type_##TYPE ? (const ir_##TYPE *) this : NULL; \
  156. }
  157. AS_CHILD(variable)
  158. AS_CHILD(function)
  159. AS_CHILD(dereference_array)
  160. AS_CHILD(dereference_variable)
  161. AS_CHILD(dereference_record)
  162. AS_CHILD(expression)
  163. AS_CHILD(loop)
  164. AS_CHILD(assignment)
  165. AS_CHILD(call)
  166. AS_CHILD(return)
  167. AS_CHILD(if)
  168. AS_CHILD(swizzle)
  169. AS_CHILD(texture)
  170. AS_CHILD(constant)
  171. AS_CHILD(discard)
  172. #undef AS_CHILD
  173. /*@}*/
  174. /**
  175. * IR equality method: Return true if the referenced instruction would
  176. * return the same value as this one.
  177. *
  178. * This intended to be used for CSE and algebraic optimizations, on rvalues
  179. * in particular. No support for other instruction types (assignments,
  180. * jumps, calls, etc.) is planned.
  181. */
  182. virtual bool equals(const ir_instruction *ir,
  183. enum ir_node_type ignore = ir_type_unset) const;
  184. protected:
  185. ir_instruction(enum ir_node_type t)
  186. : ir_type(t)
  187. {
  188. }
  189. private:
  190. ir_instruction()
  191. {
  192. assert(!"Should not get here.");
  193. }
  194. };
  195. /**
  196. * The base class for all "values"/expression trees.
  197. */
  198. class ir_rvalue : public ir_instruction {
  199. public:
  200. const struct glsl_type *type;
  201. virtual ir_rvalue *clone(void *mem_ctx, struct hash_table *) const;
  202. virtual void accept(ir_visitor *v)
  203. {
  204. v->visit(this);
  205. }
  206. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  207. virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
  208. ir_rvalue *as_rvalue_to_saturate();
  209. virtual bool is_lvalue() const
  210. {
  211. return false;
  212. }
  213. /**
  214. * Get the variable that is ultimately referenced by an r-value
  215. */
  216. virtual ir_variable *variable_referenced() const
  217. {
  218. return NULL;
  219. }
  220. /**
  221. * If an r-value is a reference to a whole variable, get that variable
  222. *
  223. * \return
  224. * Pointer to a variable that is completely dereferenced by the r-value. If
  225. * the r-value is not a dereference or the dereference does not access the
  226. * entire variable (i.e., it's just one array element, struct field), \c NULL
  227. * is returned.
  228. */
  229. virtual ir_variable *whole_variable_referenced()
  230. {
  231. return NULL;
  232. }
  233. /**
  234. * Determine if an r-value has the value zero
  235. *
  236. * The base implementation of this function always returns \c false. The
  237. * \c ir_constant class over-rides this function to return \c true \b only
  238. * for vector and scalar types that have all elements set to the value
  239. * zero (or \c false for booleans).
  240. *
  241. * \sa ir_constant::has_value, ir_rvalue::is_one, ir_rvalue::is_negative_one
  242. */
  243. virtual bool is_zero() const;
  244. /**
  245. * Determine if an r-value has the value one
  246. *
  247. * The base implementation of this function always returns \c false. The
  248. * \c ir_constant class over-rides this function to return \c true \b only
  249. * for vector and scalar types that have all elements set to the value
  250. * one (or \c true for booleans).
  251. *
  252. * \sa ir_constant::has_value, ir_rvalue::is_zero, ir_rvalue::is_negative_one
  253. */
  254. virtual bool is_one() const;
  255. /**
  256. * Determine if an r-value has the value negative one
  257. *
  258. * The base implementation of this function always returns \c false. The
  259. * \c ir_constant class over-rides this function to return \c true \b only
  260. * for vector and scalar types that have all elements set to the value
  261. * negative one. For boolean types, the result is always \c false.
  262. *
  263. * \sa ir_constant::has_value, ir_rvalue::is_zero, ir_rvalue::is_one
  264. */
  265. virtual bool is_negative_one() const;
  266. /**
  267. * Determine if an r-value is an unsigned integer constant which can be
  268. * stored in 16 bits.
  269. *
  270. * \sa ir_constant::is_uint16_constant.
  271. */
  272. virtual bool is_uint16_constant() const { return false; }
  273. /**
  274. * Return a generic value of error_type.
  275. *
  276. * Allocation will be performed with 'mem_ctx' as ralloc owner.
  277. */
  278. static ir_rvalue *error_value(void *mem_ctx);
  279. protected:
  280. ir_rvalue(enum ir_node_type t);
  281. };
  282. /**
  283. * Variable storage classes
  284. */
  285. enum ir_variable_mode {
  286. ir_var_auto = 0, /**< Function local variables and globals. */
  287. ir_var_uniform, /**< Variable declared as a uniform. */
  288. ir_var_shader_storage, /**< Variable declared as an ssbo. */
  289. ir_var_shader_in,
  290. ir_var_shader_out,
  291. ir_var_function_in,
  292. ir_var_function_out,
  293. ir_var_function_inout,
  294. ir_var_const_in, /**< "in" param that must be a constant expression */
  295. ir_var_system_value, /**< Ex: front-face, instance-id, etc. */
  296. ir_var_temporary, /**< Temporary variable generated during compilation. */
  297. ir_var_mode_count /**< Number of variable modes */
  298. };
  299. /**
  300. * Enum keeping track of how a variable was declared. For error checking of
  301. * the gl_PerVertex redeclaration rules.
  302. */
  303. enum ir_var_declaration_type {
  304. /**
  305. * Normal declaration (for most variables, this means an explicit
  306. * declaration. Exception: temporaries are always implicitly declared, but
  307. * they still use ir_var_declared_normally).
  308. *
  309. * Note: an ir_variable that represents a named interface block uses
  310. * ir_var_declared_normally.
  311. */
  312. ir_var_declared_normally = 0,
  313. /**
  314. * Variable was explicitly declared (or re-declared) in an unnamed
  315. * interface block.
  316. */
  317. ir_var_declared_in_block,
  318. /**
  319. * Variable is an implicitly declared built-in that has not been explicitly
  320. * re-declared by the shader.
  321. */
  322. ir_var_declared_implicitly,
  323. /**
  324. * Variable is implicitly generated by the compiler and should not be
  325. * visible via the API.
  326. */
  327. ir_var_hidden,
  328. };
  329. /**
  330. * \brief Layout qualifiers for gl_FragDepth.
  331. *
  332. * The AMD/ARB_conservative_depth extensions allow gl_FragDepth to be redeclared
  333. * with a layout qualifier.
  334. */
  335. enum ir_depth_layout {
  336. ir_depth_layout_none, /**< No depth layout is specified. */
  337. ir_depth_layout_any,
  338. ir_depth_layout_greater,
  339. ir_depth_layout_less,
  340. ir_depth_layout_unchanged
  341. };
  342. /**
  343. * \brief Convert depth layout qualifier to string.
  344. */
  345. const char*
  346. depth_layout_string(ir_depth_layout layout);
  347. /**
  348. * Description of built-in state associated with a uniform
  349. *
  350. * \sa ir_variable::state_slots
  351. */
  352. struct ir_state_slot {
  353. int tokens[5];
  354. int swizzle;
  355. };
  356. /**
  357. * Get the string value for an interpolation qualifier
  358. *
  359. * \return The string that would be used in a shader to specify \c
  360. * mode will be returned.
  361. *
  362. * This function is used to generate error messages of the form "shader
  363. * uses %s interpolation qualifier", so in the case where there is no
  364. * interpolation qualifier, it returns "no".
  365. *
  366. * This function should only be used on a shader input or output variable.
  367. */
  368. const char *interpolation_string(unsigned interpolation);
  369. class ir_variable : public ir_instruction {
  370. public:
  371. ir_variable(const struct glsl_type *, const char *, ir_variable_mode);
  372. virtual ir_variable *clone(void *mem_ctx, struct hash_table *ht) const;
  373. virtual void accept(ir_visitor *v)
  374. {
  375. v->visit(this);
  376. }
  377. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  378. /**
  379. * Determine how this variable should be interpolated based on its
  380. * interpolation qualifier (if present), whether it is gl_Color or
  381. * gl_SecondaryColor, and whether flatshading is enabled in the current GL
  382. * state.
  383. *
  384. * The return value will always be either INTERP_QUALIFIER_SMOOTH,
  385. * INTERP_QUALIFIER_NOPERSPECTIVE, or INTERP_QUALIFIER_FLAT.
  386. */
  387. glsl_interp_qualifier determine_interpolation_mode(bool flat_shade);
  388. /**
  389. * Determine whether or not a variable is part of a uniform or
  390. * shader storage block.
  391. */
  392. inline bool is_in_buffer_block() const
  393. {
  394. return (this->data.mode == ir_var_uniform ||
  395. this->data.mode == ir_var_shader_storage) &&
  396. this->interface_type != NULL;
  397. }
  398. /**
  399. * Determine whether or not a variable is the declaration of an interface
  400. * block
  401. *
  402. * For the first declaration below, there will be an \c ir_variable named
  403. * "instance" whose type and whose instance_type will be the same
  404. * \cglsl_type. For the second declaration, there will be an \c ir_variable
  405. * named "f" whose type is float and whose instance_type is B2.
  406. *
  407. * "instance" is an interface instance variable, but "f" is not.
  408. *
  409. * uniform B1 {
  410. * float f;
  411. * } instance;
  412. *
  413. * uniform B2 {
  414. * float f;
  415. * };
  416. */
  417. inline bool is_interface_instance() const
  418. {
  419. return this->type->without_array() == this->interface_type;
  420. }
  421. /**
  422. * Set this->interface_type on a newly created variable.
  423. */
  424. void init_interface_type(const struct glsl_type *type)
  425. {
  426. assert(this->interface_type == NULL);
  427. this->interface_type = type;
  428. if (this->is_interface_instance()) {
  429. this->u.max_ifc_array_access =
  430. rzalloc_array(this, unsigned, type->length);
  431. }
  432. }
  433. /**
  434. * Change this->interface_type on a variable that previously had a
  435. * different, but compatible, interface_type. This is used during linking
  436. * to set the size of arrays in interface blocks.
  437. */
  438. void change_interface_type(const struct glsl_type *type)
  439. {
  440. if (this->u.max_ifc_array_access != NULL) {
  441. /* max_ifc_array_access has already been allocated, so make sure the
  442. * new interface has the same number of fields as the old one.
  443. */
  444. assert(this->interface_type->length == type->length);
  445. }
  446. this->interface_type = type;
  447. }
  448. /**
  449. * Change this->interface_type on a variable that previously had a
  450. * different, and incompatible, interface_type. This is used during
  451. * compilation to handle redeclaration of the built-in gl_PerVertex
  452. * interface block.
  453. */
  454. void reinit_interface_type(const struct glsl_type *type)
  455. {
  456. if (this->u.max_ifc_array_access != NULL) {
  457. #ifndef NDEBUG
  458. /* Redeclaring gl_PerVertex is only allowed if none of the built-ins
  459. * it defines have been accessed yet; so it's safe to throw away the
  460. * old max_ifc_array_access pointer, since all of its values are
  461. * zero.
  462. */
  463. for (unsigned i = 0; i < this->interface_type->length; i++)
  464. assert(this->u.max_ifc_array_access[i] == 0);
  465. #endif
  466. ralloc_free(this->u.max_ifc_array_access);
  467. this->u.max_ifc_array_access = NULL;
  468. }
  469. this->interface_type = NULL;
  470. init_interface_type(type);
  471. }
  472. const glsl_type *get_interface_type() const
  473. {
  474. return this->interface_type;
  475. }
  476. /**
  477. * Get the max_ifc_array_access pointer
  478. *
  479. * A "set" function is not needed because the array is dynmically allocated
  480. * as necessary.
  481. */
  482. inline unsigned *get_max_ifc_array_access()
  483. {
  484. assert(this->data._num_state_slots == 0);
  485. return this->u.max_ifc_array_access;
  486. }
  487. inline unsigned get_num_state_slots() const
  488. {
  489. assert(!this->is_interface_instance()
  490. || this->data._num_state_slots == 0);
  491. return this->data._num_state_slots;
  492. }
  493. inline void set_num_state_slots(unsigned n)
  494. {
  495. assert(!this->is_interface_instance()
  496. || n == 0);
  497. this->data._num_state_slots = n;
  498. }
  499. inline ir_state_slot *get_state_slots()
  500. {
  501. return this->is_interface_instance() ? NULL : this->u.state_slots;
  502. }
  503. inline const ir_state_slot *get_state_slots() const
  504. {
  505. return this->is_interface_instance() ? NULL : this->u.state_slots;
  506. }
  507. inline ir_state_slot *allocate_state_slots(unsigned n)
  508. {
  509. assert(!this->is_interface_instance());
  510. this->u.state_slots = ralloc_array(this, ir_state_slot, n);
  511. this->data._num_state_slots = 0;
  512. if (this->u.state_slots != NULL)
  513. this->data._num_state_slots = n;
  514. return this->u.state_slots;
  515. }
  516. inline bool is_name_ralloced() const
  517. {
  518. return this->name != ir_variable::tmp_name;
  519. }
  520. /**
  521. * Enable emitting extension warnings for this variable
  522. */
  523. void enable_extension_warning(const char *extension);
  524. /**
  525. * Get the extension warning string for this variable
  526. *
  527. * If warnings are not enabled, \c NULL is returned.
  528. */
  529. const char *get_extension_warning() const;
  530. /**
  531. * Declared type of the variable
  532. */
  533. const struct glsl_type *type;
  534. /**
  535. * Declared name of the variable
  536. */
  537. const char *name;
  538. struct ir_variable_data {
  539. /**
  540. * Is the variable read-only?
  541. *
  542. * This is set for variables declared as \c const, shader inputs,
  543. * and uniforms.
  544. */
  545. unsigned read_only:1;
  546. unsigned centroid:1;
  547. unsigned sample:1;
  548. unsigned patch:1;
  549. unsigned invariant:1;
  550. unsigned precise:1;
  551. /**
  552. * Has this variable been used for reading or writing?
  553. *
  554. * Several GLSL semantic checks require knowledge of whether or not a
  555. * variable has been used. For example, it is an error to redeclare a
  556. * variable as invariant after it has been used.
  557. *
  558. * This is only maintained in the ast_to_hir.cpp path, not in
  559. * Mesa's fixed function or ARB program paths.
  560. */
  561. unsigned used:1;
  562. /**
  563. * Has this variable been statically assigned?
  564. *
  565. * This answers whether the variable was assigned in any path of
  566. * the shader during ast_to_hir. This doesn't answer whether it is
  567. * still written after dead code removal, nor is it maintained in
  568. * non-ast_to_hir.cpp (GLSL parsing) paths.
  569. */
  570. unsigned assigned:1;
  571. /**
  572. * Enum indicating how the variable was declared. See
  573. * ir_var_declaration_type.
  574. *
  575. * This is used to detect certain kinds of illegal variable redeclarations.
  576. */
  577. unsigned how_declared:2;
  578. /**
  579. * Storage class of the variable.
  580. *
  581. * \sa ir_variable_mode
  582. */
  583. unsigned mode:4;
  584. /**
  585. * Interpolation mode for shader inputs / outputs
  586. *
  587. * \sa ir_variable_interpolation
  588. */
  589. unsigned interpolation:2;
  590. /**
  591. * \name ARB_fragment_coord_conventions
  592. * @{
  593. */
  594. unsigned origin_upper_left:1;
  595. unsigned pixel_center_integer:1;
  596. /*@}*/
  597. /**
  598. * Was the location explicitly set in the shader?
  599. *
  600. * If the location is explicitly set in the shader, it \b cannot be changed
  601. * by the linker or by the API (e.g., calls to \c glBindAttribLocation have
  602. * no effect).
  603. */
  604. unsigned explicit_location:1;
  605. unsigned explicit_index:1;
  606. /**
  607. * Was an initial binding explicitly set in the shader?
  608. *
  609. * If so, constant_value contains an integer ir_constant representing the
  610. * initial binding point.
  611. */
  612. unsigned explicit_binding:1;
  613. /**
  614. * Does this variable have an initializer?
  615. *
  616. * This is used by the linker to cross-validiate initializers of global
  617. * variables.
  618. */
  619. unsigned has_initializer:1;
  620. /**
  621. * Is this variable a generic output or input that has not yet been matched
  622. * up to a variable in another stage of the pipeline?
  623. *
  624. * This is used by the linker as scratch storage while assigning locations
  625. * to generic inputs and outputs.
  626. */
  627. unsigned is_unmatched_generic_inout:1;
  628. /**
  629. * If non-zero, then this variable may be packed along with other variables
  630. * into a single varying slot, so this offset should be applied when
  631. * accessing components. For example, an offset of 1 means that the x
  632. * component of this variable is actually stored in component y of the
  633. * location specified by \c location.
  634. */
  635. unsigned location_frac:2;
  636. /**
  637. * Layout of the matrix. Uses glsl_matrix_layout values.
  638. */
  639. unsigned matrix_layout:2;
  640. /**
  641. * Non-zero if this variable was created by lowering a named interface
  642. * block which was not an array.
  643. *
  644. * Note that this variable and \c from_named_ifc_block_array will never
  645. * both be non-zero.
  646. */
  647. unsigned from_named_ifc_block_nonarray:1;
  648. /**
  649. * Non-zero if this variable was created by lowering a named interface
  650. * block which was an array.
  651. *
  652. * Note that this variable and \c from_named_ifc_block_nonarray will never
  653. * both be non-zero.
  654. */
  655. unsigned from_named_ifc_block_array:1;
  656. /**
  657. * Non-zero if the variable must be a shader input. This is useful for
  658. * constraints on function parameters.
  659. */
  660. unsigned must_be_shader_input:1;
  661. /**
  662. * Output index for dual source blending.
  663. *
  664. * \note
  665. * The GLSL spec only allows the values 0 or 1 for the index in \b dual
  666. * source blending.
  667. */
  668. unsigned index:1;
  669. /**
  670. * \brief Layout qualifier for gl_FragDepth.
  671. *
  672. * This is not equal to \c ir_depth_layout_none if and only if this
  673. * variable is \c gl_FragDepth and a layout qualifier is specified.
  674. */
  675. ir_depth_layout depth_layout:3;
  676. /**
  677. * ARB_shader_image_load_store qualifiers.
  678. */
  679. unsigned image_read_only:1; /**< "readonly" qualifier. */
  680. unsigned image_write_only:1; /**< "writeonly" qualifier. */
  681. unsigned image_coherent:1;
  682. unsigned image_volatile:1;
  683. unsigned image_restrict:1;
  684. /**
  685. * Emit a warning if this variable is accessed.
  686. */
  687. private:
  688. uint8_t warn_extension_index;
  689. public:
  690. /** Image internal format if specified explicitly, otherwise GL_NONE. */
  691. uint16_t image_format;
  692. private:
  693. /**
  694. * Number of state slots used
  695. *
  696. * \note
  697. * This could be stored in as few as 7-bits, if necessary. If it is made
  698. * smaller, add an assertion to \c ir_variable::allocate_state_slots to
  699. * be safe.
  700. */
  701. uint16_t _num_state_slots;
  702. public:
  703. /**
  704. * Initial binding point for a sampler, atomic, or UBO.
  705. *
  706. * For array types, this represents the binding point for the first element.
  707. */
  708. int16_t binding;
  709. /**
  710. * Storage location of the base of this variable
  711. *
  712. * The precise meaning of this field depends on the nature of the variable.
  713. *
  714. * - Vertex shader input: one of the values from \c gl_vert_attrib.
  715. * - Vertex shader output: one of the values from \c gl_varying_slot.
  716. * - Geometry shader input: one of the values from \c gl_varying_slot.
  717. * - Geometry shader output: one of the values from \c gl_varying_slot.
  718. * - Fragment shader input: one of the values from \c gl_varying_slot.
  719. * - Fragment shader output: one of the values from \c gl_frag_result.
  720. * - Uniforms: Per-stage uniform slot number for default uniform block.
  721. * - Uniforms: Index within the uniform block definition for UBO members.
  722. * - Other: This field is not currently used.
  723. *
  724. * If the variable is a uniform, shader input, or shader output, and the
  725. * slot has not been assigned, the value will be -1.
  726. */
  727. int location;
  728. /**
  729. * Vertex stream output identifier.
  730. */
  731. unsigned stream;
  732. /**
  733. * Location an atomic counter is stored at.
  734. */
  735. struct {
  736. unsigned offset;
  737. } atomic;
  738. /**
  739. * Highest element accessed with a constant expression array index
  740. *
  741. * Not used for non-array variables.
  742. */
  743. unsigned max_array_access;
  744. /**
  745. * Allow (only) ir_variable direct access private members.
  746. */
  747. friend class ir_variable;
  748. } data;
  749. /**
  750. * Value assigned in the initializer of a variable declared "const"
  751. */
  752. ir_constant *constant_value;
  753. /**
  754. * Constant expression assigned in the initializer of the variable
  755. *
  756. * \warning
  757. * This field and \c ::constant_value are distinct. Even if the two fields
  758. * refer to constants with the same value, they must point to separate
  759. * objects.
  760. */
  761. ir_constant *constant_initializer;
  762. private:
  763. static const char *const warn_extension_table[];
  764. union {
  765. /**
  766. * For variables which satisfy the is_interface_instance() predicate,
  767. * this points to an array of integers such that if the ith member of
  768. * the interface block is an array, max_ifc_array_access[i] is the
  769. * maximum array element of that member that has been accessed. If the
  770. * ith member of the interface block is not an array,
  771. * max_ifc_array_access[i] is unused.
  772. *
  773. * For variables whose type is not an interface block, this pointer is
  774. * NULL.
  775. */
  776. unsigned *max_ifc_array_access;
  777. /**
  778. * Built-in state that backs this uniform
  779. *
  780. * Once set at variable creation, \c state_slots must remain invariant.
  781. *
  782. * If the variable is not a uniform, \c _num_state_slots will be zero
  783. * and \c state_slots will be \c NULL.
  784. */
  785. ir_state_slot *state_slots;
  786. } u;
  787. /**
  788. * For variables that are in an interface block or are an instance of an
  789. * interface block, this is the \c GLSL_TYPE_INTERFACE type for that block.
  790. *
  791. * \sa ir_variable::location
  792. */
  793. const glsl_type *interface_type;
  794. /**
  795. * Name used for anonymous compiler temporaries
  796. */
  797. static const char tmp_name[];
  798. public:
  799. /**
  800. * Should the construct keep names for ir_var_temporary variables?
  801. *
  802. * When this global is false, names passed to the constructor for
  803. * \c ir_var_temporary variables will be dropped. Instead, the variable will
  804. * be named "compiler_temp". This name will be in static storage.
  805. *
  806. * \warning
  807. * \b NEVER change the mode of an \c ir_var_temporary.
  808. *
  809. * \warning
  810. * This variable is \b not thread-safe. It is global, \b not
  811. * per-context. It begins life false. A context can, at some point, make
  812. * it true. From that point on, it will be true forever. This should be
  813. * okay since it will only be set true while debugging.
  814. */
  815. static bool temporaries_allocate_names;
  816. };
  817. /**
  818. * A function that returns whether a built-in function is available in the
  819. * current shading language (based on version, ES or desktop, and extensions).
  820. */
  821. typedef bool (*builtin_available_predicate)(const _mesa_glsl_parse_state *);
  822. /*@{*/
  823. /**
  824. * The representation of a function instance; may be the full definition or
  825. * simply a prototype.
  826. */
  827. class ir_function_signature : public ir_instruction {
  828. /* An ir_function_signature will be part of the list of signatures in
  829. * an ir_function.
  830. */
  831. public:
  832. ir_function_signature(const glsl_type *return_type,
  833. builtin_available_predicate builtin_avail = NULL);
  834. virtual ir_function_signature *clone(void *mem_ctx,
  835. struct hash_table *ht) const;
  836. ir_function_signature *clone_prototype(void *mem_ctx,
  837. struct hash_table *ht) const;
  838. virtual void accept(ir_visitor *v)
  839. {
  840. v->visit(this);
  841. }
  842. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  843. /**
  844. * Attempt to evaluate this function as a constant expression,
  845. * given a list of the actual parameters and the variable context.
  846. * Returns NULL for non-built-ins.
  847. */
  848. ir_constant *constant_expression_value(exec_list *actual_parameters, struct hash_table *variable_context);
  849. /**
  850. * Get the name of the function for which this is a signature
  851. */
  852. const char *function_name() const;
  853. /**
  854. * Get a handle to the function for which this is a signature
  855. *
  856. * There is no setter function, this function returns a \c const pointer,
  857. * and \c ir_function_signature::_function is private for a reason. The
  858. * only way to make a connection between a function and function signature
  859. * is via \c ir_function::add_signature. This helps ensure that certain
  860. * invariants (i.e., a function signature is in the list of signatures for
  861. * its \c _function) are met.
  862. *
  863. * \sa ir_function::add_signature
  864. */
  865. inline const class ir_function *function() const
  866. {
  867. return this->_function;
  868. }
  869. /**
  870. * Check whether the qualifiers match between this signature's parameters
  871. * and the supplied parameter list. If not, returns the name of the first
  872. * parameter with mismatched qualifiers (for use in error messages).
  873. */
  874. const char *qualifiers_match(exec_list *params);
  875. /**
  876. * Replace the current parameter list with the given one. This is useful
  877. * if the current information came from a prototype, and either has invalid
  878. * or missing parameter names.
  879. */
  880. void replace_parameters(exec_list *new_params);
  881. /**
  882. * Function return type.
  883. *
  884. * \note This discards the optional precision qualifier.
  885. */
  886. const struct glsl_type *return_type;
  887. /**
  888. * List of ir_variable of function parameters.
  889. *
  890. * This represents the storage. The paramaters passed in a particular
  891. * call will be in ir_call::actual_paramaters.
  892. */
  893. struct exec_list parameters;
  894. /** Whether or not this function has a body (which may be empty). */
  895. unsigned is_defined:1;
  896. /** Whether or not this function signature is a built-in. */
  897. bool is_builtin() const;
  898. /**
  899. * Whether or not this function is an intrinsic to be implemented
  900. * by the driver.
  901. */
  902. bool is_intrinsic;
  903. /** Whether or not a built-in is available for this shader. */
  904. bool is_builtin_available(const _mesa_glsl_parse_state *state) const;
  905. /** Body of instructions in the function. */
  906. struct exec_list body;
  907. private:
  908. /**
  909. * A function pointer to a predicate that answers whether a built-in
  910. * function is available in the current shader. NULL if not a built-in.
  911. */
  912. builtin_available_predicate builtin_avail;
  913. /** Function of which this signature is one overload. */
  914. class ir_function *_function;
  915. /** Function signature of which this one is a prototype clone */
  916. const ir_function_signature *origin;
  917. friend class ir_function;
  918. /**
  919. * Helper function to run a list of instructions for constant
  920. * expression evaluation.
  921. *
  922. * The hash table represents the values of the visible variables.
  923. * There are no scoping issues because the table is indexed on
  924. * ir_variable pointers, not variable names.
  925. *
  926. * Returns false if the expression is not constant, true otherwise,
  927. * and the value in *result if result is non-NULL.
  928. */
  929. bool constant_expression_evaluate_expression_list(const struct exec_list &body,
  930. struct hash_table *variable_context,
  931. ir_constant **result);
  932. };
  933. /**
  934. * Header for tracking multiple overloaded functions with the same name.
  935. * Contains a list of ir_function_signatures representing each of the
  936. * actual functions.
  937. */
  938. class ir_function : public ir_instruction {
  939. public:
  940. ir_function(const char *name);
  941. virtual ir_function *clone(void *mem_ctx, struct hash_table *ht) const;
  942. virtual void accept(ir_visitor *v)
  943. {
  944. v->visit(this);
  945. }
  946. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  947. void add_signature(ir_function_signature *sig)
  948. {
  949. sig->_function = this;
  950. this->signatures.push_tail(sig);
  951. }
  952. /**
  953. * Find a signature that matches a set of actual parameters, taking implicit
  954. * conversions into account. Also flags whether the match was exact.
  955. */
  956. ir_function_signature *matching_signature(_mesa_glsl_parse_state *state,
  957. const exec_list *actual_param,
  958. bool allow_builtins,
  959. bool *match_is_exact);
  960. /**
  961. * Find a signature that matches a set of actual parameters, taking implicit
  962. * conversions into account.
  963. */
  964. ir_function_signature *matching_signature(_mesa_glsl_parse_state *state,
  965. const exec_list *actual_param,
  966. bool allow_builtins);
  967. /**
  968. * Find a signature that exactly matches a set of actual parameters without
  969. * any implicit type conversions.
  970. */
  971. ir_function_signature *exact_matching_signature(_mesa_glsl_parse_state *state,
  972. const exec_list *actual_ps);
  973. /**
  974. * Name of the function.
  975. */
  976. const char *name;
  977. /** Whether or not this function has a signature that isn't a built-in. */
  978. bool has_user_signature();
  979. /**
  980. * List of ir_function_signature for each overloaded function with this name.
  981. */
  982. struct exec_list signatures;
  983. };
  984. inline const char *ir_function_signature::function_name() const
  985. {
  986. return this->_function->name;
  987. }
  988. /*@}*/
  989. /**
  990. * IR instruction representing high-level if-statements
  991. */
  992. class ir_if : public ir_instruction {
  993. public:
  994. ir_if(ir_rvalue *condition)
  995. : ir_instruction(ir_type_if), condition(condition)
  996. {
  997. }
  998. virtual ir_if *clone(void *mem_ctx, struct hash_table *ht) const;
  999. virtual void accept(ir_visitor *v)
  1000. {
  1001. v->visit(this);
  1002. }
  1003. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  1004. ir_rvalue *condition;
  1005. /** List of ir_instruction for the body of the then branch */
  1006. exec_list then_instructions;
  1007. /** List of ir_instruction for the body of the else branch */
  1008. exec_list else_instructions;
  1009. };
  1010. /**
  1011. * IR instruction representing a high-level loop structure.
  1012. */
  1013. class ir_loop : public ir_instruction {
  1014. public:
  1015. ir_loop();
  1016. virtual ir_loop *clone(void *mem_ctx, struct hash_table *ht) const;
  1017. virtual void accept(ir_visitor *v)
  1018. {
  1019. v->visit(this);
  1020. }
  1021. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  1022. /** List of ir_instruction that make up the body of the loop. */
  1023. exec_list body_instructions;
  1024. };
  1025. class ir_assignment : public ir_instruction {
  1026. public:
  1027. ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition = NULL);
  1028. /**
  1029. * Construct an assignment with an explicit write mask
  1030. *
  1031. * \note
  1032. * Since a write mask is supplied, the LHS must already be a bare
  1033. * \c ir_dereference. The cannot be any swizzles in the LHS.
  1034. */
  1035. ir_assignment(ir_dereference *lhs, ir_rvalue *rhs, ir_rvalue *condition,
  1036. unsigned write_mask);
  1037. virtual ir_assignment *clone(void *mem_ctx, struct hash_table *ht) const;
  1038. virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
  1039. virtual void accept(ir_visitor *v)
  1040. {
  1041. v->visit(this);
  1042. }
  1043. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  1044. /**
  1045. * Get a whole variable written by an assignment
  1046. *
  1047. * If the LHS of the assignment writes a whole variable, the variable is
  1048. * returned. Otherwise \c NULL is returned. Examples of whole-variable
  1049. * assignment are:
  1050. *
  1051. * - Assigning to a scalar
  1052. * - Assigning to all components of a vector
  1053. * - Whole array (or matrix) assignment
  1054. * - Whole structure assignment
  1055. */
  1056. ir_variable *whole_variable_written();
  1057. /**
  1058. * Set the LHS of an assignment
  1059. */
  1060. void set_lhs(ir_rvalue *lhs);
  1061. /**
  1062. * Left-hand side of the assignment.
  1063. *
  1064. * This should be treated as read only. If you need to set the LHS of an
  1065. * assignment, use \c ir_assignment::set_lhs.
  1066. */
  1067. ir_dereference *lhs;
  1068. /**
  1069. * Value being assigned
  1070. */
  1071. ir_rvalue *rhs;
  1072. /**
  1073. * Optional condition for the assignment.
  1074. */
  1075. ir_rvalue *condition;
  1076. /**
  1077. * Component mask written
  1078. *
  1079. * For non-vector types in the LHS, this field will be zero. For vector
  1080. * types, a bit will be set for each component that is written. Note that
  1081. * for \c vec2 and \c vec3 types only the lower bits will ever be set.
  1082. *
  1083. * A partially-set write mask means that each enabled channel gets
  1084. * the value from a consecutive channel of the rhs. For example,
  1085. * to write just .xyw of gl_FrontColor with color:
  1086. *
  1087. * (assign (constant bool (1)) (xyw)
  1088. * (var_ref gl_FragColor)
  1089. * (swiz xyw (var_ref color)))
  1090. */
  1091. unsigned write_mask:4;
  1092. };
  1093. /* Update ir_expression::get_num_operands() and operator_strs when
  1094. * updating this list.
  1095. */
  1096. enum ir_expression_operation {
  1097. ir_unop_bit_not,
  1098. ir_unop_logic_not,
  1099. ir_unop_neg,
  1100. ir_unop_abs,
  1101. ir_unop_sign,
  1102. ir_unop_rcp,
  1103. ir_unop_rsq,
  1104. ir_unop_sqrt,
  1105. ir_unop_exp, /**< Log base e on gentype */
  1106. ir_unop_log, /**< Natural log on gentype */
  1107. ir_unop_exp2,
  1108. ir_unop_log2,
  1109. ir_unop_f2i, /**< Float-to-integer conversion. */
  1110. ir_unop_f2u, /**< Float-to-unsigned conversion. */
  1111. ir_unop_i2f, /**< Integer-to-float conversion. */
  1112. ir_unop_f2b, /**< Float-to-boolean conversion */
  1113. ir_unop_b2f, /**< Boolean-to-float conversion */
  1114. ir_unop_i2b, /**< int-to-boolean conversion */
  1115. ir_unop_b2i, /**< Boolean-to-int conversion */
  1116. ir_unop_u2f, /**< Unsigned-to-float conversion. */
  1117. ir_unop_i2u, /**< Integer-to-unsigned conversion. */
  1118. ir_unop_u2i, /**< Unsigned-to-integer conversion. */
  1119. ir_unop_d2f, /**< Double-to-float conversion. */
  1120. ir_unop_f2d, /**< Float-to-double conversion. */
  1121. ir_unop_d2i, /**< Double-to-integer conversion. */
  1122. ir_unop_i2d, /**< Integer-to-double conversion. */
  1123. ir_unop_d2u, /**< Double-to-unsigned conversion. */
  1124. ir_unop_u2d, /**< Unsigned-to-double conversion. */
  1125. ir_unop_d2b, /**< Double-to-boolean conversion. */
  1126. ir_unop_bitcast_i2f, /**< Bit-identical int-to-float "conversion" */
  1127. ir_unop_bitcast_f2i, /**< Bit-identical float-to-int "conversion" */
  1128. ir_unop_bitcast_u2f, /**< Bit-identical uint-to-float "conversion" */
  1129. ir_unop_bitcast_f2u, /**< Bit-identical float-to-uint "conversion" */
  1130. ir_unop_any,
  1131. /**
  1132. * \name Unary floating-point rounding operations.
  1133. */
  1134. /*@{*/
  1135. ir_unop_trunc,
  1136. ir_unop_ceil,
  1137. ir_unop_floor,
  1138. ir_unop_fract,
  1139. ir_unop_round_even,
  1140. /*@}*/
  1141. /**
  1142. * \name Trigonometric operations.
  1143. */
  1144. /*@{*/
  1145. ir_unop_sin,
  1146. ir_unop_cos,
  1147. /*@}*/
  1148. /**
  1149. * \name Partial derivatives.
  1150. */
  1151. /*@{*/
  1152. ir_unop_dFdx,
  1153. ir_unop_dFdx_coarse,
  1154. ir_unop_dFdx_fine,
  1155. ir_unop_dFdy,
  1156. ir_unop_dFdy_coarse,
  1157. ir_unop_dFdy_fine,
  1158. /*@}*/
  1159. /**
  1160. * \name Floating point pack and unpack operations.
  1161. */
  1162. /*@{*/
  1163. ir_unop_pack_snorm_2x16,
  1164. ir_unop_pack_snorm_4x8,
  1165. ir_unop_pack_unorm_2x16,
  1166. ir_unop_pack_unorm_4x8,
  1167. ir_unop_pack_half_2x16,
  1168. ir_unop_unpack_snorm_2x16,
  1169. ir_unop_unpack_snorm_4x8,
  1170. ir_unop_unpack_unorm_2x16,
  1171. ir_unop_unpack_unorm_4x8,
  1172. ir_unop_unpack_half_2x16,
  1173. /*@}*/
  1174. /**
  1175. * \name Lowered floating point unpacking operations.
  1176. *
  1177. * \see lower_packing_builtins_visitor::split_unpack_half_2x16
  1178. */
  1179. /*@{*/
  1180. ir_unop_unpack_half_2x16_split_x,
  1181. ir_unop_unpack_half_2x16_split_y,
  1182. /*@}*/
  1183. /**
  1184. * \name Bit operations, part of ARB_gpu_shader5.
  1185. */
  1186. /*@{*/
  1187. ir_unop_bitfield_reverse,
  1188. ir_unop_bit_count,
  1189. ir_unop_find_msb,
  1190. ir_unop_find_lsb,
  1191. /*@}*/
  1192. ir_unop_saturate,
  1193. /**
  1194. * \name Double packing, part of ARB_gpu_shader_fp64.
  1195. */
  1196. /*@{*/
  1197. ir_unop_pack_double_2x32,
  1198. ir_unop_unpack_double_2x32,
  1199. /*@}*/
  1200. ir_unop_frexp_sig,
  1201. ir_unop_frexp_exp,
  1202. ir_unop_noise,
  1203. /**
  1204. * Interpolate fs input at centroid
  1205. *
  1206. * operand0 is the fs input.
  1207. */
  1208. ir_unop_interpolate_at_centroid,
  1209. /**
  1210. * A sentinel marking the last of the unary operations.
  1211. */
  1212. ir_last_unop = ir_unop_interpolate_at_centroid,
  1213. ir_binop_add,
  1214. ir_binop_sub,
  1215. ir_binop_mul, /**< Floating-point or low 32-bit integer multiply. */
  1216. ir_binop_imul_high, /**< Calculates the high 32-bits of a 64-bit multiply. */
  1217. ir_binop_div,
  1218. /**
  1219. * Returns the carry resulting from the addition of the two arguments.
  1220. */
  1221. /*@{*/
  1222. ir_binop_carry,
  1223. /*@}*/
  1224. /**
  1225. * Returns the borrow resulting from the subtraction of the second argument
  1226. * from the first argument.
  1227. */
  1228. /*@{*/
  1229. ir_binop_borrow,
  1230. /*@}*/
  1231. /**
  1232. * Takes one of two combinations of arguments:
  1233. *
  1234. * - mod(vecN, vecN)
  1235. * - mod(vecN, float)
  1236. *
  1237. * Does not take integer types.
  1238. */
  1239. ir_binop_mod,
  1240. /**
  1241. * \name Binary comparison operators which return a boolean vector.
  1242. * The type of both operands must be equal.
  1243. */
  1244. /*@{*/
  1245. ir_binop_less,
  1246. ir_binop_greater,
  1247. ir_binop_lequal,
  1248. ir_binop_gequal,
  1249. ir_binop_equal,
  1250. ir_binop_nequal,
  1251. /**
  1252. * Returns single boolean for whether all components of operands[0]
  1253. * equal the components of operands[1].
  1254. */
  1255. ir_binop_all_equal,
  1256. /**
  1257. * Returns single boolean for whether any component of operands[0]
  1258. * is not equal to the corresponding component of operands[1].
  1259. */
  1260. ir_binop_any_nequal,
  1261. /*@}*/
  1262. /**
  1263. * \name Bit-wise binary operations.
  1264. */
  1265. /*@{*/
  1266. ir_binop_lshift,
  1267. ir_binop_rshift,
  1268. ir_binop_bit_and,
  1269. ir_binop_bit_xor,
  1270. ir_binop_bit_or,
  1271. /*@}*/
  1272. ir_binop_logic_and,
  1273. ir_binop_logic_xor,
  1274. ir_binop_logic_or,
  1275. ir_binop_dot,
  1276. ir_binop_min,
  1277. ir_binop_max,
  1278. ir_binop_pow,
  1279. /**
  1280. * \name Lowered floating point packing operations.
  1281. *
  1282. * \see lower_packing_builtins_visitor::split_pack_half_2x16
  1283. */
  1284. /*@{*/
  1285. ir_binop_pack_half_2x16_split,
  1286. /*@}*/
  1287. /**
  1288. * \name First half of a lowered bitfieldInsert() operation.
  1289. *
  1290. * \see lower_instructions::bitfield_insert_to_bfm_bfi
  1291. */
  1292. /*@{*/
  1293. ir_binop_bfm,
  1294. /*@}*/
  1295. /**
  1296. * Load a value the size of a given GLSL type from a uniform block.
  1297. *
  1298. * operand0 is the ir_constant uniform block index in the linked shader.
  1299. * operand1 is a byte offset within the uniform block.
  1300. */
  1301. ir_binop_ubo_load,
  1302. /**
  1303. * \name Multiplies a number by two to a power, part of ARB_gpu_shader5.
  1304. */
  1305. /*@{*/
  1306. ir_binop_ldexp,
  1307. /*@}*/
  1308. /**
  1309. * Extract a scalar from a vector
  1310. *
  1311. * operand0 is the vector
  1312. * operand1 is the index of the field to read from operand0
  1313. */
  1314. ir_binop_vector_extract,
  1315. /**
  1316. * Interpolate fs input at offset
  1317. *
  1318. * operand0 is the fs input
  1319. * operand1 is the offset from the pixel center
  1320. */
  1321. ir_binop_interpolate_at_offset,
  1322. /**
  1323. * Interpolate fs input at sample position
  1324. *
  1325. * operand0 is the fs input
  1326. * operand1 is the sample ID
  1327. */
  1328. ir_binop_interpolate_at_sample,
  1329. /**
  1330. * A sentinel marking the last of the binary operations.
  1331. */
  1332. ir_last_binop = ir_binop_interpolate_at_sample,
  1333. /**
  1334. * \name Fused floating-point multiply-add, part of ARB_gpu_shader5.
  1335. */
  1336. /*@{*/
  1337. ir_triop_fma,
  1338. /*@}*/
  1339. ir_triop_lrp,
  1340. /**
  1341. * \name Conditional Select
  1342. *
  1343. * A vector conditional select instruction (like ?:, but operating per-
  1344. * component on vectors).
  1345. *
  1346. * \see lower_instructions_visitor::ldexp_to_arith
  1347. */
  1348. /*@{*/
  1349. ir_triop_csel,
  1350. /*@}*/
  1351. /**
  1352. * \name Second half of a lowered bitfieldInsert() operation.
  1353. *
  1354. * \see lower_instructions::bitfield_insert_to_bfm_bfi
  1355. */
  1356. /*@{*/
  1357. ir_triop_bfi,
  1358. /*@}*/
  1359. ir_triop_bitfield_extract,
  1360. /**
  1361. * Generate a value with one field of a vector changed
  1362. *
  1363. * operand0 is the vector
  1364. * operand1 is the value to write into the vector result
  1365. * operand2 is the index in operand0 to be modified
  1366. */
  1367. ir_triop_vector_insert,
  1368. /**
  1369. * A sentinel marking the last of the ternary operations.
  1370. */
  1371. ir_last_triop = ir_triop_vector_insert,
  1372. ir_quadop_bitfield_insert,
  1373. ir_quadop_vector,
  1374. /**
  1375. * A sentinel marking the last of the ternary operations.
  1376. */
  1377. ir_last_quadop = ir_quadop_vector,
  1378. /**
  1379. * A sentinel marking the last of all operations.
  1380. */
  1381. ir_last_opcode = ir_quadop_vector
  1382. };
  1383. class ir_expression : public ir_rvalue {
  1384. public:
  1385. ir_expression(int op, const struct glsl_type *type,
  1386. ir_rvalue *op0, ir_rvalue *op1 = NULL,
  1387. ir_rvalue *op2 = NULL, ir_rvalue *op3 = NULL);
  1388. /**
  1389. * Constructor for unary operation expressions
  1390. */
  1391. ir_expression(int op, ir_rvalue *);
  1392. /**
  1393. * Constructor for binary operation expressions
  1394. */
  1395. ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1);
  1396. /**
  1397. * Constructor for ternary operation expressions
  1398. */
  1399. ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1, ir_rvalue *op2);
  1400. virtual bool equals(const ir_instruction *ir,
  1401. enum ir_node_type ignore = ir_type_unset) const;
  1402. virtual ir_expression *clone(void *mem_ctx, struct hash_table *ht) const;
  1403. /**
  1404. * Attempt to constant-fold the expression
  1405. *
  1406. * The "variable_context" hash table links ir_variable * to ir_constant *
  1407. * that represent the variables' values. \c NULL represents an empty
  1408. * context.
  1409. *
  1410. * If the expression cannot be constant folded, this method will return
  1411. * \c NULL.
  1412. */
  1413. virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
  1414. /**
  1415. * Determine the number of operands used by an expression
  1416. */
  1417. static unsigned int get_num_operands(ir_expression_operation);
  1418. /**
  1419. * Determine the number of operands used by an expression
  1420. */
  1421. unsigned int get_num_operands() const
  1422. {
  1423. return (this->operation == ir_quadop_vector)
  1424. ? this->type->vector_elements : get_num_operands(operation);
  1425. }
  1426. /**
  1427. * Return whether the expression operates on vectors horizontally.
  1428. */
  1429. bool is_horizontal() const
  1430. {
  1431. return operation == ir_binop_all_equal ||
  1432. operation == ir_binop_any_nequal ||
  1433. operation == ir_unop_any ||
  1434. operation == ir_binop_dot ||
  1435. operation == ir_quadop_vector;
  1436. }
  1437. /**
  1438. * Return a string representing this expression's operator.
  1439. */
  1440. const char *operator_string();
  1441. /**
  1442. * Return a string representing this expression's operator.
  1443. */
  1444. static const char *operator_string(ir_expression_operation);
  1445. /**
  1446. * Do a reverse-lookup to translate the given string into an operator.
  1447. */
  1448. static ir_expression_operation get_operator(const char *);
  1449. virtual void accept(ir_visitor *v)
  1450. {
  1451. v->visit(this);
  1452. }
  1453. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  1454. ir_expression_operation operation;
  1455. ir_rvalue *operands[4];
  1456. };
  1457. /**
  1458. * HIR instruction representing a high-level function call, containing a list
  1459. * of parameters and returning a value in the supplied temporary.
  1460. */
  1461. class ir_call : public ir_instruction {
  1462. public:
  1463. ir_call(ir_function_signature *callee,
  1464. ir_dereference_variable *return_deref,
  1465. exec_list *actual_parameters)
  1466. : ir_instruction(ir_type_call), return_deref(return_deref), callee(callee)
  1467. {
  1468. assert(callee->return_type != NULL);
  1469. actual_parameters->move_nodes_to(& this->actual_parameters);
  1470. this->use_builtin = callee->is_builtin();
  1471. }
  1472. virtual ir_call *clone(void *mem_ctx, struct hash_table *ht) const;
  1473. virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
  1474. virtual void accept(ir_visitor *v)
  1475. {
  1476. v->visit(this);
  1477. }
  1478. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  1479. /**
  1480. * Get the name of the function being called.
  1481. */
  1482. const char *callee_name() const
  1483. {
  1484. return callee->function_name();
  1485. }
  1486. /**
  1487. * Generates an inline version of the function before @ir,
  1488. * storing the return value in return_deref.
  1489. */
  1490. void generate_inline(ir_instruction *ir);
  1491. /**
  1492. * Storage for the function's return value.
  1493. * This must be NULL if the return type is void.
  1494. */
  1495. ir_dereference_variable *return_deref;
  1496. /**
  1497. * The specific function signature being called.
  1498. */
  1499. ir_function_signature *callee;
  1500. /* List of ir_rvalue of paramaters passed in this call. */
  1501. exec_list actual_parameters;
  1502. /** Should this call only bind to a built-in function? */
  1503. bool use_builtin;
  1504. };
  1505. /**
  1506. * \name Jump-like IR instructions.
  1507. *
  1508. * These include \c break, \c continue, \c return, and \c discard.
  1509. */
  1510. /*@{*/
  1511. class ir_jump : public ir_instruction {
  1512. protected:
  1513. ir_jump(enum ir_node_type t)
  1514. : ir_instruction(t)
  1515. {
  1516. }
  1517. };
  1518. class ir_return : public ir_jump {
  1519. public:
  1520. ir_return()
  1521. : ir_jump(ir_type_return), value(NULL)
  1522. {
  1523. }
  1524. ir_return(ir_rvalue *value)
  1525. : ir_jump(ir_type_return), value(value)
  1526. {
  1527. }
  1528. virtual ir_return *clone(void *mem_ctx, struct hash_table *) const;
  1529. ir_rvalue *get_value() const
  1530. {
  1531. return value;
  1532. }
  1533. virtual void accept(ir_visitor *v)
  1534. {
  1535. v->visit(this);
  1536. }
  1537. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  1538. ir_rvalue *value;
  1539. };
  1540. /**
  1541. * Jump instructions used inside loops
  1542. *
  1543. * These include \c break and \c continue. The \c break within a loop is
  1544. * different from the \c break within a switch-statement.
  1545. *
  1546. * \sa ir_switch_jump
  1547. */
  1548. class ir_loop_jump : public ir_jump {
  1549. public:
  1550. enum jump_mode {
  1551. jump_break,
  1552. jump_continue
  1553. };
  1554. ir_loop_jump(jump_mode mode)
  1555. : ir_jump(ir_type_loop_jump)
  1556. {
  1557. this->mode = mode;
  1558. }
  1559. virtual ir_loop_jump *clone(void *mem_ctx, struct hash_table *) const;
  1560. virtual void accept(ir_visitor *v)
  1561. {
  1562. v->visit(this);
  1563. }
  1564. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  1565. bool is_break() const
  1566. {
  1567. return mode == jump_break;
  1568. }
  1569. bool is_continue() const
  1570. {
  1571. return mode == jump_continue;
  1572. }
  1573. /** Mode selector for the jump instruction. */
  1574. enum jump_mode mode;
  1575. };
  1576. /**
  1577. * IR instruction representing discard statements.
  1578. */
  1579. class ir_discard : public ir_jump {
  1580. public:
  1581. ir_discard()
  1582. : ir_jump(ir_type_discard)
  1583. {
  1584. this->condition = NULL;
  1585. }
  1586. ir_discard(ir_rvalue *cond)
  1587. : ir_jump(ir_type_discard)
  1588. {
  1589. this->condition = cond;
  1590. }
  1591. virtual ir_discard *clone(void *mem_ctx, struct hash_table *ht) const;
  1592. virtual void accept(ir_visitor *v)
  1593. {
  1594. v->visit(this);
  1595. }
  1596. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  1597. ir_rvalue *condition;
  1598. };
  1599. /*@}*/
  1600. /**
  1601. * Texture sampling opcodes used in ir_texture
  1602. */
  1603. enum ir_texture_opcode {
  1604. ir_tex, /**< Regular texture look-up */
  1605. ir_txb, /**< Texture look-up with LOD bias */
  1606. ir_txl, /**< Texture look-up with explicit LOD */
  1607. ir_txd, /**< Texture look-up with partial derivatvies */
  1608. ir_txf, /**< Texel fetch with explicit LOD */
  1609. ir_txf_ms, /**< Multisample texture fetch */
  1610. ir_txs, /**< Texture size */
  1611. ir_lod, /**< Texture lod query */
  1612. ir_tg4, /**< Texture gather */
  1613. ir_query_levels /**< Texture levels query */
  1614. };
  1615. /**
  1616. * IR instruction to sample a texture
  1617. *
  1618. * The specific form of the IR instruction depends on the \c mode value
  1619. * selected from \c ir_texture_opcodes. In the printed IR, these will
  1620. * appear as:
  1621. *
  1622. * Texel offset (0 or an expression)
  1623. * | Projection divisor
  1624. * | | Shadow comparitor
  1625. * | | |
  1626. * v v v
  1627. * (tex <type> <sampler> <coordinate> 0 1 ( ))
  1628. * (txb <type> <sampler> <coordinate> 0 1 ( ) <bias>)
  1629. * (txl <type> <sampler> <coordinate> 0 1 ( ) <lod>)
  1630. * (txd <type> <sampler> <coordinate> 0 1 ( ) (dPdx dPdy))
  1631. * (txf <type> <sampler> <coordinate> 0 <lod>)
  1632. * (txf_ms
  1633. * <type> <sampler> <coordinate> <sample_index>)
  1634. * (txs <type> <sampler> <lod>)
  1635. * (lod <type> <sampler> <coordinate>)
  1636. * (tg4 <type> <sampler> <coordinate> <offset> <component>)
  1637. * (query_levels <type> <sampler>)
  1638. */
  1639. class ir_texture : public ir_rvalue {
  1640. public:
  1641. ir_texture(enum ir_texture_opcode op)
  1642. : ir_rvalue(ir_type_texture),
  1643. op(op), sampler(NULL), coordinate(NULL), projector(NULL),
  1644. shadow_comparitor(NULL), offset(NULL)
  1645. {
  1646. memset(&lod_info, 0, sizeof(lod_info));
  1647. }
  1648. virtual ir_texture *clone(void *mem_ctx, struct hash_table *) const;
  1649. virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
  1650. virtual void accept(ir_visitor *v)
  1651. {
  1652. v->visit(this);
  1653. }
  1654. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  1655. virtual bool equals(const ir_instruction *ir,
  1656. enum ir_node_type ignore = ir_type_unset) const;
  1657. /**
  1658. * Return a string representing the ir_texture_opcode.
  1659. */
  1660. const char *opcode_string();
  1661. /** Set the sampler and type. */
  1662. void set_sampler(ir_dereference *sampler, const glsl_type *type);
  1663. /**
  1664. * Do a reverse-lookup to translate a string into an ir_texture_opcode.
  1665. */
  1666. static ir_texture_opcode get_opcode(const char *);
  1667. enum ir_texture_opcode op;
  1668. /** Sampler to use for the texture access. */
  1669. ir_dereference *sampler;
  1670. /** Texture coordinate to sample */
  1671. ir_rvalue *coordinate;
  1672. /**
  1673. * Value used for projective divide.
  1674. *
  1675. * If there is no projective divide (the common case), this will be
  1676. * \c NULL. Optimization passes should check for this to point to a constant
  1677. * of 1.0 and replace that with \c NULL.
  1678. */
  1679. ir_rvalue *projector;
  1680. /**
  1681. * Coordinate used for comparison on shadow look-ups.
  1682. *
  1683. * If there is no shadow comparison, this will be \c NULL. For the
  1684. * \c ir_txf opcode, this *must* be \c NULL.
  1685. */
  1686. ir_rvalue *shadow_comparitor;
  1687. /** Texel offset. */
  1688. ir_rvalue *offset;
  1689. union {
  1690. ir_rvalue *lod; /**< Floating point LOD */
  1691. ir_rvalue *bias; /**< Floating point LOD bias */
  1692. ir_rvalue *sample_index; /**< MSAA sample index */
  1693. ir_rvalue *component; /**< Gather component selector */
  1694. struct {
  1695. ir_rvalue *dPdx; /**< Partial derivative of coordinate wrt X */
  1696. ir_rvalue *dPdy; /**< Partial derivative of coordinate wrt Y */
  1697. } grad;
  1698. } lod_info;
  1699. };
  1700. struct ir_swizzle_mask {
  1701. unsigned x:2;
  1702. unsigned y:2;
  1703. unsigned z:2;
  1704. unsigned w:2;
  1705. /**
  1706. * Number of components in the swizzle.
  1707. */
  1708. unsigned num_components:3;
  1709. /**
  1710. * Does the swizzle contain duplicate components?
  1711. *
  1712. * L-value swizzles cannot contain duplicate components.
  1713. */
  1714. unsigned has_duplicates:1;
  1715. };
  1716. class ir_swizzle : public ir_rvalue {
  1717. public:
  1718. ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
  1719. unsigned count);
  1720. ir_swizzle(ir_rvalue *val, const unsigned *components, unsigned count);
  1721. ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
  1722. virtual ir_swizzle *clone(void *mem_ctx, struct hash_table *) const;
  1723. virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
  1724. /**
  1725. * Construct an ir_swizzle from the textual representation. Can fail.
  1726. */
  1727. static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
  1728. virtual void accept(ir_visitor *v)
  1729. {
  1730. v->visit(this);
  1731. }
  1732. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  1733. virtual bool equals(const ir_instruction *ir,
  1734. enum ir_node_type ignore = ir_type_unset) const;
  1735. bool is_lvalue() const
  1736. {
  1737. return val->is_lvalue() && !mask.has_duplicates;
  1738. }
  1739. /**
  1740. * Get the variable that is ultimately referenced by an r-value
  1741. */
  1742. virtual ir_variable *variable_referenced() const;
  1743. ir_rvalue *val;
  1744. ir_swizzle_mask mask;
  1745. private:
  1746. /**
  1747. * Initialize the mask component of a swizzle
  1748. *
  1749. * This is used by the \c ir_swizzle constructors.
  1750. */
  1751. void init_mask(const unsigned *components, unsigned count);
  1752. };
  1753. class ir_dereference : public ir_rvalue {
  1754. public:
  1755. virtual ir_dereference *clone(void *mem_ctx, struct hash_table *) const = 0;
  1756. bool is_lvalue() const;
  1757. /**
  1758. * Get the variable that is ultimately referenced by an r-value
  1759. */
  1760. virtual ir_variable *variable_referenced() const = 0;
  1761. protected:
  1762. ir_dereference(enum ir_node_type t)
  1763. : ir_rvalue(t)
  1764. {
  1765. }
  1766. };
  1767. class ir_dereference_variable : public ir_dereference {
  1768. public:
  1769. ir_dereference_variable(ir_variable *var);
  1770. virtual ir_dereference_variable *clone(void *mem_ctx,
  1771. struct hash_table *) const;
  1772. virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
  1773. virtual bool equals(const ir_instruction *ir,
  1774. enum ir_node_type ignore = ir_type_unset) const;
  1775. /**
  1776. * Get the variable that is ultimately referenced by an r-value
  1777. */
  1778. virtual ir_variable *variable_referenced() const
  1779. {
  1780. return this->var;
  1781. }
  1782. virtual ir_variable *whole_variable_referenced()
  1783. {
  1784. /* ir_dereference_variable objects always dereference the entire
  1785. * variable. However, if this dereference is dereferenced by anything
  1786. * else, the complete deferefernce chain is not a whole-variable
  1787. * dereference. This method should only be called on the top most
  1788. * ir_rvalue in a dereference chain.
  1789. */
  1790. return this->var;
  1791. }
  1792. virtual void accept(ir_visitor *v)
  1793. {
  1794. v->visit(this);
  1795. }
  1796. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  1797. /**
  1798. * Object being dereferenced.
  1799. */
  1800. ir_variable *var;
  1801. };
  1802. class ir_dereference_array : public ir_dereference {
  1803. public:
  1804. ir_dereference_array(ir_rvalue *value, ir_rvalue *array_index);
  1805. ir_dereference_array(ir_variable *var, ir_rvalue *array_index);
  1806. virtual ir_dereference_array *clone(void *mem_ctx,
  1807. struct hash_table *) const;
  1808. virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
  1809. virtual bool equals(const ir_instruction *ir,
  1810. enum ir_node_type ignore = ir_type_unset) const;
  1811. /**
  1812. * Get the variable that is ultimately referenced by an r-value
  1813. */
  1814. virtual ir_variable *variable_referenced() const
  1815. {
  1816. return this->array->variable_referenced();
  1817. }
  1818. virtual void accept(ir_visitor *v)
  1819. {
  1820. v->visit(this);
  1821. }
  1822. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  1823. ir_rvalue *array;
  1824. ir_rvalue *array_index;
  1825. private:
  1826. void set_array(ir_rvalue *value);
  1827. };
  1828. class ir_dereference_record : public ir_dereference {
  1829. public:
  1830. ir_dereference_record(ir_rvalue *value, const char *field);
  1831. ir_dereference_record(ir_variable *var, const char *field);
  1832. virtual ir_dereference_record *clone(void *mem_ctx,
  1833. struct hash_table *) const;
  1834. virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
  1835. /**
  1836. * Get the variable that is ultimately referenced by an r-value
  1837. */
  1838. virtual ir_variable *variable_referenced() const
  1839. {
  1840. return this->record->variable_referenced();
  1841. }
  1842. virtual void accept(ir_visitor *v)
  1843. {
  1844. v->visit(this);
  1845. }
  1846. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  1847. ir_rvalue *record;
  1848. const char *field;
  1849. };
  1850. /**
  1851. * Data stored in an ir_constant
  1852. */
  1853. union ir_constant_data {
  1854. unsigned u[16];
  1855. int i[16];
  1856. float f[16];
  1857. bool b[16];
  1858. double d[16];
  1859. };
  1860. class ir_constant : public ir_rvalue {
  1861. public:
  1862. ir_constant(const struct glsl_type *type, const ir_constant_data *data);
  1863. ir_constant(bool b, unsigned vector_elements=1);
  1864. ir_constant(unsigned int u, unsigned vector_elements=1);
  1865. ir_constant(int i, unsigned vector_elements=1);
  1866. ir_constant(float f, unsigned vector_elements=1);
  1867. ir_constant(double d, unsigned vector_elements=1);
  1868. /**
  1869. * Construct an ir_constant from a list of ir_constant values
  1870. */
  1871. ir_constant(const struct glsl_type *type, exec_list *values);
  1872. /**
  1873. * Construct an ir_constant from a scalar component of another ir_constant
  1874. *
  1875. * The new \c ir_constant inherits the type of the component from the
  1876. * source constant.
  1877. *
  1878. * \note
  1879. * In the case of a matrix constant, the new constant is a scalar, \b not
  1880. * a vector.
  1881. */
  1882. ir_constant(const ir_constant *c, unsigned i);
  1883. /**
  1884. * Return a new ir_constant of the specified type containing all zeros.
  1885. */
  1886. static ir_constant *zero(void *mem_ctx, const glsl_type *type);
  1887. virtual ir_constant *clone(void *mem_ctx, struct hash_table *) const;
  1888. virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
  1889. virtual void accept(ir_visitor *v)
  1890. {
  1891. v->visit(this);
  1892. }
  1893. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  1894. virtual bool equals(const ir_instruction *ir,
  1895. enum ir_node_type ignore = ir_type_unset) const;
  1896. /**
  1897. * Get a particular component of a constant as a specific type
  1898. *
  1899. * This is useful, for example, to get a value from an integer constant
  1900. * as a float or bool. This appears frequently when constructors are
  1901. * called with all constant parameters.
  1902. */
  1903. /*@{*/
  1904. bool get_bool_component(unsigned i) const;
  1905. float get_float_component(unsigned i) const;
  1906. double get_double_component(unsigned i) const;
  1907. int get_int_component(unsigned i) const;
  1908. unsigned get_uint_component(unsigned i) const;
  1909. /*@}*/
  1910. ir_constant *get_array_element(unsigned i) const;
  1911. ir_constant *get_record_field(const char *name);
  1912. /**
  1913. * Copy the values on another constant at a given offset.
  1914. *
  1915. * The offset is ignored for array or struct copies, it's only for
  1916. * scalars or vectors into vectors or matrices.
  1917. *
  1918. * With identical types on both sides and zero offset it's clone()
  1919. * without creating a new object.
  1920. */
  1921. void copy_offset(ir_constant *src, int offset);
  1922. /**
  1923. * Copy the values on another constant at a given offset and
  1924. * following an assign-like mask.
  1925. *
  1926. * The mask is ignored for scalars.
  1927. *
  1928. * Note that this function only handles what assign can handle,
  1929. * i.e. at most a vector as source and a column of a matrix as
  1930. * destination.
  1931. */
  1932. void copy_masked_offset(ir_constant *src, int offset, unsigned int mask);
  1933. /**
  1934. * Determine whether a constant has the same value as another constant
  1935. *
  1936. * \sa ir_constant::is_zero, ir_constant::is_one,
  1937. * ir_constant::is_negative_one
  1938. */
  1939. bool has_value(const ir_constant *) const;
  1940. /**
  1941. * Return true if this ir_constant represents the given value.
  1942. *
  1943. * For vectors, this checks that each component is the given value.
  1944. */
  1945. virtual bool is_value(float f, int i) const;
  1946. virtual bool is_zero() const;
  1947. virtual bool is_one() const;
  1948. virtual bool is_negative_one() const;
  1949. /**
  1950. * Return true for constants that could be stored as 16-bit unsigned values.
  1951. *
  1952. * Note that this will return true even for signed integer ir_constants, as
  1953. * long as the value is non-negative and fits in 16-bits.
  1954. */
  1955. virtual bool is_uint16_constant() const;
  1956. /**
  1957. * Value of the constant.
  1958. *
  1959. * The field used to back the values supplied by the constant is determined
  1960. * by the type associated with the \c ir_instruction. Constants may be
  1961. * scalars, vectors, or matrices.
  1962. */
  1963. union ir_constant_data value;
  1964. /* Array elements */
  1965. ir_constant **array_elements;
  1966. /* Structure fields */
  1967. exec_list components;
  1968. private:
  1969. /**
  1970. * Parameterless constructor only used by the clone method
  1971. */
  1972. ir_constant(void);
  1973. };
  1974. /**
  1975. * IR instruction to emit a vertex in a geometry shader.
  1976. */
  1977. class ir_emit_vertex : public ir_instruction {
  1978. public:
  1979. ir_emit_vertex(ir_rvalue *stream)
  1980. : ir_instruction(ir_type_emit_vertex),
  1981. stream(stream)
  1982. {
  1983. assert(stream);
  1984. }
  1985. virtual void accept(ir_visitor *v)
  1986. {
  1987. v->visit(this);
  1988. }
  1989. virtual ir_emit_vertex *clone(void *mem_ctx, struct hash_table *ht) const
  1990. {
  1991. return new(mem_ctx) ir_emit_vertex(this->stream->clone(mem_ctx, ht));
  1992. }
  1993. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  1994. int stream_id() const
  1995. {
  1996. return stream->as_constant()->value.i[0];
  1997. }
  1998. ir_rvalue *stream;
  1999. };
  2000. /**
  2001. * IR instruction to complete the current primitive and start a new one in a
  2002. * geometry shader.
  2003. */
  2004. class ir_end_primitive : public ir_instruction {
  2005. public:
  2006. ir_end_primitive(ir_rvalue *stream)
  2007. : ir_instruction(ir_type_end_primitive),
  2008. stream(stream)
  2009. {
  2010. assert(stream);
  2011. }
  2012. virtual void accept(ir_visitor *v)
  2013. {
  2014. v->visit(this);
  2015. }
  2016. virtual ir_end_primitive *clone(void *mem_ctx, struct hash_table *ht) const
  2017. {
  2018. return new(mem_ctx) ir_end_primitive(this->stream->clone(mem_ctx, ht));
  2019. }
  2020. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  2021. int stream_id() const
  2022. {
  2023. return stream->as_constant()->value.i[0];
  2024. }
  2025. ir_rvalue *stream;
  2026. };
  2027. /**
  2028. * IR instruction for tessellation control and compute shader barrier.
  2029. */
  2030. class ir_barrier : public ir_instruction {
  2031. public:
  2032. ir_barrier()
  2033. : ir_instruction(ir_type_barrier)
  2034. {
  2035. }
  2036. virtual void accept(ir_visitor *v)
  2037. {
  2038. v->visit(this);
  2039. }
  2040. virtual ir_barrier *clone(void *mem_ctx, struct hash_table *) const
  2041. {
  2042. return new(mem_ctx) ir_barrier();
  2043. }
  2044. virtual ir_visitor_status accept(ir_hierarchical_visitor *);
  2045. };
  2046. /*@}*/
  2047. /**
  2048. * Apply a visitor to each IR node in a list
  2049. */
  2050. void
  2051. visit_exec_list(exec_list *list, ir_visitor *visitor);
  2052. /**
  2053. * Validate invariants on each IR node in a list
  2054. */
  2055. void validate_ir_tree(exec_list *instructions);
  2056. struct _mesa_glsl_parse_state;
  2057. struct gl_shader_program;
  2058. /**
  2059. * Detect whether an unlinked shader contains static recursion
  2060. *
  2061. * If the list of instructions is determined to contain static recursion,
  2062. * \c _mesa_glsl_error will be called to emit error messages for each function
  2063. * that is in the recursion cycle.
  2064. */
  2065. void
  2066. detect_recursion_unlinked(struct _mesa_glsl_parse_state *state,
  2067. exec_list *instructions);
  2068. /**
  2069. * Detect whether a linked shader contains static recursion
  2070. *
  2071. * If the list of instructions is determined to contain static recursion,
  2072. * \c link_error_printf will be called to emit error messages for each function
  2073. * that is in the recursion cycle. In addition,
  2074. * \c gl_shader_program::LinkStatus will be set to false.
  2075. */
  2076. void
  2077. detect_recursion_linked(struct gl_shader_program *prog,
  2078. exec_list *instructions);
  2079. /**
  2080. * Make a clone of each IR instruction in a list
  2081. *
  2082. * \param in List of IR instructions that are to be cloned
  2083. * \param out List to hold the cloned instructions
  2084. */
  2085. void
  2086. clone_ir_list(void *mem_ctx, exec_list *out, const exec_list *in);
  2087. extern void
  2088. _mesa_glsl_initialize_variables(exec_list *instructions,
  2089. struct _mesa_glsl_parse_state *state);
  2090. extern void
  2091. _mesa_glsl_initialize_functions(_mesa_glsl_parse_state *state);
  2092. extern void
  2093. _mesa_glsl_initialize_builtin_functions();
  2094. extern ir_function_signature *
  2095. _mesa_glsl_find_builtin_function(_mesa_glsl_parse_state *state,
  2096. const char *name, exec_list *actual_parameters);
  2097. extern ir_function *
  2098. _mesa_glsl_find_builtin_function_by_name(_mesa_glsl_parse_state *state,
  2099. const char *name);
  2100. extern gl_shader *
  2101. _mesa_glsl_get_builtin_function_shader(void);
  2102. extern void
  2103. _mesa_glsl_release_functions(void);
  2104. extern void
  2105. _mesa_glsl_release_builtin_functions(void);
  2106. extern void
  2107. reparent_ir(exec_list *list, void *mem_ctx);
  2108. struct glsl_symbol_table;
  2109. extern void
  2110. import_prototypes(const exec_list *source, exec_list *dest,
  2111. struct glsl_symbol_table *symbols, void *mem_ctx);
  2112. extern bool
  2113. ir_has_call(ir_instruction *ir);
  2114. extern void
  2115. do_set_program_inouts(exec_list *instructions, struct gl_program *prog,
  2116. gl_shader_stage shader_stage);
  2117. extern char *
  2118. prototype_string(const glsl_type *return_type, const char *name,
  2119. exec_list *parameters);
  2120. const char *
  2121. mode_string(const ir_variable *var);
  2122. /**
  2123. * Built-in / reserved GL variables names start with "gl_"
  2124. */
  2125. static inline bool
  2126. is_gl_identifier(const char *s)
  2127. {
  2128. return s && s[0] == 'g' && s[1] == 'l' && s[2] == '_';
  2129. }
  2130. extern "C" {
  2131. #endif /* __cplusplus */
  2132. extern void _mesa_print_ir(FILE *f, struct exec_list *instructions,
  2133. struct _mesa_glsl_parse_state *state);
  2134. extern void
  2135. fprint_ir(FILE *f, const void *instruction);
  2136. #ifdef __cplusplus
  2137. } /* extern "C" */
  2138. #endif
  2139. unsigned
  2140. vertices_per_prim(GLenum prim);
  2141. #endif /* IR_H */