shader_language.cpp 335 KB

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