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.

linker.cpp 132KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946
  1. /*
  2. * Copyright © 2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. /**
  24. * \file linker.cpp
  25. * GLSL linker implementation
  26. *
  27. * Given a set of shaders that are to be linked to generate a final program,
  28. * there are three distinct stages.
  29. *
  30. * In the first stage shaders are partitioned into groups based on the shader
  31. * type. All shaders of a particular type (e.g., vertex shaders) are linked
  32. * together.
  33. *
  34. * - Undefined references in each shader are resolve to definitions in
  35. * another shader.
  36. * - Types and qualifiers of uniforms, outputs, and global variables defined
  37. * in multiple shaders with the same name are verified to be the same.
  38. * - Initializers for uniforms and global variables defined
  39. * in multiple shaders with the same name are verified to be the same.
  40. *
  41. * The result, in the terminology of the GLSL spec, is a set of shader
  42. * executables for each processing unit.
  43. *
  44. * After the first stage is complete, a series of semantic checks are performed
  45. * on each of the shader executables.
  46. *
  47. * - Each shader executable must define a \c main function.
  48. * - Each vertex shader executable must write to \c gl_Position.
  49. * - Each fragment shader executable must write to either \c gl_FragData or
  50. * \c gl_FragColor.
  51. *
  52. * In the final stage individual shader executables are linked to create a
  53. * complete exectuable.
  54. *
  55. * - Types of uniforms defined in multiple shader stages with the same name
  56. * are verified to be the same.
  57. * - Initializers for uniforms defined in multiple shader stages with the
  58. * same name are verified to be the same.
  59. * - Types and qualifiers of outputs defined in one stage are verified to
  60. * be the same as the types and qualifiers of inputs defined with the same
  61. * name in a later stage.
  62. *
  63. * \author Ian Romanick <ian.d.romanick@intel.com>
  64. */
  65. #include <ctype.h>
  66. #include "main/core.h"
  67. #include "glsl_symbol_table.h"
  68. #include "glsl_parser_extras.h"
  69. #include "ir.h"
  70. #include "program.h"
  71. #include "program/hash_table.h"
  72. #include "linker.h"
  73. #include "link_varyings.h"
  74. #include "ir_optimization.h"
  75. #include "ir_rvalue_visitor.h"
  76. #include "ir_uniform.h"
  77. #include "main/shaderobj.h"
  78. #include "main/enums.h"
  79. void linker_error(gl_shader_program *, const char *, ...);
  80. namespace {
  81. /**
  82. * Visitor that determines whether or not a variable is ever written.
  83. */
  84. class find_assignment_visitor : public ir_hierarchical_visitor {
  85. public:
  86. find_assignment_visitor(const char *name)
  87. : name(name), found(false)
  88. {
  89. /* empty */
  90. }
  91. virtual ir_visitor_status visit_enter(ir_assignment *ir)
  92. {
  93. ir_variable *const var = ir->lhs->variable_referenced();
  94. if (strcmp(name, var->name) == 0) {
  95. found = true;
  96. return visit_stop;
  97. }
  98. return visit_continue_with_parent;
  99. }
  100. virtual ir_visitor_status visit_enter(ir_call *ir)
  101. {
  102. foreach_two_lists(formal_node, &ir->callee->parameters,
  103. actual_node, &ir->actual_parameters) {
  104. ir_rvalue *param_rval = (ir_rvalue *) actual_node;
  105. ir_variable *sig_param = (ir_variable *) formal_node;
  106. if (sig_param->data.mode == ir_var_function_out ||
  107. sig_param->data.mode == ir_var_function_inout) {
  108. ir_variable *var = param_rval->variable_referenced();
  109. if (var && strcmp(name, var->name) == 0) {
  110. found = true;
  111. return visit_stop;
  112. }
  113. }
  114. }
  115. if (ir->return_deref != NULL) {
  116. ir_variable *const var = ir->return_deref->variable_referenced();
  117. if (strcmp(name, var->name) == 0) {
  118. found = true;
  119. return visit_stop;
  120. }
  121. }
  122. return visit_continue_with_parent;
  123. }
  124. bool variable_found()
  125. {
  126. return found;
  127. }
  128. private:
  129. const char *name; /**< Find writes to a variable with this name. */
  130. bool found; /**< Was a write to the variable found? */
  131. };
  132. /**
  133. * Visitor that determines whether or not a variable is ever read.
  134. */
  135. class find_deref_visitor : public ir_hierarchical_visitor {
  136. public:
  137. find_deref_visitor(const char *name)
  138. : name(name), found(false)
  139. {
  140. /* empty */
  141. }
  142. virtual ir_visitor_status visit(ir_dereference_variable *ir)
  143. {
  144. if (strcmp(this->name, ir->var->name) == 0) {
  145. this->found = true;
  146. return visit_stop;
  147. }
  148. return visit_continue;
  149. }
  150. bool variable_found() const
  151. {
  152. return this->found;
  153. }
  154. private:
  155. const char *name; /**< Find writes to a variable with this name. */
  156. bool found; /**< Was a write to the variable found? */
  157. };
  158. class geom_array_resize_visitor : public ir_hierarchical_visitor {
  159. public:
  160. unsigned num_vertices;
  161. gl_shader_program *prog;
  162. geom_array_resize_visitor(unsigned num_vertices, gl_shader_program *prog)
  163. {
  164. this->num_vertices = num_vertices;
  165. this->prog = prog;
  166. }
  167. virtual ~geom_array_resize_visitor()
  168. {
  169. /* empty */
  170. }
  171. virtual ir_visitor_status visit(ir_variable *var)
  172. {
  173. if (!var->type->is_array() || var->data.mode != ir_var_shader_in)
  174. return visit_continue;
  175. unsigned size = var->type->length;
  176. /* Generate a link error if the shader has declared this array with an
  177. * incorrect size.
  178. */
  179. if (size && size != this->num_vertices) {
  180. linker_error(this->prog, "size of array %s declared as %u, "
  181. "but number of input vertices is %u\n",
  182. var->name, size, this->num_vertices);
  183. return visit_continue;
  184. }
  185. /* Generate a link error if the shader attempts to access an input
  186. * array using an index too large for its actual size assigned at link
  187. * time.
  188. */
  189. if (var->data.max_array_access >= this->num_vertices) {
  190. linker_error(this->prog, "geometry shader accesses element %i of "
  191. "%s, but only %i input vertices\n",
  192. var->data.max_array_access, var->name, this->num_vertices);
  193. return visit_continue;
  194. }
  195. var->type = glsl_type::get_array_instance(var->type->fields.array,
  196. this->num_vertices);
  197. var->data.max_array_access = this->num_vertices - 1;
  198. return visit_continue;
  199. }
  200. /* Dereferences of input variables need to be updated so that their type
  201. * matches the newly assigned type of the variable they are accessing. */
  202. virtual ir_visitor_status visit(ir_dereference_variable *ir)
  203. {
  204. ir->type = ir->var->type;
  205. return visit_continue;
  206. }
  207. /* Dereferences of 2D input arrays need to be updated so that their type
  208. * matches the newly assigned type of the array they are accessing. */
  209. virtual ir_visitor_status visit_leave(ir_dereference_array *ir)
  210. {
  211. const glsl_type *const vt = ir->array->type;
  212. if (vt->is_array())
  213. ir->type = vt->fields.array;
  214. return visit_continue;
  215. }
  216. };
  217. class tess_eval_array_resize_visitor : public ir_hierarchical_visitor {
  218. public:
  219. unsigned num_vertices;
  220. gl_shader_program *prog;
  221. tess_eval_array_resize_visitor(unsigned num_vertices, gl_shader_program *prog)
  222. {
  223. this->num_vertices = num_vertices;
  224. this->prog = prog;
  225. }
  226. virtual ~tess_eval_array_resize_visitor()
  227. {
  228. /* empty */
  229. }
  230. virtual ir_visitor_status visit(ir_variable *var)
  231. {
  232. if (!var->type->is_array() || var->data.mode != ir_var_shader_in || var->data.patch)
  233. return visit_continue;
  234. var->type = glsl_type::get_array_instance(var->type->fields.array,
  235. this->num_vertices);
  236. var->data.max_array_access = this->num_vertices - 1;
  237. return visit_continue;
  238. }
  239. /* Dereferences of input variables need to be updated so that their type
  240. * matches the newly assigned type of the variable they are accessing. */
  241. virtual ir_visitor_status visit(ir_dereference_variable *ir)
  242. {
  243. ir->type = ir->var->type;
  244. return visit_continue;
  245. }
  246. /* Dereferences of 2D input arrays need to be updated so that their type
  247. * matches the newly assigned type of the array they are accessing. */
  248. virtual ir_visitor_status visit_leave(ir_dereference_array *ir)
  249. {
  250. const glsl_type *const vt = ir->array->type;
  251. if (vt->is_array())
  252. ir->type = vt->fields.array;
  253. return visit_continue;
  254. }
  255. };
  256. class barrier_use_visitor : public ir_hierarchical_visitor {
  257. public:
  258. barrier_use_visitor(gl_shader_program *prog)
  259. : prog(prog), in_main(false), after_return(false), control_flow(0)
  260. {
  261. }
  262. virtual ~barrier_use_visitor()
  263. {
  264. /* empty */
  265. }
  266. virtual ir_visitor_status visit_enter(ir_function *ir)
  267. {
  268. if (strcmp(ir->name, "main") == 0)
  269. in_main = true;
  270. return visit_continue;
  271. }
  272. virtual ir_visitor_status visit_leave(ir_function *)
  273. {
  274. in_main = false;
  275. after_return = false;
  276. return visit_continue;
  277. }
  278. virtual ir_visitor_status visit_leave(ir_return *)
  279. {
  280. after_return = true;
  281. return visit_continue;
  282. }
  283. virtual ir_visitor_status visit_enter(ir_if *)
  284. {
  285. ++control_flow;
  286. return visit_continue;
  287. }
  288. virtual ir_visitor_status visit_leave(ir_if *)
  289. {
  290. --control_flow;
  291. return visit_continue;
  292. }
  293. virtual ir_visitor_status visit_enter(ir_loop *)
  294. {
  295. ++control_flow;
  296. return visit_continue;
  297. }
  298. virtual ir_visitor_status visit_leave(ir_loop *)
  299. {
  300. --control_flow;
  301. return visit_continue;
  302. }
  303. /* FINISHME: `switch` is not expressed at the IR level -- it's already
  304. * been lowered to a mess of `if`s. We'll correctly disallow any use of
  305. * barrier() in a conditional path within the switch, but not in a path
  306. * which is always hit.
  307. */
  308. virtual ir_visitor_status visit_enter(ir_call *ir)
  309. {
  310. if (ir->use_builtin && strcmp(ir->callee_name(), "barrier") == 0) {
  311. /* Use of barrier(); determine if it is legal: */
  312. if (!in_main) {
  313. linker_error(prog, "Builtin barrier() may only be used in main");
  314. return visit_stop;
  315. }
  316. if (after_return) {
  317. linker_error(prog, "Builtin barrier() may not be used after return");
  318. return visit_stop;
  319. }
  320. if (control_flow != 0) {
  321. linker_error(prog, "Builtin barrier() may not be used inside control flow");
  322. return visit_stop;
  323. }
  324. }
  325. return visit_continue;
  326. }
  327. private:
  328. gl_shader_program *prog;
  329. bool in_main, after_return;
  330. int control_flow;
  331. };
  332. /**
  333. * Visitor that determines the highest stream id to which a (geometry) shader
  334. * emits vertices. It also checks whether End{Stream}Primitive is ever called.
  335. */
  336. class find_emit_vertex_visitor : public ir_hierarchical_visitor {
  337. public:
  338. find_emit_vertex_visitor(int max_allowed)
  339. : max_stream_allowed(max_allowed),
  340. invalid_stream_id(0),
  341. invalid_stream_id_from_emit_vertex(false),
  342. end_primitive_found(false),
  343. uses_non_zero_stream(false)
  344. {
  345. /* empty */
  346. }
  347. virtual ir_visitor_status visit_leave(ir_emit_vertex *ir)
  348. {
  349. int stream_id = ir->stream_id();
  350. if (stream_id < 0) {
  351. invalid_stream_id = stream_id;
  352. invalid_stream_id_from_emit_vertex = true;
  353. return visit_stop;
  354. }
  355. if (stream_id > max_stream_allowed) {
  356. invalid_stream_id = stream_id;
  357. invalid_stream_id_from_emit_vertex = true;
  358. return visit_stop;
  359. }
  360. if (stream_id != 0)
  361. uses_non_zero_stream = true;
  362. return visit_continue;
  363. }
  364. virtual ir_visitor_status visit_leave(ir_end_primitive *ir)
  365. {
  366. end_primitive_found = true;
  367. int stream_id = ir->stream_id();
  368. if (stream_id < 0) {
  369. invalid_stream_id = stream_id;
  370. invalid_stream_id_from_emit_vertex = false;
  371. return visit_stop;
  372. }
  373. if (stream_id > max_stream_allowed) {
  374. invalid_stream_id = stream_id;
  375. invalid_stream_id_from_emit_vertex = false;
  376. return visit_stop;
  377. }
  378. if (stream_id != 0)
  379. uses_non_zero_stream = true;
  380. return visit_continue;
  381. }
  382. bool error()
  383. {
  384. return invalid_stream_id != 0;
  385. }
  386. const char *error_func()
  387. {
  388. return invalid_stream_id_from_emit_vertex ?
  389. "EmitStreamVertex" : "EndStreamPrimitive";
  390. }
  391. int error_stream()
  392. {
  393. return invalid_stream_id;
  394. }
  395. bool uses_streams()
  396. {
  397. return uses_non_zero_stream;
  398. }
  399. bool uses_end_primitive()
  400. {
  401. return end_primitive_found;
  402. }
  403. private:
  404. int max_stream_allowed;
  405. int invalid_stream_id;
  406. bool invalid_stream_id_from_emit_vertex;
  407. bool end_primitive_found;
  408. bool uses_non_zero_stream;
  409. };
  410. /* Class that finds array derefs and check if indexes are dynamic. */
  411. class dynamic_sampler_array_indexing_visitor : public ir_hierarchical_visitor
  412. {
  413. public:
  414. dynamic_sampler_array_indexing_visitor() :
  415. dynamic_sampler_array_indexing(false)
  416. {
  417. }
  418. ir_visitor_status visit_enter(ir_dereference_array *ir)
  419. {
  420. if (!ir->variable_referenced())
  421. return visit_continue;
  422. if (!ir->variable_referenced()->type->contains_sampler())
  423. return visit_continue;
  424. if (!ir->array_index->constant_expression_value()) {
  425. dynamic_sampler_array_indexing = true;
  426. return visit_stop;
  427. }
  428. return visit_continue;
  429. }
  430. bool uses_dynamic_sampler_array_indexing()
  431. {
  432. return dynamic_sampler_array_indexing;
  433. }
  434. private:
  435. bool dynamic_sampler_array_indexing;
  436. };
  437. } /* anonymous namespace */
  438. void
  439. linker_error(gl_shader_program *prog, const char *fmt, ...)
  440. {
  441. va_list ap;
  442. ralloc_strcat(&prog->InfoLog, "error: ");
  443. va_start(ap, fmt);
  444. ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
  445. va_end(ap);
  446. prog->LinkStatus = false;
  447. }
  448. void
  449. linker_warning(gl_shader_program *prog, const char *fmt, ...)
  450. {
  451. va_list ap;
  452. ralloc_strcat(&prog->InfoLog, "warning: ");
  453. va_start(ap, fmt);
  454. ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
  455. va_end(ap);
  456. }
  457. /**
  458. * Given a string identifying a program resource, break it into a base name
  459. * and an optional array index in square brackets.
  460. *
  461. * If an array index is present, \c out_base_name_end is set to point to the
  462. * "[" that precedes the array index, and the array index itself is returned
  463. * as a long.
  464. *
  465. * If no array index is present (or if the array index is negative or
  466. * mal-formed), \c out_base_name_end, is set to point to the null terminator
  467. * at the end of the input string, and -1 is returned.
  468. *
  469. * Only the final array index is parsed; if the string contains other array
  470. * indices (or structure field accesses), they are left in the base name.
  471. *
  472. * No attempt is made to check that the base name is properly formed;
  473. * typically the caller will look up the base name in a hash table, so
  474. * ill-formed base names simply turn into hash table lookup failures.
  475. */
  476. long
  477. parse_program_resource_name(const GLchar *name,
  478. const GLchar **out_base_name_end)
  479. {
  480. /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
  481. *
  482. * "When an integer array element or block instance number is part of
  483. * the name string, it will be specified in decimal form without a "+"
  484. * or "-" sign or any extra leading zeroes. Additionally, the name
  485. * string will not include white space anywhere in the string."
  486. */
  487. const size_t len = strlen(name);
  488. *out_base_name_end = name + len;
  489. if (len == 0 || name[len-1] != ']')
  490. return -1;
  491. /* Walk backwards over the string looking for a non-digit character. This
  492. * had better be the opening bracket for an array index.
  493. *
  494. * Initially, i specifies the location of the ']'. Since the string may
  495. * contain only the ']' charcater, walk backwards very carefully.
  496. */
  497. unsigned i;
  498. for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
  499. /* empty */ ;
  500. if ((i == 0) || name[i-1] != '[')
  501. return -1;
  502. long array_index = strtol(&name[i], NULL, 10);
  503. if (array_index < 0)
  504. return -1;
  505. /* Check for leading zero */
  506. if (name[i] == '0' && name[i+1] != ']')
  507. return -1;
  508. *out_base_name_end = name + (i - 1);
  509. return array_index;
  510. }
  511. void
  512. link_invalidate_variable_locations(exec_list *ir)
  513. {
  514. foreach_in_list(ir_instruction, node, ir) {
  515. ir_variable *const var = node->as_variable();
  516. if (var == NULL)
  517. continue;
  518. /* Only assign locations for variables that lack an explicit location.
  519. * Explicit locations are set for all built-in variables, generic vertex
  520. * shader inputs (via layout(location=...)), and generic fragment shader
  521. * outputs (also via layout(location=...)).
  522. */
  523. if (!var->data.explicit_location) {
  524. var->data.location = -1;
  525. var->data.location_frac = 0;
  526. }
  527. /* ir_variable::is_unmatched_generic_inout is used by the linker while
  528. * connecting outputs from one stage to inputs of the next stage.
  529. *
  530. * There are two implicit assumptions here. First, we assume that any
  531. * built-in variable (i.e., non-generic in or out) will have
  532. * explicit_location set. Second, we assume that any generic in or out
  533. * will not have explicit_location set.
  534. *
  535. * This second assumption will only be valid until
  536. * GL_ARB_separate_shader_objects is supported. When that extension is
  537. * implemented, this function will need some modifications.
  538. */
  539. if (!var->data.explicit_location) {
  540. var->data.is_unmatched_generic_inout = 1;
  541. } else {
  542. var->data.is_unmatched_generic_inout = 0;
  543. }
  544. }
  545. }
  546. /**
  547. * Set UsesClipDistance and ClipDistanceArraySize based on the given shader.
  548. *
  549. * Also check for errors based on incorrect usage of gl_ClipVertex and
  550. * gl_ClipDistance.
  551. *
  552. * Return false if an error was reported.
  553. */
  554. static void
  555. analyze_clip_usage(struct gl_shader_program *prog,
  556. struct gl_shader *shader, GLboolean *UsesClipDistance,
  557. GLuint *ClipDistanceArraySize)
  558. {
  559. *ClipDistanceArraySize = 0;
  560. if (!prog->IsES && prog->Version >= 130) {
  561. /* From section 7.1 (Vertex Shader Special Variables) of the
  562. * GLSL 1.30 spec:
  563. *
  564. * "It is an error for a shader to statically write both
  565. * gl_ClipVertex and gl_ClipDistance."
  566. *
  567. * This does not apply to GLSL ES shaders, since GLSL ES defines neither
  568. * gl_ClipVertex nor gl_ClipDistance.
  569. */
  570. find_assignment_visitor clip_vertex("gl_ClipVertex");
  571. find_assignment_visitor clip_distance("gl_ClipDistance");
  572. clip_vertex.run(shader->ir);
  573. clip_distance.run(shader->ir);
  574. if (clip_vertex.variable_found() && clip_distance.variable_found()) {
  575. linker_error(prog, "%s shader writes to both `gl_ClipVertex' "
  576. "and `gl_ClipDistance'\n",
  577. _mesa_shader_stage_to_string(shader->Stage));
  578. return;
  579. }
  580. *UsesClipDistance = clip_distance.variable_found();
  581. ir_variable *clip_distance_var =
  582. shader->symbols->get_variable("gl_ClipDistance");
  583. if (clip_distance_var)
  584. *ClipDistanceArraySize = clip_distance_var->type->length;
  585. } else {
  586. *UsesClipDistance = false;
  587. }
  588. }
  589. /**
  590. * Verify that a vertex shader executable meets all semantic requirements.
  591. *
  592. * Also sets prog->Vert.UsesClipDistance and prog->Vert.ClipDistanceArraySize
  593. * as a side effect.
  594. *
  595. * \param shader Vertex shader executable to be verified
  596. */
  597. void
  598. validate_vertex_shader_executable(struct gl_shader_program *prog,
  599. struct gl_shader *shader)
  600. {
  601. if (shader == NULL)
  602. return;
  603. /* From the GLSL 1.10 spec, page 48:
  604. *
  605. * "The variable gl_Position is available only in the vertex
  606. * language and is intended for writing the homogeneous vertex
  607. * position. All executions of a well-formed vertex shader
  608. * executable must write a value into this variable. [...] The
  609. * variable gl_Position is available only in the vertex
  610. * language and is intended for writing the homogeneous vertex
  611. * position. All executions of a well-formed vertex shader
  612. * executable must write a value into this variable."
  613. *
  614. * while in GLSL 1.40 this text is changed to:
  615. *
  616. * "The variable gl_Position is available only in the vertex
  617. * language and is intended for writing the homogeneous vertex
  618. * position. It can be written at any time during shader
  619. * execution. It may also be read back by a vertex shader
  620. * after being written. This value will be used by primitive
  621. * assembly, clipping, culling, and other fixed functionality
  622. * operations, if present, that operate on primitives after
  623. * vertex processing has occurred. Its value is undefined if
  624. * the vertex shader executable does not write gl_Position."
  625. *
  626. * All GLSL ES Versions are similar to GLSL 1.40--failing to write to
  627. * gl_Position is not an error.
  628. */
  629. if (prog->Version < (prog->IsES ? 300 : 140)) {
  630. find_assignment_visitor find("gl_Position");
  631. find.run(shader->ir);
  632. if (!find.variable_found()) {
  633. if (prog->IsES) {
  634. linker_warning(prog,
  635. "vertex shader does not write to `gl_Position'."
  636. "It's value is undefined. \n");
  637. } else {
  638. linker_error(prog,
  639. "vertex shader does not write to `gl_Position'. \n");
  640. }
  641. return;
  642. }
  643. }
  644. analyze_clip_usage(prog, shader, &prog->Vert.UsesClipDistance,
  645. &prog->Vert.ClipDistanceArraySize);
  646. }
  647. void
  648. validate_tess_eval_shader_executable(struct gl_shader_program *prog,
  649. struct gl_shader *shader)
  650. {
  651. if (shader == NULL)
  652. return;
  653. analyze_clip_usage(prog, shader, &prog->TessEval.UsesClipDistance,
  654. &prog->TessEval.ClipDistanceArraySize);
  655. }
  656. /**
  657. * Verify that a fragment shader executable meets all semantic requirements
  658. *
  659. * \param shader Fragment shader executable to be verified
  660. */
  661. void
  662. validate_fragment_shader_executable(struct gl_shader_program *prog,
  663. struct gl_shader *shader)
  664. {
  665. if (shader == NULL)
  666. return;
  667. find_assignment_visitor frag_color("gl_FragColor");
  668. find_assignment_visitor frag_data("gl_FragData");
  669. frag_color.run(shader->ir);
  670. frag_data.run(shader->ir);
  671. if (frag_color.variable_found() && frag_data.variable_found()) {
  672. linker_error(prog, "fragment shader writes to both "
  673. "`gl_FragColor' and `gl_FragData'\n");
  674. }
  675. }
  676. /**
  677. * Verify that a geometry shader executable meets all semantic requirements
  678. *
  679. * Also sets prog->Geom.VerticesIn, prog->Geom.UsesClipDistance, and
  680. * prog->Geom.ClipDistanceArraySize as a side effect.
  681. *
  682. * \param shader Geometry shader executable to be verified
  683. */
  684. void
  685. validate_geometry_shader_executable(struct gl_shader_program *prog,
  686. struct gl_shader *shader)
  687. {
  688. if (shader == NULL)
  689. return;
  690. unsigned num_vertices = vertices_per_prim(prog->Geom.InputType);
  691. prog->Geom.VerticesIn = num_vertices;
  692. analyze_clip_usage(prog, shader, &prog->Geom.UsesClipDistance,
  693. &prog->Geom.ClipDistanceArraySize);
  694. }
  695. /**
  696. * Check if geometry shaders emit to non-zero streams and do corresponding
  697. * validations.
  698. */
  699. static void
  700. validate_geometry_shader_emissions(struct gl_context *ctx,
  701. struct gl_shader_program *prog)
  702. {
  703. if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
  704. find_emit_vertex_visitor emit_vertex(ctx->Const.MaxVertexStreams - 1);
  705. emit_vertex.run(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir);
  706. if (emit_vertex.error()) {
  707. linker_error(prog, "Invalid call %s(%d). Accepted values for the "
  708. "stream parameter are in the range [0, %d].\n",
  709. emit_vertex.error_func(),
  710. emit_vertex.error_stream(),
  711. ctx->Const.MaxVertexStreams - 1);
  712. }
  713. prog->Geom.UsesStreams = emit_vertex.uses_streams();
  714. prog->Geom.UsesEndPrimitive = emit_vertex.uses_end_primitive();
  715. /* From the ARB_gpu_shader5 spec:
  716. *
  717. * "Multiple vertex streams are supported only if the output primitive
  718. * type is declared to be "points". A program will fail to link if it
  719. * contains a geometry shader calling EmitStreamVertex() or
  720. * EndStreamPrimitive() if its output primitive type is not "points".
  721. *
  722. * However, in the same spec:
  723. *
  724. * "The function EmitVertex() is equivalent to calling EmitStreamVertex()
  725. * with <stream> set to zero."
  726. *
  727. * And:
  728. *
  729. * "The function EndPrimitive() is equivalent to calling
  730. * EndStreamPrimitive() with <stream> set to zero."
  731. *
  732. * Since we can call EmitVertex() and EndPrimitive() when we output
  733. * primitives other than points, calling EmitStreamVertex(0) or
  734. * EmitEndPrimitive(0) should not produce errors. This it also what Nvidia
  735. * does. Currently we only set prog->Geom.UsesStreams to TRUE when
  736. * EmitStreamVertex() or EmitEndPrimitive() are called with a non-zero
  737. * stream.
  738. */
  739. if (prog->Geom.UsesStreams && prog->Geom.OutputType != GL_POINTS) {
  740. linker_error(prog, "EmitStreamVertex(n) and EndStreamPrimitive(n) "
  741. "with n>0 requires point output\n");
  742. }
  743. }
  744. }
  745. bool
  746. validate_intrastage_arrays(struct gl_shader_program *prog,
  747. ir_variable *const var,
  748. ir_variable *const existing)
  749. {
  750. /* Consider the types to be "the same" if both types are arrays
  751. * of the same type and one of the arrays is implicitly sized.
  752. * In addition, set the type of the linked variable to the
  753. * explicitly sized array.
  754. */
  755. if (var->type->is_array() && existing->type->is_array() &&
  756. (var->type->fields.array == existing->type->fields.array) &&
  757. ((var->type->length == 0)|| (existing->type->length == 0))) {
  758. if (var->type->length != 0) {
  759. if (var->type->length <= existing->data.max_array_access) {
  760. linker_error(prog, "%s `%s' declared as type "
  761. "`%s' but outermost dimension has an index"
  762. " of `%i'\n",
  763. mode_string(var),
  764. var->name, var->type->name,
  765. existing->data.max_array_access);
  766. }
  767. existing->type = var->type;
  768. return true;
  769. } else if (existing->type->length != 0) {
  770. if(existing->type->length <= var->data.max_array_access) {
  771. linker_error(prog, "%s `%s' declared as type "
  772. "`%s' but outermost dimension has an index"
  773. " of `%i'\n",
  774. mode_string(var),
  775. var->name, existing->type->name,
  776. var->data.max_array_access);
  777. }
  778. return true;
  779. }
  780. }
  781. return false;
  782. }
  783. /**
  784. * Perform validation of global variables used across multiple shaders
  785. */
  786. void
  787. cross_validate_globals(struct gl_shader_program *prog,
  788. struct gl_shader **shader_list,
  789. unsigned num_shaders,
  790. bool uniforms_only)
  791. {
  792. /* Examine all of the uniforms in all of the shaders and cross validate
  793. * them.
  794. */
  795. glsl_symbol_table variables;
  796. for (unsigned i = 0; i < num_shaders; i++) {
  797. if (shader_list[i] == NULL)
  798. continue;
  799. foreach_in_list(ir_instruction, node, shader_list[i]->ir) {
  800. ir_variable *const var = node->as_variable();
  801. if (var == NULL)
  802. continue;
  803. if (uniforms_only && (var->data.mode != ir_var_uniform && var->data.mode != ir_var_shader_storage))
  804. continue;
  805. /* don't cross validate subroutine uniforms */
  806. if (var->type->contains_subroutine())
  807. continue;
  808. /* Don't cross validate temporaries that are at global scope. These
  809. * will eventually get pulled into the shaders 'main'.
  810. */
  811. if (var->data.mode == ir_var_temporary)
  812. continue;
  813. /* If a global with this name has already been seen, verify that the
  814. * new instance has the same type. In addition, if the globals have
  815. * initializers, the values of the initializers must be the same.
  816. */
  817. ir_variable *const existing = variables.get_variable(var->name);
  818. if (existing != NULL) {
  819. /* Check if types match. Interface blocks have some special
  820. * rules so we handle those elsewhere.
  821. */
  822. if (var->type != existing->type &&
  823. !var->is_interface_instance()) {
  824. if (!validate_intrastage_arrays(prog, var, existing)) {
  825. if (var->type->is_record() && existing->type->is_record()
  826. && existing->type->record_compare(var->type)) {
  827. existing->type = var->type;
  828. } else {
  829. linker_error(prog, "%s `%s' declared as type "
  830. "`%s' and type `%s'\n",
  831. mode_string(var),
  832. var->name, var->type->name,
  833. existing->type->name);
  834. return;
  835. }
  836. }
  837. }
  838. if (var->data.explicit_location) {
  839. if (existing->data.explicit_location
  840. && (var->data.location != existing->data.location)) {
  841. linker_error(prog, "explicit locations for %s "
  842. "`%s' have differing values\n",
  843. mode_string(var), var->name);
  844. return;
  845. }
  846. existing->data.location = var->data.location;
  847. existing->data.explicit_location = true;
  848. }
  849. /* From the GLSL 4.20 specification:
  850. * "A link error will result if two compilation units in a program
  851. * specify different integer-constant bindings for the same
  852. * opaque-uniform name. However, it is not an error to specify a
  853. * binding on some but not all declarations for the same name"
  854. */
  855. if (var->data.explicit_binding) {
  856. if (existing->data.explicit_binding &&
  857. var->data.binding != existing->data.binding) {
  858. linker_error(prog, "explicit bindings for %s "
  859. "`%s' have differing values\n",
  860. mode_string(var), var->name);
  861. return;
  862. }
  863. existing->data.binding = var->data.binding;
  864. existing->data.explicit_binding = true;
  865. }
  866. if (var->type->contains_atomic() &&
  867. var->data.atomic.offset != existing->data.atomic.offset) {
  868. linker_error(prog, "offset specifications for %s "
  869. "`%s' have differing values\n",
  870. mode_string(var), var->name);
  871. return;
  872. }
  873. /* Validate layout qualifiers for gl_FragDepth.
  874. *
  875. * From the AMD/ARB_conservative_depth specs:
  876. *
  877. * "If gl_FragDepth is redeclared in any fragment shader in a
  878. * program, it must be redeclared in all fragment shaders in
  879. * that program that have static assignments to
  880. * gl_FragDepth. All redeclarations of gl_FragDepth in all
  881. * fragment shaders in a single program must have the same set
  882. * of qualifiers."
  883. */
  884. if (strcmp(var->name, "gl_FragDepth") == 0) {
  885. bool layout_declared = var->data.depth_layout != ir_depth_layout_none;
  886. bool layout_differs =
  887. var->data.depth_layout != existing->data.depth_layout;
  888. if (layout_declared && layout_differs) {
  889. linker_error(prog,
  890. "All redeclarations of gl_FragDepth in all "
  891. "fragment shaders in a single program must have "
  892. "the same set of qualifiers.\n");
  893. }
  894. if (var->data.used && layout_differs) {
  895. linker_error(prog,
  896. "If gl_FragDepth is redeclared with a layout "
  897. "qualifier in any fragment shader, it must be "
  898. "redeclared with the same layout qualifier in "
  899. "all fragment shaders that have assignments to "
  900. "gl_FragDepth\n");
  901. }
  902. }
  903. /* Page 35 (page 41 of the PDF) of the GLSL 4.20 spec says:
  904. *
  905. * "If a shared global has multiple initializers, the
  906. * initializers must all be constant expressions, and they
  907. * must all have the same value. Otherwise, a link error will
  908. * result. (A shared global having only one initializer does
  909. * not require that initializer to be a constant expression.)"
  910. *
  911. * Previous to 4.20 the GLSL spec simply said that initializers
  912. * must have the same value. In this case of non-constant
  913. * initializers, this was impossible to determine. As a result,
  914. * no vendor actually implemented that behavior. The 4.20
  915. * behavior matches the implemented behavior of at least one other
  916. * vendor, so we'll implement that for all GLSL versions.
  917. */
  918. if (var->constant_initializer != NULL) {
  919. if (existing->constant_initializer != NULL) {
  920. if (!var->constant_initializer->has_value(existing->constant_initializer)) {
  921. linker_error(prog, "initializers for %s "
  922. "`%s' have differing values\n",
  923. mode_string(var), var->name);
  924. return;
  925. }
  926. } else {
  927. /* If the first-seen instance of a particular uniform did not
  928. * have an initializer but a later instance does, copy the
  929. * initializer to the version stored in the symbol table.
  930. */
  931. /* FINISHME: This is wrong. The constant_value field should
  932. * FINISHME: not be modified! Imagine a case where a shader
  933. * FINISHME: without an initializer is linked in two different
  934. * FINISHME: programs with shaders that have differing
  935. * FINISHME: initializers. Linking with the first will
  936. * FINISHME: modify the shader, and linking with the second
  937. * FINISHME: will fail.
  938. */
  939. existing->constant_initializer =
  940. var->constant_initializer->clone(ralloc_parent(existing),
  941. NULL);
  942. }
  943. }
  944. if (var->data.has_initializer) {
  945. if (existing->data.has_initializer
  946. && (var->constant_initializer == NULL
  947. || existing->constant_initializer == NULL)) {
  948. linker_error(prog,
  949. "shared global variable `%s' has multiple "
  950. "non-constant initializers.\n",
  951. var->name);
  952. return;
  953. }
  954. /* Some instance had an initializer, so keep track of that. In
  955. * this location, all sorts of initializers (constant or
  956. * otherwise) will propagate the existence to the variable
  957. * stored in the symbol table.
  958. */
  959. existing->data.has_initializer = true;
  960. }
  961. if (existing->data.invariant != var->data.invariant) {
  962. linker_error(prog, "declarations for %s `%s' have "
  963. "mismatching invariant qualifiers\n",
  964. mode_string(var), var->name);
  965. return;
  966. }
  967. if (existing->data.centroid != var->data.centroid) {
  968. linker_error(prog, "declarations for %s `%s' have "
  969. "mismatching centroid qualifiers\n",
  970. mode_string(var), var->name);
  971. return;
  972. }
  973. if (existing->data.sample != var->data.sample) {
  974. linker_error(prog, "declarations for %s `%s` have "
  975. "mismatching sample qualifiers\n",
  976. mode_string(var), var->name);
  977. return;
  978. }
  979. } else
  980. variables.add_variable(var);
  981. }
  982. }
  983. }
  984. /**
  985. * Perform validation of uniforms used across multiple shader stages
  986. */
  987. void
  988. cross_validate_uniforms(struct gl_shader_program *prog)
  989. {
  990. cross_validate_globals(prog, prog->_LinkedShaders,
  991. MESA_SHADER_STAGES, true);
  992. }
  993. /**
  994. * Accumulates the array of prog->UniformBlocks and checks that all
  995. * definitons of blocks agree on their contents.
  996. */
  997. static bool
  998. interstage_cross_validate_uniform_blocks(struct gl_shader_program *prog)
  999. {
  1000. unsigned max_num_uniform_blocks = 0;
  1001. for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
  1002. if (prog->_LinkedShaders[i])
  1003. max_num_uniform_blocks += prog->_LinkedShaders[i]->NumUniformBlocks;
  1004. }
  1005. for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
  1006. struct gl_shader *sh = prog->_LinkedShaders[i];
  1007. prog->UniformBlockStageIndex[i] = ralloc_array(prog, int,
  1008. max_num_uniform_blocks);
  1009. for (unsigned int j = 0; j < max_num_uniform_blocks; j++)
  1010. prog->UniformBlockStageIndex[i][j] = -1;
  1011. if (sh == NULL)
  1012. continue;
  1013. for (unsigned int j = 0; j < sh->NumUniformBlocks; j++) {
  1014. int index = link_cross_validate_uniform_block(prog,
  1015. &prog->UniformBlocks,
  1016. &prog->NumUniformBlocks,
  1017. &sh->UniformBlocks[j]);
  1018. if (index == -1) {
  1019. linker_error(prog, "uniform block `%s' has mismatching definitions\n",
  1020. sh->UniformBlocks[j].Name);
  1021. return false;
  1022. }
  1023. prog->UniformBlockStageIndex[i][index] = j;
  1024. }
  1025. }
  1026. return true;
  1027. }
  1028. /**
  1029. * Populates a shaders symbol table with all global declarations
  1030. */
  1031. static void
  1032. populate_symbol_table(gl_shader *sh)
  1033. {
  1034. sh->symbols = new(sh) glsl_symbol_table;
  1035. foreach_in_list(ir_instruction, inst, sh->ir) {
  1036. ir_variable *var;
  1037. ir_function *func;
  1038. if ((func = inst->as_function()) != NULL) {
  1039. sh->symbols->add_function(func);
  1040. } else if ((var = inst->as_variable()) != NULL) {
  1041. if (var->data.mode != ir_var_temporary)
  1042. sh->symbols->add_variable(var);
  1043. }
  1044. }
  1045. }
  1046. /**
  1047. * Remap variables referenced in an instruction tree
  1048. *
  1049. * This is used when instruction trees are cloned from one shader and placed in
  1050. * another. These trees will contain references to \c ir_variable nodes that
  1051. * do not exist in the target shader. This function finds these \c ir_variable
  1052. * references and replaces the references with matching variables in the target
  1053. * shader.
  1054. *
  1055. * If there is no matching variable in the target shader, a clone of the
  1056. * \c ir_variable is made and added to the target shader. The new variable is
  1057. * added to \b both the instruction stream and the symbol table.
  1058. *
  1059. * \param inst IR tree that is to be processed.
  1060. * \param symbols Symbol table containing global scope symbols in the
  1061. * linked shader.
  1062. * \param instructions Instruction stream where new variable declarations
  1063. * should be added.
  1064. */
  1065. void
  1066. remap_variables(ir_instruction *inst, struct gl_shader *target,
  1067. hash_table *temps)
  1068. {
  1069. class remap_visitor : public ir_hierarchical_visitor {
  1070. public:
  1071. remap_visitor(struct gl_shader *target,
  1072. hash_table *temps)
  1073. {
  1074. this->target = target;
  1075. this->symbols = target->symbols;
  1076. this->instructions = target->ir;
  1077. this->temps = temps;
  1078. }
  1079. virtual ir_visitor_status visit(ir_dereference_variable *ir)
  1080. {
  1081. if (ir->var->data.mode == ir_var_temporary) {
  1082. ir_variable *var = (ir_variable *) hash_table_find(temps, ir->var);
  1083. assert(var != NULL);
  1084. ir->var = var;
  1085. return visit_continue;
  1086. }
  1087. ir_variable *const existing =
  1088. this->symbols->get_variable(ir->var->name);
  1089. if (existing != NULL)
  1090. ir->var = existing;
  1091. else {
  1092. ir_variable *copy = ir->var->clone(this->target, NULL);
  1093. this->symbols->add_variable(copy);
  1094. this->instructions->push_head(copy);
  1095. ir->var = copy;
  1096. }
  1097. return visit_continue;
  1098. }
  1099. private:
  1100. struct gl_shader *target;
  1101. glsl_symbol_table *symbols;
  1102. exec_list *instructions;
  1103. hash_table *temps;
  1104. };
  1105. remap_visitor v(target, temps);
  1106. inst->accept(&v);
  1107. }
  1108. /**
  1109. * Move non-declarations from one instruction stream to another
  1110. *
  1111. * The intended usage pattern of this function is to pass the pointer to the
  1112. * head sentinel of a list (i.e., a pointer to the list cast to an \c exec_node
  1113. * pointer) for \c last and \c false for \c make_copies on the first
  1114. * call. Successive calls pass the return value of the previous call for
  1115. * \c last and \c true for \c make_copies.
  1116. *
  1117. * \param instructions Source instruction stream
  1118. * \param last Instruction after which new instructions should be
  1119. * inserted in the target instruction stream
  1120. * \param make_copies Flag selecting whether instructions in \c instructions
  1121. * should be copied (via \c ir_instruction::clone) into the
  1122. * target list or moved.
  1123. *
  1124. * \return
  1125. * The new "last" instruction in the target instruction stream. This pointer
  1126. * is suitable for use as the \c last parameter of a later call to this
  1127. * function.
  1128. */
  1129. exec_node *
  1130. move_non_declarations(exec_list *instructions, exec_node *last,
  1131. bool make_copies, gl_shader *target)
  1132. {
  1133. hash_table *temps = NULL;
  1134. if (make_copies)
  1135. temps = hash_table_ctor(0, hash_table_pointer_hash,
  1136. hash_table_pointer_compare);
  1137. foreach_in_list_safe(ir_instruction, inst, instructions) {
  1138. if (inst->as_function())
  1139. continue;
  1140. ir_variable *var = inst->as_variable();
  1141. if ((var != NULL) && (var->data.mode != ir_var_temporary))
  1142. continue;
  1143. assert(inst->as_assignment()
  1144. || inst->as_call()
  1145. || inst->as_if() /* for initializers with the ?: operator */
  1146. || ((var != NULL) && (var->data.mode == ir_var_temporary)));
  1147. if (make_copies) {
  1148. inst = inst->clone(target, NULL);
  1149. if (var != NULL)
  1150. hash_table_insert(temps, inst, var);
  1151. else
  1152. remap_variables(inst, target, temps);
  1153. } else {
  1154. inst->remove();
  1155. }
  1156. last->insert_after(inst);
  1157. last = inst;
  1158. }
  1159. if (make_copies)
  1160. hash_table_dtor(temps);
  1161. return last;
  1162. }
  1163. /**
  1164. * Get the function signature for main from a shader
  1165. */
  1166. ir_function_signature *
  1167. link_get_main_function_signature(gl_shader *sh)
  1168. {
  1169. ir_function *const f = sh->symbols->get_function("main");
  1170. if (f != NULL) {
  1171. exec_list void_parameters;
  1172. /* Look for the 'void main()' signature and ensure that it's defined.
  1173. * This keeps the linker from accidentally pick a shader that just
  1174. * contains a prototype for main.
  1175. *
  1176. * We don't have to check for multiple definitions of main (in multiple
  1177. * shaders) because that would have already been caught above.
  1178. */
  1179. ir_function_signature *sig =
  1180. f->matching_signature(NULL, &void_parameters, false);
  1181. if ((sig != NULL) && sig->is_defined) {
  1182. return sig;
  1183. }
  1184. }
  1185. return NULL;
  1186. }
  1187. /**
  1188. * This class is only used in link_intrastage_shaders() below but declaring
  1189. * it inside that function leads to compiler warnings with some versions of
  1190. * gcc.
  1191. */
  1192. class array_sizing_visitor : public ir_hierarchical_visitor {
  1193. public:
  1194. array_sizing_visitor()
  1195. : mem_ctx(ralloc_context(NULL)),
  1196. unnamed_interfaces(hash_table_ctor(0, hash_table_pointer_hash,
  1197. hash_table_pointer_compare))
  1198. {
  1199. }
  1200. ~array_sizing_visitor()
  1201. {
  1202. hash_table_dtor(this->unnamed_interfaces);
  1203. ralloc_free(this->mem_ctx);
  1204. }
  1205. virtual ir_visitor_status visit(ir_variable *var)
  1206. {
  1207. fixup_type(&var->type, var->data.max_array_access);
  1208. if (var->type->is_interface()) {
  1209. if (interface_contains_unsized_arrays(var->type)) {
  1210. const glsl_type *new_type =
  1211. resize_interface_members(var->type,
  1212. var->get_max_ifc_array_access());
  1213. var->type = new_type;
  1214. var->change_interface_type(new_type);
  1215. }
  1216. } else if (var->type->is_array() &&
  1217. var->type->fields.array->is_interface()) {
  1218. if (interface_contains_unsized_arrays(var->type->fields.array)) {
  1219. const glsl_type *new_type =
  1220. resize_interface_members(var->type->fields.array,
  1221. var->get_max_ifc_array_access());
  1222. var->change_interface_type(new_type);
  1223. var->type = update_interface_members_array(var->type, new_type);
  1224. }
  1225. } else if (const glsl_type *ifc_type = var->get_interface_type()) {
  1226. /* Store a pointer to the variable in the unnamed_interfaces
  1227. * hashtable.
  1228. */
  1229. ir_variable **interface_vars = (ir_variable **)
  1230. hash_table_find(this->unnamed_interfaces, ifc_type);
  1231. if (interface_vars == NULL) {
  1232. interface_vars = rzalloc_array(mem_ctx, ir_variable *,
  1233. ifc_type->length);
  1234. hash_table_insert(this->unnamed_interfaces, interface_vars,
  1235. ifc_type);
  1236. }
  1237. unsigned index = ifc_type->field_index(var->name);
  1238. assert(index < ifc_type->length);
  1239. assert(interface_vars[index] == NULL);
  1240. interface_vars[index] = var;
  1241. }
  1242. return visit_continue;
  1243. }
  1244. /**
  1245. * For each unnamed interface block that was discovered while running the
  1246. * visitor, adjust the interface type to reflect the newly assigned array
  1247. * sizes, and fix up the ir_variable nodes to point to the new interface
  1248. * type.
  1249. */
  1250. void fixup_unnamed_interface_types()
  1251. {
  1252. hash_table_call_foreach(this->unnamed_interfaces,
  1253. fixup_unnamed_interface_type, NULL);
  1254. }
  1255. private:
  1256. /**
  1257. * If the type pointed to by \c type represents an unsized array, replace
  1258. * it with a sized array whose size is determined by max_array_access.
  1259. */
  1260. static void fixup_type(const glsl_type **type, unsigned max_array_access)
  1261. {
  1262. if ((*type)->is_unsized_array()) {
  1263. *type = glsl_type::get_array_instance((*type)->fields.array,
  1264. max_array_access + 1);
  1265. assert(*type != NULL);
  1266. }
  1267. }
  1268. static const glsl_type *
  1269. update_interface_members_array(const glsl_type *type,
  1270. const glsl_type *new_interface_type)
  1271. {
  1272. const glsl_type *element_type = type->fields.array;
  1273. if (element_type->is_array()) {
  1274. const glsl_type *new_array_type =
  1275. update_interface_members_array(element_type, new_interface_type);
  1276. return glsl_type::get_array_instance(new_array_type, type->length);
  1277. } else {
  1278. return glsl_type::get_array_instance(new_interface_type,
  1279. type->length);
  1280. }
  1281. }
  1282. /**
  1283. * Determine whether the given interface type contains unsized arrays (if
  1284. * it doesn't, array_sizing_visitor doesn't need to process it).
  1285. */
  1286. static bool interface_contains_unsized_arrays(const glsl_type *type)
  1287. {
  1288. for (unsigned i = 0; i < type->length; i++) {
  1289. const glsl_type *elem_type = type->fields.structure[i].type;
  1290. if (elem_type->is_unsized_array())
  1291. return true;
  1292. }
  1293. return false;
  1294. }
  1295. /**
  1296. * Create a new interface type based on the given type, with unsized arrays
  1297. * replaced by sized arrays whose size is determined by
  1298. * max_ifc_array_access.
  1299. */
  1300. static const glsl_type *
  1301. resize_interface_members(const glsl_type *type,
  1302. const unsigned *max_ifc_array_access)
  1303. {
  1304. unsigned num_fields = type->length;
  1305. glsl_struct_field *fields = new glsl_struct_field[num_fields];
  1306. memcpy(fields, type->fields.structure,
  1307. num_fields * sizeof(*fields));
  1308. for (unsigned i = 0; i < num_fields; i++) {
  1309. fixup_type(&fields[i].type, max_ifc_array_access[i]);
  1310. }
  1311. glsl_interface_packing packing =
  1312. (glsl_interface_packing) type->interface_packing;
  1313. const glsl_type *new_ifc_type =
  1314. glsl_type::get_interface_instance(fields, num_fields,
  1315. packing, type->name);
  1316. delete [] fields;
  1317. return new_ifc_type;
  1318. }
  1319. static void fixup_unnamed_interface_type(const void *key, void *data,
  1320. void *)
  1321. {
  1322. const glsl_type *ifc_type = (const glsl_type *) key;
  1323. ir_variable **interface_vars = (ir_variable **) data;
  1324. unsigned num_fields = ifc_type->length;
  1325. glsl_struct_field *fields = new glsl_struct_field[num_fields];
  1326. memcpy(fields, ifc_type->fields.structure,
  1327. num_fields * sizeof(*fields));
  1328. bool interface_type_changed = false;
  1329. for (unsigned i = 0; i < num_fields; i++) {
  1330. if (interface_vars[i] != NULL &&
  1331. fields[i].type != interface_vars[i]->type) {
  1332. fields[i].type = interface_vars[i]->type;
  1333. interface_type_changed = true;
  1334. }
  1335. }
  1336. if (!interface_type_changed) {
  1337. delete [] fields;
  1338. return;
  1339. }
  1340. glsl_interface_packing packing =
  1341. (glsl_interface_packing) ifc_type->interface_packing;
  1342. const glsl_type *new_ifc_type =
  1343. glsl_type::get_interface_instance(fields, num_fields, packing,
  1344. ifc_type->name);
  1345. delete [] fields;
  1346. for (unsigned i = 0; i < num_fields; i++) {
  1347. if (interface_vars[i] != NULL)
  1348. interface_vars[i]->change_interface_type(new_ifc_type);
  1349. }
  1350. }
  1351. /**
  1352. * Memory context used to allocate the data in \c unnamed_interfaces.
  1353. */
  1354. void *mem_ctx;
  1355. /**
  1356. * Hash table from const glsl_type * to an array of ir_variable *'s
  1357. * pointing to the ir_variables constituting each unnamed interface block.
  1358. */
  1359. hash_table *unnamed_interfaces;
  1360. };
  1361. /**
  1362. * Performs the cross-validation of tessellation control shader vertices and
  1363. * layout qualifiers for the attached tessellation control shaders,
  1364. * and propagates them to the linked TCS and linked shader program.
  1365. */
  1366. static void
  1367. link_tcs_out_layout_qualifiers(struct gl_shader_program *prog,
  1368. struct gl_shader *linked_shader,
  1369. struct gl_shader **shader_list,
  1370. unsigned num_shaders)
  1371. {
  1372. linked_shader->TessCtrl.VerticesOut = 0;
  1373. if (linked_shader->Stage != MESA_SHADER_TESS_CTRL)
  1374. return;
  1375. /* From the GLSL 4.0 spec (chapter 4.3.8.2):
  1376. *
  1377. * "All tessellation control shader layout declarations in a program
  1378. * must specify the same output patch vertex count. There must be at
  1379. * least one layout qualifier specifying an output patch vertex count
  1380. * in any program containing tessellation control shaders; however,
  1381. * such a declaration is not required in all tessellation control
  1382. * shaders."
  1383. */
  1384. for (unsigned i = 0; i < num_shaders; i++) {
  1385. struct gl_shader *shader = shader_list[i];
  1386. if (shader->TessCtrl.VerticesOut != 0) {
  1387. if (linked_shader->TessCtrl.VerticesOut != 0 &&
  1388. linked_shader->TessCtrl.VerticesOut != shader->TessCtrl.VerticesOut) {
  1389. linker_error(prog, "tessellation control shader defined with "
  1390. "conflicting output vertex count (%d and %d)\n",
  1391. linked_shader->TessCtrl.VerticesOut,
  1392. shader->TessCtrl.VerticesOut);
  1393. return;
  1394. }
  1395. linked_shader->TessCtrl.VerticesOut = shader->TessCtrl.VerticesOut;
  1396. }
  1397. }
  1398. /* Just do the intrastage -> interstage propagation right now,
  1399. * since we already know we're in the right type of shader program
  1400. * for doing it.
  1401. */
  1402. if (linked_shader->TessCtrl.VerticesOut == 0) {
  1403. linker_error(prog, "tessellation control shader didn't declare "
  1404. "vertices out layout qualifier\n");
  1405. return;
  1406. }
  1407. prog->TessCtrl.VerticesOut = linked_shader->TessCtrl.VerticesOut;
  1408. }
  1409. /**
  1410. * Performs the cross-validation of tessellation evaluation shader
  1411. * primitive type, vertex spacing, ordering and point_mode layout qualifiers
  1412. * for the attached tessellation evaluation shaders, and propagates them
  1413. * to the linked TES and linked shader program.
  1414. */
  1415. static void
  1416. link_tes_in_layout_qualifiers(struct gl_shader_program *prog,
  1417. struct gl_shader *linked_shader,
  1418. struct gl_shader **shader_list,
  1419. unsigned num_shaders)
  1420. {
  1421. linked_shader->TessEval.PrimitiveMode = PRIM_UNKNOWN;
  1422. linked_shader->TessEval.Spacing = 0;
  1423. linked_shader->TessEval.VertexOrder = 0;
  1424. linked_shader->TessEval.PointMode = -1;
  1425. if (linked_shader->Stage != MESA_SHADER_TESS_EVAL)
  1426. return;
  1427. /* From the GLSL 4.0 spec (chapter 4.3.8.1):
  1428. *
  1429. * "At least one tessellation evaluation shader (compilation unit) in
  1430. * a program must declare a primitive mode in its input layout.
  1431. * Declaration vertex spacing, ordering, and point mode identifiers is
  1432. * optional. It is not required that all tessellation evaluation
  1433. * shaders in a program declare a primitive mode. If spacing or
  1434. * vertex ordering declarations are omitted, the tessellation
  1435. * primitive generator will use equal spacing or counter-clockwise
  1436. * vertex ordering, respectively. If a point mode declaration is
  1437. * omitted, the tessellation primitive generator will produce lines or
  1438. * triangles according to the primitive mode."
  1439. */
  1440. for (unsigned i = 0; i < num_shaders; i++) {
  1441. struct gl_shader *shader = shader_list[i];
  1442. if (shader->TessEval.PrimitiveMode != PRIM_UNKNOWN) {
  1443. if (linked_shader->TessEval.PrimitiveMode != PRIM_UNKNOWN &&
  1444. linked_shader->TessEval.PrimitiveMode != shader->TessEval.PrimitiveMode) {
  1445. linker_error(prog, "tessellation evaluation shader defined with "
  1446. "conflicting input primitive modes.\n");
  1447. return;
  1448. }
  1449. linked_shader->TessEval.PrimitiveMode = shader->TessEval.PrimitiveMode;
  1450. }
  1451. if (shader->TessEval.Spacing != 0) {
  1452. if (linked_shader->TessEval.Spacing != 0 &&
  1453. linked_shader->TessEval.Spacing != shader->TessEval.Spacing) {
  1454. linker_error(prog, "tessellation evaluation shader defined with "
  1455. "conflicting vertex spacing.\n");
  1456. return;
  1457. }
  1458. linked_shader->TessEval.Spacing = shader->TessEval.Spacing;
  1459. }
  1460. if (shader->TessEval.VertexOrder != 0) {
  1461. if (linked_shader->TessEval.VertexOrder != 0 &&
  1462. linked_shader->TessEval.VertexOrder != shader->TessEval.VertexOrder) {
  1463. linker_error(prog, "tessellation evaluation shader defined with "
  1464. "conflicting ordering.\n");
  1465. return;
  1466. }
  1467. linked_shader->TessEval.VertexOrder = shader->TessEval.VertexOrder;
  1468. }
  1469. if (shader->TessEval.PointMode != -1) {
  1470. if (linked_shader->TessEval.PointMode != -1 &&
  1471. linked_shader->TessEval.PointMode != shader->TessEval.PointMode) {
  1472. linker_error(prog, "tessellation evaluation shader defined with "
  1473. "conflicting point modes.\n");
  1474. return;
  1475. }
  1476. linked_shader->TessEval.PointMode = shader->TessEval.PointMode;
  1477. }
  1478. }
  1479. /* Just do the intrastage -> interstage propagation right now,
  1480. * since we already know we're in the right type of shader program
  1481. * for doing it.
  1482. */
  1483. if (linked_shader->TessEval.PrimitiveMode == PRIM_UNKNOWN) {
  1484. linker_error(prog,
  1485. "tessellation evaluation shader didn't declare input "
  1486. "primitive modes.\n");
  1487. return;
  1488. }
  1489. prog->TessEval.PrimitiveMode = linked_shader->TessEval.PrimitiveMode;
  1490. if (linked_shader->TessEval.Spacing == 0)
  1491. linked_shader->TessEval.Spacing = GL_EQUAL;
  1492. prog->TessEval.Spacing = linked_shader->TessEval.Spacing;
  1493. if (linked_shader->TessEval.VertexOrder == 0)
  1494. linked_shader->TessEval.VertexOrder = GL_CCW;
  1495. prog->TessEval.VertexOrder = linked_shader->TessEval.VertexOrder;
  1496. if (linked_shader->TessEval.PointMode == -1)
  1497. linked_shader->TessEval.PointMode = GL_FALSE;
  1498. prog->TessEval.PointMode = linked_shader->TessEval.PointMode;
  1499. }
  1500. /**
  1501. * Performs the cross-validation of layout qualifiers specified in
  1502. * redeclaration of gl_FragCoord for the attached fragment shaders,
  1503. * and propagates them to the linked FS and linked shader program.
  1504. */
  1505. static void
  1506. link_fs_input_layout_qualifiers(struct gl_shader_program *prog,
  1507. struct gl_shader *linked_shader,
  1508. struct gl_shader **shader_list,
  1509. unsigned num_shaders)
  1510. {
  1511. linked_shader->redeclares_gl_fragcoord = false;
  1512. linked_shader->uses_gl_fragcoord = false;
  1513. linked_shader->origin_upper_left = false;
  1514. linked_shader->pixel_center_integer = false;
  1515. if (linked_shader->Stage != MESA_SHADER_FRAGMENT ||
  1516. (prog->Version < 150 && !prog->ARB_fragment_coord_conventions_enable))
  1517. return;
  1518. for (unsigned i = 0; i < num_shaders; i++) {
  1519. struct gl_shader *shader = shader_list[i];
  1520. /* From the GLSL 1.50 spec, page 39:
  1521. *
  1522. * "If gl_FragCoord is redeclared in any fragment shader in a program,
  1523. * it must be redeclared in all the fragment shaders in that program
  1524. * that have a static use gl_FragCoord."
  1525. */
  1526. if ((linked_shader->redeclares_gl_fragcoord
  1527. && !shader->redeclares_gl_fragcoord
  1528. && shader->uses_gl_fragcoord)
  1529. || (shader->redeclares_gl_fragcoord
  1530. && !linked_shader->redeclares_gl_fragcoord
  1531. && linked_shader->uses_gl_fragcoord)) {
  1532. linker_error(prog, "fragment shader defined with conflicting "
  1533. "layout qualifiers for gl_FragCoord\n");
  1534. }
  1535. /* From the GLSL 1.50 spec, page 39:
  1536. *
  1537. * "All redeclarations of gl_FragCoord in all fragment shaders in a
  1538. * single program must have the same set of qualifiers."
  1539. */
  1540. if (linked_shader->redeclares_gl_fragcoord && shader->redeclares_gl_fragcoord
  1541. && (shader->origin_upper_left != linked_shader->origin_upper_left
  1542. || shader->pixel_center_integer != linked_shader->pixel_center_integer)) {
  1543. linker_error(prog, "fragment shader defined with conflicting "
  1544. "layout qualifiers for gl_FragCoord\n");
  1545. }
  1546. /* Update the linked shader state. Note that uses_gl_fragcoord should
  1547. * accumulate the results. The other values should replace. If there
  1548. * are multiple redeclarations, all the fields except uses_gl_fragcoord
  1549. * are already known to be the same.
  1550. */
  1551. if (shader->redeclares_gl_fragcoord || shader->uses_gl_fragcoord) {
  1552. linked_shader->redeclares_gl_fragcoord =
  1553. shader->redeclares_gl_fragcoord;
  1554. linked_shader->uses_gl_fragcoord = linked_shader->uses_gl_fragcoord
  1555. || shader->uses_gl_fragcoord;
  1556. linked_shader->origin_upper_left = shader->origin_upper_left;
  1557. linked_shader->pixel_center_integer = shader->pixel_center_integer;
  1558. }
  1559. linked_shader->EarlyFragmentTests |= shader->EarlyFragmentTests;
  1560. }
  1561. }
  1562. /**
  1563. * Performs the cross-validation of geometry shader max_vertices and
  1564. * primitive type layout qualifiers for the attached geometry shaders,
  1565. * and propagates them to the linked GS and linked shader program.
  1566. */
  1567. static void
  1568. link_gs_inout_layout_qualifiers(struct gl_shader_program *prog,
  1569. struct gl_shader *linked_shader,
  1570. struct gl_shader **shader_list,
  1571. unsigned num_shaders)
  1572. {
  1573. linked_shader->Geom.VerticesOut = 0;
  1574. linked_shader->Geom.Invocations = 0;
  1575. linked_shader->Geom.InputType = PRIM_UNKNOWN;
  1576. linked_shader->Geom.OutputType = PRIM_UNKNOWN;
  1577. /* No in/out qualifiers defined for anything but GLSL 1.50+
  1578. * geometry shaders so far.
  1579. */
  1580. if (linked_shader->Stage != MESA_SHADER_GEOMETRY || prog->Version < 150)
  1581. return;
  1582. /* From the GLSL 1.50 spec, page 46:
  1583. *
  1584. * "All geometry shader output layout declarations in a program
  1585. * must declare the same layout and same value for
  1586. * max_vertices. There must be at least one geometry output
  1587. * layout declaration somewhere in a program, but not all
  1588. * geometry shaders (compilation units) are required to
  1589. * declare it."
  1590. */
  1591. for (unsigned i = 0; i < num_shaders; i++) {
  1592. struct gl_shader *shader = shader_list[i];
  1593. if (shader->Geom.InputType != PRIM_UNKNOWN) {
  1594. if (linked_shader->Geom.InputType != PRIM_UNKNOWN &&
  1595. linked_shader->Geom.InputType != shader->Geom.InputType) {
  1596. linker_error(prog, "geometry shader defined with conflicting "
  1597. "input types\n");
  1598. return;
  1599. }
  1600. linked_shader->Geom.InputType = shader->Geom.InputType;
  1601. }
  1602. if (shader->Geom.OutputType != PRIM_UNKNOWN) {
  1603. if (linked_shader->Geom.OutputType != PRIM_UNKNOWN &&
  1604. linked_shader->Geom.OutputType != shader->Geom.OutputType) {
  1605. linker_error(prog, "geometry shader defined with conflicting "
  1606. "output types\n");
  1607. return;
  1608. }
  1609. linked_shader->Geom.OutputType = shader->Geom.OutputType;
  1610. }
  1611. if (shader->Geom.VerticesOut != 0) {
  1612. if (linked_shader->Geom.VerticesOut != 0 &&
  1613. linked_shader->Geom.VerticesOut != shader->Geom.VerticesOut) {
  1614. linker_error(prog, "geometry shader defined with conflicting "
  1615. "output vertex count (%d and %d)\n",
  1616. linked_shader->Geom.VerticesOut,
  1617. shader->Geom.VerticesOut);
  1618. return;
  1619. }
  1620. linked_shader->Geom.VerticesOut = shader->Geom.VerticesOut;
  1621. }
  1622. if (shader->Geom.Invocations != 0) {
  1623. if (linked_shader->Geom.Invocations != 0 &&
  1624. linked_shader->Geom.Invocations != shader->Geom.Invocations) {
  1625. linker_error(prog, "geometry shader defined with conflicting "
  1626. "invocation count (%d and %d)\n",
  1627. linked_shader->Geom.Invocations,
  1628. shader->Geom.Invocations);
  1629. return;
  1630. }
  1631. linked_shader->Geom.Invocations = shader->Geom.Invocations;
  1632. }
  1633. }
  1634. /* Just do the intrastage -> interstage propagation right now,
  1635. * since we already know we're in the right type of shader program
  1636. * for doing it.
  1637. */
  1638. if (linked_shader->Geom.InputType == PRIM_UNKNOWN) {
  1639. linker_error(prog,
  1640. "geometry shader didn't declare primitive input type\n");
  1641. return;
  1642. }
  1643. prog->Geom.InputType = linked_shader->Geom.InputType;
  1644. if (linked_shader->Geom.OutputType == PRIM_UNKNOWN) {
  1645. linker_error(prog,
  1646. "geometry shader didn't declare primitive output type\n");
  1647. return;
  1648. }
  1649. prog->Geom.OutputType = linked_shader->Geom.OutputType;
  1650. if (linked_shader->Geom.VerticesOut == 0) {
  1651. linker_error(prog,
  1652. "geometry shader didn't declare max_vertices\n");
  1653. return;
  1654. }
  1655. prog->Geom.VerticesOut = linked_shader->Geom.VerticesOut;
  1656. if (linked_shader->Geom.Invocations == 0)
  1657. linked_shader->Geom.Invocations = 1;
  1658. prog->Geom.Invocations = linked_shader->Geom.Invocations;
  1659. }
  1660. /**
  1661. * Perform cross-validation of compute shader local_size_{x,y,z} layout
  1662. * qualifiers for the attached compute shaders, and propagate them to the
  1663. * linked CS and linked shader program.
  1664. */
  1665. static void
  1666. link_cs_input_layout_qualifiers(struct gl_shader_program *prog,
  1667. struct gl_shader *linked_shader,
  1668. struct gl_shader **shader_list,
  1669. unsigned num_shaders)
  1670. {
  1671. for (int i = 0; i < 3; i++)
  1672. linked_shader->Comp.LocalSize[i] = 0;
  1673. /* This function is called for all shader stages, but it only has an effect
  1674. * for compute shaders.
  1675. */
  1676. if (linked_shader->Stage != MESA_SHADER_COMPUTE)
  1677. return;
  1678. /* From the ARB_compute_shader spec, in the section describing local size
  1679. * declarations:
  1680. *
  1681. * If multiple compute shaders attached to a single program object
  1682. * declare local work-group size, the declarations must be identical;
  1683. * otherwise a link-time error results. Furthermore, if a program
  1684. * object contains any compute shaders, at least one must contain an
  1685. * input layout qualifier specifying the local work sizes of the
  1686. * program, or a link-time error will occur.
  1687. */
  1688. for (unsigned sh = 0; sh < num_shaders; sh++) {
  1689. struct gl_shader *shader = shader_list[sh];
  1690. if (shader->Comp.LocalSize[0] != 0) {
  1691. if (linked_shader->Comp.LocalSize[0] != 0) {
  1692. for (int i = 0; i < 3; i++) {
  1693. if (linked_shader->Comp.LocalSize[i] !=
  1694. shader->Comp.LocalSize[i]) {
  1695. linker_error(prog, "compute shader defined with conflicting "
  1696. "local sizes\n");
  1697. return;
  1698. }
  1699. }
  1700. }
  1701. for (int i = 0; i < 3; i++)
  1702. linked_shader->Comp.LocalSize[i] = shader->Comp.LocalSize[i];
  1703. }
  1704. }
  1705. /* Just do the intrastage -> interstage propagation right now,
  1706. * since we already know we're in the right type of shader program
  1707. * for doing it.
  1708. */
  1709. if (linked_shader->Comp.LocalSize[0] == 0) {
  1710. linker_error(prog, "compute shader didn't declare local size\n");
  1711. return;
  1712. }
  1713. for (int i = 0; i < 3; i++)
  1714. prog->Comp.LocalSize[i] = linked_shader->Comp.LocalSize[i];
  1715. }
  1716. /**
  1717. * Combine a group of shaders for a single stage to generate a linked shader
  1718. *
  1719. * \note
  1720. * If this function is supplied a single shader, it is cloned, and the new
  1721. * shader is returned.
  1722. */
  1723. static struct gl_shader *
  1724. link_intrastage_shaders(void *mem_ctx,
  1725. struct gl_context *ctx,
  1726. struct gl_shader_program *prog,
  1727. struct gl_shader **shader_list,
  1728. unsigned num_shaders)
  1729. {
  1730. struct gl_uniform_block *uniform_blocks = NULL;
  1731. /* Check that global variables defined in multiple shaders are consistent.
  1732. */
  1733. cross_validate_globals(prog, shader_list, num_shaders, false);
  1734. if (!prog->LinkStatus)
  1735. return NULL;
  1736. /* Check that interface blocks defined in multiple shaders are consistent.
  1737. */
  1738. validate_intrastage_interface_blocks(prog, (const gl_shader **)shader_list,
  1739. num_shaders);
  1740. if (!prog->LinkStatus)
  1741. return NULL;
  1742. /* Link up uniform blocks defined within this stage. */
  1743. const unsigned num_uniform_blocks =
  1744. link_uniform_blocks(mem_ctx, prog, shader_list, num_shaders,
  1745. &uniform_blocks);
  1746. if (!prog->LinkStatus)
  1747. return NULL;
  1748. /* Check that there is only a single definition of each function signature
  1749. * across all shaders.
  1750. */
  1751. for (unsigned i = 0; i < (num_shaders - 1); i++) {
  1752. foreach_in_list(ir_instruction, node, shader_list[i]->ir) {
  1753. ir_function *const f = node->as_function();
  1754. if (f == NULL)
  1755. continue;
  1756. for (unsigned j = i + 1; j < num_shaders; j++) {
  1757. ir_function *const other =
  1758. shader_list[j]->symbols->get_function(f->name);
  1759. /* If the other shader has no function (and therefore no function
  1760. * signatures) with the same name, skip to the next shader.
  1761. */
  1762. if (other == NULL)
  1763. continue;
  1764. foreach_in_list(ir_function_signature, sig, &f->signatures) {
  1765. if (!sig->is_defined || sig->is_builtin())
  1766. continue;
  1767. ir_function_signature *other_sig =
  1768. other->exact_matching_signature(NULL, &sig->parameters);
  1769. if ((other_sig != NULL) && other_sig->is_defined
  1770. && !other_sig->is_builtin()) {
  1771. linker_error(prog, "function `%s' is multiply defined\n",
  1772. f->name);
  1773. return NULL;
  1774. }
  1775. }
  1776. }
  1777. }
  1778. }
  1779. /* Find the shader that defines main, and make a clone of it.
  1780. *
  1781. * Starting with the clone, search for undefined references. If one is
  1782. * found, find the shader that defines it. Clone the reference and add
  1783. * it to the shader. Repeat until there are no undefined references or
  1784. * until a reference cannot be resolved.
  1785. */
  1786. gl_shader *main = NULL;
  1787. for (unsigned i = 0; i < num_shaders; i++) {
  1788. if (link_get_main_function_signature(shader_list[i]) != NULL) {
  1789. main = shader_list[i];
  1790. break;
  1791. }
  1792. }
  1793. if (main == NULL) {
  1794. linker_error(prog, "%s shader lacks `main'\n",
  1795. _mesa_shader_stage_to_string(shader_list[0]->Stage));
  1796. return NULL;
  1797. }
  1798. gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
  1799. linked->ir = new(linked) exec_list;
  1800. clone_ir_list(mem_ctx, linked->ir, main->ir);
  1801. linked->UniformBlocks = uniform_blocks;
  1802. linked->NumUniformBlocks = num_uniform_blocks;
  1803. ralloc_steal(linked, linked->UniformBlocks);
  1804. link_fs_input_layout_qualifiers(prog, linked, shader_list, num_shaders);
  1805. link_tcs_out_layout_qualifiers(prog, linked, shader_list, num_shaders);
  1806. link_tes_in_layout_qualifiers(prog, linked, shader_list, num_shaders);
  1807. link_gs_inout_layout_qualifiers(prog, linked, shader_list, num_shaders);
  1808. link_cs_input_layout_qualifiers(prog, linked, shader_list, num_shaders);
  1809. populate_symbol_table(linked);
  1810. /* The pointer to the main function in the final linked shader (i.e., the
  1811. * copy of the original shader that contained the main function).
  1812. */
  1813. ir_function_signature *const main_sig =
  1814. link_get_main_function_signature(linked);
  1815. /* Move any instructions other than variable declarations or function
  1816. * declarations into main.
  1817. */
  1818. exec_node *insertion_point =
  1819. move_non_declarations(linked->ir, (exec_node *) &main_sig->body, false,
  1820. linked);
  1821. for (unsigned i = 0; i < num_shaders; i++) {
  1822. if (shader_list[i] == main)
  1823. continue;
  1824. insertion_point = move_non_declarations(shader_list[i]->ir,
  1825. insertion_point, true, linked);
  1826. }
  1827. /* Check if any shader needs built-in functions. */
  1828. bool need_builtins = false;
  1829. for (unsigned i = 0; i < num_shaders; i++) {
  1830. if (shader_list[i]->uses_builtin_functions) {
  1831. need_builtins = true;
  1832. break;
  1833. }
  1834. }
  1835. bool ok;
  1836. if (need_builtins) {
  1837. /* Make a temporary array one larger than shader_list, which will hold
  1838. * the built-in function shader as well.
  1839. */
  1840. gl_shader **linking_shaders = (gl_shader **)
  1841. calloc(num_shaders + 1, sizeof(gl_shader *));
  1842. ok = linking_shaders != NULL;
  1843. if (ok) {
  1844. memcpy(linking_shaders, shader_list, num_shaders * sizeof(gl_shader *));
  1845. linking_shaders[num_shaders] = _mesa_glsl_get_builtin_function_shader();
  1846. ok = link_function_calls(prog, linked, linking_shaders, num_shaders + 1);
  1847. free(linking_shaders);
  1848. } else {
  1849. _mesa_error_no_memory(__func__);
  1850. }
  1851. } else {
  1852. ok = link_function_calls(prog, linked, shader_list, num_shaders);
  1853. }
  1854. if (!ok) {
  1855. ctx->Driver.DeleteShader(ctx, linked);
  1856. return NULL;
  1857. }
  1858. /* At this point linked should contain all of the linked IR, so
  1859. * validate it to make sure nothing went wrong.
  1860. */
  1861. validate_ir_tree(linked->ir);
  1862. /* Set the size of geometry shader input arrays */
  1863. if (linked->Stage == MESA_SHADER_GEOMETRY) {
  1864. unsigned num_vertices = vertices_per_prim(prog->Geom.InputType);
  1865. geom_array_resize_visitor input_resize_visitor(num_vertices, prog);
  1866. foreach_in_list(ir_instruction, ir, linked->ir) {
  1867. ir->accept(&input_resize_visitor);
  1868. }
  1869. }
  1870. if (ctx->Const.VertexID_is_zero_based)
  1871. lower_vertex_id(linked);
  1872. /* Validate correct usage of barrier() in the tess control shader */
  1873. if (linked->Stage == MESA_SHADER_TESS_CTRL) {
  1874. barrier_use_visitor visitor(prog);
  1875. foreach_in_list(ir_instruction, ir, linked->ir) {
  1876. ir->accept(&visitor);
  1877. }
  1878. }
  1879. /* Make a pass over all variable declarations to ensure that arrays with
  1880. * unspecified sizes have a size specified. The size is inferred from the
  1881. * max_array_access field.
  1882. */
  1883. array_sizing_visitor v;
  1884. v.run(linked->ir);
  1885. v.fixup_unnamed_interface_types();
  1886. return linked;
  1887. }
  1888. /**
  1889. * Update the sizes of linked shader uniform arrays to the maximum
  1890. * array index used.
  1891. *
  1892. * From page 81 (page 95 of the PDF) of the OpenGL 2.1 spec:
  1893. *
  1894. * If one or more elements of an array are active,
  1895. * GetActiveUniform will return the name of the array in name,
  1896. * subject to the restrictions listed above. The type of the array
  1897. * is returned in type. The size parameter contains the highest
  1898. * array element index used, plus one. The compiler or linker
  1899. * determines the highest index used. There will be only one
  1900. * active uniform reported by the GL per uniform array.
  1901. */
  1902. static void
  1903. update_array_sizes(struct gl_shader_program *prog)
  1904. {
  1905. for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
  1906. if (prog->_LinkedShaders[i] == NULL)
  1907. continue;
  1908. foreach_in_list(ir_instruction, node, prog->_LinkedShaders[i]->ir) {
  1909. ir_variable *const var = node->as_variable();
  1910. if ((var == NULL) || (var->data.mode != ir_var_uniform) ||
  1911. !var->type->is_array())
  1912. continue;
  1913. /* GL_ARB_uniform_buffer_object says that std140 uniforms
  1914. * will not be eliminated. Since we always do std140, just
  1915. * don't resize arrays in UBOs.
  1916. *
  1917. * Atomic counters are supposed to get deterministic
  1918. * locations assigned based on the declaration ordering and
  1919. * sizes, array compaction would mess that up.
  1920. *
  1921. * Subroutine uniforms are not removed.
  1922. */
  1923. if (var->is_in_buffer_block() || var->type->contains_atomic() ||
  1924. var->type->contains_subroutine())
  1925. continue;
  1926. unsigned int size = var->data.max_array_access;
  1927. for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
  1928. if (prog->_LinkedShaders[j] == NULL)
  1929. continue;
  1930. foreach_in_list(ir_instruction, node2, prog->_LinkedShaders[j]->ir) {
  1931. ir_variable *other_var = node2->as_variable();
  1932. if (!other_var)
  1933. continue;
  1934. if (strcmp(var->name, other_var->name) == 0 &&
  1935. other_var->data.max_array_access > size) {
  1936. size = other_var->data.max_array_access;
  1937. }
  1938. }
  1939. }
  1940. if (size + 1 != var->type->length) {
  1941. /* If this is a built-in uniform (i.e., it's backed by some
  1942. * fixed-function state), adjust the number of state slots to
  1943. * match the new array size. The number of slots per array entry
  1944. * is not known. It seems safe to assume that the total number of
  1945. * slots is an integer multiple of the number of array elements.
  1946. * Determine the number of slots per array element by dividing by
  1947. * the old (total) size.
  1948. */
  1949. const unsigned num_slots = var->get_num_state_slots();
  1950. if (num_slots > 0) {
  1951. var->set_num_state_slots((size + 1)
  1952. * (num_slots / var->type->length));
  1953. }
  1954. var->type = glsl_type::get_array_instance(var->type->fields.array,
  1955. size + 1);
  1956. /* FINISHME: We should update the types of array
  1957. * dereferences of this variable now.
  1958. */
  1959. }
  1960. }
  1961. }
  1962. }
  1963. /**
  1964. * Resize tessellation evaluation per-vertex inputs to the size of
  1965. * tessellation control per-vertex outputs.
  1966. */
  1967. static void
  1968. resize_tes_inputs(struct gl_context *ctx,
  1969. struct gl_shader_program *prog)
  1970. {
  1971. if (prog->_LinkedShaders[MESA_SHADER_TESS_EVAL] == NULL)
  1972. return;
  1973. gl_shader *const tcs = prog->_LinkedShaders[MESA_SHADER_TESS_CTRL];
  1974. gl_shader *const tes = prog->_LinkedShaders[MESA_SHADER_TESS_EVAL];
  1975. /* If no control shader is present, then the TES inputs are statically
  1976. * sized to MaxPatchVertices; the actual size of the arrays won't be
  1977. * known until draw time.
  1978. */
  1979. const int num_vertices = tcs
  1980. ? tcs->TessCtrl.VerticesOut
  1981. : ctx->Const.MaxPatchVertices;
  1982. tess_eval_array_resize_visitor input_resize_visitor(num_vertices, prog);
  1983. foreach_in_list(ir_instruction, ir, tes->ir) {
  1984. ir->accept(&input_resize_visitor);
  1985. }
  1986. }
  1987. /**
  1988. * Find a contiguous set of available bits in a bitmask.
  1989. *
  1990. * \param used_mask Bits representing used (1) and unused (0) locations
  1991. * \param needed_count Number of contiguous bits needed.
  1992. *
  1993. * \return
  1994. * Base location of the available bits on success or -1 on failure.
  1995. */
  1996. int
  1997. find_available_slots(unsigned used_mask, unsigned needed_count)
  1998. {
  1999. unsigned needed_mask = (1 << needed_count) - 1;
  2000. const int max_bit_to_test = (8 * sizeof(used_mask)) - needed_count;
  2001. /* The comparison to 32 is redundant, but without it GCC emits "warning:
  2002. * cannot optimize possibly infinite loops" for the loop below.
  2003. */
  2004. if ((needed_count == 0) || (max_bit_to_test < 0) || (max_bit_to_test > 32))
  2005. return -1;
  2006. for (int i = 0; i <= max_bit_to_test; i++) {
  2007. if ((needed_mask & ~used_mask) == needed_mask)
  2008. return i;
  2009. needed_mask <<= 1;
  2010. }
  2011. return -1;
  2012. }
  2013. /**
  2014. * Assign locations for either VS inputs or FS outputs
  2015. *
  2016. * \param prog Shader program whose variables need locations assigned
  2017. * \param constants Driver specific constant values for the program.
  2018. * \param target_index Selector for the program target to receive location
  2019. * assignmnets. Must be either \c MESA_SHADER_VERTEX or
  2020. * \c MESA_SHADER_FRAGMENT.
  2021. *
  2022. * \return
  2023. * If locations are successfully assigned, true is returned. Otherwise an
  2024. * error is emitted to the shader link log and false is returned.
  2025. */
  2026. bool
  2027. assign_attribute_or_color_locations(gl_shader_program *prog,
  2028. struct gl_constants *constants,
  2029. unsigned target_index)
  2030. {
  2031. /* Maximum number of generic locations. This corresponds to either the
  2032. * maximum number of draw buffers or the maximum number of generic
  2033. * attributes.
  2034. */
  2035. unsigned max_index = (target_index == MESA_SHADER_VERTEX) ?
  2036. constants->Program[target_index].MaxAttribs :
  2037. MAX2(constants->MaxDrawBuffers, constants->MaxDualSourceDrawBuffers);
  2038. /* Mark invalid locations as being used.
  2039. */
  2040. unsigned used_locations = (max_index >= 32)
  2041. ? ~0 : ~((1 << max_index) - 1);
  2042. unsigned double_storage_locations = 0;
  2043. assert((target_index == MESA_SHADER_VERTEX)
  2044. || (target_index == MESA_SHADER_FRAGMENT));
  2045. gl_shader *const sh = prog->_LinkedShaders[target_index];
  2046. if (sh == NULL)
  2047. return true;
  2048. /* Operate in a total of four passes.
  2049. *
  2050. * 1. Invalidate the location assignments for all vertex shader inputs.
  2051. *
  2052. * 2. Assign locations for inputs that have user-defined (via
  2053. * glBindVertexAttribLocation) locations and outputs that have
  2054. * user-defined locations (via glBindFragDataLocation).
  2055. *
  2056. * 3. Sort the attributes without assigned locations by number of slots
  2057. * required in decreasing order. Fragmentation caused by attribute
  2058. * locations assigned by the application may prevent large attributes
  2059. * from having enough contiguous space.
  2060. *
  2061. * 4. Assign locations to any inputs without assigned locations.
  2062. */
  2063. const int generic_base = (target_index == MESA_SHADER_VERTEX)
  2064. ? (int) VERT_ATTRIB_GENERIC0 : (int) FRAG_RESULT_DATA0;
  2065. const enum ir_variable_mode direction =
  2066. (target_index == MESA_SHADER_VERTEX)
  2067. ? ir_var_shader_in : ir_var_shader_out;
  2068. /* Temporary storage for the set of attributes that need locations assigned.
  2069. */
  2070. struct temp_attr {
  2071. unsigned slots;
  2072. ir_variable *var;
  2073. /* Used below in the call to qsort. */
  2074. static int compare(const void *a, const void *b)
  2075. {
  2076. const temp_attr *const l = (const temp_attr *) a;
  2077. const temp_attr *const r = (const temp_attr *) b;
  2078. /* Reversed because we want a descending order sort below. */
  2079. return r->slots - l->slots;
  2080. }
  2081. } to_assign[16];
  2082. unsigned num_attr = 0;
  2083. foreach_in_list(ir_instruction, node, sh->ir) {
  2084. ir_variable *const var = node->as_variable();
  2085. if ((var == NULL) || (var->data.mode != (unsigned) direction))
  2086. continue;
  2087. if (var->data.explicit_location) {
  2088. if ((var->data.location >= (int)(max_index + generic_base))
  2089. || (var->data.location < 0)) {
  2090. linker_error(prog,
  2091. "invalid explicit location %d specified for `%s'\n",
  2092. (var->data.location < 0)
  2093. ? var->data.location
  2094. : var->data.location - generic_base,
  2095. var->name);
  2096. return false;
  2097. }
  2098. } else if (target_index == MESA_SHADER_VERTEX) {
  2099. unsigned binding;
  2100. if (prog->AttributeBindings->get(binding, var->name)) {
  2101. assert(binding >= VERT_ATTRIB_GENERIC0);
  2102. var->data.location = binding;
  2103. var->data.is_unmatched_generic_inout = 0;
  2104. }
  2105. } else if (target_index == MESA_SHADER_FRAGMENT) {
  2106. unsigned binding;
  2107. unsigned index;
  2108. if (prog->FragDataBindings->get(binding, var->name)) {
  2109. assert(binding >= FRAG_RESULT_DATA0);
  2110. var->data.location = binding;
  2111. var->data.is_unmatched_generic_inout = 0;
  2112. if (prog->FragDataIndexBindings->get(index, var->name)) {
  2113. var->data.index = index;
  2114. }
  2115. }
  2116. }
  2117. /* From GL4.5 core spec, section 15.2 (Shader Execution):
  2118. *
  2119. * "Output binding assignments will cause LinkProgram to fail:
  2120. * ...
  2121. * If the program has an active output assigned to a location greater
  2122. * than or equal to the value of MAX_DUAL_SOURCE_DRAW_BUFFERS and has
  2123. * an active output assigned an index greater than or equal to one;"
  2124. */
  2125. if (target_index == MESA_SHADER_FRAGMENT && var->data.index >= 1 &&
  2126. var->data.location - generic_base >=
  2127. (int) constants->MaxDualSourceDrawBuffers) {
  2128. linker_error(prog,
  2129. "output location %d >= GL_MAX_DUAL_SOURCE_DRAW_BUFFERS "
  2130. "with index %u for %s\n",
  2131. var->data.location - generic_base, var->data.index,
  2132. var->name);
  2133. return false;
  2134. }
  2135. const unsigned slots = var->type->count_attribute_slots();
  2136. /* If the variable is not a built-in and has a location statically
  2137. * assigned in the shader (presumably via a layout qualifier), make sure
  2138. * that it doesn't collide with other assigned locations. Otherwise,
  2139. * add it to the list of variables that need linker-assigned locations.
  2140. */
  2141. if (var->data.location != -1) {
  2142. if (var->data.location >= generic_base && var->data.index < 1) {
  2143. /* From page 61 of the OpenGL 4.0 spec:
  2144. *
  2145. * "LinkProgram will fail if the attribute bindings assigned
  2146. * by BindAttribLocation do not leave not enough space to
  2147. * assign a location for an active matrix attribute or an
  2148. * active attribute array, both of which require multiple
  2149. * contiguous generic attributes."
  2150. *
  2151. * I think above text prohibits the aliasing of explicit and
  2152. * automatic assignments. But, aliasing is allowed in manual
  2153. * assignments of attribute locations. See below comments for
  2154. * the details.
  2155. *
  2156. * From OpenGL 4.0 spec, page 61:
  2157. *
  2158. * "It is possible for an application to bind more than one
  2159. * attribute name to the same location. This is referred to as
  2160. * aliasing. This will only work if only one of the aliased
  2161. * attributes is active in the executable program, or if no
  2162. * path through the shader consumes more than one attribute of
  2163. * a set of attributes aliased to the same location. A link
  2164. * error can occur if the linker determines that every path
  2165. * through the shader consumes multiple aliased attributes,
  2166. * but implementations are not required to generate an error
  2167. * in this case."
  2168. *
  2169. * From GLSL 4.30 spec, page 54:
  2170. *
  2171. * "A program will fail to link if any two non-vertex shader
  2172. * input variables are assigned to the same location. For
  2173. * vertex shaders, multiple input variables may be assigned
  2174. * to the same location using either layout qualifiers or via
  2175. * the OpenGL API. However, such aliasing is intended only to
  2176. * support vertex shaders where each execution path accesses
  2177. * at most one input per each location. Implementations are
  2178. * permitted, but not required, to generate link-time errors
  2179. * if they detect that every path through the vertex shader
  2180. * executable accesses multiple inputs assigned to any single
  2181. * location. For all shader types, a program will fail to link
  2182. * if explicit location assignments leave the linker unable
  2183. * to find space for other variables without explicit
  2184. * assignments."
  2185. *
  2186. * From OpenGL ES 3.0 spec, page 56:
  2187. *
  2188. * "Binding more than one attribute name to the same location
  2189. * is referred to as aliasing, and is not permitted in OpenGL
  2190. * ES Shading Language 3.00 vertex shaders. LinkProgram will
  2191. * fail when this condition exists. However, aliasing is
  2192. * possible in OpenGL ES Shading Language 1.00 vertex shaders.
  2193. * This will only work if only one of the aliased attributes
  2194. * is active in the executable program, or if no path through
  2195. * the shader consumes more than one attribute of a set of
  2196. * attributes aliased to the same location. A link error can
  2197. * occur if the linker determines that every path through the
  2198. * shader consumes multiple aliased attributes, but implemen-
  2199. * tations are not required to generate an error in this case."
  2200. *
  2201. * After looking at above references from OpenGL, OpenGL ES and
  2202. * GLSL specifications, we allow aliasing of vertex input variables
  2203. * in: OpenGL 2.0 (and above) and OpenGL ES 2.0.
  2204. *
  2205. * NOTE: This is not required by the spec but its worth mentioning
  2206. * here that we're not doing anything to make sure that no path
  2207. * through the vertex shader executable accesses multiple inputs
  2208. * assigned to any single location.
  2209. */
  2210. /* Mask representing the contiguous slots that will be used by
  2211. * this attribute.
  2212. */
  2213. const unsigned attr = var->data.location - generic_base;
  2214. const unsigned use_mask = (1 << slots) - 1;
  2215. const char *const string = (target_index == MESA_SHADER_VERTEX)
  2216. ? "vertex shader input" : "fragment shader output";
  2217. /* Generate a link error if the requested locations for this
  2218. * attribute exceed the maximum allowed attribute location.
  2219. */
  2220. if (attr + slots > max_index) {
  2221. linker_error(prog,
  2222. "insufficient contiguous locations "
  2223. "available for %s `%s' %d %d %d\n", string,
  2224. var->name, used_locations, use_mask, attr);
  2225. return false;
  2226. }
  2227. /* Generate a link error if the set of bits requested for this
  2228. * attribute overlaps any previously allocated bits.
  2229. */
  2230. if ((~(use_mask << attr) & used_locations) != used_locations) {
  2231. if (target_index == MESA_SHADER_FRAGMENT ||
  2232. (prog->IsES && prog->Version >= 300)) {
  2233. linker_error(prog,
  2234. "overlapping location is assigned "
  2235. "to %s `%s' %d %d %d\n", string,
  2236. var->name, used_locations, use_mask, attr);
  2237. return false;
  2238. } else {
  2239. linker_warning(prog,
  2240. "overlapping location is assigned "
  2241. "to %s `%s' %d %d %d\n", string,
  2242. var->name, used_locations, use_mask, attr);
  2243. }
  2244. }
  2245. used_locations |= (use_mask << attr);
  2246. /* From the GL 4.5 core spec, section 11.1.1 (Vertex Attributes):
  2247. *
  2248. * "A program with more than the value of MAX_VERTEX_ATTRIBS
  2249. * active attribute variables may fail to link, unless
  2250. * device-dependent optimizations are able to make the program
  2251. * fit within available hardware resources. For the purposes
  2252. * of this test, attribute variables of the type dvec3, dvec4,
  2253. * dmat2x3, dmat2x4, dmat3, dmat3x4, dmat4x3, and dmat4 may
  2254. * count as consuming twice as many attributes as equivalent
  2255. * single-precision types. While these types use the same number
  2256. * of generic attributes as their single-precision equivalents,
  2257. * implementations are permitted to consume two single-precision
  2258. * vectors of internal storage for each three- or four-component
  2259. * double-precision vector."
  2260. *
  2261. * Mark this attribute slot as taking up twice as much space
  2262. * so we can count it properly against limits. According to
  2263. * issue (3) of the GL_ARB_vertex_attrib_64bit behavior, this
  2264. * is optional behavior, but it seems preferable.
  2265. */
  2266. const glsl_type *type = var->type->without_array();
  2267. if (type == glsl_type::dvec3_type ||
  2268. type == glsl_type::dvec4_type ||
  2269. type == glsl_type::dmat2x3_type ||
  2270. type == glsl_type::dmat2x4_type ||
  2271. type == glsl_type::dmat3_type ||
  2272. type == glsl_type::dmat3x4_type ||
  2273. type == glsl_type::dmat4x3_type ||
  2274. type == glsl_type::dmat4_type) {
  2275. double_storage_locations |= (use_mask << attr);
  2276. }
  2277. }
  2278. continue;
  2279. }
  2280. to_assign[num_attr].slots = slots;
  2281. to_assign[num_attr].var = var;
  2282. num_attr++;
  2283. }
  2284. if (target_index == MESA_SHADER_VERTEX) {
  2285. unsigned total_attribs_size =
  2286. _mesa_bitcount(used_locations & ((1 << max_index) - 1)) +
  2287. _mesa_bitcount(double_storage_locations);
  2288. if (total_attribs_size > max_index) {
  2289. linker_error(prog,
  2290. "attempt to use %d vertex attribute slots only %d available ",
  2291. total_attribs_size, max_index);
  2292. return false;
  2293. }
  2294. }
  2295. /* If all of the attributes were assigned locations by the application (or
  2296. * are built-in attributes with fixed locations), return early. This should
  2297. * be the common case.
  2298. */
  2299. if (num_attr == 0)
  2300. return true;
  2301. qsort(to_assign, num_attr, sizeof(to_assign[0]), temp_attr::compare);
  2302. if (target_index == MESA_SHADER_VERTEX) {
  2303. /* VERT_ATTRIB_GENERIC0 is a pseudo-alias for VERT_ATTRIB_POS. It can
  2304. * only be explicitly assigned by via glBindAttribLocation. Mark it as
  2305. * reserved to prevent it from being automatically allocated below.
  2306. */
  2307. find_deref_visitor find("gl_Vertex");
  2308. find.run(sh->ir);
  2309. if (find.variable_found())
  2310. used_locations |= (1 << 0);
  2311. }
  2312. for (unsigned i = 0; i < num_attr; i++) {
  2313. /* Mask representing the contiguous slots that will be used by this
  2314. * attribute.
  2315. */
  2316. const unsigned use_mask = (1 << to_assign[i].slots) - 1;
  2317. int location = find_available_slots(used_locations, to_assign[i].slots);
  2318. if (location < 0) {
  2319. const char *const string = (target_index == MESA_SHADER_VERTEX)
  2320. ? "vertex shader input" : "fragment shader output";
  2321. linker_error(prog,
  2322. "insufficient contiguous locations "
  2323. "available for %s `%s'\n",
  2324. string, to_assign[i].var->name);
  2325. return false;
  2326. }
  2327. to_assign[i].var->data.location = generic_base + location;
  2328. to_assign[i].var->data.is_unmatched_generic_inout = 0;
  2329. used_locations |= (use_mask << location);
  2330. }
  2331. return true;
  2332. }
  2333. /**
  2334. * Demote shader inputs and outputs that are not used in other stages
  2335. */
  2336. void
  2337. demote_shader_inputs_and_outputs(gl_shader *sh, enum ir_variable_mode mode)
  2338. {
  2339. foreach_in_list(ir_instruction, node, sh->ir) {
  2340. ir_variable *const var = node->as_variable();
  2341. if ((var == NULL) || (var->data.mode != int(mode)))
  2342. continue;
  2343. /* A shader 'in' or 'out' variable is only really an input or output if
  2344. * its value is used by other shader stages. This will cause the variable
  2345. * to have a location assigned.
  2346. */
  2347. if (var->data.is_unmatched_generic_inout) {
  2348. assert(var->data.mode != ir_var_temporary);
  2349. var->data.mode = ir_var_auto;
  2350. }
  2351. }
  2352. }
  2353. /**
  2354. * Store the gl_FragDepth layout in the gl_shader_program struct.
  2355. */
  2356. static void
  2357. store_fragdepth_layout(struct gl_shader_program *prog)
  2358. {
  2359. if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
  2360. return;
  2361. }
  2362. struct exec_list *ir = prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
  2363. /* We don't look up the gl_FragDepth symbol directly because if
  2364. * gl_FragDepth is not used in the shader, it's removed from the IR.
  2365. * However, the symbol won't be removed from the symbol table.
  2366. *
  2367. * We're only interested in the cases where the variable is NOT removed
  2368. * from the IR.
  2369. */
  2370. foreach_in_list(ir_instruction, node, ir) {
  2371. ir_variable *const var = node->as_variable();
  2372. if (var == NULL || var->data.mode != ir_var_shader_out) {
  2373. continue;
  2374. }
  2375. if (strcmp(var->name, "gl_FragDepth") == 0) {
  2376. switch (var->data.depth_layout) {
  2377. case ir_depth_layout_none:
  2378. prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_NONE;
  2379. return;
  2380. case ir_depth_layout_any:
  2381. prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_ANY;
  2382. return;
  2383. case ir_depth_layout_greater:
  2384. prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_GREATER;
  2385. return;
  2386. case ir_depth_layout_less:
  2387. prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_LESS;
  2388. return;
  2389. case ir_depth_layout_unchanged:
  2390. prog->FragDepthLayout = FRAG_DEPTH_LAYOUT_UNCHANGED;
  2391. return;
  2392. default:
  2393. assert(0);
  2394. return;
  2395. }
  2396. }
  2397. }
  2398. }
  2399. /**
  2400. * Validate the resources used by a program versus the implementation limits
  2401. */
  2402. static void
  2403. check_resources(struct gl_context *ctx, struct gl_shader_program *prog)
  2404. {
  2405. for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
  2406. struct gl_shader *sh = prog->_LinkedShaders[i];
  2407. if (sh == NULL)
  2408. continue;
  2409. if (sh->num_samplers > ctx->Const.Program[i].MaxTextureImageUnits) {
  2410. linker_error(prog, "Too many %s shader texture samplers\n",
  2411. _mesa_shader_stage_to_string(i));
  2412. }
  2413. if (sh->num_uniform_components >
  2414. ctx->Const.Program[i].MaxUniformComponents) {
  2415. if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
  2416. linker_warning(prog, "Too many %s shader default uniform block "
  2417. "components, but the driver will try to optimize "
  2418. "them out; this is non-portable out-of-spec "
  2419. "behavior\n",
  2420. _mesa_shader_stage_to_string(i));
  2421. } else {
  2422. linker_error(prog, "Too many %s shader default uniform block "
  2423. "components\n",
  2424. _mesa_shader_stage_to_string(i));
  2425. }
  2426. }
  2427. if (sh->num_combined_uniform_components >
  2428. ctx->Const.Program[i].MaxCombinedUniformComponents) {
  2429. if (ctx->Const.GLSLSkipStrictMaxUniformLimitCheck) {
  2430. linker_warning(prog, "Too many %s shader uniform components, "
  2431. "but the driver will try to optimize them out; "
  2432. "this is non-portable out-of-spec behavior\n",
  2433. _mesa_shader_stage_to_string(i));
  2434. } else {
  2435. linker_error(prog, "Too many %s shader uniform components\n",
  2436. _mesa_shader_stage_to_string(i));
  2437. }
  2438. }
  2439. }
  2440. unsigned blocks[MESA_SHADER_STAGES] = {0};
  2441. unsigned total_uniform_blocks = 0;
  2442. for (unsigned i = 0; i < prog->NumUniformBlocks; i++) {
  2443. if (prog->UniformBlocks[i].UniformBufferSize > ctx->Const.MaxUniformBlockSize) {
  2444. linker_error(prog, "Uniform block %s too big (%d/%d)\n",
  2445. prog->UniformBlocks[i].Name,
  2446. prog->UniformBlocks[i].UniformBufferSize,
  2447. ctx->Const.MaxUniformBlockSize);
  2448. }
  2449. for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
  2450. if (prog->UniformBlockStageIndex[j][i] != -1) {
  2451. blocks[j]++;
  2452. total_uniform_blocks++;
  2453. }
  2454. }
  2455. if (total_uniform_blocks > ctx->Const.MaxCombinedUniformBlocks) {
  2456. linker_error(prog, "Too many combined uniform blocks (%d/%d)\n",
  2457. prog->NumUniformBlocks,
  2458. ctx->Const.MaxCombinedUniformBlocks);
  2459. } else {
  2460. for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
  2461. const unsigned max_uniform_blocks =
  2462. ctx->Const.Program[i].MaxUniformBlocks;
  2463. if (blocks[i] > max_uniform_blocks) {
  2464. linker_error(prog, "Too many %s uniform blocks (%d/%d)\n",
  2465. _mesa_shader_stage_to_string(i),
  2466. blocks[i],
  2467. max_uniform_blocks);
  2468. break;
  2469. }
  2470. }
  2471. }
  2472. }
  2473. }
  2474. static void
  2475. link_calculate_subroutine_compat(struct gl_shader_program *prog)
  2476. {
  2477. for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
  2478. struct gl_shader *sh = prog->_LinkedShaders[i];
  2479. int count;
  2480. if (!sh)
  2481. continue;
  2482. for (unsigned j = 0; j < sh->NumSubroutineUniformRemapTable; j++) {
  2483. struct gl_uniform_storage *uni = sh->SubroutineUniformRemapTable[j];
  2484. if (!uni)
  2485. continue;
  2486. count = 0;
  2487. for (unsigned f = 0; f < sh->NumSubroutineFunctions; f++) {
  2488. struct gl_subroutine_function *fn = &sh->SubroutineFunctions[f];
  2489. for (int k = 0; k < fn->num_compat_types; k++) {
  2490. if (fn->types[k] == uni->type) {
  2491. count++;
  2492. break;
  2493. }
  2494. }
  2495. }
  2496. uni->num_compatible_subroutines = count;
  2497. }
  2498. }
  2499. }
  2500. static void
  2501. check_subroutine_resources(struct gl_shader_program *prog)
  2502. {
  2503. for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
  2504. struct gl_shader *sh = prog->_LinkedShaders[i];
  2505. if (sh) {
  2506. if (sh->NumSubroutineUniformRemapTable > MAX_SUBROUTINE_UNIFORM_LOCATIONS)
  2507. linker_error(prog, "Too many %s shader subroutine uniforms\n",
  2508. _mesa_shader_stage_to_string(i));
  2509. }
  2510. }
  2511. }
  2512. /**
  2513. * Validate shader image resources.
  2514. */
  2515. static void
  2516. check_image_resources(struct gl_context *ctx, struct gl_shader_program *prog)
  2517. {
  2518. unsigned total_image_units = 0;
  2519. unsigned fragment_outputs = 0;
  2520. if (!ctx->Extensions.ARB_shader_image_load_store)
  2521. return;
  2522. for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
  2523. struct gl_shader *sh = prog->_LinkedShaders[i];
  2524. if (sh) {
  2525. if (sh->NumImages > ctx->Const.Program[i].MaxImageUniforms)
  2526. linker_error(prog, "Too many %s shader image uniforms (%u > %u)\n",
  2527. _mesa_shader_stage_to_string(i), sh->NumImages,
  2528. ctx->Const.Program[i].MaxImageUniforms);
  2529. total_image_units += sh->NumImages;
  2530. if (i == MESA_SHADER_FRAGMENT) {
  2531. foreach_in_list(ir_instruction, node, sh->ir) {
  2532. ir_variable *var = node->as_variable();
  2533. if (var && var->data.mode == ir_var_shader_out)
  2534. fragment_outputs += var->type->count_attribute_slots();
  2535. }
  2536. }
  2537. }
  2538. }
  2539. if (total_image_units > ctx->Const.MaxCombinedImageUniforms)
  2540. linker_error(prog, "Too many combined image uniforms\n");
  2541. if (total_image_units + fragment_outputs >
  2542. ctx->Const.MaxCombinedShaderOutputResources)
  2543. linker_error(prog, "Too many combined image uniforms and fragment outputs\n");
  2544. }
  2545. /**
  2546. * Initializes explicit location slots to INACTIVE_UNIFORM_EXPLICIT_LOCATION
  2547. * for a variable, checks for overlaps between other uniforms using explicit
  2548. * locations.
  2549. */
  2550. static bool
  2551. reserve_explicit_locations(struct gl_shader_program *prog,
  2552. string_to_uint_map *map, ir_variable *var)
  2553. {
  2554. unsigned slots = var->type->uniform_locations();
  2555. unsigned max_loc = var->data.location + slots - 1;
  2556. /* Resize remap table if locations do not fit in the current one. */
  2557. if (max_loc + 1 > prog->NumUniformRemapTable) {
  2558. prog->UniformRemapTable =
  2559. reralloc(prog, prog->UniformRemapTable,
  2560. gl_uniform_storage *,
  2561. max_loc + 1);
  2562. if (!prog->UniformRemapTable) {
  2563. linker_error(prog, "Out of memory during linking.\n");
  2564. return false;
  2565. }
  2566. /* Initialize allocated space. */
  2567. for (unsigned i = prog->NumUniformRemapTable; i < max_loc + 1; i++)
  2568. prog->UniformRemapTable[i] = NULL;
  2569. prog->NumUniformRemapTable = max_loc + 1;
  2570. }
  2571. for (unsigned i = 0; i < slots; i++) {
  2572. unsigned loc = var->data.location + i;
  2573. /* Check if location is already used. */
  2574. if (prog->UniformRemapTable[loc] == INACTIVE_UNIFORM_EXPLICIT_LOCATION) {
  2575. /* Possibly same uniform from a different stage, this is ok. */
  2576. unsigned hash_loc;
  2577. if (map->get(hash_loc, var->name) && hash_loc == loc - i)
  2578. continue;
  2579. /* ARB_explicit_uniform_location specification states:
  2580. *
  2581. * "No two default-block uniform variables in the program can have
  2582. * the same location, even if they are unused, otherwise a compiler
  2583. * or linker error will be generated."
  2584. */
  2585. linker_error(prog,
  2586. "location qualifier for uniform %s overlaps "
  2587. "previously used location\n",
  2588. var->name);
  2589. return false;
  2590. }
  2591. /* Initialize location as inactive before optimization
  2592. * rounds and location assignment.
  2593. */
  2594. prog->UniformRemapTable[loc] = INACTIVE_UNIFORM_EXPLICIT_LOCATION;
  2595. }
  2596. /* Note, base location used for arrays. */
  2597. map->put(var->data.location, var->name);
  2598. return true;
  2599. }
  2600. static bool
  2601. reserve_subroutine_explicit_locations(struct gl_shader_program *prog,
  2602. struct gl_shader *sh,
  2603. ir_variable *var)
  2604. {
  2605. unsigned slots = var->type->uniform_locations();
  2606. unsigned max_loc = var->data.location + slots - 1;
  2607. /* Resize remap table if locations do not fit in the current one. */
  2608. if (max_loc + 1 > sh->NumSubroutineUniformRemapTable) {
  2609. sh->SubroutineUniformRemapTable =
  2610. reralloc(sh, sh->SubroutineUniformRemapTable,
  2611. gl_uniform_storage *,
  2612. max_loc + 1);
  2613. if (!sh->SubroutineUniformRemapTable) {
  2614. linker_error(prog, "Out of memory during linking.\n");
  2615. return false;
  2616. }
  2617. /* Initialize allocated space. */
  2618. for (unsigned i = sh->NumSubroutineUniformRemapTable; i < max_loc + 1; i++)
  2619. sh->SubroutineUniformRemapTable[i] = NULL;
  2620. sh->NumSubroutineUniformRemapTable = max_loc + 1;
  2621. }
  2622. for (unsigned i = 0; i < slots; i++) {
  2623. unsigned loc = var->data.location + i;
  2624. /* Check if location is already used. */
  2625. if (sh->SubroutineUniformRemapTable[loc] == INACTIVE_UNIFORM_EXPLICIT_LOCATION) {
  2626. /* ARB_explicit_uniform_location specification states:
  2627. * "No two subroutine uniform variables can have the same location
  2628. * in the same shader stage, otherwise a compiler or linker error
  2629. * will be generated."
  2630. */
  2631. linker_error(prog,
  2632. "location qualifier for uniform %s overlaps "
  2633. "previously used location\n",
  2634. var->name);
  2635. return false;
  2636. }
  2637. /* Initialize location as inactive before optimization
  2638. * rounds and location assignment.
  2639. */
  2640. sh->SubroutineUniformRemapTable[loc] = INACTIVE_UNIFORM_EXPLICIT_LOCATION;
  2641. }
  2642. return true;
  2643. }
  2644. /**
  2645. * Check and reserve all explicit uniform locations, called before
  2646. * any optimizations happen to handle also inactive uniforms and
  2647. * inactive array elements that may get trimmed away.
  2648. */
  2649. static void
  2650. check_explicit_uniform_locations(struct gl_context *ctx,
  2651. struct gl_shader_program *prog)
  2652. {
  2653. if (!ctx->Extensions.ARB_explicit_uniform_location)
  2654. return;
  2655. /* This map is used to detect if overlapping explicit locations
  2656. * occur with the same uniform (from different stage) or a different one.
  2657. */
  2658. string_to_uint_map *uniform_map = new string_to_uint_map;
  2659. if (!uniform_map) {
  2660. linker_error(prog, "Out of memory during linking.\n");
  2661. return;
  2662. }
  2663. for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
  2664. struct gl_shader *sh = prog->_LinkedShaders[i];
  2665. if (!sh)
  2666. continue;
  2667. foreach_in_list(ir_instruction, node, sh->ir) {
  2668. ir_variable *var = node->as_variable();
  2669. if (var && (var->data.mode == ir_var_uniform || var->data.mode == ir_var_shader_storage) &&
  2670. var->data.explicit_location) {
  2671. bool ret;
  2672. if (var->type->is_subroutine())
  2673. ret = reserve_subroutine_explicit_locations(prog, sh, var);
  2674. else
  2675. ret = reserve_explicit_locations(prog, uniform_map, var);
  2676. if (!ret) {
  2677. delete uniform_map;
  2678. return;
  2679. }
  2680. }
  2681. }
  2682. }
  2683. delete uniform_map;
  2684. }
  2685. static bool
  2686. add_program_resource(struct gl_shader_program *prog, GLenum type,
  2687. const void *data, uint8_t stages)
  2688. {
  2689. assert(data);
  2690. /* If resource already exists, do not add it again. */
  2691. for (unsigned i = 0; i < prog->NumProgramResourceList; i++)
  2692. if (prog->ProgramResourceList[i].Data == data)
  2693. return true;
  2694. prog->ProgramResourceList =
  2695. reralloc(prog,
  2696. prog->ProgramResourceList,
  2697. gl_program_resource,
  2698. prog->NumProgramResourceList + 1);
  2699. if (!prog->ProgramResourceList) {
  2700. linker_error(prog, "Out of memory during linking.\n");
  2701. return false;
  2702. }
  2703. struct gl_program_resource *res =
  2704. &prog->ProgramResourceList[prog->NumProgramResourceList];
  2705. res->Type = type;
  2706. res->Data = data;
  2707. res->StageReferences = stages;
  2708. prog->NumProgramResourceList++;
  2709. return true;
  2710. }
  2711. /**
  2712. * Function builds a stage reference bitmask from variable name.
  2713. */
  2714. static uint8_t
  2715. build_stageref(struct gl_shader_program *shProg, const char *name,
  2716. unsigned mode)
  2717. {
  2718. uint8_t stages = 0;
  2719. /* Note, that we assume MAX 8 stages, if there will be more stages, type
  2720. * used for reference mask in gl_program_resource will need to be changed.
  2721. */
  2722. assert(MESA_SHADER_STAGES < 8);
  2723. for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
  2724. struct gl_shader *sh = shProg->_LinkedShaders[i];
  2725. if (!sh)
  2726. continue;
  2727. /* Shader symbol table may contain variables that have
  2728. * been optimized away. Search IR for the variable instead.
  2729. */
  2730. foreach_in_list(ir_instruction, node, sh->ir) {
  2731. ir_variable *var = node->as_variable();
  2732. if (var) {
  2733. unsigned baselen = strlen(var->name);
  2734. /* Type needs to match if specified, otherwise we might
  2735. * pick a variable with same name but different interface.
  2736. */
  2737. if (var->data.mode != mode)
  2738. continue;
  2739. if (strncmp(var->name, name, baselen) == 0) {
  2740. /* Check for exact name matches but also check for arrays and
  2741. * structs.
  2742. */
  2743. if (name[baselen] == '\0' ||
  2744. name[baselen] == '[' ||
  2745. name[baselen] == '.') {
  2746. stages |= (1 << i);
  2747. break;
  2748. }
  2749. }
  2750. }
  2751. }
  2752. }
  2753. return stages;
  2754. }
  2755. static bool
  2756. add_interface_variables(struct gl_shader_program *shProg,
  2757. struct gl_shader *sh, GLenum programInterface)
  2758. {
  2759. foreach_in_list(ir_instruction, node, sh->ir) {
  2760. ir_variable *var = node->as_variable();
  2761. uint8_t mask = 0;
  2762. if (!var)
  2763. continue;
  2764. switch (var->data.mode) {
  2765. /* From GL 4.3 core spec, section 11.1.1 (Vertex Attributes):
  2766. * "For GetActiveAttrib, all active vertex shader input variables
  2767. * are enumerated, including the special built-in inputs gl_VertexID
  2768. * and gl_InstanceID."
  2769. */
  2770. case ir_var_system_value:
  2771. if (var->data.location != SYSTEM_VALUE_VERTEX_ID &&
  2772. var->data.location != SYSTEM_VALUE_VERTEX_ID_ZERO_BASE &&
  2773. var->data.location != SYSTEM_VALUE_INSTANCE_ID)
  2774. continue;
  2775. /* Mark special built-in inputs referenced by the vertex stage so
  2776. * that they are considered active by the shader queries.
  2777. */
  2778. mask = (1 << (MESA_SHADER_VERTEX));
  2779. /* FALLTHROUGH */
  2780. case ir_var_shader_in:
  2781. if (programInterface != GL_PROGRAM_INPUT)
  2782. continue;
  2783. break;
  2784. case ir_var_shader_out:
  2785. if (programInterface != GL_PROGRAM_OUTPUT)
  2786. continue;
  2787. break;
  2788. default:
  2789. continue;
  2790. };
  2791. if (!add_program_resource(shProg, programInterface, var,
  2792. build_stageref(shProg, var->name,
  2793. var->data.mode) | mask))
  2794. return false;
  2795. }
  2796. return true;
  2797. }
  2798. /**
  2799. * Builds up a list of program resources that point to existing
  2800. * resource data.
  2801. */
  2802. void
  2803. build_program_resource_list(struct gl_context *ctx,
  2804. struct gl_shader_program *shProg)
  2805. {
  2806. /* Rebuild resource list. */
  2807. if (shProg->ProgramResourceList) {
  2808. ralloc_free(shProg->ProgramResourceList);
  2809. shProg->ProgramResourceList = NULL;
  2810. shProg->NumProgramResourceList = 0;
  2811. }
  2812. int input_stage = MESA_SHADER_STAGES, output_stage = 0;
  2813. /* Determine first input and final output stage. These are used to
  2814. * detect which variables should be enumerated in the resource list
  2815. * for GL_PROGRAM_INPUT and GL_PROGRAM_OUTPUT.
  2816. */
  2817. for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
  2818. if (!shProg->_LinkedShaders[i])
  2819. continue;
  2820. if (input_stage == MESA_SHADER_STAGES)
  2821. input_stage = i;
  2822. output_stage = i;
  2823. }
  2824. /* Empty shader, no resources. */
  2825. if (input_stage == MESA_SHADER_STAGES && output_stage == 0)
  2826. return;
  2827. /* Add inputs and outputs to the resource list. */
  2828. if (!add_interface_variables(shProg, shProg->_LinkedShaders[input_stage],
  2829. GL_PROGRAM_INPUT))
  2830. return;
  2831. if (!add_interface_variables(shProg, shProg->_LinkedShaders[output_stage],
  2832. GL_PROGRAM_OUTPUT))
  2833. return;
  2834. /* Add transform feedback varyings. */
  2835. if (shProg->LinkedTransformFeedback.NumVarying > 0) {
  2836. for (int i = 0; i < shProg->LinkedTransformFeedback.NumVarying; i++) {
  2837. if (!add_program_resource(shProg, GL_TRANSFORM_FEEDBACK_VARYING,
  2838. &shProg->LinkedTransformFeedback.Varyings[i],
  2839. 0))
  2840. return;
  2841. }
  2842. }
  2843. /* Add uniforms from uniform storage. */
  2844. for (unsigned i = 0; i < shProg->NumUniformStorage; i++) {
  2845. /* Do not add uniforms internally used by Mesa. */
  2846. if (shProg->UniformStorage[i].hidden)
  2847. continue;
  2848. uint8_t stageref =
  2849. build_stageref(shProg, shProg->UniformStorage[i].name,
  2850. ir_var_uniform);
  2851. /* Add stagereferences for uniforms in a uniform block. */
  2852. int block_index = shProg->UniformStorage[i].block_index;
  2853. if (block_index != -1) {
  2854. for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
  2855. if (shProg->UniformBlockStageIndex[j][block_index] != -1)
  2856. stageref |= (1 << j);
  2857. }
  2858. }
  2859. if (!add_program_resource(shProg, GL_UNIFORM,
  2860. &shProg->UniformStorage[i], stageref))
  2861. return;
  2862. }
  2863. /* Add program uniform blocks. */
  2864. for (unsigned i = 0; i < shProg->NumUniformBlocks; i++) {
  2865. if (!add_program_resource(shProg, GL_UNIFORM_BLOCK,
  2866. &shProg->UniformBlocks[i], 0))
  2867. return;
  2868. }
  2869. /* Add atomic counter buffers. */
  2870. for (unsigned i = 0; i < shProg->NumAtomicBuffers; i++) {
  2871. if (!add_program_resource(shProg, GL_ATOMIC_COUNTER_BUFFER,
  2872. &shProg->AtomicBuffers[i], 0))
  2873. return;
  2874. }
  2875. for (unsigned i = 0; i < shProg->NumUniformStorage; i++) {
  2876. GLenum type;
  2877. if (!shProg->UniformStorage[i].hidden)
  2878. continue;
  2879. for (int j = MESA_SHADER_VERTEX; j < MESA_SHADER_STAGES; j++) {
  2880. if (!shProg->UniformStorage[i].subroutine[j].active)
  2881. continue;
  2882. type = _mesa_shader_stage_to_subroutine_uniform((gl_shader_stage)j);
  2883. /* add shader subroutines */
  2884. if (!add_program_resource(shProg, type, &shProg->UniformStorage[i], 0))
  2885. return;
  2886. }
  2887. }
  2888. for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
  2889. struct gl_shader *sh = shProg->_LinkedShaders[i];
  2890. GLuint type;
  2891. if (!sh)
  2892. continue;
  2893. type = _mesa_shader_stage_to_subroutine((gl_shader_stage)i);
  2894. for (unsigned j = 0; j < sh->NumSubroutineFunctions; j++) {
  2895. if (!add_program_resource(shProg, type, &sh->SubroutineFunctions[j], 0))
  2896. return;
  2897. }
  2898. }
  2899. /* TODO - following extensions will require more resource types:
  2900. *
  2901. * GL_ARB_shader_storage_buffer_object
  2902. */
  2903. }
  2904. /**
  2905. * This check is done to make sure we allow only constant expression
  2906. * indexing and "constant-index-expression" (indexing with an expression
  2907. * that includes loop induction variable).
  2908. */
  2909. static bool
  2910. validate_sampler_array_indexing(struct gl_context *ctx,
  2911. struct gl_shader_program *prog)
  2912. {
  2913. dynamic_sampler_array_indexing_visitor v;
  2914. for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
  2915. if (prog->_LinkedShaders[i] == NULL)
  2916. continue;
  2917. bool no_dynamic_indexing =
  2918. ctx->Const.ShaderCompilerOptions[i].EmitNoIndirectSampler;
  2919. /* Search for array derefs in shader. */
  2920. v.run(prog->_LinkedShaders[i]->ir);
  2921. if (v.uses_dynamic_sampler_array_indexing()) {
  2922. const char *msg = "sampler arrays indexed with non-constant "
  2923. "expressions is forbidden in GLSL %s %u";
  2924. /* Backend has indicated that it has no dynamic indexing support. */
  2925. if (no_dynamic_indexing) {
  2926. linker_error(prog, msg, prog->IsES ? "ES" : "", prog->Version);
  2927. return false;
  2928. } else {
  2929. linker_warning(prog, msg, prog->IsES ? "ES" : "", prog->Version);
  2930. }
  2931. }
  2932. }
  2933. return true;
  2934. }
  2935. static void
  2936. link_assign_subroutine_types(struct gl_shader_program *prog)
  2937. {
  2938. for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
  2939. gl_shader *sh = prog->_LinkedShaders[i];
  2940. if (sh == NULL)
  2941. continue;
  2942. foreach_in_list(ir_instruction, node, sh->ir) {
  2943. ir_function *fn = node->as_function();
  2944. if (!fn)
  2945. continue;
  2946. if (fn->is_subroutine)
  2947. sh->NumSubroutineUniformTypes++;
  2948. if (!fn->num_subroutine_types)
  2949. continue;
  2950. sh->SubroutineFunctions = reralloc(sh, sh->SubroutineFunctions,
  2951. struct gl_subroutine_function,
  2952. sh->NumSubroutineFunctions + 1);
  2953. sh->SubroutineFunctions[sh->NumSubroutineFunctions].name = ralloc_strdup(sh, fn->name);
  2954. sh->SubroutineFunctions[sh->NumSubroutineFunctions].num_compat_types = fn->num_subroutine_types;
  2955. sh->SubroutineFunctions[sh->NumSubroutineFunctions].types =
  2956. ralloc_array(sh, const struct glsl_type *,
  2957. fn->num_subroutine_types);
  2958. for (int j = 0; j < fn->num_subroutine_types; j++)
  2959. sh->SubroutineFunctions[sh->NumSubroutineFunctions].types[j] = fn->subroutine_types[j];
  2960. sh->NumSubroutineFunctions++;
  2961. }
  2962. }
  2963. }
  2964. void
  2965. link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
  2966. {
  2967. tfeedback_decl *tfeedback_decls = NULL;
  2968. unsigned num_tfeedback_decls = prog->TransformFeedback.NumVarying;
  2969. void *mem_ctx = ralloc_context(NULL); // temporary linker context
  2970. prog->LinkStatus = true; /* All error paths will set this to false */
  2971. prog->Validated = false;
  2972. prog->_Used = false;
  2973. prog->ARB_fragment_coord_conventions_enable = false;
  2974. /* Separate the shaders into groups based on their type.
  2975. */
  2976. struct gl_shader **shader_list[MESA_SHADER_STAGES];
  2977. unsigned num_shaders[MESA_SHADER_STAGES];
  2978. for (int i = 0; i < MESA_SHADER_STAGES; i++) {
  2979. shader_list[i] = (struct gl_shader **)
  2980. calloc(prog->NumShaders, sizeof(struct gl_shader *));
  2981. num_shaders[i] = 0;
  2982. }
  2983. unsigned min_version = UINT_MAX;
  2984. unsigned max_version = 0;
  2985. const bool is_es_prog =
  2986. (prog->NumShaders > 0 && prog->Shaders[0]->IsES) ? true : false;
  2987. for (unsigned i = 0; i < prog->NumShaders; i++) {
  2988. min_version = MIN2(min_version, prog->Shaders[i]->Version);
  2989. max_version = MAX2(max_version, prog->Shaders[i]->Version);
  2990. if (prog->Shaders[i]->IsES != is_es_prog) {
  2991. linker_error(prog, "all shaders must use same shading "
  2992. "language version\n");
  2993. goto done;
  2994. }
  2995. if (prog->Shaders[i]->ARB_fragment_coord_conventions_enable) {
  2996. prog->ARB_fragment_coord_conventions_enable = true;
  2997. }
  2998. gl_shader_stage shader_type = prog->Shaders[i]->Stage;
  2999. shader_list[shader_type][num_shaders[shader_type]] = prog->Shaders[i];
  3000. num_shaders[shader_type]++;
  3001. }
  3002. /* In desktop GLSL, different shader versions may be linked together. In
  3003. * GLSL ES, all shader versions must be the same.
  3004. */
  3005. if (is_es_prog && min_version != max_version) {
  3006. linker_error(prog, "all shaders must use same shading "
  3007. "language version\n");
  3008. goto done;
  3009. }
  3010. prog->Version = max_version;
  3011. prog->IsES = is_es_prog;
  3012. /* From OpenGL 4.5 Core specification (7.3 Program Objects):
  3013. * "Linking can fail for a variety of reasons as specified in the OpenGL
  3014. * Shading Language Specification, as well as any of the following
  3015. * reasons:
  3016. *
  3017. * * No shader objects are attached to program.
  3018. *
  3019. * ..."
  3020. *
  3021. * Same rule applies for OpenGL ES >= 3.1.
  3022. */
  3023. if (prog->NumShaders == 0 &&
  3024. ((ctx->API == API_OPENGL_CORE && ctx->Version >= 45) ||
  3025. (ctx->API == API_OPENGLES2 && ctx->Version >= 31))) {
  3026. linker_error(prog, "No shader objects are attached to program.\n");
  3027. goto done;
  3028. }
  3029. /* Some shaders have to be linked with some other shaders present.
  3030. */
  3031. if (num_shaders[MESA_SHADER_GEOMETRY] > 0 &&
  3032. num_shaders[MESA_SHADER_VERTEX] == 0 &&
  3033. !prog->SeparateShader) {
  3034. linker_error(prog, "Geometry shader must be linked with "
  3035. "vertex shader\n");
  3036. goto done;
  3037. }
  3038. if (num_shaders[MESA_SHADER_TESS_EVAL] > 0 &&
  3039. num_shaders[MESA_SHADER_VERTEX] == 0 &&
  3040. !prog->SeparateShader) {
  3041. linker_error(prog, "Tessellation evaluation shader must be linked with "
  3042. "vertex shader\n");
  3043. goto done;
  3044. }
  3045. if (num_shaders[MESA_SHADER_TESS_CTRL] > 0 &&
  3046. num_shaders[MESA_SHADER_VERTEX] == 0 &&
  3047. !prog->SeparateShader) {
  3048. linker_error(prog, "Tessellation control shader must be linked with "
  3049. "vertex shader\n");
  3050. goto done;
  3051. }
  3052. /* The spec is self-contradictory here. It allows linking without a tess
  3053. * eval shader, but that can only be used with transform feedback and
  3054. * rasterization disabled. However, transform feedback isn't allowed
  3055. * with GL_PATCHES, so it can't be used.
  3056. *
  3057. * More investigation showed that the idea of transform feedback after
  3058. * a tess control shader was dropped, because some hw vendors couldn't
  3059. * support tessellation without a tess eval shader, but the linker section
  3060. * wasn't updated to reflect that.
  3061. *
  3062. * All specifications (ARB_tessellation_shader, GL 4.0-4.5) have this
  3063. * spec bug.
  3064. *
  3065. * Do what's reasonable and always require a tess eval shader if a tess
  3066. * control shader is present.
  3067. */
  3068. if (num_shaders[MESA_SHADER_TESS_CTRL] > 0 &&
  3069. num_shaders[MESA_SHADER_TESS_EVAL] == 0 &&
  3070. !prog->SeparateShader) {
  3071. linker_error(prog, "Tessellation control shader must be linked with "
  3072. "tessellation evaluation shader\n");
  3073. goto done;
  3074. }
  3075. /* Compute shaders have additional restrictions. */
  3076. if (num_shaders[MESA_SHADER_COMPUTE] > 0 &&
  3077. num_shaders[MESA_SHADER_COMPUTE] != prog->NumShaders) {
  3078. linker_error(prog, "Compute shaders may not be linked with any other "
  3079. "type of shader\n");
  3080. }
  3081. for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) {
  3082. if (prog->_LinkedShaders[i] != NULL)
  3083. ctx->Driver.DeleteShader(ctx, prog->_LinkedShaders[i]);
  3084. prog->_LinkedShaders[i] = NULL;
  3085. }
  3086. /* Link all shaders for a particular stage and validate the result.
  3087. */
  3088. for (int stage = 0; stage < MESA_SHADER_STAGES; stage++) {
  3089. if (num_shaders[stage] > 0) {
  3090. gl_shader *const sh =
  3091. link_intrastage_shaders(mem_ctx, ctx, prog, shader_list[stage],
  3092. num_shaders[stage]);
  3093. if (!prog->LinkStatus) {
  3094. if (sh)
  3095. ctx->Driver.DeleteShader(ctx, sh);
  3096. goto done;
  3097. }
  3098. switch (stage) {
  3099. case MESA_SHADER_VERTEX:
  3100. validate_vertex_shader_executable(prog, sh);
  3101. break;
  3102. case MESA_SHADER_TESS_CTRL:
  3103. /* nothing to be done */
  3104. break;
  3105. case MESA_SHADER_TESS_EVAL:
  3106. validate_tess_eval_shader_executable(prog, sh);
  3107. break;
  3108. case MESA_SHADER_GEOMETRY:
  3109. validate_geometry_shader_executable(prog, sh);
  3110. break;
  3111. case MESA_SHADER_FRAGMENT:
  3112. validate_fragment_shader_executable(prog, sh);
  3113. break;
  3114. }
  3115. if (!prog->LinkStatus) {
  3116. if (sh)
  3117. ctx->Driver.DeleteShader(ctx, sh);
  3118. goto done;
  3119. }
  3120. _mesa_reference_shader(ctx, &prog->_LinkedShaders[stage], sh);
  3121. }
  3122. }
  3123. if (num_shaders[MESA_SHADER_GEOMETRY] > 0)
  3124. prog->LastClipDistanceArraySize = prog->Geom.ClipDistanceArraySize;
  3125. else if (num_shaders[MESA_SHADER_TESS_EVAL] > 0)
  3126. prog->LastClipDistanceArraySize = prog->TessEval.ClipDistanceArraySize;
  3127. else if (num_shaders[MESA_SHADER_VERTEX] > 0)
  3128. prog->LastClipDistanceArraySize = prog->Vert.ClipDistanceArraySize;
  3129. else
  3130. prog->LastClipDistanceArraySize = 0; /* Not used */
  3131. /* Here begins the inter-stage linking phase. Some initial validation is
  3132. * performed, then locations are assigned for uniforms, attributes, and
  3133. * varyings.
  3134. */
  3135. cross_validate_uniforms(prog);
  3136. if (!prog->LinkStatus)
  3137. goto done;
  3138. unsigned prev;
  3139. for (prev = 0; prev <= MESA_SHADER_FRAGMENT; prev++) {
  3140. if (prog->_LinkedShaders[prev] != NULL)
  3141. break;
  3142. }
  3143. check_explicit_uniform_locations(ctx, prog);
  3144. link_assign_subroutine_types(prog);
  3145. if (!prog->LinkStatus)
  3146. goto done;
  3147. resize_tes_inputs(ctx, prog);
  3148. /* Validate the inputs of each stage with the output of the preceding
  3149. * stage.
  3150. */
  3151. for (unsigned i = prev + 1; i <= MESA_SHADER_FRAGMENT; i++) {
  3152. if (prog->_LinkedShaders[i] == NULL)
  3153. continue;
  3154. validate_interstage_inout_blocks(prog, prog->_LinkedShaders[prev],
  3155. prog->_LinkedShaders[i]);
  3156. if (!prog->LinkStatus)
  3157. goto done;
  3158. cross_validate_outputs_to_inputs(prog,
  3159. prog->_LinkedShaders[prev],
  3160. prog->_LinkedShaders[i]);
  3161. if (!prog->LinkStatus)
  3162. goto done;
  3163. prev = i;
  3164. }
  3165. /* Cross-validate uniform blocks between shader stages */
  3166. validate_interstage_uniform_blocks(prog, prog->_LinkedShaders,
  3167. MESA_SHADER_STAGES);
  3168. if (!prog->LinkStatus)
  3169. goto done;
  3170. for (unsigned int i = 0; i < MESA_SHADER_STAGES; i++) {
  3171. if (prog->_LinkedShaders[i] != NULL)
  3172. lower_named_interface_blocks(mem_ctx, prog->_LinkedShaders[i]);
  3173. }
  3174. /* Implement the GLSL 1.30+ rule for discard vs infinite loops Do
  3175. * it before optimization because we want most of the checks to get
  3176. * dropped thanks to constant propagation.
  3177. *
  3178. * This rule also applies to GLSL ES 3.00.
  3179. */
  3180. if (max_version >= (is_es_prog ? 300 : 130)) {
  3181. struct gl_shader *sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
  3182. if (sh) {
  3183. lower_discard_flow(sh->ir);
  3184. }
  3185. }
  3186. if (!interstage_cross_validate_uniform_blocks(prog))
  3187. goto done;
  3188. /* Do common optimization before assigning storage for attributes,
  3189. * uniforms, and varyings. Later optimization could possibly make
  3190. * some of that unused.
  3191. */
  3192. for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
  3193. if (prog->_LinkedShaders[i] == NULL)
  3194. continue;
  3195. detect_recursion_linked(prog, prog->_LinkedShaders[i]->ir);
  3196. if (!prog->LinkStatus)
  3197. goto done;
  3198. if (ctx->Const.ShaderCompilerOptions[i].LowerClipDistance) {
  3199. lower_clip_distance(prog->_LinkedShaders[i]);
  3200. }
  3201. if (ctx->Const.LowerTessLevel) {
  3202. lower_tess_level(prog->_LinkedShaders[i]);
  3203. }
  3204. while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false,
  3205. &ctx->Const.ShaderCompilerOptions[i],
  3206. ctx->Const.NativeIntegers))
  3207. ;
  3208. lower_const_arrays_to_uniforms(prog->_LinkedShaders[i]->ir);
  3209. }
  3210. /* Validation for special cases where we allow sampler array indexing
  3211. * with loop induction variable. This check emits a warning or error
  3212. * depending if backend can handle dynamic indexing.
  3213. */
  3214. if ((!prog->IsES && prog->Version < 130) ||
  3215. (prog->IsES && prog->Version < 300)) {
  3216. if (!validate_sampler_array_indexing(ctx, prog))
  3217. goto done;
  3218. }
  3219. /* Check and validate stream emissions in geometry shaders */
  3220. validate_geometry_shader_emissions(ctx, prog);
  3221. /* Mark all generic shader inputs and outputs as unpaired. */
  3222. for (unsigned i = MESA_SHADER_VERTEX; i <= MESA_SHADER_FRAGMENT; i++) {
  3223. if (prog->_LinkedShaders[i] != NULL) {
  3224. link_invalidate_variable_locations(prog->_LinkedShaders[i]->ir);
  3225. }
  3226. }
  3227. if (!assign_attribute_or_color_locations(prog, &ctx->Const,
  3228. MESA_SHADER_VERTEX)) {
  3229. goto done;
  3230. }
  3231. if (!assign_attribute_or_color_locations(prog, &ctx->Const,
  3232. MESA_SHADER_FRAGMENT)) {
  3233. goto done;
  3234. }
  3235. unsigned first, last;
  3236. first = MESA_SHADER_STAGES;
  3237. last = 0;
  3238. /* Determine first and last stage. */
  3239. for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
  3240. if (!prog->_LinkedShaders[i])
  3241. continue;
  3242. if (first == MESA_SHADER_STAGES)
  3243. first = i;
  3244. last = i;
  3245. }
  3246. if (num_tfeedback_decls != 0) {
  3247. /* From GL_EXT_transform_feedback:
  3248. * A program will fail to link if:
  3249. *
  3250. * * the <count> specified by TransformFeedbackVaryingsEXT is
  3251. * non-zero, but the program object has no vertex or geometry
  3252. * shader;
  3253. */
  3254. if (first == MESA_SHADER_FRAGMENT) {
  3255. linker_error(prog, "Transform feedback varyings specified, but "
  3256. "no vertex or geometry shader is present.\n");
  3257. goto done;
  3258. }
  3259. tfeedback_decls = ralloc_array(mem_ctx, tfeedback_decl,
  3260. prog->TransformFeedback.NumVarying);
  3261. if (!parse_tfeedback_decls(ctx, prog, mem_ctx, num_tfeedback_decls,
  3262. prog->TransformFeedback.VaryingNames,
  3263. tfeedback_decls))
  3264. goto done;
  3265. }
  3266. /* Linking the stages in the opposite order (from fragment to vertex)
  3267. * ensures that inter-shader outputs written to in an earlier stage are
  3268. * eliminated if they are (transitively) not used in a later stage.
  3269. */
  3270. int next;
  3271. if (first < MESA_SHADER_FRAGMENT) {
  3272. gl_shader *const sh = prog->_LinkedShaders[last];
  3273. if (first == MESA_SHADER_GEOMETRY) {
  3274. /* There was no vertex shader, but we still have to assign varying
  3275. * locations for use by geometry shader inputs in SSO.
  3276. *
  3277. * If the shader is not separable (i.e., prog->SeparateShader is
  3278. * false), linking will have already failed when first is
  3279. * MESA_SHADER_GEOMETRY.
  3280. */
  3281. if (!assign_varying_locations(ctx, mem_ctx, prog,
  3282. NULL, prog->_LinkedShaders[first],
  3283. num_tfeedback_decls, tfeedback_decls))
  3284. goto done;
  3285. }
  3286. if (last != MESA_SHADER_FRAGMENT &&
  3287. (num_tfeedback_decls != 0 || prog->SeparateShader)) {
  3288. /* There was no fragment shader, but we still have to assign varying
  3289. * locations for use by transform feedback.
  3290. */
  3291. if (!assign_varying_locations(ctx, mem_ctx, prog,
  3292. sh, NULL,
  3293. num_tfeedback_decls, tfeedback_decls))
  3294. goto done;
  3295. }
  3296. do_dead_builtin_varyings(ctx, sh, NULL,
  3297. num_tfeedback_decls, tfeedback_decls);
  3298. if (!prog->SeparateShader)
  3299. demote_shader_inputs_and_outputs(sh, ir_var_shader_out);
  3300. /* Eliminate code that is now dead due to unused outputs being demoted.
  3301. */
  3302. while (do_dead_code(sh->ir, false))
  3303. ;
  3304. }
  3305. else if (first == MESA_SHADER_FRAGMENT) {
  3306. /* If the program only contains a fragment shader...
  3307. */
  3308. gl_shader *const sh = prog->_LinkedShaders[first];
  3309. do_dead_builtin_varyings(ctx, NULL, sh,
  3310. num_tfeedback_decls, tfeedback_decls);
  3311. if (prog->SeparateShader) {
  3312. if (!assign_varying_locations(ctx, mem_ctx, prog,
  3313. NULL /* producer */,
  3314. sh /* consumer */,
  3315. 0 /* num_tfeedback_decls */,
  3316. NULL /* tfeedback_decls */))
  3317. goto done;
  3318. } else
  3319. demote_shader_inputs_and_outputs(sh, ir_var_shader_in);
  3320. while (do_dead_code(sh->ir, false))
  3321. ;
  3322. }
  3323. next = last;
  3324. for (int i = next - 1; i >= 0; i--) {
  3325. if (prog->_LinkedShaders[i] == NULL)
  3326. continue;
  3327. gl_shader *const sh_i = prog->_LinkedShaders[i];
  3328. gl_shader *const sh_next = prog->_LinkedShaders[next];
  3329. if (!assign_varying_locations(ctx, mem_ctx, prog, sh_i, sh_next,
  3330. next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
  3331. tfeedback_decls))
  3332. goto done;
  3333. do_dead_builtin_varyings(ctx, sh_i, sh_next,
  3334. next == MESA_SHADER_FRAGMENT ? num_tfeedback_decls : 0,
  3335. tfeedback_decls);
  3336. demote_shader_inputs_and_outputs(sh_i, ir_var_shader_out);
  3337. demote_shader_inputs_and_outputs(sh_next, ir_var_shader_in);
  3338. /* Eliminate code that is now dead due to unused outputs being demoted.
  3339. */
  3340. while (do_dead_code(sh_i->ir, false))
  3341. ;
  3342. while (do_dead_code(sh_next->ir, false))
  3343. ;
  3344. /* This must be done after all dead varyings are eliminated. */
  3345. if (!check_against_output_limit(ctx, prog, sh_i))
  3346. goto done;
  3347. if (!check_against_input_limit(ctx, prog, sh_next))
  3348. goto done;
  3349. next = i;
  3350. }
  3351. if (!store_tfeedback_info(ctx, prog, num_tfeedback_decls, tfeedback_decls))
  3352. goto done;
  3353. update_array_sizes(prog);
  3354. link_assign_uniform_locations(prog, ctx->Const.UniformBooleanTrue);
  3355. link_assign_atomic_counter_resources(ctx, prog);
  3356. store_fragdepth_layout(prog);
  3357. link_calculate_subroutine_compat(prog);
  3358. check_resources(ctx, prog);
  3359. check_subroutine_resources(prog);
  3360. check_image_resources(ctx, prog);
  3361. link_check_atomic_counter_resources(ctx, prog);
  3362. if (!prog->LinkStatus)
  3363. goto done;
  3364. /* OpenGL ES requires that a vertex shader and a fragment shader both be
  3365. * present in a linked program. GL_ARB_ES2_compatibility doesn't say
  3366. * anything about shader linking when one of the shaders (vertex or
  3367. * fragment shader) is absent. So, the extension shouldn't change the
  3368. * behavior specified in GLSL specification.
  3369. */
  3370. if (!prog->SeparateShader && ctx->API == API_OPENGLES2) {
  3371. /* With ES < 3.1 one needs to have always vertex + fragment shader. */
  3372. if (ctx->Version < 31) {
  3373. if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
  3374. linker_error(prog, "program lacks a vertex shader\n");
  3375. } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
  3376. linker_error(prog, "program lacks a fragment shader\n");
  3377. }
  3378. } else {
  3379. /* From OpenGL ES 3.1 specification (7.3 Program Objects):
  3380. * "Linking can fail for a variety of reasons as specified in the
  3381. * OpenGL ES Shading Language Specification, as well as any of the
  3382. * following reasons:
  3383. *
  3384. * ...
  3385. *
  3386. * * program contains objects to form either a vertex shader or
  3387. * fragment shader, and program is not separable, and does not
  3388. * contain objects to form both a vertex shader and fragment
  3389. * shader."
  3390. */
  3391. if (!!prog->_LinkedShaders[MESA_SHADER_VERTEX] ^
  3392. !!prog->_LinkedShaders[MESA_SHADER_FRAGMENT]) {
  3393. linker_error(prog, "Program needs to contain both vertex and "
  3394. "fragment shaders.\n");
  3395. }
  3396. }
  3397. }
  3398. /* FINISHME: Assign fragment shader output locations. */
  3399. done:
  3400. for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
  3401. free(shader_list[i]);
  3402. if (prog->_LinkedShaders[i] == NULL)
  3403. continue;
  3404. /* Do a final validation step to make sure that the IR wasn't
  3405. * invalidated by any modifications performed after intrastage linking.
  3406. */
  3407. validate_ir_tree(prog->_LinkedShaders[i]->ir);
  3408. /* Retain any live IR, but trash the rest. */
  3409. reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
  3410. /* The symbol table in the linked shaders may contain references to
  3411. * variables that were removed (e.g., unused uniforms). Since it may
  3412. * contain junk, there is no possible valid use. Delete it and set the
  3413. * pointer to NULL.
  3414. */
  3415. delete prog->_LinkedShaders[i]->symbols;
  3416. prog->_LinkedShaders[i]->symbols = NULL;
  3417. }
  3418. ralloc_free(mem_ctx);
  3419. }