shader_language.cpp 224 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299
  1. /**************************************************************************/
  2. /* shader_language.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "shader_language.h"
  31. #include "core/os/os.h"
  32. #include "core/print_string.h"
  33. #include "servers/visual_server.h"
  34. static bool _is_text_char(CharType c) {
  35. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
  36. }
  37. static bool _is_number(CharType c) {
  38. return (c >= '0' && c <= '9');
  39. }
  40. static bool _is_hex(CharType c) {
  41. return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
  42. }
  43. String ShaderLanguage::get_operator_text(Operator p_op) {
  44. static const char *op_names[OP_MAX] = { "==",
  45. "!=",
  46. "<",
  47. "<=",
  48. ">",
  49. ">=",
  50. "&&",
  51. "||",
  52. "!",
  53. "-",
  54. "+",
  55. "-",
  56. "*",
  57. "/",
  58. "%",
  59. "<<",
  60. ">>",
  61. "=",
  62. "+=",
  63. "-=",
  64. "*=",
  65. "/=",
  66. "%=",
  67. "<<=",
  68. ">>=",
  69. "&=",
  70. "|=",
  71. "^=",
  72. "&",
  73. "|",
  74. "^",
  75. "~",
  76. "++",
  77. "--",
  78. "?",
  79. ":",
  80. "++",
  81. "--",
  82. "()",
  83. "construct",
  84. "index" };
  85. return op_names[p_op];
  86. }
  87. const char *ShaderLanguage::token_names[TK_MAX] = {
  88. "EMPTY",
  89. "IDENTIFIER",
  90. "TRUE",
  91. "FALSE",
  92. "REAL_CONSTANT",
  93. "INT_CONSTANT",
  94. "TYPE_VOID",
  95. "TYPE_BOOL",
  96. "TYPE_BVEC2",
  97. "TYPE_BVEC3",
  98. "TYPE_BVEC4",
  99. "TYPE_INT",
  100. "TYPE_IVEC2",
  101. "TYPE_IVEC3",
  102. "TYPE_IVEC4",
  103. "TYPE_UINT",
  104. "TYPE_UVEC2",
  105. "TYPE_UVEC3",
  106. "TYPE_UVEC4",
  107. "TYPE_FLOAT",
  108. "TYPE_VEC2",
  109. "TYPE_VEC3",
  110. "TYPE_VEC4",
  111. "TYPE_MAT2",
  112. "TYPE_MAT3",
  113. "TYPE_MAT4",
  114. "TYPE_SAMPLER2D",
  115. "TYPE_ISAMPLER2D",
  116. "TYPE_USAMPLER2D",
  117. "TYPE_SAMPLER2DARRAY",
  118. "TYPE_ISAMPLER2DARRAY",
  119. "TYPE_USAMPLER2DARRAY",
  120. "TYPE_SAMPLER3D",
  121. "TYPE_ISAMPLER3D",
  122. "TYPE_USAMPLER3D",
  123. "TYPE_SAMPLERCUBE",
  124. "TYPE_SAMPLEREXT",
  125. "INTERPOLATION_FLAT",
  126. "INTERPOLATION_SMOOTH",
  127. "CONST",
  128. "PRECISION_LOW",
  129. "PRECISION_MID",
  130. "PRECISION_HIGH",
  131. "OP_EQUAL",
  132. "OP_NOT_EQUAL",
  133. "OP_LESS",
  134. "OP_LESS_EQUAL",
  135. "OP_GREATER",
  136. "OP_GREATER_EQUAL",
  137. "OP_AND",
  138. "OP_OR",
  139. "OP_NOT",
  140. "OP_ADD",
  141. "OP_SUB",
  142. "OP_MUL",
  143. "OP_DIV",
  144. "OP_MOD",
  145. "OP_SHIFT_LEFT",
  146. "OP_SHIFT_RIGHT",
  147. "OP_ASSIGN",
  148. "OP_ASSIGN_ADD",
  149. "OP_ASSIGN_SUB",
  150. "OP_ASSIGN_MUL",
  151. "OP_ASSIGN_DIV",
  152. "OP_ASSIGN_MOD",
  153. "OP_ASSIGN_SHIFT_LEFT",
  154. "OP_ASSIGN_SHIFT_RIGHT",
  155. "OP_ASSIGN_BIT_AND",
  156. "OP_ASSIGN_BIT_OR",
  157. "OP_ASSIGN_BIT_XOR",
  158. "OP_BIT_AND",
  159. "OP_BIT_OR",
  160. "OP_BIT_XOR",
  161. "OP_BIT_INVERT",
  162. "OP_INCREMENT",
  163. "OP_DECREMENT",
  164. "CF_IF",
  165. "CF_ELSE",
  166. "CF_FOR",
  167. "CF_WHILE",
  168. "CF_DO",
  169. "CF_SWITCH",
  170. "CF_CASE",
  171. "CF_BREAK",
  172. "CF_CONTINUE",
  173. "CF_RETURN",
  174. "CF_DISCARD",
  175. "BRACKET_OPEN",
  176. "BRACKET_CLOSE",
  177. "CURLY_BRACKET_OPEN",
  178. "CURLY_BRACKET_CLOSE",
  179. "PARENTHESIS_OPEN",
  180. "PARENTHESIS_CLOSE",
  181. "QUESTION",
  182. "COMMA",
  183. "COLON",
  184. "SEMICOLON",
  185. "PERIOD",
  186. "UNIFORM",
  187. "VARYING",
  188. "IN",
  189. "OUT",
  190. "INOUT",
  191. "RENDER_MODE",
  192. "HINT_WHITE_TEXTURE",
  193. "HINT_BLACK_TEXTURE",
  194. "HINT_TRANSPARENT_TEXTURE",
  195. "HINT_NORMAL_TEXTURE",
  196. "HINT_ANISO_TEXTURE",
  197. "HINT_ALBEDO_TEXTURE",
  198. "HINT_BLACK_ALBEDO_TEXTURE",
  199. "HINT_COLOR",
  200. "HINT_RANGE",
  201. "SHADER_TYPE",
  202. "CURSOR",
  203. "ERROR",
  204. "EOF",
  205. };
  206. String ShaderLanguage::get_token_text(Token p_token) {
  207. String name = token_names[p_token.type];
  208. if (p_token.is_integer_constant() || p_token.type == TK_REAL_CONSTANT) {
  209. name += "(" + rtos(p_token.constant) + ")";
  210. } else if (p_token.type == TK_IDENTIFIER) {
  211. name += "(" + String(p_token.text) + ")";
  212. } else if (p_token.type == TK_ERROR) {
  213. name += "(" + String(p_token.text) + ")";
  214. }
  215. return name;
  216. }
  217. ShaderLanguage::Token ShaderLanguage::_make_token(TokenType p_type, const StringName &p_text) {
  218. Token tk;
  219. tk.type = p_type;
  220. tk.text = p_text;
  221. tk.line = tk_line;
  222. if (tk.type == TK_ERROR) {
  223. _set_error(p_text);
  224. }
  225. return tk;
  226. }
  227. const ShaderLanguage::KeyWord ShaderLanguage::keyword_list[] = {
  228. { TK_TRUE, "true" },
  229. { TK_FALSE, "false" },
  230. { TK_TYPE_VOID, "void" },
  231. { TK_TYPE_BOOL, "bool" },
  232. { TK_TYPE_BVEC2, "bvec2" },
  233. { TK_TYPE_BVEC3, "bvec3" },
  234. { TK_TYPE_BVEC4, "bvec4" },
  235. { TK_TYPE_INT, "int" },
  236. { TK_TYPE_IVEC2, "ivec2" },
  237. { TK_TYPE_IVEC3, "ivec3" },
  238. { TK_TYPE_IVEC4, "ivec4" },
  239. { TK_TYPE_UINT, "uint" },
  240. { TK_TYPE_UVEC2, "uvec2" },
  241. { TK_TYPE_UVEC3, "uvec3" },
  242. { TK_TYPE_UVEC4, "uvec4" },
  243. { TK_TYPE_FLOAT, "float" },
  244. { TK_TYPE_VEC2, "vec2" },
  245. { TK_TYPE_VEC3, "vec3" },
  246. { TK_TYPE_VEC4, "vec4" },
  247. { TK_TYPE_MAT2, "mat2" },
  248. { TK_TYPE_MAT3, "mat3" },
  249. { TK_TYPE_MAT4, "mat4" },
  250. { TK_TYPE_SAMPLER2D, "sampler2D" },
  251. { TK_TYPE_ISAMPLER2D, "isampler2D" },
  252. { TK_TYPE_USAMPLER2D, "usampler2D" },
  253. { TK_TYPE_SAMPLER2DARRAY, "sampler2DArray" },
  254. { TK_TYPE_ISAMPLER2DARRAY, "isampler2DArray" },
  255. { TK_TYPE_USAMPLER2DARRAY, "usampler2DArray" },
  256. { TK_TYPE_SAMPLER3D, "sampler3D" },
  257. { TK_TYPE_ISAMPLER3D, "isampler3D" },
  258. { TK_TYPE_USAMPLER3D, "usampler3D" },
  259. { TK_TYPE_SAMPLERCUBE, "samplerCube" },
  260. { TK_TYPE_SAMPLEREXT, "samplerExternalOES" },
  261. { TK_INTERPOLATION_FLAT, "flat" },
  262. { TK_INTERPOLATION_SMOOTH, "smooth" },
  263. { TK_CONST, "const" },
  264. { TK_STRUCT, "struct" },
  265. { TK_PRECISION_LOW, "lowp" },
  266. { TK_PRECISION_MID, "mediump" },
  267. { TK_PRECISION_HIGH, "highp" },
  268. { TK_CF_IF, "if" },
  269. { TK_CF_ELSE, "else" },
  270. { TK_CF_FOR, "for" },
  271. { TK_CF_WHILE, "while" },
  272. { TK_CF_DO, "do" },
  273. { TK_CF_SWITCH, "switch" },
  274. { TK_CF_CASE, "case" },
  275. { TK_CF_DEFAULT, "default" },
  276. { TK_CF_BREAK, "break" },
  277. { TK_CF_CONTINUE, "continue" },
  278. { TK_CF_RETURN, "return" },
  279. { TK_CF_DISCARD, "discard" },
  280. { TK_UNIFORM, "uniform" },
  281. { TK_VARYING, "varying" },
  282. { TK_ARG_IN, "in" },
  283. { TK_ARG_OUT, "out" },
  284. { TK_ARG_INOUT, "inout" },
  285. { TK_RENDER_MODE, "render_mode" },
  286. { TK_HINT_WHITE_TEXTURE, "hint_white" },
  287. { TK_HINT_BLACK_TEXTURE, "hint_black" },
  288. { TK_HINT_TRANSPARENT_TEXTURE, "hint_transparent" },
  289. { TK_HINT_NORMAL_TEXTURE, "hint_normal" },
  290. { TK_HINT_ANISO_TEXTURE, "hint_aniso" },
  291. { TK_HINT_ALBEDO_TEXTURE, "hint_albedo" },
  292. { TK_HINT_BLACK_ALBEDO_TEXTURE, "hint_black_albedo" },
  293. { TK_HINT_COLOR, "hint_color" },
  294. { TK_HINT_RANGE, "hint_range" },
  295. { TK_SHADER_TYPE, "shader_type" },
  296. { TK_ERROR, nullptr }
  297. };
  298. ShaderLanguage::Token ShaderLanguage::_get_token() {
  299. #define GETCHAR(m_idx) (((char_idx + m_idx) < code.length()) ? code[char_idx + m_idx] : CharType(0))
  300. while (true) {
  301. char_idx++;
  302. switch (GETCHAR(-1)) {
  303. case 0:
  304. return _make_token(TK_EOF);
  305. case 0xFFFF:
  306. return _make_token(TK_CURSOR); //for completion
  307. case '\t':
  308. case '\r':
  309. case ' ':
  310. continue;
  311. case '\n':
  312. tk_line++;
  313. continue;
  314. case '/': {
  315. switch (GETCHAR(0)) {
  316. case '*': { // block comment
  317. char_idx++;
  318. while (true) {
  319. if (GETCHAR(0) == 0) {
  320. return _make_token(TK_EOF);
  321. }
  322. if (GETCHAR(0) == '*' && GETCHAR(1) == '/') {
  323. char_idx += 2;
  324. break;
  325. } else if (GETCHAR(0) == '\n') {
  326. tk_line++;
  327. }
  328. char_idx++;
  329. }
  330. } break;
  331. case '/': { // line comment skip
  332. while (true) {
  333. if (GETCHAR(0) == '\n') {
  334. tk_line++;
  335. char_idx++;
  336. break;
  337. }
  338. if (GETCHAR(0) == 0) {
  339. return _make_token(TK_EOF);
  340. }
  341. char_idx++;
  342. }
  343. } break;
  344. case '=': { // diveq
  345. char_idx++;
  346. return _make_token(TK_OP_ASSIGN_DIV);
  347. } break;
  348. default:
  349. return _make_token(TK_OP_DIV);
  350. }
  351. continue; //a comment, continue to next token
  352. } break;
  353. case '=': {
  354. if (GETCHAR(0) == '=') {
  355. char_idx++;
  356. return _make_token(TK_OP_EQUAL);
  357. }
  358. return _make_token(TK_OP_ASSIGN);
  359. } break;
  360. case '<': {
  361. if (GETCHAR(0) == '=') {
  362. char_idx++;
  363. return _make_token(TK_OP_LESS_EQUAL);
  364. } else if (GETCHAR(0) == '<') {
  365. char_idx++;
  366. if (GETCHAR(0) == '=') {
  367. char_idx++;
  368. return _make_token(TK_OP_ASSIGN_SHIFT_LEFT);
  369. }
  370. return _make_token(TK_OP_SHIFT_LEFT);
  371. }
  372. return _make_token(TK_OP_LESS);
  373. } break;
  374. case '>': {
  375. if (GETCHAR(0) == '=') {
  376. char_idx++;
  377. return _make_token(TK_OP_GREATER_EQUAL);
  378. } else if (GETCHAR(0) == '>') {
  379. char_idx++;
  380. if (GETCHAR(0) == '=') {
  381. char_idx++;
  382. return _make_token(TK_OP_ASSIGN_SHIFT_RIGHT);
  383. }
  384. return _make_token(TK_OP_SHIFT_RIGHT);
  385. }
  386. return _make_token(TK_OP_GREATER);
  387. } break;
  388. case '!': {
  389. if (GETCHAR(0) == '=') {
  390. char_idx++;
  391. return _make_token(TK_OP_NOT_EQUAL);
  392. }
  393. return _make_token(TK_OP_NOT);
  394. } break;
  395. //case '"' //string - no strings in shader
  396. //case '\'' //string - no strings in shader
  397. case '{':
  398. return _make_token(TK_CURLY_BRACKET_OPEN);
  399. case '}':
  400. return _make_token(TK_CURLY_BRACKET_CLOSE);
  401. case '[':
  402. return _make_token(TK_BRACKET_OPEN);
  403. case ']':
  404. return _make_token(TK_BRACKET_CLOSE);
  405. case '(':
  406. return _make_token(TK_PARENTHESIS_OPEN);
  407. case ')':
  408. return _make_token(TK_PARENTHESIS_CLOSE);
  409. case ',':
  410. return _make_token(TK_COMMA);
  411. case ';':
  412. return _make_token(TK_SEMICOLON);
  413. case '?':
  414. return _make_token(TK_QUESTION);
  415. case ':':
  416. return _make_token(TK_COLON);
  417. case '^':
  418. if (GETCHAR(0) == '=') {
  419. char_idx++;
  420. return _make_token(TK_OP_ASSIGN_BIT_XOR);
  421. }
  422. return _make_token(TK_OP_BIT_XOR);
  423. case '~':
  424. return _make_token(TK_OP_BIT_INVERT);
  425. case '&': {
  426. if (GETCHAR(0) == '=') {
  427. char_idx++;
  428. return _make_token(TK_OP_ASSIGN_BIT_AND);
  429. } else if (GETCHAR(0) == '&') {
  430. char_idx++;
  431. return _make_token(TK_OP_AND);
  432. }
  433. return _make_token(TK_OP_BIT_AND);
  434. } break;
  435. case '|': {
  436. if (GETCHAR(0) == '=') {
  437. char_idx++;
  438. return _make_token(TK_OP_ASSIGN_BIT_OR);
  439. } else if (GETCHAR(0) == '|') {
  440. char_idx++;
  441. return _make_token(TK_OP_OR);
  442. }
  443. return _make_token(TK_OP_BIT_OR);
  444. } break;
  445. case '*': {
  446. if (GETCHAR(0) == '=') {
  447. char_idx++;
  448. return _make_token(TK_OP_ASSIGN_MUL);
  449. }
  450. return _make_token(TK_OP_MUL);
  451. } break;
  452. case '+': {
  453. if (GETCHAR(0) == '=') {
  454. char_idx++;
  455. return _make_token(TK_OP_ASSIGN_ADD);
  456. } else if (GETCHAR(0) == '+') {
  457. char_idx++;
  458. return _make_token(TK_OP_INCREMENT);
  459. }
  460. return _make_token(TK_OP_ADD);
  461. } break;
  462. case '-': {
  463. if (GETCHAR(0) == '=') {
  464. char_idx++;
  465. return _make_token(TK_OP_ASSIGN_SUB);
  466. } else if (GETCHAR(0) == '-') {
  467. char_idx++;
  468. return _make_token(TK_OP_DECREMENT);
  469. }
  470. return _make_token(TK_OP_SUB);
  471. } break;
  472. case '%': {
  473. if (GETCHAR(0) == '=') {
  474. char_idx++;
  475. return _make_token(TK_OP_ASSIGN_MOD);
  476. }
  477. return _make_token(TK_OP_MOD);
  478. } break;
  479. default: {
  480. char_idx--; //go back one, since we have no idea what this is
  481. if (_is_number(GETCHAR(0)) || (GETCHAR(0) == '.' && _is_number(GETCHAR(1)))) {
  482. // parse number
  483. bool hexa_found = false;
  484. bool period_found = false;
  485. bool exponent_found = false;
  486. bool float_suffix_found = false;
  487. bool uint_suffix_found = false;
  488. bool end_suffix_found = false;
  489. enum {
  490. CASE_ALL,
  491. CASE_HEXA_PERIOD,
  492. CASE_EXPONENT,
  493. CASE_SIGN_AFTER_EXPONENT,
  494. CASE_NONE,
  495. CASE_MAX,
  496. } lut_case = CASE_ALL;
  497. static bool suffix_lut[CASE_MAX][127];
  498. if (!is_const_suffix_lut_initialized) {
  499. is_const_suffix_lut_initialized = true;
  500. for (int i = 0; i < 127; i++) {
  501. char t = char(i);
  502. suffix_lut[CASE_ALL][i] = t == '.' || t == 'x' || t == 'e' || t == 'f' || t == 'u' || t == '-' || t == '+';
  503. suffix_lut[CASE_HEXA_PERIOD][i] = t == 'e' || t == 'f';
  504. suffix_lut[CASE_EXPONENT][i] = t == 'f' || t == '-' || t == '+';
  505. suffix_lut[CASE_SIGN_AFTER_EXPONENT][i] = t == 'f';
  506. suffix_lut[CASE_NONE][i] = false;
  507. }
  508. }
  509. String str;
  510. int i = 0;
  511. while (true) {
  512. const CharType symbol = String::char_lowercase(GETCHAR(i));
  513. bool error = false;
  514. if (_is_number(symbol)) {
  515. if (end_suffix_found) {
  516. error = true;
  517. }
  518. } else {
  519. if (symbol < 0x7F && suffix_lut[lut_case][symbol]) {
  520. if (symbol == 'x') {
  521. hexa_found = true;
  522. lut_case = CASE_HEXA_PERIOD;
  523. } else if (symbol == '.') {
  524. period_found = true;
  525. lut_case = CASE_HEXA_PERIOD;
  526. } else if (symbol == 'e' && !hexa_found) {
  527. exponent_found = true;
  528. lut_case = CASE_EXPONENT;
  529. } else if (symbol == 'f' && !hexa_found) {
  530. if (!period_found && !exponent_found) {
  531. error = true;
  532. }
  533. float_suffix_found = true;
  534. end_suffix_found = true;
  535. lut_case = CASE_NONE;
  536. } else if (symbol == 'u') {
  537. uint_suffix_found = true;
  538. end_suffix_found = true;
  539. lut_case = CASE_NONE;
  540. } else if (symbol == '-' || symbol == '+') {
  541. if (exponent_found) {
  542. lut_case = CASE_SIGN_AFTER_EXPONENT;
  543. } else {
  544. break;
  545. }
  546. }
  547. } else if (!hexa_found || !_is_hex(symbol)) {
  548. if (_is_text_char(symbol)) {
  549. error = true;
  550. } else {
  551. break;
  552. }
  553. }
  554. }
  555. if (error) {
  556. if (hexa_found) {
  557. return _make_token(TK_ERROR, "Invalid (hexadecimal) numeric constant");
  558. }
  559. if (period_found || exponent_found || float_suffix_found) {
  560. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  561. }
  562. if (uint_suffix_found) {
  563. return _make_token(TK_ERROR, "Invalid (unsigned integer) numeric constant");
  564. }
  565. return _make_token(TK_ERROR, "Invalid (integer) numeric constant");
  566. }
  567. str += symbol;
  568. i++;
  569. }
  570. CharType last_char = str[str.length() - 1];
  571. if (hexa_found) {
  572. //integer(hex)
  573. if (str.size() > 11 || !str.is_valid_hex_number(true)) { // > 0xFFFFFFFF
  574. return _make_token(TK_ERROR, "Invalid (hexadecimal) numeric constant");
  575. }
  576. } else if (period_found || exponent_found || float_suffix_found) {
  577. //floats
  578. if (exponent_found && (!_is_number(last_char) && last_char != 'f')) { // checks for eg: "2E", "2E-", "2E+"
  579. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  580. }
  581. if (period_found) {
  582. if (float_suffix_found) {
  583. //checks for eg "1.f" or "1.99f" notations
  584. if (last_char != 'f') {
  585. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  586. }
  587. } else {
  588. //checks for eg. "1." or "1.99" notations
  589. if (last_char != '.' && !_is_number(last_char)) {
  590. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  591. }
  592. }
  593. } else if (float_suffix_found) {
  594. // if no period found the float suffix must be the last character, like in "2f" for "2.0"
  595. if (last_char != 'f') {
  596. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  597. }
  598. }
  599. if (float_suffix_found) {
  600. //strip the suffix
  601. str = str.left(str.length() - 1);
  602. //compensate reading cursor position
  603. char_idx += 1;
  604. }
  605. if (!str.is_valid_float()) {
  606. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  607. }
  608. } else {
  609. //integers
  610. if (uint_suffix_found) {
  611. // Strip the suffix.
  612. str = str.left(str.length() - 1);
  613. // Compensate reading cursor position.
  614. char_idx += 1;
  615. }
  616. if (!str.is_valid_integer()) {
  617. if (uint_suffix_found) {
  618. return _make_token(TK_ERROR, "Invalid (unsigned integer) numeric constant");
  619. } else {
  620. return _make_token(TK_ERROR, "Invalid (integer) numeric constant");
  621. }
  622. }
  623. }
  624. char_idx += str.length();
  625. Token tk;
  626. if (period_found || exponent_found || float_suffix_found) {
  627. tk.type = TK_REAL_CONSTANT;
  628. } else if (uint_suffix_found) {
  629. tk.type = TK_UINT_CONSTANT;
  630. } else {
  631. tk.type = TK_INT_CONSTANT;
  632. }
  633. if (hexa_found) {
  634. tk.constant = (double)str.hex_to_int64(true);
  635. } else {
  636. tk.constant = str.to_double();
  637. }
  638. tk.line = tk_line;
  639. return tk;
  640. }
  641. if (GETCHAR(0) == '.') {
  642. //parse period
  643. char_idx++;
  644. return _make_token(TK_PERIOD);
  645. }
  646. if (_is_text_char(GETCHAR(0))) {
  647. // parse identifier
  648. String str;
  649. while (_is_text_char(GETCHAR(0))) {
  650. str += CharType(GETCHAR(0));
  651. char_idx++;
  652. }
  653. //see if keyword
  654. //should be converted to a static map
  655. int idx = 0;
  656. while (keyword_list[idx].text) {
  657. if (str == keyword_list[idx].text) {
  658. return _make_token(keyword_list[idx].token);
  659. }
  660. idx++;
  661. }
  662. str = str.replace("dus_", "_");
  663. return _make_token(TK_IDENTIFIER, str);
  664. }
  665. if (GETCHAR(0) > 32) {
  666. return _make_token(TK_ERROR, "Tokenizer: Unknown character #" + itos(GETCHAR(0)) + ": '" + String::chr(GETCHAR(0)) + "'");
  667. } else {
  668. return _make_token(TK_ERROR, "Tokenizer: Unknown character #" + itos(GETCHAR(0)));
  669. }
  670. } break;
  671. }
  672. }
  673. ERR_PRINT("BUG");
  674. return Token();
  675. #undef GETCHAR
  676. }
  677. String ShaderLanguage::token_debug(const String &p_code) {
  678. clear();
  679. code = p_code;
  680. String output;
  681. Token tk = _get_token();
  682. while (tk.type != TK_EOF && tk.type != TK_ERROR) {
  683. output += itos(tk_line) + ": " + get_token_text(tk) + "\n";
  684. tk = _get_token();
  685. }
  686. return output;
  687. }
  688. bool ShaderLanguage::is_token_variable_datatype(TokenType p_type) {
  689. return (
  690. p_type == TK_TYPE_VOID ||
  691. p_type == TK_TYPE_BOOL ||
  692. p_type == TK_TYPE_BVEC2 ||
  693. p_type == TK_TYPE_BVEC3 ||
  694. p_type == TK_TYPE_BVEC4 ||
  695. p_type == TK_TYPE_INT ||
  696. p_type == TK_TYPE_IVEC2 ||
  697. p_type == TK_TYPE_IVEC3 ||
  698. p_type == TK_TYPE_IVEC4 ||
  699. p_type == TK_TYPE_UINT ||
  700. p_type == TK_TYPE_UVEC2 ||
  701. p_type == TK_TYPE_UVEC3 ||
  702. p_type == TK_TYPE_UVEC4 ||
  703. p_type == TK_TYPE_FLOAT ||
  704. p_type == TK_TYPE_VEC2 ||
  705. p_type == TK_TYPE_VEC3 ||
  706. p_type == TK_TYPE_VEC4 ||
  707. p_type == TK_TYPE_MAT2 ||
  708. p_type == TK_TYPE_MAT3 ||
  709. p_type == TK_TYPE_MAT4);
  710. }
  711. bool ShaderLanguage::is_token_datatype(TokenType p_type) {
  712. return (
  713. p_type == TK_TYPE_VOID ||
  714. p_type == TK_TYPE_BOOL ||
  715. p_type == TK_TYPE_BVEC2 ||
  716. p_type == TK_TYPE_BVEC3 ||
  717. p_type == TK_TYPE_BVEC4 ||
  718. p_type == TK_TYPE_INT ||
  719. p_type == TK_TYPE_IVEC2 ||
  720. p_type == TK_TYPE_IVEC3 ||
  721. p_type == TK_TYPE_IVEC4 ||
  722. p_type == TK_TYPE_UINT ||
  723. p_type == TK_TYPE_UVEC2 ||
  724. p_type == TK_TYPE_UVEC3 ||
  725. p_type == TK_TYPE_UVEC4 ||
  726. p_type == TK_TYPE_FLOAT ||
  727. p_type == TK_TYPE_VEC2 ||
  728. p_type == TK_TYPE_VEC3 ||
  729. p_type == TK_TYPE_VEC4 ||
  730. p_type == TK_TYPE_MAT2 ||
  731. p_type == TK_TYPE_MAT3 ||
  732. p_type == TK_TYPE_MAT4 ||
  733. p_type == TK_TYPE_SAMPLER2D ||
  734. p_type == TK_TYPE_ISAMPLER2D ||
  735. p_type == TK_TYPE_USAMPLER2D ||
  736. p_type == TK_TYPE_SAMPLER2DARRAY ||
  737. p_type == TK_TYPE_ISAMPLER2DARRAY ||
  738. p_type == TK_TYPE_USAMPLER2DARRAY ||
  739. p_type == TK_TYPE_SAMPLER3D ||
  740. p_type == TK_TYPE_ISAMPLER3D ||
  741. p_type == TK_TYPE_USAMPLER3D ||
  742. p_type == TK_TYPE_SAMPLERCUBE ||
  743. p_type == TK_TYPE_SAMPLEREXT);
  744. }
  745. ShaderLanguage::DataType ShaderLanguage::get_token_datatype(TokenType p_type) {
  746. return DataType(p_type - TK_TYPE_VOID);
  747. }
  748. bool ShaderLanguage::is_token_interpolation(TokenType p_type) {
  749. return (
  750. p_type == TK_INTERPOLATION_FLAT ||
  751. p_type == TK_INTERPOLATION_SMOOTH);
  752. }
  753. ShaderLanguage::DataInterpolation ShaderLanguage::get_token_interpolation(TokenType p_type) {
  754. if (p_type == TK_INTERPOLATION_FLAT) {
  755. return INTERPOLATION_FLAT;
  756. } else {
  757. return INTERPOLATION_SMOOTH;
  758. }
  759. }
  760. bool ShaderLanguage::is_token_precision(TokenType p_type) {
  761. return (
  762. p_type == TK_PRECISION_LOW ||
  763. p_type == TK_PRECISION_MID ||
  764. p_type == TK_PRECISION_HIGH);
  765. }
  766. ShaderLanguage::DataPrecision ShaderLanguage::get_token_precision(TokenType p_type) {
  767. if (p_type == TK_PRECISION_LOW) {
  768. return PRECISION_LOWP;
  769. } else if (p_type == TK_PRECISION_HIGH) {
  770. return PRECISION_HIGHP;
  771. } else {
  772. return PRECISION_MEDIUMP;
  773. }
  774. }
  775. String ShaderLanguage::get_precision_name(DataPrecision p_type) {
  776. switch (p_type) {
  777. case PRECISION_LOWP:
  778. return "lowp";
  779. case PRECISION_MEDIUMP:
  780. return "mediump";
  781. case PRECISION_HIGHP:
  782. return "highp";
  783. default:
  784. break;
  785. }
  786. return "";
  787. }
  788. String ShaderLanguage::get_datatype_name(DataType p_type) {
  789. switch (p_type) {
  790. case TYPE_VOID:
  791. return "void";
  792. case TYPE_BOOL:
  793. return "bool";
  794. case TYPE_BVEC2:
  795. return "bvec2";
  796. case TYPE_BVEC3:
  797. return "bvec3";
  798. case TYPE_BVEC4:
  799. return "bvec4";
  800. case TYPE_INT:
  801. return "int";
  802. case TYPE_IVEC2:
  803. return "ivec2";
  804. case TYPE_IVEC3:
  805. return "ivec3";
  806. case TYPE_IVEC4:
  807. return "ivec4";
  808. case TYPE_UINT:
  809. return "uint";
  810. case TYPE_UVEC2:
  811. return "uvec2";
  812. case TYPE_UVEC3:
  813. return "uvec3";
  814. case TYPE_UVEC4:
  815. return "uvec4";
  816. case TYPE_FLOAT:
  817. return "float";
  818. case TYPE_VEC2:
  819. return "vec2";
  820. case TYPE_VEC3:
  821. return "vec3";
  822. case TYPE_VEC4:
  823. return "vec4";
  824. case TYPE_MAT2:
  825. return "mat2";
  826. case TYPE_MAT3:
  827. return "mat3";
  828. case TYPE_MAT4:
  829. return "mat4";
  830. case TYPE_SAMPLER2D:
  831. return "sampler2D";
  832. case TYPE_ISAMPLER2D:
  833. return "isampler2D";
  834. case TYPE_USAMPLER2D:
  835. return "usampler2D";
  836. case TYPE_SAMPLER2DARRAY:
  837. return "sampler2DArray";
  838. case TYPE_ISAMPLER2DARRAY:
  839. return "isampler2DArray";
  840. case TYPE_USAMPLER2DARRAY:
  841. return "usampler2DArray";
  842. case TYPE_SAMPLER3D:
  843. return "sampler3D";
  844. case TYPE_ISAMPLER3D:
  845. return "isampler3D";
  846. case TYPE_USAMPLER3D:
  847. return "usampler3D";
  848. case TYPE_SAMPLERCUBE:
  849. return "samplerCube";
  850. case TYPE_SAMPLEREXT:
  851. return "samplerExternalOES";
  852. case TYPE_STRUCT:
  853. return "struct";
  854. default: {
  855. }
  856. }
  857. return "";
  858. }
  859. bool ShaderLanguage::is_token_nonvoid_datatype(TokenType p_type) {
  860. return is_token_datatype(p_type) && p_type != TK_TYPE_VOID;
  861. }
  862. void ShaderLanguage::clear() {
  863. current_function = StringName();
  864. completion_type = COMPLETION_NONE;
  865. completion_block = nullptr;
  866. completion_function = StringName();
  867. completion_class = TAG_GLOBAL;
  868. completion_struct = StringName();
  869. completion_base = TYPE_VOID;
  870. error_line = 0;
  871. tk_line = 1;
  872. char_idx = 0;
  873. error_set = false;
  874. error_str = "";
  875. last_const = false;
  876. while (nodes) {
  877. Node *n = nodes;
  878. nodes = nodes->next;
  879. memdelete(n);
  880. }
  881. }
  882. bool ShaderLanguage::_find_identifier(const BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, const StringName &p_identifier, DataType *r_data_type, IdentifierType *r_type, bool *r_is_const, int *r_array_size, StringName *r_struct_name, ConstantNode::Value *r_constant_value) {
  883. if (p_builtin_types.has(p_identifier)) {
  884. if (r_data_type) {
  885. *r_data_type = p_builtin_types[p_identifier].type;
  886. }
  887. if (r_is_const) {
  888. *r_is_const = p_builtin_types[p_identifier].constant;
  889. }
  890. if (r_type) {
  891. *r_type = IDENTIFIER_BUILTIN_VAR;
  892. }
  893. return true;
  894. }
  895. FunctionNode *function = nullptr;
  896. while (p_block) {
  897. if (p_block->variables.has(p_identifier)) {
  898. if (r_data_type) {
  899. *r_data_type = p_block->variables[p_identifier].type;
  900. }
  901. if (r_is_const) {
  902. *r_is_const = p_block->variables[p_identifier].is_const;
  903. }
  904. if (r_array_size) {
  905. *r_array_size = p_block->variables[p_identifier].array_size;
  906. }
  907. if (r_type) {
  908. *r_type = IDENTIFIER_LOCAL_VAR;
  909. }
  910. if (r_struct_name) {
  911. *r_struct_name = p_block->variables[p_identifier].struct_name;
  912. }
  913. return true;
  914. }
  915. if (p_block->parent_function) {
  916. function = p_block->parent_function;
  917. break;
  918. } else {
  919. ERR_FAIL_COND_V(!p_block->parent_block, false);
  920. p_block = p_block->parent_block;
  921. }
  922. }
  923. if (function) {
  924. for (int i = 0; i < function->arguments.size(); i++) {
  925. if (function->arguments[i].name == p_identifier) {
  926. if (r_data_type) {
  927. *r_data_type = function->arguments[i].type;
  928. }
  929. if (r_type) {
  930. *r_type = IDENTIFIER_FUNCTION_ARGUMENT;
  931. }
  932. if (r_struct_name) {
  933. *r_struct_name = function->arguments[i].type_str;
  934. }
  935. if (r_is_const) {
  936. *r_is_const = function->arguments[i].is_const;
  937. }
  938. return true;
  939. }
  940. }
  941. }
  942. if (shader->varyings.has(p_identifier)) {
  943. if (r_data_type) {
  944. *r_data_type = shader->varyings[p_identifier].type;
  945. }
  946. if (r_array_size) {
  947. *r_array_size = shader->varyings[p_identifier].array_size;
  948. }
  949. if (r_type) {
  950. *r_type = IDENTIFIER_VARYING;
  951. }
  952. return true;
  953. }
  954. if (shader->uniforms.has(p_identifier)) {
  955. if (r_data_type) {
  956. *r_data_type = shader->uniforms[p_identifier].type;
  957. }
  958. if (r_type) {
  959. *r_type = IDENTIFIER_UNIFORM;
  960. }
  961. return true;
  962. }
  963. if (shader->constants.has(p_identifier)) {
  964. if (r_data_type) {
  965. *r_data_type = shader->constants[p_identifier].type;
  966. }
  967. if (r_array_size) {
  968. *r_array_size = shader->constants[p_identifier].array_size;
  969. }
  970. if (r_struct_name) {
  971. *r_struct_name = shader->constants[p_identifier].type_str;
  972. }
  973. if (r_constant_value) {
  974. if (shader->constants[p_identifier].initializer && shader->constants[p_identifier].initializer->values.size() == 1) {
  975. *r_constant_value = shader->constants[p_identifier].initializer->values[0];
  976. }
  977. }
  978. if (r_type) {
  979. *r_type = IDENTIFIER_CONSTANT;
  980. }
  981. return true;
  982. }
  983. for (int i = 0; i < shader->functions.size(); i++) {
  984. if (!shader->functions[i].callable) {
  985. continue;
  986. }
  987. if (shader->functions[i].name == p_identifier) {
  988. if (r_data_type) {
  989. *r_data_type = shader->functions[i].function->return_type;
  990. }
  991. if (r_type) {
  992. *r_type = IDENTIFIER_FUNCTION;
  993. }
  994. return true;
  995. }
  996. }
  997. return false;
  998. }
  999. bool ShaderLanguage::_validate_operator(OperatorNode *p_op, DataType *r_ret_type) {
  1000. bool valid = false;
  1001. DataType ret_type = TYPE_VOID;
  1002. switch (p_op->op) {
  1003. case OP_EQUAL:
  1004. case OP_NOT_EQUAL: {
  1005. DataType na = p_op->arguments[0]->get_datatype();
  1006. DataType nb = p_op->arguments[1]->get_datatype();
  1007. valid = na == nb;
  1008. ret_type = TYPE_BOOL;
  1009. } break;
  1010. case OP_LESS:
  1011. case OP_LESS_EQUAL:
  1012. case OP_GREATER:
  1013. case OP_GREATER_EQUAL: {
  1014. DataType na = p_op->arguments[0]->get_datatype();
  1015. DataType nb = p_op->arguments[1]->get_datatype();
  1016. valid = na == nb && (na == TYPE_UINT || na == TYPE_INT || na == TYPE_FLOAT);
  1017. ret_type = TYPE_BOOL;
  1018. } break;
  1019. case OP_AND:
  1020. case OP_OR: {
  1021. DataType na = p_op->arguments[0]->get_datatype();
  1022. DataType nb = p_op->arguments[1]->get_datatype();
  1023. valid = na == nb && na == TYPE_BOOL;
  1024. ret_type = TYPE_BOOL;
  1025. } break;
  1026. case OP_NOT: {
  1027. DataType na = p_op->arguments[0]->get_datatype();
  1028. valid = na == TYPE_BOOL;
  1029. ret_type = TYPE_BOOL;
  1030. } break;
  1031. case OP_INCREMENT:
  1032. case OP_DECREMENT:
  1033. case OP_POST_INCREMENT:
  1034. case OP_POST_DECREMENT:
  1035. case OP_NEGATE: {
  1036. DataType na = p_op->arguments[0]->get_datatype();
  1037. valid = na > TYPE_BOOL && na < TYPE_MAT2;
  1038. ret_type = na;
  1039. } break;
  1040. case OP_ADD:
  1041. case OP_SUB:
  1042. case OP_MUL:
  1043. case OP_DIV: {
  1044. DataType na = p_op->arguments[0]->get_datatype();
  1045. DataType nb = p_op->arguments[1]->get_datatype();
  1046. if (na > nb) {
  1047. //make things easier;
  1048. SWAP(na, nb);
  1049. }
  1050. if (na == nb) {
  1051. valid = (na > TYPE_BOOL && na <= TYPE_MAT4);
  1052. ret_type = na;
  1053. } else if (na == TYPE_INT && nb == TYPE_IVEC2) {
  1054. valid = true;
  1055. ret_type = TYPE_IVEC2;
  1056. } else if (na == TYPE_INT && nb == TYPE_IVEC3) {
  1057. valid = true;
  1058. ret_type = TYPE_IVEC3;
  1059. } else if (na == TYPE_INT && nb == TYPE_IVEC4) {
  1060. valid = true;
  1061. ret_type = TYPE_IVEC4;
  1062. } else if (na == TYPE_UINT && nb == TYPE_UVEC2) {
  1063. valid = true;
  1064. ret_type = TYPE_UVEC2;
  1065. } else if (na == TYPE_UINT && nb == TYPE_UVEC3) {
  1066. valid = true;
  1067. ret_type = TYPE_UVEC3;
  1068. } else if (na == TYPE_UINT && nb == TYPE_UVEC4) {
  1069. valid = true;
  1070. ret_type = TYPE_UVEC4;
  1071. } else if (na == TYPE_FLOAT && nb == TYPE_VEC2) {
  1072. valid = true;
  1073. ret_type = TYPE_VEC2;
  1074. } else if (na == TYPE_FLOAT && nb == TYPE_VEC3) {
  1075. valid = true;
  1076. ret_type = TYPE_VEC3;
  1077. } else if (na == TYPE_FLOAT && nb == TYPE_VEC4) {
  1078. valid = true;
  1079. ret_type = TYPE_VEC4;
  1080. } else if (na == TYPE_FLOAT && nb == TYPE_MAT2) {
  1081. valid = true;
  1082. ret_type = TYPE_MAT2;
  1083. } else if (na == TYPE_FLOAT && nb == TYPE_MAT3) {
  1084. valid = true;
  1085. ret_type = TYPE_MAT3;
  1086. } else if (na == TYPE_FLOAT && nb == TYPE_MAT4) {
  1087. valid = true;
  1088. ret_type = TYPE_MAT4;
  1089. } else if (p_op->op == OP_MUL && na == TYPE_VEC2 && nb == TYPE_MAT2) {
  1090. valid = true;
  1091. ret_type = TYPE_VEC2;
  1092. } else if (p_op->op == OP_MUL && na == TYPE_VEC3 && nb == TYPE_MAT3) {
  1093. valid = true;
  1094. ret_type = TYPE_VEC3;
  1095. } else if (p_op->op == OP_MUL && na == TYPE_VEC4 && nb == TYPE_MAT4) {
  1096. valid = true;
  1097. ret_type = TYPE_VEC4;
  1098. }
  1099. } break;
  1100. case OP_ASSIGN_MOD:
  1101. case OP_MOD: {
  1102. /*
  1103. * The operator modulus (%) operates on signed or unsigned integers or integer vectors. The operand
  1104. * types must both be signed or both be unsigned. The operands cannot be vectors of differing size. If
  1105. * one operand is a scalar and the other vector, then the scalar is applied component-wise to the vector,
  1106. * resulting in the same type as the vector. If both are vectors of the same size, the result is computed
  1107. * component-wise.
  1108. */
  1109. DataType na = p_op->arguments[0]->get_datatype();
  1110. DataType nb = p_op->arguments[1]->get_datatype();
  1111. if (na == TYPE_INT && nb == TYPE_INT) {
  1112. valid = true;
  1113. ret_type = TYPE_INT;
  1114. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1115. valid = true;
  1116. ret_type = TYPE_IVEC2;
  1117. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1118. valid = true;
  1119. ret_type = TYPE_IVEC3;
  1120. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1121. valid = true;
  1122. ret_type = TYPE_IVEC4;
  1123. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  1124. valid = true;
  1125. ret_type = TYPE_IVEC2;
  1126. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  1127. valid = true;
  1128. ret_type = TYPE_IVEC3;
  1129. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  1130. valid = true;
  1131. ret_type = TYPE_IVEC4;
  1132. /////
  1133. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1134. valid = true;
  1135. ret_type = TYPE_UINT;
  1136. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1137. valid = true;
  1138. ret_type = TYPE_UVEC2;
  1139. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1140. valid = true;
  1141. ret_type = TYPE_UVEC3;
  1142. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1143. valid = true;
  1144. ret_type = TYPE_UVEC4;
  1145. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1146. valid = true;
  1147. ret_type = TYPE_UVEC2;
  1148. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1149. valid = true;
  1150. ret_type = TYPE_UVEC3;
  1151. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1152. valid = true;
  1153. ret_type = TYPE_UVEC4;
  1154. }
  1155. } break;
  1156. case OP_ASSIGN_SHIFT_LEFT:
  1157. case OP_ASSIGN_SHIFT_RIGHT:
  1158. case OP_SHIFT_LEFT:
  1159. case OP_SHIFT_RIGHT: {
  1160. DataType na = p_op->arguments[0]->get_datatype();
  1161. DataType nb = p_op->arguments[1]->get_datatype();
  1162. if (na == TYPE_INT && nb == TYPE_INT) {
  1163. valid = true;
  1164. ret_type = TYPE_INT;
  1165. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1166. valid = true;
  1167. ret_type = TYPE_IVEC2;
  1168. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1169. valid = true;
  1170. ret_type = TYPE_IVEC3;
  1171. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1172. valid = true;
  1173. ret_type = TYPE_IVEC4;
  1174. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  1175. valid = true;
  1176. ret_type = TYPE_IVEC2;
  1177. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  1178. valid = true;
  1179. ret_type = TYPE_IVEC3;
  1180. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  1181. valid = true;
  1182. ret_type = TYPE_IVEC4;
  1183. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1184. valid = true;
  1185. ret_type = TYPE_UINT;
  1186. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1187. valid = true;
  1188. ret_type = TYPE_UVEC2;
  1189. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1190. valid = true;
  1191. ret_type = TYPE_UVEC3;
  1192. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1193. valid = true;
  1194. ret_type = TYPE_UVEC4;
  1195. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1196. valid = true;
  1197. ret_type = TYPE_UVEC2;
  1198. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1199. valid = true;
  1200. ret_type = TYPE_UVEC3;
  1201. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1202. valid = true;
  1203. ret_type = TYPE_UVEC4;
  1204. }
  1205. } break;
  1206. case OP_ASSIGN: {
  1207. DataType na = p_op->arguments[0]->get_datatype();
  1208. DataType nb = p_op->arguments[1]->get_datatype();
  1209. if (na == TYPE_STRUCT || nb == TYPE_STRUCT) {
  1210. valid = p_op->arguments[0]->get_datatype_name() == p_op->arguments[1]->get_datatype_name();
  1211. } else {
  1212. valid = na == nb;
  1213. }
  1214. ret_type = na;
  1215. } break;
  1216. case OP_ASSIGN_ADD:
  1217. case OP_ASSIGN_SUB:
  1218. case OP_ASSIGN_MUL:
  1219. case OP_ASSIGN_DIV: {
  1220. DataType na = p_op->arguments[0]->get_datatype();
  1221. DataType nb = p_op->arguments[1]->get_datatype();
  1222. if (na == nb) {
  1223. valid = (na > TYPE_BOOL && na <= TYPE_MAT4);
  1224. ret_type = na;
  1225. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1226. valid = true;
  1227. ret_type = TYPE_IVEC2;
  1228. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1229. valid = true;
  1230. ret_type = TYPE_IVEC3;
  1231. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1232. valid = true;
  1233. ret_type = TYPE_IVEC4;
  1234. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1235. valid = true;
  1236. ret_type = TYPE_UVEC2;
  1237. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1238. valid = true;
  1239. ret_type = TYPE_UVEC3;
  1240. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1241. valid = true;
  1242. ret_type = TYPE_UVEC4;
  1243. } else if (na == TYPE_VEC2 && nb == TYPE_FLOAT) {
  1244. valid = true;
  1245. ret_type = TYPE_VEC2;
  1246. } else if (na == TYPE_VEC3 && nb == TYPE_FLOAT) {
  1247. valid = true;
  1248. ret_type = TYPE_VEC3;
  1249. } else if (na == TYPE_VEC4 && nb == TYPE_FLOAT) {
  1250. valid = true;
  1251. ret_type = TYPE_VEC4;
  1252. } else if (na == TYPE_MAT2 && nb == TYPE_FLOAT) {
  1253. valid = true;
  1254. ret_type = TYPE_MAT2;
  1255. } else if (na == TYPE_MAT3 && nb == TYPE_FLOAT) {
  1256. valid = true;
  1257. ret_type = TYPE_MAT3;
  1258. } else if (na == TYPE_MAT4 && nb == TYPE_FLOAT) {
  1259. valid = true;
  1260. ret_type = TYPE_MAT4;
  1261. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC2 && nb == TYPE_MAT2) {
  1262. valid = true;
  1263. ret_type = TYPE_VEC2;
  1264. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC3 && nb == TYPE_MAT3) {
  1265. valid = true;
  1266. ret_type = TYPE_VEC3;
  1267. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC4 && nb == TYPE_MAT4) {
  1268. valid = true;
  1269. ret_type = TYPE_VEC4;
  1270. }
  1271. } break;
  1272. case OP_ASSIGN_BIT_AND:
  1273. case OP_ASSIGN_BIT_OR:
  1274. case OP_ASSIGN_BIT_XOR:
  1275. case OP_BIT_AND:
  1276. case OP_BIT_OR:
  1277. case OP_BIT_XOR: {
  1278. /*
  1279. * The bitwise operators and (&), exclusive-or (^), and inclusive-or (|). The operands must be of type
  1280. * signed or unsigned integers or integer vectors. The operands cannot be vectors of differing size. If
  1281. * one operand is a scalar and the other a vector, the scalar is applied component-wise to the vector,
  1282. * resulting in the same type as the vector. The fundamental types of the operands (signed or unsigned)
  1283. * must match.
  1284. */
  1285. DataType na = p_op->arguments[0]->get_datatype();
  1286. DataType nb = p_op->arguments[1]->get_datatype();
  1287. if (na > nb && p_op->op >= OP_BIT_AND) {
  1288. //can swap for non assign
  1289. SWAP(na, nb);
  1290. }
  1291. if (na == TYPE_INT && nb == TYPE_INT) {
  1292. valid = true;
  1293. ret_type = TYPE_INT;
  1294. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1295. valid = true;
  1296. ret_type = TYPE_IVEC2;
  1297. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1298. valid = true;
  1299. ret_type = TYPE_IVEC3;
  1300. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1301. valid = true;
  1302. ret_type = TYPE_IVEC4;
  1303. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  1304. valid = true;
  1305. ret_type = TYPE_IVEC2;
  1306. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  1307. valid = true;
  1308. ret_type = TYPE_IVEC3;
  1309. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  1310. valid = true;
  1311. ret_type = TYPE_IVEC4;
  1312. /////
  1313. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1314. valid = true;
  1315. ret_type = TYPE_UINT;
  1316. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1317. valid = true;
  1318. ret_type = TYPE_UVEC2;
  1319. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1320. valid = true;
  1321. ret_type = TYPE_UVEC3;
  1322. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1323. valid = true;
  1324. ret_type = TYPE_UVEC4;
  1325. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1326. valid = true;
  1327. ret_type = TYPE_UVEC2;
  1328. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1329. valid = true;
  1330. ret_type = TYPE_UVEC3;
  1331. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1332. valid = true;
  1333. ret_type = TYPE_UVEC4;
  1334. }
  1335. } break;
  1336. case OP_BIT_INVERT: { //unaries
  1337. DataType na = p_op->arguments[0]->get_datatype();
  1338. valid = na >= TYPE_INT && na < TYPE_FLOAT;
  1339. ret_type = na;
  1340. } break;
  1341. case OP_SELECT_IF: {
  1342. DataType na = p_op->arguments[0]->get_datatype();
  1343. DataType nb = p_op->arguments[1]->get_datatype();
  1344. DataType nc = p_op->arguments[2]->get_datatype();
  1345. valid = na == TYPE_BOOL && (nb == nc);
  1346. ret_type = nb;
  1347. } break;
  1348. default: {
  1349. ERR_FAIL_V(false);
  1350. }
  1351. }
  1352. if (r_ret_type) {
  1353. *r_ret_type = ret_type;
  1354. }
  1355. return valid;
  1356. }
  1357. const ShaderLanguage::BuiltinFuncDef ShaderLanguage::builtin_func_defs[] = {
  1358. //constructors
  1359. { "bool", TYPE_BOOL, { TYPE_BOOL, TYPE_VOID }, TAG_GLOBAL, false },
  1360. { "bvec2", TYPE_BVEC2, { TYPE_BOOL, TYPE_VOID }, TAG_GLOBAL, false },
  1361. { "bvec2", TYPE_BVEC2, { TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, TAG_GLOBAL, false },
  1362. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_VOID }, TAG_GLOBAL, false },
  1363. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, TAG_GLOBAL, false },
  1364. { "bvec3", TYPE_BVEC3, { TYPE_BVEC2, TYPE_BOOL, TYPE_VOID }, TAG_GLOBAL, false },
  1365. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_BVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1366. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_VOID }, TAG_GLOBAL, false },
  1367. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, TAG_GLOBAL, false },
  1368. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BVEC2, TYPE_BOOL, TYPE_VOID }, TAG_GLOBAL, false },
  1369. { "bvec4", TYPE_BVEC4, { TYPE_BVEC2, TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, TAG_GLOBAL, false },
  1370. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BOOL, TYPE_BVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1371. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1372. { "bvec4", TYPE_BVEC4, { TYPE_BVEC3, TYPE_BOOL, TYPE_VOID }, TAG_GLOBAL, false },
  1373. { "bvec4", TYPE_BVEC4, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1374. { "float", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1375. { "vec2", TYPE_VEC2, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1376. { "vec2", TYPE_VEC2, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1377. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1378. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1379. { "vec3", TYPE_VEC3, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1380. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1381. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1382. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1383. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1384. { "vec4", TYPE_VEC4, { TYPE_VEC2, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1385. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1386. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1387. { "vec4", TYPE_VEC4, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1388. { "vec4", TYPE_VEC4, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1389. { "int", TYPE_INT, { TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1390. { "ivec2", TYPE_IVEC2, { TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1391. { "ivec2", TYPE_IVEC2, { TYPE_INT, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1392. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1393. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1394. { "ivec3", TYPE_IVEC3, { TYPE_IVEC2, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1395. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1396. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1397. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1398. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1399. { "ivec4", TYPE_IVEC4, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1400. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_INT, TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1401. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_IVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1402. { "ivec4", TYPE_IVEC4, { TYPE_IVEC3, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1403. { "ivec4", TYPE_IVEC4, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1404. { "uint", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1405. { "uvec2", TYPE_UVEC2, { TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1406. { "uvec2", TYPE_UVEC2, { TYPE_UINT, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1407. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1408. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1409. { "uvec3", TYPE_UVEC3, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1410. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_UVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1411. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1412. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1413. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1414. { "uvec4", TYPE_UVEC4, { TYPE_UVEC2, TYPE_UINT, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1415. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UINT, TYPE_UVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1416. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1417. { "uvec4", TYPE_UVEC4, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1418. { "uvec4", TYPE_UVEC4, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1419. { "mat2", TYPE_MAT2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1420. { "mat3", TYPE_MAT3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1421. { "mat4", TYPE_MAT4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1422. { "mat2", TYPE_MAT2, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1423. { "mat3", TYPE_MAT3, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1424. { "mat4", TYPE_MAT4, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1425. //conversion scalars
  1426. { "int", TYPE_INT, { TYPE_BOOL, TYPE_VOID }, TAG_GLOBAL, false },
  1427. { "int", TYPE_INT, { TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1428. { "int", TYPE_INT, { TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1429. { "int", TYPE_INT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1430. { "float", TYPE_FLOAT, { TYPE_BOOL, TYPE_VOID }, TAG_GLOBAL, false },
  1431. { "float", TYPE_FLOAT, { TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1432. { "float", TYPE_FLOAT, { TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1433. { "float", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1434. { "uint", TYPE_UINT, { TYPE_BOOL, TYPE_VOID }, TAG_GLOBAL, true },
  1435. { "uint", TYPE_UINT, { TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1436. { "uint", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1437. { "uint", TYPE_UINT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1438. { "bool", TYPE_BOOL, { TYPE_BOOL, TYPE_VOID }, TAG_GLOBAL, false },
  1439. { "bool", TYPE_BOOL, { TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1440. { "bool", TYPE_BOOL, { TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1441. { "bool", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1442. //conversion vectors
  1443. { "ivec2", TYPE_IVEC2, { TYPE_BVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1444. { "ivec2", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1445. { "ivec2", TYPE_IVEC2, { TYPE_UVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1446. { "ivec2", TYPE_IVEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1447. { "vec2", TYPE_VEC2, { TYPE_BVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1448. { "vec2", TYPE_VEC2, { TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1449. { "vec2", TYPE_VEC2, { TYPE_UVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1450. { "vec2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1451. { "uvec2", TYPE_UVEC2, { TYPE_BVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1452. { "uvec2", TYPE_UVEC2, { TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1453. { "uvec2", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1454. { "uvec2", TYPE_UVEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1455. { "bvec2", TYPE_BVEC2, { TYPE_BVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1456. { "bvec2", TYPE_BVEC2, { TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1457. { "bvec2", TYPE_BVEC2, { TYPE_UVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1458. { "bvec2", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1459. { "ivec3", TYPE_IVEC3, { TYPE_BVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1460. { "ivec3", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1461. { "ivec3", TYPE_IVEC3, { TYPE_UVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1462. { "ivec3", TYPE_IVEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1463. { "vec3", TYPE_VEC3, { TYPE_BVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1464. { "vec3", TYPE_VEC3, { TYPE_IVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1465. { "vec3", TYPE_VEC3, { TYPE_UVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1466. { "vec3", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1467. { "uvec3", TYPE_UVEC3, { TYPE_BVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1468. { "uvec3", TYPE_UVEC3, { TYPE_IVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1469. { "uvec3", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1470. { "uvec3", TYPE_UVEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1471. { "bvec3", TYPE_BVEC3, { TYPE_BVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1472. { "bvec3", TYPE_BVEC3, { TYPE_IVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1473. { "bvec3", TYPE_BVEC3, { TYPE_UVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1474. { "bvec3", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1475. { "ivec4", TYPE_IVEC4, { TYPE_BVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1476. { "ivec4", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1477. { "ivec4", TYPE_IVEC4, { TYPE_UVEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1478. { "ivec4", TYPE_IVEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1479. { "vec4", TYPE_VEC4, { TYPE_BVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1480. { "vec4", TYPE_VEC4, { TYPE_IVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1481. { "vec4", TYPE_VEC4, { TYPE_UVEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1482. { "vec4", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1483. { "uvec4", TYPE_UVEC4, { TYPE_BVEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1484. { "uvec4", TYPE_UVEC4, { TYPE_IVEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1485. { "uvec4", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1486. { "uvec4", TYPE_UVEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1487. { "bvec4", TYPE_BVEC4, { TYPE_BVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1488. { "bvec4", TYPE_BVEC4, { TYPE_IVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1489. { "bvec4", TYPE_BVEC4, { TYPE_UVEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1490. { "bvec4", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1491. //conversion between matrixes
  1492. { "mat2", TYPE_MAT2, { TYPE_MAT3, TYPE_VOID }, TAG_GLOBAL, false },
  1493. { "mat2", TYPE_MAT2, { TYPE_MAT4, TYPE_VOID }, TAG_GLOBAL, false },
  1494. { "mat3", TYPE_MAT3, { TYPE_MAT2, TYPE_VOID }, TAG_GLOBAL, false },
  1495. { "mat3", TYPE_MAT3, { TYPE_MAT4, TYPE_VOID }, TAG_GLOBAL, false },
  1496. { "mat4", TYPE_MAT4, { TYPE_MAT2, TYPE_VOID }, TAG_GLOBAL, false },
  1497. { "mat4", TYPE_MAT4, { TYPE_MAT3, TYPE_VOID }, TAG_GLOBAL, false },
  1498. //builtins - trigonometry
  1499. { "radians", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1500. { "radians", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1501. { "radians", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1502. { "radians", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1503. { "degrees", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1504. { "degrees", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1505. { "degrees", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1506. { "degrees", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1507. { "sin", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1508. { "sin", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1509. { "sin", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1510. { "sin", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1511. { "cos", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1512. { "cos", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1513. { "cos", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1514. { "cos", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1515. { "tan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1516. { "tan", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1517. { "tan", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1518. { "tan", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1519. { "asin", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1520. { "asin", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1521. { "asin", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1522. { "asin", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1523. { "acos", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1524. { "acos", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1525. { "acos", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1526. { "acos", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1527. { "atan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1528. { "atan", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1529. { "atan", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1530. { "atan", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1531. { "atan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1532. { "atan", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1533. { "atan", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1534. { "atan", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1535. { "sinh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1536. { "sinh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1537. { "sinh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1538. { "sinh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1539. { "cosh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1540. { "cosh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1541. { "cosh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1542. { "cosh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1543. { "tanh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1544. { "tanh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1545. { "tanh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1546. { "tanh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1547. { "asinh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1548. { "asinh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1549. { "asinh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1550. { "asinh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1551. { "acosh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1552. { "acosh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1553. { "acosh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1554. { "acosh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1555. { "atanh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1556. { "atanh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1557. { "atanh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1558. { "atanh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1559. //builtins - exponential
  1560. { "pow", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1561. { "pow", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1562. { "pow", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1563. { "pow", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1564. { "exp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1565. { "exp", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1566. { "exp", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1567. { "exp", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1568. { "log", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1569. { "log", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1570. { "log", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1571. { "log", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1572. { "exp2", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1573. { "exp2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1574. { "exp2", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1575. { "exp2", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1576. { "log2", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1577. { "log2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1578. { "log2", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1579. { "log2", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1580. { "sqrt", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1581. { "sqrt", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1582. { "sqrt", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1583. { "sqrt", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1584. { "inversesqrt", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1585. { "inversesqrt", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1586. { "inversesqrt", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1587. { "inversesqrt", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1588. //builtins - common
  1589. { "abs", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1590. { "abs", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1591. { "abs", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1592. { "abs", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1593. { "abs", TYPE_INT, { TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1594. { "abs", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1595. { "abs", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1596. { "abs", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1597. { "sign", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1598. { "sign", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1599. { "sign", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1600. { "sign", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1601. { "sign", TYPE_INT, { TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1602. { "sign", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1603. { "sign", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1604. { "sign", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1605. { "floor", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1606. { "floor", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1607. { "floor", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1608. { "floor", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1609. { "trunc", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1610. { "trunc", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1611. { "trunc", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1612. { "trunc", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1613. { "round", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1614. { "round", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1615. { "round", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1616. { "round", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1617. { "roundEven", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1618. { "roundEven", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1619. { "roundEven", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1620. { "roundEven", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1621. { "ceil", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1622. { "ceil", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1623. { "ceil", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1624. { "ceil", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1625. { "fract", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1626. { "fract", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1627. { "fract", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1628. { "fract", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1629. { "mod", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1630. { "mod", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1631. { "mod", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1632. { "mod", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1633. { "mod", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1634. { "mod", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1635. { "mod", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1636. { "modf", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1637. { "modf", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1638. { "modf", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1639. { "modf", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1640. { "min", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1641. { "min", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1642. { "min", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1643. { "min", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1644. { "min", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1645. { "min", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1646. { "min", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1647. { "min", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1648. { "min", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1649. { "min", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1650. { "min", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1651. { "min", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1652. { "min", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1653. { "min", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1654. { "min", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1655. { "min", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1656. { "min", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1657. { "min", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1658. { "min", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1659. { "min", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1660. { "min", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1661. { "max", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1662. { "max", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1663. { "max", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1664. { "max", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1665. { "max", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1666. { "max", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1667. { "max", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1668. { "max", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1669. { "max", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1670. { "max", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1671. { "max", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1672. { "max", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1673. { "max", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1674. { "max", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1675. { "max", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1676. { "max", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1677. { "max", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1678. { "max", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1679. { "max", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1680. { "max", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1681. { "max", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1682. { "clamp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1683. { "clamp", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1684. { "clamp", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1685. { "clamp", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1686. { "clamp", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1687. { "clamp", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1688. { "clamp", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1689. { "clamp", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1690. { "clamp", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1691. { "clamp", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1692. { "clamp", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1693. { "clamp", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1694. { "clamp", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1695. { "clamp", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, false },
  1696. { "clamp", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1697. { "clamp", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1698. { "clamp", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1699. { "clamp", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1700. { "clamp", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1701. { "clamp", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1702. { "clamp", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1703. { "mix", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1704. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1705. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_BVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1706. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1707. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1708. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_BVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1709. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1710. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1711. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_BVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1712. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1713. { "step", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1714. { "step", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1715. { "step", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1716. { "step", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1717. { "step", TYPE_VEC2, { TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1718. { "step", TYPE_VEC3, { TYPE_FLOAT, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1719. { "step", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1720. { "smoothstep", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1721. { "smoothstep", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1722. { "smoothstep", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1723. { "smoothstep", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1724. { "smoothstep", TYPE_VEC2, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1725. { "smoothstep", TYPE_VEC3, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1726. { "smoothstep", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1727. { "isnan", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1728. { "isnan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1729. { "isnan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1730. { "isnan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1731. { "isinf", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1732. { "isinf", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1733. { "isinf", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1734. { "isinf", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1735. { "floatBitsToInt", TYPE_INT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1736. { "floatBitsToInt", TYPE_IVEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1737. { "floatBitsToInt", TYPE_IVEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1738. { "floatBitsToInt", TYPE_IVEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1739. { "floatBitsToUint", TYPE_UINT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1740. { "floatBitsToUint", TYPE_UVEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1741. { "floatBitsToUint", TYPE_UVEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1742. { "floatBitsToUint", TYPE_UVEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1743. { "intBitsToFloat", TYPE_FLOAT, { TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1744. { "intBitsToFloat", TYPE_VEC2, { TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1745. { "intBitsToFloat", TYPE_VEC3, { TYPE_IVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1746. { "intBitsToFloat", TYPE_VEC4, { TYPE_IVEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1747. { "uintBitsToFloat", TYPE_FLOAT, { TYPE_UINT, TYPE_VOID }, TAG_GLOBAL, true },
  1748. { "uintBitsToFloat", TYPE_VEC2, { TYPE_UVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1749. { "uintBitsToFloat", TYPE_VEC3, { TYPE_UVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1750. { "uintBitsToFloat", TYPE_VEC4, { TYPE_UVEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1751. //builtins - geometric
  1752. { "length", TYPE_FLOAT, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1753. { "length", TYPE_FLOAT, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1754. { "length", TYPE_FLOAT, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1755. { "distance", TYPE_FLOAT, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1756. { "distance", TYPE_FLOAT, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1757. { "distance", TYPE_FLOAT, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1758. { "dot", TYPE_FLOAT, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1759. { "dot", TYPE_FLOAT, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1760. { "dot", TYPE_FLOAT, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1761. { "cross", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1762. { "normalize", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1763. { "normalize", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1764. { "normalize", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1765. { "reflect", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1766. { "refract", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1767. { "faceforward", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1768. { "faceforward", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1769. { "faceforward", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1770. { "matrixCompMult", TYPE_MAT2, { TYPE_MAT2, TYPE_MAT2, TYPE_VOID }, TAG_GLOBAL, false },
  1771. { "matrixCompMult", TYPE_MAT3, { TYPE_MAT3, TYPE_MAT3, TYPE_VOID }, TAG_GLOBAL, false },
  1772. { "matrixCompMult", TYPE_MAT4, { TYPE_MAT4, TYPE_MAT4, TYPE_VOID }, TAG_GLOBAL, false },
  1773. { "outerProduct", TYPE_MAT2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1774. { "outerProduct", TYPE_MAT3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1775. { "outerProduct", TYPE_MAT4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1776. { "transpose", TYPE_MAT2, { TYPE_MAT2, TYPE_VOID }, TAG_GLOBAL, false },
  1777. { "transpose", TYPE_MAT3, { TYPE_MAT3, TYPE_VOID }, TAG_GLOBAL, false },
  1778. { "transpose", TYPE_MAT4, { TYPE_MAT4, TYPE_VOID }, TAG_GLOBAL, false },
  1779. { "determinant", TYPE_FLOAT, { TYPE_MAT2, TYPE_VOID }, TAG_GLOBAL, false },
  1780. { "determinant", TYPE_FLOAT, { TYPE_MAT3, TYPE_VOID }, TAG_GLOBAL, false },
  1781. { "determinant", TYPE_FLOAT, { TYPE_MAT4, TYPE_VOID }, TAG_GLOBAL, false },
  1782. { "inverse", TYPE_MAT2, { TYPE_MAT2, TYPE_VOID }, TAG_GLOBAL, false },
  1783. { "inverse", TYPE_MAT3, { TYPE_MAT3, TYPE_VOID }, TAG_GLOBAL, false },
  1784. { "inverse", TYPE_MAT4, { TYPE_MAT4, TYPE_VOID }, TAG_GLOBAL, false },
  1785. { "lessThan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1786. { "lessThan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1787. { "lessThan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1788. { "lessThan", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1789. { "lessThan", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1790. { "lessThan", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1791. { "lessThan", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1792. { "lessThan", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1793. { "lessThan", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1794. { "greaterThan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1795. { "greaterThan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1796. { "greaterThan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1797. { "greaterThan", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1798. { "greaterThan", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1799. { "greaterThan", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1800. { "greaterThan", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1801. { "greaterThan", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1802. { "greaterThan", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1803. { "lessThanEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1804. { "lessThanEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1805. { "lessThanEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1806. { "lessThanEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1807. { "lessThanEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1808. { "lessThanEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1809. { "lessThanEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1810. { "lessThanEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1811. { "lessThanEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1812. { "greaterThanEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1813. { "greaterThanEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1814. { "greaterThanEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1815. { "greaterThanEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1816. { "greaterThanEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1817. { "greaterThanEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1818. { "greaterThanEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1819. { "greaterThanEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1820. { "greaterThanEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1821. { "equal", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1822. { "equal", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1823. { "equal", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1824. { "equal", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1825. { "equal", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1826. { "equal", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1827. { "equal", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1828. { "equal", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1829. { "equal", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1830. { "equal", TYPE_BVEC2, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1831. { "equal", TYPE_BVEC3, { TYPE_BVEC3, TYPE_BVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1832. { "equal", TYPE_BVEC4, { TYPE_BVEC4, TYPE_BVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1833. { "notEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1834. { "notEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1835. { "notEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1836. { "notEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1837. { "notEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1838. { "notEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1839. { "notEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1840. { "notEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1841. { "notEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1842. { "notEqual", TYPE_BVEC2, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1843. { "notEqual", TYPE_BVEC3, { TYPE_BVEC3, TYPE_BVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1844. { "notEqual", TYPE_BVEC4, { TYPE_BVEC4, TYPE_BVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1845. { "any", TYPE_BOOL, { TYPE_BVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1846. { "any", TYPE_BOOL, { TYPE_BVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1847. { "any", TYPE_BOOL, { TYPE_BVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1848. { "all", TYPE_BOOL, { TYPE_BVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1849. { "all", TYPE_BOOL, { TYPE_BVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1850. { "all", TYPE_BOOL, { TYPE_BVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1851. { "not", TYPE_BVEC2, { TYPE_BVEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1852. { "not", TYPE_BVEC3, { TYPE_BVEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1853. { "not", TYPE_BVEC4, { TYPE_BVEC4, TYPE_VOID }, TAG_GLOBAL, false },
  1854. //builtins - texture
  1855. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLER2D, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1856. { "textureSize", TYPE_IVEC2, { TYPE_ISAMPLER2D, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1857. { "textureSize", TYPE_IVEC2, { TYPE_USAMPLER2D, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1858. { "textureSize", TYPE_IVEC3, { TYPE_SAMPLER2DARRAY, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1859. { "textureSize", TYPE_IVEC3, { TYPE_ISAMPLER2DARRAY, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1860. { "textureSize", TYPE_IVEC3, { TYPE_USAMPLER2DARRAY, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1861. { "textureSize", TYPE_IVEC3, { TYPE_SAMPLER3D, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1862. { "textureSize", TYPE_IVEC3, { TYPE_ISAMPLER3D, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1863. { "textureSize", TYPE_IVEC3, { TYPE_USAMPLER3D, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1864. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLERCUBE, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1865. { "texture", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1866. { "texture", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1867. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1868. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1869. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1870. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1871. { "texture", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1872. { "texture", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1873. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1874. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1875. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1876. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1877. { "texture", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1878. { "texture", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1879. { "texture", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1880. { "texture", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1881. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1882. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1883. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, false },
  1884. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1885. { "texture", TYPE_VEC4, { TYPE_SAMPLEREXT, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, false },
  1886. { "texture", TYPE_VEC4, { TYPE_SAMPLEREXT, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1887. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1888. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1889. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1890. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1891. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1892. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1893. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1894. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1895. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1896. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1897. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1898. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1899. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1900. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1901. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1902. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1903. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1904. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1905. { "textureProj", TYPE_VEC4, { TYPE_SAMPLEREXT, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1906. { "textureProj", TYPE_VEC4, { TYPE_SAMPLEREXT, TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1907. { "textureProj", TYPE_VEC4, { TYPE_SAMPLEREXT, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1908. { "textureProj", TYPE_VEC4, { TYPE_SAMPLEREXT, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1909. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1910. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1911. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1912. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1913. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1914. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1915. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1916. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1917. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1918. { "textureLod", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, false },
  1919. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1920. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1921. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1922. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1923. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1924. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1925. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1926. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1927. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, TAG_GLOBAL, true },
  1928. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1929. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1930. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1931. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1932. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1933. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1934. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1935. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1936. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1937. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1938. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1939. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1940. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1941. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1942. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1943. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1944. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1945. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1946. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1947. { "dFdx", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1948. { "dFdx", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1949. { "dFdx", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1950. { "dFdx", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1951. { "dFdy", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1952. { "dFdy", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1953. { "dFdy", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1954. { "dFdy", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1955. { "fwidth", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, TAG_GLOBAL, true },
  1956. { "fwidth", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, TAG_GLOBAL, true },
  1957. { "fwidth", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, TAG_GLOBAL, true },
  1958. { "fwidth", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, TAG_GLOBAL, true },
  1959. //sub-functions
  1960. //array
  1961. { "length", TYPE_INT, { TYPE_VOID }, TAG_ARRAY, true },
  1962. { nullptr, TYPE_VOID, { TYPE_VOID }, TAG_GLOBAL, false }
  1963. };
  1964. const ShaderLanguage::BuiltinFuncOutArgs ShaderLanguage::builtin_func_out_args[] = {
  1965. //constructors
  1966. { "modf", 1 },
  1967. { nullptr, 0 }
  1968. };
  1969. bool ShaderLanguage::is_const_suffix_lut_initialized = false;
  1970. bool ShaderLanguage::_validate_function_call(BlockNode *p_block, OperatorNode *p_func, DataType *r_ret_type, StringName *r_ret_type_str) {
  1971. ERR_FAIL_COND_V(p_func->op != OP_CALL && p_func->op != OP_CONSTRUCT, false);
  1972. Vector<DataType> args;
  1973. Vector<StringName> args2;
  1974. ERR_FAIL_COND_V(p_func->arguments[0]->type != Node::TYPE_VARIABLE, false);
  1975. StringName name = static_cast<VariableNode *>(p_func->arguments[0])->name.operator String();
  1976. for (int i = 1; i < p_func->arguments.size(); i++) {
  1977. args.push_back(p_func->arguments[i]->get_datatype());
  1978. args2.push_back(p_func->arguments[i]->get_datatype_name());
  1979. }
  1980. int argcount = args.size();
  1981. bool failed_builtin = false;
  1982. bool unsupported_builtin = false;
  1983. int builtin_idx = 0;
  1984. if (argcount <= 4) {
  1985. // test builtins
  1986. int idx = 0;
  1987. while (builtin_func_defs[idx].name) {
  1988. if (completion_class != builtin_func_defs[idx].tag) {
  1989. idx++;
  1990. continue;
  1991. }
  1992. if (name == builtin_func_defs[idx].name) {
  1993. failed_builtin = true;
  1994. bool fail = false;
  1995. for (int i = 0; i < argcount; i++) {
  1996. 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])) {
  1997. //all good, but needs implicit conversion later
  1998. } else if (args[i] != builtin_func_defs[idx].args[i]) {
  1999. fail = true;
  2000. break;
  2001. }
  2002. }
  2003. if (!fail) {
  2004. if (VisualServer::get_singleton()->is_low_end()) {
  2005. if (builtin_func_defs[idx].high_end) {
  2006. fail = true;
  2007. unsupported_builtin = true;
  2008. builtin_idx = idx;
  2009. }
  2010. }
  2011. }
  2012. if (!fail && argcount < 4 && builtin_func_defs[idx].args[argcount] != TYPE_VOID) {
  2013. fail = true; //make sure the number of arguments matches
  2014. }
  2015. if (!fail) {
  2016. //make sure its not an out argument used in the wrong way
  2017. int outarg_idx = 0;
  2018. while (builtin_func_out_args[outarg_idx].name) {
  2019. if (String(name) == builtin_func_out_args[outarg_idx].name) {
  2020. int arg_idx = builtin_func_out_args[outarg_idx].argument;
  2021. if (arg_idx < argcount) {
  2022. if (p_func->arguments[arg_idx + 1]->type != Node::TYPE_VARIABLE) {
  2023. _set_error("Argument " + itos(arg_idx + 1) + " of function '" + String(name) + "' is not a variable");
  2024. return false;
  2025. }
  2026. StringName var_name = static_cast<const VariableNode *>(p_func->arguments[arg_idx + 1])->name;
  2027. if (shader->varyings.has(var_name)) {
  2028. _set_error(vformat("Varyings cannot be passed for '%s' parameter!", "out"));
  2029. return false;
  2030. }
  2031. const BlockNode *b = p_block;
  2032. bool valid = false;
  2033. while (b) {
  2034. if (b->variables.has(var_name)) {
  2035. valid = true;
  2036. break;
  2037. }
  2038. if (b->parent_function) {
  2039. for (int i = 0; i < b->parent_function->arguments.size(); i++) {
  2040. if (b->parent_function->arguments[i].name == var_name) {
  2041. valid = true;
  2042. break;
  2043. }
  2044. }
  2045. }
  2046. b = b->parent_block;
  2047. }
  2048. if (!valid) {
  2049. _set_error("Argument " + itos(arg_idx + 1) + " of function '" + String(name) + "' can only take a local variable");
  2050. return false;
  2051. }
  2052. }
  2053. }
  2054. outarg_idx++;
  2055. }
  2056. //implicitly convert values if possible
  2057. for (int i = 0; i < argcount; i++) {
  2058. 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) {
  2059. //can't do implicit conversion here
  2060. continue;
  2061. }
  2062. //this is an implicit conversion
  2063. ConstantNode *constant = static_cast<ConstantNode *>(p_func->arguments[i + 1]);
  2064. ConstantNode *conversion = alloc_node<ConstantNode>();
  2065. conversion->datatype = builtin_func_defs[idx].args[i];
  2066. conversion->values.resize(1);
  2067. convert_constant(constant, builtin_func_defs[idx].args[i], conversion->values.ptrw());
  2068. p_func->arguments.write[i + 1] = conversion;
  2069. }
  2070. if (r_ret_type) {
  2071. *r_ret_type = builtin_func_defs[idx].rettype;
  2072. }
  2073. return true;
  2074. }
  2075. }
  2076. idx++;
  2077. }
  2078. }
  2079. if (unsupported_builtin) {
  2080. String arglist = "";
  2081. for (int i = 0; i < argcount; i++) {
  2082. if (i > 0) {
  2083. arglist += ", ";
  2084. }
  2085. arglist += get_datatype_name(builtin_func_defs[builtin_idx].args[i]);
  2086. }
  2087. String err = "Built-in function \"" + String(name) + "(" + arglist + ")\" is only supported on the GLES3 backend, but your project is using GLES2.";
  2088. _set_error(err);
  2089. return false;
  2090. }
  2091. if (failed_builtin) {
  2092. String err = "Invalid arguments for built-in function: " + String(name) + "(";
  2093. for (int i = 0; i < argcount; i++) {
  2094. if (i > 0) {
  2095. err += ",";
  2096. }
  2097. if (p_func->arguments[i + 1]->type == Node::TYPE_CONSTANT && p_func->arguments[i + 1]->get_datatype() == TYPE_INT && static_cast<ConstantNode *>(p_func->arguments[i + 1])->values[0].sint < 0) {
  2098. err += "-";
  2099. }
  2100. err += get_datatype_name(args[i]);
  2101. }
  2102. err += ")";
  2103. _set_error(err);
  2104. return false;
  2105. }
  2106. // try existing functions..
  2107. StringName exclude_function;
  2108. BlockNode *block = p_block;
  2109. while (block) {
  2110. if (block->parent_function) {
  2111. exclude_function = block->parent_function->name;
  2112. }
  2113. block = block->parent_block;
  2114. }
  2115. if (name == exclude_function) {
  2116. _set_error("Recursion is not allowed");
  2117. return false;
  2118. }
  2119. for (int i = 0; i < shader->functions.size(); i++) {
  2120. if (name != shader->functions[i].name) {
  2121. continue;
  2122. }
  2123. if (!shader->functions[i].callable) {
  2124. _set_error("Function '" + String(name) + " can't be called from source code.");
  2125. return false;
  2126. }
  2127. FunctionNode *pfunc = shader->functions[i].function;
  2128. if (pfunc->arguments.size() != args.size()) {
  2129. continue;
  2130. }
  2131. bool fail = false;
  2132. for (int j = 0; j < args.size(); j++) {
  2133. if (args[j] == TYPE_STRUCT && args2[j] != pfunc->arguments[j].type_str) {
  2134. fail = true;
  2135. break;
  2136. }
  2137. if (get_scalar_type(args[j]) == args[j] && p_func->arguments[j + 1]->type == Node::TYPE_CONSTANT && convert_constant(static_cast<ConstantNode *>(p_func->arguments[j + 1]), pfunc->arguments[j].type)) {
  2138. //all good, but it needs implicit conversion later
  2139. } else if (args[j] != pfunc->arguments[j].type) {
  2140. fail = true;
  2141. break;
  2142. }
  2143. }
  2144. if (!fail) {
  2145. //implicitly convert values if possible
  2146. for (int k = 0; k < args.size(); k++) {
  2147. if (get_scalar_type(args[k]) != args[k] || args[k] == pfunc->arguments[k].type || p_func->arguments[k + 1]->type != Node::TYPE_CONSTANT) {
  2148. //can't do implicit conversion here
  2149. continue;
  2150. }
  2151. //this is an implicit conversion
  2152. ConstantNode *constant = static_cast<ConstantNode *>(p_func->arguments[k + 1]);
  2153. ConstantNode *conversion = alloc_node<ConstantNode>();
  2154. conversion->datatype = pfunc->arguments[k].type;
  2155. conversion->values.resize(1);
  2156. convert_constant(constant, pfunc->arguments[k].type, conversion->values.ptrw());
  2157. p_func->arguments.write[k + 1] = conversion;
  2158. }
  2159. if (r_ret_type) {
  2160. *r_ret_type = pfunc->return_type;
  2161. if (pfunc->return_type == TYPE_STRUCT) {
  2162. *r_ret_type_str = pfunc->return_struct_name;
  2163. }
  2164. }
  2165. return true;
  2166. }
  2167. }
  2168. return false;
  2169. }
  2170. bool ShaderLanguage::_compare_datatypes_in_nodes(Node *a, Node *b) const {
  2171. if (a->get_datatype() != b->get_datatype()) {
  2172. return false;
  2173. }
  2174. if (a->get_datatype() == TYPE_STRUCT || b->get_datatype() == TYPE_STRUCT) {
  2175. if (a->get_datatype_name() != b->get_datatype_name()) {
  2176. return false;
  2177. }
  2178. }
  2179. return true;
  2180. }
  2181. bool ShaderLanguage::_parse_function_arguments(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, OperatorNode *p_func, int *r_complete_arg) {
  2182. TkPos pos = _get_tkpos();
  2183. Token tk = _get_token();
  2184. if (tk.type == TK_PARENTHESIS_CLOSE) {
  2185. return true;
  2186. }
  2187. _set_tkpos(pos);
  2188. while (true) {
  2189. if (r_complete_arg) {
  2190. pos = _get_tkpos();
  2191. tk = _get_token();
  2192. if (tk.type == TK_CURSOR) {
  2193. *r_complete_arg = p_func->arguments.size() - 1;
  2194. } else {
  2195. _set_tkpos(pos);
  2196. }
  2197. }
  2198. Node *arg = _parse_and_reduce_expression(p_block, p_builtin_types);
  2199. if (!arg) {
  2200. return false;
  2201. }
  2202. p_func->arguments.push_back(arg);
  2203. tk = _get_token();
  2204. if (tk.type == TK_PARENTHESIS_CLOSE) {
  2205. return true;
  2206. } else if (tk.type != TK_COMMA) {
  2207. // something is broken
  2208. _set_error("Expected ',' or ')' after argument");
  2209. return false;
  2210. }
  2211. }
  2212. return true;
  2213. }
  2214. bool ShaderLanguage::is_token_operator(TokenType p_type) {
  2215. return (p_type == TK_OP_EQUAL ||
  2216. p_type == TK_OP_NOT_EQUAL ||
  2217. p_type == TK_OP_LESS ||
  2218. p_type == TK_OP_LESS_EQUAL ||
  2219. p_type == TK_OP_GREATER ||
  2220. p_type == TK_OP_GREATER_EQUAL ||
  2221. p_type == TK_OP_AND ||
  2222. p_type == TK_OP_OR ||
  2223. p_type == TK_OP_NOT ||
  2224. p_type == TK_OP_ADD ||
  2225. p_type == TK_OP_SUB ||
  2226. p_type == TK_OP_MUL ||
  2227. p_type == TK_OP_DIV ||
  2228. p_type == TK_OP_MOD ||
  2229. p_type == TK_OP_SHIFT_LEFT ||
  2230. p_type == TK_OP_SHIFT_RIGHT ||
  2231. p_type == TK_OP_ASSIGN ||
  2232. p_type == TK_OP_ASSIGN_ADD ||
  2233. p_type == TK_OP_ASSIGN_SUB ||
  2234. p_type == TK_OP_ASSIGN_MUL ||
  2235. p_type == TK_OP_ASSIGN_DIV ||
  2236. p_type == TK_OP_ASSIGN_MOD ||
  2237. p_type == TK_OP_ASSIGN_SHIFT_LEFT ||
  2238. p_type == TK_OP_ASSIGN_SHIFT_RIGHT ||
  2239. p_type == TK_OP_ASSIGN_BIT_AND ||
  2240. p_type == TK_OP_ASSIGN_BIT_OR ||
  2241. p_type == TK_OP_ASSIGN_BIT_XOR ||
  2242. p_type == TK_OP_BIT_AND ||
  2243. p_type == TK_OP_BIT_OR ||
  2244. p_type == TK_OP_BIT_XOR ||
  2245. p_type == TK_OP_BIT_INVERT ||
  2246. p_type == TK_OP_INCREMENT ||
  2247. p_type == TK_OP_DECREMENT ||
  2248. p_type == TK_QUESTION ||
  2249. p_type == TK_COLON);
  2250. }
  2251. bool ShaderLanguage::is_token_operator_assign(TokenType p_type) {
  2252. return (p_type == TK_OP_ASSIGN ||
  2253. p_type == TK_OP_ASSIGN_ADD ||
  2254. p_type == TK_OP_ASSIGN_SUB ||
  2255. p_type == TK_OP_ASSIGN_MUL ||
  2256. p_type == TK_OP_ASSIGN_DIV ||
  2257. p_type == TK_OP_ASSIGN_MOD ||
  2258. p_type == TK_OP_ASSIGN_SHIFT_LEFT ||
  2259. p_type == TK_OP_ASSIGN_SHIFT_RIGHT ||
  2260. p_type == TK_OP_ASSIGN_BIT_AND ||
  2261. p_type == TK_OP_ASSIGN_BIT_OR ||
  2262. p_type == TK_OP_ASSIGN_BIT_XOR);
  2263. }
  2264. bool ShaderLanguage::is_token_hint(TokenType p_type) {
  2265. return int(p_type) > int(TK_RENDER_MODE) && int(p_type) < int(TK_SHADER_TYPE);
  2266. }
  2267. bool ShaderLanguage::convert_constant(ConstantNode *p_constant, DataType p_to_type, ConstantNode::Value *p_value) {
  2268. if (p_constant->datatype == p_to_type) {
  2269. if (p_value) {
  2270. for (int i = 0; i < p_constant->values.size(); i++) {
  2271. p_value[i] = p_constant->values[i];
  2272. }
  2273. }
  2274. return true;
  2275. } else if (p_constant->datatype == TYPE_INT && p_to_type == TYPE_FLOAT) {
  2276. if (p_value) {
  2277. p_value->real = p_constant->values[0].sint;
  2278. }
  2279. return true;
  2280. } else if (p_constant->datatype == TYPE_UINT && p_to_type == TYPE_FLOAT) {
  2281. if (p_value) {
  2282. p_value->real = p_constant->values[0].uint;
  2283. }
  2284. return true;
  2285. } else if (p_constant->datatype == TYPE_INT && p_to_type == TYPE_UINT) {
  2286. if (p_constant->values[0].sint < 0) {
  2287. return false;
  2288. }
  2289. if (p_value) {
  2290. p_value->uint = p_constant->values[0].sint;
  2291. }
  2292. return true;
  2293. } else if (p_constant->datatype == TYPE_UINT && p_to_type == TYPE_INT) {
  2294. if (p_constant->values[0].uint > 0x7FFFFFFF) {
  2295. return false;
  2296. }
  2297. if (p_value) {
  2298. p_value->sint = p_constant->values[0].uint;
  2299. }
  2300. return true;
  2301. } else {
  2302. return false;
  2303. }
  2304. }
  2305. bool ShaderLanguage::is_scalar_type(DataType p_type) {
  2306. return p_type == TYPE_BOOL || p_type == TYPE_INT || p_type == TYPE_UINT || p_type == TYPE_FLOAT;
  2307. }
  2308. bool ShaderLanguage::is_sampler_type(DataType p_type) {
  2309. return p_type == TYPE_SAMPLER2D ||
  2310. p_type == TYPE_ISAMPLER2D ||
  2311. p_type == TYPE_USAMPLER2D ||
  2312. p_type == TYPE_SAMPLER2DARRAY ||
  2313. p_type == TYPE_ISAMPLER2DARRAY ||
  2314. p_type == TYPE_USAMPLER2DARRAY ||
  2315. p_type == TYPE_SAMPLER3D ||
  2316. p_type == TYPE_ISAMPLER3D ||
  2317. p_type == TYPE_USAMPLER3D ||
  2318. p_type == TYPE_SAMPLERCUBE ||
  2319. p_type == TYPE_SAMPLEREXT;
  2320. }
  2321. Variant ShaderLanguage::constant_value_to_variant(const Vector<ShaderLanguage::ConstantNode::Value> &p_value, DataType p_type, ShaderLanguage::ShaderNode::Uniform::Hint p_hint) {
  2322. if (p_value.size() > 0) {
  2323. Variant value;
  2324. switch (p_type) {
  2325. case ShaderLanguage::TYPE_BOOL:
  2326. value = Variant(p_value[0].boolean);
  2327. break;
  2328. case ShaderLanguage::TYPE_BVEC2:
  2329. case ShaderLanguage::TYPE_BVEC3:
  2330. case ShaderLanguage::TYPE_BVEC4:
  2331. case ShaderLanguage::TYPE_INT:
  2332. value = Variant(p_value[0].sint);
  2333. break;
  2334. case ShaderLanguage::TYPE_IVEC2:
  2335. value = Variant(Vector2(p_value[0].sint, p_value[1].sint));
  2336. break;
  2337. case ShaderLanguage::TYPE_IVEC3:
  2338. value = Variant(Vector3(p_value[0].sint, p_value[1].sint, p_value[2].sint));
  2339. break;
  2340. case ShaderLanguage::TYPE_IVEC4:
  2341. value = Variant(Plane(p_value[0].sint, p_value[1].sint, p_value[2].sint, p_value[3].sint));
  2342. break;
  2343. case ShaderLanguage::TYPE_UINT:
  2344. value = Variant(p_value[0].uint);
  2345. break;
  2346. case ShaderLanguage::TYPE_UVEC2:
  2347. value = Variant(Vector2(p_value[0].uint, p_value[1].uint));
  2348. break;
  2349. case ShaderLanguage::TYPE_UVEC3:
  2350. value = Variant(Vector3(p_value[0].uint, p_value[1].uint, p_value[2].uint));
  2351. break;
  2352. case ShaderLanguage::TYPE_UVEC4:
  2353. value = Variant(Plane(p_value[0].uint, p_value[1].uint, p_value[2].uint, p_value[3].uint));
  2354. break;
  2355. case ShaderLanguage::TYPE_FLOAT:
  2356. value = Variant(p_value[0].real);
  2357. break;
  2358. case ShaderLanguage::TYPE_VEC2:
  2359. value = Variant(Vector2(p_value[0].real, p_value[1].real));
  2360. break;
  2361. case ShaderLanguage::TYPE_VEC3:
  2362. value = Variant(Vector3(p_value[0].real, p_value[1].real, p_value[2].real));
  2363. break;
  2364. case ShaderLanguage::TYPE_VEC4:
  2365. if (p_hint == ShaderLanguage::ShaderNode::Uniform::HINT_COLOR) {
  2366. value = Variant(Color(p_value[0].real, p_value[1].real, p_value[2].real, p_value[3].real));
  2367. } else {
  2368. value = Variant(Plane(p_value[0].real, p_value[1].real, p_value[2].real, p_value[3].real));
  2369. }
  2370. break;
  2371. case ShaderLanguage::TYPE_MAT2:
  2372. value = Variant(Transform2D(p_value[0].real, p_value[2].real, p_value[1].real, p_value[3].real, 0.0, 0.0));
  2373. break;
  2374. case ShaderLanguage::TYPE_MAT3: {
  2375. Basis p;
  2376. p[0][0] = p_value[0].real;
  2377. p[0][1] = p_value[1].real;
  2378. p[0][2] = p_value[2].real;
  2379. p[1][0] = p_value[3].real;
  2380. p[1][1] = p_value[4].real;
  2381. p[1][2] = p_value[5].real;
  2382. p[2][0] = p_value[6].real;
  2383. p[2][1] = p_value[7].real;
  2384. p[2][2] = p_value[8].real;
  2385. value = Variant(p);
  2386. break;
  2387. }
  2388. case ShaderLanguage::TYPE_MAT4: {
  2389. Basis p;
  2390. p[0][0] = p_value[0].real;
  2391. p[0][1] = p_value[1].real;
  2392. p[0][2] = p_value[2].real;
  2393. p[1][0] = p_value[4].real;
  2394. p[1][1] = p_value[5].real;
  2395. p[1][2] = p_value[6].real;
  2396. p[2][0] = p_value[8].real;
  2397. p[2][1] = p_value[9].real;
  2398. p[2][2] = p_value[10].real;
  2399. Transform t = Transform(p, Vector3(p_value[3].real, p_value[7].real, p_value[11].real));
  2400. value = Variant(t);
  2401. break;
  2402. }
  2403. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  2404. case ShaderLanguage::TYPE_ISAMPLER2D:
  2405. case ShaderLanguage::TYPE_ISAMPLER3D:
  2406. case ShaderLanguage::TYPE_SAMPLER2DARRAY:
  2407. case ShaderLanguage::TYPE_SAMPLER2D:
  2408. case ShaderLanguage::TYPE_SAMPLER3D:
  2409. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  2410. case ShaderLanguage::TYPE_USAMPLER2D:
  2411. case ShaderLanguage::TYPE_USAMPLER3D:
  2412. case ShaderLanguage::TYPE_SAMPLERCUBE:
  2413. case ShaderLanguage::TYPE_SAMPLEREXT: {
  2414. // Texture types, likely not relevant here.
  2415. break;
  2416. }
  2417. case ShaderLanguage::TYPE_STRUCT:
  2418. break;
  2419. case ShaderLanguage::TYPE_VOID:
  2420. break;
  2421. default: {
  2422. }
  2423. }
  2424. return value;
  2425. }
  2426. return Variant();
  2427. }
  2428. void ShaderLanguage::get_keyword_list(List<String> *r_keywords) {
  2429. Set<String> kws;
  2430. int idx = 0;
  2431. while (keyword_list[idx].text) {
  2432. kws.insert(keyword_list[idx].text);
  2433. idx++;
  2434. }
  2435. idx = 0;
  2436. while (builtin_func_defs[idx].name) {
  2437. kws.insert(builtin_func_defs[idx].name);
  2438. idx++;
  2439. }
  2440. for (Set<String>::Element *E = kws.front(); E; E = E->next()) {
  2441. r_keywords->push_back(E->get());
  2442. }
  2443. }
  2444. bool ShaderLanguage::is_control_flow_keyword(String p_keyword) {
  2445. return p_keyword == "break" ||
  2446. p_keyword == "case" ||
  2447. p_keyword == "continue" ||
  2448. p_keyword == "default" ||
  2449. p_keyword == "do" ||
  2450. p_keyword == "else" ||
  2451. p_keyword == "for" ||
  2452. p_keyword == "if" ||
  2453. p_keyword == "return" ||
  2454. p_keyword == "switch" ||
  2455. p_keyword == "while";
  2456. }
  2457. void ShaderLanguage::get_builtin_funcs(List<String> *r_keywords) {
  2458. Set<String> kws;
  2459. int idx = 0;
  2460. while (builtin_func_defs[idx].name) {
  2461. kws.insert(builtin_func_defs[idx].name);
  2462. idx++;
  2463. }
  2464. for (Set<String>::Element *E = kws.front(); E; E = E->next()) {
  2465. r_keywords->push_back(E->get());
  2466. }
  2467. }
  2468. ShaderLanguage::DataType ShaderLanguage::get_scalar_type(DataType p_type) {
  2469. static const DataType scalar_types[] = {
  2470. TYPE_VOID,
  2471. TYPE_BOOL,
  2472. TYPE_BOOL,
  2473. TYPE_BOOL,
  2474. TYPE_BOOL,
  2475. TYPE_INT,
  2476. TYPE_INT,
  2477. TYPE_INT,
  2478. TYPE_INT,
  2479. TYPE_UINT,
  2480. TYPE_UINT,
  2481. TYPE_UINT,
  2482. TYPE_UINT,
  2483. TYPE_FLOAT,
  2484. TYPE_FLOAT,
  2485. TYPE_FLOAT,
  2486. TYPE_FLOAT,
  2487. TYPE_FLOAT,
  2488. TYPE_FLOAT,
  2489. TYPE_FLOAT,
  2490. TYPE_FLOAT,
  2491. TYPE_INT,
  2492. TYPE_UINT,
  2493. TYPE_FLOAT,
  2494. TYPE_INT,
  2495. TYPE_UINT,
  2496. TYPE_FLOAT,
  2497. TYPE_INT,
  2498. TYPE_UINT,
  2499. TYPE_FLOAT,
  2500. TYPE_FLOAT,
  2501. TYPE_VOID,
  2502. };
  2503. static_assert(sizeof(scalar_types) / sizeof(*scalar_types) == TYPE_MAX, "get_scalar_type must be updated if DataType is updated");
  2504. return scalar_types[p_type];
  2505. }
  2506. int ShaderLanguage::get_cardinality(DataType p_type) {
  2507. static const int cardinality_table[] = {
  2508. 0,
  2509. 1,
  2510. 2,
  2511. 3,
  2512. 4,
  2513. 1,
  2514. 2,
  2515. 3,
  2516. 4,
  2517. 1,
  2518. 2,
  2519. 3,
  2520. 4,
  2521. 1,
  2522. 2,
  2523. 3,
  2524. 4,
  2525. 4,
  2526. 9,
  2527. 16,
  2528. 1,
  2529. 1,
  2530. 1,
  2531. 1,
  2532. 1,
  2533. 1,
  2534. 1,
  2535. 1,
  2536. 1,
  2537. 1,
  2538. 1,
  2539. 1,
  2540. };
  2541. static_assert(sizeof(cardinality_table) / sizeof(*cardinality_table) == TYPE_MAX, "get_cardinality must be updated if DataType is updated");
  2542. return cardinality_table[p_type];
  2543. }
  2544. bool ShaderLanguage::_get_completable_identifier(BlockNode *p_block, CompletionType p_type, StringName &identifier) {
  2545. identifier = StringName();
  2546. TkPos pos = { 0, 0 };
  2547. Token tk = _get_token();
  2548. if (tk.type == TK_IDENTIFIER) {
  2549. identifier = tk.text;
  2550. pos = _get_tkpos();
  2551. tk = _get_token();
  2552. }
  2553. if (tk.type == TK_CURSOR) {
  2554. completion_type = p_type;
  2555. completion_line = tk_line;
  2556. completion_block = p_block;
  2557. pos = _get_tkpos();
  2558. tk = _get_token();
  2559. if (tk.type == TK_IDENTIFIER) {
  2560. identifier = identifier.operator String() + tk.text.operator String();
  2561. } else {
  2562. _set_tkpos(pos);
  2563. }
  2564. return true;
  2565. } else if (identifier != StringName()) {
  2566. _set_tkpos(pos);
  2567. }
  2568. return false;
  2569. }
  2570. bool ShaderLanguage::_is_operator_assign(Operator p_op) const {
  2571. switch (p_op) {
  2572. case OP_ASSIGN:
  2573. case OP_ASSIGN_ADD:
  2574. case OP_ASSIGN_SUB:
  2575. case OP_ASSIGN_MUL:
  2576. case OP_ASSIGN_DIV:
  2577. case OP_ASSIGN_MOD:
  2578. case OP_ASSIGN_SHIFT_LEFT:
  2579. case OP_ASSIGN_SHIFT_RIGHT:
  2580. case OP_ASSIGN_BIT_AND:
  2581. case OP_ASSIGN_BIT_OR:
  2582. case OP_ASSIGN_BIT_XOR:
  2583. return true;
  2584. default:
  2585. return false;
  2586. }
  2587. return false;
  2588. }
  2589. bool ShaderLanguage::_validate_varying_assign(ShaderNode::Varying &p_varying, String *r_message) {
  2590. if (current_function != String("vertex") && current_function != String("fragment")) {
  2591. *r_message = vformat(RTR("Varying may not be assigned in the '%s' function."), current_function);
  2592. return false;
  2593. }
  2594. switch (p_varying.stage) {
  2595. case ShaderNode::Varying::STAGE_UNKNOWN: // first assign
  2596. if (current_function == String("vertex")) {
  2597. p_varying.stage = ShaderNode::Varying::STAGE_VERTEX;
  2598. } else if (current_function == String("fragment")) {
  2599. p_varying.stage = ShaderNode::Varying::STAGE_FRAGMENT;
  2600. }
  2601. break;
  2602. case ShaderNode::Varying::STAGE_VERTEX_TO_FRAGMENT_LIGHT:
  2603. case ShaderNode::Varying::STAGE_VERTEX:
  2604. if (current_function == String("fragment")) {
  2605. *r_message = RTR("Varyings which were assigned in 'vertex' function may not be reassigned in 'fragment' or 'light'.");
  2606. return false;
  2607. }
  2608. break;
  2609. case ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT:
  2610. case ShaderNode::Varying::STAGE_FRAGMENT:
  2611. if (current_function == String("vertex")) {
  2612. *r_message = RTR("Varyings which were assigned in 'fragment' function may not be reassigned in 'vertex' or 'light'.");
  2613. return false;
  2614. }
  2615. break;
  2616. default:
  2617. break;
  2618. }
  2619. return true;
  2620. }
  2621. bool ShaderLanguage::_validate_assign(Node *p_node, const Map<StringName, BuiltInInfo> &p_builtin_types, String *r_message) {
  2622. if (p_node->type == Node::TYPE_OPERATOR) {
  2623. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  2624. if (op->op == OP_INDEX) {
  2625. return _validate_assign(op->arguments[0], p_builtin_types, r_message);
  2626. } else if (_is_operator_assign(op->op)) {
  2627. //chained assignment
  2628. return _validate_assign(op->arguments[1], p_builtin_types, r_message);
  2629. } else if (op->op == OP_CALL) {
  2630. if (r_message) {
  2631. *r_message = RTR("Assignment to function.");
  2632. }
  2633. return false;
  2634. }
  2635. } else if (p_node->type == Node::TYPE_MEMBER) {
  2636. MemberNode *member = static_cast<MemberNode *>(p_node);
  2637. return _validate_assign(member->owner, p_builtin_types, r_message);
  2638. } else if (p_node->type == Node::TYPE_VARIABLE) {
  2639. VariableNode *var = static_cast<VariableNode *>(p_node);
  2640. if (shader->uniforms.has(var->name)) {
  2641. if (r_message) {
  2642. *r_message = RTR("Assignment to uniform.");
  2643. }
  2644. return false;
  2645. }
  2646. if (shader->constants.has(var->name) || var->is_const) {
  2647. if (r_message) {
  2648. *r_message = RTR("Constants cannot be modified.");
  2649. }
  2650. return false;
  2651. }
  2652. if (!(p_builtin_types.has(var->name) && p_builtin_types[var->name].constant)) {
  2653. return true;
  2654. }
  2655. } else if (p_node->type == Node::TYPE_ARRAY) {
  2656. ArrayNode *arr = static_cast<ArrayNode *>(p_node);
  2657. if (shader->constants.has(arr->name) || arr->is_const) {
  2658. if (r_message) {
  2659. *r_message = RTR("Constants cannot be modified.");
  2660. }
  2661. return false;
  2662. }
  2663. return true;
  2664. }
  2665. if (r_message) {
  2666. *r_message = "Assignment to constant expression.";
  2667. }
  2668. return false;
  2669. }
  2670. Error ShaderLanguage::_parse_array_size(BlockNode *p_block, int *r_array_size) {
  2671. if (r_array_size != nullptr && *r_array_size > 0) {
  2672. _set_error("Array size is already defined!");
  2673. return ERR_PARSE_ERROR;
  2674. }
  2675. TkPos pos = _get_tkpos();
  2676. Token tk = _get_token();
  2677. int array_size = 0;
  2678. if (!tk.is_integer_constant() || ((int)tk.constant) <= 0) {
  2679. _set_tkpos(pos);
  2680. Node *n = _parse_and_reduce_expression(p_block, Map<StringName, BuiltInInfo>());
  2681. if (n) {
  2682. if (n->type == Node::TYPE_VARIABLE) {
  2683. VariableNode *vn = static_cast<VariableNode *>(n);
  2684. if (vn) {
  2685. ConstantNode::Value v;
  2686. DataType data_type;
  2687. bool is_const = false;
  2688. _find_identifier(p_block, Map<StringName, BuiltInInfo>(), vn->name, &data_type, nullptr, &is_const, nullptr, nullptr, &v);
  2689. if (is_const) {
  2690. if (data_type == TYPE_INT) {
  2691. int32_t value = v.sint;
  2692. if (value > 0) {
  2693. array_size = value;
  2694. }
  2695. } else if (data_type == TYPE_UINT) {
  2696. uint32_t value = v.uint;
  2697. if (value > 0U) {
  2698. array_size = value;
  2699. }
  2700. }
  2701. }
  2702. }
  2703. } else if (n->type == Node::TYPE_OPERATOR) {
  2704. _set_error("Array size expressions are not yet implemented.");
  2705. return ERR_PARSE_ERROR;
  2706. }
  2707. }
  2708. } else if (((int)tk.constant) > 0) {
  2709. array_size = (uint32_t)tk.constant;
  2710. }
  2711. if (array_size <= 0) {
  2712. _set_error("Expected single integer constant > 0");
  2713. return ERR_PARSE_ERROR;
  2714. }
  2715. tk = _get_token();
  2716. if (tk.type != TK_BRACKET_CLOSE) {
  2717. _set_error("Expected ']'");
  2718. return ERR_PARSE_ERROR;
  2719. }
  2720. if (r_array_size) {
  2721. *r_array_size = array_size;
  2722. }
  2723. return OK;
  2724. }
  2725. ShaderLanguage::Node *ShaderLanguage::_parse_array_constructor(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, DataType p_type, const StringName &p_struct_name, int p_array_size) {
  2726. DataType type = TYPE_VOID;
  2727. String struct_name = "";
  2728. int array_size = 0;
  2729. bool auto_size = false;
  2730. Token tk = _get_token();
  2731. if (tk.type == TK_CURLY_BRACKET_OPEN) {
  2732. auto_size = true;
  2733. } else {
  2734. if (shader->structs.has(tk.text)) {
  2735. type = TYPE_STRUCT;
  2736. struct_name = tk.text;
  2737. } else {
  2738. if (!is_token_variable_datatype(tk.type)) {
  2739. _set_error("Invalid data type for array");
  2740. return nullptr;
  2741. }
  2742. type = get_token_datatype(tk.type);
  2743. }
  2744. tk = _get_token();
  2745. if (tk.type == TK_BRACKET_OPEN) {
  2746. TkPos pos = _get_tkpos();
  2747. tk = _get_token();
  2748. if (tk.type == TK_BRACKET_CLOSE) {
  2749. array_size = p_array_size;
  2750. tk = _get_token();
  2751. } else {
  2752. _set_tkpos(pos);
  2753. Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
  2754. if (!n || n->type != Node::TYPE_CONSTANT || n->get_datatype() != TYPE_INT) {
  2755. _set_error("Expected single integer constant > 0");
  2756. return nullptr;
  2757. }
  2758. ConstantNode *cnode = (ConstantNode *)n;
  2759. if (cnode->values.size() == 1) {
  2760. array_size = cnode->values[0].sint;
  2761. if (array_size <= 0) {
  2762. _set_error("Expected single integer constant > 0");
  2763. return nullptr;
  2764. }
  2765. } else {
  2766. _set_error("Expected single integer constant > 0");
  2767. return nullptr;
  2768. }
  2769. tk = _get_token();
  2770. if (tk.type != TK_BRACKET_CLOSE) {
  2771. _set_error("Expected ']'");
  2772. return nullptr;
  2773. } else {
  2774. tk = _get_token();
  2775. }
  2776. }
  2777. } else {
  2778. _set_error("Expected '['");
  2779. return nullptr;
  2780. }
  2781. if (type != p_type || struct_name != p_struct_name || array_size != p_array_size) {
  2782. String error_str = "Cannot convert from '";
  2783. if (type == TYPE_STRUCT) {
  2784. error_str += struct_name;
  2785. } else {
  2786. error_str += get_datatype_name(type);
  2787. }
  2788. error_str += "[";
  2789. error_str += itos(array_size);
  2790. error_str += "]'";
  2791. error_str += " to '";
  2792. if (type == TYPE_STRUCT) {
  2793. error_str += p_struct_name;
  2794. } else {
  2795. error_str += get_datatype_name(p_type);
  2796. }
  2797. error_str += "[";
  2798. error_str += itos(p_array_size);
  2799. error_str += "]'";
  2800. _set_error(error_str);
  2801. return nullptr;
  2802. }
  2803. }
  2804. ArrayConstructNode *an = alloc_node<ArrayConstructNode>();
  2805. an->datatype = p_type;
  2806. an->struct_name = p_struct_name;
  2807. if (tk.type == TK_PARENTHESIS_OPEN || auto_size) { // initialization
  2808. while (true) {
  2809. Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
  2810. if (!n) {
  2811. return nullptr;
  2812. }
  2813. if (p_type != n->get_datatype() || p_struct_name != n->get_datatype_name()) {
  2814. _set_error("Invalid assignment of '" + (n->get_datatype() == TYPE_STRUCT ? n->get_datatype_name() : get_datatype_name(n->get_datatype())) + "' to '" + (type == TYPE_STRUCT ? struct_name : get_datatype_name(type)) + "'");
  2815. return nullptr;
  2816. }
  2817. tk = _get_token();
  2818. if (tk.type == TK_COMMA) {
  2819. an->initializer.push_back(n);
  2820. } else if (!auto_size && tk.type == TK_PARENTHESIS_CLOSE) {
  2821. an->initializer.push_back(n);
  2822. break;
  2823. } else if (auto_size && tk.type == TK_CURLY_BRACKET_CLOSE) {
  2824. an->initializer.push_back(n);
  2825. break;
  2826. } else {
  2827. if (auto_size) {
  2828. _set_error("Expected '}' or ','");
  2829. } else {
  2830. _set_error("Expected ')' or ','");
  2831. }
  2832. return nullptr;
  2833. }
  2834. }
  2835. if (an->initializer.size() != p_array_size) {
  2836. _set_error("Array size mismatch");
  2837. return nullptr;
  2838. }
  2839. } else {
  2840. _set_error("Expected array initialization!");
  2841. return nullptr;
  2842. }
  2843. return an;
  2844. }
  2845. ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types) {
  2846. Vector<Expression> expression;
  2847. //Vector<TokenType> operators;
  2848. while (true) {
  2849. Node *expr = nullptr;
  2850. TkPos prepos = _get_tkpos();
  2851. Token tk = _get_token();
  2852. TkPos pos = _get_tkpos();
  2853. if (tk.type == TK_PARENTHESIS_OPEN) {
  2854. //handle subexpression
  2855. expr = _parse_and_reduce_expression(p_block, p_builtin_types);
  2856. if (!expr) {
  2857. return nullptr;
  2858. }
  2859. tk = _get_token();
  2860. if (tk.type != TK_PARENTHESIS_CLOSE) {
  2861. _set_error("Expected ')' in expression");
  2862. return nullptr;
  2863. }
  2864. } else if (tk.type == TK_REAL_CONSTANT) {
  2865. ConstantNode *constant = alloc_node<ConstantNode>();
  2866. ConstantNode::Value v;
  2867. v.real = tk.constant;
  2868. constant->values.push_back(v);
  2869. constant->datatype = TYPE_FLOAT;
  2870. expr = constant;
  2871. } else if (tk.type == TK_INT_CONSTANT) {
  2872. ConstantNode *constant = alloc_node<ConstantNode>();
  2873. ConstantNode::Value v;
  2874. v.sint = tk.constant;
  2875. constant->values.push_back(v);
  2876. constant->datatype = TYPE_INT;
  2877. expr = constant;
  2878. } else if (tk.type == TK_UINT_CONSTANT) {
  2879. ConstantNode *constant = alloc_node<ConstantNode>();
  2880. ConstantNode::Value v;
  2881. v.uint = tk.constant;
  2882. constant->values.push_back(v);
  2883. constant->datatype = TYPE_UINT;
  2884. expr = constant;
  2885. } else if (tk.type == TK_TRUE) {
  2886. //handle true constant
  2887. ConstantNode *constant = alloc_node<ConstantNode>();
  2888. ConstantNode::Value v;
  2889. v.boolean = true;
  2890. constant->values.push_back(v);
  2891. constant->datatype = TYPE_BOOL;
  2892. expr = constant;
  2893. } else if (tk.type == TK_FALSE) {
  2894. //handle false constant
  2895. ConstantNode *constant = alloc_node<ConstantNode>();
  2896. ConstantNode::Value v;
  2897. v.boolean = false;
  2898. constant->values.push_back(v);
  2899. constant->datatype = TYPE_BOOL;
  2900. expr = constant;
  2901. } else if (tk.type == TK_TYPE_VOID) {
  2902. //make sure void is not used in expression
  2903. _set_error("Void value not allowed in Expression");
  2904. return nullptr;
  2905. } else if (is_token_nonvoid_datatype(tk.type)) {
  2906. //basic type constructor
  2907. OperatorNode *func = alloc_node<OperatorNode>();
  2908. func->op = OP_CONSTRUCT;
  2909. if (is_token_precision(tk.type)) {
  2910. func->return_precision_cache = get_token_precision(tk.type);
  2911. tk = _get_token();
  2912. }
  2913. VariableNode *funcname = alloc_node<VariableNode>();
  2914. funcname->name = get_datatype_name(get_token_datatype(tk.type));
  2915. func->arguments.push_back(funcname);
  2916. tk = _get_token();
  2917. if (tk.type != TK_PARENTHESIS_OPEN) {
  2918. _set_error("Expected '(' after type name");
  2919. return nullptr;
  2920. }
  2921. int carg = -1;
  2922. bool ok = _parse_function_arguments(p_block, p_builtin_types, func, &carg);
  2923. if (carg >= 0) {
  2924. completion_type = COMPLETION_CALL_ARGUMENTS;
  2925. completion_line = tk_line;
  2926. completion_block = p_block;
  2927. completion_function = funcname->name;
  2928. completion_argument = carg;
  2929. }
  2930. if (!ok) {
  2931. return nullptr;
  2932. }
  2933. if (!_validate_function_call(p_block, func, &func->return_cache, &func->struct_name)) {
  2934. _set_error("No matching constructor found for: '" + String(funcname->name) + "'");
  2935. return nullptr;
  2936. }
  2937. expr = _reduce_expression(p_block, func);
  2938. } else if (tk.type == TK_IDENTIFIER) {
  2939. _set_tkpos(prepos);
  2940. StringName identifier;
  2941. StructNode *pstruct = nullptr;
  2942. bool struct_init = false;
  2943. _get_completable_identifier(p_block, COMPLETION_IDENTIFIER, identifier);
  2944. if (shader->structs.has(identifier)) {
  2945. pstruct = shader->structs[identifier].shader_struct;
  2946. struct_init = true;
  2947. }
  2948. tk = _get_token();
  2949. if (tk.type == TK_PARENTHESIS_OPEN) {
  2950. if (struct_init) { //a struct constructor
  2951. const StringName &name = identifier;
  2952. OperatorNode *func = alloc_node<OperatorNode>();
  2953. func->op = OP_STRUCT;
  2954. func->struct_name = name;
  2955. func->return_cache = TYPE_STRUCT;
  2956. VariableNode *funcname = alloc_node<VariableNode>();
  2957. funcname->name = name;
  2958. func->arguments.push_back(funcname);
  2959. for (int i = 0; i < pstruct->members.size(); i++) {
  2960. Node *nexpr;
  2961. if (pstruct->members[i]->array_size != 0) {
  2962. nexpr = _parse_array_constructor(p_block, p_builtin_types, pstruct->members[i]->get_datatype(), pstruct->members[i]->struct_name, pstruct->members[i]->array_size);
  2963. if (!nexpr) {
  2964. return nullptr;
  2965. }
  2966. } else {
  2967. nexpr = _parse_and_reduce_expression(p_block, p_builtin_types);
  2968. if (!nexpr) {
  2969. return nullptr;
  2970. }
  2971. Node *node = pstruct->members[i];
  2972. if (!_compare_datatypes_in_nodes(pstruct->members[i], nexpr)) {
  2973. String type_name = nexpr->get_datatype() == TYPE_STRUCT ? nexpr->get_datatype_name() : get_datatype_name(nexpr->get_datatype());
  2974. String type_name2 = node->get_datatype() == TYPE_STRUCT ? node->get_datatype_name() : get_datatype_name(node->get_datatype());
  2975. _set_error("Invalid assignment of '" + type_name + "' to '" + type_name2 + "'");
  2976. return nullptr;
  2977. }
  2978. }
  2979. if (i + 1 < pstruct->members.size()) {
  2980. tk = _get_token();
  2981. if (tk.type != TK_COMMA) {
  2982. _set_error("Expected ','");
  2983. return nullptr;
  2984. }
  2985. }
  2986. func->arguments.push_back(nexpr);
  2987. }
  2988. tk = _get_token();
  2989. if (tk.type != TK_PARENTHESIS_CLOSE) {
  2990. _set_error("Expected ')'");
  2991. return nullptr;
  2992. }
  2993. expr = func;
  2994. } else { //a function
  2995. const StringName &name = identifier;
  2996. OperatorNode *func = alloc_node<OperatorNode>();
  2997. func->op = OP_CALL;
  2998. VariableNode *funcname = alloc_node<VariableNode>();
  2999. funcname->name = name;
  3000. func->arguments.push_back(funcname);
  3001. int carg = -1;
  3002. bool ok = _parse_function_arguments(p_block, p_builtin_types, func, &carg);
  3003. // Check if block has a variable with the same name as function to prevent shader crash.
  3004. ShaderLanguage::BlockNode *bnode = p_block;
  3005. while (bnode) {
  3006. if (bnode->variables.has(name)) {
  3007. _set_error("Expected function name");
  3008. return nullptr;
  3009. }
  3010. bnode = bnode->parent_block;
  3011. }
  3012. int function_index = -1;
  3013. //test if function was parsed first
  3014. for (int i = 0; i < shader->functions.size(); i++) {
  3015. if (shader->functions[i].name == name) {
  3016. //add to current function as dependency
  3017. for (int j = 0; j < shader->functions.size(); j++) {
  3018. if (shader->functions[j].name == current_function) {
  3019. shader->functions.write[j].uses_function.push_back(name);
  3020. break;
  3021. }
  3022. }
  3023. function_index = i;
  3024. break;
  3025. }
  3026. }
  3027. if (carg >= 0) {
  3028. completion_type = COMPLETION_CALL_ARGUMENTS;
  3029. completion_line = tk_line;
  3030. completion_block = p_block;
  3031. completion_function = funcname->name;
  3032. completion_argument = carg;
  3033. }
  3034. if (!ok) {
  3035. return nullptr;
  3036. }
  3037. if (!_validate_function_call(p_block, func, &func->return_cache, &func->struct_name)) {
  3038. _set_error("No matching function found for: '" + String(funcname->name) + "'");
  3039. return nullptr;
  3040. }
  3041. completion_class = TAG_GLOBAL; // reset sub-class
  3042. if (function_index >= 0) {
  3043. FunctionNode *call_function = shader->functions[function_index].function;
  3044. if (call_function) {
  3045. for (int i = 0; i < call_function->arguments.size(); i++) {
  3046. int argidx = i + 1;
  3047. if (argidx < func->arguments.size()) {
  3048. bool error = false;
  3049. Node *n = func->arguments[argidx];
  3050. ArgumentQualifier arg_qual = call_function->arguments[i].qualifier;
  3051. bool is_out_arg = arg_qual != ArgumentQualifier::ARGUMENT_QUALIFIER_IN;
  3052. if (n->type == Node::TYPE_VARIABLE || n->type == Node::TYPE_ARRAY) {
  3053. StringName varname;
  3054. if (n->type == Node::TYPE_VARIABLE) {
  3055. VariableNode *vn = static_cast<VariableNode *>(n);
  3056. varname = vn->name;
  3057. } else { // TYPE_ARRAY
  3058. ArrayNode *an = static_cast<ArrayNode *>(n);
  3059. varname = an->name;
  3060. }
  3061. if (shader->varyings.has(varname)) {
  3062. switch (shader->varyings[varname].stage) {
  3063. case ShaderNode::Varying::STAGE_UNKNOWN: {
  3064. _set_error(vformat("Varying '%s' must be assigned in the vertex or fragment function first!", varname));
  3065. return nullptr;
  3066. }
  3067. case ShaderNode::Varying::STAGE_VERTEX_TO_FRAGMENT_LIGHT:
  3068. FALLTHROUGH;
  3069. case ShaderNode::Varying::STAGE_VERTEX:
  3070. if (is_out_arg && current_function != varying_function_names.vertex) { // inout/out
  3071. error = true;
  3072. }
  3073. break;
  3074. case ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT:
  3075. FALLTHROUGH;
  3076. case ShaderNode::Varying::STAGE_FRAGMENT:
  3077. if (!is_out_arg) {
  3078. if (current_function != varying_function_names.fragment && current_function != varying_function_names.light) {
  3079. error = true;
  3080. }
  3081. } else if (current_function != varying_function_names.fragment) { // inout/out
  3082. error = true;
  3083. }
  3084. break;
  3085. default:
  3086. break;
  3087. }
  3088. if (error) {
  3089. _set_error(vformat("Varying '%s' cannot be passed for the '%s' parameter in that context!", varname, _get_qualifier_str(arg_qual)));
  3090. return nullptr;
  3091. }
  3092. }
  3093. }
  3094. bool is_const_arg = call_function->arguments[i].is_const;
  3095. if (is_const_arg || is_out_arg) {
  3096. StringName varname;
  3097. if (n->type == Node::TYPE_CONSTANT || n->type == Node::TYPE_OPERATOR) {
  3098. if (!is_const_arg) {
  3099. error = true;
  3100. }
  3101. } else if (n->type == Node::TYPE_ARRAY) {
  3102. ArrayNode *an = static_cast<ArrayNode *>(n);
  3103. if (!is_const_arg && (an->call_expression != nullptr || an->is_const)) {
  3104. error = true;
  3105. }
  3106. varname = an->name;
  3107. } else if (n->type == Node::TYPE_VARIABLE) {
  3108. VariableNode *vn = static_cast<VariableNode *>(n);
  3109. if (vn->is_const && !is_const_arg) {
  3110. error = true;
  3111. }
  3112. varname = vn->name;
  3113. } else if (n->type == Node::TYPE_MEMBER) {
  3114. MemberNode *mn = static_cast<MemberNode *>(n);
  3115. if (mn->basetype_const && is_out_arg) {
  3116. error = true;
  3117. }
  3118. }
  3119. if (!error && varname != StringName()) {
  3120. if (shader->constants.has(varname)) {
  3121. error = true;
  3122. } else if (shader->uniforms.has(varname)) {
  3123. error = true;
  3124. } else if (p_builtin_types.has(varname)) {
  3125. BuiltInInfo info = p_builtin_types[varname];
  3126. if (info.constant) {
  3127. error = true;
  3128. }
  3129. }
  3130. }
  3131. if (error) {
  3132. _set_error(vformat("Constant value cannot be passed for '%s' parameter!", _get_qualifier_str(arg_qual)));
  3133. return nullptr;
  3134. }
  3135. }
  3136. } else {
  3137. break;
  3138. }
  3139. }
  3140. }
  3141. }
  3142. expr = func;
  3143. }
  3144. } else {
  3145. //an identifier
  3146. last_const = false;
  3147. _set_tkpos(pos);
  3148. DataType data_type;
  3149. IdentifierType ident_type;
  3150. bool is_const = false;
  3151. int array_size = 0;
  3152. StringName struct_name;
  3153. bool is_local = false;
  3154. if (p_block && p_block->block_tag != SubClassTag::TAG_GLOBAL) {
  3155. int idx = 0;
  3156. bool found = false;
  3157. while (builtin_func_defs[idx].name) {
  3158. if (builtin_func_defs[idx].tag == p_block->block_tag && builtin_func_defs[idx].name == identifier) {
  3159. found = true;
  3160. break;
  3161. }
  3162. idx++;
  3163. }
  3164. if (!found) {
  3165. _set_error("Unknown identifier in expression: " + String(identifier));
  3166. return nullptr;
  3167. }
  3168. } else {
  3169. if (!_find_identifier(p_block, p_builtin_types, identifier, &data_type, &ident_type, &is_const, &array_size, &struct_name)) {
  3170. _set_error("Unknown identifier in expression: " + String(identifier));
  3171. return nullptr;
  3172. }
  3173. last_const = is_const;
  3174. if (ident_type == IDENTIFIER_VARYING) {
  3175. TkPos prev_pos = _get_tkpos();
  3176. Token next_token = _get_token();
  3177. // An array of varyings.
  3178. if (next_token.type == TK_BRACKET_OPEN) {
  3179. _get_token(); // Pass constant.
  3180. _get_token(); // Pass TK_BRACKET_CLOSE.
  3181. next_token = _get_token();
  3182. }
  3183. _set_tkpos(prev_pos);
  3184. String error;
  3185. if (is_token_operator_assign(next_token.type)) {
  3186. if (!_validate_varying_assign(shader->varyings[identifier], &error)) {
  3187. _set_error(error);
  3188. return nullptr;
  3189. }
  3190. } else {
  3191. ShaderNode::Varying &var = shader->varyings[identifier];
  3192. switch (var.stage) {
  3193. case ShaderNode::Varying::STAGE_VERTEX:
  3194. if (current_function == varying_function_names.fragment || current_function == varying_function_names.light) {
  3195. var.stage = ShaderNode::Varying::STAGE_VERTEX_TO_FRAGMENT_LIGHT;
  3196. }
  3197. break;
  3198. case ShaderNode::Varying::STAGE_FRAGMENT:
  3199. if (current_function == varying_function_names.light) {
  3200. var.stage = ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT;
  3201. }
  3202. break;
  3203. default:
  3204. break;
  3205. }
  3206. }
  3207. }
  3208. if (ident_type == IDENTIFIER_FUNCTION) {
  3209. _set_error("Can't use function as identifier: " + String(identifier));
  3210. return nullptr;
  3211. }
  3212. is_local = ident_type == IDENTIFIER_LOCAL_VAR || ident_type == IDENTIFIER_FUNCTION_ARGUMENT;
  3213. }
  3214. Node *index_expression = nullptr;
  3215. Node *call_expression = nullptr;
  3216. Node *assign_expression = nullptr;
  3217. if (array_size > 0) {
  3218. tk = _get_token();
  3219. if (tk.type != TK_BRACKET_OPEN && tk.type != TK_PERIOD && tk.type != TK_OP_ASSIGN) {
  3220. _set_error("Expected '[','.' or '='");
  3221. return nullptr;
  3222. }
  3223. if (tk.type == TK_OP_ASSIGN) {
  3224. if (is_const) {
  3225. _set_error("Constants cannot be modified.");
  3226. return nullptr;
  3227. }
  3228. if (shader->varyings.has(identifier) && current_function != String("vertex")) {
  3229. _set_error("Varyings can only be assigned in vertex function.");
  3230. return nullptr;
  3231. }
  3232. assign_expression = _parse_array_constructor(p_block, p_builtin_types, data_type, struct_name, array_size);
  3233. if (!assign_expression) {
  3234. return nullptr;
  3235. }
  3236. } else if (tk.type == TK_PERIOD) {
  3237. completion_class = TAG_ARRAY;
  3238. if (p_block != nullptr) {
  3239. p_block->block_tag = SubClassTag::TAG_ARRAY;
  3240. }
  3241. call_expression = _parse_and_reduce_expression(p_block, p_builtin_types);
  3242. if (p_block != nullptr) {
  3243. p_block->block_tag = SubClassTag::TAG_GLOBAL;
  3244. }
  3245. if (!call_expression) {
  3246. return nullptr;
  3247. }
  3248. data_type = call_expression->get_datatype();
  3249. } else { // indexing
  3250. index_expression = _parse_and_reduce_expression(p_block, p_builtin_types);
  3251. if (!index_expression) {
  3252. return nullptr;
  3253. }
  3254. if (index_expression->get_datatype() != TYPE_INT && index_expression->get_datatype() != TYPE_UINT) {
  3255. _set_error("Only integer expressions are allowed for indexing");
  3256. return nullptr;
  3257. }
  3258. if (index_expression->type == Node::TYPE_CONSTANT) {
  3259. ConstantNode *cnode = (ConstantNode *)index_expression;
  3260. if (cnode) {
  3261. if (!cnode->values.empty()) {
  3262. int value = cnode->values[0].sint;
  3263. if (value < 0 || value >= array_size) {
  3264. _set_error(vformat("Index [%s] out of range [%s..%s]", value, 0, array_size - 1));
  3265. return nullptr;
  3266. }
  3267. }
  3268. }
  3269. }
  3270. tk = _get_token();
  3271. if (tk.type != TK_BRACKET_CLOSE) {
  3272. _set_error("Expected ']'");
  3273. return nullptr;
  3274. }
  3275. }
  3276. ArrayNode *arrname = alloc_node<ArrayNode>();
  3277. arrname->name = identifier;
  3278. arrname->datatype_cache = data_type;
  3279. arrname->struct_name = struct_name;
  3280. arrname->index_expression = index_expression;
  3281. arrname->call_expression = call_expression;
  3282. arrname->assign_expression = assign_expression;
  3283. arrname->is_const = is_const;
  3284. arrname->is_local = is_local;
  3285. expr = arrname;
  3286. } else {
  3287. VariableNode *varname = alloc_node<VariableNode>();
  3288. varname->name = identifier;
  3289. varname->datatype_cache = data_type;
  3290. varname->is_const = is_const;
  3291. varname->struct_name = struct_name;
  3292. varname->is_local = is_local;
  3293. expr = varname;
  3294. }
  3295. }
  3296. } else if (tk.type == TK_OP_ADD) {
  3297. continue; //this one does nothing
  3298. } 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) {
  3299. Expression e;
  3300. e.is_op = true;
  3301. switch (tk.type) {
  3302. case TK_OP_SUB:
  3303. e.op = OP_NEGATE;
  3304. break;
  3305. case TK_OP_NOT:
  3306. e.op = OP_NOT;
  3307. break;
  3308. case TK_OP_BIT_INVERT:
  3309. e.op = OP_BIT_INVERT;
  3310. break;
  3311. case TK_OP_INCREMENT:
  3312. e.op = OP_INCREMENT;
  3313. break;
  3314. case TK_OP_DECREMENT:
  3315. e.op = OP_DECREMENT;
  3316. break;
  3317. default:
  3318. ERR_FAIL_V(nullptr);
  3319. }
  3320. expression.push_back(e);
  3321. continue;
  3322. } else {
  3323. _set_error("Expected expression, found: " + get_token_text(tk));
  3324. return nullptr;
  3325. //nothing
  3326. }
  3327. ERR_FAIL_COND_V(!expr, nullptr);
  3328. /* OK now see what's NEXT to the operator.. */
  3329. /* OK now see what's NEXT to the operator.. */
  3330. /* OK now see what's NEXT to the operator.. */
  3331. while (true) {
  3332. TkPos pos2 = _get_tkpos();
  3333. tk = _get_token();
  3334. if (tk.type == TK_CURSOR) {
  3335. //do nothing
  3336. } else if (tk.type == TK_PERIOD) {
  3337. DataType dt = expr->get_datatype();
  3338. String st = expr->get_datatype_name();
  3339. StringName identifier;
  3340. if (_get_completable_identifier(p_block, dt == TYPE_STRUCT ? COMPLETION_STRUCT : COMPLETION_INDEX, identifier)) {
  3341. if (dt == TYPE_STRUCT) {
  3342. completion_struct = st;
  3343. } else {
  3344. completion_base = dt;
  3345. }
  3346. }
  3347. if (identifier == StringName()) {
  3348. _set_error("Expected identifier as member");
  3349. return nullptr;
  3350. }
  3351. String ident = identifier;
  3352. bool ok = true;
  3353. DataType member_type = TYPE_VOID;
  3354. StringName member_struct_name = "";
  3355. int array_size = 0;
  3356. switch (dt) {
  3357. case TYPE_STRUCT: {
  3358. ok = false;
  3359. String member_name = String(ident.ptr());
  3360. if (shader->structs.has(st)) {
  3361. StructNode *n = shader->structs[st].shader_struct;
  3362. for (List<MemberNode *>::Element *E = n->members.front(); E; E = E->next()) {
  3363. if (String(E->get()->name) == member_name) {
  3364. member_type = E->get()->datatype;
  3365. array_size = E->get()->array_size;
  3366. if (member_type == TYPE_STRUCT) {
  3367. member_struct_name = E->get()->struct_name;
  3368. }
  3369. ok = true;
  3370. break;
  3371. }
  3372. }
  3373. }
  3374. } break;
  3375. case TYPE_BVEC2:
  3376. case TYPE_IVEC2:
  3377. case TYPE_UVEC2:
  3378. case TYPE_VEC2: {
  3379. int l = ident.length();
  3380. if (l == 1) {
  3381. member_type = DataType(dt - 1);
  3382. } else if (l == 2) {
  3383. member_type = dt;
  3384. } else if (l == 3) {
  3385. member_type = DataType(dt + 1);
  3386. } else if (l == 4) {
  3387. member_type = DataType(dt + 2);
  3388. } else {
  3389. ok = false;
  3390. break;
  3391. }
  3392. const CharType *c = ident.ptr();
  3393. for (int i = 0; i < l; i++) {
  3394. switch (c[i]) {
  3395. case 'r':
  3396. case 'g':
  3397. case 'x':
  3398. case 'y':
  3399. break;
  3400. default:
  3401. ok = false;
  3402. break;
  3403. }
  3404. }
  3405. } break;
  3406. case TYPE_BVEC3:
  3407. case TYPE_IVEC3:
  3408. case TYPE_UVEC3:
  3409. case TYPE_VEC3: {
  3410. int l = ident.length();
  3411. if (l == 1) {
  3412. member_type = DataType(dt - 2);
  3413. } else if (l == 2) {
  3414. member_type = DataType(dt - 1);
  3415. } else if (l == 3) {
  3416. member_type = dt;
  3417. } else if (l == 4) {
  3418. member_type = DataType(dt + 1);
  3419. } else {
  3420. ok = false;
  3421. break;
  3422. }
  3423. const CharType *c = ident.ptr();
  3424. for (int i = 0; i < l; i++) {
  3425. switch (c[i]) {
  3426. case 'r':
  3427. case 'g':
  3428. case 'b':
  3429. case 'x':
  3430. case 'y':
  3431. case 'z':
  3432. break;
  3433. default:
  3434. ok = false;
  3435. break;
  3436. }
  3437. }
  3438. } break;
  3439. case TYPE_BVEC4:
  3440. case TYPE_IVEC4:
  3441. case TYPE_UVEC4:
  3442. case TYPE_VEC4: {
  3443. int l = ident.length();
  3444. if (l == 1) {
  3445. member_type = DataType(dt - 3);
  3446. } else if (l == 2) {
  3447. member_type = DataType(dt - 2);
  3448. } else if (l == 3) {
  3449. member_type = DataType(dt - 1);
  3450. } else if (l == 4) {
  3451. member_type = dt;
  3452. } else {
  3453. ok = false;
  3454. break;
  3455. }
  3456. const CharType *c = ident.ptr();
  3457. for (int i = 0; i < l; i++) {
  3458. switch (c[i]) {
  3459. case 'r':
  3460. case 'g':
  3461. case 'b':
  3462. case 'a':
  3463. case 'x':
  3464. case 'y':
  3465. case 'z':
  3466. case 'w':
  3467. break;
  3468. default:
  3469. ok = false;
  3470. break;
  3471. }
  3472. }
  3473. } break;
  3474. default: {
  3475. ok = false;
  3476. }
  3477. }
  3478. if (!ok) {
  3479. _set_error("Invalid member for " + (dt == TYPE_STRUCT ? st : get_datatype_name(dt)) + " expression: ." + ident);
  3480. return nullptr;
  3481. }
  3482. MemberNode *mn = alloc_node<MemberNode>();
  3483. mn->basetype = dt;
  3484. mn->basetype_const = last_const;
  3485. mn->datatype = member_type;
  3486. mn->base_struct_name = st;
  3487. mn->struct_name = member_struct_name;
  3488. mn->array_size = array_size;
  3489. mn->name = ident;
  3490. mn->owner = expr;
  3491. if (array_size > 0) {
  3492. tk = _get_token();
  3493. if (tk.type == TK_OP_ASSIGN) {
  3494. if (last_const) {
  3495. last_const = false;
  3496. _set_error("Constants cannot be modified.");
  3497. return nullptr;
  3498. }
  3499. Node *assign_expression = _parse_array_constructor(p_block, p_builtin_types, member_type, member_struct_name, array_size);
  3500. if (!assign_expression) {
  3501. return nullptr;
  3502. }
  3503. mn->assign_expression = assign_expression;
  3504. } else if (tk.type == TK_PERIOD) {
  3505. _set_error("Nested array length() is not yet implemented");
  3506. return nullptr;
  3507. } else if (tk.type == TK_BRACKET_OPEN) {
  3508. Node *index_expression = _parse_and_reduce_expression(p_block, p_builtin_types);
  3509. if (!index_expression) {
  3510. return nullptr;
  3511. }
  3512. if (index_expression->get_datatype() != TYPE_INT && index_expression->get_datatype() != TYPE_UINT) {
  3513. _set_error("Only integer expressions are allowed for indexing");
  3514. return nullptr;
  3515. }
  3516. if (index_expression->type == Node::TYPE_CONSTANT) {
  3517. ConstantNode *cnode = (ConstantNode *)index_expression;
  3518. if (cnode) {
  3519. if (!cnode->values.empty()) {
  3520. int value = cnode->values[0].sint;
  3521. if (value < 0 || value >= array_size) {
  3522. _set_error(vformat("Index [%s] out of range [%s..%s]", value, 0, array_size - 1));
  3523. return nullptr;
  3524. }
  3525. }
  3526. }
  3527. }
  3528. tk = _get_token();
  3529. if (tk.type != TK_BRACKET_CLOSE) {
  3530. _set_error("Expected ']'");
  3531. return nullptr;
  3532. }
  3533. mn->index_expression = index_expression;
  3534. } else {
  3535. _set_error("Expected '[','.' or '='");
  3536. return nullptr;
  3537. }
  3538. }
  3539. expr = mn;
  3540. //todo
  3541. //member (period) has priority over any operator
  3542. //creates a subindexing expression in place
  3543. /*} else if (tk.type==TK_BRACKET_OPEN) {
  3544. //todo
  3545. //subindexing has priority over any operator
  3546. //creates a subindexing expression in place
  3547. */
  3548. } else if (tk.type == TK_BRACKET_OPEN) {
  3549. Node *index = _parse_and_reduce_expression(p_block, p_builtin_types);
  3550. if (!index) {
  3551. return nullptr;
  3552. }
  3553. if (index->get_datatype() != TYPE_INT && index->get_datatype() != TYPE_UINT) {
  3554. _set_error("Only integer datatypes are allowed for indexing");
  3555. return nullptr;
  3556. }
  3557. DataType member_type = TYPE_VOID;
  3558. switch (expr->get_datatype()) {
  3559. case TYPE_BVEC2:
  3560. case TYPE_VEC2:
  3561. case TYPE_IVEC2:
  3562. case TYPE_UVEC2:
  3563. case TYPE_MAT2:
  3564. if (index->type == Node::TYPE_CONSTANT) {
  3565. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  3566. if (index_constant >= 2) {
  3567. _set_error("Index out of range (0-1)");
  3568. return nullptr;
  3569. }
  3570. } else {
  3571. _set_error("Only integer constants are allowed as index at the moment");
  3572. return nullptr;
  3573. }
  3574. switch (expr->get_datatype()) {
  3575. case TYPE_BVEC2:
  3576. member_type = TYPE_BOOL;
  3577. break;
  3578. case TYPE_VEC2:
  3579. member_type = TYPE_FLOAT;
  3580. break;
  3581. case TYPE_IVEC2:
  3582. member_type = TYPE_INT;
  3583. break;
  3584. case TYPE_UVEC2:
  3585. member_type = TYPE_UINT;
  3586. break;
  3587. case TYPE_MAT2:
  3588. member_type = TYPE_VEC2;
  3589. break;
  3590. default:
  3591. break;
  3592. }
  3593. break;
  3594. case TYPE_BVEC3:
  3595. case TYPE_VEC3:
  3596. case TYPE_IVEC3:
  3597. case TYPE_UVEC3:
  3598. case TYPE_MAT3:
  3599. if (index->type == Node::TYPE_CONSTANT) {
  3600. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  3601. if (index_constant >= 3) {
  3602. _set_error("Index out of range (0-2)");
  3603. return nullptr;
  3604. }
  3605. } else {
  3606. _set_error("Only integer constants are allowed as index at the moment");
  3607. return nullptr;
  3608. }
  3609. switch (expr->get_datatype()) {
  3610. case TYPE_BVEC3:
  3611. member_type = TYPE_BOOL;
  3612. break;
  3613. case TYPE_VEC3:
  3614. member_type = TYPE_FLOAT;
  3615. break;
  3616. case TYPE_IVEC3:
  3617. member_type = TYPE_INT;
  3618. break;
  3619. case TYPE_UVEC3:
  3620. member_type = TYPE_UINT;
  3621. break;
  3622. case TYPE_MAT3:
  3623. member_type = TYPE_VEC3;
  3624. break;
  3625. default:
  3626. break;
  3627. }
  3628. break;
  3629. case TYPE_BVEC4:
  3630. case TYPE_VEC4:
  3631. case TYPE_IVEC4:
  3632. case TYPE_UVEC4:
  3633. case TYPE_MAT4:
  3634. if (index->type == Node::TYPE_CONSTANT) {
  3635. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  3636. if (index_constant >= 4) {
  3637. _set_error("Index out of range (0-3)");
  3638. return nullptr;
  3639. }
  3640. } else {
  3641. _set_error("Only integer constants are allowed as index at the moment");
  3642. return nullptr;
  3643. }
  3644. switch (expr->get_datatype()) {
  3645. case TYPE_BVEC4:
  3646. member_type = TYPE_BOOL;
  3647. break;
  3648. case TYPE_VEC4:
  3649. member_type = TYPE_FLOAT;
  3650. break;
  3651. case TYPE_IVEC4:
  3652. member_type = TYPE_INT;
  3653. break;
  3654. case TYPE_UVEC4:
  3655. member_type = TYPE_UINT;
  3656. break;
  3657. case TYPE_MAT4:
  3658. member_type = TYPE_VEC4;
  3659. break;
  3660. default:
  3661. break;
  3662. }
  3663. break;
  3664. default: {
  3665. _set_error("Object of type '" + (expr->get_datatype() == TYPE_STRUCT ? expr->get_datatype_name() : get_datatype_name(expr->get_datatype())) + "' can't be indexed");
  3666. return nullptr;
  3667. }
  3668. }
  3669. OperatorNode *op = alloc_node<OperatorNode>();
  3670. op->op = OP_INDEX;
  3671. op->return_cache = member_type;
  3672. op->arguments.push_back(expr);
  3673. op->arguments.push_back(index);
  3674. expr = op;
  3675. tk = _get_token();
  3676. if (tk.type != TK_BRACKET_CLOSE) {
  3677. _set_error("Expected ']' after indexing expression");
  3678. return nullptr;
  3679. }
  3680. } else if (tk.type == TK_OP_INCREMENT || tk.type == TK_OP_DECREMENT) {
  3681. OperatorNode *op = alloc_node<OperatorNode>();
  3682. op->op = tk.type == TK_OP_DECREMENT ? OP_POST_DECREMENT : OP_POST_INCREMENT;
  3683. op->arguments.push_back(expr);
  3684. if (!_validate_operator(op, &op->return_cache)) {
  3685. _set_error("Invalid base type for increment/decrement operator");
  3686. return nullptr;
  3687. }
  3688. if (!_validate_assign(expr, p_builtin_types)) {
  3689. _set_error("Invalid use of increment/decrement operator in constant expression.");
  3690. return nullptr;
  3691. }
  3692. expr = op;
  3693. } else {
  3694. _set_tkpos(pos2);
  3695. break;
  3696. }
  3697. }
  3698. Expression e;
  3699. e.is_op = false;
  3700. e.node = expr;
  3701. expression.push_back(e);
  3702. pos = _get_tkpos();
  3703. tk = _get_token();
  3704. if (is_token_operator(tk.type)) {
  3705. Expression o;
  3706. o.is_op = true;
  3707. switch (tk.type) {
  3708. case TK_OP_EQUAL:
  3709. o.op = OP_EQUAL;
  3710. break;
  3711. case TK_OP_NOT_EQUAL:
  3712. o.op = OP_NOT_EQUAL;
  3713. break;
  3714. case TK_OP_LESS:
  3715. o.op = OP_LESS;
  3716. break;
  3717. case TK_OP_LESS_EQUAL:
  3718. o.op = OP_LESS_EQUAL;
  3719. break;
  3720. case TK_OP_GREATER:
  3721. o.op = OP_GREATER;
  3722. break;
  3723. case TK_OP_GREATER_EQUAL:
  3724. o.op = OP_GREATER_EQUAL;
  3725. break;
  3726. case TK_OP_AND:
  3727. o.op = OP_AND;
  3728. break;
  3729. case TK_OP_OR:
  3730. o.op = OP_OR;
  3731. break;
  3732. case TK_OP_ADD:
  3733. o.op = OP_ADD;
  3734. break;
  3735. case TK_OP_SUB:
  3736. o.op = OP_SUB;
  3737. break;
  3738. case TK_OP_MUL:
  3739. o.op = OP_MUL;
  3740. break;
  3741. case TK_OP_DIV:
  3742. o.op = OP_DIV;
  3743. break;
  3744. case TK_OP_MOD:
  3745. o.op = OP_MOD;
  3746. break;
  3747. case TK_OP_SHIFT_LEFT:
  3748. o.op = OP_SHIFT_LEFT;
  3749. break;
  3750. case TK_OP_SHIFT_RIGHT:
  3751. o.op = OP_SHIFT_RIGHT;
  3752. break;
  3753. case TK_OP_ASSIGN:
  3754. o.op = OP_ASSIGN;
  3755. break;
  3756. case TK_OP_ASSIGN_ADD:
  3757. o.op = OP_ASSIGN_ADD;
  3758. break;
  3759. case TK_OP_ASSIGN_SUB:
  3760. o.op = OP_ASSIGN_SUB;
  3761. break;
  3762. case TK_OP_ASSIGN_MUL:
  3763. o.op = OP_ASSIGN_MUL;
  3764. break;
  3765. case TK_OP_ASSIGN_DIV:
  3766. o.op = OP_ASSIGN_DIV;
  3767. break;
  3768. case TK_OP_ASSIGN_MOD:
  3769. o.op = OP_ASSIGN_MOD;
  3770. break;
  3771. case TK_OP_ASSIGN_SHIFT_LEFT:
  3772. o.op = OP_ASSIGN_SHIFT_LEFT;
  3773. break;
  3774. case TK_OP_ASSIGN_SHIFT_RIGHT:
  3775. o.op = OP_ASSIGN_SHIFT_RIGHT;
  3776. break;
  3777. case TK_OP_ASSIGN_BIT_AND:
  3778. o.op = OP_ASSIGN_BIT_AND;
  3779. break;
  3780. case TK_OP_ASSIGN_BIT_OR:
  3781. o.op = OP_ASSIGN_BIT_OR;
  3782. break;
  3783. case TK_OP_ASSIGN_BIT_XOR:
  3784. o.op = OP_ASSIGN_BIT_XOR;
  3785. break;
  3786. case TK_OP_BIT_AND:
  3787. o.op = OP_BIT_AND;
  3788. break;
  3789. case TK_OP_BIT_OR:
  3790. o.op = OP_BIT_OR;
  3791. break;
  3792. case TK_OP_BIT_XOR:
  3793. o.op = OP_BIT_XOR;
  3794. break;
  3795. case TK_QUESTION:
  3796. o.op = OP_SELECT_IF;
  3797. break;
  3798. case TK_COLON:
  3799. o.op = OP_SELECT_ELSE;
  3800. break;
  3801. default: {
  3802. _set_error("Invalid token for operator: " + get_token_text(tk));
  3803. return nullptr;
  3804. }
  3805. }
  3806. expression.push_back(o);
  3807. } else {
  3808. _set_tkpos(pos); //something else, so rollback and end
  3809. break;
  3810. }
  3811. }
  3812. /* Reduce the set set of expressions and place them in an operator tree, respecting precedence */
  3813. while (expression.size() > 1) {
  3814. int next_op = -1;
  3815. int min_priority = 0xFFFFF;
  3816. bool is_unary = false;
  3817. bool is_ternary = false;
  3818. for (int i = 0; i < expression.size(); i++) {
  3819. if (!expression[i].is_op) {
  3820. continue;
  3821. }
  3822. bool unary = false;
  3823. bool ternary = false;
  3824. int priority;
  3825. switch (expression[i].op) {
  3826. case OP_EQUAL:
  3827. priority = 8;
  3828. break;
  3829. case OP_NOT_EQUAL:
  3830. priority = 8;
  3831. break;
  3832. case OP_LESS:
  3833. priority = 7;
  3834. break;
  3835. case OP_LESS_EQUAL:
  3836. priority = 7;
  3837. break;
  3838. case OP_GREATER:
  3839. priority = 7;
  3840. break;
  3841. case OP_GREATER_EQUAL:
  3842. priority = 7;
  3843. break;
  3844. case OP_AND:
  3845. priority = 12;
  3846. break;
  3847. case OP_OR:
  3848. priority = 14;
  3849. break;
  3850. case OP_NOT:
  3851. priority = 3;
  3852. unary = true;
  3853. break;
  3854. case OP_NEGATE:
  3855. priority = 3;
  3856. unary = true;
  3857. break;
  3858. case OP_ADD:
  3859. priority = 5;
  3860. break;
  3861. case OP_SUB:
  3862. priority = 5;
  3863. break;
  3864. case OP_MUL:
  3865. priority = 4;
  3866. break;
  3867. case OP_DIV:
  3868. priority = 4;
  3869. break;
  3870. case OP_MOD:
  3871. priority = 4;
  3872. break;
  3873. case OP_SHIFT_LEFT:
  3874. priority = 6;
  3875. break;
  3876. case OP_SHIFT_RIGHT:
  3877. priority = 6;
  3878. break;
  3879. case OP_ASSIGN:
  3880. priority = 16;
  3881. break;
  3882. case OP_ASSIGN_ADD:
  3883. priority = 16;
  3884. break;
  3885. case OP_ASSIGN_SUB:
  3886. priority = 16;
  3887. break;
  3888. case OP_ASSIGN_MUL:
  3889. priority = 16;
  3890. break;
  3891. case OP_ASSIGN_DIV:
  3892. priority = 16;
  3893. break;
  3894. case OP_ASSIGN_MOD:
  3895. priority = 16;
  3896. break;
  3897. case OP_ASSIGN_SHIFT_LEFT:
  3898. priority = 16;
  3899. break;
  3900. case OP_ASSIGN_SHIFT_RIGHT:
  3901. priority = 16;
  3902. break;
  3903. case OP_ASSIGN_BIT_AND:
  3904. priority = 16;
  3905. break;
  3906. case OP_ASSIGN_BIT_OR:
  3907. priority = 16;
  3908. break;
  3909. case OP_ASSIGN_BIT_XOR:
  3910. priority = 16;
  3911. break;
  3912. case OP_BIT_AND:
  3913. priority = 9;
  3914. break;
  3915. case OP_BIT_OR:
  3916. priority = 11;
  3917. break;
  3918. case OP_BIT_XOR:
  3919. priority = 10;
  3920. break;
  3921. case OP_BIT_INVERT:
  3922. priority = 3;
  3923. unary = true;
  3924. break;
  3925. case OP_INCREMENT:
  3926. priority = 3;
  3927. unary = true;
  3928. break;
  3929. case OP_DECREMENT:
  3930. priority = 3;
  3931. unary = true;
  3932. break;
  3933. case OP_SELECT_IF:
  3934. priority = 15;
  3935. ternary = true;
  3936. break;
  3937. case OP_SELECT_ELSE:
  3938. priority = 15;
  3939. ternary = true;
  3940. break;
  3941. default:
  3942. ERR_FAIL_V(nullptr); //unexpected operator
  3943. }
  3944. if (priority < min_priority) {
  3945. // < is used for left to right (default)
  3946. // <= is used for right to left
  3947. next_op = i;
  3948. min_priority = priority;
  3949. is_unary = unary;
  3950. is_ternary = ternary;
  3951. }
  3952. }
  3953. ERR_FAIL_COND_V(next_op == -1, nullptr);
  3954. // OK! create operator..
  3955. // OK! create operator..
  3956. if (is_unary) {
  3957. int expr_pos = next_op;
  3958. while (expression[expr_pos].is_op) {
  3959. expr_pos++;
  3960. if (expr_pos == expression.size()) {
  3961. //can happen..
  3962. _set_error("Unexpected end of expression...");
  3963. return nullptr;
  3964. }
  3965. }
  3966. //consecutively do unary operators
  3967. for (int i = expr_pos - 1; i >= next_op; i--) {
  3968. OperatorNode *op = alloc_node<OperatorNode>();
  3969. op->op = expression[i].op;
  3970. if ((op->op == OP_INCREMENT || op->op == OP_DECREMENT) && !_validate_assign(expression[i + 1].node, p_builtin_types)) {
  3971. _set_error("Can't use increment/decrement operator in constant expression.");
  3972. return nullptr;
  3973. }
  3974. op->arguments.push_back(expression[i + 1].node);
  3975. expression.write[i].is_op = false;
  3976. expression.write[i].node = op;
  3977. if (!_validate_operator(op, &op->return_cache)) {
  3978. String at;
  3979. for (int j = 0; j < op->arguments.size(); j++) {
  3980. if (j > 0) {
  3981. at += " and ";
  3982. }
  3983. at += get_datatype_name(op->arguments[j]->get_datatype());
  3984. }
  3985. _set_error("Invalid arguments to unary operator '" + get_operator_text(op->op) + "' :" + at);
  3986. return nullptr;
  3987. }
  3988. expression.remove(i + 1);
  3989. }
  3990. } else if (is_ternary) {
  3991. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  3992. _set_error("Parser bug...");
  3993. ERR_FAIL_V(nullptr);
  3994. }
  3995. if (next_op + 2 >= expression.size() || !expression[next_op + 2].is_op || expression[next_op + 2].op != OP_SELECT_ELSE) {
  3996. _set_error("Missing matching ':' for select operator");
  3997. return nullptr;
  3998. }
  3999. OperatorNode *op = alloc_node<OperatorNode>();
  4000. op->op = expression[next_op].op;
  4001. op->arguments.push_back(expression[next_op - 1].node);
  4002. op->arguments.push_back(expression[next_op + 1].node);
  4003. op->arguments.push_back(expression[next_op + 3].node);
  4004. expression.write[next_op - 1].is_op = false;
  4005. expression.write[next_op - 1].node = op;
  4006. if (!_validate_operator(op, &op->return_cache)) {
  4007. String at;
  4008. for (int i = 0; i < op->arguments.size(); i++) {
  4009. if (i > 0) {
  4010. at += " and ";
  4011. }
  4012. at += get_datatype_name(op->arguments[i]->get_datatype());
  4013. }
  4014. _set_error("Invalid argument to ternary ?: operator: " + at);
  4015. return nullptr;
  4016. }
  4017. for (int i = 0; i < 4; i++) {
  4018. expression.remove(next_op);
  4019. }
  4020. } else {
  4021. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  4022. _set_error("Parser bug...");
  4023. ERR_FAIL_V(nullptr);
  4024. }
  4025. OperatorNode *op = alloc_node<OperatorNode>();
  4026. op->op = expression[next_op].op;
  4027. if (expression[next_op - 1].is_op) {
  4028. _set_error("Parser bug...");
  4029. ERR_FAIL_V(nullptr);
  4030. }
  4031. if (_is_operator_assign(op->op)) {
  4032. String assign_message;
  4033. if (!_validate_assign(expression[next_op - 1].node, p_builtin_types, &assign_message)) {
  4034. _set_error(assign_message);
  4035. return nullptr;
  4036. }
  4037. }
  4038. if (expression[next_op + 1].is_op) {
  4039. // this is not invalid and can really appear
  4040. // but it becomes invalid anyway because no binary op
  4041. // can be followed by a unary op in a valid combination,
  4042. // due to how precedence works, unaries will always disappear first
  4043. _set_error("Parser bug...");
  4044. }
  4045. op->arguments.push_back(expression[next_op - 1].node); //expression goes as left
  4046. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as right
  4047. expression.write[next_op - 1].node = op;
  4048. //replace all 3 nodes by this operator and make it an expression
  4049. if (!_validate_operator(op, &op->return_cache)) {
  4050. String at;
  4051. for (int i = 0; i < op->arguments.size(); i++) {
  4052. if (i > 0) {
  4053. at += " and ";
  4054. }
  4055. if (op->arguments[i]->get_datatype() == TYPE_STRUCT) {
  4056. at += op->arguments[i]->get_datatype_name();
  4057. } else {
  4058. at += get_datatype_name(op->arguments[i]->get_datatype());
  4059. }
  4060. }
  4061. _set_error("Invalid arguments to operator '" + get_operator_text(op->op) + "' :" + at);
  4062. return nullptr;
  4063. }
  4064. expression.remove(next_op);
  4065. expression.remove(next_op);
  4066. }
  4067. }
  4068. return expression[0].node;
  4069. }
  4070. ShaderLanguage::Node *ShaderLanguage::_reduce_expression(BlockNode *p_block, ShaderLanguage::Node *p_node) {
  4071. if (p_node->type != Node::TYPE_OPERATOR) {
  4072. return p_node;
  4073. }
  4074. //for now only reduce simple constructors
  4075. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  4076. if (op->op == OP_CONSTRUCT) {
  4077. ERR_FAIL_COND_V(op->arguments[0]->type != Node::TYPE_VARIABLE, p_node);
  4078. DataType type = op->get_datatype();
  4079. DataType base = get_scalar_type(type);
  4080. int cardinality = get_cardinality(type);
  4081. Vector<ConstantNode::Value> values;
  4082. for (int i = 1; i < op->arguments.size(); i++) {
  4083. op->arguments.write[i] = _reduce_expression(p_block, op->arguments[i]);
  4084. if (op->arguments[i]->type == Node::TYPE_CONSTANT) {
  4085. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[i]);
  4086. if (get_scalar_type(cn->datatype) == base) {
  4087. for (int j = 0; j < cn->values.size(); j++) {
  4088. values.push_back(cn->values[j]);
  4089. }
  4090. } else if (get_scalar_type(cn->datatype) == cn->datatype) {
  4091. ConstantNode::Value v;
  4092. if (!convert_constant(cn, base, &v)) {
  4093. return p_node;
  4094. }
  4095. values.push_back(v);
  4096. } else {
  4097. return p_node;
  4098. }
  4099. } else {
  4100. return p_node;
  4101. }
  4102. }
  4103. if (values.size() == 1) {
  4104. if (type >= TYPE_MAT2 && type <= TYPE_MAT4) {
  4105. ConstantNode::Value value = values[0];
  4106. ConstantNode::Value zero;
  4107. zero.real = 0.0f;
  4108. int size = 2 + (type - TYPE_MAT2);
  4109. values.clear();
  4110. for (int i = 0; i < size; i++) {
  4111. for (int j = 0; j < size; j++) {
  4112. values.push_back(i == j ? value : zero);
  4113. }
  4114. }
  4115. } else {
  4116. ConstantNode::Value value = values[0];
  4117. for (int i = 1; i < cardinality; i++) {
  4118. values.push_back(value);
  4119. }
  4120. }
  4121. } else if (values.size() != cardinality) {
  4122. ERR_PRINT("Failed to reduce expression, values and cardinality mismatch.");
  4123. return p_node;
  4124. }
  4125. ConstantNode *cn = alloc_node<ConstantNode>();
  4126. cn->datatype = op->get_datatype();
  4127. cn->values = values;
  4128. return cn;
  4129. } else if (op->op == OP_NEGATE) {
  4130. op->arguments.write[0] = _reduce_expression(p_block, op->arguments[0]);
  4131. if (op->arguments[0]->type == Node::TYPE_CONSTANT) {
  4132. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[0]);
  4133. DataType base = get_scalar_type(cn->datatype);
  4134. Vector<ConstantNode::Value> values;
  4135. for (int i = 0; i < cn->values.size(); i++) {
  4136. ConstantNode::Value nv;
  4137. switch (base) {
  4138. case TYPE_BOOL: {
  4139. nv.boolean = !cn->values[i].boolean;
  4140. } break;
  4141. case TYPE_INT: {
  4142. nv.sint = -cn->values[i].sint;
  4143. } break;
  4144. case TYPE_UINT: {
  4145. // Intentionally wrap the unsigned int value, because GLSL does.
  4146. nv.uint = 0 - cn->values[i].uint;
  4147. } break;
  4148. case TYPE_FLOAT: {
  4149. nv.real = -cn->values[i].real;
  4150. } break;
  4151. default: {
  4152. }
  4153. }
  4154. values.push_back(nv);
  4155. }
  4156. cn->values = values;
  4157. return cn;
  4158. }
  4159. }
  4160. return p_node;
  4161. }
  4162. ShaderLanguage::Node *ShaderLanguage::_parse_and_reduce_expression(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types) {
  4163. ShaderLanguage::Node *expr = _parse_expression(p_block, p_builtin_types);
  4164. if (!expr) { //errored
  4165. return nullptr;
  4166. }
  4167. expr = _reduce_expression(p_block, expr);
  4168. return expr;
  4169. }
  4170. Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, bool p_just_one, bool p_can_break, bool p_can_continue) {
  4171. while (true) {
  4172. TkPos pos = _get_tkpos();
  4173. Token tk = _get_token();
  4174. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_SWITCH) {
  4175. if (tk.type != TK_CF_CASE && tk.type != TK_CF_DEFAULT && tk.type != TK_CURLY_BRACKET_CLOSE) {
  4176. _set_error("Switch may contains only case and default blocks");
  4177. return ERR_PARSE_ERROR;
  4178. }
  4179. }
  4180. bool is_struct = shader->structs.has(tk.text);
  4181. if (tk.type == TK_CURLY_BRACKET_CLOSE) { //end of block
  4182. if (p_just_one) {
  4183. _set_error("Unexpected '}'");
  4184. return ERR_PARSE_ERROR;
  4185. }
  4186. return OK;
  4187. } else if (tk.type == TK_CONST || is_token_precision(tk.type) || is_token_nonvoid_datatype(tk.type) || is_struct) {
  4188. String struct_name = "";
  4189. if (is_struct) {
  4190. struct_name = tk.text;
  4191. }
  4192. bool is_const = false;
  4193. if (tk.type == TK_CONST) {
  4194. is_const = true;
  4195. tk = _get_token();
  4196. if (!is_struct) {
  4197. is_struct = shader->structs.has(tk.text); // check again.
  4198. struct_name = tk.text;
  4199. }
  4200. }
  4201. DataPrecision precision = PRECISION_DEFAULT;
  4202. if (is_token_precision(tk.type)) {
  4203. precision = get_token_precision(tk.type);
  4204. tk = _get_token();
  4205. if (!is_struct) {
  4206. is_struct = shader->structs.has(tk.text); // check again.
  4207. }
  4208. if (is_struct && precision != PRECISION_DEFAULT) {
  4209. _set_error("Precision modifier cannot be used on structs.");
  4210. return ERR_PARSE_ERROR;
  4211. }
  4212. if (!is_token_nonvoid_datatype(tk.type)) {
  4213. _set_error("Expected datatype after precision");
  4214. return ERR_PARSE_ERROR;
  4215. }
  4216. }
  4217. if (!is_struct) {
  4218. if (!is_token_variable_datatype(tk.type)) {
  4219. _set_error("Invalid data type for variable (samplers not allowed)");
  4220. return ERR_PARSE_ERROR;
  4221. }
  4222. }
  4223. DataType type = is_struct ? TYPE_STRUCT : get_token_datatype(tk.type);
  4224. if (_validate_datatype(type) != OK) {
  4225. return ERR_PARSE_ERROR;
  4226. }
  4227. tk = _get_token();
  4228. Node *vardecl = nullptr;
  4229. while (true) {
  4230. if (tk.type != TK_IDENTIFIER) {
  4231. _set_error("Expected identifier after type");
  4232. return ERR_PARSE_ERROR;
  4233. }
  4234. StringName name = tk.text;
  4235. ShaderLanguage::IdentifierType itype;
  4236. if (_find_identifier(p_block, p_builtin_types, name, (ShaderLanguage::DataType *)nullptr, &itype)) {
  4237. if (itype != IDENTIFIER_FUNCTION) {
  4238. _set_error("Redefinition of '" + String(name) + "'");
  4239. return ERR_PARSE_ERROR;
  4240. }
  4241. }
  4242. BlockNode::Variable var;
  4243. var.type = type;
  4244. var.precision = precision;
  4245. var.line = tk_line;
  4246. var.array_size = 0;
  4247. var.is_const = is_const;
  4248. var.struct_name = struct_name;
  4249. tk = _get_token();
  4250. if (tk.type == TK_BRACKET_OPEN) {
  4251. bool unknown_size = false;
  4252. if (VisualServer::get_singleton()->is_low_end() && is_const) {
  4253. _set_error("Local const arrays are only supported on the GLES3 backend, but your project is using GLES2.");
  4254. return ERR_PARSE_ERROR;
  4255. }
  4256. ArrayDeclarationNode *node = alloc_node<ArrayDeclarationNode>();
  4257. if (is_struct) {
  4258. node->struct_name = struct_name;
  4259. node->datatype = TYPE_STRUCT;
  4260. } else {
  4261. node->datatype = type;
  4262. }
  4263. node->precision = precision;
  4264. node->is_const = is_const;
  4265. vardecl = (Node *)node;
  4266. ArrayDeclarationNode::Declaration decl;
  4267. decl.name = name;
  4268. decl.size = 0U;
  4269. tk = _get_token();
  4270. if (tk.type == TK_BRACKET_CLOSE) {
  4271. unknown_size = true;
  4272. } else {
  4273. if (!tk.is_integer_constant() || ((int)tk.constant) <= 0) {
  4274. _set_error("Expected integer constant > 0 or ']'");
  4275. return ERR_PARSE_ERROR;
  4276. }
  4277. decl.size = ((uint32_t)tk.constant);
  4278. tk = _get_token();
  4279. if (tk.type != TK_BRACKET_CLOSE) {
  4280. _set_error("Expected ']'");
  4281. return ERR_PARSE_ERROR;
  4282. }
  4283. var.array_size = decl.size;
  4284. }
  4285. bool full_def = false;
  4286. tk = _get_token();
  4287. if (tk.type == TK_OP_ASSIGN) {
  4288. if (VisualServer::get_singleton()->is_low_end()) {
  4289. _set_error("Array initialization is only supported on the GLES3 backend, but your project is using GLES2.");
  4290. return ERR_PARSE_ERROR;
  4291. }
  4292. tk = _get_token();
  4293. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  4294. if (unknown_size) {
  4295. _set_error("Expected '{'");
  4296. return ERR_PARSE_ERROR;
  4297. }
  4298. full_def = true;
  4299. DataPrecision precision2 = PRECISION_DEFAULT;
  4300. if (is_token_precision(tk.type)) {
  4301. precision2 = get_token_precision(tk.type);
  4302. tk = _get_token();
  4303. if (shader->structs.has(tk.text)) {
  4304. _set_error("Precision modifier cannot be used on structs.");
  4305. return ERR_PARSE_ERROR;
  4306. }
  4307. if (!is_token_nonvoid_datatype(tk.type)) {
  4308. _set_error("Expected datatype after precision");
  4309. return ERR_PARSE_ERROR;
  4310. }
  4311. }
  4312. DataType type2;
  4313. StringName struct_name2 = "";
  4314. if (shader->structs.has(tk.text)) {
  4315. type2 = TYPE_STRUCT;
  4316. struct_name2 = tk.text;
  4317. } else {
  4318. if (!is_token_variable_datatype(tk.type)) {
  4319. _set_error("Invalid data type for array");
  4320. return ERR_PARSE_ERROR;
  4321. }
  4322. type2 = get_token_datatype(tk.type);
  4323. }
  4324. int array_size2 = 0;
  4325. tk = _get_token();
  4326. if (tk.type == TK_BRACKET_OPEN) {
  4327. TkPos pos2 = _get_tkpos();
  4328. tk = _get_token();
  4329. if (tk.type == TK_BRACKET_CLOSE) {
  4330. array_size2 = var.array_size;
  4331. tk = _get_token();
  4332. } else {
  4333. _set_tkpos(pos2);
  4334. Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
  4335. if (!n || n->type != Node::TYPE_CONSTANT || n->get_datatype() != TYPE_INT) {
  4336. _set_error("Expected single integer constant > 0");
  4337. return ERR_PARSE_ERROR;
  4338. }
  4339. ConstantNode *cnode = (ConstantNode *)n;
  4340. if (cnode->values.size() == 1) {
  4341. array_size2 = cnode->values[0].sint;
  4342. if (array_size2 <= 0) {
  4343. _set_error("Expected single integer constant > 0");
  4344. return ERR_PARSE_ERROR;
  4345. }
  4346. } else {
  4347. _set_error("Expected single integer constant > 0");
  4348. return ERR_PARSE_ERROR;
  4349. }
  4350. tk = _get_token();
  4351. if (tk.type != TK_BRACKET_CLOSE) {
  4352. _set_error("Expected ']'");
  4353. return ERR_PARSE_ERROR;
  4354. } else {
  4355. tk = _get_token();
  4356. }
  4357. }
  4358. } else {
  4359. _set_error("Expected '['");
  4360. return ERR_PARSE_ERROR;
  4361. }
  4362. if (precision != precision2 || type != type2 || struct_name != struct_name2 || var.array_size != array_size2) {
  4363. String error_str = "Cannot convert from '";
  4364. if (precision2 != PRECISION_DEFAULT) {
  4365. error_str += get_precision_name(precision2);
  4366. error_str += " ";
  4367. }
  4368. if (type2 == TYPE_STRUCT) {
  4369. error_str += struct_name2;
  4370. } else {
  4371. error_str += get_datatype_name(type2);
  4372. }
  4373. error_str += "[";
  4374. error_str += itos(array_size2);
  4375. error_str += "]'";
  4376. error_str += " to '";
  4377. if (precision != PRECISION_DEFAULT) {
  4378. error_str += get_precision_name(precision);
  4379. error_str += " ";
  4380. }
  4381. if (type == TYPE_STRUCT) {
  4382. error_str += struct_name;
  4383. } else {
  4384. error_str += get_datatype_name(type);
  4385. }
  4386. error_str += "[";
  4387. error_str += itos(var.array_size);
  4388. error_str += "]'";
  4389. _set_error(error_str);
  4390. return ERR_PARSE_ERROR;
  4391. }
  4392. }
  4393. bool curly = tk.type == TK_CURLY_BRACKET_OPEN;
  4394. if (unknown_size) {
  4395. if (!curly) {
  4396. _set_error("Expected '{'");
  4397. return ERR_PARSE_ERROR;
  4398. }
  4399. } else {
  4400. if (full_def) {
  4401. if (curly) {
  4402. _set_error("Expected '('");
  4403. return ERR_PARSE_ERROR;
  4404. }
  4405. }
  4406. }
  4407. if (tk.type == TK_PARENTHESIS_OPEN || curly) { // initialization
  4408. while (true) {
  4409. Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
  4410. if (!n) {
  4411. return ERR_PARSE_ERROR;
  4412. }
  4413. if (node->is_const && n->type == Node::TYPE_OPERATOR && ((OperatorNode *)n)->op == OP_CALL) {
  4414. _set_error("Expected constant expression");
  4415. return ERR_PARSE_ERROR;
  4416. }
  4417. if (var.type != n->get_datatype() || struct_name != n->get_datatype_name()) {
  4418. _set_error("Invalid assignment of '" + (n->get_datatype() == TYPE_STRUCT ? n->get_datatype_name() : get_datatype_name(n->get_datatype())) + "' to '" + (var.type == TYPE_STRUCT ? struct_name : get_datatype_name(var.type)) + "'");
  4419. return ERR_PARSE_ERROR;
  4420. }
  4421. tk = _get_token();
  4422. if (tk.type == TK_COMMA) {
  4423. decl.initializer.push_back(n);
  4424. continue;
  4425. } else if (!curly && tk.type == TK_PARENTHESIS_CLOSE) {
  4426. decl.initializer.push_back(n);
  4427. break;
  4428. } else if (curly && tk.type == TK_CURLY_BRACKET_CLOSE) {
  4429. decl.initializer.push_back(n);
  4430. break;
  4431. } else {
  4432. if (curly) {
  4433. _set_error("Expected '}' or ','");
  4434. } else {
  4435. _set_error("Expected ')' or ','");
  4436. }
  4437. return ERR_PARSE_ERROR;
  4438. }
  4439. }
  4440. if (unknown_size) {
  4441. decl.size = decl.initializer.size();
  4442. var.array_size = decl.initializer.size();
  4443. } else if (decl.initializer.size() != var.array_size) {
  4444. _set_error("Array size mismatch");
  4445. return ERR_PARSE_ERROR;
  4446. }
  4447. tk = _get_token();
  4448. }
  4449. } else {
  4450. if (unknown_size) {
  4451. _set_error("Expected array initialization");
  4452. return ERR_PARSE_ERROR;
  4453. }
  4454. if (is_const) {
  4455. _set_error("Expected initialization of constant");
  4456. return ERR_PARSE_ERROR;
  4457. }
  4458. }
  4459. node->declarations.push_back(decl);
  4460. } else if (tk.type == TK_OP_ASSIGN) {
  4461. VariableDeclarationNode *node = alloc_node<VariableDeclarationNode>();
  4462. if (is_struct) {
  4463. node->struct_name = struct_name;
  4464. node->datatype = TYPE_STRUCT;
  4465. } else {
  4466. node->datatype = type;
  4467. }
  4468. node->precision = precision;
  4469. node->is_const = is_const;
  4470. vardecl = (Node *)node;
  4471. VariableDeclarationNode::Declaration decl;
  4472. decl.name = name;
  4473. decl.initializer = nullptr;
  4474. //variable created with assignment! must parse an expression
  4475. Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
  4476. if (!n) {
  4477. return ERR_PARSE_ERROR;
  4478. }
  4479. if (node->is_const && n->type == Node::TYPE_OPERATOR && ((OperatorNode *)n)->op == OP_CALL) {
  4480. _set_error("Expected constant expression after '='");
  4481. return ERR_PARSE_ERROR;
  4482. }
  4483. decl.initializer = n;
  4484. if (var.type == TYPE_STRUCT ? (var.struct_name != n->get_datatype_name()) : (var.type != n->get_datatype())) {
  4485. _set_error("Invalid assignment of '" + (n->get_datatype() == TYPE_STRUCT ? n->get_datatype_name() : get_datatype_name(n->get_datatype())) + "' to '" + (var.type == TYPE_STRUCT ? String(var.struct_name) : get_datatype_name(var.type)) + "'");
  4486. return ERR_PARSE_ERROR;
  4487. }
  4488. tk = _get_token();
  4489. node->declarations.push_back(decl);
  4490. } else {
  4491. if (is_const) {
  4492. _set_error("Expected initialization of constant");
  4493. return ERR_PARSE_ERROR;
  4494. }
  4495. VariableDeclarationNode *node = alloc_node<VariableDeclarationNode>();
  4496. if (is_struct) {
  4497. node->struct_name = struct_name;
  4498. node->datatype = TYPE_STRUCT;
  4499. } else {
  4500. node->datatype = type;
  4501. }
  4502. node->precision = precision;
  4503. vardecl = (Node *)node;
  4504. VariableDeclarationNode::Declaration decl;
  4505. decl.name = name;
  4506. decl.initializer = nullptr;
  4507. node->declarations.push_back(decl);
  4508. }
  4509. p_block->statements.push_back(vardecl);
  4510. p_block->variables[name] = var;
  4511. if (tk.type == TK_COMMA) {
  4512. tk = _get_token();
  4513. //another variable
  4514. } else if (tk.type == TK_SEMICOLON) {
  4515. break;
  4516. } else {
  4517. _set_error("Expected ',' or ';' after variable");
  4518. return ERR_PARSE_ERROR;
  4519. }
  4520. }
  4521. } else if (tk.type == TK_CURLY_BRACKET_OPEN) {
  4522. //a sub block, just because..
  4523. BlockNode *block = alloc_node<BlockNode>();
  4524. block->parent_block = p_block;
  4525. _parse_block(block, p_builtin_types, false, p_can_break, p_can_continue);
  4526. p_block->statements.push_back(block);
  4527. } else if (tk.type == TK_CF_IF) {
  4528. //if () {}
  4529. tk = _get_token();
  4530. if (tk.type != TK_PARENTHESIS_OPEN) {
  4531. _set_error("Expected '(' after if");
  4532. return ERR_PARSE_ERROR;
  4533. }
  4534. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  4535. cf->flow_op = FLOW_OP_IF;
  4536. Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
  4537. if (!n) {
  4538. return ERR_PARSE_ERROR;
  4539. }
  4540. if (n->get_datatype() != TYPE_BOOL) {
  4541. _set_error("Expected boolean expression");
  4542. return ERR_PARSE_ERROR;
  4543. }
  4544. tk = _get_token();
  4545. if (tk.type != TK_PARENTHESIS_CLOSE) {
  4546. _set_error("Expected ')' after expression");
  4547. return ERR_PARSE_ERROR;
  4548. }
  4549. BlockNode *block = alloc_node<BlockNode>();
  4550. block->parent_block = p_block;
  4551. cf->expressions.push_back(n);
  4552. cf->blocks.push_back(block);
  4553. p_block->statements.push_back(cf);
  4554. Error err = _parse_block(block, p_builtin_types, true, p_can_break, p_can_continue);
  4555. if (err) {
  4556. return err;
  4557. }
  4558. pos = _get_tkpos();
  4559. tk = _get_token();
  4560. if (tk.type == TK_CF_ELSE) {
  4561. block = alloc_node<BlockNode>();
  4562. block->parent_block = p_block;
  4563. cf->blocks.push_back(block);
  4564. err = _parse_block(block, p_builtin_types, true, p_can_break, p_can_continue);
  4565. if (err) {
  4566. return err;
  4567. }
  4568. } else {
  4569. _set_tkpos(pos); //rollback
  4570. }
  4571. } else if (tk.type == TK_CF_SWITCH) {
  4572. if (VisualServer::get_singleton()->is_low_end()) {
  4573. _set_error("\"switch\" operator is only supported on the GLES3 backend, but your project is using GLES2.");
  4574. return ERR_PARSE_ERROR;
  4575. }
  4576. // switch() {}
  4577. tk = _get_token();
  4578. if (tk.type != TK_PARENTHESIS_OPEN) {
  4579. _set_error("Expected '(' after switch");
  4580. return ERR_PARSE_ERROR;
  4581. }
  4582. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  4583. cf->flow_op = FLOW_OP_SWITCH;
  4584. Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
  4585. if (!n) {
  4586. return ERR_PARSE_ERROR;
  4587. }
  4588. if (n->get_datatype() != TYPE_INT) {
  4589. _set_error("Expected integer expression");
  4590. return ERR_PARSE_ERROR;
  4591. }
  4592. tk = _get_token();
  4593. if (tk.type != TK_PARENTHESIS_CLOSE) {
  4594. _set_error("Expected ')' after expression");
  4595. return ERR_PARSE_ERROR;
  4596. }
  4597. tk = _get_token();
  4598. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  4599. _set_error("Expected '{' after switch statement");
  4600. return ERR_PARSE_ERROR;
  4601. }
  4602. BlockNode *switch_block = alloc_node<BlockNode>();
  4603. switch_block->block_type = BlockNode::BLOCK_TYPE_SWITCH;
  4604. switch_block->parent_block = p_block;
  4605. cf->expressions.push_back(n);
  4606. cf->blocks.push_back(switch_block);
  4607. p_block->statements.push_back(cf);
  4608. int prev_type = TK_CF_CASE;
  4609. while (true) { // Go-through multiple cases.
  4610. if (_parse_block(switch_block, p_builtin_types, true, true, false) != OK) {
  4611. return ERR_PARSE_ERROR;
  4612. }
  4613. pos = _get_tkpos();
  4614. tk = _get_token();
  4615. if (tk.type == TK_CF_CASE || tk.type == TK_CF_DEFAULT) {
  4616. if (prev_type == TK_CF_DEFAULT) {
  4617. if (tk.type == TK_CF_CASE) {
  4618. _set_error("Cases must be defined before default case.");
  4619. return ERR_PARSE_ERROR;
  4620. } else if (prev_type == TK_CF_DEFAULT) {
  4621. _set_error("Default case must be defined only once.");
  4622. return ERR_PARSE_ERROR;
  4623. }
  4624. }
  4625. prev_type = tk.type;
  4626. _set_tkpos(pos);
  4627. continue;
  4628. } else {
  4629. Set<int> constants;
  4630. for (int i = 0; i < switch_block->statements.size(); i++) { // Checks for duplicates.
  4631. ControlFlowNode *flow = (ControlFlowNode *)switch_block->statements[i];
  4632. if (flow) {
  4633. if (flow->flow_op == FLOW_OP_CASE) {
  4634. ConstantNode *n2 = static_cast<ConstantNode *>(flow->expressions[0]);
  4635. if (!n2) {
  4636. return ERR_PARSE_ERROR;
  4637. }
  4638. if (n2->values.empty()) {
  4639. return ERR_PARSE_ERROR;
  4640. }
  4641. if (constants.has(n2->values[0].sint)) {
  4642. _set_error("Duplicated case label: '" + itos(n2->values[0].sint) + "'");
  4643. return ERR_PARSE_ERROR;
  4644. }
  4645. constants.insert(n2->values[0].sint);
  4646. } else if (flow->flow_op == FLOW_OP_DEFAULT) {
  4647. continue;
  4648. } else {
  4649. return ERR_PARSE_ERROR;
  4650. }
  4651. } else {
  4652. return ERR_PARSE_ERROR;
  4653. }
  4654. }
  4655. break;
  4656. }
  4657. }
  4658. } else if (tk.type == TK_CF_CASE) {
  4659. // case x : break; | return;
  4660. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_CASE) {
  4661. _set_tkpos(pos);
  4662. return OK;
  4663. }
  4664. if (!p_block || (p_block->block_type != BlockNode::BLOCK_TYPE_SWITCH)) {
  4665. _set_error("case must be placed within switch block");
  4666. return ERR_PARSE_ERROR;
  4667. }
  4668. tk = _get_token();
  4669. int sign = 1;
  4670. if (tk.type == TK_OP_SUB) {
  4671. sign = -1;
  4672. tk = _get_token();
  4673. }
  4674. if (!tk.is_integer_constant()) {
  4675. _set_error("Expected integer constant");
  4676. return ERR_PARSE_ERROR;
  4677. }
  4678. int constant = (int)tk.constant * sign;
  4679. tk = _get_token();
  4680. if (tk.type != TK_COLON) {
  4681. _set_error("Expected ':'");
  4682. return ERR_PARSE_ERROR;
  4683. }
  4684. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  4685. cf->flow_op = FLOW_OP_CASE;
  4686. ConstantNode *n = alloc_node<ConstantNode>();
  4687. ConstantNode::Value v;
  4688. v.sint = constant;
  4689. n->values.push_back(v);
  4690. n->datatype = TYPE_INT;
  4691. BlockNode *case_block = alloc_node<BlockNode>();
  4692. case_block->block_type = BlockNode::BLOCK_TYPE_CASE;
  4693. case_block->parent_block = p_block;
  4694. cf->expressions.push_back(n);
  4695. cf->blocks.push_back(case_block);
  4696. p_block->statements.push_back(cf);
  4697. Error err = _parse_block(case_block, p_builtin_types, false, true, false);
  4698. if (err) {
  4699. return err;
  4700. }
  4701. return OK;
  4702. } else if (tk.type == TK_CF_DEFAULT) {
  4703. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_CASE) {
  4704. _set_tkpos(pos);
  4705. return OK;
  4706. }
  4707. if (!p_block || (p_block->block_type != BlockNode::BLOCK_TYPE_SWITCH)) {
  4708. _set_error("default must be placed within switch block");
  4709. return ERR_PARSE_ERROR;
  4710. }
  4711. tk = _get_token();
  4712. if (tk.type != TK_COLON) {
  4713. _set_error("Expected ':'");
  4714. return ERR_PARSE_ERROR;
  4715. }
  4716. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  4717. cf->flow_op = FLOW_OP_DEFAULT;
  4718. BlockNode *default_block = alloc_node<BlockNode>();
  4719. default_block->block_type = BlockNode::BLOCK_TYPE_DEFAULT;
  4720. default_block->parent_block = p_block;
  4721. cf->blocks.push_back(default_block);
  4722. p_block->statements.push_back(cf);
  4723. Error err = _parse_block(default_block, p_builtin_types, false, true, false);
  4724. if (err) {
  4725. return err;
  4726. }
  4727. return OK;
  4728. } else if (tk.type == TK_CF_DO || tk.type == TK_CF_WHILE) {
  4729. // do {} while()
  4730. // while() {}
  4731. bool is_do = tk.type == TK_CF_DO;
  4732. BlockNode *do_block = nullptr;
  4733. if (is_do) {
  4734. do_block = alloc_node<BlockNode>();
  4735. do_block->parent_block = p_block;
  4736. Error err = _parse_block(do_block, p_builtin_types, true, true, true);
  4737. if (err) {
  4738. return err;
  4739. }
  4740. tk = _get_token();
  4741. if (tk.type != TK_CF_WHILE) {
  4742. _set_error("Expected while after do");
  4743. return ERR_PARSE_ERROR;
  4744. }
  4745. }
  4746. tk = _get_token();
  4747. if (tk.type != TK_PARENTHESIS_OPEN) {
  4748. _set_error("Expected '(' after while");
  4749. return ERR_PARSE_ERROR;
  4750. }
  4751. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  4752. if (is_do) {
  4753. cf->flow_op = FLOW_OP_DO;
  4754. } else {
  4755. cf->flow_op = FLOW_OP_WHILE;
  4756. }
  4757. Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
  4758. if (!n) {
  4759. return ERR_PARSE_ERROR;
  4760. }
  4761. tk = _get_token();
  4762. if (tk.type != TK_PARENTHESIS_CLOSE) {
  4763. _set_error("Expected ')' after expression");
  4764. return ERR_PARSE_ERROR;
  4765. }
  4766. if (!is_do) {
  4767. BlockNode *block = alloc_node<BlockNode>();
  4768. block->parent_block = p_block;
  4769. cf->expressions.push_back(n);
  4770. cf->blocks.push_back(block);
  4771. p_block->statements.push_back(cf);
  4772. Error err = _parse_block(block, p_builtin_types, true, true, true);
  4773. if (err) {
  4774. return err;
  4775. }
  4776. } else {
  4777. cf->expressions.push_back(n);
  4778. cf->blocks.push_back(do_block);
  4779. p_block->statements.push_back(cf);
  4780. tk = _get_token();
  4781. if (tk.type != TK_SEMICOLON) {
  4782. _set_error("Expected ';'");
  4783. return ERR_PARSE_ERROR;
  4784. }
  4785. }
  4786. } else if (tk.type == TK_CF_FOR) {
  4787. // for() {}
  4788. tk = _get_token();
  4789. if (tk.type != TK_PARENTHESIS_OPEN) {
  4790. _set_error("Expected '(' after for");
  4791. return ERR_PARSE_ERROR;
  4792. }
  4793. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  4794. cf->flow_op = FLOW_OP_FOR;
  4795. BlockNode *init_block = alloc_node<BlockNode>();
  4796. init_block->parent_block = p_block;
  4797. init_block->single_statement = true;
  4798. cf->blocks.push_back(init_block);
  4799. if (_parse_block(init_block, p_builtin_types, true, false, false) != OK) {
  4800. return ERR_PARSE_ERROR;
  4801. }
  4802. Node *n = _parse_and_reduce_expression(init_block, p_builtin_types);
  4803. if (!n) {
  4804. return ERR_PARSE_ERROR;
  4805. }
  4806. if (n->get_datatype() != TYPE_BOOL) {
  4807. _set_error("Middle expression is expected to be boolean.");
  4808. return ERR_PARSE_ERROR;
  4809. }
  4810. tk = _get_token();
  4811. if (tk.type != TK_SEMICOLON) {
  4812. _set_error("Expected ';' after middle expression");
  4813. return ERR_PARSE_ERROR;
  4814. }
  4815. cf->expressions.push_back(n);
  4816. n = _parse_and_reduce_expression(init_block, p_builtin_types);
  4817. if (!n) {
  4818. return ERR_PARSE_ERROR;
  4819. }
  4820. cf->expressions.push_back(n);
  4821. tk = _get_token();
  4822. if (tk.type != TK_PARENTHESIS_CLOSE) {
  4823. _set_error("Expected ')' after third expression");
  4824. return ERR_PARSE_ERROR;
  4825. }
  4826. BlockNode *block = alloc_node<BlockNode>();
  4827. block->parent_block = init_block;
  4828. cf->blocks.push_back(block);
  4829. p_block->statements.push_back(cf);
  4830. Error err = _parse_block(block, p_builtin_types, true, true, true);
  4831. if (err) {
  4832. return err;
  4833. }
  4834. } else if (tk.type == TK_CF_RETURN) {
  4835. //check return type
  4836. BlockNode *b = p_block;
  4837. while (b && !b->parent_function) {
  4838. b = b->parent_block;
  4839. }
  4840. if (!b) {
  4841. _set_error("Bug");
  4842. return ERR_BUG;
  4843. }
  4844. if (b && b->parent_function && (b->parent_function->name == "vertex" || b->parent_function->name == "fragment" || b->parent_function->name == "light")) {
  4845. _set_error(vformat("Using 'return' in '%s' processor function results in undefined behavior!", b->parent_function->name));
  4846. return ERR_PARSE_ERROR;
  4847. }
  4848. String return_struct_name = String(b->parent_function->return_struct_name);
  4849. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  4850. flow->flow_op = FLOW_OP_RETURN;
  4851. pos = _get_tkpos();
  4852. tk = _get_token();
  4853. if (tk.type == TK_SEMICOLON) {
  4854. //all is good
  4855. if (b->parent_function->return_type != TYPE_VOID) {
  4856. _set_error("Expected return with an expression of type '" + (return_struct_name != "" ? return_struct_name : get_datatype_name(b->parent_function->return_type)) + "'");
  4857. return ERR_PARSE_ERROR;
  4858. }
  4859. } else {
  4860. _set_tkpos(pos); //rollback, wants expression
  4861. Node *expr = _parse_and_reduce_expression(p_block, p_builtin_types);
  4862. if (!expr) {
  4863. return ERR_PARSE_ERROR;
  4864. }
  4865. if (b->parent_function->return_type != expr->get_datatype() || return_struct_name != expr->get_datatype_name()) {
  4866. _set_error("Expected return with an expression of type '" + (return_struct_name != "" ? return_struct_name : get_datatype_name(b->parent_function->return_type)) + "'");
  4867. return ERR_PARSE_ERROR;
  4868. }
  4869. tk = _get_token();
  4870. if (tk.type != TK_SEMICOLON) {
  4871. _set_error("Expected ';' after return expression");
  4872. return ERR_PARSE_ERROR;
  4873. }
  4874. flow->expressions.push_back(expr);
  4875. }
  4876. p_block->statements.push_back(flow);
  4877. BlockNode *block = p_block;
  4878. while (block) {
  4879. if (block->block_type == BlockNode::BLOCK_TYPE_CASE || block->block_type == BlockNode::BLOCK_TYPE_DEFAULT) {
  4880. return OK;
  4881. }
  4882. block = block->parent_block;
  4883. }
  4884. } else if (tk.type == TK_CF_DISCARD) {
  4885. //check return type
  4886. BlockNode *b = p_block;
  4887. while (b && !b->parent_function) {
  4888. b = b->parent_block;
  4889. }
  4890. if (!b) {
  4891. _set_error("Bug");
  4892. return ERR_BUG;
  4893. }
  4894. if (!b->parent_function->can_discard) {
  4895. _set_error("Use of 'discard' is not allowed here.");
  4896. return ERR_PARSE_ERROR;
  4897. }
  4898. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  4899. flow->flow_op = FLOW_OP_DISCARD;
  4900. pos = _get_tkpos();
  4901. tk = _get_token();
  4902. if (tk.type != TK_SEMICOLON) {
  4903. _set_error("Expected ';' after discard");
  4904. return ERR_PARSE_ERROR;
  4905. }
  4906. p_block->statements.push_back(flow);
  4907. } else if (tk.type == TK_CF_BREAK) {
  4908. if (!p_can_break) {
  4909. _set_error("'break' is not allowed outside of a loop or 'switch' statement");
  4910. return ERR_PARSE_ERROR;
  4911. }
  4912. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  4913. flow->flow_op = FLOW_OP_BREAK;
  4914. pos = _get_tkpos();
  4915. tk = _get_token();
  4916. if (tk.type != TK_SEMICOLON) {
  4917. _set_error("Expected ';' after break");
  4918. return ERR_PARSE_ERROR;
  4919. }
  4920. p_block->statements.push_back(flow);
  4921. BlockNode *block = p_block;
  4922. while (block) {
  4923. if (block->block_type == BlockNode::BLOCK_TYPE_CASE || block->block_type == BlockNode::BLOCK_TYPE_DEFAULT) {
  4924. return OK;
  4925. }
  4926. block = block->parent_block;
  4927. }
  4928. } else if (tk.type == TK_CF_CONTINUE) {
  4929. if (!p_can_continue) {
  4930. _set_error("'continue' is not allowed outside of a loop");
  4931. return ERR_PARSE_ERROR;
  4932. }
  4933. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  4934. flow->flow_op = FLOW_OP_CONTINUE;
  4935. pos = _get_tkpos();
  4936. tk = _get_token();
  4937. if (tk.type != TK_SEMICOLON) {
  4938. //all is good
  4939. _set_error("Expected ';' after continue");
  4940. return ERR_PARSE_ERROR;
  4941. }
  4942. p_block->statements.push_back(flow);
  4943. } else {
  4944. //nothing else, so expression
  4945. _set_tkpos(pos); //rollback
  4946. Node *expr = _parse_and_reduce_expression(p_block, p_builtin_types);
  4947. if (!expr) {
  4948. return ERR_PARSE_ERROR;
  4949. }
  4950. p_block->statements.push_back(expr);
  4951. tk = _get_token();
  4952. if (tk.type != TK_SEMICOLON) {
  4953. _set_error("Expected ';' after statement");
  4954. return ERR_PARSE_ERROR;
  4955. }
  4956. }
  4957. if (p_just_one) {
  4958. break;
  4959. }
  4960. }
  4961. return OK;
  4962. }
  4963. String ShaderLanguage::_get_shader_type_list(const Set<String> &p_shader_types) const {
  4964. // Return a list of shader types as an human-readable string
  4965. String valid_types;
  4966. for (const Set<String>::Element *E = p_shader_types.front(); E; E = E->next()) {
  4967. if (valid_types != String()) {
  4968. valid_types += ", ";
  4969. }
  4970. valid_types += "'" + E->get() + "'";
  4971. }
  4972. return valid_types;
  4973. }
  4974. String ShaderLanguage::_get_qualifier_str(ArgumentQualifier p_qualifier) const {
  4975. switch (p_qualifier) {
  4976. case ArgumentQualifier::ARGUMENT_QUALIFIER_IN:
  4977. return "in";
  4978. case ArgumentQualifier::ARGUMENT_QUALIFIER_OUT:
  4979. return "out";
  4980. case ArgumentQualifier::ARGUMENT_QUALIFIER_INOUT:
  4981. return "inout";
  4982. }
  4983. return "";
  4984. }
  4985. Error ShaderLanguage::_validate_datatype(DataType p_type) {
  4986. if (VisualServer::get_singleton()->is_low_end()) {
  4987. bool invalid_type = false;
  4988. switch (p_type) {
  4989. case TYPE_UINT:
  4990. case TYPE_UVEC2:
  4991. case TYPE_UVEC3:
  4992. case TYPE_UVEC4:
  4993. case TYPE_ISAMPLER2D:
  4994. case TYPE_USAMPLER2D:
  4995. case TYPE_SAMPLER3D:
  4996. case TYPE_ISAMPLER3D:
  4997. case TYPE_USAMPLER3D:
  4998. case TYPE_SAMPLER2DARRAY:
  4999. case TYPE_USAMPLER2DARRAY:
  5000. case TYPE_ISAMPLER2DARRAY:
  5001. invalid_type = true;
  5002. break;
  5003. default:
  5004. break;
  5005. }
  5006. if (invalid_type) {
  5007. _set_error(vformat("\"%s\" type is only supported on the GLES3 backend, but your project is using GLES2.", get_datatype_name(p_type)));
  5008. return ERR_UNAVAILABLE;
  5009. }
  5010. }
  5011. return OK;
  5012. }
  5013. Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types) {
  5014. Token tk = _get_token();
  5015. if (tk.type != TK_SHADER_TYPE) {
  5016. _set_error("Expected 'shader_type' at the beginning of shader. Valid types are: " + _get_shader_type_list(p_shader_types));
  5017. return ERR_PARSE_ERROR;
  5018. }
  5019. StringName shader_type_identifier;
  5020. _get_completable_identifier(nullptr, COMPLETION_SHADER_TYPE, shader_type_identifier);
  5021. if (shader_type_identifier == StringName()) {
  5022. _set_error("Expected identifier after 'shader_type', indicating type of shader. Valid types are: " + _get_shader_type_list(p_shader_types));
  5023. return ERR_PARSE_ERROR;
  5024. }
  5025. if (!p_shader_types.has(shader_type_identifier)) {
  5026. _set_error("Invalid shader type. Valid types are: " + _get_shader_type_list(p_shader_types));
  5027. return ERR_PARSE_ERROR;
  5028. }
  5029. tk = _get_token();
  5030. if (tk.type != TK_SEMICOLON) {
  5031. _set_error("Expected ';' after 'shader_type <type>'.");
  5032. }
  5033. tk = _get_token();
  5034. int texture_uniforms = 0;
  5035. int uniforms = 0;
  5036. while (tk.type != TK_EOF) {
  5037. switch (tk.type) {
  5038. case TK_RENDER_MODE: {
  5039. while (true) {
  5040. StringName mode;
  5041. _get_completable_identifier(nullptr, COMPLETION_RENDER_MODE, mode);
  5042. if (mode == StringName()) {
  5043. _set_error("Expected identifier for render mode");
  5044. return ERR_PARSE_ERROR;
  5045. }
  5046. if (p_render_modes.find(mode) == -1) {
  5047. _set_error("Invalid render mode: '" + String(mode) + "'");
  5048. return ERR_PARSE_ERROR;
  5049. }
  5050. if (shader->render_modes.find(mode) != -1) {
  5051. _set_error("Duplicate render mode: '" + String(mode) + "'");
  5052. return ERR_PARSE_ERROR;
  5053. }
  5054. shader->render_modes.push_back(mode);
  5055. tk = _get_token();
  5056. if (tk.type == TK_COMMA) {
  5057. //all good, do nothing
  5058. } else if (tk.type == TK_SEMICOLON) {
  5059. break; //done
  5060. } else {
  5061. _set_error("Unexpected token: " + get_token_text(tk));
  5062. return ERR_PARSE_ERROR;
  5063. }
  5064. }
  5065. } break;
  5066. case TK_STRUCT: {
  5067. ShaderNode::Struct st;
  5068. DataType type;
  5069. tk = _get_token();
  5070. if (tk.type == TK_IDENTIFIER) {
  5071. st.name = tk.text;
  5072. if (shader->structs.has(st.name)) {
  5073. _set_error("Redefinition of '" + String(st.name) + "'");
  5074. return ERR_PARSE_ERROR;
  5075. }
  5076. tk = _get_token();
  5077. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  5078. _set_error("Expected '{'");
  5079. return ERR_PARSE_ERROR;
  5080. }
  5081. } else {
  5082. _set_error("Expected struct identifier!");
  5083. return ERR_PARSE_ERROR;
  5084. }
  5085. StructNode *st_node = alloc_node<StructNode>();
  5086. st.shader_struct = st_node;
  5087. int member_count = 0;
  5088. Set<String> member_names;
  5089. while (true) { // variables list
  5090. tk = _get_token();
  5091. if (tk.type == TK_CURLY_BRACKET_CLOSE) {
  5092. break;
  5093. }
  5094. StringName struct_name = "";
  5095. bool struct_dt = false;
  5096. bool use_precision = false;
  5097. DataPrecision precision = DataPrecision::PRECISION_DEFAULT;
  5098. if (tk.type == TK_STRUCT) {
  5099. _set_error("nested structs are not allowed!");
  5100. return ERR_PARSE_ERROR;
  5101. }
  5102. if (is_token_precision(tk.type)) {
  5103. precision = get_token_precision(tk.type);
  5104. use_precision = true;
  5105. tk = _get_token();
  5106. }
  5107. if (shader->structs.has(tk.text)) {
  5108. struct_name = tk.text;
  5109. struct_dt = true;
  5110. if (use_precision) {
  5111. _set_error("Precision modifier cannot be used on structs.");
  5112. return ERR_PARSE_ERROR;
  5113. }
  5114. }
  5115. if (!is_token_datatype(tk.type) && !struct_dt) {
  5116. _set_error("Expected datatype.");
  5117. return ERR_PARSE_ERROR;
  5118. } else {
  5119. type = struct_dt ? TYPE_STRUCT : get_token_datatype(tk.type);
  5120. if (is_sampler_type(type)) {
  5121. _set_error("sampler datatype not allowed here");
  5122. return ERR_PARSE_ERROR;
  5123. } else if (type == TYPE_VOID) {
  5124. _set_error("void datatype not allowed here");
  5125. return ERR_PARSE_ERROR;
  5126. }
  5127. bool first = true;
  5128. bool fixed_array_size = false;
  5129. int array_size = 0;
  5130. do {
  5131. tk = _get_token();
  5132. if (first) {
  5133. first = false;
  5134. if (tk.type != TK_IDENTIFIER && tk.type != TK_BRACKET_OPEN) {
  5135. _set_error("Expected identifier or '['.");
  5136. return ERR_PARSE_ERROR;
  5137. }
  5138. if (tk.type == TK_BRACKET_OPEN) {
  5139. Error error = _parse_array_size(nullptr, &array_size);
  5140. if (error != OK) {
  5141. return error;
  5142. }
  5143. fixed_array_size = true;
  5144. tk = _get_token();
  5145. }
  5146. }
  5147. if (tk.type != TK_IDENTIFIER) {
  5148. _set_error("Expected identifier!");
  5149. return ERR_PARSE_ERROR;
  5150. }
  5151. MemberNode *member = alloc_node<MemberNode>();
  5152. member->precision = precision;
  5153. member->datatype = type;
  5154. member->struct_name = struct_name;
  5155. member->name = tk.text;
  5156. member->array_size = array_size;
  5157. if (member_names.has(member->name)) {
  5158. _set_error("Redefinition of '" + String(member->name) + "'");
  5159. return ERR_PARSE_ERROR;
  5160. }
  5161. member_names.insert(member->name);
  5162. tk = _get_token();
  5163. if (tk.type == TK_BRACKET_OPEN) {
  5164. Error error = _parse_array_size(nullptr, &member->array_size);
  5165. if (error != OK) {
  5166. return error;
  5167. }
  5168. tk = _get_token();
  5169. }
  5170. if (!fixed_array_size) {
  5171. array_size = 0;
  5172. }
  5173. if (tk.type != TK_SEMICOLON && tk.type != TK_COMMA) {
  5174. _set_error("Expected ',' or ';' after struct member.");
  5175. return ERR_PARSE_ERROR;
  5176. }
  5177. st_node->members.push_back(member);
  5178. member_count++;
  5179. } while (tk.type == TK_COMMA); // another member
  5180. }
  5181. }
  5182. if (member_count == 0) {
  5183. _set_error("Empty structs are not allowed!");
  5184. return ERR_PARSE_ERROR;
  5185. }
  5186. tk = _get_token();
  5187. if (tk.type != TK_SEMICOLON) {
  5188. _set_error("Expected ';'");
  5189. return ERR_PARSE_ERROR;
  5190. }
  5191. shader->structs[st.name] = st;
  5192. shader->vstructs.push_back(st); // struct's order is important!
  5193. } break;
  5194. case TK_UNIFORM:
  5195. case TK_VARYING: {
  5196. bool uniform = tk.type == TK_UNIFORM;
  5197. DataPrecision precision = PRECISION_DEFAULT;
  5198. DataInterpolation interpolation = INTERPOLATION_SMOOTH;
  5199. DataType type;
  5200. StringName name;
  5201. int array_size = 0;
  5202. tk = _get_token();
  5203. if (is_token_interpolation(tk.type)) {
  5204. interpolation = get_token_interpolation(tk.type);
  5205. tk = _get_token();
  5206. }
  5207. if (is_token_precision(tk.type)) {
  5208. precision = get_token_precision(tk.type);
  5209. tk = _get_token();
  5210. }
  5211. if (!is_token_datatype(tk.type)) {
  5212. _set_error("Expected datatype. ");
  5213. return ERR_PARSE_ERROR;
  5214. }
  5215. type = get_token_datatype(tk.type);
  5216. if (type == TYPE_VOID) {
  5217. _set_error("void datatype not allowed here");
  5218. return ERR_PARSE_ERROR;
  5219. }
  5220. if (!uniform && (type < TYPE_FLOAT || type > TYPE_MAT4)) {
  5221. _set_error("Invalid type for varying, only float,vec2,vec3,vec4,mat2,mat3,mat4 or array of these types allowed.");
  5222. return ERR_PARSE_ERROR;
  5223. }
  5224. tk = _get_token();
  5225. if (tk.type == TK_BRACKET_OPEN) {
  5226. if (uniform) {
  5227. _set_error(vformat("Uniform arrays are not yet implemented!"));
  5228. return ERR_PARSE_ERROR;
  5229. }
  5230. tk = _get_token();
  5231. if (tk.type == TK_INT_CONSTANT && tk.constant > 0) {
  5232. array_size = (int)tk.constant;
  5233. tk = _get_token();
  5234. if (tk.type == TK_BRACKET_CLOSE) {
  5235. tk = _get_token();
  5236. } else {
  5237. _set_error("Expected ']'");
  5238. return ERR_PARSE_ERROR;
  5239. }
  5240. } else {
  5241. _set_error("Expected integer constant > 0");
  5242. return ERR_PARSE_ERROR;
  5243. }
  5244. }
  5245. if (tk.type != TK_IDENTIFIER) {
  5246. _set_error("Expected identifier!");
  5247. return ERR_PARSE_ERROR;
  5248. }
  5249. TkPos name_pos = _get_tkpos();
  5250. name = tk.text;
  5251. if (_find_identifier(nullptr, Map<StringName, BuiltInInfo>(), name)) {
  5252. _set_error("Redefinition of '" + String(name) + "'");
  5253. return ERR_PARSE_ERROR;
  5254. }
  5255. if (has_builtin(p_functions, name)) {
  5256. _set_error("Redefinition of '" + String(name) + "'");
  5257. return ERR_PARSE_ERROR;
  5258. }
  5259. if (uniform) {
  5260. ShaderNode::Uniform uniform2;
  5261. if (is_sampler_type(type)) {
  5262. uniform2.texture_order = texture_uniforms++;
  5263. uniform2.order = -1;
  5264. if (_validate_datatype(type) != OK) {
  5265. return ERR_PARSE_ERROR;
  5266. }
  5267. } else {
  5268. uniform2.texture_order = -1;
  5269. uniform2.order = uniforms++;
  5270. }
  5271. uniform2.type = type;
  5272. uniform2.precision = precision;
  5273. //todo parse default value
  5274. tk = _get_token();
  5275. if (tk.type == TK_COLON) {
  5276. completion_type = COMPLETION_HINT;
  5277. completion_base = type;
  5278. //hint
  5279. tk = _get_token();
  5280. completion_line = tk.line;
  5281. if (!is_token_hint(tk.type)) {
  5282. _set_error("Expected valid type hint after ':'.");
  5283. return ERR_PARSE_ERROR;
  5284. }
  5285. if (tk.type == TK_HINT_WHITE_TEXTURE) {
  5286. uniform2.hint = ShaderNode::Uniform::HINT_WHITE;
  5287. } else if (tk.type == TK_HINT_BLACK_TEXTURE) {
  5288. uniform2.hint = ShaderNode::Uniform::HINT_BLACK;
  5289. } else if (tk.type == TK_HINT_TRANSPARENT_TEXTURE) {
  5290. uniform2.hint = ShaderNode::Uniform::HINT_TRANSPARENT;
  5291. } else if (tk.type == TK_HINT_NORMAL_TEXTURE) {
  5292. uniform2.hint = ShaderNode::Uniform::HINT_NORMAL;
  5293. } else if (tk.type == TK_HINT_ANISO_TEXTURE) {
  5294. uniform2.hint = ShaderNode::Uniform::HINT_ANISO;
  5295. } else if (tk.type == TK_HINT_ALBEDO_TEXTURE) {
  5296. uniform2.hint = ShaderNode::Uniform::HINT_ALBEDO;
  5297. } else if (tk.type == TK_HINT_BLACK_ALBEDO_TEXTURE) {
  5298. uniform2.hint = ShaderNode::Uniform::HINT_BLACK_ALBEDO;
  5299. } else if (tk.type == TK_HINT_COLOR) {
  5300. if (type != TYPE_VEC4) {
  5301. _set_error("Color hint is for vec4 only");
  5302. return ERR_PARSE_ERROR;
  5303. }
  5304. uniform2.hint = ShaderNode::Uniform::HINT_COLOR;
  5305. } else if (tk.type == TK_HINT_RANGE) {
  5306. uniform2.hint = ShaderNode::Uniform::HINT_RANGE;
  5307. if (type != TYPE_FLOAT && type != TYPE_INT) {
  5308. _set_error("Range hint is for float and int only");
  5309. return ERR_PARSE_ERROR;
  5310. }
  5311. tk = _get_token();
  5312. if (tk.type != TK_PARENTHESIS_OPEN) {
  5313. _set_error("Expected '(' after hint_range");
  5314. return ERR_PARSE_ERROR;
  5315. }
  5316. tk = _get_token();
  5317. float sign = 1.0;
  5318. if (tk.type == TK_OP_SUB) {
  5319. sign = -1.0;
  5320. tk = _get_token();
  5321. }
  5322. if (tk.type != TK_REAL_CONSTANT && !tk.is_integer_constant()) {
  5323. _set_error("Expected integer constant");
  5324. return ERR_PARSE_ERROR;
  5325. }
  5326. uniform2.hint_range[0] = tk.constant;
  5327. uniform2.hint_range[0] *= sign;
  5328. tk = _get_token();
  5329. if (tk.type != TK_COMMA) {
  5330. _set_error("Expected ',' after integer constant");
  5331. return ERR_PARSE_ERROR;
  5332. }
  5333. tk = _get_token();
  5334. sign = 1.0;
  5335. if (tk.type == TK_OP_SUB) {
  5336. sign = -1.0;
  5337. tk = _get_token();
  5338. }
  5339. if (tk.type != TK_REAL_CONSTANT && !tk.is_integer_constant()) {
  5340. _set_error("Expected integer constant after ','");
  5341. return ERR_PARSE_ERROR;
  5342. }
  5343. uniform2.hint_range[1] = tk.constant;
  5344. uniform2.hint_range[1] *= sign;
  5345. tk = _get_token();
  5346. if (tk.type == TK_COMMA) {
  5347. tk = _get_token();
  5348. if (tk.type != TK_REAL_CONSTANT && !tk.is_integer_constant()) {
  5349. _set_error("Expected integer constant after ','");
  5350. return ERR_PARSE_ERROR;
  5351. }
  5352. uniform2.hint_range[2] = tk.constant;
  5353. tk = _get_token();
  5354. } else {
  5355. if (type == TYPE_INT) {
  5356. uniform2.hint_range[2] = 1;
  5357. } else {
  5358. uniform2.hint_range[2] = 0.001;
  5359. }
  5360. }
  5361. if (tk.type != TK_PARENTHESIS_CLOSE) {
  5362. _set_error("Expected ','");
  5363. return ERR_PARSE_ERROR;
  5364. }
  5365. }
  5366. if (uniform2.hint != ShaderNode::Uniform::HINT_RANGE && uniform2.hint != ShaderNode::Uniform::HINT_NONE && uniform2.hint != ShaderNode::Uniform::HINT_COLOR && type <= TYPE_MAT4) {
  5367. _set_error("This hint is only for sampler types");
  5368. return ERR_PARSE_ERROR;
  5369. }
  5370. tk = _get_token();
  5371. }
  5372. if (tk.type == TK_OP_ASSIGN) {
  5373. Node *expr = _parse_and_reduce_expression(nullptr, Map<StringName, BuiltInInfo>());
  5374. if (!expr) {
  5375. return ERR_PARSE_ERROR;
  5376. }
  5377. if (expr->type != Node::TYPE_CONSTANT) {
  5378. _set_error("Expected constant expression after '='");
  5379. return ERR_PARSE_ERROR;
  5380. }
  5381. ConstantNode *cn = static_cast<ConstantNode *>(expr);
  5382. uniform2.default_value.resize(cn->values.size());
  5383. if (!convert_constant(cn, uniform2.type, uniform2.default_value.ptrw())) {
  5384. _set_error("Can't convert constant to " + get_datatype_name(uniform2.type));
  5385. return ERR_PARSE_ERROR;
  5386. }
  5387. tk = _get_token();
  5388. }
  5389. shader->uniforms[name] = uniform2;
  5390. if (tk.type != TK_SEMICOLON) {
  5391. _set_error("Expected ';'");
  5392. return ERR_PARSE_ERROR;
  5393. }
  5394. completion_type = COMPLETION_NONE;
  5395. } else { // varying
  5396. ShaderNode::Varying varying;
  5397. varying.type = type;
  5398. varying.precision = precision;
  5399. varying.interpolation = interpolation;
  5400. varying.tkpos = name_pos;
  5401. varying.array_size = array_size;
  5402. tk = _get_token();
  5403. if (tk.type != TK_SEMICOLON && tk.type != TK_BRACKET_OPEN) {
  5404. _set_error("Expected ';' or '['");
  5405. return ERR_PARSE_ERROR;
  5406. }
  5407. if (tk.type == TK_BRACKET_OPEN) {
  5408. if (array_size > 0) {
  5409. _set_error("Array size is already defined!");
  5410. return ERR_PARSE_ERROR;
  5411. }
  5412. tk = _get_token();
  5413. if (tk.is_integer_constant() && tk.constant > 0) {
  5414. varying.array_size = (int)tk.constant;
  5415. tk = _get_token();
  5416. if (tk.type == TK_BRACKET_CLOSE) {
  5417. tk = _get_token();
  5418. if (tk.type != TK_SEMICOLON) {
  5419. _set_error("Expected ';'");
  5420. return ERR_PARSE_ERROR;
  5421. }
  5422. } else {
  5423. _set_error("Expected ']'");
  5424. return ERR_PARSE_ERROR;
  5425. }
  5426. } else {
  5427. _set_error("Expected single integer constant > 0");
  5428. return ERR_PARSE_ERROR;
  5429. }
  5430. }
  5431. shader->varyings[name] = varying;
  5432. }
  5433. } break;
  5434. case TK_SHADER_TYPE: {
  5435. _set_error("Shader type is already defined.");
  5436. return ERR_PARSE_ERROR;
  5437. } break;
  5438. default: {
  5439. //function or constant variable
  5440. bool is_constant = false;
  5441. bool is_struct = false;
  5442. StringName struct_name;
  5443. DataPrecision precision = PRECISION_DEFAULT;
  5444. DataType type;
  5445. StringName name;
  5446. if (tk.type == TK_CONST) {
  5447. is_constant = true;
  5448. tk = _get_token();
  5449. }
  5450. if (is_token_precision(tk.type)) {
  5451. precision = get_token_precision(tk.type);
  5452. tk = _get_token();
  5453. }
  5454. if (shader->structs.has(tk.text)) {
  5455. if (precision != PRECISION_DEFAULT) {
  5456. _set_error("Precision modifier cannot be used on structs.");
  5457. return ERR_PARSE_ERROR;
  5458. }
  5459. is_struct = true;
  5460. struct_name = tk.text;
  5461. } else {
  5462. if (!is_token_datatype(tk.type)) {
  5463. _set_error("Expected constant, function, uniform or varying");
  5464. return ERR_PARSE_ERROR;
  5465. }
  5466. if (!is_token_variable_datatype(tk.type)) {
  5467. _set_error("Invalid data type for constants or function return (samplers not allowed)");
  5468. return ERR_PARSE_ERROR;
  5469. }
  5470. }
  5471. if (is_struct) {
  5472. type = TYPE_STRUCT;
  5473. } else {
  5474. type = get_token_datatype(tk.type);
  5475. }
  5476. TkPos prev_pos = _get_tkpos();
  5477. tk = _get_token();
  5478. if (tk.type == TK_BRACKET_OPEN) {
  5479. _set_error("Cannot use arrays as return types");
  5480. return ERR_PARSE_ERROR;
  5481. }
  5482. _set_tkpos(prev_pos);
  5483. _get_completable_identifier(nullptr, COMPLETION_MAIN_FUNCTION, name);
  5484. if (name == StringName()) {
  5485. _set_error("Expected function name after datatype");
  5486. return ERR_PARSE_ERROR;
  5487. }
  5488. if (_find_identifier(nullptr, Map<StringName, BuiltInInfo>(), name)) {
  5489. _set_error("Redefinition of '" + String(name) + "'");
  5490. return ERR_PARSE_ERROR;
  5491. }
  5492. if (has_builtin(p_functions, name)) {
  5493. _set_error("Redefinition of '" + String(name) + "'");
  5494. return ERR_PARSE_ERROR;
  5495. }
  5496. tk = _get_token();
  5497. if (tk.type != TK_PARENTHESIS_OPEN) {
  5498. if (type == TYPE_VOID) {
  5499. _set_error("Expected '(' after function identifier");
  5500. return ERR_PARSE_ERROR;
  5501. }
  5502. //variable
  5503. while (true) {
  5504. ShaderNode::Constant constant;
  5505. constant.name = name;
  5506. constant.type = is_struct ? TYPE_STRUCT : type;
  5507. constant.type_str = struct_name;
  5508. constant.precision = precision;
  5509. constant.initializer = nullptr;
  5510. constant.array_size = 0;
  5511. bool unknown_size = false;
  5512. if (tk.type == TK_BRACKET_OPEN) {
  5513. if (VisualServer::get_singleton()->is_low_end()) {
  5514. _set_error("Global const arrays are supported only on high-end platform!");
  5515. return ERR_PARSE_ERROR;
  5516. }
  5517. tk = _get_token();
  5518. if (tk.type == TK_BRACKET_CLOSE) {
  5519. unknown_size = true;
  5520. tk = _get_token();
  5521. } else if (tk.is_integer_constant() && ((int)tk.constant) > 0) {
  5522. constant.array_size = (int)tk.constant;
  5523. tk = _get_token();
  5524. if (tk.type != TK_BRACKET_CLOSE) {
  5525. _set_error("Expected ']'");
  5526. return ERR_PARSE_ERROR;
  5527. }
  5528. tk = _get_token();
  5529. } else {
  5530. _set_error("Expected integer constant > 0 or ']'");
  5531. return ERR_PARSE_ERROR;
  5532. }
  5533. }
  5534. if (tk.type == TK_OP_ASSIGN) {
  5535. if (!is_constant) {
  5536. _set_error("Expected 'const' keyword before constant definition");
  5537. return ERR_PARSE_ERROR;
  5538. }
  5539. if (constant.array_size > 0 || unknown_size) {
  5540. bool full_def = false;
  5541. ArrayDeclarationNode::Declaration decl;
  5542. decl.name = name;
  5543. decl.size = constant.array_size;
  5544. tk = _get_token();
  5545. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  5546. if (unknown_size) {
  5547. _set_error("Expected '{'");
  5548. return ERR_PARSE_ERROR;
  5549. }
  5550. full_def = true;
  5551. DataPrecision precision2 = PRECISION_DEFAULT;
  5552. if (is_token_precision(tk.type)) {
  5553. precision2 = get_token_precision(tk.type);
  5554. tk = _get_token();
  5555. if (!is_token_nonvoid_datatype(tk.type)) {
  5556. _set_error("Expected datatype after precision");
  5557. return ERR_PARSE_ERROR;
  5558. }
  5559. }
  5560. StringName struct_name2;
  5561. DataType type2;
  5562. if (shader->structs.has(tk.text)) {
  5563. type2 = TYPE_STRUCT;
  5564. struct_name2 = tk.text;
  5565. } else {
  5566. if (!is_token_variable_datatype(tk.type)) {
  5567. _set_error("Invalid data type for array");
  5568. return ERR_PARSE_ERROR;
  5569. }
  5570. type2 = get_token_datatype(tk.type);
  5571. }
  5572. int array_size2 = 0;
  5573. tk = _get_token();
  5574. if (tk.type == TK_BRACKET_OPEN) {
  5575. TkPos pos2 = _get_tkpos();
  5576. tk = _get_token();
  5577. if (tk.type == TK_BRACKET_CLOSE) {
  5578. array_size2 = constant.array_size;
  5579. tk = _get_token();
  5580. } else {
  5581. _set_tkpos(pos2);
  5582. Node *n = _parse_and_reduce_expression(NULL, Map<StringName, BuiltInInfo>());
  5583. if (!n || n->type != Node::TYPE_CONSTANT || n->get_datatype() != TYPE_INT) {
  5584. _set_error("Expected single integer constant > 0");
  5585. return ERR_PARSE_ERROR;
  5586. }
  5587. ConstantNode *cnode = (ConstantNode *)n;
  5588. if (cnode->values.size() == 1) {
  5589. array_size2 = cnode->values[0].sint;
  5590. if (array_size2 <= 0) {
  5591. _set_error("Expected single integer constant > 0");
  5592. return ERR_PARSE_ERROR;
  5593. }
  5594. } else {
  5595. _set_error("Expected single integer constant > 0");
  5596. return ERR_PARSE_ERROR;
  5597. }
  5598. tk = _get_token();
  5599. if (tk.type != TK_BRACKET_CLOSE) {
  5600. _set_error("Expected ']");
  5601. return ERR_PARSE_ERROR;
  5602. } else {
  5603. tk = _get_token();
  5604. }
  5605. }
  5606. } else {
  5607. _set_error("Expected '[");
  5608. return ERR_PARSE_ERROR;
  5609. }
  5610. if (constant.precision != precision2 || constant.type != type2 || struct_name != struct_name2 || constant.array_size != array_size2) {
  5611. String error_str = "Cannot convert from '";
  5612. if (type2 == TYPE_STRUCT) {
  5613. error_str += struct_name2;
  5614. } else {
  5615. if (precision2 != PRECISION_DEFAULT) {
  5616. error_str += get_precision_name(precision2);
  5617. error_str += " ";
  5618. }
  5619. error_str += get_datatype_name(type2);
  5620. }
  5621. error_str += "[";
  5622. error_str += itos(array_size2);
  5623. error_str += "]'";
  5624. error_str += " to '";
  5625. if (type == TYPE_STRUCT) {
  5626. error_str += struct_name;
  5627. } else {
  5628. if (precision != PRECISION_DEFAULT) {
  5629. error_str += get_precision_name(precision);
  5630. error_str += " ";
  5631. }
  5632. error_str += get_datatype_name(type);
  5633. }
  5634. error_str += "[";
  5635. error_str += itos(constant.array_size);
  5636. error_str += "]'";
  5637. _set_error(error_str);
  5638. return ERR_PARSE_ERROR;
  5639. }
  5640. }
  5641. bool curly = tk.type == TK_CURLY_BRACKET_OPEN;
  5642. if (unknown_size) {
  5643. if (!curly) {
  5644. _set_error("Expected '{'");
  5645. return ERR_PARSE_ERROR;
  5646. }
  5647. } else {
  5648. if (full_def) {
  5649. if (curly) {
  5650. _set_error("Expected '('");
  5651. return ERR_PARSE_ERROR;
  5652. }
  5653. }
  5654. }
  5655. if (tk.type == TK_PARENTHESIS_OPEN || curly) { // initialization
  5656. while (true) {
  5657. Node *n = _parse_and_reduce_expression(NULL, Map<StringName, BuiltInInfo>());
  5658. if (!n) {
  5659. return ERR_PARSE_ERROR;
  5660. }
  5661. if (n->type == Node::TYPE_OPERATOR && ((OperatorNode *)n)->op == OP_CALL) {
  5662. _set_error("Expected constant expression");
  5663. return ERR_PARSE_ERROR;
  5664. }
  5665. if (constant.type != n->get_datatype() || n->get_datatype_name() != struct_name) {
  5666. _set_error("Invalid assignment of '" + (n->get_datatype() == TYPE_STRUCT ? n->get_datatype_name() : get_datatype_name(n->get_datatype())) + "' to '" + (is_struct ? String(struct_name) : get_datatype_name(constant.type)) + "'");
  5667. return ERR_PARSE_ERROR;
  5668. }
  5669. tk = _get_token();
  5670. if (tk.type == TK_COMMA) {
  5671. decl.initializer.push_back(n);
  5672. continue;
  5673. } else if (!curly && tk.type == TK_PARENTHESIS_CLOSE) {
  5674. decl.initializer.push_back(n);
  5675. break;
  5676. } else if (curly && tk.type == TK_CURLY_BRACKET_CLOSE) {
  5677. decl.initializer.push_back(n);
  5678. break;
  5679. } else {
  5680. if (curly)
  5681. _set_error("Expected '}' or ','");
  5682. else
  5683. _set_error("Expected ')' or ','");
  5684. return ERR_PARSE_ERROR;
  5685. }
  5686. }
  5687. if (unknown_size) {
  5688. decl.size = decl.initializer.size();
  5689. constant.array_size = decl.initializer.size();
  5690. } else if (decl.initializer.size() != constant.array_size) {
  5691. _set_error("Array size mismatch");
  5692. return ERR_PARSE_ERROR;
  5693. }
  5694. }
  5695. ConstantNode *expr = memnew(ConstantNode);
  5696. expr->datatype = constant.type;
  5697. expr->struct_name = constant.type_str;
  5698. expr->array_size = constant.array_size;
  5699. expr->array_declarations.push_back(decl);
  5700. constant.initializer = static_cast<ConstantNode *>(expr);
  5701. } else {
  5702. //variable created with assignment! must parse an expression
  5703. Node *expr = _parse_and_reduce_expression(NULL, Map<StringName, BuiltInInfo>());
  5704. if (!expr)
  5705. return ERR_PARSE_ERROR;
  5706. if (expr->type == Node::TYPE_OPERATOR && ((OperatorNode *)expr)->op == OP_CALL) {
  5707. _set_error("Expected constant expression after '='");
  5708. return ERR_PARSE_ERROR;
  5709. }
  5710. constant.initializer = static_cast<ConstantNode *>(expr);
  5711. if (type != expr->get_datatype() || expr->get_datatype_name() != struct_name) {
  5712. _set_error("Invalid assignment of '" + (expr->get_datatype() == TYPE_STRUCT ? expr->get_datatype_name() : get_datatype_name(expr->get_datatype())) + "' to '" + (is_struct ? String(struct_name) : get_datatype_name(type)) + "'");
  5713. return ERR_PARSE_ERROR;
  5714. }
  5715. }
  5716. tk = _get_token();
  5717. } else {
  5718. if (constant.array_size > 0 || unknown_size) {
  5719. _set_error("Expected array initialization");
  5720. return ERR_PARSE_ERROR;
  5721. } else {
  5722. _set_error("Expected initialization of constant");
  5723. return ERR_PARSE_ERROR;
  5724. }
  5725. }
  5726. shader->constants[name] = constant;
  5727. shader->vconstants.push_back(constant);
  5728. if (tk.type == TK_COMMA) {
  5729. tk = _get_token();
  5730. if (tk.type != TK_IDENTIFIER) {
  5731. _set_error("Expected identifier after type");
  5732. return ERR_PARSE_ERROR;
  5733. }
  5734. name = tk.text;
  5735. if (_find_identifier(nullptr, Map<StringName, BuiltInInfo>(), name)) {
  5736. _set_error("Redefinition of '" + String(name) + "'");
  5737. return ERR_PARSE_ERROR;
  5738. }
  5739. if (has_builtin(p_functions, name)) {
  5740. _set_error("Redefinition of '" + String(name) + "'");
  5741. return ERR_PARSE_ERROR;
  5742. }
  5743. tk = _get_token();
  5744. } else if (tk.type == TK_SEMICOLON) {
  5745. break;
  5746. } else {
  5747. _set_error("Expected ',' or ';' after constant");
  5748. return ERR_PARSE_ERROR;
  5749. }
  5750. }
  5751. break;
  5752. }
  5753. Map<StringName, BuiltInInfo> builtin_types;
  5754. if (p_functions.has(name)) {
  5755. builtin_types = p_functions[name].built_ins;
  5756. }
  5757. if (p_functions.has("global")) { // Adds global variables: 'TIME'
  5758. for (Map<StringName, BuiltInInfo>::Element *E = p_functions["global"].built_ins.front(); E; E = E->next()) {
  5759. builtin_types.insert(E->key(), E->value());
  5760. }
  5761. }
  5762. for (int i = 0; i < shader->functions.size(); i++) {
  5763. if (!shader->functions[i].callable && shader->functions[i].name == name) {
  5764. _set_error("Redefinition of '" + String(name) + "'");
  5765. return ERR_PARSE_ERROR;
  5766. }
  5767. }
  5768. ShaderNode::Function function;
  5769. function.callable = !p_functions.has(name);
  5770. function.name = name;
  5771. FunctionNode *func_node = alloc_node<FunctionNode>();
  5772. function.function = func_node;
  5773. shader->functions.push_back(function);
  5774. func_node->name = name;
  5775. func_node->return_type = type;
  5776. func_node->return_struct_name = struct_name;
  5777. func_node->return_precision = precision;
  5778. if (p_functions.has(name)) {
  5779. func_node->can_discard = p_functions[name].can_discard;
  5780. }
  5781. func_node->body = alloc_node<BlockNode>();
  5782. func_node->body->parent_function = func_node;
  5783. tk = _get_token();
  5784. while (true) {
  5785. if (tk.type == TK_PARENTHESIS_CLOSE) {
  5786. break;
  5787. }
  5788. bool is_const = false;
  5789. if (tk.type == TK_CONST) {
  5790. is_const = true;
  5791. tk = _get_token();
  5792. }
  5793. ArgumentQualifier qualifier = ARGUMENT_QUALIFIER_IN;
  5794. if (tk.type == TK_ARG_IN) {
  5795. qualifier = ARGUMENT_QUALIFIER_IN;
  5796. tk = _get_token();
  5797. } else if (tk.type == TK_ARG_OUT) {
  5798. if (is_const) {
  5799. _set_error("'out' qualifier cannot be used within a function parameter declared with 'const'.");
  5800. return ERR_PARSE_ERROR;
  5801. }
  5802. qualifier = ARGUMENT_QUALIFIER_OUT;
  5803. tk = _get_token();
  5804. } else if (tk.type == TK_ARG_INOUT) {
  5805. if (is_const) {
  5806. _set_error("'inout' qualifier cannot be used within a function parameter declared with 'const'.");
  5807. return ERR_PARSE_ERROR;
  5808. }
  5809. qualifier = ARGUMENT_QUALIFIER_INOUT;
  5810. tk = _get_token();
  5811. }
  5812. DataType ptype;
  5813. StringName pname;
  5814. StringName param_struct_name;
  5815. DataPrecision pprecision = PRECISION_DEFAULT;
  5816. bool use_precision = false;
  5817. if (is_token_precision(tk.type)) {
  5818. pprecision = get_token_precision(tk.type);
  5819. tk = _get_token();
  5820. use_precision = true;
  5821. }
  5822. is_struct = false;
  5823. if (shader->structs.has(tk.text)) {
  5824. is_struct = true;
  5825. param_struct_name = tk.text;
  5826. if (use_precision) {
  5827. _set_error("Precision modifier cannot be used on structs.");
  5828. return ERR_PARSE_ERROR;
  5829. }
  5830. }
  5831. if (!is_struct && !is_token_datatype(tk.type)) {
  5832. _set_error("Expected a valid datatype for argument");
  5833. return ERR_PARSE_ERROR;
  5834. }
  5835. if (is_struct) {
  5836. ptype = TYPE_STRUCT;
  5837. } else {
  5838. ptype = get_token_datatype(tk.type);
  5839. if (_validate_datatype(ptype) != OK) {
  5840. return ERR_PARSE_ERROR;
  5841. }
  5842. if (ptype == TYPE_VOID) {
  5843. _set_error("void not allowed in argument");
  5844. return ERR_PARSE_ERROR;
  5845. }
  5846. }
  5847. tk = _get_token();
  5848. if (tk.type == TK_BRACKET_OPEN) {
  5849. _set_error("Arrays as parameters are not implemented yet");
  5850. return ERR_PARSE_ERROR;
  5851. }
  5852. if (tk.type != TK_IDENTIFIER) {
  5853. _set_error("Expected identifier for argument name");
  5854. return ERR_PARSE_ERROR;
  5855. }
  5856. pname = tk.text;
  5857. ShaderLanguage::IdentifierType itype;
  5858. if (_find_identifier(func_node->body, builtin_types, pname, (ShaderLanguage::DataType *)nullptr, &itype)) {
  5859. if (itype != IDENTIFIER_FUNCTION) {
  5860. _set_error("Redefinition of '" + String(pname) + "'");
  5861. return ERR_PARSE_ERROR;
  5862. }
  5863. }
  5864. if (has_builtin(p_functions, pname)) {
  5865. _set_error("Redefinition of '" + String(pname) + "'");
  5866. return ERR_PARSE_ERROR;
  5867. }
  5868. FunctionNode::Argument arg;
  5869. arg.type = ptype;
  5870. arg.name = pname;
  5871. arg.type_str = param_struct_name;
  5872. arg.precision = pprecision;
  5873. arg.qualifier = qualifier;
  5874. arg.is_const = is_const;
  5875. func_node->arguments.push_back(arg);
  5876. tk = _get_token();
  5877. if (tk.type == TK_BRACKET_OPEN) {
  5878. _set_error("Arrays as parameters are not implemented yet");
  5879. return ERR_PARSE_ERROR;
  5880. }
  5881. if (tk.type == TK_COMMA) {
  5882. tk = _get_token();
  5883. //do none and go on
  5884. } else if (tk.type != TK_PARENTHESIS_CLOSE) {
  5885. _set_error("Expected ',' or ')' after identifier");
  5886. return ERR_PARSE_ERROR;
  5887. }
  5888. }
  5889. if (p_functions.has(name)) {
  5890. //if one of the core functions, make sure they are of the correct form
  5891. if (func_node->arguments.size() > 0) {
  5892. _set_error("Function '" + String(name) + "' expects no arguments.");
  5893. return ERR_PARSE_ERROR;
  5894. }
  5895. if (func_node->return_type != TYPE_VOID) {
  5896. _set_error("Function '" + String(name) + "' must be of void return type.");
  5897. return ERR_PARSE_ERROR;
  5898. }
  5899. }
  5900. //all good let's parse inside the function!
  5901. tk = _get_token();
  5902. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  5903. _set_error("Expected '{' to begin function");
  5904. return ERR_PARSE_ERROR;
  5905. }
  5906. current_function = name;
  5907. Error err = _parse_block(func_node->body, builtin_types);
  5908. if (err) {
  5909. return err;
  5910. }
  5911. if (func_node->return_type != DataType::TYPE_VOID) {
  5912. BlockNode *block = func_node->body;
  5913. if (_find_last_flow_op_in_block(block, FlowOperation::FLOW_OP_RETURN) != OK) {
  5914. _set_error("Expected at least one return statement in a non-void function.");
  5915. return ERR_PARSE_ERROR;
  5916. }
  5917. }
  5918. current_function = StringName();
  5919. }
  5920. }
  5921. tk = _get_token();
  5922. }
  5923. return OK;
  5924. }
  5925. bool ShaderLanguage::has_builtin(const Map<StringName, ShaderLanguage::FunctionInfo> &p_functions, const StringName &p_name) {
  5926. if (p_functions.has("vertex")) {
  5927. if (p_functions["vertex"].built_ins.has(p_name)) {
  5928. return true;
  5929. }
  5930. }
  5931. if (p_functions.has("fragment")) {
  5932. if (p_functions["fragment"].built_ins.has(p_name)) {
  5933. return true;
  5934. }
  5935. }
  5936. if (p_functions.has("light")) {
  5937. if (p_functions["light"].built_ins.has(p_name)) {
  5938. return true;
  5939. }
  5940. }
  5941. return false;
  5942. }
  5943. Error ShaderLanguage::_find_last_flow_op_in_op(ControlFlowNode *p_flow, FlowOperation p_op) {
  5944. bool found = false;
  5945. for (int i = p_flow->blocks.size() - 1; i >= 0; i--) {
  5946. if (p_flow->blocks[i]->type == Node::TYPE_BLOCK) {
  5947. BlockNode *last_block = (BlockNode *)p_flow->blocks[i];
  5948. if (_find_last_flow_op_in_block(last_block, p_op) == OK) {
  5949. found = true;
  5950. break;
  5951. }
  5952. }
  5953. }
  5954. if (found) {
  5955. return OK;
  5956. }
  5957. return FAILED;
  5958. }
  5959. Error ShaderLanguage::_find_last_flow_op_in_block(BlockNode *p_block, FlowOperation p_op) {
  5960. bool found = false;
  5961. for (int i = p_block->statements.size() - 1; i >= 0; i--) {
  5962. if (p_block->statements[i]->type == Node::TYPE_CONTROL_FLOW) {
  5963. ControlFlowNode *flow = (ControlFlowNode *)p_block->statements[i];
  5964. if (flow->flow_op == p_op) {
  5965. found = true;
  5966. break;
  5967. } else {
  5968. if (_find_last_flow_op_in_op(flow, p_op) == OK) {
  5969. found = true;
  5970. break;
  5971. }
  5972. }
  5973. } else if (p_block->statements[i]->type == Node::TYPE_BLOCK) {
  5974. BlockNode *block = (BlockNode *)p_block->statements[i];
  5975. if (_find_last_flow_op_in_block(block, p_op) == OK) {
  5976. found = true;
  5977. break;
  5978. }
  5979. }
  5980. }
  5981. if (found) {
  5982. return OK;
  5983. }
  5984. return FAILED;
  5985. }
  5986. // skips over whitespace and /* */ and // comments
  5987. static int _get_first_ident_pos(const String &p_code) {
  5988. int idx = 0;
  5989. #define GETCHAR(m_idx) (((idx + m_idx) < p_code.length()) ? p_code[idx + m_idx] : CharType(0))
  5990. while (true) {
  5991. if (GETCHAR(0) == '/' && GETCHAR(1) == '/') {
  5992. idx += 2;
  5993. while (true) {
  5994. if (GETCHAR(0) == 0) {
  5995. return 0;
  5996. }
  5997. if (GETCHAR(0) == '\n') {
  5998. idx++;
  5999. break; // loop
  6000. }
  6001. idx++;
  6002. }
  6003. } else if (GETCHAR(0) == '/' && GETCHAR(1) == '*') {
  6004. idx += 2;
  6005. while (true) {
  6006. if (GETCHAR(0) == 0) {
  6007. return 0;
  6008. }
  6009. if (GETCHAR(0) == '*' && GETCHAR(1) == '/') {
  6010. idx += 2;
  6011. break; // loop
  6012. }
  6013. idx++;
  6014. }
  6015. } else {
  6016. switch (GETCHAR(0)) {
  6017. case ' ':
  6018. case '\t':
  6019. case '\r':
  6020. case '\n': {
  6021. idx++;
  6022. } break; // switch
  6023. default:
  6024. return idx;
  6025. }
  6026. }
  6027. }
  6028. #undef GETCHAR
  6029. }
  6030. String ShaderLanguage::get_shader_type(const String &p_code) {
  6031. bool reading_type = false;
  6032. String cur_identifier;
  6033. for (int i = _get_first_ident_pos(p_code); i < p_code.length(); i++) {
  6034. if (p_code[i] == ';') {
  6035. break;
  6036. } else if (p_code[i] <= 32) {
  6037. if (cur_identifier != String()) {
  6038. if (!reading_type) {
  6039. if (cur_identifier != "shader_type") {
  6040. return String();
  6041. }
  6042. reading_type = true;
  6043. cur_identifier = String();
  6044. } else {
  6045. return cur_identifier;
  6046. }
  6047. }
  6048. } else {
  6049. cur_identifier += String::chr(p_code[i]);
  6050. }
  6051. }
  6052. if (reading_type) {
  6053. return cur_identifier;
  6054. }
  6055. return String();
  6056. }
  6057. Error ShaderLanguage::compile(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types) {
  6058. clear();
  6059. code = p_code;
  6060. nodes = nullptr;
  6061. shader = alloc_node<ShaderNode>();
  6062. Error err = _parse_shader(p_functions, p_render_modes, p_shader_types);
  6063. if (err != OK) {
  6064. return err;
  6065. }
  6066. return OK;
  6067. }
  6068. Error ShaderLanguage::complete(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types, List<ScriptCodeCompletionOption> *r_options, String &r_call_hint) {
  6069. clear();
  6070. code = p_code;
  6071. nodes = nullptr;
  6072. shader = alloc_node<ShaderNode>();
  6073. _parse_shader(p_functions, p_render_modes, p_shader_types);
  6074. switch (completion_type) {
  6075. case COMPLETION_NONE: {
  6076. //do nothing
  6077. return OK;
  6078. } break;
  6079. case COMPLETION_SHADER_TYPE: {
  6080. for (const Set<String>::Element *E = p_shader_types.front(); E; E = E->next()) {
  6081. ScriptCodeCompletionOption option(E->get(), ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  6082. r_options->push_back(option);
  6083. }
  6084. return OK;
  6085. } break;
  6086. case COMPLETION_RENDER_MODE: {
  6087. for (int i = 0; i < p_render_modes.size(); i++) {
  6088. ScriptCodeCompletionOption option(p_render_modes[i], ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  6089. r_options->push_back(option);
  6090. }
  6091. return OK;
  6092. } break;
  6093. case COMPLETION_STRUCT: {
  6094. if (shader->structs.has(completion_struct)) {
  6095. StructNode *node = shader->structs[completion_struct].shader_struct;
  6096. for (int i = 0; i < node->members.size(); i++) {
  6097. ScriptCodeCompletionOption option(node->members[i]->name, ScriptCodeCompletionOption::KIND_MEMBER);
  6098. r_options->push_back(option);
  6099. }
  6100. }
  6101. return OK;
  6102. } break;
  6103. case COMPLETION_MAIN_FUNCTION: {
  6104. for (const Map<StringName, FunctionInfo>::Element *E = p_functions.front(); E; E = E->next()) {
  6105. if (!E->get().main_function) {
  6106. continue;
  6107. }
  6108. bool found = false;
  6109. for (int i = 0; i < shader->functions.size(); i++) {
  6110. if (shader->functions[i].name == E->key()) {
  6111. found = true;
  6112. break;
  6113. }
  6114. }
  6115. if (found) {
  6116. continue;
  6117. }
  6118. ScriptCodeCompletionOption option(E->key(), ScriptCodeCompletionOption::KIND_FUNCTION);
  6119. r_options->push_back(option);
  6120. }
  6121. return OK;
  6122. } break;
  6123. case COMPLETION_IDENTIFIER:
  6124. case COMPLETION_FUNCTION_CALL: {
  6125. bool comp_ident = completion_type == COMPLETION_IDENTIFIER;
  6126. Map<String, ScriptCodeCompletionOption::Kind> matches;
  6127. StringName skip_function;
  6128. BlockNode *block = completion_block;
  6129. if (completion_class == TAG_GLOBAL) {
  6130. while (block) {
  6131. if (comp_ident) {
  6132. for (const Map<StringName, BlockNode::Variable>::Element *E = block->variables.front(); E; E = E->next()) {
  6133. if (E->get().line < completion_line) {
  6134. matches.insert(E->key(), ScriptCodeCompletionOption::KIND_VARIABLE);
  6135. }
  6136. }
  6137. }
  6138. if (block->parent_function) {
  6139. if (comp_ident) {
  6140. for (int i = 0; i < block->parent_function->arguments.size(); i++) {
  6141. matches.insert(block->parent_function->arguments[i].name, ScriptCodeCompletionOption::KIND_VARIABLE);
  6142. }
  6143. }
  6144. skip_function = block->parent_function->name;
  6145. }
  6146. block = block->parent_block;
  6147. }
  6148. if (comp_ident) {
  6149. if (p_functions.has("global")) {
  6150. for (Map<StringName, BuiltInInfo>::Element *E = p_functions["global"].built_ins.front(); E; E = E->next()) {
  6151. ScriptCodeCompletionOption::Kind kind = ScriptCodeCompletionOption::KIND_MEMBER;
  6152. if (E->get().constant) {
  6153. kind = ScriptCodeCompletionOption::KIND_CONSTANT;
  6154. }
  6155. matches.insert(E->key(), kind);
  6156. }
  6157. }
  6158. if (skip_function != StringName() && p_functions.has(skip_function)) {
  6159. for (Map<StringName, BuiltInInfo>::Element *E = p_functions[skip_function].built_ins.front(); E; E = E->next()) {
  6160. ScriptCodeCompletionOption::Kind kind = ScriptCodeCompletionOption::KIND_MEMBER;
  6161. if (E->get().constant) {
  6162. kind = ScriptCodeCompletionOption::KIND_CONSTANT;
  6163. }
  6164. matches.insert(E->key(), kind);
  6165. }
  6166. }
  6167. for (Map<StringName, ShaderNode::Constant>::Element *E = shader->constants.front(); E; E = E->next()) {
  6168. matches.insert(E->key(), ScriptCodeCompletionOption::KIND_CONSTANT);
  6169. }
  6170. for (OrderedHashMap<StringName, ShaderNode::Varying>::Element E = shader->varyings.front(); E; E = E.next()) {
  6171. matches.insert(E.key(), ScriptCodeCompletionOption::KIND_VARIABLE);
  6172. }
  6173. for (OrderedHashMap<StringName, ShaderNode::Uniform>::Element E = shader->uniforms.front(); E; E = E.next()) {
  6174. matches.insert(E.key(), ScriptCodeCompletionOption::KIND_MEMBER);
  6175. }
  6176. }
  6177. for (int i = 0; i < shader->functions.size(); i++) {
  6178. if (!shader->functions[i].callable || shader->functions[i].name == skip_function) {
  6179. continue;
  6180. }
  6181. matches.insert(String(shader->functions[i].name), ScriptCodeCompletionOption::KIND_FUNCTION);
  6182. }
  6183. int idx = 0;
  6184. bool low_end = VisualServer::get_singleton()->is_low_end();
  6185. while (builtin_func_defs[idx].name) {
  6186. if (low_end && builtin_func_defs[idx].high_end) {
  6187. idx++;
  6188. continue;
  6189. }
  6190. matches.insert(String(builtin_func_defs[idx].name), ScriptCodeCompletionOption::KIND_FUNCTION);
  6191. idx++;
  6192. }
  6193. } else { // sub-class
  6194. int idx = 0;
  6195. bool low_end = VisualServer::get_singleton()->is_low_end();
  6196. while (builtin_func_defs[idx].name) {
  6197. if (low_end && builtin_func_defs[idx].high_end) {
  6198. idx++;
  6199. continue;
  6200. }
  6201. if (builtin_func_defs[idx].tag == completion_class) {
  6202. matches.insert(String(builtin_func_defs[idx].name), ScriptCodeCompletionOption::KIND_FUNCTION);
  6203. }
  6204. idx++;
  6205. }
  6206. }
  6207. for (Map<String, ScriptCodeCompletionOption::Kind>::Element *E = matches.front(); E; E = E->next()) {
  6208. ScriptCodeCompletionOption option(E->key(), E->value());
  6209. if (E->value() == ScriptCodeCompletionOption::KIND_FUNCTION) {
  6210. option.insert_text += "(";
  6211. }
  6212. r_options->push_back(option);
  6213. }
  6214. return OK;
  6215. } break;
  6216. case COMPLETION_CALL_ARGUMENTS: {
  6217. for (int i = 0; i < shader->functions.size(); i++) {
  6218. if (!shader->functions[i].callable) {
  6219. continue;
  6220. }
  6221. if (shader->functions[i].name == completion_function) {
  6222. String calltip;
  6223. calltip += get_datatype_name(shader->functions[i].function->return_type);
  6224. calltip += " ";
  6225. calltip += shader->functions[i].name;
  6226. calltip += "(";
  6227. for (int j = 0; j < shader->functions[i].function->arguments.size(); j++) {
  6228. if (j > 0) {
  6229. calltip += ", ";
  6230. } else {
  6231. calltip += " ";
  6232. }
  6233. if (j == completion_argument) {
  6234. calltip += CharType(0xFFFF);
  6235. }
  6236. calltip += get_datatype_name(shader->functions[i].function->arguments[j].type);
  6237. calltip += " ";
  6238. calltip += shader->functions[i].function->arguments[j].name;
  6239. if (j == completion_argument) {
  6240. calltip += CharType(0xFFFF);
  6241. }
  6242. if (shader->functions[i].function->arguments[j].is_const) {
  6243. calltip += "const ";
  6244. }
  6245. }
  6246. if (shader->functions[i].function->arguments.size()) {
  6247. calltip += " ";
  6248. }
  6249. calltip += ")";
  6250. r_call_hint = calltip;
  6251. return OK;
  6252. }
  6253. }
  6254. int idx = 0;
  6255. String calltip;
  6256. bool low_end = VisualServer::get_singleton()->is_low_end();
  6257. while (builtin_func_defs[idx].name) {
  6258. if (low_end && builtin_func_defs[idx].high_end) {
  6259. idx++;
  6260. continue;
  6261. }
  6262. if (completion_function == builtin_func_defs[idx].name) {
  6263. if (builtin_func_defs[idx].tag != completion_class) {
  6264. idx++;
  6265. continue;
  6266. }
  6267. if (calltip.length()) {
  6268. calltip += "\n";
  6269. }
  6270. calltip += get_datatype_name(builtin_func_defs[idx].rettype);
  6271. calltip += " ";
  6272. calltip += builtin_func_defs[idx].name;
  6273. calltip += "(";
  6274. bool found_arg = false;
  6275. for (int i = 0; i < 4; i++) {
  6276. if (builtin_func_defs[idx].args[i] == TYPE_VOID) {
  6277. break;
  6278. }
  6279. if (i > 0) {
  6280. calltip += ", ";
  6281. } else {
  6282. calltip += " ";
  6283. }
  6284. if (i == completion_argument) {
  6285. calltip += CharType(0xFFFF);
  6286. }
  6287. calltip += get_datatype_name(builtin_func_defs[idx].args[i]);
  6288. if (i == completion_argument) {
  6289. calltip += CharType(0xFFFF);
  6290. }
  6291. found_arg = true;
  6292. }
  6293. if (found_arg) {
  6294. calltip += " ";
  6295. }
  6296. calltip += ")";
  6297. }
  6298. idx++;
  6299. }
  6300. r_call_hint = calltip;
  6301. return OK;
  6302. } break;
  6303. case COMPLETION_INDEX: {
  6304. const char colv[4] = { 'r', 'g', 'b', 'a' };
  6305. const char coordv[4] = { 'x', 'y', 'z', 'w' };
  6306. int limit = 0;
  6307. switch (completion_base) {
  6308. case TYPE_BVEC2:
  6309. case TYPE_IVEC2:
  6310. case TYPE_UVEC2:
  6311. case TYPE_VEC2: {
  6312. limit = 2;
  6313. } break;
  6314. case TYPE_BVEC3:
  6315. case TYPE_IVEC3:
  6316. case TYPE_UVEC3:
  6317. case TYPE_VEC3: {
  6318. limit = 3;
  6319. } break;
  6320. case TYPE_BVEC4:
  6321. case TYPE_IVEC4:
  6322. case TYPE_UVEC4:
  6323. case TYPE_VEC4: {
  6324. limit = 4;
  6325. } break;
  6326. default: {
  6327. }
  6328. }
  6329. for (int i = 0; i < limit; i++) {
  6330. r_options->push_back(ScriptCodeCompletionOption(String::chr(colv[i]), ScriptCodeCompletionOption::KIND_PLAIN_TEXT));
  6331. r_options->push_back(ScriptCodeCompletionOption(String::chr(coordv[i]), ScriptCodeCompletionOption::KIND_PLAIN_TEXT));
  6332. }
  6333. } break;
  6334. case COMPLETION_HINT: {
  6335. if (completion_base == DataType::TYPE_VEC4) {
  6336. ScriptCodeCompletionOption option("hint_color", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  6337. r_options->push_back(option);
  6338. } else if ((completion_base == DataType::TYPE_INT || completion_base == DataType::TYPE_FLOAT)) {
  6339. ScriptCodeCompletionOption option("hint_range", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  6340. if (completion_base == DataType::TYPE_INT) {
  6341. option.insert_text = "hint_range(0, 100, 1)";
  6342. } else {
  6343. option.insert_text = "hint_range(0.0, 1.0, 0.1)";
  6344. }
  6345. r_options->push_back(option);
  6346. } else if ((int(completion_base) > int(TYPE_MAT4) && int(completion_base) < int(TYPE_STRUCT))) {
  6347. static Vector<String> options;
  6348. if (options.empty()) {
  6349. options.push_back("hint_albedo");
  6350. options.push_back("hint_aniso");
  6351. options.push_back("hint_black");
  6352. options.push_back("hint_black_albedo");
  6353. options.push_back("hint_normal");
  6354. options.push_back("hint_white");
  6355. }
  6356. for (int i = 0; i < options.size(); i++) {
  6357. ScriptCodeCompletionOption option(options[i], ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  6358. r_options->push_back(option);
  6359. }
  6360. }
  6361. } break;
  6362. }
  6363. return ERR_PARSE_ERROR;
  6364. }
  6365. String ShaderLanguage::get_error_text() {
  6366. return error_str;
  6367. }
  6368. int ShaderLanguage::get_error_line() {
  6369. return error_line;
  6370. }
  6371. ShaderLanguage::ShaderNode *ShaderLanguage::get_shader() {
  6372. return shader;
  6373. }
  6374. ShaderLanguage::ShaderLanguage() {
  6375. nodes = nullptr;
  6376. completion_class = TAG_GLOBAL;
  6377. }
  6378. ShaderLanguage::~ShaderLanguage() {
  6379. clear();
  6380. }