shader_language.cpp 284 KB

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