shader_language.cpp 357 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898
  1. /**************************************************************************/
  2. /* shader_language.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "shader_language.h"
  31. #include "core/os/os.h"
  32. #include "core/string/print_string.h"
  33. #include "core/templates/local_vector.h"
  34. #include "servers/rendering/renderer_compositor.h"
  35. #include "servers/rendering_server.h"
  36. #include "shader_types.h"
  37. #define HAS_WARNING(flag) (warning_flags & flag)
  38. int ShaderLanguage::instance_counter = 0;
  39. String ShaderLanguage::get_operator_text(Operator p_op) {
  40. static const char *op_names[OP_MAX] = { "==",
  41. "!=",
  42. "<",
  43. "<=",
  44. ">",
  45. ">=",
  46. "&&",
  47. "||",
  48. "!",
  49. "-",
  50. "+",
  51. "-",
  52. "*",
  53. "/",
  54. "%",
  55. "<<",
  56. ">>",
  57. "=",
  58. "+=",
  59. "-=",
  60. "*=",
  61. "/=",
  62. "%=",
  63. "<<=",
  64. ">>=",
  65. "&=",
  66. "|=",
  67. "^=",
  68. "&",
  69. "|",
  70. "^",
  71. "~",
  72. "++",
  73. "--",
  74. "?",
  75. ":",
  76. "++",
  77. "--",
  78. "()",
  79. "construct",
  80. "index",
  81. "empty" };
  82. return op_names[p_op];
  83. }
  84. const char *ShaderLanguage::token_names[TK_MAX] = {
  85. "EMPTY",
  86. "IDENTIFIER",
  87. "TRUE",
  88. "FALSE",
  89. "FLOAT_CONSTANT",
  90. "INT_CONSTANT",
  91. "UINT_CONSTANT",
  92. "TYPE_VOID",
  93. "TYPE_BOOL",
  94. "TYPE_BVEC2",
  95. "TYPE_BVEC3",
  96. "TYPE_BVEC4",
  97. "TYPE_INT",
  98. "TYPE_IVEC2",
  99. "TYPE_IVEC3",
  100. "TYPE_IVEC4",
  101. "TYPE_UINT",
  102. "TYPE_UVEC2",
  103. "TYPE_UVEC3",
  104. "TYPE_UVEC4",
  105. "TYPE_FLOAT",
  106. "TYPE_VEC2",
  107. "TYPE_VEC3",
  108. "TYPE_VEC4",
  109. "TYPE_MAT2",
  110. "TYPE_MAT3",
  111. "TYPE_MAT4",
  112. "TYPE_SAMPLER2D",
  113. "TYPE_ISAMPLER2D",
  114. "TYPE_USAMPLER2D",
  115. "TYPE_SAMPLER2DARRAY",
  116. "TYPE_ISAMPLER2DARRAY",
  117. "TYPE_USAMPLER2DARRAY",
  118. "TYPE_SAMPLER3D",
  119. "TYPE_ISAMPLER3D",
  120. "TYPE_USAMPLER3D",
  121. "TYPE_SAMPLERCUBE",
  122. "TYPE_SAMPLERCUBEARRAY",
  123. "INTERPOLATION_FLAT",
  124. "INTERPOLATION_SMOOTH",
  125. "CONST",
  126. "STRUCT",
  127. "PRECISION_LOW",
  128. "PRECISION_MID",
  129. "PRECISION_HIGH",
  130. "OP_EQUAL",
  131. "OP_NOT_EQUAL",
  132. "OP_LESS",
  133. "OP_LESS_EQUAL",
  134. "OP_GREATER",
  135. "OP_GREATER_EQUAL",
  136. "OP_AND",
  137. "OP_OR",
  138. "OP_NOT",
  139. "OP_ADD",
  140. "OP_SUB",
  141. "OP_MUL",
  142. "OP_DIV",
  143. "OP_MOD",
  144. "OP_SHIFT_LEFT",
  145. "OP_SHIFT_RIGHT",
  146. "OP_ASSIGN",
  147. "OP_ASSIGN_ADD",
  148. "OP_ASSIGN_SUB",
  149. "OP_ASSIGN_MUL",
  150. "OP_ASSIGN_DIV",
  151. "OP_ASSIGN_MOD",
  152. "OP_ASSIGN_SHIFT_LEFT",
  153. "OP_ASSIGN_SHIFT_RIGHT",
  154. "OP_ASSIGN_BIT_AND",
  155. "OP_ASSIGN_BIT_OR",
  156. "OP_ASSIGN_BIT_XOR",
  157. "OP_BIT_AND",
  158. "OP_BIT_OR",
  159. "OP_BIT_XOR",
  160. "OP_BIT_INVERT",
  161. "OP_INCREMENT",
  162. "OP_DECREMENT",
  163. "CF_IF",
  164. "CF_ELSE",
  165. "CF_FOR",
  166. "CF_WHILE",
  167. "CF_DO",
  168. "CF_SWITCH",
  169. "CF_CASE",
  170. "CF_DEFAULT",
  171. "CF_BREAK",
  172. "CF_CONTINUE",
  173. "CF_RETURN",
  174. "CF_DISCARD",
  175. "BRACKET_OPEN",
  176. "BRACKET_CLOSE",
  177. "CURLY_BRACKET_OPEN",
  178. "CURLY_BRACKET_CLOSE",
  179. "PARENTHESIS_OPEN",
  180. "PARENTHESIS_CLOSE",
  181. "QUESTION",
  182. "COMMA",
  183. "COLON",
  184. "SEMICOLON",
  185. "PERIOD",
  186. "UNIFORM",
  187. "UNIFORM_GROUP",
  188. "INSTANCE",
  189. "GLOBAL",
  190. "VARYING",
  191. "ARG_IN",
  192. "ARG_OUT",
  193. "ARG_INOUT",
  194. "RENDER_MODE",
  195. "HINT_DEFAULT_WHITE_TEXTURE",
  196. "HINT_DEFAULT_BLACK_TEXTURE",
  197. "HINT_DEFAULT_TRANSPARENT_TEXTURE",
  198. "HINT_NORMAL_TEXTURE",
  199. "HINT_ROUGHNESS_NORMAL_TEXTURE",
  200. "HINT_ROUGHNESS_R",
  201. "HINT_ROUGHNESS_G",
  202. "HINT_ROUGHNESS_B",
  203. "HINT_ROUGHNESS_A",
  204. "HINT_ROUGHNESS_GRAY",
  205. "HINT_ANISOTROPY_TEXTURE",
  206. "HINT_SOURCE_COLOR",
  207. "HINT_RANGE",
  208. "HINT_INSTANCE_INDEX",
  209. "HINT_SCREEN_TEXTURE",
  210. "HINT_NORMAL_ROUGHNESS_TEXTURE",
  211. "HINT_DEPTH_TEXTURE",
  212. "FILTER_NEAREST",
  213. "FILTER_LINEAR",
  214. "FILTER_NEAREST_MIPMAP",
  215. "FILTER_LINEAR_MIPMAP",
  216. "FILTER_NEAREST_MIPMAP_ANISOTROPIC",
  217. "FILTER_LINEAR_MIPMAP_ANISOTROPIC",
  218. "REPEAT_ENABLE",
  219. "REPEAT_DISABLE",
  220. "SHADER_TYPE",
  221. "CURSOR",
  222. "ERROR",
  223. "EOF",
  224. };
  225. String ShaderLanguage::get_token_text(Token p_token) {
  226. String name = token_names[p_token.type];
  227. if (p_token.is_integer_constant() || p_token.type == TK_FLOAT_CONSTANT) {
  228. name += "(" + rtos(p_token.constant) + ")";
  229. } else if (p_token.type == TK_IDENTIFIER) {
  230. name += "(" + String(p_token.text) + ")";
  231. } else if (p_token.type == TK_ERROR) {
  232. name += "(" + String(p_token.text) + ")";
  233. }
  234. return name;
  235. }
  236. ShaderLanguage::Token ShaderLanguage::_make_token(TokenType p_type, const StringName &p_text) {
  237. Token tk;
  238. tk.type = p_type;
  239. tk.text = p_text;
  240. tk.line = tk_line;
  241. if (tk.type == TK_ERROR) {
  242. _set_error(p_text);
  243. }
  244. return tk;
  245. }
  246. enum ContextFlag : uint32_t {
  247. CF_UNSPECIFIED = 0U,
  248. CF_BLOCK = 1U, // "void test() { <x> }"
  249. CF_FUNC_DECL_PARAM_SPEC = 2U, // "void test(<x> int param) {}"
  250. CF_FUNC_DECL_PARAM_TYPE = 4U, // "void test(<x> param) {}"
  251. CF_IF_DECL = 8U, // "if(<x>) {}"
  252. CF_BOOLEAN = 16U, // "bool t = <x>;"
  253. CF_GLOBAL_SPACE = 32U, // "struct", "const", "void" etc.
  254. CF_DATATYPE = 64U, // "<x> value;"
  255. CF_UNIFORM_TYPE = 128U, // "uniform <x> myUniform;"
  256. CF_VARYING_TYPE = 256U, // "varying <x> myVarying;"
  257. CF_PRECISION_MODIFIER = 512U, // "<x> vec4 a = vec4(0.0, 1.0, 2.0, 3.0);"
  258. CF_INTERPOLATION_QUALIFIER = 1024U, // "varying <x> vec3 myColor;"
  259. CF_UNIFORM_KEYWORD = 2048U, // "uniform"
  260. CF_CONST_KEYWORD = 4096U, // "const"
  261. CF_UNIFORM_QUALIFIER = 8192U, // "<x> uniform float t;"
  262. CF_SHADER_TYPE = 16384U, // "shader_type"
  263. };
  264. const uint32_t KCF_DATATYPE = CF_BLOCK | CF_GLOBAL_SPACE | CF_DATATYPE | CF_FUNC_DECL_PARAM_TYPE | CF_UNIFORM_TYPE;
  265. const uint32_t KCF_SAMPLER_DATATYPE = CF_FUNC_DECL_PARAM_TYPE | CF_UNIFORM_TYPE;
  266. const ShaderLanguage::KeyWord ShaderLanguage::keyword_list[] = {
  267. { TK_TRUE, "true", CF_BLOCK | CF_IF_DECL | CF_BOOLEAN, {}, {} },
  268. { TK_FALSE, "false", CF_BLOCK | CF_IF_DECL | CF_BOOLEAN, {}, {} },
  269. // data types
  270. { TK_TYPE_VOID, "void", CF_GLOBAL_SPACE, {}, {} },
  271. { TK_TYPE_BOOL, "bool", KCF_DATATYPE, {}, {} },
  272. { TK_TYPE_BVEC2, "bvec2", KCF_DATATYPE, {}, {} },
  273. { TK_TYPE_BVEC3, "bvec3", KCF_DATATYPE, {}, {} },
  274. { TK_TYPE_BVEC4, "bvec4", KCF_DATATYPE, {}, {} },
  275. { TK_TYPE_INT, "int", KCF_DATATYPE, {}, {} },
  276. { TK_TYPE_IVEC2, "ivec2", KCF_DATATYPE, {}, {} },
  277. { TK_TYPE_IVEC3, "ivec3", KCF_DATATYPE, {}, {} },
  278. { TK_TYPE_IVEC4, "ivec4", KCF_DATATYPE, {}, {} },
  279. { TK_TYPE_UINT, "uint", KCF_DATATYPE, {}, {} },
  280. { TK_TYPE_UVEC2, "uvec2", KCF_DATATYPE, {}, {} },
  281. { TK_TYPE_UVEC3, "uvec3", KCF_DATATYPE, {}, {} },
  282. { TK_TYPE_UVEC4, "uvec4", KCF_DATATYPE, {}, {} },
  283. { TK_TYPE_FLOAT, "float", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  284. { TK_TYPE_VEC2, "vec2", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  285. { TK_TYPE_VEC3, "vec3", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  286. { TK_TYPE_VEC4, "vec4", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  287. { TK_TYPE_MAT2, "mat2", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  288. { TK_TYPE_MAT3, "mat3", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  289. { TK_TYPE_MAT4, "mat4", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  290. { TK_TYPE_SAMPLER2D, "sampler2D", KCF_SAMPLER_DATATYPE, {}, {} },
  291. { TK_TYPE_ISAMPLER2D, "isampler2D", KCF_SAMPLER_DATATYPE, {}, {} },
  292. { TK_TYPE_USAMPLER2D, "usampler2D", KCF_SAMPLER_DATATYPE, {}, {} },
  293. { TK_TYPE_SAMPLER2DARRAY, "sampler2DArray", KCF_SAMPLER_DATATYPE, {}, {} },
  294. { TK_TYPE_ISAMPLER2DARRAY, "isampler2DArray", KCF_SAMPLER_DATATYPE, {}, {} },
  295. { TK_TYPE_USAMPLER2DARRAY, "usampler2DArray", KCF_SAMPLER_DATATYPE, {}, {} },
  296. { TK_TYPE_SAMPLER3D, "sampler3D", KCF_SAMPLER_DATATYPE, {}, {} },
  297. { TK_TYPE_ISAMPLER3D, "isampler3D", KCF_SAMPLER_DATATYPE, {}, {} },
  298. { TK_TYPE_USAMPLER3D, "usampler3D", KCF_SAMPLER_DATATYPE, {}, {} },
  299. { TK_TYPE_SAMPLERCUBE, "samplerCube", KCF_SAMPLER_DATATYPE, {}, {} },
  300. { TK_TYPE_SAMPLERCUBEARRAY, "samplerCubeArray", KCF_SAMPLER_DATATYPE, {}, {} },
  301. // interpolation qualifiers
  302. { TK_INTERPOLATION_FLAT, "flat", CF_INTERPOLATION_QUALIFIER, {}, {} },
  303. { TK_INTERPOLATION_SMOOTH, "smooth", CF_INTERPOLATION_QUALIFIER, {}, {} },
  304. // precision modifiers
  305. { TK_PRECISION_LOW, "lowp", CF_BLOCK | CF_PRECISION_MODIFIER, {}, {} },
  306. { TK_PRECISION_MID, "mediump", CF_BLOCK | CF_PRECISION_MODIFIER, {}, {} },
  307. { TK_PRECISION_HIGH, "highp", CF_BLOCK | CF_PRECISION_MODIFIER, {}, {} },
  308. // global space keywords
  309. { TK_UNIFORM, "uniform", CF_GLOBAL_SPACE | CF_UNIFORM_KEYWORD, {}, {} },
  310. { TK_UNIFORM_GROUP, "group_uniforms", CF_GLOBAL_SPACE, {}, {} },
  311. { TK_VARYING, "varying", CF_GLOBAL_SPACE, { "particles", "sky", "fog" }, {} },
  312. { TK_CONST, "const", CF_BLOCK | CF_GLOBAL_SPACE | CF_CONST_KEYWORD, {}, {} },
  313. { TK_STRUCT, "struct", CF_GLOBAL_SPACE, {}, {} },
  314. { TK_SHADER_TYPE, "shader_type", CF_SHADER_TYPE, {}, {} },
  315. { TK_RENDER_MODE, "render_mode", CF_GLOBAL_SPACE, {}, {} },
  316. // uniform qualifiers
  317. { TK_INSTANCE, "instance", CF_GLOBAL_SPACE | CF_UNIFORM_QUALIFIER, {}, {} },
  318. { TK_GLOBAL, "global", CF_GLOBAL_SPACE | CF_UNIFORM_QUALIFIER, {}, {} },
  319. // block keywords
  320. { TK_CF_IF, "if", CF_BLOCK, {}, {} },
  321. { TK_CF_ELSE, "else", CF_BLOCK, {}, {} },
  322. { TK_CF_FOR, "for", CF_BLOCK, {}, {} },
  323. { TK_CF_WHILE, "while", CF_BLOCK, {}, {} },
  324. { TK_CF_DO, "do", CF_BLOCK, {}, {} },
  325. { TK_CF_SWITCH, "switch", CF_BLOCK, {}, {} },
  326. { TK_CF_CASE, "case", CF_BLOCK, {}, {} },
  327. { TK_CF_DEFAULT, "default", CF_BLOCK, {}, {} },
  328. { TK_CF_BREAK, "break", CF_BLOCK, {}, {} },
  329. { TK_CF_CONTINUE, "continue", CF_BLOCK, {}, {} },
  330. { TK_CF_RETURN, "return", CF_BLOCK, {}, {} },
  331. { TK_CF_DISCARD, "discard", CF_BLOCK, { "particles", "sky", "fog" }, { "fragment" } },
  332. // function specifier keywords
  333. { TK_ARG_IN, "in", CF_FUNC_DECL_PARAM_SPEC, {}, {} },
  334. { TK_ARG_OUT, "out", CF_FUNC_DECL_PARAM_SPEC, {}, {} },
  335. { TK_ARG_INOUT, "inout", CF_FUNC_DECL_PARAM_SPEC, {}, {} },
  336. // hints
  337. { TK_HINT_SOURCE_COLOR, "source_color", CF_UNSPECIFIED, {}, {} },
  338. { TK_HINT_RANGE, "hint_range", CF_UNSPECIFIED, {}, {} },
  339. { TK_HINT_INSTANCE_INDEX, "instance_index", CF_UNSPECIFIED, {}, {} },
  340. // sampler hints
  341. { TK_HINT_NORMAL_TEXTURE, "hint_normal", CF_UNSPECIFIED, {}, {} },
  342. { TK_HINT_DEFAULT_WHITE_TEXTURE, "hint_default_white", CF_UNSPECIFIED, {}, {} },
  343. { TK_HINT_DEFAULT_BLACK_TEXTURE, "hint_default_black", CF_UNSPECIFIED, {}, {} },
  344. { TK_HINT_DEFAULT_TRANSPARENT_TEXTURE, "hint_default_transparent", CF_UNSPECIFIED, {}, {} },
  345. { TK_HINT_ANISOTROPY_TEXTURE, "hint_anisotropy", CF_UNSPECIFIED, {}, {} },
  346. { TK_HINT_ROUGHNESS_R, "hint_roughness_r", CF_UNSPECIFIED, {}, {} },
  347. { TK_HINT_ROUGHNESS_G, "hint_roughness_g", CF_UNSPECIFIED, {}, {} },
  348. { TK_HINT_ROUGHNESS_B, "hint_roughness_b", CF_UNSPECIFIED, {}, {} },
  349. { TK_HINT_ROUGHNESS_A, "hint_roughness_a", CF_UNSPECIFIED, {}, {} },
  350. { TK_HINT_ROUGHNESS_NORMAL_TEXTURE, "hint_roughness_normal", CF_UNSPECIFIED, {}, {} },
  351. { TK_HINT_ROUGHNESS_GRAY, "hint_roughness_gray", CF_UNSPECIFIED, {}, {} },
  352. { TK_HINT_SCREEN_TEXTURE, "hint_screen_texture", CF_UNSPECIFIED, {}, {} },
  353. { TK_HINT_NORMAL_ROUGHNESS_TEXTURE, "hint_normal_roughness_texture", CF_UNSPECIFIED, {}, {} },
  354. { TK_HINT_DEPTH_TEXTURE, "hint_depth_texture", CF_UNSPECIFIED, {}, {} },
  355. { TK_FILTER_NEAREST, "filter_nearest", CF_UNSPECIFIED, {}, {} },
  356. { TK_FILTER_LINEAR, "filter_linear", CF_UNSPECIFIED, {}, {} },
  357. { TK_FILTER_NEAREST_MIPMAP, "filter_nearest_mipmap", CF_UNSPECIFIED, {}, {} },
  358. { TK_FILTER_LINEAR_MIPMAP, "filter_linear_mipmap", CF_UNSPECIFIED, {}, {} },
  359. { TK_FILTER_NEAREST_MIPMAP_ANISOTROPIC, "filter_nearest_mipmap_anisotropic", CF_UNSPECIFIED, {}, {} },
  360. { TK_FILTER_LINEAR_MIPMAP_ANISOTROPIC, "filter_linear_mipmap_anisotropic", CF_UNSPECIFIED, {}, {} },
  361. { TK_REPEAT_ENABLE, "repeat_enable", CF_UNSPECIFIED, {}, {} },
  362. { TK_REPEAT_DISABLE, "repeat_disable", CF_UNSPECIFIED, {}, {} },
  363. { TK_ERROR, nullptr, CF_UNSPECIFIED, {}, {} }
  364. };
  365. ShaderLanguage::Token ShaderLanguage::_get_token() {
  366. #define GETCHAR(m_idx) (((char_idx + m_idx) < code.length()) ? code[char_idx + m_idx] : char32_t(0))
  367. while (true) {
  368. char_idx++;
  369. switch (GETCHAR(-1)) {
  370. case 0:
  371. return _make_token(TK_EOF);
  372. case 0xFFFF:
  373. return _make_token(TK_CURSOR); //for completion
  374. case '\t':
  375. case '\r':
  376. case ' ':
  377. continue;
  378. case '\n':
  379. tk_line++;
  380. continue;
  381. case '/': {
  382. switch (GETCHAR(0)) {
  383. case '*': { // block comment
  384. char_idx++;
  385. while (true) {
  386. if (GETCHAR(0) == 0) {
  387. return _make_token(TK_EOF);
  388. }
  389. if (GETCHAR(0) == '*' && GETCHAR(1) == '/') {
  390. char_idx += 2;
  391. break;
  392. } else if (GETCHAR(0) == '\n') {
  393. tk_line++;
  394. }
  395. char_idx++;
  396. }
  397. } break;
  398. case '/': { // line comment skip
  399. while (true) {
  400. if (GETCHAR(0) == '\n') {
  401. tk_line++;
  402. char_idx++;
  403. break;
  404. }
  405. if (GETCHAR(0) == 0) {
  406. return _make_token(TK_EOF);
  407. }
  408. char_idx++;
  409. }
  410. } break;
  411. case '=': { // diveq
  412. char_idx++;
  413. return _make_token(TK_OP_ASSIGN_DIV);
  414. } break;
  415. default:
  416. return _make_token(TK_OP_DIV);
  417. }
  418. continue; //a comment, continue to next token
  419. } break;
  420. case '=': {
  421. if (GETCHAR(0) == '=') {
  422. char_idx++;
  423. return _make_token(TK_OP_EQUAL);
  424. }
  425. return _make_token(TK_OP_ASSIGN);
  426. } break;
  427. case '<': {
  428. if (GETCHAR(0) == '=') {
  429. char_idx++;
  430. return _make_token(TK_OP_LESS_EQUAL);
  431. } else if (GETCHAR(0) == '<') {
  432. char_idx++;
  433. if (GETCHAR(0) == '=') {
  434. char_idx++;
  435. return _make_token(TK_OP_ASSIGN_SHIFT_LEFT);
  436. }
  437. return _make_token(TK_OP_SHIFT_LEFT);
  438. }
  439. return _make_token(TK_OP_LESS);
  440. } break;
  441. case '>': {
  442. if (GETCHAR(0) == '=') {
  443. char_idx++;
  444. return _make_token(TK_OP_GREATER_EQUAL);
  445. } else if (GETCHAR(0) == '>') {
  446. char_idx++;
  447. if (GETCHAR(0) == '=') {
  448. char_idx++;
  449. return _make_token(TK_OP_ASSIGN_SHIFT_RIGHT);
  450. }
  451. return _make_token(TK_OP_SHIFT_RIGHT);
  452. }
  453. return _make_token(TK_OP_GREATER);
  454. } break;
  455. case '!': {
  456. if (GETCHAR(0) == '=') {
  457. char_idx++;
  458. return _make_token(TK_OP_NOT_EQUAL);
  459. }
  460. return _make_token(TK_OP_NOT);
  461. } break;
  462. //case '"' //string - no strings in shader
  463. //case '\'' //string - no strings in shader
  464. case '{':
  465. return _make_token(TK_CURLY_BRACKET_OPEN);
  466. case '}':
  467. return _make_token(TK_CURLY_BRACKET_CLOSE);
  468. case '[':
  469. return _make_token(TK_BRACKET_OPEN);
  470. case ']':
  471. return _make_token(TK_BRACKET_CLOSE);
  472. case '(':
  473. return _make_token(TK_PARENTHESIS_OPEN);
  474. case ')':
  475. return _make_token(TK_PARENTHESIS_CLOSE);
  476. case ',':
  477. return _make_token(TK_COMMA);
  478. case ';':
  479. return _make_token(TK_SEMICOLON);
  480. case '?':
  481. return _make_token(TK_QUESTION);
  482. case ':':
  483. return _make_token(TK_COLON);
  484. case '^':
  485. if (GETCHAR(0) == '=') {
  486. char_idx++;
  487. return _make_token(TK_OP_ASSIGN_BIT_XOR);
  488. }
  489. return _make_token(TK_OP_BIT_XOR);
  490. case '~':
  491. return _make_token(TK_OP_BIT_INVERT);
  492. case '&': {
  493. if (GETCHAR(0) == '=') {
  494. char_idx++;
  495. return _make_token(TK_OP_ASSIGN_BIT_AND);
  496. } else if (GETCHAR(0) == '&') {
  497. char_idx++;
  498. return _make_token(TK_OP_AND);
  499. }
  500. return _make_token(TK_OP_BIT_AND);
  501. } break;
  502. case '|': {
  503. if (GETCHAR(0) == '=') {
  504. char_idx++;
  505. return _make_token(TK_OP_ASSIGN_BIT_OR);
  506. } else if (GETCHAR(0) == '|') {
  507. char_idx++;
  508. return _make_token(TK_OP_OR);
  509. }
  510. return _make_token(TK_OP_BIT_OR);
  511. } break;
  512. case '*': {
  513. if (GETCHAR(0) == '=') {
  514. char_idx++;
  515. return _make_token(TK_OP_ASSIGN_MUL);
  516. }
  517. return _make_token(TK_OP_MUL);
  518. } break;
  519. case '+': {
  520. if (GETCHAR(0) == '=') {
  521. char_idx++;
  522. return _make_token(TK_OP_ASSIGN_ADD);
  523. } else if (GETCHAR(0) == '+') {
  524. char_idx++;
  525. return _make_token(TK_OP_INCREMENT);
  526. }
  527. return _make_token(TK_OP_ADD);
  528. } break;
  529. case '-': {
  530. if (GETCHAR(0) == '=') {
  531. char_idx++;
  532. return _make_token(TK_OP_ASSIGN_SUB);
  533. } else if (GETCHAR(0) == '-') {
  534. char_idx++;
  535. return _make_token(TK_OP_DECREMENT);
  536. }
  537. return _make_token(TK_OP_SUB);
  538. } break;
  539. case '%': {
  540. if (GETCHAR(0) == '=') {
  541. char_idx++;
  542. return _make_token(TK_OP_ASSIGN_MOD);
  543. }
  544. return _make_token(TK_OP_MOD);
  545. } break;
  546. case '@': {
  547. if (GETCHAR(0) == '@' && GETCHAR(1) == '>') {
  548. char_idx += 2;
  549. LocalVector<char32_t> incp;
  550. while (GETCHAR(0) != '\n') {
  551. incp.push_back(GETCHAR(0));
  552. char_idx++;
  553. }
  554. incp.push_back(0); // Zero end it.
  555. String include_path(incp.ptr());
  556. include_positions.write[include_positions.size() - 1].line = tk_line;
  557. String marker = ">>" + include_path;
  558. if (!include_markers_handled.has(marker)) {
  559. include_markers_handled.insert(marker);
  560. FilePosition fp;
  561. fp.file = include_path;
  562. fp.line = 0;
  563. tk_line = 0;
  564. include_positions.push_back(fp);
  565. }
  566. } else if (GETCHAR(0) == '@' && GETCHAR(1) == '<') {
  567. char_idx += 2;
  568. LocalVector<char32_t> incp;
  569. while (GETCHAR(0) != '\n') {
  570. incp.push_back(GETCHAR(0));
  571. char_idx++;
  572. }
  573. incp.push_back(0); // Zero end it.
  574. String include_path(incp.ptr());
  575. String marker = "<<" + include_path;
  576. if (!include_markers_handled.has(marker)) {
  577. include_markers_handled.insert(marker);
  578. if (include_positions.size() == 1) {
  579. return _make_token(TK_ERROR, "Invalid include exit hint @@< without matching enter hint.");
  580. }
  581. include_positions.resize(include_positions.size() - 1); // Pop back.
  582. }
  583. tk_line = include_positions[include_positions.size() - 1].line - 1; // Restore line.
  584. } else {
  585. return _make_token(TK_ERROR, "Invalid include enter/exit hint token (@@> and @@<)");
  586. }
  587. } break;
  588. default: {
  589. char_idx--; //go back one, since we have no idea what this is
  590. if (is_digit(GETCHAR(0)) || (GETCHAR(0) == '.' && is_digit(GETCHAR(1)))) {
  591. // parse number
  592. bool hexa_found = false;
  593. bool period_found = false;
  594. bool exponent_found = false;
  595. bool float_suffix_found = false;
  596. bool uint_suffix_found = false;
  597. bool end_suffix_found = false;
  598. enum {
  599. CASE_ALL,
  600. CASE_HEXA_PERIOD,
  601. CASE_EXPONENT,
  602. CASE_SIGN_AFTER_EXPONENT,
  603. CASE_NONE,
  604. CASE_MAX,
  605. } lut_case = CASE_ALL;
  606. static bool suffix_lut[CASE_MAX][127];
  607. if (!is_const_suffix_lut_initialized) {
  608. is_const_suffix_lut_initialized = true;
  609. for (int i = 0; i < 127; i++) {
  610. char t = char(i);
  611. suffix_lut[CASE_ALL][i] = t == '.' || t == 'x' || t == 'e' || t == 'f' || t == 'u' || t == '-' || t == '+';
  612. suffix_lut[CASE_HEXA_PERIOD][i] = t == 'e' || t == 'f' || t == 'u';
  613. suffix_lut[CASE_EXPONENT][i] = t == 'f' || t == '-' || t == '+';
  614. suffix_lut[CASE_SIGN_AFTER_EXPONENT][i] = t == 'f';
  615. suffix_lut[CASE_NONE][i] = false;
  616. }
  617. }
  618. String str;
  619. int i = 0;
  620. bool digit_after_exp = false;
  621. while (true) {
  622. const char32_t symbol = String::char_lowercase(GETCHAR(i));
  623. bool error = false;
  624. if (is_digit(symbol)) {
  625. if (exponent_found) {
  626. digit_after_exp = true;
  627. }
  628. if (end_suffix_found) {
  629. error = true;
  630. }
  631. } else {
  632. if (symbol < 0x7F && suffix_lut[lut_case][symbol]) {
  633. if (symbol == 'x') {
  634. hexa_found = true;
  635. lut_case = CASE_HEXA_PERIOD;
  636. } else if (symbol == '.') {
  637. period_found = true;
  638. lut_case = CASE_HEXA_PERIOD;
  639. } else if (symbol == 'e' && !hexa_found) {
  640. exponent_found = true;
  641. lut_case = CASE_EXPONENT;
  642. } else if (symbol == 'f' && !hexa_found) {
  643. if (!period_found && !exponent_found) {
  644. error = true;
  645. }
  646. float_suffix_found = true;
  647. end_suffix_found = true;
  648. lut_case = CASE_NONE;
  649. } else if (symbol == 'u') {
  650. uint_suffix_found = true;
  651. end_suffix_found = true;
  652. lut_case = CASE_NONE;
  653. } else if (symbol == '-' || symbol == '+') {
  654. if (exponent_found) {
  655. lut_case = CASE_SIGN_AFTER_EXPONENT;
  656. } else {
  657. break;
  658. }
  659. }
  660. } else if (!hexa_found || !is_hex_digit(symbol)) {
  661. if (is_ascii_identifier_char(symbol)) {
  662. error = true;
  663. } else {
  664. break;
  665. }
  666. }
  667. }
  668. if (error) {
  669. if (hexa_found) {
  670. return _make_token(TK_ERROR, "Invalid (hexadecimal) numeric constant");
  671. }
  672. if (period_found || exponent_found || float_suffix_found) {
  673. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  674. }
  675. if (uint_suffix_found) {
  676. return _make_token(TK_ERROR, "Invalid (unsigned integer) numeric constant");
  677. }
  678. return _make_token(TK_ERROR, "Invalid (integer) numeric constant");
  679. }
  680. str += symbol;
  681. i++;
  682. }
  683. char32_t last_char = str[str.length() - 1];
  684. if (hexa_found) { // Integer (hex).
  685. if (uint_suffix_found) {
  686. // Strip the suffix.
  687. str = str.left(str.length() - 1);
  688. // Compensate reading cursor position.
  689. char_idx += 1;
  690. }
  691. if (str.size() > 11 || !str.is_valid_hex_number(true)) { // > 0xFFFFFFFF
  692. return _make_token(TK_ERROR, "Invalid (hexadecimal) numeric constant");
  693. }
  694. } else if (period_found || exponent_found || float_suffix_found) { // Float
  695. if (exponent_found && (!digit_after_exp || (!is_digit(last_char) && last_char != 'f'))) { // Checks for eg: "2E", "2E-", "2E+" and 0ef, 0e+f, 0.0ef, 0.0e-f (exponent without digit after it).
  696. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  697. }
  698. if (period_found) {
  699. if (float_suffix_found) {
  700. //checks for eg "1.f" or "1.99f" notations
  701. if (last_char != 'f') {
  702. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  703. }
  704. } else {
  705. //checks for eg. "1." or "1.99" notations
  706. if (last_char != '.' && !is_digit(last_char)) {
  707. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  708. }
  709. }
  710. } else if (float_suffix_found) {
  711. // if no period found the float suffix must be the last character, like in "2f" for "2.0"
  712. if (last_char != 'f') {
  713. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  714. }
  715. }
  716. if (float_suffix_found) {
  717. // Strip the suffix.
  718. str = str.left(str.length() - 1);
  719. // Compensate reading cursor position.
  720. char_idx += 1;
  721. }
  722. if (!str.is_valid_float()) {
  723. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  724. }
  725. } else { // Integer
  726. if (uint_suffix_found) {
  727. // Strip the suffix.
  728. str = str.left(str.length() - 1);
  729. // Compensate reading cursor position.
  730. char_idx += 1;
  731. }
  732. if (!str.is_valid_int()) {
  733. if (uint_suffix_found) {
  734. return _make_token(TK_ERROR, "Invalid (unsigned integer) numeric constant");
  735. } else {
  736. return _make_token(TK_ERROR, "Invalid (integer) numeric constant");
  737. }
  738. }
  739. }
  740. char_idx += str.length();
  741. Token tk;
  742. if (period_found || exponent_found || float_suffix_found) {
  743. tk.type = TK_FLOAT_CONSTANT;
  744. } else if (uint_suffix_found) {
  745. tk.type = TK_UINT_CONSTANT;
  746. } else {
  747. tk.type = TK_INT_CONSTANT;
  748. }
  749. if (hexa_found) {
  750. tk.constant = (double)str.hex_to_int();
  751. } else {
  752. tk.constant = str.to_float();
  753. }
  754. tk.line = tk_line;
  755. return tk;
  756. }
  757. if (GETCHAR(0) == '.') {
  758. //parse period
  759. char_idx++;
  760. return _make_token(TK_PERIOD);
  761. }
  762. if (is_ascii_identifier_char(GETCHAR(0))) {
  763. // parse identifier
  764. String str;
  765. while (is_ascii_identifier_char(GETCHAR(0))) {
  766. str += char32_t(GETCHAR(0));
  767. char_idx++;
  768. }
  769. //see if keyword
  770. //should be converted to a static map
  771. int idx = 0;
  772. while (keyword_list[idx].text) {
  773. if (str == keyword_list[idx].text) {
  774. return _make_token(keyword_list[idx].token);
  775. }
  776. idx++;
  777. }
  778. str = str.replace("dus_", "_");
  779. return _make_token(TK_IDENTIFIER, str);
  780. }
  781. if (GETCHAR(0) > 32) {
  782. return _make_token(TK_ERROR, "Tokenizer: Unknown character #" + itos(GETCHAR(0)) + ": '" + String::chr(GETCHAR(0)) + "'");
  783. } else {
  784. return _make_token(TK_ERROR, "Tokenizer: Unknown character #" + itos(GETCHAR(0)));
  785. }
  786. } break;
  787. }
  788. }
  789. ERR_PRINT("BUG");
  790. return Token();
  791. #undef GETCHAR
  792. }
  793. bool ShaderLanguage::_lookup_next(Token &r_tk) {
  794. TkPos pre_pos = _get_tkpos();
  795. int line = pre_pos.tk_line;
  796. _get_token();
  797. Token tk = _get_token();
  798. _set_tkpos(pre_pos);
  799. if (tk.line == line) {
  800. r_tk = tk;
  801. return true;
  802. }
  803. return false;
  804. }
  805. ShaderLanguage::Token ShaderLanguage::_peek() {
  806. TkPos pre_pos = _get_tkpos();
  807. Token tk = _get_token();
  808. _set_tkpos(pre_pos);
  809. return tk;
  810. }
  811. String ShaderLanguage::token_debug(const String &p_code) {
  812. clear();
  813. code = p_code;
  814. String output;
  815. Token tk = _get_token();
  816. while (tk.type != TK_EOF && tk.type != TK_ERROR) {
  817. output += itos(tk_line) + ": " + get_token_text(tk) + "\n";
  818. tk = _get_token();
  819. }
  820. return output;
  821. }
  822. bool ShaderLanguage::is_token_variable_datatype(TokenType p_type) {
  823. return (
  824. p_type == TK_TYPE_VOID ||
  825. p_type == TK_TYPE_BOOL ||
  826. p_type == TK_TYPE_BVEC2 ||
  827. p_type == TK_TYPE_BVEC3 ||
  828. p_type == TK_TYPE_BVEC4 ||
  829. p_type == TK_TYPE_INT ||
  830. p_type == TK_TYPE_IVEC2 ||
  831. p_type == TK_TYPE_IVEC3 ||
  832. p_type == TK_TYPE_IVEC4 ||
  833. p_type == TK_TYPE_UINT ||
  834. p_type == TK_TYPE_UVEC2 ||
  835. p_type == TK_TYPE_UVEC3 ||
  836. p_type == TK_TYPE_UVEC4 ||
  837. p_type == TK_TYPE_FLOAT ||
  838. p_type == TK_TYPE_VEC2 ||
  839. p_type == TK_TYPE_VEC3 ||
  840. p_type == TK_TYPE_VEC4 ||
  841. p_type == TK_TYPE_MAT2 ||
  842. p_type == TK_TYPE_MAT3 ||
  843. p_type == TK_TYPE_MAT4);
  844. }
  845. bool ShaderLanguage::is_token_datatype(TokenType p_type) {
  846. return (
  847. p_type == TK_TYPE_VOID ||
  848. p_type == TK_TYPE_BOOL ||
  849. p_type == TK_TYPE_BVEC2 ||
  850. p_type == TK_TYPE_BVEC3 ||
  851. p_type == TK_TYPE_BVEC4 ||
  852. p_type == TK_TYPE_INT ||
  853. p_type == TK_TYPE_IVEC2 ||
  854. p_type == TK_TYPE_IVEC3 ||
  855. p_type == TK_TYPE_IVEC4 ||
  856. p_type == TK_TYPE_UINT ||
  857. p_type == TK_TYPE_UVEC2 ||
  858. p_type == TK_TYPE_UVEC3 ||
  859. p_type == TK_TYPE_UVEC4 ||
  860. p_type == TK_TYPE_FLOAT ||
  861. p_type == TK_TYPE_VEC2 ||
  862. p_type == TK_TYPE_VEC3 ||
  863. p_type == TK_TYPE_VEC4 ||
  864. p_type == TK_TYPE_MAT2 ||
  865. p_type == TK_TYPE_MAT3 ||
  866. p_type == TK_TYPE_MAT4 ||
  867. p_type == TK_TYPE_SAMPLER2D ||
  868. p_type == TK_TYPE_ISAMPLER2D ||
  869. p_type == TK_TYPE_USAMPLER2D ||
  870. p_type == TK_TYPE_SAMPLER2DARRAY ||
  871. p_type == TK_TYPE_ISAMPLER2DARRAY ||
  872. p_type == TK_TYPE_USAMPLER2DARRAY ||
  873. p_type == TK_TYPE_SAMPLER3D ||
  874. p_type == TK_TYPE_ISAMPLER3D ||
  875. p_type == TK_TYPE_USAMPLER3D ||
  876. p_type == TK_TYPE_SAMPLERCUBE ||
  877. p_type == TK_TYPE_SAMPLERCUBEARRAY);
  878. }
  879. ShaderLanguage::DataType ShaderLanguage::get_token_datatype(TokenType p_type) {
  880. return DataType(p_type - TK_TYPE_VOID);
  881. }
  882. bool ShaderLanguage::is_token_interpolation(TokenType p_type) {
  883. return (
  884. p_type == TK_INTERPOLATION_FLAT ||
  885. p_type == TK_INTERPOLATION_SMOOTH);
  886. }
  887. ShaderLanguage::DataInterpolation ShaderLanguage::get_token_interpolation(TokenType p_type) {
  888. if (p_type == TK_INTERPOLATION_FLAT) {
  889. return INTERPOLATION_FLAT;
  890. } else {
  891. return INTERPOLATION_SMOOTH;
  892. }
  893. }
  894. bool ShaderLanguage::is_token_precision(TokenType p_type) {
  895. return (
  896. p_type == TK_PRECISION_LOW ||
  897. p_type == TK_PRECISION_MID ||
  898. p_type == TK_PRECISION_HIGH);
  899. }
  900. bool ShaderLanguage::is_token_arg_qual(TokenType p_type) {
  901. return (
  902. p_type == TK_ARG_IN ||
  903. p_type == TK_ARG_OUT ||
  904. p_type == TK_ARG_INOUT);
  905. }
  906. ShaderLanguage::DataPrecision ShaderLanguage::get_token_precision(TokenType p_type) {
  907. if (p_type == TK_PRECISION_LOW) {
  908. return PRECISION_LOWP;
  909. } else if (p_type == TK_PRECISION_HIGH) {
  910. return PRECISION_HIGHP;
  911. } else {
  912. return PRECISION_MEDIUMP;
  913. }
  914. }
  915. String ShaderLanguage::get_precision_name(DataPrecision p_type) {
  916. switch (p_type) {
  917. case PRECISION_LOWP:
  918. return "lowp";
  919. case PRECISION_MEDIUMP:
  920. return "mediump";
  921. case PRECISION_HIGHP:
  922. return "highp";
  923. default:
  924. break;
  925. }
  926. return "";
  927. }
  928. String ShaderLanguage::get_interpolation_name(DataInterpolation p_interpolation) {
  929. switch (p_interpolation) {
  930. case INTERPOLATION_FLAT:
  931. return "flat";
  932. case INTERPOLATION_SMOOTH:
  933. return "smooth";
  934. default:
  935. break;
  936. }
  937. return "";
  938. }
  939. String ShaderLanguage::get_datatype_name(DataType p_type) {
  940. switch (p_type) {
  941. case TYPE_VOID:
  942. return "void";
  943. case TYPE_BOOL:
  944. return "bool";
  945. case TYPE_BVEC2:
  946. return "bvec2";
  947. case TYPE_BVEC3:
  948. return "bvec3";
  949. case TYPE_BVEC4:
  950. return "bvec4";
  951. case TYPE_INT:
  952. return "int";
  953. case TYPE_IVEC2:
  954. return "ivec2";
  955. case TYPE_IVEC3:
  956. return "ivec3";
  957. case TYPE_IVEC4:
  958. return "ivec4";
  959. case TYPE_UINT:
  960. return "uint";
  961. case TYPE_UVEC2:
  962. return "uvec2";
  963. case TYPE_UVEC3:
  964. return "uvec3";
  965. case TYPE_UVEC4:
  966. return "uvec4";
  967. case TYPE_FLOAT:
  968. return "float";
  969. case TYPE_VEC2:
  970. return "vec2";
  971. case TYPE_VEC3:
  972. return "vec3";
  973. case TYPE_VEC4:
  974. return "vec4";
  975. case TYPE_MAT2:
  976. return "mat2";
  977. case TYPE_MAT3:
  978. return "mat3";
  979. case TYPE_MAT4:
  980. return "mat4";
  981. case TYPE_SAMPLER2D:
  982. return "sampler2D";
  983. case TYPE_ISAMPLER2D:
  984. return "isampler2D";
  985. case TYPE_USAMPLER2D:
  986. return "usampler2D";
  987. case TYPE_SAMPLER2DARRAY:
  988. return "sampler2DArray";
  989. case TYPE_ISAMPLER2DARRAY:
  990. return "isampler2DArray";
  991. case TYPE_USAMPLER2DARRAY:
  992. return "usampler2DArray";
  993. case TYPE_SAMPLER3D:
  994. return "sampler3D";
  995. case TYPE_ISAMPLER3D:
  996. return "isampler3D";
  997. case TYPE_USAMPLER3D:
  998. return "usampler3D";
  999. case TYPE_SAMPLERCUBE:
  1000. return "samplerCube";
  1001. case TYPE_SAMPLERCUBEARRAY:
  1002. return "samplerCubeArray";
  1003. case TYPE_STRUCT:
  1004. return "struct";
  1005. case TYPE_MAX:
  1006. return "invalid";
  1007. }
  1008. return "";
  1009. }
  1010. String ShaderLanguage::get_uniform_hint_name(ShaderNode::Uniform::Hint p_hint) {
  1011. String result;
  1012. switch (p_hint) {
  1013. case ShaderNode::Uniform::HINT_RANGE: {
  1014. result = "hint_range";
  1015. } break;
  1016. case ShaderNode::Uniform::HINT_SOURCE_COLOR: {
  1017. result = "source_color";
  1018. } break;
  1019. case ShaderNode::Uniform::HINT_NORMAL: {
  1020. result = "hint_normal";
  1021. } break;
  1022. case ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL: {
  1023. result = "hint_roughness_normal";
  1024. } break;
  1025. case ShaderNode::Uniform::HINT_ROUGHNESS_R: {
  1026. result = "hint_roughness_r";
  1027. } break;
  1028. case ShaderNode::Uniform::HINT_ROUGHNESS_G: {
  1029. result = "hint_roughness_g";
  1030. } break;
  1031. case ShaderNode::Uniform::HINT_ROUGHNESS_B: {
  1032. result = "hint_roughness_b";
  1033. } break;
  1034. case ShaderNode::Uniform::HINT_ROUGHNESS_A: {
  1035. result = "hint_roughness_a";
  1036. } break;
  1037. case ShaderNode::Uniform::HINT_ROUGHNESS_GRAY: {
  1038. result = "hint_roughness_gray";
  1039. } break;
  1040. case ShaderNode::Uniform::HINT_DEFAULT_BLACK: {
  1041. result = "hint_default_black";
  1042. } break;
  1043. case ShaderNode::Uniform::HINT_DEFAULT_WHITE: {
  1044. result = "hint_default_white";
  1045. } break;
  1046. case ShaderNode::Uniform::HINT_DEFAULT_TRANSPARENT: {
  1047. result = "hint_default_transparent";
  1048. } break;
  1049. case ShaderNode::Uniform::HINT_ANISOTROPY: {
  1050. result = "hint_anisotropy";
  1051. } break;
  1052. case ShaderNode::Uniform::HINT_SCREEN_TEXTURE: {
  1053. result = "hint_screen_texture";
  1054. } break;
  1055. case ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE: {
  1056. result = "hint_normal_roughness_texture";
  1057. } break;
  1058. case ShaderNode::Uniform::HINT_DEPTH_TEXTURE: {
  1059. result = "hint_depth_texture";
  1060. } break;
  1061. default:
  1062. break;
  1063. }
  1064. return result;
  1065. }
  1066. String ShaderLanguage::get_texture_filter_name(TextureFilter p_filter) {
  1067. String result;
  1068. switch (p_filter) {
  1069. case FILTER_NEAREST: {
  1070. result = "filter_nearest";
  1071. } break;
  1072. case FILTER_LINEAR: {
  1073. result = "filter_linear";
  1074. } break;
  1075. case FILTER_NEAREST_MIPMAP: {
  1076. result = "filter_nearest_mipmap";
  1077. } break;
  1078. case FILTER_LINEAR_MIPMAP: {
  1079. result = "filter_linear_mipmap";
  1080. } break;
  1081. case FILTER_NEAREST_MIPMAP_ANISOTROPIC: {
  1082. result = "filter_nearest_mipmap_anisotropic";
  1083. } break;
  1084. case FILTER_LINEAR_MIPMAP_ANISOTROPIC: {
  1085. result = "filter_linear_mipmap_anisotropic";
  1086. } break;
  1087. default: {
  1088. } break;
  1089. }
  1090. return result;
  1091. }
  1092. String ShaderLanguage::get_texture_repeat_name(TextureRepeat p_repeat) {
  1093. String result;
  1094. switch (p_repeat) {
  1095. case REPEAT_DISABLE: {
  1096. result = "repeat_disable";
  1097. } break;
  1098. case REPEAT_ENABLE: {
  1099. result = "repeat_enable";
  1100. } break;
  1101. default: {
  1102. } break;
  1103. }
  1104. return result;
  1105. }
  1106. bool ShaderLanguage::is_token_nonvoid_datatype(TokenType p_type) {
  1107. return is_token_datatype(p_type) && p_type != TK_TYPE_VOID;
  1108. }
  1109. void ShaderLanguage::clear() {
  1110. current_function = StringName();
  1111. last_name = StringName();
  1112. last_type = IDENTIFIER_MAX;
  1113. current_uniform_group_name = "";
  1114. current_uniform_subgroup_name = "";
  1115. current_uniform_hint = ShaderNode::Uniform::HINT_NONE;
  1116. current_uniform_filter = FILTER_DEFAULT;
  1117. current_uniform_repeat = REPEAT_DEFAULT;
  1118. current_uniform_instance_index_defined = false;
  1119. completion_type = COMPLETION_NONE;
  1120. completion_block = nullptr;
  1121. completion_function = StringName();
  1122. completion_class = TAG_GLOBAL;
  1123. completion_struct = StringName();
  1124. completion_base = TYPE_VOID;
  1125. completion_base_array = false;
  1126. include_positions.clear();
  1127. include_positions.push_back(FilePosition());
  1128. include_markers_handled.clear();
  1129. calls_info.clear();
  1130. #ifdef DEBUG_ENABLED
  1131. keyword_completion_context = CF_UNSPECIFIED;
  1132. used_constants.clear();
  1133. used_varyings.clear();
  1134. used_uniforms.clear();
  1135. used_functions.clear();
  1136. used_structs.clear();
  1137. used_local_vars.clear();
  1138. warnings.clear();
  1139. #endif // DEBUG_ENABLED
  1140. error_line = 0;
  1141. tk_line = 1;
  1142. char_idx = 0;
  1143. error_set = false;
  1144. error_str = "";
  1145. is_const_decl = false;
  1146. while (nodes) {
  1147. Node *n = nodes;
  1148. nodes = nodes->next;
  1149. memdelete(n);
  1150. }
  1151. }
  1152. #ifdef DEBUG_ENABLED
  1153. void ShaderLanguage::_parse_used_identifier(const StringName &p_identifier, IdentifierType p_type, const StringName &p_function) {
  1154. switch (p_type) {
  1155. case IdentifierType::IDENTIFIER_CONSTANT:
  1156. if (HAS_WARNING(ShaderWarning::UNUSED_CONSTANT_FLAG) && used_constants.has(p_identifier)) {
  1157. used_constants[p_identifier].used = true;
  1158. }
  1159. break;
  1160. case IdentifierType::IDENTIFIER_VARYING:
  1161. if (HAS_WARNING(ShaderWarning::UNUSED_VARYING_FLAG) && used_varyings.has(p_identifier)) {
  1162. if (shader->varyings[p_identifier].stage != ShaderNode::Varying::STAGE_VERTEX && shader->varyings[p_identifier].stage != ShaderNode::Varying::STAGE_FRAGMENT) {
  1163. used_varyings[p_identifier].used = true;
  1164. }
  1165. }
  1166. break;
  1167. case IdentifierType::IDENTIFIER_UNIFORM:
  1168. if (HAS_WARNING(ShaderWarning::UNUSED_UNIFORM_FLAG) && used_uniforms.has(p_identifier)) {
  1169. used_uniforms[p_identifier].used = true;
  1170. }
  1171. break;
  1172. case IdentifierType::IDENTIFIER_FUNCTION:
  1173. if (HAS_WARNING(ShaderWarning::UNUSED_FUNCTION_FLAG) && used_functions.has(p_identifier)) {
  1174. used_functions[p_identifier].used = true;
  1175. }
  1176. break;
  1177. case IdentifierType::IDENTIFIER_LOCAL_VAR:
  1178. if (HAS_WARNING(ShaderWarning::UNUSED_LOCAL_VARIABLE_FLAG) && used_local_vars.has(p_function) && used_local_vars[p_function].has(p_identifier)) {
  1179. used_local_vars[p_function][p_identifier].used = true;
  1180. }
  1181. break;
  1182. default:
  1183. break;
  1184. }
  1185. }
  1186. #endif // DEBUG_ENABLED
  1187. 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) {
  1188. if (is_shader_inc) {
  1189. for (int i = 0; i < RenderingServer::SHADER_MAX; i++) {
  1190. for (const KeyValue<StringName, FunctionInfo> &E : ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(i))) {
  1191. if ((current_function == E.key || E.key == "global" || E.key == "constants") && E.value.built_ins.has(p_identifier)) {
  1192. if (r_data_type) {
  1193. *r_data_type = E.value.built_ins[p_identifier].type;
  1194. }
  1195. if (r_is_const) {
  1196. *r_is_const = E.value.built_ins[p_identifier].constant;
  1197. }
  1198. if (r_type) {
  1199. *r_type = IDENTIFIER_BUILTIN_VAR;
  1200. }
  1201. return true;
  1202. }
  1203. }
  1204. }
  1205. } else {
  1206. if (p_function_info.built_ins.has(p_identifier)) {
  1207. if (r_data_type) {
  1208. *r_data_type = p_function_info.built_ins[p_identifier].type;
  1209. }
  1210. if (r_is_const) {
  1211. *r_is_const = p_function_info.built_ins[p_identifier].constant;
  1212. }
  1213. if (r_type) {
  1214. *r_type = IDENTIFIER_BUILTIN_VAR;
  1215. }
  1216. return true;
  1217. }
  1218. }
  1219. if (p_function_info.stage_functions.has(p_identifier)) {
  1220. if (r_data_type) {
  1221. *r_data_type = p_function_info.stage_functions[p_identifier].return_type;
  1222. }
  1223. if (r_is_const) {
  1224. *r_is_const = true;
  1225. }
  1226. if (r_type) {
  1227. *r_type = IDENTIFIER_FUNCTION;
  1228. }
  1229. return true;
  1230. }
  1231. FunctionNode *function = nullptr;
  1232. while (p_block) {
  1233. if (p_block->variables.has(p_identifier)) {
  1234. if (r_data_type) {
  1235. *r_data_type = p_block->variables[p_identifier].type;
  1236. }
  1237. if (r_is_const) {
  1238. *r_is_const = p_block->variables[p_identifier].is_const;
  1239. }
  1240. if (r_array_size) {
  1241. *r_array_size = p_block->variables[p_identifier].array_size;
  1242. }
  1243. if (r_struct_name) {
  1244. *r_struct_name = p_block->variables[p_identifier].struct_name;
  1245. }
  1246. if (r_constant_value) {
  1247. *r_constant_value = p_block->variables[p_identifier].value;
  1248. }
  1249. if (r_type) {
  1250. *r_type = IDENTIFIER_LOCAL_VAR;
  1251. }
  1252. return true;
  1253. }
  1254. if (p_block->parent_function) {
  1255. function = p_block->parent_function;
  1256. break;
  1257. } else {
  1258. if (p_allow_reassign) {
  1259. break;
  1260. }
  1261. ERR_FAIL_NULL_V(p_block->parent_block, false);
  1262. p_block = p_block->parent_block;
  1263. }
  1264. }
  1265. if (function) {
  1266. for (int i = 0; i < function->arguments.size(); i++) {
  1267. if (function->arguments[i].name == p_identifier) {
  1268. if (r_data_type) {
  1269. *r_data_type = function->arguments[i].type;
  1270. }
  1271. if (r_struct_name) {
  1272. *r_struct_name = function->arguments[i].struct_name;
  1273. }
  1274. if (r_array_size) {
  1275. *r_array_size = function->arguments[i].array_size;
  1276. }
  1277. if (r_is_const) {
  1278. *r_is_const = function->arguments[i].is_const;
  1279. }
  1280. if (r_type) {
  1281. *r_type = IDENTIFIER_FUNCTION_ARGUMENT;
  1282. }
  1283. return true;
  1284. }
  1285. }
  1286. }
  1287. if (shader->varyings.has(p_identifier)) {
  1288. if (r_data_type) {
  1289. *r_data_type = shader->varyings[p_identifier].type;
  1290. }
  1291. if (r_array_size) {
  1292. *r_array_size = shader->varyings[p_identifier].array_size;
  1293. }
  1294. if (r_type) {
  1295. *r_type = IDENTIFIER_VARYING;
  1296. }
  1297. return true;
  1298. }
  1299. if (shader->uniforms.has(p_identifier)) {
  1300. if (r_data_type) {
  1301. *r_data_type = shader->uniforms[p_identifier].type;
  1302. }
  1303. if (r_array_size) {
  1304. *r_array_size = shader->uniforms[p_identifier].array_size;
  1305. }
  1306. if (r_type) {
  1307. *r_type = IDENTIFIER_UNIFORM;
  1308. }
  1309. return true;
  1310. }
  1311. if (shader->constants.has(p_identifier)) {
  1312. if (r_is_const) {
  1313. *r_is_const = true;
  1314. }
  1315. if (r_data_type) {
  1316. *r_data_type = shader->constants[p_identifier].type;
  1317. }
  1318. if (r_array_size) {
  1319. *r_array_size = shader->constants[p_identifier].array_size;
  1320. }
  1321. if (r_struct_name) {
  1322. *r_struct_name = shader->constants[p_identifier].struct_name;
  1323. }
  1324. if (r_constant_value) {
  1325. if (shader->constants[p_identifier].initializer && shader->constants[p_identifier].initializer->type == Node::NODE_TYPE_CONSTANT) {
  1326. ConstantNode *cnode = static_cast<ConstantNode *>(shader->constants[p_identifier].initializer);
  1327. if (cnode->values.size() == 1) {
  1328. *r_constant_value = cnode->values[0];
  1329. }
  1330. }
  1331. }
  1332. if (r_type) {
  1333. *r_type = IDENTIFIER_CONSTANT;
  1334. }
  1335. return true;
  1336. }
  1337. for (int i = 0; i < shader->vfunctions.size(); i++) {
  1338. if (!shader->vfunctions[i].callable) {
  1339. continue;
  1340. }
  1341. if (shader->vfunctions[i].name == p_identifier) {
  1342. if (r_data_type) {
  1343. *r_data_type = shader->vfunctions[i].function->return_type;
  1344. }
  1345. if (r_array_size) {
  1346. *r_array_size = shader->vfunctions[i].function->return_array_size;
  1347. }
  1348. if (r_type) {
  1349. *r_type = IDENTIFIER_FUNCTION;
  1350. }
  1351. return true;
  1352. }
  1353. }
  1354. return false;
  1355. }
  1356. bool ShaderLanguage::_validate_operator(OperatorNode *p_op, DataType *r_ret_type, int *r_ret_size) {
  1357. bool valid = false;
  1358. DataType ret_type = TYPE_VOID;
  1359. int ret_size = 0;
  1360. switch (p_op->op) {
  1361. case OP_EQUAL:
  1362. case OP_NOT_EQUAL: {
  1363. 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)) {
  1364. break; // don't accept arrays
  1365. }
  1366. DataType na = p_op->arguments[0]->get_datatype();
  1367. DataType nb = p_op->arguments[1]->get_datatype();
  1368. valid = na == nb;
  1369. ret_type = TYPE_BOOL;
  1370. } break;
  1371. case OP_LESS:
  1372. case OP_LESS_EQUAL:
  1373. case OP_GREATER:
  1374. case OP_GREATER_EQUAL: {
  1375. 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)) {
  1376. break; // don't accept arrays
  1377. }
  1378. DataType na = p_op->arguments[0]->get_datatype();
  1379. DataType nb = p_op->arguments[1]->get_datatype();
  1380. valid = na == nb && (na == TYPE_UINT || na == TYPE_INT || na == TYPE_FLOAT);
  1381. ret_type = TYPE_BOOL;
  1382. } break;
  1383. case OP_AND:
  1384. case OP_OR: {
  1385. 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)) {
  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. valid = na == nb && na == TYPE_BOOL;
  1391. ret_type = TYPE_BOOL;
  1392. } break;
  1393. case OP_NOT: {
  1394. if (!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) {
  1395. break; // don't accept arrays
  1396. }
  1397. DataType na = p_op->arguments[0]->get_datatype();
  1398. valid = na == TYPE_BOOL;
  1399. ret_type = TYPE_BOOL;
  1400. } break;
  1401. case OP_INCREMENT:
  1402. case OP_DECREMENT:
  1403. case OP_POST_INCREMENT:
  1404. case OP_POST_DECREMENT:
  1405. case OP_NEGATE: {
  1406. if (!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) {
  1407. break; // don't accept arrays
  1408. }
  1409. DataType na = p_op->arguments[0]->get_datatype();
  1410. valid = na > TYPE_BVEC4 && na < TYPE_MAT2;
  1411. ret_type = na;
  1412. } break;
  1413. case OP_ADD:
  1414. case OP_SUB:
  1415. case OP_MUL:
  1416. case OP_DIV: {
  1417. 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)) {
  1418. break; // don't accept arrays
  1419. }
  1420. DataType na = p_op->arguments[0]->get_datatype();
  1421. DataType nb = p_op->arguments[1]->get_datatype();
  1422. if (na > nb) {
  1423. //make things easier;
  1424. SWAP(na, nb);
  1425. }
  1426. if (na == nb) {
  1427. valid = (na > TYPE_BVEC4 && na <= TYPE_MAT4);
  1428. ret_type = na;
  1429. } else if (na == TYPE_INT && nb == TYPE_IVEC2) {
  1430. valid = true;
  1431. ret_type = TYPE_IVEC2;
  1432. } else if (na == TYPE_INT && nb == TYPE_IVEC3) {
  1433. valid = true;
  1434. ret_type = TYPE_IVEC3;
  1435. } else if (na == TYPE_INT && nb == TYPE_IVEC4) {
  1436. valid = true;
  1437. ret_type = TYPE_IVEC4;
  1438. } else if (na == TYPE_UINT && nb == TYPE_UVEC2) {
  1439. valid = true;
  1440. ret_type = TYPE_UVEC2;
  1441. } else if (na == TYPE_UINT && nb == TYPE_UVEC3) {
  1442. valid = true;
  1443. ret_type = TYPE_UVEC3;
  1444. } else if (na == TYPE_UINT && nb == TYPE_UVEC4) {
  1445. valid = true;
  1446. ret_type = TYPE_UVEC4;
  1447. } else if (na == TYPE_FLOAT && nb == TYPE_VEC2) {
  1448. valid = true;
  1449. ret_type = TYPE_VEC2;
  1450. } else if (na == TYPE_FLOAT && nb == TYPE_VEC3) {
  1451. valid = true;
  1452. ret_type = TYPE_VEC3;
  1453. } else if (na == TYPE_FLOAT && nb == TYPE_VEC4) {
  1454. valid = true;
  1455. ret_type = TYPE_VEC4;
  1456. } else if (na == TYPE_FLOAT && nb == TYPE_MAT2) {
  1457. valid = true;
  1458. ret_type = TYPE_MAT2;
  1459. } else if (na == TYPE_FLOAT && nb == TYPE_MAT3) {
  1460. valid = true;
  1461. ret_type = TYPE_MAT3;
  1462. } else if (na == TYPE_FLOAT && nb == TYPE_MAT4) {
  1463. valid = true;
  1464. ret_type = TYPE_MAT4;
  1465. } else if (p_op->op == OP_MUL && na == TYPE_VEC2 && nb == TYPE_MAT2) {
  1466. valid = true;
  1467. ret_type = TYPE_VEC2;
  1468. } else if (p_op->op == OP_MUL && na == TYPE_VEC3 && nb == TYPE_MAT3) {
  1469. valid = true;
  1470. ret_type = TYPE_VEC3;
  1471. } else if (p_op->op == OP_MUL && na == TYPE_VEC4 && nb == TYPE_MAT4) {
  1472. valid = true;
  1473. ret_type = TYPE_VEC4;
  1474. }
  1475. } break;
  1476. case OP_ASSIGN_MOD:
  1477. case OP_MOD: {
  1478. /*
  1479. * The operator modulus (%) operates on signed or unsigned integers or integer vectors. The operand
  1480. * types must both be signed or both be unsigned. The operands cannot be vectors of differing size. If
  1481. * one operand is a scalar and the other vector, then the scalar is applied component-wise to the vector,
  1482. * resulting in the same type as the vector. If both are vectors of the same size, the result is computed
  1483. * component-wise.
  1484. */
  1485. 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)) {
  1486. break; // don't accept arrays
  1487. }
  1488. DataType na = p_op->arguments[0]->get_datatype();
  1489. DataType nb = p_op->arguments[1]->get_datatype();
  1490. if (na == TYPE_INT && nb == TYPE_INT) {
  1491. valid = true;
  1492. ret_type = TYPE_INT;
  1493. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1494. valid = true;
  1495. ret_type = TYPE_IVEC2;
  1496. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1497. valid = true;
  1498. ret_type = TYPE_IVEC3;
  1499. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1500. valid = true;
  1501. ret_type = TYPE_IVEC4;
  1502. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  1503. valid = true;
  1504. ret_type = TYPE_IVEC2;
  1505. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  1506. valid = true;
  1507. ret_type = TYPE_IVEC3;
  1508. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  1509. valid = true;
  1510. ret_type = TYPE_IVEC4;
  1511. /////
  1512. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1513. valid = true;
  1514. ret_type = TYPE_UINT;
  1515. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1516. valid = true;
  1517. ret_type = TYPE_UVEC2;
  1518. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1519. valid = true;
  1520. ret_type = TYPE_UVEC3;
  1521. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1522. valid = true;
  1523. ret_type = TYPE_UVEC4;
  1524. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1525. valid = true;
  1526. ret_type = TYPE_UVEC2;
  1527. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1528. valid = true;
  1529. ret_type = TYPE_UVEC3;
  1530. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1531. valid = true;
  1532. ret_type = TYPE_UVEC4;
  1533. }
  1534. } break;
  1535. case OP_ASSIGN_SHIFT_LEFT:
  1536. case OP_ASSIGN_SHIFT_RIGHT:
  1537. case OP_SHIFT_LEFT:
  1538. case OP_SHIFT_RIGHT: {
  1539. 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)) {
  1540. break; // don't accept arrays
  1541. }
  1542. DataType na = p_op->arguments[0]->get_datatype();
  1543. DataType nb = p_op->arguments[1]->get_datatype();
  1544. if (na == TYPE_INT && nb == TYPE_INT) {
  1545. valid = true;
  1546. ret_type = TYPE_INT;
  1547. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1548. valid = true;
  1549. ret_type = TYPE_IVEC2;
  1550. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1551. valid = true;
  1552. ret_type = TYPE_IVEC3;
  1553. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1554. valid = true;
  1555. ret_type = TYPE_IVEC4;
  1556. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  1557. valid = true;
  1558. ret_type = TYPE_IVEC2;
  1559. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  1560. valid = true;
  1561. ret_type = TYPE_IVEC3;
  1562. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  1563. valid = true;
  1564. ret_type = TYPE_IVEC4;
  1565. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1566. valid = true;
  1567. ret_type = TYPE_UINT;
  1568. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1569. valid = true;
  1570. ret_type = TYPE_UVEC2;
  1571. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1572. valid = true;
  1573. ret_type = TYPE_UVEC3;
  1574. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1575. valid = true;
  1576. ret_type = TYPE_UVEC4;
  1577. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1578. valid = true;
  1579. ret_type = TYPE_UVEC2;
  1580. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1581. valid = true;
  1582. ret_type = TYPE_UVEC3;
  1583. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1584. valid = true;
  1585. ret_type = TYPE_UVEC4;
  1586. }
  1587. } break;
  1588. case OP_ASSIGN: {
  1589. int sa = 0;
  1590. int sb = 0;
  1591. if (!p_op->arguments[0]->is_indexed()) {
  1592. sa = p_op->arguments[0]->get_array_size();
  1593. }
  1594. if (!p_op->arguments[1]->is_indexed()) {
  1595. sb = p_op->arguments[1]->get_array_size();
  1596. }
  1597. if (sa != sb) {
  1598. break; // don't accept arrays if their sizes are not equal
  1599. }
  1600. DataType na = p_op->arguments[0]->get_datatype();
  1601. DataType nb = p_op->arguments[1]->get_datatype();
  1602. if (na == TYPE_STRUCT || nb == TYPE_STRUCT) {
  1603. valid = p_op->arguments[0]->get_datatype_name() == p_op->arguments[1]->get_datatype_name();
  1604. } else {
  1605. valid = na == nb;
  1606. }
  1607. ret_type = na;
  1608. ret_size = sa;
  1609. } break;
  1610. case OP_ASSIGN_ADD:
  1611. case OP_ASSIGN_SUB:
  1612. case OP_ASSIGN_MUL:
  1613. case OP_ASSIGN_DIV: {
  1614. int sa = 0;
  1615. int sb = 0;
  1616. if (!p_op->arguments[0]->is_indexed()) {
  1617. sa = p_op->arguments[0]->get_array_size();
  1618. }
  1619. if (!p_op->arguments[1]->is_indexed()) {
  1620. sb = p_op->arguments[1]->get_array_size();
  1621. }
  1622. if (sa > 0 || sb > 0) {
  1623. break; // don't accept arrays
  1624. }
  1625. DataType na = p_op->arguments[0]->get_datatype();
  1626. DataType nb = p_op->arguments[1]->get_datatype();
  1627. if (na == nb) {
  1628. valid = (na > TYPE_BVEC4 && na <= TYPE_MAT4);
  1629. ret_type = na;
  1630. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1631. valid = true;
  1632. ret_type = TYPE_IVEC2;
  1633. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1634. valid = true;
  1635. ret_type = TYPE_IVEC3;
  1636. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1637. valid = true;
  1638. ret_type = TYPE_IVEC4;
  1639. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1640. valid = true;
  1641. ret_type = TYPE_UVEC2;
  1642. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1643. valid = true;
  1644. ret_type = TYPE_UVEC3;
  1645. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1646. valid = true;
  1647. ret_type = TYPE_UVEC4;
  1648. } else if (na == TYPE_VEC2 && nb == TYPE_FLOAT) {
  1649. valid = true;
  1650. ret_type = TYPE_VEC2;
  1651. } else if (na == TYPE_VEC3 && nb == TYPE_FLOAT) {
  1652. valid = true;
  1653. ret_type = TYPE_VEC3;
  1654. } else if (na == TYPE_VEC4 && nb == TYPE_FLOAT) {
  1655. valid = true;
  1656. ret_type = TYPE_VEC4;
  1657. } else if (na == TYPE_MAT2 && nb == TYPE_FLOAT) {
  1658. valid = true;
  1659. ret_type = TYPE_MAT2;
  1660. } else if (na == TYPE_MAT3 && nb == TYPE_FLOAT) {
  1661. valid = true;
  1662. ret_type = TYPE_MAT3;
  1663. } else if (na == TYPE_MAT4 && nb == TYPE_FLOAT) {
  1664. valid = true;
  1665. ret_type = TYPE_MAT4;
  1666. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC2 && nb == TYPE_MAT2) {
  1667. valid = true;
  1668. ret_type = TYPE_VEC2;
  1669. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC3 && nb == TYPE_MAT3) {
  1670. valid = true;
  1671. ret_type = TYPE_VEC3;
  1672. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC4 && nb == TYPE_MAT4) {
  1673. valid = true;
  1674. ret_type = TYPE_VEC4;
  1675. }
  1676. } break;
  1677. case OP_ASSIGN_BIT_AND:
  1678. case OP_ASSIGN_BIT_OR:
  1679. case OP_ASSIGN_BIT_XOR:
  1680. case OP_BIT_AND:
  1681. case OP_BIT_OR:
  1682. case OP_BIT_XOR: {
  1683. /*
  1684. * The bitwise operators and (&), exclusive-or (^), and inclusive-or (|). The operands must be of type
  1685. * signed or unsigned integers or integer vectors. The operands cannot be vectors of differing size. If
  1686. * one operand is a scalar and the other a vector, the scalar is applied component-wise to the vector,
  1687. * resulting in the same type as the vector. The fundamental types of the operands (signed or unsigned)
  1688. * must match.
  1689. */
  1690. int sa = 0;
  1691. int sb = 0;
  1692. if (!p_op->arguments[0]->is_indexed()) {
  1693. sa = p_op->arguments[0]->get_array_size();
  1694. }
  1695. if (!p_op->arguments[1]->is_indexed()) {
  1696. sb = p_op->arguments[1]->get_array_size();
  1697. }
  1698. if (sa > 0 || sb > 0) {
  1699. break; // don't accept arrays
  1700. }
  1701. DataType na = p_op->arguments[0]->get_datatype();
  1702. DataType nb = p_op->arguments[1]->get_datatype();
  1703. if (na > nb && p_op->op >= OP_BIT_AND) {
  1704. //can swap for non assign
  1705. SWAP(na, nb);
  1706. }
  1707. if (na == TYPE_INT && nb == TYPE_INT) {
  1708. valid = true;
  1709. ret_type = TYPE_INT;
  1710. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1711. valid = true;
  1712. ret_type = TYPE_IVEC2;
  1713. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1714. valid = true;
  1715. ret_type = TYPE_IVEC3;
  1716. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1717. valid = true;
  1718. ret_type = TYPE_IVEC4;
  1719. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  1720. valid = true;
  1721. ret_type = TYPE_IVEC2;
  1722. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  1723. valid = true;
  1724. ret_type = TYPE_IVEC3;
  1725. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  1726. valid = true;
  1727. ret_type = TYPE_IVEC4;
  1728. /////
  1729. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1730. valid = true;
  1731. ret_type = TYPE_UINT;
  1732. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1733. valid = true;
  1734. ret_type = TYPE_UVEC2;
  1735. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1736. valid = true;
  1737. ret_type = TYPE_UVEC3;
  1738. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1739. valid = true;
  1740. ret_type = TYPE_UVEC4;
  1741. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1742. valid = true;
  1743. ret_type = TYPE_UVEC2;
  1744. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1745. valid = true;
  1746. ret_type = TYPE_UVEC3;
  1747. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1748. valid = true;
  1749. ret_type = TYPE_UVEC4;
  1750. }
  1751. } break;
  1752. case OP_BIT_INVERT: { //unaries
  1753. if (!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) {
  1754. break; // don't accept arrays
  1755. }
  1756. DataType na = p_op->arguments[0]->get_datatype();
  1757. valid = na >= TYPE_INT && na < TYPE_FLOAT;
  1758. ret_type = na;
  1759. } break;
  1760. case OP_SELECT_IF: {
  1761. int sa = 0;
  1762. int sb = 0;
  1763. if (!p_op->arguments[1]->is_indexed()) {
  1764. sa = p_op->arguments[1]->get_array_size();
  1765. }
  1766. if (!p_op->arguments[2]->is_indexed()) {
  1767. sb = p_op->arguments[2]->get_array_size();
  1768. }
  1769. if (sa != sb) {
  1770. break; // don't accept arrays if their sizes are not equal
  1771. }
  1772. DataType na = p_op->arguments[0]->get_datatype();
  1773. DataType nb = p_op->arguments[1]->get_datatype();
  1774. DataType nc = p_op->arguments[2]->get_datatype();
  1775. valid = na == TYPE_BOOL && (nb == nc);
  1776. ret_type = nb;
  1777. ret_size = sa;
  1778. } break;
  1779. default: {
  1780. ERR_FAIL_V(false);
  1781. }
  1782. }
  1783. if (r_ret_type) {
  1784. *r_ret_type = ret_type;
  1785. }
  1786. if (r_ret_size) {
  1787. *r_ret_size = ret_size;
  1788. }
  1789. return valid;
  1790. }
  1791. const ShaderLanguage::BuiltinFuncDef ShaderLanguage::builtin_func_defs[] = {
  1792. // Constructors.
  1793. { "bool", TYPE_BOOL, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1794. { "bvec2", TYPE_BVEC2, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1795. { "bvec2", TYPE_BVEC2, { TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1796. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1797. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1798. { "bvec3", TYPE_BVEC3, { TYPE_BVEC2, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1799. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1800. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1801. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1802. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BVEC2, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1803. { "bvec4", TYPE_BVEC4, { TYPE_BVEC2, TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1804. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BOOL, TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1805. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1806. { "bvec4", TYPE_BVEC4, { TYPE_BVEC3, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1807. { "bvec4", TYPE_BVEC4, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1808. { "float", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1809. { "vec2", TYPE_VEC2, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1810. { "vec2", TYPE_VEC2, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1811. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1812. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1813. { "vec3", TYPE_VEC3, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1814. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1815. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1816. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1817. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1818. { "vec4", TYPE_VEC4, { TYPE_VEC2, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1819. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1820. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1821. { "vec4", TYPE_VEC4, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1822. { "vec4", TYPE_VEC4, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1823. { "int", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1824. { "ivec2", TYPE_IVEC2, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1825. { "ivec2", TYPE_IVEC2, { TYPE_INT, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1826. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1827. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1828. { "ivec3", TYPE_IVEC3, { TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1829. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1830. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1831. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1832. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1833. { "ivec4", TYPE_IVEC4, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1834. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_INT, TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1835. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1836. { "ivec4", TYPE_IVEC4, { TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1837. { "ivec4", TYPE_IVEC4, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1838. { "uint", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1839. { "uvec2", TYPE_UVEC2, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1840. { "uvec2", TYPE_UVEC2, { TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1841. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1842. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1843. { "uvec3", TYPE_UVEC3, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1844. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1845. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1846. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1847. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1848. { "uvec4", TYPE_UVEC4, { TYPE_UVEC2, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1849. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UINT, TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1850. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1851. { "uvec4", TYPE_UVEC4, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1852. { "uvec4", TYPE_UVEC4, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1853. { "mat2", TYPE_MAT2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1854. { "mat3", TYPE_MAT3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1855. { "mat4", TYPE_MAT4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1856. { "mat2", TYPE_MAT2, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1857. { "mat3", TYPE_MAT3, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1858. { "mat4", TYPE_MAT4, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1859. // Conversion scalars.
  1860. { "int", TYPE_INT, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1861. { "int", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1862. { "int", TYPE_INT, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1863. { "int", TYPE_INT, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1864. { "float", TYPE_FLOAT, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1865. { "float", TYPE_FLOAT, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1866. { "float", TYPE_FLOAT, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1867. { "float", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1868. { "uint", TYPE_UINT, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1869. { "uint", TYPE_UINT, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1870. { "uint", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1871. { "uint", TYPE_UINT, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1872. { "bool", TYPE_BOOL, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1873. { "bool", TYPE_BOOL, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1874. { "bool", TYPE_BOOL, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1875. { "bool", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1876. // Conversion vectors.
  1877. { "ivec2", TYPE_IVEC2, { TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1878. { "ivec2", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1879. { "ivec2", TYPE_IVEC2, { TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1880. { "ivec2", TYPE_IVEC2, { TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1881. { "vec2", TYPE_VEC2, { TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1882. { "vec2", TYPE_VEC2, { TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1883. { "vec2", TYPE_VEC2, { TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1884. { "vec2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1885. { "uvec2", TYPE_UVEC2, { TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1886. { "uvec2", TYPE_UVEC2, { TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1887. { "uvec2", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1888. { "uvec2", TYPE_UVEC2, { TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1889. { "bvec2", TYPE_BVEC2, { TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1890. { "bvec2", TYPE_BVEC2, { TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1891. { "bvec2", TYPE_BVEC2, { TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1892. { "bvec2", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1893. { "ivec3", TYPE_IVEC3, { TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1894. { "ivec3", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1895. { "ivec3", TYPE_IVEC3, { TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1896. { "ivec3", TYPE_IVEC3, { TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1897. { "vec3", TYPE_VEC3, { TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1898. { "vec3", TYPE_VEC3, { TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1899. { "vec3", TYPE_VEC3, { TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1900. { "vec3", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1901. { "uvec3", TYPE_UVEC3, { TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1902. { "uvec3", TYPE_UVEC3, { TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1903. { "uvec3", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1904. { "uvec3", TYPE_UVEC3, { TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1905. { "bvec3", TYPE_BVEC3, { TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1906. { "bvec3", TYPE_BVEC3, { TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1907. { "bvec3", TYPE_BVEC3, { TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1908. { "bvec3", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1909. { "ivec4", TYPE_IVEC4, { TYPE_BVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1910. { "ivec4", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1911. { "ivec4", TYPE_IVEC4, { TYPE_UVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1912. { "ivec4", TYPE_IVEC4, { TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1913. { "vec4", TYPE_VEC4, { TYPE_BVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1914. { "vec4", TYPE_VEC4, { TYPE_IVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1915. { "vec4", TYPE_VEC4, { TYPE_UVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1916. { "vec4", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1917. { "uvec4", TYPE_UVEC4, { TYPE_BVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1918. { "uvec4", TYPE_UVEC4, { TYPE_IVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1919. { "uvec4", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1920. { "uvec4", TYPE_UVEC4, { TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1921. { "bvec4", TYPE_BVEC4, { TYPE_BVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1922. { "bvec4", TYPE_BVEC4, { TYPE_IVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1923. { "bvec4", TYPE_BVEC4, { TYPE_UVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1924. { "bvec4", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1925. // Conversion between matrixes.
  1926. { "mat2", TYPE_MAT2, { TYPE_MAT3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1927. { "mat2", TYPE_MAT2, { TYPE_MAT4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1928. { "mat3", TYPE_MAT3, { TYPE_MAT2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1929. { "mat3", TYPE_MAT3, { TYPE_MAT4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1930. { "mat4", TYPE_MAT4, { TYPE_MAT2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1931. { "mat4", TYPE_MAT4, { TYPE_MAT3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1932. // Built-ins - trigonometric functions.
  1933. // radians
  1934. { "radians", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "degrees" }, TAG_GLOBAL, false },
  1935. { "radians", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "degrees" }, TAG_GLOBAL, false },
  1936. { "radians", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "degrees" }, TAG_GLOBAL, false },
  1937. { "radians", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "degrees" }, TAG_GLOBAL, false },
  1938. // degrees
  1939. { "degrees", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "radians" }, TAG_GLOBAL, false },
  1940. { "degrees", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "radians" }, TAG_GLOBAL, false },
  1941. { "degrees", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "radians" }, TAG_GLOBAL, false },
  1942. { "degrees", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "radians" }, TAG_GLOBAL, false },
  1943. // sin
  1944. { "sin", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1945. { "sin", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1946. { "sin", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1947. { "sin", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1948. // cos
  1949. { "cos", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1950. { "cos", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1951. { "cos", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1952. { "cos", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1953. // tan
  1954. { "tan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1955. { "tan", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1956. { "tan", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1957. { "tan", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1958. // asin
  1959. { "asin", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1960. { "asin", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1961. { "asin", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1962. { "asin", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1963. // acos
  1964. { "acos", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1965. { "acos", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1966. { "acos", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1967. { "acos", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1968. // atan
  1969. { "atan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "y_over_x" }, TAG_GLOBAL, false },
  1970. { "atan", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "y_over_x" }, TAG_GLOBAL, false },
  1971. { "atan", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "y_over_x" }, TAG_GLOBAL, false },
  1972. { "atan", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "y_over_x" }, TAG_GLOBAL, false },
  1973. { "atan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "y", "x" }, TAG_GLOBAL, false },
  1974. { "atan", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "y", "x" }, TAG_GLOBAL, false },
  1975. { "atan", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "y", "x" }, TAG_GLOBAL, false },
  1976. { "atan", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "y", "x" }, TAG_GLOBAL, false },
  1977. // sinh
  1978. { "sinh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1979. { "sinh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1980. { "sinh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1981. { "sinh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1982. // cosh
  1983. { "cosh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1984. { "cosh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1985. { "cosh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1986. { "cosh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1987. // tanh
  1988. { "tanh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1989. { "tanh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1990. { "tanh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1991. { "tanh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1992. // asinh
  1993. { "asinh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1994. { "asinh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1995. { "asinh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1996. { "asinh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1997. // acosh
  1998. { "acosh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1999. { "acosh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2000. { "acosh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2001. { "acosh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2002. // atanh
  2003. { "atanh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2004. { "atanh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2005. { "atanh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2006. { "atanh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2007. // Builtins - exponential functions.
  2008. // pow
  2009. { "pow", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2010. { "pow", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2011. { "pow", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2012. { "pow", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2013. // exp
  2014. { "exp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2015. { "exp", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2016. { "exp", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2017. { "exp", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2018. // log
  2019. { "log", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2020. { "log", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2021. { "log", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2022. { "log", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2023. // exp2
  2024. { "exp2", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2025. { "exp2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2026. { "exp2", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2027. { "exp2", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2028. // log2
  2029. { "log2", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2030. { "log2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2031. { "log2", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2032. { "log2", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2033. // sqrt
  2034. { "sqrt", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2035. { "sqrt", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2036. { "sqrt", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2037. { "sqrt", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2038. // inversesqrt
  2039. { "inversesqrt", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2040. { "inversesqrt", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2041. { "inversesqrt", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2042. { "inversesqrt", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2043. // Built-ins - common functions.
  2044. // abs
  2045. { "abs", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2046. { "abs", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2047. { "abs", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2048. { "abs", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2049. { "abs", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2050. { "abs", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2051. { "abs", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2052. { "abs", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2053. // sign
  2054. { "sign", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2055. { "sign", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2056. { "sign", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2057. { "sign", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2058. { "sign", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2059. { "sign", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2060. { "sign", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2061. { "sign", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2062. // floor
  2063. { "floor", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2064. { "floor", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2065. { "floor", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2066. { "floor", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2067. // trunc
  2068. { "trunc", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2069. { "trunc", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2070. { "trunc", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2071. { "trunc", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2072. // round
  2073. { "round", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2074. { "round", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2075. { "round", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2076. { "round", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2077. // roundEven
  2078. { "roundEven", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2079. { "roundEven", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2080. { "roundEven", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2081. { "roundEven", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2082. // ceil
  2083. { "ceil", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2084. { "ceil", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2085. { "ceil", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2086. { "ceil", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2087. // fract
  2088. { "fract", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2089. { "fract", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2090. { "fract", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2091. { "fract", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2092. // mod
  2093. { "mod", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2094. { "mod", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2095. { "mod", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2096. { "mod", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2097. { "mod", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2098. { "mod", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2099. { "mod", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2100. // modf
  2101. { "modf", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "i" }, TAG_GLOBAL, false },
  2102. { "modf", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "x", "i" }, TAG_GLOBAL, false },
  2103. { "modf", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "x", "i" }, TAG_GLOBAL, false },
  2104. { "modf", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "x", "i" }, TAG_GLOBAL, false },
  2105. // min
  2106. { "min", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2107. { "min", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2108. { "min", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2109. { "min", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2110. { "min", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2111. { "min", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2112. { "min", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2113. { "min", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2114. { "min", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2115. { "min", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2116. { "min", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2117. { "min", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2118. { "min", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2119. { "min", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2120. { "min", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2121. { "min", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2122. { "min", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2123. { "min", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2124. { "min", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2125. { "min", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2126. { "min", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2127. // max
  2128. { "max", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2129. { "max", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2130. { "max", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2131. { "max", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2132. { "max", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2133. { "max", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2134. { "max", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2135. { "max", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2136. { "max", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2137. { "max", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2138. { "max", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2139. { "max", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2140. { "max", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2141. { "max", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2142. { "max", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2143. { "max", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2144. { "max", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2145. { "max", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2146. { "max", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2147. { "max", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2148. { "max", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2149. // clamp
  2150. { "clamp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2151. { "clamp", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2152. { "clamp", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2153. { "clamp", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2154. { "clamp", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2155. { "clamp", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2156. { "clamp", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2157. { "clamp", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2158. { "clamp", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2159. { "clamp", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2160. { "clamp", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2161. { "clamp", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2162. { "clamp", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2163. { "clamp", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2164. { "clamp", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2165. { "clamp", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2166. { "clamp", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2167. { "clamp", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2168. { "clamp", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2169. { "clamp", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2170. { "clamp", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2171. // mix
  2172. { "mix", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2173. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2174. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_BVEC2, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2175. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2176. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2177. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_BVEC3, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2178. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2179. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2180. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_BVEC4, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2181. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2182. // step
  2183. { "step", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2184. { "step", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2185. { "step", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2186. { "step", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2187. { "step", TYPE_VEC2, { TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2188. { "step", TYPE_VEC3, { TYPE_FLOAT, TYPE_VEC3, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2189. { "step", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC4, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2190. // smoothstep
  2191. { "smoothstep", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2192. { "smoothstep", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2193. { "smoothstep", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2194. { "smoothstep", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2195. { "smoothstep", TYPE_VEC2, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2196. { "smoothstep", TYPE_VEC3, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC3, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2197. { "smoothstep", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC4, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2198. // isnan
  2199. { "isnan", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2200. { "isnan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2201. { "isnan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2202. { "isnan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2203. // isinf
  2204. { "isinf", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2205. { "isinf", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2206. { "isinf", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2207. { "isinf", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2208. // floatBitsToInt
  2209. { "floatBitsToInt", TYPE_INT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2210. { "floatBitsToInt", TYPE_IVEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2211. { "floatBitsToInt", TYPE_IVEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2212. { "floatBitsToInt", TYPE_IVEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2213. // floatBitsToUint
  2214. { "floatBitsToUint", TYPE_UINT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2215. { "floatBitsToUint", TYPE_UVEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2216. { "floatBitsToUint", TYPE_UVEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2217. { "floatBitsToUint", TYPE_UVEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2218. // intBitsToFloat
  2219. { "intBitsToFloat", TYPE_FLOAT, { TYPE_INT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2220. { "intBitsToFloat", TYPE_VEC2, { TYPE_IVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2221. { "intBitsToFloat", TYPE_VEC3, { TYPE_IVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2222. { "intBitsToFloat", TYPE_VEC4, { TYPE_IVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2223. // uintBitsToFloat
  2224. { "uintBitsToFloat", TYPE_FLOAT, { TYPE_UINT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2225. { "uintBitsToFloat", TYPE_VEC2, { TYPE_UVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2226. { "uintBitsToFloat", TYPE_VEC3, { TYPE_UVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2227. { "uintBitsToFloat", TYPE_VEC4, { TYPE_UVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2228. // Built-ins - geometric functions.
  2229. // length
  2230. { "length", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2231. { "length", TYPE_FLOAT, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2232. { "length", TYPE_FLOAT, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2233. { "length", TYPE_FLOAT, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2234. // distance
  2235. { "distance", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2236. { "distance", TYPE_FLOAT, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2237. { "distance", TYPE_FLOAT, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2238. { "distance", TYPE_FLOAT, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2239. // dot
  2240. { "dot", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2241. { "dot", TYPE_FLOAT, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2242. { "dot", TYPE_FLOAT, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2243. { "dot", TYPE_FLOAT, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2244. // cross
  2245. { "cross", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2246. // normalize
  2247. { "normalize", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2248. { "normalize", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2249. { "normalize", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2250. { "normalize", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2251. // reflect
  2252. { "reflect", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "I", "N" }, TAG_GLOBAL, false },
  2253. { "reflect", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "I", "N" }, TAG_GLOBAL, false },
  2254. { "reflect", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "I", "N" }, TAG_GLOBAL, false },
  2255. // refract
  2256. { "refract", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "I", "N", "eta" }, TAG_GLOBAL, false },
  2257. { "refract", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "I", "N", "eta" }, TAG_GLOBAL, false },
  2258. { "refract", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "I", "N", "eta" }, TAG_GLOBAL, false },
  2259. // faceforward
  2260. { "faceforward", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "N", "I", "Nref" }, TAG_GLOBAL, false },
  2261. { "faceforward", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "N", "I", "Nref" }, TAG_GLOBAL, false },
  2262. { "faceforward", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "N", "I", "Nref" }, TAG_GLOBAL, false },
  2263. // matrixCompMult
  2264. { "matrixCompMult", TYPE_MAT2, { TYPE_MAT2, TYPE_MAT2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2265. { "matrixCompMult", TYPE_MAT3, { TYPE_MAT3, TYPE_MAT3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2266. { "matrixCompMult", TYPE_MAT4, { TYPE_MAT4, TYPE_MAT4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2267. // outerProduct
  2268. { "outerProduct", TYPE_MAT2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "c", "r" }, TAG_GLOBAL, false },
  2269. { "outerProduct", TYPE_MAT3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "c", "r" }, TAG_GLOBAL, false },
  2270. { "outerProduct", TYPE_MAT4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "c", "r" }, TAG_GLOBAL, false },
  2271. // transpose
  2272. { "transpose", TYPE_MAT2, { TYPE_MAT2, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2273. { "transpose", TYPE_MAT3, { TYPE_MAT3, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2274. { "transpose", TYPE_MAT4, { TYPE_MAT4, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2275. // determinant
  2276. { "determinant", TYPE_FLOAT, { TYPE_MAT2, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2277. { "determinant", TYPE_FLOAT, { TYPE_MAT3, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2278. { "determinant", TYPE_FLOAT, { TYPE_MAT4, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2279. // inverse
  2280. { "inverse", TYPE_MAT2, { TYPE_MAT2, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2281. { "inverse", TYPE_MAT3, { TYPE_MAT3, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2282. { "inverse", TYPE_MAT4, { TYPE_MAT4, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2283. // lessThan
  2284. { "lessThan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2285. { "lessThan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2286. { "lessThan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2287. { "lessThan", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2288. { "lessThan", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2289. { "lessThan", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2290. { "lessThan", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2291. { "lessThan", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2292. { "lessThan", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2293. // greaterThan
  2294. { "greaterThan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2295. { "greaterThan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2296. { "greaterThan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2297. { "greaterThan", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2298. { "greaterThan", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2299. { "greaterThan", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2300. { "greaterThan", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2301. { "greaterThan", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2302. { "greaterThan", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2303. // lessThanEqual
  2304. { "lessThanEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2305. { "lessThanEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2306. { "lessThanEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2307. { "lessThanEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2308. { "lessThanEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2309. { "lessThanEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2310. { "lessThanEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2311. { "lessThanEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2312. { "lessThanEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2313. // greaterThanEqual
  2314. { "greaterThanEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2315. { "greaterThanEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2316. { "greaterThanEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2317. { "greaterThanEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2318. { "greaterThanEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2319. { "greaterThanEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2320. { "greaterThanEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2321. { "greaterThanEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2322. { "greaterThanEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2323. // equal
  2324. { "equal", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2325. { "equal", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2326. { "equal", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2327. { "equal", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2328. { "equal", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2329. { "equal", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2330. { "equal", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2331. { "equal", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2332. { "equal", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2333. { "equal", TYPE_BVEC2, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2334. { "equal", TYPE_BVEC3, { TYPE_BVEC3, TYPE_BVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2335. { "equal", TYPE_BVEC4, { TYPE_BVEC4, TYPE_BVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2336. // notEqual
  2337. { "notEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2338. { "notEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2339. { "notEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2340. { "notEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2341. { "notEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2342. { "notEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2343. { "notEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2344. { "notEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2345. { "notEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2346. { "notEqual", TYPE_BVEC2, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2347. { "notEqual", TYPE_BVEC3, { TYPE_BVEC3, TYPE_BVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2348. { "notEqual", TYPE_BVEC4, { TYPE_BVEC4, TYPE_BVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2349. // any
  2350. { "any", TYPE_BOOL, { TYPE_BVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2351. { "any", TYPE_BOOL, { TYPE_BVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2352. { "any", TYPE_BOOL, { TYPE_BVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2353. // all
  2354. { "all", TYPE_BOOL, { TYPE_BVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2355. { "all", TYPE_BOOL, { TYPE_BVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2356. { "all", TYPE_BOOL, { TYPE_BVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2357. // not
  2358. { "not", TYPE_BVEC2, { TYPE_BVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2359. { "not", TYPE_BVEC3, { TYPE_BVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2360. { "not", TYPE_BVEC4, { TYPE_BVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2361. // Built-ins: texture functions.
  2362. // textureSize
  2363. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLER2D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2364. { "textureSize", TYPE_IVEC2, { TYPE_ISAMPLER2D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2365. { "textureSize", TYPE_IVEC2, { TYPE_USAMPLER2D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2366. { "textureSize", TYPE_IVEC3, { TYPE_SAMPLER2DARRAY, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2367. { "textureSize", TYPE_IVEC3, { TYPE_ISAMPLER2DARRAY, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2368. { "textureSize", TYPE_IVEC3, { TYPE_USAMPLER2DARRAY, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2369. { "textureSize", TYPE_IVEC3, { TYPE_SAMPLER3D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2370. { "textureSize", TYPE_IVEC3, { TYPE_ISAMPLER3D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2371. { "textureSize", TYPE_IVEC3, { TYPE_USAMPLER3D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2372. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLERCUBE, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2373. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLERCUBEARRAY, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2374. // texture
  2375. { "texture", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2376. { "texture", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2377. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2378. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2379. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2380. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2381. { "texture", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2382. { "texture", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2383. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2384. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2385. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2386. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2387. { "texture", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2388. { "texture", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2389. { "texture", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2390. { "texture", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2391. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2392. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2393. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2394. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2395. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBEARRAY, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2396. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBEARRAY, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2397. // textureProj
  2398. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2399. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2400. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2401. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2402. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2403. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2404. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2405. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2406. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2407. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2408. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2409. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2410. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2411. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2412. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2413. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2414. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2415. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2416. // textureLod
  2417. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2418. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2419. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2420. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2421. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2422. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2423. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2424. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2425. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2426. { "textureLod", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2427. { "textureLod", TYPE_VEC4, { TYPE_SAMPLERCUBEARRAY, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2428. // texelFetch
  2429. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2430. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2431. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2432. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2433. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2434. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2435. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2436. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2437. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2438. // textureProjLod
  2439. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2440. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2441. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2442. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2443. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2444. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2445. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2446. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2447. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2448. // textureGrad
  2449. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2450. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2451. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2452. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2453. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2454. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2455. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2456. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2457. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2458. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2459. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLERCUBEARRAY, TYPE_VEC4, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2460. // textureProjGrad
  2461. { "textureProjGrad", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2462. { "textureProjGrad", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2463. { "textureProjGrad", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2464. { "textureProjGrad", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2465. { "textureProjGrad", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2466. { "textureProjGrad", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2467. { "textureProjGrad", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2468. { "textureProjGrad", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2469. { "textureProjGrad", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2470. // textureGather
  2471. { "textureGather", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2472. { "textureGather", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2473. { "textureGather", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2474. { "textureGather", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2475. { "textureGather", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2476. { "textureGather", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2477. { "textureGather", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2478. { "textureGather", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2479. { "textureGather", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2480. { "textureGather", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2481. { "textureGather", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2482. { "textureGather", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2483. { "textureGather", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2484. { "textureGather", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2485. // textureQueryLod
  2486. { "textureQueryLod", TYPE_VEC2, { TYPE_SAMPLER2D, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2487. { "textureQueryLod", TYPE_VEC2, { TYPE_ISAMPLER2D, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2488. { "textureQueryLod", TYPE_VEC2, { TYPE_USAMPLER2D, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2489. { "textureQueryLod", TYPE_VEC2, { TYPE_SAMPLER2DARRAY, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2490. { "textureQueryLod", TYPE_VEC2, { TYPE_ISAMPLER2DARRAY, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2491. { "textureQueryLod", TYPE_VEC2, { TYPE_USAMPLER2DARRAY, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2492. { "textureQueryLod", TYPE_VEC2, { TYPE_SAMPLER3D, TYPE_VEC3 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2493. { "textureQueryLod", TYPE_VEC2, { TYPE_ISAMPLER3D, TYPE_VEC3 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2494. { "textureQueryLod", TYPE_VEC2, { TYPE_USAMPLER3D, TYPE_VEC3 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2495. { "textureQueryLod", TYPE_VEC2, { TYPE_SAMPLERCUBE, TYPE_VEC3 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2496. // textureQueryLevels
  2497. { "textureQueryLevels", TYPE_INT, { TYPE_SAMPLER2D }, { "sampler" }, TAG_GLOBAL, true },
  2498. { "textureQueryLevels", TYPE_INT, { TYPE_ISAMPLER2D }, { "sampler" }, TAG_GLOBAL, true },
  2499. { "textureQueryLevels", TYPE_INT, { TYPE_USAMPLER2D }, { "sampler" }, TAG_GLOBAL, true },
  2500. { "textureQueryLevels", TYPE_INT, { TYPE_SAMPLER2DARRAY }, { "sampler" }, TAG_GLOBAL, true },
  2501. { "textureQueryLevels", TYPE_INT, { TYPE_ISAMPLER2DARRAY }, { "sampler" }, TAG_GLOBAL, true },
  2502. { "textureQueryLevels", TYPE_INT, { TYPE_USAMPLER2DARRAY }, { "sampler" }, TAG_GLOBAL, true },
  2503. { "textureQueryLevels", TYPE_INT, { TYPE_SAMPLER3D }, { "sampler" }, TAG_GLOBAL, true },
  2504. { "textureQueryLevels", TYPE_INT, { TYPE_ISAMPLER3D }, { "sampler" }, TAG_GLOBAL, true },
  2505. { "textureQueryLevels", TYPE_INT, { TYPE_USAMPLER3D }, { "sampler" }, TAG_GLOBAL, true },
  2506. { "textureQueryLevels", TYPE_INT, { TYPE_SAMPLERCUBE }, { "sampler" }, TAG_GLOBAL, true },
  2507. // dFdx
  2508. { "dFdx", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2509. { "dFdx", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2510. { "dFdx", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2511. { "dFdx", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2512. // dFdxCoarse
  2513. { "dFdxCoarse", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2514. { "dFdxCoarse", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2515. { "dFdxCoarse", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2516. { "dFdxCoarse", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2517. // dFdxFine
  2518. { "dFdxFine", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2519. { "dFdxFine", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2520. { "dFdxFine", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2521. { "dFdxFine", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2522. // dFdy
  2523. { "dFdy", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2524. { "dFdy", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2525. { "dFdy", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2526. { "dFdy", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2527. // dFdyCoarse
  2528. { "dFdyCoarse", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2529. { "dFdyCoarse", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2530. { "dFdyCoarse", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2531. { "dFdyCoarse", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2532. // dFdyFine
  2533. { "dFdyFine", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2534. { "dFdyFine", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2535. { "dFdyFine", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2536. { "dFdyFine", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2537. // fwidth
  2538. { "fwidth", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2539. { "fwidth", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2540. { "fwidth", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2541. { "fwidth", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2542. // fwidthCoarse
  2543. { "fwidthCoarse", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2544. { "fwidthCoarse", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2545. { "fwidthCoarse", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2546. { "fwidthCoarse", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2547. // fwidthFine
  2548. { "fwidthFine", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2549. { "fwidthFine", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2550. { "fwidthFine", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2551. { "fwidthFine", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2552. // Sub-functions.
  2553. // array
  2554. { "length", TYPE_INT, { TYPE_VOID }, { "" }, TAG_ARRAY, false },
  2555. // Modern functions.
  2556. // fma
  2557. { "fma", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "c" }, TAG_GLOBAL, true },
  2558. { "fma", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b", "c" }, TAG_GLOBAL, true },
  2559. { "fma", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b", "c" }, TAG_GLOBAL, true },
  2560. { "fma", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b", "c" }, TAG_GLOBAL, true },
  2561. // Packing/Unpacking functions.
  2562. { "packHalf2x16", TYPE_UINT, { TYPE_VEC2, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2563. { "packUnorm2x16", TYPE_UINT, { TYPE_VEC2, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2564. { "packSnorm2x16", TYPE_UINT, { TYPE_VEC2, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2565. { "packUnorm4x8", TYPE_UINT, { TYPE_VEC4, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2566. { "packSnorm4x8", TYPE_UINT, { TYPE_VEC4, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2567. { "unpackHalf2x16", TYPE_VEC2, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2568. { "unpackUnorm2x16", TYPE_VEC2, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2569. { "unpackSnorm2x16", TYPE_VEC2, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2570. { "unpackUnorm4x8", TYPE_VEC4, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2571. { "unpackSnorm4x8", TYPE_VEC4, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2572. // bitfieldExtract
  2573. { "bitfieldExtract", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2574. { "bitfieldExtract", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2575. { "bitfieldExtract", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2576. { "bitfieldExtract", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2577. { "bitfieldExtract", TYPE_UINT, { TYPE_UINT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2578. { "bitfieldExtract", TYPE_UVEC2, { TYPE_UVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2579. { "bitfieldExtract", TYPE_UVEC3, { TYPE_UVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2580. { "bitfieldExtract", TYPE_UVEC4, { TYPE_UVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2581. // bitfieldInsert
  2582. { "bitfieldInsert", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2583. { "bitfieldInsert", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2584. { "bitfieldInsert", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2585. { "bitfieldInsert", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2586. { "bitfieldInsert", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2587. { "bitfieldInsert", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2588. { "bitfieldInsert", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2589. { "bitfieldInsert", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2590. // bitfieldReverse
  2591. { "bitfieldReverse", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2592. { "bitfieldReverse", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2593. { "bitfieldReverse", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2594. { "bitfieldReverse", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2595. { "bitfieldReverse", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2596. { "bitfieldReverse", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2597. { "bitfieldReverse", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2598. { "bitfieldReverse", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2599. // bitCount
  2600. { "bitCount", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2601. { "bitCount", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2602. { "bitCount", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2603. { "bitCount", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2604. { "bitCount", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2605. { "bitCount", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2606. { "bitCount", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2607. { "bitCount", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2608. // findLSB
  2609. { "findLSB", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2610. { "findLSB", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2611. { "findLSB", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2612. { "findLSB", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2613. { "findLSB", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2614. { "findLSB", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2615. { "findLSB", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2616. { "findLSB", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2617. // findMSB
  2618. { "findMSB", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2619. { "findMSB", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2620. { "findMSB", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2621. { "findMSB", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2622. { "findMSB", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2623. { "findMSB", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2624. { "findMSB", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2625. { "findMSB", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2626. // umulExtended
  2627. { "umulExtended", TYPE_VOID, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2628. { "umulExtended", TYPE_VOID, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2629. { "umulExtended", TYPE_VOID, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2630. { "umulExtended", TYPE_VOID, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2631. // imulExtended
  2632. { "imulExtended", TYPE_VOID, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2633. { "imulExtended", TYPE_VOID, { TYPE_IVEC2, TYPE_IVEC2, TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2634. { "imulExtended", TYPE_VOID, { TYPE_IVEC3, TYPE_IVEC3, TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2635. { "imulExtended", TYPE_VOID, { TYPE_IVEC4, TYPE_IVEC4, TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2636. // uaddCarry
  2637. { "uaddCarry", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "y", "carry" }, TAG_GLOBAL, true },
  2638. { "uaddCarry", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "x", "y", "carry" }, TAG_GLOBAL, true },
  2639. { "uaddCarry", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "x", "y", "carry" }, TAG_GLOBAL, true },
  2640. { "uaddCarry", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "x", "y", "carry" }, TAG_GLOBAL, true },
  2641. // usubBorrow
  2642. { "usubBorrow", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "y", "borrow" }, TAG_GLOBAL, true },
  2643. { "usubBorrow", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "x", "y", "borrow" }, TAG_GLOBAL, true },
  2644. { "usubBorrow", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "x", "y", "borrow" }, TAG_GLOBAL, true },
  2645. { "usubBorrow", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "x", "y", "borrow" }, TAG_GLOBAL, true },
  2646. // ldexp
  2647. { "ldexp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_INT, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2648. { "ldexp", TYPE_VEC2, { TYPE_VEC2, TYPE_IVEC2, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2649. { "ldexp", TYPE_VEC3, { TYPE_VEC3, TYPE_IVEC3, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2650. { "ldexp", TYPE_VEC4, { TYPE_VEC4, TYPE_IVEC4, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2651. // frexp
  2652. { "frexp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_INT, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2653. { "frexp", TYPE_VEC2, { TYPE_VEC2, TYPE_IVEC2, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2654. { "frexp", TYPE_VEC3, { TYPE_VEC3, TYPE_IVEC3, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2655. { "frexp", TYPE_VEC4, { TYPE_VEC4, TYPE_IVEC4, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2656. { nullptr, TYPE_VOID, { TYPE_VOID }, { "" }, TAG_GLOBAL, false }
  2657. };
  2658. HashSet<StringName> global_func_set;
  2659. const ShaderLanguage::BuiltinFuncOutArgs ShaderLanguage::builtin_func_out_args[] = {
  2660. { "modf", { 1, -1 } },
  2661. { "umulExtended", { 2, 3 } },
  2662. { "imulExtended", { 2, 3 } },
  2663. { "uaddCarry", { 2, -1 } },
  2664. { "usubBorrow", { 2, -1 } },
  2665. { "ldexp", { 1, -1 } },
  2666. { "frexp", { 1, -1 } },
  2667. { nullptr, { 0, -1 } }
  2668. };
  2669. const ShaderLanguage::BuiltinFuncConstArgs ShaderLanguage::builtin_func_const_args[] = {
  2670. { "textureGather", 2, 0, 3 },
  2671. { nullptr, 0, 0, 0 }
  2672. };
  2673. const ShaderLanguage::BuiltinEntry ShaderLanguage::frag_only_func_defs[] = {
  2674. { "dFdx" },
  2675. { "dFdxCoarse" },
  2676. { "dFdxFine" },
  2677. { "dFdy" },
  2678. { "dFdyCoarse" },
  2679. { "dFdyFine" },
  2680. { "fwidth" },
  2681. { "fwidthCoarse" },
  2682. { "fwidthFine" },
  2683. { nullptr }
  2684. };
  2685. bool ShaderLanguage::is_const_suffix_lut_initialized = false;
  2686. 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, bool *r_is_custom_function) {
  2687. ERR_FAIL_COND_V(p_func->op != OP_CALL && p_func->op != OP_CONSTRUCT, false);
  2688. Vector<DataType> args;
  2689. Vector<StringName> args2;
  2690. Vector<int> args3;
  2691. ERR_FAIL_COND_V(p_func->arguments[0]->type != Node::NODE_TYPE_VARIABLE, false);
  2692. StringName name = static_cast<VariableNode *>(p_func->arguments[0])->name.operator String();
  2693. for (int i = 1; i < p_func->arguments.size(); i++) {
  2694. args.push_back(p_func->arguments[i]->get_datatype());
  2695. args2.push_back(p_func->arguments[i]->get_datatype_name());
  2696. args3.push_back(p_func->arguments[i]->get_array_size());
  2697. }
  2698. int argcount = args.size();
  2699. if (p_function_info.stage_functions.has(name)) {
  2700. //stage based function
  2701. const StageFunctionInfo &sf = p_function_info.stage_functions[name];
  2702. if (argcount != sf.arguments.size()) {
  2703. _set_error(vformat(RTR("Invalid number of arguments when calling stage function '%s', which expects %d arguments."), String(name), sf.arguments.size()));
  2704. return false;
  2705. }
  2706. //validate arguments
  2707. for (int i = 0; i < argcount; i++) {
  2708. if (args[i] != sf.arguments[i].type) {
  2709. _set_error(vformat(RTR("Invalid argument type when calling stage function '%s', type expected is '%s'."), String(name), get_datatype_name(sf.arguments[i].type)));
  2710. return false;
  2711. }
  2712. }
  2713. if (r_ret_type) {
  2714. *r_ret_type = sf.return_type;
  2715. }
  2716. if (r_ret_type_str) {
  2717. *r_ret_type_str = "";
  2718. }
  2719. return true;
  2720. }
  2721. bool failed_builtin = false;
  2722. bool unsupported_builtin = false;
  2723. int builtin_idx = 0;
  2724. if (argcount <= 4) {
  2725. // test builtins
  2726. int idx = 0;
  2727. while (builtin_func_defs[idx].name) {
  2728. if (completion_class != builtin_func_defs[idx].tag) {
  2729. idx++;
  2730. continue;
  2731. }
  2732. if (name == builtin_func_defs[idx].name) {
  2733. failed_builtin = true;
  2734. bool fail = false;
  2735. for (int i = 0; i < argcount; i++) {
  2736. if (p_func->arguments[i + 1]->type == Node::NODE_TYPE_ARRAY) {
  2737. const ArrayNode *anode = static_cast<const ArrayNode *>(p_func->arguments[i + 1]);
  2738. if (anode->call_expression == nullptr && !anode->is_indexed()) {
  2739. fail = true;
  2740. break;
  2741. }
  2742. }
  2743. if (get_scalar_type(args[i]) == args[i] && p_func->arguments[i + 1]->type == Node::NODE_TYPE_CONSTANT && convert_constant(static_cast<ConstantNode *>(p_func->arguments[i + 1]), builtin_func_defs[idx].args[i])) {
  2744. //all good, but needs implicit conversion later
  2745. } else if (args[i] != builtin_func_defs[idx].args[i]) {
  2746. fail = true;
  2747. break;
  2748. }
  2749. }
  2750. if (!fail) {
  2751. if (RenderingServer::get_singleton()->is_low_end()) {
  2752. if (builtin_func_defs[idx].high_end) {
  2753. fail = true;
  2754. unsupported_builtin = true;
  2755. builtin_idx = idx;
  2756. }
  2757. }
  2758. }
  2759. if (!fail && argcount < 4 && builtin_func_defs[idx].args[argcount] != TYPE_VOID) {
  2760. fail = true; //make sure the number of arguments matches
  2761. }
  2762. if (!fail) {
  2763. {
  2764. int constarg_idx = 0;
  2765. while (builtin_func_const_args[constarg_idx].name) {
  2766. if (String(name) == builtin_func_const_args[constarg_idx].name) {
  2767. int arg = builtin_func_const_args[constarg_idx].arg + 1;
  2768. if (p_func->arguments.size() <= arg) {
  2769. break;
  2770. }
  2771. int min = builtin_func_const_args[constarg_idx].min;
  2772. int max = builtin_func_const_args[constarg_idx].max;
  2773. bool error = false;
  2774. if (p_func->arguments[arg]->type == Node::NODE_TYPE_VARIABLE) {
  2775. const VariableNode *vn = static_cast<VariableNode *>(p_func->arguments[arg]);
  2776. bool is_const = false;
  2777. ConstantNode::Value value;
  2778. value.sint = -1;
  2779. _find_identifier(p_block, false, p_function_info, vn->name, nullptr, nullptr, &is_const, nullptr, nullptr, &value);
  2780. if (!is_const || value.sint < min || value.sint > max) {
  2781. error = true;
  2782. }
  2783. } else {
  2784. if (p_func->arguments[arg]->type == Node::NODE_TYPE_CONSTANT) {
  2785. const ConstantNode *cn = static_cast<ConstantNode *>(p_func->arguments[arg]);
  2786. if (cn->get_datatype() == TYPE_INT && cn->values.size() == 1) {
  2787. int value = cn->values[0].sint;
  2788. if (value < min || value > max) {
  2789. error = true;
  2790. }
  2791. } else {
  2792. error = true;
  2793. }
  2794. } else {
  2795. error = true;
  2796. }
  2797. }
  2798. if (error) {
  2799. _set_error(vformat(RTR("Expected integer constant within [%d..%d] range."), min, max));
  2800. return false;
  2801. }
  2802. }
  2803. constarg_idx++;
  2804. }
  2805. }
  2806. //make sure its not an out argument used in the wrong way
  2807. int outarg_idx = 0;
  2808. while (builtin_func_out_args[outarg_idx].name) {
  2809. if (String(name) == builtin_func_out_args[outarg_idx].name) {
  2810. for (int arg = 0; arg < BuiltinFuncOutArgs::MAX_ARGS; arg++) {
  2811. int arg_idx = builtin_func_out_args[outarg_idx].arguments[arg];
  2812. if (arg_idx == -1) {
  2813. break;
  2814. }
  2815. if (arg_idx < argcount) {
  2816. if (p_func->arguments[arg_idx + 1]->type != Node::NODE_TYPE_VARIABLE && p_func->arguments[arg_idx + 1]->type != Node::NODE_TYPE_MEMBER && p_func->arguments[arg_idx + 1]->type != Node::NODE_TYPE_ARRAY) {
  2817. _set_error(vformat(RTR("Argument %d of function '%s' is not a variable, array, or member."), arg_idx + 1, String(name)));
  2818. return false;
  2819. }
  2820. if (p_func->arguments[arg_idx + 1]->type == Node::NODE_TYPE_ARRAY) {
  2821. ArrayNode *mn = static_cast<ArrayNode *>(p_func->arguments[arg_idx + 1]);
  2822. if (mn->is_const) {
  2823. fail = true;
  2824. }
  2825. } else if (p_func->arguments[arg_idx + 1]->type == Node::NODE_TYPE_MEMBER) {
  2826. MemberNode *mn = static_cast<MemberNode *>(p_func->arguments[arg_idx + 1]);
  2827. if (mn->basetype_const) {
  2828. fail = true;
  2829. }
  2830. } else { // TYPE_VARIABLE
  2831. VariableNode *vn = static_cast<VariableNode *>(p_func->arguments[arg_idx + 1]);
  2832. if (vn->is_const) {
  2833. fail = true;
  2834. } else {
  2835. StringName varname = vn->name;
  2836. if (shader->uniforms.has(varname)) {
  2837. fail = true;
  2838. } else {
  2839. if (shader->varyings.has(varname)) {
  2840. _set_error(vformat(RTR("Varyings cannot be passed for the '%s' parameter."), "out"));
  2841. return false;
  2842. }
  2843. if (p_function_info.built_ins.has(varname)) {
  2844. BuiltInInfo info = p_function_info.built_ins[varname];
  2845. if (info.constant) {
  2846. fail = true;
  2847. }
  2848. }
  2849. }
  2850. }
  2851. }
  2852. if (fail) {
  2853. _set_error(vformat(RTR("A constant value cannot be passed for the '%s' parameter."), "out"));
  2854. return false;
  2855. }
  2856. StringName var_name;
  2857. if (p_func->arguments[arg_idx + 1]->type == Node::NODE_TYPE_ARRAY) {
  2858. var_name = static_cast<const ArrayNode *>(p_func->arguments[arg_idx + 1])->name;
  2859. } else if (p_func->arguments[arg_idx + 1]->type == Node::NODE_TYPE_MEMBER) {
  2860. Node *n = static_cast<const MemberNode *>(p_func->arguments[arg_idx + 1])->owner;
  2861. while (n->type == Node::NODE_TYPE_MEMBER) {
  2862. n = static_cast<const MemberNode *>(n)->owner;
  2863. }
  2864. if (n->type != Node::NODE_TYPE_VARIABLE && n->type != Node::NODE_TYPE_ARRAY) {
  2865. _set_error(vformat(RTR("Argument %d of function '%s' is not a variable, array, or member."), arg_idx + 1, String(name)));
  2866. return false;
  2867. }
  2868. if (n->type == Node::NODE_TYPE_VARIABLE) {
  2869. var_name = static_cast<const VariableNode *>(n)->name;
  2870. } else { // TYPE_ARRAY
  2871. var_name = static_cast<const ArrayNode *>(n)->name;
  2872. }
  2873. } else { // TYPE_VARIABLE
  2874. var_name = static_cast<const VariableNode *>(p_func->arguments[arg_idx + 1])->name;
  2875. }
  2876. const BlockNode *b = p_block;
  2877. bool valid = false;
  2878. while (b) {
  2879. if (b->variables.has(var_name) || p_function_info.built_ins.has(var_name)) {
  2880. valid = true;
  2881. break;
  2882. }
  2883. if (b->parent_function) {
  2884. for (int i = 0; i < b->parent_function->arguments.size(); i++) {
  2885. if (b->parent_function->arguments[i].name == var_name) {
  2886. valid = true;
  2887. break;
  2888. }
  2889. }
  2890. }
  2891. b = b->parent_block;
  2892. }
  2893. if (!valid) {
  2894. _set_error(vformat(RTR("Argument %d of function '%s' can only take a local variable, array, or member."), arg_idx + 1, String(name)));
  2895. return false;
  2896. }
  2897. }
  2898. }
  2899. }
  2900. outarg_idx++;
  2901. }
  2902. //implicitly convert values if possible
  2903. for (int i = 0; i < argcount; i++) {
  2904. if (get_scalar_type(args[i]) != args[i] || args[i] == builtin_func_defs[idx].args[i] || p_func->arguments[i + 1]->type != Node::NODE_TYPE_CONSTANT) {
  2905. //can't do implicit conversion here
  2906. continue;
  2907. }
  2908. //this is an implicit conversion
  2909. ConstantNode *constant = static_cast<ConstantNode *>(p_func->arguments[i + 1]);
  2910. ConstantNode *conversion = alloc_node<ConstantNode>();
  2911. conversion->datatype = builtin_func_defs[idx].args[i];
  2912. conversion->values.resize(1);
  2913. convert_constant(constant, builtin_func_defs[idx].args[i], conversion->values.ptrw());
  2914. p_func->arguments.write[i + 1] = conversion;
  2915. }
  2916. if (r_ret_type) {
  2917. *r_ret_type = builtin_func_defs[idx].rettype;
  2918. }
  2919. return true;
  2920. }
  2921. }
  2922. idx++;
  2923. }
  2924. }
  2925. if (unsupported_builtin) {
  2926. String arglist = "";
  2927. for (int i = 0; i < argcount; i++) {
  2928. if (i > 0) {
  2929. arglist += ", ";
  2930. }
  2931. arglist += get_datatype_name(builtin_func_defs[builtin_idx].args[i]);
  2932. }
  2933. _set_error(vformat(RTR("Built-in function \"%s(%s)\" is only supported on high-end platforms."), String(name), arglist));
  2934. return false;
  2935. }
  2936. if (failed_builtin) {
  2937. String arg_list;
  2938. for (int i = 0; i < argcount; i++) {
  2939. if (i > 0) {
  2940. arg_list += ",";
  2941. }
  2942. String arg_name;
  2943. if (args[i] == TYPE_STRUCT) {
  2944. arg_name = args2[i];
  2945. } else {
  2946. arg_name = get_datatype_name(args[i]);
  2947. }
  2948. if (args3[i] > 0) {
  2949. arg_name += "[";
  2950. arg_name += itos(args3[i]);
  2951. arg_name += "]";
  2952. }
  2953. arg_list += arg_name;
  2954. }
  2955. _set_error(vformat(RTR("Invalid arguments for the built-in function: \"%s(%s)\"."), String(name), arg_list));
  2956. return false;
  2957. }
  2958. // try existing functions..
  2959. StringName exclude_function;
  2960. BlockNode *block = p_block;
  2961. while (block) {
  2962. if (block->parent_function) {
  2963. exclude_function = block->parent_function->name;
  2964. }
  2965. block = block->parent_block;
  2966. }
  2967. if (name == exclude_function) {
  2968. _set_error(RTR("Recursion is not allowed."));
  2969. return false;
  2970. }
  2971. int last_arg_count = 0;
  2972. bool exists = false;
  2973. String arg_list = "";
  2974. for (int i = 0; i < shader->vfunctions.size(); i++) {
  2975. if (name != shader->vfunctions[i].name) {
  2976. continue;
  2977. }
  2978. exists = true;
  2979. if (!shader->vfunctions[i].callable) {
  2980. _set_error(vformat(RTR("Function '%s' can't be called from source code."), String(name)));
  2981. return false;
  2982. }
  2983. FunctionNode *pfunc = shader->vfunctions[i].function;
  2984. if (arg_list.is_empty()) {
  2985. for (int j = 0; j < pfunc->arguments.size(); j++) {
  2986. if (j > 0) {
  2987. arg_list += ", ";
  2988. }
  2989. String func_arg_name;
  2990. if (pfunc->arguments[j].type == TYPE_STRUCT) {
  2991. func_arg_name = pfunc->arguments[j].struct_name;
  2992. } else {
  2993. func_arg_name = get_datatype_name(pfunc->arguments[j].type);
  2994. }
  2995. if (pfunc->arguments[j].array_size > 0) {
  2996. func_arg_name += "[";
  2997. func_arg_name += itos(pfunc->arguments[j].array_size);
  2998. func_arg_name += "]";
  2999. }
  3000. arg_list += func_arg_name;
  3001. }
  3002. }
  3003. if (pfunc->arguments.size() != args.size()) {
  3004. last_arg_count = pfunc->arguments.size();
  3005. continue;
  3006. }
  3007. bool fail = false;
  3008. for (int j = 0; j < args.size(); j++) {
  3009. if (get_scalar_type(args[j]) == args[j] && p_func->arguments[j + 1]->type == Node::NODE_TYPE_CONSTANT && args3[j] == 0 && convert_constant(static_cast<ConstantNode *>(p_func->arguments[j + 1]), pfunc->arguments[j].type)) {
  3010. //all good, but it needs implicit conversion later
  3011. } else if (args[j] != pfunc->arguments[j].type || (args[j] == TYPE_STRUCT && args2[j] != pfunc->arguments[j].struct_name) || args3[j] != pfunc->arguments[j].array_size) {
  3012. String func_arg_name;
  3013. if (pfunc->arguments[j].type == TYPE_STRUCT) {
  3014. func_arg_name = pfunc->arguments[j].struct_name;
  3015. } else {
  3016. func_arg_name = get_datatype_name(pfunc->arguments[j].type);
  3017. }
  3018. if (pfunc->arguments[j].array_size > 0) {
  3019. func_arg_name += "[";
  3020. func_arg_name += itos(pfunc->arguments[j].array_size);
  3021. func_arg_name += "]";
  3022. }
  3023. String arg_name;
  3024. if (args[j] == TYPE_STRUCT) {
  3025. arg_name = args2[j];
  3026. } else {
  3027. arg_name = get_datatype_name(args[j]);
  3028. }
  3029. if (args3[j] > 0) {
  3030. arg_name += "[";
  3031. arg_name += itos(args3[j]);
  3032. arg_name += "]";
  3033. }
  3034. _set_error(vformat(RTR("Invalid argument for \"%s(%s)\" function: argument %d should be %s but is %s."), String(name), arg_list, j + 1, func_arg_name, arg_name));
  3035. fail = true;
  3036. break;
  3037. }
  3038. }
  3039. if (!fail) {
  3040. //implicitly convert values if possible
  3041. for (int k = 0; k < args.size(); k++) {
  3042. if (get_scalar_type(args[k]) != args[k] || args[k] == pfunc->arguments[k].type || p_func->arguments[k + 1]->type != Node::NODE_TYPE_CONSTANT) {
  3043. //can't do implicit conversion here
  3044. continue;
  3045. }
  3046. //this is an implicit conversion
  3047. ConstantNode *constant = static_cast<ConstantNode *>(p_func->arguments[k + 1]);
  3048. ConstantNode *conversion = alloc_node<ConstantNode>();
  3049. conversion->datatype = pfunc->arguments[k].type;
  3050. conversion->values.resize(1);
  3051. convert_constant(constant, pfunc->arguments[k].type, conversion->values.ptrw());
  3052. p_func->arguments.write[k + 1] = conversion;
  3053. }
  3054. if (r_ret_type) {
  3055. *r_ret_type = pfunc->return_type;
  3056. if (pfunc->return_type == TYPE_STRUCT) {
  3057. *r_ret_type_str = pfunc->return_struct_name;
  3058. }
  3059. }
  3060. if (r_is_custom_function) {
  3061. *r_is_custom_function = true;
  3062. }
  3063. return true;
  3064. }
  3065. }
  3066. if (exists) {
  3067. if (last_arg_count > args.size()) {
  3068. _set_error(vformat(RTR("Too few arguments for \"%s(%s)\" call. Expected at least %d but received %d."), String(name), arg_list, last_arg_count, args.size()));
  3069. } else if (last_arg_count < args.size()) {
  3070. _set_error(vformat(RTR("Too many arguments for \"%s(%s)\" call. Expected at most %d but received %d."), String(name), arg_list, last_arg_count, args.size()));
  3071. }
  3072. }
  3073. return false;
  3074. }
  3075. 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) {
  3076. bool result = true;
  3077. if (p_datatype_a == TYPE_STRUCT || p_datatype_b == TYPE_STRUCT) {
  3078. if (p_datatype_name_a != p_datatype_name_b) {
  3079. result = false;
  3080. }
  3081. } else {
  3082. if (p_datatype_a != p_datatype_b) {
  3083. result = false;
  3084. }
  3085. }
  3086. if (p_array_size_a != p_array_size_b) {
  3087. result = false;
  3088. }
  3089. if (!result) {
  3090. String type_name = p_datatype_a == TYPE_STRUCT ? p_datatype_name_a : get_datatype_name(p_datatype_a);
  3091. if (p_array_size_a > 0) {
  3092. type_name += "[";
  3093. type_name += itos(p_array_size_a);
  3094. type_name += "]";
  3095. }
  3096. String type_name2 = p_datatype_b == TYPE_STRUCT ? p_datatype_name_b : get_datatype_name(p_datatype_b);
  3097. if (p_array_size_b > 0) {
  3098. type_name2 += "[";
  3099. type_name2 += itos(p_array_size_b);
  3100. type_name2 += "]";
  3101. }
  3102. _set_error(vformat(RTR("Invalid assignment of '%s' to '%s'."), type_name2, type_name));
  3103. }
  3104. return result;
  3105. }
  3106. bool ShaderLanguage::_compare_datatypes_in_nodes(Node *a, Node *b) {
  3107. 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());
  3108. }
  3109. bool ShaderLanguage::_parse_function_arguments(BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_func, int *r_complete_arg) {
  3110. TkPos pos = _get_tkpos();
  3111. Token tk = _get_token();
  3112. if (tk.type == TK_PARENTHESIS_CLOSE) {
  3113. return true;
  3114. }
  3115. _set_tkpos(pos);
  3116. while (true) {
  3117. if (r_complete_arg) {
  3118. pos = _get_tkpos();
  3119. tk = _get_token();
  3120. if (tk.type == TK_CURSOR) {
  3121. *r_complete_arg = p_func->arguments.size() - 1;
  3122. } else {
  3123. _set_tkpos(pos);
  3124. }
  3125. }
  3126. Node *arg = _parse_and_reduce_expression(p_block, p_function_info);
  3127. if (!arg) {
  3128. return false;
  3129. }
  3130. if (is_const_decl && arg->type == Node::NODE_TYPE_VARIABLE) {
  3131. const VariableNode *var = static_cast<const VariableNode *>(arg);
  3132. if (!var->is_const) {
  3133. _set_error(RTR("Expected constant expression."));
  3134. return false;
  3135. }
  3136. }
  3137. p_func->arguments.push_back(arg);
  3138. tk = _get_token();
  3139. if (tk.type == TK_PARENTHESIS_CLOSE) {
  3140. return true;
  3141. } else if (tk.type != TK_COMMA) {
  3142. // something is broken
  3143. _set_error(RTR("Expected ',' or ')' after argument."));
  3144. return false;
  3145. }
  3146. }
  3147. return true;
  3148. }
  3149. bool ShaderLanguage::is_token_operator(TokenType p_type) {
  3150. return (p_type == TK_OP_EQUAL ||
  3151. p_type == TK_OP_NOT_EQUAL ||
  3152. p_type == TK_OP_LESS ||
  3153. p_type == TK_OP_LESS_EQUAL ||
  3154. p_type == TK_OP_GREATER ||
  3155. p_type == TK_OP_GREATER_EQUAL ||
  3156. p_type == TK_OP_AND ||
  3157. p_type == TK_OP_OR ||
  3158. p_type == TK_OP_NOT ||
  3159. p_type == TK_OP_ADD ||
  3160. p_type == TK_OP_SUB ||
  3161. p_type == TK_OP_MUL ||
  3162. p_type == TK_OP_DIV ||
  3163. p_type == TK_OP_MOD ||
  3164. p_type == TK_OP_SHIFT_LEFT ||
  3165. p_type == TK_OP_SHIFT_RIGHT ||
  3166. p_type == TK_OP_ASSIGN ||
  3167. p_type == TK_OP_ASSIGN_ADD ||
  3168. p_type == TK_OP_ASSIGN_SUB ||
  3169. p_type == TK_OP_ASSIGN_MUL ||
  3170. p_type == TK_OP_ASSIGN_DIV ||
  3171. p_type == TK_OP_ASSIGN_MOD ||
  3172. p_type == TK_OP_ASSIGN_SHIFT_LEFT ||
  3173. p_type == TK_OP_ASSIGN_SHIFT_RIGHT ||
  3174. p_type == TK_OP_ASSIGN_BIT_AND ||
  3175. p_type == TK_OP_ASSIGN_BIT_OR ||
  3176. p_type == TK_OP_ASSIGN_BIT_XOR ||
  3177. p_type == TK_OP_BIT_AND ||
  3178. p_type == TK_OP_BIT_OR ||
  3179. p_type == TK_OP_BIT_XOR ||
  3180. p_type == TK_OP_BIT_INVERT ||
  3181. p_type == TK_OP_INCREMENT ||
  3182. p_type == TK_OP_DECREMENT ||
  3183. p_type == TK_QUESTION ||
  3184. p_type == TK_COLON);
  3185. }
  3186. bool ShaderLanguage::is_token_operator_assign(TokenType p_type) {
  3187. return (p_type == TK_OP_ASSIGN ||
  3188. p_type == TK_OP_ASSIGN_ADD ||
  3189. p_type == TK_OP_ASSIGN_SUB ||
  3190. p_type == TK_OP_ASSIGN_MUL ||
  3191. p_type == TK_OP_ASSIGN_DIV ||
  3192. p_type == TK_OP_ASSIGN_MOD ||
  3193. p_type == TK_OP_ASSIGN_SHIFT_LEFT ||
  3194. p_type == TK_OP_ASSIGN_SHIFT_RIGHT ||
  3195. p_type == TK_OP_ASSIGN_BIT_AND ||
  3196. p_type == TK_OP_ASSIGN_BIT_OR ||
  3197. p_type == TK_OP_ASSIGN_BIT_XOR);
  3198. }
  3199. bool ShaderLanguage::is_token_hint(TokenType p_type) {
  3200. return int(p_type) > int(TK_RENDER_MODE) && int(p_type) < int(TK_SHADER_TYPE);
  3201. }
  3202. bool ShaderLanguage::convert_constant(ConstantNode *p_constant, DataType p_to_type, ConstantNode::Value *p_value) {
  3203. if (p_constant->datatype == p_to_type) {
  3204. if (p_value) {
  3205. for (int i = 0; i < p_constant->values.size(); i++) {
  3206. p_value[i] = p_constant->values[i];
  3207. }
  3208. }
  3209. return true;
  3210. } else if (p_constant->datatype == TYPE_INT && p_to_type == TYPE_FLOAT) {
  3211. if (p_value) {
  3212. p_value->real = p_constant->values[0].sint;
  3213. }
  3214. return true;
  3215. } else if (p_constant->datatype == TYPE_UINT && p_to_type == TYPE_FLOAT) {
  3216. if (p_value) {
  3217. p_value->real = p_constant->values[0].uint;
  3218. }
  3219. return true;
  3220. } else if (p_constant->datatype == TYPE_INT && p_to_type == TYPE_UINT) {
  3221. if (p_constant->values[0].sint < 0) {
  3222. return false;
  3223. }
  3224. if (p_value) {
  3225. p_value->uint = p_constant->values[0].sint;
  3226. }
  3227. return true;
  3228. } else if (p_constant->datatype == TYPE_UINT && p_to_type == TYPE_INT) {
  3229. if (p_constant->values[0].uint > 0x7FFFFFFF) {
  3230. return false;
  3231. }
  3232. if (p_value) {
  3233. p_value->sint = p_constant->values[0].uint;
  3234. }
  3235. return true;
  3236. } else {
  3237. return false;
  3238. }
  3239. }
  3240. bool ShaderLanguage::is_scalar_type(DataType p_type) {
  3241. return p_type == TYPE_BOOL || p_type == TYPE_INT || p_type == TYPE_UINT || p_type == TYPE_FLOAT;
  3242. }
  3243. bool ShaderLanguage::is_float_type(DataType p_type) {
  3244. switch (p_type) {
  3245. case TYPE_FLOAT:
  3246. case TYPE_VEC2:
  3247. case TYPE_VEC3:
  3248. case TYPE_VEC4:
  3249. case TYPE_MAT2:
  3250. case TYPE_MAT3:
  3251. case TYPE_MAT4:
  3252. case TYPE_SAMPLER2D:
  3253. case TYPE_SAMPLER2DARRAY:
  3254. case TYPE_SAMPLER3D:
  3255. case TYPE_SAMPLERCUBE:
  3256. case TYPE_SAMPLERCUBEARRAY: {
  3257. return true;
  3258. }
  3259. default: {
  3260. return false;
  3261. }
  3262. }
  3263. }
  3264. bool ShaderLanguage::is_sampler_type(DataType p_type) {
  3265. return p_type > TYPE_MAT4 && p_type < TYPE_STRUCT;
  3266. }
  3267. 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) {
  3268. int array_size = p_array_size;
  3269. if (p_value.size() > 0) {
  3270. Variant value;
  3271. switch (p_type) {
  3272. case ShaderLanguage::TYPE_BOOL:
  3273. if (array_size > 0) {
  3274. PackedInt32Array array;
  3275. for (int i = 0; i < array_size; i++) {
  3276. array.push_back(p_value[i].boolean);
  3277. }
  3278. value = Variant(array);
  3279. } else {
  3280. value = Variant(p_value[0].boolean);
  3281. }
  3282. break;
  3283. case ShaderLanguage::TYPE_BVEC2:
  3284. array_size *= 2;
  3285. if (array_size > 0) {
  3286. PackedInt32Array array;
  3287. for (int i = 0; i < array_size; i++) {
  3288. array.push_back(p_value[i].boolean);
  3289. }
  3290. value = Variant(array);
  3291. } else {
  3292. value = Variant(p_value[0].boolean);
  3293. }
  3294. break;
  3295. case ShaderLanguage::TYPE_BVEC3:
  3296. array_size *= 3;
  3297. if (array_size > 0) {
  3298. PackedInt32Array array;
  3299. for (int i = 0; i < array_size; i++) {
  3300. array.push_back(p_value[i].boolean);
  3301. }
  3302. value = Variant(array);
  3303. } else {
  3304. value = Variant(p_value[0].boolean);
  3305. }
  3306. break;
  3307. case ShaderLanguage::TYPE_BVEC4:
  3308. array_size *= 4;
  3309. if (array_size > 0) {
  3310. PackedInt32Array array;
  3311. for (int i = 0; i < array_size; i++) {
  3312. array.push_back(p_value[i].boolean);
  3313. }
  3314. value = Variant(array);
  3315. } else {
  3316. value = Variant(p_value[0].boolean);
  3317. }
  3318. break;
  3319. case ShaderLanguage::TYPE_INT:
  3320. if (array_size > 0) {
  3321. PackedInt32Array array;
  3322. for (int i = 0; i < array_size; i++) {
  3323. array.push_back(p_value[i].sint);
  3324. }
  3325. value = Variant(array);
  3326. } else {
  3327. value = Variant(p_value[0].sint);
  3328. }
  3329. break;
  3330. case ShaderLanguage::TYPE_IVEC2:
  3331. if (array_size > 0) {
  3332. array_size *= 2;
  3333. PackedInt32Array array;
  3334. for (int i = 0; i < array_size; i++) {
  3335. array.push_back(p_value[i].sint);
  3336. }
  3337. value = Variant(array);
  3338. } else {
  3339. value = Variant(Vector2i(p_value[0].sint, p_value[1].sint));
  3340. }
  3341. break;
  3342. case ShaderLanguage::TYPE_IVEC3:
  3343. if (array_size > 0) {
  3344. array_size *= 3;
  3345. PackedInt32Array array;
  3346. for (int i = 0; i < array_size; i++) {
  3347. array.push_back(p_value[i].sint);
  3348. }
  3349. value = Variant(array);
  3350. } else {
  3351. value = Variant(Vector3i(p_value[0].sint, p_value[1].sint, p_value[2].sint));
  3352. }
  3353. break;
  3354. case ShaderLanguage::TYPE_IVEC4:
  3355. if (array_size > 0) {
  3356. array_size *= 4;
  3357. PackedInt32Array array;
  3358. for (int i = 0; i < array_size; i++) {
  3359. array.push_back(p_value[i].sint);
  3360. }
  3361. value = Variant(array);
  3362. } else {
  3363. value = Variant(Vector4i(p_value[0].sint, p_value[1].sint, p_value[2].sint, p_value[3].sint));
  3364. }
  3365. break;
  3366. case ShaderLanguage::TYPE_UINT:
  3367. if (array_size > 0) {
  3368. PackedInt32Array array;
  3369. for (int i = 0; i < array_size; i++) {
  3370. array.push_back(p_value[i].uint);
  3371. }
  3372. value = Variant(array);
  3373. } else {
  3374. value = Variant(p_value[0].uint);
  3375. }
  3376. break;
  3377. case ShaderLanguage::TYPE_UVEC2:
  3378. if (array_size > 0) {
  3379. array_size *= 2;
  3380. PackedInt32Array array;
  3381. for (int i = 0; i < array_size; i++) {
  3382. array.push_back(p_value[i].uint);
  3383. }
  3384. value = Variant(array);
  3385. } else {
  3386. value = Variant(Vector2i(p_value[0].uint, p_value[1].uint));
  3387. }
  3388. break;
  3389. case ShaderLanguage::TYPE_UVEC3:
  3390. if (array_size > 0) {
  3391. array_size *= 3;
  3392. PackedInt32Array array;
  3393. for (int i = 0; i < array_size; i++) {
  3394. array.push_back(p_value[i].uint);
  3395. }
  3396. value = Variant(array);
  3397. } else {
  3398. value = Variant(Vector3i(p_value[0].uint, p_value[1].uint, p_value[2].uint));
  3399. }
  3400. break;
  3401. case ShaderLanguage::TYPE_UVEC4:
  3402. if (array_size > 0) {
  3403. array_size *= 4;
  3404. PackedInt32Array array;
  3405. for (int i = 0; i < array_size; i++) {
  3406. array.push_back(p_value[i].uint);
  3407. }
  3408. value = Variant(array);
  3409. } else {
  3410. value = Variant(Vector4i(p_value[0].uint, p_value[1].uint, p_value[2].uint, p_value[3].uint));
  3411. }
  3412. break;
  3413. case ShaderLanguage::TYPE_FLOAT:
  3414. if (array_size > 0) {
  3415. PackedFloat32Array array;
  3416. for (int i = 0; i < array_size; i++) {
  3417. array.push_back(p_value[i].real);
  3418. }
  3419. value = Variant(array);
  3420. } else {
  3421. value = Variant(p_value[0].real);
  3422. }
  3423. break;
  3424. case ShaderLanguage::TYPE_VEC2:
  3425. if (array_size > 0) {
  3426. array_size *= 2;
  3427. PackedVector2Array array;
  3428. for (int i = 0; i < array_size; i += 2) {
  3429. array.push_back(Vector2(p_value[i].real, p_value[i + 1].real));
  3430. }
  3431. value = Variant(array);
  3432. } else {
  3433. value = Variant(Vector2(p_value[0].real, p_value[1].real));
  3434. }
  3435. break;
  3436. case ShaderLanguage::TYPE_VEC3:
  3437. if (array_size > 0) {
  3438. array_size *= 3;
  3439. if (p_hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3440. PackedColorArray array;
  3441. for (int i = 0; i < array_size; i += 3) {
  3442. array.push_back(Color(p_value[i].real, p_value[i + 1].real, p_value[i + 2].real));
  3443. }
  3444. value = Variant(array);
  3445. } else {
  3446. PackedVector3Array array;
  3447. for (int i = 0; i < array_size; i += 3) {
  3448. array.push_back(Vector3(p_value[i].real, p_value[i + 1].real, p_value[i + 2].real));
  3449. }
  3450. value = Variant(array);
  3451. }
  3452. } else {
  3453. if (p_hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3454. value = Variant(Color(p_value[0].real, p_value[1].real, p_value[2].real));
  3455. } else {
  3456. value = Variant(Vector3(p_value[0].real, p_value[1].real, p_value[2].real));
  3457. }
  3458. }
  3459. break;
  3460. case ShaderLanguage::TYPE_VEC4:
  3461. if (array_size > 0) {
  3462. array_size *= 4;
  3463. if (p_hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3464. PackedColorArray array;
  3465. for (int i = 0; i < array_size; i += 4) {
  3466. array.push_back(Color(p_value[i].real, p_value[i + 1].real, p_value[i + 2].real, p_value[i + 3].real));
  3467. }
  3468. value = Variant(array);
  3469. } else {
  3470. PackedVector4Array array;
  3471. for (int i = 0; i < array_size; i += 4) {
  3472. array.push_back(Vector4(p_value[i].real, p_value[i + 1].real, p_value[i + 2].real, p_value[i + 3].real));
  3473. }
  3474. value = Variant(array);
  3475. }
  3476. } else {
  3477. if (p_hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3478. value = Variant(Color(p_value[0].real, p_value[1].real, p_value[2].real, p_value[3].real));
  3479. } else {
  3480. value = Variant(Vector4(p_value[0].real, p_value[1].real, p_value[2].real, p_value[3].real));
  3481. }
  3482. }
  3483. break;
  3484. case ShaderLanguage::TYPE_MAT2:
  3485. if (array_size > 0) {
  3486. array_size *= 4;
  3487. PackedFloat32Array array;
  3488. for (int i = 0; i < array_size; i += 4) {
  3489. array.push_back(p_value[i].real);
  3490. array.push_back(p_value[i + 1].real);
  3491. array.push_back(p_value[i + 2].real);
  3492. array.push_back(p_value[i + 3].real);
  3493. }
  3494. value = Variant(array);
  3495. } else {
  3496. value = Variant(Transform2D(p_value[0].real, p_value[2].real, p_value[1].real, p_value[3].real, 0.0, 0.0));
  3497. }
  3498. break;
  3499. case ShaderLanguage::TYPE_MAT3: {
  3500. if (array_size > 0) {
  3501. array_size *= 9;
  3502. PackedFloat32Array array;
  3503. for (int i = 0; i < array_size; i += 9) {
  3504. for (int j = 0; j < 9; j++) {
  3505. array.push_back(p_value[i + j].real);
  3506. }
  3507. }
  3508. value = Variant(array);
  3509. } else {
  3510. Basis p;
  3511. p[0][0] = p_value[0].real;
  3512. p[0][1] = p_value[1].real;
  3513. p[0][2] = p_value[2].real;
  3514. p[1][0] = p_value[3].real;
  3515. p[1][1] = p_value[4].real;
  3516. p[1][2] = p_value[5].real;
  3517. p[2][0] = p_value[6].real;
  3518. p[2][1] = p_value[7].real;
  3519. p[2][2] = p_value[8].real;
  3520. value = Variant(p);
  3521. }
  3522. break;
  3523. }
  3524. case ShaderLanguage::TYPE_MAT4: {
  3525. if (array_size > 0) {
  3526. array_size *= 16;
  3527. PackedFloat32Array array;
  3528. for (int i = 0; i < array_size; i += 16) {
  3529. for (int j = 0; j < 16; j++) {
  3530. array.push_back(p_value[i + j].real);
  3531. }
  3532. }
  3533. value = Variant(array);
  3534. } else {
  3535. Projection p = Projection(Vector4(p_value[0].real, p_value[1].real, p_value[2].real, p_value[3].real),
  3536. Vector4(p_value[4].real, p_value[5].real, p_value[6].real, p_value[7].real),
  3537. Vector4(p_value[8].real, p_value[9].real, p_value[10].real, p_value[11].real),
  3538. Vector4(p_value[12].real, p_value[13].real, p_value[14].real, p_value[15].real));
  3539. value = Variant(p);
  3540. }
  3541. break;
  3542. }
  3543. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  3544. case ShaderLanguage::TYPE_ISAMPLER2D:
  3545. case ShaderLanguage::TYPE_ISAMPLER3D:
  3546. case ShaderLanguage::TYPE_SAMPLER2DARRAY:
  3547. case ShaderLanguage::TYPE_SAMPLER2D:
  3548. case ShaderLanguage::TYPE_SAMPLER3D:
  3549. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  3550. case ShaderLanguage::TYPE_USAMPLER2D:
  3551. case ShaderLanguage::TYPE_USAMPLER3D:
  3552. case ShaderLanguage::TYPE_SAMPLERCUBE:
  3553. case ShaderLanguage::TYPE_SAMPLERCUBEARRAY: {
  3554. // Texture types, likely not relevant here.
  3555. break;
  3556. }
  3557. case ShaderLanguage::TYPE_STRUCT:
  3558. break;
  3559. case ShaderLanguage::TYPE_VOID:
  3560. break;
  3561. case ShaderLanguage::TYPE_MAX:
  3562. break;
  3563. }
  3564. return value;
  3565. }
  3566. return Variant();
  3567. }
  3568. PropertyInfo ShaderLanguage::uniform_to_property_info(const ShaderNode::Uniform &p_uniform) {
  3569. PropertyInfo pi;
  3570. switch (p_uniform.type) {
  3571. case ShaderLanguage::TYPE_VOID:
  3572. pi.type = Variant::NIL;
  3573. break;
  3574. case ShaderLanguage::TYPE_BOOL:
  3575. if (p_uniform.array_size > 0) {
  3576. pi.type = Variant::PACKED_INT32_ARRAY;
  3577. pi.hint = PROPERTY_HINT_TYPE_STRING;
  3578. pi.hint_string = itos(Variant::INT) + "/" + itos(PROPERTY_HINT_FLAGS) + ":" + RTR("On");
  3579. } else {
  3580. pi.type = Variant::BOOL;
  3581. }
  3582. break;
  3583. case ShaderLanguage::TYPE_BVEC2:
  3584. if (p_uniform.array_size > 0) {
  3585. pi.type = Variant::PACKED_INT32_ARRAY;
  3586. pi.hint = PROPERTY_HINT_TYPE_STRING;
  3587. pi.hint_string = itos(Variant::INT) + "/" + itos(PROPERTY_HINT_FLAGS) + ":x,y";
  3588. } else {
  3589. pi.type = Variant::INT;
  3590. pi.hint = PROPERTY_HINT_FLAGS;
  3591. pi.hint_string = "x,y";
  3592. }
  3593. break;
  3594. case ShaderLanguage::TYPE_BVEC3:
  3595. if (p_uniform.array_size > 0) {
  3596. pi.type = Variant::PACKED_INT32_ARRAY;
  3597. pi.hint = PROPERTY_HINT_TYPE_STRING;
  3598. pi.hint_string = itos(Variant::INT) + "/" + itos(PROPERTY_HINT_FLAGS) + ":x,y,z";
  3599. } else {
  3600. pi.type = Variant::INT;
  3601. pi.hint = PROPERTY_HINT_FLAGS;
  3602. pi.hint_string = "x,y,z";
  3603. }
  3604. break;
  3605. case ShaderLanguage::TYPE_BVEC4:
  3606. if (p_uniform.array_size > 0) {
  3607. pi.type = Variant::PACKED_INT32_ARRAY;
  3608. pi.hint = PROPERTY_HINT_TYPE_STRING;
  3609. pi.hint_string = itos(Variant::INT) + "/" + itos(PROPERTY_HINT_FLAGS) + ":x,y,z,w";
  3610. } else {
  3611. pi.type = Variant::INT;
  3612. pi.hint = PROPERTY_HINT_FLAGS;
  3613. pi.hint_string = "x,y,z,w";
  3614. }
  3615. break;
  3616. case ShaderLanguage::TYPE_UINT:
  3617. case ShaderLanguage::TYPE_INT: {
  3618. if (p_uniform.array_size > 0) {
  3619. pi.type = Variant::PACKED_INT32_ARRAY;
  3620. // TODO: Handle range and encoding for for unsigned values.
  3621. } else {
  3622. pi.type = Variant::INT;
  3623. pi.hint = PROPERTY_HINT_RANGE;
  3624. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_RANGE) {
  3625. pi.hint_string = rtos(p_uniform.hint_range[0]) + "," + rtos(p_uniform.hint_range[1]) + "," + rtos(p_uniform.hint_range[2]);
  3626. } else if (p_uniform.type == ShaderLanguage::TYPE_UINT) {
  3627. pi.hint_string = "0," + itos(UINT32_MAX);
  3628. } else {
  3629. pi.hint_string = itos(INT32_MIN) + "," + itos(INT32_MAX);
  3630. }
  3631. }
  3632. } break;
  3633. case ShaderLanguage::TYPE_UVEC2:
  3634. case ShaderLanguage::TYPE_IVEC2: {
  3635. if (p_uniform.array_size > 0) {
  3636. pi.type = Variant::PACKED_INT32_ARRAY;
  3637. // TODO: Handle vector pairs?
  3638. } else {
  3639. pi.type = Variant::VECTOR2I;
  3640. }
  3641. } break;
  3642. case ShaderLanguage::TYPE_UVEC3:
  3643. case ShaderLanguage::TYPE_IVEC3: {
  3644. if (p_uniform.array_size > 0) {
  3645. pi.type = Variant::PACKED_INT32_ARRAY;
  3646. // TODO: Handle vector pairs?
  3647. } else {
  3648. pi.type = Variant::VECTOR3I;
  3649. }
  3650. } break;
  3651. case ShaderLanguage::TYPE_UVEC4:
  3652. case ShaderLanguage::TYPE_IVEC4: {
  3653. if (p_uniform.array_size > 0) {
  3654. pi.type = Variant::PACKED_INT32_ARRAY;
  3655. // TODO: Handle vector pairs?
  3656. } else {
  3657. pi.type = Variant::VECTOR4I;
  3658. }
  3659. } break;
  3660. case ShaderLanguage::TYPE_FLOAT: {
  3661. if (p_uniform.array_size > 0) {
  3662. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  3663. } else {
  3664. pi.type = Variant::FLOAT;
  3665. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_RANGE) {
  3666. pi.hint = PROPERTY_HINT_RANGE;
  3667. pi.hint_string = rtos(p_uniform.hint_range[0]) + "," + rtos(p_uniform.hint_range[1]) + "," + rtos(p_uniform.hint_range[2]);
  3668. }
  3669. }
  3670. } break;
  3671. case ShaderLanguage::TYPE_VEC2:
  3672. if (p_uniform.array_size > 0) {
  3673. pi.type = Variant::PACKED_VECTOR2_ARRAY;
  3674. } else {
  3675. pi.type = Variant::VECTOR2;
  3676. }
  3677. break;
  3678. case ShaderLanguage::TYPE_VEC3:
  3679. if (p_uniform.array_size > 0) {
  3680. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3681. pi.hint = PROPERTY_HINT_COLOR_NO_ALPHA;
  3682. pi.type = Variant::PACKED_COLOR_ARRAY;
  3683. } else {
  3684. pi.type = Variant::PACKED_VECTOR3_ARRAY;
  3685. }
  3686. } else {
  3687. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3688. pi.hint = PROPERTY_HINT_COLOR_NO_ALPHA;
  3689. pi.type = Variant::COLOR;
  3690. } else {
  3691. pi.type = Variant::VECTOR3;
  3692. }
  3693. }
  3694. break;
  3695. case ShaderLanguage::TYPE_VEC4: {
  3696. if (p_uniform.array_size > 0) {
  3697. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3698. pi.type = Variant::PACKED_COLOR_ARRAY;
  3699. } else {
  3700. pi.type = Variant::PACKED_VECTOR4_ARRAY;
  3701. }
  3702. } else {
  3703. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3704. pi.type = Variant::COLOR;
  3705. } else {
  3706. pi.type = Variant::VECTOR4;
  3707. }
  3708. }
  3709. } break;
  3710. case ShaderLanguage::TYPE_MAT2:
  3711. if (p_uniform.array_size > 0) {
  3712. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  3713. } else {
  3714. pi.type = Variant::TRANSFORM2D;
  3715. }
  3716. break;
  3717. case ShaderLanguage::TYPE_MAT3:
  3718. if (p_uniform.array_size > 0) {
  3719. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  3720. } else {
  3721. pi.type = Variant::BASIS;
  3722. }
  3723. break;
  3724. case ShaderLanguage::TYPE_MAT4:
  3725. if (p_uniform.array_size > 0) {
  3726. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  3727. } else {
  3728. pi.type = Variant::PROJECTION;
  3729. }
  3730. break;
  3731. case ShaderLanguage::TYPE_SAMPLER2D:
  3732. case ShaderLanguage::TYPE_ISAMPLER2D:
  3733. case ShaderLanguage::TYPE_USAMPLER2D: {
  3734. if (p_uniform.array_size > 0) {
  3735. pi.type = Variant::ARRAY;
  3736. pi.hint = PROPERTY_HINT_ARRAY_TYPE;
  3737. pi.hint_string = MAKE_RESOURCE_TYPE_HINT("Texture2D");
  3738. } else {
  3739. pi.type = Variant::OBJECT;
  3740. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3741. pi.hint_string = "Texture2D";
  3742. }
  3743. } break;
  3744. case ShaderLanguage::TYPE_SAMPLER2DARRAY:
  3745. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  3746. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  3747. case ShaderLanguage::TYPE_SAMPLERCUBE:
  3748. case ShaderLanguage::TYPE_SAMPLERCUBEARRAY: {
  3749. if (p_uniform.array_size > 0) {
  3750. pi.type = Variant::ARRAY;
  3751. pi.hint = PROPERTY_HINT_ARRAY_TYPE;
  3752. pi.hint_string = MAKE_RESOURCE_TYPE_HINT("TextureLayered");
  3753. } else {
  3754. pi.type = Variant::OBJECT;
  3755. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3756. pi.hint_string = "TextureLayered";
  3757. }
  3758. } break;
  3759. case ShaderLanguage::TYPE_SAMPLER3D:
  3760. case ShaderLanguage::TYPE_ISAMPLER3D:
  3761. case ShaderLanguage::TYPE_USAMPLER3D: {
  3762. if (p_uniform.array_size > 0) {
  3763. pi.type = Variant::ARRAY;
  3764. pi.hint = PROPERTY_HINT_ARRAY_TYPE;
  3765. pi.hint_string = MAKE_RESOURCE_TYPE_HINT("Texture3D");
  3766. } else {
  3767. pi.type = Variant::OBJECT;
  3768. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3769. pi.hint_string = "Texture3D";
  3770. }
  3771. } break;
  3772. case ShaderLanguage::TYPE_STRUCT: {
  3773. // FIXME: Implement this.
  3774. } break;
  3775. case ShaderLanguage::TYPE_MAX:
  3776. break;
  3777. }
  3778. return pi;
  3779. }
  3780. uint32_t ShaderLanguage::get_datatype_size(ShaderLanguage::DataType p_type) {
  3781. switch (p_type) {
  3782. case TYPE_VOID:
  3783. return 0;
  3784. case TYPE_BOOL:
  3785. return 4;
  3786. case TYPE_BVEC2:
  3787. return 8;
  3788. case TYPE_BVEC3:
  3789. return 12;
  3790. case TYPE_BVEC4:
  3791. return 16;
  3792. case TYPE_INT:
  3793. return 4;
  3794. case TYPE_IVEC2:
  3795. return 8;
  3796. case TYPE_IVEC3:
  3797. return 12;
  3798. case TYPE_IVEC4:
  3799. return 16;
  3800. case TYPE_UINT:
  3801. return 4;
  3802. case TYPE_UVEC2:
  3803. return 8;
  3804. case TYPE_UVEC3:
  3805. return 12;
  3806. case TYPE_UVEC4:
  3807. return 16;
  3808. case TYPE_FLOAT:
  3809. return 4;
  3810. case TYPE_VEC2:
  3811. return 8;
  3812. case TYPE_VEC3:
  3813. return 12;
  3814. case TYPE_VEC4:
  3815. return 16;
  3816. case TYPE_MAT2:
  3817. return 32; // 4 * 4 + 4 * 4
  3818. case TYPE_MAT3:
  3819. return 48; // 4 * 4 + 4 * 4 + 4 * 4
  3820. case TYPE_MAT4:
  3821. return 64;
  3822. case TYPE_SAMPLER2D:
  3823. return 16;
  3824. case TYPE_ISAMPLER2D:
  3825. return 16;
  3826. case TYPE_USAMPLER2D:
  3827. return 16;
  3828. case TYPE_SAMPLER2DARRAY:
  3829. return 16;
  3830. case TYPE_ISAMPLER2DARRAY:
  3831. return 16;
  3832. case TYPE_USAMPLER2DARRAY:
  3833. return 16;
  3834. case TYPE_SAMPLER3D:
  3835. return 16;
  3836. case TYPE_ISAMPLER3D:
  3837. return 16;
  3838. case TYPE_USAMPLER3D:
  3839. return 16;
  3840. case TYPE_SAMPLERCUBE:
  3841. return 16;
  3842. case TYPE_SAMPLERCUBEARRAY:
  3843. return 16;
  3844. case TYPE_STRUCT:
  3845. return 0;
  3846. case TYPE_MAX: {
  3847. ERR_FAIL_V(0);
  3848. };
  3849. }
  3850. ERR_FAIL_V(0);
  3851. }
  3852. void ShaderLanguage::get_keyword_list(List<String> *r_keywords) {
  3853. HashSet<String> kws;
  3854. int idx = 0;
  3855. while (keyword_list[idx].text) {
  3856. kws.insert(keyword_list[idx].text);
  3857. idx++;
  3858. }
  3859. idx = 0;
  3860. while (builtin_func_defs[idx].name) {
  3861. kws.insert(builtin_func_defs[idx].name);
  3862. idx++;
  3863. }
  3864. for (const String &E : kws) {
  3865. r_keywords->push_back(E);
  3866. }
  3867. }
  3868. bool ShaderLanguage::is_control_flow_keyword(String p_keyword) {
  3869. return p_keyword == "break" ||
  3870. p_keyword == "case" ||
  3871. p_keyword == "continue" ||
  3872. p_keyword == "default" ||
  3873. p_keyword == "do" ||
  3874. p_keyword == "else" ||
  3875. p_keyword == "for" ||
  3876. p_keyword == "if" ||
  3877. p_keyword == "return" ||
  3878. p_keyword == "switch" ||
  3879. p_keyword == "while";
  3880. }
  3881. void ShaderLanguage::get_builtin_funcs(List<String> *r_keywords) {
  3882. HashSet<String> kws;
  3883. int idx = 0;
  3884. while (builtin_func_defs[idx].name) {
  3885. kws.insert(builtin_func_defs[idx].name);
  3886. idx++;
  3887. }
  3888. for (const String &E : kws) {
  3889. r_keywords->push_back(E);
  3890. }
  3891. }
  3892. ShaderLanguage::DataType ShaderLanguage::get_scalar_type(DataType p_type) {
  3893. static const DataType scalar_types[] = {
  3894. TYPE_VOID,
  3895. TYPE_BOOL,
  3896. TYPE_BOOL,
  3897. TYPE_BOOL,
  3898. TYPE_BOOL,
  3899. TYPE_INT,
  3900. TYPE_INT,
  3901. TYPE_INT,
  3902. TYPE_INT,
  3903. TYPE_UINT,
  3904. TYPE_UINT,
  3905. TYPE_UINT,
  3906. TYPE_UINT,
  3907. TYPE_FLOAT,
  3908. TYPE_FLOAT,
  3909. TYPE_FLOAT,
  3910. TYPE_FLOAT,
  3911. TYPE_FLOAT,
  3912. TYPE_FLOAT,
  3913. TYPE_FLOAT,
  3914. TYPE_FLOAT,
  3915. TYPE_INT,
  3916. TYPE_UINT,
  3917. TYPE_FLOAT,
  3918. TYPE_INT,
  3919. TYPE_UINT,
  3920. TYPE_FLOAT,
  3921. TYPE_INT,
  3922. TYPE_UINT,
  3923. TYPE_FLOAT,
  3924. TYPE_FLOAT,
  3925. TYPE_VOID,
  3926. };
  3927. static_assert(sizeof(scalar_types) / sizeof(*scalar_types) == TYPE_MAX);
  3928. return scalar_types[p_type];
  3929. }
  3930. int ShaderLanguage::get_cardinality(DataType p_type) {
  3931. static const int cardinality_table[] = {
  3932. 0,
  3933. 1,
  3934. 2,
  3935. 3,
  3936. 4,
  3937. 1,
  3938. 2,
  3939. 3,
  3940. 4,
  3941. 1,
  3942. 2,
  3943. 3,
  3944. 4,
  3945. 1,
  3946. 2,
  3947. 3,
  3948. 4,
  3949. 4,
  3950. 9,
  3951. 16,
  3952. 1,
  3953. 1,
  3954. 1,
  3955. 1,
  3956. 1,
  3957. 1,
  3958. 1,
  3959. 1,
  3960. 1,
  3961. 1,
  3962. 1,
  3963. 1,
  3964. };
  3965. static_assert(sizeof(cardinality_table) / sizeof(*cardinality_table) == TYPE_MAX);
  3966. return cardinality_table[p_type];
  3967. }
  3968. bool ShaderLanguage::_get_completable_identifier(BlockNode *p_block, CompletionType p_type, StringName &identifier) {
  3969. identifier = StringName();
  3970. TkPos pos = { 0, 0 };
  3971. Token tk = _get_token();
  3972. if (tk.type == TK_IDENTIFIER) {
  3973. identifier = tk.text;
  3974. pos = _get_tkpos();
  3975. tk = _get_token();
  3976. }
  3977. if (tk.type == TK_CURSOR) {
  3978. completion_type = p_type;
  3979. completion_line = tk_line;
  3980. completion_block = p_block;
  3981. pos = _get_tkpos();
  3982. tk = _get_token();
  3983. if (tk.type == TK_IDENTIFIER) {
  3984. identifier = identifier.operator String() + tk.text.operator String();
  3985. } else {
  3986. _set_tkpos(pos);
  3987. }
  3988. return true;
  3989. } else if (identifier != StringName()) {
  3990. _set_tkpos(pos);
  3991. }
  3992. return false;
  3993. }
  3994. bool ShaderLanguage::_is_operator_assign(Operator p_op) const {
  3995. switch (p_op) {
  3996. case OP_ASSIGN:
  3997. case OP_ASSIGN_ADD:
  3998. case OP_ASSIGN_SUB:
  3999. case OP_ASSIGN_MUL:
  4000. case OP_ASSIGN_DIV:
  4001. case OP_ASSIGN_MOD:
  4002. case OP_ASSIGN_SHIFT_LEFT:
  4003. case OP_ASSIGN_SHIFT_RIGHT:
  4004. case OP_ASSIGN_BIT_AND:
  4005. case OP_ASSIGN_BIT_OR:
  4006. case OP_ASSIGN_BIT_XOR:
  4007. return true;
  4008. default:
  4009. return false;
  4010. }
  4011. }
  4012. bool ShaderLanguage::_validate_varying_assign(ShaderNode::Varying &p_varying, String *r_message) {
  4013. if (current_function != "vertex" && current_function != "fragment") {
  4014. *r_message = vformat(RTR("Varying may not be assigned in the '%s' function."), current_function);
  4015. return false;
  4016. }
  4017. switch (p_varying.stage) {
  4018. case ShaderNode::Varying::STAGE_UNKNOWN: // first assign
  4019. if (current_function == varying_function_names.vertex) {
  4020. if (p_varying.type < TYPE_INT) {
  4021. *r_message = vformat(RTR("Varying with '%s' data type may only be assigned in the 'fragment' function."), get_datatype_name(p_varying.type));
  4022. return false;
  4023. }
  4024. p_varying.stage = ShaderNode::Varying::STAGE_VERTEX;
  4025. } else if (current_function == varying_function_names.fragment) {
  4026. p_varying.stage = ShaderNode::Varying::STAGE_FRAGMENT;
  4027. }
  4028. break;
  4029. case ShaderNode::Varying::STAGE_VERTEX_TO_FRAGMENT_LIGHT:
  4030. case ShaderNode::Varying::STAGE_VERTEX:
  4031. if (current_function == varying_function_names.fragment) {
  4032. *r_message = RTR("Varyings which assigned in 'vertex' function may not be reassigned in 'fragment' or 'light'.");
  4033. return false;
  4034. }
  4035. break;
  4036. case ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT:
  4037. case ShaderNode::Varying::STAGE_FRAGMENT:
  4038. if (current_function == varying_function_names.vertex) {
  4039. *r_message = RTR("Varyings which assigned in 'fragment' function may not be reassigned in 'vertex' or 'light'.");
  4040. return false;
  4041. }
  4042. break;
  4043. default:
  4044. break;
  4045. }
  4046. return true;
  4047. }
  4048. bool ShaderLanguage::_check_node_constness(const Node *p_node) const {
  4049. switch (p_node->type) {
  4050. case Node::NODE_TYPE_OPERATOR: {
  4051. const OperatorNode *op_node = static_cast<const OperatorNode *>(p_node);
  4052. for (int i = int(op_node->op == OP_CALL); i < op_node->arguments.size(); i++) {
  4053. if (!_check_node_constness(op_node->arguments[i])) {
  4054. return false;
  4055. }
  4056. }
  4057. } break;
  4058. case Node::NODE_TYPE_CONSTANT:
  4059. break;
  4060. case Node::NODE_TYPE_VARIABLE: {
  4061. const VariableNode *var_node = static_cast<const VariableNode *>(p_node);
  4062. if (!var_node->is_const) {
  4063. return false;
  4064. }
  4065. } break;
  4066. case Node::NODE_TYPE_ARRAY: {
  4067. const ArrayNode *arr_node = static_cast<const ArrayNode *>(p_node);
  4068. if (!arr_node->is_const) {
  4069. return false;
  4070. }
  4071. } break;
  4072. default:
  4073. return false;
  4074. }
  4075. return true;
  4076. }
  4077. bool ShaderLanguage::_check_restricted_func(const StringName &p_name, const StringName &p_current_function) const {
  4078. int idx = 0;
  4079. while (frag_only_func_defs[idx].name) {
  4080. if (StringName(frag_only_func_defs[idx].name) == p_name) {
  4081. if (is_supported_frag_only_funcs) {
  4082. if (p_current_function == "vertex" && stages->has(p_current_function)) {
  4083. return true;
  4084. }
  4085. } else {
  4086. return true;
  4087. }
  4088. break;
  4089. }
  4090. idx++;
  4091. }
  4092. return false;
  4093. }
  4094. bool ShaderLanguage::_validate_restricted_func(const StringName &p_name, const CallInfo *p_func_info, bool p_is_builtin_hint) {
  4095. const bool is_in_restricted_function = p_func_info->name == "vertex";
  4096. // No need to check up the hierarchy if it's a built-in.
  4097. if (!p_is_builtin_hint) {
  4098. for (const CallInfo *func_info : p_func_info->calls) {
  4099. if (is_in_restricted_function && func_info->name != p_name) {
  4100. // Skips check for non-called method.
  4101. continue;
  4102. }
  4103. if (!_validate_restricted_func(p_name, func_info)) {
  4104. return false;
  4105. }
  4106. }
  4107. }
  4108. if (!p_func_info->uses_restricted_items.is_empty()) {
  4109. const Pair<StringName, CallInfo::Item> &first_element = p_func_info->uses_restricted_items.get(0);
  4110. if (first_element.second.type == CallInfo::Item::ITEM_TYPE_VARYING) {
  4111. const ShaderNode::Varying &varying = shader->varyings[first_element.first];
  4112. if (varying.stage == ShaderNode::Varying::STAGE_VERTEX) {
  4113. return true;
  4114. }
  4115. }
  4116. _set_tkpos(first_element.second.pos);
  4117. if (is_in_restricted_function) {
  4118. _set_error(vformat(RTR("'%s' cannot be used within the '%s' processor function."), first_element.first, "vertex"));
  4119. } else {
  4120. _set_error(vformat(RTR("'%s' cannot be used here, because '%s' is called by the '%s' processor function (which is not allowed)."), first_element.first, p_func_info->name, "vertex"));
  4121. }
  4122. return false;
  4123. }
  4124. return true;
  4125. }
  4126. bool ShaderLanguage::_validate_assign(Node *p_node, const FunctionInfo &p_function_info, String *r_message) {
  4127. if (p_node->type == Node::NODE_TYPE_OPERATOR) {
  4128. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  4129. if (op->op == OP_INDEX) {
  4130. return _validate_assign(op->arguments[0], p_function_info, r_message);
  4131. } else if (_is_operator_assign(op->op)) {
  4132. //chained assignment
  4133. return _validate_assign(op->arguments[1], p_function_info, r_message);
  4134. } else if (op->op == OP_CALL) {
  4135. if (r_message) {
  4136. *r_message = RTR("Assignment to function.");
  4137. }
  4138. return false;
  4139. }
  4140. } else if (p_node->type == Node::NODE_TYPE_MEMBER) {
  4141. MemberNode *member = static_cast<MemberNode *>(p_node);
  4142. if (member->has_swizzling_duplicates) {
  4143. if (r_message) {
  4144. *r_message = RTR("Swizzling assignment contains duplicates.");
  4145. }
  4146. return false;
  4147. }
  4148. return _validate_assign(member->owner, p_function_info, r_message);
  4149. } else if (p_node->type == Node::NODE_TYPE_VARIABLE) {
  4150. VariableNode *var = static_cast<VariableNode *>(p_node);
  4151. if (shader->uniforms.has(var->name)) {
  4152. if (r_message) {
  4153. *r_message = RTR("Assignment to uniform.");
  4154. }
  4155. return false;
  4156. }
  4157. if (shader->constants.has(var->name) || var->is_const) {
  4158. if (r_message) {
  4159. *r_message = RTR("Constants cannot be modified.");
  4160. }
  4161. return false;
  4162. }
  4163. if (shader->varyings.has(var->name)) {
  4164. return _validate_varying_assign(shader->varyings[var->name], r_message);
  4165. }
  4166. if (!(p_function_info.built_ins.has(var->name) && p_function_info.built_ins[var->name].constant)) {
  4167. return true;
  4168. }
  4169. } else if (p_node->type == Node::NODE_TYPE_ARRAY) {
  4170. ArrayNode *arr = static_cast<ArrayNode *>(p_node);
  4171. if (shader->constants.has(arr->name) || arr->is_const) {
  4172. if (r_message) {
  4173. *r_message = RTR("Constants cannot be modified.");
  4174. }
  4175. return false;
  4176. }
  4177. return true;
  4178. }
  4179. if (r_message) {
  4180. *r_message = "Assignment to constant expression.";
  4181. }
  4182. return false;
  4183. }
  4184. ShaderLanguage::ShaderNode::Uniform::Hint ShaderLanguage::_sanitize_hint(ShaderNode::Uniform::Hint p_hint) {
  4185. if (p_hint == ShaderNode::Uniform::HINT_SCREEN_TEXTURE ||
  4186. p_hint == ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE ||
  4187. p_hint == ShaderNode::Uniform::HINT_DEPTH_TEXTURE) {
  4188. return p_hint;
  4189. }
  4190. return ShaderNode::Uniform::HINT_NONE;
  4191. }
  4192. bool ShaderLanguage::_propagate_function_call_sampler_uniform_settings(const StringName &p_name, int p_argument, TextureFilter p_filter, TextureRepeat p_repeat, ShaderNode::Uniform::Hint p_hint) {
  4193. for (int i = 0; i < shader->vfunctions.size(); i++) {
  4194. if (shader->vfunctions[i].name == p_name) {
  4195. ERR_FAIL_INDEX_V(p_argument, shader->vfunctions[i].function->arguments.size(), false);
  4196. FunctionNode::Argument *arg = &shader->vfunctions[i].function->arguments.write[p_argument];
  4197. if (arg->tex_builtin_check) {
  4198. _set_error(vformat(RTR("Sampler argument %d of function '%s' called more than once using both built-ins and uniform textures, this is not supported (use either one or the other)."), p_argument, String(p_name)));
  4199. return false;
  4200. } else if (arg->tex_argument_check) {
  4201. // Was checked, verify that filter, repeat, and hint are the same.
  4202. if (arg->tex_argument_filter == p_filter && arg->tex_argument_repeat == p_repeat && arg->tex_hint == _sanitize_hint(p_hint)) {
  4203. return true;
  4204. } else {
  4205. _set_error(vformat(RTR("Sampler argument %d of function '%s' called more than once using textures that differ in either filter, repeat, or texture hint setting."), p_argument, String(p_name)));
  4206. return false;
  4207. }
  4208. } else {
  4209. arg->tex_argument_check = true;
  4210. arg->tex_argument_filter = p_filter;
  4211. arg->tex_argument_repeat = p_repeat;
  4212. arg->tex_hint = _sanitize_hint(p_hint);
  4213. for (KeyValue<StringName, HashSet<int>> &E : arg->tex_argument_connect) {
  4214. for (const int &F : E.value) {
  4215. if (!_propagate_function_call_sampler_uniform_settings(E.key, F, p_filter, p_repeat, p_hint)) {
  4216. return false;
  4217. }
  4218. }
  4219. }
  4220. return true;
  4221. }
  4222. }
  4223. }
  4224. ERR_FAIL_V(false); //bug? function not found
  4225. }
  4226. bool ShaderLanguage::_propagate_function_call_sampler_builtin_reference(const StringName &p_name, int p_argument, const StringName &p_builtin) {
  4227. for (int i = 0; i < shader->vfunctions.size(); i++) {
  4228. if (shader->vfunctions[i].name == p_name) {
  4229. ERR_FAIL_INDEX_V(p_argument, shader->vfunctions[i].function->arguments.size(), false);
  4230. FunctionNode::Argument *arg = &shader->vfunctions[i].function->arguments.write[p_argument];
  4231. if (arg->tex_argument_check) {
  4232. _set_error(vformat(RTR("Sampler argument %d of function '%s' called more than once using both built-ins and uniform textures, this is not supported (use either one or the other)."), p_argument, String(p_name)));
  4233. return false;
  4234. } else if (arg->tex_builtin_check) {
  4235. //was checked, verify that the built-in is the same
  4236. if (arg->tex_builtin == p_builtin) {
  4237. return true;
  4238. } else {
  4239. _set_error(vformat(RTR("Sampler argument %d of function '%s' called more than once using different built-ins. Only calling with the same built-in is supported."), p_argument, String(p_name)));
  4240. return false;
  4241. }
  4242. } else {
  4243. arg->tex_builtin_check = true;
  4244. arg->tex_builtin = p_builtin;
  4245. for (KeyValue<StringName, HashSet<int>> &E : arg->tex_argument_connect) {
  4246. for (const int &F : E.value) {
  4247. if (!_propagate_function_call_sampler_builtin_reference(E.key, F, p_builtin)) {
  4248. return false;
  4249. }
  4250. }
  4251. }
  4252. return true;
  4253. }
  4254. }
  4255. }
  4256. ERR_FAIL_V(false); //bug? function not found
  4257. }
  4258. Error ShaderLanguage::_parse_array_size(BlockNode *p_block, const FunctionInfo &p_function_info, bool p_forbid_unknown_size, Node **r_size_expression, int *r_array_size, bool *r_unknown_size) {
  4259. bool error = false;
  4260. if (r_array_size != nullptr && *r_array_size > 0) {
  4261. error = true;
  4262. }
  4263. if (r_unknown_size != nullptr && *r_unknown_size) {
  4264. error = true;
  4265. }
  4266. if (error) {
  4267. _set_error(vformat(RTR("Array size is already defined.")));
  4268. return ERR_PARSE_ERROR;
  4269. }
  4270. TkPos pos = _get_tkpos();
  4271. Token tk = _get_token();
  4272. if (tk.type == TK_BRACKET_CLOSE) {
  4273. if (p_forbid_unknown_size) {
  4274. _set_error(vformat(RTR("Unknown array size is forbidden in that context.")));
  4275. return ERR_PARSE_ERROR;
  4276. }
  4277. if (r_unknown_size != nullptr) {
  4278. *r_unknown_size = true;
  4279. }
  4280. } else {
  4281. int array_size = 0;
  4282. if (!tk.is_integer_constant() || ((int)tk.constant) <= 0) {
  4283. _set_tkpos(pos);
  4284. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  4285. if (n) {
  4286. if (n->type == Node::NODE_TYPE_VARIABLE) {
  4287. VariableNode *vn = static_cast<VariableNode *>(n);
  4288. if (vn) {
  4289. ConstantNode::Value v;
  4290. DataType data_type;
  4291. bool is_const = false;
  4292. _find_identifier(p_block, false, p_function_info, vn->name, &data_type, nullptr, &is_const, nullptr, nullptr, &v);
  4293. if (is_const) {
  4294. if (data_type == TYPE_INT) {
  4295. int32_t value = v.sint;
  4296. if (value > 0) {
  4297. array_size = value;
  4298. }
  4299. } else if (data_type == TYPE_UINT) {
  4300. uint32_t value = v.uint;
  4301. if (value > 0U) {
  4302. array_size = value;
  4303. }
  4304. }
  4305. }
  4306. }
  4307. } else if (n->type == Node::NODE_TYPE_OPERATOR) {
  4308. _set_error(vformat(RTR("Array size expressions are not supported.")));
  4309. return ERR_PARSE_ERROR;
  4310. }
  4311. if (r_size_expression != nullptr) {
  4312. *r_size_expression = n;
  4313. }
  4314. }
  4315. } else if (((int)tk.constant) > 0) {
  4316. array_size = (uint32_t)tk.constant;
  4317. }
  4318. if (array_size <= 0) {
  4319. _set_error(RTR("Expected a positive integer constant."));
  4320. return ERR_PARSE_ERROR;
  4321. }
  4322. tk = _get_token();
  4323. if (tk.type != TK_BRACKET_CLOSE) {
  4324. _set_expected_error("]");
  4325. return ERR_PARSE_ERROR;
  4326. }
  4327. if (r_array_size != nullptr) {
  4328. *r_array_size = array_size;
  4329. }
  4330. }
  4331. return OK;
  4332. }
  4333. ShaderLanguage::Node *ShaderLanguage::_parse_array_constructor(BlockNode *p_block, const FunctionInfo &p_function_info) {
  4334. DataType type = TYPE_VOID;
  4335. String struct_name = "";
  4336. int array_size = 0;
  4337. bool auto_size = false;
  4338. bool undefined_size = false;
  4339. Token tk = _get_token();
  4340. if (tk.type == TK_CURLY_BRACKET_OPEN) {
  4341. auto_size = true;
  4342. } else {
  4343. if (shader->structs.has(tk.text)) {
  4344. type = TYPE_STRUCT;
  4345. struct_name = tk.text;
  4346. } else {
  4347. if (!is_token_variable_datatype(tk.type)) {
  4348. _set_error(RTR("Invalid data type for the array."));
  4349. return nullptr;
  4350. }
  4351. type = get_token_datatype(tk.type);
  4352. }
  4353. tk = _get_token();
  4354. if (tk.type == TK_BRACKET_OPEN) {
  4355. Error error = _parse_array_size(p_block, p_function_info, false, nullptr, &array_size, &undefined_size);
  4356. if (error != OK) {
  4357. return nullptr;
  4358. }
  4359. tk = _get_token();
  4360. } else {
  4361. _set_expected_error("[");
  4362. return nullptr;
  4363. }
  4364. }
  4365. ArrayConstructNode *an = alloc_node<ArrayConstructNode>();
  4366. if (tk.type == TK_PARENTHESIS_OPEN || auto_size) { // initialization
  4367. int idx = 0;
  4368. while (true) {
  4369. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  4370. if (!n) {
  4371. return nullptr;
  4372. }
  4373. // define type by using the first member
  4374. if (auto_size && idx == 0) {
  4375. type = n->get_datatype();
  4376. if (type == TYPE_STRUCT) {
  4377. struct_name = n->get_datatype_name();
  4378. }
  4379. } else {
  4380. if (!_compare_datatypes(type, struct_name, 0, n->get_datatype(), n->get_datatype_name(), 0)) {
  4381. return nullptr;
  4382. }
  4383. }
  4384. tk = _get_token();
  4385. if (tk.type == TK_COMMA) {
  4386. an->initializer.push_back(n);
  4387. } else if (!auto_size && tk.type == TK_PARENTHESIS_CLOSE) {
  4388. an->initializer.push_back(n);
  4389. break;
  4390. } else if (auto_size && tk.type == TK_CURLY_BRACKET_CLOSE) {
  4391. an->initializer.push_back(n);
  4392. break;
  4393. } else {
  4394. if (auto_size) {
  4395. _set_expected_error("}", ",");
  4396. } else {
  4397. _set_expected_error(")", ",");
  4398. }
  4399. return nullptr;
  4400. }
  4401. idx++;
  4402. }
  4403. if (!auto_size && !undefined_size && an->initializer.size() != array_size) {
  4404. _set_error(RTR("Array size mismatch."));
  4405. return nullptr;
  4406. }
  4407. } else {
  4408. _set_error(RTR("Expected array initialization."));
  4409. return nullptr;
  4410. }
  4411. an->datatype = type;
  4412. an->struct_name = struct_name;
  4413. return an;
  4414. }
  4415. 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) {
  4416. DataType type = TYPE_VOID;
  4417. String struct_name = "";
  4418. int array_size = 0;
  4419. bool auto_size = false;
  4420. TkPos prev_pos = _get_tkpos();
  4421. Token tk = _get_token();
  4422. if (tk.type == TK_CURLY_BRACKET_OPEN) {
  4423. auto_size = true;
  4424. } else {
  4425. if (shader->structs.has(tk.text)) {
  4426. type = TYPE_STRUCT;
  4427. struct_name = tk.text;
  4428. } else {
  4429. if (!is_token_variable_datatype(tk.type)) {
  4430. _set_tkpos(prev_pos);
  4431. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  4432. if (!n) {
  4433. _set_error(RTR("Invalid data type for the array."));
  4434. return nullptr;
  4435. }
  4436. if (!_compare_datatypes(p_type, p_struct_name, p_array_size, n->get_datatype(), n->get_datatype_name(), n->get_array_size())) {
  4437. return nullptr;
  4438. }
  4439. return n;
  4440. }
  4441. type = get_token_datatype(tk.type);
  4442. }
  4443. tk = _get_token();
  4444. if (tk.type == TK_BRACKET_OPEN) {
  4445. bool is_unknown_size = false;
  4446. Error error = _parse_array_size(p_block, p_function_info, false, nullptr, &array_size, &is_unknown_size);
  4447. if (error != OK) {
  4448. return nullptr;
  4449. }
  4450. if (is_unknown_size) {
  4451. array_size = p_array_size;
  4452. }
  4453. tk = _get_token();
  4454. } else {
  4455. _set_expected_error("[");
  4456. return nullptr;
  4457. }
  4458. if (type != p_type || struct_name != p_struct_name || array_size != p_array_size) {
  4459. String from;
  4460. if (type == TYPE_STRUCT) {
  4461. from += struct_name;
  4462. } else {
  4463. from += get_datatype_name(type);
  4464. }
  4465. from += "[";
  4466. from += itos(array_size);
  4467. from += "]'";
  4468. String to;
  4469. if (type == TYPE_STRUCT) {
  4470. to += p_struct_name;
  4471. } else {
  4472. to += get_datatype_name(p_type);
  4473. }
  4474. to += "[";
  4475. to += itos(p_array_size);
  4476. to += "]'";
  4477. _set_error(vformat(RTR("Cannot convert from '%s' to '%s'."), from, to));
  4478. return nullptr;
  4479. }
  4480. }
  4481. ArrayConstructNode *an = alloc_node<ArrayConstructNode>();
  4482. an->datatype = p_type;
  4483. an->struct_name = p_struct_name;
  4484. if (tk.type == TK_PARENTHESIS_OPEN || auto_size) { // initialization
  4485. while (true) {
  4486. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  4487. if (!n) {
  4488. return nullptr;
  4489. }
  4490. if (!_compare_datatypes(p_type, p_struct_name, 0, n->get_datatype(), n->get_datatype_name(), n->get_array_size())) {
  4491. return nullptr;
  4492. }
  4493. tk = _get_token();
  4494. if (tk.type == TK_COMMA) {
  4495. an->initializer.push_back(n);
  4496. } else if (!auto_size && tk.type == TK_PARENTHESIS_CLOSE) {
  4497. an->initializer.push_back(n);
  4498. break;
  4499. } else if (auto_size && tk.type == TK_CURLY_BRACKET_CLOSE) {
  4500. an->initializer.push_back(n);
  4501. break;
  4502. } else {
  4503. if (auto_size) {
  4504. _set_expected_error("}", ",");
  4505. } else {
  4506. _set_expected_error(")", ",");
  4507. }
  4508. return nullptr;
  4509. }
  4510. }
  4511. if (an->initializer.size() != p_array_size) {
  4512. _set_error(RTR("Array size mismatch."));
  4513. return nullptr;
  4514. }
  4515. } else {
  4516. _set_error(RTR("Expected array initialization."));
  4517. return nullptr;
  4518. }
  4519. return an;
  4520. }
  4521. ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, const FunctionInfo &p_function_info) {
  4522. Vector<Expression> expression;
  4523. //Vector<TokenType> operators;
  4524. #ifdef DEBUG_ENABLED
  4525. bool check_position_write = check_warnings && HAS_WARNING(ShaderWarning::MAGIC_POSITION_WRITE_FLAG);
  4526. check_position_write = check_position_write && String(shader_type_identifier) == "spatial" && current_function == "vertex";
  4527. #endif
  4528. while (true) {
  4529. Node *expr = nullptr;
  4530. TkPos prepos = _get_tkpos();
  4531. Token tk = _get_token();
  4532. TkPos pos = _get_tkpos();
  4533. bool is_const = false;
  4534. if (tk.type == TK_PARENTHESIS_OPEN) {
  4535. //handle subexpression
  4536. expr = _parse_and_reduce_expression(p_block, p_function_info);
  4537. if (!expr) {
  4538. return nullptr;
  4539. }
  4540. tk = _get_token();
  4541. if (tk.type != TK_PARENTHESIS_CLOSE) {
  4542. _set_error(RTR("Expected ')' in expression."));
  4543. return nullptr;
  4544. }
  4545. } else if (tk.type == TK_FLOAT_CONSTANT) {
  4546. ConstantNode *constant = alloc_node<ConstantNode>();
  4547. ConstantNode::Value v;
  4548. v.real = tk.constant;
  4549. constant->values.push_back(v);
  4550. constant->datatype = TYPE_FLOAT;
  4551. expr = constant;
  4552. } else if (tk.type == TK_INT_CONSTANT) {
  4553. ConstantNode *constant = alloc_node<ConstantNode>();
  4554. ConstantNode::Value v;
  4555. v.sint = tk.constant;
  4556. constant->values.push_back(v);
  4557. constant->datatype = TYPE_INT;
  4558. expr = constant;
  4559. } else if (tk.type == TK_UINT_CONSTANT) {
  4560. ConstantNode *constant = alloc_node<ConstantNode>();
  4561. ConstantNode::Value v;
  4562. v.uint = tk.constant;
  4563. constant->values.push_back(v);
  4564. constant->datatype = TYPE_UINT;
  4565. expr = constant;
  4566. } else if (tk.type == TK_TRUE) {
  4567. //handle true constant
  4568. ConstantNode *constant = alloc_node<ConstantNode>();
  4569. ConstantNode::Value v;
  4570. v.boolean = true;
  4571. constant->values.push_back(v);
  4572. constant->datatype = TYPE_BOOL;
  4573. expr = constant;
  4574. } else if (tk.type == TK_FALSE) {
  4575. //handle false constant
  4576. ConstantNode *constant = alloc_node<ConstantNode>();
  4577. ConstantNode::Value v;
  4578. v.boolean = false;
  4579. constant->values.push_back(v);
  4580. constant->datatype = TYPE_BOOL;
  4581. expr = constant;
  4582. } else if (tk.type == TK_TYPE_VOID) {
  4583. //make sure void is not used in expression
  4584. _set_error(RTR("Void value not allowed in expression."));
  4585. return nullptr;
  4586. } else if (is_token_nonvoid_datatype(tk.type) || tk.type == TK_CURLY_BRACKET_OPEN) {
  4587. if (tk.type == TK_CURLY_BRACKET_OPEN) {
  4588. //array constructor
  4589. _set_tkpos(prepos);
  4590. expr = _parse_array_constructor(p_block, p_function_info);
  4591. } else {
  4592. DataType datatype;
  4593. DataPrecision precision = PRECISION_DEFAULT;
  4594. if (is_token_precision(tk.type)) {
  4595. precision = get_token_precision(tk.type);
  4596. tk = _get_token();
  4597. }
  4598. datatype = get_token_datatype(tk.type);
  4599. if (precision != PRECISION_DEFAULT && _validate_precision(datatype, precision) != OK) {
  4600. return nullptr;
  4601. }
  4602. tk = _get_token();
  4603. if (tk.type == TK_BRACKET_OPEN) {
  4604. //array constructor
  4605. _set_tkpos(prepos);
  4606. expr = _parse_array_constructor(p_block, p_function_info);
  4607. } else {
  4608. if (tk.type != TK_PARENTHESIS_OPEN) {
  4609. _set_error(RTR("Expected '(' after the type name."));
  4610. return nullptr;
  4611. }
  4612. //basic type constructor
  4613. OperatorNode *func = alloc_node<OperatorNode>();
  4614. func->op = OP_CONSTRUCT;
  4615. if (precision != PRECISION_DEFAULT) {
  4616. func->return_precision_cache = precision;
  4617. }
  4618. VariableNode *funcname = alloc_node<VariableNode>();
  4619. funcname->name = get_datatype_name(datatype);
  4620. func->arguments.push_back(funcname);
  4621. int carg = -1;
  4622. bool ok = _parse_function_arguments(p_block, p_function_info, func, &carg);
  4623. if (carg >= 0) {
  4624. completion_type = COMPLETION_CALL_ARGUMENTS;
  4625. completion_line = tk_line;
  4626. completion_block = p_block;
  4627. completion_function = funcname->name;
  4628. completion_argument = carg;
  4629. }
  4630. if (!ok) {
  4631. return nullptr;
  4632. }
  4633. if (!_validate_function_call(p_block, p_function_info, func, &func->return_cache, &func->struct_name)) {
  4634. _set_error(vformat(RTR("No matching constructor found for: '%s'."), String(funcname->name)));
  4635. return nullptr;
  4636. }
  4637. expr = _reduce_expression(p_block, func);
  4638. }
  4639. }
  4640. } else if (tk.type == TK_IDENTIFIER) {
  4641. _set_tkpos(prepos);
  4642. StringName identifier;
  4643. StructNode *pstruct = nullptr;
  4644. bool struct_init = false;
  4645. _get_completable_identifier(p_block, COMPLETION_IDENTIFIER, identifier);
  4646. if (shader->structs.has(identifier)) {
  4647. pstruct = shader->structs[identifier].shader_struct;
  4648. struct_init = true;
  4649. }
  4650. tk = _get_token();
  4651. if (tk.type == TK_PARENTHESIS_OPEN) {
  4652. if (struct_init) { //a struct constructor
  4653. const StringName &name = identifier;
  4654. OperatorNode *func = alloc_node<OperatorNode>();
  4655. func->op = OP_STRUCT;
  4656. func->struct_name = name;
  4657. func->return_cache = TYPE_STRUCT;
  4658. VariableNode *funcname = alloc_node<VariableNode>();
  4659. funcname->name = name;
  4660. func->arguments.push_back(funcname);
  4661. for (List<ShaderLanguage::MemberNode *>::Element *E = pstruct->members.front(); E; E = E->next()) {
  4662. Node *nexpr;
  4663. if (E->get()->array_size != 0) {
  4664. nexpr = _parse_array_constructor(p_block, p_function_info, E->get()->get_datatype(), E->get()->struct_name, E->get()->array_size);
  4665. if (!nexpr) {
  4666. return nullptr;
  4667. }
  4668. } else {
  4669. nexpr = _parse_and_reduce_expression(p_block, p_function_info);
  4670. if (!nexpr) {
  4671. return nullptr;
  4672. }
  4673. if (!_compare_datatypes_in_nodes(E->get(), nexpr)) {
  4674. return nullptr;
  4675. }
  4676. }
  4677. if (E->next()) {
  4678. tk = _get_token();
  4679. if (tk.type != TK_COMMA) {
  4680. _set_expected_error(",");
  4681. return nullptr;
  4682. }
  4683. }
  4684. func->arguments.push_back(nexpr);
  4685. }
  4686. tk = _get_token();
  4687. if (tk.type != TK_PARENTHESIS_CLOSE) {
  4688. _set_expected_error(")");
  4689. return nullptr;
  4690. }
  4691. expr = func;
  4692. } else { //a function call
  4693. // Non-builtin function call is forbidden for constant declaration.
  4694. if (is_const_decl) {
  4695. if (shader->functions.has(identifier)) {
  4696. _set_error(RTR("Expected constant expression."));
  4697. return nullptr;
  4698. }
  4699. }
  4700. const StringName &name = identifier;
  4701. if (name != current_function) { // Recursion is not allowed.
  4702. // Register call.
  4703. if (calls_info.has(name)) {
  4704. calls_info[current_function].calls.push_back(&calls_info[name]);
  4705. }
  4706. int idx = 0;
  4707. bool is_builtin = false;
  4708. while (frag_only_func_defs[idx].name) {
  4709. if (frag_only_func_defs[idx].name == name) {
  4710. // If a built-in function not found for the current shader type, then it shouldn't be parsed further.
  4711. if (!is_supported_frag_only_funcs) {
  4712. _set_error(vformat(RTR("Built-in function '%s' is not supported for the '%s' shader type."), name, shader_type_identifier));
  4713. return nullptr;
  4714. }
  4715. // Register usage of the restricted function.
  4716. calls_info[current_function].uses_restricted_items.push_back(Pair<StringName, CallInfo::Item>(name, CallInfo::Item(CallInfo::Item::ITEM_TYPE_BUILTIN, _get_tkpos())));
  4717. is_builtin = true;
  4718. break;
  4719. }
  4720. idx++;
  4721. }
  4722. // Recursively checks for the restricted function call.
  4723. if (is_supported_frag_only_funcs && current_function == "vertex" && stages->has(current_function) && !_validate_restricted_func(name, &calls_info[current_function], is_builtin)) {
  4724. return nullptr;
  4725. }
  4726. }
  4727. OperatorNode *func = alloc_node<OperatorNode>();
  4728. func->op = OP_CALL;
  4729. VariableNode *funcname = alloc_node<VariableNode>();
  4730. funcname->name = name;
  4731. func->arguments.push_back(funcname);
  4732. int carg = -1;
  4733. bool ok = _parse_function_arguments(p_block, p_function_info, func, &carg);
  4734. // Check if block has a variable with the same name as function to prevent shader crash.
  4735. ShaderLanguage::BlockNode *bnode = p_block;
  4736. while (bnode) {
  4737. if (bnode->variables.has(name)) {
  4738. _set_error(RTR("Expected a function name."));
  4739. return nullptr;
  4740. }
  4741. bnode = bnode->parent_block;
  4742. }
  4743. //test if function was parsed first
  4744. int function_index = -1;
  4745. for (int i = 0; i < shader->vfunctions.size(); i++) {
  4746. if (shader->vfunctions[i].name == name) {
  4747. //add to current function as dependency
  4748. for (int j = 0; j < shader->vfunctions.size(); j++) {
  4749. if (shader->vfunctions[j].name == current_function) {
  4750. shader->vfunctions.write[j].uses_function.insert(name);
  4751. break;
  4752. }
  4753. }
  4754. //see if texture arguments must connect
  4755. function_index = i;
  4756. break;
  4757. }
  4758. }
  4759. if (carg >= 0) {
  4760. completion_type = COMPLETION_CALL_ARGUMENTS;
  4761. completion_line = tk_line;
  4762. completion_block = p_block;
  4763. completion_function = funcname->name;
  4764. completion_argument = carg;
  4765. }
  4766. if (!ok) {
  4767. return nullptr;
  4768. }
  4769. bool is_custom_func = false;
  4770. if (!_validate_function_call(p_block, p_function_info, func, &func->return_cache, &func->struct_name, &is_custom_func)) {
  4771. _set_error(vformat(RTR("No matching function found for: '%s'."), String(funcname->name)));
  4772. return nullptr;
  4773. }
  4774. completion_class = TAG_GLOBAL; // reset sub-class
  4775. if (function_index >= 0) {
  4776. //connect texture arguments, so we can cache in the
  4777. //argument what type of filter and repeat to use
  4778. FunctionNode *call_function = shader->vfunctions[function_index].function;
  4779. if (call_function) {
  4780. func->return_cache = call_function->get_datatype();
  4781. func->struct_name = call_function->get_datatype_name();
  4782. func->return_array_size = call_function->get_array_size();
  4783. //get current base function
  4784. FunctionNode *base_function = nullptr;
  4785. {
  4786. BlockNode *b = p_block;
  4787. while (b) {
  4788. if (b->parent_function) {
  4789. base_function = b->parent_function;
  4790. break;
  4791. } else {
  4792. b = b->parent_block;
  4793. }
  4794. }
  4795. }
  4796. ERR_FAIL_NULL_V(base_function, nullptr); // Bug, wtf.
  4797. for (int i = 0; i < call_function->arguments.size(); i++) {
  4798. int argidx = i + 1;
  4799. if (argidx < func->arguments.size()) {
  4800. bool error = false;
  4801. Node *n = func->arguments[argidx];
  4802. ArgumentQualifier arg_qual = call_function->arguments[i].qualifier;
  4803. bool is_out_arg = arg_qual != ArgumentQualifier::ARGUMENT_QUALIFIER_IN;
  4804. if (n->type == Node::NODE_TYPE_VARIABLE || n->type == Node::NODE_TYPE_ARRAY) {
  4805. StringName varname;
  4806. if (n->type == Node::NODE_TYPE_VARIABLE) {
  4807. VariableNode *vn = static_cast<VariableNode *>(n);
  4808. varname = vn->name;
  4809. } else { // TYPE_ARRAY
  4810. ArrayNode *an = static_cast<ArrayNode *>(n);
  4811. varname = an->name;
  4812. }
  4813. if (shader->varyings.has(varname)) {
  4814. switch (shader->varyings[varname].stage) {
  4815. case ShaderNode::Varying::STAGE_UNKNOWN:
  4816. if (is_out_arg) {
  4817. error = true;
  4818. }
  4819. break;
  4820. case ShaderNode::Varying::STAGE_VERTEX_TO_FRAGMENT_LIGHT:
  4821. [[fallthrough]];
  4822. case ShaderNode::Varying::STAGE_VERTEX:
  4823. if (is_out_arg && current_function != varying_function_names.vertex) { // inout/out
  4824. error = true;
  4825. }
  4826. break;
  4827. case ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT:
  4828. [[fallthrough]];
  4829. case ShaderNode::Varying::STAGE_FRAGMENT:
  4830. if (!is_out_arg) {
  4831. if (current_function != varying_function_names.fragment && current_function != varying_function_names.light) {
  4832. error = true;
  4833. }
  4834. } else if (current_function != varying_function_names.fragment) { // inout/out
  4835. error = true;
  4836. }
  4837. break;
  4838. default:
  4839. break;
  4840. }
  4841. if (error) {
  4842. _set_error(vformat(RTR("Varying '%s' cannot be passed for the '%s' parameter in that context."), varname, _get_qualifier_str(arg_qual)));
  4843. return nullptr;
  4844. }
  4845. }
  4846. }
  4847. bool is_const_arg = call_function->arguments[i].is_const;
  4848. if (is_const_arg || is_out_arg) {
  4849. StringName varname;
  4850. if (n->type == Node::NODE_TYPE_CONSTANT || n->type == Node::NODE_TYPE_OPERATOR || n->type == Node::NODE_TYPE_ARRAY_CONSTRUCT) {
  4851. if (!is_const_arg) {
  4852. error = true;
  4853. }
  4854. } else if (n->type == Node::NODE_TYPE_ARRAY) {
  4855. ArrayNode *an = static_cast<ArrayNode *>(n);
  4856. if (!is_const_arg && (an->call_expression != nullptr || an->is_const)) {
  4857. error = true;
  4858. }
  4859. varname = an->name;
  4860. } else if (n->type == Node::NODE_TYPE_VARIABLE) {
  4861. VariableNode *vn = static_cast<VariableNode *>(n);
  4862. if (vn->is_const && !is_const_arg) {
  4863. error = true;
  4864. }
  4865. varname = vn->name;
  4866. } else if (n->type == Node::NODE_TYPE_MEMBER) {
  4867. MemberNode *mn = static_cast<MemberNode *>(n);
  4868. if (mn->basetype_const && is_out_arg) {
  4869. error = true;
  4870. }
  4871. }
  4872. if (!error && varname != StringName()) {
  4873. if (shader->constants.has(varname)) {
  4874. error = true;
  4875. } else if (shader->uniforms.has(varname)) {
  4876. error = true;
  4877. } else if (p_function_info.built_ins.has(varname)) {
  4878. BuiltInInfo info = p_function_info.built_ins[varname];
  4879. if (info.constant) {
  4880. error = true;
  4881. }
  4882. }
  4883. }
  4884. if (error) {
  4885. _set_error(vformat(RTR("A constant value cannot be passed for the '%s' parameter."), _get_qualifier_str(arg_qual)));
  4886. return nullptr;
  4887. }
  4888. }
  4889. if (is_sampler_type(call_function->arguments[i].type)) {
  4890. // Let's see where our argument comes from.
  4891. StringName varname;
  4892. if (n->type == Node::NODE_TYPE_VARIABLE) {
  4893. VariableNode *vn = static_cast<VariableNode *>(n);
  4894. varname = vn->name;
  4895. } else if (n->type == Node::NODE_TYPE_ARRAY) {
  4896. ArrayNode *an = static_cast<ArrayNode *>(n);
  4897. varname = an->name;
  4898. }
  4899. if (shader->uniforms.has(varname)) {
  4900. //being sampler, this either comes from a uniform
  4901. ShaderNode::Uniform *u = &shader->uniforms[varname];
  4902. ERR_CONTINUE(u->type != call_function->arguments[i].type); //this should have been validated previously
  4903. if (RendererCompositor::get_singleton()->is_xr_enabled() && is_custom_func) {
  4904. ShaderNode::Uniform::Hint hint = u->hint;
  4905. if (hint == ShaderNode::Uniform::HINT_DEPTH_TEXTURE || hint == ShaderNode::Uniform::HINT_SCREEN_TEXTURE || hint == ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE) {
  4906. _set_error(vformat(RTR("Unable to pass a multiview texture sampler as a parameter to custom function. Consider to sample it in the main function and then pass the vector result to it."), get_uniform_hint_name(hint)));
  4907. return nullptr;
  4908. }
  4909. }
  4910. //propagate
  4911. if (!_propagate_function_call_sampler_uniform_settings(name, i, u->filter, u->repeat, u->hint)) {
  4912. return nullptr;
  4913. }
  4914. } else if (p_function_info.built_ins.has(varname)) {
  4915. //a built-in
  4916. if (!_propagate_function_call_sampler_builtin_reference(name, i, varname)) {
  4917. return nullptr;
  4918. }
  4919. } else {
  4920. //or this comes from an argument, but nothing else can be a sampler
  4921. bool found = false;
  4922. for (int j = 0; j < base_function->arguments.size(); j++) {
  4923. if (base_function->arguments[j].name == varname) {
  4924. if (!base_function->arguments[j].tex_argument_connect.has(call_function->name)) {
  4925. base_function->arguments.write[j].tex_argument_connect[call_function->name] = HashSet<int>();
  4926. }
  4927. base_function->arguments.write[j].tex_argument_connect[call_function->name].insert(i);
  4928. found = true;
  4929. break;
  4930. }
  4931. }
  4932. ERR_CONTINUE(!found);
  4933. }
  4934. }
  4935. } else {
  4936. break;
  4937. }
  4938. }
  4939. }
  4940. }
  4941. expr = func;
  4942. #ifdef DEBUG_ENABLED
  4943. if (check_warnings) {
  4944. StringName func_name;
  4945. if (p_block && p_block->parent_function) {
  4946. func_name = p_block->parent_function->name;
  4947. }
  4948. _parse_used_identifier(name, IdentifierType::IDENTIFIER_FUNCTION, func_name);
  4949. }
  4950. #endif // DEBUG_ENABLED
  4951. }
  4952. } else {
  4953. //an identifier
  4954. last_name = identifier;
  4955. last_type = IDENTIFIER_MAX;
  4956. _set_tkpos(pos);
  4957. DataType data_type = TYPE_MAX;
  4958. IdentifierType ident_type = IDENTIFIER_MAX;
  4959. int array_size = 0;
  4960. StringName struct_name;
  4961. bool is_local = false;
  4962. if (p_block && p_block->block_tag != SubClassTag::TAG_GLOBAL) {
  4963. int idx = 0;
  4964. bool found = false;
  4965. while (builtin_func_defs[idx].name) {
  4966. if (builtin_func_defs[idx].tag == p_block->block_tag && builtin_func_defs[idx].name == identifier) {
  4967. found = true;
  4968. break;
  4969. }
  4970. idx++;
  4971. }
  4972. if (!found) {
  4973. _set_error(vformat(RTR("Unknown identifier in expression: '%s'."), String(identifier)));
  4974. return nullptr;
  4975. }
  4976. } else {
  4977. if (!_find_identifier(p_block, false, p_function_info, identifier, &data_type, &ident_type, &is_const, &array_size, &struct_name)) {
  4978. if (identifier == "SCREEN_TEXTURE" || identifier == "DEPTH_TEXTURE" || identifier == "NORMAL_ROUGHNESS_TEXTURE") {
  4979. String name = String(identifier);
  4980. String name_lower = name.to_lower();
  4981. _set_error(vformat(RTR("%s has been removed in favor of using hint_%s with a uniform.\nTo continue with minimal code changes add 'uniform sampler2D %s : hint_%s, filter_linear_mipmap;' near the top of your shader."), name, name_lower, name, name_lower));
  4982. return nullptr;
  4983. }
  4984. _set_error(vformat(RTR("Unknown identifier in expression: '%s'."), String(identifier)));
  4985. return nullptr;
  4986. }
  4987. if (is_const_decl && !is_const) {
  4988. _set_error(RTR("Expected constant expression."));
  4989. return nullptr;
  4990. }
  4991. if (ident_type == IDENTIFIER_VARYING) {
  4992. TkPos prev_pos = _get_tkpos();
  4993. Token next_token = _get_token();
  4994. // An array of varyings.
  4995. if (next_token.type == TK_BRACKET_OPEN) {
  4996. _get_token(); // Pass constant.
  4997. _get_token(); // Pass TK_BRACKET_CLOSE.
  4998. next_token = _get_token();
  4999. }
  5000. _set_tkpos(prev_pos);
  5001. ShaderNode::Varying &var = shader->varyings[identifier];
  5002. calls_info[current_function].uses_restricted_items.push_back(Pair<StringName, CallInfo::Item>(identifier, CallInfo::Item(CallInfo::Item::ITEM_TYPE_VARYING, prev_pos)));
  5003. String error;
  5004. if (is_token_operator_assign(next_token.type)) {
  5005. if (!_validate_varying_assign(shader->varyings[identifier], &error)) {
  5006. _set_error(error);
  5007. return nullptr;
  5008. }
  5009. } else {
  5010. switch (var.stage) {
  5011. case ShaderNode::Varying::STAGE_UNKNOWN: {
  5012. if (var.type < TYPE_INT) {
  5013. if (current_function == varying_function_names.vertex) {
  5014. _set_error(vformat(RTR("Varying with '%s' data type may only be used in the 'fragment' function."), get_datatype_name(var.type)));
  5015. } else {
  5016. _set_error(vformat(RTR("Varying '%s' must be assigned in the 'fragment' function first."), identifier));
  5017. }
  5018. return nullptr;
  5019. }
  5020. } break;
  5021. case ShaderNode::Varying::STAGE_VERTEX:
  5022. if (current_function == varying_function_names.fragment || current_function == varying_function_names.light) {
  5023. var.stage = ShaderNode::Varying::STAGE_VERTEX_TO_FRAGMENT_LIGHT;
  5024. }
  5025. break;
  5026. case ShaderNode::Varying::STAGE_FRAGMENT:
  5027. if (current_function == varying_function_names.light) {
  5028. var.stage = ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT;
  5029. }
  5030. break;
  5031. default:
  5032. break;
  5033. }
  5034. }
  5035. if ((var.stage != ShaderNode::Varying::STAGE_FRAGMENT && var.stage != ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT) && var.type < TYPE_FLOAT && var.interpolation != INTERPOLATION_FLAT) {
  5036. _set_tkpos(var.tkpos);
  5037. _set_error(RTR("Varying with integer data type must be declared with `flat` interpolation qualifier."));
  5038. return nullptr;
  5039. }
  5040. }
  5041. if (ident_type == IDENTIFIER_FUNCTION) {
  5042. _set_error(vformat(RTR("Can't use function as identifier: '%s'."), String(identifier)));
  5043. return nullptr;
  5044. }
  5045. #ifdef DEBUG_ENABLED
  5046. if (check_position_write && ident_type == IDENTIFIER_BUILTIN_VAR) {
  5047. if (String(identifier) == "POSITION") {
  5048. // Check if the user wrote "POSITION = vec4(VERTEX," and warn if they did.
  5049. TkPos prev_pos = _get_tkpos();
  5050. if (_get_token().type == TK_OP_ASSIGN &&
  5051. _get_token().type == TK_TYPE_VEC4 &&
  5052. _get_token().type == TK_PARENTHESIS_OPEN &&
  5053. _get_token().text == "VERTEX" &&
  5054. _get_token().type == TK_COMMA) {
  5055. _add_line_warning(ShaderWarning::MAGIC_POSITION_WRITE);
  5056. }
  5057. // Reset the position so compiling can continue as normal.
  5058. _set_tkpos(prev_pos);
  5059. }
  5060. }
  5061. #endif
  5062. if (is_const) {
  5063. last_type = IDENTIFIER_CONSTANT;
  5064. } else {
  5065. last_type = ident_type;
  5066. }
  5067. is_local = ident_type == IDENTIFIER_LOCAL_VAR || ident_type == IDENTIFIER_FUNCTION_ARGUMENT;
  5068. }
  5069. Node *index_expression = nullptr;
  5070. Node *call_expression = nullptr;
  5071. Node *assign_expression = nullptr;
  5072. if (array_size > 0) {
  5073. prepos = _get_tkpos();
  5074. tk = _get_token();
  5075. if (tk.type == TK_OP_ASSIGN) {
  5076. if (is_const) {
  5077. _set_error(RTR("Constants cannot be modified."));
  5078. return nullptr;
  5079. }
  5080. assign_expression = _parse_array_constructor(p_block, p_function_info, data_type, struct_name, array_size);
  5081. if (!assign_expression) {
  5082. return nullptr;
  5083. }
  5084. } else if (tk.type == TK_PERIOD) {
  5085. completion_class = TAG_ARRAY;
  5086. if (p_block != nullptr) {
  5087. p_block->block_tag = SubClassTag::TAG_ARRAY;
  5088. }
  5089. call_expression = _parse_and_reduce_expression(p_block, p_function_info);
  5090. if (p_block != nullptr) {
  5091. p_block->block_tag = SubClassTag::TAG_GLOBAL;
  5092. }
  5093. if (!call_expression) {
  5094. return nullptr;
  5095. }
  5096. } else if (tk.type == TK_BRACKET_OPEN) { // indexing
  5097. index_expression = _parse_and_reduce_expression(p_block, p_function_info);
  5098. if (!index_expression) {
  5099. return nullptr;
  5100. }
  5101. if (index_expression->get_array_size() != 0 || (index_expression->get_datatype() != TYPE_INT && index_expression->get_datatype() != TYPE_UINT)) {
  5102. _set_error(RTR("Only integer expressions are allowed for indexing."));
  5103. return nullptr;
  5104. }
  5105. if (index_expression->type == Node::NODE_TYPE_CONSTANT) {
  5106. ConstantNode *cnode = static_cast<ConstantNode *>(index_expression);
  5107. if (cnode) {
  5108. if (!cnode->values.is_empty()) {
  5109. int value = cnode->values[0].sint;
  5110. if (value < 0 || value >= array_size) {
  5111. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), value, 0, array_size - 1));
  5112. return nullptr;
  5113. }
  5114. }
  5115. }
  5116. }
  5117. tk = _get_token();
  5118. if (tk.type != TK_BRACKET_CLOSE) {
  5119. _set_expected_error("]");
  5120. return nullptr;
  5121. }
  5122. } else {
  5123. _set_tkpos(prepos);
  5124. }
  5125. ArrayNode *arrname = alloc_node<ArrayNode>();
  5126. arrname->name = identifier;
  5127. arrname->datatype_cache = data_type;
  5128. arrname->struct_name = struct_name;
  5129. arrname->index_expression = index_expression;
  5130. arrname->call_expression = call_expression;
  5131. arrname->assign_expression = assign_expression;
  5132. arrname->is_const = is_const;
  5133. arrname->array_size = array_size;
  5134. arrname->is_local = is_local;
  5135. expr = arrname;
  5136. } else {
  5137. VariableNode *varname = alloc_node<VariableNode>();
  5138. varname->name = identifier;
  5139. varname->datatype_cache = data_type;
  5140. varname->is_const = is_const;
  5141. varname->struct_name = struct_name;
  5142. varname->is_local = is_local;
  5143. expr = varname;
  5144. }
  5145. #ifdef DEBUG_ENABLED
  5146. if (check_warnings) {
  5147. StringName func_name;
  5148. BlockNode *b = p_block;
  5149. while (b) {
  5150. if (b->parent_function) {
  5151. func_name = b->parent_function->name;
  5152. break;
  5153. } else {
  5154. b = b->parent_block;
  5155. }
  5156. }
  5157. _parse_used_identifier(identifier, ident_type, func_name);
  5158. }
  5159. #endif // DEBUG_ENABLED
  5160. }
  5161. } else if (tk.type == TK_OP_ADD) {
  5162. continue; //this one does nothing
  5163. } 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) {
  5164. Expression e;
  5165. e.is_op = true;
  5166. switch (tk.type) {
  5167. case TK_OP_SUB:
  5168. e.op = OP_NEGATE;
  5169. break;
  5170. case TK_OP_NOT:
  5171. e.op = OP_NOT;
  5172. break;
  5173. case TK_OP_BIT_INVERT:
  5174. e.op = OP_BIT_INVERT;
  5175. break;
  5176. case TK_OP_INCREMENT:
  5177. e.op = OP_INCREMENT;
  5178. break;
  5179. case TK_OP_DECREMENT:
  5180. e.op = OP_DECREMENT;
  5181. break;
  5182. default:
  5183. ERR_FAIL_V(nullptr);
  5184. }
  5185. expression.push_back(e);
  5186. continue;
  5187. } else {
  5188. bool valid = false;
  5189. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_FOR_EXPRESSION && tk.type == TK_PARENTHESIS_CLOSE) {
  5190. valid = true;
  5191. _set_tkpos(prepos);
  5192. OperatorNode *func = alloc_node<OperatorNode>();
  5193. func->op = OP_EMPTY;
  5194. expr = func;
  5195. }
  5196. if (!valid) {
  5197. if (tk.type != TK_SEMICOLON) {
  5198. _set_error(vformat(RTR("Expected expression, found: '%s'."), get_token_text(tk)));
  5199. return nullptr;
  5200. } else {
  5201. #ifdef DEBUG_ENABLED
  5202. if (!p_block || (p_block->block_type != BlockNode::BLOCK_TYPE_FOR_INIT && p_block->block_type != BlockNode::BLOCK_TYPE_FOR_CONDITION)) {
  5203. if (check_warnings && HAS_WARNING(ShaderWarning::FORMATTING_ERROR_FLAG)) {
  5204. _add_line_warning(ShaderWarning::FORMATTING_ERROR, RTR("Empty statement. Remove ';' to fix this warning."));
  5205. }
  5206. }
  5207. #endif // DEBUG_ENABLED
  5208. _set_tkpos(prepos);
  5209. OperatorNode *func = alloc_node<OperatorNode>();
  5210. func->op = OP_EMPTY;
  5211. expr = func;
  5212. }
  5213. }
  5214. }
  5215. ERR_FAIL_NULL_V(expr, nullptr);
  5216. /* OK now see what's NEXT to the operator.. */
  5217. while (true) {
  5218. TkPos pos2 = _get_tkpos();
  5219. tk = _get_token();
  5220. if (tk.type == TK_CURSOR) {
  5221. //do nothing
  5222. } else if (tk.type == TK_PERIOD) {
  5223. #ifdef DEBUG_ENABLED
  5224. uint32_t prev_keyword_completion_context = keyword_completion_context;
  5225. keyword_completion_context = CF_UNSPECIFIED;
  5226. #endif
  5227. DataType dt = expr->get_datatype();
  5228. String st = expr->get_datatype_name();
  5229. if (!expr->is_indexed() && expr->get_array_size() > 0) {
  5230. completion_class = TAG_ARRAY;
  5231. if (p_block != nullptr) {
  5232. p_block->block_tag = SubClassTag::TAG_ARRAY;
  5233. }
  5234. Node *call_expression = _parse_and_reduce_expression(p_block, p_function_info);
  5235. if (p_block != nullptr) {
  5236. p_block->block_tag = SubClassTag::TAG_GLOBAL;
  5237. }
  5238. if (!call_expression) {
  5239. return nullptr;
  5240. }
  5241. expr = call_expression;
  5242. break;
  5243. }
  5244. StringName identifier;
  5245. if (_get_completable_identifier(p_block, dt == TYPE_STRUCT ? COMPLETION_STRUCT : COMPLETION_INDEX, identifier)) {
  5246. if (dt == TYPE_STRUCT) {
  5247. completion_struct = st;
  5248. } else {
  5249. completion_base = dt;
  5250. }
  5251. }
  5252. if (identifier == StringName()) {
  5253. _set_error(RTR("Expected an identifier as a member."));
  5254. return nullptr;
  5255. }
  5256. String ident = identifier;
  5257. bool ok = true;
  5258. bool repeated = false;
  5259. DataType member_type = TYPE_VOID;
  5260. StringName member_struct_name = "";
  5261. int array_size = 0;
  5262. RBSet<char> position_symbols;
  5263. RBSet<char> color_symbols;
  5264. RBSet<char> texture_symbols;
  5265. bool mix_error = false;
  5266. switch (dt) {
  5267. case TYPE_STRUCT: {
  5268. ok = false;
  5269. String member_name = String(ident.ptr());
  5270. if (shader->structs.has(st)) {
  5271. StructNode *n = shader->structs[st].shader_struct;
  5272. for (const MemberNode *E : n->members) {
  5273. if (String(E->name) == member_name) {
  5274. member_type = E->datatype;
  5275. array_size = E->array_size;
  5276. if (member_type == TYPE_STRUCT) {
  5277. member_struct_name = E->struct_name;
  5278. }
  5279. ok = true;
  5280. break;
  5281. }
  5282. }
  5283. }
  5284. } break;
  5285. case TYPE_BVEC2:
  5286. case TYPE_IVEC2:
  5287. case TYPE_UVEC2:
  5288. case TYPE_VEC2: {
  5289. int l = ident.length();
  5290. if (l == 1) {
  5291. member_type = DataType(dt - 1);
  5292. } else if (l == 2) {
  5293. member_type = dt;
  5294. } else if (l == 3) {
  5295. member_type = DataType(dt + 1);
  5296. } else if (l == 4) {
  5297. member_type = DataType(dt + 2);
  5298. } else {
  5299. ok = false;
  5300. break;
  5301. }
  5302. const char32_t *c = ident.ptr();
  5303. for (int i = 0; i < l; i++) {
  5304. switch (c[i]) {
  5305. case 'r':
  5306. case 'g':
  5307. if (position_symbols.size() > 0 || texture_symbols.size() > 0) {
  5308. mix_error = true;
  5309. break;
  5310. }
  5311. if (!color_symbols.has(c[i])) {
  5312. color_symbols.insert(c[i]);
  5313. } else {
  5314. repeated = true;
  5315. }
  5316. break;
  5317. case 'x':
  5318. case 'y':
  5319. if (color_symbols.size() > 0 || texture_symbols.size() > 0) {
  5320. mix_error = true;
  5321. break;
  5322. }
  5323. if (!position_symbols.has(c[i])) {
  5324. position_symbols.insert(c[i]);
  5325. } else {
  5326. repeated = true;
  5327. }
  5328. break;
  5329. case 's':
  5330. case 't':
  5331. if (color_symbols.size() > 0 || position_symbols.size() > 0) {
  5332. mix_error = true;
  5333. break;
  5334. }
  5335. if (!texture_symbols.has(c[i])) {
  5336. texture_symbols.insert(c[i]);
  5337. } else {
  5338. repeated = true;
  5339. }
  5340. break;
  5341. default:
  5342. ok = false;
  5343. break;
  5344. }
  5345. }
  5346. } break;
  5347. case TYPE_BVEC3:
  5348. case TYPE_IVEC3:
  5349. case TYPE_UVEC3:
  5350. case TYPE_VEC3: {
  5351. int l = ident.length();
  5352. if (l == 1) {
  5353. member_type = DataType(dt - 2);
  5354. } else if (l == 2) {
  5355. member_type = DataType(dt - 1);
  5356. } else if (l == 3) {
  5357. member_type = dt;
  5358. } else if (l == 4) {
  5359. member_type = DataType(dt + 1);
  5360. } else {
  5361. ok = false;
  5362. break;
  5363. }
  5364. const char32_t *c = ident.ptr();
  5365. for (int i = 0; i < l; i++) {
  5366. switch (c[i]) {
  5367. case 'r':
  5368. case 'g':
  5369. case 'b':
  5370. if (position_symbols.size() > 0 || texture_symbols.size() > 0) {
  5371. mix_error = true;
  5372. break;
  5373. }
  5374. if (!color_symbols.has(c[i])) {
  5375. color_symbols.insert(c[i]);
  5376. } else {
  5377. repeated = true;
  5378. }
  5379. break;
  5380. case 'x':
  5381. case 'y':
  5382. case 'z':
  5383. if (color_symbols.size() > 0 || texture_symbols.size() > 0) {
  5384. mix_error = true;
  5385. break;
  5386. }
  5387. if (!position_symbols.has(c[i])) {
  5388. position_symbols.insert(c[i]);
  5389. } else {
  5390. repeated = true;
  5391. }
  5392. break;
  5393. case 's':
  5394. case 't':
  5395. case 'p':
  5396. if (color_symbols.size() > 0 || position_symbols.size() > 0) {
  5397. mix_error = true;
  5398. break;
  5399. }
  5400. if (!texture_symbols.has(c[i])) {
  5401. texture_symbols.insert(c[i]);
  5402. } else {
  5403. repeated = true;
  5404. }
  5405. break;
  5406. default:
  5407. ok = false;
  5408. break;
  5409. }
  5410. }
  5411. } break;
  5412. case TYPE_BVEC4:
  5413. case TYPE_IVEC4:
  5414. case TYPE_UVEC4:
  5415. case TYPE_VEC4: {
  5416. int l = ident.length();
  5417. if (l == 1) {
  5418. member_type = DataType(dt - 3);
  5419. } else if (l == 2) {
  5420. member_type = DataType(dt - 2);
  5421. } else if (l == 3) {
  5422. member_type = DataType(dt - 1);
  5423. } else if (l == 4) {
  5424. member_type = dt;
  5425. } else {
  5426. ok = false;
  5427. break;
  5428. }
  5429. const char32_t *c = ident.ptr();
  5430. for (int i = 0; i < l; i++) {
  5431. switch (c[i]) {
  5432. case 'r':
  5433. case 'g':
  5434. case 'b':
  5435. case 'a':
  5436. if (position_symbols.size() > 0 || texture_symbols.size() > 0) {
  5437. mix_error = true;
  5438. break;
  5439. }
  5440. if (!color_symbols.has(c[i])) {
  5441. color_symbols.insert(c[i]);
  5442. } else {
  5443. repeated = true;
  5444. }
  5445. break;
  5446. case 'x':
  5447. case 'y':
  5448. case 'z':
  5449. case 'w':
  5450. if (color_symbols.size() > 0 || texture_symbols.size() > 0) {
  5451. mix_error = true;
  5452. break;
  5453. }
  5454. if (!position_symbols.has(c[i])) {
  5455. position_symbols.insert(c[i]);
  5456. } else {
  5457. repeated = true;
  5458. }
  5459. break;
  5460. case 's':
  5461. case 't':
  5462. case 'p':
  5463. case 'q':
  5464. if (color_symbols.size() > 0 || position_symbols.size() > 0) {
  5465. mix_error = true;
  5466. break;
  5467. }
  5468. if (!texture_symbols.has(c[i])) {
  5469. texture_symbols.insert(c[i]);
  5470. } else {
  5471. repeated = true;
  5472. }
  5473. break;
  5474. default:
  5475. ok = false;
  5476. break;
  5477. }
  5478. }
  5479. } break;
  5480. default: {
  5481. ok = false;
  5482. }
  5483. }
  5484. if (mix_error) {
  5485. _set_error(vformat(RTR("Cannot combine symbols from different sets in expression '.%s'."), ident));
  5486. return nullptr;
  5487. }
  5488. if (!ok) {
  5489. _set_error(vformat(RTR("Invalid member for '%s' expression: '.%s'."), (dt == TYPE_STRUCT ? st : get_datatype_name(dt)), ident));
  5490. return nullptr;
  5491. }
  5492. MemberNode *mn = alloc_node<MemberNode>();
  5493. mn->basetype = dt;
  5494. mn->basetype_const = is_const;
  5495. mn->datatype = member_type;
  5496. mn->base_struct_name = st;
  5497. mn->struct_name = member_struct_name;
  5498. mn->array_size = array_size;
  5499. mn->name = ident;
  5500. mn->owner = expr;
  5501. mn->has_swizzling_duplicates = repeated;
  5502. if (array_size > 0) {
  5503. TkPos prev_pos = _get_tkpos();
  5504. tk = _get_token();
  5505. if (tk.type == TK_OP_ASSIGN) {
  5506. if (last_type == IDENTIFIER_CONSTANT) {
  5507. _set_error(RTR("Constants cannot be modified."));
  5508. return nullptr;
  5509. }
  5510. Node *assign_expression = _parse_array_constructor(p_block, p_function_info, member_type, member_struct_name, array_size);
  5511. if (!assign_expression) {
  5512. return nullptr;
  5513. }
  5514. mn->assign_expression = assign_expression;
  5515. } else if (tk.type == TK_PERIOD) {
  5516. completion_class = TAG_ARRAY;
  5517. if (p_block != nullptr) {
  5518. p_block->block_tag = SubClassTag::TAG_ARRAY;
  5519. }
  5520. mn->call_expression = _parse_and_reduce_expression(p_block, p_function_info);
  5521. if (p_block != nullptr) {
  5522. p_block->block_tag = SubClassTag::TAG_GLOBAL;
  5523. }
  5524. if (!mn->call_expression) {
  5525. return nullptr;
  5526. }
  5527. } else if (tk.type == TK_BRACKET_OPEN) {
  5528. Node *index_expression = _parse_and_reduce_expression(p_block, p_function_info);
  5529. if (!index_expression) {
  5530. return nullptr;
  5531. }
  5532. if (index_expression->get_array_size() != 0 || (index_expression->get_datatype() != TYPE_INT && index_expression->get_datatype() != TYPE_UINT)) {
  5533. _set_error(RTR("Only integer expressions are allowed for indexing."));
  5534. return nullptr;
  5535. }
  5536. if (index_expression->type == Node::NODE_TYPE_CONSTANT) {
  5537. ConstantNode *cnode = static_cast<ConstantNode *>(index_expression);
  5538. if (cnode) {
  5539. if (!cnode->values.is_empty()) {
  5540. int value = cnode->values[0].sint;
  5541. if (value < 0 || value >= array_size) {
  5542. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), value, 0, array_size - 1));
  5543. return nullptr;
  5544. }
  5545. }
  5546. }
  5547. }
  5548. tk = _get_token();
  5549. if (tk.type != TK_BRACKET_CLOSE) {
  5550. _set_expected_error("]");
  5551. return nullptr;
  5552. }
  5553. mn->index_expression = index_expression;
  5554. } else {
  5555. _set_tkpos(prev_pos);
  5556. }
  5557. }
  5558. expr = mn;
  5559. #ifdef DEBUG_ENABLED
  5560. keyword_completion_context = prev_keyword_completion_context;
  5561. #endif
  5562. //todo
  5563. //member (period) has priority over any operator
  5564. //creates a subindexing expression in place
  5565. /*} else if (tk.type==TK_BRACKET_OPEN) {
  5566. //todo
  5567. //subindexing has priority over any operator
  5568. //creates a subindexing expression in place
  5569. */
  5570. } else if (tk.type == TK_BRACKET_OPEN) {
  5571. Node *index = _parse_and_reduce_expression(p_block, p_function_info);
  5572. if (!index) {
  5573. return nullptr;
  5574. }
  5575. if (index->get_array_size() != 0 || (index->get_datatype() != TYPE_INT && index->get_datatype() != TYPE_UINT)) {
  5576. _set_error(RTR("Only integer expressions are allowed for indexing."));
  5577. return nullptr;
  5578. }
  5579. DataType member_type = TYPE_VOID;
  5580. String member_struct_name;
  5581. if (expr->get_array_size() > 0) {
  5582. if (index->type == Node::NODE_TYPE_CONSTANT) {
  5583. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  5584. if (index_constant >= (uint32_t)expr->get_array_size()) {
  5585. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), index_constant, 0, expr->get_array_size() - 1));
  5586. return nullptr;
  5587. }
  5588. }
  5589. member_type = expr->get_datatype();
  5590. if (member_type == TYPE_STRUCT) {
  5591. member_struct_name = expr->get_datatype_name();
  5592. }
  5593. } else {
  5594. switch (expr->get_datatype()) {
  5595. case TYPE_BVEC2:
  5596. case TYPE_VEC2:
  5597. case TYPE_IVEC2:
  5598. case TYPE_UVEC2:
  5599. case TYPE_MAT2:
  5600. if (index->type == Node::NODE_TYPE_CONSTANT) {
  5601. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  5602. if (index_constant >= 2) {
  5603. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), index_constant, 0, 1));
  5604. return nullptr;
  5605. }
  5606. }
  5607. switch (expr->get_datatype()) {
  5608. case TYPE_BVEC2:
  5609. member_type = TYPE_BOOL;
  5610. break;
  5611. case TYPE_VEC2:
  5612. member_type = TYPE_FLOAT;
  5613. break;
  5614. case TYPE_IVEC2:
  5615. member_type = TYPE_INT;
  5616. break;
  5617. case TYPE_UVEC2:
  5618. member_type = TYPE_UINT;
  5619. break;
  5620. case TYPE_MAT2:
  5621. member_type = TYPE_VEC2;
  5622. break;
  5623. default:
  5624. break;
  5625. }
  5626. break;
  5627. case TYPE_BVEC3:
  5628. case TYPE_VEC3:
  5629. case TYPE_IVEC3:
  5630. case TYPE_UVEC3:
  5631. case TYPE_MAT3:
  5632. if (index->type == Node::NODE_TYPE_CONSTANT) {
  5633. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  5634. if (index_constant >= 3) {
  5635. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), index_constant, 0, 2));
  5636. return nullptr;
  5637. }
  5638. }
  5639. switch (expr->get_datatype()) {
  5640. case TYPE_BVEC3:
  5641. member_type = TYPE_BOOL;
  5642. break;
  5643. case TYPE_VEC3:
  5644. member_type = TYPE_FLOAT;
  5645. break;
  5646. case TYPE_IVEC3:
  5647. member_type = TYPE_INT;
  5648. break;
  5649. case TYPE_UVEC3:
  5650. member_type = TYPE_UINT;
  5651. break;
  5652. case TYPE_MAT3:
  5653. member_type = TYPE_VEC3;
  5654. break;
  5655. default:
  5656. break;
  5657. }
  5658. break;
  5659. case TYPE_BVEC4:
  5660. case TYPE_VEC4:
  5661. case TYPE_IVEC4:
  5662. case TYPE_UVEC4:
  5663. case TYPE_MAT4:
  5664. if (index->type == Node::NODE_TYPE_CONSTANT) {
  5665. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  5666. if (index_constant >= 4) {
  5667. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), index_constant, 0, 3));
  5668. return nullptr;
  5669. }
  5670. }
  5671. switch (expr->get_datatype()) {
  5672. case TYPE_BVEC4:
  5673. member_type = TYPE_BOOL;
  5674. break;
  5675. case TYPE_VEC4:
  5676. member_type = TYPE_FLOAT;
  5677. break;
  5678. case TYPE_IVEC4:
  5679. member_type = TYPE_INT;
  5680. break;
  5681. case TYPE_UVEC4:
  5682. member_type = TYPE_UINT;
  5683. break;
  5684. case TYPE_MAT4:
  5685. member_type = TYPE_VEC4;
  5686. break;
  5687. default:
  5688. break;
  5689. }
  5690. break;
  5691. default: {
  5692. _set_error(vformat(RTR("An object of type '%s' can't be indexed."), (expr->get_datatype() == TYPE_STRUCT ? expr->get_datatype_name() : get_datatype_name(expr->get_datatype()))));
  5693. return nullptr;
  5694. }
  5695. }
  5696. }
  5697. OperatorNode *op = alloc_node<OperatorNode>();
  5698. op->op = OP_INDEX;
  5699. op->return_cache = member_type;
  5700. op->struct_name = member_struct_name;
  5701. op->arguments.push_back(expr);
  5702. op->arguments.push_back(index);
  5703. expr = op;
  5704. tk = _get_token();
  5705. if (tk.type != TK_BRACKET_CLOSE) {
  5706. _set_expected_error("]");
  5707. return nullptr;
  5708. }
  5709. } else if (tk.type == TK_OP_INCREMENT || tk.type == TK_OP_DECREMENT) {
  5710. OperatorNode *op = alloc_node<OperatorNode>();
  5711. op->op = tk.type == TK_OP_DECREMENT ? OP_POST_DECREMENT : OP_POST_INCREMENT;
  5712. op->arguments.push_back(expr);
  5713. if (!_validate_operator(op, &op->return_cache, &op->return_array_size)) {
  5714. _set_error(RTR("Invalid base type for increment/decrement operator."));
  5715. return nullptr;
  5716. }
  5717. if (!_validate_assign(expr, p_function_info)) {
  5718. _set_error(RTR("Invalid use of increment/decrement operator in a constant expression."));
  5719. return nullptr;
  5720. }
  5721. expr = op;
  5722. } else {
  5723. _set_tkpos(pos2);
  5724. break;
  5725. }
  5726. }
  5727. Expression e;
  5728. e.is_op = false;
  5729. e.node = expr;
  5730. expression.push_back(e);
  5731. pos = _get_tkpos();
  5732. tk = _get_token();
  5733. if (is_token_operator(tk.type)) {
  5734. Expression o;
  5735. o.is_op = true;
  5736. switch (tk.type) {
  5737. case TK_OP_EQUAL:
  5738. o.op = OP_EQUAL;
  5739. break;
  5740. case TK_OP_NOT_EQUAL:
  5741. o.op = OP_NOT_EQUAL;
  5742. break;
  5743. case TK_OP_LESS:
  5744. o.op = OP_LESS;
  5745. break;
  5746. case TK_OP_LESS_EQUAL:
  5747. o.op = OP_LESS_EQUAL;
  5748. break;
  5749. case TK_OP_GREATER:
  5750. o.op = OP_GREATER;
  5751. break;
  5752. case TK_OP_GREATER_EQUAL:
  5753. o.op = OP_GREATER_EQUAL;
  5754. break;
  5755. case TK_OP_AND:
  5756. o.op = OP_AND;
  5757. break;
  5758. case TK_OP_OR:
  5759. o.op = OP_OR;
  5760. break;
  5761. case TK_OP_ADD:
  5762. o.op = OP_ADD;
  5763. break;
  5764. case TK_OP_SUB:
  5765. o.op = OP_SUB;
  5766. break;
  5767. case TK_OP_MUL:
  5768. o.op = OP_MUL;
  5769. break;
  5770. case TK_OP_DIV:
  5771. o.op = OP_DIV;
  5772. break;
  5773. case TK_OP_MOD:
  5774. o.op = OP_MOD;
  5775. break;
  5776. case TK_OP_SHIFT_LEFT:
  5777. o.op = OP_SHIFT_LEFT;
  5778. break;
  5779. case TK_OP_SHIFT_RIGHT:
  5780. o.op = OP_SHIFT_RIGHT;
  5781. break;
  5782. case TK_OP_ASSIGN:
  5783. o.op = OP_ASSIGN;
  5784. break;
  5785. case TK_OP_ASSIGN_ADD:
  5786. o.op = OP_ASSIGN_ADD;
  5787. break;
  5788. case TK_OP_ASSIGN_SUB:
  5789. o.op = OP_ASSIGN_SUB;
  5790. break;
  5791. case TK_OP_ASSIGN_MUL:
  5792. o.op = OP_ASSIGN_MUL;
  5793. break;
  5794. case TK_OP_ASSIGN_DIV:
  5795. o.op = OP_ASSIGN_DIV;
  5796. break;
  5797. case TK_OP_ASSIGN_MOD:
  5798. o.op = OP_ASSIGN_MOD;
  5799. break;
  5800. case TK_OP_ASSIGN_SHIFT_LEFT:
  5801. o.op = OP_ASSIGN_SHIFT_LEFT;
  5802. break;
  5803. case TK_OP_ASSIGN_SHIFT_RIGHT:
  5804. o.op = OP_ASSIGN_SHIFT_RIGHT;
  5805. break;
  5806. case TK_OP_ASSIGN_BIT_AND:
  5807. o.op = OP_ASSIGN_BIT_AND;
  5808. break;
  5809. case TK_OP_ASSIGN_BIT_OR:
  5810. o.op = OP_ASSIGN_BIT_OR;
  5811. break;
  5812. case TK_OP_ASSIGN_BIT_XOR:
  5813. o.op = OP_ASSIGN_BIT_XOR;
  5814. break;
  5815. case TK_OP_BIT_AND:
  5816. o.op = OP_BIT_AND;
  5817. break;
  5818. case TK_OP_BIT_OR:
  5819. o.op = OP_BIT_OR;
  5820. break;
  5821. case TK_OP_BIT_XOR:
  5822. o.op = OP_BIT_XOR;
  5823. break;
  5824. case TK_QUESTION:
  5825. o.op = OP_SELECT_IF;
  5826. break;
  5827. case TK_COLON:
  5828. o.op = OP_SELECT_ELSE;
  5829. break;
  5830. default: {
  5831. _set_error(vformat(RTR("Invalid token for the operator: '%s'."), get_token_text(tk)));
  5832. return nullptr;
  5833. }
  5834. }
  5835. expression.push_back(o);
  5836. } else {
  5837. _set_tkpos(pos); //something else, so rollback and end
  5838. break;
  5839. }
  5840. }
  5841. /* Reduce the set set of expressions and place them in an operator tree, respecting precedence */
  5842. while (expression.size() > 1) {
  5843. int next_op = -1;
  5844. int min_priority = 0xFFFFF;
  5845. bool is_unary = false;
  5846. bool is_ternary = false;
  5847. for (int i = 0; i < expression.size(); i++) {
  5848. if (!expression[i].is_op) {
  5849. continue;
  5850. }
  5851. bool unary = false;
  5852. bool ternary = false;
  5853. Operator op = expression[i].op;
  5854. int priority;
  5855. switch (op) {
  5856. case OP_EQUAL:
  5857. priority = 8;
  5858. break;
  5859. case OP_NOT_EQUAL:
  5860. priority = 8;
  5861. break;
  5862. case OP_LESS:
  5863. priority = 7;
  5864. break;
  5865. case OP_LESS_EQUAL:
  5866. priority = 7;
  5867. break;
  5868. case OP_GREATER:
  5869. priority = 7;
  5870. break;
  5871. case OP_GREATER_EQUAL:
  5872. priority = 7;
  5873. break;
  5874. case OP_AND:
  5875. priority = 12;
  5876. break;
  5877. case OP_OR:
  5878. priority = 14;
  5879. break;
  5880. case OP_NOT:
  5881. priority = 3;
  5882. unary = true;
  5883. break;
  5884. case OP_NEGATE:
  5885. priority = 3;
  5886. unary = true;
  5887. break;
  5888. case OP_ADD:
  5889. priority = 5;
  5890. break;
  5891. case OP_SUB:
  5892. priority = 5;
  5893. break;
  5894. case OP_MUL:
  5895. priority = 4;
  5896. break;
  5897. case OP_DIV:
  5898. priority = 4;
  5899. break;
  5900. case OP_MOD:
  5901. priority = 4;
  5902. break;
  5903. case OP_SHIFT_LEFT:
  5904. priority = 6;
  5905. break;
  5906. case OP_SHIFT_RIGHT:
  5907. priority = 6;
  5908. break;
  5909. case OP_ASSIGN:
  5910. priority = 16;
  5911. break;
  5912. case OP_ASSIGN_ADD:
  5913. priority = 16;
  5914. break;
  5915. case OP_ASSIGN_SUB:
  5916. priority = 16;
  5917. break;
  5918. case OP_ASSIGN_MUL:
  5919. priority = 16;
  5920. break;
  5921. case OP_ASSIGN_DIV:
  5922. priority = 16;
  5923. break;
  5924. case OP_ASSIGN_MOD:
  5925. priority = 16;
  5926. break;
  5927. case OP_ASSIGN_SHIFT_LEFT:
  5928. priority = 16;
  5929. break;
  5930. case OP_ASSIGN_SHIFT_RIGHT:
  5931. priority = 16;
  5932. break;
  5933. case OP_ASSIGN_BIT_AND:
  5934. priority = 16;
  5935. break;
  5936. case OP_ASSIGN_BIT_OR:
  5937. priority = 16;
  5938. break;
  5939. case OP_ASSIGN_BIT_XOR:
  5940. priority = 16;
  5941. break;
  5942. case OP_BIT_AND:
  5943. priority = 9;
  5944. break;
  5945. case OP_BIT_OR:
  5946. priority = 11;
  5947. break;
  5948. case OP_BIT_XOR:
  5949. priority = 10;
  5950. break;
  5951. case OP_BIT_INVERT:
  5952. priority = 3;
  5953. unary = true;
  5954. break;
  5955. case OP_INCREMENT:
  5956. priority = 3;
  5957. unary = true;
  5958. break;
  5959. case OP_DECREMENT:
  5960. priority = 3;
  5961. unary = true;
  5962. break;
  5963. case OP_SELECT_IF:
  5964. priority = 15;
  5965. ternary = true;
  5966. break;
  5967. case OP_SELECT_ELSE:
  5968. priority = 15;
  5969. ternary = true;
  5970. break;
  5971. default:
  5972. ERR_FAIL_V(nullptr); //unexpected operator
  5973. }
  5974. #ifdef DEBUG_ENABLED
  5975. if (check_warnings && HAS_WARNING(ShaderWarning::FLOAT_COMPARISON_FLAG) && (op == OP_EQUAL || op == OP_NOT_EQUAL) &&
  5976. (!expression[i - 1].is_op && !expression[i + 1].is_op) &&
  5977. (expression[i - 1].node->get_datatype() == TYPE_FLOAT && expression[i + 1].node->get_datatype() == TYPE_FLOAT)) {
  5978. _add_line_warning(ShaderWarning::FLOAT_COMPARISON);
  5979. }
  5980. #endif // DEBUG_ENABLED
  5981. if (priority < min_priority) {
  5982. // < is used for left to right (default)
  5983. // <= is used for right to left
  5984. next_op = i;
  5985. min_priority = priority;
  5986. is_unary = unary;
  5987. is_ternary = ternary;
  5988. }
  5989. }
  5990. ERR_FAIL_COND_V(next_op == -1, nullptr);
  5991. // OK! create operator..
  5992. if (is_unary) {
  5993. int expr_pos = next_op;
  5994. while (expression[expr_pos].is_op) {
  5995. expr_pos++;
  5996. if (expr_pos == expression.size()) {
  5997. //can happen..
  5998. _set_error(RTR("Unexpected end of expression."));
  5999. return nullptr;
  6000. }
  6001. }
  6002. //consecutively do unary operators
  6003. for (int i = expr_pos - 1; i >= next_op; i--) {
  6004. OperatorNode *op = alloc_node<OperatorNode>();
  6005. op->op = expression[i].op;
  6006. if ((op->op == OP_INCREMENT || op->op == OP_DECREMENT) && !_validate_assign(expression[i + 1].node, p_function_info)) {
  6007. _set_error(RTR("Invalid use of increment/decrement operator in a constant expression."));
  6008. return nullptr;
  6009. }
  6010. op->arguments.push_back(expression[i + 1].node);
  6011. expression.write[i].is_op = false;
  6012. expression.write[i].node = op;
  6013. if (!_validate_operator(op, &op->return_cache, &op->return_array_size)) {
  6014. String at;
  6015. for (int j = 0; j < op->arguments.size(); j++) {
  6016. if (j > 0) {
  6017. at += ", ";
  6018. }
  6019. at += get_datatype_name(op->arguments[j]->get_datatype());
  6020. if (!op->arguments[j]->is_indexed() && op->arguments[j]->get_array_size() > 0) {
  6021. at += "[";
  6022. at += itos(op->arguments[j]->get_array_size());
  6023. at += "]";
  6024. }
  6025. }
  6026. _set_error(vformat(RTR("Invalid arguments to unary operator '%s': %s."), get_operator_text(op->op), at));
  6027. return nullptr;
  6028. }
  6029. expression.remove_at(i + 1);
  6030. }
  6031. } else if (is_ternary) {
  6032. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  6033. _set_parsing_error();
  6034. ERR_FAIL_V(nullptr);
  6035. }
  6036. if (next_op + 2 >= expression.size() || !expression[next_op + 2].is_op || expression[next_op + 2].op != OP_SELECT_ELSE) {
  6037. _set_error(RTR("Missing matching ':' for select operator."));
  6038. return nullptr;
  6039. }
  6040. OperatorNode *op = alloc_node<OperatorNode>();
  6041. op->op = expression[next_op].op;
  6042. op->arguments.push_back(expression[next_op - 1].node);
  6043. op->arguments.push_back(expression[next_op + 1].node);
  6044. op->arguments.push_back(expression[next_op + 3].node);
  6045. expression.write[next_op - 1].is_op = false;
  6046. expression.write[next_op - 1].node = op;
  6047. if (!_validate_operator(op, &op->return_cache, &op->return_array_size)) {
  6048. String at;
  6049. for (int i = 0; i < op->arguments.size(); i++) {
  6050. if (i > 0) {
  6051. at += ", ";
  6052. }
  6053. at += get_datatype_name(op->arguments[i]->get_datatype());
  6054. if (!op->arguments[i]->is_indexed() && op->arguments[i]->get_array_size() > 0) {
  6055. at += "[";
  6056. at += itos(op->arguments[i]->get_array_size());
  6057. at += "]";
  6058. }
  6059. }
  6060. _set_error(vformat(RTR("Invalid argument to ternary operator: '%s'."), at));
  6061. return nullptr;
  6062. }
  6063. for (int i = 0; i < 4; i++) {
  6064. expression.remove_at(next_op);
  6065. }
  6066. } else {
  6067. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  6068. _set_parsing_error();
  6069. ERR_FAIL_V(nullptr);
  6070. }
  6071. OperatorNode *op = alloc_node<OperatorNode>();
  6072. op->op = expression[next_op].op;
  6073. if (expression[next_op - 1].is_op) {
  6074. _set_parsing_error();
  6075. ERR_FAIL_V(nullptr);
  6076. }
  6077. if (_is_operator_assign(op->op)) {
  6078. String assign_message;
  6079. if (!_validate_assign(expression[next_op - 1].node, p_function_info, &assign_message)) {
  6080. _set_error(assign_message);
  6081. return nullptr;
  6082. }
  6083. }
  6084. if (expression[next_op + 1].is_op) {
  6085. // this is not invalid and can really appear
  6086. // but it becomes invalid anyway because no binary op
  6087. // can be followed by a unary op in a valid combination,
  6088. // due to how precedence works, unaries will always disappear first
  6089. _set_parsing_error();
  6090. }
  6091. op->arguments.push_back(expression[next_op - 1].node); //expression goes as left
  6092. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as right
  6093. expression.write[next_op - 1].node = op;
  6094. //replace all 3 nodes by this operator and make it an expression
  6095. if (!_validate_operator(op, &op->return_cache, &op->return_array_size)) {
  6096. String at;
  6097. for (int i = 0; i < op->arguments.size(); i++) {
  6098. if (i > 0) {
  6099. at += ", ";
  6100. }
  6101. if (op->arguments[i]->get_datatype() == TYPE_STRUCT) {
  6102. at += op->arguments[i]->get_datatype_name();
  6103. } else {
  6104. at += get_datatype_name(op->arguments[i]->get_datatype());
  6105. }
  6106. if (!op->arguments[i]->is_indexed() && op->arguments[i]->get_array_size() > 0) {
  6107. at += "[";
  6108. at += itos(op->arguments[i]->get_array_size());
  6109. at += "]";
  6110. }
  6111. }
  6112. _set_error(vformat(RTR("Invalid arguments to operator '%s': '%s'."), get_operator_text(op->op), at));
  6113. return nullptr;
  6114. }
  6115. expression.remove_at(next_op);
  6116. expression.remove_at(next_op);
  6117. }
  6118. }
  6119. return expression[0].node;
  6120. }
  6121. ShaderLanguage::Node *ShaderLanguage::_reduce_expression(BlockNode *p_block, ShaderLanguage::Node *p_node) {
  6122. if (p_node->type != Node::NODE_TYPE_OPERATOR) {
  6123. return p_node;
  6124. }
  6125. //for now only reduce simple constructors
  6126. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  6127. if (op->op == OP_CONSTRUCT) {
  6128. ERR_FAIL_COND_V(op->arguments[0]->type != Node::NODE_TYPE_VARIABLE, p_node);
  6129. DataType type = op->get_datatype();
  6130. DataType base = get_scalar_type(type);
  6131. int cardinality = get_cardinality(type);
  6132. Vector<ConstantNode::Value> values;
  6133. for (int i = 1; i < op->arguments.size(); i++) {
  6134. op->arguments.write[i] = _reduce_expression(p_block, op->arguments[i]);
  6135. if (op->arguments[i]->type == Node::NODE_TYPE_CONSTANT) {
  6136. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[i]);
  6137. if (get_scalar_type(cn->datatype) == base) {
  6138. for (int j = 0; j < cn->values.size(); j++) {
  6139. values.push_back(cn->values[j]);
  6140. }
  6141. } else if (get_scalar_type(cn->datatype) == cn->datatype) {
  6142. ConstantNode::Value v;
  6143. if (!convert_constant(cn, base, &v)) {
  6144. return p_node;
  6145. }
  6146. values.push_back(v);
  6147. } else {
  6148. return p_node;
  6149. }
  6150. } else {
  6151. return p_node;
  6152. }
  6153. }
  6154. if (values.size() == 1) {
  6155. if (type >= TYPE_MAT2 && type <= TYPE_MAT4) {
  6156. ConstantNode::Value value = values[0];
  6157. ConstantNode::Value zero;
  6158. zero.real = 0.0f;
  6159. int size = 2 + (type - TYPE_MAT2);
  6160. values.clear();
  6161. for (int i = 0; i < size; i++) {
  6162. for (int j = 0; j < size; j++) {
  6163. values.push_back(i == j ? value : zero);
  6164. }
  6165. }
  6166. } else {
  6167. ConstantNode::Value value = values[0];
  6168. for (int i = 1; i < cardinality; i++) {
  6169. values.push_back(value);
  6170. }
  6171. }
  6172. } else if (values.size() != cardinality) {
  6173. ERR_PRINT("Failed to reduce expression, values and cardinality mismatch.");
  6174. return p_node;
  6175. }
  6176. ConstantNode *cn = alloc_node<ConstantNode>();
  6177. cn->datatype = op->get_datatype();
  6178. cn->values = values;
  6179. return cn;
  6180. } else if (op->op == OP_NEGATE) {
  6181. op->arguments.write[0] = _reduce_expression(p_block, op->arguments[0]);
  6182. if (op->arguments[0]->type == Node::NODE_TYPE_CONSTANT) {
  6183. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[0]);
  6184. DataType base = get_scalar_type(cn->datatype);
  6185. Vector<ConstantNode::Value> values;
  6186. for (int i = 0; i < cn->values.size(); i++) {
  6187. ConstantNode::Value nv;
  6188. switch (base) {
  6189. case TYPE_BOOL: {
  6190. nv.boolean = !cn->values[i].boolean;
  6191. } break;
  6192. case TYPE_INT: {
  6193. nv.sint = -cn->values[i].sint;
  6194. } break;
  6195. case TYPE_UINT: {
  6196. // Intentionally wrap the unsigned int value, because GLSL does.
  6197. nv.uint = 0 - cn->values[i].uint;
  6198. } break;
  6199. case TYPE_FLOAT: {
  6200. nv.real = -cn->values[i].real;
  6201. } break;
  6202. default: {
  6203. }
  6204. }
  6205. values.push_back(nv);
  6206. }
  6207. cn->values = values;
  6208. return cn;
  6209. }
  6210. }
  6211. return p_node;
  6212. }
  6213. ShaderLanguage::Node *ShaderLanguage::_parse_and_reduce_expression(BlockNode *p_block, const FunctionInfo &p_function_info) {
  6214. ShaderLanguage::Node *expr = _parse_expression(p_block, p_function_info);
  6215. if (!expr) { //errored
  6216. return nullptr;
  6217. }
  6218. expr = _reduce_expression(p_block, expr);
  6219. return expr;
  6220. }
  6221. Error ShaderLanguage::_parse_block(BlockNode *p_block, const FunctionInfo &p_function_info, bool p_just_one, bool p_can_break, bool p_can_continue) {
  6222. while (true) {
  6223. TkPos pos = _get_tkpos();
  6224. Token tk = _get_token();
  6225. #ifdef DEBUG_ENABLED
  6226. Token next;
  6227. #endif // DEBUG_ENABLED
  6228. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_SWITCH) {
  6229. if (tk.type != TK_CF_CASE && tk.type != TK_CF_DEFAULT && tk.type != TK_CURLY_BRACKET_CLOSE) {
  6230. _set_error(vformat(RTR("A switch may only contain '%s' and '%s' blocks."), "case", "default"));
  6231. return ERR_PARSE_ERROR;
  6232. }
  6233. }
  6234. bool is_struct = shader->structs.has(tk.text);
  6235. bool is_var_init = false;
  6236. bool is_condition = false;
  6237. if (tk.type == TK_CURLY_BRACKET_CLOSE) { //end of block
  6238. if (p_just_one) {
  6239. _set_expected_error("}");
  6240. return ERR_PARSE_ERROR;
  6241. }
  6242. return OK;
  6243. } else if (tk.type == TK_CONST || is_token_precision(tk.type) || is_token_nonvoid_datatype(tk.type) || is_struct) {
  6244. is_var_init = true;
  6245. String struct_name = "";
  6246. if (is_struct) {
  6247. struct_name = tk.text;
  6248. #ifdef DEBUG_ENABLED
  6249. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_STRUCT_FLAG) && used_structs.has(struct_name)) {
  6250. used_structs[struct_name].used = true;
  6251. }
  6252. #endif // DEBUG_ENABLED
  6253. }
  6254. #ifdef DEBUG_ENABLED
  6255. uint32_t precision_flag = CF_PRECISION_MODIFIER;
  6256. keyword_completion_context = CF_DATATYPE;
  6257. if (!is_token_precision(tk.type)) {
  6258. if (!is_struct) {
  6259. keyword_completion_context |= precision_flag;
  6260. }
  6261. }
  6262. #endif // DEBUG_ENABLED
  6263. bool is_const = false;
  6264. if (tk.type == TK_CONST) {
  6265. is_const = true;
  6266. tk = _get_token();
  6267. if (!is_struct) {
  6268. is_struct = shader->structs.has(tk.text); // check again.
  6269. struct_name = tk.text;
  6270. }
  6271. }
  6272. DataPrecision precision = PRECISION_DEFAULT;
  6273. if (is_token_precision(tk.type)) {
  6274. precision = get_token_precision(tk.type);
  6275. tk = _get_token();
  6276. if (!is_struct) {
  6277. is_struct = shader->structs.has(tk.text); // check again.
  6278. }
  6279. #ifdef DEBUG_ENABLED
  6280. if (keyword_completion_context & precision_flag) {
  6281. keyword_completion_context ^= precision_flag;
  6282. }
  6283. #endif // DEBUG_ENABLED
  6284. }
  6285. #ifdef DEBUG_ENABLED
  6286. if (is_const && _lookup_next(next)) {
  6287. if (is_token_precision(next.type)) {
  6288. keyword_completion_context = CF_UNSPECIFIED;
  6289. }
  6290. if (is_token_datatype(next.type)) {
  6291. keyword_completion_context ^= CF_DATATYPE;
  6292. }
  6293. }
  6294. #endif // DEBUG_ENABLED
  6295. if (precision != PRECISION_DEFAULT) {
  6296. if (!is_token_nonvoid_datatype(tk.type)) {
  6297. _set_error(RTR("Expected variable type after precision modifier."));
  6298. return ERR_PARSE_ERROR;
  6299. }
  6300. }
  6301. if (!is_struct) {
  6302. if (!is_token_variable_datatype(tk.type)) {
  6303. _set_error(RTR("Invalid variable type (samplers are not allowed)."));
  6304. return ERR_PARSE_ERROR;
  6305. }
  6306. }
  6307. DataType type = is_struct ? TYPE_STRUCT : get_token_datatype(tk.type);
  6308. if (precision != PRECISION_DEFAULT && _validate_precision(type, precision) != OK) {
  6309. return ERR_PARSE_ERROR;
  6310. }
  6311. #ifdef DEBUG_ENABLED
  6312. keyword_completion_context = CF_UNSPECIFIED;
  6313. #endif // DEBUG_ENABLED
  6314. int array_size = 0;
  6315. bool fixed_array_size = false;
  6316. bool first = true;
  6317. VariableDeclarationNode *vdnode = alloc_node<VariableDeclarationNode>();
  6318. vdnode->precision = precision;
  6319. if (is_struct) {
  6320. vdnode->struct_name = struct_name;
  6321. vdnode->datatype = TYPE_STRUCT;
  6322. } else {
  6323. vdnode->datatype = type;
  6324. };
  6325. vdnode->is_const = is_const;
  6326. do {
  6327. bool unknown_size = false;
  6328. VariableDeclarationNode::Declaration decl;
  6329. tk = _get_token();
  6330. if (first) {
  6331. first = false;
  6332. if (tk.type != TK_IDENTIFIER && tk.type != TK_BRACKET_OPEN) {
  6333. _set_error(RTR("Expected an identifier or '[' after type."));
  6334. return ERR_PARSE_ERROR;
  6335. }
  6336. if (tk.type == TK_BRACKET_OPEN) {
  6337. Error error = _parse_array_size(p_block, p_function_info, false, &decl.size_expression, &array_size, &unknown_size);
  6338. if (error != OK) {
  6339. return error;
  6340. }
  6341. decl.size = array_size;
  6342. fixed_array_size = true;
  6343. tk = _get_token();
  6344. }
  6345. }
  6346. if (tk.type != TK_IDENTIFIER) {
  6347. _set_error(RTR("Expected an identifier."));
  6348. return ERR_PARSE_ERROR;
  6349. }
  6350. StringName name = tk.text;
  6351. ShaderLanguage::IdentifierType itype;
  6352. if (_find_identifier(p_block, true, p_function_info, name, (ShaderLanguage::DataType *)nullptr, &itype)) {
  6353. if (itype != IDENTIFIER_FUNCTION) {
  6354. _set_redefinition_error(String(name));
  6355. return ERR_PARSE_ERROR;
  6356. }
  6357. }
  6358. decl.name = name;
  6359. #ifdef DEBUG_ENABLED
  6360. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_LOCAL_VARIABLE_FLAG) && p_block) {
  6361. FunctionNode *parent_function = nullptr;
  6362. {
  6363. BlockNode *block = p_block;
  6364. while (block && !block->parent_function) {
  6365. block = block->parent_block;
  6366. }
  6367. parent_function = block->parent_function;
  6368. }
  6369. if (parent_function) {
  6370. StringName func_name = parent_function->name;
  6371. if (!used_local_vars.has(func_name)) {
  6372. used_local_vars.insert(func_name, HashMap<StringName, Usage>());
  6373. }
  6374. used_local_vars[func_name].insert(name, Usage(tk_line));
  6375. }
  6376. }
  6377. #endif // DEBUG_ENABLED
  6378. is_const_decl = is_const;
  6379. BlockNode::Variable var;
  6380. var.type = type;
  6381. var.precision = precision;
  6382. var.line = tk_line;
  6383. var.array_size = array_size;
  6384. var.is_const = is_const;
  6385. var.struct_name = struct_name;
  6386. tk = _get_token();
  6387. if (tk.type == TK_BRACKET_OPEN) {
  6388. Error error = _parse_array_size(p_block, p_function_info, false, &decl.size_expression, &var.array_size, &unknown_size);
  6389. if (error != OK) {
  6390. return error;
  6391. }
  6392. decl.size = var.array_size;
  6393. array_size = var.array_size;
  6394. tk = _get_token();
  6395. }
  6396. #ifdef DEBUG_ENABLED
  6397. if (var.type == DataType::TYPE_BOOL) {
  6398. keyword_completion_context = CF_BOOLEAN;
  6399. }
  6400. #endif // DEBUG_ENABLED
  6401. if (var.array_size > 0 || unknown_size) {
  6402. bool full_def = false;
  6403. if (tk.type == TK_OP_ASSIGN) {
  6404. TkPos prev_pos = _get_tkpos();
  6405. tk = _get_token();
  6406. if (tk.type == TK_IDENTIFIER) { // a function call array initialization
  6407. _set_tkpos(prev_pos);
  6408. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  6409. if (!n) {
  6410. _set_error(RTR("Expected array initializer."));
  6411. return ERR_PARSE_ERROR;
  6412. } else {
  6413. if (unknown_size) {
  6414. decl.size = n->get_array_size();
  6415. var.array_size = n->get_array_size();
  6416. }
  6417. if (!_compare_datatypes(var.type, var.struct_name, var.array_size, n->get_datatype(), n->get_datatype_name(), n->get_array_size())) {
  6418. return ERR_PARSE_ERROR;
  6419. }
  6420. decl.single_expression = true;
  6421. decl.initializer.push_back(n);
  6422. }
  6423. tk = _get_token();
  6424. } else {
  6425. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  6426. if (unknown_size) {
  6427. _set_expected_error("{");
  6428. return ERR_PARSE_ERROR;
  6429. }
  6430. full_def = true;
  6431. DataPrecision precision2 = PRECISION_DEFAULT;
  6432. if (is_token_precision(tk.type)) {
  6433. precision2 = get_token_precision(tk.type);
  6434. tk = _get_token();
  6435. if (!is_token_nonvoid_datatype(tk.type)) {
  6436. _set_error(RTR("Expected data type after precision modifier."));
  6437. return ERR_PARSE_ERROR;
  6438. }
  6439. }
  6440. DataType type2;
  6441. StringName struct_name2 = "";
  6442. if (shader->structs.has(tk.text)) {
  6443. type2 = TYPE_STRUCT;
  6444. struct_name2 = tk.text;
  6445. } else {
  6446. if (!is_token_variable_datatype(tk.type)) {
  6447. _set_error(RTR("Invalid data type for the array."));
  6448. return ERR_PARSE_ERROR;
  6449. }
  6450. type2 = get_token_datatype(tk.type);
  6451. }
  6452. if (precision2 != PRECISION_DEFAULT && _validate_precision(type2, precision2) != OK) {
  6453. return ERR_PARSE_ERROR;
  6454. }
  6455. int array_size2 = 0;
  6456. tk = _get_token();
  6457. if (tk.type == TK_BRACKET_OPEN) {
  6458. bool is_unknown_size = false;
  6459. Error error = _parse_array_size(p_block, p_function_info, false, nullptr, &array_size2, &is_unknown_size);
  6460. if (error != OK) {
  6461. return error;
  6462. }
  6463. if (is_unknown_size) {
  6464. array_size2 = var.array_size;
  6465. }
  6466. tk = _get_token();
  6467. } else {
  6468. _set_expected_error("[");
  6469. return ERR_PARSE_ERROR;
  6470. }
  6471. if (precision != precision2 || type != type2 || struct_name != struct_name2 || var.array_size != array_size2) {
  6472. String from;
  6473. if (precision2 != PRECISION_DEFAULT) {
  6474. from += get_precision_name(precision2);
  6475. from += " ";
  6476. }
  6477. if (type2 == TYPE_STRUCT) {
  6478. from += struct_name2;
  6479. } else {
  6480. from += get_datatype_name(type2);
  6481. }
  6482. from += "[";
  6483. from += itos(array_size2);
  6484. from += "]'";
  6485. String to;
  6486. if (precision != PRECISION_DEFAULT) {
  6487. to += get_precision_name(precision);
  6488. to += " ";
  6489. }
  6490. if (type == TYPE_STRUCT) {
  6491. to += struct_name;
  6492. } else {
  6493. to += get_datatype_name(type);
  6494. }
  6495. to += "[";
  6496. to += itos(var.array_size);
  6497. to += "]'";
  6498. _set_error(vformat(RTR("Cannot convert from '%s' to '%s'."), from, to));
  6499. return ERR_PARSE_ERROR;
  6500. }
  6501. }
  6502. bool curly = tk.type == TK_CURLY_BRACKET_OPEN;
  6503. if (unknown_size) {
  6504. if (!curly) {
  6505. _set_expected_error("{");
  6506. return ERR_PARSE_ERROR;
  6507. }
  6508. } else {
  6509. if (full_def) {
  6510. if (curly) {
  6511. _set_expected_error("(");
  6512. return ERR_PARSE_ERROR;
  6513. }
  6514. }
  6515. }
  6516. if (tk.type == TK_PARENTHESIS_OPEN || curly) { // initialization
  6517. while (true) {
  6518. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  6519. if (!n) {
  6520. return ERR_PARSE_ERROR;
  6521. }
  6522. if (is_const && n->type == Node::NODE_TYPE_OPERATOR && static_cast<OperatorNode *>(n)->op == OP_CALL) {
  6523. _set_error(RTR("Expected a constant expression."));
  6524. return ERR_PARSE_ERROR;
  6525. }
  6526. if (!_compare_datatypes(var.type, struct_name, 0, n->get_datatype(), n->get_datatype_name(), 0)) {
  6527. return ERR_PARSE_ERROR;
  6528. }
  6529. tk = _get_token();
  6530. if (tk.type == TK_COMMA) {
  6531. decl.initializer.push_back(n);
  6532. continue;
  6533. } else if (!curly && tk.type == TK_PARENTHESIS_CLOSE) {
  6534. decl.initializer.push_back(n);
  6535. break;
  6536. } else if (curly && tk.type == TK_CURLY_BRACKET_CLOSE) {
  6537. decl.initializer.push_back(n);
  6538. break;
  6539. } else {
  6540. if (curly) {
  6541. _set_expected_error("}", ",");
  6542. } else {
  6543. _set_expected_error(")", ",");
  6544. }
  6545. return ERR_PARSE_ERROR;
  6546. }
  6547. }
  6548. if (unknown_size) {
  6549. decl.size = decl.initializer.size();
  6550. var.array_size = decl.initializer.size();
  6551. } else if (decl.initializer.size() != var.array_size) {
  6552. _set_error(RTR("Array size mismatch."));
  6553. return ERR_PARSE_ERROR;
  6554. }
  6555. tk = _get_token();
  6556. } else {
  6557. _set_expected_error("(");
  6558. return ERR_PARSE_ERROR;
  6559. }
  6560. }
  6561. } else {
  6562. if (unknown_size) {
  6563. _set_error(RTR("Expected array initialization."));
  6564. return ERR_PARSE_ERROR;
  6565. }
  6566. if (is_const) {
  6567. _set_error(RTR("Expected initialization of constant."));
  6568. return ERR_PARSE_ERROR;
  6569. }
  6570. }
  6571. array_size = var.array_size;
  6572. } else if (tk.type == TK_OP_ASSIGN) {
  6573. //variable created with assignment! must parse an expression
  6574. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  6575. if (!n) {
  6576. return ERR_PARSE_ERROR;
  6577. }
  6578. if (is_const && n->type == Node::NODE_TYPE_OPERATOR && static_cast<OperatorNode *>(n)->op == OP_CALL) {
  6579. OperatorNode *op = static_cast<OperatorNode *>(n);
  6580. for (int i = 1; i < op->arguments.size(); i++) {
  6581. if (!_check_node_constness(op->arguments[i])) {
  6582. _set_error(vformat(RTR("Expected constant expression for argument %d of function call after '='."), i - 1));
  6583. return ERR_PARSE_ERROR;
  6584. }
  6585. }
  6586. }
  6587. if (n->type == Node::NODE_TYPE_CONSTANT) {
  6588. ConstantNode *const_node = static_cast<ConstantNode *>(n);
  6589. if (const_node && const_node->values.size() == 1) {
  6590. var.value = const_node->values[0];
  6591. }
  6592. }
  6593. if (!_compare_datatypes(var.type, var.struct_name, var.array_size, n->get_datatype(), n->get_datatype_name(), n->get_array_size())) {
  6594. return ERR_PARSE_ERROR;
  6595. }
  6596. decl.initializer.push_back(n);
  6597. tk = _get_token();
  6598. } else {
  6599. if (is_const) {
  6600. _set_error(RTR("Expected initialization of constant."));
  6601. return ERR_PARSE_ERROR;
  6602. }
  6603. }
  6604. vdnode->declarations.push_back(decl);
  6605. p_block->variables[name] = var;
  6606. is_const_decl = false;
  6607. if (!fixed_array_size) {
  6608. array_size = 0;
  6609. }
  6610. if (tk.type == TK_SEMICOLON) {
  6611. break;
  6612. } else if (tk.type != TK_COMMA) {
  6613. _set_expected_error(",", ";");
  6614. return ERR_PARSE_ERROR;
  6615. }
  6616. } while (tk.type == TK_COMMA); //another variable
  6617. #ifdef DEBUG_ENABLED
  6618. keyword_completion_context = CF_BLOCK;
  6619. #endif // DEBUG_ENABLED
  6620. p_block->statements.push_back(static_cast<Node *>(vdnode));
  6621. } else if (tk.type == TK_CURLY_BRACKET_OPEN) {
  6622. //a sub block, just because..
  6623. BlockNode *block = alloc_node<BlockNode>();
  6624. block->parent_block = p_block;
  6625. if (_parse_block(block, p_function_info, false, p_can_break, p_can_continue) != OK) {
  6626. return ERR_PARSE_ERROR;
  6627. }
  6628. p_block->statements.push_back(block);
  6629. } else if (tk.type == TK_CF_IF) {
  6630. //if () {}
  6631. tk = _get_token();
  6632. if (tk.type != TK_PARENTHESIS_OPEN) {
  6633. _set_expected_after_error("(", "if");
  6634. return ERR_PARSE_ERROR;
  6635. }
  6636. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6637. cf->flow_op = FLOW_OP_IF;
  6638. #ifdef DEBUG_ENABLED
  6639. keyword_completion_context = CF_IF_DECL;
  6640. #endif // DEBUG_ENABLED
  6641. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  6642. if (!n) {
  6643. return ERR_PARSE_ERROR;
  6644. }
  6645. #ifdef DEBUG_ENABLED
  6646. keyword_completion_context = CF_BLOCK;
  6647. #endif // DEBUG_ENABLED
  6648. if (n->get_datatype() != TYPE_BOOL) {
  6649. _set_error(RTR("Expected a boolean expression."));
  6650. return ERR_PARSE_ERROR;
  6651. }
  6652. tk = _get_token();
  6653. if (tk.type != TK_PARENTHESIS_CLOSE) {
  6654. _set_expected_error(")");
  6655. return ERR_PARSE_ERROR;
  6656. }
  6657. BlockNode *block = alloc_node<BlockNode>();
  6658. block->parent_block = p_block;
  6659. cf->expressions.push_back(n);
  6660. cf->blocks.push_back(block);
  6661. p_block->statements.push_back(cf);
  6662. Error err = _parse_block(block, p_function_info, true, p_can_break, p_can_continue);
  6663. if (err) {
  6664. return err;
  6665. }
  6666. pos = _get_tkpos();
  6667. tk = _get_token();
  6668. if (tk.type == TK_CF_ELSE) {
  6669. block = alloc_node<BlockNode>();
  6670. block->parent_block = p_block;
  6671. cf->blocks.push_back(block);
  6672. err = _parse_block(block, p_function_info, true, p_can_break, p_can_continue);
  6673. if (err) {
  6674. return err;
  6675. }
  6676. } else {
  6677. _set_tkpos(pos); //rollback
  6678. }
  6679. } else if (tk.type == TK_CF_SWITCH) {
  6680. // switch() {}
  6681. tk = _get_token();
  6682. if (tk.type != TK_PARENTHESIS_OPEN) {
  6683. _set_expected_after_error("(", "switch");
  6684. return ERR_PARSE_ERROR;
  6685. }
  6686. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6687. cf->flow_op = FLOW_OP_SWITCH;
  6688. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  6689. if (!n) {
  6690. return ERR_PARSE_ERROR;
  6691. }
  6692. {
  6693. const ShaderLanguage::DataType switch_type = n->get_datatype();
  6694. if (switch_type != TYPE_INT && switch_type != TYPE_UINT) {
  6695. _set_error(RTR("Expected an integer expression."));
  6696. return ERR_PARSE_ERROR;
  6697. }
  6698. }
  6699. tk = _get_token();
  6700. if (tk.type != TK_PARENTHESIS_CLOSE) {
  6701. _set_expected_error(")");
  6702. return ERR_PARSE_ERROR;
  6703. }
  6704. tk = _get_token();
  6705. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  6706. _set_expected_after_error("{", "switch");
  6707. return ERR_PARSE_ERROR;
  6708. }
  6709. BlockNode *switch_block = alloc_node<BlockNode>();
  6710. switch_block->block_type = BlockNode::BLOCK_TYPE_SWITCH;
  6711. switch_block->parent_block = p_block;
  6712. cf->expressions.push_back(n);
  6713. cf->blocks.push_back(switch_block);
  6714. p_block->statements.push_back(cf);
  6715. int prev_type = TK_CF_CASE;
  6716. while (true) { // Go-through multiple cases.
  6717. if (_parse_block(switch_block, p_function_info, true, true, false) != OK) {
  6718. return ERR_PARSE_ERROR;
  6719. }
  6720. pos = _get_tkpos();
  6721. tk = _get_token();
  6722. if (tk.type == TK_CF_CASE || tk.type == TK_CF_DEFAULT) {
  6723. if (prev_type == TK_CF_DEFAULT) {
  6724. if (tk.type == TK_CF_CASE) {
  6725. _set_error(RTR("Cases must be defined before default case."));
  6726. return ERR_PARSE_ERROR;
  6727. } else if (prev_type == TK_CF_DEFAULT) {
  6728. _set_error(RTR("Default case must be defined only once."));
  6729. return ERR_PARSE_ERROR;
  6730. }
  6731. }
  6732. prev_type = tk.type;
  6733. _set_tkpos(pos);
  6734. continue;
  6735. } else {
  6736. HashSet<int> constants;
  6737. for (ShaderLanguage::Node *statement : switch_block->statements) { // Checks for duplicates.
  6738. ControlFlowNode *flow = static_cast<ControlFlowNode *>(statement);
  6739. if (flow) {
  6740. if (flow->flow_op == FLOW_OP_CASE) {
  6741. if (flow->expressions[0]->type == Node::NODE_TYPE_CONSTANT) {
  6742. ConstantNode *cn = static_cast<ConstantNode *>(flow->expressions[0]);
  6743. if (!cn || cn->values.is_empty()) {
  6744. return ERR_PARSE_ERROR;
  6745. }
  6746. if (constants.has(cn->values[0].sint)) {
  6747. _set_error(vformat(RTR("Duplicated case label: %d."), cn->values[0].sint));
  6748. return ERR_PARSE_ERROR;
  6749. }
  6750. constants.insert(cn->values[0].sint);
  6751. } else if (flow->expressions[0]->type == Node::NODE_TYPE_VARIABLE) {
  6752. VariableNode *vn = static_cast<VariableNode *>(flow->expressions[0]);
  6753. if (!vn) {
  6754. return ERR_PARSE_ERROR;
  6755. }
  6756. ConstantNode::Value v;
  6757. _find_identifier(p_block, false, p_function_info, vn->name, nullptr, nullptr, nullptr, nullptr, nullptr, &v);
  6758. if (constants.has(v.sint)) {
  6759. _set_error(vformat(RTR("Duplicated case label: %d."), v.sint));
  6760. return ERR_PARSE_ERROR;
  6761. }
  6762. constants.insert(v.sint);
  6763. }
  6764. } else if (flow->flow_op == FLOW_OP_DEFAULT) {
  6765. continue;
  6766. } else {
  6767. return ERR_PARSE_ERROR;
  6768. }
  6769. } else {
  6770. return ERR_PARSE_ERROR;
  6771. }
  6772. }
  6773. break;
  6774. }
  6775. }
  6776. } else if (tk.type == TK_CF_CASE) {
  6777. // case x : break; | return;
  6778. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_CASE) {
  6779. _set_tkpos(pos);
  6780. return OK;
  6781. }
  6782. if (!p_block || (p_block->block_type != BlockNode::BLOCK_TYPE_SWITCH)) {
  6783. _set_error(vformat(RTR("'%s' must be placed within a '%s' block."), "case", "switch"));
  6784. return ERR_PARSE_ERROR;
  6785. }
  6786. tk = _get_token();
  6787. int sign = 1;
  6788. if (tk.type == TK_OP_SUB) {
  6789. sign = -1;
  6790. tk = _get_token();
  6791. }
  6792. Node *n = nullptr;
  6793. if (!tk.is_integer_constant()) {
  6794. bool correct_constant_expression = false;
  6795. DataType data_type;
  6796. if (tk.type == TK_IDENTIFIER) {
  6797. bool is_const;
  6798. _find_identifier(p_block, false, p_function_info, tk.text, &data_type, nullptr, &is_const);
  6799. if (is_const) {
  6800. if (data_type == TYPE_INT) {
  6801. correct_constant_expression = true;
  6802. }
  6803. }
  6804. }
  6805. if (!correct_constant_expression) {
  6806. _set_error(RTR("Expected an integer constant."));
  6807. return ERR_PARSE_ERROR;
  6808. }
  6809. VariableNode *vn = alloc_node<VariableNode>();
  6810. vn->name = tk.text;
  6811. n = vn;
  6812. } else {
  6813. ConstantNode::Value v;
  6814. if (tk.type == TK_UINT_CONSTANT) {
  6815. v.uint = (uint32_t)tk.constant;
  6816. } else {
  6817. v.sint = (int)tk.constant * sign;
  6818. }
  6819. ConstantNode *cn = alloc_node<ConstantNode>();
  6820. cn->values.push_back(v);
  6821. cn->datatype = (tk.type == TK_UINT_CONSTANT ? TYPE_UINT : TYPE_INT);
  6822. n = cn;
  6823. }
  6824. tk = _get_token();
  6825. if (tk.type != TK_COLON) {
  6826. _set_expected_error(":");
  6827. return ERR_PARSE_ERROR;
  6828. }
  6829. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6830. cf->flow_op = FLOW_OP_CASE;
  6831. BlockNode *case_block = alloc_node<BlockNode>();
  6832. case_block->block_type = BlockNode::BLOCK_TYPE_CASE;
  6833. case_block->parent_block = p_block;
  6834. cf->expressions.push_back(n);
  6835. cf->blocks.push_back(case_block);
  6836. p_block->statements.push_back(cf);
  6837. Error err = _parse_block(case_block, p_function_info, false, true, false);
  6838. if (err) {
  6839. return err;
  6840. }
  6841. return OK;
  6842. } else if (tk.type == TK_CF_DEFAULT) {
  6843. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_CASE) {
  6844. _set_tkpos(pos);
  6845. return OK;
  6846. }
  6847. if (!p_block || (p_block->block_type != BlockNode::BLOCK_TYPE_SWITCH)) {
  6848. _set_error(vformat(RTR("'%s' must be placed within a '%s' block."), "default", "switch"));
  6849. return ERR_PARSE_ERROR;
  6850. }
  6851. tk = _get_token();
  6852. if (tk.type != TK_COLON) {
  6853. _set_expected_error(":");
  6854. return ERR_PARSE_ERROR;
  6855. }
  6856. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6857. cf->flow_op = FLOW_OP_DEFAULT;
  6858. BlockNode *default_block = alloc_node<BlockNode>();
  6859. default_block->block_type = BlockNode::BLOCK_TYPE_DEFAULT;
  6860. default_block->parent_block = p_block;
  6861. cf->blocks.push_back(default_block);
  6862. p_block->statements.push_back(cf);
  6863. Error err = _parse_block(default_block, p_function_info, false, true, false);
  6864. if (err) {
  6865. return err;
  6866. }
  6867. return OK;
  6868. } else if (tk.type == TK_CF_DO || tk.type == TK_CF_WHILE) {
  6869. // do {} while()
  6870. // while() {}
  6871. bool is_do = tk.type == TK_CF_DO;
  6872. BlockNode *do_block = nullptr;
  6873. if (is_do) {
  6874. do_block = alloc_node<BlockNode>();
  6875. do_block->parent_block = p_block;
  6876. Error err = _parse_block(do_block, p_function_info, true, true, true);
  6877. if (err) {
  6878. return err;
  6879. }
  6880. tk = _get_token();
  6881. if (tk.type != TK_CF_WHILE) {
  6882. _set_expected_after_error("while", "do");
  6883. return ERR_PARSE_ERROR;
  6884. }
  6885. }
  6886. tk = _get_token();
  6887. if (tk.type != TK_PARENTHESIS_OPEN) {
  6888. _set_expected_after_error("(", "while");
  6889. return ERR_PARSE_ERROR;
  6890. }
  6891. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6892. if (is_do) {
  6893. cf->flow_op = FLOW_OP_DO;
  6894. } else {
  6895. cf->flow_op = FLOW_OP_WHILE;
  6896. }
  6897. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  6898. if (!n) {
  6899. return ERR_PARSE_ERROR;
  6900. }
  6901. tk = _get_token();
  6902. if (tk.type != TK_PARENTHESIS_CLOSE) {
  6903. _set_expected_error(")");
  6904. return ERR_PARSE_ERROR;
  6905. }
  6906. if (!is_do) {
  6907. BlockNode *block = alloc_node<BlockNode>();
  6908. block->parent_block = p_block;
  6909. cf->expressions.push_back(n);
  6910. cf->blocks.push_back(block);
  6911. p_block->statements.push_back(cf);
  6912. Error err = _parse_block(block, p_function_info, true, true, true);
  6913. if (err) {
  6914. return err;
  6915. }
  6916. } else {
  6917. cf->expressions.push_back(n);
  6918. cf->blocks.push_back(do_block);
  6919. p_block->statements.push_back(cf);
  6920. tk = _get_token();
  6921. if (tk.type != TK_SEMICOLON) {
  6922. _set_expected_error(";");
  6923. return ERR_PARSE_ERROR;
  6924. }
  6925. }
  6926. } else if (tk.type == TK_CF_FOR) {
  6927. // for() {}
  6928. tk = _get_token();
  6929. if (tk.type != TK_PARENTHESIS_OPEN) {
  6930. _set_expected_after_error("(", "for");
  6931. return ERR_PARSE_ERROR;
  6932. }
  6933. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6934. cf->flow_op = FLOW_OP_FOR;
  6935. BlockNode *init_block = alloc_node<BlockNode>();
  6936. init_block->block_type = BlockNode::BLOCK_TYPE_FOR_INIT;
  6937. init_block->parent_block = p_block;
  6938. init_block->single_statement = true;
  6939. cf->blocks.push_back(init_block);
  6940. #ifdef DEBUG_ENABLED
  6941. keyword_completion_context = CF_DATATYPE;
  6942. #endif // DEBUG_ENABLED
  6943. Error err = _parse_block(init_block, p_function_info, true, false, false);
  6944. if (err != OK) {
  6945. return err;
  6946. }
  6947. #ifdef DEBUG_ENABLED
  6948. keyword_completion_context = CF_UNSPECIFIED;
  6949. #endif // DEBUG_ENABLED
  6950. BlockNode *condition_block = alloc_node<BlockNode>();
  6951. condition_block->block_type = BlockNode::BLOCK_TYPE_FOR_CONDITION;
  6952. condition_block->parent_block = init_block;
  6953. condition_block->single_statement = true;
  6954. condition_block->use_comma_between_statements = true;
  6955. cf->blocks.push_back(condition_block);
  6956. err = _parse_block(condition_block, p_function_info, true, false, false);
  6957. if (err != OK) {
  6958. return err;
  6959. }
  6960. BlockNode *expression_block = alloc_node<BlockNode>();
  6961. expression_block->block_type = BlockNode::BLOCK_TYPE_FOR_EXPRESSION;
  6962. expression_block->parent_block = init_block;
  6963. expression_block->single_statement = true;
  6964. expression_block->use_comma_between_statements = true;
  6965. cf->blocks.push_back(expression_block);
  6966. err = _parse_block(expression_block, p_function_info, true, false, false);
  6967. if (err != OK) {
  6968. return err;
  6969. }
  6970. BlockNode *block = alloc_node<BlockNode>();
  6971. block->parent_block = init_block;
  6972. cf->blocks.push_back(block);
  6973. p_block->statements.push_back(cf);
  6974. #ifdef DEBUG_ENABLED
  6975. keyword_completion_context = CF_BLOCK;
  6976. #endif // DEBUG_ENABLED
  6977. err = _parse_block(block, p_function_info, true, true, true);
  6978. if (err != OK) {
  6979. return err;
  6980. }
  6981. } else if (tk.type == TK_CF_RETURN) {
  6982. //check return type
  6983. BlockNode *b = p_block;
  6984. while (b && !b->parent_function) {
  6985. b = b->parent_block;
  6986. }
  6987. if (!b) {
  6988. _set_parsing_error();
  6989. return ERR_BUG;
  6990. }
  6991. if (b->parent_function && p_function_info.main_function) {
  6992. _set_error(vformat(RTR("Using '%s' in the '%s' processor function is incorrect."), "return", b->parent_function->name));
  6993. return ERR_PARSE_ERROR;
  6994. }
  6995. String return_struct_name = String(b->parent_function->return_struct_name);
  6996. String array_size_string;
  6997. if (b->parent_function->return_array_size > 0) {
  6998. array_size_string = "[" + itos(b->parent_function->return_array_size) + "]";
  6999. }
  7000. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  7001. flow->flow_op = FLOW_OP_RETURN;
  7002. pos = _get_tkpos();
  7003. tk = _get_token();
  7004. if (tk.type == TK_SEMICOLON) {
  7005. //all is good
  7006. if (b->parent_function->return_type != TYPE_VOID) {
  7007. _set_error(vformat(RTR("Expected '%s' with an expression of type '%s'."), "return", (!return_struct_name.is_empty() ? return_struct_name : get_datatype_name(b->parent_function->return_type)) + array_size_string));
  7008. return ERR_PARSE_ERROR;
  7009. }
  7010. } else {
  7011. _set_tkpos(pos); //rollback, wants expression
  7012. #ifdef DEBUG_ENABLED
  7013. if (b->parent_function->return_type == DataType::TYPE_BOOL) {
  7014. keyword_completion_context = CF_BOOLEAN;
  7015. }
  7016. #endif // DEBUG_ENABLED
  7017. Node *expr = _parse_and_reduce_expression(p_block, p_function_info);
  7018. if (!expr) {
  7019. return ERR_PARSE_ERROR;
  7020. }
  7021. 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()) {
  7022. _set_error(vformat(RTR("Expected return with an expression of type '%s'."), (!return_struct_name.is_empty() ? return_struct_name : get_datatype_name(b->parent_function->return_type)) + array_size_string));
  7023. return ERR_PARSE_ERROR;
  7024. }
  7025. tk = _get_token();
  7026. if (tk.type != TK_SEMICOLON) {
  7027. _set_expected_after_error(";", "return");
  7028. return ERR_PARSE_ERROR;
  7029. }
  7030. #ifdef DEBUG_ENABLED
  7031. if (b->parent_function->return_type == DataType::TYPE_BOOL) {
  7032. keyword_completion_context = CF_BLOCK;
  7033. }
  7034. #endif // DEBUG_ENABLED
  7035. flow->expressions.push_back(expr);
  7036. }
  7037. p_block->statements.push_back(flow);
  7038. BlockNode *block = p_block;
  7039. while (block) {
  7040. if (block->block_type == BlockNode::BLOCK_TYPE_CASE || block->block_type == BlockNode::BLOCK_TYPE_DEFAULT) {
  7041. return OK;
  7042. }
  7043. block = block->parent_block;
  7044. }
  7045. } else if (tk.type == TK_CF_DISCARD) {
  7046. //check return type
  7047. BlockNode *b = p_block;
  7048. while (b && !b->parent_function) {
  7049. b = b->parent_block;
  7050. }
  7051. if (!b) {
  7052. _set_parsing_error();
  7053. return ERR_BUG;
  7054. }
  7055. if (!b->parent_function->can_discard) {
  7056. _set_error(vformat(RTR("Use of '%s' is not allowed here."), "discard"));
  7057. return ERR_PARSE_ERROR;
  7058. }
  7059. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  7060. flow->flow_op = FLOW_OP_DISCARD;
  7061. pos = _get_tkpos();
  7062. tk = _get_token();
  7063. if (tk.type != TK_SEMICOLON) {
  7064. _set_expected_after_error(";", "discard");
  7065. return ERR_PARSE_ERROR;
  7066. }
  7067. p_block->statements.push_back(flow);
  7068. } else if (tk.type == TK_CF_BREAK) {
  7069. if (!p_can_break) {
  7070. _set_error(vformat(RTR("'%s' is not allowed outside of a loop or '%s' statement."), "break", "switch"));
  7071. return ERR_PARSE_ERROR;
  7072. }
  7073. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  7074. flow->flow_op = FLOW_OP_BREAK;
  7075. pos = _get_tkpos();
  7076. tk = _get_token();
  7077. if (tk.type != TK_SEMICOLON) {
  7078. _set_expected_after_error(";", "break");
  7079. return ERR_PARSE_ERROR;
  7080. }
  7081. p_block->statements.push_back(flow);
  7082. BlockNode *block = p_block;
  7083. while (block) {
  7084. if (block->block_type == BlockNode::BLOCK_TYPE_CASE || block->block_type == BlockNode::BLOCK_TYPE_DEFAULT) {
  7085. return OK;
  7086. }
  7087. block = block->parent_block;
  7088. }
  7089. } else if (tk.type == TK_CF_CONTINUE) {
  7090. if (!p_can_continue) {
  7091. _set_error(vformat(RTR("'%s' is not allowed outside of a loop."), "continue"));
  7092. return ERR_PARSE_ERROR;
  7093. }
  7094. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  7095. flow->flow_op = FLOW_OP_CONTINUE;
  7096. pos = _get_tkpos();
  7097. tk = _get_token();
  7098. if (tk.type != TK_SEMICOLON) {
  7099. //all is good
  7100. _set_expected_after_error(";", "continue");
  7101. return ERR_PARSE_ERROR;
  7102. }
  7103. p_block->statements.push_back(flow);
  7104. } else {
  7105. //nothing else, so expression
  7106. _set_tkpos(pos); //rollback
  7107. Node *expr = _parse_and_reduce_expression(p_block, p_function_info);
  7108. if (!expr) {
  7109. return ERR_PARSE_ERROR;
  7110. }
  7111. is_condition = expr->type == Node::NODE_TYPE_OPERATOR && expr->get_datatype() == TYPE_BOOL;
  7112. if (expr->type == Node::NODE_TYPE_OPERATOR) {
  7113. OperatorNode *op = static_cast<OperatorNode *>(expr);
  7114. if (op->op == OP_EMPTY) {
  7115. is_var_init = true;
  7116. is_condition = true;
  7117. }
  7118. }
  7119. p_block->statements.push_back(expr);
  7120. tk = _get_token();
  7121. if (p_block->block_type == BlockNode::BLOCK_TYPE_FOR_CONDITION) {
  7122. if (tk.type == TK_COMMA) {
  7123. if (!is_condition) {
  7124. _set_error(RTR("The middle expression is expected to be a boolean operator."));
  7125. return ERR_PARSE_ERROR;
  7126. }
  7127. tk = _peek();
  7128. if (tk.type == TK_SEMICOLON) {
  7129. _set_error(vformat(RTR("Expected expression, found: '%s'."), get_token_text(tk)));
  7130. return ERR_PARSE_ERROR;
  7131. }
  7132. continue;
  7133. }
  7134. if (tk.type != TK_SEMICOLON) {
  7135. _set_expected_error(",", ";");
  7136. return ERR_PARSE_ERROR;
  7137. }
  7138. } else if (p_block->block_type == BlockNode::BLOCK_TYPE_FOR_EXPRESSION) {
  7139. if (tk.type == TK_COMMA) {
  7140. tk = _peek();
  7141. if (tk.type == TK_PARENTHESIS_CLOSE) {
  7142. _set_error(vformat(RTR("Expected expression, found: '%s'."), get_token_text(tk)));
  7143. return ERR_PARSE_ERROR;
  7144. }
  7145. continue;
  7146. }
  7147. if (tk.type != TK_PARENTHESIS_CLOSE) {
  7148. _set_expected_error(",", ")");
  7149. return ERR_PARSE_ERROR;
  7150. }
  7151. } else if (tk.type != TK_SEMICOLON) {
  7152. _set_expected_error(";");
  7153. return ERR_PARSE_ERROR;
  7154. }
  7155. }
  7156. if (p_block) {
  7157. if (p_block->block_type == BlockNode::BLOCK_TYPE_FOR_INIT && !is_var_init) {
  7158. _set_error(RTR("The left expression is expected to be a variable declaration."));
  7159. return ERR_PARSE_ERROR;
  7160. }
  7161. if (p_block->block_type == BlockNode::BLOCK_TYPE_FOR_CONDITION && !is_condition) {
  7162. _set_error(RTR("The middle expression is expected to be a boolean operator."));
  7163. return ERR_PARSE_ERROR;
  7164. }
  7165. }
  7166. if (p_just_one) {
  7167. break;
  7168. }
  7169. }
  7170. return OK;
  7171. }
  7172. String ShaderLanguage::_get_shader_type_list(const HashSet<String> &p_shader_types) const {
  7173. // Return a list of shader types as an human-readable string
  7174. String valid_types;
  7175. for (const String &E : p_shader_types) {
  7176. if (!valid_types.is_empty()) {
  7177. valid_types += ", ";
  7178. }
  7179. valid_types += "'" + E + "'";
  7180. }
  7181. return valid_types;
  7182. }
  7183. String ShaderLanguage::_get_qualifier_str(ArgumentQualifier p_qualifier) const {
  7184. switch (p_qualifier) {
  7185. case ArgumentQualifier::ARGUMENT_QUALIFIER_IN:
  7186. return "in";
  7187. case ArgumentQualifier::ARGUMENT_QUALIFIER_OUT:
  7188. return "out";
  7189. case ArgumentQualifier::ARGUMENT_QUALIFIER_INOUT:
  7190. return "inout";
  7191. }
  7192. return "";
  7193. }
  7194. Error ShaderLanguage::_validate_precision(DataType p_type, DataPrecision p_precision) {
  7195. switch (p_type) {
  7196. case TYPE_STRUCT: {
  7197. _set_error(RTR("The precision modifier cannot be used on structs."));
  7198. return FAILED;
  7199. } break;
  7200. case TYPE_BOOL:
  7201. case TYPE_BVEC2:
  7202. case TYPE_BVEC3:
  7203. case TYPE_BVEC4: {
  7204. _set_error(RTR("The precision modifier cannot be used on boolean types."));
  7205. return FAILED;
  7206. } break;
  7207. default:
  7208. break;
  7209. }
  7210. return OK;
  7211. }
  7212. Error ShaderLanguage::_parse_shader(const HashMap<StringName, FunctionInfo> &p_functions, const Vector<ModeInfo> &p_render_modes, const HashSet<String> &p_shader_types) {
  7213. Token tk;
  7214. TkPos prev_pos;
  7215. Token next;
  7216. if (!is_shader_inc) {
  7217. #ifdef DEBUG_ENABLED
  7218. keyword_completion_context = CF_SHADER_TYPE;
  7219. #endif // DEBUG_ENABLED
  7220. tk = _get_token();
  7221. if (tk.type != TK_SHADER_TYPE) {
  7222. _set_error(vformat(RTR("Expected '%s' at the beginning of shader. Valid types are: %s."), "shader_type", _get_shader_type_list(p_shader_types)));
  7223. return ERR_PARSE_ERROR;
  7224. }
  7225. #ifdef DEBUG_ENABLED
  7226. keyword_completion_context = CF_UNSPECIFIED;
  7227. #endif // DEBUG_ENABLED
  7228. _get_completable_identifier(nullptr, COMPLETION_SHADER_TYPE, shader_type_identifier);
  7229. if (shader_type_identifier == StringName()) {
  7230. _set_error(vformat(RTR("Expected an identifier after '%s', indicating the type of shader. Valid types are: %s."), "shader_type", _get_shader_type_list(p_shader_types)));
  7231. return ERR_PARSE_ERROR;
  7232. }
  7233. if (!p_shader_types.has(shader_type_identifier)) {
  7234. _set_error(vformat(RTR("Invalid shader type. Valid types are: %s"), _get_shader_type_list(p_shader_types)));
  7235. return ERR_PARSE_ERROR;
  7236. }
  7237. prev_pos = _get_tkpos();
  7238. tk = _get_token();
  7239. if (tk.type != TK_SEMICOLON) {
  7240. _set_tkpos(prev_pos);
  7241. _set_expected_after_error(";", "shader_type " + String(shader_type_identifier));
  7242. return ERR_PARSE_ERROR;
  7243. }
  7244. }
  7245. #ifdef DEBUG_ENABLED
  7246. keyword_completion_context = CF_GLOBAL_SPACE;
  7247. #endif // DEBUG_ENABLED
  7248. tk = _get_token();
  7249. int texture_uniforms = 0;
  7250. int texture_binding = 0;
  7251. int uniforms = 0;
  7252. int instance_index = 0;
  7253. #ifdef DEBUG_ENABLED
  7254. uint64_t uniform_buffer_size = 0;
  7255. uint64_t max_uniform_buffer_size = 0;
  7256. int uniform_buffer_exceeded_line = -1;
  7257. bool check_device_limit_warnings = false;
  7258. {
  7259. RenderingDevice *device = RenderingDevice::get_singleton();
  7260. if (device != nullptr) {
  7261. check_device_limit_warnings = check_warnings && HAS_WARNING(ShaderWarning::DEVICE_LIMIT_EXCEEDED_FLAG);
  7262. max_uniform_buffer_size = device->limit_get(RenderingDevice::LIMIT_MAX_UNIFORM_BUFFER_SIZE);
  7263. }
  7264. }
  7265. #endif // DEBUG_ENABLED
  7266. ShaderNode::Uniform::Scope uniform_scope = ShaderNode::Uniform::SCOPE_LOCAL;
  7267. stages = &p_functions;
  7268. is_supported_frag_only_funcs = shader_type_identifier == "canvas_item" || shader_type_identifier == "spatial" || shader_type_identifier == "sky";
  7269. const FunctionInfo &constants = p_functions.has("constants") ? p_functions["constants"] : FunctionInfo();
  7270. HashMap<String, String> defined_modes;
  7271. while (tk.type != TK_EOF) {
  7272. switch (tk.type) {
  7273. case TK_RENDER_MODE: {
  7274. #ifdef DEBUG_ENABLED
  7275. keyword_completion_context = CF_UNSPECIFIED;
  7276. #endif // DEBUG_ENABLED
  7277. while (true) {
  7278. StringName mode;
  7279. _get_completable_identifier(nullptr, COMPLETION_RENDER_MODE, mode);
  7280. if (mode == StringName()) {
  7281. _set_error(RTR("Expected an identifier for render mode."));
  7282. return ERR_PARSE_ERROR;
  7283. }
  7284. const String smode = String(mode);
  7285. if (shader->render_modes.has(mode)) {
  7286. _set_error(vformat(RTR("Duplicated render mode: '%s'."), smode));
  7287. return ERR_PARSE_ERROR;
  7288. }
  7289. bool found = false;
  7290. if (is_shader_inc) {
  7291. for (int i = 0; i < RenderingServer::SHADER_MAX; i++) {
  7292. const Vector<ModeInfo> modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(i));
  7293. for (int j = 0; j < modes.size(); j++) {
  7294. const ModeInfo &info = modes[j];
  7295. const String name = String(info.name);
  7296. if (smode.begins_with(name)) {
  7297. if (!info.options.is_empty()) {
  7298. if (info.options.has(smode.substr(name.length() + 1))) {
  7299. found = true;
  7300. if (defined_modes.has(name)) {
  7301. _set_error(vformat(RTR("Redefinition of render mode: '%s'. The '%s' mode has already been set to '%s'."), smode, name, defined_modes[name]));
  7302. return ERR_PARSE_ERROR;
  7303. }
  7304. defined_modes.insert(name, smode);
  7305. break;
  7306. }
  7307. } else {
  7308. found = true;
  7309. break;
  7310. }
  7311. }
  7312. }
  7313. }
  7314. } else {
  7315. for (int i = 0; i < p_render_modes.size(); i++) {
  7316. const ModeInfo &info = p_render_modes[i];
  7317. const String name = String(info.name);
  7318. if (smode.begins_with(name)) {
  7319. if (!info.options.is_empty()) {
  7320. if (info.options.has(smode.substr(name.length() + 1))) {
  7321. found = true;
  7322. if (defined_modes.has(name)) {
  7323. _set_error(vformat(RTR("Redefinition of render mode: '%s'. The '%s' mode has already been set to '%s'."), smode, name, defined_modes[name]));
  7324. return ERR_PARSE_ERROR;
  7325. }
  7326. defined_modes.insert(name, smode);
  7327. break;
  7328. }
  7329. } else {
  7330. found = true;
  7331. break;
  7332. }
  7333. }
  7334. }
  7335. }
  7336. if (!found) {
  7337. _set_error(vformat(RTR("Invalid render mode: '%s'."), smode));
  7338. return ERR_PARSE_ERROR;
  7339. }
  7340. shader->render_modes.push_back(mode);
  7341. tk = _get_token();
  7342. if (tk.type == TK_COMMA) {
  7343. //all good, do nothing
  7344. } else if (tk.type == TK_SEMICOLON) {
  7345. break; //done
  7346. } else {
  7347. _set_error(vformat(RTR("Unexpected token: '%s'."), get_token_text(tk)));
  7348. return ERR_PARSE_ERROR;
  7349. }
  7350. }
  7351. #ifdef DEBUG_ENABLED
  7352. keyword_completion_context = CF_GLOBAL_SPACE;
  7353. #endif // DEBUG_ENABLED
  7354. } break;
  7355. case TK_STRUCT: {
  7356. ShaderNode::Struct st;
  7357. DataType type;
  7358. #ifdef DEBUG_ENABLED
  7359. keyword_completion_context = CF_UNSPECIFIED;
  7360. #endif // DEBUG_ENABLED
  7361. tk = _get_token();
  7362. if (tk.type == TK_IDENTIFIER) {
  7363. st.name = tk.text;
  7364. if (shader->constants.has(st.name) || shader->structs.has(st.name)) {
  7365. _set_redefinition_error(String(st.name));
  7366. return ERR_PARSE_ERROR;
  7367. }
  7368. tk = _get_token();
  7369. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  7370. _set_expected_error("{");
  7371. return ERR_PARSE_ERROR;
  7372. }
  7373. } else {
  7374. _set_error(RTR("Expected a struct identifier."));
  7375. return ERR_PARSE_ERROR;
  7376. }
  7377. StructNode *st_node = alloc_node<StructNode>();
  7378. st.shader_struct = st_node;
  7379. int member_count = 0;
  7380. HashSet<String> member_names;
  7381. while (true) { // variables list
  7382. #ifdef DEBUG_ENABLED
  7383. keyword_completion_context = CF_DATATYPE | CF_PRECISION_MODIFIER;
  7384. #endif // DEBUG_ENABLED
  7385. tk = _get_token();
  7386. if (tk.type == TK_CURLY_BRACKET_CLOSE) {
  7387. break;
  7388. }
  7389. StringName struct_name = "";
  7390. bool struct_dt = false;
  7391. DataPrecision precision = PRECISION_DEFAULT;
  7392. if (tk.type == TK_STRUCT) {
  7393. _set_error(RTR("Nested structs are not allowed."));
  7394. return ERR_PARSE_ERROR;
  7395. }
  7396. if (is_token_precision(tk.type)) {
  7397. precision = get_token_precision(tk.type);
  7398. tk = _get_token();
  7399. #ifdef DEBUG_ENABLED
  7400. keyword_completion_context ^= CF_PRECISION_MODIFIER;
  7401. #endif // DEBUG_ENABLED
  7402. }
  7403. if (shader->structs.has(tk.text)) {
  7404. struct_name = tk.text;
  7405. #ifdef DEBUG_ENABLED
  7406. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_STRUCT_FLAG) && used_structs.has(struct_name)) {
  7407. used_structs[struct_name].used = true;
  7408. }
  7409. #endif // DEBUG_ENABLED
  7410. struct_dt = true;
  7411. }
  7412. if (!is_token_datatype(tk.type) && !struct_dt) {
  7413. _set_error(RTR("Expected data type."));
  7414. return ERR_PARSE_ERROR;
  7415. } else {
  7416. type = struct_dt ? TYPE_STRUCT : get_token_datatype(tk.type);
  7417. if (precision != PRECISION_DEFAULT && _validate_precision(type, precision) != OK) {
  7418. return ERR_PARSE_ERROR;
  7419. }
  7420. if (type == TYPE_VOID || is_sampler_type(type)) {
  7421. _set_error(vformat(RTR("A '%s' data type is not allowed here."), get_datatype_name(type)));
  7422. return ERR_PARSE_ERROR;
  7423. }
  7424. #ifdef DEBUG_ENABLED
  7425. keyword_completion_context = CF_UNSPECIFIED;
  7426. #endif // DEBUG_ENABLED
  7427. bool first = true;
  7428. bool fixed_array_size = false;
  7429. int array_size = 0;
  7430. do {
  7431. tk = _get_token();
  7432. if (first) {
  7433. first = false;
  7434. if (tk.type != TK_IDENTIFIER && tk.type != TK_BRACKET_OPEN) {
  7435. _set_error(RTR("Expected an identifier or '['."));
  7436. return ERR_PARSE_ERROR;
  7437. }
  7438. if (tk.type == TK_BRACKET_OPEN) {
  7439. Error error = _parse_array_size(nullptr, constants, true, nullptr, &array_size, nullptr);
  7440. if (error != OK) {
  7441. return error;
  7442. }
  7443. fixed_array_size = true;
  7444. tk = _get_token();
  7445. }
  7446. }
  7447. if (tk.type != TK_IDENTIFIER) {
  7448. _set_error(RTR("Expected an identifier."));
  7449. return ERR_PARSE_ERROR;
  7450. }
  7451. MemberNode *member = alloc_node<MemberNode>();
  7452. member->precision = precision;
  7453. member->datatype = type;
  7454. member->struct_name = struct_name;
  7455. member->name = tk.text;
  7456. member->array_size = array_size;
  7457. if (member_names.has(member->name)) {
  7458. _set_redefinition_error(String(member->name));
  7459. return ERR_PARSE_ERROR;
  7460. }
  7461. member_names.insert(member->name);
  7462. tk = _get_token();
  7463. if (tk.type == TK_BRACKET_OPEN) {
  7464. Error error = _parse_array_size(nullptr, constants, true, nullptr, &member->array_size, nullptr);
  7465. if (error != OK) {
  7466. return error;
  7467. }
  7468. tk = _get_token();
  7469. }
  7470. if (!fixed_array_size) {
  7471. array_size = 0;
  7472. }
  7473. if (tk.type != TK_SEMICOLON && tk.type != TK_COMMA) {
  7474. _set_expected_error(",", ";");
  7475. return ERR_PARSE_ERROR;
  7476. }
  7477. st_node->members.push_back(member);
  7478. member_count++;
  7479. } while (tk.type == TK_COMMA); // another member
  7480. }
  7481. }
  7482. if (member_count == 0) {
  7483. _set_error(RTR("Empty structs are not allowed."));
  7484. return ERR_PARSE_ERROR;
  7485. }
  7486. #ifdef DEBUG_ENABLED
  7487. keyword_completion_context = CF_UNSPECIFIED;
  7488. #endif // DEBUG_ENABLED
  7489. tk = _get_token();
  7490. if (tk.type != TK_SEMICOLON) {
  7491. _set_expected_error(";");
  7492. return ERR_PARSE_ERROR;
  7493. }
  7494. #ifdef DEBUG_ENABLED
  7495. keyword_completion_context = CF_GLOBAL_SPACE;
  7496. #endif // DEBUG_ENABLED
  7497. shader->structs[st.name] = st;
  7498. shader->vstructs.push_back(st); // struct's order is important!
  7499. #ifdef DEBUG_ENABLED
  7500. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_STRUCT_FLAG)) {
  7501. used_structs.insert(st.name, Usage(tk_line));
  7502. }
  7503. #endif // DEBUG_ENABLED
  7504. } break;
  7505. case TK_GLOBAL: {
  7506. #ifdef DEBUG_ENABLED
  7507. keyword_completion_context = CF_UNIFORM_KEYWORD;
  7508. if (_lookup_next(next)) {
  7509. if (next.type == TK_UNIFORM) {
  7510. keyword_completion_context ^= CF_UNIFORM_KEYWORD;
  7511. }
  7512. }
  7513. #endif // DEBUG_ENABLED
  7514. tk = _get_token();
  7515. if (tk.type != TK_UNIFORM) {
  7516. _set_expected_after_error("uniform", "global");
  7517. return ERR_PARSE_ERROR;
  7518. }
  7519. uniform_scope = ShaderNode::Uniform::SCOPE_GLOBAL;
  7520. };
  7521. [[fallthrough]];
  7522. case TK_INSTANCE: {
  7523. if (tk.type == TK_INSTANCE) {
  7524. #ifdef DEBUG_ENABLED
  7525. keyword_completion_context = CF_UNIFORM_KEYWORD;
  7526. if (_lookup_next(next)) {
  7527. if (next.type == TK_UNIFORM) {
  7528. keyword_completion_context ^= CF_UNIFORM_KEYWORD;
  7529. }
  7530. }
  7531. #endif // DEBUG_ENABLED
  7532. if (shader_type_identifier != StringName() && String(shader_type_identifier) != "spatial") {
  7533. _set_error(vformat(RTR("Uniform instances are not yet implemented for '%s' shaders."), shader_type_identifier));
  7534. return ERR_PARSE_ERROR;
  7535. }
  7536. if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  7537. _set_error(RTR("Uniform instances are not supported in gl_compatibility shaders."));
  7538. return ERR_PARSE_ERROR;
  7539. }
  7540. if (uniform_scope == ShaderNode::Uniform::SCOPE_LOCAL) {
  7541. tk = _get_token();
  7542. if (tk.type != TK_UNIFORM) {
  7543. _set_expected_after_error("uniform", "instance");
  7544. return ERR_PARSE_ERROR;
  7545. }
  7546. uniform_scope = ShaderNode::Uniform::SCOPE_INSTANCE;
  7547. }
  7548. }
  7549. };
  7550. [[fallthrough]];
  7551. case TK_UNIFORM:
  7552. case TK_VARYING: {
  7553. bool is_uniform = tk.type == TK_UNIFORM;
  7554. #ifdef DEBUG_ENABLED
  7555. keyword_completion_context = CF_UNSPECIFIED;
  7556. #endif // DEBUG_ENABLED
  7557. if (!is_uniform) {
  7558. if (shader_type_identifier == "particles" || shader_type_identifier == "sky" || shader_type_identifier == "fog") {
  7559. _set_error(vformat(RTR("Varyings cannot be used in '%s' shaders."), shader_type_identifier));
  7560. return ERR_PARSE_ERROR;
  7561. }
  7562. }
  7563. DataPrecision precision = PRECISION_DEFAULT;
  7564. DataInterpolation interpolation = INTERPOLATION_DEFAULT;
  7565. DataType type;
  7566. StringName name;
  7567. int array_size = 0;
  7568. tk = _get_token();
  7569. #ifdef DEBUG_ENABLED
  7570. bool temp_error = false;
  7571. uint32_t datatype_flag;
  7572. if (!is_uniform) {
  7573. datatype_flag = CF_VARYING_TYPE;
  7574. keyword_completion_context = CF_INTERPOLATION_QUALIFIER | CF_PRECISION_MODIFIER | datatype_flag;
  7575. if (_lookup_next(next)) {
  7576. if (is_token_interpolation(next.type)) {
  7577. keyword_completion_context ^= (CF_INTERPOLATION_QUALIFIER | datatype_flag);
  7578. } else if (is_token_precision(next.type)) {
  7579. keyword_completion_context ^= (CF_PRECISION_MODIFIER | datatype_flag);
  7580. } else if (is_token_datatype(next.type)) {
  7581. keyword_completion_context ^= datatype_flag;
  7582. }
  7583. }
  7584. } else {
  7585. datatype_flag = CF_UNIFORM_TYPE;
  7586. keyword_completion_context = CF_PRECISION_MODIFIER | datatype_flag;
  7587. if (_lookup_next(next)) {
  7588. if (is_token_precision(next.type)) {
  7589. keyword_completion_context ^= (CF_PRECISION_MODIFIER | datatype_flag);
  7590. } else if (is_token_datatype(next.type)) {
  7591. keyword_completion_context ^= datatype_flag;
  7592. }
  7593. }
  7594. }
  7595. #endif // DEBUG_ENABLED
  7596. if (is_token_interpolation(tk.type)) {
  7597. if (is_uniform) {
  7598. _set_error(RTR("Interpolation qualifiers are not supported for uniforms."));
  7599. #ifdef DEBUG_ENABLED
  7600. temp_error = true;
  7601. #else
  7602. return ERR_PARSE_ERROR;
  7603. #endif // DEBUG_ENABLED
  7604. }
  7605. interpolation = get_token_interpolation(tk.type);
  7606. tk = _get_token();
  7607. #ifdef DEBUG_ENABLED
  7608. if (keyword_completion_context & CF_INTERPOLATION_QUALIFIER) {
  7609. keyword_completion_context ^= CF_INTERPOLATION_QUALIFIER;
  7610. }
  7611. if (_lookup_next(next)) {
  7612. if (is_token_precision(next.type)) {
  7613. keyword_completion_context ^= CF_PRECISION_MODIFIER;
  7614. } else if (is_token_datatype(next.type)) {
  7615. keyword_completion_context ^= datatype_flag;
  7616. }
  7617. }
  7618. if (temp_error) {
  7619. return ERR_PARSE_ERROR;
  7620. }
  7621. #endif // DEBUG_ENABLED
  7622. }
  7623. if (is_token_precision(tk.type)) {
  7624. precision = get_token_precision(tk.type);
  7625. tk = _get_token();
  7626. #ifdef DEBUG_ENABLED
  7627. if (keyword_completion_context & CF_INTERPOLATION_QUALIFIER) {
  7628. keyword_completion_context ^= CF_INTERPOLATION_QUALIFIER;
  7629. }
  7630. if (keyword_completion_context & CF_PRECISION_MODIFIER) {
  7631. keyword_completion_context ^= CF_PRECISION_MODIFIER;
  7632. }
  7633. if (_lookup_next(next)) {
  7634. if (is_token_datatype(next.type)) {
  7635. keyword_completion_context = CF_UNSPECIFIED;
  7636. }
  7637. }
  7638. #endif // DEBUG_ENABLED
  7639. }
  7640. if (shader->structs.has(tk.text)) {
  7641. if (is_uniform) {
  7642. _set_error(vformat(RTR("The '%s' data type is not supported for uniforms."), "struct"));
  7643. return ERR_PARSE_ERROR;
  7644. } else {
  7645. _set_error(vformat(RTR("The '%s' data type is not allowed here."), "struct"));
  7646. return ERR_PARSE_ERROR;
  7647. }
  7648. }
  7649. if (!is_token_datatype(tk.type)) {
  7650. _set_error(RTR("Expected data type."));
  7651. return ERR_PARSE_ERROR;
  7652. }
  7653. type = get_token_datatype(tk.type);
  7654. if (precision != PRECISION_DEFAULT && _validate_precision(type, precision) != OK) {
  7655. return ERR_PARSE_ERROR;
  7656. }
  7657. if (type == TYPE_VOID) {
  7658. _set_error(vformat(RTR("The '%s' data type is not allowed here."), "void"));
  7659. return ERR_PARSE_ERROR;
  7660. }
  7661. if (!is_uniform && interpolation != INTERPOLATION_DEFAULT && type < TYPE_INT) {
  7662. _set_error(vformat(RTR("Interpolation modifier '%s' cannot be used with boolean types."), get_interpolation_name(interpolation)));
  7663. return ERR_PARSE_ERROR;
  7664. }
  7665. if (!is_uniform && type > TYPE_MAT4) {
  7666. _set_error(RTR("Invalid data type for varying."));
  7667. return ERR_PARSE_ERROR;
  7668. }
  7669. #ifdef DEBUG_ENABLED
  7670. keyword_completion_context = CF_UNSPECIFIED;
  7671. #endif // DEBUG_ENABLED
  7672. tk = _get_token();
  7673. if (tk.type != TK_IDENTIFIER && tk.type != TK_BRACKET_OPEN) {
  7674. _set_error(RTR("Expected an identifier or '['."));
  7675. return ERR_PARSE_ERROR;
  7676. }
  7677. if (tk.type == TK_BRACKET_OPEN) {
  7678. Error error = _parse_array_size(nullptr, constants, true, nullptr, &array_size, nullptr);
  7679. if (error != OK) {
  7680. return error;
  7681. }
  7682. tk = _get_token();
  7683. }
  7684. if (tk.type != TK_IDENTIFIER) {
  7685. _set_error(RTR("Expected an identifier."));
  7686. return ERR_PARSE_ERROR;
  7687. }
  7688. prev_pos = _get_tkpos();
  7689. name = tk.text;
  7690. if (_find_identifier(nullptr, false, constants, name)) {
  7691. _set_redefinition_error(String(name));
  7692. return ERR_PARSE_ERROR;
  7693. }
  7694. if (has_builtin(p_functions, name)) {
  7695. _set_redefinition_error(String(name));
  7696. return ERR_PARSE_ERROR;
  7697. }
  7698. if (is_uniform) {
  7699. if (uniform_scope == ShaderNode::Uniform::SCOPE_GLOBAL && Engine::get_singleton()->is_editor_hint()) { // Type checking for global uniforms is not allowed outside the editor.
  7700. //validate global uniform
  7701. DataType gvtype = global_shader_uniform_get_type_func(name);
  7702. if (gvtype == TYPE_MAX) {
  7703. _set_error(vformat(RTR("Global uniform '%s' does not exist. Create it in Project Settings."), String(name)));
  7704. return ERR_PARSE_ERROR;
  7705. }
  7706. if (type != gvtype) {
  7707. _set_error(vformat(RTR("Global uniform '%s' must be of type '%s'."), String(name), get_datatype_name(gvtype)));
  7708. return ERR_PARSE_ERROR;
  7709. }
  7710. }
  7711. ShaderNode::Uniform uniform;
  7712. uniform.type = type;
  7713. uniform.scope = uniform_scope;
  7714. uniform.precision = precision;
  7715. uniform.array_size = array_size;
  7716. uniform.group = current_uniform_group_name;
  7717. uniform.subgroup = current_uniform_subgroup_name;
  7718. tk = _get_token();
  7719. if (tk.type == TK_BRACKET_OPEN) {
  7720. Error error = _parse_array_size(nullptr, constants, true, nullptr, &uniform.array_size, nullptr);
  7721. if (error != OK) {
  7722. return error;
  7723. }
  7724. tk = _get_token();
  7725. }
  7726. if (is_sampler_type(type)) {
  7727. if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE) {
  7728. _set_error(vformat(RTR("The '%s' qualifier is not supported for sampler types."), "SCOPE_INSTANCE"));
  7729. return ERR_PARSE_ERROR;
  7730. }
  7731. uniform.texture_order = texture_uniforms++;
  7732. uniform.texture_binding = texture_binding;
  7733. if (uniform.array_size > 0) {
  7734. texture_binding += uniform.array_size;
  7735. } else {
  7736. ++texture_binding;
  7737. }
  7738. uniform.order = -1;
  7739. } else {
  7740. if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE && (type == TYPE_MAT2 || type == TYPE_MAT3 || type == TYPE_MAT4)) {
  7741. _set_error(vformat(RTR("The '%s' qualifier is not supported for matrix types."), "SCOPE_INSTANCE"));
  7742. return ERR_PARSE_ERROR;
  7743. }
  7744. uniform.texture_order = -1;
  7745. if (uniform_scope != ShaderNode::Uniform::SCOPE_INSTANCE) {
  7746. uniform.order = uniforms++;
  7747. #ifdef DEBUG_ENABLED
  7748. if (check_device_limit_warnings) {
  7749. if (uniform.array_size > 0) {
  7750. int size = get_datatype_size(uniform.type) * uniform.array_size;
  7751. int m = (16 * uniform.array_size);
  7752. if ((size % m) != 0U) {
  7753. size += m - (size % m);
  7754. }
  7755. uniform_buffer_size += size;
  7756. } else {
  7757. uniform_buffer_size += get_datatype_size(uniform.type);
  7758. }
  7759. if (uniform_buffer_exceeded_line == -1 && uniform_buffer_size > max_uniform_buffer_size) {
  7760. uniform_buffer_exceeded_line = tk_line;
  7761. }
  7762. }
  7763. #endif // DEBUG_ENABLED
  7764. }
  7765. }
  7766. if (uniform.array_size > 0) {
  7767. if (uniform_scope == ShaderNode::Uniform::SCOPE_GLOBAL) {
  7768. _set_error(vformat(RTR("The '%s' qualifier is not supported for uniform arrays."), "SCOPE_GLOBAL"));
  7769. return ERR_PARSE_ERROR;
  7770. }
  7771. if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE) {
  7772. _set_error(vformat(RTR("The '%s' qualifier is not supported for uniform arrays."), "SCOPE_INSTANCE"));
  7773. return ERR_PARSE_ERROR;
  7774. }
  7775. }
  7776. int custom_instance_index = -1;
  7777. if (tk.type == TK_COLON) {
  7778. completion_type = COMPLETION_HINT;
  7779. completion_base = type;
  7780. completion_base_array = uniform.array_size > 0;
  7781. //hint
  7782. do {
  7783. tk = _get_token();
  7784. completion_line = tk.line;
  7785. if (!is_token_hint(tk.type)) {
  7786. _set_error(RTR("Expected valid type hint after ':'."));
  7787. return ERR_PARSE_ERROR;
  7788. }
  7789. if (uniform.array_size > 0) {
  7790. static Vector<int> supported_hints = {
  7791. TK_HINT_SOURCE_COLOR, TK_REPEAT_DISABLE, TK_REPEAT_ENABLE,
  7792. TK_FILTER_LINEAR, TK_FILTER_LINEAR_MIPMAP, TK_FILTER_LINEAR_MIPMAP_ANISOTROPIC,
  7793. TK_FILTER_NEAREST, TK_FILTER_NEAREST_MIPMAP, TK_FILTER_NEAREST_MIPMAP_ANISOTROPIC
  7794. };
  7795. if (!supported_hints.has(tk.type)) {
  7796. _set_error(RTR("This hint is not supported for uniform arrays."));
  7797. return ERR_PARSE_ERROR;
  7798. }
  7799. }
  7800. ShaderNode::Uniform::Hint new_hint = ShaderNode::Uniform::HINT_NONE;
  7801. TextureFilter new_filter = FILTER_DEFAULT;
  7802. TextureRepeat new_repeat = REPEAT_DEFAULT;
  7803. switch (tk.type) {
  7804. case TK_HINT_SOURCE_COLOR: {
  7805. if (type != TYPE_VEC3 && type != TYPE_VEC4 && !is_sampler_type(type)) {
  7806. _set_error(vformat(RTR("Source color hint is for '%s', '%s' or sampler types only."), "vec3", "vec4"));
  7807. return ERR_PARSE_ERROR;
  7808. }
  7809. if (is_sampler_type(type)) {
  7810. if (uniform.use_color) {
  7811. _set_error(vformat(RTR("Duplicated hint: '%s'."), "source_color"));
  7812. return ERR_PARSE_ERROR;
  7813. }
  7814. uniform.use_color = true;
  7815. } else {
  7816. new_hint = ShaderNode::Uniform::HINT_SOURCE_COLOR;
  7817. }
  7818. } break;
  7819. case TK_HINT_DEFAULT_BLACK_TEXTURE: {
  7820. new_hint = ShaderNode::Uniform::HINT_DEFAULT_BLACK;
  7821. } break;
  7822. case TK_HINT_DEFAULT_WHITE_TEXTURE: {
  7823. new_hint = ShaderNode::Uniform::HINT_DEFAULT_WHITE;
  7824. } break;
  7825. case TK_HINT_DEFAULT_TRANSPARENT_TEXTURE: {
  7826. new_hint = ShaderNode::Uniform::HINT_DEFAULT_TRANSPARENT;
  7827. } break;
  7828. case TK_HINT_NORMAL_TEXTURE: {
  7829. new_hint = ShaderNode::Uniform::HINT_NORMAL;
  7830. } break;
  7831. case TK_HINT_ROUGHNESS_NORMAL_TEXTURE: {
  7832. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL;
  7833. } break;
  7834. case TK_HINT_ROUGHNESS_R: {
  7835. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_R;
  7836. } break;
  7837. case TK_HINT_ROUGHNESS_G: {
  7838. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_G;
  7839. } break;
  7840. case TK_HINT_ROUGHNESS_B: {
  7841. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_B;
  7842. } break;
  7843. case TK_HINT_ROUGHNESS_A: {
  7844. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_A;
  7845. } break;
  7846. case TK_HINT_ROUGHNESS_GRAY: {
  7847. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_GRAY;
  7848. } break;
  7849. case TK_HINT_ANISOTROPY_TEXTURE: {
  7850. new_hint = ShaderNode::Uniform::HINT_ANISOTROPY;
  7851. } break;
  7852. case TK_HINT_RANGE: {
  7853. if (type != TYPE_FLOAT && type != TYPE_INT) {
  7854. _set_error(vformat(RTR("Range hint is for '%s' and '%s' only."), "float", "int"));
  7855. return ERR_PARSE_ERROR;
  7856. }
  7857. tk = _get_token();
  7858. if (tk.type != TK_PARENTHESIS_OPEN) {
  7859. _set_expected_after_error("(", "hint_range");
  7860. return ERR_PARSE_ERROR;
  7861. }
  7862. tk = _get_token();
  7863. float sign = 1.0;
  7864. if (tk.type == TK_OP_SUB) {
  7865. sign = -1.0;
  7866. tk = _get_token();
  7867. }
  7868. if (tk.type != TK_FLOAT_CONSTANT && !tk.is_integer_constant()) {
  7869. _set_error(RTR("Expected an integer constant."));
  7870. return ERR_PARSE_ERROR;
  7871. }
  7872. uniform.hint_range[0] = tk.constant;
  7873. uniform.hint_range[0] *= sign;
  7874. tk = _get_token();
  7875. if (tk.type != TK_COMMA) {
  7876. _set_error(RTR("Expected ',' after integer constant."));
  7877. return ERR_PARSE_ERROR;
  7878. }
  7879. tk = _get_token();
  7880. sign = 1.0;
  7881. if (tk.type == TK_OP_SUB) {
  7882. sign = -1.0;
  7883. tk = _get_token();
  7884. }
  7885. if (tk.type != TK_FLOAT_CONSTANT && !tk.is_integer_constant()) {
  7886. _set_error(RTR("Expected an integer constant after ','."));
  7887. return ERR_PARSE_ERROR;
  7888. }
  7889. uniform.hint_range[1] = tk.constant;
  7890. uniform.hint_range[1] *= sign;
  7891. tk = _get_token();
  7892. if (tk.type == TK_COMMA) {
  7893. tk = _get_token();
  7894. if (tk.type != TK_FLOAT_CONSTANT && !tk.is_integer_constant()) {
  7895. _set_error(RTR("Expected an integer constant after ','."));
  7896. return ERR_PARSE_ERROR;
  7897. }
  7898. uniform.hint_range[2] = tk.constant;
  7899. tk = _get_token();
  7900. } else {
  7901. if (type == TYPE_INT) {
  7902. uniform.hint_range[2] = 1;
  7903. } else {
  7904. uniform.hint_range[2] = 0.001;
  7905. }
  7906. }
  7907. if (tk.type != TK_PARENTHESIS_CLOSE) {
  7908. _set_expected_error(")");
  7909. return ERR_PARSE_ERROR;
  7910. }
  7911. new_hint = ShaderNode::Uniform::HINT_RANGE;
  7912. } break;
  7913. case TK_HINT_INSTANCE_INDEX: {
  7914. if (custom_instance_index != -1) {
  7915. _set_error(vformat(RTR("Can only specify '%s' once."), "instance_index"));
  7916. return ERR_PARSE_ERROR;
  7917. }
  7918. tk = _get_token();
  7919. if (tk.type != TK_PARENTHESIS_OPEN) {
  7920. _set_expected_after_error("(", "instance_index");
  7921. return ERR_PARSE_ERROR;
  7922. }
  7923. tk = _get_token();
  7924. if (tk.type == TK_OP_SUB) {
  7925. _set_error(RTR("The instance index can't be negative."));
  7926. return ERR_PARSE_ERROR;
  7927. }
  7928. if (!tk.is_integer_constant()) {
  7929. _set_error(RTR("Expected an integer constant."));
  7930. return ERR_PARSE_ERROR;
  7931. }
  7932. custom_instance_index = tk.constant;
  7933. current_uniform_instance_index_defined = true;
  7934. if (custom_instance_index >= MAX_INSTANCE_UNIFORM_INDICES) {
  7935. _set_error(vformat(RTR("Allowed instance uniform indices must be within [0..%d] range."), MAX_INSTANCE_UNIFORM_INDICES - 1));
  7936. return ERR_PARSE_ERROR;
  7937. }
  7938. tk = _get_token();
  7939. if (tk.type != TK_PARENTHESIS_CLOSE) {
  7940. _set_expected_error(")");
  7941. return ERR_PARSE_ERROR;
  7942. }
  7943. } break;
  7944. case TK_HINT_SCREEN_TEXTURE: {
  7945. new_hint = ShaderNode::Uniform::HINT_SCREEN_TEXTURE;
  7946. --texture_uniforms;
  7947. --texture_binding;
  7948. } break;
  7949. case TK_HINT_NORMAL_ROUGHNESS_TEXTURE: {
  7950. new_hint = ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE;
  7951. --texture_uniforms;
  7952. --texture_binding;
  7953. if (OS::get_singleton()->get_current_rendering_method() != "forward_plus") {
  7954. _set_error(RTR("'hint_normal_roughness_texture' is only available when using the Forward+ backend."));
  7955. return ERR_PARSE_ERROR;
  7956. }
  7957. if (shader_type_identifier != StringName() && String(shader_type_identifier) != "spatial") {
  7958. _set_error(vformat(RTR("'hint_normal_roughness_texture' is not supported in '%s' shaders."), shader_type_identifier));
  7959. return ERR_PARSE_ERROR;
  7960. }
  7961. } break;
  7962. case TK_HINT_DEPTH_TEXTURE: {
  7963. new_hint = ShaderNode::Uniform::HINT_DEPTH_TEXTURE;
  7964. --texture_uniforms;
  7965. --texture_binding;
  7966. if (shader_type_identifier != StringName() && String(shader_type_identifier) != "spatial") {
  7967. _set_error(vformat(RTR("'hint_depth_texture' is not supported in '%s' shaders."), shader_type_identifier));
  7968. return ERR_PARSE_ERROR;
  7969. }
  7970. } break;
  7971. case TK_FILTER_NEAREST: {
  7972. new_filter = FILTER_NEAREST;
  7973. } break;
  7974. case TK_FILTER_LINEAR: {
  7975. new_filter = FILTER_LINEAR;
  7976. } break;
  7977. case TK_FILTER_NEAREST_MIPMAP: {
  7978. new_filter = FILTER_NEAREST_MIPMAP;
  7979. } break;
  7980. case TK_FILTER_LINEAR_MIPMAP: {
  7981. new_filter = FILTER_LINEAR_MIPMAP;
  7982. } break;
  7983. case TK_FILTER_NEAREST_MIPMAP_ANISOTROPIC: {
  7984. new_filter = FILTER_NEAREST_MIPMAP_ANISOTROPIC;
  7985. } break;
  7986. case TK_FILTER_LINEAR_MIPMAP_ANISOTROPIC: {
  7987. new_filter = FILTER_LINEAR_MIPMAP_ANISOTROPIC;
  7988. } break;
  7989. case TK_REPEAT_DISABLE: {
  7990. new_repeat = REPEAT_DISABLE;
  7991. } break;
  7992. case TK_REPEAT_ENABLE: {
  7993. new_repeat = REPEAT_ENABLE;
  7994. } break;
  7995. default:
  7996. break;
  7997. }
  7998. if (((new_filter != FILTER_DEFAULT || new_repeat != REPEAT_DEFAULT) || (new_hint != ShaderNode::Uniform::HINT_NONE && new_hint != ShaderNode::Uniform::HINT_SOURCE_COLOR && new_hint != ShaderNode::Uniform::HINT_RANGE)) && !is_sampler_type(type)) {
  7999. _set_error(RTR("This hint is only for sampler types."));
  8000. return ERR_PARSE_ERROR;
  8001. }
  8002. if (new_hint != ShaderNode::Uniform::HINT_NONE) {
  8003. if (uniform.hint != ShaderNode::Uniform::HINT_NONE) {
  8004. if (uniform.hint == new_hint) {
  8005. _set_error(vformat(RTR("Duplicated hint: '%s'."), get_uniform_hint_name(new_hint)));
  8006. } else {
  8007. _set_error(vformat(RTR("Redefinition of hint: '%s'. The hint has already been set to '%s'."), get_uniform_hint_name(new_hint), get_uniform_hint_name(uniform.hint)));
  8008. }
  8009. return ERR_PARSE_ERROR;
  8010. } else {
  8011. uniform.hint = new_hint;
  8012. current_uniform_hint = new_hint;
  8013. }
  8014. }
  8015. if (new_filter != FILTER_DEFAULT) {
  8016. if (uniform.filter != FILTER_DEFAULT) {
  8017. if (uniform.filter == new_filter) {
  8018. _set_error(vformat(RTR("Duplicated filter mode: '%s'."), get_texture_filter_name(new_filter)));
  8019. } else {
  8020. _set_error(vformat(RTR("Redefinition of filter mode: '%s'. The filter mode has already been set to '%s'."), get_texture_filter_name(new_filter), get_texture_filter_name(uniform.filter)));
  8021. }
  8022. return ERR_PARSE_ERROR;
  8023. } else {
  8024. uniform.filter = new_filter;
  8025. current_uniform_filter = new_filter;
  8026. }
  8027. }
  8028. if (new_repeat != REPEAT_DEFAULT) {
  8029. if (uniform.repeat != REPEAT_DEFAULT) {
  8030. if (uniform.repeat == new_repeat) {
  8031. _set_error(vformat(RTR("Duplicated repeat mode: '%s'."), get_texture_repeat_name(new_repeat)));
  8032. } else {
  8033. _set_error(vformat(RTR("Redefinition of repeat mode: '%s'. The repeat mode has already been set to '%s'."), get_texture_repeat_name(new_repeat), get_texture_repeat_name(uniform.repeat)));
  8034. }
  8035. return ERR_PARSE_ERROR;
  8036. } else {
  8037. uniform.repeat = new_repeat;
  8038. current_uniform_repeat = new_repeat;
  8039. }
  8040. }
  8041. tk = _get_token();
  8042. } while (tk.type == TK_COMMA);
  8043. }
  8044. if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE) {
  8045. if (custom_instance_index >= 0) {
  8046. uniform.instance_index = custom_instance_index;
  8047. } else {
  8048. uniform.instance_index = instance_index++;
  8049. if (instance_index > MAX_INSTANCE_UNIFORM_INDICES) {
  8050. _set_error(vformat(RTR("Too many '%s' uniforms in shader, maximum supported is %d."), "instance", MAX_INSTANCE_UNIFORM_INDICES));
  8051. return ERR_PARSE_ERROR;
  8052. }
  8053. }
  8054. }
  8055. //reset scope for next uniform
  8056. if (tk.type == TK_OP_ASSIGN) {
  8057. if (uniform.array_size > 0) {
  8058. _set_error(RTR("Setting default values to uniform arrays is not supported."));
  8059. return ERR_PARSE_ERROR;
  8060. }
  8061. Node *expr = _parse_and_reduce_expression(nullptr, constants);
  8062. if (!expr) {
  8063. return ERR_PARSE_ERROR;
  8064. }
  8065. if (expr->type != Node::NODE_TYPE_CONSTANT) {
  8066. _set_error(RTR("Expected constant expression after '='."));
  8067. return ERR_PARSE_ERROR;
  8068. }
  8069. ConstantNode *cn = static_cast<ConstantNode *>(expr);
  8070. uniform.default_value.resize(cn->values.size());
  8071. if (!convert_constant(cn, uniform.type, uniform.default_value.ptrw())) {
  8072. _set_error(vformat(RTR("Can't convert constant to '%s'."), get_datatype_name(uniform.type)));
  8073. return ERR_PARSE_ERROR;
  8074. }
  8075. tk = _get_token();
  8076. }
  8077. shader->uniforms[name] = uniform;
  8078. #ifdef DEBUG_ENABLED
  8079. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_UNIFORM_FLAG)) {
  8080. used_uniforms.insert(name, Usage(tk_line));
  8081. }
  8082. #endif // DEBUG_ENABLED
  8083. //reset scope for next uniform
  8084. uniform_scope = ShaderNode::Uniform::SCOPE_LOCAL;
  8085. if (tk.type != TK_SEMICOLON) {
  8086. _set_expected_error(";");
  8087. return ERR_PARSE_ERROR;
  8088. }
  8089. #ifdef DEBUG_ENABLED
  8090. keyword_completion_context = CF_GLOBAL_SPACE;
  8091. #endif // DEBUG_ENABLED
  8092. completion_type = COMPLETION_NONE;
  8093. current_uniform_hint = ShaderNode::Uniform::HINT_NONE;
  8094. current_uniform_filter = FILTER_DEFAULT;
  8095. current_uniform_repeat = REPEAT_DEFAULT;
  8096. current_uniform_instance_index_defined = false;
  8097. } else { // varying
  8098. ShaderNode::Varying varying;
  8099. varying.type = type;
  8100. varying.precision = precision;
  8101. varying.interpolation = interpolation;
  8102. varying.tkpos = prev_pos;
  8103. varying.array_size = array_size;
  8104. tk = _get_token();
  8105. if (tk.type != TK_SEMICOLON && tk.type != TK_BRACKET_OPEN) {
  8106. if (array_size == 0) {
  8107. _set_expected_error(";", "[");
  8108. } else {
  8109. _set_expected_error(";");
  8110. }
  8111. return ERR_PARSE_ERROR;
  8112. }
  8113. if (tk.type == TK_BRACKET_OPEN) {
  8114. Error error = _parse_array_size(nullptr, constants, true, nullptr, &varying.array_size, nullptr);
  8115. if (error != OK) {
  8116. return error;
  8117. }
  8118. tk = _get_token();
  8119. }
  8120. shader->varyings[name] = varying;
  8121. #ifdef DEBUG_ENABLED
  8122. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_VARYING_FLAG)) {
  8123. used_varyings.insert(name, Usage(tk_line));
  8124. }
  8125. #endif // DEBUG_ENABLED
  8126. }
  8127. } break;
  8128. case TK_UNIFORM_GROUP: {
  8129. tk = _get_token();
  8130. if (tk.type == TK_IDENTIFIER) {
  8131. current_uniform_group_name = tk.text;
  8132. current_uniform_subgroup_name = "";
  8133. tk = _get_token();
  8134. if (tk.type == TK_PERIOD) {
  8135. tk = _get_token();
  8136. if (tk.type == TK_IDENTIFIER) {
  8137. current_uniform_subgroup_name = tk.text;
  8138. tk = _get_token();
  8139. if (tk.type != TK_SEMICOLON) {
  8140. _set_expected_error(";");
  8141. return ERR_PARSE_ERROR;
  8142. }
  8143. } else {
  8144. _set_error(RTR("Expected an uniform subgroup identifier."));
  8145. return ERR_PARSE_ERROR;
  8146. }
  8147. } else if (tk.type != TK_SEMICOLON) {
  8148. _set_expected_error(";", ".");
  8149. return ERR_PARSE_ERROR;
  8150. }
  8151. } else {
  8152. if (tk.type != TK_SEMICOLON) {
  8153. if (current_uniform_group_name.is_empty()) {
  8154. _set_error(RTR("Expected an uniform group identifier."));
  8155. } else {
  8156. _set_error(RTR("Expected an uniform group identifier or `;`."));
  8157. }
  8158. return ERR_PARSE_ERROR;
  8159. } else if (current_uniform_group_name.is_empty()) {
  8160. _set_error(RTR("Group needs to be opened before."));
  8161. return ERR_PARSE_ERROR;
  8162. } else {
  8163. current_uniform_group_name = "";
  8164. current_uniform_subgroup_name = "";
  8165. }
  8166. }
  8167. } break;
  8168. case TK_SHADER_TYPE: {
  8169. _set_error(RTR("Shader type is already defined."));
  8170. return ERR_PARSE_ERROR;
  8171. } break;
  8172. default: {
  8173. //function or constant variable
  8174. bool is_constant = false;
  8175. bool is_struct = false;
  8176. StringName struct_name;
  8177. DataPrecision precision = PRECISION_DEFAULT;
  8178. DataType type;
  8179. StringName name;
  8180. int array_size = 0;
  8181. if (tk.type == TK_CONST) {
  8182. is_constant = true;
  8183. tk = _get_token();
  8184. }
  8185. if (is_token_precision(tk.type)) {
  8186. precision = get_token_precision(tk.type);
  8187. tk = _get_token();
  8188. }
  8189. if (shader->structs.has(tk.text)) {
  8190. is_struct = true;
  8191. struct_name = tk.text;
  8192. } else {
  8193. #ifdef DEBUG_ENABLED
  8194. if (_lookup_next(next)) {
  8195. if (next.type == TK_UNIFORM) {
  8196. keyword_completion_context = CF_UNIFORM_QUALIFIER;
  8197. }
  8198. }
  8199. #endif // DEBUG_ENABLED
  8200. if (!is_token_datatype(tk.type)) {
  8201. _set_error(RTR("Expected constant, function, uniform or varying."));
  8202. return ERR_PARSE_ERROR;
  8203. }
  8204. if (!is_token_variable_datatype(tk.type)) {
  8205. if (is_constant) {
  8206. _set_error(RTR("Invalid constant type (samplers are not allowed)."));
  8207. } else {
  8208. _set_error(RTR("Invalid function type (samplers are not allowed)."));
  8209. }
  8210. return ERR_PARSE_ERROR;
  8211. }
  8212. }
  8213. if (is_struct) {
  8214. type = TYPE_STRUCT;
  8215. } else {
  8216. type = get_token_datatype(tk.type);
  8217. }
  8218. if (precision != PRECISION_DEFAULT && _validate_precision(type, precision) != OK) {
  8219. return ERR_PARSE_ERROR;
  8220. }
  8221. prev_pos = _get_tkpos();
  8222. tk = _get_token();
  8223. #ifdef DEBUG_ENABLED
  8224. keyword_completion_context = CF_UNSPECIFIED;
  8225. #endif // DEBUG_ENABLED
  8226. bool unknown_size = false;
  8227. bool fixed_array_size = false;
  8228. if (tk.type == TK_BRACKET_OPEN) {
  8229. Error error = _parse_array_size(nullptr, constants, !is_constant, nullptr, &array_size, &unknown_size);
  8230. if (error != OK) {
  8231. return error;
  8232. }
  8233. fixed_array_size = true;
  8234. prev_pos = _get_tkpos();
  8235. }
  8236. _set_tkpos(prev_pos);
  8237. _get_completable_identifier(nullptr, COMPLETION_MAIN_FUNCTION, name);
  8238. if (name == StringName()) {
  8239. if (is_constant) {
  8240. _set_error(RTR("Expected an identifier or '[' after type."));
  8241. } else {
  8242. _set_error(RTR("Expected a function name after type."));
  8243. }
  8244. return ERR_PARSE_ERROR;
  8245. }
  8246. if (shader->structs.has(name) || _find_identifier(nullptr, false, constants, name) || has_builtin(p_functions, name, !is_constant)) {
  8247. _set_redefinition_error(String(name));
  8248. return ERR_PARSE_ERROR;
  8249. }
  8250. tk = _get_token();
  8251. if (tk.type != TK_PARENTHESIS_OPEN) {
  8252. if (type == TYPE_VOID) {
  8253. _set_error(RTR("Expected '(' after function identifier."));
  8254. return ERR_PARSE_ERROR;
  8255. }
  8256. //variable
  8257. while (true) {
  8258. ShaderNode::Constant constant;
  8259. constant.name = name;
  8260. constant.type = is_struct ? TYPE_STRUCT : type;
  8261. constant.struct_name = struct_name;
  8262. constant.precision = precision;
  8263. constant.initializer = nullptr;
  8264. constant.array_size = array_size;
  8265. is_const_decl = true;
  8266. if (tk.type == TK_BRACKET_OPEN) {
  8267. Error error = _parse_array_size(nullptr, constants, false, nullptr, &constant.array_size, &unknown_size);
  8268. if (error != OK) {
  8269. return error;
  8270. }
  8271. tk = _get_token();
  8272. }
  8273. if (tk.type == TK_OP_ASSIGN) {
  8274. if (!is_constant) {
  8275. _set_error(vformat(RTR("Global non-constant variables are not supported. Expected '%s' keyword before constant definition."), "const"));
  8276. return ERR_PARSE_ERROR;
  8277. }
  8278. if (constant.array_size > 0 || unknown_size) {
  8279. bool full_def = false;
  8280. VariableDeclarationNode::Declaration decl;
  8281. decl.name = name;
  8282. decl.size = constant.array_size;
  8283. tk = _get_token();
  8284. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  8285. if (unknown_size) {
  8286. _set_expected_error("{");
  8287. return ERR_PARSE_ERROR;
  8288. }
  8289. full_def = true;
  8290. DataPrecision precision2 = PRECISION_DEFAULT;
  8291. if (is_token_precision(tk.type)) {
  8292. precision2 = get_token_precision(tk.type);
  8293. tk = _get_token();
  8294. if (!is_token_nonvoid_datatype(tk.type)) {
  8295. _set_error(RTR("Expected data type after precision modifier."));
  8296. return ERR_PARSE_ERROR;
  8297. }
  8298. }
  8299. StringName struct_name2;
  8300. DataType type2;
  8301. if (shader->structs.has(tk.text)) {
  8302. type2 = TYPE_STRUCT;
  8303. struct_name2 = tk.text;
  8304. } else {
  8305. if (!is_token_variable_datatype(tk.type)) {
  8306. _set_error(RTR("Invalid data type for the array."));
  8307. return ERR_PARSE_ERROR;
  8308. }
  8309. type2 = get_token_datatype(tk.type);
  8310. }
  8311. int array_size2 = 0;
  8312. tk = _get_token();
  8313. if (tk.type == TK_BRACKET_OPEN) {
  8314. bool is_unknown_size = false;
  8315. Error error = _parse_array_size(nullptr, constants, false, nullptr, &array_size2, &is_unknown_size);
  8316. if (error != OK) {
  8317. return error;
  8318. }
  8319. if (is_unknown_size) {
  8320. array_size2 = constant.array_size;
  8321. }
  8322. tk = _get_token();
  8323. } else {
  8324. _set_expected_error("[");
  8325. return ERR_PARSE_ERROR;
  8326. }
  8327. if (constant.precision != precision2 || constant.type != type2 || struct_name != struct_name2 || constant.array_size != array_size2) {
  8328. String from;
  8329. if (type2 == TYPE_STRUCT) {
  8330. from += struct_name2;
  8331. } else {
  8332. if (precision2 != PRECISION_DEFAULT) {
  8333. from += get_precision_name(precision2);
  8334. from += " ";
  8335. }
  8336. from += get_datatype_name(type2);
  8337. }
  8338. from += "[";
  8339. from += itos(array_size2);
  8340. from += "]'";
  8341. String to;
  8342. if (type == TYPE_STRUCT) {
  8343. to += struct_name;
  8344. } else {
  8345. if (precision != PRECISION_DEFAULT) {
  8346. to += get_precision_name(precision);
  8347. to += " ";
  8348. }
  8349. to += get_datatype_name(type);
  8350. }
  8351. to += "[";
  8352. to += itos(constant.array_size);
  8353. to += "]'";
  8354. _set_error(vformat(RTR("Cannot convert from '%s' to '%s'."), from, to));
  8355. return ERR_PARSE_ERROR;
  8356. }
  8357. }
  8358. bool curly = tk.type == TK_CURLY_BRACKET_OPEN;
  8359. if (unknown_size) {
  8360. if (!curly) {
  8361. _set_expected_error("{");
  8362. return ERR_PARSE_ERROR;
  8363. }
  8364. } else {
  8365. if (full_def) {
  8366. if (curly) {
  8367. _set_expected_error("(");
  8368. return ERR_PARSE_ERROR;
  8369. }
  8370. }
  8371. }
  8372. if (tk.type == TK_PARENTHESIS_OPEN || curly) { // initialization
  8373. while (true) {
  8374. Node *n = _parse_and_reduce_expression(nullptr, constants);
  8375. if (!n) {
  8376. return ERR_PARSE_ERROR;
  8377. }
  8378. if (n->type == Node::NODE_TYPE_OPERATOR && static_cast<OperatorNode *>(n)->op == OP_CALL) {
  8379. _set_error(RTR("Expected constant expression."));
  8380. return ERR_PARSE_ERROR;
  8381. }
  8382. if (!_compare_datatypes(constant.type, struct_name, 0, n->get_datatype(), n->get_datatype_name(), 0)) {
  8383. return ERR_PARSE_ERROR;
  8384. }
  8385. tk = _get_token();
  8386. if (tk.type == TK_COMMA) {
  8387. decl.initializer.push_back(n);
  8388. continue;
  8389. } else if (!curly && tk.type == TK_PARENTHESIS_CLOSE) {
  8390. decl.initializer.push_back(n);
  8391. break;
  8392. } else if (curly && tk.type == TK_CURLY_BRACKET_CLOSE) {
  8393. decl.initializer.push_back(n);
  8394. break;
  8395. } else {
  8396. if (curly) {
  8397. _set_expected_error("}", ",");
  8398. } else {
  8399. _set_expected_error(")", ",");
  8400. }
  8401. return ERR_PARSE_ERROR;
  8402. }
  8403. }
  8404. if (unknown_size) {
  8405. decl.size = decl.initializer.size();
  8406. constant.array_size = decl.initializer.size();
  8407. } else if (decl.initializer.size() != constant.array_size) {
  8408. _set_error(RTR("Array size mismatch."));
  8409. return ERR_PARSE_ERROR;
  8410. }
  8411. } else {
  8412. _set_expected_error("(");
  8413. return ERR_PARSE_ERROR;
  8414. }
  8415. array_size = constant.array_size;
  8416. ConstantNode *expr = memnew(ConstantNode);
  8417. expr->datatype = constant.type;
  8418. expr->struct_name = constant.struct_name;
  8419. expr->array_size = constant.array_size;
  8420. expr->array_declarations.push_back(decl);
  8421. constant.initializer = static_cast<ConstantNode *>(expr);
  8422. } else {
  8423. #ifdef DEBUG_ENABLED
  8424. if (constant.type == DataType::TYPE_BOOL) {
  8425. keyword_completion_context = CF_BOOLEAN;
  8426. }
  8427. #endif // DEBUG_ENABLED
  8428. //variable created with assignment! must parse an expression
  8429. Node *expr = _parse_and_reduce_expression(nullptr, constants);
  8430. if (!expr) {
  8431. return ERR_PARSE_ERROR;
  8432. }
  8433. #ifdef DEBUG_ENABLED
  8434. if (constant.type == DataType::TYPE_BOOL) {
  8435. keyword_completion_context = CF_GLOBAL_SPACE;
  8436. }
  8437. #endif // DEBUG_ENABLED
  8438. if (expr->type == Node::NODE_TYPE_OPERATOR && static_cast<OperatorNode *>(expr)->op == OP_CALL) {
  8439. OperatorNode *op = static_cast<OperatorNode *>(expr);
  8440. for (int i = 1; i < op->arguments.size(); i++) {
  8441. if (!_check_node_constness(op->arguments[i])) {
  8442. _set_error(vformat(RTR("Expected constant expression for argument %d of function call after '='."), i - 1));
  8443. return ERR_PARSE_ERROR;
  8444. }
  8445. }
  8446. }
  8447. constant.initializer = expr;
  8448. if (!_compare_datatypes(type, struct_name, 0, expr->get_datatype(), expr->get_datatype_name(), expr->get_array_size())) {
  8449. return ERR_PARSE_ERROR;
  8450. }
  8451. }
  8452. tk = _get_token();
  8453. } else {
  8454. if (constant.array_size > 0 || unknown_size) {
  8455. _set_error(RTR("Expected array initialization."));
  8456. return ERR_PARSE_ERROR;
  8457. } else {
  8458. _set_error(RTR("Expected initialization of constant."));
  8459. return ERR_PARSE_ERROR;
  8460. }
  8461. }
  8462. shader->constants[name] = constant;
  8463. shader->vconstants.push_back(constant);
  8464. #ifdef DEBUG_ENABLED
  8465. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_CONSTANT_FLAG)) {
  8466. used_constants.insert(name, Usage(tk_line));
  8467. }
  8468. #endif // DEBUG_ENABLED
  8469. if (tk.type == TK_COMMA) {
  8470. tk = _get_token();
  8471. if (tk.type != TK_IDENTIFIER) {
  8472. _set_error(RTR("Expected an identifier after type."));
  8473. return ERR_PARSE_ERROR;
  8474. }
  8475. name = tk.text;
  8476. if (_find_identifier(nullptr, false, constants, name)) {
  8477. _set_redefinition_error(String(name));
  8478. return ERR_PARSE_ERROR;
  8479. }
  8480. if (has_builtin(p_functions, name)) {
  8481. _set_redefinition_error(String(name));
  8482. return ERR_PARSE_ERROR;
  8483. }
  8484. tk = _get_token();
  8485. if (!fixed_array_size) {
  8486. array_size = 0;
  8487. }
  8488. unknown_size = false;
  8489. } else if (tk.type == TK_SEMICOLON) {
  8490. is_const_decl = false;
  8491. break;
  8492. } else {
  8493. _set_expected_error(",", ";");
  8494. return ERR_PARSE_ERROR;
  8495. }
  8496. }
  8497. break;
  8498. }
  8499. FunctionInfo builtins;
  8500. if (p_functions.has(name)) {
  8501. builtins = p_functions[name];
  8502. }
  8503. if (p_functions.has("global")) { // Adds global variables: 'TIME'
  8504. for (const KeyValue<StringName, BuiltInInfo> &E : p_functions["global"].built_ins) {
  8505. builtins.built_ins.insert(E.key, E.value);
  8506. }
  8507. }
  8508. if (p_functions.has("constants")) { // Adds global constants: 'PI', 'TAU', 'E'
  8509. for (const KeyValue<StringName, BuiltInInfo> &E : p_functions["constants"].built_ins) {
  8510. builtins.built_ins.insert(E.key, E.value);
  8511. }
  8512. }
  8513. for (int i = 0; i < shader->vfunctions.size(); i++) {
  8514. if (!shader->vfunctions[i].callable && shader->vfunctions[i].name == name) {
  8515. _set_redefinition_error(String(name));
  8516. return ERR_PARSE_ERROR;
  8517. }
  8518. }
  8519. ShaderNode::Function function;
  8520. function.callable = !p_functions.has(name);
  8521. function.name = name;
  8522. FunctionNode *func_node = alloc_node<FunctionNode>();
  8523. function.function = func_node;
  8524. shader->functions.insert(name, function);
  8525. shader->vfunctions.push_back(function);
  8526. CallInfo call_info;
  8527. call_info.name = name;
  8528. calls_info.insert(name, call_info);
  8529. func_node->name = name;
  8530. func_node->return_type = type;
  8531. func_node->return_struct_name = struct_name;
  8532. func_node->return_precision = precision;
  8533. func_node->return_array_size = array_size;
  8534. if (p_functions.has(name)) {
  8535. func_node->can_discard = p_functions[name].can_discard;
  8536. } else {
  8537. #ifdef DEBUG_ENABLED
  8538. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_FUNCTION_FLAG)) {
  8539. used_functions.insert(name, Usage(tk_line));
  8540. }
  8541. #endif // DEBUG_ENABLED
  8542. }
  8543. func_node->body = alloc_node<BlockNode>();
  8544. func_node->body->parent_function = func_node;
  8545. tk = _get_token();
  8546. while (true) {
  8547. if (tk.type == TK_PARENTHESIS_CLOSE) {
  8548. break;
  8549. }
  8550. #ifdef DEBUG_ENABLED
  8551. keyword_completion_context = CF_CONST_KEYWORD | CF_FUNC_DECL_PARAM_SPEC | CF_PRECISION_MODIFIER | CF_FUNC_DECL_PARAM_TYPE; // eg. const in mediump float
  8552. if (_lookup_next(next)) {
  8553. if (next.type == TK_CONST) {
  8554. keyword_completion_context = CF_UNSPECIFIED;
  8555. } else if (is_token_arg_qual(next.type)) {
  8556. keyword_completion_context = CF_CONST_KEYWORD;
  8557. } else if (is_token_precision(next.type)) {
  8558. keyword_completion_context = (CF_CONST_KEYWORD | CF_FUNC_DECL_PARAM_SPEC | CF_FUNC_DECL_PARAM_TYPE);
  8559. } else if (is_token_datatype(next.type)) {
  8560. keyword_completion_context = (CF_CONST_KEYWORD | CF_FUNC_DECL_PARAM_SPEC | CF_PRECISION_MODIFIER);
  8561. }
  8562. }
  8563. #endif // DEBUG_ENABLED
  8564. bool param_is_const = false;
  8565. if (tk.type == TK_CONST) {
  8566. param_is_const = true;
  8567. tk = _get_token();
  8568. #ifdef DEBUG_ENABLED
  8569. if (keyword_completion_context & CF_CONST_KEYWORD) {
  8570. keyword_completion_context ^= CF_CONST_KEYWORD;
  8571. }
  8572. if (_lookup_next(next)) {
  8573. if (is_token_arg_qual(next.type)) {
  8574. keyword_completion_context = CF_UNSPECIFIED;
  8575. } else if (is_token_precision(next.type)) {
  8576. keyword_completion_context = (CF_FUNC_DECL_PARAM_SPEC | CF_FUNC_DECL_PARAM_TYPE);
  8577. } else if (is_token_datatype(next.type)) {
  8578. keyword_completion_context = (CF_FUNC_DECL_PARAM_SPEC | CF_PRECISION_MODIFIER);
  8579. }
  8580. }
  8581. #endif // DEBUG_ENABLED
  8582. }
  8583. ArgumentQualifier param_qualifier = ARGUMENT_QUALIFIER_IN;
  8584. if (is_token_arg_qual(tk.type)) {
  8585. bool error = false;
  8586. switch (tk.type) {
  8587. case TK_ARG_IN: {
  8588. param_qualifier = ARGUMENT_QUALIFIER_IN;
  8589. } break;
  8590. case TK_ARG_OUT: {
  8591. if (param_is_const) {
  8592. _set_error(vformat(RTR("The '%s' qualifier cannot be used within a function parameter declared with '%s'."), "out", "const"));
  8593. error = true;
  8594. }
  8595. param_qualifier = ARGUMENT_QUALIFIER_OUT;
  8596. } break;
  8597. case TK_ARG_INOUT: {
  8598. if (param_is_const) {
  8599. _set_error(vformat(RTR("The '%s' qualifier cannot be used within a function parameter declared with '%s'."), "inout", "const"));
  8600. error = true;
  8601. }
  8602. param_qualifier = ARGUMENT_QUALIFIER_INOUT;
  8603. } break;
  8604. default:
  8605. error = true;
  8606. break;
  8607. }
  8608. tk = _get_token();
  8609. #ifdef DEBUG_ENABLED
  8610. if (keyword_completion_context & CF_CONST_KEYWORD) {
  8611. keyword_completion_context ^= CF_CONST_KEYWORD;
  8612. }
  8613. if (keyword_completion_context & CF_FUNC_DECL_PARAM_SPEC) {
  8614. keyword_completion_context ^= CF_FUNC_DECL_PARAM_SPEC;
  8615. }
  8616. if (_lookup_next(next)) {
  8617. if (is_token_precision(next.type)) {
  8618. keyword_completion_context = CF_FUNC_DECL_PARAM_TYPE;
  8619. } else if (is_token_datatype(next.type)) {
  8620. keyword_completion_context = CF_PRECISION_MODIFIER;
  8621. }
  8622. }
  8623. #endif // DEBUG_ENABLED
  8624. if (error) {
  8625. return ERR_PARSE_ERROR;
  8626. }
  8627. }
  8628. DataType param_type;
  8629. StringName param_name;
  8630. StringName param_struct_name;
  8631. DataPrecision param_precision = PRECISION_DEFAULT;
  8632. int arg_array_size = 0;
  8633. if (is_token_precision(tk.type)) {
  8634. param_precision = get_token_precision(tk.type);
  8635. tk = _get_token();
  8636. #ifdef DEBUG_ENABLED
  8637. if (keyword_completion_context & CF_CONST_KEYWORD) {
  8638. keyword_completion_context ^= CF_CONST_KEYWORD;
  8639. }
  8640. if (keyword_completion_context & CF_FUNC_DECL_PARAM_SPEC) {
  8641. keyword_completion_context ^= CF_FUNC_DECL_PARAM_SPEC;
  8642. }
  8643. if (keyword_completion_context & CF_PRECISION_MODIFIER) {
  8644. keyword_completion_context ^= CF_PRECISION_MODIFIER;
  8645. }
  8646. if (_lookup_next(next)) {
  8647. if (is_token_datatype(next.type)) {
  8648. keyword_completion_context = CF_UNSPECIFIED;
  8649. }
  8650. }
  8651. #endif // DEBUG_ENABLED
  8652. }
  8653. is_struct = false;
  8654. if (shader->structs.has(tk.text)) {
  8655. is_struct = true;
  8656. param_struct_name = tk.text;
  8657. #ifdef DEBUG_ENABLED
  8658. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_STRUCT_FLAG) && used_structs.has(param_struct_name)) {
  8659. used_structs[param_struct_name].used = true;
  8660. }
  8661. #endif // DEBUG_ENABLED
  8662. }
  8663. if (!is_struct && !is_token_datatype(tk.type)) {
  8664. _set_error(RTR("Expected a valid data type for argument."));
  8665. return ERR_PARSE_ERROR;
  8666. }
  8667. if (param_qualifier == ARGUMENT_QUALIFIER_OUT || param_qualifier == ARGUMENT_QUALIFIER_INOUT) {
  8668. if (is_sampler_type(get_token_datatype(tk.type))) {
  8669. _set_error(RTR("Opaque types cannot be output parameters."));
  8670. return ERR_PARSE_ERROR;
  8671. }
  8672. }
  8673. if (is_struct) {
  8674. param_type = TYPE_STRUCT;
  8675. } else {
  8676. param_type = get_token_datatype(tk.type);
  8677. if (param_type == TYPE_VOID) {
  8678. _set_error(RTR("Void type not allowed as argument."));
  8679. return ERR_PARSE_ERROR;
  8680. }
  8681. }
  8682. if (param_precision != PRECISION_DEFAULT && _validate_precision(param_type, param_precision) != OK) {
  8683. return ERR_PARSE_ERROR;
  8684. }
  8685. #ifdef DEBUG_ENABLED
  8686. keyword_completion_context = CF_UNSPECIFIED;
  8687. #endif // DEBUG_ENABLED
  8688. tk = _get_token();
  8689. if (tk.type == TK_BRACKET_OPEN) {
  8690. Error error = _parse_array_size(nullptr, constants, true, nullptr, &arg_array_size, nullptr);
  8691. if (error != OK) {
  8692. return error;
  8693. }
  8694. tk = _get_token();
  8695. }
  8696. if (tk.type != TK_IDENTIFIER) {
  8697. _set_error(RTR("Expected an identifier for argument name."));
  8698. return ERR_PARSE_ERROR;
  8699. }
  8700. param_name = tk.text;
  8701. ShaderLanguage::IdentifierType itype;
  8702. if (_find_identifier(func_node->body, false, builtins, param_name, (ShaderLanguage::DataType *)nullptr, &itype)) {
  8703. if (itype != IDENTIFIER_FUNCTION) {
  8704. _set_redefinition_error(String(param_name));
  8705. return ERR_PARSE_ERROR;
  8706. }
  8707. }
  8708. if (has_builtin(p_functions, param_name)) {
  8709. _set_redefinition_error(String(param_name));
  8710. return ERR_PARSE_ERROR;
  8711. }
  8712. FunctionNode::Argument arg;
  8713. arg.type = param_type;
  8714. arg.name = param_name;
  8715. arg.struct_name = param_struct_name;
  8716. arg.precision = param_precision;
  8717. arg.qualifier = param_qualifier;
  8718. arg.tex_argument_check = false;
  8719. arg.tex_builtin_check = false;
  8720. arg.tex_argument_filter = FILTER_DEFAULT;
  8721. arg.tex_argument_repeat = REPEAT_DEFAULT;
  8722. arg.is_const = param_is_const;
  8723. tk = _get_token();
  8724. if (tk.type == TK_BRACKET_OPEN) {
  8725. Error error = _parse_array_size(nullptr, constants, true, nullptr, &arg_array_size, nullptr);
  8726. if (error != OK) {
  8727. return error;
  8728. }
  8729. tk = _get_token();
  8730. }
  8731. arg.array_size = arg_array_size;
  8732. func_node->arguments.push_back(arg);
  8733. if (tk.type == TK_COMMA) {
  8734. tk = _get_token();
  8735. //do none and go on
  8736. } else if (tk.type != TK_PARENTHESIS_CLOSE) {
  8737. _set_expected_error(",", ")");
  8738. return ERR_PARSE_ERROR;
  8739. }
  8740. }
  8741. if (p_functions.has(name)) {
  8742. //if one of the core functions, make sure they are of the correct form
  8743. if (func_node->arguments.size() > 0) {
  8744. _set_error(vformat(RTR("Function '%s' expects no arguments."), String(name)));
  8745. return ERR_PARSE_ERROR;
  8746. }
  8747. if (func_node->return_type != TYPE_VOID) {
  8748. _set_error(vformat(RTR("Function '%s' must be of '%s' return type."), String(name), "void"));
  8749. return ERR_PARSE_ERROR;
  8750. }
  8751. }
  8752. //all good let's parse inside the function!
  8753. tk = _get_token();
  8754. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  8755. _set_error(RTR("Expected a '{' to begin function."));
  8756. return ERR_PARSE_ERROR;
  8757. }
  8758. current_function = name;
  8759. #ifdef DEBUG_ENABLED
  8760. keyword_completion_context = CF_BLOCK;
  8761. #endif // DEBUG_ENABLED
  8762. Error err = _parse_block(func_node->body, builtins);
  8763. if (err) {
  8764. return err;
  8765. }
  8766. #ifdef DEBUG_ENABLED
  8767. keyword_completion_context = CF_GLOBAL_SPACE;
  8768. #endif // DEBUG_ENABLED
  8769. if (func_node->return_type != DataType::TYPE_VOID) {
  8770. BlockNode *block = func_node->body;
  8771. if (_find_last_flow_op_in_block(block, FlowOperation::FLOW_OP_RETURN) != OK) {
  8772. _set_error(vformat(RTR("Expected at least one '%s' statement in a non-void function."), "return"));
  8773. return ERR_PARSE_ERROR;
  8774. }
  8775. }
  8776. current_function = StringName();
  8777. }
  8778. }
  8779. tk = _get_token();
  8780. }
  8781. #ifdef DEBUG_ENABLED
  8782. if (check_device_limit_warnings && uniform_buffer_exceeded_line != -1) {
  8783. _add_warning(ShaderWarning::DEVICE_LIMIT_EXCEEDED, uniform_buffer_exceeded_line, RTR("uniform buffer"), { uniform_buffer_size, max_uniform_buffer_size });
  8784. }
  8785. #endif // DEBUG_ENABLED
  8786. return OK;
  8787. }
  8788. bool ShaderLanguage::has_builtin(const HashMap<StringName, ShaderLanguage::FunctionInfo> &p_functions, const StringName &p_name, bool p_check_global_funcs) {
  8789. if (p_check_global_funcs && global_func_set.has(p_name)) {
  8790. return true;
  8791. }
  8792. for (const KeyValue<StringName, ShaderLanguage::FunctionInfo> &E : p_functions) {
  8793. if (E.value.built_ins.has(p_name)) {
  8794. return true;
  8795. }
  8796. }
  8797. return false;
  8798. }
  8799. Error ShaderLanguage::_find_last_flow_op_in_op(ControlFlowNode *p_flow, FlowOperation p_op) {
  8800. bool found = false;
  8801. for (int i = p_flow->blocks.size() - 1; i >= 0; i--) {
  8802. if (p_flow->blocks[i]->type == Node::NODE_TYPE_BLOCK) {
  8803. BlockNode *last_block = static_cast<BlockNode *>(p_flow->blocks[i]);
  8804. if (_find_last_flow_op_in_block(last_block, p_op) == OK) {
  8805. found = true;
  8806. break;
  8807. }
  8808. }
  8809. }
  8810. if (found) {
  8811. return OK;
  8812. }
  8813. return FAILED;
  8814. }
  8815. Error ShaderLanguage::_find_last_flow_op_in_block(BlockNode *p_block, FlowOperation p_op) {
  8816. bool found = false;
  8817. for (List<ShaderLanguage::Node *>::Element *E = p_block->statements.back(); E; E = E->prev()) {
  8818. if (E->get()->type == Node::NODE_TYPE_CONTROL_FLOW) {
  8819. ControlFlowNode *flow = static_cast<ControlFlowNode *>(E->get());
  8820. if (flow->flow_op == p_op) {
  8821. found = true;
  8822. break;
  8823. } else {
  8824. if (_find_last_flow_op_in_op(flow, p_op) == OK) {
  8825. found = true;
  8826. break;
  8827. }
  8828. }
  8829. } else if (E->get()->type == Node::NODE_TYPE_BLOCK) {
  8830. BlockNode *block = static_cast<BlockNode *>(E->get());
  8831. if (_find_last_flow_op_in_block(block, p_op) == OK) {
  8832. found = true;
  8833. break;
  8834. }
  8835. }
  8836. }
  8837. if (found) {
  8838. return OK;
  8839. }
  8840. return FAILED;
  8841. }
  8842. // skips over whitespace and /* */ and // comments
  8843. static int _get_first_ident_pos(const String &p_code) {
  8844. int idx = 0;
  8845. #define GETCHAR(m_idx) (((idx + m_idx) < p_code.length()) ? p_code[idx + m_idx] : char32_t(0))
  8846. while (true) {
  8847. if (GETCHAR(0) == '/' && GETCHAR(1) == '/') {
  8848. idx += 2;
  8849. while (true) {
  8850. if (GETCHAR(0) == 0) {
  8851. return 0;
  8852. }
  8853. if (GETCHAR(0) == '\n') {
  8854. idx++;
  8855. break; // loop
  8856. }
  8857. idx++;
  8858. }
  8859. } else if (GETCHAR(0) == '/' && GETCHAR(1) == '*') {
  8860. idx += 2;
  8861. while (true) {
  8862. if (GETCHAR(0) == 0) {
  8863. return 0;
  8864. }
  8865. if (GETCHAR(0) == '*' && GETCHAR(1) == '/') {
  8866. idx += 2;
  8867. break; // loop
  8868. }
  8869. idx++;
  8870. }
  8871. } else {
  8872. switch (GETCHAR(0)) {
  8873. case ' ':
  8874. case '\t':
  8875. case '\r':
  8876. case '\n': {
  8877. idx++;
  8878. } break; // switch
  8879. default:
  8880. return idx;
  8881. }
  8882. }
  8883. }
  8884. #undef GETCHAR
  8885. }
  8886. String ShaderLanguage::get_shader_type(const String &p_code) {
  8887. bool reading_type = false;
  8888. String cur_identifier;
  8889. for (int i = _get_first_ident_pos(p_code); i < p_code.length(); i++) {
  8890. if (p_code[i] == ';') {
  8891. break;
  8892. } else if (p_code[i] <= 32) {
  8893. if (!cur_identifier.is_empty()) {
  8894. if (!reading_type) {
  8895. if (cur_identifier != "shader_type") {
  8896. return String();
  8897. }
  8898. reading_type = true;
  8899. cur_identifier = String();
  8900. } else {
  8901. return cur_identifier;
  8902. }
  8903. }
  8904. } else {
  8905. cur_identifier += String::chr(p_code[i]);
  8906. }
  8907. }
  8908. if (reading_type) {
  8909. return cur_identifier;
  8910. }
  8911. return String();
  8912. }
  8913. bool ShaderLanguage::is_builtin_func_out_parameter(const String &p_name, int p_param) {
  8914. int i = 0;
  8915. while (builtin_func_out_args[i].name) {
  8916. if (p_name == builtin_func_out_args[i].name) {
  8917. for (int j = 0; j < BuiltinFuncOutArgs::MAX_ARGS; j++) {
  8918. int arg = builtin_func_out_args[i].arguments[j];
  8919. if (arg == p_param) {
  8920. return true;
  8921. }
  8922. if (arg < 0) {
  8923. return false;
  8924. }
  8925. }
  8926. }
  8927. i++;
  8928. }
  8929. return false;
  8930. }
  8931. #ifdef DEBUG_ENABLED
  8932. void ShaderLanguage::_check_warning_accums() {
  8933. for (const KeyValue<ShaderWarning::Code, HashMap<StringName, HashMap<StringName, Usage>> *> &E : warnings_check_map2) {
  8934. for (const KeyValue<StringName, HashMap<StringName, Usage>> &T : *E.value) {
  8935. for (const KeyValue<StringName, Usage> &U : T.value) {
  8936. if (!U.value.used) {
  8937. _add_warning(E.key, U.value.decl_line, U.key);
  8938. }
  8939. }
  8940. }
  8941. }
  8942. for (const KeyValue<ShaderWarning::Code, HashMap<StringName, Usage> *> &E : warnings_check_map) {
  8943. for (const KeyValue<StringName, Usage> &U : (*E.value)) {
  8944. if (!U.value.used) {
  8945. _add_warning(E.key, U.value.decl_line, U.key);
  8946. }
  8947. }
  8948. }
  8949. }
  8950. List<ShaderWarning>::Element *ShaderLanguage::get_warnings_ptr() {
  8951. return warnings.front();
  8952. }
  8953. void ShaderLanguage::enable_warning_checking(bool p_enabled) {
  8954. check_warnings = p_enabled;
  8955. }
  8956. bool ShaderLanguage::is_warning_checking_enabled() const {
  8957. return check_warnings;
  8958. }
  8959. void ShaderLanguage::set_warning_flags(uint32_t p_flags) {
  8960. warning_flags = p_flags;
  8961. }
  8962. uint32_t ShaderLanguage::get_warning_flags() const {
  8963. return warning_flags;
  8964. }
  8965. #endif // DEBUG_ENABLED
  8966. Error ShaderLanguage::compile(const String &p_code, const ShaderCompileInfo &p_info) {
  8967. clear();
  8968. is_shader_inc = p_info.is_include;
  8969. code = p_code;
  8970. global_shader_uniform_get_type_func = p_info.global_shader_uniform_type_func;
  8971. varying_function_names = p_info.varying_function_names;
  8972. nodes = nullptr;
  8973. shader = alloc_node<ShaderNode>();
  8974. Error err = _parse_shader(p_info.functions, p_info.render_modes, p_info.shader_types);
  8975. #ifdef DEBUG_ENABLED
  8976. if (check_warnings) {
  8977. _check_warning_accums();
  8978. }
  8979. #endif // DEBUG_ENABLED
  8980. if (err != OK) {
  8981. return err;
  8982. }
  8983. return OK;
  8984. }
  8985. Error ShaderLanguage::complete(const String &p_code, const ShaderCompileInfo &p_info, List<ScriptLanguage::CodeCompletionOption> *r_options, String &r_call_hint) {
  8986. clear();
  8987. is_shader_inc = p_info.is_include;
  8988. code = p_code;
  8989. varying_function_names = p_info.varying_function_names;
  8990. nodes = nullptr;
  8991. global_shader_uniform_get_type_func = p_info.global_shader_uniform_type_func;
  8992. shader = alloc_node<ShaderNode>();
  8993. _parse_shader(p_info.functions, p_info.render_modes, p_info.shader_types);
  8994. #ifdef DEBUG_ENABLED
  8995. // Adds context keywords.
  8996. if (keyword_completion_context != CF_UNSPECIFIED) {
  8997. int sz = sizeof(keyword_list) / sizeof(KeyWord);
  8998. for (int i = 0; i < sz; i++) {
  8999. if (keyword_list[i].flags == CF_UNSPECIFIED) {
  9000. break; // Ignore hint keywords (parsed below).
  9001. }
  9002. if (keyword_list[i].flags & keyword_completion_context) {
  9003. if (keyword_list[i].excluded_shader_types.has(shader_type_identifier)) {
  9004. continue;
  9005. }
  9006. if (!keyword_list[i].functions.is_empty() && !keyword_list[i].functions.has(current_function)) {
  9007. continue;
  9008. }
  9009. ScriptLanguage::CodeCompletionOption option(keyword_list[i].text, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9010. r_options->push_back(option);
  9011. }
  9012. }
  9013. }
  9014. #endif // DEBUG_ENABLED
  9015. switch (completion_type) {
  9016. case COMPLETION_NONE: {
  9017. //do nothing
  9018. return OK;
  9019. } break;
  9020. case COMPLETION_SHADER_TYPE: {
  9021. for (const String &shader_type : p_info.shader_types) {
  9022. ScriptLanguage::CodeCompletionOption option(shader_type, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9023. r_options->push_back(option);
  9024. }
  9025. return OK;
  9026. } break;
  9027. case COMPLETION_RENDER_MODE: {
  9028. if (is_shader_inc) {
  9029. for (int i = 0; i < RenderingServer::SHADER_MAX; i++) {
  9030. const Vector<ModeInfo> modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(i));
  9031. for (int j = 0; j < modes.size(); j++) {
  9032. const ModeInfo &info = modes[j];
  9033. if (!info.options.is_empty()) {
  9034. bool found = false;
  9035. for (int k = 0; k < info.options.size(); k++) {
  9036. if (shader->render_modes.has(String(info.name) + "_" + String(info.options[k]))) {
  9037. found = true;
  9038. }
  9039. }
  9040. if (!found) {
  9041. for (int k = 0; k < info.options.size(); k++) {
  9042. ScriptLanguage::CodeCompletionOption option(String(info.name) + "_" + String(info.options[k]), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9043. r_options->push_back(option);
  9044. }
  9045. }
  9046. } else {
  9047. const String name = String(info.name);
  9048. if (!shader->render_modes.has(name)) {
  9049. ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9050. r_options->push_back(option);
  9051. }
  9052. }
  9053. }
  9054. }
  9055. } else {
  9056. for (int i = 0; i < p_info.render_modes.size(); i++) {
  9057. const ModeInfo &info = p_info.render_modes[i];
  9058. if (!info.options.is_empty()) {
  9059. bool found = false;
  9060. for (int j = 0; j < info.options.size(); j++) {
  9061. if (shader->render_modes.has(String(info.name) + "_" + String(info.options[j]))) {
  9062. found = true;
  9063. }
  9064. }
  9065. if (!found) {
  9066. for (int j = 0; j < info.options.size(); j++) {
  9067. ScriptLanguage::CodeCompletionOption option(String(info.name) + "_" + String(info.options[j]), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9068. r_options->push_back(option);
  9069. }
  9070. }
  9071. } else {
  9072. const String name = String(info.name);
  9073. if (!shader->render_modes.has(name)) {
  9074. ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9075. r_options->push_back(option);
  9076. }
  9077. }
  9078. }
  9079. }
  9080. return OK;
  9081. } break;
  9082. case COMPLETION_STRUCT: {
  9083. if (shader->structs.has(completion_struct)) {
  9084. StructNode *node = shader->structs[completion_struct].shader_struct;
  9085. for (ShaderLanguage::MemberNode *member : node->members) {
  9086. ScriptLanguage::CodeCompletionOption option(member->name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER);
  9087. r_options->push_back(option);
  9088. }
  9089. }
  9090. return OK;
  9091. } break;
  9092. case COMPLETION_MAIN_FUNCTION: {
  9093. for (const KeyValue<StringName, FunctionInfo> &E : p_info.functions) {
  9094. if (!E.value.main_function) {
  9095. continue;
  9096. }
  9097. bool found = false;
  9098. for (int i = 0; i < shader->vfunctions.size(); i++) {
  9099. if (shader->vfunctions[i].name == E.key) {
  9100. found = true;
  9101. break;
  9102. }
  9103. }
  9104. if (found) {
  9105. continue;
  9106. }
  9107. ScriptLanguage::CodeCompletionOption option(E.key, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  9108. r_options->push_back(option);
  9109. }
  9110. return OK;
  9111. } break;
  9112. case COMPLETION_IDENTIFIER:
  9113. case COMPLETION_FUNCTION_CALL: {
  9114. bool comp_ident = completion_type == COMPLETION_IDENTIFIER;
  9115. HashMap<String, ScriptLanguage::CodeCompletionKind> matches;
  9116. StringName skip_function;
  9117. BlockNode *block = completion_block;
  9118. if (completion_class == TAG_GLOBAL) {
  9119. while (block) {
  9120. if (comp_ident) {
  9121. for (const KeyValue<StringName, BlockNode::Variable> &E : block->variables) {
  9122. if (E.value.line < completion_line) {
  9123. matches.insert(E.key, ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE);
  9124. }
  9125. }
  9126. }
  9127. if (block->parent_function) {
  9128. if (comp_ident) {
  9129. for (int i = 0; i < block->parent_function->arguments.size(); i++) {
  9130. matches.insert(block->parent_function->arguments[i].name, ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE);
  9131. }
  9132. }
  9133. skip_function = block->parent_function->name;
  9134. }
  9135. block = block->parent_block;
  9136. }
  9137. if (comp_ident) {
  9138. if (is_shader_inc) {
  9139. for (int i = 0; i < RenderingServer::SHADER_MAX; i++) {
  9140. const HashMap<StringName, ShaderLanguage::FunctionInfo> info = ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(i));
  9141. if (info.has("global")) {
  9142. for (const KeyValue<StringName, BuiltInInfo> &E : info["global"].built_ins) {
  9143. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  9144. if (E.value.constant) {
  9145. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  9146. }
  9147. matches.insert(E.key, kind);
  9148. }
  9149. }
  9150. if (info.has("constants")) {
  9151. for (const KeyValue<StringName, BuiltInInfo> &E : info["constants"].built_ins) {
  9152. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  9153. if (E.value.constant) {
  9154. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  9155. }
  9156. matches.insert(E.key, kind);
  9157. }
  9158. }
  9159. if (skip_function != StringName() && info.has(skip_function)) {
  9160. for (const KeyValue<StringName, BuiltInInfo> &E : info[skip_function].built_ins) {
  9161. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  9162. if (E.value.constant) {
  9163. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  9164. }
  9165. matches.insert(E.key, kind);
  9166. }
  9167. }
  9168. }
  9169. } else {
  9170. if (p_info.functions.has("global")) {
  9171. for (const KeyValue<StringName, BuiltInInfo> &E : p_info.functions["global"].built_ins) {
  9172. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  9173. if (E.value.constant) {
  9174. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  9175. }
  9176. matches.insert(E.key, kind);
  9177. }
  9178. }
  9179. if (p_info.functions.has("constants")) {
  9180. for (const KeyValue<StringName, BuiltInInfo> &E : p_info.functions["constants"].built_ins) {
  9181. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  9182. if (E.value.constant) {
  9183. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  9184. }
  9185. matches.insert(E.key, kind);
  9186. }
  9187. }
  9188. if (skip_function != StringName() && p_info.functions.has(skip_function)) {
  9189. for (const KeyValue<StringName, BuiltInInfo> &E : p_info.functions[skip_function].built_ins) {
  9190. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  9191. if (E.value.constant) {
  9192. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  9193. }
  9194. matches.insert(E.key, kind);
  9195. }
  9196. }
  9197. }
  9198. for (const KeyValue<StringName, ShaderNode::Constant> &E : shader->constants) {
  9199. matches.insert(E.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  9200. }
  9201. for (const KeyValue<StringName, ShaderNode::Varying> &E : shader->varyings) {
  9202. matches.insert(E.key, ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE);
  9203. }
  9204. for (const KeyValue<StringName, ShaderNode::Uniform> &E : shader->uniforms) {
  9205. matches.insert(E.key, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER);
  9206. }
  9207. }
  9208. for (int i = 0; i < shader->vfunctions.size(); i++) {
  9209. if (!shader->vfunctions[i].callable || shader->vfunctions[i].name == skip_function) {
  9210. continue;
  9211. }
  9212. matches.insert(String(shader->vfunctions[i].name), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  9213. }
  9214. int idx = 0;
  9215. bool low_end = RenderingServer::get_singleton()->is_low_end();
  9216. if (stages && stages->has(skip_function)) {
  9217. for (const KeyValue<StringName, StageFunctionInfo> &E : (*stages)[skip_function].stage_functions) {
  9218. matches.insert(String(E.key), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  9219. }
  9220. }
  9221. while (builtin_func_defs[idx].name) {
  9222. if ((low_end && builtin_func_defs[idx].high_end) || _check_restricted_func(builtin_func_defs[idx].name, skip_function)) {
  9223. idx++;
  9224. continue;
  9225. }
  9226. matches.insert(String(builtin_func_defs[idx].name), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  9227. idx++;
  9228. }
  9229. } else { // sub-class
  9230. int idx = 0;
  9231. bool low_end = RenderingServer::get_singleton()->is_low_end();
  9232. while (builtin_func_defs[idx].name) {
  9233. if (low_end && builtin_func_defs[idx].high_end) {
  9234. idx++;
  9235. continue;
  9236. }
  9237. if (builtin_func_defs[idx].tag == completion_class) {
  9238. matches.insert(String(builtin_func_defs[idx].name), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  9239. }
  9240. idx++;
  9241. }
  9242. }
  9243. for (const KeyValue<String, ScriptLanguage::CodeCompletionKind> &E : matches) {
  9244. ScriptLanguage::CodeCompletionOption option(E.key, E.value);
  9245. if (E.value == ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION) {
  9246. option.insert_text += "(";
  9247. }
  9248. r_options->push_back(option);
  9249. }
  9250. return OK;
  9251. } break;
  9252. case COMPLETION_CALL_ARGUMENTS: {
  9253. StringName block_function;
  9254. BlockNode *block = completion_block;
  9255. while (block) {
  9256. if (block->parent_function) {
  9257. block_function = block->parent_function->name;
  9258. }
  9259. block = block->parent_block;
  9260. }
  9261. for (int i = 0; i < shader->vfunctions.size(); i++) {
  9262. if (!shader->vfunctions[i].callable) {
  9263. continue;
  9264. }
  9265. if (shader->vfunctions[i].name == completion_function) {
  9266. String calltip;
  9267. if (shader->vfunctions[i].function->return_type == TYPE_STRUCT) {
  9268. calltip += String(shader->vfunctions[i].function->return_struct_name);
  9269. } else {
  9270. calltip += get_datatype_name(shader->vfunctions[i].function->return_type);
  9271. }
  9272. if (shader->vfunctions[i].function->return_array_size > 0) {
  9273. calltip += "[";
  9274. calltip += itos(shader->vfunctions[i].function->return_array_size);
  9275. calltip += "]";
  9276. }
  9277. calltip += " ";
  9278. calltip += shader->vfunctions[i].name;
  9279. calltip += "(";
  9280. for (int j = 0; j < shader->vfunctions[i].function->arguments.size(); j++) {
  9281. if (j > 0) {
  9282. calltip += ", ";
  9283. } else {
  9284. calltip += " ";
  9285. }
  9286. if (j == completion_argument) {
  9287. calltip += char32_t(0xFFFF);
  9288. }
  9289. if (shader->vfunctions[i].function->arguments[j].is_const) {
  9290. calltip += "const ";
  9291. }
  9292. if (shader->vfunctions[i].function->arguments[j].qualifier != ArgumentQualifier::ARGUMENT_QUALIFIER_IN) {
  9293. if (shader->vfunctions[i].function->arguments[j].qualifier == ArgumentQualifier::ARGUMENT_QUALIFIER_OUT) {
  9294. calltip += "out ";
  9295. } else { // ArgumentQualifier::ARGUMENT_QUALIFIER_INOUT
  9296. calltip += "inout ";
  9297. }
  9298. }
  9299. if (shader->vfunctions[i].function->arguments[j].type == TYPE_STRUCT) {
  9300. calltip += String(shader->vfunctions[i].function->arguments[j].struct_name);
  9301. } else {
  9302. calltip += get_datatype_name(shader->vfunctions[i].function->arguments[j].type);
  9303. }
  9304. calltip += " ";
  9305. calltip += shader->vfunctions[i].function->arguments[j].name;
  9306. if (shader->vfunctions[i].function->arguments[j].array_size > 0) {
  9307. calltip += "[";
  9308. calltip += itos(shader->vfunctions[i].function->arguments[j].array_size);
  9309. calltip += "]";
  9310. }
  9311. if (j == completion_argument) {
  9312. calltip += char32_t(0xFFFF);
  9313. }
  9314. }
  9315. if (shader->vfunctions[i].function->arguments.size()) {
  9316. calltip += " ";
  9317. }
  9318. calltip += ")";
  9319. r_call_hint = calltip;
  9320. return OK;
  9321. }
  9322. }
  9323. int idx = 0;
  9324. String calltip;
  9325. bool low_end = RenderingServer::get_singleton()->is_low_end();
  9326. if (stages && stages->has(block_function)) {
  9327. for (const KeyValue<StringName, StageFunctionInfo> &E : (*stages)[block_function].stage_functions) {
  9328. if (completion_function == E.key) {
  9329. calltip += get_datatype_name(E.value.return_type);
  9330. calltip += " ";
  9331. calltip += E.key;
  9332. calltip += "(";
  9333. for (int i = 0; i < E.value.arguments.size(); i++) {
  9334. if (i > 0) {
  9335. calltip += ", ";
  9336. } else {
  9337. calltip += " ";
  9338. }
  9339. if (i == completion_argument) {
  9340. calltip += char32_t(0xFFFF);
  9341. }
  9342. calltip += get_datatype_name(E.value.arguments[i].type);
  9343. calltip += " ";
  9344. calltip += E.value.arguments[i].name;
  9345. if (i == completion_argument) {
  9346. calltip += char32_t(0xFFFF);
  9347. }
  9348. }
  9349. if (E.value.arguments.size()) {
  9350. calltip += " ";
  9351. }
  9352. calltip += ")";
  9353. r_call_hint = calltip;
  9354. return OK;
  9355. }
  9356. }
  9357. }
  9358. while (builtin_func_defs[idx].name) {
  9359. if ((low_end && builtin_func_defs[idx].high_end) || _check_restricted_func(builtin_func_defs[idx].name, block_function)) {
  9360. idx++;
  9361. continue;
  9362. }
  9363. int idx2 = 0;
  9364. HashSet<int> out_args;
  9365. while (builtin_func_out_args[idx2].name != nullptr) {
  9366. if (builtin_func_out_args[idx2].name == builtin_func_defs[idx].name) {
  9367. for (int i = 0; i < BuiltinFuncOutArgs::MAX_ARGS; i++) {
  9368. int arg = builtin_func_out_args[idx2].arguments[i];
  9369. if (arg == -1) {
  9370. break;
  9371. }
  9372. out_args.insert(arg);
  9373. }
  9374. break;
  9375. }
  9376. idx2++;
  9377. }
  9378. if (completion_function == builtin_func_defs[idx].name) {
  9379. if (builtin_func_defs[idx].tag != completion_class) {
  9380. idx++;
  9381. continue;
  9382. }
  9383. if (calltip.length()) {
  9384. calltip += "\n";
  9385. }
  9386. calltip += get_datatype_name(builtin_func_defs[idx].rettype);
  9387. calltip += " ";
  9388. calltip += builtin_func_defs[idx].name;
  9389. calltip += "(";
  9390. bool found_arg = false;
  9391. for (int i = 0; i < BuiltinFuncDef::MAX_ARGS - 1; i++) {
  9392. if (builtin_func_defs[idx].args[i] == TYPE_VOID) {
  9393. break;
  9394. }
  9395. if (i > 0) {
  9396. calltip += ", ";
  9397. } else {
  9398. calltip += " ";
  9399. }
  9400. if (i == completion_argument) {
  9401. calltip += char32_t(0xFFFF);
  9402. }
  9403. if (out_args.has(i)) {
  9404. calltip += "out ";
  9405. }
  9406. calltip += get_datatype_name(builtin_func_defs[idx].args[i]);
  9407. String arg_name = (String)builtin_func_defs[idx].args_names[i];
  9408. if (!arg_name.is_empty()) {
  9409. calltip += " ";
  9410. calltip += arg_name;
  9411. }
  9412. if (i == completion_argument) {
  9413. calltip += char32_t(0xFFFF);
  9414. }
  9415. found_arg = true;
  9416. }
  9417. if (found_arg) {
  9418. calltip += " ";
  9419. }
  9420. calltip += ")";
  9421. }
  9422. idx++;
  9423. }
  9424. r_call_hint = calltip;
  9425. return OK;
  9426. } break;
  9427. case COMPLETION_INDEX: {
  9428. const char colv[4] = { 'r', 'g', 'b', 'a' };
  9429. const char coordv[4] = { 'x', 'y', 'z', 'w' };
  9430. const char coordt[4] = { 's', 't', 'p', 'q' };
  9431. const String theme_color_names[4] = { "axis_x_color", "axis_y_color", "axis_z_color", "axis_w_color" };
  9432. int limit = 0;
  9433. switch (completion_base) {
  9434. case TYPE_BVEC2:
  9435. case TYPE_IVEC2:
  9436. case TYPE_UVEC2:
  9437. case TYPE_VEC2: {
  9438. limit = 2;
  9439. } break;
  9440. case TYPE_BVEC3:
  9441. case TYPE_IVEC3:
  9442. case TYPE_UVEC3:
  9443. case TYPE_VEC3: {
  9444. limit = 3;
  9445. } break;
  9446. case TYPE_BVEC4:
  9447. case TYPE_IVEC4:
  9448. case TYPE_UVEC4:
  9449. case TYPE_VEC4: {
  9450. limit = 4;
  9451. } break;
  9452. default: {
  9453. }
  9454. }
  9455. for (int i = 0; i < limit; i++) {
  9456. r_options->push_back(ScriptLanguage::CodeCompletionOption(String::chr(colv[i]), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT, ScriptLanguage::LOCATION_OTHER, theme_color_names[i]));
  9457. r_options->push_back(ScriptLanguage::CodeCompletionOption(String::chr(coordv[i]), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT, ScriptLanguage::LOCATION_OTHER, theme_color_names[i]));
  9458. r_options->push_back(ScriptLanguage::CodeCompletionOption(String::chr(coordt[i]), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT, ScriptLanguage::LOCATION_OTHER, theme_color_names[i]));
  9459. }
  9460. } break;
  9461. case COMPLETION_HINT: {
  9462. if (completion_base == DataType::TYPE_VEC3 || completion_base == DataType::TYPE_VEC4) {
  9463. if (current_uniform_hint == ShaderNode::Uniform::HINT_NONE) {
  9464. ScriptLanguage::CodeCompletionOption option("source_color", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9465. r_options->push_back(option);
  9466. }
  9467. } else if ((completion_base == DataType::TYPE_INT || completion_base == DataType::TYPE_FLOAT) && !completion_base_array) {
  9468. if (current_uniform_hint == ShaderNode::Uniform::HINT_NONE) {
  9469. ScriptLanguage::CodeCompletionOption option("hint_range", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9470. if (completion_base == DataType::TYPE_INT) {
  9471. option.insert_text = "hint_range(0, 100, 1)";
  9472. } else {
  9473. option.insert_text = "hint_range(0.0, 1.0, 0.1)";
  9474. }
  9475. r_options->push_back(option);
  9476. }
  9477. } else if ((int(completion_base) > int(TYPE_MAT4) && int(completion_base) < int(TYPE_STRUCT))) {
  9478. Vector<String> options;
  9479. if (current_uniform_filter == FILTER_DEFAULT) {
  9480. options.push_back("filter_linear");
  9481. options.push_back("filter_linear_mipmap");
  9482. options.push_back("filter_linear_mipmap_anisotropic");
  9483. options.push_back("filter_nearest");
  9484. options.push_back("filter_nearest_mipmap");
  9485. options.push_back("filter_nearest_mipmap_anisotropic");
  9486. }
  9487. if (current_uniform_repeat == REPEAT_DEFAULT) {
  9488. options.push_back("repeat_enable");
  9489. options.push_back("repeat_disable");
  9490. }
  9491. if (completion_base_array) {
  9492. if (current_uniform_hint == ShaderNode::Uniform::HINT_NONE) {
  9493. options.push_back("source_color");
  9494. }
  9495. } else {
  9496. if (current_uniform_hint == ShaderNode::Uniform::HINT_NONE) {
  9497. options.push_back("hint_anisotropy");
  9498. options.push_back("hint_default_black");
  9499. options.push_back("hint_default_white");
  9500. options.push_back("hint_default_transparent");
  9501. options.push_back("hint_normal");
  9502. options.push_back("hint_roughness_a");
  9503. options.push_back("hint_roughness_b");
  9504. options.push_back("hint_roughness_g");
  9505. options.push_back("hint_roughness_gray");
  9506. options.push_back("hint_roughness_normal");
  9507. options.push_back("hint_roughness_r");
  9508. options.push_back("hint_screen_texture");
  9509. options.push_back("hint_normal_roughness_texture");
  9510. options.push_back("hint_depth_texture");
  9511. options.push_back("source_color");
  9512. }
  9513. }
  9514. for (int i = 0; i < options.size(); i++) {
  9515. ScriptLanguage::CodeCompletionOption option(options[i], ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9516. r_options->push_back(option);
  9517. }
  9518. }
  9519. if (!completion_base_array && !current_uniform_instance_index_defined) {
  9520. ScriptLanguage::CodeCompletionOption option("instance_index", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9521. option.insert_text = "instance_index(0)";
  9522. r_options->push_back(option);
  9523. }
  9524. } break;
  9525. }
  9526. return ERR_PARSE_ERROR;
  9527. }
  9528. String ShaderLanguage::get_error_text() {
  9529. return error_str;
  9530. }
  9531. Vector<ShaderLanguage::FilePosition> ShaderLanguage::get_include_positions() {
  9532. return include_positions;
  9533. }
  9534. int ShaderLanguage::get_error_line() {
  9535. return error_line;
  9536. }
  9537. ShaderLanguage::ShaderNode *ShaderLanguage::get_shader() {
  9538. return shader;
  9539. }
  9540. ShaderLanguage::ShaderLanguage() {
  9541. nodes = nullptr;
  9542. completion_class = TAG_GLOBAL;
  9543. if (instance_counter == 0) {
  9544. int idx = 0;
  9545. while (builtin_func_defs[idx].name) {
  9546. if (builtin_func_defs[idx].tag == SubClassTag::TAG_GLOBAL) {
  9547. global_func_set.insert(builtin_func_defs[idx].name);
  9548. }
  9549. idx++;
  9550. }
  9551. }
  9552. instance_counter++;
  9553. #ifdef DEBUG_ENABLED
  9554. warnings_check_map.insert(ShaderWarning::UNUSED_CONSTANT, &used_constants);
  9555. warnings_check_map.insert(ShaderWarning::UNUSED_FUNCTION, &used_functions);
  9556. warnings_check_map.insert(ShaderWarning::UNUSED_STRUCT, &used_structs);
  9557. warnings_check_map.insert(ShaderWarning::UNUSED_UNIFORM, &used_uniforms);
  9558. warnings_check_map.insert(ShaderWarning::UNUSED_VARYING, &used_varyings);
  9559. warnings_check_map2.insert(ShaderWarning::UNUSED_LOCAL_VARIABLE, &used_local_vars);
  9560. #endif // DEBUG_ENABLED
  9561. }
  9562. ShaderLanguage::~ShaderLanguage() {
  9563. clear();
  9564. instance_counter--;
  9565. if (instance_counter == 0) {
  9566. global_func_set.clear();
  9567. }
  9568. }