shader_language.cpp 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825
  1. /*************************************************************************/
  2. /* shader_language.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "shader_language.h"
  31. #include "core/os/os.h"
  32. #include "core/print_string.h"
  33. static bool _is_text_char(CharType c) {
  34. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
  35. }
  36. static bool _is_number(CharType c) {
  37. return (c >= '0' && c <= '9');
  38. }
  39. static bool _is_hex(CharType c) {
  40. return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
  41. }
  42. String ShaderLanguage::get_operator_text(Operator p_op) {
  43. static const char *op_names[OP_MAX] = { "==",
  44. "!=",
  45. "<",
  46. "<=",
  47. ">",
  48. ">=",
  49. "&&",
  50. "||",
  51. "!",
  52. "-",
  53. "+",
  54. "-",
  55. "*",
  56. "/",
  57. "%",
  58. "<<",
  59. ">>",
  60. "=",
  61. "+=",
  62. "-=",
  63. "*=",
  64. "/=",
  65. "%=",
  66. "<<=",
  67. ">>=",
  68. "&=",
  69. "|=",
  70. "^=",
  71. "&",
  72. "|",
  73. "^",
  74. "~",
  75. "++",
  76. "--",
  77. "?",
  78. ":",
  79. "++",
  80. "--",
  81. "()",
  82. "construct",
  83. "index" };
  84. return op_names[p_op];
  85. }
  86. const char *ShaderLanguage::token_names[TK_MAX] = {
  87. "EMPTY",
  88. "IDENTIFIER",
  89. "TRUE",
  90. "FALSE",
  91. "REAL_CONSTANT",
  92. "INT_CONSTANT",
  93. "TYPE_VOID",
  94. "TYPE_BOOL",
  95. "TYPE_BVEC2",
  96. "TYPE_BVEC3",
  97. "TYPE_BVEC4",
  98. "TYPE_INT",
  99. "TYPE_IVEC2",
  100. "TYPE_IVEC3",
  101. "TYPE_IVEC4",
  102. "TYPE_UINT",
  103. "TYPE_UVEC2",
  104. "TYPE_UVEC3",
  105. "TYPE_UVEC4",
  106. "TYPE_FLOAT",
  107. "TYPE_VEC2",
  108. "TYPE_VEC3",
  109. "TYPE_VEC4",
  110. "TYPE_MAT2",
  111. "TYPE_MAT3",
  112. "TYPE_MAT4",
  113. "TYPE_SAMPLER2D",
  114. "TYPE_ISAMPLER2D",
  115. "TYPE_USAMPLER2D",
  116. "TYPE_SAMPLER2DARRAY",
  117. "TYPE_ISAMPLER2DARRAY",
  118. "TYPE_USAMPLER2DARRAY",
  119. "TYPE_SAMPLER3D",
  120. "TYPE_ISAMPLER3D",
  121. "TYPE_USAMPLER3D",
  122. "TYPE_SAMPLERCUBE",
  123. "INTERPOLATION_FLAT",
  124. "INTERPOLATION_SMOOTH",
  125. "PRECISION_LOW",
  126. "PRECISION_MID",
  127. "PRECISION_HIGH",
  128. "OP_EQUAL",
  129. "OP_NOT_EQUAL",
  130. "OP_LESS",
  131. "OP_LESS_EQUAL",
  132. "OP_GREATER",
  133. "OP_GREATER_EQUAL",
  134. "OP_AND",
  135. "OP_OR",
  136. "OP_NOT",
  137. "OP_ADD",
  138. "OP_SUB",
  139. "OP_MUL",
  140. "OP_DIV",
  141. "OP_MOD",
  142. "OP_SHIFT_LEFT",
  143. "OP_SHIFT_RIGHT",
  144. "OP_ASSIGN",
  145. "OP_ASSIGN_ADD",
  146. "OP_ASSIGN_SUB",
  147. "OP_ASSIGN_MUL",
  148. "OP_ASSIGN_DIV",
  149. "OP_ASSIGN_MOD",
  150. "OP_ASSIGN_SHIFT_LEFT",
  151. "OP_ASSIGN_SHIFT_RIGHT",
  152. "OP_ASSIGN_BIT_AND",
  153. "OP_ASSIGN_BIT_OR",
  154. "OP_ASSIGN_BIT_XOR",
  155. "OP_BIT_AND",
  156. "OP_BIT_OR",
  157. "OP_BIT_XOR",
  158. "OP_BIT_INVERT",
  159. "OP_INCREMENT",
  160. "OP_DECREMENT",
  161. "CF_IF",
  162. "CF_ELSE",
  163. "CF_FOR",
  164. "CF_WHILE",
  165. "CF_DO",
  166. "CF_SWITCH",
  167. "CF_CASE",
  168. "CF_BREAK",
  169. "CF_CONTINUE",
  170. "CF_RETURN",
  171. "CF_DISCARD",
  172. "BRACKET_OPEN",
  173. "BRACKET_CLOSE",
  174. "CURLY_BRACKET_OPEN",
  175. "CURLY_BRACKET_CLOSE",
  176. "PARENTHESIS_OPEN",
  177. "PARENTHESIS_CLOSE",
  178. "QUESTION",
  179. "COMMA",
  180. "COLON",
  181. "SEMICOLON",
  182. "PERIOD",
  183. "UNIFORM",
  184. "VARYING",
  185. "IN",
  186. "OUT",
  187. "INOUT",
  188. "RENDER_MODE",
  189. "HINT_WHITE_TEXTURE",
  190. "HINT_BLACK_TEXTURE",
  191. "HINT_NORMAL_TEXTURE",
  192. "HINT_ANISO_TEXTURE",
  193. "HINT_ALBEDO_TEXTURE",
  194. "HINT_BLACK_ALBEDO_TEXTURE",
  195. "HINT_COLOR",
  196. "HINT_RANGE",
  197. "SHADER_TYPE",
  198. "CURSOR",
  199. "ERROR",
  200. "EOF",
  201. };
  202. String ShaderLanguage::get_token_text(Token p_token) {
  203. String name = token_names[p_token.type];
  204. if (p_token.type == TK_INT_CONSTANT || p_token.type == TK_REAL_CONSTANT) {
  205. name += "(" + rtos(p_token.constant) + ")";
  206. } else if (p_token.type == TK_IDENTIFIER) {
  207. name += "(" + String(p_token.text) + ")";
  208. } else if (p_token.type == TK_ERROR) {
  209. name += "(" + String(p_token.text) + ")";
  210. }
  211. return name;
  212. }
  213. ShaderLanguage::Token ShaderLanguage::_make_token(TokenType p_type, const StringName &p_text) {
  214. Token tk;
  215. tk.type = p_type;
  216. tk.text = p_text;
  217. tk.line = tk_line;
  218. if (tk.type == TK_ERROR) {
  219. _set_error(p_text);
  220. }
  221. return tk;
  222. }
  223. const ShaderLanguage::KeyWord ShaderLanguage::keyword_list[] = {
  224. { TK_TRUE, "true" },
  225. { TK_FALSE, "false" },
  226. { TK_TYPE_VOID, "void" },
  227. { TK_TYPE_BOOL, "bool" },
  228. { TK_TYPE_BVEC2, "bvec2" },
  229. { TK_TYPE_BVEC3, "bvec3" },
  230. { TK_TYPE_BVEC4, "bvec4" },
  231. { TK_TYPE_INT, "int" },
  232. { TK_TYPE_IVEC2, "ivec2" },
  233. { TK_TYPE_IVEC3, "ivec3" },
  234. { TK_TYPE_IVEC4, "ivec4" },
  235. { TK_TYPE_UINT, "uint" },
  236. { TK_TYPE_UVEC2, "uvec2" },
  237. { TK_TYPE_UVEC3, "uvec3" },
  238. { TK_TYPE_UVEC4, "uvec4" },
  239. { TK_TYPE_FLOAT, "float" },
  240. { TK_TYPE_VEC2, "vec2" },
  241. { TK_TYPE_VEC3, "vec3" },
  242. { TK_TYPE_VEC4, "vec4" },
  243. { TK_TYPE_MAT2, "mat2" },
  244. { TK_TYPE_MAT3, "mat3" },
  245. { TK_TYPE_MAT4, "mat4" },
  246. { TK_TYPE_SAMPLER2D, "sampler2D" },
  247. { TK_TYPE_ISAMPLER2D, "isampler2D" },
  248. { TK_TYPE_USAMPLER2D, "usampler2D" },
  249. { TK_TYPE_SAMPLER2DARRAY, "sampler2DArray" },
  250. { TK_TYPE_ISAMPLER2DARRAY, "isampler2DArray" },
  251. { TK_TYPE_USAMPLER2DARRAY, "usampler2DArray" },
  252. { TK_TYPE_SAMPLER3D, "sampler3D" },
  253. { TK_TYPE_ISAMPLER3D, "isampler3D" },
  254. { TK_TYPE_USAMPLER3D, "usampler3D" },
  255. { TK_TYPE_SAMPLERCUBE, "samplerCube" },
  256. { TK_INTERPOLATION_FLAT, "flat" },
  257. { TK_INTERPOLATION_SMOOTH, "smooth" },
  258. { TK_PRECISION_LOW, "lowp" },
  259. { TK_PRECISION_MID, "mediump" },
  260. { TK_PRECISION_HIGH, "highp" },
  261. { TK_CF_IF, "if" },
  262. { TK_CF_ELSE, "else" },
  263. { TK_CF_FOR, "for" },
  264. { TK_CF_WHILE, "while" },
  265. { TK_CF_DO, "do" },
  266. { TK_CF_SWITCH, "switch" },
  267. { TK_CF_CASE, "case" },
  268. { TK_CF_BREAK, "break" },
  269. { TK_CF_CONTINUE, "continue" },
  270. { TK_CF_RETURN, "return" },
  271. { TK_CF_DISCARD, "discard" },
  272. { TK_UNIFORM, "uniform" },
  273. { TK_VARYING, "varying" },
  274. { TK_ARG_IN, "in" },
  275. { TK_ARG_OUT, "out" },
  276. { TK_ARG_INOUT, "inout" },
  277. { TK_RENDER_MODE, "render_mode" },
  278. { TK_HINT_WHITE_TEXTURE, "hint_white" },
  279. { TK_HINT_BLACK_TEXTURE, "hint_black" },
  280. { TK_HINT_NORMAL_TEXTURE, "hint_normal" },
  281. { TK_HINT_ANISO_TEXTURE, "hint_aniso" },
  282. { TK_HINT_ALBEDO_TEXTURE, "hint_albedo" },
  283. { TK_HINT_BLACK_ALBEDO_TEXTURE, "hint_black_albedo" },
  284. { TK_HINT_COLOR, "hint_color" },
  285. { TK_HINT_RANGE, "hint_range" },
  286. { TK_SHADER_TYPE, "shader_type" },
  287. { TK_ERROR, NULL }
  288. };
  289. ShaderLanguage::Token ShaderLanguage::_get_token() {
  290. #define GETCHAR(m_idx) (((char_idx + m_idx) < code.length()) ? code[char_idx + m_idx] : CharType(0))
  291. while (true) {
  292. char_idx++;
  293. switch (GETCHAR(-1)) {
  294. case 0:
  295. return _make_token(TK_EOF);
  296. case 0xFFFF:
  297. return _make_token(TK_CURSOR); //for completion
  298. case '\t':
  299. case '\r':
  300. case ' ':
  301. continue;
  302. case '\n':
  303. tk_line++;
  304. continue;
  305. case '/': {
  306. switch (GETCHAR(0)) {
  307. case '*': { // block comment
  308. char_idx++;
  309. while (true) {
  310. if (GETCHAR(0) == 0) {
  311. return _make_token(TK_EOF);
  312. }
  313. if (GETCHAR(0) == '*' && GETCHAR(1) == '/') {
  314. char_idx += 2;
  315. break;
  316. } else if (GETCHAR(0) == '\n') {
  317. tk_line++;
  318. }
  319. char_idx++;
  320. }
  321. } break;
  322. case '/': { // line comment skip
  323. while (true) {
  324. if (GETCHAR(0) == '\n') {
  325. tk_line++;
  326. char_idx++;
  327. break;
  328. }
  329. if (GETCHAR(0) == 0) {
  330. return _make_token(TK_EOF);
  331. }
  332. char_idx++;
  333. }
  334. } break;
  335. case '=': { // diveq
  336. char_idx++;
  337. return _make_token(TK_OP_ASSIGN_DIV);
  338. } break;
  339. default:
  340. return _make_token(TK_OP_DIV);
  341. }
  342. continue; //a comment, continue to next token
  343. } break;
  344. case '=': {
  345. if (GETCHAR(0) == '=') {
  346. char_idx++;
  347. return _make_token(TK_OP_EQUAL);
  348. }
  349. return _make_token(TK_OP_ASSIGN);
  350. } break;
  351. case '<': {
  352. if (GETCHAR(0) == '=') {
  353. char_idx++;
  354. return _make_token(TK_OP_LESS_EQUAL);
  355. } else if (GETCHAR(0) == '<') {
  356. char_idx++;
  357. if (GETCHAR(0) == '=') {
  358. char_idx++;
  359. return _make_token(TK_OP_ASSIGN_SHIFT_LEFT);
  360. }
  361. return _make_token(TK_OP_SHIFT_LEFT);
  362. }
  363. return _make_token(TK_OP_LESS);
  364. } break;
  365. case '>': {
  366. if (GETCHAR(0) == '=') {
  367. char_idx++;
  368. return _make_token(TK_OP_GREATER_EQUAL);
  369. } else if (GETCHAR(0) == '>') {
  370. char_idx++;
  371. if (GETCHAR(0) == '=') {
  372. char_idx++;
  373. return _make_token(TK_OP_ASSIGN_SHIFT_RIGHT);
  374. }
  375. return _make_token(TK_OP_SHIFT_RIGHT);
  376. }
  377. return _make_token(TK_OP_GREATER);
  378. } break;
  379. case '!': {
  380. if (GETCHAR(0) == '=') {
  381. char_idx++;
  382. return _make_token(TK_OP_NOT_EQUAL);
  383. }
  384. return _make_token(TK_OP_NOT);
  385. } break;
  386. //case '"' //string - no strings in shader
  387. //case '\'' //string - no strings in shader
  388. case '{':
  389. return _make_token(TK_CURLY_BRACKET_OPEN);
  390. case '}':
  391. return _make_token(TK_CURLY_BRACKET_CLOSE);
  392. case '[':
  393. return _make_token(TK_BRACKET_OPEN);
  394. case ']':
  395. return _make_token(TK_BRACKET_CLOSE);
  396. case '(':
  397. return _make_token(TK_PARENTHESIS_OPEN);
  398. case ')':
  399. return _make_token(TK_PARENTHESIS_CLOSE);
  400. case ',':
  401. return _make_token(TK_COMMA);
  402. case ';':
  403. return _make_token(TK_SEMICOLON);
  404. case '?':
  405. return _make_token(TK_QUESTION);
  406. case ':':
  407. return _make_token(TK_COLON);
  408. case '^':
  409. return _make_token(TK_OP_BIT_XOR);
  410. case '~':
  411. return _make_token(TK_OP_BIT_INVERT);
  412. case '&': {
  413. if (GETCHAR(0) == '=') {
  414. char_idx++;
  415. return _make_token(TK_OP_ASSIGN_BIT_AND);
  416. } else if (GETCHAR(0) == '&') {
  417. char_idx++;
  418. return _make_token(TK_OP_AND);
  419. }
  420. return _make_token(TK_OP_BIT_AND);
  421. } break;
  422. case '|': {
  423. if (GETCHAR(0) == '=') {
  424. char_idx++;
  425. return _make_token(TK_OP_ASSIGN_BIT_OR);
  426. } else if (GETCHAR(0) == '|') {
  427. char_idx++;
  428. return _make_token(TK_OP_OR);
  429. }
  430. return _make_token(TK_OP_BIT_OR);
  431. } break;
  432. case '*': {
  433. if (GETCHAR(0) == '=') {
  434. char_idx++;
  435. return _make_token(TK_OP_ASSIGN_MUL);
  436. }
  437. return _make_token(TK_OP_MUL);
  438. } break;
  439. case '+': {
  440. if (GETCHAR(0) == '=') {
  441. char_idx++;
  442. return _make_token(TK_OP_ASSIGN_ADD);
  443. } else if (GETCHAR(0) == '+') {
  444. char_idx++;
  445. return _make_token(TK_OP_INCREMENT);
  446. }
  447. return _make_token(TK_OP_ADD);
  448. } break;
  449. case '-': {
  450. if (GETCHAR(0) == '=') {
  451. char_idx++;
  452. return _make_token(TK_OP_ASSIGN_SUB);
  453. } else if (GETCHAR(0) == '-') {
  454. char_idx++;
  455. return _make_token(TK_OP_DECREMENT);
  456. }
  457. return _make_token(TK_OP_SUB);
  458. } break;
  459. case '%': {
  460. if (GETCHAR(0) == '=') {
  461. char_idx++;
  462. return _make_token(TK_OP_ASSIGN_MOD);
  463. }
  464. return _make_token(TK_OP_MOD);
  465. } break;
  466. default: {
  467. char_idx--; //go back one, since we have no idea what this is
  468. if (_is_number(GETCHAR(0)) || (GETCHAR(0) == '.' && _is_number(GETCHAR(1)))) {
  469. // parse number
  470. bool period_found = false;
  471. bool exponent_found = false;
  472. bool hexa_found = false;
  473. bool sign_found = false;
  474. bool float_suffix_found = false;
  475. String str;
  476. int i = 0;
  477. while (true) {
  478. if (GETCHAR(i) == '.') {
  479. if (period_found || exponent_found || hexa_found || float_suffix_found)
  480. return _make_token(TK_ERROR, "Invalid numeric constant");
  481. period_found = true;
  482. } else if (GETCHAR(i) == 'x') {
  483. if (hexa_found || str.length() != 1 || str[0] != '0')
  484. return _make_token(TK_ERROR, "Invalid numeric constant");
  485. hexa_found = true;
  486. } else if (GETCHAR(i) == 'e') {
  487. if (hexa_found || exponent_found || float_suffix_found)
  488. return _make_token(TK_ERROR, "Invalid numeric constant");
  489. exponent_found = true;
  490. } else if (GETCHAR(i) == 'f') {
  491. if (hexa_found || exponent_found)
  492. return _make_token(TK_ERROR, "Invalid numeric constant");
  493. float_suffix_found = true;
  494. } else if (_is_number(GETCHAR(i))) {
  495. if (float_suffix_found)
  496. return _make_token(TK_ERROR, "Invalid numeric constant");
  497. } else if (hexa_found && _is_hex(GETCHAR(i))) {
  498. } else if ((GETCHAR(i) == '-' || GETCHAR(i) == '+') && exponent_found) {
  499. if (sign_found)
  500. return _make_token(TK_ERROR, "Invalid numeric constant");
  501. sign_found = true;
  502. } else
  503. break;
  504. str += CharType(GETCHAR(i));
  505. i++;
  506. }
  507. CharType last_char = str[str.length() - 1];
  508. if (hexa_found) {
  509. //hex integers eg."0xFF" or "0x12AB", etc - NOT supported yet
  510. return _make_token(TK_ERROR, "Invalid (hexadecimal) numeric constant - Not supported");
  511. } else if (period_found || exponent_found || float_suffix_found) {
  512. //floats
  513. if (period_found) {
  514. if (float_suffix_found) {
  515. //checks for eg "1.f" or "1.99f" notations
  516. if (last_char != 'f') {
  517. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  518. }
  519. } else {
  520. //checks for eg. "1." or "1.99" notations
  521. if (last_char != '.' && !_is_number(last_char)) {
  522. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  523. }
  524. }
  525. } else if (float_suffix_found) {
  526. // if no period found the float suffix must be the last character, like in "2f" for "2.0"
  527. if (last_char != 'f') {
  528. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  529. }
  530. }
  531. if (float_suffix_found) {
  532. //strip the suffix
  533. str = str.left(str.length() - 1);
  534. //compensate reading cursor position
  535. char_idx += 1;
  536. }
  537. if (!str.is_valid_float()) {
  538. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  539. }
  540. } else {
  541. //integers
  542. if (!_is_number(last_char)) {
  543. return _make_token(TK_ERROR, "Invalid (integer) numeric constant");
  544. }
  545. if (!str.is_valid_integer()) {
  546. return _make_token(TK_ERROR, "Invalid numeric constant");
  547. }
  548. }
  549. char_idx += str.length();
  550. Token tk;
  551. if (period_found || exponent_found || float_suffix_found)
  552. tk.type = TK_REAL_CONSTANT;
  553. else
  554. tk.type = TK_INT_CONSTANT;
  555. tk.constant = str.to_double(); //wont work with hex
  556. tk.line = tk_line;
  557. return tk;
  558. }
  559. if (GETCHAR(0) == '.') {
  560. //parse period
  561. char_idx++;
  562. return _make_token(TK_PERIOD);
  563. }
  564. if (_is_text_char(GETCHAR(0))) {
  565. // parse identifier
  566. String str;
  567. while (_is_text_char(GETCHAR(0))) {
  568. str += CharType(GETCHAR(0));
  569. char_idx++;
  570. }
  571. //see if keyword
  572. //should be converted to a static map
  573. int idx = 0;
  574. while (keyword_list[idx].text) {
  575. if (str == keyword_list[idx].text) {
  576. return _make_token(keyword_list[idx].token);
  577. }
  578. idx++;
  579. }
  580. return _make_token(TK_IDENTIFIER, str);
  581. }
  582. if (GETCHAR(0) > 32)
  583. return _make_token(TK_ERROR, "Tokenizer: Unknown character #" + itos(GETCHAR(0)) + ": '" + String::chr(GETCHAR(0)) + "'");
  584. else
  585. return _make_token(TK_ERROR, "Tokenizer: Unknown character #" + itos(GETCHAR(0)));
  586. } break;
  587. }
  588. }
  589. ERR_PRINT("BUG");
  590. return Token();
  591. #undef GETCHAR
  592. }
  593. String ShaderLanguage::token_debug(const String &p_code) {
  594. clear();
  595. code = p_code;
  596. String output;
  597. Token tk = _get_token();
  598. while (tk.type != TK_EOF && tk.type != TK_ERROR) {
  599. output += itos(tk_line) + ": " + get_token_text(tk) + "\n";
  600. tk = _get_token();
  601. }
  602. return output;
  603. }
  604. bool ShaderLanguage::is_token_variable_datatype(TokenType p_type) {
  605. return (
  606. p_type == TK_TYPE_VOID ||
  607. p_type == TK_TYPE_BOOL ||
  608. p_type == TK_TYPE_BVEC2 ||
  609. p_type == TK_TYPE_BVEC3 ||
  610. p_type == TK_TYPE_BVEC4 ||
  611. p_type == TK_TYPE_INT ||
  612. p_type == TK_TYPE_IVEC2 ||
  613. p_type == TK_TYPE_IVEC3 ||
  614. p_type == TK_TYPE_IVEC4 ||
  615. p_type == TK_TYPE_UINT ||
  616. p_type == TK_TYPE_UVEC2 ||
  617. p_type == TK_TYPE_UVEC3 ||
  618. p_type == TK_TYPE_UVEC4 ||
  619. p_type == TK_TYPE_FLOAT ||
  620. p_type == TK_TYPE_VEC2 ||
  621. p_type == TK_TYPE_VEC3 ||
  622. p_type == TK_TYPE_VEC4 ||
  623. p_type == TK_TYPE_MAT2 ||
  624. p_type == TK_TYPE_MAT3 ||
  625. p_type == TK_TYPE_MAT4);
  626. }
  627. bool ShaderLanguage::is_token_datatype(TokenType p_type) {
  628. return (
  629. p_type == TK_TYPE_VOID ||
  630. p_type == TK_TYPE_BOOL ||
  631. p_type == TK_TYPE_BVEC2 ||
  632. p_type == TK_TYPE_BVEC3 ||
  633. p_type == TK_TYPE_BVEC4 ||
  634. p_type == TK_TYPE_INT ||
  635. p_type == TK_TYPE_IVEC2 ||
  636. p_type == TK_TYPE_IVEC3 ||
  637. p_type == TK_TYPE_IVEC4 ||
  638. p_type == TK_TYPE_UINT ||
  639. p_type == TK_TYPE_UVEC2 ||
  640. p_type == TK_TYPE_UVEC3 ||
  641. p_type == TK_TYPE_UVEC4 ||
  642. p_type == TK_TYPE_FLOAT ||
  643. p_type == TK_TYPE_VEC2 ||
  644. p_type == TK_TYPE_VEC3 ||
  645. p_type == TK_TYPE_VEC4 ||
  646. p_type == TK_TYPE_MAT2 ||
  647. p_type == TK_TYPE_MAT3 ||
  648. p_type == TK_TYPE_MAT4 ||
  649. p_type == TK_TYPE_SAMPLER2D ||
  650. p_type == TK_TYPE_ISAMPLER2D ||
  651. p_type == TK_TYPE_USAMPLER2D ||
  652. p_type == TK_TYPE_SAMPLER2DARRAY ||
  653. p_type == TK_TYPE_ISAMPLER2DARRAY ||
  654. p_type == TK_TYPE_USAMPLER2DARRAY ||
  655. p_type == TK_TYPE_SAMPLER3D ||
  656. p_type == TK_TYPE_ISAMPLER3D ||
  657. p_type == TK_TYPE_USAMPLER3D ||
  658. p_type == TK_TYPE_SAMPLERCUBE);
  659. }
  660. ShaderLanguage::DataType ShaderLanguage::get_token_datatype(TokenType p_type) {
  661. return DataType(p_type - TK_TYPE_VOID);
  662. }
  663. bool ShaderLanguage::is_token_interpolation(TokenType p_type) {
  664. return (
  665. p_type == TK_INTERPOLATION_FLAT ||
  666. p_type == TK_INTERPOLATION_SMOOTH);
  667. }
  668. ShaderLanguage::DataInterpolation ShaderLanguage::get_token_interpolation(TokenType p_type) {
  669. if (p_type == TK_INTERPOLATION_FLAT)
  670. return INTERPOLATION_FLAT;
  671. else
  672. return INTERPOLATION_SMOOTH;
  673. }
  674. bool ShaderLanguage::is_token_precision(TokenType p_type) {
  675. return (
  676. p_type == TK_PRECISION_LOW ||
  677. p_type == TK_PRECISION_MID ||
  678. p_type == TK_PRECISION_HIGH);
  679. }
  680. ShaderLanguage::DataPrecision ShaderLanguage::get_token_precision(TokenType p_type) {
  681. if (p_type == TK_PRECISION_LOW)
  682. return PRECISION_LOWP;
  683. else if (p_type == TK_PRECISION_HIGH)
  684. return PRECISION_HIGHP;
  685. else
  686. return PRECISION_MEDIUMP;
  687. }
  688. String ShaderLanguage::get_datatype_name(DataType p_type) {
  689. switch (p_type) {
  690. case TYPE_VOID: return "void";
  691. case TYPE_BOOL: return "bool";
  692. case TYPE_BVEC2: return "bvec2";
  693. case TYPE_BVEC3: return "bvec3";
  694. case TYPE_BVEC4: return "bvec4";
  695. case TYPE_INT: return "int";
  696. case TYPE_IVEC2: return "ivec2";
  697. case TYPE_IVEC3: return "ivec3";
  698. case TYPE_IVEC4: return "ivec4";
  699. case TYPE_UINT: return "uint";
  700. case TYPE_UVEC2: return "uvec2";
  701. case TYPE_UVEC3: return "uvec3";
  702. case TYPE_UVEC4: return "uvec4";
  703. case TYPE_FLOAT: return "float";
  704. case TYPE_VEC2: return "vec2";
  705. case TYPE_VEC3: return "vec3";
  706. case TYPE_VEC4: return "vec4";
  707. case TYPE_MAT2: return "mat2";
  708. case TYPE_MAT3: return "mat3";
  709. case TYPE_MAT4: return "mat4";
  710. case TYPE_SAMPLER2D: return "sampler2D";
  711. case TYPE_ISAMPLER2D: return "isampler2D";
  712. case TYPE_USAMPLER2D: return "usampler2D";
  713. case TYPE_SAMPLER2DARRAY: return "sampler2DArray";
  714. case TYPE_ISAMPLER2DARRAY: return "isampler2DArray";
  715. case TYPE_USAMPLER2DARRAY: return "usampler2DArray";
  716. case TYPE_SAMPLER3D: return "sampler3D";
  717. case TYPE_ISAMPLER3D: return "isampler3D";
  718. case TYPE_USAMPLER3D: return "usampler3D";
  719. case TYPE_SAMPLERCUBE: return "samplerCube";
  720. }
  721. return "";
  722. }
  723. bool ShaderLanguage::is_token_nonvoid_datatype(TokenType p_type) {
  724. return is_token_datatype(p_type) && p_type != TK_TYPE_VOID;
  725. }
  726. void ShaderLanguage::clear() {
  727. current_function = StringName();
  728. completion_type = COMPLETION_NONE;
  729. completion_block = NULL;
  730. completion_function = StringName();
  731. error_line = 0;
  732. tk_line = 1;
  733. char_idx = 0;
  734. error_set = false;
  735. error_str = "";
  736. while (nodes) {
  737. Node *n = nodes;
  738. nodes = nodes->next;
  739. memdelete(n);
  740. }
  741. }
  742. bool ShaderLanguage::_find_identifier(const BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, const StringName &p_identifier, DataType *r_data_type, IdentifierType *r_type) {
  743. if (p_builtin_types.has(p_identifier)) {
  744. if (r_data_type) {
  745. *r_data_type = p_builtin_types[p_identifier].type;
  746. }
  747. if (r_type) {
  748. *r_type = IDENTIFIER_BUILTIN_VAR;
  749. }
  750. return true;
  751. }
  752. FunctionNode *function = NULL;
  753. while (p_block) {
  754. if (p_block->variables.has(p_identifier)) {
  755. if (r_data_type) {
  756. *r_data_type = p_block->variables[p_identifier].type;
  757. }
  758. if (r_type) {
  759. *r_type = IDENTIFIER_LOCAL_VAR;
  760. }
  761. return true;
  762. }
  763. if (p_block->parent_function) {
  764. function = p_block->parent_function;
  765. break;
  766. } else {
  767. ERR_FAIL_COND_V(!p_block->parent_block, false);
  768. p_block = p_block->parent_block;
  769. }
  770. }
  771. if (function) {
  772. for (int i = 0; i < function->arguments.size(); i++) {
  773. if (function->arguments[i].name == p_identifier) {
  774. if (r_data_type) {
  775. *r_data_type = function->arguments[i].type;
  776. }
  777. if (r_type) {
  778. *r_type = IDENTIFIER_FUNCTION_ARGUMENT;
  779. }
  780. return true;
  781. }
  782. }
  783. }
  784. if (shader->varyings.has(p_identifier)) {
  785. if (r_data_type) {
  786. *r_data_type = shader->varyings[p_identifier].type;
  787. }
  788. if (r_type) {
  789. *r_type = IDENTIFIER_VARYING;
  790. }
  791. return true;
  792. }
  793. if (shader->uniforms.has(p_identifier)) {
  794. if (r_data_type) {
  795. *r_data_type = shader->uniforms[p_identifier].type;
  796. }
  797. if (r_type) {
  798. *r_type = IDENTIFIER_UNIFORM;
  799. }
  800. return true;
  801. }
  802. for (int i = 0; i < shader->functions.size(); i++) {
  803. if (!shader->functions[i].callable)
  804. continue;
  805. if (shader->functions[i].name == p_identifier) {
  806. if (r_data_type) {
  807. *r_data_type = shader->functions[i].function->return_type;
  808. }
  809. if (r_type) {
  810. *r_type = IDENTIFIER_FUNCTION;
  811. }
  812. }
  813. }
  814. return false;
  815. }
  816. bool ShaderLanguage::_validate_operator(OperatorNode *p_op, DataType *r_ret_type) {
  817. bool valid = false;
  818. DataType ret_type;
  819. switch (p_op->op) {
  820. case OP_EQUAL:
  821. case OP_NOT_EQUAL: {
  822. DataType na = p_op->arguments[0]->get_datatype();
  823. DataType nb = p_op->arguments[1]->get_datatype();
  824. valid = na == nb;
  825. ret_type = TYPE_BOOL;
  826. } break;
  827. case OP_LESS:
  828. case OP_LESS_EQUAL:
  829. case OP_GREATER:
  830. case OP_GREATER_EQUAL: {
  831. DataType na = p_op->arguments[0]->get_datatype();
  832. DataType nb = p_op->arguments[1]->get_datatype();
  833. valid = na == nb && (na == TYPE_UINT || na == TYPE_INT || na == TYPE_FLOAT);
  834. ret_type = TYPE_BOOL;
  835. } break;
  836. case OP_AND:
  837. case OP_OR: {
  838. DataType na = p_op->arguments[0]->get_datatype();
  839. DataType nb = p_op->arguments[1]->get_datatype();
  840. valid = na == nb && na == TYPE_BOOL;
  841. ret_type = TYPE_BOOL;
  842. } break;
  843. case OP_NOT: {
  844. DataType na = p_op->arguments[0]->get_datatype();
  845. valid = na == TYPE_BOOL;
  846. ret_type = TYPE_BOOL;
  847. } break;
  848. case OP_INCREMENT:
  849. case OP_DECREMENT:
  850. case OP_POST_INCREMENT:
  851. case OP_POST_DECREMENT:
  852. case OP_NEGATE: {
  853. DataType na = p_op->arguments[0]->get_datatype();
  854. valid = na > TYPE_BOOL && na < TYPE_MAT2;
  855. ret_type = na;
  856. } break;
  857. case OP_ADD:
  858. case OP_SUB:
  859. case OP_MUL:
  860. case OP_DIV: {
  861. DataType na = p_op->arguments[0]->get_datatype();
  862. DataType nb = p_op->arguments[1]->get_datatype();
  863. if (na > nb) {
  864. //make things easier;
  865. SWAP(na, nb);
  866. }
  867. if (na == nb) {
  868. valid = (na > TYPE_BOOL && na <= TYPE_MAT4);
  869. ret_type = na;
  870. } else if (na == TYPE_INT && nb == TYPE_IVEC2) {
  871. valid = true;
  872. ret_type = TYPE_IVEC2;
  873. } else if (na == TYPE_INT && nb == TYPE_IVEC3) {
  874. valid = true;
  875. ret_type = TYPE_IVEC3;
  876. } else if (na == TYPE_INT && nb == TYPE_IVEC4) {
  877. valid = true;
  878. ret_type = TYPE_IVEC4;
  879. } else if (na == TYPE_UINT && nb == TYPE_UVEC2) {
  880. valid = true;
  881. ret_type = TYPE_UVEC2;
  882. } else if (na == TYPE_UINT && nb == TYPE_UVEC3) {
  883. valid = true;
  884. ret_type = TYPE_UVEC3;
  885. } else if (na == TYPE_UINT && nb == TYPE_UVEC4) {
  886. valid = true;
  887. ret_type = TYPE_UVEC4;
  888. } else if (na == TYPE_FLOAT && nb == TYPE_VEC2) {
  889. valid = true;
  890. ret_type = TYPE_VEC2;
  891. } else if (na == TYPE_FLOAT && nb == TYPE_VEC3) {
  892. valid = true;
  893. ret_type = TYPE_VEC3;
  894. } else if (na == TYPE_FLOAT && nb == TYPE_VEC4) {
  895. valid = true;
  896. ret_type = TYPE_VEC4;
  897. } else if (p_op->op == OP_MUL && na == TYPE_FLOAT && nb == TYPE_MAT2) {
  898. valid = true;
  899. ret_type = TYPE_MAT2;
  900. } else if (p_op->op == OP_MUL && na == TYPE_FLOAT && nb == TYPE_MAT3) {
  901. valid = true;
  902. ret_type = TYPE_MAT3;
  903. } else if (p_op->op == OP_MUL && na == TYPE_FLOAT && nb == TYPE_MAT4) {
  904. valid = true;
  905. ret_type = TYPE_MAT4;
  906. } else if (p_op->op == OP_MUL && na == TYPE_VEC2 && nb == TYPE_MAT2) {
  907. valid = true;
  908. ret_type = TYPE_VEC2;
  909. } else if (p_op->op == OP_MUL && na == TYPE_VEC3 && nb == TYPE_MAT3) {
  910. valid = true;
  911. ret_type = TYPE_VEC3;
  912. } else if (p_op->op == OP_MUL && na == TYPE_VEC4 && nb == TYPE_MAT4) {
  913. valid = true;
  914. ret_type = TYPE_VEC4;
  915. }
  916. } break;
  917. case OP_ASSIGN_MOD:
  918. case OP_MOD: {
  919. /*
  920. * The operator modulus (%) operates on signed or unsigned integers or integer vectors. The operand
  921. * types must both be signed or both be unsigned. The operands cannot be vectors of differing size. If
  922. * one operand is a scalar and the other vector, then the scalar is applied component-wise to the vector,
  923. * resulting in the same type as the vector. If both are vectors of the same size, the result is computed
  924. * component-wise.
  925. */
  926. DataType na = p_op->arguments[0]->get_datatype();
  927. DataType nb = p_op->arguments[1]->get_datatype();
  928. if (na == TYPE_INT && nb == TYPE_INT) {
  929. valid = true;
  930. ret_type = TYPE_INT;
  931. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  932. valid = true;
  933. ret_type = TYPE_IVEC2;
  934. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  935. valid = true;
  936. ret_type = TYPE_IVEC3;
  937. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  938. valid = true;
  939. ret_type = TYPE_IVEC4;
  940. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  941. valid = true;
  942. ret_type = TYPE_IVEC2;
  943. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  944. valid = true;
  945. ret_type = TYPE_IVEC3;
  946. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  947. valid = true;
  948. ret_type = TYPE_IVEC4;
  949. /////
  950. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  951. valid = true;
  952. ret_type = TYPE_UINT;
  953. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  954. valid = true;
  955. ret_type = TYPE_UVEC2;
  956. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  957. valid = true;
  958. ret_type = TYPE_UVEC3;
  959. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  960. valid = true;
  961. ret_type = TYPE_UVEC4;
  962. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  963. valid = true;
  964. ret_type = TYPE_UVEC2;
  965. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  966. valid = true;
  967. ret_type = TYPE_UVEC3;
  968. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  969. valid = true;
  970. ret_type = TYPE_UVEC4;
  971. }
  972. } break;
  973. case OP_ASSIGN_SHIFT_LEFT:
  974. case OP_ASSIGN_SHIFT_RIGHT:
  975. case OP_SHIFT_LEFT:
  976. case OP_SHIFT_RIGHT: {
  977. DataType na = p_op->arguments[0]->get_datatype();
  978. DataType nb = p_op->arguments[1]->get_datatype();
  979. if (na == TYPE_INT && nb == TYPE_INT) {
  980. valid = true;
  981. ret_type = TYPE_INT;
  982. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  983. valid = true;
  984. ret_type = TYPE_IVEC2;
  985. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  986. valid = true;
  987. ret_type = TYPE_IVEC3;
  988. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  989. valid = true;
  990. ret_type = TYPE_IVEC4;
  991. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  992. valid = true;
  993. ret_type = TYPE_IVEC2;
  994. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  995. valid = true;
  996. ret_type = TYPE_IVEC3;
  997. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  998. valid = true;
  999. ret_type = TYPE_IVEC4;
  1000. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1001. valid = true;
  1002. ret_type = TYPE_UINT;
  1003. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1004. valid = true;
  1005. ret_type = TYPE_UVEC2;
  1006. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1007. valid = true;
  1008. ret_type = TYPE_UVEC3;
  1009. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1010. valid = true;
  1011. ret_type = TYPE_UVEC4;
  1012. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1013. valid = true;
  1014. ret_type = TYPE_UVEC2;
  1015. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1016. valid = true;
  1017. ret_type = TYPE_UVEC3;
  1018. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1019. valid = true;
  1020. ret_type = TYPE_UVEC4;
  1021. }
  1022. } break;
  1023. case OP_ASSIGN: {
  1024. DataType na = p_op->arguments[0]->get_datatype();
  1025. DataType nb = p_op->arguments[1]->get_datatype();
  1026. valid = na == nb;
  1027. ret_type = na;
  1028. } break;
  1029. case OP_ASSIGN_ADD:
  1030. case OP_ASSIGN_SUB:
  1031. case OP_ASSIGN_MUL:
  1032. case OP_ASSIGN_DIV: {
  1033. DataType na = p_op->arguments[0]->get_datatype();
  1034. DataType nb = p_op->arguments[1]->get_datatype();
  1035. if (na == nb) {
  1036. valid = (na > TYPE_BOOL && na < TYPE_MAT2) || (p_op->op == OP_ASSIGN_MUL && na >= TYPE_MAT2 && na <= TYPE_MAT4);
  1037. ret_type = na;
  1038. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1039. valid = true;
  1040. ret_type = TYPE_IVEC2;
  1041. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1042. valid = true;
  1043. ret_type = TYPE_IVEC3;
  1044. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1045. valid = true;
  1046. ret_type = TYPE_IVEC4;
  1047. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1048. valid = true;
  1049. ret_type = TYPE_UVEC2;
  1050. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1051. valid = true;
  1052. ret_type = TYPE_UVEC3;
  1053. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1054. valid = true;
  1055. ret_type = TYPE_UVEC4;
  1056. } else if (na == TYPE_VEC2 && nb == TYPE_FLOAT) {
  1057. valid = true;
  1058. ret_type = TYPE_VEC2;
  1059. } else if (na == TYPE_VEC3 && nb == TYPE_FLOAT) {
  1060. valid = true;
  1061. ret_type = TYPE_VEC3;
  1062. } else if (na == TYPE_VEC4 && nb == TYPE_FLOAT) {
  1063. valid = true;
  1064. ret_type = TYPE_VEC4;
  1065. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_MAT2 && nb == TYPE_VEC2) {
  1066. valid = true;
  1067. ret_type = TYPE_MAT2;
  1068. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_MAT3 && nb == TYPE_VEC3) {
  1069. valid = true;
  1070. ret_type = TYPE_MAT3;
  1071. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_MAT4 && nb == TYPE_VEC4) {
  1072. valid = true;
  1073. ret_type = TYPE_MAT4;
  1074. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC2 && nb == TYPE_MAT2) {
  1075. valid = true;
  1076. ret_type = TYPE_VEC2;
  1077. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC3 && nb == TYPE_MAT3) {
  1078. valid = true;
  1079. ret_type = TYPE_VEC3;
  1080. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC4 && nb == TYPE_MAT4) {
  1081. valid = true;
  1082. ret_type = TYPE_VEC4;
  1083. }
  1084. } break;
  1085. case OP_ASSIGN_BIT_AND:
  1086. case OP_ASSIGN_BIT_OR:
  1087. case OP_ASSIGN_BIT_XOR:
  1088. case OP_BIT_AND:
  1089. case OP_BIT_OR:
  1090. case OP_BIT_XOR: {
  1091. /*
  1092. * The bitwise operators and (&), exclusive-or (^), and inclusive-or (|). The operands must be of type
  1093. * signed or unsigned integers or integer vectors. The operands cannot be vectors of differing size. If
  1094. * one operand is a scalar and the other a vector, the scalar is applied component-wise to the vector,
  1095. * resulting in the same type as the vector. The fundamental types of the operands (signed or unsigned)
  1096. * must match.
  1097. */
  1098. DataType na = p_op->arguments[0]->get_datatype();
  1099. DataType nb = p_op->arguments[1]->get_datatype();
  1100. if (na > nb && p_op->op >= OP_BIT_AND) {
  1101. //can swap for non assign
  1102. SWAP(na, nb);
  1103. }
  1104. if (na == TYPE_INT && nb == TYPE_INT) {
  1105. valid = true;
  1106. ret_type = TYPE_INT;
  1107. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1108. valid = true;
  1109. ret_type = TYPE_IVEC2;
  1110. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1111. valid = true;
  1112. ret_type = TYPE_IVEC3;
  1113. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1114. valid = true;
  1115. ret_type = TYPE_IVEC4;
  1116. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  1117. valid = true;
  1118. ret_type = TYPE_IVEC2;
  1119. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  1120. valid = true;
  1121. ret_type = TYPE_IVEC3;
  1122. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  1123. valid = true;
  1124. ret_type = TYPE_IVEC4;
  1125. /////
  1126. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1127. valid = true;
  1128. ret_type = TYPE_UINT;
  1129. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1130. valid = true;
  1131. ret_type = TYPE_UVEC2;
  1132. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1133. valid = true;
  1134. ret_type = TYPE_UVEC3;
  1135. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1136. valid = true;
  1137. ret_type = TYPE_UVEC4;
  1138. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1139. valid = true;
  1140. ret_type = TYPE_UVEC2;
  1141. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1142. valid = true;
  1143. ret_type = TYPE_UVEC3;
  1144. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1145. valid = true;
  1146. ret_type = TYPE_UVEC4;
  1147. }
  1148. } break;
  1149. case OP_BIT_INVERT: { //unaries
  1150. DataType na = p_op->arguments[0]->get_datatype();
  1151. valid = na >= TYPE_INT && na < TYPE_FLOAT;
  1152. ret_type = na;
  1153. } break;
  1154. case OP_SELECT_IF: {
  1155. DataType na = p_op->arguments[0]->get_datatype();
  1156. DataType nb = p_op->arguments[1]->get_datatype();
  1157. DataType nc = p_op->arguments[2]->get_datatype();
  1158. valid = na == TYPE_BOOL && (nb == nc);
  1159. ret_type = nb;
  1160. } break;
  1161. default: {
  1162. ERR_FAIL_V(false);
  1163. }
  1164. }
  1165. if (r_ret_type)
  1166. *r_ret_type = ret_type;
  1167. return valid;
  1168. }
  1169. const ShaderLanguage::BuiltinFuncDef ShaderLanguage::builtin_func_defs[] = {
  1170. //constructors
  1171. { "bool", TYPE_BOOL, { TYPE_BOOL, TYPE_VOID } },
  1172. { "bvec2", TYPE_BVEC2, { TYPE_BOOL, TYPE_VOID } },
  1173. { "bvec2", TYPE_BVEC2, { TYPE_BOOL, TYPE_BOOL, TYPE_VOID } },
  1174. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_VOID } },
  1175. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_VOID } },
  1176. { "bvec3", TYPE_BVEC3, { TYPE_BVEC2, TYPE_BOOL, TYPE_VOID } },
  1177. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_BVEC2, TYPE_VOID } },
  1178. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_VOID } },
  1179. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_VOID } },
  1180. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BVEC2, TYPE_BOOL, TYPE_VOID } },
  1181. { "bvec4", TYPE_BVEC4, { TYPE_BVEC2, TYPE_BOOL, TYPE_BOOL, TYPE_VOID } },
  1182. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BOOL, TYPE_BVEC2, TYPE_VOID } },
  1183. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BVEC3, TYPE_VOID } },
  1184. { "bvec4", TYPE_BVEC4, { TYPE_BVEC3, TYPE_BOOL, TYPE_VOID } },
  1185. { "bvec4", TYPE_BVEC4, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID } },
  1186. { "float", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1187. { "vec2", TYPE_VEC2, { TYPE_FLOAT, TYPE_VOID } },
  1188. { "vec2", TYPE_VEC2, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1189. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_VOID } },
  1190. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1191. { "vec3", TYPE_VEC3, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1192. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_VEC2, TYPE_VOID } },
  1193. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VOID } },
  1194. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1195. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1196. { "vec4", TYPE_VEC4, { TYPE_VEC2, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1197. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC2, TYPE_VOID } },
  1198. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC3, TYPE_VOID } },
  1199. { "vec4", TYPE_VEC4, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1200. { "vec4", TYPE_VEC4, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1201. { "int", TYPE_INT, { TYPE_INT, TYPE_VOID } },
  1202. { "ivec2", TYPE_IVEC2, { TYPE_INT, TYPE_VOID } },
  1203. { "ivec2", TYPE_IVEC2, { TYPE_INT, TYPE_INT, TYPE_VOID } },
  1204. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_VOID } },
  1205. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID } },
  1206. { "ivec3", TYPE_IVEC3, { TYPE_IVEC2, TYPE_INT, TYPE_VOID } },
  1207. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_IVEC2, TYPE_VOID } },
  1208. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_VOID } },
  1209. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID } },
  1210. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_IVEC2, TYPE_INT, TYPE_VOID } },
  1211. { "ivec4", TYPE_IVEC4, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID } },
  1212. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_INT, TYPE_IVEC2, TYPE_VOID } },
  1213. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_IVEC3, TYPE_VOID } },
  1214. { "ivec4", TYPE_IVEC4, { TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1215. { "ivec4", TYPE_IVEC4, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1216. { "uint", TYPE_UINT, { TYPE_UINT, TYPE_VOID } },
  1217. { "uvec2", TYPE_UVEC2, { TYPE_UINT, TYPE_VOID } },
  1218. { "uvec2", TYPE_UVEC2, { TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1219. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_VOID } },
  1220. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1221. { "uvec3", TYPE_UVEC3, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID } },
  1222. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_UVEC2, TYPE_VOID } },
  1223. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_VOID } },
  1224. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1225. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UVEC2, TYPE_UINT, TYPE_VOID } },
  1226. { "uvec4", TYPE_UVEC4, { TYPE_UVEC2, TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1227. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UINT, TYPE_UVEC2, TYPE_VOID } },
  1228. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UVEC3, TYPE_VOID } },
  1229. { "uvec4", TYPE_UVEC4, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID } },
  1230. { "uvec4", TYPE_UVEC4, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1231. { "mat2", TYPE_MAT2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1232. { "mat3", TYPE_MAT3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1233. { "mat4", TYPE_MAT4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1234. { "mat2", TYPE_MAT2, { TYPE_FLOAT, TYPE_VOID } },
  1235. { "mat3", TYPE_MAT3, { TYPE_FLOAT, TYPE_VOID } },
  1236. { "mat4", TYPE_MAT4, { TYPE_FLOAT, TYPE_VOID } },
  1237. //conversion scalars
  1238. { "int", TYPE_INT, { TYPE_BOOL, TYPE_VOID } },
  1239. { "int", TYPE_INT, { TYPE_INT, TYPE_VOID } },
  1240. { "int", TYPE_INT, { TYPE_UINT, TYPE_VOID } },
  1241. { "int", TYPE_INT, { TYPE_FLOAT, TYPE_VOID } },
  1242. { "float", TYPE_FLOAT, { TYPE_BOOL, TYPE_VOID } },
  1243. { "float", TYPE_FLOAT, { TYPE_INT, TYPE_VOID } },
  1244. { "float", TYPE_FLOAT, { TYPE_UINT, TYPE_VOID } },
  1245. { "float", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1246. { "uint", TYPE_UINT, { TYPE_BOOL, TYPE_VOID } },
  1247. { "uint", TYPE_UINT, { TYPE_INT, TYPE_VOID } },
  1248. { "uint", TYPE_UINT, { TYPE_UINT, TYPE_VOID } },
  1249. { "uint", TYPE_UINT, { TYPE_FLOAT, TYPE_VOID } },
  1250. { "bool", TYPE_BOOL, { TYPE_BOOL, TYPE_VOID } },
  1251. { "bool", TYPE_BOOL, { TYPE_INT, TYPE_VOID } },
  1252. { "bool", TYPE_BOOL, { TYPE_UINT, TYPE_VOID } },
  1253. { "bool", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID } },
  1254. //conversion vectors
  1255. { "ivec2", TYPE_IVEC2, { TYPE_BVEC2, TYPE_VOID } },
  1256. { "ivec2", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID } },
  1257. { "ivec2", TYPE_IVEC2, { TYPE_UVEC2, TYPE_VOID } },
  1258. { "ivec2", TYPE_IVEC2, { TYPE_VEC2, TYPE_VOID } },
  1259. { "vec2", TYPE_VEC2, { TYPE_BVEC2, TYPE_VOID } },
  1260. { "vec2", TYPE_VEC2, { TYPE_IVEC2, TYPE_VOID } },
  1261. { "vec2", TYPE_VEC2, { TYPE_UVEC2, TYPE_VOID } },
  1262. { "vec2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1263. { "uvec2", TYPE_UVEC2, { TYPE_BVEC2, TYPE_VOID } },
  1264. { "uvec2", TYPE_UVEC2, { TYPE_IVEC2, TYPE_VOID } },
  1265. { "uvec2", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID } },
  1266. { "uvec2", TYPE_UVEC2, { TYPE_VEC2, TYPE_VOID } },
  1267. { "bvec2", TYPE_BVEC2, { TYPE_BVEC2, TYPE_VOID } },
  1268. { "bvec2", TYPE_BVEC2, { TYPE_IVEC2, TYPE_VOID } },
  1269. { "bvec2", TYPE_BVEC2, { TYPE_UVEC2, TYPE_VOID } },
  1270. { "bvec2", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID } },
  1271. { "ivec3", TYPE_IVEC3, { TYPE_BVEC3, TYPE_VOID } },
  1272. { "ivec3", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID } },
  1273. { "ivec3", TYPE_IVEC3, { TYPE_UVEC3, TYPE_VOID } },
  1274. { "ivec3", TYPE_IVEC3, { TYPE_VEC3, TYPE_VOID } },
  1275. { "vec3", TYPE_VEC3, { TYPE_BVEC3, TYPE_VOID } },
  1276. { "vec3", TYPE_VEC3, { TYPE_IVEC3, TYPE_VOID } },
  1277. { "vec3", TYPE_VEC3, { TYPE_UVEC3, TYPE_VOID } },
  1278. { "vec3", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1279. { "uvec3", TYPE_UVEC3, { TYPE_BVEC3, TYPE_VOID } },
  1280. { "uvec3", TYPE_UVEC3, { TYPE_IVEC3, TYPE_VOID } },
  1281. { "uvec3", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID } },
  1282. { "uvec3", TYPE_UVEC3, { TYPE_VEC3, TYPE_VOID } },
  1283. { "bvec3", TYPE_BVEC3, { TYPE_BVEC3, TYPE_VOID } },
  1284. { "bvec3", TYPE_BVEC3, { TYPE_IVEC3, TYPE_VOID } },
  1285. { "bvec3", TYPE_BVEC3, { TYPE_UVEC3, TYPE_VOID } },
  1286. { "bvec3", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID } },
  1287. { "ivec4", TYPE_IVEC4, { TYPE_BVEC4, TYPE_VOID } },
  1288. { "ivec4", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID } },
  1289. { "ivec4", TYPE_IVEC4, { TYPE_UVEC4, TYPE_VOID } },
  1290. { "ivec4", TYPE_IVEC4, { TYPE_VEC4, TYPE_VOID } },
  1291. { "vec4", TYPE_VEC4, { TYPE_BVEC4, TYPE_VOID } },
  1292. { "vec4", TYPE_VEC4, { TYPE_IVEC4, TYPE_VOID } },
  1293. { "vec4", TYPE_VEC4, { TYPE_UVEC4, TYPE_VOID } },
  1294. { "vec4", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1295. { "uvec4", TYPE_UVEC4, { TYPE_BVEC4, TYPE_VOID } },
  1296. { "uvec4", TYPE_UVEC4, { TYPE_IVEC4, TYPE_VOID } },
  1297. { "uvec4", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID } },
  1298. { "uvec4", TYPE_UVEC4, { TYPE_VEC4, TYPE_VOID } },
  1299. { "bvec4", TYPE_BVEC4, { TYPE_BVEC4, TYPE_VOID } },
  1300. { "bvec4", TYPE_BVEC4, { TYPE_IVEC4, TYPE_VOID } },
  1301. { "bvec4", TYPE_BVEC4, { TYPE_UVEC4, TYPE_VOID } },
  1302. { "bvec4", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID } },
  1303. //conversion between matrixes
  1304. { "mat2", TYPE_MAT2, { TYPE_MAT3, TYPE_VOID } },
  1305. { "mat2", TYPE_MAT2, { TYPE_MAT4, TYPE_VOID } },
  1306. { "mat3", TYPE_MAT3, { TYPE_MAT2, TYPE_VOID } },
  1307. { "mat3", TYPE_MAT3, { TYPE_MAT4, TYPE_VOID } },
  1308. { "mat4", TYPE_MAT4, { TYPE_MAT2, TYPE_VOID } },
  1309. { "mat4", TYPE_MAT4, { TYPE_MAT3, TYPE_VOID } },
  1310. //builtins - trigonometry
  1311. { "radians", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1312. { "radians", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1313. { "radians", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1314. { "radians", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1315. { "degrees", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1316. { "degrees", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1317. { "degrees", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1318. { "degrees", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1319. { "sin", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1320. { "sin", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1321. { "sin", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1322. { "sin", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1323. { "cos", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1324. { "cos", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1325. { "cos", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1326. { "cos", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1327. { "tan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1328. { "tan", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1329. { "tan", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1330. { "tan", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1331. { "asin", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1332. { "asin", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1333. { "asin", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1334. { "asin", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1335. { "acos", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1336. { "acos", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1337. { "acos", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1338. { "acos", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1339. { "atan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1340. { "atan", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1341. { "atan", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1342. { "atan", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1343. { "atan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1344. { "atan", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1345. { "atan", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1346. { "atan", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1347. { "sinh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1348. { "sinh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1349. { "sinh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1350. { "sinh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1351. { "cosh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1352. { "cosh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1353. { "cosh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1354. { "cosh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1355. { "tanh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1356. { "tanh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1357. { "tanh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1358. { "tanh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1359. { "asinh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1360. { "asinh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1361. { "asinh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1362. { "asinh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1363. { "acosh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1364. { "acosh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1365. { "acosh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1366. { "acosh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1367. { "atanh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1368. { "atanh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1369. { "atanh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1370. { "atanh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1371. //builtins - exponential
  1372. { "pow", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1373. { "pow", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1374. { "pow", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1375. { "pow", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1376. { "exp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1377. { "exp", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1378. { "exp", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1379. { "exp", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1380. { "log", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1381. { "log", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1382. { "log", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1383. { "log", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1384. { "exp2", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1385. { "exp2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1386. { "exp2", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1387. { "exp2", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1388. { "log2", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1389. { "log2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1390. { "log2", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1391. { "log2", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1392. { "sqrt", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1393. { "sqrt", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1394. { "sqrt", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1395. { "sqrt", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1396. { "inversesqrt", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1397. { "inversesqrt", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1398. { "inversesqrt", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1399. { "inversesqrt", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1400. //builtins - common
  1401. { "abs", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1402. { "abs", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1403. { "abs", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1404. { "abs", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1405. { "abs", TYPE_INT, { TYPE_INT, TYPE_VOID } },
  1406. { "abs", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID } },
  1407. { "abs", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID } },
  1408. { "abs", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID } },
  1409. { "sign", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1410. { "sign", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1411. { "sign", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1412. { "sign", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1413. { "sign", TYPE_INT, { TYPE_INT, TYPE_VOID } },
  1414. { "sign", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID } },
  1415. { "sign", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID } },
  1416. { "sign", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID } },
  1417. { "floor", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1418. { "floor", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1419. { "floor", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1420. { "floor", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1421. { "trunc", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1422. { "trunc", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1423. { "trunc", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1424. { "trunc", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1425. { "round", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1426. { "round", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1427. { "round", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1428. { "round", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1429. { "roundEven", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1430. { "roundEven", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1431. { "roundEven", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1432. { "roundEven", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1433. { "ceil", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1434. { "ceil", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1435. { "ceil", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1436. { "ceil", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1437. { "fract", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1438. { "fract", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1439. { "fract", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1440. { "fract", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1441. { "mod", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1442. { "mod", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1443. { "mod", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1444. { "mod", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1445. { "mod", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1446. { "mod", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1447. { "mod", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1448. { "modf", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1449. { "modf", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1450. { "modf", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1451. { "modf", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1452. { "min", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1453. { "min", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1454. { "min", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1455. { "min", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1456. { "min", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1457. { "min", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1458. { "min", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1459. { "min", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_VOID } },
  1460. { "min", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1461. { "min", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_VOID } },
  1462. { "min", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1463. { "min", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1464. { "min", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1465. { "min", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_VOID } },
  1466. { "min", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1467. { "min", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1468. { "min", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID } },
  1469. { "min", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1470. { "min", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID } },
  1471. { "min", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1472. { "min", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_VOID } },
  1473. { "max", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1474. { "max", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1475. { "max", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1476. { "max", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1477. { "max", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1478. { "max", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1479. { "max", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1480. { "max", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_VOID } },
  1481. { "max", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1482. { "max", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_VOID } },
  1483. { "max", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1484. { "max", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1485. { "max", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1486. { "max", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_VOID } },
  1487. { "max", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1488. { "max", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1489. { "max", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID } },
  1490. { "max", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1491. { "max", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID } },
  1492. { "max", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1493. { "max", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_VOID } },
  1494. { "clamp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1495. { "clamp", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1496. { "clamp", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1497. { "clamp", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1498. { "clamp", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1499. { "clamp", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1500. { "clamp", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1501. { "clamp", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID } },
  1502. { "clamp", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1503. { "clamp", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1504. { "clamp", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1505. { "clamp", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID } },
  1506. { "clamp", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_INT, TYPE_VOID } },
  1507. { "clamp", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_INT, TYPE_VOID } },
  1508. { "clamp", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1509. { "clamp", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1510. { "clamp", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1511. { "clamp", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1512. { "clamp", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1513. { "clamp", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1514. { "clamp", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1515. { "mix", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1516. { "mix", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_BOOL, TYPE_VOID } },
  1517. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1518. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_BOOL, TYPE_VOID } },
  1519. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_BVEC2, TYPE_VOID } },
  1520. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1521. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1522. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_BOOL, TYPE_VOID } },
  1523. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_BVEC3, TYPE_VOID } },
  1524. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1525. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1526. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_BOOL, TYPE_VOID } },
  1527. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_BVEC4, TYPE_VOID } },
  1528. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1529. { "step", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1530. { "step", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1531. { "step", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1532. { "step", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1533. { "step", TYPE_VEC2, { TYPE_FLOAT, TYPE_VEC2, TYPE_VOID } },
  1534. { "step", TYPE_VEC3, { TYPE_FLOAT, TYPE_VEC3, TYPE_VOID } },
  1535. { "step", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC4, TYPE_VOID } },
  1536. { "smoothstep", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1537. { "smoothstep", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1538. { "smoothstep", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1539. { "smoothstep", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1540. { "smoothstep", TYPE_VEC2, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC2, TYPE_VOID } },
  1541. { "smoothstep", TYPE_VEC3, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC3, TYPE_VOID } },
  1542. { "smoothstep", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC4, TYPE_VOID } },
  1543. { "isnan", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID } },
  1544. { "isnan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID } },
  1545. { "isnan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID } },
  1546. { "isnan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID } },
  1547. { "isinf", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID } },
  1548. { "isinf", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID } },
  1549. { "isinf", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID } },
  1550. { "isinf", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID } },
  1551. { "floatBitsToInt", TYPE_INT, { TYPE_FLOAT, TYPE_VOID } },
  1552. { "floatBitsToInt", TYPE_IVEC2, { TYPE_VEC2, TYPE_VOID } },
  1553. { "floatBitsToInt", TYPE_IVEC3, { TYPE_VEC3, TYPE_VOID } },
  1554. { "floatBitsToInt", TYPE_IVEC4, { TYPE_VEC4, TYPE_VOID } },
  1555. { "floatBitsToUint", TYPE_UINT, { TYPE_FLOAT, TYPE_VOID } },
  1556. { "floatBitsToUint", TYPE_UVEC2, { TYPE_VEC2, TYPE_VOID } },
  1557. { "floatBitsToUint", TYPE_UVEC3, { TYPE_VEC3, TYPE_VOID } },
  1558. { "floatBitsToUint", TYPE_UVEC4, { TYPE_VEC4, TYPE_VOID } },
  1559. { "intBitsToFloat", TYPE_FLOAT, { TYPE_INT, TYPE_VOID } },
  1560. { "intBitsToFloat", TYPE_VEC2, { TYPE_IVEC2, TYPE_VOID } },
  1561. { "intBitsToFloat", TYPE_VEC3, { TYPE_IVEC3, TYPE_VOID } },
  1562. { "intBitsToFloat", TYPE_VEC4, { TYPE_IVEC4, TYPE_VOID } },
  1563. { "uintBitsToFloat", TYPE_FLOAT, { TYPE_UINT, TYPE_VOID } },
  1564. { "uintBitsToFloat", TYPE_VEC2, { TYPE_UVEC2, TYPE_VOID } },
  1565. { "uintBitsToFloat", TYPE_VEC3, { TYPE_UVEC3, TYPE_VOID } },
  1566. { "uintBitsToFloat", TYPE_VEC4, { TYPE_UVEC4, TYPE_VOID } },
  1567. //builtins - geometric
  1568. { "length", TYPE_FLOAT, { TYPE_VEC2, TYPE_VOID } },
  1569. { "length", TYPE_FLOAT, { TYPE_VEC3, TYPE_VOID } },
  1570. { "length", TYPE_FLOAT, { TYPE_VEC4, TYPE_VOID } },
  1571. { "distance", TYPE_FLOAT, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1572. { "distance", TYPE_FLOAT, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1573. { "distance", TYPE_FLOAT, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1574. { "dot", TYPE_FLOAT, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1575. { "dot", TYPE_FLOAT, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1576. { "dot", TYPE_FLOAT, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1577. { "cross", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1578. { "normalize", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1579. { "normalize", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1580. { "normalize", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1581. { "reflect", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1582. { "refract", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1583. { "faceforward", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1584. { "faceforward", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1585. { "faceforward", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1586. { "matrixCompMult", TYPE_MAT2, { TYPE_MAT2, TYPE_MAT2, TYPE_VOID } },
  1587. { "matrixCompMult", TYPE_MAT3, { TYPE_MAT3, TYPE_MAT3, TYPE_VOID } },
  1588. { "matrixCompMult", TYPE_MAT4, { TYPE_MAT4, TYPE_MAT4, TYPE_VOID } },
  1589. { "outerProduct", TYPE_MAT2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1590. { "outerProduct", TYPE_MAT3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1591. { "outerProduct", TYPE_MAT4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1592. { "transpose", TYPE_MAT2, { TYPE_MAT2, TYPE_VOID } },
  1593. { "transpose", TYPE_MAT3, { TYPE_MAT3, TYPE_VOID } },
  1594. { "transpose", TYPE_MAT4, { TYPE_MAT4, TYPE_VOID } },
  1595. { "determinant", TYPE_FLOAT, { TYPE_MAT2, TYPE_VOID } },
  1596. { "determinant", TYPE_FLOAT, { TYPE_MAT3, TYPE_VOID } },
  1597. { "determinant", TYPE_FLOAT, { TYPE_MAT4, TYPE_VOID } },
  1598. { "inverse", TYPE_MAT2, { TYPE_MAT2, TYPE_VOID } },
  1599. { "inverse", TYPE_MAT3, { TYPE_MAT3, TYPE_VOID } },
  1600. { "inverse", TYPE_MAT4, { TYPE_MAT4, TYPE_VOID } },
  1601. { "lessThan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1602. { "lessThan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1603. { "lessThan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1604. { "lessThan", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1605. { "lessThan", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1606. { "lessThan", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1607. { "lessThan", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1608. { "lessThan", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1609. { "lessThan", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1610. { "greaterThan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1611. { "greaterThan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1612. { "greaterThan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1613. { "greaterThan", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1614. { "greaterThan", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1615. { "greaterThan", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1616. { "greaterThan", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1617. { "greaterThan", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1618. { "greaterThan", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1619. { "lessThanEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1620. { "lessThanEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1621. { "lessThanEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1622. { "lessThanEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1623. { "lessThanEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1624. { "lessThanEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1625. { "lessThanEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1626. { "lessThanEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1627. { "lessThanEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1628. { "greaterThanEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1629. { "greaterThanEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1630. { "greaterThanEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1631. { "greaterThanEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1632. { "greaterThanEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1633. { "greaterThanEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1634. { "greaterThanEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1635. { "greaterThanEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1636. { "greaterThanEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1637. { "equal", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1638. { "equal", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1639. { "equal", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1640. { "equal", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1641. { "equal", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1642. { "equal", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1643. { "equal", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1644. { "equal", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1645. { "equal", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1646. { "equal", TYPE_BVEC2, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID } },
  1647. { "equal", TYPE_BVEC3, { TYPE_BVEC3, TYPE_BVEC3, TYPE_VOID } },
  1648. { "equal", TYPE_BVEC4, { TYPE_BVEC4, TYPE_BVEC4, TYPE_VOID } },
  1649. { "notEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1650. { "notEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1651. { "notEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1652. { "notEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1653. { "notEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1654. { "notEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1655. { "notEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1656. { "notEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1657. { "notEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1658. { "notEqual", TYPE_BVEC2, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID } },
  1659. { "notEqual", TYPE_BVEC3, { TYPE_BVEC3, TYPE_BVEC3, TYPE_VOID } },
  1660. { "notEqual", TYPE_BVEC4, { TYPE_BVEC4, TYPE_BVEC4, TYPE_VOID } },
  1661. { "any", TYPE_BOOL, { TYPE_BVEC2, TYPE_VOID } },
  1662. { "any", TYPE_BOOL, { TYPE_BVEC3, TYPE_VOID } },
  1663. { "any", TYPE_BOOL, { TYPE_BVEC4, TYPE_VOID } },
  1664. { "all", TYPE_BOOL, { TYPE_BVEC2, TYPE_VOID } },
  1665. { "all", TYPE_BOOL, { TYPE_BVEC3, TYPE_VOID } },
  1666. { "all", TYPE_BOOL, { TYPE_BVEC4, TYPE_VOID } },
  1667. { "not", TYPE_BOOL, { TYPE_BVEC2, TYPE_VOID } },
  1668. { "not", TYPE_BOOL, { TYPE_BVEC3, TYPE_VOID } },
  1669. { "not", TYPE_BOOL, { TYPE_BVEC4, TYPE_VOID } },
  1670. //builtins - texture
  1671. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLER2D, TYPE_INT, TYPE_VOID } },
  1672. { "textureSize", TYPE_IVEC2, { TYPE_ISAMPLER2D, TYPE_INT, TYPE_VOID } },
  1673. { "textureSize", TYPE_IVEC2, { TYPE_USAMPLER2D, TYPE_INT, TYPE_VOID } },
  1674. { "textureSize", TYPE_IVEC3, { TYPE_SAMPLER2DARRAY, TYPE_INT, TYPE_VOID } },
  1675. { "textureSize", TYPE_IVEC3, { TYPE_ISAMPLER2DARRAY, TYPE_INT, TYPE_VOID } },
  1676. { "textureSize", TYPE_IVEC3, { TYPE_USAMPLER2DARRAY, TYPE_INT, TYPE_VOID } },
  1677. { "textureSize", TYPE_IVEC3, { TYPE_SAMPLER3D, TYPE_INT, TYPE_VOID } },
  1678. { "textureSize", TYPE_IVEC3, { TYPE_ISAMPLER3D, TYPE_INT, TYPE_VOID } },
  1679. { "textureSize", TYPE_IVEC3, { TYPE_USAMPLER3D, TYPE_INT, TYPE_VOID } },
  1680. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLERCUBE, TYPE_INT, TYPE_VOID } },
  1681. { "texture", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VOID } },
  1682. { "texture", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1683. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VOID } },
  1684. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1685. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VOID } },
  1686. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1687. { "texture", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID } },
  1688. { "texture", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1689. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID } },
  1690. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1691. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID } },
  1692. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1693. { "texture", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_VOID } },
  1694. { "texture", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1695. { "texture", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_VOID } },
  1696. { "texture", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1697. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_VOID } },
  1698. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1699. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VOID } },
  1700. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1701. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_VOID } },
  1702. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_VOID } },
  1703. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1704. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1705. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_VOID } },
  1706. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_VOID } },
  1707. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1708. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1709. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_VOID } },
  1710. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_VOID } },
  1711. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1712. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1713. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_VOID } },
  1714. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1715. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_VOID } },
  1716. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1717. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_VOID } },
  1718. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1719. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1720. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1721. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1722. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1723. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1724. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1725. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1726. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1727. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1728. { "textureLod", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1729. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID } },
  1730. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID } },
  1731. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID } },
  1732. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1733. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1734. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1735. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1736. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1737. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1738. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1739. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1740. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1741. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1742. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1743. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1744. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1745. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1746. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1747. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1748. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1749. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1750. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1751. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1752. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1753. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1754. { "dFdx", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1755. { "dFdx", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1756. { "dFdx", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1757. { "dFdx", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1758. { "dFdy", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1759. { "dFdy", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1760. { "dFdy", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1761. { "dFdy", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1762. { "fwidth", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1763. { "fwidth", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1764. { "fwidth", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1765. { "fwidth", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1766. { NULL, TYPE_VOID, { TYPE_VOID } }
  1767. };
  1768. const ShaderLanguage::BuiltinFuncOutArgs ShaderLanguage::builtin_func_out_args[] = {
  1769. //constructors
  1770. { "modf", 1 },
  1771. { NULL, 0 }
  1772. };
  1773. bool ShaderLanguage::_validate_function_call(BlockNode *p_block, OperatorNode *p_func, DataType *r_ret_type) {
  1774. ERR_FAIL_COND_V(p_func->op != OP_CALL && p_func->op != OP_CONSTRUCT, NULL);
  1775. Vector<DataType> args;
  1776. ERR_FAIL_COND_V(p_func->arguments[0]->type != Node::TYPE_VARIABLE, NULL);
  1777. StringName name = static_cast<VariableNode *>(p_func->arguments[0])->name.operator String();
  1778. for (int i = 1; i < p_func->arguments.size(); i++) {
  1779. args.push_back(p_func->arguments[i]->get_datatype());
  1780. }
  1781. int argcount = args.size();
  1782. bool failed_builtin = false;
  1783. if (argcount <= 4) {
  1784. // test builtins
  1785. int idx = 0;
  1786. while (builtin_func_defs[idx].name) {
  1787. if (name == builtin_func_defs[idx].name) {
  1788. failed_builtin = true;
  1789. bool fail = false;
  1790. for (int i = 0; i < argcount; i++) {
  1791. if (get_scalar_type(args[i]) == args[i] && p_func->arguments[i + 1]->type == Node::TYPE_CONSTANT && convert_constant(static_cast<ConstantNode *>(p_func->arguments[i + 1]), builtin_func_defs[idx].args[i])) {
  1792. //all good
  1793. } else if (args[i] != builtin_func_defs[idx].args[i]) {
  1794. fail = true;
  1795. break;
  1796. }
  1797. }
  1798. if (!fail && argcount < 4 && builtin_func_defs[idx].args[argcount] != TYPE_VOID)
  1799. fail = true; //make sure the number of arguments matches
  1800. if (!fail) {
  1801. //make sure its not an out argument used in the wrong way
  1802. int outarg_idx = 0;
  1803. while (builtin_func_out_args[outarg_idx].name) {
  1804. if (String(name) == builtin_func_out_args[outarg_idx].name) {
  1805. int arg_idx = builtin_func_out_args[outarg_idx].argument;
  1806. if (arg_idx < argcount) {
  1807. if (p_func->arguments[arg_idx + 1]->type != Node::TYPE_VARIABLE) {
  1808. _set_error("Argument " + itos(arg_idx + 1) + " of function '" + String(name) + "' is not a variable");
  1809. return false;
  1810. }
  1811. StringName var_name = static_cast<const VariableNode *>(p_func->arguments[arg_idx + 1])->name;
  1812. const BlockNode *b = p_block;
  1813. bool valid = false;
  1814. while (b) {
  1815. if (b->variables.has(var_name)) {
  1816. valid = true;
  1817. break;
  1818. }
  1819. b = b->parent_block;
  1820. }
  1821. if (!valid) {
  1822. _set_error("Argument " + itos(arg_idx + 1) + " of function '" + String(name) + "' can only take a local variable");
  1823. return false;
  1824. }
  1825. }
  1826. }
  1827. outarg_idx++;
  1828. }
  1829. if (r_ret_type)
  1830. *r_ret_type = builtin_func_defs[idx].rettype;
  1831. return true;
  1832. }
  1833. }
  1834. idx++;
  1835. }
  1836. }
  1837. if (failed_builtin) {
  1838. String err = "Invalid arguments for built-in function: " + String(name) + "(";
  1839. for (int i = 0; i < argcount; i++) {
  1840. if (i > 0)
  1841. err += ",";
  1842. if (p_func->arguments[i + 1]->type == Node::TYPE_CONSTANT && p_func->arguments[i + 1]->get_datatype() == TYPE_INT && static_cast<ConstantNode *>(p_func->arguments[i + 1])->values[0].sint < 0) {
  1843. err += "-";
  1844. }
  1845. err += get_datatype_name(args[i]);
  1846. }
  1847. err += ")";
  1848. _set_error(err);
  1849. return false;
  1850. }
  1851. // try existing functions..
  1852. StringName exclude_function;
  1853. BlockNode *block = p_block;
  1854. while (block) {
  1855. if (block->parent_function) {
  1856. exclude_function = block->parent_function->name;
  1857. }
  1858. block = block->parent_block;
  1859. }
  1860. if (name == exclude_function) {
  1861. _set_error("Recursion is not allowed");
  1862. return false;
  1863. }
  1864. for (int i = 0; i < shader->functions.size(); i++) {
  1865. if (name != shader->functions[i].name)
  1866. continue;
  1867. if (!shader->functions[i].callable) {
  1868. _set_error("Function '" + String(name) + " can't be called from source code.");
  1869. return false;
  1870. }
  1871. FunctionNode *pfunc = shader->functions[i].function;
  1872. if (pfunc->arguments.size() != args.size())
  1873. continue;
  1874. bool fail = false;
  1875. for (int i = 0; i < args.size(); i++) {
  1876. if (get_scalar_type(args[i]) == args[i] && p_func->arguments[i + 1]->type == Node::TYPE_CONSTANT && convert_constant(static_cast<ConstantNode *>(p_func->arguments[i + 1]), pfunc->arguments[i].type)) {
  1877. //all good
  1878. } else if (args[i] != pfunc->arguments[i].type) {
  1879. fail = true;
  1880. break;
  1881. }
  1882. }
  1883. if (!fail) {
  1884. if (r_ret_type)
  1885. *r_ret_type = pfunc->return_type;
  1886. return true;
  1887. }
  1888. }
  1889. return false;
  1890. }
  1891. bool ShaderLanguage::_parse_function_arguments(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, OperatorNode *p_func, int *r_complete_arg) {
  1892. TkPos pos = _get_tkpos();
  1893. Token tk = _get_token();
  1894. if (tk.type == TK_PARENTHESIS_CLOSE) {
  1895. return true;
  1896. }
  1897. _set_tkpos(pos);
  1898. while (true) {
  1899. if (r_complete_arg) {
  1900. pos = _get_tkpos();
  1901. tk = _get_token();
  1902. if (tk.type == TK_CURSOR) {
  1903. *r_complete_arg = p_func->arguments.size() - 1;
  1904. } else {
  1905. _set_tkpos(pos);
  1906. }
  1907. }
  1908. Node *arg = _parse_and_reduce_expression(p_block, p_builtin_types);
  1909. if (!arg) {
  1910. return false;
  1911. }
  1912. p_func->arguments.push_back(arg);
  1913. tk = _get_token();
  1914. if (tk.type == TK_PARENTHESIS_CLOSE) {
  1915. return true;
  1916. } else if (tk.type != TK_COMMA) {
  1917. // something is broken
  1918. _set_error("Expected ',' or ')' after argument");
  1919. return false;
  1920. }
  1921. }
  1922. return true;
  1923. }
  1924. bool ShaderLanguage::is_token_operator(TokenType p_type) {
  1925. return (p_type == TK_OP_EQUAL ||
  1926. p_type == TK_OP_NOT_EQUAL ||
  1927. p_type == TK_OP_LESS ||
  1928. p_type == TK_OP_LESS_EQUAL ||
  1929. p_type == TK_OP_GREATER ||
  1930. p_type == TK_OP_GREATER_EQUAL ||
  1931. p_type == TK_OP_AND ||
  1932. p_type == TK_OP_OR ||
  1933. p_type == TK_OP_NOT ||
  1934. p_type == TK_OP_ADD ||
  1935. p_type == TK_OP_SUB ||
  1936. p_type == TK_OP_MUL ||
  1937. p_type == TK_OP_DIV ||
  1938. p_type == TK_OP_MOD ||
  1939. p_type == TK_OP_SHIFT_LEFT ||
  1940. p_type == TK_OP_SHIFT_RIGHT ||
  1941. p_type == TK_OP_ASSIGN ||
  1942. p_type == TK_OP_ASSIGN_ADD ||
  1943. p_type == TK_OP_ASSIGN_SUB ||
  1944. p_type == TK_OP_ASSIGN_MUL ||
  1945. p_type == TK_OP_ASSIGN_DIV ||
  1946. p_type == TK_OP_ASSIGN_MOD ||
  1947. p_type == TK_OP_ASSIGN_SHIFT_LEFT ||
  1948. p_type == TK_OP_ASSIGN_SHIFT_RIGHT ||
  1949. p_type == TK_OP_ASSIGN_BIT_AND ||
  1950. p_type == TK_OP_ASSIGN_BIT_OR ||
  1951. p_type == TK_OP_ASSIGN_BIT_XOR ||
  1952. p_type == TK_OP_BIT_AND ||
  1953. p_type == TK_OP_BIT_OR ||
  1954. p_type == TK_OP_BIT_XOR ||
  1955. p_type == TK_OP_BIT_INVERT ||
  1956. p_type == TK_OP_INCREMENT ||
  1957. p_type == TK_OP_DECREMENT ||
  1958. p_type == TK_QUESTION ||
  1959. p_type == TK_COLON);
  1960. }
  1961. bool ShaderLanguage::convert_constant(ConstantNode *p_constant, DataType p_to_type, ConstantNode::Value *p_value) {
  1962. if (p_constant->datatype == p_to_type) {
  1963. if (p_value) {
  1964. for (int i = 0; i < p_constant->values.size(); i++) {
  1965. p_value[i] = p_constant->values[i];
  1966. }
  1967. }
  1968. return true;
  1969. } else if (p_constant->datatype == TYPE_INT && p_to_type == TYPE_FLOAT) {
  1970. if (p_value) {
  1971. p_value->real = p_constant->values[0].sint;
  1972. }
  1973. return true;
  1974. } else if (p_constant->datatype == TYPE_UINT && p_to_type == TYPE_FLOAT) {
  1975. if (p_value) {
  1976. p_value->real = p_constant->values[0].uint;
  1977. }
  1978. return true;
  1979. } else if (p_constant->datatype == TYPE_INT && p_to_type == TYPE_UINT) {
  1980. if (p_constant->values[0].sint < 0) {
  1981. return false;
  1982. }
  1983. if (p_value) {
  1984. p_value->uint = p_constant->values[0].sint;
  1985. }
  1986. return true;
  1987. } else if (p_constant->datatype == TYPE_UINT && p_to_type == TYPE_INT) {
  1988. if (p_constant->values[0].uint > 0x7FFFFFFF) {
  1989. return false;
  1990. }
  1991. if (p_value) {
  1992. p_value->sint = p_constant->values[0].uint;
  1993. }
  1994. return true;
  1995. } else
  1996. return false;
  1997. }
  1998. bool ShaderLanguage::is_scalar_type(DataType p_type) {
  1999. return p_type == TYPE_BOOL || p_type == TYPE_INT || p_type == TYPE_UINT || p_type == TYPE_FLOAT;
  2000. }
  2001. bool ShaderLanguage::is_sampler_type(DataType p_type) {
  2002. return p_type == TYPE_SAMPLER2D ||
  2003. p_type == TYPE_ISAMPLER2D ||
  2004. p_type == TYPE_USAMPLER2D ||
  2005. p_type == TYPE_SAMPLER2DARRAY ||
  2006. p_type == TYPE_ISAMPLER2DARRAY ||
  2007. p_type == TYPE_USAMPLER2DARRAY ||
  2008. p_type == TYPE_SAMPLER3D ||
  2009. p_type == TYPE_ISAMPLER3D ||
  2010. p_type == TYPE_USAMPLER3D ||
  2011. p_type == TYPE_SAMPLERCUBE;
  2012. }
  2013. Variant ShaderLanguage::constant_value_to_variant(const Vector<ShaderLanguage::ConstantNode::Value> &p_value, DataType p_type, ShaderLanguage::ShaderNode::Uniform::Hint p_hint) {
  2014. if (p_value.size() > 0) {
  2015. Variant value;
  2016. switch (p_type) {
  2017. case ShaderLanguage::TYPE_BOOL:
  2018. value = Variant(p_value[0].boolean);
  2019. break;
  2020. case ShaderLanguage::TYPE_BVEC2:
  2021. case ShaderLanguage::TYPE_BVEC3:
  2022. case ShaderLanguage::TYPE_BVEC4:
  2023. case ShaderLanguage::TYPE_INT:
  2024. value = Variant(p_value[0].sint);
  2025. break;
  2026. case ShaderLanguage::TYPE_IVEC2:
  2027. value = Variant(Vector2(p_value[0].sint, p_value[1].sint));
  2028. break;
  2029. case ShaderLanguage::TYPE_IVEC3:
  2030. value = Variant(Vector3(p_value[0].sint, p_value[1].sint, p_value[2].sint));
  2031. break;
  2032. case ShaderLanguage::TYPE_IVEC4:
  2033. value = Variant(Plane(p_value[0].sint, p_value[1].sint, p_value[2].sint, p_value[3].sint));
  2034. break;
  2035. case ShaderLanguage::TYPE_UINT:
  2036. value = Variant(p_value[0].uint);
  2037. break;
  2038. case ShaderLanguage::TYPE_UVEC2:
  2039. value = Variant(Vector2(p_value[0].uint, p_value[1].uint));
  2040. break;
  2041. case ShaderLanguage::TYPE_UVEC3:
  2042. value = Variant(Vector3(p_value[0].uint, p_value[1].uint, p_value[2].uint));
  2043. break;
  2044. case ShaderLanguage::TYPE_UVEC4:
  2045. value = Variant(Plane(p_value[0].uint, p_value[1].uint, p_value[2].uint, p_value[3].uint));
  2046. break;
  2047. case ShaderLanguage::TYPE_FLOAT:
  2048. value = Variant(p_value[0].real);
  2049. break;
  2050. case ShaderLanguage::TYPE_VEC2:
  2051. value = Variant(Vector2(p_value[0].real, p_value[1].real));
  2052. break;
  2053. case ShaderLanguage::TYPE_VEC3:
  2054. value = Variant(Vector3(p_value[0].real, p_value[1].real, p_value[2].real));
  2055. break;
  2056. case ShaderLanguage::TYPE_VEC4:
  2057. if (p_hint == ShaderLanguage::ShaderNode::Uniform::HINT_COLOR) {
  2058. value = Variant(Color(p_value[0].real, p_value[1].real, p_value[2].real, p_value[3].real));
  2059. } else {
  2060. value = Variant(Plane(p_value[0].real, p_value[1].real, p_value[2].real, p_value[3].real));
  2061. }
  2062. break;
  2063. case ShaderLanguage::TYPE_MAT2:
  2064. value = Variant(Transform2D(p_value[0].real, p_value[2].real, p_value[1].real, p_value[3].real, 0.0, 0.0));
  2065. break;
  2066. case ShaderLanguage::TYPE_MAT3: {
  2067. Basis p;
  2068. p[0][0] = p_value[0].real;
  2069. p[0][1] = p_value[1].real;
  2070. p[0][2] = p_value[2].real;
  2071. p[1][0] = p_value[3].real;
  2072. p[1][1] = p_value[4].real;
  2073. p[1][2] = p_value[5].real;
  2074. p[2][0] = p_value[6].real;
  2075. p[2][1] = p_value[7].real;
  2076. p[2][2] = p_value[8].real;
  2077. value = Variant(p);
  2078. break;
  2079. }
  2080. case ShaderLanguage::TYPE_MAT4: {
  2081. Basis p;
  2082. p[0][0] = p_value[0].real;
  2083. p[0][1] = p_value[1].real;
  2084. p[0][2] = p_value[2].real;
  2085. p[1][0] = p_value[4].real;
  2086. p[1][1] = p_value[5].real;
  2087. p[1][2] = p_value[6].real;
  2088. p[2][0] = p_value[8].real;
  2089. p[2][1] = p_value[9].real;
  2090. p[2][2] = p_value[10].real;
  2091. Transform t = Transform(p, Vector3(p_value[3].real, p_value[7].real, p_value[11].real));
  2092. value = Variant(t);
  2093. break;
  2094. }
  2095. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  2096. case ShaderLanguage::TYPE_ISAMPLER2D:
  2097. case ShaderLanguage::TYPE_ISAMPLER3D:
  2098. case ShaderLanguage::TYPE_SAMPLER2DARRAY:
  2099. case ShaderLanguage::TYPE_SAMPLER2D:
  2100. case ShaderLanguage::TYPE_SAMPLER3D:
  2101. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  2102. case ShaderLanguage::TYPE_USAMPLER2D:
  2103. case ShaderLanguage::TYPE_USAMPLER3D:
  2104. case ShaderLanguage::TYPE_SAMPLERCUBE: {
  2105. // Texture types, likely not relevant here.
  2106. break;
  2107. }
  2108. case ShaderLanguage::TYPE_VOID:
  2109. break;
  2110. }
  2111. return value;
  2112. }
  2113. return Variant();
  2114. }
  2115. void ShaderLanguage::get_keyword_list(List<String> *r_keywords) {
  2116. Set<String> kws;
  2117. int idx = 0;
  2118. while (keyword_list[idx].text) {
  2119. kws.insert(keyword_list[idx].text);
  2120. idx++;
  2121. }
  2122. idx = 0;
  2123. while (builtin_func_defs[idx].name) {
  2124. kws.insert(builtin_func_defs[idx].name);
  2125. idx++;
  2126. }
  2127. for (Set<String>::Element *E = kws.front(); E; E = E->next()) {
  2128. r_keywords->push_back(E->get());
  2129. }
  2130. }
  2131. void ShaderLanguage::get_builtin_funcs(List<String> *r_keywords) {
  2132. Set<String> kws;
  2133. int idx = 0;
  2134. while (builtin_func_defs[idx].name) {
  2135. kws.insert(builtin_func_defs[idx].name);
  2136. idx++;
  2137. }
  2138. for (Set<String>::Element *E = kws.front(); E; E = E->next()) {
  2139. r_keywords->push_back(E->get());
  2140. }
  2141. }
  2142. ShaderLanguage::DataType ShaderLanguage::get_scalar_type(DataType p_type) {
  2143. static const DataType scalar_types[] = {
  2144. TYPE_VOID,
  2145. TYPE_BOOL,
  2146. TYPE_BOOL,
  2147. TYPE_BOOL,
  2148. TYPE_BOOL,
  2149. TYPE_INT,
  2150. TYPE_INT,
  2151. TYPE_INT,
  2152. TYPE_INT,
  2153. TYPE_UINT,
  2154. TYPE_UINT,
  2155. TYPE_UINT,
  2156. TYPE_UINT,
  2157. TYPE_FLOAT,
  2158. TYPE_FLOAT,
  2159. TYPE_FLOAT,
  2160. TYPE_FLOAT,
  2161. TYPE_FLOAT,
  2162. TYPE_FLOAT,
  2163. TYPE_FLOAT,
  2164. TYPE_FLOAT,
  2165. TYPE_INT,
  2166. TYPE_UINT,
  2167. TYPE_FLOAT,
  2168. };
  2169. return scalar_types[p_type];
  2170. }
  2171. int ShaderLanguage::get_cardinality(DataType p_type) {
  2172. static const int cardinality_table[] = {
  2173. 0,
  2174. 1,
  2175. 2,
  2176. 3,
  2177. 4,
  2178. 1,
  2179. 2,
  2180. 3,
  2181. 4,
  2182. 1,
  2183. 2,
  2184. 3,
  2185. 4,
  2186. 1,
  2187. 2,
  2188. 3,
  2189. 4,
  2190. 4,
  2191. 9,
  2192. 16,
  2193. 1,
  2194. 1,
  2195. 1,
  2196. 1,
  2197. };
  2198. return cardinality_table[p_type];
  2199. }
  2200. bool ShaderLanguage::_get_completable_identifier(BlockNode *p_block, CompletionType p_type, StringName &identifier) {
  2201. identifier = StringName();
  2202. TkPos pos = { 0, 0 };
  2203. Token tk = _get_token();
  2204. if (tk.type == TK_IDENTIFIER) {
  2205. identifier = tk.text;
  2206. pos = _get_tkpos();
  2207. tk = _get_token();
  2208. }
  2209. if (tk.type == TK_CURSOR) {
  2210. completion_type = p_type;
  2211. completion_line = tk_line;
  2212. completion_block = p_block;
  2213. pos = _get_tkpos();
  2214. tk = _get_token();
  2215. if (tk.type == TK_IDENTIFIER) {
  2216. identifier = identifier.operator String() + tk.text.operator String();
  2217. } else {
  2218. _set_tkpos(pos);
  2219. }
  2220. return true;
  2221. } else if (identifier != StringName()) {
  2222. _set_tkpos(pos);
  2223. }
  2224. return false;
  2225. }
  2226. bool ShaderLanguage::_is_operator_assign(Operator p_op) const {
  2227. switch (p_op) {
  2228. case OP_ASSIGN:
  2229. case OP_ASSIGN_ADD:
  2230. case OP_ASSIGN_SUB:
  2231. case OP_ASSIGN_MUL:
  2232. case OP_ASSIGN_DIV:
  2233. case OP_ASSIGN_MOD:
  2234. case OP_ASSIGN_SHIFT_LEFT:
  2235. case OP_ASSIGN_SHIFT_RIGHT:
  2236. case OP_ASSIGN_BIT_AND:
  2237. case OP_ASSIGN_BIT_OR:
  2238. case OP_ASSIGN_BIT_XOR:
  2239. return true;
  2240. default:
  2241. return false;
  2242. }
  2243. return false;
  2244. }
  2245. bool ShaderLanguage::_validate_assign(Node *p_node, const Map<StringName, BuiltInInfo> &p_builtin_types, String *r_message) {
  2246. if (p_node->type == Node::TYPE_OPERATOR) {
  2247. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  2248. if (op->op == OP_INDEX) {
  2249. return _validate_assign(op->arguments[0], p_builtin_types, r_message);
  2250. } else if (_is_operator_assign(op->op)) {
  2251. //chained assignment
  2252. return _validate_assign(op->arguments[1], p_builtin_types, r_message);
  2253. } else if (op->op == OP_CALL) {
  2254. if (r_message)
  2255. *r_message = RTR("Assignment to function.");
  2256. return false;
  2257. }
  2258. } else if (p_node->type == Node::TYPE_MEMBER) {
  2259. MemberNode *member = static_cast<MemberNode *>(p_node);
  2260. return _validate_assign(member->owner, p_builtin_types, r_message);
  2261. } else if (p_node->type == Node::TYPE_VARIABLE) {
  2262. VariableNode *var = static_cast<VariableNode *>(p_node);
  2263. if (shader->uniforms.has(var->name)) {
  2264. if (r_message)
  2265. *r_message = RTR("Assignment to uniform.");
  2266. return false;
  2267. }
  2268. if (shader->varyings.has(var->name) && current_function != String("vertex")) {
  2269. if (r_message)
  2270. *r_message = RTR("Varyings can only be assigned in vertex function.");
  2271. return false;
  2272. }
  2273. if (!(p_builtin_types.has(var->name) && p_builtin_types[var->name].constant)) {
  2274. return true;
  2275. }
  2276. }
  2277. if (r_message)
  2278. *r_message = "Assignment to constant expression.";
  2279. return false;
  2280. }
  2281. ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types) {
  2282. Vector<Expression> expression;
  2283. //Vector<TokenType> operators;
  2284. while (true) {
  2285. Node *expr = NULL;
  2286. TkPos prepos = _get_tkpos();
  2287. Token tk = _get_token();
  2288. TkPos pos = _get_tkpos();
  2289. if (tk.type == TK_PARENTHESIS_OPEN) {
  2290. //handle subexpression
  2291. expr = _parse_and_reduce_expression(p_block, p_builtin_types);
  2292. if (!expr)
  2293. return NULL;
  2294. tk = _get_token();
  2295. if (tk.type != TK_PARENTHESIS_CLOSE) {
  2296. _set_error("Expected ')' in expression");
  2297. return NULL;
  2298. }
  2299. } else if (tk.type == TK_REAL_CONSTANT) {
  2300. ConstantNode *constant = alloc_node<ConstantNode>();
  2301. ConstantNode::Value v;
  2302. v.real = tk.constant;
  2303. constant->values.push_back(v);
  2304. constant->datatype = TYPE_FLOAT;
  2305. expr = constant;
  2306. } else if (tk.type == TK_INT_CONSTANT) {
  2307. ConstantNode *constant = alloc_node<ConstantNode>();
  2308. ConstantNode::Value v;
  2309. v.sint = tk.constant;
  2310. constant->values.push_back(v);
  2311. constant->datatype = TYPE_INT;
  2312. expr = constant;
  2313. } else if (tk.type == TK_TRUE) {
  2314. //handle true constant
  2315. ConstantNode *constant = alloc_node<ConstantNode>();
  2316. ConstantNode::Value v;
  2317. v.boolean = true;
  2318. constant->values.push_back(v);
  2319. constant->datatype = TYPE_BOOL;
  2320. expr = constant;
  2321. } else if (tk.type == TK_FALSE) {
  2322. //handle false constant
  2323. ConstantNode *constant = alloc_node<ConstantNode>();
  2324. ConstantNode::Value v;
  2325. v.boolean = false;
  2326. constant->values.push_back(v);
  2327. constant->datatype = TYPE_BOOL;
  2328. expr = constant;
  2329. } else if (tk.type == TK_TYPE_VOID) {
  2330. //make sure void is not used in expression
  2331. _set_error("Void value not allowed in Expression");
  2332. return NULL;
  2333. } else if (is_token_nonvoid_datatype(tk.type)) {
  2334. //basic type constructor
  2335. OperatorNode *func = alloc_node<OperatorNode>();
  2336. func->op = OP_CONSTRUCT;
  2337. if (is_token_precision(tk.type)) {
  2338. func->return_precision_cache = get_token_precision(tk.type);
  2339. tk = _get_token();
  2340. }
  2341. VariableNode *funcname = alloc_node<VariableNode>();
  2342. funcname->name = get_datatype_name(get_token_datatype(tk.type));
  2343. func->arguments.push_back(funcname);
  2344. tk = _get_token();
  2345. if (tk.type != TK_PARENTHESIS_OPEN) {
  2346. _set_error("Expected '(' after type name");
  2347. return NULL;
  2348. }
  2349. int carg = -1;
  2350. bool ok = _parse_function_arguments(p_block, p_builtin_types, func, &carg);
  2351. if (carg >= 0) {
  2352. completion_type = COMPLETION_CALL_ARGUMENTS;
  2353. completion_line = tk_line;
  2354. completion_block = p_block;
  2355. completion_function = funcname->name;
  2356. completion_argument = carg;
  2357. }
  2358. if (!ok)
  2359. return NULL;
  2360. if (!_validate_function_call(p_block, func, &func->return_cache)) {
  2361. _set_error("No matching constructor found for: '" + String(funcname->name) + "'");
  2362. return NULL;
  2363. }
  2364. //validate_Function_call()
  2365. expr = _reduce_expression(p_block, func);
  2366. } else if (tk.type == TK_IDENTIFIER) {
  2367. _set_tkpos(prepos);
  2368. StringName identifier;
  2369. _get_completable_identifier(p_block, COMPLETION_IDENTIFIER, identifier);
  2370. tk = _get_token();
  2371. if (tk.type == TK_PARENTHESIS_OPEN) {
  2372. //a function
  2373. StringName name = identifier;
  2374. OperatorNode *func = alloc_node<OperatorNode>();
  2375. func->op = OP_CALL;
  2376. VariableNode *funcname = alloc_node<VariableNode>();
  2377. funcname->name = name;
  2378. func->arguments.push_back(funcname);
  2379. int carg = -1;
  2380. bool ok = _parse_function_arguments(p_block, p_builtin_types, func, &carg);
  2381. //test if function was parsed first
  2382. for (int i = 0; i < shader->functions.size(); i++) {
  2383. if (shader->functions[i].name == name) {
  2384. //add to current function as dependency
  2385. for (int j = 0; j < shader->functions.size(); j++) {
  2386. if (shader->functions[j].name == current_function) {
  2387. shader->functions.write[j].uses_function.insert(name);
  2388. break;
  2389. }
  2390. }
  2391. break;
  2392. }
  2393. }
  2394. if (carg >= 0) {
  2395. completion_type = COMPLETION_CALL_ARGUMENTS;
  2396. completion_line = tk_line;
  2397. completion_block = p_block;
  2398. completion_function = funcname->name;
  2399. completion_argument = carg;
  2400. }
  2401. if (!ok)
  2402. return NULL;
  2403. if (!_validate_function_call(p_block, func, &func->return_cache)) {
  2404. _set_error("No matching function found for: '" + String(funcname->name) + "'");
  2405. return NULL;
  2406. }
  2407. expr = func;
  2408. } else {
  2409. //an identifier
  2410. _set_tkpos(pos);
  2411. DataType data_type;
  2412. IdentifierType ident_type;
  2413. if (!_find_identifier(p_block, p_builtin_types, identifier, &data_type, &ident_type)) {
  2414. _set_error("Unknown identifier in expression: " + String(identifier));
  2415. return NULL;
  2416. }
  2417. if (ident_type == IDENTIFIER_FUNCTION) {
  2418. _set_error("Can't use function as identifier: " + String(identifier));
  2419. return NULL;
  2420. }
  2421. VariableNode *varname = alloc_node<VariableNode>();
  2422. varname->name = identifier;
  2423. varname->datatype_cache = data_type;
  2424. expr = varname;
  2425. }
  2426. } else if (tk.type == TK_OP_ADD) {
  2427. continue; //this one does nothing
  2428. } else if (tk.type == TK_OP_SUB || tk.type == TK_OP_NOT || tk.type == TK_OP_BIT_INVERT || tk.type == TK_OP_INCREMENT || tk.type == TK_OP_DECREMENT) {
  2429. Expression e;
  2430. e.is_op = true;
  2431. switch (tk.type) {
  2432. case TK_OP_SUB: e.op = OP_NEGATE; break;
  2433. case TK_OP_NOT: e.op = OP_NOT; break;
  2434. case TK_OP_BIT_INVERT: e.op = OP_BIT_INVERT; break;
  2435. case TK_OP_INCREMENT: e.op = OP_INCREMENT; break;
  2436. case TK_OP_DECREMENT: e.op = OP_DECREMENT; break;
  2437. default: ERR_FAIL_V(NULL);
  2438. }
  2439. expression.push_back(e);
  2440. continue;
  2441. } else {
  2442. _set_error("Expected expression, found: " + get_token_text(tk));
  2443. return NULL;
  2444. //nothing
  2445. }
  2446. ERR_FAIL_COND_V(!expr, NULL);
  2447. /* OK now see what's NEXT to the operator.. */
  2448. /* OK now see what's NEXT to the operator.. */
  2449. /* OK now see what's NEXT to the operator.. */
  2450. while (true) {
  2451. TkPos pos = _get_tkpos();
  2452. tk = _get_token();
  2453. if (tk.type == TK_CURSOR) {
  2454. //do nothing
  2455. } else if (tk.type == TK_PERIOD) {
  2456. StringName identifier;
  2457. if (_get_completable_identifier(p_block, COMPLETION_INDEX, identifier)) {
  2458. completion_base = expr->get_datatype();
  2459. }
  2460. if (identifier == StringName()) {
  2461. _set_error("Expected identifier as member");
  2462. return NULL;
  2463. }
  2464. DataType dt = expr->get_datatype();
  2465. String ident = identifier;
  2466. bool ok = true;
  2467. DataType member_type;
  2468. switch (dt) {
  2469. case TYPE_BVEC2:
  2470. case TYPE_IVEC2:
  2471. case TYPE_UVEC2:
  2472. case TYPE_VEC2: {
  2473. int l = ident.length();
  2474. if (l == 1) {
  2475. member_type = DataType(dt - 1);
  2476. } else if (l == 2) {
  2477. member_type = dt;
  2478. } else {
  2479. ok = false;
  2480. break;
  2481. }
  2482. const CharType *c = ident.ptr();
  2483. for (int i = 0; i < l; i++) {
  2484. switch (c[i]) {
  2485. case 'r':
  2486. case 'g':
  2487. case 'x':
  2488. case 'y':
  2489. break;
  2490. default:
  2491. ok = false;
  2492. break;
  2493. }
  2494. }
  2495. } break;
  2496. case TYPE_BVEC3:
  2497. case TYPE_IVEC3:
  2498. case TYPE_UVEC3:
  2499. case TYPE_VEC3: {
  2500. int l = ident.length();
  2501. if (l == 1) {
  2502. member_type = DataType(dt - 2);
  2503. } else if (l == 2) {
  2504. member_type = DataType(dt - 1);
  2505. } else if (l == 3) {
  2506. member_type = dt;
  2507. } else {
  2508. ok = false;
  2509. break;
  2510. }
  2511. const CharType *c = ident.ptr();
  2512. for (int i = 0; i < l; i++) {
  2513. switch (c[i]) {
  2514. case 'r':
  2515. case 'g':
  2516. case 'b':
  2517. case 'x':
  2518. case 'y':
  2519. case 'z':
  2520. break;
  2521. default:
  2522. ok = false;
  2523. break;
  2524. }
  2525. }
  2526. } break;
  2527. case TYPE_BVEC4:
  2528. case TYPE_IVEC4:
  2529. case TYPE_UVEC4:
  2530. case TYPE_VEC4: {
  2531. int l = ident.length();
  2532. if (l == 1) {
  2533. member_type = DataType(dt - 3);
  2534. } else if (l == 2) {
  2535. member_type = DataType(dt - 2);
  2536. } else if (l == 3) {
  2537. member_type = DataType(dt - 1);
  2538. } else if (l == 4) {
  2539. member_type = dt;
  2540. } else {
  2541. ok = false;
  2542. break;
  2543. }
  2544. const CharType *c = ident.ptr();
  2545. for (int i = 0; i < l; i++) {
  2546. switch (c[i]) {
  2547. case 'r':
  2548. case 'g':
  2549. case 'b':
  2550. case 'a':
  2551. case 'x':
  2552. case 'y':
  2553. case 'z':
  2554. case 'w':
  2555. break;
  2556. default:
  2557. ok = false;
  2558. break;
  2559. }
  2560. }
  2561. } break;
  2562. default: {
  2563. ok = false;
  2564. }
  2565. }
  2566. if (!ok) {
  2567. _set_error("Invalid member for " + get_datatype_name(dt) + " expression: ." + ident);
  2568. return NULL;
  2569. }
  2570. MemberNode *mn = alloc_node<MemberNode>();
  2571. mn->basetype = dt;
  2572. mn->datatype = member_type;
  2573. mn->name = ident;
  2574. mn->owner = expr;
  2575. expr = mn;
  2576. //todo
  2577. //member (period) has priority over any operator
  2578. //creates a subindexing expression in place
  2579. /*} else if (tk.type==TK_BRACKET_OPEN) {
  2580. //todo
  2581. //subindexing has priority over any operator
  2582. //creates a subindexing expression in place
  2583. */
  2584. } else if (tk.type == TK_BRACKET_OPEN) {
  2585. Node *index = _parse_and_reduce_expression(p_block, p_builtin_types);
  2586. if (!index)
  2587. return NULL;
  2588. if (index->get_datatype() != TYPE_INT && index->get_datatype() != TYPE_UINT) {
  2589. _set_error("Only integer datatypes are allowed for indexing");
  2590. return NULL;
  2591. }
  2592. DataType member_type = TYPE_VOID;
  2593. switch (expr->get_datatype()) {
  2594. case TYPE_BVEC2:
  2595. case TYPE_VEC2:
  2596. case TYPE_IVEC2:
  2597. case TYPE_UVEC2:
  2598. case TYPE_MAT2:
  2599. if (index->type == Node::TYPE_CONSTANT) {
  2600. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  2601. if (index_constant >= 2) {
  2602. _set_error("Index out of range (0-1)");
  2603. return NULL;
  2604. }
  2605. } else {
  2606. _set_error("Only integer constants are allowed as index at the moment");
  2607. return NULL;
  2608. }
  2609. switch (expr->get_datatype()) {
  2610. case TYPE_BVEC2: member_type = TYPE_BOOL; break;
  2611. case TYPE_VEC2: member_type = TYPE_FLOAT; break;
  2612. case TYPE_IVEC2: member_type = TYPE_INT; break;
  2613. case TYPE_UVEC2: member_type = TYPE_UINT; break;
  2614. case TYPE_MAT2: member_type = TYPE_VEC2; break;
  2615. default: break;
  2616. }
  2617. break;
  2618. case TYPE_BVEC3:
  2619. case TYPE_VEC3:
  2620. case TYPE_IVEC3:
  2621. case TYPE_UVEC3:
  2622. case TYPE_MAT3:
  2623. if (index->type == Node::TYPE_CONSTANT) {
  2624. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  2625. if (index_constant >= 3) {
  2626. _set_error("Index out of range (0-2)");
  2627. return NULL;
  2628. }
  2629. } else {
  2630. _set_error("Only integer constants are allowed as index at the moment");
  2631. return NULL;
  2632. }
  2633. switch (expr->get_datatype()) {
  2634. case TYPE_BVEC3: member_type = TYPE_BOOL; break;
  2635. case TYPE_VEC3: member_type = TYPE_FLOAT; break;
  2636. case TYPE_IVEC3: member_type = TYPE_INT; break;
  2637. case TYPE_UVEC3: member_type = TYPE_UINT; break;
  2638. case TYPE_MAT3: member_type = TYPE_VEC3; break;
  2639. default: break;
  2640. }
  2641. break;
  2642. case TYPE_BVEC4:
  2643. case TYPE_VEC4:
  2644. case TYPE_IVEC4:
  2645. case TYPE_UVEC4:
  2646. case TYPE_MAT4:
  2647. if (index->type == Node::TYPE_CONSTANT) {
  2648. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  2649. if (index_constant >= 4) {
  2650. _set_error("Index out of range (0-3)");
  2651. return NULL;
  2652. }
  2653. } else {
  2654. _set_error("Only integer constants are allowed as index at the moment");
  2655. return NULL;
  2656. }
  2657. switch (expr->get_datatype()) {
  2658. case TYPE_BVEC4: member_type = TYPE_BOOL; break;
  2659. case TYPE_VEC4: member_type = TYPE_FLOAT; break;
  2660. case TYPE_IVEC4: member_type = TYPE_INT; break;
  2661. case TYPE_UVEC4: member_type = TYPE_UINT; break;
  2662. case TYPE_MAT4: member_type = TYPE_VEC4; break;
  2663. default: break;
  2664. }
  2665. break;
  2666. default: {
  2667. _set_error("Object of type '" + get_datatype_name(expr->get_datatype()) + "' can't be indexed");
  2668. return NULL;
  2669. }
  2670. }
  2671. OperatorNode *op = alloc_node<OperatorNode>();
  2672. op->op = OP_INDEX;
  2673. op->return_cache = member_type;
  2674. op->arguments.push_back(expr);
  2675. op->arguments.push_back(index);
  2676. expr = op;
  2677. tk = _get_token();
  2678. if (tk.type != TK_BRACKET_CLOSE) {
  2679. _set_error("Expected ']' after indexing expression");
  2680. return NULL;
  2681. }
  2682. } else if (tk.type == TK_OP_INCREMENT || tk.type == TK_OP_DECREMENT) {
  2683. OperatorNode *op = alloc_node<OperatorNode>();
  2684. op->op = tk.type == TK_OP_DECREMENT ? OP_POST_DECREMENT : OP_POST_INCREMENT;
  2685. op->arguments.push_back(expr);
  2686. if (!_validate_operator(op, &op->return_cache)) {
  2687. _set_error("Invalid base type for increment/decrement operator");
  2688. return NULL;
  2689. }
  2690. if (!_validate_assign(expr, p_builtin_types)) {
  2691. _set_error("Invalid use of increment/decrement operator in constant expression.");
  2692. return NULL;
  2693. }
  2694. expr = op;
  2695. } else {
  2696. _set_tkpos(pos);
  2697. break;
  2698. }
  2699. }
  2700. Expression e;
  2701. e.is_op = false;
  2702. e.node = expr;
  2703. expression.push_back(e);
  2704. pos = _get_tkpos();
  2705. tk = _get_token();
  2706. if (is_token_operator(tk.type)) {
  2707. Expression o;
  2708. o.is_op = true;
  2709. switch (tk.type) {
  2710. case TK_OP_EQUAL: o.op = OP_EQUAL; break;
  2711. case TK_OP_NOT_EQUAL: o.op = OP_NOT_EQUAL; break;
  2712. case TK_OP_LESS: o.op = OP_LESS; break;
  2713. case TK_OP_LESS_EQUAL: o.op = OP_LESS_EQUAL; break;
  2714. case TK_OP_GREATER: o.op = OP_GREATER; break;
  2715. case TK_OP_GREATER_EQUAL: o.op = OP_GREATER_EQUAL; break;
  2716. case TK_OP_AND: o.op = OP_AND; break;
  2717. case TK_OP_OR: o.op = OP_OR; break;
  2718. case TK_OP_ADD: o.op = OP_ADD; break;
  2719. case TK_OP_SUB: o.op = OP_SUB; break;
  2720. case TK_OP_MUL: o.op = OP_MUL; break;
  2721. case TK_OP_DIV: o.op = OP_DIV; break;
  2722. case TK_OP_MOD: o.op = OP_MOD; break;
  2723. case TK_OP_SHIFT_LEFT: o.op = OP_SHIFT_LEFT; break;
  2724. case TK_OP_SHIFT_RIGHT: o.op = OP_SHIFT_RIGHT; break;
  2725. case TK_OP_ASSIGN: o.op = OP_ASSIGN; break;
  2726. case TK_OP_ASSIGN_ADD: o.op = OP_ASSIGN_ADD; break;
  2727. case TK_OP_ASSIGN_SUB: o.op = OP_ASSIGN_SUB; break;
  2728. case TK_OP_ASSIGN_MUL: o.op = OP_ASSIGN_MUL; break;
  2729. case TK_OP_ASSIGN_DIV: o.op = OP_ASSIGN_DIV; break;
  2730. case TK_OP_ASSIGN_MOD: o.op = OP_ASSIGN_MOD; break;
  2731. case TK_OP_ASSIGN_SHIFT_LEFT: o.op = OP_ASSIGN_SHIFT_LEFT; break;
  2732. case TK_OP_ASSIGN_SHIFT_RIGHT: o.op = OP_ASSIGN_SHIFT_RIGHT; break;
  2733. case TK_OP_ASSIGN_BIT_AND: o.op = OP_ASSIGN_BIT_AND; break;
  2734. case TK_OP_ASSIGN_BIT_OR: o.op = OP_ASSIGN_BIT_OR; break;
  2735. case TK_OP_ASSIGN_BIT_XOR: o.op = OP_ASSIGN_BIT_XOR; break;
  2736. case TK_OP_BIT_AND: o.op = OP_BIT_AND; break;
  2737. case TK_OP_BIT_OR: o.op = OP_BIT_OR; break;
  2738. case TK_OP_BIT_XOR: o.op = OP_BIT_XOR; break;
  2739. case TK_QUESTION: o.op = OP_SELECT_IF; break;
  2740. case TK_COLON: o.op = OP_SELECT_ELSE; break;
  2741. default: {
  2742. _set_error("Invalid token for operator: " + get_token_text(tk));
  2743. return NULL;
  2744. }
  2745. }
  2746. expression.push_back(o);
  2747. } else {
  2748. _set_tkpos(pos); //something else, so rollback and end
  2749. break;
  2750. }
  2751. }
  2752. /* Reduce the set set of expressions and place them in an operator tree, respecting precedence */
  2753. while (expression.size() > 1) {
  2754. int next_op = -1;
  2755. int min_priority = 0xFFFFF;
  2756. bool is_unary = false;
  2757. bool is_ternary = false;
  2758. for (int i = 0; i < expression.size(); i++) {
  2759. if (!expression[i].is_op) {
  2760. continue;
  2761. }
  2762. bool unary = false;
  2763. bool ternary = false;
  2764. int priority;
  2765. switch (expression[i].op) {
  2766. case OP_EQUAL: priority = 8; break;
  2767. case OP_NOT_EQUAL: priority = 8; break;
  2768. case OP_LESS: priority = 7; break;
  2769. case OP_LESS_EQUAL: priority = 7; break;
  2770. case OP_GREATER: priority = 7; break;
  2771. case OP_GREATER_EQUAL: priority = 7; break;
  2772. case OP_AND: priority = 12; break;
  2773. case OP_OR: priority = 14; break;
  2774. case OP_NOT:
  2775. priority = 3;
  2776. unary = true;
  2777. break;
  2778. case OP_NEGATE:
  2779. priority = 3;
  2780. unary = true;
  2781. break;
  2782. case OP_ADD: priority = 5; break;
  2783. case OP_SUB: priority = 5; break;
  2784. case OP_MUL: priority = 4; break;
  2785. case OP_DIV: priority = 4; break;
  2786. case OP_MOD: priority = 4; break;
  2787. case OP_SHIFT_LEFT: priority = 6; break;
  2788. case OP_SHIFT_RIGHT: priority = 6; break;
  2789. case OP_ASSIGN: priority = 16; break;
  2790. case OP_ASSIGN_ADD: priority = 16; break;
  2791. case OP_ASSIGN_SUB: priority = 16; break;
  2792. case OP_ASSIGN_MUL: priority = 16; break;
  2793. case OP_ASSIGN_DIV: priority = 16; break;
  2794. case OP_ASSIGN_MOD: priority = 16; break;
  2795. case OP_ASSIGN_SHIFT_LEFT: priority = 16; break;
  2796. case OP_ASSIGN_SHIFT_RIGHT: priority = 16; break;
  2797. case OP_ASSIGN_BIT_AND: priority = 16; break;
  2798. case OP_ASSIGN_BIT_OR: priority = 16; break;
  2799. case OP_ASSIGN_BIT_XOR: priority = 16; break;
  2800. case OP_BIT_AND: priority = 9; break;
  2801. case OP_BIT_OR: priority = 11; break;
  2802. case OP_BIT_XOR: priority = 10; break;
  2803. case OP_BIT_INVERT:
  2804. priority = 3;
  2805. unary = true;
  2806. break;
  2807. case OP_INCREMENT:
  2808. priority = 3;
  2809. unary = true;
  2810. break;
  2811. case OP_DECREMENT:
  2812. priority = 3;
  2813. unary = true;
  2814. break;
  2815. case OP_SELECT_IF:
  2816. priority = 15;
  2817. ternary = true;
  2818. break;
  2819. case OP_SELECT_ELSE:
  2820. priority = 15;
  2821. ternary = true;
  2822. break;
  2823. default:
  2824. ERR_FAIL_V(NULL); //unexpected operator
  2825. }
  2826. if (priority < min_priority) {
  2827. // < is used for left to right (default)
  2828. // <= is used for right to left
  2829. next_op = i;
  2830. min_priority = priority;
  2831. is_unary = unary;
  2832. is_ternary = ternary;
  2833. }
  2834. }
  2835. ERR_FAIL_COND_V(next_op == -1, NULL);
  2836. // OK! create operator..
  2837. // OK! create operator..
  2838. if (is_unary) {
  2839. int expr_pos = next_op;
  2840. while (expression[expr_pos].is_op) {
  2841. expr_pos++;
  2842. if (expr_pos == expression.size()) {
  2843. //can happen..
  2844. _set_error("Unexpected end of expression...");
  2845. return NULL;
  2846. }
  2847. }
  2848. //consecutively do unary opeators
  2849. for (int i = expr_pos - 1; i >= next_op; i--) {
  2850. OperatorNode *op = alloc_node<OperatorNode>();
  2851. op->op = expression[i].op;
  2852. if ((op->op == OP_INCREMENT || op->op == OP_DECREMENT) && !_validate_assign(expression[i + 1].node, p_builtin_types)) {
  2853. _set_error("Can't use increment/decrement operator in constant expression.");
  2854. return NULL;
  2855. }
  2856. op->arguments.push_back(expression[i + 1].node);
  2857. expression.write[i].is_op = false;
  2858. expression.write[i].node = op;
  2859. if (!_validate_operator(op, &op->return_cache)) {
  2860. String at;
  2861. for (int i = 0; i < op->arguments.size(); i++) {
  2862. if (i > 0)
  2863. at += " and ";
  2864. at += get_datatype_name(op->arguments[i]->get_datatype());
  2865. }
  2866. _set_error("Invalid arguments to unary operator '" + get_operator_text(op->op) + "' :" + at);
  2867. return NULL;
  2868. }
  2869. expression.remove(i + 1);
  2870. }
  2871. } else if (is_ternary) {
  2872. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  2873. _set_error("Parser bug...");
  2874. ERR_FAIL_V(NULL);
  2875. }
  2876. if (next_op + 2 >= expression.size() || !expression[next_op + 2].is_op || expression[next_op + 2].op != OP_SELECT_ELSE) {
  2877. _set_error("Missing matching ':' for select operator");
  2878. return NULL;
  2879. }
  2880. OperatorNode *op = alloc_node<OperatorNode>();
  2881. op->op = expression[next_op].op;
  2882. op->arguments.push_back(expression[next_op - 1].node);
  2883. op->arguments.push_back(expression[next_op + 1].node);
  2884. op->arguments.push_back(expression[next_op + 3].node);
  2885. expression.write[next_op - 1].is_op = false;
  2886. expression.write[next_op - 1].node = op;
  2887. if (!_validate_operator(op, &op->return_cache)) {
  2888. String at;
  2889. for (int i = 0; i < op->arguments.size(); i++) {
  2890. if (i > 0)
  2891. at += " and ";
  2892. at += get_datatype_name(op->arguments[i]->get_datatype());
  2893. }
  2894. _set_error("Invalid argument to ternary ?: operator: " + at);
  2895. return NULL;
  2896. }
  2897. for (int i = 0; i < 4; i++) {
  2898. expression.remove(next_op);
  2899. }
  2900. } else {
  2901. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  2902. _set_error("Parser bug...");
  2903. ERR_FAIL_V(NULL);
  2904. }
  2905. OperatorNode *op = alloc_node<OperatorNode>();
  2906. op->op = expression[next_op].op;
  2907. if (expression[next_op - 1].is_op) {
  2908. _set_error("Parser bug...");
  2909. ERR_FAIL_V(NULL);
  2910. }
  2911. if (_is_operator_assign(op->op)) {
  2912. String assign_message;
  2913. if (!_validate_assign(expression[next_op - 1].node, p_builtin_types, &assign_message)) {
  2914. _set_error(assign_message);
  2915. return NULL;
  2916. }
  2917. }
  2918. if (expression[next_op + 1].is_op) {
  2919. // this is not invalid and can really appear
  2920. // but it becomes invalid anyway because no binary op
  2921. // can be followed by a unary op in a valid combination,
  2922. // due to how precedence works, unaries will always disappear first
  2923. _set_error("Parser bug...");
  2924. }
  2925. op->arguments.push_back(expression[next_op - 1].node); //expression goes as left
  2926. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as right
  2927. expression.write[next_op - 1].node = op;
  2928. //replace all 3 nodes by this operator and make it an expression
  2929. if (!_validate_operator(op, &op->return_cache)) {
  2930. String at;
  2931. for (int i = 0; i < op->arguments.size(); i++) {
  2932. if (i > 0)
  2933. at += " and ";
  2934. at += get_datatype_name(op->arguments[i]->get_datatype());
  2935. }
  2936. _set_error("Invalid arguments to operator '" + get_operator_text(op->op) + "' :" + at);
  2937. return NULL;
  2938. }
  2939. expression.remove(next_op);
  2940. expression.remove(next_op);
  2941. }
  2942. }
  2943. return expression[0].node;
  2944. }
  2945. ShaderLanguage::Node *ShaderLanguage::_reduce_expression(BlockNode *p_block, ShaderLanguage::Node *p_node) {
  2946. if (p_node->type != Node::TYPE_OPERATOR)
  2947. return p_node;
  2948. //for now only reduce simple constructors
  2949. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  2950. if (op->op == OP_CONSTRUCT) {
  2951. ERR_FAIL_COND_V(op->arguments[0]->type != Node::TYPE_VARIABLE, p_node);
  2952. DataType type = op->get_datatype();
  2953. DataType base = get_scalar_type(type);
  2954. int cardinality = get_cardinality(type);
  2955. Vector<ConstantNode::Value> values;
  2956. for (int i = 1; i < op->arguments.size(); i++) {
  2957. op->arguments.write[i] = _reduce_expression(p_block, op->arguments[i]);
  2958. if (op->arguments[i]->type == Node::TYPE_CONSTANT) {
  2959. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[i]);
  2960. if (get_scalar_type(cn->datatype) == base) {
  2961. for (int j = 0; j < cn->values.size(); j++) {
  2962. values.push_back(cn->values[j]);
  2963. }
  2964. } else if (get_scalar_type(cn->datatype) == cn->datatype) {
  2965. ConstantNode::Value v;
  2966. if (!convert_constant(cn, base, &v)) {
  2967. return p_node;
  2968. }
  2969. values.push_back(v);
  2970. } else {
  2971. return p_node;
  2972. }
  2973. } else {
  2974. return p_node;
  2975. }
  2976. }
  2977. if (values.size() == 1) {
  2978. if (type >= TYPE_MAT2 && type <= TYPE_MAT4) {
  2979. ConstantNode::Value value = values[0];
  2980. ConstantNode::Value zero;
  2981. zero.real = 0.0f;
  2982. int size = 2 + (type - TYPE_MAT2);
  2983. values.clear();
  2984. for (int i = 0; i < size; i++) {
  2985. for (int j = 0; j < size; j++) {
  2986. values.push_back(i == j ? value : zero);
  2987. }
  2988. }
  2989. } else {
  2990. ConstantNode::Value value = values[0];
  2991. for (int i = 1; i < cardinality; i++) {
  2992. values.push_back(value);
  2993. }
  2994. }
  2995. } else if (values.size() != cardinality) {
  2996. ERR_PRINT("Failed to reduce expression, values and cardinality mismatch.");
  2997. return p_node;
  2998. }
  2999. ConstantNode *cn = alloc_node<ConstantNode>();
  3000. cn->datatype = op->get_datatype();
  3001. cn->values = values;
  3002. return cn;
  3003. } else if (op->op == OP_NEGATE) {
  3004. op->arguments.write[0] = _reduce_expression(p_block, op->arguments[0]);
  3005. if (op->arguments[0]->type == Node::TYPE_CONSTANT) {
  3006. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[0]);
  3007. DataType base = get_scalar_type(cn->datatype);
  3008. Vector<ConstantNode::Value> values;
  3009. for (int i = 0; i < cn->values.size(); i++) {
  3010. ConstantNode::Value nv;
  3011. switch (base) {
  3012. case TYPE_BOOL: {
  3013. nv.boolean = !cn->values[i].boolean;
  3014. } break;
  3015. case TYPE_INT: {
  3016. nv.sint = -cn->values[i].sint;
  3017. } break;
  3018. case TYPE_UINT: {
  3019. // FIXME: This can't work on uint
  3020. nv.uint = -cn->values[i].uint;
  3021. } break;
  3022. case TYPE_FLOAT: {
  3023. nv.real = -cn->values[i].real;
  3024. } break;
  3025. default: {}
  3026. }
  3027. values.push_back(nv);
  3028. }
  3029. cn->values = values;
  3030. return cn;
  3031. }
  3032. }
  3033. return p_node;
  3034. }
  3035. ShaderLanguage::Node *ShaderLanguage::_parse_and_reduce_expression(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types) {
  3036. ShaderLanguage::Node *expr = _parse_expression(p_block, p_builtin_types);
  3037. if (!expr) //errored
  3038. return NULL;
  3039. expr = _reduce_expression(p_block, expr);
  3040. return expr;
  3041. }
  3042. Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, bool p_just_one, bool p_can_break, bool p_can_continue) {
  3043. while (true) {
  3044. TkPos pos = _get_tkpos();
  3045. Token tk = _get_token();
  3046. if (tk.type == TK_CURLY_BRACKET_CLOSE) { //end of block
  3047. if (p_just_one) {
  3048. _set_error("Unexpected '}'");
  3049. return ERR_PARSE_ERROR;
  3050. }
  3051. return OK;
  3052. } else if (is_token_precision(tk.type) || is_token_nonvoid_datatype(tk.type)) {
  3053. DataPrecision precision = PRECISION_DEFAULT;
  3054. if (is_token_precision(tk.type)) {
  3055. precision = get_token_precision(tk.type);
  3056. tk = _get_token();
  3057. if (!is_token_nonvoid_datatype(tk.type)) {
  3058. _set_error("Expected datatype after precision");
  3059. return ERR_PARSE_ERROR;
  3060. }
  3061. }
  3062. if (!is_token_variable_datatype(tk.type)) {
  3063. _set_error("Invalid data type for variable (samplers not allowed)");
  3064. return ERR_PARSE_ERROR;
  3065. }
  3066. DataType type = get_token_datatype(tk.type);
  3067. tk = _get_token();
  3068. VariableDeclarationNode *vardecl = alloc_node<VariableDeclarationNode>();
  3069. vardecl->datatype = type;
  3070. vardecl->precision = precision;
  3071. p_block->statements.push_back(vardecl);
  3072. while (true) {
  3073. if (tk.type != TK_IDENTIFIER) {
  3074. _set_error("Expected identifier after type");
  3075. return ERR_PARSE_ERROR;
  3076. }
  3077. StringName name = tk.text;
  3078. if (_find_identifier(p_block, p_builtin_types, name)) {
  3079. _set_error("Redefinition of '" + String(name) + "'");
  3080. return ERR_PARSE_ERROR;
  3081. }
  3082. BlockNode::Variable var;
  3083. var.type = type;
  3084. var.precision = precision;
  3085. var.line = tk_line;
  3086. VariableDeclarationNode::Declaration decl;
  3087. decl.name = name;
  3088. decl.initializer = NULL;
  3089. tk = _get_token();
  3090. if (tk.type == TK_OP_ASSIGN) {
  3091. //variable created with assignment! must parse an expression
  3092. Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
  3093. if (!n)
  3094. return ERR_PARSE_ERROR;
  3095. decl.initializer = n;
  3096. if (var.type != n->get_datatype()) {
  3097. _set_error("Invalid assignment of '" + get_datatype_name(n->get_datatype()) + "' to '" + get_datatype_name(var.type) + "'");
  3098. return ERR_PARSE_ERROR;
  3099. }
  3100. tk = _get_token();
  3101. }
  3102. p_block->variables[name] = var;
  3103. vardecl->declarations.push_back(decl);
  3104. if (tk.type == TK_COMMA) {
  3105. tk = _get_token();
  3106. //another variable
  3107. } else if (tk.type == TK_SEMICOLON) {
  3108. break;
  3109. } else {
  3110. _set_error("Expected ',' or ';' after variable");
  3111. return ERR_PARSE_ERROR;
  3112. }
  3113. }
  3114. } else if (tk.type == TK_CURLY_BRACKET_OPEN) {
  3115. //a sub block, just because..
  3116. BlockNode *block = alloc_node<BlockNode>();
  3117. block->parent_block = p_block;
  3118. _parse_block(block, p_builtin_types, false, p_can_break, p_can_continue);
  3119. p_block->statements.push_back(block);
  3120. } else if (tk.type == TK_CF_IF) {
  3121. //if () {}
  3122. tk = _get_token();
  3123. if (tk.type != TK_PARENTHESIS_OPEN) {
  3124. _set_error("Expected '(' after if");
  3125. return ERR_PARSE_ERROR;
  3126. }
  3127. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  3128. cf->flow_op = FLOW_OP_IF;
  3129. Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
  3130. if (!n)
  3131. return ERR_PARSE_ERROR;
  3132. tk = _get_token();
  3133. if (tk.type != TK_PARENTHESIS_CLOSE) {
  3134. _set_error("Expected ')' after expression");
  3135. return ERR_PARSE_ERROR;
  3136. }
  3137. BlockNode *block = alloc_node<BlockNode>();
  3138. block->parent_block = p_block;
  3139. cf->expressions.push_back(n);
  3140. cf->blocks.push_back(block);
  3141. p_block->statements.push_back(cf);
  3142. Error err = _parse_block(block, p_builtin_types, true, p_can_break, p_can_continue);
  3143. if (err)
  3144. return err;
  3145. pos = _get_tkpos();
  3146. tk = _get_token();
  3147. if (tk.type == TK_CF_ELSE) {
  3148. block = alloc_node<BlockNode>();
  3149. block->parent_block = p_block;
  3150. cf->blocks.push_back(block);
  3151. err = _parse_block(block, p_builtin_types, true, p_can_break, p_can_continue);
  3152. } else {
  3153. _set_tkpos(pos); //rollback
  3154. }
  3155. } else if (tk.type == TK_CF_WHILE) {
  3156. //if () {}
  3157. tk = _get_token();
  3158. if (tk.type != TK_PARENTHESIS_OPEN) {
  3159. _set_error("Expected '(' after while");
  3160. return ERR_PARSE_ERROR;
  3161. }
  3162. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  3163. cf->flow_op = FLOW_OP_WHILE;
  3164. Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
  3165. if (!n)
  3166. return ERR_PARSE_ERROR;
  3167. tk = _get_token();
  3168. if (tk.type != TK_PARENTHESIS_CLOSE) {
  3169. _set_error("Expected ')' after expression");
  3170. return ERR_PARSE_ERROR;
  3171. }
  3172. BlockNode *block = alloc_node<BlockNode>();
  3173. block->parent_block = p_block;
  3174. cf->expressions.push_back(n);
  3175. cf->blocks.push_back(block);
  3176. p_block->statements.push_back(cf);
  3177. Error err = _parse_block(block, p_builtin_types, true, true, true);
  3178. if (err)
  3179. return err;
  3180. } else if (tk.type == TK_CF_FOR) {
  3181. //if () {}
  3182. tk = _get_token();
  3183. if (tk.type != TK_PARENTHESIS_OPEN) {
  3184. _set_error("Expected '(' after for");
  3185. return ERR_PARSE_ERROR;
  3186. }
  3187. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  3188. cf->flow_op = FLOW_OP_FOR;
  3189. BlockNode *init_block = alloc_node<BlockNode>();
  3190. init_block->parent_block = p_block;
  3191. init_block->single_statement = true;
  3192. cf->blocks.push_back(init_block);
  3193. if (_parse_block(init_block, p_builtin_types, true, false, false) != OK) {
  3194. return ERR_PARSE_ERROR;
  3195. }
  3196. Node *n = _parse_and_reduce_expression(init_block, p_builtin_types);
  3197. if (!n)
  3198. return ERR_PARSE_ERROR;
  3199. if (n->get_datatype() != TYPE_BOOL) {
  3200. _set_error("Middle expression is expected to be boolean.");
  3201. return ERR_PARSE_ERROR;
  3202. }
  3203. tk = _get_token();
  3204. if (tk.type != TK_SEMICOLON) {
  3205. _set_error("Expected ';' after middle expression");
  3206. return ERR_PARSE_ERROR;
  3207. }
  3208. cf->expressions.push_back(n);
  3209. n = _parse_and_reduce_expression(init_block, p_builtin_types);
  3210. if (!n)
  3211. return ERR_PARSE_ERROR;
  3212. cf->expressions.push_back(n);
  3213. tk = _get_token();
  3214. if (tk.type != TK_PARENTHESIS_CLOSE) {
  3215. _set_error("Expected ')' after third expression");
  3216. return ERR_PARSE_ERROR;
  3217. }
  3218. BlockNode *block = alloc_node<BlockNode>();
  3219. block->parent_block = init_block;
  3220. cf->blocks.push_back(block);
  3221. p_block->statements.push_back(cf);
  3222. Error err = _parse_block(block, p_builtin_types, true, true, true);
  3223. if (err)
  3224. return err;
  3225. } else if (tk.type == TK_CF_RETURN) {
  3226. //check return type
  3227. BlockNode *b = p_block;
  3228. while (b && !b->parent_function) {
  3229. b = b->parent_block;
  3230. }
  3231. if (!b) {
  3232. _set_error("Bug");
  3233. return ERR_BUG;
  3234. }
  3235. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  3236. flow->flow_op = FLOW_OP_RETURN;
  3237. pos = _get_tkpos();
  3238. tk = _get_token();
  3239. if (tk.type == TK_SEMICOLON) {
  3240. //all is good
  3241. if (b->parent_function->return_type != TYPE_VOID) {
  3242. _set_error("Expected return with expression of type '" + get_datatype_name(b->parent_function->return_type) + "'");
  3243. return ERR_PARSE_ERROR;
  3244. }
  3245. } else {
  3246. _set_tkpos(pos); //rollback, wants expression
  3247. Node *expr = _parse_and_reduce_expression(p_block, p_builtin_types);
  3248. if (!expr)
  3249. return ERR_PARSE_ERROR;
  3250. if (b->parent_function->return_type != expr->get_datatype()) {
  3251. _set_error("Expected return expression of type '" + get_datatype_name(b->parent_function->return_type) + "'");
  3252. return ERR_PARSE_ERROR;
  3253. }
  3254. tk = _get_token();
  3255. if (tk.type != TK_SEMICOLON) {
  3256. _set_error("Expected ';' after return expression");
  3257. return ERR_PARSE_ERROR;
  3258. }
  3259. flow->expressions.push_back(expr);
  3260. }
  3261. p_block->statements.push_back(flow);
  3262. } else if (tk.type == TK_CF_DISCARD) {
  3263. //check return type
  3264. BlockNode *b = p_block;
  3265. while (b && !b->parent_function) {
  3266. b = b->parent_block;
  3267. }
  3268. if (!b) {
  3269. _set_error("Bug");
  3270. return ERR_BUG;
  3271. }
  3272. if (!b->parent_function->can_discard) {
  3273. _set_error("Use of 'discard' is not allowed here.");
  3274. return ERR_PARSE_ERROR;
  3275. }
  3276. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  3277. flow->flow_op = FLOW_OP_DISCARD;
  3278. pos = _get_tkpos();
  3279. tk = _get_token();
  3280. if (tk.type != TK_SEMICOLON) {
  3281. //all is good
  3282. _set_error("Expected ';' after discard");
  3283. }
  3284. p_block->statements.push_back(flow);
  3285. } else if (tk.type == TK_CF_BREAK) {
  3286. if (!p_can_break) {
  3287. //all is good
  3288. _set_error("Breaking is not allowed here");
  3289. }
  3290. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  3291. flow->flow_op = FLOW_OP_BREAK;
  3292. pos = _get_tkpos();
  3293. tk = _get_token();
  3294. if (tk.type != TK_SEMICOLON) {
  3295. //all is good
  3296. _set_error("Expected ';' after break");
  3297. }
  3298. p_block->statements.push_back(flow);
  3299. } else if (tk.type == TK_CF_CONTINUE) {
  3300. if (!p_can_break) {
  3301. //all is good
  3302. _set_error("Continuing is not allowed here");
  3303. }
  3304. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  3305. flow->flow_op = FLOW_OP_CONTINUE;
  3306. pos = _get_tkpos();
  3307. tk = _get_token();
  3308. if (tk.type != TK_SEMICOLON) {
  3309. //all is good
  3310. _set_error("Expected ';' after continue");
  3311. }
  3312. p_block->statements.push_back(flow);
  3313. } else {
  3314. //nothng else, so expression
  3315. _set_tkpos(pos); //rollback
  3316. Node *expr = _parse_and_reduce_expression(p_block, p_builtin_types);
  3317. if (!expr)
  3318. return ERR_PARSE_ERROR;
  3319. p_block->statements.push_back(expr);
  3320. tk = _get_token();
  3321. if (tk.type != TK_SEMICOLON) {
  3322. _set_error("Expected ';' after statement");
  3323. return ERR_PARSE_ERROR;
  3324. }
  3325. }
  3326. if (p_just_one)
  3327. break;
  3328. }
  3329. return OK;
  3330. }
  3331. Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types) {
  3332. Token tk = _get_token();
  3333. if (tk.type != TK_SHADER_TYPE) {
  3334. _set_error("Expected 'shader_type' at the beginning of shader.");
  3335. return ERR_PARSE_ERROR;
  3336. }
  3337. tk = _get_token();
  3338. if (tk.type != TK_IDENTIFIER) {
  3339. _set_error("Expected identifier after 'shader_type', indicating type of shader.");
  3340. return ERR_PARSE_ERROR;
  3341. }
  3342. String shader_type_identifier;
  3343. shader_type_identifier = tk.text;
  3344. if (!p_shader_types.has(shader_type_identifier)) {
  3345. String valid;
  3346. for (Set<String>::Element *E = p_shader_types.front(); E; E = E->next()) {
  3347. if (valid != String()) {
  3348. valid += ", ";
  3349. }
  3350. valid += "'" + E->get() + "'";
  3351. }
  3352. _set_error("Invalid shader type, valid types are: " + valid);
  3353. return ERR_PARSE_ERROR;
  3354. }
  3355. tk = _get_token();
  3356. if (tk.type != TK_SEMICOLON) {
  3357. _set_error("Expected ';' after 'shader_type <type>'.");
  3358. }
  3359. tk = _get_token();
  3360. int texture_uniforms = 0;
  3361. int uniforms = 0;
  3362. while (tk.type != TK_EOF) {
  3363. switch (tk.type) {
  3364. case TK_RENDER_MODE: {
  3365. while (true) {
  3366. StringName mode;
  3367. _get_completable_identifier(NULL, COMPLETION_RENDER_MODE, mode);
  3368. if (mode == StringName()) {
  3369. _set_error("Expected identifier for render mode");
  3370. return ERR_PARSE_ERROR;
  3371. }
  3372. if (p_render_modes.find(mode) == -1) {
  3373. _set_error("Invalid render mode: '" + String(mode) + "'");
  3374. return ERR_PARSE_ERROR;
  3375. }
  3376. if (shader->render_modes.find(mode) != -1) {
  3377. _set_error("Duplicate render mode: '" + String(mode) + "'");
  3378. return ERR_PARSE_ERROR;
  3379. }
  3380. shader->render_modes.push_back(mode);
  3381. tk = _get_token();
  3382. if (tk.type == TK_COMMA) {
  3383. //all good, do nothing
  3384. } else if (tk.type == TK_SEMICOLON) {
  3385. break; //done
  3386. } else {
  3387. _set_error("Unexpected token: " + get_token_text(tk));
  3388. return ERR_PARSE_ERROR;
  3389. }
  3390. }
  3391. } break;
  3392. case TK_UNIFORM:
  3393. case TK_VARYING: {
  3394. bool uniform = tk.type == TK_UNIFORM;
  3395. DataPrecision precision = PRECISION_DEFAULT;
  3396. DataInterpolation interpolation = INTERPOLATION_SMOOTH;
  3397. DataType type;
  3398. StringName name;
  3399. tk = _get_token();
  3400. if (is_token_interpolation(tk.type)) {
  3401. interpolation = get_token_interpolation(tk.type);
  3402. tk = _get_token();
  3403. }
  3404. if (is_token_precision(tk.type)) {
  3405. precision = get_token_precision(tk.type);
  3406. tk = _get_token();
  3407. }
  3408. if (!is_token_datatype(tk.type)) {
  3409. _set_error("Expected datatype. ");
  3410. return ERR_PARSE_ERROR;
  3411. }
  3412. type = get_token_datatype(tk.type);
  3413. if (type == TYPE_VOID) {
  3414. _set_error("void datatype not allowed here");
  3415. return ERR_PARSE_ERROR;
  3416. }
  3417. if (!uniform && (type < TYPE_FLOAT || type > TYPE_MAT4)) {
  3418. _set_error("Invalid type for varying, only float,vec2,vec3,vec4,mat2,mat3,mat4 allowed.");
  3419. return ERR_PARSE_ERROR;
  3420. }
  3421. tk = _get_token();
  3422. if (tk.type != TK_IDENTIFIER) {
  3423. _set_error("Expected identifier!");
  3424. return ERR_PARSE_ERROR;
  3425. }
  3426. name = tk.text;
  3427. if (_find_identifier(NULL, Map<StringName, BuiltInInfo>(), name)) {
  3428. _set_error("Redefinition of '" + String(name) + "'");
  3429. return ERR_PARSE_ERROR;
  3430. }
  3431. if (uniform) {
  3432. ShaderNode::Uniform uniform;
  3433. if (is_sampler_type(type)) {
  3434. uniform.texture_order = texture_uniforms++;
  3435. uniform.order = -1;
  3436. } else {
  3437. uniform.texture_order = -1;
  3438. uniform.order = uniforms++;
  3439. }
  3440. uniform.type = type;
  3441. uniform.precission = precision;
  3442. //todo parse default value
  3443. tk = _get_token();
  3444. if (tk.type == TK_COLON) {
  3445. //hint
  3446. tk = _get_token();
  3447. if (tk.type == TK_HINT_WHITE_TEXTURE) {
  3448. uniform.hint = ShaderNode::Uniform::HINT_WHITE;
  3449. } else if (tk.type == TK_HINT_BLACK_TEXTURE) {
  3450. uniform.hint = ShaderNode::Uniform::HINT_BLACK;
  3451. } else if (tk.type == TK_HINT_NORMAL_TEXTURE) {
  3452. uniform.hint = ShaderNode::Uniform::HINT_NORMAL;
  3453. } else if (tk.type == TK_HINT_ANISO_TEXTURE) {
  3454. uniform.hint = ShaderNode::Uniform::HINT_ANISO;
  3455. } else if (tk.type == TK_HINT_ALBEDO_TEXTURE) {
  3456. uniform.hint = ShaderNode::Uniform::HINT_ALBEDO;
  3457. } else if (tk.type == TK_HINT_BLACK_ALBEDO_TEXTURE) {
  3458. uniform.hint = ShaderNode::Uniform::HINT_BLACK_ALBEDO;
  3459. } else if (tk.type == TK_HINT_COLOR) {
  3460. if (type != TYPE_VEC4) {
  3461. _set_error("Color hint is for vec4 only");
  3462. return ERR_PARSE_ERROR;
  3463. }
  3464. uniform.hint = ShaderNode::Uniform::HINT_COLOR;
  3465. } else if (tk.type == TK_HINT_RANGE) {
  3466. uniform.hint = ShaderNode::Uniform::HINT_RANGE;
  3467. if (type != TYPE_FLOAT && type != TYPE_INT) {
  3468. _set_error("Range hint is for float and int only");
  3469. return ERR_PARSE_ERROR;
  3470. }
  3471. tk = _get_token();
  3472. if (tk.type != TK_PARENTHESIS_OPEN) {
  3473. _set_error("Expected '(' after hint_range");
  3474. return ERR_PARSE_ERROR;
  3475. }
  3476. tk = _get_token();
  3477. float sign = 1.0;
  3478. if (tk.type == TK_OP_SUB) {
  3479. sign = -1.0;
  3480. tk = _get_token();
  3481. }
  3482. if (tk.type != TK_REAL_CONSTANT && tk.type != TK_INT_CONSTANT) {
  3483. _set_error("Expected integer constant");
  3484. return ERR_PARSE_ERROR;
  3485. }
  3486. uniform.hint_range[0] = tk.constant;
  3487. uniform.hint_range[0] *= sign;
  3488. tk = _get_token();
  3489. if (tk.type != TK_COMMA) {
  3490. _set_error("Expected ',' after integer constant");
  3491. return ERR_PARSE_ERROR;
  3492. }
  3493. tk = _get_token();
  3494. sign = 1.0;
  3495. if (tk.type == TK_OP_SUB) {
  3496. sign = -1.0;
  3497. tk = _get_token();
  3498. }
  3499. if (tk.type != TK_REAL_CONSTANT && tk.type != TK_INT_CONSTANT) {
  3500. _set_error("Expected integer constant after ','");
  3501. return ERR_PARSE_ERROR;
  3502. }
  3503. uniform.hint_range[1] = tk.constant;
  3504. uniform.hint_range[1] *= sign;
  3505. tk = _get_token();
  3506. if (tk.type == TK_COMMA) {
  3507. tk = _get_token();
  3508. if (tk.type != TK_REAL_CONSTANT && tk.type != TK_INT_CONSTANT) {
  3509. _set_error("Expected integer constant after ','");
  3510. return ERR_PARSE_ERROR;
  3511. }
  3512. uniform.hint_range[2] = tk.constant;
  3513. tk = _get_token();
  3514. } else {
  3515. if (type == TYPE_INT) {
  3516. uniform.hint_range[2] = 1;
  3517. } else {
  3518. uniform.hint_range[2] = 0.001;
  3519. }
  3520. }
  3521. if (tk.type != TK_PARENTHESIS_CLOSE) {
  3522. _set_error("Expected ','");
  3523. return ERR_PARSE_ERROR;
  3524. }
  3525. } else {
  3526. _set_error("Expected valid type hint after ':'.");
  3527. }
  3528. if (uniform.hint != ShaderNode::Uniform::HINT_RANGE && uniform.hint != ShaderNode::Uniform::HINT_NONE && uniform.hint != ShaderNode::Uniform::HINT_COLOR && type <= TYPE_MAT4) {
  3529. _set_error("This hint is only for sampler types");
  3530. return ERR_PARSE_ERROR;
  3531. }
  3532. tk = _get_token();
  3533. }
  3534. if (tk.type == TK_OP_ASSIGN) {
  3535. Node *expr = _parse_and_reduce_expression(NULL, Map<StringName, BuiltInInfo>());
  3536. if (!expr)
  3537. return ERR_PARSE_ERROR;
  3538. if (expr->type != Node::TYPE_CONSTANT) {
  3539. _set_error("Expected constant expression after '='");
  3540. return ERR_PARSE_ERROR;
  3541. }
  3542. ConstantNode *cn = static_cast<ConstantNode *>(expr);
  3543. uniform.default_value.resize(cn->values.size());
  3544. if (!convert_constant(cn, uniform.type, uniform.default_value.ptrw())) {
  3545. _set_error("Can't convert constant to " + get_datatype_name(uniform.type));
  3546. return ERR_PARSE_ERROR;
  3547. }
  3548. tk = _get_token();
  3549. }
  3550. shader->uniforms[name] = uniform;
  3551. if (tk.type != TK_SEMICOLON) {
  3552. _set_error("Expected ';'");
  3553. return ERR_PARSE_ERROR;
  3554. }
  3555. } else {
  3556. ShaderNode::Varying varying;
  3557. varying.type = type;
  3558. varying.precission = precision;
  3559. varying.interpolation = interpolation;
  3560. shader->varyings[name] = varying;
  3561. tk = _get_token();
  3562. if (tk.type != TK_SEMICOLON) {
  3563. _set_error("Expected ';'");
  3564. return ERR_PARSE_ERROR;
  3565. }
  3566. }
  3567. } break;
  3568. default: {
  3569. //function
  3570. DataPrecision precision = PRECISION_DEFAULT;
  3571. DataType type;
  3572. StringName name;
  3573. if (is_token_precision(tk.type)) {
  3574. precision = get_token_precision(tk.type);
  3575. tk = _get_token();
  3576. }
  3577. if (!is_token_datatype(tk.type)) {
  3578. _set_error("Expected function, uniform or varying ");
  3579. return ERR_PARSE_ERROR;
  3580. }
  3581. if (!is_token_variable_datatype(tk.type)) {
  3582. _set_error("Invalid data type for function return (samplers not allowed)");
  3583. return ERR_PARSE_ERROR;
  3584. }
  3585. type = get_token_datatype(tk.type);
  3586. _get_completable_identifier(NULL, COMPLETION_MAIN_FUNCTION, name);
  3587. if (name == StringName()) {
  3588. _set_error("Expected function name after datatype");
  3589. return ERR_PARSE_ERROR;
  3590. }
  3591. if (_find_identifier(NULL, Map<StringName, BuiltInInfo>(), name)) {
  3592. _set_error("Redefinition of '" + String(name) + "'");
  3593. return ERR_PARSE_ERROR;
  3594. }
  3595. tk = _get_token();
  3596. if (tk.type != TK_PARENTHESIS_OPEN) {
  3597. _set_error("Expected '(' after identifier");
  3598. return ERR_PARSE_ERROR;
  3599. }
  3600. Map<StringName, BuiltInInfo> builtin_types;
  3601. if (p_functions.has(name)) {
  3602. builtin_types = p_functions[name].built_ins;
  3603. }
  3604. ShaderNode::Function function;
  3605. function.callable = !p_functions.has(name);
  3606. function.name = name;
  3607. FunctionNode *func_node = alloc_node<FunctionNode>();
  3608. function.function = func_node;
  3609. shader->functions.push_back(function);
  3610. func_node->name = name;
  3611. func_node->return_type = type;
  3612. func_node->return_precision = precision;
  3613. if (p_functions.has(name)) {
  3614. func_node->can_discard = p_functions[name].can_discard;
  3615. }
  3616. func_node->body = alloc_node<BlockNode>();
  3617. func_node->body->parent_function = func_node;
  3618. tk = _get_token();
  3619. while (true) {
  3620. if (tk.type == TK_PARENTHESIS_CLOSE) {
  3621. break;
  3622. }
  3623. ArgumentQualifier qualifier = ARGUMENT_QUALIFIER_IN;
  3624. if (tk.type == TK_ARG_IN) {
  3625. qualifier = ARGUMENT_QUALIFIER_IN;
  3626. tk = _get_token();
  3627. } else if (tk.type == TK_ARG_OUT) {
  3628. qualifier = ARGUMENT_QUALIFIER_OUT;
  3629. tk = _get_token();
  3630. } else if (tk.type == TK_ARG_INOUT) {
  3631. qualifier = ARGUMENT_QUALIFIER_INOUT;
  3632. tk = _get_token();
  3633. }
  3634. DataType ptype;
  3635. StringName pname;
  3636. DataPrecision pprecision = PRECISION_DEFAULT;
  3637. if (is_token_precision(tk.type)) {
  3638. pprecision = get_token_precision(tk.type);
  3639. tk = _get_token();
  3640. }
  3641. if (!is_token_datatype(tk.type)) {
  3642. _set_error("Expected a valid datatype for argument");
  3643. return ERR_PARSE_ERROR;
  3644. }
  3645. ptype = get_token_datatype(tk.type);
  3646. if (ptype == TYPE_VOID) {
  3647. _set_error("void not allowed in argument");
  3648. return ERR_PARSE_ERROR;
  3649. }
  3650. tk = _get_token();
  3651. if (tk.type != TK_IDENTIFIER) {
  3652. _set_error("Expected identifier for argument name");
  3653. return ERR_PARSE_ERROR;
  3654. }
  3655. pname = tk.text;
  3656. if (_find_identifier(func_node->body, builtin_types, pname)) {
  3657. _set_error("Redefinition of '" + String(pname) + "'");
  3658. return ERR_PARSE_ERROR;
  3659. }
  3660. FunctionNode::Argument arg;
  3661. arg.type = ptype;
  3662. arg.name = pname;
  3663. arg.precision = pprecision;
  3664. arg.qualifier = qualifier;
  3665. func_node->arguments.push_back(arg);
  3666. tk = _get_token();
  3667. if (tk.type == TK_COMMA) {
  3668. tk = _get_token();
  3669. //do none and go on
  3670. } else if (tk.type != TK_PARENTHESIS_CLOSE) {
  3671. _set_error("Expected ',' or ')' after identifier");
  3672. return ERR_PARSE_ERROR;
  3673. }
  3674. }
  3675. if (p_functions.has(name)) {
  3676. //if one of the core functions, make sure they are of the correct form
  3677. if (func_node->arguments.size() > 0) {
  3678. _set_error("Function '" + String(name) + "' expects no arguments.");
  3679. return ERR_PARSE_ERROR;
  3680. }
  3681. if (func_node->return_type != TYPE_VOID) {
  3682. _set_error("Function '" + String(name) + "' must be of void return type.");
  3683. return ERR_PARSE_ERROR;
  3684. }
  3685. }
  3686. //all good let's parse inside the function!
  3687. tk = _get_token();
  3688. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  3689. _set_error("Expected '{' to begin function");
  3690. return ERR_PARSE_ERROR;
  3691. }
  3692. current_function = name;
  3693. Error err = _parse_block(func_node->body, builtin_types);
  3694. if (err)
  3695. return err;
  3696. current_function = StringName();
  3697. }
  3698. }
  3699. tk = _get_token();
  3700. }
  3701. return OK;
  3702. }
  3703. // skips over whitespace and /* */ and // comments
  3704. static int _get_first_ident_pos(const String &p_code) {
  3705. int idx = 0;
  3706. #define GETCHAR(m_idx) (((idx + m_idx) < p_code.length()) ? p_code[idx + m_idx] : CharType(0))
  3707. while (true) {
  3708. if (GETCHAR(0) == '/' && GETCHAR(1) == '/') {
  3709. idx += 2;
  3710. while (true) {
  3711. if (GETCHAR(0) == 0) return 0;
  3712. if (GETCHAR(0) == '\n') {
  3713. idx++;
  3714. break; // loop
  3715. }
  3716. idx++;
  3717. }
  3718. } else if (GETCHAR(0) == '/' && GETCHAR(1) == '*') {
  3719. idx += 2;
  3720. while (true) {
  3721. if (GETCHAR(0) == 0) return 0;
  3722. if (GETCHAR(0) == '*' && GETCHAR(1) == '/') {
  3723. idx += 2;
  3724. break; // loop
  3725. }
  3726. idx++;
  3727. }
  3728. } else {
  3729. switch (GETCHAR(0)) {
  3730. case ' ':
  3731. case '\t':
  3732. case '\r':
  3733. case '\n': {
  3734. idx++;
  3735. } break; // switch
  3736. default:
  3737. return idx;
  3738. }
  3739. }
  3740. }
  3741. #undef GETCHAR
  3742. }
  3743. String ShaderLanguage::get_shader_type(const String &p_code) {
  3744. bool reading_type = false;
  3745. String cur_identifier;
  3746. for (int i = _get_first_ident_pos(p_code); i < p_code.length(); i++) {
  3747. if (p_code[i] == ';') {
  3748. break;
  3749. } else if (p_code[i] <= 32) {
  3750. if (cur_identifier != String()) {
  3751. if (!reading_type) {
  3752. if (cur_identifier != "shader_type") {
  3753. return String();
  3754. }
  3755. reading_type = true;
  3756. cur_identifier = String();
  3757. } else {
  3758. return cur_identifier;
  3759. }
  3760. }
  3761. } else {
  3762. cur_identifier += String::chr(p_code[i]);
  3763. }
  3764. }
  3765. if (reading_type)
  3766. return cur_identifier;
  3767. return String();
  3768. }
  3769. Error ShaderLanguage::compile(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types) {
  3770. clear();
  3771. code = p_code;
  3772. nodes = NULL;
  3773. shader = alloc_node<ShaderNode>();
  3774. Error err = _parse_shader(p_functions, p_render_modes, p_shader_types);
  3775. if (err != OK) {
  3776. return err;
  3777. }
  3778. return OK;
  3779. }
  3780. Error ShaderLanguage::complete(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types, List<String> *r_options, String &r_call_hint) {
  3781. clear();
  3782. code = p_code;
  3783. nodes = NULL;
  3784. shader = alloc_node<ShaderNode>();
  3785. Error err = _parse_shader(p_functions, p_render_modes, p_shader_types);
  3786. if (err != OK)
  3787. ERR_PRINT("Failed to parse shader");
  3788. switch (completion_type) {
  3789. case COMPLETION_NONE: {
  3790. //do nothing
  3791. return OK;
  3792. } break;
  3793. case COMPLETION_RENDER_MODE: {
  3794. for (int i = 0; i < p_render_modes.size(); i++) {
  3795. r_options->push_back(p_render_modes[i]);
  3796. }
  3797. return OK;
  3798. } break;
  3799. case COMPLETION_MAIN_FUNCTION: {
  3800. for (const Map<StringName, FunctionInfo>::Element *E = p_functions.front(); E; E = E->next()) {
  3801. r_options->push_back(E->key());
  3802. }
  3803. return OK;
  3804. } break;
  3805. case COMPLETION_IDENTIFIER:
  3806. case COMPLETION_FUNCTION_CALL: {
  3807. bool comp_ident = completion_type == COMPLETION_IDENTIFIER;
  3808. Set<String> matches;
  3809. StringName skip_function;
  3810. BlockNode *block = completion_block;
  3811. while (block) {
  3812. if (comp_ident) {
  3813. for (const Map<StringName, BlockNode::Variable>::Element *E = block->variables.front(); E; E = E->next()) {
  3814. if (E->get().line < completion_line) {
  3815. matches.insert(E->key());
  3816. }
  3817. }
  3818. }
  3819. if (block->parent_function) {
  3820. if (comp_ident) {
  3821. for (int i = 0; i < block->parent_function->arguments.size(); i++) {
  3822. matches.insert(block->parent_function->arguments[i].name);
  3823. }
  3824. }
  3825. skip_function = block->parent_function->name;
  3826. }
  3827. block = block->parent_block;
  3828. }
  3829. if (comp_ident && skip_function != StringName() && p_functions.has(skip_function)) {
  3830. for (Map<StringName, BuiltInInfo>::Element *E = p_functions[skip_function].built_ins.front(); E; E = E->next()) {
  3831. matches.insert(E->key());
  3832. }
  3833. }
  3834. if (comp_ident) {
  3835. for (const Map<StringName, ShaderNode::Varying>::Element *E = shader->varyings.front(); E; E = E->next()) {
  3836. matches.insert(E->key());
  3837. }
  3838. for (const Map<StringName, ShaderNode::Uniform>::Element *E = shader->uniforms.front(); E; E = E->next()) {
  3839. matches.insert(E->key());
  3840. }
  3841. }
  3842. for (int i = 0; i < shader->functions.size(); i++) {
  3843. if (!shader->functions[i].callable || shader->functions[i].name == skip_function)
  3844. continue;
  3845. matches.insert(String(shader->functions[i].name) + "(");
  3846. }
  3847. int idx = 0;
  3848. while (builtin_func_defs[idx].name) {
  3849. matches.insert(String(builtin_func_defs[idx].name) + "(");
  3850. idx++;
  3851. }
  3852. for (Set<String>::Element *E = matches.front(); E; E = E->next()) {
  3853. r_options->push_back(E->get());
  3854. }
  3855. return OK;
  3856. } break;
  3857. case COMPLETION_CALL_ARGUMENTS: {
  3858. for (int i = 0; i < shader->functions.size(); i++) {
  3859. if (!shader->functions[i].callable)
  3860. continue;
  3861. if (shader->functions[i].name == completion_function) {
  3862. String calltip;
  3863. calltip += get_datatype_name(shader->functions[i].function->return_type);
  3864. calltip += " ";
  3865. calltip += shader->functions[i].name;
  3866. calltip += "(";
  3867. for (int j = 0; j < shader->functions[i].function->arguments.size(); j++) {
  3868. if (j > 0)
  3869. calltip += ", ";
  3870. else
  3871. calltip += " ";
  3872. if (j == completion_argument) {
  3873. calltip += CharType(0xFFFF);
  3874. }
  3875. calltip += get_datatype_name(shader->functions[i].function->arguments[j].type);
  3876. calltip += " ";
  3877. calltip += shader->functions[i].function->arguments[j].name;
  3878. if (j == completion_argument) {
  3879. calltip += CharType(0xFFFF);
  3880. }
  3881. }
  3882. if (shader->functions[i].function->arguments.size())
  3883. calltip += " ";
  3884. calltip += ")";
  3885. r_call_hint = calltip;
  3886. return OK;
  3887. }
  3888. }
  3889. int idx = 0;
  3890. String calltip;
  3891. while (builtin_func_defs[idx].name) {
  3892. if (completion_function == builtin_func_defs[idx].name) {
  3893. if (calltip.length())
  3894. calltip += "\n";
  3895. calltip += get_datatype_name(builtin_func_defs[idx].rettype);
  3896. calltip += " ";
  3897. calltip += builtin_func_defs[idx].name;
  3898. calltip += "(";
  3899. bool found_arg = false;
  3900. for (int i = 0; i < 4; i++) {
  3901. if (builtin_func_defs[idx].args[i] == TYPE_VOID)
  3902. break;
  3903. if (i > 0)
  3904. calltip += ", ";
  3905. else
  3906. calltip += " ";
  3907. if (i == completion_argument) {
  3908. calltip += CharType(0xFFFF);
  3909. }
  3910. calltip += get_datatype_name(builtin_func_defs[idx].args[i]);
  3911. if (i == completion_argument) {
  3912. calltip += CharType(0xFFFF);
  3913. }
  3914. found_arg = true;
  3915. }
  3916. if (found_arg)
  3917. calltip += " ";
  3918. calltip += ")";
  3919. }
  3920. idx++;
  3921. }
  3922. r_call_hint = calltip;
  3923. return OK;
  3924. } break;
  3925. case COMPLETION_INDEX: {
  3926. const char colv[4] = { 'r', 'g', 'b', 'a' };
  3927. const char coordv[4] = { 'x', 'y', 'z', 'w' };
  3928. int limit = 0;
  3929. switch (completion_base) {
  3930. case TYPE_BVEC2:
  3931. case TYPE_IVEC2:
  3932. case TYPE_UVEC2:
  3933. case TYPE_VEC2: {
  3934. limit = 2;
  3935. } break;
  3936. case TYPE_BVEC3:
  3937. case TYPE_IVEC3:
  3938. case TYPE_UVEC3:
  3939. case TYPE_VEC3: {
  3940. limit = 3;
  3941. } break;
  3942. case TYPE_BVEC4:
  3943. case TYPE_IVEC4:
  3944. case TYPE_UVEC4:
  3945. case TYPE_VEC4: {
  3946. limit = 4;
  3947. } break;
  3948. case TYPE_MAT2: limit = 2; break;
  3949. case TYPE_MAT3: limit = 3; break;
  3950. case TYPE_MAT4: limit = 4; break;
  3951. default: {}
  3952. }
  3953. for (int i = 0; i < limit; i++) {
  3954. r_options->push_back(String::chr(colv[i]));
  3955. r_options->push_back(String::chr(coordv[i]));
  3956. }
  3957. } break;
  3958. }
  3959. return ERR_PARSE_ERROR;
  3960. }
  3961. String ShaderLanguage::get_error_text() {
  3962. return error_str;
  3963. }
  3964. int ShaderLanguage::get_error_line() {
  3965. return error_line;
  3966. }
  3967. ShaderLanguage::ShaderNode *ShaderLanguage::get_shader() {
  3968. return shader;
  3969. }
  3970. ShaderLanguage::ShaderLanguage() {
  3971. nodes = NULL;
  3972. }
  3973. ShaderLanguage::~ShaderLanguage() {
  3974. clear();
  3975. }