shader_language.cpp 285 KB

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