shader_language.cpp 299 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427
  1. /*************************************************************************/
  2. /* shader_language.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/string/print_string.h"
  33. #include "servers/rendering_server.h"
  34. #define HAS_WARNING(flag) (warning_flags & flag)
  35. static bool _is_text_char(char32_t c) {
  36. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
  37. }
  38. static bool _is_number(char32_t c) {
  39. return (c >= '0' && c <= '9');
  40. }
  41. static bool _is_hex(char32_t c) {
  42. return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
  43. }
  44. String ShaderLanguage::get_operator_text(Operator p_op) {
  45. static const char *op_names[OP_MAX] = { "==",
  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. "--",
  83. "()",
  84. "construct",
  85. "index" };
  86. return op_names[p_op];
  87. }
  88. const char *ShaderLanguage::token_names[TK_MAX] = {
  89. "EMPTY",
  90. "IDENTIFIER",
  91. "TRUE",
  92. "FALSE",
  93. "REAL_CONSTANT",
  94. "INT_CONSTANT",
  95. "TYPE_VOID",
  96. "TYPE_BOOL",
  97. "TYPE_BVEC2",
  98. "TYPE_BVEC3",
  99. "TYPE_BVEC4",
  100. "TYPE_INT",
  101. "TYPE_IVEC2",
  102. "TYPE_IVEC3",
  103. "TYPE_IVEC4",
  104. "TYPE_UINT",
  105. "TYPE_UVEC2",
  106. "TYPE_UVEC3",
  107. "TYPE_UVEC4",
  108. "TYPE_FLOAT",
  109. "TYPE_VEC2",
  110. "TYPE_VEC3",
  111. "TYPE_VEC4",
  112. "TYPE_MAT2",
  113. "TYPE_MAT3",
  114. "TYPE_MAT4",
  115. "TYPE_SAMPLER2D",
  116. "TYPE_ISAMPLER2D",
  117. "TYPE_USAMPLER2D",
  118. "TYPE_SAMPLER2DARRAY",
  119. "TYPE_ISAMPLER2DARRAY",
  120. "TYPE_USAMPLER2DARRAY",
  121. "TYPE_SAMPLER3D",
  122. "TYPE_ISAMPLER3D",
  123. "TYPE_USAMPLER3D",
  124. "TYPE_SAMPLERCUBE",
  125. "TYPE_SAMPLERCUBEARRAY",
  126. "INTERPOLATION_FLAT",
  127. "INTERPOLATION_SMOOTH",
  128. "CONST",
  129. "PRECISION_LOW",
  130. "PRECISION_MID",
  131. "PRECISION_HIGH",
  132. "OP_EQUAL",
  133. "OP_NOT_EQUAL",
  134. "OP_LESS",
  135. "OP_LESS_EQUAL",
  136. "OP_GREATER",
  137. "OP_GREATER_EQUAL",
  138. "OP_AND",
  139. "OP_OR",
  140. "OP_NOT",
  141. "OP_ADD",
  142. "OP_SUB",
  143. "OP_MUL",
  144. "OP_DIV",
  145. "OP_MOD",
  146. "OP_SHIFT_LEFT",
  147. "OP_SHIFT_RIGHT",
  148. "OP_ASSIGN",
  149. "OP_ASSIGN_ADD",
  150. "OP_ASSIGN_SUB",
  151. "OP_ASSIGN_MUL",
  152. "OP_ASSIGN_DIV",
  153. "OP_ASSIGN_MOD",
  154. "OP_ASSIGN_SHIFT_LEFT",
  155. "OP_ASSIGN_SHIFT_RIGHT",
  156. "OP_ASSIGN_BIT_AND",
  157. "OP_ASSIGN_BIT_OR",
  158. "OP_ASSIGN_BIT_XOR",
  159. "OP_BIT_AND",
  160. "OP_BIT_OR",
  161. "OP_BIT_XOR",
  162. "OP_BIT_INVERT",
  163. "OP_INCREMENT",
  164. "OP_DECREMENT",
  165. "CF_IF",
  166. "CF_ELSE",
  167. "CF_FOR",
  168. "CF_WHILE",
  169. "CF_DO",
  170. "CF_SWITCH",
  171. "CF_CASE",
  172. "CF_BREAK",
  173. "CF_CONTINUE",
  174. "CF_RETURN",
  175. "CF_DISCARD",
  176. "BRACKET_OPEN",
  177. "BRACKET_CLOSE",
  178. "CURLY_BRACKET_OPEN",
  179. "CURLY_BRACKET_CLOSE",
  180. "PARENTHESIS_OPEN",
  181. "PARENTHESIS_CLOSE",
  182. "QUESTION",
  183. "COMMA",
  184. "COLON",
  185. "SEMICOLON",
  186. "PERIOD",
  187. "UNIFORM",
  188. "INSTANCE",
  189. "GLOBAL",
  190. "VARYING",
  191. "IN",
  192. "OUT",
  193. "INOUT",
  194. "RENDER_MODE",
  195. "HINT_WHITE_TEXTURE",
  196. "HINT_BLACK_TEXTURE",
  197. "HINT_NORMAL_TEXTURE",
  198. "HINT_ANISO_TEXTURE",
  199. "HINT_ALBEDO_TEXTURE",
  200. "HINT_BLACK_ALBEDO_TEXTURE",
  201. "HINT_COLOR",
  202. "HINT_RANGE",
  203. "HINT_INSTANCE_INDEX",
  204. "FILTER_NEAREST",
  205. "FILTER_LINEAR",
  206. "FILTER_NEAREST_MIPMAP",
  207. "FILTER_LINEAR_MIPMAP",
  208. "FILTER_NEAREST_MIPMAP_ANISO",
  209. "FILTER_LINEAR_MIPMAP_ANISO",
  210. "REPEAT_ENABLE",
  211. "REPEAT_DISABLE",
  212. "SHADER_TYPE",
  213. "CURSOR",
  214. "ERROR",
  215. "EOF",
  216. };
  217. String ShaderLanguage::get_token_text(Token p_token) {
  218. String name = token_names[p_token.type];
  219. if (p_token.type == TK_INT_CONSTANT || p_token.type == TK_FLOAT_CONSTANT) {
  220. name += "(" + rtos(p_token.constant) + ")";
  221. } else if (p_token.type == TK_IDENTIFIER) {
  222. name += "(" + String(p_token.text) + ")";
  223. } else if (p_token.type == TK_ERROR) {
  224. name += "(" + String(p_token.text) + ")";
  225. }
  226. return name;
  227. }
  228. ShaderLanguage::Token ShaderLanguage::_make_token(TokenType p_type, const StringName &p_text) {
  229. Token tk;
  230. tk.type = p_type;
  231. tk.text = p_text;
  232. tk.line = tk_line;
  233. if (tk.type == TK_ERROR) {
  234. _set_error(p_text);
  235. }
  236. return tk;
  237. }
  238. const ShaderLanguage::KeyWord ShaderLanguage::keyword_list[] = {
  239. { TK_TRUE, "true" },
  240. { TK_FALSE, "false" },
  241. { TK_TYPE_VOID, "void" },
  242. { TK_TYPE_BOOL, "bool" },
  243. { TK_TYPE_BVEC2, "bvec2" },
  244. { TK_TYPE_BVEC3, "bvec3" },
  245. { TK_TYPE_BVEC4, "bvec4" },
  246. { TK_TYPE_INT, "int" },
  247. { TK_TYPE_IVEC2, "ivec2" },
  248. { TK_TYPE_IVEC3, "ivec3" },
  249. { TK_TYPE_IVEC4, "ivec4" },
  250. { TK_TYPE_UINT, "uint" },
  251. { TK_TYPE_UVEC2, "uvec2" },
  252. { TK_TYPE_UVEC3, "uvec3" },
  253. { TK_TYPE_UVEC4, "uvec4" },
  254. { TK_TYPE_FLOAT, "float" },
  255. { TK_TYPE_VEC2, "vec2" },
  256. { TK_TYPE_VEC3, "vec3" },
  257. { TK_TYPE_VEC4, "vec4" },
  258. { TK_TYPE_MAT2, "mat2" },
  259. { TK_TYPE_MAT3, "mat3" },
  260. { TK_TYPE_MAT4, "mat4" },
  261. { TK_TYPE_SAMPLER2D, "sampler2D" },
  262. { TK_TYPE_ISAMPLER2D, "isampler2D" },
  263. { TK_TYPE_USAMPLER2D, "usampler2D" },
  264. { TK_TYPE_SAMPLER2DARRAY, "sampler2DArray" },
  265. { TK_TYPE_ISAMPLER2DARRAY, "isampler2DArray" },
  266. { TK_TYPE_USAMPLER2DARRAY, "usampler2DArray" },
  267. { TK_TYPE_SAMPLER3D, "sampler3D" },
  268. { TK_TYPE_ISAMPLER3D, "isampler3D" },
  269. { TK_TYPE_USAMPLER3D, "usampler3D" },
  270. { TK_TYPE_SAMPLERCUBE, "samplerCube" },
  271. { TK_TYPE_SAMPLERCUBEARRAY, "samplerCubeArray" },
  272. { TK_INTERPOLATION_FLAT, "flat" },
  273. { TK_INTERPOLATION_SMOOTH, "smooth" },
  274. { TK_CONST, "const" },
  275. { TK_STRUCT, "struct" },
  276. { TK_PRECISION_LOW, "lowp" },
  277. { TK_PRECISION_MID, "mediump" },
  278. { TK_PRECISION_HIGH, "highp" },
  279. { TK_CF_IF, "if" },
  280. { TK_CF_ELSE, "else" },
  281. { TK_CF_FOR, "for" },
  282. { TK_CF_WHILE, "while" },
  283. { TK_CF_DO, "do" },
  284. { TK_CF_SWITCH, "switch" },
  285. { TK_CF_CASE, "case" },
  286. { TK_CF_DEFAULT, "default" },
  287. { TK_CF_BREAK, "break" },
  288. { TK_CF_CONTINUE, "continue" },
  289. { TK_CF_RETURN, "return" },
  290. { TK_CF_DISCARD, "discard" },
  291. { TK_UNIFORM, "uniform" },
  292. { TK_INSTANCE, "instance" },
  293. { TK_GLOBAL, "global" },
  294. { TK_VARYING, "varying" },
  295. { TK_ARG_IN, "in" },
  296. { TK_ARG_OUT, "out" },
  297. { TK_ARG_INOUT, "inout" },
  298. { TK_RENDER_MODE, "render_mode" },
  299. { TK_HINT_WHITE_TEXTURE, "hint_white" },
  300. { TK_HINT_BLACK_TEXTURE, "hint_black" },
  301. { TK_HINT_NORMAL_TEXTURE, "hint_normal" },
  302. { TK_HINT_ROUGHNESS_NORMAL_TEXTURE, "hint_roughness_normal" },
  303. { TK_HINT_ROUGHNESS_R, "hint_roughness_r" },
  304. { TK_HINT_ROUGHNESS_G, "hint_roughness_g" },
  305. { TK_HINT_ROUGHNESS_B, "hint_roughness_b" },
  306. { TK_HINT_ROUGHNESS_A, "hint_roughness_a" },
  307. { TK_HINT_ROUGHNESS_GRAY, "hint_roughness_gray" },
  308. { TK_HINT_ANISO_TEXTURE, "hint_aniso" },
  309. { TK_HINT_ALBEDO_TEXTURE, "hint_albedo" },
  310. { TK_HINT_BLACK_ALBEDO_TEXTURE, "hint_black_albedo" },
  311. { TK_HINT_COLOR, "hint_color" },
  312. { TK_HINT_RANGE, "hint_range" },
  313. { TK_HINT_INSTANCE_INDEX, "instance_index" },
  314. { TK_FILTER_NEAREST, "filter_nearest" },
  315. { TK_FILTER_LINEAR, "filter_linear" },
  316. { TK_FILTER_NEAREST_MIPMAP, "filter_nearest_mipmap" },
  317. { TK_FILTER_LINEAR_MIPMAP, "filter_linear_mipmap" },
  318. { TK_FILTER_NEAREST_MIPMAP_ANISO, "filter_nearest_mipmap_aniso" },
  319. { TK_FILTER_LINEAR_MIPMAP_ANISO, "filter_linear_mipmap_aniso" },
  320. { TK_REPEAT_ENABLE, "repeat_enable" },
  321. { TK_REPEAT_DISABLE, "repeat_disable" },
  322. { TK_SHADER_TYPE, "shader_type" },
  323. { TK_ERROR, nullptr }
  324. };
  325. ShaderLanguage::Token ShaderLanguage::_get_token() {
  326. #define GETCHAR(m_idx) (((char_idx + m_idx) < code.length()) ? code[char_idx + m_idx] : char32_t(0))
  327. while (true) {
  328. char_idx++;
  329. switch (GETCHAR(-1)) {
  330. case 0:
  331. return _make_token(TK_EOF);
  332. case 0xFFFF:
  333. return _make_token(TK_CURSOR); //for completion
  334. case '\t':
  335. case '\r':
  336. case ' ':
  337. continue;
  338. case '\n':
  339. tk_line++;
  340. continue;
  341. case '/': {
  342. switch (GETCHAR(0)) {
  343. case '*': { // block comment
  344. char_idx++;
  345. while (true) {
  346. if (GETCHAR(0) == 0) {
  347. return _make_token(TK_EOF);
  348. }
  349. if (GETCHAR(0) == '*' && GETCHAR(1) == '/') {
  350. char_idx += 2;
  351. break;
  352. } else if (GETCHAR(0) == '\n') {
  353. tk_line++;
  354. }
  355. char_idx++;
  356. }
  357. } break;
  358. case '/': { // line comment skip
  359. while (true) {
  360. if (GETCHAR(0) == '\n') {
  361. tk_line++;
  362. char_idx++;
  363. break;
  364. }
  365. if (GETCHAR(0) == 0) {
  366. return _make_token(TK_EOF);
  367. }
  368. char_idx++;
  369. }
  370. } break;
  371. case '=': { // diveq
  372. char_idx++;
  373. return _make_token(TK_OP_ASSIGN_DIV);
  374. } break;
  375. default:
  376. return _make_token(TK_OP_DIV);
  377. }
  378. continue; //a comment, continue to next token
  379. } break;
  380. case '=': {
  381. if (GETCHAR(0) == '=') {
  382. char_idx++;
  383. return _make_token(TK_OP_EQUAL);
  384. }
  385. return _make_token(TK_OP_ASSIGN);
  386. } break;
  387. case '<': {
  388. if (GETCHAR(0) == '=') {
  389. char_idx++;
  390. return _make_token(TK_OP_LESS_EQUAL);
  391. } else if (GETCHAR(0) == '<') {
  392. char_idx++;
  393. if (GETCHAR(0) == '=') {
  394. char_idx++;
  395. return _make_token(TK_OP_ASSIGN_SHIFT_LEFT);
  396. }
  397. return _make_token(TK_OP_SHIFT_LEFT);
  398. }
  399. return _make_token(TK_OP_LESS);
  400. } break;
  401. case '>': {
  402. if (GETCHAR(0) == '=') {
  403. char_idx++;
  404. return _make_token(TK_OP_GREATER_EQUAL);
  405. } else if (GETCHAR(0) == '>') {
  406. char_idx++;
  407. if (GETCHAR(0) == '=') {
  408. char_idx++;
  409. return _make_token(TK_OP_ASSIGN_SHIFT_RIGHT);
  410. }
  411. return _make_token(TK_OP_SHIFT_RIGHT);
  412. }
  413. return _make_token(TK_OP_GREATER);
  414. } break;
  415. case '!': {
  416. if (GETCHAR(0) == '=') {
  417. char_idx++;
  418. return _make_token(TK_OP_NOT_EQUAL);
  419. }
  420. return _make_token(TK_OP_NOT);
  421. } break;
  422. //case '"' //string - no strings in shader
  423. //case '\'' //string - no strings in shader
  424. case '{':
  425. return _make_token(TK_CURLY_BRACKET_OPEN);
  426. case '}':
  427. return _make_token(TK_CURLY_BRACKET_CLOSE);
  428. case '[':
  429. return _make_token(TK_BRACKET_OPEN);
  430. case ']':
  431. return _make_token(TK_BRACKET_CLOSE);
  432. case '(':
  433. return _make_token(TK_PARENTHESIS_OPEN);
  434. case ')':
  435. return _make_token(TK_PARENTHESIS_CLOSE);
  436. case ',':
  437. return _make_token(TK_COMMA);
  438. case ';':
  439. return _make_token(TK_SEMICOLON);
  440. case '?':
  441. return _make_token(TK_QUESTION);
  442. case ':':
  443. return _make_token(TK_COLON);
  444. case '^':
  445. return _make_token(TK_OP_BIT_XOR);
  446. case '~':
  447. return _make_token(TK_OP_BIT_INVERT);
  448. case '&': {
  449. if (GETCHAR(0) == '=') {
  450. char_idx++;
  451. return _make_token(TK_OP_ASSIGN_BIT_AND);
  452. } else if (GETCHAR(0) == '&') {
  453. char_idx++;
  454. return _make_token(TK_OP_AND);
  455. }
  456. return _make_token(TK_OP_BIT_AND);
  457. } break;
  458. case '|': {
  459. if (GETCHAR(0) == '=') {
  460. char_idx++;
  461. return _make_token(TK_OP_ASSIGN_BIT_OR);
  462. } else if (GETCHAR(0) == '|') {
  463. char_idx++;
  464. return _make_token(TK_OP_OR);
  465. }
  466. return _make_token(TK_OP_BIT_OR);
  467. } break;
  468. case '*': {
  469. if (GETCHAR(0) == '=') {
  470. char_idx++;
  471. return _make_token(TK_OP_ASSIGN_MUL);
  472. }
  473. return _make_token(TK_OP_MUL);
  474. } break;
  475. case '+': {
  476. if (GETCHAR(0) == '=') {
  477. char_idx++;
  478. return _make_token(TK_OP_ASSIGN_ADD);
  479. } else if (GETCHAR(0) == '+') {
  480. char_idx++;
  481. return _make_token(TK_OP_INCREMENT);
  482. }
  483. return _make_token(TK_OP_ADD);
  484. } break;
  485. case '-': {
  486. if (GETCHAR(0) == '=') {
  487. char_idx++;
  488. return _make_token(TK_OP_ASSIGN_SUB);
  489. } else if (GETCHAR(0) == '-') {
  490. char_idx++;
  491. return _make_token(TK_OP_DECREMENT);
  492. }
  493. return _make_token(TK_OP_SUB);
  494. } break;
  495. case '%': {
  496. if (GETCHAR(0) == '=') {
  497. char_idx++;
  498. return _make_token(TK_OP_ASSIGN_MOD);
  499. }
  500. return _make_token(TK_OP_MOD);
  501. } break;
  502. default: {
  503. char_idx--; //go back one, since we have no idea what this is
  504. if (_is_number(GETCHAR(0)) || (GETCHAR(0) == '.' && _is_number(GETCHAR(1)))) {
  505. // parse number
  506. bool period_found = false;
  507. bool exponent_found = false;
  508. bool hexa_found = false;
  509. bool sign_found = false;
  510. bool float_suffix_found = false;
  511. String str;
  512. int i = 0;
  513. while (true) {
  514. if (GETCHAR(i) == '.') {
  515. if (period_found || exponent_found || hexa_found || float_suffix_found) {
  516. return _make_token(TK_ERROR, "Invalid numeric constant");
  517. }
  518. period_found = true;
  519. } else if (GETCHAR(i) == 'x') {
  520. if (hexa_found || str.length() != 1 || str[0] != '0') {
  521. return _make_token(TK_ERROR, "Invalid numeric constant");
  522. }
  523. hexa_found = true;
  524. } else if (GETCHAR(i) == 'e' && !hexa_found) {
  525. if (exponent_found || float_suffix_found) {
  526. return _make_token(TK_ERROR, "Invalid numeric constant");
  527. }
  528. exponent_found = true;
  529. } else if (GETCHAR(i) == 'f' && !hexa_found) {
  530. if (exponent_found) {
  531. return _make_token(TK_ERROR, "Invalid numeric constant");
  532. }
  533. float_suffix_found = true;
  534. } else if (_is_number(GETCHAR(i))) {
  535. if (float_suffix_found) {
  536. return _make_token(TK_ERROR, "Invalid numeric constant");
  537. }
  538. } else if (hexa_found && _is_hex(GETCHAR(i))) {
  539. } else if ((GETCHAR(i) == '-' || GETCHAR(i) == '+') && exponent_found) {
  540. if (sign_found) {
  541. return _make_token(TK_ERROR, "Invalid numeric constant");
  542. }
  543. sign_found = true;
  544. } else {
  545. break;
  546. }
  547. str += char32_t(GETCHAR(i));
  548. i++;
  549. }
  550. char32_t last_char = str[str.length() - 1];
  551. if (hexa_found) {
  552. //integer(hex)
  553. if (str.size() > 11 || !str.is_valid_hex_number(true)) { // > 0xFFFFFFFF
  554. return _make_token(TK_ERROR, "Invalid (hexadecimal) numeric constant");
  555. }
  556. } else if (period_found || exponent_found || float_suffix_found) {
  557. //floats
  558. if (period_found) {
  559. if (float_suffix_found) {
  560. //checks for eg "1.f" or "1.99f" notations
  561. if (last_char != 'f') {
  562. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  563. }
  564. } else {
  565. //checks for eg. "1." or "1.99" notations
  566. if (last_char != '.' && !_is_number(last_char)) {
  567. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  568. }
  569. }
  570. } else if (float_suffix_found) {
  571. // if no period found the float suffix must be the last character, like in "2f" for "2.0"
  572. if (last_char != 'f') {
  573. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  574. }
  575. }
  576. if (float_suffix_found) {
  577. //strip the suffix
  578. str = str.left(str.length() - 1);
  579. //compensate reading cursor position
  580. char_idx += 1;
  581. }
  582. if (!str.is_valid_float()) {
  583. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  584. }
  585. } else {
  586. //integers
  587. if (!_is_number(last_char)) {
  588. return _make_token(TK_ERROR, "Invalid (integer) numeric constant");
  589. }
  590. if (!str.is_valid_int()) {
  591. return _make_token(TK_ERROR, "Invalid numeric constant");
  592. }
  593. }
  594. char_idx += str.length();
  595. Token tk;
  596. if (period_found || exponent_found || float_suffix_found) {
  597. tk.type = TK_FLOAT_CONSTANT;
  598. } else {
  599. tk.type = TK_INT_CONSTANT;
  600. }
  601. if (hexa_found) {
  602. tk.constant = (double)str.hex_to_int();
  603. } else {
  604. tk.constant = str.to_float();
  605. }
  606. tk.line = tk_line;
  607. return tk;
  608. }
  609. if (GETCHAR(0) == '.') {
  610. //parse period
  611. char_idx++;
  612. return _make_token(TK_PERIOD);
  613. }
  614. if (_is_text_char(GETCHAR(0))) {
  615. // parse identifier
  616. String str;
  617. while (_is_text_char(GETCHAR(0))) {
  618. str += char32_t(GETCHAR(0));
  619. char_idx++;
  620. }
  621. //see if keyword
  622. //should be converted to a static map
  623. int idx = 0;
  624. while (keyword_list[idx].text) {
  625. if (str == keyword_list[idx].text) {
  626. return _make_token(keyword_list[idx].token);
  627. }
  628. idx++;
  629. }
  630. str = str.replace("dus_", "_");
  631. return _make_token(TK_IDENTIFIER, str);
  632. }
  633. if (GETCHAR(0) > 32) {
  634. return _make_token(TK_ERROR, "Tokenizer: Unknown character #" + itos(GETCHAR(0)) + ": '" + String::chr(GETCHAR(0)) + "'");
  635. } else {
  636. return _make_token(TK_ERROR, "Tokenizer: Unknown character #" + itos(GETCHAR(0)));
  637. }
  638. } break;
  639. }
  640. }
  641. ERR_PRINT("BUG");
  642. return Token();
  643. #undef GETCHAR
  644. }
  645. String ShaderLanguage::token_debug(const String &p_code) {
  646. clear();
  647. code = p_code;
  648. String output;
  649. Token tk = _get_token();
  650. while (tk.type != TK_EOF && tk.type != TK_ERROR) {
  651. output += itos(tk_line) + ": " + get_token_text(tk) + "\n";
  652. tk = _get_token();
  653. }
  654. return output;
  655. }
  656. bool ShaderLanguage::is_token_variable_datatype(TokenType p_type) {
  657. return (
  658. p_type == TK_TYPE_VOID ||
  659. p_type == TK_TYPE_BOOL ||
  660. p_type == TK_TYPE_BVEC2 ||
  661. p_type == TK_TYPE_BVEC3 ||
  662. p_type == TK_TYPE_BVEC4 ||
  663. p_type == TK_TYPE_INT ||
  664. p_type == TK_TYPE_IVEC2 ||
  665. p_type == TK_TYPE_IVEC3 ||
  666. p_type == TK_TYPE_IVEC4 ||
  667. p_type == TK_TYPE_UINT ||
  668. p_type == TK_TYPE_UVEC2 ||
  669. p_type == TK_TYPE_UVEC3 ||
  670. p_type == TK_TYPE_UVEC4 ||
  671. p_type == TK_TYPE_FLOAT ||
  672. p_type == TK_TYPE_VEC2 ||
  673. p_type == TK_TYPE_VEC3 ||
  674. p_type == TK_TYPE_VEC4 ||
  675. p_type == TK_TYPE_MAT2 ||
  676. p_type == TK_TYPE_MAT3 ||
  677. p_type == TK_TYPE_MAT4);
  678. }
  679. bool ShaderLanguage::is_token_datatype(TokenType p_type) {
  680. return (
  681. p_type == TK_TYPE_VOID ||
  682. p_type == TK_TYPE_BOOL ||
  683. p_type == TK_TYPE_BVEC2 ||
  684. p_type == TK_TYPE_BVEC3 ||
  685. p_type == TK_TYPE_BVEC4 ||
  686. p_type == TK_TYPE_INT ||
  687. p_type == TK_TYPE_IVEC2 ||
  688. p_type == TK_TYPE_IVEC3 ||
  689. p_type == TK_TYPE_IVEC4 ||
  690. p_type == TK_TYPE_UINT ||
  691. p_type == TK_TYPE_UVEC2 ||
  692. p_type == TK_TYPE_UVEC3 ||
  693. p_type == TK_TYPE_UVEC4 ||
  694. p_type == TK_TYPE_FLOAT ||
  695. p_type == TK_TYPE_VEC2 ||
  696. p_type == TK_TYPE_VEC3 ||
  697. p_type == TK_TYPE_VEC4 ||
  698. p_type == TK_TYPE_MAT2 ||
  699. p_type == TK_TYPE_MAT3 ||
  700. p_type == TK_TYPE_MAT4 ||
  701. p_type == TK_TYPE_SAMPLER2D ||
  702. p_type == TK_TYPE_ISAMPLER2D ||
  703. p_type == TK_TYPE_USAMPLER2D ||
  704. p_type == TK_TYPE_SAMPLER2DARRAY ||
  705. p_type == TK_TYPE_ISAMPLER2DARRAY ||
  706. p_type == TK_TYPE_USAMPLER2DARRAY ||
  707. p_type == TK_TYPE_SAMPLER3D ||
  708. p_type == TK_TYPE_ISAMPLER3D ||
  709. p_type == TK_TYPE_USAMPLER3D ||
  710. p_type == TK_TYPE_SAMPLERCUBE ||
  711. p_type == TK_TYPE_SAMPLERCUBEARRAY);
  712. }
  713. ShaderLanguage::DataType ShaderLanguage::get_token_datatype(TokenType p_type) {
  714. return DataType(p_type - TK_TYPE_VOID);
  715. }
  716. bool ShaderLanguage::is_token_interpolation(TokenType p_type) {
  717. return (
  718. p_type == TK_INTERPOLATION_FLAT ||
  719. p_type == TK_INTERPOLATION_SMOOTH);
  720. }
  721. ShaderLanguage::DataInterpolation ShaderLanguage::get_token_interpolation(TokenType p_type) {
  722. if (p_type == TK_INTERPOLATION_FLAT) {
  723. return INTERPOLATION_FLAT;
  724. } else {
  725. return INTERPOLATION_SMOOTH;
  726. }
  727. }
  728. bool ShaderLanguage::is_token_precision(TokenType p_type) {
  729. return (
  730. p_type == TK_PRECISION_LOW ||
  731. p_type == TK_PRECISION_MID ||
  732. p_type == TK_PRECISION_HIGH);
  733. }
  734. ShaderLanguage::DataPrecision ShaderLanguage::get_token_precision(TokenType p_type) {
  735. if (p_type == TK_PRECISION_LOW) {
  736. return PRECISION_LOWP;
  737. } else if (p_type == TK_PRECISION_HIGH) {
  738. return PRECISION_HIGHP;
  739. } else {
  740. return PRECISION_MEDIUMP;
  741. }
  742. }
  743. String ShaderLanguage::get_precision_name(DataPrecision p_type) {
  744. switch (p_type) {
  745. case PRECISION_LOWP:
  746. return "lowp";
  747. case PRECISION_MEDIUMP:
  748. return "mediump";
  749. case PRECISION_HIGHP:
  750. return "highp";
  751. default:
  752. break;
  753. }
  754. return "";
  755. }
  756. String ShaderLanguage::get_datatype_name(DataType p_type) {
  757. switch (p_type) {
  758. case TYPE_VOID:
  759. return "void";
  760. case TYPE_BOOL:
  761. return "bool";
  762. case TYPE_BVEC2:
  763. return "bvec2";
  764. case TYPE_BVEC3:
  765. return "bvec3";
  766. case TYPE_BVEC4:
  767. return "bvec4";
  768. case TYPE_INT:
  769. return "int";
  770. case TYPE_IVEC2:
  771. return "ivec2";
  772. case TYPE_IVEC3:
  773. return "ivec3";
  774. case TYPE_IVEC4:
  775. return "ivec4";
  776. case TYPE_UINT:
  777. return "uint";
  778. case TYPE_UVEC2:
  779. return "uvec2";
  780. case TYPE_UVEC3:
  781. return "uvec3";
  782. case TYPE_UVEC4:
  783. return "uvec4";
  784. case TYPE_FLOAT:
  785. return "float";
  786. case TYPE_VEC2:
  787. return "vec2";
  788. case TYPE_VEC3:
  789. return "vec3";
  790. case TYPE_VEC4:
  791. return "vec4";
  792. case TYPE_MAT2:
  793. return "mat2";
  794. case TYPE_MAT3:
  795. return "mat3";
  796. case TYPE_MAT4:
  797. return "mat4";
  798. case TYPE_SAMPLER2D:
  799. return "sampler2D";
  800. case TYPE_ISAMPLER2D:
  801. return "isampler2D";
  802. case TYPE_USAMPLER2D:
  803. return "usampler2D";
  804. case TYPE_SAMPLER2DARRAY:
  805. return "sampler2DArray";
  806. case TYPE_ISAMPLER2DARRAY:
  807. return "isampler2DArray";
  808. case TYPE_USAMPLER2DARRAY:
  809. return "usampler2DArray";
  810. case TYPE_SAMPLER3D:
  811. return "sampler3D";
  812. case TYPE_ISAMPLER3D:
  813. return "isampler3D";
  814. case TYPE_USAMPLER3D:
  815. return "usampler3D";
  816. case TYPE_SAMPLERCUBE:
  817. return "samplerCube";
  818. case TYPE_SAMPLERCUBEARRAY:
  819. return "samplerCubeArray";
  820. case TYPE_STRUCT:
  821. return "struct";
  822. case TYPE_MAX:
  823. return "invalid";
  824. }
  825. return "";
  826. }
  827. bool ShaderLanguage::is_token_nonvoid_datatype(TokenType p_type) {
  828. return is_token_datatype(p_type) && p_type != TK_TYPE_VOID;
  829. }
  830. void ShaderLanguage::clear() {
  831. current_function = StringName();
  832. last_name = StringName();
  833. last_type = IDENTIFIER_MAX;
  834. completion_type = COMPLETION_NONE;
  835. completion_block = nullptr;
  836. completion_function = StringName();
  837. completion_class = SubClassTag::TAG_GLOBAL;
  838. completion_struct = StringName();
  839. unknown_varying_usages.clear();
  840. #ifdef DEBUG_ENABLED
  841. used_constants.clear();
  842. used_varyings.clear();
  843. used_uniforms.clear();
  844. used_functions.clear();
  845. used_structs.clear();
  846. used_local_vars.clear();
  847. warnings.clear();
  848. #endif // DEBUG_ENABLED
  849. error_line = 0;
  850. tk_line = 1;
  851. char_idx = 0;
  852. error_set = false;
  853. error_str = "";
  854. last_const = false;
  855. while (nodes) {
  856. Node *n = nodes;
  857. nodes = nodes->next;
  858. memdelete(n);
  859. }
  860. }
  861. #ifdef DEBUG_ENABLED
  862. void ShaderLanguage::_parse_used_identifier(const StringName &p_identifier, IdentifierType p_type, const StringName &p_function) {
  863. switch (p_type) {
  864. case IdentifierType::IDENTIFIER_CONSTANT:
  865. if (HAS_WARNING(ShaderWarning::UNUSED_CONSTANT_FLAG) && used_constants.has(p_identifier)) {
  866. used_constants[p_identifier].used = true;
  867. }
  868. break;
  869. case IdentifierType::IDENTIFIER_VARYING:
  870. if (HAS_WARNING(ShaderWarning::UNUSED_VARYING_FLAG) && used_varyings.has(p_identifier)) {
  871. if (shader->varyings[p_identifier].stage != ShaderNode::Varying::STAGE_VERTEX && shader->varyings[p_identifier].stage != ShaderNode::Varying::STAGE_FRAGMENT) {
  872. used_varyings[p_identifier].used = true;
  873. }
  874. }
  875. break;
  876. case IdentifierType::IDENTIFIER_UNIFORM:
  877. if (HAS_WARNING(ShaderWarning::UNUSED_UNIFORM_FLAG) && used_uniforms.has(p_identifier)) {
  878. used_uniforms[p_identifier].used = true;
  879. }
  880. break;
  881. case IdentifierType::IDENTIFIER_FUNCTION:
  882. if (HAS_WARNING(ShaderWarning::UNUSED_FUNCTION_FLAG) && used_functions.has(p_identifier)) {
  883. used_functions[p_identifier].used = true;
  884. }
  885. break;
  886. case IdentifierType::IDENTIFIER_LOCAL_VAR:
  887. if (HAS_WARNING(ShaderWarning::UNUSED_LOCAL_VARIABLE_FLAG) && used_local_vars.has(p_function) && used_local_vars[p_function].has(p_identifier)) {
  888. used_local_vars[p_function][p_identifier].used = true;
  889. }
  890. break;
  891. default:
  892. break;
  893. }
  894. }
  895. #endif // DEBUG_ENABLED
  896. bool ShaderLanguage::_find_identifier(const BlockNode *p_block, bool p_allow_reassign, const FunctionInfo &p_function_info, const StringName &p_identifier, DataType *r_data_type, IdentifierType *r_type, bool *r_is_const, int *r_array_size, StringName *r_struct_name, ConstantNode::Value *r_constant_value) {
  897. if (p_function_info.built_ins.has(p_identifier)) {
  898. if (r_data_type) {
  899. *r_data_type = p_function_info.built_ins[p_identifier].type;
  900. }
  901. if (r_is_const) {
  902. *r_is_const = p_function_info.built_ins[p_identifier].constant;
  903. }
  904. if (r_type) {
  905. *r_type = IDENTIFIER_BUILTIN_VAR;
  906. }
  907. return true;
  908. }
  909. if (p_function_info.stage_functions.has(p_identifier)) {
  910. if (r_data_type) {
  911. *r_data_type = p_function_info.stage_functions[p_identifier].return_type;
  912. }
  913. if (r_is_const) {
  914. *r_is_const = true;
  915. }
  916. if (r_type) {
  917. *r_type = IDENTIFIER_FUNCTION;
  918. }
  919. return true;
  920. }
  921. FunctionNode *function = nullptr;
  922. while (p_block) {
  923. if (p_block->variables.has(p_identifier)) {
  924. if (r_data_type) {
  925. *r_data_type = p_block->variables[p_identifier].type;
  926. }
  927. if (r_is_const) {
  928. *r_is_const = p_block->variables[p_identifier].is_const;
  929. }
  930. if (r_array_size) {
  931. *r_array_size = p_block->variables[p_identifier].array_size;
  932. }
  933. if (r_struct_name) {
  934. *r_struct_name = p_block->variables[p_identifier].struct_name;
  935. }
  936. if (r_constant_value) {
  937. *r_constant_value = p_block->variables[p_identifier].value;
  938. }
  939. if (r_type) {
  940. *r_type = IDENTIFIER_LOCAL_VAR;
  941. }
  942. return true;
  943. }
  944. if (p_block->parent_function) {
  945. function = p_block->parent_function;
  946. break;
  947. } else {
  948. if (p_allow_reassign) {
  949. break;
  950. }
  951. ERR_FAIL_COND_V(!p_block->parent_block, false);
  952. p_block = p_block->parent_block;
  953. }
  954. }
  955. if (function) {
  956. for (int i = 0; i < function->arguments.size(); i++) {
  957. if (function->arguments[i].name == p_identifier) {
  958. if (r_data_type) {
  959. *r_data_type = function->arguments[i].type;
  960. }
  961. if (r_struct_name) {
  962. *r_struct_name = function->arguments[i].type_str;
  963. }
  964. if (r_array_size) {
  965. *r_array_size = function->arguments[i].array_size;
  966. }
  967. if (r_is_const) {
  968. *r_is_const = function->arguments[i].is_const;
  969. }
  970. if (r_type) {
  971. *r_type = IDENTIFIER_FUNCTION_ARGUMENT;
  972. }
  973. return true;
  974. }
  975. }
  976. }
  977. if (shader->varyings.has(p_identifier)) {
  978. if (r_data_type) {
  979. *r_data_type = shader->varyings[p_identifier].type;
  980. }
  981. if (r_array_size) {
  982. *r_array_size = shader->varyings[p_identifier].array_size;
  983. }
  984. if (r_type) {
  985. *r_type = IDENTIFIER_VARYING;
  986. }
  987. return true;
  988. }
  989. if (shader->uniforms.has(p_identifier)) {
  990. if (r_data_type) {
  991. *r_data_type = shader->uniforms[p_identifier].type;
  992. }
  993. if (r_array_size) {
  994. *r_array_size = shader->uniforms[p_identifier].array_size;
  995. }
  996. if (r_type) {
  997. *r_type = IDENTIFIER_UNIFORM;
  998. }
  999. return true;
  1000. }
  1001. if (shader->constants.has(p_identifier)) {
  1002. if (r_is_const) {
  1003. *r_is_const = true;
  1004. }
  1005. if (r_data_type) {
  1006. *r_data_type = shader->constants[p_identifier].type;
  1007. }
  1008. if (r_array_size) {
  1009. *r_array_size = shader->constants[p_identifier].array_size;
  1010. }
  1011. if (r_struct_name) {
  1012. *r_struct_name = shader->constants[p_identifier].type_str;
  1013. }
  1014. if (r_constant_value) {
  1015. if (shader->constants[p_identifier].initializer && shader->constants[p_identifier].initializer->values.size() == 1) {
  1016. *r_constant_value = shader->constants[p_identifier].initializer->values[0];
  1017. }
  1018. }
  1019. if (r_type) {
  1020. *r_type = IDENTIFIER_CONSTANT;
  1021. }
  1022. return true;
  1023. }
  1024. for (int i = 0; i < shader->functions.size(); i++) {
  1025. if (!shader->functions[i].callable) {
  1026. continue;
  1027. }
  1028. if (shader->functions[i].name == p_identifier) {
  1029. if (r_data_type) {
  1030. *r_data_type = shader->functions[i].function->return_type;
  1031. }
  1032. if (r_array_size) {
  1033. *r_array_size = shader->functions[i].function->return_array_size;
  1034. }
  1035. if (r_type) {
  1036. *r_type = IDENTIFIER_FUNCTION;
  1037. }
  1038. return true;
  1039. }
  1040. }
  1041. return false;
  1042. }
  1043. bool ShaderLanguage::_validate_operator(OperatorNode *p_op, DataType *r_ret_type, int *r_ret_size) {
  1044. bool valid = false;
  1045. DataType ret_type = TYPE_VOID;
  1046. int ret_size = 0;
  1047. switch (p_op->op) {
  1048. case OP_EQUAL:
  1049. case OP_NOT_EQUAL: {
  1050. if ((!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) || (!p_op->arguments[1]->is_indexed() && p_op->arguments[1]->get_array_size() > 0)) {
  1051. break; // don't accept arrays
  1052. }
  1053. DataType na = p_op->arguments[0]->get_datatype();
  1054. DataType nb = p_op->arguments[1]->get_datatype();
  1055. valid = na == nb;
  1056. ret_type = TYPE_BOOL;
  1057. } break;
  1058. case OP_LESS:
  1059. case OP_LESS_EQUAL:
  1060. case OP_GREATER:
  1061. case OP_GREATER_EQUAL: {
  1062. if ((!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) || (!p_op->arguments[1]->is_indexed() && p_op->arguments[1]->get_array_size() > 0)) {
  1063. break; // don't accept arrays
  1064. }
  1065. DataType na = p_op->arguments[0]->get_datatype();
  1066. DataType nb = p_op->arguments[1]->get_datatype();
  1067. valid = na == nb && (na == TYPE_UINT || na == TYPE_INT || na == TYPE_FLOAT);
  1068. ret_type = TYPE_BOOL;
  1069. } break;
  1070. case OP_AND:
  1071. case OP_OR: {
  1072. if ((!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) || (!p_op->arguments[1]->is_indexed() && p_op->arguments[1]->get_array_size() > 0)) {
  1073. break; // don't accept arrays
  1074. }
  1075. DataType na = p_op->arguments[0]->get_datatype();
  1076. DataType nb = p_op->arguments[1]->get_datatype();
  1077. valid = na == nb && na == TYPE_BOOL;
  1078. ret_type = TYPE_BOOL;
  1079. } break;
  1080. case OP_NOT: {
  1081. if (!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) {
  1082. break; // don't accept arrays
  1083. }
  1084. DataType na = p_op->arguments[0]->get_datatype();
  1085. valid = na == TYPE_BOOL;
  1086. ret_type = TYPE_BOOL;
  1087. } break;
  1088. case OP_INCREMENT:
  1089. case OP_DECREMENT:
  1090. case OP_POST_INCREMENT:
  1091. case OP_POST_DECREMENT:
  1092. case OP_NEGATE: {
  1093. if (!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) {
  1094. break; // don't accept arrays
  1095. }
  1096. DataType na = p_op->arguments[0]->get_datatype();
  1097. valid = na > TYPE_BOOL && na < TYPE_MAT2;
  1098. ret_type = na;
  1099. } break;
  1100. case OP_ADD:
  1101. case OP_SUB:
  1102. case OP_MUL:
  1103. case OP_DIV: {
  1104. if ((!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) || (!p_op->arguments[1]->is_indexed() && p_op->arguments[1]->get_array_size() > 0)) {
  1105. break; // don't accept arrays
  1106. }
  1107. DataType na = p_op->arguments[0]->get_datatype();
  1108. DataType nb = p_op->arguments[1]->get_datatype();
  1109. if (na > nb) {
  1110. //make things easier;
  1111. SWAP(na, nb);
  1112. }
  1113. if (na == nb) {
  1114. valid = (na > TYPE_BOOL && na <= TYPE_MAT4);
  1115. ret_type = na;
  1116. } else if (na == TYPE_INT && nb == TYPE_IVEC2) {
  1117. valid = true;
  1118. ret_type = TYPE_IVEC2;
  1119. } else if (na == TYPE_INT && nb == TYPE_IVEC3) {
  1120. valid = true;
  1121. ret_type = TYPE_IVEC3;
  1122. } else if (na == TYPE_INT && nb == TYPE_IVEC4) {
  1123. valid = true;
  1124. ret_type = TYPE_IVEC4;
  1125. } else if (na == TYPE_UINT && nb == TYPE_UVEC2) {
  1126. valid = true;
  1127. ret_type = TYPE_UVEC2;
  1128. } else if (na == TYPE_UINT && nb == TYPE_UVEC3) {
  1129. valid = true;
  1130. ret_type = TYPE_UVEC3;
  1131. } else if (na == TYPE_UINT && nb == TYPE_UVEC4) {
  1132. valid = true;
  1133. ret_type = TYPE_UVEC4;
  1134. } else if (na == TYPE_FLOAT && nb == TYPE_VEC2) {
  1135. valid = true;
  1136. ret_type = TYPE_VEC2;
  1137. } else if (na == TYPE_FLOAT && nb == TYPE_VEC3) {
  1138. valid = true;
  1139. ret_type = TYPE_VEC3;
  1140. } else if (na == TYPE_FLOAT && nb == TYPE_VEC4) {
  1141. valid = true;
  1142. ret_type = TYPE_VEC4;
  1143. } else if (na == TYPE_FLOAT && nb == TYPE_MAT2) {
  1144. valid = true;
  1145. ret_type = TYPE_MAT2;
  1146. } else if (na == TYPE_FLOAT && nb == TYPE_MAT3) {
  1147. valid = true;
  1148. ret_type = TYPE_MAT3;
  1149. } else if (na == TYPE_FLOAT && nb == TYPE_MAT4) {
  1150. valid = true;
  1151. ret_type = TYPE_MAT4;
  1152. } else if (p_op->op == OP_MUL && na == TYPE_VEC2 && nb == TYPE_MAT2) {
  1153. valid = true;
  1154. ret_type = TYPE_VEC2;
  1155. } else if (p_op->op == OP_MUL && na == TYPE_VEC3 && nb == TYPE_MAT3) {
  1156. valid = true;
  1157. ret_type = TYPE_VEC3;
  1158. } else if (p_op->op == OP_MUL && na == TYPE_VEC4 && nb == TYPE_MAT4) {
  1159. valid = true;
  1160. ret_type = TYPE_VEC4;
  1161. }
  1162. } break;
  1163. case OP_ASSIGN_MOD:
  1164. case OP_MOD: {
  1165. /*
  1166. * The operator modulus (%) operates on signed or unsigned integers or integer vectors. The operand
  1167. * types must both be signed or both be unsigned. The operands cannot be vectors of differing size. If
  1168. * one operand is a scalar and the other vector, then the scalar is applied component-wise to the vector,
  1169. * resulting in the same type as the vector. If both are vectors of the same size, the result is computed
  1170. * component-wise.
  1171. */
  1172. if ((!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) || (!p_op->arguments[1]->is_indexed() && p_op->arguments[1]->get_array_size() > 0)) {
  1173. break; // don't accept arrays
  1174. }
  1175. DataType na = p_op->arguments[0]->get_datatype();
  1176. DataType nb = p_op->arguments[1]->get_datatype();
  1177. if (na == TYPE_INT && nb == TYPE_INT) {
  1178. valid = true;
  1179. ret_type = TYPE_INT;
  1180. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1181. valid = true;
  1182. ret_type = TYPE_IVEC2;
  1183. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1184. valid = true;
  1185. ret_type = TYPE_IVEC3;
  1186. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1187. valid = true;
  1188. ret_type = TYPE_IVEC4;
  1189. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  1190. valid = true;
  1191. ret_type = TYPE_IVEC2;
  1192. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  1193. valid = true;
  1194. ret_type = TYPE_IVEC3;
  1195. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  1196. valid = true;
  1197. ret_type = TYPE_IVEC4;
  1198. /////
  1199. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1200. valid = true;
  1201. ret_type = TYPE_UINT;
  1202. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1203. valid = true;
  1204. ret_type = TYPE_UVEC2;
  1205. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1206. valid = true;
  1207. ret_type = TYPE_UVEC3;
  1208. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1209. valid = true;
  1210. ret_type = TYPE_UVEC4;
  1211. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1212. valid = true;
  1213. ret_type = TYPE_UVEC2;
  1214. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1215. valid = true;
  1216. ret_type = TYPE_UVEC3;
  1217. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1218. valid = true;
  1219. ret_type = TYPE_UVEC4;
  1220. }
  1221. } break;
  1222. case OP_ASSIGN_SHIFT_LEFT:
  1223. case OP_ASSIGN_SHIFT_RIGHT:
  1224. case OP_SHIFT_LEFT:
  1225. case OP_SHIFT_RIGHT: {
  1226. if ((!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) || (!p_op->arguments[1]->is_indexed() && p_op->arguments[1]->get_array_size() > 0)) {
  1227. break; // don't accept arrays
  1228. }
  1229. DataType na = p_op->arguments[0]->get_datatype();
  1230. DataType nb = p_op->arguments[1]->get_datatype();
  1231. if (na == TYPE_INT && nb == TYPE_INT) {
  1232. valid = true;
  1233. ret_type = TYPE_INT;
  1234. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1235. valid = true;
  1236. ret_type = TYPE_IVEC2;
  1237. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1238. valid = true;
  1239. ret_type = TYPE_IVEC3;
  1240. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1241. valid = true;
  1242. ret_type = TYPE_IVEC4;
  1243. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  1244. valid = true;
  1245. ret_type = TYPE_IVEC2;
  1246. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  1247. valid = true;
  1248. ret_type = TYPE_IVEC3;
  1249. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  1250. valid = true;
  1251. ret_type = TYPE_IVEC4;
  1252. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1253. valid = true;
  1254. ret_type = TYPE_UINT;
  1255. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1256. valid = true;
  1257. ret_type = TYPE_UVEC2;
  1258. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1259. valid = true;
  1260. ret_type = TYPE_UVEC3;
  1261. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1262. valid = true;
  1263. ret_type = TYPE_UVEC4;
  1264. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1265. valid = true;
  1266. ret_type = TYPE_UVEC2;
  1267. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1268. valid = true;
  1269. ret_type = TYPE_UVEC3;
  1270. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1271. valid = true;
  1272. ret_type = TYPE_UVEC4;
  1273. }
  1274. } break;
  1275. case OP_ASSIGN: {
  1276. int sa = 0;
  1277. int sb = 0;
  1278. if (!p_op->arguments[0]->is_indexed()) {
  1279. sa = p_op->arguments[0]->get_array_size();
  1280. }
  1281. if (!p_op->arguments[1]->is_indexed()) {
  1282. sb = p_op->arguments[1]->get_array_size();
  1283. }
  1284. if (sa != sb) {
  1285. break; // don't accept arrays if their sizes are not equal
  1286. }
  1287. DataType na = p_op->arguments[0]->get_datatype();
  1288. DataType nb = p_op->arguments[1]->get_datatype();
  1289. if (na == TYPE_STRUCT || nb == TYPE_STRUCT) {
  1290. valid = p_op->arguments[0]->get_datatype_name() == p_op->arguments[1]->get_datatype_name();
  1291. } else {
  1292. valid = na == nb;
  1293. }
  1294. ret_type = na;
  1295. ret_size = sa;
  1296. } break;
  1297. case OP_ASSIGN_ADD:
  1298. case OP_ASSIGN_SUB:
  1299. case OP_ASSIGN_MUL:
  1300. case OP_ASSIGN_DIV: {
  1301. int sa = 0;
  1302. int sb = 0;
  1303. if (!p_op->arguments[0]->is_indexed()) {
  1304. sa = p_op->arguments[0]->get_array_size();
  1305. }
  1306. if (!p_op->arguments[1]->is_indexed()) {
  1307. sb = p_op->arguments[1]->get_array_size();
  1308. }
  1309. if (sa > 0 || sb > 0) {
  1310. break; // don't accept arrays
  1311. }
  1312. DataType na = p_op->arguments[0]->get_datatype();
  1313. DataType nb = p_op->arguments[1]->get_datatype();
  1314. if (na == nb) {
  1315. valid = (na > TYPE_BOOL && na <= TYPE_MAT4);
  1316. ret_type = na;
  1317. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1318. valid = true;
  1319. ret_type = TYPE_IVEC2;
  1320. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1321. valid = true;
  1322. ret_type = TYPE_IVEC3;
  1323. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1324. valid = true;
  1325. ret_type = TYPE_IVEC4;
  1326. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1327. valid = true;
  1328. ret_type = TYPE_UVEC2;
  1329. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1330. valid = true;
  1331. ret_type = TYPE_UVEC3;
  1332. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1333. valid = true;
  1334. ret_type = TYPE_UVEC4;
  1335. } else if (na == TYPE_VEC2 && nb == TYPE_FLOAT) {
  1336. valid = true;
  1337. ret_type = TYPE_VEC2;
  1338. } else if (na == TYPE_VEC3 && nb == TYPE_FLOAT) {
  1339. valid = true;
  1340. ret_type = TYPE_VEC3;
  1341. } else if (na == TYPE_VEC4 && nb == TYPE_FLOAT) {
  1342. valid = true;
  1343. ret_type = TYPE_VEC4;
  1344. } else if (na == TYPE_MAT2 && nb == TYPE_FLOAT) {
  1345. valid = true;
  1346. ret_type = TYPE_MAT2;
  1347. } else if (na == TYPE_MAT3 && nb == TYPE_FLOAT) {
  1348. valid = true;
  1349. ret_type = TYPE_MAT3;
  1350. } else if (na == TYPE_MAT4 && nb == TYPE_FLOAT) {
  1351. valid = true;
  1352. ret_type = TYPE_MAT4;
  1353. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC2 && nb == TYPE_MAT2) {
  1354. valid = true;
  1355. ret_type = TYPE_VEC2;
  1356. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC3 && nb == TYPE_MAT3) {
  1357. valid = true;
  1358. ret_type = TYPE_VEC3;
  1359. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC4 && nb == TYPE_MAT4) {
  1360. valid = true;
  1361. ret_type = TYPE_VEC4;
  1362. }
  1363. } break;
  1364. case OP_ASSIGN_BIT_AND:
  1365. case OP_ASSIGN_BIT_OR:
  1366. case OP_ASSIGN_BIT_XOR:
  1367. case OP_BIT_AND:
  1368. case OP_BIT_OR:
  1369. case OP_BIT_XOR: {
  1370. /*
  1371. * The bitwise operators and (&), exclusive-or (^), and inclusive-or (|). The operands must be of type
  1372. * signed or unsigned integers or integer vectors. The operands cannot be vectors of differing size. If
  1373. * one operand is a scalar and the other a vector, the scalar is applied component-wise to the vector,
  1374. * resulting in the same type as the vector. The fundamental types of the operands (signed or unsigned)
  1375. * must match.
  1376. */
  1377. int sa = 0;
  1378. int sb = 0;
  1379. if (!p_op->arguments[0]->is_indexed()) {
  1380. sa = p_op->arguments[0]->get_array_size();
  1381. }
  1382. if (!p_op->arguments[1]->is_indexed()) {
  1383. sb = p_op->arguments[1]->get_array_size();
  1384. }
  1385. if (sa > 0 || sb > 0) {
  1386. break; // don't accept arrays
  1387. }
  1388. DataType na = p_op->arguments[0]->get_datatype();
  1389. DataType nb = p_op->arguments[1]->get_datatype();
  1390. if (na > nb && p_op->op >= OP_BIT_AND) {
  1391. //can swap for non assign
  1392. SWAP(na, nb);
  1393. }
  1394. if (na == TYPE_INT && nb == TYPE_INT) {
  1395. valid = true;
  1396. ret_type = TYPE_INT;
  1397. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1398. valid = true;
  1399. ret_type = TYPE_IVEC2;
  1400. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1401. valid = true;
  1402. ret_type = TYPE_IVEC3;
  1403. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1404. valid = true;
  1405. ret_type = TYPE_IVEC4;
  1406. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  1407. valid = true;
  1408. ret_type = TYPE_IVEC2;
  1409. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  1410. valid = true;
  1411. ret_type = TYPE_IVEC3;
  1412. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  1413. valid = true;
  1414. ret_type = TYPE_IVEC4;
  1415. /////
  1416. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1417. valid = true;
  1418. ret_type = TYPE_UINT;
  1419. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1420. valid = true;
  1421. ret_type = TYPE_UVEC2;
  1422. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1423. valid = true;
  1424. ret_type = TYPE_UVEC3;
  1425. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1426. valid = true;
  1427. ret_type = TYPE_UVEC4;
  1428. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1429. valid = true;
  1430. ret_type = TYPE_UVEC2;
  1431. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1432. valid = true;
  1433. ret_type = TYPE_UVEC3;
  1434. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1435. valid = true;
  1436. ret_type = TYPE_UVEC4;
  1437. }
  1438. } break;
  1439. case OP_BIT_INVERT: { //unaries
  1440. if (!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) {
  1441. break; // don't accept arrays
  1442. }
  1443. DataType na = p_op->arguments[0]->get_datatype();
  1444. valid = na >= TYPE_INT && na < TYPE_FLOAT;
  1445. ret_type = na;
  1446. } break;
  1447. case OP_SELECT_IF: {
  1448. int sa = 0;
  1449. int sb = 0;
  1450. if (!p_op->arguments[1]->is_indexed()) {
  1451. sa = p_op->arguments[1]->get_array_size();
  1452. }
  1453. if (!p_op->arguments[2]->is_indexed()) {
  1454. sb = p_op->arguments[2]->get_array_size();
  1455. }
  1456. if (sa != sb) {
  1457. break; // don't accept arrays if their sizes are not equal
  1458. }
  1459. DataType na = p_op->arguments[0]->get_datatype();
  1460. DataType nb = p_op->arguments[1]->get_datatype();
  1461. DataType nc = p_op->arguments[2]->get_datatype();
  1462. valid = na == TYPE_BOOL && (nb == nc);
  1463. ret_type = nb;
  1464. ret_size = sa;
  1465. } break;
  1466. default: {
  1467. ERR_FAIL_V(false);
  1468. }
  1469. }
  1470. if (r_ret_type) {
  1471. *r_ret_type = ret_type;
  1472. }
  1473. if (r_ret_size) {
  1474. *r_ret_size = ret_size;
  1475. }
  1476. return valid;
  1477. }
  1478. const ShaderLanguage::BuiltinFuncDef ShaderLanguage::builtin_func_defs[] = {
  1479. // Constructors.
  1480. { "bool", TYPE_BOOL, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1481. { "bvec2", TYPE_BVEC2, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1482. { "bvec2", TYPE_BVEC2, { TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1483. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1484. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1485. { "bvec3", TYPE_BVEC3, { TYPE_BVEC2, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1486. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1487. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1488. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1489. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BVEC2, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1490. { "bvec4", TYPE_BVEC4, { TYPE_BVEC2, TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1491. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BOOL, TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1492. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1493. { "bvec4", TYPE_BVEC4, { TYPE_BVEC3, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1494. { "bvec4", TYPE_BVEC4, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1495. { "float", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1496. { "vec2", TYPE_VEC2, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1497. { "vec2", TYPE_VEC2, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1498. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1499. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1500. { "vec3", TYPE_VEC3, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1501. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1502. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1503. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1504. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1505. { "vec4", TYPE_VEC4, { TYPE_VEC2, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1506. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1507. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1508. { "vec4", TYPE_VEC4, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1509. { "vec4", TYPE_VEC4, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1510. { "int", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1511. { "ivec2", TYPE_IVEC2, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1512. { "ivec2", TYPE_IVEC2, { TYPE_INT, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1513. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1514. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1515. { "ivec3", TYPE_IVEC3, { TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1516. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1517. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1518. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1519. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1520. { "ivec4", TYPE_IVEC4, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1521. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_INT, TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1522. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1523. { "ivec4", TYPE_IVEC4, { TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1524. { "ivec4", TYPE_IVEC4, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1525. { "uint", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1526. { "uvec2", TYPE_UVEC2, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1527. { "uvec2", TYPE_UVEC2, { TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1528. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1529. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1530. { "uvec3", TYPE_UVEC3, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1531. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1532. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1533. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1534. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1535. { "uvec4", TYPE_UVEC4, { TYPE_UVEC2, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1536. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UINT, TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1537. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1538. { "uvec4", TYPE_UVEC4, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1539. { "uvec4", TYPE_UVEC4, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1540. { "mat2", TYPE_MAT2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1541. { "mat3", TYPE_MAT3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1542. { "mat4", TYPE_MAT4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1543. { "mat2", TYPE_MAT2, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1544. { "mat3", TYPE_MAT3, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1545. { "mat4", TYPE_MAT4, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1546. // Conversion scalars.
  1547. { "int", TYPE_INT, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1548. { "int", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1549. { "int", TYPE_INT, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1550. { "int", TYPE_INT, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1551. { "float", TYPE_FLOAT, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1552. { "float", TYPE_FLOAT, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1553. { "float", TYPE_FLOAT, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1554. { "float", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1555. { "uint", TYPE_UINT, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1556. { "uint", TYPE_UINT, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1557. { "uint", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1558. { "uint", TYPE_UINT, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1559. { "bool", TYPE_BOOL, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1560. { "bool", TYPE_BOOL, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1561. { "bool", TYPE_BOOL, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1562. { "bool", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1563. // Conversion vectors.
  1564. { "ivec2", TYPE_IVEC2, { TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1565. { "ivec2", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1566. { "ivec2", TYPE_IVEC2, { TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1567. { "ivec2", TYPE_IVEC2, { TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1568. { "vec2", TYPE_VEC2, { TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1569. { "vec2", TYPE_VEC2, { TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1570. { "vec2", TYPE_VEC2, { TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1571. { "vec2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1572. { "uvec2", TYPE_UVEC2, { TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1573. { "uvec2", TYPE_UVEC2, { TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1574. { "uvec2", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1575. { "uvec2", TYPE_UVEC2, { TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1576. { "bvec2", TYPE_BVEC2, { TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1577. { "bvec2", TYPE_BVEC2, { TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1578. { "bvec2", TYPE_BVEC2, { TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1579. { "bvec2", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1580. { "ivec3", TYPE_IVEC3, { TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1581. { "ivec3", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1582. { "ivec3", TYPE_IVEC3, { TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1583. { "ivec3", TYPE_IVEC3, { TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1584. { "vec3", TYPE_VEC3, { TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1585. { "vec3", TYPE_VEC3, { TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1586. { "vec3", TYPE_VEC3, { TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1587. { "vec3", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1588. { "uvec3", TYPE_UVEC3, { TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1589. { "uvec3", TYPE_UVEC3, { TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1590. { "uvec3", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1591. { "uvec3", TYPE_UVEC3, { TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1592. { "bvec3", TYPE_BVEC3, { TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1593. { "bvec3", TYPE_BVEC3, { TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1594. { "bvec3", TYPE_BVEC3, { TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1595. { "bvec3", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1596. { "ivec4", TYPE_IVEC4, { TYPE_BVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1597. { "ivec4", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1598. { "ivec4", TYPE_IVEC4, { TYPE_UVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1599. { "ivec4", TYPE_IVEC4, { TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1600. { "vec4", TYPE_VEC4, { TYPE_BVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1601. { "vec4", TYPE_VEC4, { TYPE_IVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1602. { "vec4", TYPE_VEC4, { TYPE_UVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1603. { "vec4", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1604. { "uvec4", TYPE_UVEC4, { TYPE_BVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1605. { "uvec4", TYPE_UVEC4, { TYPE_IVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1606. { "uvec4", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1607. { "uvec4", TYPE_UVEC4, { TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1608. { "bvec4", TYPE_BVEC4, { TYPE_BVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1609. { "bvec4", TYPE_BVEC4, { TYPE_IVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1610. { "bvec4", TYPE_BVEC4, { TYPE_UVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, true },
  1611. { "bvec4", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1612. // Conversion between matrixes.
  1613. { "mat2", TYPE_MAT2, { TYPE_MAT3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1614. { "mat2", TYPE_MAT2, { TYPE_MAT4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1615. { "mat3", TYPE_MAT3, { TYPE_MAT2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1616. { "mat3", TYPE_MAT3, { TYPE_MAT4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1617. { "mat4", TYPE_MAT4, { TYPE_MAT2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1618. { "mat4", TYPE_MAT4, { TYPE_MAT3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1619. // Built-ins - trigonometric functions.
  1620. // radians
  1621. { "radians", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "degrees" }, TAG_GLOBAL, false },
  1622. { "radians", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "degrees" }, TAG_GLOBAL, false },
  1623. { "radians", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "degrees" }, TAG_GLOBAL, false },
  1624. { "radians", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "degrees" }, TAG_GLOBAL, false },
  1625. // degrees
  1626. { "degrees", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "radians" }, TAG_GLOBAL, false },
  1627. { "degrees", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "radians" }, TAG_GLOBAL, false },
  1628. { "degrees", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "radians" }, TAG_GLOBAL, false },
  1629. { "degrees", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "radians" }, TAG_GLOBAL, false },
  1630. // sin
  1631. { "sin", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1632. { "sin", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1633. { "sin", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1634. { "sin", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1635. // cos
  1636. { "cos", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1637. { "cos", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1638. { "cos", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1639. { "cos", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1640. // tan
  1641. { "tan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1642. { "tan", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1643. { "tan", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1644. { "tan", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1645. // asin
  1646. { "asin", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1647. { "asin", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1648. { "asin", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1649. { "asin", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1650. // acos
  1651. { "acos", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1652. { "acos", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1653. { "acos", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1654. { "acos", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1655. // atan
  1656. { "atan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "y_over_x" }, TAG_GLOBAL, false },
  1657. { "atan", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "y_over_x" }, TAG_GLOBAL, false },
  1658. { "atan", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "y_over_x" }, TAG_GLOBAL, false },
  1659. { "atan", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "y_over_x" }, TAG_GLOBAL, false },
  1660. { "atan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "y", "x" }, TAG_GLOBAL, false },
  1661. { "atan", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "y", "x" }, TAG_GLOBAL, false },
  1662. { "atan", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "y", "x" }, TAG_GLOBAL, false },
  1663. { "atan", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "y", "x" }, TAG_GLOBAL, false },
  1664. // sinh
  1665. { "sinh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1666. { "sinh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1667. { "sinh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1668. { "sinh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1669. // cosh
  1670. { "cosh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1671. { "cosh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1672. { "cosh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1673. { "cosh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1674. // tanh
  1675. { "tanh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1676. { "tanh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1677. { "tanh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1678. { "tanh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1679. // asinh
  1680. { "asinh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1681. { "asinh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1682. { "asinh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1683. { "asinh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1684. // acosh
  1685. { "acosh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1686. { "acosh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1687. { "acosh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1688. { "acosh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1689. // atanh
  1690. { "atanh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1691. { "atanh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1692. { "atanh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1693. { "atanh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1694. // Builtins - exponential functions.
  1695. // pow
  1696. { "pow", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  1697. { "pow", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  1698. { "pow", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  1699. { "pow", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  1700. // exp
  1701. { "exp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1702. { "exp", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1703. { "exp", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1704. { "exp", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1705. // log
  1706. { "log", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1707. { "log", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1708. { "log", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1709. { "log", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1710. // exp2
  1711. { "exp2", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1712. { "exp2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1713. { "exp2", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1714. { "exp2", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1715. // log2
  1716. { "log2", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1717. { "log2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1718. { "log2", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1719. { "log2", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1720. // sqrt
  1721. { "sqrt", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1722. { "sqrt", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1723. { "sqrt", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1724. { "sqrt", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1725. // inversesqrt
  1726. { "inversesqrt", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1727. { "inversesqrt", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1728. { "inversesqrt", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1729. { "inversesqrt", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1730. // Built-ins - common functions.
  1731. // abs
  1732. { "abs", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1733. { "abs", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1734. { "abs", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1735. { "abs", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1736. { "abs", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1737. { "abs", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1738. { "abs", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1739. { "abs", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1740. // sign
  1741. { "sign", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1742. { "sign", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1743. { "sign", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1744. { "sign", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1745. { "sign", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1746. { "sign", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1747. { "sign", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1748. { "sign", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1749. // floor
  1750. { "floor", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1751. { "floor", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1752. { "floor", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1753. { "floor", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1754. // trunc
  1755. { "trunc", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1756. { "trunc", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1757. { "trunc", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1758. { "trunc", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1759. // round
  1760. { "round", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1761. { "round", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1762. { "round", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1763. { "round", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1764. // roundEven
  1765. { "roundEven", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1766. { "roundEven", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1767. { "roundEven", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1768. { "roundEven", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1769. // ceil
  1770. { "ceil", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1771. { "ceil", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1772. { "ceil", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1773. { "ceil", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1774. // fract
  1775. { "fract", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1776. { "fract", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1777. { "fract", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1778. { "fract", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1779. // mod
  1780. { "mod", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  1781. { "mod", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  1782. { "mod", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  1783. { "mod", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  1784. { "mod", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  1785. { "mod", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  1786. { "mod", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  1787. // modf
  1788. { "modf", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "i" }, TAG_GLOBAL, true },
  1789. { "modf", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "x", "i" }, TAG_GLOBAL, true },
  1790. { "modf", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "x", "i" }, TAG_GLOBAL, true },
  1791. { "modf", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "x", "i" }, TAG_GLOBAL, true },
  1792. // min
  1793. { "min", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1794. { "min", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1795. { "min", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1796. { "min", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1797. { "min", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1798. { "min", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1799. { "min", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1800. { "min", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1801. { "min", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1802. { "min", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1803. { "min", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1804. { "min", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1805. { "min", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1806. { "min", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1807. { "min", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1808. { "min", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1809. { "min", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1810. { "min", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1811. { "min", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1812. { "min", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1813. { "min", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1814. // max
  1815. { "max", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1816. { "max", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1817. { "max", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1818. { "max", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1819. { "max", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1820. { "max", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1821. { "max", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1822. { "max", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1823. { "max", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1824. { "max", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1825. { "max", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1826. { "max", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1827. { "max", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1828. { "max", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1829. { "max", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1830. { "max", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1831. { "max", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1832. { "max", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1833. { "max", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1834. { "max", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1835. { "max", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1836. // clamp
  1837. { "clamp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  1838. { "clamp", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  1839. { "clamp", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  1840. { "clamp", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  1841. { "clamp", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  1842. { "clamp", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  1843. { "clamp", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  1844. { "clamp", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  1845. { "clamp", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  1846. { "clamp", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  1847. { "clamp", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  1848. { "clamp", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  1849. { "clamp", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  1850. { "clamp", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  1851. { "clamp", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, true },
  1852. { "clamp", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, true },
  1853. { "clamp", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, true },
  1854. { "clamp", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, true },
  1855. { "clamp", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, true },
  1856. { "clamp", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, true },
  1857. { "clamp", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, true },
  1858. // mix
  1859. { "mix", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  1860. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  1861. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_BVEC2, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  1862. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  1863. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  1864. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_BVEC3, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  1865. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  1866. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  1867. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_BVEC4, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  1868. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  1869. // step
  1870. { "step", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  1871. { "step", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  1872. { "step", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  1873. { "step", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  1874. { "step", TYPE_VEC2, { TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  1875. { "step", TYPE_VEC3, { TYPE_FLOAT, TYPE_VEC3, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  1876. { "step", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC4, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  1877. // smoothstep
  1878. { "smoothstep", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  1879. { "smoothstep", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  1880. { "smoothstep", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  1881. { "smoothstep", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  1882. { "smoothstep", TYPE_VEC2, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  1883. { "smoothstep", TYPE_VEC3, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC3, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  1884. { "smoothstep", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC4, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  1885. // isnan
  1886. { "isnan", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1887. { "isnan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1888. { "isnan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1889. { "isnan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1890. // isinf
  1891. { "isinf", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1892. { "isinf", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1893. { "isinf", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1894. { "isinf", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1895. // floatBitsToInt
  1896. { "floatBitsToInt", TYPE_INT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, true },
  1897. { "floatBitsToInt", TYPE_IVEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, true },
  1898. { "floatBitsToInt", TYPE_IVEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, true },
  1899. { "floatBitsToInt", TYPE_IVEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, true },
  1900. // floatBitsToUint
  1901. { "floatBitsToUint", TYPE_UINT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, true },
  1902. { "floatBitsToUint", TYPE_UVEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, true },
  1903. { "floatBitsToUint", TYPE_UVEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, true },
  1904. { "floatBitsToUint", TYPE_UVEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, true },
  1905. // intBitsToFloat
  1906. { "intBitsToFloat", TYPE_FLOAT, { TYPE_INT, TYPE_VOID }, { "x" }, TAG_GLOBAL, true },
  1907. { "intBitsToFloat", TYPE_VEC2, { TYPE_IVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, true },
  1908. { "intBitsToFloat", TYPE_VEC3, { TYPE_IVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, true },
  1909. { "intBitsToFloat", TYPE_VEC4, { TYPE_IVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, true },
  1910. // uintBitsToFloat
  1911. { "uintBitsToFloat", TYPE_FLOAT, { TYPE_UINT, TYPE_VOID }, { "x" }, TAG_GLOBAL, true },
  1912. { "uintBitsToFloat", TYPE_VEC2, { TYPE_UVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, true },
  1913. { "uintBitsToFloat", TYPE_VEC3, { TYPE_UVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, true },
  1914. { "uintBitsToFloat", TYPE_VEC4, { TYPE_UVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, true },
  1915. // Built-ins - geometric functions.
  1916. // length
  1917. { "length", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1918. { "length", TYPE_FLOAT, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1919. { "length", TYPE_FLOAT, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1920. { "length", TYPE_FLOAT, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1921. // distance
  1922. { "distance", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1923. { "distance", TYPE_FLOAT, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1924. { "distance", TYPE_FLOAT, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1925. { "distance", TYPE_FLOAT, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1926. // dot
  1927. { "dot", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1928. { "dot", TYPE_FLOAT, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1929. { "dot", TYPE_FLOAT, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1930. { "dot", TYPE_FLOAT, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1931. // cross
  1932. { "cross", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1933. // normalize
  1934. { "normalize", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  1935. { "normalize", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  1936. { "normalize", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  1937. { "normalize", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  1938. // reflect
  1939. { "reflect", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "I", "N" }, TAG_GLOBAL, false },
  1940. // refract
  1941. { "refract", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "I", "N", "eta" }, TAG_GLOBAL, false },
  1942. // faceforward
  1943. { "faceforward", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "N", "I", "Nref" }, TAG_GLOBAL, false },
  1944. { "faceforward", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "N", "I", "Nref" }, TAG_GLOBAL, false },
  1945. { "faceforward", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "N", "I", "Nref" }, TAG_GLOBAL, false },
  1946. // matrixCompMult
  1947. { "matrixCompMult", TYPE_MAT2, { TYPE_MAT2, TYPE_MAT2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1948. { "matrixCompMult", TYPE_MAT3, { TYPE_MAT3, TYPE_MAT3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1949. { "matrixCompMult", TYPE_MAT4, { TYPE_MAT4, TYPE_MAT4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1950. // outerProduct
  1951. { "outerProduct", TYPE_MAT2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "c", "r" }, TAG_GLOBAL, false },
  1952. { "outerProduct", TYPE_MAT3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "c", "r" }, TAG_GLOBAL, false },
  1953. { "outerProduct", TYPE_MAT4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "c", "r" }, TAG_GLOBAL, false },
  1954. // transpose
  1955. { "transpose", TYPE_MAT2, { TYPE_MAT2, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  1956. { "transpose", TYPE_MAT3, { TYPE_MAT3, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  1957. { "transpose", TYPE_MAT4, { TYPE_MAT4, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  1958. // determinant
  1959. { "determinant", TYPE_FLOAT, { TYPE_MAT2, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  1960. { "determinant", TYPE_FLOAT, { TYPE_MAT3, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  1961. { "determinant", TYPE_FLOAT, { TYPE_MAT4, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  1962. // inverse
  1963. { "inverse", TYPE_MAT2, { TYPE_MAT2, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  1964. { "inverse", TYPE_MAT3, { TYPE_MAT3, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  1965. { "inverse", TYPE_MAT4, { TYPE_MAT4, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  1966. // lessThan
  1967. { "lessThan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1968. { "lessThan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1969. { "lessThan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1970. { "lessThan", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1971. { "lessThan", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1972. { "lessThan", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1973. { "lessThan", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1974. { "lessThan", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1975. { "lessThan", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1976. // greaterThan
  1977. { "greaterThan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1978. { "greaterThan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1979. { "greaterThan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1980. { "greaterThan", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1981. { "greaterThan", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1982. { "greaterThan", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1983. { "greaterThan", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1984. { "greaterThan", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1985. { "greaterThan", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1986. // lessThanEqual
  1987. { "lessThanEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1988. { "lessThanEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1989. { "lessThanEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1990. { "lessThanEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1991. { "lessThanEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1992. { "lessThanEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1993. { "lessThanEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1994. { "lessThanEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1995. { "lessThanEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  1996. // greaterThanEqual
  1997. { "greaterThanEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1998. { "greaterThanEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  1999. { "greaterThanEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2000. { "greaterThanEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2001. { "greaterThanEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2002. { "greaterThanEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2003. { "greaterThanEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  2004. { "greaterThanEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  2005. { "greaterThanEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  2006. // equal
  2007. { "equal", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2008. { "equal", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2009. { "equal", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2010. { "equal", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2011. { "equal", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2012. { "equal", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2013. { "equal", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  2014. { "equal", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  2015. { "equal", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  2016. { "equal", TYPE_BVEC2, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2017. { "equal", TYPE_BVEC3, { TYPE_BVEC3, TYPE_BVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2018. { "equal", TYPE_BVEC4, { TYPE_BVEC4, TYPE_BVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2019. // notEqual
  2020. { "notEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2021. { "notEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2022. { "notEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2023. { "notEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2024. { "notEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2025. { "notEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2026. { "notEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  2027. { "notEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  2028. { "notEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, true },
  2029. { "notEqual", TYPE_BVEC2, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2030. { "notEqual", TYPE_BVEC3, { TYPE_BVEC3, TYPE_BVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2031. { "notEqual", TYPE_BVEC4, { TYPE_BVEC4, TYPE_BVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2032. // any
  2033. { "any", TYPE_BOOL, { TYPE_BVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2034. { "any", TYPE_BOOL, { TYPE_BVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2035. { "any", TYPE_BOOL, { TYPE_BVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2036. // all
  2037. { "all", TYPE_BOOL, { TYPE_BVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2038. { "all", TYPE_BOOL, { TYPE_BVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2039. { "all", TYPE_BOOL, { TYPE_BVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2040. // not
  2041. { "not", TYPE_BVEC2, { TYPE_BVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2042. { "not", TYPE_BVEC3, { TYPE_BVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2043. { "not", TYPE_BVEC4, { TYPE_BVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2044. // Built-ins: texture functions.
  2045. // textureSize
  2046. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLER2D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, true },
  2047. { "textureSize", TYPE_IVEC2, { TYPE_ISAMPLER2D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, true },
  2048. { "textureSize", TYPE_IVEC2, { TYPE_USAMPLER2D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, true },
  2049. { "textureSize", TYPE_IVEC3, { TYPE_SAMPLER2DARRAY, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, true },
  2050. { "textureSize", TYPE_IVEC3, { TYPE_ISAMPLER2DARRAY, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, true },
  2051. { "textureSize", TYPE_IVEC3, { TYPE_USAMPLER2DARRAY, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, true },
  2052. { "textureSize", TYPE_IVEC3, { TYPE_SAMPLER3D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, true },
  2053. { "textureSize", TYPE_IVEC3, { TYPE_ISAMPLER3D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, true },
  2054. { "textureSize", TYPE_IVEC3, { TYPE_USAMPLER3D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, true },
  2055. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLERCUBE, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, true },
  2056. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLERCUBEARRAY, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, true },
  2057. // texture
  2058. { "texture", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2059. { "texture", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2060. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2061. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, true },
  2062. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2063. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, true },
  2064. { "texture", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2065. { "texture", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2066. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2067. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, true },
  2068. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2069. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, true },
  2070. { "texture", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2071. { "texture", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2072. { "texture", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2073. { "texture", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, true },
  2074. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2075. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, true },
  2076. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2077. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2078. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBEARRAY, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2079. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBEARRAY, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2080. // textureProj
  2081. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2082. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2083. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, true },
  2084. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, true },
  2085. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2086. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2087. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, true },
  2088. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, true },
  2089. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2090. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2091. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, true },
  2092. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, true },
  2093. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2094. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, true },
  2095. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2096. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, true },
  2097. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2098. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, true },
  2099. // textureLod
  2100. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2101. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2102. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2103. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2104. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2105. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2106. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2107. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2108. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2109. { "textureLod", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2110. { "textureLod", TYPE_VEC4, { TYPE_SAMPLERCUBEARRAY, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2111. // texelFetch
  2112. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2113. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2114. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2115. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2116. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2117. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2118. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2119. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2120. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2121. // textureProjLod
  2122. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2123. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2124. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2125. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2126. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2127. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2128. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2129. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2130. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, true },
  2131. // textureGrad
  2132. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, true },
  2133. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, true },
  2134. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, true },
  2135. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, true },
  2136. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, true },
  2137. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, true },
  2138. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, true },
  2139. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, true },
  2140. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, true },
  2141. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, true },
  2142. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLERCUBEARRAY, TYPE_VEC4, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, true },
  2143. // textureGather
  2144. { "textureGather", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2145. { "textureGather", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2146. { "textureGather", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2147. { "textureGather", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, true },
  2148. { "textureGather", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, true },
  2149. { "textureGather", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, true },
  2150. { "textureGather", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2151. { "textureGather", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2152. { "textureGather", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2153. { "textureGather", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, true },
  2154. { "textureGather", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, true },
  2155. { "textureGather", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, true },
  2156. { "textureGather", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2157. { "textureGather", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, true },
  2158. // dFdx
  2159. { "dFdx", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2160. { "dFdx", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2161. { "dFdx", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2162. { "dFdx", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2163. // dFdy
  2164. { "dFdy", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2165. { "dFdy", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2166. { "dFdy", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2167. { "dFdy", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2168. // fwidth
  2169. { "fwidth", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2170. { "fwidth", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2171. { "fwidth", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2172. { "fwidth", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2173. // Sub-functions.
  2174. // array
  2175. { "length", TYPE_INT, { TYPE_VOID }, { "" }, TAG_ARRAY, true },
  2176. // Modern functions.
  2177. // fma
  2178. { "fma", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "c" }, TAG_GLOBAL, false },
  2179. { "fma", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b", "c" }, TAG_GLOBAL, false },
  2180. { "fma", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b", "c" }, TAG_GLOBAL, false },
  2181. { "fma", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b", "c" }, TAG_GLOBAL, false },
  2182. // Packing/Unpacking functions.
  2183. { "packHalf2x16", TYPE_UINT, { TYPE_VEC2, TYPE_VOID }, { "v" }, TAG_GLOBAL, true },
  2184. { "packUnorm2x16", TYPE_UINT, { TYPE_VEC2, TYPE_VOID }, { "v" }, TAG_GLOBAL, true },
  2185. { "packSnorm2x16", TYPE_UINT, { TYPE_VEC2, TYPE_VOID }, { "v" }, TAG_GLOBAL, true },
  2186. { "packUnorm4x8", TYPE_UINT, { TYPE_VEC4, TYPE_VOID }, { "v" }, TAG_GLOBAL, true },
  2187. { "packSnorm4x8", TYPE_UINT, { TYPE_VEC4, TYPE_VOID }, { "v" }, TAG_GLOBAL, true },
  2188. { "unpackHalf2x16", TYPE_VEC2, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, true },
  2189. { "unpackUnorm2x16", TYPE_VEC2, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, true },
  2190. { "unpackSnorm2x16", TYPE_VEC2, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, true },
  2191. { "unpackUnorm4x8", TYPE_VEC4, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, true },
  2192. { "unpackSnorm4x8", TYPE_VEC4, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, true },
  2193. // bitfieldExtract
  2194. { "bitfieldExtract", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2195. { "bitfieldExtract", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2196. { "bitfieldExtract", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2197. { "bitfieldExtract", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2198. { "bitfieldExtract", TYPE_UINT, { TYPE_UINT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2199. { "bitfieldExtract", TYPE_UVEC2, { TYPE_UVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2200. { "bitfieldExtract", TYPE_UVEC3, { TYPE_UVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2201. { "bitfieldExtract", TYPE_UVEC4, { TYPE_UVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2202. // bitfieldInsert
  2203. { "bitfieldInsert", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2204. { "bitfieldInsert", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2205. { "bitfieldInsert", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2206. { "bitfieldInsert", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2207. { "bitfieldInsert", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2208. { "bitfieldInsert", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2209. { "bitfieldInsert", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2210. { "bitfieldInsert", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2211. // bitfieldReverse
  2212. { "bitfieldReverse", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2213. { "bitfieldReverse", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2214. { "bitfieldReverse", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2215. { "bitfieldReverse", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2216. { "bitfieldReverse", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2217. { "bitfieldReverse", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2218. { "bitfieldReverse", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2219. { "bitfieldReverse", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2220. // bitCount
  2221. { "bitCount", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2222. { "bitCount", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2223. { "bitCount", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2224. { "bitCount", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2225. { "bitCount", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2226. { "bitCount", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2227. { "bitCount", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2228. { "bitCount", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2229. // findLSB
  2230. { "findLSB", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2231. { "findLSB", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2232. { "findLSB", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2233. { "findLSB", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2234. { "findLSB", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2235. { "findLSB", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2236. { "findLSB", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2237. { "findLSB", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2238. // findMSB
  2239. { "findMSB", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2240. { "findMSB", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2241. { "findMSB", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2242. { "findMSB", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2243. { "findMSB", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2244. { "findMSB", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2245. { "findMSB", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2246. { "findMSB", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2247. // umulExtended
  2248. { "umulExtended", TYPE_VOID, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2249. { "umulExtended", TYPE_VOID, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2250. { "umulExtended", TYPE_VOID, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2251. { "umulExtended", TYPE_VOID, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2252. // imulExtended
  2253. { "imulExtended", TYPE_VOID, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2254. { "imulExtended", TYPE_VOID, { TYPE_IVEC2, TYPE_IVEC2, TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2255. { "imulExtended", TYPE_VOID, { TYPE_IVEC3, TYPE_IVEC3, TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2256. { "imulExtended", TYPE_VOID, { TYPE_IVEC4, TYPE_IVEC4, TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2257. // uaddCarry
  2258. { "uaddCarry", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "y", "carry" }, TAG_GLOBAL, true },
  2259. { "uaddCarry", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "x", "y", "carry" }, TAG_GLOBAL, true },
  2260. { "uaddCarry", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "x", "y", "carry" }, TAG_GLOBAL, true },
  2261. { "uaddCarry", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "x", "y", "carry" }, TAG_GLOBAL, true },
  2262. // usubBorrow
  2263. { "usubBorrow", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "y", "borrow" }, TAG_GLOBAL, true },
  2264. { "usubBorrow", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "x", "y", "borrow" }, TAG_GLOBAL, true },
  2265. { "usubBorrow", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "x", "y", "borrow" }, TAG_GLOBAL, true },
  2266. { "usubBorrow", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "x", "y", "borrow" }, TAG_GLOBAL, true },
  2267. // ldexp
  2268. { "ldexp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_INT, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2269. { "ldexp", TYPE_VEC2, { TYPE_VEC2, TYPE_IVEC2, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2270. { "ldexp", TYPE_VEC3, { TYPE_VEC3, TYPE_IVEC3, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2271. { "ldexp", TYPE_VEC4, { TYPE_VEC4, TYPE_IVEC4, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2272. // frexp
  2273. { "frexp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_INT, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2274. { "frexp", TYPE_VEC2, { TYPE_VEC2, TYPE_IVEC2, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2275. { "frexp", TYPE_VEC3, { TYPE_VEC3, TYPE_IVEC3, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2276. { "frexp", TYPE_VEC4, { TYPE_VEC4, TYPE_IVEC4, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2277. { nullptr, TYPE_VOID, { TYPE_VOID }, { "" }, TAG_GLOBAL, false }
  2278. };
  2279. const ShaderLanguage::BuiltinFuncOutArgs ShaderLanguage::builtin_func_out_args[] = {
  2280. { "modf", { 1, -1 } },
  2281. { "umulExtended", { 2, 3 } },
  2282. { "imulExtended", { 2, 3 } },
  2283. { "uaddCarry", { 2, -1 } },
  2284. { "usubBorrow", { 2, -1 } },
  2285. { "ldexp", { 1, -1 } },
  2286. { "frexp", { 1, -1 } },
  2287. { nullptr, { 0, -1 } }
  2288. };
  2289. const ShaderLanguage::BuiltinFuncConstArgs ShaderLanguage::builtin_func_const_args[] = {
  2290. { "textureGather", 2, 0, 3 },
  2291. { nullptr, 0, 0, 0 }
  2292. };
  2293. bool ShaderLanguage::_validate_function_call(BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_func, DataType *r_ret_type, StringName *r_ret_type_str) {
  2294. ERR_FAIL_COND_V(p_func->op != OP_CALL && p_func->op != OP_CONSTRUCT, false);
  2295. Vector<DataType> args;
  2296. Vector<StringName> args2;
  2297. Vector<int> args3;
  2298. ERR_FAIL_COND_V(p_func->arguments[0]->type != Node::TYPE_VARIABLE, false);
  2299. StringName name = static_cast<VariableNode *>(p_func->arguments[0])->name.operator String();
  2300. for (int i = 1; i < p_func->arguments.size(); i++) {
  2301. args.push_back(p_func->arguments[i]->get_datatype());
  2302. args2.push_back(p_func->arguments[i]->get_datatype_name());
  2303. args3.push_back(p_func->arguments[i]->get_array_size());
  2304. }
  2305. int argcount = args.size();
  2306. if (p_function_info.stage_functions.has(name)) {
  2307. //stage based function
  2308. const StageFunctionInfo &sf = p_function_info.stage_functions[name];
  2309. if (argcount != sf.arguments.size()) {
  2310. _set_error(vformat("Invalid number of arguments when calling stage function '%s', which expects %d arguments.", String(name), sf.arguments.size()));
  2311. return false;
  2312. }
  2313. //validate arguments
  2314. for (int i = 0; i < argcount; i++) {
  2315. if (args[i] != sf.arguments[i].type) {
  2316. _set_error(vformat("Invalid argument type when calling stage function '%s', type expected is '%s'.", String(name), String(get_datatype_name(sf.arguments[i].type))));
  2317. return false;
  2318. }
  2319. }
  2320. if (r_ret_type) {
  2321. *r_ret_type = sf.return_type;
  2322. }
  2323. if (r_ret_type_str) {
  2324. *r_ret_type_str = "";
  2325. }
  2326. return true;
  2327. }
  2328. bool failed_builtin = false;
  2329. bool unsupported_builtin = false;
  2330. int builtin_idx = 0;
  2331. if (argcount <= 4) {
  2332. // test builtins
  2333. int idx = 0;
  2334. while (builtin_func_defs[idx].name) {
  2335. if (completion_class != builtin_func_defs[idx].tag) {
  2336. idx++;
  2337. continue;
  2338. }
  2339. if (name == builtin_func_defs[idx].name) {
  2340. failed_builtin = true;
  2341. bool fail = false;
  2342. for (int i = 0; i < argcount; i++) {
  2343. if (p_func->arguments[i + 1]->type == Node::TYPE_ARRAY) {
  2344. const ArrayNode *anode = static_cast<const ArrayNode *>(p_func->arguments[i + 1]);
  2345. if (anode->call_expression == nullptr && !anode->is_indexed()) {
  2346. fail = true;
  2347. break;
  2348. }
  2349. }
  2350. 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])) {
  2351. //all good, but needs implicit conversion later
  2352. } else if (args[i] != builtin_func_defs[idx].args[i]) {
  2353. fail = true;
  2354. break;
  2355. }
  2356. }
  2357. if (!fail) {
  2358. if (RenderingServer::get_singleton()->is_low_end()) {
  2359. if (builtin_func_defs[idx].high_end) {
  2360. fail = true;
  2361. unsupported_builtin = true;
  2362. builtin_idx = idx;
  2363. }
  2364. }
  2365. }
  2366. if (!fail && argcount < 4 && builtin_func_defs[idx].args[argcount] != TYPE_VOID) {
  2367. fail = true; //make sure the number of arguments matches
  2368. }
  2369. if (!fail) {
  2370. {
  2371. int constarg_idx = 0;
  2372. while (builtin_func_const_args[constarg_idx].name) {
  2373. if (String(name) == builtin_func_const_args[constarg_idx].name) {
  2374. int arg = builtin_func_const_args[constarg_idx].arg + 1;
  2375. if (p_func->arguments.size() <= arg) {
  2376. break;
  2377. }
  2378. int min = builtin_func_const_args[constarg_idx].min;
  2379. int max = builtin_func_const_args[constarg_idx].max;
  2380. bool error = false;
  2381. if (p_func->arguments[arg]->type == Node::TYPE_VARIABLE) {
  2382. const VariableNode *vn = (VariableNode *)p_func->arguments[arg];
  2383. bool is_const = false;
  2384. ConstantNode::Value value;
  2385. value.sint = -1;
  2386. _find_identifier(p_block, false, p_function_info, vn->name, nullptr, nullptr, &is_const, nullptr, nullptr, &value);
  2387. if (!is_const || value.sint < min || value.sint > max) {
  2388. error = true;
  2389. }
  2390. } else {
  2391. if (p_func->arguments[arg]->type == Node::TYPE_CONSTANT) {
  2392. ConstantNode *cn = (ConstantNode *)p_func->arguments[arg];
  2393. if (cn->get_datatype() == TYPE_INT && cn->values.size() == 1) {
  2394. int value = cn->values[0].sint;
  2395. if (value < min || value > max) {
  2396. error = true;
  2397. }
  2398. } else {
  2399. error = true;
  2400. }
  2401. } else {
  2402. error = true;
  2403. }
  2404. }
  2405. if (error) {
  2406. _set_error(vformat("Expected integer constant within %s..%s range.", min, max));
  2407. return false;
  2408. }
  2409. }
  2410. constarg_idx++;
  2411. }
  2412. }
  2413. //make sure its not an out argument used in the wrong way
  2414. int outarg_idx = 0;
  2415. while (builtin_func_out_args[outarg_idx].name) {
  2416. if (String(name) == builtin_func_out_args[outarg_idx].name) {
  2417. for (int arg = 0; arg < BuiltinFuncOutArgs::MAX_ARGS; arg++) {
  2418. int arg_idx = builtin_func_out_args[outarg_idx].arguments[arg];
  2419. if (arg_idx == -1) {
  2420. break;
  2421. }
  2422. if (arg_idx < argcount) {
  2423. if (p_func->arguments[arg_idx + 1]->type != Node::TYPE_VARIABLE && p_func->arguments[arg_idx + 1]->type != Node::TYPE_MEMBER && p_func->arguments[arg_idx + 1]->type != Node::TYPE_ARRAY) {
  2424. _set_error("Argument " + itos(arg_idx + 1) + " of function '" + String(name) + "' is not a variable, array or member.");
  2425. return false;
  2426. }
  2427. if (p_func->arguments[arg_idx + 1]->type == Node::TYPE_ARRAY) {
  2428. ArrayNode *mn = static_cast<ArrayNode *>(p_func->arguments[arg_idx + 1]);
  2429. if (mn->is_const) {
  2430. fail = true;
  2431. }
  2432. } else if (p_func->arguments[arg_idx + 1]->type == Node::TYPE_MEMBER) {
  2433. MemberNode *mn = static_cast<MemberNode *>(p_func->arguments[arg_idx + 1]);
  2434. if (mn->basetype_const) {
  2435. fail = true;
  2436. }
  2437. } else { // TYPE_VARIABLE
  2438. VariableNode *vn = static_cast<VariableNode *>(p_func->arguments[arg_idx + 1]);
  2439. if (vn->is_const) {
  2440. fail = true;
  2441. } else {
  2442. StringName varname = vn->name;
  2443. if (shader->uniforms.has(varname)) {
  2444. fail = true;
  2445. } else {
  2446. if (shader->varyings.has(varname)) {
  2447. _set_error(vformat("Varyings cannot be passed for '%s' parameter!", "out"));
  2448. return false;
  2449. }
  2450. if (p_function_info.built_ins.has(varname)) {
  2451. BuiltInInfo info = p_function_info.built_ins[varname];
  2452. if (info.constant) {
  2453. fail = true;
  2454. }
  2455. }
  2456. }
  2457. }
  2458. }
  2459. if (fail) {
  2460. _set_error(vformat("Constant value cannot be passed for '%s' parameter!", "out"));
  2461. return false;
  2462. }
  2463. StringName var_name;
  2464. if (p_func->arguments[arg_idx + 1]->type == Node::TYPE_ARRAY) {
  2465. var_name = static_cast<const ArrayNode *>(p_func->arguments[arg_idx + 1])->name;
  2466. } else if (p_func->arguments[arg_idx + 1]->type == Node::TYPE_MEMBER) {
  2467. Node *n = static_cast<const MemberNode *>(p_func->arguments[arg_idx + 1])->owner;
  2468. while (n->type == Node::TYPE_MEMBER) {
  2469. n = static_cast<const MemberNode *>(n)->owner;
  2470. }
  2471. if (n->type != Node::TYPE_VARIABLE && n->type != Node::TYPE_ARRAY) {
  2472. _set_error("Argument " + itos(arg_idx + 1) + " of function '" + String(name) + "' is not a variable, array or member.");
  2473. return false;
  2474. }
  2475. if (n->type == Node::TYPE_VARIABLE) {
  2476. var_name = static_cast<const VariableNode *>(n)->name;
  2477. } else { // TYPE_ARRAY
  2478. var_name = static_cast<const ArrayNode *>(n)->name;
  2479. }
  2480. } else { // TYPE_VARIABLE
  2481. var_name = static_cast<const VariableNode *>(p_func->arguments[arg_idx + 1])->name;
  2482. }
  2483. const BlockNode *b = p_block;
  2484. bool valid = false;
  2485. while (b) {
  2486. if (b->variables.has(var_name) || p_function_info.built_ins.has(var_name)) {
  2487. valid = true;
  2488. break;
  2489. }
  2490. if (b->parent_function) {
  2491. for (int i = 0; i < b->parent_function->arguments.size(); i++) {
  2492. if (b->parent_function->arguments[i].name == var_name) {
  2493. valid = true;
  2494. break;
  2495. }
  2496. }
  2497. }
  2498. b = b->parent_block;
  2499. }
  2500. if (!valid) {
  2501. _set_error("Argument " + itos(arg_idx + 1) + " of function '" + String(name) + "' can only take a local variable, array or member.");
  2502. return false;
  2503. }
  2504. }
  2505. }
  2506. }
  2507. outarg_idx++;
  2508. }
  2509. //implicitly convert values if possible
  2510. for (int i = 0; i < argcount; i++) {
  2511. if (get_scalar_type(args[i]) != args[i] || args[i] == builtin_func_defs[idx].args[i] || p_func->arguments[i + 1]->type != Node::TYPE_CONSTANT) {
  2512. //can't do implicit conversion here
  2513. continue;
  2514. }
  2515. //this is an implicit conversion
  2516. ConstantNode *constant = static_cast<ConstantNode *>(p_func->arguments[i + 1]);
  2517. ConstantNode *conversion = alloc_node<ConstantNode>();
  2518. conversion->datatype = builtin_func_defs[idx].args[i];
  2519. conversion->values.resize(1);
  2520. convert_constant(constant, builtin_func_defs[idx].args[i], conversion->values.ptrw());
  2521. p_func->arguments.write[i + 1] = conversion;
  2522. }
  2523. if (r_ret_type) {
  2524. *r_ret_type = builtin_func_defs[idx].rettype;
  2525. }
  2526. return true;
  2527. }
  2528. }
  2529. idx++;
  2530. }
  2531. }
  2532. if (unsupported_builtin) {
  2533. String arglist = "";
  2534. for (int i = 0; i < argcount; i++) {
  2535. if (i > 0) {
  2536. arglist += ", ";
  2537. }
  2538. arglist += get_datatype_name(builtin_func_defs[builtin_idx].args[i]);
  2539. }
  2540. String err = "Built-in function \"" + String(name) + "(" + arglist + ")\" is supported only on high-end platform!";
  2541. _set_error(err);
  2542. return false;
  2543. }
  2544. if (failed_builtin) {
  2545. String err = "Invalid arguments for built-in function: " + String(name) + "(";
  2546. for (int i = 0; i < argcount; i++) {
  2547. if (i > 0) {
  2548. err += ",";
  2549. }
  2550. String arg_name;
  2551. if (args[i] == TYPE_STRUCT) {
  2552. arg_name = args2[i];
  2553. } else {
  2554. arg_name = get_datatype_name(args[i]);
  2555. }
  2556. if (args3[i] > 0) {
  2557. arg_name += "[";
  2558. arg_name += itos(args3[i]);
  2559. arg_name += "]";
  2560. }
  2561. err += arg_name;
  2562. }
  2563. err += ")";
  2564. _set_error(err);
  2565. return false;
  2566. }
  2567. // try existing functions..
  2568. StringName exclude_function;
  2569. BlockNode *block = p_block;
  2570. while (block) {
  2571. if (block->parent_function) {
  2572. exclude_function = block->parent_function->name;
  2573. }
  2574. block = block->parent_block;
  2575. }
  2576. if (name == exclude_function) {
  2577. _set_error("Recursion is not allowed");
  2578. return false;
  2579. }
  2580. int last_arg_count = 0;
  2581. String arg_list = "";
  2582. for (int i = 0; i < shader->functions.size(); i++) {
  2583. if (name != shader->functions[i].name) {
  2584. continue;
  2585. }
  2586. if (!shader->functions[i].callable) {
  2587. _set_error("Function '" + String(name) + " can't be called from source code.");
  2588. return false;
  2589. }
  2590. FunctionNode *pfunc = shader->functions[i].function;
  2591. if (arg_list == "") {
  2592. for (int j = 0; j < pfunc->arguments.size(); j++) {
  2593. if (j > 0) {
  2594. arg_list += ", ";
  2595. }
  2596. String func_arg_name;
  2597. if (pfunc->arguments[j].type == TYPE_STRUCT) {
  2598. func_arg_name = pfunc->arguments[j].type_str;
  2599. } else {
  2600. func_arg_name = get_datatype_name(pfunc->arguments[j].type);
  2601. }
  2602. if (pfunc->arguments[j].array_size > 0) {
  2603. func_arg_name += "[";
  2604. func_arg_name += itos(pfunc->arguments[j].array_size);
  2605. func_arg_name += "]";
  2606. }
  2607. arg_list += func_arg_name;
  2608. }
  2609. }
  2610. if (pfunc->arguments.size() != args.size()) {
  2611. last_arg_count = pfunc->arguments.size();
  2612. continue;
  2613. }
  2614. bool fail = false;
  2615. for (int j = 0; j < args.size(); j++) {
  2616. if (get_scalar_type(args[j]) == args[j] && p_func->arguments[j + 1]->type == Node::TYPE_CONSTANT && args3[j] == 0 && convert_constant(static_cast<ConstantNode *>(p_func->arguments[j + 1]), pfunc->arguments[j].type)) {
  2617. //all good, but it needs implicit conversion later
  2618. } else if (args[j] != pfunc->arguments[j].type || (args[j] == TYPE_STRUCT && args2[j] != pfunc->arguments[j].type_str) || args3[j] != pfunc->arguments[j].array_size) {
  2619. String func_arg_name;
  2620. if (pfunc->arguments[j].type == TYPE_STRUCT) {
  2621. func_arg_name = pfunc->arguments[j].type_str;
  2622. } else {
  2623. func_arg_name = get_datatype_name(pfunc->arguments[j].type);
  2624. }
  2625. if (pfunc->arguments[j].array_size > 0) {
  2626. func_arg_name += "[";
  2627. func_arg_name += itos(pfunc->arguments[j].array_size);
  2628. func_arg_name += "]";
  2629. }
  2630. String arg_name;
  2631. if (args[j] == TYPE_STRUCT) {
  2632. arg_name = args2[j];
  2633. } else {
  2634. arg_name = get_datatype_name(args[j]);
  2635. }
  2636. if (args3[j] > 0) {
  2637. arg_name += "[";
  2638. arg_name += itos(args3[j]);
  2639. arg_name += "]";
  2640. }
  2641. _set_error(vformat("Invalid argument for \"%s(%s)\" function: argument %s should be %s but is %s.", String(name), arg_list, j + 1, func_arg_name, arg_name));
  2642. fail = true;
  2643. break;
  2644. }
  2645. }
  2646. if (!fail) {
  2647. //implicitly convert values if possible
  2648. for (int k = 0; k < args.size(); k++) {
  2649. if (get_scalar_type(args[k]) != args[k] || args[k] == pfunc->arguments[k].type || p_func->arguments[k + 1]->type != Node::TYPE_CONSTANT) {
  2650. //can't do implicit conversion here
  2651. continue;
  2652. }
  2653. //this is an implicit conversion
  2654. ConstantNode *constant = static_cast<ConstantNode *>(p_func->arguments[k + 1]);
  2655. ConstantNode *conversion = alloc_node<ConstantNode>();
  2656. conversion->datatype = pfunc->arguments[k].type;
  2657. conversion->values.resize(1);
  2658. convert_constant(constant, pfunc->arguments[k].type, conversion->values.ptrw());
  2659. p_func->arguments.write[k + 1] = conversion;
  2660. }
  2661. if (r_ret_type) {
  2662. *r_ret_type = pfunc->return_type;
  2663. if (pfunc->return_type == TYPE_STRUCT) {
  2664. *r_ret_type_str = pfunc->return_struct_name;
  2665. }
  2666. }
  2667. return true;
  2668. }
  2669. }
  2670. if (last_arg_count > args.size()) {
  2671. _set_error(vformat("Too few arguments for \"%s(%s)\" call. Expected at least %s but received %s.", String(name), arg_list, last_arg_count, args.size()));
  2672. } else if (last_arg_count < args.size()) {
  2673. _set_error(vformat("Too many arguments for \"%s(%s)\" call. Expected at most %s but received %s.", String(name), arg_list, last_arg_count, args.size()));
  2674. }
  2675. return false;
  2676. }
  2677. bool ShaderLanguage::_compare_datatypes(DataType p_datatype_a, String p_datatype_name_a, int p_array_size_a, DataType p_datatype_b, String p_datatype_name_b, int p_array_size_b) {
  2678. bool result = true;
  2679. if (p_datatype_a == TYPE_STRUCT || p_datatype_b == TYPE_STRUCT) {
  2680. if (p_datatype_name_a != p_datatype_name_b) {
  2681. result = false;
  2682. }
  2683. } else {
  2684. if (p_datatype_a != p_datatype_b) {
  2685. result = false;
  2686. }
  2687. }
  2688. if (p_array_size_a != p_array_size_b) {
  2689. result = false;
  2690. }
  2691. if (!result) {
  2692. String type_name = p_datatype_a == TYPE_STRUCT ? p_datatype_name_a : get_datatype_name(p_datatype_a);
  2693. if (p_array_size_a > 0) {
  2694. type_name += "[";
  2695. type_name += itos(p_array_size_a);
  2696. type_name += "]";
  2697. }
  2698. String type_name2 = p_datatype_b == TYPE_STRUCT ? p_datatype_name_b : get_datatype_name(p_datatype_b);
  2699. if (p_array_size_b > 0) {
  2700. type_name2 += "[";
  2701. type_name2 += itos(p_array_size_b);
  2702. type_name2 += "]";
  2703. }
  2704. _set_error("Invalid assignment of '" + type_name2 + "' to '" + type_name + "'");
  2705. }
  2706. return result;
  2707. }
  2708. bool ShaderLanguage::_compare_datatypes_in_nodes(Node *a, Node *b) {
  2709. return _compare_datatypes(a->get_datatype(), a->get_datatype_name(), a->get_array_size(), b->get_datatype(), b->get_datatype_name(), b->get_array_size());
  2710. }
  2711. bool ShaderLanguage::_parse_function_arguments(BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_func, int *r_complete_arg) {
  2712. TkPos pos = _get_tkpos();
  2713. Token tk = _get_token();
  2714. if (tk.type == TK_PARENTHESIS_CLOSE) {
  2715. return true;
  2716. }
  2717. _set_tkpos(pos);
  2718. while (true) {
  2719. if (r_complete_arg) {
  2720. pos = _get_tkpos();
  2721. tk = _get_token();
  2722. if (tk.type == TK_CURSOR) {
  2723. *r_complete_arg = p_func->arguments.size() - 1;
  2724. } else {
  2725. _set_tkpos(pos);
  2726. }
  2727. }
  2728. Node *arg = _parse_and_reduce_expression(p_block, p_function_info);
  2729. if (!arg) {
  2730. return false;
  2731. }
  2732. p_func->arguments.push_back(arg);
  2733. tk = _get_token();
  2734. if (tk.type == TK_PARENTHESIS_CLOSE) {
  2735. return true;
  2736. } else if (tk.type != TK_COMMA) {
  2737. // something is broken
  2738. _set_error("Expected ',' or ')' after argument");
  2739. return false;
  2740. }
  2741. }
  2742. return true;
  2743. }
  2744. bool ShaderLanguage::is_token_operator(TokenType p_type) {
  2745. return (p_type == TK_OP_EQUAL ||
  2746. p_type == TK_OP_NOT_EQUAL ||
  2747. p_type == TK_OP_LESS ||
  2748. p_type == TK_OP_LESS_EQUAL ||
  2749. p_type == TK_OP_GREATER ||
  2750. p_type == TK_OP_GREATER_EQUAL ||
  2751. p_type == TK_OP_AND ||
  2752. p_type == TK_OP_OR ||
  2753. p_type == TK_OP_NOT ||
  2754. p_type == TK_OP_ADD ||
  2755. p_type == TK_OP_SUB ||
  2756. p_type == TK_OP_MUL ||
  2757. p_type == TK_OP_DIV ||
  2758. p_type == TK_OP_MOD ||
  2759. p_type == TK_OP_SHIFT_LEFT ||
  2760. p_type == TK_OP_SHIFT_RIGHT ||
  2761. p_type == TK_OP_ASSIGN ||
  2762. p_type == TK_OP_ASSIGN_ADD ||
  2763. p_type == TK_OP_ASSIGN_SUB ||
  2764. p_type == TK_OP_ASSIGN_MUL ||
  2765. p_type == TK_OP_ASSIGN_DIV ||
  2766. p_type == TK_OP_ASSIGN_MOD ||
  2767. p_type == TK_OP_ASSIGN_SHIFT_LEFT ||
  2768. p_type == TK_OP_ASSIGN_SHIFT_RIGHT ||
  2769. p_type == TK_OP_ASSIGN_BIT_AND ||
  2770. p_type == TK_OP_ASSIGN_BIT_OR ||
  2771. p_type == TK_OP_ASSIGN_BIT_XOR ||
  2772. p_type == TK_OP_BIT_AND ||
  2773. p_type == TK_OP_BIT_OR ||
  2774. p_type == TK_OP_BIT_XOR ||
  2775. p_type == TK_OP_BIT_INVERT ||
  2776. p_type == TK_OP_INCREMENT ||
  2777. p_type == TK_OP_DECREMENT ||
  2778. p_type == TK_QUESTION ||
  2779. p_type == TK_COLON);
  2780. }
  2781. bool ShaderLanguage::is_token_operator_assign(TokenType p_type) {
  2782. return (p_type == TK_OP_ASSIGN ||
  2783. p_type == TK_OP_ASSIGN_ADD ||
  2784. p_type == TK_OP_ASSIGN_SUB ||
  2785. p_type == TK_OP_ASSIGN_MUL ||
  2786. p_type == TK_OP_ASSIGN_DIV ||
  2787. p_type == TK_OP_ASSIGN_MOD ||
  2788. p_type == TK_OP_ASSIGN_SHIFT_LEFT ||
  2789. p_type == TK_OP_ASSIGN_SHIFT_RIGHT ||
  2790. p_type == TK_OP_ASSIGN_BIT_AND ||
  2791. p_type == TK_OP_ASSIGN_BIT_OR ||
  2792. p_type == TK_OP_ASSIGN_BIT_XOR);
  2793. }
  2794. bool ShaderLanguage::convert_constant(ConstantNode *p_constant, DataType p_to_type, ConstantNode::Value *p_value) {
  2795. if (p_constant->datatype == p_to_type) {
  2796. if (p_value) {
  2797. for (int i = 0; i < p_constant->values.size(); i++) {
  2798. p_value[i] = p_constant->values[i];
  2799. }
  2800. }
  2801. return true;
  2802. } else if (p_constant->datatype == TYPE_INT && p_to_type == TYPE_FLOAT) {
  2803. if (p_value) {
  2804. p_value->real = p_constant->values[0].sint;
  2805. }
  2806. return true;
  2807. } else if (p_constant->datatype == TYPE_UINT && p_to_type == TYPE_FLOAT) {
  2808. if (p_value) {
  2809. p_value->real = p_constant->values[0].uint;
  2810. }
  2811. return true;
  2812. } else if (p_constant->datatype == TYPE_INT && p_to_type == TYPE_UINT) {
  2813. if (p_constant->values[0].sint < 0) {
  2814. return false;
  2815. }
  2816. if (p_value) {
  2817. p_value->uint = p_constant->values[0].sint;
  2818. }
  2819. return true;
  2820. } else if (p_constant->datatype == TYPE_UINT && p_to_type == TYPE_INT) {
  2821. if (p_constant->values[0].uint > 0x7FFFFFFF) {
  2822. return false;
  2823. }
  2824. if (p_value) {
  2825. p_value->sint = p_constant->values[0].uint;
  2826. }
  2827. return true;
  2828. } else {
  2829. return false;
  2830. }
  2831. }
  2832. bool ShaderLanguage::is_scalar_type(DataType p_type) {
  2833. return p_type == TYPE_BOOL || p_type == TYPE_INT || p_type == TYPE_UINT || p_type == TYPE_FLOAT;
  2834. }
  2835. bool ShaderLanguage::is_float_type(DataType p_type) {
  2836. switch (p_type) {
  2837. case TYPE_FLOAT:
  2838. case TYPE_VEC2:
  2839. case TYPE_VEC3:
  2840. case TYPE_VEC4:
  2841. case TYPE_MAT2:
  2842. case TYPE_MAT3:
  2843. case TYPE_MAT4:
  2844. case TYPE_SAMPLER2D:
  2845. case TYPE_SAMPLER2DARRAY:
  2846. case TYPE_SAMPLER3D:
  2847. case TYPE_SAMPLERCUBE:
  2848. case TYPE_SAMPLERCUBEARRAY: {
  2849. return true;
  2850. }
  2851. default: {
  2852. return false;
  2853. }
  2854. }
  2855. }
  2856. bool ShaderLanguage::is_sampler_type(DataType p_type) {
  2857. return p_type == TYPE_SAMPLER2D ||
  2858. p_type == TYPE_ISAMPLER2D ||
  2859. p_type == TYPE_USAMPLER2D ||
  2860. p_type == TYPE_SAMPLER2DARRAY ||
  2861. p_type == TYPE_ISAMPLER2DARRAY ||
  2862. p_type == TYPE_USAMPLER2DARRAY ||
  2863. p_type == TYPE_SAMPLER3D ||
  2864. p_type == TYPE_ISAMPLER3D ||
  2865. p_type == TYPE_USAMPLER3D ||
  2866. p_type == TYPE_SAMPLERCUBE ||
  2867. p_type == TYPE_SAMPLERCUBEARRAY;
  2868. }
  2869. Variant ShaderLanguage::constant_value_to_variant(const Vector<ShaderLanguage::ConstantNode::Value> &p_value, DataType p_type, int p_array_size, ShaderLanguage::ShaderNode::Uniform::Hint p_hint) {
  2870. int array_size = p_array_size;
  2871. if (p_value.size() > 0) {
  2872. Variant value;
  2873. switch (p_type) {
  2874. case ShaderLanguage::TYPE_BOOL:
  2875. if (array_size > 0) {
  2876. PackedInt32Array array = PackedInt32Array();
  2877. for (int i = 0; i < array_size; i++) {
  2878. array.push_back(p_value[i].boolean);
  2879. }
  2880. value = Variant(array);
  2881. } else {
  2882. value = Variant(p_value[0].boolean);
  2883. }
  2884. break;
  2885. case ShaderLanguage::TYPE_BVEC2:
  2886. array_size *= 2;
  2887. if (array_size > 0) {
  2888. PackedInt32Array array = PackedInt32Array();
  2889. for (int i = 0; i < array_size; i++) {
  2890. array.push_back(p_value[i].boolean);
  2891. }
  2892. value = Variant(array);
  2893. } else {
  2894. value = Variant(p_value[0].boolean);
  2895. }
  2896. break;
  2897. case ShaderLanguage::TYPE_BVEC3:
  2898. array_size *= 3;
  2899. if (array_size > 0) {
  2900. PackedInt32Array array = PackedInt32Array();
  2901. for (int i = 0; i < array_size; i++) {
  2902. array.push_back(p_value[i].boolean);
  2903. }
  2904. value = Variant(array);
  2905. } else {
  2906. value = Variant(p_value[0].boolean);
  2907. }
  2908. break;
  2909. case ShaderLanguage::TYPE_BVEC4:
  2910. array_size *= 4;
  2911. if (array_size > 0) {
  2912. PackedInt32Array array = PackedInt32Array();
  2913. for (int i = 0; i < array_size; i++) {
  2914. array.push_back(p_value[i].boolean);
  2915. }
  2916. value = Variant(array);
  2917. } else {
  2918. value = Variant(p_value[0].boolean);
  2919. }
  2920. break;
  2921. case ShaderLanguage::TYPE_INT:
  2922. if (array_size > 0) {
  2923. PackedInt32Array array = PackedInt32Array();
  2924. for (int i = 0; i < array_size; i++) {
  2925. array.push_back(p_value[i].sint);
  2926. }
  2927. value = Variant(array);
  2928. } else {
  2929. value = Variant(p_value[0].sint);
  2930. }
  2931. break;
  2932. case ShaderLanguage::TYPE_IVEC2:
  2933. if (array_size > 0) {
  2934. array_size *= 2;
  2935. PackedInt32Array array = PackedInt32Array();
  2936. for (int i = 0; i < array_size; i++) {
  2937. array.push_back(p_value[i].sint);
  2938. }
  2939. value = Variant(array);
  2940. } else {
  2941. value = Variant(Vector2(p_value[0].sint, p_value[1].sint));
  2942. }
  2943. break;
  2944. case ShaderLanguage::TYPE_IVEC3:
  2945. if (array_size > 0) {
  2946. array_size *= 3;
  2947. PackedInt32Array array = PackedInt32Array();
  2948. for (int i = 0; i < array_size; i++) {
  2949. array.push_back(p_value[i].sint);
  2950. }
  2951. value = Variant(array);
  2952. } else {
  2953. value = Variant(Vector3(p_value[0].sint, p_value[1].sint, p_value[2].sint));
  2954. }
  2955. break;
  2956. case ShaderLanguage::TYPE_IVEC4:
  2957. if (array_size > 0) {
  2958. array_size *= 4;
  2959. PackedInt32Array array = PackedInt32Array();
  2960. for (int i = 0; i < array_size; i++) {
  2961. array.push_back(p_value[i].sint);
  2962. }
  2963. value = Variant(array);
  2964. } else {
  2965. value = Variant(Plane(p_value[0].sint, p_value[1].sint, p_value[2].sint, p_value[3].sint));
  2966. }
  2967. break;
  2968. case ShaderLanguage::TYPE_UINT:
  2969. if (array_size > 0) {
  2970. PackedInt32Array array = PackedInt32Array();
  2971. for (int i = 0; i < array_size; i++) {
  2972. array.push_back(p_value[i].uint);
  2973. }
  2974. value = Variant(array);
  2975. } else {
  2976. value = Variant(p_value[0].uint);
  2977. }
  2978. break;
  2979. case ShaderLanguage::TYPE_UVEC2:
  2980. if (array_size > 0) {
  2981. array_size *= 2;
  2982. PackedInt32Array array = PackedInt32Array();
  2983. for (int i = 0; i < array_size; i++) {
  2984. array.push_back(p_value[i].uint);
  2985. }
  2986. value = Variant(array);
  2987. } else {
  2988. value = Variant(Vector2(p_value[0].uint, p_value[1].uint));
  2989. }
  2990. break;
  2991. case ShaderLanguage::TYPE_UVEC3:
  2992. if (array_size > 0) {
  2993. array_size *= 3;
  2994. PackedInt32Array array = PackedInt32Array();
  2995. for (int i = 0; i < array_size; i++) {
  2996. array.push_back(p_value[i].uint);
  2997. }
  2998. value = Variant(array);
  2999. } else {
  3000. value = Variant(Vector3(p_value[0].uint, p_value[1].uint, p_value[2].uint));
  3001. }
  3002. break;
  3003. case ShaderLanguage::TYPE_UVEC4:
  3004. if (array_size > 0) {
  3005. array_size *= 4;
  3006. PackedInt32Array array = PackedInt32Array();
  3007. for (int i = 0; i < array_size; i++) {
  3008. array.push_back(p_value[i].uint);
  3009. }
  3010. value = Variant(array);
  3011. } else {
  3012. value = Variant(Plane(p_value[0].uint, p_value[1].uint, p_value[2].uint, p_value[3].uint));
  3013. }
  3014. break;
  3015. case ShaderLanguage::TYPE_FLOAT:
  3016. if (array_size > 0) {
  3017. PackedFloat32Array array = PackedFloat32Array();
  3018. for (int i = 0; i < array_size; i++) {
  3019. array.push_back(p_value[i].real);
  3020. }
  3021. value = Variant(array);
  3022. } else {
  3023. value = Variant(p_value[0].real);
  3024. }
  3025. break;
  3026. case ShaderLanguage::TYPE_VEC2:
  3027. if (array_size > 0) {
  3028. array_size *= 2;
  3029. PackedVector2Array array = PackedVector2Array();
  3030. for (int i = 0; i < array_size; i += 2) {
  3031. array.push_back(Vector2(p_value[i].real, p_value[i + 1].real));
  3032. }
  3033. value = Variant(array);
  3034. } else {
  3035. value = Variant(Vector2(p_value[0].real, p_value[1].real));
  3036. }
  3037. break;
  3038. case ShaderLanguage::TYPE_VEC3:
  3039. if (array_size > 0) {
  3040. array_size *= 3;
  3041. PackedVector3Array array = PackedVector3Array();
  3042. for (int i = 0; i < array_size; i += 3) {
  3043. array.push_back(Vector3(p_value[i].real, p_value[i + 1].real, p_value[i + 2].real));
  3044. }
  3045. value = Variant(array);
  3046. } else {
  3047. value = Variant(Vector3(p_value[0].real, p_value[1].real, p_value[2].real));
  3048. }
  3049. break;
  3050. case ShaderLanguage::TYPE_VEC4:
  3051. if (array_size > 0) {
  3052. array_size *= 4;
  3053. if (p_hint == ShaderLanguage::ShaderNode::Uniform::HINT_COLOR) {
  3054. PackedColorArray array = PackedColorArray();
  3055. for (int i = 0; i < array_size; i += 4) {
  3056. array.push_back(Color(p_value[i].real, p_value[i + 1].real, p_value[i + 2].real, p_value[i + 3].real));
  3057. }
  3058. value = Variant(array);
  3059. } else {
  3060. PackedFloat32Array array = PackedFloat32Array();
  3061. for (int i = 0; i < array_size; i += 4) {
  3062. array.push_back(p_value[i].real);
  3063. array.push_back(p_value[i + 1].real);
  3064. array.push_back(p_value[i + 2].real);
  3065. array.push_back(p_value[i + 3].real);
  3066. }
  3067. value = Variant(array);
  3068. }
  3069. } else {
  3070. if (p_hint == ShaderLanguage::ShaderNode::Uniform::HINT_COLOR) {
  3071. value = Variant(Color(p_value[0].real, p_value[1].real, p_value[2].real, p_value[3].real));
  3072. } else {
  3073. value = Variant(Plane(p_value[0].real, p_value[1].real, p_value[2].real, p_value[3].real));
  3074. }
  3075. }
  3076. break;
  3077. case ShaderLanguage::TYPE_MAT2:
  3078. if (array_size > 0) {
  3079. array_size *= 4;
  3080. PackedFloat32Array array = PackedFloat32Array();
  3081. for (int i = 0; i < array_size; i += 4) {
  3082. array.push_back(p_value[i].real);
  3083. array.push_back(p_value[i + 1].real);
  3084. array.push_back(p_value[i + 2].real);
  3085. array.push_back(p_value[i + 3].real);
  3086. }
  3087. value = Variant(array);
  3088. } else {
  3089. value = Variant(Transform2D(p_value[0].real, p_value[2].real, p_value[1].real, p_value[3].real, 0.0, 0.0));
  3090. }
  3091. break;
  3092. case ShaderLanguage::TYPE_MAT3: {
  3093. if (array_size > 0) {
  3094. array_size *= 9;
  3095. PackedFloat32Array array = PackedFloat32Array();
  3096. for (int i = 0; i < array_size; i += 9) {
  3097. for (int j = 0; j < 9; j++) {
  3098. array.push_back(p_value[i + j].real);
  3099. }
  3100. }
  3101. value = Variant(array);
  3102. } else {
  3103. Basis p;
  3104. p[0][0] = p_value[0].real;
  3105. p[0][1] = p_value[1].real;
  3106. p[0][2] = p_value[2].real;
  3107. p[1][0] = p_value[3].real;
  3108. p[1][1] = p_value[4].real;
  3109. p[1][2] = p_value[5].real;
  3110. p[2][0] = p_value[6].real;
  3111. p[2][1] = p_value[7].real;
  3112. p[2][2] = p_value[8].real;
  3113. value = Variant(p);
  3114. }
  3115. break;
  3116. }
  3117. case ShaderLanguage::TYPE_MAT4: {
  3118. if (array_size > 0) {
  3119. array_size *= 16;
  3120. PackedFloat32Array array = PackedFloat32Array();
  3121. for (int i = 0; i < array_size; i += 16) {
  3122. for (int j = 0; j < 16; j++) {
  3123. array.push_back(p_value[i + j].real);
  3124. }
  3125. }
  3126. value = Variant(array);
  3127. } else {
  3128. Basis p;
  3129. p[0][0] = p_value[0].real;
  3130. p[0][1] = p_value[1].real;
  3131. p[0][2] = p_value[2].real;
  3132. p[1][0] = p_value[4].real;
  3133. p[1][1] = p_value[5].real;
  3134. p[1][2] = p_value[6].real;
  3135. p[2][0] = p_value[8].real;
  3136. p[2][1] = p_value[9].real;
  3137. p[2][2] = p_value[10].real;
  3138. Transform3D t = Transform3D(p, Vector3(p_value[3].real, p_value[7].real, p_value[11].real));
  3139. value = Variant(t);
  3140. }
  3141. break;
  3142. }
  3143. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  3144. case ShaderLanguage::TYPE_ISAMPLER2D:
  3145. case ShaderLanguage::TYPE_ISAMPLER3D:
  3146. case ShaderLanguage::TYPE_SAMPLER2DARRAY:
  3147. case ShaderLanguage::TYPE_SAMPLER2D:
  3148. case ShaderLanguage::TYPE_SAMPLER3D:
  3149. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  3150. case ShaderLanguage::TYPE_USAMPLER2D:
  3151. case ShaderLanguage::TYPE_USAMPLER3D:
  3152. case ShaderLanguage::TYPE_SAMPLERCUBE:
  3153. case ShaderLanguage::TYPE_SAMPLERCUBEARRAY: {
  3154. // Texture types, likely not relevant here.
  3155. break;
  3156. }
  3157. case ShaderLanguage::TYPE_STRUCT:
  3158. break;
  3159. case ShaderLanguage::TYPE_VOID:
  3160. break;
  3161. case ShaderLanguage::TYPE_MAX:
  3162. break;
  3163. }
  3164. return value;
  3165. }
  3166. return Variant();
  3167. }
  3168. PropertyInfo ShaderLanguage::uniform_to_property_info(const ShaderNode::Uniform &p_uniform) {
  3169. PropertyInfo pi;
  3170. switch (p_uniform.type) {
  3171. case ShaderLanguage::TYPE_VOID:
  3172. pi.type = Variant::NIL;
  3173. break;
  3174. case ShaderLanguage::TYPE_BOOL:
  3175. if (p_uniform.array_size > 0) {
  3176. pi.type = Variant::PACKED_INT32_ARRAY;
  3177. } else {
  3178. pi.type = Variant::BOOL;
  3179. }
  3180. break;
  3181. case ShaderLanguage::TYPE_BVEC2:
  3182. if (p_uniform.array_size > 0) {
  3183. pi.type = Variant::PACKED_INT32_ARRAY;
  3184. } else {
  3185. pi.type = Variant::INT;
  3186. pi.hint = PROPERTY_HINT_FLAGS;
  3187. pi.hint_string = "x,y";
  3188. }
  3189. break;
  3190. case ShaderLanguage::TYPE_BVEC3:
  3191. if (p_uniform.array_size > 0) {
  3192. pi.type = Variant::PACKED_INT32_ARRAY;
  3193. } else {
  3194. pi.type = Variant::INT;
  3195. pi.hint = PROPERTY_HINT_FLAGS;
  3196. pi.hint_string = "x,y,z";
  3197. }
  3198. break;
  3199. case ShaderLanguage::TYPE_BVEC4:
  3200. if (p_uniform.array_size > 0) {
  3201. pi.type = Variant::PACKED_INT32_ARRAY;
  3202. } else {
  3203. pi.type = Variant::INT;
  3204. pi.hint = PROPERTY_HINT_FLAGS;
  3205. pi.hint_string = "x,y,z,w";
  3206. }
  3207. break;
  3208. case ShaderLanguage::TYPE_UINT:
  3209. case ShaderLanguage::TYPE_INT: {
  3210. if (p_uniform.array_size > 0) {
  3211. pi.type = Variant::PACKED_INT32_ARRAY;
  3212. } else {
  3213. pi.type = Variant::INT;
  3214. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_RANGE) {
  3215. pi.hint = PROPERTY_HINT_RANGE;
  3216. pi.hint_string = rtos(p_uniform.hint_range[0]) + "," + rtos(p_uniform.hint_range[1]) + "," + rtos(p_uniform.hint_range[2]);
  3217. }
  3218. }
  3219. } break;
  3220. case ShaderLanguage::TYPE_IVEC2:
  3221. case ShaderLanguage::TYPE_IVEC3:
  3222. case ShaderLanguage::TYPE_IVEC4:
  3223. case ShaderLanguage::TYPE_UVEC2:
  3224. case ShaderLanguage::TYPE_UVEC3:
  3225. case ShaderLanguage::TYPE_UVEC4: {
  3226. pi.type = Variant::PACKED_INT32_ARRAY;
  3227. } break;
  3228. case ShaderLanguage::TYPE_FLOAT: {
  3229. if (p_uniform.array_size > 0) {
  3230. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  3231. } else {
  3232. pi.type = Variant::FLOAT;
  3233. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_RANGE) {
  3234. pi.hint = PROPERTY_HINT_RANGE;
  3235. pi.hint_string = rtos(p_uniform.hint_range[0]) + "," + rtos(p_uniform.hint_range[1]) + "," + rtos(p_uniform.hint_range[2]);
  3236. }
  3237. }
  3238. } break;
  3239. case ShaderLanguage::TYPE_VEC2:
  3240. if (p_uniform.array_size > 0) {
  3241. pi.type = Variant::PACKED_VECTOR2_ARRAY;
  3242. } else {
  3243. pi.type = Variant::VECTOR2;
  3244. }
  3245. break;
  3246. case ShaderLanguage::TYPE_VEC3:
  3247. if (p_uniform.array_size > 0) {
  3248. pi.type = Variant::PACKED_VECTOR3_ARRAY;
  3249. } else {
  3250. pi.type = Variant::VECTOR3;
  3251. }
  3252. break;
  3253. case ShaderLanguage::TYPE_VEC4: {
  3254. if (p_uniform.array_size > 0) {
  3255. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_COLOR) {
  3256. pi.type = Variant::PACKED_COLOR_ARRAY;
  3257. } else {
  3258. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  3259. }
  3260. } else {
  3261. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_COLOR) {
  3262. pi.type = Variant::COLOR;
  3263. } else {
  3264. pi.type = Variant::PLANE;
  3265. }
  3266. }
  3267. } break;
  3268. case ShaderLanguage::TYPE_MAT2:
  3269. if (p_uniform.array_size > 0) {
  3270. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  3271. } else {
  3272. pi.type = Variant::TRANSFORM2D;
  3273. }
  3274. break;
  3275. case ShaderLanguage::TYPE_MAT3:
  3276. if (p_uniform.array_size > 0) {
  3277. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  3278. } else {
  3279. pi.type = Variant::BASIS;
  3280. }
  3281. break;
  3282. case ShaderLanguage::TYPE_MAT4:
  3283. if (p_uniform.array_size > 0) {
  3284. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  3285. } else {
  3286. pi.type = Variant::TRANSFORM3D;
  3287. }
  3288. break;
  3289. case ShaderLanguage::TYPE_SAMPLER2D:
  3290. case ShaderLanguage::TYPE_ISAMPLER2D:
  3291. case ShaderLanguage::TYPE_USAMPLER2D: {
  3292. if (p_uniform.array_size > 0) {
  3293. pi.type = Variant::ARRAY;
  3294. } else {
  3295. pi.type = Variant::OBJECT;
  3296. }
  3297. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3298. pi.hint_string = "Texture2D";
  3299. } break;
  3300. case ShaderLanguage::TYPE_SAMPLER2DARRAY:
  3301. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  3302. case ShaderLanguage::TYPE_USAMPLER2DARRAY: {
  3303. if (p_uniform.array_size > 0) {
  3304. pi.type = Variant::ARRAY;
  3305. } else {
  3306. pi.type = Variant::OBJECT;
  3307. }
  3308. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3309. pi.hint_string = "TextureLayered";
  3310. } break;
  3311. case ShaderLanguage::TYPE_SAMPLER3D:
  3312. case ShaderLanguage::TYPE_ISAMPLER3D:
  3313. case ShaderLanguage::TYPE_USAMPLER3D: {
  3314. if (p_uniform.array_size > 0) {
  3315. pi.type = Variant::ARRAY;
  3316. } else {
  3317. pi.type = Variant::OBJECT;
  3318. }
  3319. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3320. pi.hint_string = "Texture3D";
  3321. } break;
  3322. case ShaderLanguage::TYPE_SAMPLERCUBE:
  3323. case ShaderLanguage::TYPE_SAMPLERCUBEARRAY: {
  3324. if (p_uniform.array_size > 0) {
  3325. pi.type = Variant::ARRAY;
  3326. } else {
  3327. pi.type = Variant::OBJECT;
  3328. }
  3329. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3330. pi.hint_string = "TextureLayered";
  3331. } break;
  3332. case ShaderLanguage::TYPE_STRUCT: {
  3333. // FIXME: Implement this.
  3334. } break;
  3335. case ShaderLanguage::TYPE_MAX:
  3336. break;
  3337. }
  3338. return pi;
  3339. }
  3340. uint32_t ShaderLanguage::get_type_size(DataType p_type) {
  3341. switch (p_type) {
  3342. case TYPE_VOID:
  3343. return 0;
  3344. case TYPE_BOOL:
  3345. case TYPE_INT:
  3346. case TYPE_UINT:
  3347. case TYPE_FLOAT:
  3348. return 4;
  3349. case TYPE_BVEC2:
  3350. case TYPE_IVEC2:
  3351. case TYPE_UVEC2:
  3352. case TYPE_VEC2:
  3353. return 8;
  3354. case TYPE_BVEC3:
  3355. case TYPE_IVEC3:
  3356. case TYPE_UVEC3:
  3357. case TYPE_VEC3:
  3358. return 12;
  3359. case TYPE_BVEC4:
  3360. case TYPE_IVEC4:
  3361. case TYPE_UVEC4:
  3362. case TYPE_VEC4:
  3363. return 16;
  3364. case TYPE_MAT2:
  3365. return 8;
  3366. case TYPE_MAT3:
  3367. return 12;
  3368. case TYPE_MAT4:
  3369. return 16;
  3370. case TYPE_SAMPLER2D:
  3371. case TYPE_ISAMPLER2D:
  3372. case TYPE_USAMPLER2D:
  3373. case TYPE_SAMPLER2DARRAY:
  3374. case TYPE_ISAMPLER2DARRAY:
  3375. case TYPE_USAMPLER2DARRAY:
  3376. case TYPE_SAMPLER3D:
  3377. case TYPE_ISAMPLER3D:
  3378. case TYPE_USAMPLER3D:
  3379. case TYPE_SAMPLERCUBE:
  3380. case TYPE_SAMPLERCUBEARRAY:
  3381. return 4; //not really, but useful for indices
  3382. case TYPE_STRUCT:
  3383. // FIXME: Implement.
  3384. return 0;
  3385. case ShaderLanguage::TYPE_MAX:
  3386. return 0;
  3387. }
  3388. return 0;
  3389. }
  3390. void ShaderLanguage::get_keyword_list(List<String> *r_keywords) {
  3391. Set<String> kws;
  3392. int idx = 0;
  3393. while (keyword_list[idx].text) {
  3394. kws.insert(keyword_list[idx].text);
  3395. idx++;
  3396. }
  3397. idx = 0;
  3398. while (builtin_func_defs[idx].name) {
  3399. kws.insert(builtin_func_defs[idx].name);
  3400. idx++;
  3401. }
  3402. for (Set<String>::Element *E = kws.front(); E; E = E->next()) {
  3403. r_keywords->push_back(E->get());
  3404. }
  3405. }
  3406. bool ShaderLanguage::is_control_flow_keyword(String p_keyword) {
  3407. return p_keyword == "break" ||
  3408. p_keyword == "case" ||
  3409. p_keyword == "continue" ||
  3410. p_keyword == "default" ||
  3411. p_keyword == "do" ||
  3412. p_keyword == "else" ||
  3413. p_keyword == "for" ||
  3414. p_keyword == "if" ||
  3415. p_keyword == "return" ||
  3416. p_keyword == "switch" ||
  3417. p_keyword == "while";
  3418. }
  3419. void ShaderLanguage::get_builtin_funcs(List<String> *r_keywords) {
  3420. Set<String> kws;
  3421. int idx = 0;
  3422. while (builtin_func_defs[idx].name) {
  3423. kws.insert(builtin_func_defs[idx].name);
  3424. idx++;
  3425. }
  3426. for (Set<String>::Element *E = kws.front(); E; E = E->next()) {
  3427. r_keywords->push_back(E->get());
  3428. }
  3429. }
  3430. ShaderLanguage::DataType ShaderLanguage::get_scalar_type(DataType p_type) {
  3431. static const DataType scalar_types[] = {
  3432. TYPE_VOID,
  3433. TYPE_BOOL,
  3434. TYPE_BOOL,
  3435. TYPE_BOOL,
  3436. TYPE_BOOL,
  3437. TYPE_INT,
  3438. TYPE_INT,
  3439. TYPE_INT,
  3440. TYPE_INT,
  3441. TYPE_UINT,
  3442. TYPE_UINT,
  3443. TYPE_UINT,
  3444. TYPE_UINT,
  3445. TYPE_FLOAT,
  3446. TYPE_FLOAT,
  3447. TYPE_FLOAT,
  3448. TYPE_FLOAT,
  3449. TYPE_FLOAT,
  3450. TYPE_FLOAT,
  3451. TYPE_FLOAT,
  3452. TYPE_FLOAT,
  3453. TYPE_INT,
  3454. TYPE_UINT,
  3455. TYPE_FLOAT,
  3456. };
  3457. return scalar_types[p_type];
  3458. }
  3459. int ShaderLanguage::get_cardinality(DataType p_type) {
  3460. static const int cardinality_table[] = {
  3461. 0,
  3462. 1,
  3463. 2,
  3464. 3,
  3465. 4,
  3466. 1,
  3467. 2,
  3468. 3,
  3469. 4,
  3470. 1,
  3471. 2,
  3472. 3,
  3473. 4,
  3474. 1,
  3475. 2,
  3476. 3,
  3477. 4,
  3478. 4,
  3479. 9,
  3480. 16,
  3481. 1,
  3482. 1,
  3483. 1,
  3484. 1,
  3485. };
  3486. return cardinality_table[p_type];
  3487. }
  3488. bool ShaderLanguage::_get_completable_identifier(BlockNode *p_block, CompletionType p_type, StringName &identifier) {
  3489. identifier = StringName();
  3490. TkPos pos = { 0, 0 };
  3491. Token tk = _get_token();
  3492. if (tk.type == TK_IDENTIFIER) {
  3493. identifier = tk.text;
  3494. pos = _get_tkpos();
  3495. tk = _get_token();
  3496. }
  3497. if (tk.type == TK_CURSOR) {
  3498. completion_type = p_type;
  3499. completion_line = tk_line;
  3500. completion_block = p_block;
  3501. pos = _get_tkpos();
  3502. tk = _get_token();
  3503. if (tk.type == TK_IDENTIFIER) {
  3504. identifier = identifier.operator String() + tk.text.operator String();
  3505. } else {
  3506. _set_tkpos(pos);
  3507. }
  3508. return true;
  3509. } else if (identifier != StringName()) {
  3510. _set_tkpos(pos);
  3511. }
  3512. return false;
  3513. }
  3514. bool ShaderLanguage::_is_operator_assign(Operator p_op) const {
  3515. switch (p_op) {
  3516. case OP_ASSIGN:
  3517. case OP_ASSIGN_ADD:
  3518. case OP_ASSIGN_SUB:
  3519. case OP_ASSIGN_MUL:
  3520. case OP_ASSIGN_DIV:
  3521. case OP_ASSIGN_MOD:
  3522. case OP_ASSIGN_SHIFT_LEFT:
  3523. case OP_ASSIGN_SHIFT_RIGHT:
  3524. case OP_ASSIGN_BIT_AND:
  3525. case OP_ASSIGN_BIT_OR:
  3526. case OP_ASSIGN_BIT_XOR:
  3527. return true;
  3528. default:
  3529. return false;
  3530. }
  3531. return false;
  3532. }
  3533. bool ShaderLanguage::_validate_varying_assign(ShaderNode::Varying &p_varying, String *r_message) {
  3534. if (current_function != String("vertex") && current_function != String("fragment")) {
  3535. *r_message = vformat(RTR("Varying may not be assigned in the '%s' function."), current_function);
  3536. return false;
  3537. }
  3538. switch (p_varying.stage) {
  3539. case ShaderNode::Varying::STAGE_UNKNOWN: // first assign
  3540. if (current_function == varying_function_names.vertex) {
  3541. p_varying.stage = ShaderNode::Varying::STAGE_VERTEX;
  3542. } else if (current_function == varying_function_names.fragment) {
  3543. p_varying.stage = ShaderNode::Varying::STAGE_FRAGMENT;
  3544. }
  3545. break;
  3546. case ShaderNode::Varying::STAGE_VERTEX_TO_FRAGMENT_LIGHT:
  3547. case ShaderNode::Varying::STAGE_VERTEX:
  3548. if (current_function == varying_function_names.fragment) {
  3549. *r_message = RTR("Varyings which assigned in 'vertex' function may not be reassigned in 'fragment' or 'light'.");
  3550. return false;
  3551. }
  3552. break;
  3553. case ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT:
  3554. case ShaderNode::Varying::STAGE_FRAGMENT:
  3555. if (current_function == varying_function_names.vertex) {
  3556. *r_message = RTR("Varyings which assigned in 'fragment' function may not be reassigned in 'vertex' or 'light'.");
  3557. return false;
  3558. }
  3559. break;
  3560. default:
  3561. break;
  3562. }
  3563. return true;
  3564. }
  3565. bool ShaderLanguage::_validate_varying_using(ShaderNode::Varying &p_varying, String *r_message) {
  3566. switch (p_varying.stage) {
  3567. case ShaderNode::Varying::STAGE_UNKNOWN:
  3568. VaryingUsage usage;
  3569. usage.var = &p_varying;
  3570. usage.line = tk_line;
  3571. unknown_varying_usages.push_back(usage);
  3572. break;
  3573. case ShaderNode::Varying::STAGE_VERTEX:
  3574. if (current_function == varying_function_names.fragment || current_function == varying_function_names.light) {
  3575. p_varying.stage = ShaderNode::Varying::STAGE_VERTEX_TO_FRAGMENT_LIGHT;
  3576. }
  3577. break;
  3578. case ShaderNode::Varying::STAGE_FRAGMENT:
  3579. if (current_function == varying_function_names.light) {
  3580. p_varying.stage = ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT;
  3581. }
  3582. break;
  3583. default:
  3584. break;
  3585. }
  3586. return true;
  3587. }
  3588. bool ShaderLanguage::_check_varying_usages(int *r_error_line, String *r_error_message) const {
  3589. for (const List<ShaderLanguage::VaryingUsage>::Element *E = unknown_varying_usages.front(); E; E = E->next()) {
  3590. ShaderNode::Varying::Stage stage = E->get().var->stage;
  3591. if (stage != ShaderNode::Varying::STAGE_UNKNOWN && stage != ShaderNode::Varying::STAGE_VERTEX && stage != ShaderNode::Varying::STAGE_VERTEX_TO_FRAGMENT_LIGHT) {
  3592. *r_error_line = E->get().line;
  3593. *r_error_message = RTR("Fragment-stage varying could not been accessed in custom function!");
  3594. return false;
  3595. }
  3596. }
  3597. return true;
  3598. }
  3599. bool ShaderLanguage::_check_node_constness(const Node *p_node) const {
  3600. switch (p_node->type) {
  3601. case Node::TYPE_OPERATOR: {
  3602. OperatorNode *op_node = (OperatorNode *)p_node;
  3603. for (int i = int(op_node->op == OP_CALL); i < op_node->arguments.size(); i++) {
  3604. if (!_check_node_constness(op_node->arguments[i])) {
  3605. return false;
  3606. }
  3607. }
  3608. } break;
  3609. case Node::TYPE_CONSTANT:
  3610. break;
  3611. case Node::TYPE_VARIABLE: {
  3612. VariableNode *varn = (VariableNode *)p_node;
  3613. if (!varn->is_const) {
  3614. return false;
  3615. }
  3616. } break;
  3617. case Node::TYPE_ARRAY: {
  3618. ArrayNode *arrn = (ArrayNode *)p_node;
  3619. if (!arrn->is_const) {
  3620. return false;
  3621. }
  3622. } break;
  3623. default:
  3624. return false;
  3625. }
  3626. return true;
  3627. }
  3628. bool ShaderLanguage::_validate_assign(Node *p_node, const FunctionInfo &p_function_info, String *r_message) {
  3629. if (p_node->type == Node::TYPE_OPERATOR) {
  3630. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  3631. if (op->op == OP_INDEX) {
  3632. return _validate_assign(op->arguments[0], p_function_info, r_message);
  3633. } else if (_is_operator_assign(op->op)) {
  3634. //chained assignment
  3635. return _validate_assign(op->arguments[1], p_function_info, r_message);
  3636. } else if (op->op == OP_CALL) {
  3637. if (r_message) {
  3638. *r_message = RTR("Assignment to function.");
  3639. }
  3640. return false;
  3641. }
  3642. } else if (p_node->type == Node::TYPE_MEMBER) {
  3643. MemberNode *member = static_cast<MemberNode *>(p_node);
  3644. if (member->has_swizzling_duplicates) {
  3645. if (r_message) {
  3646. *r_message = RTR("Swizzling assignment contains duplicates.");
  3647. }
  3648. return false;
  3649. }
  3650. return _validate_assign(member->owner, p_function_info, r_message);
  3651. } else if (p_node->type == Node::TYPE_VARIABLE) {
  3652. VariableNode *var = static_cast<VariableNode *>(p_node);
  3653. if (shader->uniforms.has(var->name)) {
  3654. if (r_message) {
  3655. *r_message = RTR("Assignment to uniform.");
  3656. }
  3657. return false;
  3658. }
  3659. if (shader->constants.has(var->name) || var->is_const) {
  3660. if (r_message) {
  3661. *r_message = RTR("Constants cannot be modified.");
  3662. }
  3663. return false;
  3664. }
  3665. if (!(p_function_info.built_ins.has(var->name) && p_function_info.built_ins[var->name].constant)) {
  3666. return true;
  3667. }
  3668. } else if (p_node->type == Node::TYPE_ARRAY) {
  3669. ArrayNode *arr = static_cast<ArrayNode *>(p_node);
  3670. if (shader->constants.has(arr->name) || arr->is_const) {
  3671. if (r_message) {
  3672. *r_message = RTR("Constants cannot be modified.");
  3673. }
  3674. return false;
  3675. }
  3676. return true;
  3677. }
  3678. if (r_message) {
  3679. *r_message = "Assignment to constant expression.";
  3680. }
  3681. return false;
  3682. }
  3683. bool ShaderLanguage::_propagate_function_call_sampler_uniform_settings(StringName p_name, int p_argument, TextureFilter p_filter, TextureRepeat p_repeat) {
  3684. for (int i = 0; i < shader->functions.size(); i++) {
  3685. if (shader->functions[i].name == p_name) {
  3686. ERR_FAIL_INDEX_V(p_argument, shader->functions[i].function->arguments.size(), false);
  3687. FunctionNode::Argument *arg = &shader->functions[i].function->arguments.write[p_argument];
  3688. if (arg->tex_builtin_check) {
  3689. _set_error("Sampler argument #" + itos(p_argument) + " of function '" + String(p_name) + "' called more than once using both built-ins and uniform textures, this is not supported (use either one or the other).");
  3690. return false;
  3691. } else if (arg->tex_argument_check) {
  3692. //was checked, verify that filter and repeat are the same
  3693. if (arg->tex_argument_filter == p_filter && arg->tex_argument_repeat == p_repeat) {
  3694. return true;
  3695. } else {
  3696. _set_error("Sampler argument #" + itos(p_argument) + " of function '" + String(p_name) + "' called more than once using textures that differ in either filter or repeat setting.");
  3697. return false;
  3698. }
  3699. } else {
  3700. arg->tex_argument_check = true;
  3701. arg->tex_argument_filter = p_filter;
  3702. arg->tex_argument_repeat = p_repeat;
  3703. for (KeyValue<StringName, Set<int>> &E : arg->tex_argument_connect) {
  3704. for (Set<int>::Element *F = E.value.front(); F; F = F->next()) {
  3705. if (!_propagate_function_call_sampler_uniform_settings(E.key, F->get(), p_filter, p_repeat)) {
  3706. return false;
  3707. }
  3708. }
  3709. }
  3710. return true;
  3711. }
  3712. }
  3713. }
  3714. ERR_FAIL_V(false); //bug? function not found
  3715. }
  3716. bool ShaderLanguage::_propagate_function_call_sampler_builtin_reference(StringName p_name, int p_argument, const StringName &p_builtin) {
  3717. for (int i = 0; i < shader->functions.size(); i++) {
  3718. if (shader->functions[i].name == p_name) {
  3719. ERR_FAIL_INDEX_V(p_argument, shader->functions[i].function->arguments.size(), false);
  3720. FunctionNode::Argument *arg = &shader->functions[i].function->arguments.write[p_argument];
  3721. if (arg->tex_argument_check) {
  3722. _set_error("Sampler argument #" + itos(p_argument) + " of function '" + String(p_name) + "' called more than once using both built-ins and uniform textures, this is not supported (use either one or the other).");
  3723. return false;
  3724. } else if (arg->tex_builtin_check) {
  3725. //was checked, verify that the built-in is the same
  3726. if (arg->tex_builtin == p_builtin) {
  3727. return true;
  3728. } else {
  3729. _set_error("Sampler argument #" + itos(p_argument) + " of function '" + String(p_name) + "' called more than once using different built-ins. Only calling with the same built-in is supported.");
  3730. return false;
  3731. }
  3732. } else {
  3733. arg->tex_builtin_check = true;
  3734. arg->tex_builtin = p_builtin;
  3735. for (KeyValue<StringName, Set<int>> &E : arg->tex_argument_connect) {
  3736. for (Set<int>::Element *F = E.value.front(); F; F = F->next()) {
  3737. if (!_propagate_function_call_sampler_builtin_reference(E.key, F->get(), p_builtin)) {
  3738. return false;
  3739. }
  3740. }
  3741. }
  3742. return true;
  3743. }
  3744. }
  3745. }
  3746. ERR_FAIL_V(false); //bug? function not found
  3747. }
  3748. ShaderLanguage::Node *ShaderLanguage::_parse_array_size(BlockNode *p_block, const FunctionInfo &p_function_info, int &r_array_size) {
  3749. int array_size = 0;
  3750. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  3751. if (n) {
  3752. if (n->type == Node::TYPE_VARIABLE) {
  3753. VariableNode *vn = static_cast<VariableNode *>(n);
  3754. if (vn) {
  3755. ConstantNode::Value v;
  3756. DataType data_type;
  3757. bool is_const = false;
  3758. _find_identifier(p_block, false, p_function_info, vn->name, &data_type, nullptr, &is_const, nullptr, nullptr, &v);
  3759. if (is_const) {
  3760. if (data_type == TYPE_INT) {
  3761. int32_t value = v.sint;
  3762. if (value > 0) {
  3763. array_size = value;
  3764. }
  3765. } else if (data_type == TYPE_UINT) {
  3766. uint32_t value = v.uint;
  3767. if (value > 0U) {
  3768. array_size = value;
  3769. }
  3770. }
  3771. }
  3772. }
  3773. } else if (n->type == Node::TYPE_OPERATOR) {
  3774. _set_error("Array size expressions are not yet implemented.");
  3775. return nullptr;
  3776. }
  3777. }
  3778. r_array_size = array_size;
  3779. return n;
  3780. }
  3781. Error ShaderLanguage::_parse_global_array_size(int &r_array_size) {
  3782. if (r_array_size > 0) {
  3783. _set_error("Array size is already defined!");
  3784. return ERR_PARSE_ERROR;
  3785. }
  3786. TkPos pos = _get_tkpos();
  3787. Token tk = _get_token();
  3788. int array_size = 0;
  3789. if (tk.type != TK_INT_CONSTANT || ((int)tk.constant) <= 0) {
  3790. _set_tkpos(pos);
  3791. Node *n = _parse_array_size(nullptr, FunctionInfo(), array_size);
  3792. if (!n) {
  3793. return ERR_PARSE_ERROR;
  3794. }
  3795. } else if (((int)tk.constant) > 0) {
  3796. array_size = (uint32_t)tk.constant;
  3797. }
  3798. if (array_size <= 0) {
  3799. _set_error("Expected single integer constant > 0");
  3800. return ERR_PARSE_ERROR;
  3801. }
  3802. tk = _get_token();
  3803. if (tk.type != TK_BRACKET_CLOSE) {
  3804. _set_error("Expected ']'");
  3805. return ERR_PARSE_ERROR;
  3806. }
  3807. r_array_size = array_size;
  3808. return OK;
  3809. }
  3810. Error ShaderLanguage::_parse_local_array_size(BlockNode *p_block, const FunctionInfo &p_function_info, ArrayDeclarationNode *p_node, ArrayDeclarationNode::Declaration *p_decl, int &r_array_size, bool &r_is_unknown_size) {
  3811. TkPos pos = _get_tkpos();
  3812. Token tk = _get_token();
  3813. if (tk.type == TK_BRACKET_CLOSE) {
  3814. r_is_unknown_size = true;
  3815. } else {
  3816. if (tk.type != TK_INT_CONSTANT || ((int)tk.constant) <= 0) {
  3817. _set_tkpos(pos);
  3818. int array_size = 0;
  3819. Node *n = _parse_array_size(p_block, p_function_info, array_size);
  3820. if (!n) {
  3821. return ERR_PARSE_ERROR;
  3822. }
  3823. p_decl->size = array_size;
  3824. p_node->size_expression = n;
  3825. } else if (((int)tk.constant) > 0) {
  3826. p_decl->size = (uint32_t)tk.constant;
  3827. }
  3828. if (p_decl->size <= 0) {
  3829. _set_error("Expected single integer constant > 0");
  3830. return ERR_PARSE_ERROR;
  3831. }
  3832. tk = _get_token();
  3833. if (tk.type != TK_BRACKET_CLOSE) {
  3834. _set_error("Expected ']'");
  3835. return ERR_PARSE_ERROR;
  3836. }
  3837. r_array_size = p_decl->size;
  3838. }
  3839. return OK;
  3840. }
  3841. ShaderLanguage::Node *ShaderLanguage::_parse_array_constructor(BlockNode *p_block, const FunctionInfo &p_function_info) {
  3842. DataType type = TYPE_VOID;
  3843. String struct_name = "";
  3844. int array_size = 0;
  3845. bool auto_size = false;
  3846. bool undefined_size = false;
  3847. Token tk = _get_token();
  3848. if (tk.type == TK_CURLY_BRACKET_OPEN) {
  3849. auto_size = true;
  3850. } else {
  3851. if (shader->structs.has(tk.text)) {
  3852. type = TYPE_STRUCT;
  3853. struct_name = tk.text;
  3854. } else {
  3855. if (!is_token_variable_datatype(tk.type)) {
  3856. _set_error("Invalid data type for array");
  3857. return nullptr;
  3858. }
  3859. type = get_token_datatype(tk.type);
  3860. }
  3861. tk = _get_token();
  3862. if (tk.type == TK_BRACKET_OPEN) {
  3863. TkPos pos = _get_tkpos();
  3864. tk = _get_token();
  3865. if (tk.type == TK_BRACKET_CLOSE) {
  3866. undefined_size = true;
  3867. tk = _get_token();
  3868. } else {
  3869. _set_tkpos(pos);
  3870. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  3871. if (!n || n->type != Node::TYPE_CONSTANT || n->get_datatype() != TYPE_INT) {
  3872. _set_error("Expected single integer constant > 0");
  3873. return nullptr;
  3874. }
  3875. ConstantNode *cnode = (ConstantNode *)n;
  3876. if (cnode->values.size() == 1) {
  3877. array_size = cnode->values[0].sint;
  3878. if (array_size <= 0) {
  3879. _set_error("Expected single integer constant > 0");
  3880. return nullptr;
  3881. }
  3882. } else {
  3883. _set_error("Expected single integer constant > 0");
  3884. return nullptr;
  3885. }
  3886. tk = _get_token();
  3887. if (tk.type != TK_BRACKET_CLOSE) {
  3888. _set_error("Expected ']'");
  3889. return nullptr;
  3890. } else {
  3891. tk = _get_token();
  3892. }
  3893. }
  3894. } else {
  3895. _set_error("Expected '['");
  3896. return nullptr;
  3897. }
  3898. }
  3899. ArrayConstructNode *an = alloc_node<ArrayConstructNode>();
  3900. if (tk.type == TK_PARENTHESIS_OPEN || auto_size) { // initialization
  3901. int idx = 0;
  3902. while (true) {
  3903. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  3904. if (!n) {
  3905. return nullptr;
  3906. }
  3907. // define type by using the first member
  3908. if (auto_size && idx == 0) {
  3909. type = n->get_datatype();
  3910. if (type == TYPE_STRUCT) {
  3911. struct_name = n->get_datatype_name();
  3912. }
  3913. } else {
  3914. if (!_compare_datatypes(type, struct_name, 0, n->get_datatype(), n->get_datatype_name(), 0)) {
  3915. return nullptr;
  3916. }
  3917. }
  3918. tk = _get_token();
  3919. if (tk.type == TK_COMMA) {
  3920. an->initializer.push_back(n);
  3921. } else if (!auto_size && tk.type == TK_PARENTHESIS_CLOSE) {
  3922. an->initializer.push_back(n);
  3923. break;
  3924. } else if (auto_size && tk.type == TK_CURLY_BRACKET_CLOSE) {
  3925. an->initializer.push_back(n);
  3926. break;
  3927. } else {
  3928. if (auto_size) {
  3929. _set_error("Expected '}' or ','");
  3930. } else {
  3931. _set_error("Expected ')' or ','");
  3932. }
  3933. return nullptr;
  3934. }
  3935. idx++;
  3936. }
  3937. if (!auto_size && !undefined_size && an->initializer.size() != array_size) {
  3938. _set_error("Array size mismatch");
  3939. return nullptr;
  3940. }
  3941. } else {
  3942. _set_error("Expected array initialization!");
  3943. return nullptr;
  3944. }
  3945. an->datatype = type;
  3946. an->struct_name = struct_name;
  3947. return an;
  3948. }
  3949. ShaderLanguage::Node *ShaderLanguage::_parse_array_constructor(BlockNode *p_block, const FunctionInfo &p_function_info, DataType p_type, const StringName &p_struct_name, int p_array_size) {
  3950. DataType type = TYPE_VOID;
  3951. String struct_name = "";
  3952. int array_size = 0;
  3953. bool auto_size = false;
  3954. TkPos prev_pos = _get_tkpos();
  3955. Token tk = _get_token();
  3956. if (tk.type == TK_CURLY_BRACKET_OPEN) {
  3957. auto_size = true;
  3958. } else {
  3959. if (shader->structs.has(tk.text)) {
  3960. type = TYPE_STRUCT;
  3961. struct_name = tk.text;
  3962. } else {
  3963. if (!is_token_variable_datatype(tk.type)) {
  3964. _set_tkpos(prev_pos);
  3965. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  3966. if (!n) {
  3967. _set_error("Invalid data type for array");
  3968. return nullptr;
  3969. }
  3970. if (!_compare_datatypes(p_type, p_struct_name, p_array_size, n->get_datatype(), n->get_datatype_name(), n->get_array_size())) {
  3971. return nullptr;
  3972. }
  3973. return n;
  3974. }
  3975. type = get_token_datatype(tk.type);
  3976. }
  3977. tk = _get_token();
  3978. if (tk.type == TK_BRACKET_OPEN) {
  3979. TkPos pos = _get_tkpos();
  3980. tk = _get_token();
  3981. if (tk.type == TK_BRACKET_CLOSE) {
  3982. array_size = p_array_size;
  3983. tk = _get_token();
  3984. } else {
  3985. _set_tkpos(pos);
  3986. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  3987. if (!n || n->type != Node::TYPE_CONSTANT || n->get_datatype() != TYPE_INT) {
  3988. _set_error("Expected single integer constant > 0");
  3989. return nullptr;
  3990. }
  3991. ConstantNode *cnode = (ConstantNode *)n;
  3992. if (cnode->values.size() == 1) {
  3993. array_size = cnode->values[0].sint;
  3994. if (array_size <= 0) {
  3995. _set_error("Expected single integer constant > 0");
  3996. return nullptr;
  3997. }
  3998. } else {
  3999. _set_error("Expected single integer constant > 0");
  4000. return nullptr;
  4001. }
  4002. tk = _get_token();
  4003. if (tk.type != TK_BRACKET_CLOSE) {
  4004. _set_error("Expected ']'");
  4005. return nullptr;
  4006. } else {
  4007. tk = _get_token();
  4008. }
  4009. }
  4010. } else {
  4011. _set_error("Expected '['");
  4012. return nullptr;
  4013. }
  4014. if (type != p_type || struct_name != p_struct_name || array_size != p_array_size) {
  4015. String error_str = "Cannot convert from '";
  4016. if (type == TYPE_STRUCT) {
  4017. error_str += struct_name;
  4018. } else {
  4019. error_str += get_datatype_name(type);
  4020. }
  4021. error_str += "[";
  4022. error_str += itos(array_size);
  4023. error_str += "]'";
  4024. error_str += " to '";
  4025. if (type == TYPE_STRUCT) {
  4026. error_str += p_struct_name;
  4027. } else {
  4028. error_str += get_datatype_name(p_type);
  4029. }
  4030. error_str += "[";
  4031. error_str += itos(p_array_size);
  4032. error_str += "]'";
  4033. _set_error(error_str);
  4034. return nullptr;
  4035. }
  4036. }
  4037. ArrayConstructNode *an = alloc_node<ArrayConstructNode>();
  4038. an->datatype = p_type;
  4039. an->struct_name = p_struct_name;
  4040. if (tk.type == TK_PARENTHESIS_OPEN || auto_size) { // initialization
  4041. while (true) {
  4042. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  4043. if (!n) {
  4044. return nullptr;
  4045. }
  4046. if (!_compare_datatypes(p_type, p_struct_name, 0, n->get_datatype(), n->get_datatype_name(), n->get_array_size())) {
  4047. return nullptr;
  4048. }
  4049. tk = _get_token();
  4050. if (tk.type == TK_COMMA) {
  4051. an->initializer.push_back(n);
  4052. } else if (!auto_size && tk.type == TK_PARENTHESIS_CLOSE) {
  4053. an->initializer.push_back(n);
  4054. break;
  4055. } else if (auto_size && tk.type == TK_CURLY_BRACKET_CLOSE) {
  4056. an->initializer.push_back(n);
  4057. break;
  4058. } else {
  4059. if (auto_size) {
  4060. _set_error("Expected '}' or ','");
  4061. } else {
  4062. _set_error("Expected ')' or ','");
  4063. }
  4064. return nullptr;
  4065. }
  4066. }
  4067. if (an->initializer.size() != p_array_size) {
  4068. _set_error("Array size mismatch");
  4069. return nullptr;
  4070. }
  4071. } else {
  4072. _set_error("Expected array initialization!");
  4073. return nullptr;
  4074. }
  4075. return an;
  4076. }
  4077. ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, const FunctionInfo &p_function_info) {
  4078. Vector<Expression> expression;
  4079. //Vector<TokenType> operators;
  4080. while (true) {
  4081. Node *expr = nullptr;
  4082. TkPos prepos = _get_tkpos();
  4083. Token tk = _get_token();
  4084. TkPos pos = _get_tkpos();
  4085. bool is_const = false;
  4086. if (tk.type == TK_PARENTHESIS_OPEN) {
  4087. //handle subexpression
  4088. expr = _parse_and_reduce_expression(p_block, p_function_info);
  4089. if (!expr) {
  4090. return nullptr;
  4091. }
  4092. tk = _get_token();
  4093. if (tk.type != TK_PARENTHESIS_CLOSE) {
  4094. _set_error("Expected ')' in expression");
  4095. return nullptr;
  4096. }
  4097. } else if (tk.type == TK_FLOAT_CONSTANT) {
  4098. ConstantNode *constant = alloc_node<ConstantNode>();
  4099. ConstantNode::Value v;
  4100. v.real = tk.constant;
  4101. constant->values.push_back(v);
  4102. constant->datatype = TYPE_FLOAT;
  4103. expr = constant;
  4104. } else if (tk.type == TK_INT_CONSTANT) {
  4105. ConstantNode *constant = alloc_node<ConstantNode>();
  4106. ConstantNode::Value v;
  4107. v.sint = tk.constant;
  4108. constant->values.push_back(v);
  4109. constant->datatype = TYPE_INT;
  4110. expr = constant;
  4111. } else if (tk.type == TK_TRUE) {
  4112. //handle true constant
  4113. ConstantNode *constant = alloc_node<ConstantNode>();
  4114. ConstantNode::Value v;
  4115. v.boolean = true;
  4116. constant->values.push_back(v);
  4117. constant->datatype = TYPE_BOOL;
  4118. expr = constant;
  4119. } else if (tk.type == TK_FALSE) {
  4120. //handle false constant
  4121. ConstantNode *constant = alloc_node<ConstantNode>();
  4122. ConstantNode::Value v;
  4123. v.boolean = false;
  4124. constant->values.push_back(v);
  4125. constant->datatype = TYPE_BOOL;
  4126. expr = constant;
  4127. } else if (tk.type == TK_TYPE_VOID) {
  4128. //make sure void is not used in expression
  4129. _set_error("Void value not allowed in Expression");
  4130. return nullptr;
  4131. } else if (is_token_nonvoid_datatype(tk.type) || tk.type == TK_CURLY_BRACKET_OPEN) {
  4132. if (tk.type == TK_CURLY_BRACKET_OPEN) {
  4133. //array constructor
  4134. _set_tkpos(prepos);
  4135. expr = _parse_array_constructor(p_block, p_function_info);
  4136. } else {
  4137. DataType datatype;
  4138. DataPrecision precision;
  4139. bool precision_defined = false;
  4140. if (is_token_precision(tk.type)) {
  4141. precision = get_token_precision(tk.type);
  4142. precision_defined = true;
  4143. tk = _get_token();
  4144. }
  4145. datatype = get_token_datatype(tk.type);
  4146. tk = _get_token();
  4147. if (tk.type == TK_BRACKET_OPEN) {
  4148. //array constructor
  4149. _set_tkpos(prepos);
  4150. expr = _parse_array_constructor(p_block, p_function_info);
  4151. } else {
  4152. if (tk.type != TK_PARENTHESIS_OPEN) {
  4153. _set_error("Expected '(' after type name");
  4154. return nullptr;
  4155. }
  4156. //basic type constructor
  4157. OperatorNode *func = alloc_node<OperatorNode>();
  4158. func->op = OP_CONSTRUCT;
  4159. if (precision_defined) {
  4160. func->return_precision_cache = precision;
  4161. }
  4162. VariableNode *funcname = alloc_node<VariableNode>();
  4163. funcname->name = get_datatype_name(datatype);
  4164. func->arguments.push_back(funcname);
  4165. int carg = -1;
  4166. bool ok = _parse_function_arguments(p_block, p_function_info, func, &carg);
  4167. if (carg >= 0) {
  4168. completion_type = COMPLETION_CALL_ARGUMENTS;
  4169. completion_line = tk_line;
  4170. completion_block = p_block;
  4171. completion_function = funcname->name;
  4172. completion_argument = carg;
  4173. }
  4174. if (!ok) {
  4175. return nullptr;
  4176. }
  4177. if (!_validate_function_call(p_block, p_function_info, func, &func->return_cache, &func->struct_name)) {
  4178. _set_error("No matching constructor found for: '" + String(funcname->name) + "'");
  4179. return nullptr;
  4180. }
  4181. expr = _reduce_expression(p_block, func);
  4182. }
  4183. }
  4184. } else if (tk.type == TK_IDENTIFIER) {
  4185. _set_tkpos(prepos);
  4186. StringName identifier;
  4187. StructNode *pstruct = nullptr;
  4188. bool struct_init = false;
  4189. _get_completable_identifier(p_block, COMPLETION_IDENTIFIER, identifier);
  4190. if (shader->structs.has(identifier)) {
  4191. pstruct = shader->structs[identifier].shader_struct;
  4192. #ifdef DEBUG_ENABLED
  4193. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_STRUCT_FLAG) && used_structs.has(identifier)) {
  4194. used_structs[identifier].used = true;
  4195. }
  4196. #endif // DEBUG_ENABLED
  4197. struct_init = true;
  4198. }
  4199. tk = _get_token();
  4200. if (tk.type == TK_PARENTHESIS_OPEN) {
  4201. if (struct_init) { //a struct constructor
  4202. const StringName &name = identifier;
  4203. OperatorNode *func = alloc_node<OperatorNode>();
  4204. func->op = OP_STRUCT;
  4205. func->struct_name = name;
  4206. func->return_cache = TYPE_STRUCT;
  4207. VariableNode *funcname = alloc_node<VariableNode>();
  4208. funcname->name = name;
  4209. func->arguments.push_back(funcname);
  4210. for (int i = 0; i < pstruct->members.size(); i++) {
  4211. Node *nexpr;
  4212. if (pstruct->members[i]->array_size != 0) {
  4213. nexpr = _parse_array_constructor(p_block, p_function_info, pstruct->members[i]->get_datatype(), pstruct->members[i]->struct_name, pstruct->members[i]->array_size);
  4214. if (!nexpr) {
  4215. return nullptr;
  4216. }
  4217. } else {
  4218. nexpr = _parse_and_reduce_expression(p_block, p_function_info);
  4219. if (!nexpr) {
  4220. return nullptr;
  4221. }
  4222. if (!_compare_datatypes_in_nodes(pstruct->members[i], nexpr)) {
  4223. return nullptr;
  4224. }
  4225. }
  4226. if (i + 1 < pstruct->members.size()) {
  4227. tk = _get_token();
  4228. if (tk.type != TK_COMMA) {
  4229. _set_error("Expected ','");
  4230. return nullptr;
  4231. }
  4232. }
  4233. func->arguments.push_back(nexpr);
  4234. }
  4235. tk = _get_token();
  4236. if (tk.type != TK_PARENTHESIS_CLOSE) {
  4237. _set_error("Expected ')'");
  4238. return nullptr;
  4239. }
  4240. expr = func;
  4241. } else { //a function call
  4242. const StringName &name = identifier;
  4243. OperatorNode *func = alloc_node<OperatorNode>();
  4244. func->op = OP_CALL;
  4245. VariableNode *funcname = alloc_node<VariableNode>();
  4246. funcname->name = name;
  4247. func->arguments.push_back(funcname);
  4248. int carg = -1;
  4249. bool ok = _parse_function_arguments(p_block, p_function_info, func, &carg);
  4250. // Check if block has a variable with the same name as function to prevent shader crash.
  4251. ShaderLanguage::BlockNode *bnode = p_block;
  4252. while (bnode) {
  4253. if (bnode->variables.has(name)) {
  4254. _set_error("Expected function name");
  4255. return nullptr;
  4256. }
  4257. bnode = bnode->parent_block;
  4258. }
  4259. //test if function was parsed first
  4260. int function_index = -1;
  4261. for (int i = 0; i < shader->functions.size(); i++) {
  4262. if (shader->functions[i].name == name) {
  4263. //add to current function as dependency
  4264. for (int j = 0; j < shader->functions.size(); j++) {
  4265. if (shader->functions[j].name == current_function) {
  4266. shader->functions.write[j].uses_function.insert(name);
  4267. break;
  4268. }
  4269. }
  4270. //see if texture arguments must connect
  4271. function_index = i;
  4272. break;
  4273. }
  4274. }
  4275. if (carg >= 0) {
  4276. completion_type = COMPLETION_CALL_ARGUMENTS;
  4277. completion_line = tk_line;
  4278. completion_block = p_block;
  4279. completion_function = funcname->name;
  4280. completion_argument = carg;
  4281. }
  4282. if (!ok) {
  4283. return nullptr;
  4284. }
  4285. if (!_validate_function_call(p_block, p_function_info, func, &func->return_cache, &func->struct_name)) {
  4286. _set_error("No matching function found for: '" + String(funcname->name) + "'");
  4287. return nullptr;
  4288. }
  4289. completion_class = TAG_GLOBAL; // reset sub-class
  4290. if (function_index >= 0) {
  4291. //connect texture arguments, so we can cache in the
  4292. //argument what type of filter and repeat to use
  4293. FunctionNode *call_function = shader->functions[function_index].function;
  4294. if (call_function) {
  4295. func->return_cache = call_function->get_datatype();
  4296. func->struct_name = call_function->get_datatype_name();
  4297. func->return_array_size = call_function->get_array_size();
  4298. //get current base function
  4299. FunctionNode *base_function = nullptr;
  4300. {
  4301. BlockNode *b = p_block;
  4302. while (b) {
  4303. if (b->parent_function) {
  4304. base_function = b->parent_function;
  4305. break;
  4306. } else {
  4307. b = b->parent_block;
  4308. }
  4309. }
  4310. }
  4311. ERR_FAIL_COND_V(!base_function, nullptr); //bug, wtf
  4312. for (int i = 0; i < call_function->arguments.size(); i++) {
  4313. int argidx = i + 1;
  4314. if (argidx < func->arguments.size()) {
  4315. if (call_function->arguments[i].is_const || call_function->arguments[i].qualifier == ArgumentQualifier::ARGUMENT_QUALIFIER_OUT || call_function->arguments[i].qualifier == ArgumentQualifier::ARGUMENT_QUALIFIER_INOUT) {
  4316. bool error = false;
  4317. Node *n = func->arguments[argidx];
  4318. if (n->type == Node::TYPE_CONSTANT || n->type == Node::TYPE_OPERATOR) {
  4319. error = true;
  4320. } else if (n->type == Node::TYPE_ARRAY) {
  4321. ArrayNode *an = static_cast<ArrayNode *>(n);
  4322. if (an->call_expression != nullptr || an->is_const) {
  4323. error = true;
  4324. }
  4325. } else if (n->type == Node::TYPE_VARIABLE) {
  4326. VariableNode *vn = static_cast<VariableNode *>(n);
  4327. if (vn->is_const) {
  4328. error = true;
  4329. } else {
  4330. StringName varname = vn->name;
  4331. if (shader->constants.has(varname)) {
  4332. error = true;
  4333. } else if (shader->uniforms.has(varname)) {
  4334. error = true;
  4335. } else {
  4336. if (shader->varyings.has(varname)) {
  4337. _set_error(vformat("Varyings cannot be passed for '%s' parameter!", _get_qualifier_str(call_function->arguments[i].qualifier)));
  4338. return nullptr;
  4339. }
  4340. if (p_function_info.built_ins.has(varname)) {
  4341. BuiltInInfo info = p_function_info.built_ins[varname];
  4342. if (info.constant) {
  4343. error = true;
  4344. }
  4345. }
  4346. }
  4347. }
  4348. } else if (n->type == Node::TYPE_MEMBER) {
  4349. MemberNode *mn = static_cast<MemberNode *>(n);
  4350. if (mn->basetype_const) {
  4351. error = true;
  4352. }
  4353. }
  4354. if (error) {
  4355. _set_error(vformat("Constant value cannot be passed for '%s' parameter!", _get_qualifier_str(call_function->arguments[i].qualifier)));
  4356. return nullptr;
  4357. }
  4358. }
  4359. if (is_sampler_type(call_function->arguments[i].type)) {
  4360. //let's see where our argument comes from
  4361. Node *n = func->arguments[argidx];
  4362. ERR_CONTINUE(n->type != Node::TYPE_VARIABLE); //bug? this should always be a variable
  4363. VariableNode *vn = static_cast<VariableNode *>(n);
  4364. StringName varname = vn->name;
  4365. if (shader->uniforms.has(varname)) {
  4366. //being sampler, this either comes from a uniform
  4367. ShaderNode::Uniform *u = &shader->uniforms[varname];
  4368. ERR_CONTINUE(u->type != call_function->arguments[i].type); //this should have been validated previously
  4369. //propagate
  4370. if (!_propagate_function_call_sampler_uniform_settings(name, i, u->filter, u->repeat)) {
  4371. return nullptr;
  4372. }
  4373. } else if (p_function_info.built_ins.has(varname)) {
  4374. //a built-in
  4375. if (!_propagate_function_call_sampler_builtin_reference(name, i, varname)) {
  4376. return nullptr;
  4377. }
  4378. } else {
  4379. //or this comes from an argument, but nothing else can be a sampler
  4380. bool found = false;
  4381. for (int j = 0; j < base_function->arguments.size(); j++) {
  4382. if (base_function->arguments[j].name == varname) {
  4383. if (!base_function->arguments[j].tex_argument_connect.has(call_function->name)) {
  4384. base_function->arguments.write[j].tex_argument_connect[call_function->name] = Set<int>();
  4385. }
  4386. base_function->arguments.write[j].tex_argument_connect[call_function->name].insert(i);
  4387. found = true;
  4388. break;
  4389. }
  4390. }
  4391. ERR_CONTINUE(!found);
  4392. }
  4393. }
  4394. } else {
  4395. break;
  4396. }
  4397. }
  4398. }
  4399. }
  4400. expr = func;
  4401. #ifdef DEBUG_ENABLED
  4402. if (check_warnings) {
  4403. StringName func_name;
  4404. if (p_block && p_block->parent_function) {
  4405. func_name = p_block->parent_function->name;
  4406. }
  4407. _parse_used_identifier(name, IdentifierType::IDENTIFIER_FUNCTION, func_name);
  4408. }
  4409. #endif // DEBUG_ENABLED
  4410. }
  4411. } else {
  4412. //an identifier
  4413. last_name = identifier;
  4414. last_type = IDENTIFIER_MAX;
  4415. _set_tkpos(pos);
  4416. DataType data_type;
  4417. IdentifierType ident_type;
  4418. int array_size = 0;
  4419. StringName struct_name;
  4420. bool is_local = false;
  4421. if (p_block && p_block->block_tag != SubClassTag::TAG_GLOBAL) {
  4422. int idx = 0;
  4423. bool found = false;
  4424. while (builtin_func_defs[idx].name) {
  4425. if (builtin_func_defs[idx].tag == p_block->block_tag && builtin_func_defs[idx].name == identifier) {
  4426. found = true;
  4427. break;
  4428. }
  4429. idx++;
  4430. }
  4431. if (!found) {
  4432. _set_error("Unknown identifier in expression: " + String(identifier));
  4433. return nullptr;
  4434. }
  4435. } else {
  4436. if (!_find_identifier(p_block, false, p_function_info, identifier, &data_type, &ident_type, &is_const, &array_size, &struct_name)) {
  4437. _set_error("Unknown identifier in expression: " + String(identifier));
  4438. return nullptr;
  4439. }
  4440. if (ident_type == IDENTIFIER_VARYING) {
  4441. TkPos prev_pos = _get_tkpos();
  4442. Token next_token = _get_token();
  4443. // An array of varyings.
  4444. if (next_token.type == TK_BRACKET_OPEN) {
  4445. _get_token(); // Pass constant.
  4446. _get_token(); // Pass TK_BRACKET_CLOSE.
  4447. next_token = _get_token();
  4448. }
  4449. _set_tkpos(prev_pos);
  4450. String error;
  4451. if (is_token_operator_assign(next_token.type)) {
  4452. if (!_validate_varying_assign(shader->varyings[identifier], &error)) {
  4453. _set_error(error);
  4454. return nullptr;
  4455. }
  4456. } else {
  4457. if (!_validate_varying_using(shader->varyings[identifier], &error)) {
  4458. _set_error(error);
  4459. return nullptr;
  4460. }
  4461. }
  4462. }
  4463. if (ident_type == IDENTIFIER_FUNCTION) {
  4464. _set_error("Can't use function as identifier: " + String(identifier));
  4465. return nullptr;
  4466. }
  4467. if (is_const) {
  4468. last_type = IDENTIFIER_CONSTANT;
  4469. } else {
  4470. last_type = ident_type;
  4471. }
  4472. is_local = ident_type == IDENTIFIER_LOCAL_VAR || ident_type == IDENTIFIER_FUNCTION_ARGUMENT;
  4473. }
  4474. Node *index_expression = nullptr;
  4475. Node *call_expression = nullptr;
  4476. Node *assign_expression = nullptr;
  4477. if (array_size > 0) {
  4478. prepos = _get_tkpos();
  4479. tk = _get_token();
  4480. if (tk.type == TK_OP_ASSIGN) {
  4481. if (is_const) {
  4482. _set_error("Constants cannot be modified.");
  4483. return nullptr;
  4484. }
  4485. assign_expression = _parse_array_constructor(p_block, p_function_info, data_type, struct_name, array_size);
  4486. if (!assign_expression) {
  4487. return nullptr;
  4488. }
  4489. } else if (tk.type == TK_PERIOD) {
  4490. completion_class = TAG_ARRAY;
  4491. p_block->block_tag = SubClassTag::TAG_ARRAY;
  4492. call_expression = _parse_and_reduce_expression(p_block, p_function_info);
  4493. p_block->block_tag = SubClassTag::TAG_GLOBAL;
  4494. if (!call_expression) {
  4495. return nullptr;
  4496. }
  4497. data_type = call_expression->get_datatype();
  4498. } else if (tk.type == TK_BRACKET_OPEN) { // indexing
  4499. index_expression = _parse_and_reduce_expression(p_block, p_function_info);
  4500. if (!index_expression) {
  4501. return nullptr;
  4502. }
  4503. if (index_expression->get_array_size() != 0 || (index_expression->get_datatype() != TYPE_INT && index_expression->get_datatype() != TYPE_UINT)) {
  4504. _set_error("Only integer expressions are allowed for indexing.");
  4505. return nullptr;
  4506. }
  4507. if (index_expression->type == Node::TYPE_CONSTANT) {
  4508. ConstantNode *cnode = (ConstantNode *)index_expression;
  4509. if (cnode) {
  4510. if (!cnode->values.is_empty()) {
  4511. int value = cnode->values[0].sint;
  4512. if (value < 0 || value >= array_size) {
  4513. _set_error(vformat("Index [%s] out of range [%s..%s]", value, 0, array_size - 1));
  4514. return nullptr;
  4515. }
  4516. }
  4517. }
  4518. }
  4519. tk = _get_token();
  4520. if (tk.type != TK_BRACKET_CLOSE) {
  4521. _set_error("Expected ']'");
  4522. return nullptr;
  4523. }
  4524. } else {
  4525. _set_tkpos(prepos);
  4526. }
  4527. ArrayNode *arrname = alloc_node<ArrayNode>();
  4528. arrname->name = identifier;
  4529. arrname->datatype_cache = data_type;
  4530. arrname->struct_name = struct_name;
  4531. arrname->index_expression = index_expression;
  4532. arrname->call_expression = call_expression;
  4533. arrname->assign_expression = assign_expression;
  4534. arrname->is_const = is_const;
  4535. arrname->array_size = array_size;
  4536. arrname->is_local = is_local;
  4537. expr = arrname;
  4538. } else {
  4539. VariableNode *varname = alloc_node<VariableNode>();
  4540. varname->name = identifier;
  4541. varname->datatype_cache = data_type;
  4542. varname->is_const = is_const;
  4543. varname->struct_name = struct_name;
  4544. varname->is_local = is_local;
  4545. expr = varname;
  4546. }
  4547. #ifdef DEBUG_ENABLED
  4548. if (check_warnings) {
  4549. StringName func_name;
  4550. if (p_block && p_block->parent_function) {
  4551. func_name = p_block->parent_function->name;
  4552. }
  4553. _parse_used_identifier(identifier, ident_type, func_name);
  4554. }
  4555. #endif // DEBUG_ENABLED
  4556. }
  4557. } else if (tk.type == TK_OP_ADD) {
  4558. continue; //this one does nothing
  4559. } 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) {
  4560. Expression e;
  4561. e.is_op = true;
  4562. switch (tk.type) {
  4563. case TK_OP_SUB:
  4564. e.op = OP_NEGATE;
  4565. break;
  4566. case TK_OP_NOT:
  4567. e.op = OP_NOT;
  4568. break;
  4569. case TK_OP_BIT_INVERT:
  4570. e.op = OP_BIT_INVERT;
  4571. break;
  4572. case TK_OP_INCREMENT:
  4573. e.op = OP_INCREMENT;
  4574. break;
  4575. case TK_OP_DECREMENT:
  4576. e.op = OP_DECREMENT;
  4577. break;
  4578. default:
  4579. ERR_FAIL_V(nullptr);
  4580. }
  4581. expression.push_back(e);
  4582. continue;
  4583. } else {
  4584. _set_error("Expected expression, found: " + get_token_text(tk));
  4585. return nullptr;
  4586. //nothing
  4587. }
  4588. ERR_FAIL_COND_V(!expr, nullptr);
  4589. /* OK now see what's NEXT to the operator.. */
  4590. while (true) {
  4591. TkPos pos2 = _get_tkpos();
  4592. tk = _get_token();
  4593. if (tk.type == TK_CURSOR) {
  4594. //do nothing
  4595. } else if (tk.type == TK_IDENTIFIER) {
  4596. } else if (tk.type == TK_PERIOD) {
  4597. DataType dt = expr->get_datatype();
  4598. String st = expr->get_datatype_name();
  4599. if (!expr->is_indexed() && expr->get_array_size() > 0) {
  4600. completion_class = TAG_ARRAY;
  4601. p_block->block_tag = SubClassTag::TAG_ARRAY;
  4602. Node *call_expression = _parse_and_reduce_expression(p_block, p_function_info);
  4603. p_block->block_tag = SubClassTag::TAG_GLOBAL;
  4604. if (!call_expression) {
  4605. return nullptr;
  4606. }
  4607. expr = call_expression;
  4608. break;
  4609. }
  4610. StringName identifier;
  4611. if (_get_completable_identifier(p_block, dt == TYPE_STRUCT ? COMPLETION_STRUCT : COMPLETION_INDEX, identifier)) {
  4612. if (dt == TYPE_STRUCT) {
  4613. completion_struct = st;
  4614. } else {
  4615. completion_base = dt;
  4616. }
  4617. }
  4618. if (identifier == StringName()) {
  4619. _set_error("Expected identifier as member");
  4620. return nullptr;
  4621. }
  4622. String ident = identifier;
  4623. bool ok = true;
  4624. bool repeated = false;
  4625. DataType member_type = TYPE_VOID;
  4626. StringName member_struct_name = "";
  4627. int array_size = 0;
  4628. Set<char> position_symbols;
  4629. Set<char> color_symbols;
  4630. Set<char> texture_symbols;
  4631. bool mix_error = false;
  4632. switch (dt) {
  4633. case TYPE_STRUCT: {
  4634. ok = false;
  4635. String member_name = String(ident.ptr());
  4636. if (shader->structs.has(st)) {
  4637. StructNode *n = shader->structs[st].shader_struct;
  4638. for (const MemberNode *E : n->members) {
  4639. if (String(E->name) == member_name) {
  4640. member_type = E->datatype;
  4641. array_size = E->array_size;
  4642. if (member_type == TYPE_STRUCT) {
  4643. member_struct_name = E->struct_name;
  4644. }
  4645. ok = true;
  4646. break;
  4647. }
  4648. }
  4649. }
  4650. } break;
  4651. case TYPE_BVEC2:
  4652. case TYPE_IVEC2:
  4653. case TYPE_UVEC2:
  4654. case TYPE_VEC2: {
  4655. int l = ident.length();
  4656. if (l == 1) {
  4657. member_type = DataType(dt - 1);
  4658. } else if (l == 2) {
  4659. member_type = dt;
  4660. } else if (l == 3) {
  4661. member_type = DataType(dt + 1);
  4662. } else if (l == 4) {
  4663. member_type = DataType(dt + 2);
  4664. } else {
  4665. ok = false;
  4666. break;
  4667. }
  4668. const char32_t *c = ident.ptr();
  4669. for (int i = 0; i < l; i++) {
  4670. switch (c[i]) {
  4671. case 'r':
  4672. case 'g':
  4673. if (position_symbols.size() > 0 || texture_symbols.size() > 0) {
  4674. mix_error = true;
  4675. break;
  4676. }
  4677. if (!color_symbols.has(c[i])) {
  4678. color_symbols.insert(c[i]);
  4679. } else {
  4680. repeated = true;
  4681. }
  4682. break;
  4683. case 'x':
  4684. case 'y':
  4685. if (color_symbols.size() > 0 || texture_symbols.size() > 0) {
  4686. mix_error = true;
  4687. break;
  4688. }
  4689. if (!position_symbols.has(c[i])) {
  4690. position_symbols.insert(c[i]);
  4691. } else {
  4692. repeated = true;
  4693. }
  4694. break;
  4695. case 's':
  4696. case 't':
  4697. if (color_symbols.size() > 0 || position_symbols.size() > 0) {
  4698. mix_error = true;
  4699. break;
  4700. }
  4701. if (!texture_symbols.has(c[i])) {
  4702. texture_symbols.insert(c[i]);
  4703. } else {
  4704. repeated = true;
  4705. }
  4706. break;
  4707. default:
  4708. ok = false;
  4709. break;
  4710. }
  4711. }
  4712. } break;
  4713. case TYPE_BVEC3:
  4714. case TYPE_IVEC3:
  4715. case TYPE_UVEC3:
  4716. case TYPE_VEC3: {
  4717. int l = ident.length();
  4718. if (l == 1) {
  4719. member_type = DataType(dt - 2);
  4720. } else if (l == 2) {
  4721. member_type = DataType(dt - 1);
  4722. } else if (l == 3) {
  4723. member_type = dt;
  4724. } else if (l == 4) {
  4725. member_type = DataType(dt + 1);
  4726. } else {
  4727. ok = false;
  4728. break;
  4729. }
  4730. const char32_t *c = ident.ptr();
  4731. for (int i = 0; i < l; i++) {
  4732. switch (c[i]) {
  4733. case 'r':
  4734. case 'g':
  4735. case 'b':
  4736. if (position_symbols.size() > 0 || texture_symbols.size() > 0) {
  4737. mix_error = true;
  4738. break;
  4739. }
  4740. if (!color_symbols.has(c[i])) {
  4741. color_symbols.insert(c[i]);
  4742. } else {
  4743. repeated = true;
  4744. }
  4745. break;
  4746. case 'x':
  4747. case 'y':
  4748. case 'z':
  4749. if (color_symbols.size() > 0 || texture_symbols.size() > 0) {
  4750. mix_error = true;
  4751. break;
  4752. }
  4753. if (!position_symbols.has(c[i])) {
  4754. position_symbols.insert(c[i]);
  4755. } else {
  4756. repeated = true;
  4757. }
  4758. break;
  4759. case 's':
  4760. case 't':
  4761. case 'p':
  4762. if (color_symbols.size() > 0 || position_symbols.size() > 0) {
  4763. mix_error = true;
  4764. break;
  4765. }
  4766. if (!texture_symbols.has(c[i])) {
  4767. texture_symbols.insert(c[i]);
  4768. } else {
  4769. repeated = true;
  4770. }
  4771. break;
  4772. default:
  4773. ok = false;
  4774. break;
  4775. }
  4776. }
  4777. } break;
  4778. case TYPE_BVEC4:
  4779. case TYPE_IVEC4:
  4780. case TYPE_UVEC4:
  4781. case TYPE_VEC4: {
  4782. int l = ident.length();
  4783. if (l == 1) {
  4784. member_type = DataType(dt - 3);
  4785. } else if (l == 2) {
  4786. member_type = DataType(dt - 2);
  4787. } else if (l == 3) {
  4788. member_type = DataType(dt - 1);
  4789. } else if (l == 4) {
  4790. member_type = dt;
  4791. } else {
  4792. ok = false;
  4793. break;
  4794. }
  4795. const char32_t *c = ident.ptr();
  4796. for (int i = 0; i < l; i++) {
  4797. switch (c[i]) {
  4798. case 'r':
  4799. case 'g':
  4800. case 'b':
  4801. case 'a':
  4802. if (position_symbols.size() > 0 || texture_symbols.size() > 0) {
  4803. mix_error = true;
  4804. break;
  4805. }
  4806. if (!color_symbols.has(c[i])) {
  4807. color_symbols.insert(c[i]);
  4808. } else {
  4809. repeated = true;
  4810. }
  4811. break;
  4812. case 'x':
  4813. case 'y':
  4814. case 'z':
  4815. case 'w':
  4816. if (color_symbols.size() > 0 || texture_symbols.size() > 0) {
  4817. mix_error = true;
  4818. break;
  4819. }
  4820. if (!position_symbols.has(c[i])) {
  4821. position_symbols.insert(c[i]);
  4822. } else {
  4823. repeated = true;
  4824. }
  4825. break;
  4826. case 's':
  4827. case 't':
  4828. case 'p':
  4829. case 'q':
  4830. if (color_symbols.size() > 0 || position_symbols.size() > 0) {
  4831. mix_error = true;
  4832. break;
  4833. }
  4834. if (!texture_symbols.has(c[i])) {
  4835. texture_symbols.insert(c[i]);
  4836. } else {
  4837. repeated = true;
  4838. }
  4839. break;
  4840. default:
  4841. ok = false;
  4842. break;
  4843. }
  4844. }
  4845. } break;
  4846. default: {
  4847. ok = false;
  4848. }
  4849. }
  4850. if (mix_error) {
  4851. _set_error("Cannot combine symbols from different sets in expression ." + ident);
  4852. return nullptr;
  4853. }
  4854. if (!ok) {
  4855. _set_error("Invalid member for " + (dt == TYPE_STRUCT ? st : get_datatype_name(dt)) + " expression: ." + ident);
  4856. return nullptr;
  4857. }
  4858. MemberNode *mn = alloc_node<MemberNode>();
  4859. mn->basetype = dt;
  4860. mn->basetype_const = is_const;
  4861. mn->datatype = member_type;
  4862. mn->base_struct_name = st;
  4863. mn->struct_name = member_struct_name;
  4864. mn->array_size = array_size;
  4865. mn->name = ident;
  4866. mn->owner = expr;
  4867. mn->has_swizzling_duplicates = repeated;
  4868. if (array_size > 0) {
  4869. TkPos prev_pos = _get_tkpos();
  4870. tk = _get_token();
  4871. if (tk.type == TK_OP_ASSIGN) {
  4872. if (last_type == IDENTIFIER_CONSTANT) {
  4873. _set_error("Constants cannot be modified.");
  4874. return nullptr;
  4875. }
  4876. Node *assign_expression = _parse_array_constructor(p_block, p_function_info, member_type, member_struct_name, array_size);
  4877. if (!assign_expression) {
  4878. return nullptr;
  4879. }
  4880. mn->assign_expression = assign_expression;
  4881. } else if (tk.type == TK_PERIOD) {
  4882. completion_class = TAG_ARRAY;
  4883. p_block->block_tag = SubClassTag::TAG_ARRAY;
  4884. Node *call_expression = _parse_and_reduce_expression(p_block, p_function_info);
  4885. p_block->block_tag = SubClassTag::TAG_GLOBAL;
  4886. if (!call_expression) {
  4887. return nullptr;
  4888. }
  4889. mn->datatype = call_expression->get_datatype();
  4890. mn->call_expression = call_expression;
  4891. } else if (tk.type == TK_BRACKET_OPEN) {
  4892. Node *index_expression = _parse_and_reduce_expression(p_block, p_function_info);
  4893. if (!index_expression) {
  4894. return nullptr;
  4895. }
  4896. if (index_expression->get_array_size() != 0 || (index_expression->get_datatype() != TYPE_INT && index_expression->get_datatype() != TYPE_UINT)) {
  4897. _set_error("Only integer expressions are allowed for indexing.");
  4898. return nullptr;
  4899. }
  4900. if (index_expression->type == Node::TYPE_CONSTANT) {
  4901. ConstantNode *cnode = (ConstantNode *)index_expression;
  4902. if (cnode) {
  4903. if (!cnode->values.is_empty()) {
  4904. int value = cnode->values[0].sint;
  4905. if (value < 0 || value >= array_size) {
  4906. _set_error(vformat("Index [%s] out of range [%s..%s]", value, 0, array_size - 1));
  4907. return nullptr;
  4908. }
  4909. }
  4910. }
  4911. }
  4912. tk = _get_token();
  4913. if (tk.type != TK_BRACKET_CLOSE) {
  4914. _set_error("Expected ']'");
  4915. return nullptr;
  4916. }
  4917. mn->index_expression = index_expression;
  4918. } else {
  4919. _set_tkpos(prev_pos);
  4920. }
  4921. }
  4922. expr = mn;
  4923. //todo
  4924. //member (period) has priority over any operator
  4925. //creates a subindexing expression in place
  4926. /*} else if (tk.type==TK_BRACKET_OPEN) {
  4927. //todo
  4928. //subindexing has priority over any operator
  4929. //creates a subindexing expression in place
  4930. */
  4931. } else if (tk.type == TK_BRACKET_OPEN) {
  4932. Node *index = _parse_and_reduce_expression(p_block, p_function_info);
  4933. if (!index) {
  4934. return nullptr;
  4935. }
  4936. if (index->get_array_size() != 0 || (index->get_datatype() != TYPE_INT && index->get_datatype() != TYPE_UINT)) {
  4937. _set_error("Only integer expressions are allowed for indexing.");
  4938. return nullptr;
  4939. }
  4940. DataType member_type = TYPE_VOID;
  4941. String member_struct_name;
  4942. if (expr->get_array_size() > 0) {
  4943. if (index->type == Node::TYPE_CONSTANT) {
  4944. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  4945. if (index_constant >= (uint32_t)expr->get_array_size()) {
  4946. _set_error(vformat("Index [%s] out of range [%s..%s]", index_constant, 0, expr->get_array_size() - 1));
  4947. return nullptr;
  4948. }
  4949. }
  4950. member_type = expr->get_datatype();
  4951. if (member_type == TYPE_STRUCT) {
  4952. member_struct_name = expr->get_datatype_name();
  4953. }
  4954. } else {
  4955. switch (expr->get_datatype()) {
  4956. case TYPE_BVEC2:
  4957. case TYPE_VEC2:
  4958. case TYPE_IVEC2:
  4959. case TYPE_UVEC2:
  4960. case TYPE_MAT2:
  4961. if (index->type == Node::TYPE_CONSTANT) {
  4962. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  4963. if (index_constant >= 2) {
  4964. _set_error("Index out of range (0-1)");
  4965. return nullptr;
  4966. }
  4967. }
  4968. switch (expr->get_datatype()) {
  4969. case TYPE_BVEC2:
  4970. member_type = TYPE_BOOL;
  4971. break;
  4972. case TYPE_VEC2:
  4973. member_type = TYPE_FLOAT;
  4974. break;
  4975. case TYPE_IVEC2:
  4976. member_type = TYPE_INT;
  4977. break;
  4978. case TYPE_UVEC2:
  4979. member_type = TYPE_UINT;
  4980. break;
  4981. case TYPE_MAT2:
  4982. member_type = TYPE_VEC2;
  4983. break;
  4984. default:
  4985. break;
  4986. }
  4987. break;
  4988. case TYPE_BVEC3:
  4989. case TYPE_VEC3:
  4990. case TYPE_IVEC3:
  4991. case TYPE_UVEC3:
  4992. case TYPE_MAT3:
  4993. if (index->type == Node::TYPE_CONSTANT) {
  4994. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  4995. if (index_constant >= 3) {
  4996. _set_error("Index out of range (0-2)");
  4997. return nullptr;
  4998. }
  4999. }
  5000. switch (expr->get_datatype()) {
  5001. case TYPE_BVEC3:
  5002. member_type = TYPE_BOOL;
  5003. break;
  5004. case TYPE_VEC3:
  5005. member_type = TYPE_FLOAT;
  5006. break;
  5007. case TYPE_IVEC3:
  5008. member_type = TYPE_INT;
  5009. break;
  5010. case TYPE_UVEC3:
  5011. member_type = TYPE_UINT;
  5012. break;
  5013. case TYPE_MAT3:
  5014. member_type = TYPE_VEC3;
  5015. break;
  5016. default:
  5017. break;
  5018. }
  5019. break;
  5020. case TYPE_BVEC4:
  5021. case TYPE_VEC4:
  5022. case TYPE_IVEC4:
  5023. case TYPE_UVEC4:
  5024. case TYPE_MAT4:
  5025. if (index->type == Node::TYPE_CONSTANT) {
  5026. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  5027. if (index_constant >= 4) {
  5028. _set_error("Index out of range (0-3)");
  5029. return nullptr;
  5030. }
  5031. }
  5032. switch (expr->get_datatype()) {
  5033. case TYPE_BVEC4:
  5034. member_type = TYPE_BOOL;
  5035. break;
  5036. case TYPE_VEC4:
  5037. member_type = TYPE_FLOAT;
  5038. break;
  5039. case TYPE_IVEC4:
  5040. member_type = TYPE_INT;
  5041. break;
  5042. case TYPE_UVEC4:
  5043. member_type = TYPE_UINT;
  5044. break;
  5045. case TYPE_MAT4:
  5046. member_type = TYPE_VEC4;
  5047. break;
  5048. default:
  5049. break;
  5050. }
  5051. break;
  5052. default: {
  5053. _set_error("Object of type '" + (expr->get_datatype() == TYPE_STRUCT ? expr->get_datatype_name() : get_datatype_name(expr->get_datatype())) + "' can't be indexed");
  5054. return nullptr;
  5055. }
  5056. }
  5057. }
  5058. OperatorNode *op = alloc_node<OperatorNode>();
  5059. op->op = OP_INDEX;
  5060. op->return_cache = member_type;
  5061. op->struct_name = member_struct_name;
  5062. op->arguments.push_back(expr);
  5063. op->arguments.push_back(index);
  5064. expr = op;
  5065. tk = _get_token();
  5066. if (tk.type != TK_BRACKET_CLOSE) {
  5067. _set_error("Expected ']' after indexing expression");
  5068. return nullptr;
  5069. }
  5070. } else if (tk.type == TK_OP_INCREMENT || tk.type == TK_OP_DECREMENT) {
  5071. OperatorNode *op = alloc_node<OperatorNode>();
  5072. op->op = tk.type == TK_OP_DECREMENT ? OP_POST_DECREMENT : OP_POST_INCREMENT;
  5073. op->arguments.push_back(expr);
  5074. if (!_validate_operator(op, &op->return_cache, &op->return_array_size)) {
  5075. _set_error("Invalid base type for increment/decrement operator");
  5076. return nullptr;
  5077. }
  5078. if (!_validate_assign(expr, p_function_info)) {
  5079. _set_error("Invalid use of increment/decrement operator in constant expression.");
  5080. return nullptr;
  5081. }
  5082. expr = op;
  5083. } else {
  5084. _set_tkpos(pos2);
  5085. break;
  5086. }
  5087. }
  5088. Expression e;
  5089. e.is_op = false;
  5090. e.node = expr;
  5091. expression.push_back(e);
  5092. pos = _get_tkpos();
  5093. tk = _get_token();
  5094. if (is_token_operator(tk.type)) {
  5095. Expression o;
  5096. o.is_op = true;
  5097. switch (tk.type) {
  5098. case TK_OP_EQUAL:
  5099. o.op = OP_EQUAL;
  5100. break;
  5101. case TK_OP_NOT_EQUAL:
  5102. o.op = OP_NOT_EQUAL;
  5103. break;
  5104. case TK_OP_LESS:
  5105. o.op = OP_LESS;
  5106. break;
  5107. case TK_OP_LESS_EQUAL:
  5108. o.op = OP_LESS_EQUAL;
  5109. break;
  5110. case TK_OP_GREATER:
  5111. o.op = OP_GREATER;
  5112. break;
  5113. case TK_OP_GREATER_EQUAL:
  5114. o.op = OP_GREATER_EQUAL;
  5115. break;
  5116. case TK_OP_AND:
  5117. o.op = OP_AND;
  5118. break;
  5119. case TK_OP_OR:
  5120. o.op = OP_OR;
  5121. break;
  5122. case TK_OP_ADD:
  5123. o.op = OP_ADD;
  5124. break;
  5125. case TK_OP_SUB:
  5126. o.op = OP_SUB;
  5127. break;
  5128. case TK_OP_MUL:
  5129. o.op = OP_MUL;
  5130. break;
  5131. case TK_OP_DIV:
  5132. o.op = OP_DIV;
  5133. break;
  5134. case TK_OP_MOD:
  5135. o.op = OP_MOD;
  5136. break;
  5137. case TK_OP_SHIFT_LEFT:
  5138. o.op = OP_SHIFT_LEFT;
  5139. break;
  5140. case TK_OP_SHIFT_RIGHT:
  5141. o.op = OP_SHIFT_RIGHT;
  5142. break;
  5143. case TK_OP_ASSIGN:
  5144. o.op = OP_ASSIGN;
  5145. break;
  5146. case TK_OP_ASSIGN_ADD:
  5147. o.op = OP_ASSIGN_ADD;
  5148. break;
  5149. case TK_OP_ASSIGN_SUB:
  5150. o.op = OP_ASSIGN_SUB;
  5151. break;
  5152. case TK_OP_ASSIGN_MUL:
  5153. o.op = OP_ASSIGN_MUL;
  5154. break;
  5155. case TK_OP_ASSIGN_DIV:
  5156. o.op = OP_ASSIGN_DIV;
  5157. break;
  5158. case TK_OP_ASSIGN_MOD:
  5159. o.op = OP_ASSIGN_MOD;
  5160. break;
  5161. case TK_OP_ASSIGN_SHIFT_LEFT:
  5162. o.op = OP_ASSIGN_SHIFT_LEFT;
  5163. break;
  5164. case TK_OP_ASSIGN_SHIFT_RIGHT:
  5165. o.op = OP_ASSIGN_SHIFT_RIGHT;
  5166. break;
  5167. case TK_OP_ASSIGN_BIT_AND:
  5168. o.op = OP_ASSIGN_BIT_AND;
  5169. break;
  5170. case TK_OP_ASSIGN_BIT_OR:
  5171. o.op = OP_ASSIGN_BIT_OR;
  5172. break;
  5173. case TK_OP_ASSIGN_BIT_XOR:
  5174. o.op = OP_ASSIGN_BIT_XOR;
  5175. break;
  5176. case TK_OP_BIT_AND:
  5177. o.op = OP_BIT_AND;
  5178. break;
  5179. case TK_OP_BIT_OR:
  5180. o.op = OP_BIT_OR;
  5181. break;
  5182. case TK_OP_BIT_XOR:
  5183. o.op = OP_BIT_XOR;
  5184. break;
  5185. case TK_QUESTION:
  5186. o.op = OP_SELECT_IF;
  5187. break;
  5188. case TK_COLON:
  5189. o.op = OP_SELECT_ELSE;
  5190. break;
  5191. default: {
  5192. _set_error("Invalid token for operator: " + get_token_text(tk));
  5193. return nullptr;
  5194. }
  5195. }
  5196. expression.push_back(o);
  5197. } else {
  5198. _set_tkpos(pos); //something else, so rollback and end
  5199. break;
  5200. }
  5201. }
  5202. /* Reduce the set set of expressions and place them in an operator tree, respecting precedence */
  5203. while (expression.size() > 1) {
  5204. int next_op = -1;
  5205. int min_priority = 0xFFFFF;
  5206. bool is_unary = false;
  5207. bool is_ternary = false;
  5208. for (int i = 0; i < expression.size(); i++) {
  5209. if (!expression[i].is_op) {
  5210. continue;
  5211. }
  5212. bool unary = false;
  5213. bool ternary = false;
  5214. Operator op = expression[i].op;
  5215. int priority;
  5216. switch (op) {
  5217. case OP_EQUAL:
  5218. priority = 8;
  5219. break;
  5220. case OP_NOT_EQUAL:
  5221. priority = 8;
  5222. break;
  5223. case OP_LESS:
  5224. priority = 7;
  5225. break;
  5226. case OP_LESS_EQUAL:
  5227. priority = 7;
  5228. break;
  5229. case OP_GREATER:
  5230. priority = 7;
  5231. break;
  5232. case OP_GREATER_EQUAL:
  5233. priority = 7;
  5234. break;
  5235. case OP_AND:
  5236. priority = 12;
  5237. break;
  5238. case OP_OR:
  5239. priority = 14;
  5240. break;
  5241. case OP_NOT:
  5242. priority = 3;
  5243. unary = true;
  5244. break;
  5245. case OP_NEGATE:
  5246. priority = 3;
  5247. unary = true;
  5248. break;
  5249. case OP_ADD:
  5250. priority = 5;
  5251. break;
  5252. case OP_SUB:
  5253. priority = 5;
  5254. break;
  5255. case OP_MUL:
  5256. priority = 4;
  5257. break;
  5258. case OP_DIV:
  5259. priority = 4;
  5260. break;
  5261. case OP_MOD:
  5262. priority = 4;
  5263. break;
  5264. case OP_SHIFT_LEFT:
  5265. priority = 6;
  5266. break;
  5267. case OP_SHIFT_RIGHT:
  5268. priority = 6;
  5269. break;
  5270. case OP_ASSIGN:
  5271. priority = 16;
  5272. break;
  5273. case OP_ASSIGN_ADD:
  5274. priority = 16;
  5275. break;
  5276. case OP_ASSIGN_SUB:
  5277. priority = 16;
  5278. break;
  5279. case OP_ASSIGN_MUL:
  5280. priority = 16;
  5281. break;
  5282. case OP_ASSIGN_DIV:
  5283. priority = 16;
  5284. break;
  5285. case OP_ASSIGN_MOD:
  5286. priority = 16;
  5287. break;
  5288. case OP_ASSIGN_SHIFT_LEFT:
  5289. priority = 16;
  5290. break;
  5291. case OP_ASSIGN_SHIFT_RIGHT:
  5292. priority = 16;
  5293. break;
  5294. case OP_ASSIGN_BIT_AND:
  5295. priority = 16;
  5296. break;
  5297. case OP_ASSIGN_BIT_OR:
  5298. priority = 16;
  5299. break;
  5300. case OP_ASSIGN_BIT_XOR:
  5301. priority = 16;
  5302. break;
  5303. case OP_BIT_AND:
  5304. priority = 9;
  5305. break;
  5306. case OP_BIT_OR:
  5307. priority = 11;
  5308. break;
  5309. case OP_BIT_XOR:
  5310. priority = 10;
  5311. break;
  5312. case OP_BIT_INVERT:
  5313. priority = 3;
  5314. unary = true;
  5315. break;
  5316. case OP_INCREMENT:
  5317. priority = 3;
  5318. unary = true;
  5319. break;
  5320. case OP_DECREMENT:
  5321. priority = 3;
  5322. unary = true;
  5323. break;
  5324. case OP_SELECT_IF:
  5325. priority = 15;
  5326. ternary = true;
  5327. break;
  5328. case OP_SELECT_ELSE:
  5329. priority = 15;
  5330. ternary = true;
  5331. break;
  5332. default:
  5333. ERR_FAIL_V(nullptr); //unexpected operator
  5334. }
  5335. #if DEBUG_ENABLED
  5336. if (check_warnings && HAS_WARNING(ShaderWarning::FLOAT_COMPARISON_FLAG) && (op == OP_EQUAL || op == OP_NOT_EQUAL) &&
  5337. (!expression[i - 1].is_op && !expression[i + 1].is_op) &&
  5338. (expression[i - 1].node->get_datatype() == TYPE_FLOAT && expression[i + 1].node->get_datatype() == TYPE_FLOAT)) {
  5339. _add_line_warning(ShaderWarning::FLOAT_COMPARISON);
  5340. }
  5341. #endif // DEBUG_ENABLED
  5342. if (priority < min_priority) {
  5343. // < is used for left to right (default)
  5344. // <= is used for right to left
  5345. next_op = i;
  5346. min_priority = priority;
  5347. is_unary = unary;
  5348. is_ternary = ternary;
  5349. }
  5350. }
  5351. ERR_FAIL_COND_V(next_op == -1, nullptr);
  5352. // OK! create operator..
  5353. if (is_unary) {
  5354. int expr_pos = next_op;
  5355. while (expression[expr_pos].is_op) {
  5356. expr_pos++;
  5357. if (expr_pos == expression.size()) {
  5358. //can happen..
  5359. _set_error("Unexpected end of expression...");
  5360. return nullptr;
  5361. }
  5362. }
  5363. //consecutively do unary operators
  5364. for (int i = expr_pos - 1; i >= next_op; i--) {
  5365. OperatorNode *op = alloc_node<OperatorNode>();
  5366. op->op = expression[i].op;
  5367. if ((op->op == OP_INCREMENT || op->op == OP_DECREMENT) && !_validate_assign(expression[i + 1].node, p_function_info)) {
  5368. _set_error("Can't use increment/decrement operator in constant expression.");
  5369. return nullptr;
  5370. }
  5371. op->arguments.push_back(expression[i + 1].node);
  5372. expression.write[i].is_op = false;
  5373. expression.write[i].node = op;
  5374. if (!_validate_operator(op, &op->return_cache, &op->return_array_size)) {
  5375. String at;
  5376. for (int j = 0; j < op->arguments.size(); j++) {
  5377. if (j > 0) {
  5378. at += " and ";
  5379. }
  5380. at += get_datatype_name(op->arguments[j]->get_datatype());
  5381. if (!op->arguments[j]->is_indexed() && op->arguments[j]->get_array_size() > 0) {
  5382. at += "[";
  5383. at += itos(op->arguments[j]->get_array_size());
  5384. at += "]";
  5385. }
  5386. }
  5387. _set_error("Invalid arguments to unary operator '" + get_operator_text(op->op) + "' :" + at);
  5388. return nullptr;
  5389. }
  5390. expression.remove(i + 1);
  5391. }
  5392. } else if (is_ternary) {
  5393. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  5394. _set_error("Parser bug...");
  5395. ERR_FAIL_V(nullptr);
  5396. }
  5397. if (next_op + 2 >= expression.size() || !expression[next_op + 2].is_op || expression[next_op + 2].op != OP_SELECT_ELSE) {
  5398. _set_error("Missing matching ':' for select operator");
  5399. return nullptr;
  5400. }
  5401. OperatorNode *op = alloc_node<OperatorNode>();
  5402. op->op = expression[next_op].op;
  5403. op->arguments.push_back(expression[next_op - 1].node);
  5404. op->arguments.push_back(expression[next_op + 1].node);
  5405. op->arguments.push_back(expression[next_op + 3].node);
  5406. expression.write[next_op - 1].is_op = false;
  5407. expression.write[next_op - 1].node = op;
  5408. if (!_validate_operator(op, &op->return_cache, &op->return_array_size)) {
  5409. String at;
  5410. for (int i = 0; i < op->arguments.size(); i++) {
  5411. if (i > 0) {
  5412. at += " and ";
  5413. }
  5414. at += get_datatype_name(op->arguments[i]->get_datatype());
  5415. if (!op->arguments[i]->is_indexed() && op->arguments[i]->get_array_size() > 0) {
  5416. at += "[";
  5417. at += itos(op->arguments[i]->get_array_size());
  5418. at += "]";
  5419. }
  5420. }
  5421. _set_error("Invalid argument to ternary ?: operator: " + at);
  5422. return nullptr;
  5423. }
  5424. for (int i = 0; i < 4; i++) {
  5425. expression.remove(next_op);
  5426. }
  5427. } else {
  5428. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  5429. _set_error("Parser bug...");
  5430. ERR_FAIL_V(nullptr);
  5431. }
  5432. OperatorNode *op = alloc_node<OperatorNode>();
  5433. op->op = expression[next_op].op;
  5434. if (expression[next_op - 1].is_op) {
  5435. _set_error("Parser bug...");
  5436. ERR_FAIL_V(nullptr);
  5437. }
  5438. if (_is_operator_assign(op->op)) {
  5439. String assign_message;
  5440. if (!_validate_assign(expression[next_op - 1].node, p_function_info, &assign_message)) {
  5441. _set_error(assign_message);
  5442. return nullptr;
  5443. }
  5444. }
  5445. if (expression[next_op + 1].is_op) {
  5446. // this is not invalid and can really appear
  5447. // but it becomes invalid anyway because no binary op
  5448. // can be followed by a unary op in a valid combination,
  5449. // due to how precedence works, unaries will always disappear first
  5450. _set_error("Parser bug...");
  5451. }
  5452. op->arguments.push_back(expression[next_op - 1].node); //expression goes as left
  5453. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as right
  5454. expression.write[next_op - 1].node = op;
  5455. //replace all 3 nodes by this operator and make it an expression
  5456. if (!_validate_operator(op, &op->return_cache, &op->return_array_size)) {
  5457. String at;
  5458. for (int i = 0; i < op->arguments.size(); i++) {
  5459. if (i > 0) {
  5460. at += " and ";
  5461. }
  5462. if (op->arguments[i]->get_datatype() == TYPE_STRUCT) {
  5463. at += op->arguments[i]->get_datatype_name();
  5464. } else {
  5465. at += get_datatype_name(op->arguments[i]->get_datatype());
  5466. }
  5467. if (!op->arguments[i]->is_indexed() && op->arguments[i]->get_array_size() > 0) {
  5468. at += "[";
  5469. at += itos(op->arguments[i]->get_array_size());
  5470. at += "]";
  5471. }
  5472. }
  5473. _set_error("Invalid arguments to operator '" + get_operator_text(op->op) + "' :" + at);
  5474. return nullptr;
  5475. }
  5476. expression.remove(next_op);
  5477. expression.remove(next_op);
  5478. }
  5479. }
  5480. return expression[0].node;
  5481. }
  5482. ShaderLanguage::Node *ShaderLanguage::_reduce_expression(BlockNode *p_block, ShaderLanguage::Node *p_node) {
  5483. if (p_node->type != Node::TYPE_OPERATOR) {
  5484. return p_node;
  5485. }
  5486. //for now only reduce simple constructors
  5487. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  5488. if (op->op == OP_CONSTRUCT) {
  5489. ERR_FAIL_COND_V(op->arguments[0]->type != Node::TYPE_VARIABLE, p_node);
  5490. DataType type = op->get_datatype();
  5491. DataType base = get_scalar_type(type);
  5492. int cardinality = get_cardinality(type);
  5493. Vector<ConstantNode::Value> values;
  5494. for (int i = 1; i < op->arguments.size(); i++) {
  5495. op->arguments.write[i] = _reduce_expression(p_block, op->arguments[i]);
  5496. if (op->arguments[i]->type == Node::TYPE_CONSTANT) {
  5497. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[i]);
  5498. if (get_scalar_type(cn->datatype) == base) {
  5499. for (int j = 0; j < cn->values.size(); j++) {
  5500. values.push_back(cn->values[j]);
  5501. }
  5502. } else if (get_scalar_type(cn->datatype) == cn->datatype) {
  5503. ConstantNode::Value v;
  5504. if (!convert_constant(cn, base, &v)) {
  5505. return p_node;
  5506. }
  5507. values.push_back(v);
  5508. } else {
  5509. return p_node;
  5510. }
  5511. } else {
  5512. return p_node;
  5513. }
  5514. }
  5515. if (values.size() == 1) {
  5516. if (type >= TYPE_MAT2 && type <= TYPE_MAT4) {
  5517. ConstantNode::Value value = values[0];
  5518. ConstantNode::Value zero;
  5519. zero.real = 0.0f;
  5520. int size = 2 + (type - TYPE_MAT2);
  5521. values.clear();
  5522. for (int i = 0; i < size; i++) {
  5523. for (int j = 0; j < size; j++) {
  5524. values.push_back(i == j ? value : zero);
  5525. }
  5526. }
  5527. } else {
  5528. ConstantNode::Value value = values[0];
  5529. for (int i = 1; i < cardinality; i++) {
  5530. values.push_back(value);
  5531. }
  5532. }
  5533. } else if (values.size() != cardinality) {
  5534. ERR_PRINT("Failed to reduce expression, values and cardinality mismatch.");
  5535. return p_node;
  5536. }
  5537. ConstantNode *cn = alloc_node<ConstantNode>();
  5538. cn->datatype = op->get_datatype();
  5539. cn->values = values;
  5540. return cn;
  5541. } else if (op->op == OP_NEGATE) {
  5542. op->arguments.write[0] = _reduce_expression(p_block, op->arguments[0]);
  5543. if (op->arguments[0]->type == Node::TYPE_CONSTANT) {
  5544. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[0]);
  5545. DataType base = get_scalar_type(cn->datatype);
  5546. Vector<ConstantNode::Value> values;
  5547. for (int i = 0; i < cn->values.size(); i++) {
  5548. ConstantNode::Value nv;
  5549. switch (base) {
  5550. case TYPE_BOOL: {
  5551. nv.boolean = !cn->values[i].boolean;
  5552. } break;
  5553. case TYPE_INT: {
  5554. nv.sint = -cn->values[i].sint;
  5555. } break;
  5556. case TYPE_UINT: {
  5557. // Intentionally wrap the unsigned int value, because GLSL does.
  5558. nv.uint = 0 - cn->values[i].uint;
  5559. } break;
  5560. case TYPE_FLOAT: {
  5561. nv.real = -cn->values[i].real;
  5562. } break;
  5563. default: {
  5564. }
  5565. }
  5566. values.push_back(nv);
  5567. }
  5568. cn->values = values;
  5569. return cn;
  5570. }
  5571. }
  5572. return p_node;
  5573. }
  5574. ShaderLanguage::Node *ShaderLanguage::_parse_and_reduce_expression(BlockNode *p_block, const FunctionInfo &p_function_info) {
  5575. ShaderLanguage::Node *expr = _parse_expression(p_block, p_function_info);
  5576. if (!expr) { //errored
  5577. return nullptr;
  5578. }
  5579. expr = _reduce_expression(p_block, expr);
  5580. return expr;
  5581. }
  5582. Error ShaderLanguage::_parse_block(BlockNode *p_block, const FunctionInfo &p_function_info, bool p_just_one, bool p_can_break, bool p_can_continue) {
  5583. while (true) {
  5584. TkPos pos = _get_tkpos();
  5585. Token tk = _get_token();
  5586. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_SWITCH) {
  5587. if (tk.type != TK_CF_CASE && tk.type != TK_CF_DEFAULT && tk.type != TK_CURLY_BRACKET_CLOSE) {
  5588. _set_error("Switch may contains only case and default blocks");
  5589. return ERR_PARSE_ERROR;
  5590. }
  5591. }
  5592. bool is_struct = shader->structs.has(tk.text);
  5593. if (tk.type == TK_CURLY_BRACKET_CLOSE) { //end of block
  5594. if (p_just_one) {
  5595. _set_error("Unexpected '}'");
  5596. return ERR_PARSE_ERROR;
  5597. }
  5598. return OK;
  5599. } else if (tk.type == TK_CONST || is_token_precision(tk.type) || is_token_nonvoid_datatype(tk.type) || is_struct) {
  5600. String struct_name = "";
  5601. if (is_struct) {
  5602. struct_name = tk.text;
  5603. }
  5604. bool is_const = false;
  5605. if (tk.type == TK_CONST) {
  5606. is_const = true;
  5607. tk = _get_token();
  5608. if (!is_struct) {
  5609. is_struct = shader->structs.has(tk.text); // check again.
  5610. struct_name = tk.text;
  5611. }
  5612. }
  5613. DataPrecision precision = PRECISION_DEFAULT;
  5614. if (is_token_precision(tk.type)) {
  5615. precision = get_token_precision(tk.type);
  5616. tk = _get_token();
  5617. if (!is_struct) {
  5618. is_struct = shader->structs.has(tk.text); // check again.
  5619. }
  5620. if (is_struct && precision != PRECISION_DEFAULT) {
  5621. _set_error("Precision modifier cannot be used on structs.");
  5622. return ERR_PARSE_ERROR;
  5623. }
  5624. if (!is_token_nonvoid_datatype(tk.type)) {
  5625. _set_error("Expected datatype after precision");
  5626. return ERR_PARSE_ERROR;
  5627. }
  5628. }
  5629. if (!is_struct) {
  5630. if (!is_token_variable_datatype(tk.type)) {
  5631. _set_error("Invalid data type for variable (samplers not allowed)");
  5632. return ERR_PARSE_ERROR;
  5633. }
  5634. }
  5635. DataType type = is_struct ? TYPE_STRUCT : get_token_datatype(tk.type);
  5636. if (_validate_datatype(type) != OK) {
  5637. return ERR_PARSE_ERROR;
  5638. }
  5639. tk = _get_token();
  5640. Node *vardecl = nullptr;
  5641. while (true) {
  5642. bool unknown_size = false;
  5643. int array_size = 0;
  5644. ArrayDeclarationNode *anode = nullptr;
  5645. ArrayDeclarationNode::Declaration adecl;
  5646. if (tk.type != TK_IDENTIFIER && tk.type != TK_BRACKET_OPEN) {
  5647. _set_error("Expected identifier or '[' after type.");
  5648. return ERR_PARSE_ERROR;
  5649. }
  5650. if (tk.type == TK_BRACKET_OPEN) {
  5651. anode = alloc_node<ArrayDeclarationNode>();
  5652. if (is_struct) {
  5653. anode->struct_name = struct_name;
  5654. anode->datatype = TYPE_STRUCT;
  5655. } else {
  5656. anode->datatype = type;
  5657. }
  5658. anode->precision = precision;
  5659. anode->is_const = is_const;
  5660. vardecl = (Node *)anode;
  5661. adecl.size = 0U;
  5662. adecl.single_expression = false;
  5663. Error error = _parse_local_array_size(p_block, p_function_info, anode, &adecl, array_size, unknown_size);
  5664. if (error != OK) {
  5665. return error;
  5666. }
  5667. tk = _get_token();
  5668. if (tk.type != TK_IDENTIFIER) {
  5669. _set_error("Expected identifier!");
  5670. return ERR_PARSE_ERROR;
  5671. }
  5672. }
  5673. StringName name = tk.text;
  5674. ShaderLanguage::IdentifierType itype;
  5675. if (_find_identifier(p_block, true, p_function_info, name, (ShaderLanguage::DataType *)nullptr, &itype)) {
  5676. if (itype != IDENTIFIER_FUNCTION) {
  5677. _set_error("Redefinition of '" + String(name) + "'");
  5678. return ERR_PARSE_ERROR;
  5679. }
  5680. }
  5681. adecl.name = name;
  5682. #ifdef DEBUG_ENABLED
  5683. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_LOCAL_VARIABLE_FLAG)) {
  5684. if (p_block && p_block->parent_function) {
  5685. StringName func_name = p_block->parent_function->name;
  5686. if (!used_local_vars.has(func_name)) {
  5687. used_local_vars.insert(func_name, Map<StringName, Usage>());
  5688. }
  5689. used_local_vars[func_name].insert(name, Usage(tk_line));
  5690. }
  5691. }
  5692. #endif // DEBUG_ENABLED
  5693. BlockNode::Variable var;
  5694. var.type = type;
  5695. var.precision = precision;
  5696. var.line = tk_line;
  5697. var.array_size = array_size;
  5698. var.is_const = is_const;
  5699. var.struct_name = struct_name;
  5700. tk = _get_token();
  5701. if (tk.type == TK_BRACKET_OPEN) {
  5702. if (var.array_size > 0 || unknown_size) {
  5703. _set_error("Array size is already defined!");
  5704. return ERR_PARSE_ERROR;
  5705. }
  5706. if (RenderingServer::get_singleton()->is_low_end() && is_const) {
  5707. _set_error("Local const arrays are supported only on high-end platform!");
  5708. return ERR_PARSE_ERROR;
  5709. }
  5710. anode = alloc_node<ArrayDeclarationNode>();
  5711. if (is_struct) {
  5712. anode->struct_name = struct_name;
  5713. anode->datatype = TYPE_STRUCT;
  5714. } else {
  5715. anode->datatype = type;
  5716. }
  5717. anode->precision = precision;
  5718. anode->is_const = is_const;
  5719. vardecl = (Node *)anode;
  5720. adecl.size = 0U;
  5721. adecl.single_expression = false;
  5722. Error error = _parse_local_array_size(p_block, p_function_info, anode, &adecl, var.array_size, unknown_size);
  5723. if (error != OK) {
  5724. return error;
  5725. }
  5726. tk = _get_token();
  5727. }
  5728. if (var.array_size > 0 || unknown_size) {
  5729. bool full_def = false;
  5730. if (tk.type == TK_OP_ASSIGN) {
  5731. if (RenderingServer::get_singleton()->is_low_end()) {
  5732. _set_error("Array initialization is supported only on high-end platform!");
  5733. return ERR_PARSE_ERROR;
  5734. }
  5735. TkPos prev_pos = _get_tkpos();
  5736. tk = _get_token();
  5737. if (tk.type == TK_IDENTIFIER) { // a function call array initialization
  5738. _set_tkpos(prev_pos);
  5739. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  5740. if (!n) {
  5741. _set_error("Expected correct array initializer!");
  5742. return ERR_PARSE_ERROR;
  5743. } else {
  5744. if (unknown_size) {
  5745. adecl.size = n->get_array_size();
  5746. var.array_size = n->get_array_size();
  5747. }
  5748. if (!_compare_datatypes(var.type, var.struct_name, var.array_size, n->get_datatype(), n->get_datatype_name(), n->get_array_size())) {
  5749. return ERR_PARSE_ERROR;
  5750. }
  5751. adecl.single_expression = true;
  5752. adecl.initializer.push_back(n);
  5753. }
  5754. tk = _get_token();
  5755. } else {
  5756. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  5757. if (unknown_size) {
  5758. _set_error("Expected '{'");
  5759. return ERR_PARSE_ERROR;
  5760. }
  5761. full_def = true;
  5762. DataPrecision precision2 = PRECISION_DEFAULT;
  5763. if (is_token_precision(tk.type)) {
  5764. precision2 = get_token_precision(tk.type);
  5765. tk = _get_token();
  5766. if (shader->structs.has(tk.text)) {
  5767. _set_error("Precision modifier cannot be used on structs.");
  5768. return ERR_PARSE_ERROR;
  5769. }
  5770. if (!is_token_nonvoid_datatype(tk.type)) {
  5771. _set_error("Expected datatype after precision");
  5772. return ERR_PARSE_ERROR;
  5773. }
  5774. }
  5775. DataType type2;
  5776. StringName struct_name2 = "";
  5777. if (shader->structs.has(tk.text)) {
  5778. type2 = TYPE_STRUCT;
  5779. struct_name2 = tk.text;
  5780. } else {
  5781. if (!is_token_variable_datatype(tk.type)) {
  5782. _set_error("Invalid data type for array");
  5783. return ERR_PARSE_ERROR;
  5784. }
  5785. type2 = get_token_datatype(tk.type);
  5786. }
  5787. int array_size2 = 0;
  5788. tk = _get_token();
  5789. if (tk.type == TK_BRACKET_OPEN) {
  5790. TkPos pos2 = _get_tkpos();
  5791. tk = _get_token();
  5792. if (tk.type == TK_BRACKET_CLOSE) {
  5793. array_size2 = var.array_size;
  5794. tk = _get_token();
  5795. } else {
  5796. _set_tkpos(pos2);
  5797. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  5798. if (!n || n->type != Node::TYPE_CONSTANT || n->get_datatype() != TYPE_INT) {
  5799. _set_error("Expected single integer constant > 0");
  5800. return ERR_PARSE_ERROR;
  5801. }
  5802. ConstantNode *cnode = (ConstantNode *)n;
  5803. if (cnode->values.size() == 1) {
  5804. array_size2 = cnode->values[0].sint;
  5805. if (array_size2 <= 0) {
  5806. _set_error("Expected single integer constant > 0");
  5807. return ERR_PARSE_ERROR;
  5808. }
  5809. } else {
  5810. _set_error("Expected single integer constant > 0");
  5811. return ERR_PARSE_ERROR;
  5812. }
  5813. tk = _get_token();
  5814. if (tk.type != TK_BRACKET_CLOSE) {
  5815. _set_error("Expected ']'");
  5816. return ERR_PARSE_ERROR;
  5817. } else {
  5818. tk = _get_token();
  5819. }
  5820. }
  5821. } else {
  5822. _set_error("Expected '['");
  5823. return ERR_PARSE_ERROR;
  5824. }
  5825. if (precision != precision2 || type != type2 || struct_name != struct_name2 || var.array_size != array_size2) {
  5826. String error_str = "Cannot convert from '";
  5827. if (precision2 != PRECISION_DEFAULT) {
  5828. error_str += get_precision_name(precision2);
  5829. error_str += " ";
  5830. }
  5831. if (type2 == TYPE_STRUCT) {
  5832. error_str += struct_name2;
  5833. } else {
  5834. error_str += get_datatype_name(type2);
  5835. }
  5836. error_str += "[";
  5837. error_str += itos(array_size2);
  5838. error_str += "]'";
  5839. error_str += " to '";
  5840. if (precision != PRECISION_DEFAULT) {
  5841. error_str += get_precision_name(precision);
  5842. error_str += " ";
  5843. }
  5844. if (type == TYPE_STRUCT) {
  5845. error_str += struct_name;
  5846. } else {
  5847. error_str += get_datatype_name(type);
  5848. }
  5849. error_str += "[";
  5850. error_str += itos(var.array_size);
  5851. error_str += "]'";
  5852. _set_error(error_str);
  5853. return ERR_PARSE_ERROR;
  5854. }
  5855. }
  5856. bool curly = tk.type == TK_CURLY_BRACKET_OPEN;
  5857. if (unknown_size) {
  5858. if (!curly) {
  5859. _set_error("Expected '{'");
  5860. return ERR_PARSE_ERROR;
  5861. }
  5862. } else {
  5863. if (full_def) {
  5864. if (curly) {
  5865. _set_error("Expected '('");
  5866. return ERR_PARSE_ERROR;
  5867. }
  5868. }
  5869. }
  5870. if (tk.type == TK_PARENTHESIS_OPEN || curly) { // initialization
  5871. while (true) {
  5872. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  5873. if (!n) {
  5874. return ERR_PARSE_ERROR;
  5875. }
  5876. if (anode->is_const && n->type == Node::TYPE_OPERATOR && ((OperatorNode *)n)->op == OP_CALL) {
  5877. _set_error("Expected constant expression");
  5878. return ERR_PARSE_ERROR;
  5879. }
  5880. if (!_compare_datatypes(var.type, struct_name, 0, n->get_datatype(), n->get_datatype_name(), 0)) {
  5881. return ERR_PARSE_ERROR;
  5882. }
  5883. tk = _get_token();
  5884. if (tk.type == TK_COMMA) {
  5885. adecl.initializer.push_back(n);
  5886. continue;
  5887. } else if (!curly && tk.type == TK_PARENTHESIS_CLOSE) {
  5888. adecl.initializer.push_back(n);
  5889. break;
  5890. } else if (curly && tk.type == TK_CURLY_BRACKET_CLOSE) {
  5891. adecl.initializer.push_back(n);
  5892. break;
  5893. } else {
  5894. if (curly) {
  5895. _set_error("Expected '}' or ','");
  5896. } else {
  5897. _set_error("Expected ')' or ','");
  5898. }
  5899. return ERR_PARSE_ERROR;
  5900. }
  5901. }
  5902. if (unknown_size) {
  5903. adecl.size = adecl.initializer.size();
  5904. var.array_size = adecl.initializer.size();
  5905. } else if (adecl.initializer.size() != var.array_size) {
  5906. _set_error("Array size mismatch");
  5907. return ERR_PARSE_ERROR;
  5908. }
  5909. tk = _get_token();
  5910. }
  5911. }
  5912. } else {
  5913. if (unknown_size) {
  5914. _set_error("Expected array initialization");
  5915. return ERR_PARSE_ERROR;
  5916. }
  5917. if (anode->is_const) {
  5918. _set_error("Expected initialization of constant");
  5919. return ERR_PARSE_ERROR;
  5920. }
  5921. }
  5922. anode->declarations.push_back(adecl);
  5923. } else if (tk.type == TK_OP_ASSIGN) {
  5924. VariableDeclarationNode *node = alloc_node<VariableDeclarationNode>();
  5925. if (is_struct) {
  5926. node->struct_name = struct_name;
  5927. node->datatype = TYPE_STRUCT;
  5928. } else {
  5929. node->datatype = type;
  5930. }
  5931. node->precision = precision;
  5932. node->is_const = is_const;
  5933. vardecl = (Node *)node;
  5934. VariableDeclarationNode::Declaration decl;
  5935. decl.name = name;
  5936. decl.initializer = nullptr;
  5937. //variable created with assignment! must parse an expression
  5938. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  5939. if (!n) {
  5940. return ERR_PARSE_ERROR;
  5941. }
  5942. if (node->is_const && n->type == Node::TYPE_OPERATOR && ((OperatorNode *)n)->op == OP_CALL) {
  5943. OperatorNode *op = ((OperatorNode *)n);
  5944. for (int i = 1; i < op->arguments.size(); i++) {
  5945. if (!_check_node_constness(op->arguments[i])) {
  5946. _set_error("Expected constant expression for argument '" + itos(i - 1) + "' of function call after '='");
  5947. return ERR_PARSE_ERROR;
  5948. }
  5949. }
  5950. }
  5951. decl.initializer = n;
  5952. if (n->type == Node::TYPE_CONSTANT) {
  5953. ConstantNode *const_node = static_cast<ConstantNode *>(n);
  5954. if (const_node && const_node->values.size() == 1) {
  5955. var.value = const_node->values[0];
  5956. }
  5957. }
  5958. if (!_compare_datatypes(var.type, var.struct_name, var.array_size, n->get_datatype(), n->get_datatype_name(), n->get_array_size())) {
  5959. return ERR_PARSE_ERROR;
  5960. }
  5961. tk = _get_token();
  5962. node->declarations.push_back(decl);
  5963. } else {
  5964. if (is_const) {
  5965. _set_error("Expected initialization of constant");
  5966. return ERR_PARSE_ERROR;
  5967. }
  5968. VariableDeclarationNode *node = alloc_node<VariableDeclarationNode>();
  5969. if (is_struct) {
  5970. node->struct_name = struct_name;
  5971. node->datatype = TYPE_STRUCT;
  5972. } else {
  5973. node->datatype = type;
  5974. }
  5975. node->precision = precision;
  5976. vardecl = (Node *)node;
  5977. VariableDeclarationNode::Declaration decl;
  5978. decl.name = name;
  5979. decl.initializer = nullptr;
  5980. node->declarations.push_back(decl);
  5981. }
  5982. p_block->statements.push_back(vardecl);
  5983. p_block->variables[name] = var;
  5984. if (tk.type == TK_COMMA) {
  5985. if (p_block->block_type == BlockNode::BLOCK_TYPE_FOR) {
  5986. _set_error("Multiple declarations in 'for' loop are not implemented yet.");
  5987. return ERR_PARSE_ERROR;
  5988. }
  5989. tk = _get_token();
  5990. //another variable
  5991. } else if (tk.type == TK_SEMICOLON) {
  5992. break;
  5993. } else {
  5994. _set_error("Expected ',' or ';' after variable");
  5995. return ERR_PARSE_ERROR;
  5996. }
  5997. }
  5998. } else if (tk.type == TK_CURLY_BRACKET_OPEN) {
  5999. //a sub block, just because..
  6000. BlockNode *block = alloc_node<BlockNode>();
  6001. block->parent_block = p_block;
  6002. if (_parse_block(block, p_function_info, false, p_can_break, p_can_continue) != OK) {
  6003. return ERR_PARSE_ERROR;
  6004. }
  6005. p_block->statements.push_back(block);
  6006. } else if (tk.type == TK_CF_IF) {
  6007. //if () {}
  6008. tk = _get_token();
  6009. if (tk.type != TK_PARENTHESIS_OPEN) {
  6010. _set_error("Expected '(' after if");
  6011. return ERR_PARSE_ERROR;
  6012. }
  6013. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6014. cf->flow_op = FLOW_OP_IF;
  6015. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  6016. if (!n) {
  6017. return ERR_PARSE_ERROR;
  6018. }
  6019. if (n->get_datatype() != TYPE_BOOL) {
  6020. _set_error("Expected boolean expression");
  6021. return ERR_PARSE_ERROR;
  6022. }
  6023. tk = _get_token();
  6024. if (tk.type != TK_PARENTHESIS_CLOSE) {
  6025. _set_error("Expected ')' after expression");
  6026. return ERR_PARSE_ERROR;
  6027. }
  6028. BlockNode *block = alloc_node<BlockNode>();
  6029. block->parent_block = p_block;
  6030. cf->expressions.push_back(n);
  6031. cf->blocks.push_back(block);
  6032. p_block->statements.push_back(cf);
  6033. Error err = _parse_block(block, p_function_info, true, p_can_break, p_can_continue);
  6034. if (err) {
  6035. return err;
  6036. }
  6037. pos = _get_tkpos();
  6038. tk = _get_token();
  6039. if (tk.type == TK_CF_ELSE) {
  6040. block = alloc_node<BlockNode>();
  6041. block->parent_block = p_block;
  6042. cf->blocks.push_back(block);
  6043. err = _parse_block(block, p_function_info, true, p_can_break, p_can_continue);
  6044. } else {
  6045. _set_tkpos(pos); //rollback
  6046. }
  6047. } else if (tk.type == TK_CF_SWITCH) {
  6048. if (RenderingServer::get_singleton()->is_low_end()) {
  6049. _set_error("\"switch\" operator is supported only on high-end platform!");
  6050. return ERR_PARSE_ERROR;
  6051. }
  6052. // switch() {}
  6053. tk = _get_token();
  6054. if (tk.type != TK_PARENTHESIS_OPEN) {
  6055. _set_error("Expected '(' after switch");
  6056. return ERR_PARSE_ERROR;
  6057. }
  6058. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6059. cf->flow_op = FLOW_OP_SWITCH;
  6060. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  6061. if (!n) {
  6062. return ERR_PARSE_ERROR;
  6063. }
  6064. if (n->get_datatype() != TYPE_INT) {
  6065. _set_error("Expected integer expression");
  6066. return ERR_PARSE_ERROR;
  6067. }
  6068. tk = _get_token();
  6069. if (tk.type != TK_PARENTHESIS_CLOSE) {
  6070. _set_error("Expected ')' after expression");
  6071. return ERR_PARSE_ERROR;
  6072. }
  6073. tk = _get_token();
  6074. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  6075. _set_error("Expected '{' after switch statement");
  6076. return ERR_PARSE_ERROR;
  6077. }
  6078. BlockNode *switch_block = alloc_node<BlockNode>();
  6079. switch_block->block_type = BlockNode::BLOCK_TYPE_SWITCH;
  6080. switch_block->parent_block = p_block;
  6081. cf->expressions.push_back(n);
  6082. cf->blocks.push_back(switch_block);
  6083. p_block->statements.push_back(cf);
  6084. int prev_type = TK_CF_CASE;
  6085. while (true) { // Go-through multiple cases.
  6086. if (_parse_block(switch_block, p_function_info, true, true, false) != OK) {
  6087. return ERR_PARSE_ERROR;
  6088. }
  6089. pos = _get_tkpos();
  6090. tk = _get_token();
  6091. if (tk.type == TK_CF_CASE || tk.type == TK_CF_DEFAULT) {
  6092. if (prev_type == TK_CF_DEFAULT) {
  6093. if (tk.type == TK_CF_CASE) {
  6094. _set_error("Cases must be defined before default case.");
  6095. return ERR_PARSE_ERROR;
  6096. } else if (prev_type == TK_CF_DEFAULT) {
  6097. _set_error("Default case must be defined only once.");
  6098. return ERR_PARSE_ERROR;
  6099. }
  6100. }
  6101. prev_type = tk.type;
  6102. _set_tkpos(pos);
  6103. continue;
  6104. } else {
  6105. Set<int> constants;
  6106. for (int i = 0; i < switch_block->statements.size(); i++) { // Checks for duplicates.
  6107. ControlFlowNode *flow = (ControlFlowNode *)switch_block->statements[i];
  6108. if (flow) {
  6109. if (flow->flow_op == FLOW_OP_CASE) {
  6110. if (flow->expressions[0]->type == Node::TYPE_CONSTANT) {
  6111. ConstantNode *cn = static_cast<ConstantNode *>(flow->expressions[0]);
  6112. if (!cn || cn->values.is_empty()) {
  6113. return ERR_PARSE_ERROR;
  6114. }
  6115. if (constants.has(cn->values[0].sint)) {
  6116. _set_error("Duplicated case label: '" + itos(cn->values[0].sint) + "'");
  6117. return ERR_PARSE_ERROR;
  6118. }
  6119. constants.insert(cn->values[0].sint);
  6120. } else if (flow->expressions[0]->type == Node::TYPE_VARIABLE) {
  6121. VariableNode *vn = static_cast<VariableNode *>(flow->expressions[0]);
  6122. if (!vn) {
  6123. return ERR_PARSE_ERROR;
  6124. }
  6125. ConstantNode::Value v;
  6126. _find_identifier(p_block, false, p_function_info, vn->name, nullptr, nullptr, nullptr, nullptr, nullptr, &v);
  6127. if (constants.has(v.sint)) {
  6128. _set_error("Duplicated case label: '" + itos(v.sint) + "'");
  6129. return ERR_PARSE_ERROR;
  6130. }
  6131. constants.insert(v.sint);
  6132. }
  6133. } else if (flow->flow_op == FLOW_OP_DEFAULT) {
  6134. continue;
  6135. } else {
  6136. return ERR_PARSE_ERROR;
  6137. }
  6138. } else {
  6139. return ERR_PARSE_ERROR;
  6140. }
  6141. }
  6142. break;
  6143. }
  6144. }
  6145. } else if (tk.type == TK_CF_CASE) {
  6146. // case x : break; | return;
  6147. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_CASE) {
  6148. _set_tkpos(pos);
  6149. return OK;
  6150. }
  6151. if (!p_block || (p_block->block_type != BlockNode::BLOCK_TYPE_SWITCH)) {
  6152. _set_error("case must be placed within switch block");
  6153. return ERR_PARSE_ERROR;
  6154. }
  6155. tk = _get_token();
  6156. int sign = 1;
  6157. if (tk.type == TK_OP_SUB) {
  6158. sign = -1;
  6159. tk = _get_token();
  6160. }
  6161. Node *n = nullptr;
  6162. if (tk.type != TK_INT_CONSTANT) {
  6163. bool correct_constant_expression = false;
  6164. DataType data_type;
  6165. if (tk.type == TK_IDENTIFIER) {
  6166. bool is_const;
  6167. _find_identifier(p_block, false, p_function_info, tk.text, &data_type, nullptr, &is_const);
  6168. if (is_const) {
  6169. if (data_type == TYPE_INT) {
  6170. correct_constant_expression = true;
  6171. }
  6172. }
  6173. }
  6174. if (!correct_constant_expression) {
  6175. _set_error("Expected integer constant");
  6176. return ERR_PARSE_ERROR;
  6177. }
  6178. VariableNode *vn = alloc_node<VariableNode>();
  6179. vn->name = tk.text;
  6180. n = vn;
  6181. } else {
  6182. ConstantNode::Value v;
  6183. v.sint = (int)tk.constant * sign;
  6184. ConstantNode *cn = alloc_node<ConstantNode>();
  6185. cn->values.push_back(v);
  6186. cn->datatype = TYPE_INT;
  6187. n = cn;
  6188. }
  6189. tk = _get_token();
  6190. if (tk.type != TK_COLON) {
  6191. _set_error("Expected ':'");
  6192. return ERR_PARSE_ERROR;
  6193. }
  6194. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6195. cf->flow_op = FLOW_OP_CASE;
  6196. BlockNode *case_block = alloc_node<BlockNode>();
  6197. case_block->block_type = BlockNode::BLOCK_TYPE_CASE;
  6198. case_block->parent_block = p_block;
  6199. cf->expressions.push_back(n);
  6200. cf->blocks.push_back(case_block);
  6201. p_block->statements.push_back(cf);
  6202. Error err = _parse_block(case_block, p_function_info, false, true, false);
  6203. if (err) {
  6204. return err;
  6205. }
  6206. return OK;
  6207. } else if (tk.type == TK_CF_DEFAULT) {
  6208. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_CASE) {
  6209. _set_tkpos(pos);
  6210. return OK;
  6211. }
  6212. if (!p_block || (p_block->block_type != BlockNode::BLOCK_TYPE_SWITCH)) {
  6213. _set_error("default must be placed within switch block");
  6214. return ERR_PARSE_ERROR;
  6215. }
  6216. tk = _get_token();
  6217. if (tk.type != TK_COLON) {
  6218. _set_error("Expected ':'");
  6219. return ERR_PARSE_ERROR;
  6220. }
  6221. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6222. cf->flow_op = FLOW_OP_DEFAULT;
  6223. BlockNode *default_block = alloc_node<BlockNode>();
  6224. default_block->block_type = BlockNode::BLOCK_TYPE_DEFAULT;
  6225. default_block->parent_block = p_block;
  6226. cf->blocks.push_back(default_block);
  6227. p_block->statements.push_back(cf);
  6228. Error err = _parse_block(default_block, p_function_info, false, true, false);
  6229. if (err) {
  6230. return err;
  6231. }
  6232. return OK;
  6233. } else if (tk.type == TK_CF_DO || tk.type == TK_CF_WHILE) {
  6234. // do {} while()
  6235. // while() {}
  6236. bool is_do = tk.type == TK_CF_DO;
  6237. BlockNode *do_block = nullptr;
  6238. if (is_do) {
  6239. do_block = alloc_node<BlockNode>();
  6240. do_block->parent_block = p_block;
  6241. Error err = _parse_block(do_block, p_function_info, true, true, true);
  6242. if (err) {
  6243. return err;
  6244. }
  6245. tk = _get_token();
  6246. if (tk.type != TK_CF_WHILE) {
  6247. _set_error("Expected while after do");
  6248. return ERR_PARSE_ERROR;
  6249. }
  6250. }
  6251. tk = _get_token();
  6252. if (tk.type != TK_PARENTHESIS_OPEN) {
  6253. _set_error("Expected '(' after while");
  6254. return ERR_PARSE_ERROR;
  6255. }
  6256. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6257. if (is_do) {
  6258. cf->flow_op = FLOW_OP_DO;
  6259. } else {
  6260. cf->flow_op = FLOW_OP_WHILE;
  6261. }
  6262. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  6263. if (!n) {
  6264. return ERR_PARSE_ERROR;
  6265. }
  6266. tk = _get_token();
  6267. if (tk.type != TK_PARENTHESIS_CLOSE) {
  6268. _set_error("Expected ')' after expression");
  6269. return ERR_PARSE_ERROR;
  6270. }
  6271. if (!is_do) {
  6272. BlockNode *block = alloc_node<BlockNode>();
  6273. block->parent_block = p_block;
  6274. cf->expressions.push_back(n);
  6275. cf->blocks.push_back(block);
  6276. p_block->statements.push_back(cf);
  6277. Error err = _parse_block(block, p_function_info, true, true, true);
  6278. if (err) {
  6279. return err;
  6280. }
  6281. } else {
  6282. cf->expressions.push_back(n);
  6283. cf->blocks.push_back(do_block);
  6284. p_block->statements.push_back(cf);
  6285. tk = _get_token();
  6286. if (tk.type != TK_SEMICOLON) {
  6287. _set_error("Expected ';'");
  6288. return ERR_PARSE_ERROR;
  6289. }
  6290. }
  6291. } else if (tk.type == TK_CF_FOR) {
  6292. // for() {}
  6293. tk = _get_token();
  6294. if (tk.type != TK_PARENTHESIS_OPEN) {
  6295. _set_error("Expected '(' after for");
  6296. return ERR_PARSE_ERROR;
  6297. }
  6298. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6299. cf->flow_op = FLOW_OP_FOR;
  6300. BlockNode *init_block = alloc_node<BlockNode>();
  6301. init_block->block_type = BlockNode::BLOCK_TYPE_FOR;
  6302. init_block->parent_block = p_block;
  6303. init_block->single_statement = true;
  6304. cf->blocks.push_back(init_block);
  6305. if (_parse_block(init_block, p_function_info, true, false, false) != OK) {
  6306. return ERR_PARSE_ERROR;
  6307. }
  6308. Node *n = _parse_and_reduce_expression(init_block, p_function_info);
  6309. if (!n) {
  6310. return ERR_PARSE_ERROR;
  6311. }
  6312. if (n->get_datatype() != TYPE_BOOL) {
  6313. _set_error("Middle expression is expected to be boolean.");
  6314. return ERR_PARSE_ERROR;
  6315. }
  6316. tk = _get_token();
  6317. if (tk.type != TK_SEMICOLON) {
  6318. _set_error("Expected ';' after middle expression");
  6319. return ERR_PARSE_ERROR;
  6320. }
  6321. cf->expressions.push_back(n);
  6322. n = _parse_and_reduce_expression(init_block, p_function_info);
  6323. if (!n) {
  6324. return ERR_PARSE_ERROR;
  6325. }
  6326. cf->expressions.push_back(n);
  6327. tk = _get_token();
  6328. if (tk.type != TK_PARENTHESIS_CLOSE) {
  6329. _set_error("Expected ')' after third expression");
  6330. return ERR_PARSE_ERROR;
  6331. }
  6332. BlockNode *block = alloc_node<BlockNode>();
  6333. block->parent_block = init_block;
  6334. cf->blocks.push_back(block);
  6335. p_block->statements.push_back(cf);
  6336. Error err = _parse_block(block, p_function_info, true, true, true);
  6337. if (err) {
  6338. return err;
  6339. }
  6340. } else if (tk.type == TK_CF_RETURN) {
  6341. //check return type
  6342. BlockNode *b = p_block;
  6343. if (b && b->parent_function && p_function_info.main_function) {
  6344. _set_error(vformat("Using 'return' in '%s' processor function results in undefined behavior!", b->parent_function->name));
  6345. return ERR_PARSE_ERROR;
  6346. }
  6347. while (b && !b->parent_function) {
  6348. b = b->parent_block;
  6349. }
  6350. if (!b) {
  6351. _set_error("Bug");
  6352. return ERR_BUG;
  6353. }
  6354. String return_struct_name = String(b->parent_function->return_struct_name);
  6355. String array_size_string;
  6356. if (b->parent_function->return_array_size > 0) {
  6357. array_size_string = "[" + itos(b->parent_function->return_array_size) + "]";
  6358. }
  6359. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  6360. flow->flow_op = FLOW_OP_RETURN;
  6361. pos = _get_tkpos();
  6362. tk = _get_token();
  6363. if (tk.type == TK_SEMICOLON) {
  6364. //all is good
  6365. if (b->parent_function->return_type != TYPE_VOID) {
  6366. _set_error("Expected return with an expression of type '" + (return_struct_name != "" ? return_struct_name : get_datatype_name(b->parent_function->return_type)) + array_size_string + "'");
  6367. return ERR_PARSE_ERROR;
  6368. }
  6369. } else {
  6370. _set_tkpos(pos); //rollback, wants expression
  6371. Node *expr = _parse_and_reduce_expression(p_block, p_function_info);
  6372. if (!expr) {
  6373. return ERR_PARSE_ERROR;
  6374. }
  6375. if (b->parent_function->return_type != expr->get_datatype() || b->parent_function->return_array_size != expr->get_array_size() || return_struct_name != expr->get_datatype_name()) {
  6376. _set_error("Expected return with an expression of type '" + (return_struct_name != "" ? return_struct_name : get_datatype_name(b->parent_function->return_type)) + array_size_string + "'");
  6377. return ERR_PARSE_ERROR;
  6378. }
  6379. tk = _get_token();
  6380. if (tk.type != TK_SEMICOLON) {
  6381. _set_error("Expected ';' after return expression");
  6382. return ERR_PARSE_ERROR;
  6383. }
  6384. flow->expressions.push_back(expr);
  6385. }
  6386. p_block->statements.push_back(flow);
  6387. BlockNode *block = p_block;
  6388. while (block) {
  6389. if (block->block_type == BlockNode::BLOCK_TYPE_CASE || block->block_type == BlockNode::BLOCK_TYPE_DEFAULT) {
  6390. return OK;
  6391. }
  6392. block = block->parent_block;
  6393. }
  6394. } else if (tk.type == TK_CF_DISCARD) {
  6395. //check return type
  6396. BlockNode *b = p_block;
  6397. while (b && !b->parent_function) {
  6398. b = b->parent_block;
  6399. }
  6400. if (!b) {
  6401. _set_error("Bug");
  6402. return ERR_BUG;
  6403. }
  6404. if (!b->parent_function->can_discard) {
  6405. _set_error("Use of 'discard' is not allowed here.");
  6406. return ERR_PARSE_ERROR;
  6407. }
  6408. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  6409. flow->flow_op = FLOW_OP_DISCARD;
  6410. pos = _get_tkpos();
  6411. tk = _get_token();
  6412. if (tk.type != TK_SEMICOLON) {
  6413. _set_error("Expected ';' after discard");
  6414. return ERR_PARSE_ERROR;
  6415. }
  6416. p_block->statements.push_back(flow);
  6417. } else if (tk.type == TK_CF_BREAK) {
  6418. if (!p_can_break) {
  6419. _set_error("'break' is not allowed outside of a loop or 'switch' statement");
  6420. return ERR_PARSE_ERROR;
  6421. }
  6422. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  6423. flow->flow_op = FLOW_OP_BREAK;
  6424. pos = _get_tkpos();
  6425. tk = _get_token();
  6426. if (tk.type != TK_SEMICOLON) {
  6427. _set_error("Expected ';' after break");
  6428. return ERR_PARSE_ERROR;
  6429. }
  6430. p_block->statements.push_back(flow);
  6431. BlockNode *block = p_block;
  6432. while (block) {
  6433. if (block->block_type == BlockNode::BLOCK_TYPE_CASE || block->block_type == BlockNode::BLOCK_TYPE_DEFAULT) {
  6434. return OK;
  6435. }
  6436. block = block->parent_block;
  6437. }
  6438. } else if (tk.type == TK_CF_CONTINUE) {
  6439. if (!p_can_continue) {
  6440. _set_error("'continue' is not allowed outside of a loop");
  6441. return ERR_PARSE_ERROR;
  6442. }
  6443. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  6444. flow->flow_op = FLOW_OP_CONTINUE;
  6445. pos = _get_tkpos();
  6446. tk = _get_token();
  6447. if (tk.type != TK_SEMICOLON) {
  6448. //all is good
  6449. _set_error("Expected ';' after continue");
  6450. return ERR_PARSE_ERROR;
  6451. }
  6452. p_block->statements.push_back(flow);
  6453. } else {
  6454. //nothing else, so expression
  6455. _set_tkpos(pos); //rollback
  6456. Node *expr = _parse_and_reduce_expression(p_block, p_function_info);
  6457. if (!expr) {
  6458. return ERR_PARSE_ERROR;
  6459. }
  6460. p_block->statements.push_back(expr);
  6461. tk = _get_token();
  6462. if (tk.type != TK_SEMICOLON) {
  6463. _set_error("Expected ';' after statement");
  6464. return ERR_PARSE_ERROR;
  6465. }
  6466. }
  6467. if (p_just_one) {
  6468. break;
  6469. }
  6470. }
  6471. return OK;
  6472. }
  6473. String ShaderLanguage::_get_shader_type_list(const Set<String> &p_shader_types) const {
  6474. // Return a list of shader types as an human-readable string
  6475. String valid_types;
  6476. for (const Set<String>::Element *E = p_shader_types.front(); E; E = E->next()) {
  6477. if (valid_types != String()) {
  6478. valid_types += ", ";
  6479. }
  6480. valid_types += "'" + E->get() + "'";
  6481. }
  6482. return valid_types;
  6483. }
  6484. String ShaderLanguage::_get_qualifier_str(ArgumentQualifier p_qualifier) const {
  6485. switch (p_qualifier) {
  6486. case ArgumentQualifier::ARGUMENT_QUALIFIER_IN:
  6487. return "in";
  6488. case ArgumentQualifier::ARGUMENT_QUALIFIER_OUT:
  6489. return "out";
  6490. case ArgumentQualifier::ARGUMENT_QUALIFIER_INOUT:
  6491. return "inout";
  6492. }
  6493. return "";
  6494. }
  6495. Error ShaderLanguage::_validate_datatype(DataType p_type) {
  6496. if (RenderingServer::get_singleton()->is_low_end()) {
  6497. bool invalid_type = false;
  6498. switch (p_type) {
  6499. case TYPE_UINT:
  6500. case TYPE_UVEC2:
  6501. case TYPE_UVEC3:
  6502. case TYPE_UVEC4:
  6503. case TYPE_ISAMPLER2D:
  6504. case TYPE_USAMPLER2D:
  6505. case TYPE_ISAMPLER3D:
  6506. case TYPE_USAMPLER3D:
  6507. case TYPE_USAMPLER2DARRAY:
  6508. case TYPE_ISAMPLER2DARRAY:
  6509. invalid_type = true;
  6510. break;
  6511. default:
  6512. break;
  6513. }
  6514. if (invalid_type) {
  6515. _set_error(vformat("\"%s\" type is supported only on high-end platform!", get_datatype_name(p_type)));
  6516. return ERR_UNAVAILABLE;
  6517. }
  6518. }
  6519. return OK;
  6520. }
  6521. Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types) {
  6522. Token tk = _get_token();
  6523. TkPos prev_pos;
  6524. if (tk.type != TK_SHADER_TYPE) {
  6525. _set_error("Expected 'shader_type' at the beginning of shader. Valid types are: " + _get_shader_type_list(p_shader_types));
  6526. return ERR_PARSE_ERROR;
  6527. }
  6528. tk = _get_token();
  6529. if (tk.type != TK_IDENTIFIER) {
  6530. _set_error("Expected identifier after 'shader_type', indicating type of shader. Valid types are: " + _get_shader_type_list(p_shader_types));
  6531. return ERR_PARSE_ERROR;
  6532. }
  6533. String shader_type_identifier;
  6534. shader_type_identifier = tk.text;
  6535. if (!p_shader_types.has(shader_type_identifier)) {
  6536. _set_error("Invalid shader type. Valid types are: " + _get_shader_type_list(p_shader_types));
  6537. return ERR_PARSE_ERROR;
  6538. }
  6539. prev_pos = _get_tkpos();
  6540. tk = _get_token();
  6541. if (tk.type != TK_SEMICOLON) {
  6542. _set_tkpos(prev_pos);
  6543. _set_error("Expected ';' after 'shader_type <type>'.");
  6544. return ERR_PARSE_ERROR;
  6545. }
  6546. tk = _get_token();
  6547. int texture_uniforms = 0;
  6548. int texture_binding = 0;
  6549. int uniforms = 0;
  6550. int instance_index = 0;
  6551. ShaderNode::Uniform::Scope uniform_scope = ShaderNode::Uniform::SCOPE_LOCAL;
  6552. stages = &p_functions;
  6553. while (tk.type != TK_EOF) {
  6554. switch (tk.type) {
  6555. case TK_RENDER_MODE: {
  6556. while (true) {
  6557. StringName mode;
  6558. _get_completable_identifier(nullptr, COMPLETION_RENDER_MODE, mode);
  6559. if (mode == StringName()) {
  6560. _set_error("Expected identifier for render mode");
  6561. return ERR_PARSE_ERROR;
  6562. }
  6563. if (p_render_modes.find(mode) == -1) {
  6564. _set_error("Invalid render mode: '" + String(mode) + "'");
  6565. return ERR_PARSE_ERROR;
  6566. }
  6567. if (shader->render_modes.find(mode) != -1) {
  6568. _set_error("Duplicate render mode: '" + String(mode) + "'");
  6569. return ERR_PARSE_ERROR;
  6570. }
  6571. shader->render_modes.push_back(mode);
  6572. tk = _get_token();
  6573. if (tk.type == TK_COMMA) {
  6574. //all good, do nothing
  6575. } else if (tk.type == TK_SEMICOLON) {
  6576. break; //done
  6577. } else {
  6578. _set_error("Unexpected token: " + get_token_text(tk));
  6579. return ERR_PARSE_ERROR;
  6580. }
  6581. }
  6582. } break;
  6583. case TK_STRUCT: {
  6584. ShaderNode::Struct st;
  6585. DataType type;
  6586. tk = _get_token();
  6587. if (tk.type == TK_IDENTIFIER) {
  6588. st.name = tk.text;
  6589. if (shader->structs.has(st.name)) {
  6590. _set_error("Redefinition of '" + String(st.name) + "'");
  6591. return ERR_PARSE_ERROR;
  6592. }
  6593. tk = _get_token();
  6594. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  6595. _set_error("Expected '{'");
  6596. return ERR_PARSE_ERROR;
  6597. }
  6598. } else {
  6599. _set_error("Expected struct identifier!");
  6600. return ERR_PARSE_ERROR;
  6601. }
  6602. StructNode *st_node = alloc_node<StructNode>();
  6603. st.shader_struct = st_node;
  6604. int member_count = 0;
  6605. Set<String> member_names;
  6606. while (true) { // variables list
  6607. tk = _get_token();
  6608. if (tk.type == TK_CURLY_BRACKET_CLOSE) {
  6609. break;
  6610. }
  6611. StringName struct_name = "";
  6612. bool struct_dt = false;
  6613. bool use_precision = false;
  6614. DataPrecision precision = DataPrecision::PRECISION_DEFAULT;
  6615. if (tk.type == TK_STRUCT) {
  6616. _set_error("nested structs are not allowed!");
  6617. return ERR_PARSE_ERROR;
  6618. }
  6619. if (is_token_precision(tk.type)) {
  6620. precision = get_token_precision(tk.type);
  6621. use_precision = true;
  6622. tk = _get_token();
  6623. }
  6624. if (shader->structs.has(tk.text)) {
  6625. struct_name = tk.text;
  6626. struct_dt = true;
  6627. if (use_precision) {
  6628. _set_error("Precision modifier cannot be used on structs.");
  6629. return ERR_PARSE_ERROR;
  6630. }
  6631. }
  6632. if (!is_token_datatype(tk.type) && !struct_dt) {
  6633. _set_error("Expected datatype.");
  6634. return ERR_PARSE_ERROR;
  6635. } else {
  6636. type = struct_dt ? TYPE_STRUCT : get_token_datatype(tk.type);
  6637. if (is_sampler_type(type)) {
  6638. _set_error("sampler datatype not allowed here");
  6639. return ERR_PARSE_ERROR;
  6640. } else if (type == TYPE_VOID) {
  6641. _set_error("void datatype not allowed here");
  6642. return ERR_PARSE_ERROR;
  6643. }
  6644. tk = _get_token();
  6645. if (tk.type != TK_IDENTIFIER && tk.type != TK_BRACKET_OPEN) {
  6646. _set_error("Expected identifier or '['.");
  6647. return ERR_PARSE_ERROR;
  6648. }
  6649. int array_size = 0;
  6650. if (tk.type == TK_BRACKET_OPEN) {
  6651. Error error = _parse_global_array_size(array_size);
  6652. if (error != OK) {
  6653. return error;
  6654. }
  6655. tk = _get_token();
  6656. }
  6657. if (tk.type != TK_IDENTIFIER) {
  6658. _set_error("Expected identifier!");
  6659. return ERR_PARSE_ERROR;
  6660. }
  6661. MemberNode *member = alloc_node<MemberNode>();
  6662. member->precision = precision;
  6663. member->datatype = type;
  6664. member->struct_name = struct_name;
  6665. member->name = tk.text;
  6666. member->array_size = array_size;
  6667. if (member_names.has(member->name)) {
  6668. _set_error("Redefinition of '" + String(member->name) + "'");
  6669. return ERR_PARSE_ERROR;
  6670. }
  6671. member_names.insert(member->name);
  6672. tk = _get_token();
  6673. if (tk.type == TK_BRACKET_OPEN) {
  6674. Error error = _parse_global_array_size(member->array_size);
  6675. if (error != OK) {
  6676. return error;
  6677. }
  6678. tk = _get_token();
  6679. }
  6680. if (tk.type != TK_SEMICOLON) {
  6681. _set_error("Expected ';'");
  6682. return ERR_PARSE_ERROR;
  6683. }
  6684. st_node->members.push_back(member);
  6685. member_count++;
  6686. }
  6687. }
  6688. if (member_count == 0) {
  6689. _set_error("Empty structs are not allowed!");
  6690. return ERR_PARSE_ERROR;
  6691. }
  6692. tk = _get_token();
  6693. if (tk.type != TK_SEMICOLON) {
  6694. _set_error("Expected ';'");
  6695. return ERR_PARSE_ERROR;
  6696. }
  6697. shader->structs[st.name] = st;
  6698. shader->vstructs.push_back(st); // struct's order is important!
  6699. #ifdef DEBUG_ENABLED
  6700. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_STRUCT_FLAG)) {
  6701. used_structs.insert(st.name, Usage(tk_line));
  6702. }
  6703. #endif // DEBUG_ENABLED
  6704. } break;
  6705. case TK_GLOBAL: {
  6706. tk = _get_token();
  6707. if (tk.type != TK_UNIFORM) {
  6708. _set_error("Expected 'uniform' after 'global'");
  6709. return ERR_PARSE_ERROR;
  6710. }
  6711. uniform_scope = ShaderNode::Uniform::SCOPE_GLOBAL;
  6712. };
  6713. [[fallthrough]];
  6714. case TK_INSTANCE: {
  6715. if (uniform_scope == ShaderNode::Uniform::SCOPE_LOCAL) {
  6716. tk = _get_token();
  6717. if (tk.type != TK_UNIFORM) {
  6718. _set_error("Expected 'uniform' after 'instance'");
  6719. return ERR_PARSE_ERROR;
  6720. }
  6721. uniform_scope = ShaderNode::Uniform::SCOPE_INSTANCE;
  6722. }
  6723. };
  6724. [[fallthrough]];
  6725. case TK_UNIFORM:
  6726. case TK_VARYING: {
  6727. bool uniform = tk.type == TK_UNIFORM;
  6728. if (!uniform) {
  6729. if (shader_type_identifier == "particles" || shader_type_identifier == "sky") {
  6730. _set_error(vformat("Varyings cannot be used in '%s' shaders!", shader_type_identifier));
  6731. return ERR_PARSE_ERROR;
  6732. }
  6733. }
  6734. bool precision_defined = false;
  6735. DataPrecision precision = PRECISION_DEFAULT;
  6736. DataInterpolation interpolation = INTERPOLATION_SMOOTH;
  6737. DataType type;
  6738. StringName name;
  6739. int array_size = 0;
  6740. tk = _get_token();
  6741. if (is_token_interpolation(tk.type)) {
  6742. if (uniform) {
  6743. _set_error("Interpolation qualifiers are not supported for uniforms!");
  6744. return ERR_PARSE_ERROR;
  6745. }
  6746. interpolation = get_token_interpolation(tk.type);
  6747. tk = _get_token();
  6748. }
  6749. if (is_token_precision(tk.type)) {
  6750. precision = get_token_precision(tk.type);
  6751. precision_defined = true;
  6752. tk = _get_token();
  6753. }
  6754. if (shader->structs.has(tk.text)) {
  6755. if (uniform) {
  6756. if (precision_defined) {
  6757. _set_error("Precision modifier cannot be used on structs.");
  6758. return ERR_PARSE_ERROR;
  6759. }
  6760. _set_error("struct datatype is not yet supported for uniforms!");
  6761. return ERR_PARSE_ERROR;
  6762. } else {
  6763. _set_error("struct datatype not allowed here");
  6764. return ERR_PARSE_ERROR;
  6765. }
  6766. }
  6767. if (!is_token_datatype(tk.type)) {
  6768. _set_error("Expected datatype. ");
  6769. return ERR_PARSE_ERROR;
  6770. }
  6771. type = get_token_datatype(tk.type);
  6772. if (type == TYPE_VOID) {
  6773. _set_error("void datatype not allowed here");
  6774. return ERR_PARSE_ERROR;
  6775. }
  6776. if (!uniform && (type < TYPE_FLOAT || type > TYPE_MAT4)) {
  6777. _set_error("Invalid type for varying, only float,vec2,vec3,vec4,mat2,mat3,mat4 or array of these types allowed.");
  6778. return ERR_PARSE_ERROR;
  6779. }
  6780. tk = _get_token();
  6781. if (tk.type != TK_IDENTIFIER && tk.type != TK_BRACKET_OPEN) {
  6782. _set_error("Expected identifier or '['.");
  6783. return ERR_PARSE_ERROR;
  6784. }
  6785. if (tk.type == TK_BRACKET_OPEN) {
  6786. Error error = _parse_global_array_size(array_size);
  6787. if (error != OK) {
  6788. return error;
  6789. }
  6790. tk = _get_token();
  6791. }
  6792. if (tk.type != TK_IDENTIFIER) {
  6793. _set_error("Expected identifier!");
  6794. return ERR_PARSE_ERROR;
  6795. }
  6796. prev_pos = _get_tkpos();
  6797. name = tk.text;
  6798. if (_find_identifier(nullptr, false, FunctionInfo(), name)) {
  6799. _set_error("Redefinition of '" + String(name) + "'");
  6800. return ERR_PARSE_ERROR;
  6801. }
  6802. if (has_builtin(p_functions, name)) {
  6803. _set_error("Redefinition of '" + String(name) + "'");
  6804. return ERR_PARSE_ERROR;
  6805. }
  6806. if (uniform) {
  6807. if (uniform_scope == ShaderNode::Uniform::SCOPE_GLOBAL) {
  6808. //validate global uniform
  6809. DataType gvtype = global_var_get_type_func(name);
  6810. if (gvtype == TYPE_MAX) {
  6811. _set_error("Global uniform '" + String(name) + "' does not exist. Create it in Project Settings.");
  6812. return ERR_PARSE_ERROR;
  6813. }
  6814. if (type != gvtype) {
  6815. _set_error("Global uniform '" + String(name) + "' must be of type '" + get_datatype_name(gvtype) + "'.");
  6816. return ERR_PARSE_ERROR;
  6817. }
  6818. }
  6819. ShaderNode::Uniform uniform2;
  6820. uniform2.type = type;
  6821. uniform2.scope = uniform_scope;
  6822. uniform2.precision = precision;
  6823. uniform2.array_size = array_size;
  6824. tk = _get_token();
  6825. if (tk.type == TK_BRACKET_OPEN) {
  6826. Error error = _parse_global_array_size(uniform2.array_size);
  6827. if (error != OK) {
  6828. return error;
  6829. }
  6830. tk = _get_token();
  6831. }
  6832. if (is_sampler_type(type)) {
  6833. if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE) {
  6834. _set_error("Uniforms with 'instance' qualifiers can't be of sampler type.");
  6835. return ERR_PARSE_ERROR;
  6836. }
  6837. uniform2.texture_order = texture_uniforms++;
  6838. uniform2.texture_binding = texture_binding;
  6839. if (uniform2.array_size > 0) {
  6840. texture_binding += uniform2.array_size;
  6841. } else {
  6842. ++texture_binding;
  6843. }
  6844. uniform2.order = -1;
  6845. if (_validate_datatype(type) != OK) {
  6846. return ERR_PARSE_ERROR;
  6847. }
  6848. } else {
  6849. if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE && (type == TYPE_MAT2 || type == TYPE_MAT3 || type == TYPE_MAT4)) {
  6850. _set_error("Uniforms with 'instance' qualifiers can't be of matrix type.");
  6851. return ERR_PARSE_ERROR;
  6852. }
  6853. uniform2.texture_order = -1;
  6854. if (uniform_scope != ShaderNode::Uniform::SCOPE_INSTANCE) {
  6855. uniform2.order = uniforms++;
  6856. }
  6857. }
  6858. if (uniform2.array_size > 0) {
  6859. if (uniform_scope == ShaderNode::Uniform::SCOPE_GLOBAL) {
  6860. _set_error("'SCOPE_GLOBAL' qualifier is not yet supported for uniform array!");
  6861. return ERR_PARSE_ERROR;
  6862. }
  6863. if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE) {
  6864. _set_error("'SCOPE_INSTANCE' qualifier is not yet supported for uniform array!");
  6865. return ERR_PARSE_ERROR;
  6866. }
  6867. }
  6868. int custom_instance_index = -1;
  6869. if (tk.type == TK_COLON) {
  6870. //hint
  6871. do {
  6872. tk = _get_token();
  6873. if (uniform2.array_size > 0) {
  6874. if (tk.type != TK_HINT_COLOR) {
  6875. _set_error("This hint is not yet supported for uniform arrays!");
  6876. return ERR_PARSE_ERROR;
  6877. }
  6878. }
  6879. if (tk.type == TK_HINT_WHITE_TEXTURE) {
  6880. uniform2.hint = ShaderNode::Uniform::HINT_WHITE;
  6881. } else if (tk.type == TK_HINT_BLACK_TEXTURE) {
  6882. uniform2.hint = ShaderNode::Uniform::HINT_BLACK;
  6883. } else if (tk.type == TK_HINT_NORMAL_TEXTURE) {
  6884. uniform2.hint = ShaderNode::Uniform::HINT_NORMAL;
  6885. } else if (tk.type == TK_HINT_ROUGHNESS_NORMAL_TEXTURE) {
  6886. uniform2.hint = ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL;
  6887. } else if (tk.type == TK_HINT_ROUGHNESS_R) {
  6888. uniform2.hint = ShaderNode::Uniform::HINT_ROUGHNESS_R;
  6889. } else if (tk.type == TK_HINT_ROUGHNESS_G) {
  6890. uniform2.hint = ShaderNode::Uniform::HINT_ROUGHNESS_G;
  6891. } else if (tk.type == TK_HINT_ROUGHNESS_B) {
  6892. uniform2.hint = ShaderNode::Uniform::HINT_ROUGHNESS_B;
  6893. } else if (tk.type == TK_HINT_ROUGHNESS_A) {
  6894. uniform2.hint = ShaderNode::Uniform::HINT_ROUGHNESS_A;
  6895. } else if (tk.type == TK_HINT_ROUGHNESS_GRAY) {
  6896. uniform2.hint = ShaderNode::Uniform::HINT_ROUGHNESS_GRAY;
  6897. } else if (tk.type == TK_HINT_ANISO_TEXTURE) {
  6898. uniform2.hint = ShaderNode::Uniform::HINT_ANISO;
  6899. } else if (tk.type == TK_HINT_ALBEDO_TEXTURE) {
  6900. uniform2.hint = ShaderNode::Uniform::HINT_ALBEDO;
  6901. } else if (tk.type == TK_HINT_BLACK_ALBEDO_TEXTURE) {
  6902. uniform2.hint = ShaderNode::Uniform::HINT_BLACK_ALBEDO;
  6903. } else if (tk.type == TK_HINT_COLOR) {
  6904. if (type != TYPE_VEC4) {
  6905. _set_error("Color hint is for vec4 only");
  6906. return ERR_PARSE_ERROR;
  6907. }
  6908. uniform2.hint = ShaderNode::Uniform::HINT_COLOR;
  6909. } else if (tk.type == TK_HINT_RANGE) {
  6910. uniform2.hint = ShaderNode::Uniform::HINT_RANGE;
  6911. if (type != TYPE_FLOAT && type != TYPE_INT) {
  6912. _set_error("Range hint is for float and int only");
  6913. return ERR_PARSE_ERROR;
  6914. }
  6915. tk = _get_token();
  6916. if (tk.type != TK_PARENTHESIS_OPEN) {
  6917. _set_error("Expected '(' after hint_range");
  6918. return ERR_PARSE_ERROR;
  6919. }
  6920. tk = _get_token();
  6921. float sign = 1.0;
  6922. if (tk.type == TK_OP_SUB) {
  6923. sign = -1.0;
  6924. tk = _get_token();
  6925. }
  6926. if (tk.type != TK_FLOAT_CONSTANT && tk.type != TK_INT_CONSTANT) {
  6927. _set_error("Expected integer constant");
  6928. return ERR_PARSE_ERROR;
  6929. }
  6930. uniform2.hint_range[0] = tk.constant;
  6931. uniform2.hint_range[0] *= sign;
  6932. tk = _get_token();
  6933. if (tk.type != TK_COMMA) {
  6934. _set_error("Expected ',' after integer constant");
  6935. return ERR_PARSE_ERROR;
  6936. }
  6937. tk = _get_token();
  6938. sign = 1.0;
  6939. if (tk.type == TK_OP_SUB) {
  6940. sign = -1.0;
  6941. tk = _get_token();
  6942. }
  6943. if (tk.type != TK_FLOAT_CONSTANT && tk.type != TK_INT_CONSTANT) {
  6944. _set_error("Expected integer constant after ','");
  6945. return ERR_PARSE_ERROR;
  6946. }
  6947. uniform2.hint_range[1] = tk.constant;
  6948. uniform2.hint_range[1] *= sign;
  6949. tk = _get_token();
  6950. if (tk.type == TK_COMMA) {
  6951. tk = _get_token();
  6952. if (tk.type != TK_FLOAT_CONSTANT && tk.type != TK_INT_CONSTANT) {
  6953. _set_error("Expected integer constant after ','");
  6954. return ERR_PARSE_ERROR;
  6955. }
  6956. uniform2.hint_range[2] = tk.constant;
  6957. tk = _get_token();
  6958. } else {
  6959. if (type == TYPE_INT) {
  6960. uniform2.hint_range[2] = 1;
  6961. } else {
  6962. uniform2.hint_range[2] = 0.001;
  6963. }
  6964. }
  6965. if (tk.type != TK_PARENTHESIS_CLOSE) {
  6966. _set_error("Expected ')'");
  6967. return ERR_PARSE_ERROR;
  6968. }
  6969. } else if (tk.type == TK_HINT_INSTANCE_INDEX) {
  6970. if (custom_instance_index != -1) {
  6971. _set_error("Can only specify 'instance_index' once.");
  6972. return ERR_PARSE_ERROR;
  6973. }
  6974. tk = _get_token();
  6975. if (tk.type != TK_PARENTHESIS_OPEN) {
  6976. _set_error("Expected '(' after 'instance_index'");
  6977. return ERR_PARSE_ERROR;
  6978. }
  6979. tk = _get_token();
  6980. if (tk.type == TK_OP_SUB) {
  6981. _set_error("The instance index can't be negative.");
  6982. return ERR_PARSE_ERROR;
  6983. }
  6984. if (tk.type != TK_INT_CONSTANT) {
  6985. _set_error("Expected integer constant");
  6986. return ERR_PARSE_ERROR;
  6987. }
  6988. custom_instance_index = tk.constant;
  6989. if (custom_instance_index >= MAX_INSTANCE_UNIFORM_INDICES) {
  6990. _set_error("Allowed instance uniform indices are 0-" + itos(MAX_INSTANCE_UNIFORM_INDICES - 1));
  6991. return ERR_PARSE_ERROR;
  6992. }
  6993. tk = _get_token();
  6994. if (tk.type != TK_PARENTHESIS_CLOSE) {
  6995. _set_error("Expected ')'");
  6996. return ERR_PARSE_ERROR;
  6997. }
  6998. } else if (tk.type == TK_FILTER_LINEAR) {
  6999. uniform2.filter = FILTER_LINEAR;
  7000. } else if (tk.type == TK_FILTER_NEAREST) {
  7001. uniform2.filter = FILTER_NEAREST;
  7002. } else if (tk.type == TK_FILTER_NEAREST_MIPMAP) {
  7003. uniform2.filter = FILTER_NEAREST_MIPMAP;
  7004. } else if (tk.type == TK_FILTER_LINEAR_MIPMAP) {
  7005. uniform2.filter = FILTER_LINEAR_MIPMAP;
  7006. } else if (tk.type == TK_FILTER_NEAREST_MIPMAP_ANISO) {
  7007. uniform2.filter = FILTER_NEAREST_MIPMAP_ANISO;
  7008. } else if (tk.type == TK_FILTER_LINEAR_MIPMAP_ANISO) {
  7009. uniform2.filter = FILTER_LINEAR_MIPMAP_ANISO;
  7010. } else if (tk.type == TK_REPEAT_DISABLE) {
  7011. uniform2.repeat = REPEAT_DISABLE;
  7012. } else if (tk.type == TK_REPEAT_ENABLE) {
  7013. uniform2.repeat = REPEAT_ENABLE;
  7014. } else {
  7015. _set_error("Expected valid type hint after ':'.");
  7016. }
  7017. if (uniform2.hint != ShaderNode::Uniform::HINT_RANGE && uniform2.hint != ShaderNode::Uniform::HINT_NONE && uniform2.hint != ShaderNode::Uniform::HINT_COLOR && type <= TYPE_MAT4) {
  7018. _set_error("This hint is only for sampler types");
  7019. return ERR_PARSE_ERROR;
  7020. }
  7021. tk = _get_token();
  7022. } while (tk.type == TK_COMMA);
  7023. }
  7024. if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE) {
  7025. if (custom_instance_index >= 0) {
  7026. uniform2.instance_index = custom_instance_index;
  7027. } else {
  7028. uniform2.instance_index = instance_index++;
  7029. if (instance_index > MAX_INSTANCE_UNIFORM_INDICES) {
  7030. _set_error("Too many 'instance' uniforms in shader, maximum supported is " + itos(MAX_INSTANCE_UNIFORM_INDICES));
  7031. return ERR_PARSE_ERROR;
  7032. }
  7033. }
  7034. }
  7035. //reset scope for next uniform
  7036. if (tk.type == TK_OP_ASSIGN) {
  7037. if (uniform2.array_size > 0) {
  7038. _set_error("Setting default value to a uniform array is not yet supported!");
  7039. return ERR_PARSE_ERROR;
  7040. }
  7041. Node *expr = _parse_and_reduce_expression(nullptr, FunctionInfo());
  7042. if (!expr) {
  7043. return ERR_PARSE_ERROR;
  7044. }
  7045. if (expr->type != Node::TYPE_CONSTANT) {
  7046. _set_error("Expected constant expression after '='");
  7047. return ERR_PARSE_ERROR;
  7048. }
  7049. ConstantNode *cn = static_cast<ConstantNode *>(expr);
  7050. uniform2.default_value.resize(cn->values.size());
  7051. if (!convert_constant(cn, uniform2.type, uniform2.default_value.ptrw())) {
  7052. _set_error("Can't convert constant to " + get_datatype_name(uniform2.type));
  7053. return ERR_PARSE_ERROR;
  7054. }
  7055. tk = _get_token();
  7056. }
  7057. shader->uniforms[name] = uniform2;
  7058. #ifdef DEBUG_ENABLED
  7059. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_UNIFORM_FLAG)) {
  7060. used_uniforms.insert(name, Usage(tk_line));
  7061. }
  7062. #endif // DEBUG_ENABLED
  7063. //reset scope for next uniform
  7064. uniform_scope = ShaderNode::Uniform::SCOPE_LOCAL;
  7065. if (tk.type != TK_SEMICOLON) {
  7066. _set_error("Expected ';'");
  7067. return ERR_PARSE_ERROR;
  7068. }
  7069. } else { // varying
  7070. ShaderNode::Varying varying;
  7071. varying.type = type;
  7072. varying.precision = precision;
  7073. varying.interpolation = interpolation;
  7074. varying.tkpos = prev_pos;
  7075. varying.array_size = array_size;
  7076. tk = _get_token();
  7077. if (tk.type != TK_SEMICOLON && tk.type != TK_BRACKET_OPEN) {
  7078. if (array_size == 0) {
  7079. _set_error("Expected ';' or '['");
  7080. } else {
  7081. _set_error("Expected ';'");
  7082. }
  7083. return ERR_PARSE_ERROR;
  7084. }
  7085. if (tk.type == TK_BRACKET_OPEN) {
  7086. if (array_size > 0) {
  7087. _set_error("Array size is already defined!");
  7088. return ERR_PARSE_ERROR;
  7089. }
  7090. tk = _get_token();
  7091. if (tk.type == TK_INT_CONSTANT && tk.constant > 0) {
  7092. varying.array_size = (int)tk.constant;
  7093. tk = _get_token();
  7094. if (tk.type == TK_BRACKET_CLOSE) {
  7095. tk = _get_token();
  7096. if (tk.type != TK_SEMICOLON) {
  7097. _set_error("Expected ';'");
  7098. return ERR_PARSE_ERROR;
  7099. }
  7100. } else {
  7101. _set_error("Expected ']'");
  7102. return ERR_PARSE_ERROR;
  7103. }
  7104. } else {
  7105. _set_error("Expected integer constant > 0");
  7106. return ERR_PARSE_ERROR;
  7107. }
  7108. }
  7109. shader->varyings[name] = varying;
  7110. #ifdef DEBUG_ENABLED
  7111. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_VARYING_FLAG)) {
  7112. used_varyings.insert(name, Usage(tk_line));
  7113. }
  7114. #endif // DEBUG_ENABLED
  7115. }
  7116. } break;
  7117. default: {
  7118. //function or constant variable
  7119. bool is_constant = false;
  7120. bool is_struct = false;
  7121. StringName struct_name;
  7122. DataPrecision precision = PRECISION_DEFAULT;
  7123. DataType type;
  7124. StringName name;
  7125. int return_array_size = 0;
  7126. if (tk.type == TK_CONST) {
  7127. is_constant = true;
  7128. tk = _get_token();
  7129. }
  7130. if (is_token_precision(tk.type)) {
  7131. precision = get_token_precision(tk.type);
  7132. tk = _get_token();
  7133. }
  7134. if (shader->structs.has(tk.text)) {
  7135. if (precision != PRECISION_DEFAULT) {
  7136. _set_error("Precision modifier cannot be used on structs.");
  7137. return ERR_PARSE_ERROR;
  7138. }
  7139. is_struct = true;
  7140. struct_name = tk.text;
  7141. } else {
  7142. if (!is_token_datatype(tk.type)) {
  7143. _set_error("Expected constant, function, uniform or varying");
  7144. return ERR_PARSE_ERROR;
  7145. }
  7146. if (!is_token_variable_datatype(tk.type)) {
  7147. _set_error("Invalid data type for constants or function return (samplers not allowed)");
  7148. return ERR_PARSE_ERROR;
  7149. }
  7150. }
  7151. if (is_struct) {
  7152. type = TYPE_STRUCT;
  7153. } else {
  7154. type = get_token_datatype(tk.type);
  7155. }
  7156. prev_pos = _get_tkpos();
  7157. tk = _get_token();
  7158. if (tk.type == TK_BRACKET_OPEN) {
  7159. bool error = false;
  7160. tk = _get_token();
  7161. if (tk.type == TK_INT_CONSTANT) {
  7162. return_array_size = (int)tk.constant;
  7163. if (return_array_size > 0) {
  7164. tk = _get_token();
  7165. if (tk.type != TK_BRACKET_CLOSE) {
  7166. _set_error("Expected ']'");
  7167. return ERR_PARSE_ERROR;
  7168. }
  7169. } else {
  7170. error = true;
  7171. }
  7172. } else {
  7173. error = true;
  7174. }
  7175. if (error) {
  7176. _set_error("Expected integer constant > 0");
  7177. return ERR_PARSE_ERROR;
  7178. }
  7179. prev_pos = _get_tkpos();
  7180. }
  7181. _set_tkpos(prev_pos);
  7182. _get_completable_identifier(nullptr, COMPLETION_MAIN_FUNCTION, name);
  7183. if (name == StringName()) {
  7184. _set_error("Expected function name after datatype");
  7185. return ERR_PARSE_ERROR;
  7186. }
  7187. if (_find_identifier(nullptr, false, FunctionInfo(), name)) {
  7188. _set_error("Redefinition of '" + String(name) + "'");
  7189. return ERR_PARSE_ERROR;
  7190. }
  7191. if (has_builtin(p_functions, name)) {
  7192. _set_error("Redefinition of '" + String(name) + "'");
  7193. return ERR_PARSE_ERROR;
  7194. }
  7195. tk = _get_token();
  7196. if (tk.type != TK_PARENTHESIS_OPEN) {
  7197. if (type == TYPE_VOID) {
  7198. _set_error("Expected '(' after function identifier");
  7199. return ERR_PARSE_ERROR;
  7200. }
  7201. //variable
  7202. while (true) {
  7203. ShaderNode::Constant constant;
  7204. constant.name = name;
  7205. constant.type = is_struct ? TYPE_STRUCT : type;
  7206. constant.type_str = struct_name;
  7207. constant.precision = precision;
  7208. constant.initializer = nullptr;
  7209. constant.array_size = 0;
  7210. bool unknown_size = false;
  7211. if (tk.type == TK_BRACKET_OPEN) {
  7212. if (RenderingServer::get_singleton()->is_low_end()) {
  7213. _set_error("Global const arrays are supported only on high-end platform!");
  7214. return ERR_PARSE_ERROR;
  7215. }
  7216. tk = _get_token();
  7217. if (tk.type == TK_BRACKET_CLOSE) {
  7218. unknown_size = true;
  7219. tk = _get_token();
  7220. } else if (tk.type == TK_INT_CONSTANT && ((int)tk.constant) > 0) {
  7221. constant.array_size = (int)tk.constant;
  7222. tk = _get_token();
  7223. if (tk.type != TK_BRACKET_CLOSE) {
  7224. _set_error("Expected ']'");
  7225. return ERR_PARSE_ERROR;
  7226. }
  7227. tk = _get_token();
  7228. } else {
  7229. _set_error("Expected integer constant > 0 or ']'");
  7230. return ERR_PARSE_ERROR;
  7231. }
  7232. }
  7233. if (tk.type == TK_OP_ASSIGN) {
  7234. if (!is_constant) {
  7235. _set_error("Expected 'const' keyword before constant definition");
  7236. return ERR_PARSE_ERROR;
  7237. }
  7238. if (constant.array_size > 0 || unknown_size) {
  7239. bool full_def = false;
  7240. ArrayDeclarationNode::Declaration decl;
  7241. decl.name = name;
  7242. decl.size = constant.array_size;
  7243. tk = _get_token();
  7244. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  7245. if (unknown_size) {
  7246. _set_error("Expected '{'");
  7247. return ERR_PARSE_ERROR;
  7248. }
  7249. full_def = true;
  7250. DataPrecision precision2 = PRECISION_DEFAULT;
  7251. if (is_token_precision(tk.type)) {
  7252. precision2 = get_token_precision(tk.type);
  7253. tk = _get_token();
  7254. if (!is_token_nonvoid_datatype(tk.type)) {
  7255. _set_error("Expected datatype after precision");
  7256. return ERR_PARSE_ERROR;
  7257. }
  7258. }
  7259. StringName struct_name2;
  7260. DataType type2;
  7261. if (shader->structs.has(tk.text)) {
  7262. type2 = TYPE_STRUCT;
  7263. struct_name2 = tk.text;
  7264. } else {
  7265. if (!is_token_variable_datatype(tk.type)) {
  7266. _set_error("Invalid data type for array");
  7267. return ERR_PARSE_ERROR;
  7268. }
  7269. type2 = get_token_datatype(tk.type);
  7270. }
  7271. int array_size2 = 0;
  7272. tk = _get_token();
  7273. if (tk.type == TK_BRACKET_OPEN) {
  7274. prev_pos = _get_tkpos();
  7275. tk = _get_token();
  7276. if (tk.type == TK_BRACKET_CLOSE) {
  7277. array_size2 = constant.array_size;
  7278. tk = _get_token();
  7279. } else {
  7280. _set_tkpos(prev_pos);
  7281. Node *n = _parse_and_reduce_expression(nullptr, FunctionInfo());
  7282. if (!n || n->type != Node::TYPE_CONSTANT || n->get_datatype() != TYPE_INT) {
  7283. _set_error("Expected single integer constant > 0");
  7284. return ERR_PARSE_ERROR;
  7285. }
  7286. ConstantNode *cnode = (ConstantNode *)n;
  7287. if (cnode->values.size() == 1) {
  7288. array_size2 = cnode->values[0].sint;
  7289. if (array_size2 <= 0) {
  7290. _set_error("Expected single integer constant > 0");
  7291. return ERR_PARSE_ERROR;
  7292. }
  7293. } else {
  7294. _set_error("Expected single integer constant > 0");
  7295. return ERR_PARSE_ERROR;
  7296. }
  7297. tk = _get_token();
  7298. if (tk.type != TK_BRACKET_CLOSE) {
  7299. _set_error("Expected ']");
  7300. return ERR_PARSE_ERROR;
  7301. } else {
  7302. tk = _get_token();
  7303. }
  7304. }
  7305. } else {
  7306. _set_error("Expected '[");
  7307. return ERR_PARSE_ERROR;
  7308. }
  7309. if (constant.precision != precision2 || constant.type != type2 || struct_name != struct_name2 || constant.array_size != array_size2) {
  7310. String error_str = "Cannot convert from '";
  7311. if (type2 == TYPE_STRUCT) {
  7312. error_str += struct_name2;
  7313. } else {
  7314. if (precision2 != PRECISION_DEFAULT) {
  7315. error_str += get_precision_name(precision2);
  7316. error_str += " ";
  7317. }
  7318. error_str += get_datatype_name(type2);
  7319. }
  7320. error_str += "[";
  7321. error_str += itos(array_size2);
  7322. error_str += "]'";
  7323. error_str += " to '";
  7324. if (type == TYPE_STRUCT) {
  7325. error_str += struct_name;
  7326. } else {
  7327. if (precision != PRECISION_DEFAULT) {
  7328. error_str += get_precision_name(precision);
  7329. error_str += " ";
  7330. }
  7331. error_str += get_datatype_name(type);
  7332. }
  7333. error_str += "[";
  7334. error_str += itos(constant.array_size);
  7335. error_str += "]'";
  7336. _set_error(error_str);
  7337. return ERR_PARSE_ERROR;
  7338. }
  7339. }
  7340. bool curly = tk.type == TK_CURLY_BRACKET_OPEN;
  7341. if (unknown_size) {
  7342. if (!curly) {
  7343. _set_error("Expected '{'");
  7344. return ERR_PARSE_ERROR;
  7345. }
  7346. } else {
  7347. if (full_def) {
  7348. if (curly) {
  7349. _set_error("Expected '('");
  7350. return ERR_PARSE_ERROR;
  7351. }
  7352. }
  7353. }
  7354. if (tk.type == TK_PARENTHESIS_OPEN || curly) { // initialization
  7355. while (true) {
  7356. Node *n = _parse_and_reduce_expression(nullptr, FunctionInfo());
  7357. if (!n) {
  7358. return ERR_PARSE_ERROR;
  7359. }
  7360. if (n->type == Node::TYPE_OPERATOR && ((OperatorNode *)n)->op == OP_CALL) {
  7361. _set_error("Expected constant expression");
  7362. return ERR_PARSE_ERROR;
  7363. }
  7364. if (!_compare_datatypes(constant.type, struct_name, 0, n->get_datatype(), n->get_datatype_name(), 0)) {
  7365. return ERR_PARSE_ERROR;
  7366. }
  7367. tk = _get_token();
  7368. if (tk.type == TK_COMMA) {
  7369. decl.initializer.push_back(n);
  7370. continue;
  7371. } else if (!curly && tk.type == TK_PARENTHESIS_CLOSE) {
  7372. decl.initializer.push_back(n);
  7373. break;
  7374. } else if (curly && tk.type == TK_CURLY_BRACKET_CLOSE) {
  7375. decl.initializer.push_back(n);
  7376. break;
  7377. } else {
  7378. if (curly) {
  7379. _set_error("Expected '}' or ','");
  7380. } else {
  7381. _set_error("Expected ')' or ','");
  7382. }
  7383. return ERR_PARSE_ERROR;
  7384. }
  7385. }
  7386. if (unknown_size) {
  7387. decl.size = decl.initializer.size();
  7388. constant.array_size = decl.initializer.size();
  7389. } else if (decl.initializer.size() != constant.array_size) {
  7390. _set_error("Array size mismatch");
  7391. return ERR_PARSE_ERROR;
  7392. }
  7393. }
  7394. ConstantNode *expr = memnew(ConstantNode);
  7395. expr->datatype = constant.type;
  7396. expr->struct_name = constant.type_str;
  7397. expr->array_size = constant.array_size;
  7398. expr->array_declarations.push_back(decl);
  7399. constant.initializer = static_cast<ConstantNode *>(expr);
  7400. } else {
  7401. //variable created with assignment! must parse an expression
  7402. Node *expr = _parse_and_reduce_expression(nullptr, FunctionInfo());
  7403. if (!expr) {
  7404. return ERR_PARSE_ERROR;
  7405. }
  7406. if (expr->type == Node::TYPE_OPERATOR && ((OperatorNode *)expr)->op == OP_CALL) {
  7407. OperatorNode *op = ((OperatorNode *)expr);
  7408. for (int i = 1; i < op->arguments.size(); i++) {
  7409. if (!_check_node_constness(op->arguments[i])) {
  7410. _set_error("Expected constant expression for argument '" + itos(i - 1) + "' of function call after '='");
  7411. return ERR_PARSE_ERROR;
  7412. }
  7413. }
  7414. }
  7415. constant.initializer = static_cast<ConstantNode *>(expr);
  7416. if (!_compare_datatypes(type, struct_name, 0, expr->get_datatype(), expr->get_datatype_name(), 0)) {
  7417. return ERR_PARSE_ERROR;
  7418. }
  7419. }
  7420. tk = _get_token();
  7421. } else {
  7422. if (constant.array_size > 0 || unknown_size) {
  7423. _set_error("Expected array initialization");
  7424. return ERR_PARSE_ERROR;
  7425. } else {
  7426. _set_error("Expected initialization of constant");
  7427. return ERR_PARSE_ERROR;
  7428. }
  7429. }
  7430. shader->constants[name] = constant;
  7431. shader->vconstants.push_back(constant);
  7432. #ifdef DEBUG_ENABLED
  7433. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_CONSTANT_FLAG)) {
  7434. used_constants.insert(name, Usage(tk_line));
  7435. }
  7436. #endif // DEBUG_ENABLED
  7437. if (tk.type == TK_COMMA) {
  7438. tk = _get_token();
  7439. if (tk.type != TK_IDENTIFIER) {
  7440. _set_error("Expected identifier after type");
  7441. return ERR_PARSE_ERROR;
  7442. }
  7443. name = tk.text;
  7444. if (_find_identifier(nullptr, false, FunctionInfo(), name)) {
  7445. _set_error("Redefinition of '" + String(name) + "'");
  7446. return ERR_PARSE_ERROR;
  7447. }
  7448. if (has_builtin(p_functions, name)) {
  7449. _set_error("Redefinition of '" + String(name) + "'");
  7450. return ERR_PARSE_ERROR;
  7451. }
  7452. tk = _get_token();
  7453. } else if (tk.type == TK_SEMICOLON) {
  7454. break;
  7455. } else {
  7456. _set_error("Expected ',' or ';' after constant");
  7457. return ERR_PARSE_ERROR;
  7458. }
  7459. }
  7460. break;
  7461. }
  7462. FunctionInfo builtins;
  7463. if (p_functions.has(name)) {
  7464. builtins = p_functions[name];
  7465. }
  7466. if (p_functions.has("global")) { // Adds global variables: 'TIME'
  7467. for (const KeyValue<StringName, BuiltInInfo> &E : p_functions["global"].built_ins) {
  7468. builtins.built_ins.insert(E.key, E.value);
  7469. }
  7470. }
  7471. ShaderNode::Function function;
  7472. function.callable = !p_functions.has(name);
  7473. function.name = name;
  7474. FunctionNode *func_node = alloc_node<FunctionNode>();
  7475. function.function = func_node;
  7476. shader->functions.push_back(function);
  7477. func_node->name = name;
  7478. func_node->return_type = type;
  7479. func_node->return_struct_name = struct_name;
  7480. func_node->return_precision = precision;
  7481. func_node->return_array_size = return_array_size;
  7482. if (p_functions.has(name)) {
  7483. func_node->can_discard = p_functions[name].can_discard;
  7484. } else {
  7485. #ifdef DEBUG_ENABLED
  7486. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_FUNCTION_FLAG)) {
  7487. used_functions.insert(name, Usage(tk_line));
  7488. }
  7489. #endif // DEBUG_ENABLED
  7490. }
  7491. func_node->body = alloc_node<BlockNode>();
  7492. func_node->body->parent_function = func_node;
  7493. tk = _get_token();
  7494. while (true) {
  7495. if (tk.type == TK_PARENTHESIS_CLOSE) {
  7496. break;
  7497. }
  7498. bool is_const = false;
  7499. if (tk.type == TK_CONST) {
  7500. is_const = true;
  7501. tk = _get_token();
  7502. }
  7503. ArgumentQualifier qualifier = ARGUMENT_QUALIFIER_IN;
  7504. if (tk.type == TK_ARG_IN) {
  7505. qualifier = ARGUMENT_QUALIFIER_IN;
  7506. tk = _get_token();
  7507. } else if (tk.type == TK_ARG_OUT) {
  7508. if (is_const) {
  7509. _set_error("'out' qualifier cannot be used within a function parameter declared with 'const'.");
  7510. return ERR_PARSE_ERROR;
  7511. }
  7512. qualifier = ARGUMENT_QUALIFIER_OUT;
  7513. tk = _get_token();
  7514. } else if (tk.type == TK_ARG_INOUT) {
  7515. if (is_const) {
  7516. _set_error("'inout' qualifier cannot be used within a function parameter declared with 'const'.");
  7517. return ERR_PARSE_ERROR;
  7518. }
  7519. qualifier = ARGUMENT_QUALIFIER_INOUT;
  7520. tk = _get_token();
  7521. }
  7522. DataType ptype;
  7523. StringName pname;
  7524. StringName param_struct_name;
  7525. DataPrecision pprecision = PRECISION_DEFAULT;
  7526. bool use_precision = false;
  7527. int array_size = 0;
  7528. if (is_token_precision(tk.type)) {
  7529. pprecision = get_token_precision(tk.type);
  7530. tk = _get_token();
  7531. use_precision = true;
  7532. }
  7533. is_struct = false;
  7534. if (shader->structs.has(tk.text)) {
  7535. is_struct = true;
  7536. param_struct_name = tk.text;
  7537. if (use_precision) {
  7538. _set_error("Precision modifier cannot be used on structs.");
  7539. return ERR_PARSE_ERROR;
  7540. }
  7541. }
  7542. if (!is_struct && !is_token_datatype(tk.type)) {
  7543. _set_error("Expected a valid datatype for argument");
  7544. return ERR_PARSE_ERROR;
  7545. }
  7546. if (qualifier == ARGUMENT_QUALIFIER_OUT || qualifier == ARGUMENT_QUALIFIER_INOUT) {
  7547. if (is_sampler_type(get_token_datatype(tk.type))) {
  7548. _set_error("Opaque types cannot be output parameters.");
  7549. return ERR_PARSE_ERROR;
  7550. }
  7551. }
  7552. if (is_struct) {
  7553. ptype = TYPE_STRUCT;
  7554. } else {
  7555. ptype = get_token_datatype(tk.type);
  7556. if (_validate_datatype(ptype) != OK) {
  7557. return ERR_PARSE_ERROR;
  7558. }
  7559. if (ptype == TYPE_VOID) {
  7560. _set_error("void not allowed in argument");
  7561. return ERR_PARSE_ERROR;
  7562. }
  7563. }
  7564. tk = _get_token();
  7565. if (tk.type == TK_BRACKET_OPEN) {
  7566. bool error = false;
  7567. tk = _get_token();
  7568. if (tk.type == TK_INT_CONSTANT) {
  7569. array_size = (int)tk.constant;
  7570. if (array_size > 0) {
  7571. tk = _get_token();
  7572. if (tk.type != TK_BRACKET_CLOSE) {
  7573. _set_error("Expected ']'");
  7574. return ERR_PARSE_ERROR;
  7575. }
  7576. } else {
  7577. error = true;
  7578. }
  7579. } else {
  7580. error = true;
  7581. }
  7582. if (error) {
  7583. _set_error("Expected integer constant > 0");
  7584. return ERR_PARSE_ERROR;
  7585. }
  7586. tk = _get_token();
  7587. }
  7588. if (tk.type != TK_IDENTIFIER) {
  7589. _set_error("Expected identifier for argument name");
  7590. return ERR_PARSE_ERROR;
  7591. }
  7592. pname = tk.text;
  7593. ShaderLanguage::IdentifierType itype;
  7594. if (_find_identifier(func_node->body, false, builtins, pname, (ShaderLanguage::DataType *)nullptr, &itype)) {
  7595. if (itype != IDENTIFIER_FUNCTION) {
  7596. _set_error("Redefinition of '" + String(pname) + "'");
  7597. return ERR_PARSE_ERROR;
  7598. }
  7599. }
  7600. if (has_builtin(p_functions, pname)) {
  7601. _set_error("Redefinition of '" + String(pname) + "'");
  7602. return ERR_PARSE_ERROR;
  7603. }
  7604. FunctionNode::Argument arg;
  7605. arg.type = ptype;
  7606. arg.name = pname;
  7607. arg.type_str = param_struct_name;
  7608. arg.precision = pprecision;
  7609. arg.qualifier = qualifier;
  7610. arg.tex_argument_check = false;
  7611. arg.tex_builtin_check = false;
  7612. arg.tex_argument_filter = FILTER_DEFAULT;
  7613. arg.tex_argument_repeat = REPEAT_DEFAULT;
  7614. arg.is_const = is_const;
  7615. tk = _get_token();
  7616. if (tk.type == TK_BRACKET_OPEN) {
  7617. if (array_size > 0) {
  7618. _set_error("Array size is already defined!");
  7619. return ERR_PARSE_ERROR;
  7620. }
  7621. bool error = false;
  7622. tk = _get_token();
  7623. if (tk.type == TK_INT_CONSTANT) {
  7624. array_size = (int)tk.constant;
  7625. if (array_size > 0) {
  7626. tk = _get_token();
  7627. if (tk.type != TK_BRACKET_CLOSE) {
  7628. _set_error("Expected ']'");
  7629. return ERR_PARSE_ERROR;
  7630. }
  7631. } else {
  7632. error = true;
  7633. }
  7634. } else {
  7635. error = true;
  7636. }
  7637. if (error) {
  7638. _set_error("Expected integer constant > 0");
  7639. return ERR_PARSE_ERROR;
  7640. }
  7641. tk = _get_token();
  7642. }
  7643. arg.array_size = array_size;
  7644. func_node->arguments.push_back(arg);
  7645. if (tk.type == TK_COMMA) {
  7646. tk = _get_token();
  7647. //do none and go on
  7648. } else if (tk.type != TK_PARENTHESIS_CLOSE) {
  7649. _set_error("Expected ',' or ')' after identifier");
  7650. return ERR_PARSE_ERROR;
  7651. }
  7652. }
  7653. if (p_functions.has(name)) {
  7654. //if one of the core functions, make sure they are of the correct form
  7655. if (func_node->arguments.size() > 0) {
  7656. _set_error("Function '" + String(name) + "' expects no arguments.");
  7657. return ERR_PARSE_ERROR;
  7658. }
  7659. if (func_node->return_type != TYPE_VOID) {
  7660. _set_error("Function '" + String(name) + "' must be of void return type.");
  7661. return ERR_PARSE_ERROR;
  7662. }
  7663. }
  7664. //all good let's parse inside the function!
  7665. tk = _get_token();
  7666. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  7667. _set_error("Expected '{' to begin function");
  7668. return ERR_PARSE_ERROR;
  7669. }
  7670. current_function = name;
  7671. Error err = _parse_block(func_node->body, builtins);
  7672. if (err) {
  7673. return err;
  7674. }
  7675. if (func_node->return_type != DataType::TYPE_VOID) {
  7676. BlockNode *block = func_node->body;
  7677. if (_find_last_flow_op_in_block(block, FlowOperation::FLOW_OP_RETURN) != OK) {
  7678. _set_error("Expected at least one return statement in a non-void function.");
  7679. return ERR_PARSE_ERROR;
  7680. }
  7681. }
  7682. current_function = StringName();
  7683. }
  7684. }
  7685. tk = _get_token();
  7686. }
  7687. int error_line;
  7688. String error_message;
  7689. if (!_check_varying_usages(&error_line, &error_message)) {
  7690. _set_tkpos({ 0, error_line });
  7691. _set_error(error_message);
  7692. return ERR_PARSE_ERROR;
  7693. }
  7694. return OK;
  7695. }
  7696. bool ShaderLanguage::has_builtin(const Map<StringName, ShaderLanguage::FunctionInfo> &p_functions, const StringName &p_name) {
  7697. for (const KeyValue<StringName, ShaderLanguage::FunctionInfo> &E : p_functions) {
  7698. if (E.value.built_ins.has(p_name)) {
  7699. return true;
  7700. }
  7701. }
  7702. return false;
  7703. }
  7704. Error ShaderLanguage::_find_last_flow_op_in_op(ControlFlowNode *p_flow, FlowOperation p_op) {
  7705. bool found = false;
  7706. for (int i = p_flow->blocks.size() - 1; i >= 0; i--) {
  7707. if (p_flow->blocks[i]->type == Node::TYPE_BLOCK) {
  7708. BlockNode *last_block = (BlockNode *)p_flow->blocks[i];
  7709. if (_find_last_flow_op_in_block(last_block, p_op) == OK) {
  7710. found = true;
  7711. break;
  7712. }
  7713. }
  7714. }
  7715. if (found) {
  7716. return OK;
  7717. }
  7718. return FAILED;
  7719. }
  7720. Error ShaderLanguage::_find_last_flow_op_in_block(BlockNode *p_block, FlowOperation p_op) {
  7721. bool found = false;
  7722. for (int i = p_block->statements.size() - 1; i >= 0; i--) {
  7723. if (p_block->statements[i]->type == Node::TYPE_CONTROL_FLOW) {
  7724. ControlFlowNode *flow = (ControlFlowNode *)p_block->statements[i];
  7725. if (flow->flow_op == p_op) {
  7726. found = true;
  7727. break;
  7728. } else {
  7729. if (_find_last_flow_op_in_op(flow, p_op) == OK) {
  7730. found = true;
  7731. break;
  7732. }
  7733. }
  7734. } else if (p_block->statements[i]->type == Node::TYPE_BLOCK) {
  7735. BlockNode *block = (BlockNode *)p_block->statements[i];
  7736. if (_find_last_flow_op_in_block(block, p_op) == OK) {
  7737. found = true;
  7738. break;
  7739. }
  7740. }
  7741. }
  7742. if (found) {
  7743. return OK;
  7744. }
  7745. return FAILED;
  7746. }
  7747. // skips over whitespace and /* */ and // comments
  7748. static int _get_first_ident_pos(const String &p_code) {
  7749. int idx = 0;
  7750. #define GETCHAR(m_idx) (((idx + m_idx) < p_code.length()) ? p_code[idx + m_idx] : char32_t(0))
  7751. while (true) {
  7752. if (GETCHAR(0) == '/' && GETCHAR(1) == '/') {
  7753. idx += 2;
  7754. while (true) {
  7755. if (GETCHAR(0) == 0) {
  7756. return 0;
  7757. }
  7758. if (GETCHAR(0) == '\n') {
  7759. idx++;
  7760. break; // loop
  7761. }
  7762. idx++;
  7763. }
  7764. } else if (GETCHAR(0) == '/' && GETCHAR(1) == '*') {
  7765. idx += 2;
  7766. while (true) {
  7767. if (GETCHAR(0) == 0) {
  7768. return 0;
  7769. }
  7770. if (GETCHAR(0) == '*' && GETCHAR(1) == '/') {
  7771. idx += 2;
  7772. break; // loop
  7773. }
  7774. idx++;
  7775. }
  7776. } else {
  7777. switch (GETCHAR(0)) {
  7778. case ' ':
  7779. case '\t':
  7780. case '\r':
  7781. case '\n': {
  7782. idx++;
  7783. } break; // switch
  7784. default:
  7785. return idx;
  7786. }
  7787. }
  7788. }
  7789. #undef GETCHAR
  7790. }
  7791. String ShaderLanguage::get_shader_type(const String &p_code) {
  7792. bool reading_type = false;
  7793. String cur_identifier;
  7794. for (int i = _get_first_ident_pos(p_code); i < p_code.length(); i++) {
  7795. if (p_code[i] == ';') {
  7796. break;
  7797. } else if (p_code[i] <= 32) {
  7798. if (cur_identifier != String()) {
  7799. if (!reading_type) {
  7800. if (cur_identifier != "shader_type") {
  7801. return String();
  7802. }
  7803. reading_type = true;
  7804. cur_identifier = String();
  7805. } else {
  7806. return cur_identifier;
  7807. }
  7808. }
  7809. } else {
  7810. cur_identifier += String::chr(p_code[i]);
  7811. }
  7812. }
  7813. if (reading_type) {
  7814. return cur_identifier;
  7815. }
  7816. return String();
  7817. }
  7818. #ifdef DEBUG_ENABLED
  7819. void ShaderLanguage::_check_warning_accums() {
  7820. for (const KeyValue<ShaderWarning::Code, Map<StringName, Map<StringName, Usage>> *> &E : warnings_check_map2) {
  7821. for (Map<StringName, Map<StringName, Usage>>::Element *T = (*E.value).front(); T; T = T->next()) {
  7822. for (const KeyValue<StringName, Usage> &U : T->get()) {
  7823. if (!U.value.used) {
  7824. _add_warning(E.key, U.value.decl_line, U.key);
  7825. }
  7826. }
  7827. }
  7828. }
  7829. for (const KeyValue<ShaderWarning::Code, Map<StringName, Usage> *> &E : warnings_check_map) {
  7830. for (const Map<StringName, Usage>::Element *U = (*E.value).front(); U; U = U->next()) {
  7831. if (!U->get().used) {
  7832. _add_warning(E.key, U->get().decl_line, U->key());
  7833. }
  7834. }
  7835. }
  7836. }
  7837. List<ShaderWarning>::Element *ShaderLanguage::get_warnings_ptr() {
  7838. return warnings.front();
  7839. }
  7840. void ShaderLanguage::enable_warning_checking(bool p_enabled) {
  7841. check_warnings = p_enabled;
  7842. }
  7843. bool ShaderLanguage::is_warning_checking_enabled() const {
  7844. return check_warnings;
  7845. }
  7846. void ShaderLanguage::set_warning_flags(uint32_t p_flags) {
  7847. warning_flags = p_flags;
  7848. }
  7849. uint32_t ShaderLanguage::get_warning_flags() const {
  7850. return warning_flags;
  7851. }
  7852. #endif // DEBUG_ENABLED
  7853. Error ShaderLanguage::compile(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const VaryingFunctionNames &p_varying_function_names, const Set<String> &p_shader_types, GlobalVariableGetTypeFunc p_global_variable_type_func) {
  7854. clear();
  7855. code = p_code;
  7856. global_var_get_type_func = p_global_variable_type_func;
  7857. varying_function_names = p_varying_function_names;
  7858. nodes = nullptr;
  7859. shader = alloc_node<ShaderNode>();
  7860. Error err = _parse_shader(p_functions, p_render_modes, p_shader_types);
  7861. #ifdef DEBUG_ENABLED
  7862. if (check_warnings) {
  7863. _check_warning_accums();
  7864. }
  7865. #endif // DEBUG_ENABLED
  7866. if (err != OK) {
  7867. return err;
  7868. }
  7869. return OK;
  7870. }
  7871. Error ShaderLanguage::complete(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const VaryingFunctionNames &p_varying_function_names, const Set<String> &p_shader_types, GlobalVariableGetTypeFunc p_global_variable_type_func, List<ScriptCodeCompletionOption> *r_options, String &r_call_hint) {
  7872. clear();
  7873. code = p_code;
  7874. varying_function_names = p_varying_function_names;
  7875. nodes = nullptr;
  7876. global_var_get_type_func = p_global_variable_type_func;
  7877. shader = alloc_node<ShaderNode>();
  7878. _parse_shader(p_functions, p_render_modes, p_shader_types);
  7879. switch (completion_type) {
  7880. case COMPLETION_NONE: {
  7881. //do nothing
  7882. return OK;
  7883. } break;
  7884. case COMPLETION_RENDER_MODE: {
  7885. for (int i = 0; i < p_render_modes.size(); i++) {
  7886. ScriptCodeCompletionOption option(p_render_modes[i], ScriptCodeCompletionOption::KIND_ENUM);
  7887. r_options->push_back(option);
  7888. }
  7889. return OK;
  7890. } break;
  7891. case COMPLETION_STRUCT: {
  7892. if (shader->structs.has(completion_struct)) {
  7893. StructNode *node = shader->structs[completion_struct].shader_struct;
  7894. for (int i = 0; i < node->members.size(); i++) {
  7895. ScriptCodeCompletionOption option(node->members[i]->name, ScriptCodeCompletionOption::KIND_MEMBER);
  7896. r_options->push_back(option);
  7897. }
  7898. }
  7899. return OK;
  7900. } break;
  7901. case COMPLETION_MAIN_FUNCTION: {
  7902. for (const KeyValue<StringName, FunctionInfo> &E : p_functions) {
  7903. ScriptCodeCompletionOption option(E.key, ScriptCodeCompletionOption::KIND_FUNCTION);
  7904. r_options->push_back(option);
  7905. }
  7906. return OK;
  7907. } break;
  7908. case COMPLETION_IDENTIFIER:
  7909. case COMPLETION_FUNCTION_CALL: {
  7910. bool comp_ident = completion_type == COMPLETION_IDENTIFIER;
  7911. Map<String, ScriptCodeCompletionOption::Kind> matches;
  7912. StringName skip_function;
  7913. BlockNode *block = completion_block;
  7914. if (completion_class == TAG_GLOBAL) {
  7915. while (block) {
  7916. if (comp_ident) {
  7917. for (const KeyValue<StringName, BlockNode::Variable> &E : block->variables) {
  7918. if (E.value.line < completion_line) {
  7919. matches.insert(E.key, ScriptCodeCompletionOption::KIND_VARIABLE);
  7920. }
  7921. }
  7922. }
  7923. if (block->parent_function) {
  7924. if (comp_ident) {
  7925. for (int i = 0; i < block->parent_function->arguments.size(); i++) {
  7926. matches.insert(block->parent_function->arguments[i].name, ScriptCodeCompletionOption::KIND_VARIABLE);
  7927. }
  7928. }
  7929. skip_function = block->parent_function->name;
  7930. }
  7931. block = block->parent_block;
  7932. }
  7933. if (comp_ident) {
  7934. if (p_functions.has("global")) {
  7935. for (const KeyValue<StringName, BuiltInInfo> &E : p_functions["global"].built_ins) {
  7936. ScriptCodeCompletionOption::Kind kind = ScriptCodeCompletionOption::KIND_MEMBER;
  7937. if (E.value.constant) {
  7938. kind = ScriptCodeCompletionOption::KIND_CONSTANT;
  7939. }
  7940. matches.insert(E.key, kind);
  7941. }
  7942. }
  7943. if (skip_function != StringName() && p_functions.has(skip_function)) {
  7944. for (const KeyValue<StringName, BuiltInInfo> &E : p_functions[skip_function].built_ins) {
  7945. ScriptCodeCompletionOption::Kind kind = ScriptCodeCompletionOption::KIND_MEMBER;
  7946. if (E.value.constant) {
  7947. kind = ScriptCodeCompletionOption::KIND_CONSTANT;
  7948. }
  7949. matches.insert(E.key, kind);
  7950. }
  7951. }
  7952. for (const KeyValue<StringName, ShaderNode::Varying> &E : shader->varyings) {
  7953. matches.insert(E.key, ScriptCodeCompletionOption::KIND_VARIABLE);
  7954. }
  7955. for (const KeyValue<StringName, ShaderNode::Uniform> &E : shader->uniforms) {
  7956. matches.insert(E.key, ScriptCodeCompletionOption::KIND_MEMBER);
  7957. }
  7958. }
  7959. for (int i = 0; i < shader->functions.size(); i++) {
  7960. if (!shader->functions[i].callable || shader->functions[i].name == skip_function) {
  7961. continue;
  7962. }
  7963. matches.insert(String(shader->functions[i].name), ScriptCodeCompletionOption::KIND_FUNCTION);
  7964. }
  7965. int idx = 0;
  7966. bool low_end = RenderingServer::get_singleton()->is_low_end();
  7967. if (stages && stages->has(skip_function)) {
  7968. for (const KeyValue<StringName, StageFunctionInfo> &E : (*stages)[skip_function].stage_functions) {
  7969. matches.insert(String(E.key), ScriptCodeCompletionOption::KIND_FUNCTION);
  7970. }
  7971. }
  7972. while (builtin_func_defs[idx].name) {
  7973. if (low_end && builtin_func_defs[idx].high_end) {
  7974. idx++;
  7975. continue;
  7976. }
  7977. matches.insert(String(builtin_func_defs[idx].name), ScriptCodeCompletionOption::KIND_FUNCTION);
  7978. idx++;
  7979. }
  7980. } else { // sub-class
  7981. int idx = 0;
  7982. bool low_end = RenderingServer::get_singleton()->is_low_end();
  7983. while (builtin_func_defs[idx].name) {
  7984. if (low_end && builtin_func_defs[idx].high_end) {
  7985. idx++;
  7986. continue;
  7987. }
  7988. if (builtin_func_defs[idx].tag == completion_class) {
  7989. matches.insert(String(builtin_func_defs[idx].name), ScriptCodeCompletionOption::KIND_FUNCTION);
  7990. }
  7991. idx++;
  7992. }
  7993. }
  7994. for (const KeyValue<String, ScriptCodeCompletionOption::Kind> &E : matches) {
  7995. ScriptCodeCompletionOption option(E.key, E.value);
  7996. if (E.value == ScriptCodeCompletionOption::KIND_FUNCTION) {
  7997. option.insert_text += "(";
  7998. }
  7999. r_options->push_back(option);
  8000. }
  8001. return OK;
  8002. } break;
  8003. case COMPLETION_CALL_ARGUMENTS: {
  8004. StringName block_function;
  8005. BlockNode *block = completion_block;
  8006. while (block) {
  8007. if (block->parent_function) {
  8008. block_function = block->parent_function->name;
  8009. }
  8010. block = block->parent_block;
  8011. }
  8012. for (int i = 0; i < shader->functions.size(); i++) {
  8013. if (!shader->functions[i].callable) {
  8014. continue;
  8015. }
  8016. if (shader->functions[i].name == completion_function) {
  8017. String calltip;
  8018. calltip += get_datatype_name(shader->functions[i].function->return_type);
  8019. if (shader->functions[i].function->return_array_size > 0) {
  8020. calltip += "[";
  8021. calltip += itos(shader->functions[i].function->return_array_size);
  8022. calltip += "]";
  8023. }
  8024. calltip += " ";
  8025. calltip += shader->functions[i].name;
  8026. calltip += "(";
  8027. for (int j = 0; j < shader->functions[i].function->arguments.size(); j++) {
  8028. if (j > 0) {
  8029. calltip += ", ";
  8030. } else {
  8031. calltip += " ";
  8032. }
  8033. if (j == completion_argument) {
  8034. calltip += char32_t(0xFFFF);
  8035. }
  8036. if (shader->functions[i].function->arguments[j].is_const) {
  8037. calltip += "const ";
  8038. }
  8039. if (shader->functions[i].function->arguments[j].qualifier != ArgumentQualifier::ARGUMENT_QUALIFIER_IN) {
  8040. if (shader->functions[i].function->arguments[j].qualifier == ArgumentQualifier::ARGUMENT_QUALIFIER_OUT) {
  8041. calltip += "out ";
  8042. } else { // ArgumentQualifier::ARGUMENT_QUALIFIER_INOUT
  8043. calltip += "inout ";
  8044. }
  8045. }
  8046. calltip += get_datatype_name(shader->functions[i].function->arguments[j].type);
  8047. calltip += " ";
  8048. calltip += shader->functions[i].function->arguments[j].name;
  8049. if (shader->functions[i].function->arguments[j].array_size > 0) {
  8050. calltip += "[";
  8051. calltip += itos(shader->functions[i].function->arguments[j].array_size);
  8052. calltip += "]";
  8053. }
  8054. if (j == completion_argument) {
  8055. calltip += char32_t(0xFFFF);
  8056. }
  8057. }
  8058. if (shader->functions[i].function->arguments.size()) {
  8059. calltip += " ";
  8060. }
  8061. calltip += ")";
  8062. r_call_hint = calltip;
  8063. return OK;
  8064. }
  8065. }
  8066. int idx = 0;
  8067. String calltip;
  8068. bool low_end = RenderingServer::get_singleton()->is_low_end();
  8069. if (stages && stages->has(block_function)) {
  8070. for (const KeyValue<StringName, StageFunctionInfo> &E : (*stages)[block_function].stage_functions) {
  8071. if (completion_function == E.key) {
  8072. calltip += get_datatype_name(E.value.return_type);
  8073. calltip += " ";
  8074. calltip += E.key;
  8075. calltip += "(";
  8076. for (int i = 0; i < E.value.arguments.size(); i++) {
  8077. if (i > 0) {
  8078. calltip += ", ";
  8079. } else {
  8080. calltip += " ";
  8081. }
  8082. if (i == completion_argument) {
  8083. calltip += char32_t(0xFFFF);
  8084. }
  8085. calltip += get_datatype_name(E.value.arguments[i].type);
  8086. calltip += " ";
  8087. calltip += E.value.arguments[i].name;
  8088. if (i == completion_argument) {
  8089. calltip += char32_t(0xFFFF);
  8090. }
  8091. }
  8092. if (E.value.arguments.size()) {
  8093. calltip += " ";
  8094. }
  8095. calltip += ")";
  8096. r_call_hint = calltip;
  8097. return OK;
  8098. }
  8099. }
  8100. }
  8101. while (builtin_func_defs[idx].name) {
  8102. if (low_end && builtin_func_defs[idx].high_end) {
  8103. idx++;
  8104. continue;
  8105. }
  8106. int idx2 = 0;
  8107. Set<int> out_args;
  8108. while (builtin_func_out_args[idx2].name != nullptr) {
  8109. if (builtin_func_out_args[idx2].name == builtin_func_defs[idx].name) {
  8110. for (int i = 0; i < BuiltinFuncOutArgs::MAX_ARGS; i++) {
  8111. int arg = builtin_func_out_args[idx2].arguments[i];
  8112. if (arg == -1) {
  8113. break;
  8114. }
  8115. out_args.insert(arg);
  8116. }
  8117. break;
  8118. }
  8119. idx2++;
  8120. }
  8121. if (completion_function == builtin_func_defs[idx].name) {
  8122. if (builtin_func_defs[idx].tag != completion_class) {
  8123. idx++;
  8124. continue;
  8125. }
  8126. if (calltip.length()) {
  8127. calltip += "\n";
  8128. }
  8129. calltip += get_datatype_name(builtin_func_defs[idx].rettype);
  8130. calltip += " ";
  8131. calltip += builtin_func_defs[idx].name;
  8132. calltip += "(";
  8133. bool found_arg = false;
  8134. for (int i = 0; i < BuiltinFuncDef::MAX_ARGS - 1; i++) {
  8135. if (builtin_func_defs[idx].args[i] == TYPE_VOID) {
  8136. break;
  8137. }
  8138. if (i > 0) {
  8139. calltip += ", ";
  8140. } else {
  8141. calltip += " ";
  8142. }
  8143. if (i == completion_argument) {
  8144. calltip += char32_t(0xFFFF);
  8145. }
  8146. if (out_args.has(i)) {
  8147. calltip += "out ";
  8148. }
  8149. calltip += get_datatype_name(builtin_func_defs[idx].args[i]);
  8150. String arg_name = (String)builtin_func_defs[idx].args_names[i];
  8151. if (!arg_name.is_empty()) {
  8152. calltip += " ";
  8153. calltip += arg_name;
  8154. }
  8155. if (i == completion_argument) {
  8156. calltip += char32_t(0xFFFF);
  8157. }
  8158. found_arg = true;
  8159. }
  8160. if (found_arg) {
  8161. calltip += " ";
  8162. }
  8163. calltip += ")";
  8164. }
  8165. idx++;
  8166. }
  8167. r_call_hint = calltip;
  8168. return OK;
  8169. } break;
  8170. case COMPLETION_INDEX: {
  8171. const char colv[4] = { 'r', 'g', 'b', 'a' };
  8172. const char coordv[4] = { 'x', 'y', 'z', 'w' };
  8173. const char coordt[4] = { 's', 't', 'p', 'q' };
  8174. int limit = 0;
  8175. switch (completion_base) {
  8176. case TYPE_BVEC2:
  8177. case TYPE_IVEC2:
  8178. case TYPE_UVEC2:
  8179. case TYPE_VEC2: {
  8180. limit = 2;
  8181. } break;
  8182. case TYPE_BVEC3:
  8183. case TYPE_IVEC3:
  8184. case TYPE_UVEC3:
  8185. case TYPE_VEC3: {
  8186. limit = 3;
  8187. } break;
  8188. case TYPE_BVEC4:
  8189. case TYPE_IVEC4:
  8190. case TYPE_UVEC4:
  8191. case TYPE_VEC4: {
  8192. limit = 4;
  8193. } break;
  8194. case TYPE_MAT2:
  8195. limit = 2;
  8196. break;
  8197. case TYPE_MAT3:
  8198. limit = 3;
  8199. break;
  8200. case TYPE_MAT4:
  8201. limit = 4;
  8202. break;
  8203. default: {
  8204. }
  8205. }
  8206. for (int i = 0; i < limit; i++) {
  8207. r_options->push_back(ScriptCodeCompletionOption(String::chr(colv[i]), ScriptCodeCompletionOption::KIND_PLAIN_TEXT));
  8208. r_options->push_back(ScriptCodeCompletionOption(String::chr(coordv[i]), ScriptCodeCompletionOption::KIND_PLAIN_TEXT));
  8209. r_options->push_back(ScriptCodeCompletionOption(String::chr(coordt[i]), ScriptCodeCompletionOption::KIND_PLAIN_TEXT));
  8210. }
  8211. } break;
  8212. }
  8213. return ERR_PARSE_ERROR;
  8214. }
  8215. String ShaderLanguage::get_error_text() {
  8216. return error_str;
  8217. }
  8218. int ShaderLanguage::get_error_line() {
  8219. return error_line;
  8220. }
  8221. ShaderLanguage::ShaderNode *ShaderLanguage::get_shader() {
  8222. return shader;
  8223. }
  8224. ShaderLanguage::ShaderLanguage() {
  8225. nodes = nullptr;
  8226. completion_class = TAG_GLOBAL;
  8227. #if DEBUG_ENABLED
  8228. warnings_check_map.insert(ShaderWarning::UNUSED_CONSTANT, &used_constants);
  8229. warnings_check_map.insert(ShaderWarning::UNUSED_FUNCTION, &used_functions);
  8230. warnings_check_map.insert(ShaderWarning::UNUSED_STRUCT, &used_structs);
  8231. warnings_check_map.insert(ShaderWarning::UNUSED_UNIFORM, &used_uniforms);
  8232. warnings_check_map.insert(ShaderWarning::UNUSED_VARYING, &used_varyings);
  8233. warnings_check_map2.insert(ShaderWarning::UNUSED_LOCAL_VARIABLE, &used_local_vars);
  8234. #endif // DEBUG_ENABLED
  8235. }
  8236. ShaderLanguage::~ShaderLanguage() {
  8237. clear();
  8238. }