shader_language.cpp 372 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441
  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. "STRING_CONSTANT",
  93. "TYPE_VOID",
  94. "TYPE_BOOL",
  95. "TYPE_BVEC2",
  96. "TYPE_BVEC3",
  97. "TYPE_BVEC4",
  98. "TYPE_INT",
  99. "TYPE_IVEC2",
  100. "TYPE_IVEC3",
  101. "TYPE_IVEC4",
  102. "TYPE_UINT",
  103. "TYPE_UVEC2",
  104. "TYPE_UVEC3",
  105. "TYPE_UVEC4",
  106. "TYPE_FLOAT",
  107. "TYPE_VEC2",
  108. "TYPE_VEC3",
  109. "TYPE_VEC4",
  110. "TYPE_MAT2",
  111. "TYPE_MAT3",
  112. "TYPE_MAT4",
  113. "TYPE_SAMPLER2D",
  114. "TYPE_ISAMPLER2D",
  115. "TYPE_USAMPLER2D",
  116. "TYPE_SAMPLER2DARRAY",
  117. "TYPE_ISAMPLER2DARRAY",
  118. "TYPE_USAMPLER2DARRAY",
  119. "TYPE_SAMPLER3D",
  120. "TYPE_ISAMPLER3D",
  121. "TYPE_USAMPLER3D",
  122. "TYPE_SAMPLERCUBE",
  123. "TYPE_SAMPLERCUBEARRAY",
  124. "INTERPOLATION_FLAT",
  125. "INTERPOLATION_SMOOTH",
  126. "CONST",
  127. "STRUCT",
  128. "PRECISION_LOW",
  129. "PRECISION_MID",
  130. "PRECISION_HIGH",
  131. "OP_EQUAL",
  132. "OP_NOT_EQUAL",
  133. "OP_LESS",
  134. "OP_LESS_EQUAL",
  135. "OP_GREATER",
  136. "OP_GREATER_EQUAL",
  137. "OP_AND",
  138. "OP_OR",
  139. "OP_NOT",
  140. "OP_ADD",
  141. "OP_SUB",
  142. "OP_MUL",
  143. "OP_DIV",
  144. "OP_MOD",
  145. "OP_SHIFT_LEFT",
  146. "OP_SHIFT_RIGHT",
  147. "OP_ASSIGN",
  148. "OP_ASSIGN_ADD",
  149. "OP_ASSIGN_SUB",
  150. "OP_ASSIGN_MUL",
  151. "OP_ASSIGN_DIV",
  152. "OP_ASSIGN_MOD",
  153. "OP_ASSIGN_SHIFT_LEFT",
  154. "OP_ASSIGN_SHIFT_RIGHT",
  155. "OP_ASSIGN_BIT_AND",
  156. "OP_ASSIGN_BIT_OR",
  157. "OP_ASSIGN_BIT_XOR",
  158. "OP_BIT_AND",
  159. "OP_BIT_OR",
  160. "OP_BIT_XOR",
  161. "OP_BIT_INVERT",
  162. "OP_INCREMENT",
  163. "OP_DECREMENT",
  164. "CF_IF",
  165. "CF_ELSE",
  166. "CF_FOR",
  167. "CF_WHILE",
  168. "CF_DO",
  169. "CF_SWITCH",
  170. "CF_CASE",
  171. "CF_DEFAULT",
  172. "CF_BREAK",
  173. "CF_CONTINUE",
  174. "CF_RETURN",
  175. "CF_DISCARD",
  176. "BRACKET_OPEN",
  177. "BRACKET_CLOSE",
  178. "CURLY_BRACKET_OPEN",
  179. "CURLY_BRACKET_CLOSE",
  180. "PARENTHESIS_OPEN",
  181. "PARENTHESIS_CLOSE",
  182. "QUESTION",
  183. "COMMA",
  184. "COLON",
  185. "SEMICOLON",
  186. "PERIOD",
  187. "UNIFORM",
  188. "UNIFORM_GROUP",
  189. "INSTANCE",
  190. "GLOBAL",
  191. "VARYING",
  192. "ARG_IN",
  193. "ARG_OUT",
  194. "ARG_INOUT",
  195. "RENDER_MODE",
  196. "HINT_DEFAULT_WHITE_TEXTURE",
  197. "HINT_DEFAULT_BLACK_TEXTURE",
  198. "HINT_DEFAULT_TRANSPARENT_TEXTURE",
  199. "HINT_NORMAL_TEXTURE",
  200. "HINT_ROUGHNESS_NORMAL_TEXTURE",
  201. "HINT_ROUGHNESS_R",
  202. "HINT_ROUGHNESS_G",
  203. "HINT_ROUGHNESS_B",
  204. "HINT_ROUGHNESS_A",
  205. "HINT_ROUGHNESS_GRAY",
  206. "HINT_ANISOTROPY_TEXTURE",
  207. "HINT_SOURCE_COLOR",
  208. "HINT_RANGE",
  209. "HINT_ENUM",
  210. "HINT_INSTANCE_INDEX",
  211. "HINT_SCREEN_TEXTURE",
  212. "HINT_NORMAL_ROUGHNESS_TEXTURE",
  213. "HINT_DEPTH_TEXTURE",
  214. "FILTER_NEAREST",
  215. "FILTER_LINEAR",
  216. "FILTER_NEAREST_MIPMAP",
  217. "FILTER_LINEAR_MIPMAP",
  218. "FILTER_NEAREST_MIPMAP_ANISOTROPIC",
  219. "FILTER_LINEAR_MIPMAP_ANISOTROPIC",
  220. "REPEAT_ENABLE",
  221. "REPEAT_DISABLE",
  222. "SHADER_TYPE",
  223. "CURSOR",
  224. "ERROR",
  225. "EOF",
  226. };
  227. String ShaderLanguage::get_token_text(Token p_token) {
  228. String name = token_names[p_token.type];
  229. if (p_token.is_integer_constant() || p_token.type == TK_FLOAT_CONSTANT) {
  230. name += "(" + rtos(p_token.constant) + ")";
  231. } else if (p_token.type == TK_IDENTIFIER) {
  232. name += "(" + String(p_token.text) + ")";
  233. } else if (p_token.type == TK_ERROR) {
  234. name += "(" + String(p_token.text) + ")";
  235. }
  236. return name;
  237. }
  238. ShaderLanguage::Token ShaderLanguage::_make_token(TokenType p_type, const StringName &p_text) {
  239. Token tk;
  240. tk.type = p_type;
  241. tk.text = p_text;
  242. tk.line = tk_line;
  243. if (tk.type == TK_ERROR) {
  244. _set_error(p_text);
  245. }
  246. return tk;
  247. }
  248. enum ContextFlag : uint32_t {
  249. CF_UNSPECIFIED = 0U,
  250. CF_BLOCK = 1U, // "void test() { <x> }"
  251. CF_FUNC_DECL_PARAM_SPEC = 2U, // "void test(<x> int param) {}"
  252. CF_FUNC_DECL_PARAM_TYPE = 4U, // "void test(<x> param) {}"
  253. CF_IF_DECL = 8U, // "if(<x>) {}"
  254. CF_BOOLEAN = 16U, // "bool t = <x>;"
  255. CF_GLOBAL_SPACE = 32U, // "struct", "const", "void" etc.
  256. CF_DATATYPE = 64U, // "<x> value;"
  257. CF_UNIFORM_TYPE = 128U, // "uniform <x> myUniform;"
  258. CF_VARYING_TYPE = 256U, // "varying <x> myVarying;"
  259. CF_PRECISION_MODIFIER = 512U, // "<x> vec4 a = vec4(0.0, 1.0, 2.0, 3.0);"
  260. CF_INTERPOLATION_QUALIFIER = 1024U, // "varying <x> vec3 myColor;"
  261. CF_UNIFORM_KEYWORD = 2048U, // "uniform"
  262. CF_CONST_KEYWORD = 4096U, // "const"
  263. CF_UNIFORM_QUALIFIER = 8192U, // "<x> uniform float t;"
  264. CF_SHADER_TYPE = 16384U, // "shader_type"
  265. };
  266. const uint32_t KCF_DATATYPE = CF_BLOCK | CF_GLOBAL_SPACE | CF_DATATYPE | CF_FUNC_DECL_PARAM_TYPE | CF_UNIFORM_TYPE;
  267. const uint32_t KCF_SAMPLER_DATATYPE = CF_FUNC_DECL_PARAM_TYPE | CF_UNIFORM_TYPE;
  268. const ShaderLanguage::KeyWord ShaderLanguage::keyword_list[] = {
  269. { TK_TRUE, "true", CF_BLOCK | CF_IF_DECL | CF_BOOLEAN, {}, {} },
  270. { TK_FALSE, "false", CF_BLOCK | CF_IF_DECL | CF_BOOLEAN, {}, {} },
  271. // data types
  272. { TK_TYPE_VOID, "void", CF_GLOBAL_SPACE, {}, {} },
  273. { TK_TYPE_BOOL, "bool", KCF_DATATYPE, {}, {} },
  274. { TK_TYPE_BVEC2, "bvec2", KCF_DATATYPE, {}, {} },
  275. { TK_TYPE_BVEC3, "bvec3", KCF_DATATYPE, {}, {} },
  276. { TK_TYPE_BVEC4, "bvec4", KCF_DATATYPE, {}, {} },
  277. { TK_TYPE_INT, "int", KCF_DATATYPE, {}, {} },
  278. { TK_TYPE_IVEC2, "ivec2", KCF_DATATYPE, {}, {} },
  279. { TK_TYPE_IVEC3, "ivec3", KCF_DATATYPE, {}, {} },
  280. { TK_TYPE_IVEC4, "ivec4", KCF_DATATYPE, {}, {} },
  281. { TK_TYPE_UINT, "uint", KCF_DATATYPE, {}, {} },
  282. { TK_TYPE_UVEC2, "uvec2", KCF_DATATYPE, {}, {} },
  283. { TK_TYPE_UVEC3, "uvec3", KCF_DATATYPE, {}, {} },
  284. { TK_TYPE_UVEC4, "uvec4", KCF_DATATYPE, {}, {} },
  285. { TK_TYPE_FLOAT, "float", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  286. { TK_TYPE_VEC2, "vec2", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  287. { TK_TYPE_VEC3, "vec3", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  288. { TK_TYPE_VEC4, "vec4", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  289. { TK_TYPE_MAT2, "mat2", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  290. { TK_TYPE_MAT3, "mat3", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  291. { TK_TYPE_MAT4, "mat4", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  292. { TK_TYPE_SAMPLER2D, "sampler2D", KCF_SAMPLER_DATATYPE, {}, {} },
  293. { TK_TYPE_ISAMPLER2D, "isampler2D", KCF_SAMPLER_DATATYPE, {}, {} },
  294. { TK_TYPE_USAMPLER2D, "usampler2D", KCF_SAMPLER_DATATYPE, {}, {} },
  295. { TK_TYPE_SAMPLER2DARRAY, "sampler2DArray", KCF_SAMPLER_DATATYPE, {}, {} },
  296. { TK_TYPE_ISAMPLER2DARRAY, "isampler2DArray", KCF_SAMPLER_DATATYPE, {}, {} },
  297. { TK_TYPE_USAMPLER2DARRAY, "usampler2DArray", KCF_SAMPLER_DATATYPE, {}, {} },
  298. { TK_TYPE_SAMPLER3D, "sampler3D", KCF_SAMPLER_DATATYPE, {}, {} },
  299. { TK_TYPE_ISAMPLER3D, "isampler3D", KCF_SAMPLER_DATATYPE, {}, {} },
  300. { TK_TYPE_USAMPLER3D, "usampler3D", KCF_SAMPLER_DATATYPE, {}, {} },
  301. { TK_TYPE_SAMPLERCUBE, "samplerCube", KCF_SAMPLER_DATATYPE, {}, {} },
  302. { TK_TYPE_SAMPLERCUBEARRAY, "samplerCubeArray", KCF_SAMPLER_DATATYPE, {}, {} },
  303. // interpolation qualifiers
  304. { TK_INTERPOLATION_FLAT, "flat", CF_INTERPOLATION_QUALIFIER, {}, {} },
  305. { TK_INTERPOLATION_SMOOTH, "smooth", CF_INTERPOLATION_QUALIFIER, {}, {} },
  306. // precision modifiers
  307. { TK_PRECISION_LOW, "lowp", CF_BLOCK | CF_PRECISION_MODIFIER, {}, {} },
  308. { TK_PRECISION_MID, "mediump", CF_BLOCK | CF_PRECISION_MODIFIER, {}, {} },
  309. { TK_PRECISION_HIGH, "highp", CF_BLOCK | CF_PRECISION_MODIFIER, {}, {} },
  310. // global space keywords
  311. { TK_UNIFORM, "uniform", CF_GLOBAL_SPACE | CF_UNIFORM_KEYWORD, {}, {} },
  312. { TK_UNIFORM_GROUP, "group_uniforms", CF_GLOBAL_SPACE, {}, {} },
  313. { TK_VARYING, "varying", CF_GLOBAL_SPACE, { "particles", "sky", "fog" }, {} },
  314. { TK_CONST, "const", CF_BLOCK | CF_GLOBAL_SPACE | CF_CONST_KEYWORD, {}, {} },
  315. { TK_STRUCT, "struct", CF_GLOBAL_SPACE, {}, {} },
  316. { TK_SHADER_TYPE, "shader_type", CF_SHADER_TYPE, {}, {} },
  317. { TK_RENDER_MODE, "render_mode", CF_GLOBAL_SPACE, {}, {} },
  318. // uniform qualifiers
  319. { TK_INSTANCE, "instance", CF_GLOBAL_SPACE | CF_UNIFORM_QUALIFIER, {}, {} },
  320. { TK_GLOBAL, "global", CF_GLOBAL_SPACE | CF_UNIFORM_QUALIFIER, {}, {} },
  321. // block keywords
  322. { TK_CF_IF, "if", CF_BLOCK, {}, {} },
  323. { TK_CF_ELSE, "else", CF_BLOCK, {}, {} },
  324. { TK_CF_FOR, "for", CF_BLOCK, {}, {} },
  325. { TK_CF_WHILE, "while", CF_BLOCK, {}, {} },
  326. { TK_CF_DO, "do", CF_BLOCK, {}, {} },
  327. { TK_CF_SWITCH, "switch", CF_BLOCK, {}, {} },
  328. { TK_CF_CASE, "case", CF_BLOCK, {}, {} },
  329. { TK_CF_DEFAULT, "default", CF_BLOCK, {}, {} },
  330. { TK_CF_BREAK, "break", CF_BLOCK, {}, {} },
  331. { TK_CF_CONTINUE, "continue", CF_BLOCK, {}, {} },
  332. { TK_CF_RETURN, "return", CF_BLOCK, {}, {} },
  333. { TK_CF_DISCARD, "discard", CF_BLOCK, { "particles", "sky", "fog" }, { "fragment" } },
  334. // function specifier keywords
  335. { TK_ARG_IN, "in", CF_FUNC_DECL_PARAM_SPEC, {}, {} },
  336. { TK_ARG_OUT, "out", CF_FUNC_DECL_PARAM_SPEC, {}, {} },
  337. { TK_ARG_INOUT, "inout", CF_FUNC_DECL_PARAM_SPEC, {}, {} },
  338. // hints
  339. { TK_HINT_SOURCE_COLOR, "source_color", CF_UNSPECIFIED, {}, {} },
  340. { TK_HINT_RANGE, "hint_range", CF_UNSPECIFIED, {}, {} },
  341. { TK_HINT_ENUM, "hint_enum", CF_UNSPECIFIED, {}, {} },
  342. { TK_HINT_INSTANCE_INDEX, "instance_index", CF_UNSPECIFIED, {}, {} },
  343. // sampler hints
  344. { TK_HINT_NORMAL_TEXTURE, "hint_normal", CF_UNSPECIFIED, {}, {} },
  345. { TK_HINT_DEFAULT_WHITE_TEXTURE, "hint_default_white", CF_UNSPECIFIED, {}, {} },
  346. { TK_HINT_DEFAULT_BLACK_TEXTURE, "hint_default_black", CF_UNSPECIFIED, {}, {} },
  347. { TK_HINT_DEFAULT_TRANSPARENT_TEXTURE, "hint_default_transparent", CF_UNSPECIFIED, {}, {} },
  348. { TK_HINT_ANISOTROPY_TEXTURE, "hint_anisotropy", CF_UNSPECIFIED, {}, {} },
  349. { TK_HINT_ROUGHNESS_R, "hint_roughness_r", CF_UNSPECIFIED, {}, {} },
  350. { TK_HINT_ROUGHNESS_G, "hint_roughness_g", CF_UNSPECIFIED, {}, {} },
  351. { TK_HINT_ROUGHNESS_B, "hint_roughness_b", CF_UNSPECIFIED, {}, {} },
  352. { TK_HINT_ROUGHNESS_A, "hint_roughness_a", CF_UNSPECIFIED, {}, {} },
  353. { TK_HINT_ROUGHNESS_NORMAL_TEXTURE, "hint_roughness_normal", CF_UNSPECIFIED, {}, {} },
  354. { TK_HINT_ROUGHNESS_GRAY, "hint_roughness_gray", CF_UNSPECIFIED, {}, {} },
  355. { TK_HINT_SCREEN_TEXTURE, "hint_screen_texture", CF_UNSPECIFIED, {}, {} },
  356. { TK_HINT_NORMAL_ROUGHNESS_TEXTURE, "hint_normal_roughness_texture", CF_UNSPECIFIED, {}, {} },
  357. { TK_HINT_DEPTH_TEXTURE, "hint_depth_texture", CF_UNSPECIFIED, {}, {} },
  358. { TK_FILTER_NEAREST, "filter_nearest", CF_UNSPECIFIED, {}, {} },
  359. { TK_FILTER_LINEAR, "filter_linear", CF_UNSPECIFIED, {}, {} },
  360. { TK_FILTER_NEAREST_MIPMAP, "filter_nearest_mipmap", CF_UNSPECIFIED, {}, {} },
  361. { TK_FILTER_LINEAR_MIPMAP, "filter_linear_mipmap", CF_UNSPECIFIED, {}, {} },
  362. { TK_FILTER_NEAREST_MIPMAP_ANISOTROPIC, "filter_nearest_mipmap_anisotropic", CF_UNSPECIFIED, {}, {} },
  363. { TK_FILTER_LINEAR_MIPMAP_ANISOTROPIC, "filter_linear_mipmap_anisotropic", CF_UNSPECIFIED, {}, {} },
  364. { TK_REPEAT_ENABLE, "repeat_enable", CF_UNSPECIFIED, {}, {} },
  365. { TK_REPEAT_DISABLE, "repeat_disable", CF_UNSPECIFIED, {}, {} },
  366. { TK_ERROR, nullptr, CF_UNSPECIFIED, {}, {} }
  367. };
  368. ShaderLanguage::Token ShaderLanguage::_get_token() {
  369. #define GETCHAR(m_idx) (((char_idx + m_idx) < code.length()) ? code[char_idx + m_idx] : char32_t(0))
  370. while (true) {
  371. char_idx++;
  372. switch (GETCHAR(-1)) {
  373. case 0:
  374. return _make_token(TK_EOF);
  375. case 0xFFFF:
  376. return _make_token(TK_CURSOR); //for completion
  377. case '\t':
  378. case '\r':
  379. case ' ':
  380. continue;
  381. case '\n':
  382. tk_line++;
  383. continue;
  384. case '/': {
  385. switch (GETCHAR(0)) {
  386. case '*': { // block comment
  387. char_idx++;
  388. while (true) {
  389. if (GETCHAR(0) == 0) {
  390. return _make_token(TK_EOF);
  391. }
  392. if (GETCHAR(0) == '*' && GETCHAR(1) == '/') {
  393. char_idx += 2;
  394. break;
  395. } else if (GETCHAR(0) == '\n') {
  396. tk_line++;
  397. }
  398. char_idx++;
  399. }
  400. } break;
  401. case '/': { // line comment skip
  402. while (true) {
  403. if (GETCHAR(0) == '\n') {
  404. tk_line++;
  405. char_idx++;
  406. break;
  407. }
  408. if (GETCHAR(0) == 0) {
  409. return _make_token(TK_EOF);
  410. }
  411. char_idx++;
  412. }
  413. } break;
  414. case '=': { // diveq
  415. char_idx++;
  416. return _make_token(TK_OP_ASSIGN_DIV);
  417. } break;
  418. default:
  419. return _make_token(TK_OP_DIV);
  420. }
  421. continue; //a comment, continue to next token
  422. } break;
  423. case '=': {
  424. if (GETCHAR(0) == '=') {
  425. char_idx++;
  426. return _make_token(TK_OP_EQUAL);
  427. }
  428. return _make_token(TK_OP_ASSIGN);
  429. } break;
  430. case '<': {
  431. if (GETCHAR(0) == '=') {
  432. char_idx++;
  433. return _make_token(TK_OP_LESS_EQUAL);
  434. } else if (GETCHAR(0) == '<') {
  435. char_idx++;
  436. if (GETCHAR(0) == '=') {
  437. char_idx++;
  438. return _make_token(TK_OP_ASSIGN_SHIFT_LEFT);
  439. }
  440. return _make_token(TK_OP_SHIFT_LEFT);
  441. }
  442. return _make_token(TK_OP_LESS);
  443. } break;
  444. case '>': {
  445. if (GETCHAR(0) == '=') {
  446. char_idx++;
  447. return _make_token(TK_OP_GREATER_EQUAL);
  448. } else if (GETCHAR(0) == '>') {
  449. char_idx++;
  450. if (GETCHAR(0) == '=') {
  451. char_idx++;
  452. return _make_token(TK_OP_ASSIGN_SHIFT_RIGHT);
  453. }
  454. return _make_token(TK_OP_SHIFT_RIGHT);
  455. }
  456. return _make_token(TK_OP_GREATER);
  457. } break;
  458. case '!': {
  459. if (GETCHAR(0) == '=') {
  460. char_idx++;
  461. return _make_token(TK_OP_NOT_EQUAL);
  462. }
  463. return _make_token(TK_OP_NOT);
  464. } break;
  465. case '"': {
  466. String _content = "";
  467. bool _previous_backslash = false;
  468. while (true) {
  469. bool _ended = false;
  470. char32_t c = GETCHAR(0);
  471. if (c == 0) {
  472. return _make_token(TK_ERROR, "EOF reached before string termination.");
  473. }
  474. switch (c) {
  475. case '"': {
  476. if (_previous_backslash) {
  477. _content += '"';
  478. _previous_backslash = false;
  479. } else {
  480. _ended = true;
  481. }
  482. break;
  483. }
  484. case '\\': {
  485. if (_previous_backslash) {
  486. _content += '\\';
  487. }
  488. _previous_backslash = !_previous_backslash;
  489. break;
  490. }
  491. case '\n': {
  492. return _make_token(TK_ERROR, "Unexpected end of string.");
  493. }
  494. default: {
  495. if (!_previous_backslash) {
  496. _content += c;
  497. } else {
  498. return _make_token(TK_ERROR, "Only \\\" and \\\\ escape characters supported.");
  499. }
  500. break;
  501. }
  502. }
  503. char_idx++;
  504. if (_ended) {
  505. break;
  506. }
  507. }
  508. return _make_token(TK_STRING_CONSTANT, _content);
  509. } break;
  510. //case '\'' //string - no strings in shader
  511. case '{':
  512. return _make_token(TK_CURLY_BRACKET_OPEN);
  513. case '}':
  514. return _make_token(TK_CURLY_BRACKET_CLOSE);
  515. case '[':
  516. return _make_token(TK_BRACKET_OPEN);
  517. case ']':
  518. return _make_token(TK_BRACKET_CLOSE);
  519. case '(':
  520. return _make_token(TK_PARENTHESIS_OPEN);
  521. case ')':
  522. return _make_token(TK_PARENTHESIS_CLOSE);
  523. case ',':
  524. return _make_token(TK_COMMA);
  525. case ';':
  526. return _make_token(TK_SEMICOLON);
  527. case '?':
  528. return _make_token(TK_QUESTION);
  529. case ':':
  530. return _make_token(TK_COLON);
  531. case '^':
  532. if (GETCHAR(0) == '=') {
  533. char_idx++;
  534. return _make_token(TK_OP_ASSIGN_BIT_XOR);
  535. }
  536. return _make_token(TK_OP_BIT_XOR);
  537. case '~':
  538. return _make_token(TK_OP_BIT_INVERT);
  539. case '&': {
  540. if (GETCHAR(0) == '=') {
  541. char_idx++;
  542. return _make_token(TK_OP_ASSIGN_BIT_AND);
  543. } else if (GETCHAR(0) == '&') {
  544. char_idx++;
  545. return _make_token(TK_OP_AND);
  546. }
  547. return _make_token(TK_OP_BIT_AND);
  548. } break;
  549. case '|': {
  550. if (GETCHAR(0) == '=') {
  551. char_idx++;
  552. return _make_token(TK_OP_ASSIGN_BIT_OR);
  553. } else if (GETCHAR(0) == '|') {
  554. char_idx++;
  555. return _make_token(TK_OP_OR);
  556. }
  557. return _make_token(TK_OP_BIT_OR);
  558. } break;
  559. case '*': {
  560. if (GETCHAR(0) == '=') {
  561. char_idx++;
  562. return _make_token(TK_OP_ASSIGN_MUL);
  563. }
  564. return _make_token(TK_OP_MUL);
  565. } break;
  566. case '+': {
  567. if (GETCHAR(0) == '=') {
  568. char_idx++;
  569. return _make_token(TK_OP_ASSIGN_ADD);
  570. } else if (GETCHAR(0) == '+') {
  571. char_idx++;
  572. return _make_token(TK_OP_INCREMENT);
  573. }
  574. return _make_token(TK_OP_ADD);
  575. } break;
  576. case '-': {
  577. if (GETCHAR(0) == '=') {
  578. char_idx++;
  579. return _make_token(TK_OP_ASSIGN_SUB);
  580. } else if (GETCHAR(0) == '-') {
  581. char_idx++;
  582. return _make_token(TK_OP_DECREMENT);
  583. }
  584. return _make_token(TK_OP_SUB);
  585. } break;
  586. case '%': {
  587. if (GETCHAR(0) == '=') {
  588. char_idx++;
  589. return _make_token(TK_OP_ASSIGN_MOD);
  590. }
  591. return _make_token(TK_OP_MOD);
  592. } break;
  593. case '@': {
  594. if (GETCHAR(0) == '@' && GETCHAR(1) == '>') {
  595. char_idx += 2;
  596. LocalVector<char32_t> incp;
  597. while (GETCHAR(0) != '\n') {
  598. incp.push_back(GETCHAR(0));
  599. char_idx++;
  600. }
  601. incp.push_back(0); // Zero end it.
  602. String include_path(incp.ptr());
  603. include_positions.write[include_positions.size() - 1].line = tk_line;
  604. String marker = ">>" + include_path;
  605. if (!include_markers_handled.has(marker)) {
  606. include_markers_handled.insert(marker);
  607. FilePosition fp;
  608. fp.file = include_path;
  609. fp.line = 0;
  610. tk_line = 0;
  611. include_positions.push_back(fp);
  612. }
  613. } else if (GETCHAR(0) == '@' && GETCHAR(1) == '<') {
  614. char_idx += 2;
  615. LocalVector<char32_t> incp;
  616. while (GETCHAR(0) != '\n') {
  617. incp.push_back(GETCHAR(0));
  618. char_idx++;
  619. }
  620. incp.push_back(0); // Zero end it.
  621. String include_path(incp.ptr());
  622. String marker = "<<" + include_path;
  623. if (!include_markers_handled.has(marker)) {
  624. include_markers_handled.insert(marker);
  625. if (include_positions.size() == 1) {
  626. return _make_token(TK_ERROR, "Invalid include exit hint @@< without matching enter hint.");
  627. }
  628. include_positions.resize(include_positions.size() - 1); // Pop back.
  629. }
  630. tk_line = include_positions[include_positions.size() - 1].line - 1; // Restore line.
  631. } else {
  632. return _make_token(TK_ERROR, "Invalid include enter/exit hint token (@@> and @@<)");
  633. }
  634. } break;
  635. default: {
  636. char_idx--; //go back one, since we have no idea what this is
  637. if (is_digit(GETCHAR(0)) || (GETCHAR(0) == '.' && is_digit(GETCHAR(1)))) {
  638. // parse number
  639. bool hexa_found = false;
  640. bool period_found = false;
  641. bool exponent_found = false;
  642. bool float_suffix_found = false;
  643. bool uint_suffix_found = false;
  644. bool end_suffix_found = false;
  645. enum {
  646. CASE_ALL,
  647. CASE_HEXA_PERIOD,
  648. CASE_EXPONENT,
  649. CASE_SIGN_AFTER_EXPONENT,
  650. CASE_NONE,
  651. CASE_MAX,
  652. } lut_case = CASE_ALL;
  653. static bool suffix_lut[CASE_MAX][127];
  654. if (!is_const_suffix_lut_initialized) {
  655. is_const_suffix_lut_initialized = true;
  656. for (int i = 0; i < 127; i++) {
  657. char t = char(i);
  658. suffix_lut[CASE_ALL][i] = t == '.' || t == 'x' || t == 'e' || t == 'f' || t == 'u' || t == '-' || t == '+';
  659. suffix_lut[CASE_HEXA_PERIOD][i] = t == 'e' || t == 'f' || t == 'u';
  660. suffix_lut[CASE_EXPONENT][i] = t == 'f' || t == '-' || t == '+';
  661. suffix_lut[CASE_SIGN_AFTER_EXPONENT][i] = t == 'f';
  662. suffix_lut[CASE_NONE][i] = false;
  663. }
  664. }
  665. String str;
  666. int i = 0;
  667. bool digit_after_exp = false;
  668. while (true) {
  669. const char32_t symbol = String::char_lowercase(GETCHAR(i));
  670. bool error = false;
  671. if (is_digit(symbol)) {
  672. if (exponent_found) {
  673. digit_after_exp = true;
  674. }
  675. if (end_suffix_found) {
  676. error = true;
  677. }
  678. } else {
  679. if (symbol < 0x7F && suffix_lut[lut_case][symbol]) {
  680. if (symbol == 'x') {
  681. hexa_found = true;
  682. lut_case = CASE_HEXA_PERIOD;
  683. } else if (symbol == '.') {
  684. period_found = true;
  685. lut_case = CASE_HEXA_PERIOD;
  686. } else if (symbol == 'e' && !hexa_found) {
  687. exponent_found = true;
  688. lut_case = CASE_EXPONENT;
  689. } else if (symbol == 'f' && !hexa_found) {
  690. if (!period_found && !exponent_found) {
  691. error = true;
  692. }
  693. float_suffix_found = true;
  694. end_suffix_found = true;
  695. lut_case = CASE_NONE;
  696. } else if (symbol == 'u') {
  697. uint_suffix_found = true;
  698. end_suffix_found = true;
  699. lut_case = CASE_NONE;
  700. } else if (symbol == '-' || symbol == '+') {
  701. if (exponent_found) {
  702. lut_case = CASE_SIGN_AFTER_EXPONENT;
  703. } else {
  704. break;
  705. }
  706. }
  707. } else if (!hexa_found || !is_hex_digit(symbol)) {
  708. if (is_ascii_identifier_char(symbol)) {
  709. error = true;
  710. } else {
  711. break;
  712. }
  713. }
  714. }
  715. if (error) {
  716. if (hexa_found) {
  717. return _make_token(TK_ERROR, "Invalid (hexadecimal) numeric constant");
  718. }
  719. if (period_found || exponent_found || float_suffix_found) {
  720. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  721. }
  722. if (uint_suffix_found) {
  723. return _make_token(TK_ERROR, "Invalid (unsigned integer) numeric constant");
  724. }
  725. return _make_token(TK_ERROR, "Invalid (integer) numeric constant");
  726. }
  727. str += symbol;
  728. i++;
  729. }
  730. char32_t last_char = str[str.length() - 1];
  731. if (hexa_found) { // Integer (hex).
  732. if (uint_suffix_found) {
  733. // Strip the suffix.
  734. str = str.left(str.length() - 1);
  735. // Compensate reading cursor position.
  736. char_idx += 1;
  737. }
  738. if (str.size() > 11 || !str.is_valid_hex_number(true)) { // > 0xFFFFFFFF
  739. return _make_token(TK_ERROR, "Invalid (hexadecimal) numeric constant");
  740. }
  741. } else if (period_found || exponent_found || float_suffix_found) { // Float
  742. 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).
  743. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  744. }
  745. if (period_found) {
  746. if (float_suffix_found) {
  747. //checks for eg "1.f" or "1.99f" notations
  748. if (last_char != 'f') {
  749. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  750. }
  751. } else {
  752. //checks for eg. "1." or "1.99" notations
  753. if (last_char != '.' && !is_digit(last_char)) {
  754. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  755. }
  756. }
  757. } else if (float_suffix_found) {
  758. // if no period found the float suffix must be the last character, like in "2f" for "2.0"
  759. if (last_char != 'f') {
  760. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  761. }
  762. }
  763. if (float_suffix_found) {
  764. // Strip the suffix.
  765. str = str.left(str.length() - 1);
  766. // Compensate reading cursor position.
  767. char_idx += 1;
  768. }
  769. if (!str.is_valid_float()) {
  770. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  771. }
  772. } else { // Integer
  773. if (uint_suffix_found) {
  774. // Strip the suffix.
  775. str = str.left(str.length() - 1);
  776. // Compensate reading cursor position.
  777. char_idx += 1;
  778. }
  779. if (!str.is_valid_int()) {
  780. if (uint_suffix_found) {
  781. return _make_token(TK_ERROR, "Invalid (unsigned integer) numeric constant");
  782. } else {
  783. return _make_token(TK_ERROR, "Invalid (integer) numeric constant");
  784. }
  785. }
  786. }
  787. char_idx += str.length();
  788. Token tk;
  789. if (period_found || exponent_found || float_suffix_found) {
  790. tk.type = TK_FLOAT_CONSTANT;
  791. } else if (uint_suffix_found) {
  792. tk.type = TK_UINT_CONSTANT;
  793. } else {
  794. tk.type = TK_INT_CONSTANT;
  795. }
  796. if (hexa_found) {
  797. tk.constant = (double)str.hex_to_int();
  798. } else {
  799. tk.constant = str.to_float();
  800. }
  801. tk.line = tk_line;
  802. return tk;
  803. }
  804. if (GETCHAR(0) == '.') {
  805. //parse period
  806. char_idx++;
  807. return _make_token(TK_PERIOD);
  808. }
  809. if (is_ascii_identifier_char(GETCHAR(0))) {
  810. // parse identifier
  811. String str;
  812. while (is_ascii_identifier_char(GETCHAR(0))) {
  813. str += char32_t(GETCHAR(0));
  814. char_idx++;
  815. }
  816. //see if keyword
  817. //should be converted to a static map
  818. int idx = 0;
  819. while (keyword_list[idx].text) {
  820. if (str == keyword_list[idx].text) {
  821. return _make_token(keyword_list[idx].token);
  822. }
  823. idx++;
  824. }
  825. str = str.replace("dus_", "_");
  826. return _make_token(TK_IDENTIFIER, str);
  827. }
  828. if (GETCHAR(0) > 32) {
  829. return _make_token(TK_ERROR, "Tokenizer: Unknown character #" + itos(GETCHAR(0)) + ": '" + String::chr(GETCHAR(0)) + "'");
  830. } else {
  831. return _make_token(TK_ERROR, "Tokenizer: Unknown character #" + itos(GETCHAR(0)));
  832. }
  833. } break;
  834. }
  835. }
  836. ERR_PRINT("BUG");
  837. return Token();
  838. #undef GETCHAR
  839. }
  840. bool ShaderLanguage::_lookup_next(Token &r_tk) {
  841. TkPos pre_pos = _get_tkpos();
  842. int line = pre_pos.tk_line;
  843. _get_token();
  844. Token tk = _get_token();
  845. _set_tkpos(pre_pos);
  846. if (tk.line == line) {
  847. r_tk = tk;
  848. return true;
  849. }
  850. return false;
  851. }
  852. ShaderLanguage::Token ShaderLanguage::_peek() {
  853. TkPos pre_pos = _get_tkpos();
  854. Token tk = _get_token();
  855. _set_tkpos(pre_pos);
  856. return tk;
  857. }
  858. String ShaderLanguage::token_debug(const String &p_code) {
  859. clear();
  860. code = p_code;
  861. String output;
  862. Token tk = _get_token();
  863. while (tk.type != TK_EOF && tk.type != TK_ERROR) {
  864. output += itos(tk_line) + ": " + get_token_text(tk) + "\n";
  865. tk = _get_token();
  866. }
  867. return output;
  868. }
  869. bool ShaderLanguage::is_token_variable_datatype(TokenType p_type) {
  870. return (
  871. p_type == TK_TYPE_VOID ||
  872. p_type == TK_TYPE_BOOL ||
  873. p_type == TK_TYPE_BVEC2 ||
  874. p_type == TK_TYPE_BVEC3 ||
  875. p_type == TK_TYPE_BVEC4 ||
  876. p_type == TK_TYPE_INT ||
  877. p_type == TK_TYPE_IVEC2 ||
  878. p_type == TK_TYPE_IVEC3 ||
  879. p_type == TK_TYPE_IVEC4 ||
  880. p_type == TK_TYPE_UINT ||
  881. p_type == TK_TYPE_UVEC2 ||
  882. p_type == TK_TYPE_UVEC3 ||
  883. p_type == TK_TYPE_UVEC4 ||
  884. p_type == TK_TYPE_FLOAT ||
  885. p_type == TK_TYPE_VEC2 ||
  886. p_type == TK_TYPE_VEC3 ||
  887. p_type == TK_TYPE_VEC4 ||
  888. p_type == TK_TYPE_MAT2 ||
  889. p_type == TK_TYPE_MAT3 ||
  890. p_type == TK_TYPE_MAT4);
  891. }
  892. bool ShaderLanguage::is_token_datatype(TokenType p_type) {
  893. return (
  894. p_type == TK_TYPE_VOID ||
  895. p_type == TK_TYPE_BOOL ||
  896. p_type == TK_TYPE_BVEC2 ||
  897. p_type == TK_TYPE_BVEC3 ||
  898. p_type == TK_TYPE_BVEC4 ||
  899. p_type == TK_TYPE_INT ||
  900. p_type == TK_TYPE_IVEC2 ||
  901. p_type == TK_TYPE_IVEC3 ||
  902. p_type == TK_TYPE_IVEC4 ||
  903. p_type == TK_TYPE_UINT ||
  904. p_type == TK_TYPE_UVEC2 ||
  905. p_type == TK_TYPE_UVEC3 ||
  906. p_type == TK_TYPE_UVEC4 ||
  907. p_type == TK_TYPE_FLOAT ||
  908. p_type == TK_TYPE_VEC2 ||
  909. p_type == TK_TYPE_VEC3 ||
  910. p_type == TK_TYPE_VEC4 ||
  911. p_type == TK_TYPE_MAT2 ||
  912. p_type == TK_TYPE_MAT3 ||
  913. p_type == TK_TYPE_MAT4 ||
  914. p_type == TK_TYPE_SAMPLER2D ||
  915. p_type == TK_TYPE_ISAMPLER2D ||
  916. p_type == TK_TYPE_USAMPLER2D ||
  917. p_type == TK_TYPE_SAMPLER2DARRAY ||
  918. p_type == TK_TYPE_ISAMPLER2DARRAY ||
  919. p_type == TK_TYPE_USAMPLER2DARRAY ||
  920. p_type == TK_TYPE_SAMPLER3D ||
  921. p_type == TK_TYPE_ISAMPLER3D ||
  922. p_type == TK_TYPE_USAMPLER3D ||
  923. p_type == TK_TYPE_SAMPLERCUBE ||
  924. p_type == TK_TYPE_SAMPLERCUBEARRAY);
  925. }
  926. ShaderLanguage::DataType ShaderLanguage::get_token_datatype(TokenType p_type) {
  927. return DataType(p_type - TK_TYPE_VOID);
  928. }
  929. bool ShaderLanguage::is_token_interpolation(TokenType p_type) {
  930. return (
  931. p_type == TK_INTERPOLATION_FLAT ||
  932. p_type == TK_INTERPOLATION_SMOOTH);
  933. }
  934. ShaderLanguage::DataInterpolation ShaderLanguage::get_token_interpolation(TokenType p_type) {
  935. if (p_type == TK_INTERPOLATION_FLAT) {
  936. return INTERPOLATION_FLAT;
  937. } else {
  938. return INTERPOLATION_SMOOTH;
  939. }
  940. }
  941. bool ShaderLanguage::is_token_precision(TokenType p_type) {
  942. return (
  943. p_type == TK_PRECISION_LOW ||
  944. p_type == TK_PRECISION_MID ||
  945. p_type == TK_PRECISION_HIGH);
  946. }
  947. bool ShaderLanguage::is_token_arg_qual(TokenType p_type) {
  948. return (
  949. p_type == TK_ARG_IN ||
  950. p_type == TK_ARG_OUT ||
  951. p_type == TK_ARG_INOUT);
  952. }
  953. ShaderLanguage::DataPrecision ShaderLanguage::get_token_precision(TokenType p_type) {
  954. if (p_type == TK_PRECISION_LOW) {
  955. return PRECISION_LOWP;
  956. } else if (p_type == TK_PRECISION_HIGH) {
  957. return PRECISION_HIGHP;
  958. } else {
  959. return PRECISION_MEDIUMP;
  960. }
  961. }
  962. String ShaderLanguage::get_precision_name(DataPrecision p_type) {
  963. switch (p_type) {
  964. case PRECISION_LOWP:
  965. return "lowp";
  966. case PRECISION_MEDIUMP:
  967. return "mediump";
  968. case PRECISION_HIGHP:
  969. return "highp";
  970. default:
  971. break;
  972. }
  973. return "";
  974. }
  975. String ShaderLanguage::get_interpolation_name(DataInterpolation p_interpolation) {
  976. switch (p_interpolation) {
  977. case INTERPOLATION_FLAT:
  978. return "flat";
  979. case INTERPOLATION_SMOOTH:
  980. return "smooth";
  981. default:
  982. break;
  983. }
  984. return "";
  985. }
  986. String ShaderLanguage::get_datatype_name(DataType p_type) {
  987. switch (p_type) {
  988. case TYPE_VOID:
  989. return "void";
  990. case TYPE_BOOL:
  991. return "bool";
  992. case TYPE_BVEC2:
  993. return "bvec2";
  994. case TYPE_BVEC3:
  995. return "bvec3";
  996. case TYPE_BVEC4:
  997. return "bvec4";
  998. case TYPE_INT:
  999. return "int";
  1000. case TYPE_IVEC2:
  1001. return "ivec2";
  1002. case TYPE_IVEC3:
  1003. return "ivec3";
  1004. case TYPE_IVEC4:
  1005. return "ivec4";
  1006. case TYPE_UINT:
  1007. return "uint";
  1008. case TYPE_UVEC2:
  1009. return "uvec2";
  1010. case TYPE_UVEC3:
  1011. return "uvec3";
  1012. case TYPE_UVEC4:
  1013. return "uvec4";
  1014. case TYPE_FLOAT:
  1015. return "float";
  1016. case TYPE_VEC2:
  1017. return "vec2";
  1018. case TYPE_VEC3:
  1019. return "vec3";
  1020. case TYPE_VEC4:
  1021. return "vec4";
  1022. case TYPE_MAT2:
  1023. return "mat2";
  1024. case TYPE_MAT3:
  1025. return "mat3";
  1026. case TYPE_MAT4:
  1027. return "mat4";
  1028. case TYPE_SAMPLER2D:
  1029. return "sampler2D";
  1030. case TYPE_ISAMPLER2D:
  1031. return "isampler2D";
  1032. case TYPE_USAMPLER2D:
  1033. return "usampler2D";
  1034. case TYPE_SAMPLER2DARRAY:
  1035. return "sampler2DArray";
  1036. case TYPE_ISAMPLER2DARRAY:
  1037. return "isampler2DArray";
  1038. case TYPE_USAMPLER2DARRAY:
  1039. return "usampler2DArray";
  1040. case TYPE_SAMPLER3D:
  1041. return "sampler3D";
  1042. case TYPE_ISAMPLER3D:
  1043. return "isampler3D";
  1044. case TYPE_USAMPLER3D:
  1045. return "usampler3D";
  1046. case TYPE_SAMPLERCUBE:
  1047. return "samplerCube";
  1048. case TYPE_SAMPLERCUBEARRAY:
  1049. return "samplerCubeArray";
  1050. case TYPE_STRUCT:
  1051. return "struct";
  1052. case TYPE_MAX:
  1053. return "invalid";
  1054. }
  1055. return "";
  1056. }
  1057. String ShaderLanguage::get_uniform_hint_name(ShaderNode::Uniform::Hint p_hint) {
  1058. String result;
  1059. switch (p_hint) {
  1060. case ShaderNode::Uniform::HINT_RANGE: {
  1061. result = "hint_range";
  1062. } break;
  1063. case ShaderNode::Uniform::HINT_ENUM: {
  1064. result = "hint_enum";
  1065. } break;
  1066. case ShaderNode::Uniform::HINT_SOURCE_COLOR: {
  1067. result = "source_color";
  1068. } break;
  1069. case ShaderNode::Uniform::HINT_NORMAL: {
  1070. result = "hint_normal";
  1071. } break;
  1072. case ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL: {
  1073. result = "hint_roughness_normal";
  1074. } break;
  1075. case ShaderNode::Uniform::HINT_ROUGHNESS_R: {
  1076. result = "hint_roughness_r";
  1077. } break;
  1078. case ShaderNode::Uniform::HINT_ROUGHNESS_G: {
  1079. result = "hint_roughness_g";
  1080. } break;
  1081. case ShaderNode::Uniform::HINT_ROUGHNESS_B: {
  1082. result = "hint_roughness_b";
  1083. } break;
  1084. case ShaderNode::Uniform::HINT_ROUGHNESS_A: {
  1085. result = "hint_roughness_a";
  1086. } break;
  1087. case ShaderNode::Uniform::HINT_ROUGHNESS_GRAY: {
  1088. result = "hint_roughness_gray";
  1089. } break;
  1090. case ShaderNode::Uniform::HINT_DEFAULT_BLACK: {
  1091. result = "hint_default_black";
  1092. } break;
  1093. case ShaderNode::Uniform::HINT_DEFAULT_WHITE: {
  1094. result = "hint_default_white";
  1095. } break;
  1096. case ShaderNode::Uniform::HINT_DEFAULT_TRANSPARENT: {
  1097. result = "hint_default_transparent";
  1098. } break;
  1099. case ShaderNode::Uniform::HINT_ANISOTROPY: {
  1100. result = "hint_anisotropy";
  1101. } break;
  1102. case ShaderNode::Uniform::HINT_SCREEN_TEXTURE: {
  1103. result = "hint_screen_texture";
  1104. } break;
  1105. case ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE: {
  1106. result = "hint_normal_roughness_texture";
  1107. } break;
  1108. case ShaderNode::Uniform::HINT_DEPTH_TEXTURE: {
  1109. result = "hint_depth_texture";
  1110. } break;
  1111. default:
  1112. break;
  1113. }
  1114. return result;
  1115. }
  1116. String ShaderLanguage::get_texture_filter_name(TextureFilter p_filter) {
  1117. String result;
  1118. switch (p_filter) {
  1119. case FILTER_NEAREST: {
  1120. result = "filter_nearest";
  1121. } break;
  1122. case FILTER_LINEAR: {
  1123. result = "filter_linear";
  1124. } break;
  1125. case FILTER_NEAREST_MIPMAP: {
  1126. result = "filter_nearest_mipmap";
  1127. } break;
  1128. case FILTER_LINEAR_MIPMAP: {
  1129. result = "filter_linear_mipmap";
  1130. } break;
  1131. case FILTER_NEAREST_MIPMAP_ANISOTROPIC: {
  1132. result = "filter_nearest_mipmap_anisotropic";
  1133. } break;
  1134. case FILTER_LINEAR_MIPMAP_ANISOTROPIC: {
  1135. result = "filter_linear_mipmap_anisotropic";
  1136. } break;
  1137. default: {
  1138. } break;
  1139. }
  1140. return result;
  1141. }
  1142. String ShaderLanguage::get_texture_repeat_name(TextureRepeat p_repeat) {
  1143. String result;
  1144. switch (p_repeat) {
  1145. case REPEAT_DISABLE: {
  1146. result = "repeat_disable";
  1147. } break;
  1148. case REPEAT_ENABLE: {
  1149. result = "repeat_enable";
  1150. } break;
  1151. default: {
  1152. } break;
  1153. }
  1154. return result;
  1155. }
  1156. bool ShaderLanguage::is_token_nonvoid_datatype(TokenType p_type) {
  1157. return is_token_datatype(p_type) && p_type != TK_TYPE_VOID;
  1158. }
  1159. void ShaderLanguage::clear() {
  1160. current_function = StringName();
  1161. last_name = StringName();
  1162. last_type = IDENTIFIER_MAX;
  1163. current_uniform_group_name = "";
  1164. current_uniform_subgroup_name = "";
  1165. current_uniform_hint = ShaderNode::Uniform::HINT_NONE;
  1166. current_uniform_filter = FILTER_DEFAULT;
  1167. current_uniform_repeat = REPEAT_DEFAULT;
  1168. current_uniform_instance_index_defined = false;
  1169. completion_type = COMPLETION_NONE;
  1170. completion_block = nullptr;
  1171. completion_function = StringName();
  1172. completion_class = TAG_GLOBAL;
  1173. completion_struct = StringName();
  1174. completion_base = TYPE_VOID;
  1175. completion_base_array = false;
  1176. include_positions.clear();
  1177. include_positions.push_back(FilePosition());
  1178. include_markers_handled.clear();
  1179. calls_info.clear();
  1180. #ifdef DEBUG_ENABLED
  1181. keyword_completion_context = CF_UNSPECIFIED;
  1182. used_constants.clear();
  1183. used_varyings.clear();
  1184. used_uniforms.clear();
  1185. used_functions.clear();
  1186. used_structs.clear();
  1187. used_local_vars.clear();
  1188. warnings.clear();
  1189. #endif // DEBUG_ENABLED
  1190. error_line = 0;
  1191. tk_line = 1;
  1192. char_idx = 0;
  1193. error_set = false;
  1194. error_str = "";
  1195. is_const_decl = false;
  1196. while (nodes) {
  1197. Node *n = nodes;
  1198. nodes = nodes->next;
  1199. memdelete(n);
  1200. }
  1201. }
  1202. #ifdef DEBUG_ENABLED
  1203. void ShaderLanguage::_parse_used_identifier(const StringName &p_identifier, IdentifierType p_type, const StringName &p_function) {
  1204. switch (p_type) {
  1205. case IdentifierType::IDENTIFIER_CONSTANT:
  1206. if (HAS_WARNING(ShaderWarning::UNUSED_CONSTANT_FLAG) && used_constants.has(p_identifier)) {
  1207. used_constants[p_identifier].used = true;
  1208. }
  1209. break;
  1210. case IdentifierType::IDENTIFIER_VARYING:
  1211. if (HAS_WARNING(ShaderWarning::UNUSED_VARYING_FLAG) && used_varyings.has(p_identifier)) {
  1212. if (shader->varyings[p_identifier].stage != ShaderNode::Varying::STAGE_VERTEX && shader->varyings[p_identifier].stage != ShaderNode::Varying::STAGE_FRAGMENT) {
  1213. used_varyings[p_identifier].used = true;
  1214. }
  1215. }
  1216. break;
  1217. case IdentifierType::IDENTIFIER_UNIFORM:
  1218. if (HAS_WARNING(ShaderWarning::UNUSED_UNIFORM_FLAG) && used_uniforms.has(p_identifier)) {
  1219. used_uniforms[p_identifier].used = true;
  1220. }
  1221. break;
  1222. case IdentifierType::IDENTIFIER_FUNCTION:
  1223. if (HAS_WARNING(ShaderWarning::UNUSED_FUNCTION_FLAG) && used_functions.has(p_identifier)) {
  1224. used_functions[p_identifier].used = true;
  1225. }
  1226. break;
  1227. case IdentifierType::IDENTIFIER_LOCAL_VAR:
  1228. if (HAS_WARNING(ShaderWarning::UNUSED_LOCAL_VARIABLE_FLAG) && used_local_vars.has(p_function) && used_local_vars[p_function].has(p_identifier)) {
  1229. used_local_vars[p_function][p_identifier].used = true;
  1230. }
  1231. break;
  1232. default:
  1233. break;
  1234. }
  1235. }
  1236. #endif // DEBUG_ENABLED
  1237. 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, Vector<Scalar> *r_constant_values) {
  1238. if (is_shader_inc) {
  1239. for (int i = 0; i < RenderingServer::SHADER_MAX; i++) {
  1240. for (const KeyValue<StringName, FunctionInfo> &E : ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(i))) {
  1241. if ((current_function == E.key || E.key == "global" || E.key == "constants") && E.value.built_ins.has(p_identifier)) {
  1242. if (r_data_type) {
  1243. *r_data_type = E.value.built_ins[p_identifier].type;
  1244. }
  1245. if (r_is_const) {
  1246. *r_is_const = E.value.built_ins[p_identifier].constant;
  1247. }
  1248. if (r_type) {
  1249. *r_type = IDENTIFIER_BUILTIN_VAR;
  1250. }
  1251. return true;
  1252. }
  1253. }
  1254. }
  1255. } else {
  1256. if (p_function_info.built_ins.has(p_identifier)) {
  1257. if (r_data_type) {
  1258. *r_data_type = p_function_info.built_ins[p_identifier].type;
  1259. }
  1260. if (r_is_const) {
  1261. *r_is_const = p_function_info.built_ins[p_identifier].constant;
  1262. }
  1263. if (r_type) {
  1264. *r_type = IDENTIFIER_BUILTIN_VAR;
  1265. }
  1266. return true;
  1267. }
  1268. }
  1269. if (p_function_info.stage_functions.has(p_identifier)) {
  1270. if (r_data_type) {
  1271. *r_data_type = p_function_info.stage_functions[p_identifier].return_type;
  1272. }
  1273. if (r_is_const) {
  1274. *r_is_const = true;
  1275. }
  1276. if (r_type) {
  1277. *r_type = IDENTIFIER_FUNCTION;
  1278. }
  1279. return true;
  1280. }
  1281. FunctionNode *function = nullptr;
  1282. while (p_block) {
  1283. if (p_block->variables.has(p_identifier)) {
  1284. if (r_data_type) {
  1285. *r_data_type = p_block->variables[p_identifier].type;
  1286. }
  1287. if (r_is_const) {
  1288. *r_is_const = p_block->variables[p_identifier].is_const;
  1289. }
  1290. if (r_array_size) {
  1291. *r_array_size = p_block->variables[p_identifier].array_size;
  1292. }
  1293. if (r_struct_name) {
  1294. *r_struct_name = p_block->variables[p_identifier].struct_name;
  1295. }
  1296. if (r_constant_values && !p_block->variables[p_identifier].values.is_empty()) {
  1297. *r_constant_values = p_block->variables[p_identifier].values;
  1298. }
  1299. if (r_type) {
  1300. *r_type = IDENTIFIER_LOCAL_VAR;
  1301. }
  1302. return true;
  1303. }
  1304. if (p_block->parent_function) {
  1305. function = p_block->parent_function;
  1306. break;
  1307. } else {
  1308. if (p_allow_reassign) {
  1309. break;
  1310. }
  1311. ERR_FAIL_NULL_V(p_block->parent_block, false);
  1312. p_block = p_block->parent_block;
  1313. }
  1314. }
  1315. if (function) {
  1316. for (int i = 0; i < function->arguments.size(); i++) {
  1317. if (function->arguments[i].name == p_identifier) {
  1318. if (r_data_type) {
  1319. *r_data_type = function->arguments[i].type;
  1320. }
  1321. if (r_struct_name) {
  1322. *r_struct_name = function->arguments[i].struct_name;
  1323. }
  1324. if (r_array_size) {
  1325. *r_array_size = function->arguments[i].array_size;
  1326. }
  1327. if (r_is_const) {
  1328. *r_is_const = function->arguments[i].is_const;
  1329. }
  1330. if (r_type) {
  1331. *r_type = IDENTIFIER_FUNCTION_ARGUMENT;
  1332. }
  1333. return true;
  1334. }
  1335. }
  1336. }
  1337. if (shader->varyings.has(p_identifier)) {
  1338. if (r_data_type) {
  1339. *r_data_type = shader->varyings[p_identifier].type;
  1340. }
  1341. if (r_array_size) {
  1342. *r_array_size = shader->varyings[p_identifier].array_size;
  1343. }
  1344. if (r_type) {
  1345. *r_type = IDENTIFIER_VARYING;
  1346. }
  1347. return true;
  1348. }
  1349. if (shader->uniforms.has(p_identifier)) {
  1350. if (r_data_type) {
  1351. *r_data_type = shader->uniforms[p_identifier].type;
  1352. }
  1353. if (r_array_size) {
  1354. *r_array_size = shader->uniforms[p_identifier].array_size;
  1355. }
  1356. if (r_type) {
  1357. *r_type = IDENTIFIER_UNIFORM;
  1358. }
  1359. return true;
  1360. }
  1361. if (shader->constants.has(p_identifier)) {
  1362. if (r_is_const) {
  1363. *r_is_const = true;
  1364. }
  1365. if (r_data_type) {
  1366. *r_data_type = shader->constants[p_identifier].type;
  1367. }
  1368. if (r_array_size) {
  1369. *r_array_size = shader->constants[p_identifier].array_size;
  1370. }
  1371. if (r_struct_name) {
  1372. *r_struct_name = shader->constants[p_identifier].struct_name;
  1373. }
  1374. if (r_constant_values) {
  1375. if (shader->constants[p_identifier].initializer && !shader->constants[p_identifier].initializer->get_values().is_empty()) {
  1376. *r_constant_values = shader->constants[p_identifier].initializer->get_values();
  1377. }
  1378. }
  1379. if (r_type) {
  1380. *r_type = IDENTIFIER_CONSTANT;
  1381. }
  1382. return true;
  1383. }
  1384. for (int i = 0; i < shader->vfunctions.size(); i++) {
  1385. if (!shader->vfunctions[i].callable) {
  1386. continue;
  1387. }
  1388. if (shader->vfunctions[i].name == p_identifier) {
  1389. if (r_data_type) {
  1390. *r_data_type = shader->vfunctions[i].function->return_type;
  1391. }
  1392. if (r_array_size) {
  1393. *r_array_size = shader->vfunctions[i].function->return_array_size;
  1394. }
  1395. if (r_type) {
  1396. *r_type = IDENTIFIER_FUNCTION;
  1397. }
  1398. return true;
  1399. }
  1400. }
  1401. return false;
  1402. }
  1403. bool ShaderLanguage::_validate_operator(const BlockNode *p_block, OperatorNode *p_op, DataType *r_ret_type, int *r_ret_size) {
  1404. bool valid = false;
  1405. DataType ret_type = TYPE_VOID;
  1406. int ret_size = 0;
  1407. switch (p_op->op) {
  1408. case OP_EQUAL:
  1409. case OP_NOT_EQUAL: {
  1410. 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)) {
  1411. break; // don't accept arrays
  1412. }
  1413. DataType na = p_op->arguments[0]->get_datatype();
  1414. DataType nb = p_op->arguments[1]->get_datatype();
  1415. valid = na == nb;
  1416. ret_type = TYPE_BOOL;
  1417. } break;
  1418. case OP_LESS:
  1419. case OP_LESS_EQUAL:
  1420. case OP_GREATER:
  1421. case OP_GREATER_EQUAL: {
  1422. 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)) {
  1423. break; // don't accept arrays
  1424. }
  1425. DataType na = p_op->arguments[0]->get_datatype();
  1426. DataType nb = p_op->arguments[1]->get_datatype();
  1427. valid = na == nb && (na == TYPE_UINT || na == TYPE_INT || na == TYPE_FLOAT);
  1428. ret_type = TYPE_BOOL;
  1429. } break;
  1430. case OP_AND:
  1431. case OP_OR: {
  1432. 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)) {
  1433. break; // don't accept arrays
  1434. }
  1435. DataType na = p_op->arguments[0]->get_datatype();
  1436. DataType nb = p_op->arguments[1]->get_datatype();
  1437. valid = na == nb && na == TYPE_BOOL;
  1438. ret_type = TYPE_BOOL;
  1439. } break;
  1440. case OP_NOT: {
  1441. if (!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) {
  1442. break; // don't accept arrays
  1443. }
  1444. DataType na = p_op->arguments[0]->get_datatype();
  1445. valid = na == TYPE_BOOL;
  1446. ret_type = TYPE_BOOL;
  1447. } break;
  1448. case OP_INCREMENT:
  1449. case OP_DECREMENT:
  1450. case OP_POST_INCREMENT:
  1451. case OP_POST_DECREMENT:
  1452. case OP_NEGATE: {
  1453. if (!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) {
  1454. break; // don't accept arrays
  1455. }
  1456. DataType na = p_op->arguments[0]->get_datatype();
  1457. valid = na > TYPE_BVEC4 && na < TYPE_MAT2;
  1458. ret_type = na;
  1459. } break;
  1460. case OP_ADD:
  1461. case OP_SUB:
  1462. case OP_MUL:
  1463. case OP_DIV: {
  1464. 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)) {
  1465. break; // don't accept arrays
  1466. }
  1467. DataType na = p_op->arguments[0]->get_datatype();
  1468. DataType nb = p_op->arguments[1]->get_datatype();
  1469. if (na > nb) {
  1470. //make things easier;
  1471. SWAP(na, nb);
  1472. }
  1473. if (na == nb) {
  1474. valid = (na > TYPE_BVEC4 && na <= TYPE_MAT4);
  1475. ret_type = na;
  1476. } else if (na == TYPE_INT && nb == TYPE_IVEC2) {
  1477. valid = true;
  1478. ret_type = TYPE_IVEC2;
  1479. } else if (na == TYPE_INT && nb == TYPE_IVEC3) {
  1480. valid = true;
  1481. ret_type = TYPE_IVEC3;
  1482. } else if (na == TYPE_INT && nb == TYPE_IVEC4) {
  1483. valid = true;
  1484. ret_type = TYPE_IVEC4;
  1485. } else if (na == TYPE_UINT && nb == TYPE_UVEC2) {
  1486. valid = true;
  1487. ret_type = TYPE_UVEC2;
  1488. } else if (na == TYPE_UINT && nb == TYPE_UVEC3) {
  1489. valid = true;
  1490. ret_type = TYPE_UVEC3;
  1491. } else if (na == TYPE_UINT && nb == TYPE_UVEC4) {
  1492. valid = true;
  1493. ret_type = TYPE_UVEC4;
  1494. } else if (na == TYPE_FLOAT && nb == TYPE_VEC2) {
  1495. valid = true;
  1496. ret_type = TYPE_VEC2;
  1497. } else if (na == TYPE_FLOAT && nb == TYPE_VEC3) {
  1498. valid = true;
  1499. ret_type = TYPE_VEC3;
  1500. } else if (na == TYPE_FLOAT && nb == TYPE_VEC4) {
  1501. valid = true;
  1502. ret_type = TYPE_VEC4;
  1503. } else if (na == TYPE_FLOAT && nb == TYPE_MAT2) {
  1504. valid = true;
  1505. ret_type = TYPE_MAT2;
  1506. } else if (na == TYPE_FLOAT && nb == TYPE_MAT3) {
  1507. valid = true;
  1508. ret_type = TYPE_MAT3;
  1509. } else if (na == TYPE_FLOAT && nb == TYPE_MAT4) {
  1510. valid = true;
  1511. ret_type = TYPE_MAT4;
  1512. } else if (p_op->op == OP_MUL && na == TYPE_VEC2 && nb == TYPE_MAT2) {
  1513. valid = true;
  1514. ret_type = TYPE_VEC2;
  1515. } else if (p_op->op == OP_MUL && na == TYPE_VEC3 && nb == TYPE_MAT3) {
  1516. valid = true;
  1517. ret_type = TYPE_VEC3;
  1518. } else if (p_op->op == OP_MUL && na == TYPE_VEC4 && nb == TYPE_MAT4) {
  1519. valid = true;
  1520. ret_type = TYPE_VEC4;
  1521. }
  1522. } break;
  1523. case OP_ASSIGN_MOD:
  1524. case OP_MOD: {
  1525. /*
  1526. * The operator modulus (%) operates on signed or unsigned integers or integer vectors. The operand
  1527. * types must both be signed or both be unsigned. The operands cannot be vectors of differing size. If
  1528. * one operand is a scalar and the other vector, then the scalar is applied component-wise to the vector,
  1529. * resulting in the same type as the vector. If both are vectors of the same size, the result is computed
  1530. * component-wise.
  1531. */
  1532. 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)) {
  1533. break; // don't accept arrays
  1534. }
  1535. DataType na = p_op->arguments[0]->get_datatype();
  1536. DataType nb = p_op->arguments[1]->get_datatype();
  1537. if (na == TYPE_INT && nb == TYPE_INT) {
  1538. valid = true;
  1539. ret_type = TYPE_INT;
  1540. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1541. valid = true;
  1542. ret_type = TYPE_IVEC2;
  1543. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1544. valid = true;
  1545. ret_type = TYPE_IVEC3;
  1546. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1547. valid = true;
  1548. ret_type = TYPE_IVEC4;
  1549. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  1550. valid = true;
  1551. ret_type = TYPE_IVEC2;
  1552. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  1553. valid = true;
  1554. ret_type = TYPE_IVEC3;
  1555. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  1556. valid = true;
  1557. ret_type = TYPE_IVEC4;
  1558. /////
  1559. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1560. valid = true;
  1561. ret_type = TYPE_UINT;
  1562. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1563. valid = true;
  1564. ret_type = TYPE_UVEC2;
  1565. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1566. valid = true;
  1567. ret_type = TYPE_UVEC3;
  1568. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1569. valid = true;
  1570. ret_type = TYPE_UVEC4;
  1571. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1572. valid = true;
  1573. ret_type = TYPE_UVEC2;
  1574. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1575. valid = true;
  1576. ret_type = TYPE_UVEC3;
  1577. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1578. valid = true;
  1579. ret_type = TYPE_UVEC4;
  1580. }
  1581. } break;
  1582. case OP_ASSIGN_SHIFT_LEFT:
  1583. case OP_ASSIGN_SHIFT_RIGHT:
  1584. case OP_SHIFT_LEFT:
  1585. case OP_SHIFT_RIGHT: {
  1586. 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)) {
  1587. break; // don't accept arrays
  1588. }
  1589. DataType na = p_op->arguments[0]->get_datatype();
  1590. DataType nb = p_op->arguments[1]->get_datatype();
  1591. if (na == TYPE_INT && nb == TYPE_INT) {
  1592. valid = true;
  1593. ret_type = TYPE_INT;
  1594. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1595. valid = true;
  1596. ret_type = TYPE_IVEC2;
  1597. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1598. valid = true;
  1599. ret_type = TYPE_IVEC3;
  1600. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1601. valid = true;
  1602. ret_type = TYPE_IVEC4;
  1603. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  1604. valid = true;
  1605. ret_type = TYPE_IVEC2;
  1606. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  1607. valid = true;
  1608. ret_type = TYPE_IVEC3;
  1609. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  1610. valid = true;
  1611. ret_type = TYPE_IVEC4;
  1612. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1613. valid = true;
  1614. ret_type = TYPE_UINT;
  1615. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1616. valid = true;
  1617. ret_type = TYPE_UVEC2;
  1618. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1619. valid = true;
  1620. ret_type = TYPE_UVEC3;
  1621. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1622. valid = true;
  1623. ret_type = TYPE_UVEC4;
  1624. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1625. valid = true;
  1626. ret_type = TYPE_UVEC2;
  1627. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1628. valid = true;
  1629. ret_type = TYPE_UVEC3;
  1630. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1631. valid = true;
  1632. ret_type = TYPE_UVEC4;
  1633. }
  1634. } break;
  1635. case OP_ASSIGN: {
  1636. int sa = 0;
  1637. int sb = 0;
  1638. if (!p_op->arguments[0]->is_indexed()) {
  1639. sa = p_op->arguments[0]->get_array_size();
  1640. }
  1641. if (!p_op->arguments[1]->is_indexed()) {
  1642. sb = p_op->arguments[1]->get_array_size();
  1643. }
  1644. if (sa != sb) {
  1645. break; // don't accept arrays if their sizes are not equal
  1646. }
  1647. DataType na = p_op->arguments[0]->get_datatype();
  1648. DataType nb = p_op->arguments[1]->get_datatype();
  1649. if (na == TYPE_STRUCT || nb == TYPE_STRUCT) {
  1650. valid = p_op->arguments[0]->get_datatype_name() == p_op->arguments[1]->get_datatype_name();
  1651. } else {
  1652. valid = na == nb;
  1653. }
  1654. ret_type = na;
  1655. ret_size = sa;
  1656. } break;
  1657. case OP_ASSIGN_ADD:
  1658. case OP_ASSIGN_SUB:
  1659. case OP_ASSIGN_MUL:
  1660. case OP_ASSIGN_DIV: {
  1661. int sa = 0;
  1662. int sb = 0;
  1663. if (!p_op->arguments[0]->is_indexed()) {
  1664. sa = p_op->arguments[0]->get_array_size();
  1665. }
  1666. if (!p_op->arguments[1]->is_indexed()) {
  1667. sb = p_op->arguments[1]->get_array_size();
  1668. }
  1669. if (sa > 0 || sb > 0) {
  1670. break; // don't accept arrays
  1671. }
  1672. DataType na = p_op->arguments[0]->get_datatype();
  1673. DataType nb = p_op->arguments[1]->get_datatype();
  1674. if (na == nb) {
  1675. valid = (na > TYPE_BVEC4 && na <= TYPE_MAT4);
  1676. ret_type = na;
  1677. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1678. valid = true;
  1679. ret_type = TYPE_IVEC2;
  1680. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1681. valid = true;
  1682. ret_type = TYPE_IVEC3;
  1683. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1684. valid = true;
  1685. ret_type = TYPE_IVEC4;
  1686. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1687. valid = true;
  1688. ret_type = TYPE_UVEC2;
  1689. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1690. valid = true;
  1691. ret_type = TYPE_UVEC3;
  1692. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1693. valid = true;
  1694. ret_type = TYPE_UVEC4;
  1695. } else if (na == TYPE_VEC2 && nb == TYPE_FLOAT) {
  1696. valid = true;
  1697. ret_type = TYPE_VEC2;
  1698. } else if (na == TYPE_VEC3 && nb == TYPE_FLOAT) {
  1699. valid = true;
  1700. ret_type = TYPE_VEC3;
  1701. } else if (na == TYPE_VEC4 && nb == TYPE_FLOAT) {
  1702. valid = true;
  1703. ret_type = TYPE_VEC4;
  1704. } else if (na == TYPE_MAT2 && nb == TYPE_FLOAT) {
  1705. valid = true;
  1706. ret_type = TYPE_MAT2;
  1707. } else if (na == TYPE_MAT3 && nb == TYPE_FLOAT) {
  1708. valid = true;
  1709. ret_type = TYPE_MAT3;
  1710. } else if (na == TYPE_MAT4 && nb == TYPE_FLOAT) {
  1711. valid = true;
  1712. ret_type = TYPE_MAT4;
  1713. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC2 && nb == TYPE_MAT2) {
  1714. valid = true;
  1715. ret_type = TYPE_VEC2;
  1716. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC3 && nb == TYPE_MAT3) {
  1717. valid = true;
  1718. ret_type = TYPE_VEC3;
  1719. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC4 && nb == TYPE_MAT4) {
  1720. valid = true;
  1721. ret_type = TYPE_VEC4;
  1722. }
  1723. } break;
  1724. case OP_ASSIGN_BIT_AND:
  1725. case OP_ASSIGN_BIT_OR:
  1726. case OP_ASSIGN_BIT_XOR:
  1727. case OP_BIT_AND:
  1728. case OP_BIT_OR:
  1729. case OP_BIT_XOR: {
  1730. /*
  1731. * The bitwise operators and (&), exclusive-or (^), and inclusive-or (|). The operands must be of type
  1732. * signed or unsigned integers or integer vectors. The operands cannot be vectors of differing size. If
  1733. * one operand is a scalar and the other a vector, the scalar is applied component-wise to the vector,
  1734. * resulting in the same type as the vector. The fundamental types of the operands (signed or unsigned)
  1735. * must match.
  1736. */
  1737. int sa = 0;
  1738. int sb = 0;
  1739. if (!p_op->arguments[0]->is_indexed()) {
  1740. sa = p_op->arguments[0]->get_array_size();
  1741. }
  1742. if (!p_op->arguments[1]->is_indexed()) {
  1743. sb = p_op->arguments[1]->get_array_size();
  1744. }
  1745. if (sa > 0 || sb > 0) {
  1746. break; // don't accept arrays
  1747. }
  1748. DataType na = p_op->arguments[0]->get_datatype();
  1749. DataType nb = p_op->arguments[1]->get_datatype();
  1750. if (na > nb && p_op->op >= OP_BIT_AND) {
  1751. //can swap for non assign
  1752. SWAP(na, nb);
  1753. }
  1754. if (na == TYPE_INT && nb == TYPE_INT) {
  1755. valid = true;
  1756. ret_type = TYPE_INT;
  1757. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1758. valid = true;
  1759. ret_type = TYPE_IVEC2;
  1760. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1761. valid = true;
  1762. ret_type = TYPE_IVEC3;
  1763. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1764. valid = true;
  1765. ret_type = TYPE_IVEC4;
  1766. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  1767. valid = true;
  1768. ret_type = TYPE_IVEC2;
  1769. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  1770. valid = true;
  1771. ret_type = TYPE_IVEC3;
  1772. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  1773. valid = true;
  1774. ret_type = TYPE_IVEC4;
  1775. /////
  1776. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1777. valid = true;
  1778. ret_type = TYPE_UINT;
  1779. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1780. valid = true;
  1781. ret_type = TYPE_UVEC2;
  1782. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1783. valid = true;
  1784. ret_type = TYPE_UVEC3;
  1785. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1786. valid = true;
  1787. ret_type = TYPE_UVEC4;
  1788. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1789. valid = true;
  1790. ret_type = TYPE_UVEC2;
  1791. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1792. valid = true;
  1793. ret_type = TYPE_UVEC3;
  1794. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1795. valid = true;
  1796. ret_type = TYPE_UVEC4;
  1797. }
  1798. } break;
  1799. case OP_BIT_INVERT: { //unaries
  1800. if (!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) {
  1801. break; // don't accept arrays
  1802. }
  1803. DataType na = p_op->arguments[0]->get_datatype();
  1804. valid = na >= TYPE_INT && na < TYPE_FLOAT;
  1805. ret_type = na;
  1806. } break;
  1807. case OP_SELECT_IF: {
  1808. int sa = 0;
  1809. int sb = 0;
  1810. if (!p_op->arguments[1]->is_indexed()) {
  1811. sa = p_op->arguments[1]->get_array_size();
  1812. }
  1813. if (!p_op->arguments[2]->is_indexed()) {
  1814. sb = p_op->arguments[2]->get_array_size();
  1815. }
  1816. if (sa != sb) {
  1817. break; // don't accept arrays if their sizes are not equal
  1818. }
  1819. DataType na = p_op->arguments[0]->get_datatype();
  1820. DataType nb = p_op->arguments[1]->get_datatype();
  1821. DataType nc = p_op->arguments[2]->get_datatype();
  1822. valid = na == TYPE_BOOL && (nb == nc);
  1823. ret_type = nb;
  1824. ret_size = sa;
  1825. } break;
  1826. default: {
  1827. ERR_FAIL_V(false);
  1828. }
  1829. }
  1830. if (r_ret_type) {
  1831. *r_ret_type = ret_type;
  1832. }
  1833. if (r_ret_size) {
  1834. *r_ret_size = ret_size;
  1835. }
  1836. if (valid && (!p_block || p_block->use_op_eval)) {
  1837. // Need to be placed here and not in the `_reduce_expression` because otherwise expressions like `1 + 2 / 2` will not work correctly.
  1838. valid = _eval_operator(p_block, p_op);
  1839. }
  1840. return valid;
  1841. }
  1842. Vector<ShaderLanguage::Scalar> ShaderLanguage::_get_node_values(const BlockNode *p_block, Node *p_node) {
  1843. Vector<Scalar> result;
  1844. switch (p_node->type) {
  1845. case Node::NODE_TYPE_VARIABLE: {
  1846. _find_identifier(p_block, false, FunctionInfo(), static_cast<VariableNode *>(p_node)->name, nullptr, nullptr, nullptr, nullptr, nullptr, &result);
  1847. } break;
  1848. default: {
  1849. result = p_node->get_values();
  1850. } break;
  1851. }
  1852. return result;
  1853. }
  1854. bool ShaderLanguage::_eval_operator(const BlockNode *p_block, OperatorNode *p_op) {
  1855. bool is_valid = true;
  1856. switch (p_op->op) {
  1857. case OP_EQUAL:
  1858. case OP_NOT_EQUAL:
  1859. case OP_LESS:
  1860. case OP_LESS_EQUAL:
  1861. case OP_GREATER:
  1862. case OP_GREATER_EQUAL:
  1863. case OP_AND:
  1864. case OP_OR:
  1865. case OP_ADD:
  1866. case OP_SUB:
  1867. case OP_MUL:
  1868. case OP_DIV:
  1869. case OP_MOD:
  1870. case OP_SHIFT_LEFT:
  1871. case OP_SHIFT_RIGHT:
  1872. case OP_BIT_AND:
  1873. case OP_BIT_OR:
  1874. case OP_BIT_XOR: {
  1875. DataType a = p_op->arguments[0]->get_datatype();
  1876. DataType b = p_op->arguments[1]->get_datatype();
  1877. bool is_op_vec_transform = false;
  1878. if (p_op->op == OP_MUL) {
  1879. DataType ta = a;
  1880. DataType tb = b;
  1881. if (ta > tb) {
  1882. SWAP(ta, tb);
  1883. }
  1884. if (ta == TYPE_VEC2 && tb == TYPE_MAT2) {
  1885. is_op_vec_transform = true;
  1886. } else if (ta == TYPE_VEC3 && tb == TYPE_MAT3) {
  1887. is_op_vec_transform = true;
  1888. } else if (ta == TYPE_VEC4 && tb == TYPE_MAT4) {
  1889. is_op_vec_transform = true;
  1890. }
  1891. }
  1892. Vector<Scalar> va = _get_node_values(p_block, p_op->arguments[0]);
  1893. Vector<Scalar> vb = _get_node_values(p_block, p_op->arguments[1]);
  1894. if (is_op_vec_transform) {
  1895. p_op->values = _eval_vector_transform(va, vb, a, b, p_op->get_datatype());
  1896. } else {
  1897. p_op->values = _eval_vector(va, vb, a, b, p_op->get_datatype(), p_op->op, is_valid);
  1898. }
  1899. } break;
  1900. case OP_NOT:
  1901. case OP_NEGATE:
  1902. case OP_BIT_INVERT: {
  1903. p_op->values = _eval_unary_vector(_get_node_values(p_block, p_op->arguments[0]), p_op->get_datatype(), p_op->op);
  1904. } break;
  1905. default: {
  1906. } break;
  1907. }
  1908. return is_valid;
  1909. }
  1910. ShaderLanguage::Scalar ShaderLanguage::_eval_unary_scalar(const Scalar &p_a, Operator p_op, DataType p_ret_type) {
  1911. Scalar scalar;
  1912. switch (p_op) {
  1913. case OP_NOT: {
  1914. scalar.boolean = !p_a.boolean;
  1915. } break;
  1916. case OP_NEGATE: {
  1917. if (p_ret_type >= TYPE_INT && p_ret_type <= TYPE_IVEC4) {
  1918. scalar.sint = -p_a.sint;
  1919. } else if (p_ret_type >= TYPE_UINT && p_ret_type <= TYPE_UVEC4) {
  1920. // Intentionally wrap the unsigned int value, because GLSL does.
  1921. scalar.uint = 0 - p_a.uint;
  1922. } else { // float types
  1923. scalar.real = -scalar.real;
  1924. }
  1925. } break;
  1926. case OP_BIT_INVERT: {
  1927. if (p_ret_type >= TYPE_INT && p_ret_type <= TYPE_IVEC4) {
  1928. scalar.sint = ~p_a.sint;
  1929. } else { // uint types
  1930. scalar.uint = ~p_a.uint;
  1931. }
  1932. } break;
  1933. default: {
  1934. } break;
  1935. }
  1936. return scalar;
  1937. }
  1938. ShaderLanguage::Scalar ShaderLanguage::_eval_scalar(const Scalar &p_a, const Scalar &p_b, Operator p_op, DataType p_ret_type, bool &r_is_valid) {
  1939. Scalar scalar;
  1940. switch (p_op) {
  1941. case OP_EQUAL: {
  1942. scalar.boolean = p_a.boolean == p_b.boolean;
  1943. } break;
  1944. case OP_NOT_EQUAL: {
  1945. scalar.boolean = p_a.boolean != p_b.boolean;
  1946. } break;
  1947. case OP_LESS: {
  1948. if (p_ret_type == TYPE_INT) {
  1949. scalar.boolean = p_a.sint < p_b.sint;
  1950. } else if (p_ret_type == TYPE_UINT) {
  1951. scalar.boolean = p_a.uint < p_b.uint;
  1952. } else { // float type
  1953. scalar.boolean = p_a.real < p_b.real;
  1954. }
  1955. } break;
  1956. case OP_LESS_EQUAL: {
  1957. if (p_ret_type == TYPE_INT) {
  1958. scalar.boolean = p_a.sint <= p_b.sint;
  1959. } else if (p_ret_type == TYPE_UINT) {
  1960. scalar.boolean = p_a.uint <= p_b.uint;
  1961. } else { // float type
  1962. scalar.boolean = p_a.real <= p_b.real;
  1963. }
  1964. } break;
  1965. case OP_GREATER: {
  1966. if (p_ret_type == TYPE_INT) {
  1967. scalar.boolean = p_a.sint > p_b.sint;
  1968. } else if (p_ret_type == TYPE_UINT) {
  1969. scalar.boolean = p_a.uint > p_b.uint;
  1970. } else { // float type
  1971. scalar.boolean = p_a.real > p_b.real;
  1972. }
  1973. } break;
  1974. case OP_GREATER_EQUAL: {
  1975. if (p_ret_type == TYPE_INT) {
  1976. scalar.boolean = p_a.sint >= p_b.sint;
  1977. } else if (p_ret_type == TYPE_UINT) {
  1978. scalar.boolean = p_a.uint >= p_b.uint;
  1979. } else { // float type
  1980. scalar.boolean = p_a.real >= p_b.real;
  1981. }
  1982. } break;
  1983. case OP_AND: {
  1984. scalar.boolean = p_a.boolean && p_b.boolean;
  1985. } break;
  1986. case OP_OR: {
  1987. scalar.boolean = p_a.boolean || p_b.boolean;
  1988. } break;
  1989. case OP_ADD: {
  1990. if (p_ret_type >= TYPE_INT && p_ret_type <= TYPE_IVEC4) {
  1991. scalar.sint = p_a.sint + p_b.sint;
  1992. } else if (p_ret_type >= TYPE_UINT && p_ret_type <= TYPE_UVEC4) {
  1993. scalar.uint = p_a.uint + p_b.uint;
  1994. } else { // float + matrix types
  1995. scalar.real = p_a.real + p_b.real;
  1996. }
  1997. } break;
  1998. case OP_SUB: {
  1999. if (p_ret_type >= TYPE_INT && p_ret_type <= TYPE_IVEC4) {
  2000. scalar.sint = p_a.sint - p_b.sint;
  2001. } else if (p_ret_type >= TYPE_UINT && p_ret_type <= TYPE_UVEC4) {
  2002. scalar.uint = p_a.uint - p_b.uint;
  2003. } else { // float + matrix types
  2004. scalar.real = p_a.real - p_b.real;
  2005. }
  2006. } break;
  2007. case OP_MUL: {
  2008. if (p_ret_type >= TYPE_INT && p_ret_type <= TYPE_IVEC4) {
  2009. scalar.sint = p_a.sint * p_b.sint;
  2010. } else if (p_ret_type >= TYPE_UINT && p_ret_type <= TYPE_UVEC4) {
  2011. scalar.uint = p_a.uint * p_b.uint;
  2012. } else { // float + matrix types
  2013. scalar.real = p_a.real * p_b.real;
  2014. }
  2015. } break;
  2016. case OP_DIV: {
  2017. if (p_ret_type >= TYPE_INT && p_ret_type <= TYPE_IVEC4) {
  2018. if (p_b.sint == 0) {
  2019. _set_error(RTR("Division by zero error."));
  2020. r_is_valid = false;
  2021. break;
  2022. }
  2023. scalar.sint = p_a.sint / p_b.sint;
  2024. } else if (p_ret_type == TYPE_UINT && p_ret_type <= TYPE_UVEC4) {
  2025. if (p_b.uint == 0U) {
  2026. _set_error(RTR("Division by zero error."));
  2027. r_is_valid = false;
  2028. break;
  2029. }
  2030. scalar.uint = p_a.uint / p_b.uint;
  2031. } else { // float + matrix types
  2032. scalar.real = p_a.real / p_b.real;
  2033. }
  2034. } break;
  2035. case OP_MOD: {
  2036. if (p_ret_type >= TYPE_INT && p_ret_type <= TYPE_IVEC4) {
  2037. if (p_b.sint == 0) {
  2038. _set_error(RTR("Modulo by zero error."));
  2039. r_is_valid = false;
  2040. break;
  2041. }
  2042. scalar.sint = p_a.sint % p_b.sint;
  2043. } else { // uint types
  2044. if (p_b.uint == 0U) {
  2045. _set_error(RTR("Modulo by zero error."));
  2046. r_is_valid = false;
  2047. break;
  2048. }
  2049. scalar.uint = p_a.uint % p_b.uint;
  2050. }
  2051. } break;
  2052. case OP_SHIFT_LEFT: {
  2053. if (p_ret_type >= TYPE_INT && p_ret_type <= TYPE_IVEC4) {
  2054. scalar.sint = p_a.sint << p_b.sint;
  2055. } else { // uint types
  2056. scalar.uint = p_a.uint << p_b.uint;
  2057. }
  2058. } break;
  2059. case OP_SHIFT_RIGHT: {
  2060. if (p_ret_type >= TYPE_INT && p_ret_type <= TYPE_IVEC4) {
  2061. scalar.sint = p_a.sint >> p_b.sint;
  2062. } else { // uint types
  2063. scalar.uint = p_a.uint >> p_b.uint;
  2064. }
  2065. } break;
  2066. case OP_BIT_AND: {
  2067. if (p_ret_type >= TYPE_INT && p_ret_type <= TYPE_IVEC4) {
  2068. scalar.sint = p_a.sint & p_b.sint;
  2069. } else { // uint types
  2070. scalar.uint = p_a.uint & p_b.uint;
  2071. }
  2072. } break;
  2073. case OP_BIT_OR: {
  2074. if (p_ret_type >= TYPE_INT && p_ret_type <= TYPE_IVEC4) {
  2075. scalar.sint = p_a.sint | p_b.sint;
  2076. } else { // uint types
  2077. scalar.uint = p_a.uint | p_b.uint;
  2078. }
  2079. } break;
  2080. case OP_BIT_XOR: {
  2081. if (p_ret_type >= TYPE_INT && p_ret_type <= TYPE_IVEC4) {
  2082. scalar.sint = p_a.sint ^ p_b.sint;
  2083. } else { // uint types
  2084. scalar.uint = p_a.uint ^ p_b.uint;
  2085. }
  2086. } break;
  2087. default: {
  2088. } break;
  2089. }
  2090. return scalar;
  2091. }
  2092. Vector<ShaderLanguage::Scalar> ShaderLanguage::_eval_unary_vector(const Vector<Scalar> &p_va, DataType p_ret_type, Operator p_op) {
  2093. uint32_t size = get_datatype_component_count(p_ret_type);
  2094. if (p_va.size() != p_ret_type) {
  2095. return Vector<Scalar>(); // Non-evaluable values should not be parsed further.
  2096. }
  2097. Vector<Scalar> value;
  2098. value.resize(size);
  2099. Scalar *w = value.ptrw();
  2100. for (uint32_t i = 0U; i < size; i++) {
  2101. w[i] = _eval_unary_scalar(p_va[i], p_op, p_ret_type);
  2102. }
  2103. return value;
  2104. }
  2105. Vector<ShaderLanguage::Scalar> ShaderLanguage::_eval_vector(const Vector<Scalar> &p_va, const Vector<Scalar> &p_vb, DataType p_left_type, DataType p_right_type, DataType p_ret_type, Operator p_op, bool &r_is_valid) {
  2106. uint32_t left_size = get_datatype_component_count(p_left_type);
  2107. uint32_t right_size = get_datatype_component_count(p_right_type);
  2108. if (p_va.size() != left_size || p_vb.size() != right_size) {
  2109. return Vector<Scalar>(); // Non-evaluable values should not be parsed further.
  2110. }
  2111. uint32_t ret_size = get_datatype_component_count(p_ret_type);
  2112. Vector<Scalar> value;
  2113. value.resize(ret_size);
  2114. Scalar *w = value.ptrw();
  2115. for (uint32_t i = 0U; i < ret_size; i++) {
  2116. w[i] = _eval_scalar(p_va[MIN(i, left_size - 1)], p_vb[MIN(i, right_size - 1)], p_op, p_ret_type, r_is_valid);
  2117. if (!r_is_valid) {
  2118. return value;
  2119. }
  2120. }
  2121. return value;
  2122. }
  2123. Vector<ShaderLanguage::Scalar> ShaderLanguage::_eval_vector_transform(const Vector<Scalar> &p_va, const Vector<Scalar> &p_vb, DataType p_left_type, DataType p_right_type, DataType p_ret_type) {
  2124. uint32_t left_size = get_datatype_component_count(p_left_type);
  2125. uint32_t right_size = get_datatype_component_count(p_right_type);
  2126. if (p_va.size() != left_size || p_vb.size() != right_size) {
  2127. return Vector<Scalar>(); // Non-evaluable values should not be parsed further.
  2128. }
  2129. uint32_t ret_size = get_datatype_component_count(p_ret_type);
  2130. Vector<Scalar> value;
  2131. value.resize_zeroed(ret_size);
  2132. Scalar *w = value.ptrw();
  2133. switch (p_ret_type) {
  2134. case TYPE_VEC2: {
  2135. if (left_size == 2) { // v * m
  2136. Vector2 v = Vector2(p_va[0].real, p_va[1].real);
  2137. w[0].real = (p_vb[0].real * v.x + p_vb[1].real * v.y);
  2138. w[1].real = (p_vb[2].real * v.x + p_vb[3].real * v.y);
  2139. } else { // m * v
  2140. Vector2 v = Vector2(p_vb[0].real, p_vb[1].real);
  2141. w[0].real = (p_va[0].real * v.x + p_va[2].real * v.y);
  2142. w[1].real = (p_va[1].real * v.x + p_va[3].real * v.y);
  2143. }
  2144. } break;
  2145. case TYPE_VEC3: {
  2146. if (left_size == 3) { // v * m
  2147. Vector3 v = Vector3(p_va[0].real, p_va[1].real, p_va[2].real);
  2148. w[0].real = (p_vb[0].real * v.x + p_vb[1].real * v.y + p_vb[2].real * v.z);
  2149. w[1].real = (p_vb[3].real * v.x + p_vb[4].real * v.y + p_vb[5].real * v.z);
  2150. w[2].real = (p_vb[6].real * v.x + p_vb[7].real * v.y + p_vb[8].real * v.z);
  2151. } else { // m * v
  2152. Vector3 v = Vector3(p_vb[0].real, p_vb[1].real, p_vb[2].real);
  2153. w[0].real = (p_va[0].real * v.x + p_va[3].real * v.y + p_va[6].real * v.z);
  2154. w[1].real = (p_va[1].real * v.x + p_va[4].real * v.y + p_va[7].real * v.z);
  2155. w[2].real = (p_va[2].real * v.x + p_va[5].real * v.y + p_va[8].real * v.z);
  2156. }
  2157. } break;
  2158. case TYPE_VEC4: {
  2159. if (left_size == 4) { // v * m
  2160. Vector4 v = Vector4(p_va[0].real, p_va[1].real, p_va[2].real, p_va[3].real);
  2161. w[0].real = (p_vb[0].real * v.x + p_vb[1].real * v.y + p_vb[2].real * v.z + p_vb[3].real * v.w);
  2162. w[1].real = (p_vb[4].real * v.x + p_vb[5].real * v.y + p_vb[6].real * v.z + p_vb[7].real * v.w);
  2163. w[2].real = (p_vb[8].real * v.x + p_vb[9].real * v.y + p_vb[10].real * v.z + p_vb[11].real * v.w);
  2164. w[3].real = (p_vb[12].real * v.x + p_vb[13].real * v.y + p_vb[14].real * v.z + p_vb[15].real * v.w);
  2165. } else { // m * v
  2166. Vector4 v = Vector4(p_vb[0].real, p_vb[1].real, p_vb[2].real, p_vb[3].real);
  2167. w[0].real = (p_vb[0].real * v.x + p_vb[4].real * v.y + p_vb[8].real * v.z + p_vb[12].real * v.w);
  2168. w[1].real = (p_vb[1].real * v.x + p_vb[5].real * v.y + p_vb[9].real * v.z + p_vb[13].real * v.w);
  2169. w[2].real = (p_vb[2].real * v.x + p_vb[6].real * v.y + p_vb[10].real * v.z + p_vb[14].real * v.w);
  2170. w[3].real = (p_vb[3].real * v.x + p_vb[7].real * v.y + p_vb[11].real * v.z + p_vb[15].real * v.w);
  2171. }
  2172. } break;
  2173. default: {
  2174. } break;
  2175. }
  2176. return value;
  2177. }
  2178. const ShaderLanguage::BuiltinFuncDef ShaderLanguage::builtin_func_defs[] = {
  2179. // Constructors.
  2180. { "bool", TYPE_BOOL, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2181. { "bvec2", TYPE_BVEC2, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2182. { "bvec2", TYPE_BVEC2, { TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2183. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2184. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2185. { "bvec3", TYPE_BVEC3, { TYPE_BVEC2, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2186. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2187. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2188. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2189. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BVEC2, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2190. { "bvec4", TYPE_BVEC4, { TYPE_BVEC2, TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2191. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BOOL, TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2192. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2193. { "bvec4", TYPE_BVEC4, { TYPE_BVEC3, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2194. { "bvec4", TYPE_BVEC4, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2195. { "float", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2196. { "vec2", TYPE_VEC2, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2197. { "vec2", TYPE_VEC2, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2198. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2199. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2200. { "vec3", TYPE_VEC3, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2201. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2202. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2203. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2204. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2205. { "vec4", TYPE_VEC4, { TYPE_VEC2, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2206. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2207. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2208. { "vec4", TYPE_VEC4, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2209. { "vec4", TYPE_VEC4, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2210. { "int", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2211. { "ivec2", TYPE_IVEC2, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2212. { "ivec2", TYPE_IVEC2, { TYPE_INT, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2213. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2214. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2215. { "ivec3", TYPE_IVEC3, { TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2216. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2217. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2218. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2219. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2220. { "ivec4", TYPE_IVEC4, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2221. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_INT, TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2222. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2223. { "ivec4", TYPE_IVEC4, { TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2224. { "ivec4", TYPE_IVEC4, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2225. { "uint", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2226. { "uvec2", TYPE_UVEC2, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2227. { "uvec2", TYPE_UVEC2, { TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2228. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2229. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2230. { "uvec3", TYPE_UVEC3, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2231. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2232. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2233. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2234. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2235. { "uvec4", TYPE_UVEC4, { TYPE_UVEC2, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2236. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UINT, TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2237. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2238. { "uvec4", TYPE_UVEC4, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2239. { "uvec4", TYPE_UVEC4, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2240. { "mat2", TYPE_MAT2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2241. { "mat3", TYPE_MAT3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2242. { "mat4", TYPE_MAT4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2243. { "mat2", TYPE_MAT2, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2244. { "mat3", TYPE_MAT3, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2245. { "mat4", TYPE_MAT4, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2246. // Conversion scalars.
  2247. { "int", TYPE_INT, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2248. { "int", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2249. { "int", TYPE_INT, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2250. { "int", TYPE_INT, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2251. { "float", TYPE_FLOAT, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2252. { "float", TYPE_FLOAT, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2253. { "float", TYPE_FLOAT, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2254. { "float", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2255. { "uint", TYPE_UINT, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2256. { "uint", TYPE_UINT, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2257. { "uint", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2258. { "uint", TYPE_UINT, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2259. { "bool", TYPE_BOOL, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2260. { "bool", TYPE_BOOL, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2261. { "bool", TYPE_BOOL, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2262. { "bool", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2263. // Conversion vectors.
  2264. { "ivec2", TYPE_IVEC2, { TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2265. { "ivec2", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2266. { "ivec2", TYPE_IVEC2, { TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2267. { "ivec2", TYPE_IVEC2, { TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2268. { "vec2", TYPE_VEC2, { TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2269. { "vec2", TYPE_VEC2, { TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2270. { "vec2", TYPE_VEC2, { TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2271. { "vec2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2272. { "uvec2", TYPE_UVEC2, { TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2273. { "uvec2", TYPE_UVEC2, { TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2274. { "uvec2", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2275. { "uvec2", TYPE_UVEC2, { TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2276. { "bvec2", TYPE_BVEC2, { TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2277. { "bvec2", TYPE_BVEC2, { TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2278. { "bvec2", TYPE_BVEC2, { TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2279. { "bvec2", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2280. { "ivec3", TYPE_IVEC3, { TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2281. { "ivec3", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2282. { "ivec3", TYPE_IVEC3, { TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2283. { "ivec3", TYPE_IVEC3, { TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2284. { "vec3", TYPE_VEC3, { TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2285. { "vec3", TYPE_VEC3, { TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2286. { "vec3", TYPE_VEC3, { TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2287. { "vec3", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2288. { "uvec3", TYPE_UVEC3, { TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2289. { "uvec3", TYPE_UVEC3, { TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2290. { "uvec3", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2291. { "uvec3", TYPE_UVEC3, { TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2292. { "bvec3", TYPE_BVEC3, { TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2293. { "bvec3", TYPE_BVEC3, { TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2294. { "bvec3", TYPE_BVEC3, { TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2295. { "bvec3", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2296. { "ivec4", TYPE_IVEC4, { TYPE_BVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2297. { "ivec4", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2298. { "ivec4", TYPE_IVEC4, { TYPE_UVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2299. { "ivec4", TYPE_IVEC4, { TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2300. { "vec4", TYPE_VEC4, { TYPE_BVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2301. { "vec4", TYPE_VEC4, { TYPE_IVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2302. { "vec4", TYPE_VEC4, { TYPE_UVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2303. { "vec4", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2304. { "uvec4", TYPE_UVEC4, { TYPE_BVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2305. { "uvec4", TYPE_UVEC4, { TYPE_IVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2306. { "uvec4", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2307. { "uvec4", TYPE_UVEC4, { TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2308. { "bvec4", TYPE_BVEC4, { TYPE_BVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2309. { "bvec4", TYPE_BVEC4, { TYPE_IVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2310. { "bvec4", TYPE_BVEC4, { TYPE_UVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2311. { "bvec4", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2312. // Conversion between matrixes.
  2313. { "mat2", TYPE_MAT2, { TYPE_MAT3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2314. { "mat2", TYPE_MAT2, { TYPE_MAT4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2315. { "mat3", TYPE_MAT3, { TYPE_MAT2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2316. { "mat3", TYPE_MAT3, { TYPE_MAT4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2317. { "mat4", TYPE_MAT4, { TYPE_MAT2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2318. { "mat4", TYPE_MAT4, { TYPE_MAT3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  2319. // Built-ins - trigonometric functions.
  2320. // radians
  2321. { "radians", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "degrees" }, TAG_GLOBAL, false },
  2322. { "radians", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "degrees" }, TAG_GLOBAL, false },
  2323. { "radians", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "degrees" }, TAG_GLOBAL, false },
  2324. { "radians", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "degrees" }, TAG_GLOBAL, false },
  2325. // degrees
  2326. { "degrees", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "radians" }, TAG_GLOBAL, false },
  2327. { "degrees", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "radians" }, TAG_GLOBAL, false },
  2328. { "degrees", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "radians" }, TAG_GLOBAL, false },
  2329. { "degrees", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "radians" }, TAG_GLOBAL, false },
  2330. // sin
  2331. { "sin", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  2332. { "sin", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  2333. { "sin", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  2334. { "sin", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  2335. // cos
  2336. { "cos", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  2337. { "cos", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  2338. { "cos", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  2339. { "cos", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  2340. // tan
  2341. { "tan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  2342. { "tan", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  2343. { "tan", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  2344. { "tan", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  2345. // asin
  2346. { "asin", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2347. { "asin", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2348. { "asin", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2349. { "asin", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2350. // acos
  2351. { "acos", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2352. { "acos", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2353. { "acos", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2354. { "acos", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2355. // atan
  2356. { "atan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "y_over_x" }, TAG_GLOBAL, false },
  2357. { "atan", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "y_over_x" }, TAG_GLOBAL, false },
  2358. { "atan", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "y_over_x" }, TAG_GLOBAL, false },
  2359. { "atan", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "y_over_x" }, TAG_GLOBAL, false },
  2360. { "atan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "y", "x" }, TAG_GLOBAL, false },
  2361. { "atan", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "y", "x" }, TAG_GLOBAL, false },
  2362. { "atan", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "y", "x" }, TAG_GLOBAL, false },
  2363. { "atan", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "y", "x" }, TAG_GLOBAL, false },
  2364. // sinh
  2365. { "sinh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2366. { "sinh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2367. { "sinh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2368. { "sinh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2369. // cosh
  2370. { "cosh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2371. { "cosh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2372. { "cosh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2373. { "cosh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2374. // tanh
  2375. { "tanh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2376. { "tanh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2377. { "tanh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2378. { "tanh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2379. // asinh
  2380. { "asinh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2381. { "asinh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2382. { "asinh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2383. { "asinh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2384. // acosh
  2385. { "acosh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2386. { "acosh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2387. { "acosh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2388. { "acosh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2389. // atanh
  2390. { "atanh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2391. { "atanh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2392. { "atanh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2393. { "atanh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2394. // Builtins - exponential functions.
  2395. // pow
  2396. { "pow", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2397. { "pow", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2398. { "pow", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2399. { "pow", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2400. // exp
  2401. { "exp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2402. { "exp", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2403. { "exp", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2404. { "exp", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2405. // log
  2406. { "log", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2407. { "log", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2408. { "log", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2409. { "log", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2410. // exp2
  2411. { "exp2", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2412. { "exp2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2413. { "exp2", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2414. { "exp2", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2415. // log2
  2416. { "log2", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2417. { "log2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2418. { "log2", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2419. { "log2", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2420. // sqrt
  2421. { "sqrt", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2422. { "sqrt", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2423. { "sqrt", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2424. { "sqrt", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2425. // inversesqrt
  2426. { "inversesqrt", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2427. { "inversesqrt", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2428. { "inversesqrt", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2429. { "inversesqrt", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2430. // Built-ins - common functions.
  2431. // abs
  2432. { "abs", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2433. { "abs", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2434. { "abs", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2435. { "abs", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2436. { "abs", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2437. { "abs", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2438. { "abs", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2439. { "abs", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2440. // sign
  2441. { "sign", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2442. { "sign", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2443. { "sign", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2444. { "sign", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2445. { "sign", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2446. { "sign", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2447. { "sign", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2448. { "sign", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2449. // floor
  2450. { "floor", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2451. { "floor", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2452. { "floor", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2453. { "floor", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2454. // trunc
  2455. { "trunc", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2456. { "trunc", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2457. { "trunc", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2458. { "trunc", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2459. // round
  2460. { "round", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2461. { "round", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2462. { "round", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2463. { "round", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2464. // roundEven
  2465. { "roundEven", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2466. { "roundEven", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2467. { "roundEven", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2468. { "roundEven", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2469. // ceil
  2470. { "ceil", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2471. { "ceil", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2472. { "ceil", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2473. { "ceil", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2474. // fract
  2475. { "fract", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2476. { "fract", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2477. { "fract", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2478. { "fract", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2479. // mod
  2480. { "mod", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2481. { "mod", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2482. { "mod", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2483. { "mod", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2484. { "mod", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2485. { "mod", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2486. { "mod", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2487. // modf
  2488. { "modf", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "i" }, TAG_GLOBAL, false },
  2489. { "modf", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "x", "i" }, TAG_GLOBAL, false },
  2490. { "modf", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "x", "i" }, TAG_GLOBAL, false },
  2491. { "modf", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "x", "i" }, TAG_GLOBAL, false },
  2492. // min
  2493. { "min", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2494. { "min", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2495. { "min", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2496. { "min", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2497. { "min", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2498. { "min", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2499. { "min", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2500. { "min", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2501. { "min", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2502. { "min", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2503. { "min", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2504. { "min", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2505. { "min", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2506. { "min", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2507. { "min", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2508. { "min", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2509. { "min", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2510. { "min", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2511. { "min", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2512. { "min", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2513. { "min", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2514. // max
  2515. { "max", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2516. { "max", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2517. { "max", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2518. { "max", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2519. { "max", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2520. { "max", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2521. { "max", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2522. { "max", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2523. { "max", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2524. { "max", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2525. { "max", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2526. { "max", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2527. { "max", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2528. { "max", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2529. { "max", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2530. { "max", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2531. { "max", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2532. { "max", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2533. { "max", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2534. { "max", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2535. { "max", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2536. // clamp
  2537. { "clamp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2538. { "clamp", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2539. { "clamp", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2540. { "clamp", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2541. { "clamp", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2542. { "clamp", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2543. { "clamp", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2544. { "clamp", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2545. { "clamp", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2546. { "clamp", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2547. { "clamp", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2548. { "clamp", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2549. { "clamp", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2550. { "clamp", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2551. { "clamp", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2552. { "clamp", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2553. { "clamp", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2554. { "clamp", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2555. { "clamp", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2556. { "clamp", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2557. { "clamp", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2558. // mix
  2559. { "mix", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2560. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2561. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_BVEC2, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2562. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2563. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2564. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_BVEC3, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2565. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2566. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2567. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_BVEC4, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2568. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2569. // step
  2570. { "step", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2571. { "step", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2572. { "step", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2573. { "step", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2574. { "step", TYPE_VEC2, { TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2575. { "step", TYPE_VEC3, { TYPE_FLOAT, TYPE_VEC3, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2576. { "step", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC4, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2577. // smoothstep
  2578. { "smoothstep", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2579. { "smoothstep", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2580. { "smoothstep", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2581. { "smoothstep", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2582. { "smoothstep", TYPE_VEC2, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2583. { "smoothstep", TYPE_VEC3, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC3, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2584. { "smoothstep", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC4, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2585. // isnan
  2586. { "isnan", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2587. { "isnan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2588. { "isnan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2589. { "isnan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2590. // isinf
  2591. { "isinf", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2592. { "isinf", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2593. { "isinf", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2594. { "isinf", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2595. // floatBitsToInt
  2596. { "floatBitsToInt", TYPE_INT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2597. { "floatBitsToInt", TYPE_IVEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2598. { "floatBitsToInt", TYPE_IVEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2599. { "floatBitsToInt", TYPE_IVEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2600. // floatBitsToUint
  2601. { "floatBitsToUint", TYPE_UINT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2602. { "floatBitsToUint", TYPE_UVEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2603. { "floatBitsToUint", TYPE_UVEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2604. { "floatBitsToUint", TYPE_UVEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2605. // intBitsToFloat
  2606. { "intBitsToFloat", TYPE_FLOAT, { TYPE_INT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2607. { "intBitsToFloat", TYPE_VEC2, { TYPE_IVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2608. { "intBitsToFloat", TYPE_VEC3, { TYPE_IVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2609. { "intBitsToFloat", TYPE_VEC4, { TYPE_IVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2610. // uintBitsToFloat
  2611. { "uintBitsToFloat", TYPE_FLOAT, { TYPE_UINT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2612. { "uintBitsToFloat", TYPE_VEC2, { TYPE_UVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2613. { "uintBitsToFloat", TYPE_VEC3, { TYPE_UVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2614. { "uintBitsToFloat", TYPE_VEC4, { TYPE_UVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2615. // Built-ins - geometric functions.
  2616. // length
  2617. { "length", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2618. { "length", TYPE_FLOAT, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2619. { "length", TYPE_FLOAT, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2620. { "length", TYPE_FLOAT, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2621. // distance
  2622. { "distance", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2623. { "distance", TYPE_FLOAT, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2624. { "distance", TYPE_FLOAT, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2625. { "distance", TYPE_FLOAT, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2626. // dot
  2627. { "dot", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2628. { "dot", TYPE_FLOAT, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2629. { "dot", TYPE_FLOAT, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2630. { "dot", TYPE_FLOAT, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2631. // cross
  2632. { "cross", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2633. // normalize
  2634. { "normalize", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2635. { "normalize", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2636. { "normalize", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2637. { "normalize", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2638. // reflect
  2639. { "reflect", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "I", "N" }, TAG_GLOBAL, false },
  2640. { "reflect", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "I", "N" }, TAG_GLOBAL, false },
  2641. { "reflect", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "I", "N" }, TAG_GLOBAL, false },
  2642. // refract
  2643. { "refract", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "I", "N", "eta" }, TAG_GLOBAL, false },
  2644. { "refract", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "I", "N", "eta" }, TAG_GLOBAL, false },
  2645. { "refract", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "I", "N", "eta" }, TAG_GLOBAL, false },
  2646. // faceforward
  2647. { "faceforward", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "N", "I", "Nref" }, TAG_GLOBAL, false },
  2648. { "faceforward", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "N", "I", "Nref" }, TAG_GLOBAL, false },
  2649. { "faceforward", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "N", "I", "Nref" }, TAG_GLOBAL, false },
  2650. // matrixCompMult
  2651. { "matrixCompMult", TYPE_MAT2, { TYPE_MAT2, TYPE_MAT2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2652. { "matrixCompMult", TYPE_MAT3, { TYPE_MAT3, TYPE_MAT3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2653. { "matrixCompMult", TYPE_MAT4, { TYPE_MAT4, TYPE_MAT4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2654. // outerProduct
  2655. { "outerProduct", TYPE_MAT2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "c", "r" }, TAG_GLOBAL, false },
  2656. { "outerProduct", TYPE_MAT3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "c", "r" }, TAG_GLOBAL, false },
  2657. { "outerProduct", TYPE_MAT4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "c", "r" }, TAG_GLOBAL, false },
  2658. // transpose
  2659. { "transpose", TYPE_MAT2, { TYPE_MAT2, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2660. { "transpose", TYPE_MAT3, { TYPE_MAT3, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2661. { "transpose", TYPE_MAT4, { TYPE_MAT4, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2662. // determinant
  2663. { "determinant", TYPE_FLOAT, { TYPE_MAT2, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2664. { "determinant", TYPE_FLOAT, { TYPE_MAT3, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2665. { "determinant", TYPE_FLOAT, { TYPE_MAT4, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2666. // inverse
  2667. { "inverse", TYPE_MAT2, { TYPE_MAT2, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2668. { "inverse", TYPE_MAT3, { TYPE_MAT3, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2669. { "inverse", TYPE_MAT4, { TYPE_MAT4, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2670. // lessThan
  2671. { "lessThan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2672. { "lessThan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2673. { "lessThan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2674. { "lessThan", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2675. { "lessThan", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2676. { "lessThan", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2677. { "lessThan", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2678. { "lessThan", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2679. { "lessThan", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2680. // greaterThan
  2681. { "greaterThan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2682. { "greaterThan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2683. { "greaterThan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2684. { "greaterThan", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2685. { "greaterThan", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2686. { "greaterThan", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2687. { "greaterThan", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2688. { "greaterThan", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2689. { "greaterThan", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2690. // lessThanEqual
  2691. { "lessThanEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2692. { "lessThanEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2693. { "lessThanEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2694. { "lessThanEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2695. { "lessThanEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2696. { "lessThanEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2697. { "lessThanEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2698. { "lessThanEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2699. { "lessThanEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2700. // greaterThanEqual
  2701. { "greaterThanEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2702. { "greaterThanEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2703. { "greaterThanEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2704. { "greaterThanEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2705. { "greaterThanEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2706. { "greaterThanEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2707. { "greaterThanEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2708. { "greaterThanEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2709. { "greaterThanEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2710. // equal
  2711. { "equal", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2712. { "equal", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2713. { "equal", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2714. { "equal", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2715. { "equal", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2716. { "equal", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2717. { "equal", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2718. { "equal", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2719. { "equal", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2720. { "equal", TYPE_BVEC2, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2721. { "equal", TYPE_BVEC3, { TYPE_BVEC3, TYPE_BVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2722. { "equal", TYPE_BVEC4, { TYPE_BVEC4, TYPE_BVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2723. // notEqual
  2724. { "notEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2725. { "notEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2726. { "notEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2727. { "notEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2728. { "notEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2729. { "notEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2730. { "notEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2731. { "notEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2732. { "notEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2733. { "notEqual", TYPE_BVEC2, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2734. { "notEqual", TYPE_BVEC3, { TYPE_BVEC3, TYPE_BVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2735. { "notEqual", TYPE_BVEC4, { TYPE_BVEC4, TYPE_BVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2736. // any
  2737. { "any", TYPE_BOOL, { TYPE_BVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2738. { "any", TYPE_BOOL, { TYPE_BVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2739. { "any", TYPE_BOOL, { TYPE_BVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2740. // all
  2741. { "all", TYPE_BOOL, { TYPE_BVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2742. { "all", TYPE_BOOL, { TYPE_BVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2743. { "all", TYPE_BOOL, { TYPE_BVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2744. // not
  2745. { "not", TYPE_BVEC2, { TYPE_BVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2746. { "not", TYPE_BVEC3, { TYPE_BVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2747. { "not", TYPE_BVEC4, { TYPE_BVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2748. // Built-ins: texture functions.
  2749. // textureSize
  2750. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLER2D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2751. { "textureSize", TYPE_IVEC2, { TYPE_ISAMPLER2D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2752. { "textureSize", TYPE_IVEC2, { TYPE_USAMPLER2D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2753. { "textureSize", TYPE_IVEC3, { TYPE_SAMPLER2DARRAY, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2754. { "textureSize", TYPE_IVEC3, { TYPE_ISAMPLER2DARRAY, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2755. { "textureSize", TYPE_IVEC3, { TYPE_USAMPLER2DARRAY, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2756. { "textureSize", TYPE_IVEC3, { TYPE_SAMPLER3D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2757. { "textureSize", TYPE_IVEC3, { TYPE_ISAMPLER3D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2758. { "textureSize", TYPE_IVEC3, { TYPE_USAMPLER3D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2759. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLERCUBE, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2760. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLERCUBEARRAY, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2761. // texture
  2762. { "texture", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2763. { "texture", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2764. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2765. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2766. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2767. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2768. { "texture", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2769. { "texture", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2770. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2771. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2772. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2773. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2774. { "texture", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2775. { "texture", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2776. { "texture", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2777. { "texture", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2778. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2779. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2780. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2781. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2782. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBEARRAY, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2783. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBEARRAY, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2784. // textureProj
  2785. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2786. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2787. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2788. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2789. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2790. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2791. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2792. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2793. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2794. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2795. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2796. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2797. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2798. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2799. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2800. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2801. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2802. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2803. // textureLod
  2804. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2805. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2806. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2807. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2808. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2809. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2810. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2811. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2812. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2813. { "textureLod", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2814. { "textureLod", TYPE_VEC4, { TYPE_SAMPLERCUBEARRAY, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2815. // texelFetch
  2816. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2817. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2818. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2819. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2820. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2821. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2822. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2823. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2824. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2825. // textureProjLod
  2826. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2827. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2828. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2829. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2830. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2831. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2832. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2833. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2834. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2835. // textureGrad
  2836. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2837. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2838. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2839. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2840. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2841. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2842. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2843. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2844. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2845. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2846. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLERCUBEARRAY, TYPE_VEC4, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2847. // textureProjGrad
  2848. { "textureProjGrad", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2849. { "textureProjGrad", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2850. { "textureProjGrad", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2851. { "textureProjGrad", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2852. { "textureProjGrad", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2853. { "textureProjGrad", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2854. { "textureProjGrad", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2855. { "textureProjGrad", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2856. { "textureProjGrad", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2857. // textureGather
  2858. { "textureGather", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2859. { "textureGather", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2860. { "textureGather", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2861. { "textureGather", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2862. { "textureGather", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2863. { "textureGather", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2864. { "textureGather", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2865. { "textureGather", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2866. { "textureGather", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2867. { "textureGather", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2868. { "textureGather", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2869. { "textureGather", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2870. { "textureGather", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2871. { "textureGather", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2872. // textureQueryLod
  2873. { "textureQueryLod", TYPE_VEC2, { TYPE_SAMPLER2D, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2874. { "textureQueryLod", TYPE_VEC2, { TYPE_ISAMPLER2D, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2875. { "textureQueryLod", TYPE_VEC2, { TYPE_USAMPLER2D, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2876. { "textureQueryLod", TYPE_VEC2, { TYPE_SAMPLER2DARRAY, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2877. { "textureQueryLod", TYPE_VEC2, { TYPE_ISAMPLER2DARRAY, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2878. { "textureQueryLod", TYPE_VEC2, { TYPE_USAMPLER2DARRAY, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2879. { "textureQueryLod", TYPE_VEC2, { TYPE_SAMPLER3D, TYPE_VEC3 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2880. { "textureQueryLod", TYPE_VEC2, { TYPE_ISAMPLER3D, TYPE_VEC3 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2881. { "textureQueryLod", TYPE_VEC2, { TYPE_USAMPLER3D, TYPE_VEC3 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2882. { "textureQueryLod", TYPE_VEC2, { TYPE_SAMPLERCUBE, TYPE_VEC3 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2883. // textureQueryLevels
  2884. { "textureQueryLevels", TYPE_INT, { TYPE_SAMPLER2D }, { "sampler" }, TAG_GLOBAL, true },
  2885. { "textureQueryLevels", TYPE_INT, { TYPE_ISAMPLER2D }, { "sampler" }, TAG_GLOBAL, true },
  2886. { "textureQueryLevels", TYPE_INT, { TYPE_USAMPLER2D }, { "sampler" }, TAG_GLOBAL, true },
  2887. { "textureQueryLevels", TYPE_INT, { TYPE_SAMPLER2DARRAY }, { "sampler" }, TAG_GLOBAL, true },
  2888. { "textureQueryLevels", TYPE_INT, { TYPE_ISAMPLER2DARRAY }, { "sampler" }, TAG_GLOBAL, true },
  2889. { "textureQueryLevels", TYPE_INT, { TYPE_USAMPLER2DARRAY }, { "sampler" }, TAG_GLOBAL, true },
  2890. { "textureQueryLevels", TYPE_INT, { TYPE_SAMPLER3D }, { "sampler" }, TAG_GLOBAL, true },
  2891. { "textureQueryLevels", TYPE_INT, { TYPE_ISAMPLER3D }, { "sampler" }, TAG_GLOBAL, true },
  2892. { "textureQueryLevels", TYPE_INT, { TYPE_USAMPLER3D }, { "sampler" }, TAG_GLOBAL, true },
  2893. { "textureQueryLevels", TYPE_INT, { TYPE_SAMPLERCUBE }, { "sampler" }, TAG_GLOBAL, true },
  2894. // dFdx
  2895. { "dFdx", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2896. { "dFdx", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2897. { "dFdx", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2898. { "dFdx", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2899. // dFdxCoarse
  2900. { "dFdxCoarse", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2901. { "dFdxCoarse", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2902. { "dFdxCoarse", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2903. { "dFdxCoarse", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2904. // dFdxFine
  2905. { "dFdxFine", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2906. { "dFdxFine", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2907. { "dFdxFine", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2908. { "dFdxFine", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2909. // dFdy
  2910. { "dFdy", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2911. { "dFdy", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2912. { "dFdy", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2913. { "dFdy", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2914. // dFdyCoarse
  2915. { "dFdyCoarse", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2916. { "dFdyCoarse", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2917. { "dFdyCoarse", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2918. { "dFdyCoarse", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2919. // dFdyFine
  2920. { "dFdyFine", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2921. { "dFdyFine", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2922. { "dFdyFine", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2923. { "dFdyFine", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2924. // fwidth
  2925. { "fwidth", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2926. { "fwidth", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2927. { "fwidth", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2928. { "fwidth", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2929. // fwidthCoarse
  2930. { "fwidthCoarse", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2931. { "fwidthCoarse", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2932. { "fwidthCoarse", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2933. { "fwidthCoarse", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2934. // fwidthFine
  2935. { "fwidthFine", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2936. { "fwidthFine", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2937. { "fwidthFine", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2938. { "fwidthFine", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2939. // Sub-functions.
  2940. // array
  2941. { "length", TYPE_INT, { TYPE_VOID }, { "" }, TAG_ARRAY, false },
  2942. // Modern functions.
  2943. // fma
  2944. { "fma", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "c" }, TAG_GLOBAL, true },
  2945. { "fma", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b", "c" }, TAG_GLOBAL, true },
  2946. { "fma", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b", "c" }, TAG_GLOBAL, true },
  2947. { "fma", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b", "c" }, TAG_GLOBAL, true },
  2948. // Packing/Unpacking functions.
  2949. { "packHalf2x16", TYPE_UINT, { TYPE_VEC2, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2950. { "packUnorm2x16", TYPE_UINT, { TYPE_VEC2, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2951. { "packSnorm2x16", TYPE_UINT, { TYPE_VEC2, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2952. { "packUnorm4x8", TYPE_UINT, { TYPE_VEC4, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2953. { "packSnorm4x8", TYPE_UINT, { TYPE_VEC4, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2954. { "unpackHalf2x16", TYPE_VEC2, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2955. { "unpackUnorm2x16", TYPE_VEC2, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2956. { "unpackSnorm2x16", TYPE_VEC2, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2957. { "unpackUnorm4x8", TYPE_VEC4, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2958. { "unpackSnorm4x8", TYPE_VEC4, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2959. // bitfieldExtract
  2960. { "bitfieldExtract", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2961. { "bitfieldExtract", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2962. { "bitfieldExtract", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2963. { "bitfieldExtract", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2964. { "bitfieldExtract", TYPE_UINT, { TYPE_UINT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2965. { "bitfieldExtract", TYPE_UVEC2, { TYPE_UVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2966. { "bitfieldExtract", TYPE_UVEC3, { TYPE_UVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2967. { "bitfieldExtract", TYPE_UVEC4, { TYPE_UVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2968. // bitfieldInsert
  2969. { "bitfieldInsert", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2970. { "bitfieldInsert", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2971. { "bitfieldInsert", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2972. { "bitfieldInsert", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2973. { "bitfieldInsert", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2974. { "bitfieldInsert", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2975. { "bitfieldInsert", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2976. { "bitfieldInsert", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2977. // bitfieldReverse
  2978. { "bitfieldReverse", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2979. { "bitfieldReverse", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2980. { "bitfieldReverse", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2981. { "bitfieldReverse", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2982. { "bitfieldReverse", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2983. { "bitfieldReverse", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2984. { "bitfieldReverse", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2985. { "bitfieldReverse", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2986. // bitCount
  2987. { "bitCount", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2988. { "bitCount", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2989. { "bitCount", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2990. { "bitCount", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2991. { "bitCount", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2992. { "bitCount", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2993. { "bitCount", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2994. { "bitCount", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2995. // findLSB
  2996. { "findLSB", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2997. { "findLSB", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2998. { "findLSB", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2999. { "findLSB", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  3000. { "findLSB", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  3001. { "findLSB", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  3002. { "findLSB", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  3003. { "findLSB", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  3004. // findMSB
  3005. { "findMSB", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  3006. { "findMSB", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  3007. { "findMSB", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  3008. { "findMSB", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  3009. { "findMSB", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  3010. { "findMSB", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  3011. { "findMSB", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  3012. { "findMSB", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  3013. // umulExtended
  3014. { "umulExtended", TYPE_VOID, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  3015. { "umulExtended", TYPE_VOID, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  3016. { "umulExtended", TYPE_VOID, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  3017. { "umulExtended", TYPE_VOID, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  3018. // imulExtended
  3019. { "imulExtended", TYPE_VOID, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  3020. { "imulExtended", TYPE_VOID, { TYPE_IVEC2, TYPE_IVEC2, TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  3021. { "imulExtended", TYPE_VOID, { TYPE_IVEC3, TYPE_IVEC3, TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  3022. { "imulExtended", TYPE_VOID, { TYPE_IVEC4, TYPE_IVEC4, TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  3023. // uaddCarry
  3024. { "uaddCarry", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "y", "carry" }, TAG_GLOBAL, true },
  3025. { "uaddCarry", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "x", "y", "carry" }, TAG_GLOBAL, true },
  3026. { "uaddCarry", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "x", "y", "carry" }, TAG_GLOBAL, true },
  3027. { "uaddCarry", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "x", "y", "carry" }, TAG_GLOBAL, true },
  3028. // usubBorrow
  3029. { "usubBorrow", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "y", "borrow" }, TAG_GLOBAL, true },
  3030. { "usubBorrow", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "x", "y", "borrow" }, TAG_GLOBAL, true },
  3031. { "usubBorrow", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "x", "y", "borrow" }, TAG_GLOBAL, true },
  3032. { "usubBorrow", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "x", "y", "borrow" }, TAG_GLOBAL, true },
  3033. // ldexp
  3034. { "ldexp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_INT, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  3035. { "ldexp", TYPE_VEC2, { TYPE_VEC2, TYPE_IVEC2, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  3036. { "ldexp", TYPE_VEC3, { TYPE_VEC3, TYPE_IVEC3, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  3037. { "ldexp", TYPE_VEC4, { TYPE_VEC4, TYPE_IVEC4, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  3038. // frexp
  3039. { "frexp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_INT, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  3040. { "frexp", TYPE_VEC2, { TYPE_VEC2, TYPE_IVEC2, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  3041. { "frexp", TYPE_VEC3, { TYPE_VEC3, TYPE_IVEC3, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  3042. { "frexp", TYPE_VEC4, { TYPE_VEC4, TYPE_IVEC4, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  3043. { nullptr, TYPE_VOID, { TYPE_VOID }, { "" }, TAG_GLOBAL, false }
  3044. };
  3045. HashSet<StringName> global_func_set;
  3046. const ShaderLanguage::BuiltinFuncOutArgs ShaderLanguage::builtin_func_out_args[] = {
  3047. { "modf", { 1, -1 } },
  3048. { "umulExtended", { 2, 3 } },
  3049. { "imulExtended", { 2, 3 } },
  3050. { "uaddCarry", { 2, -1 } },
  3051. { "usubBorrow", { 2, -1 } },
  3052. { "ldexp", { 1, -1 } },
  3053. { "frexp", { 1, -1 } },
  3054. { nullptr, { 0, -1 } }
  3055. };
  3056. const ShaderLanguage::BuiltinFuncConstArgs ShaderLanguage::builtin_func_const_args[] = {
  3057. { "textureGather", 2, 0, 3 },
  3058. { nullptr, 0, 0, 0 }
  3059. };
  3060. const ShaderLanguage::BuiltinEntry ShaderLanguage::frag_only_func_defs[] = {
  3061. { "dFdx" },
  3062. { "dFdxCoarse" },
  3063. { "dFdxFine" },
  3064. { "dFdy" },
  3065. { "dFdyCoarse" },
  3066. { "dFdyFine" },
  3067. { "fwidth" },
  3068. { "fwidthCoarse" },
  3069. { "fwidthFine" },
  3070. { nullptr }
  3071. };
  3072. bool ShaderLanguage::is_const_suffix_lut_initialized = false;
  3073. 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) {
  3074. ERR_FAIL_COND_V(p_func->op != OP_CALL && p_func->op != OP_CONSTRUCT, false);
  3075. Vector<DataType> args;
  3076. Vector<StringName> args2;
  3077. Vector<int> args3;
  3078. ERR_FAIL_COND_V(p_func->arguments[0]->type != Node::NODE_TYPE_VARIABLE, false);
  3079. StringName name = static_cast<VariableNode *>(p_func->arguments[0])->name.operator String();
  3080. for (int i = 1; i < p_func->arguments.size(); i++) {
  3081. args.push_back(p_func->arguments[i]->get_datatype());
  3082. args2.push_back(p_func->arguments[i]->get_datatype_name());
  3083. args3.push_back(p_func->arguments[i]->get_array_size());
  3084. }
  3085. int argcount = args.size();
  3086. if (p_function_info.stage_functions.has(name)) {
  3087. //stage based function
  3088. const StageFunctionInfo &sf = p_function_info.stage_functions[name];
  3089. if (argcount != sf.arguments.size()) {
  3090. _set_error(vformat(RTR("Invalid number of arguments when calling stage function '%s', which expects %d arguments."), String(name), sf.arguments.size()));
  3091. return false;
  3092. }
  3093. //validate arguments
  3094. for (int i = 0; i < argcount; i++) {
  3095. if (args[i] != sf.arguments[i].type) {
  3096. _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)));
  3097. return false;
  3098. }
  3099. }
  3100. if (r_ret_type) {
  3101. *r_ret_type = sf.return_type;
  3102. }
  3103. if (r_ret_type_str) {
  3104. *r_ret_type_str = "";
  3105. }
  3106. return true;
  3107. }
  3108. bool failed_builtin = false;
  3109. bool unsupported_builtin = false;
  3110. int builtin_idx = 0;
  3111. if (argcount <= 4) {
  3112. // test builtins
  3113. int idx = 0;
  3114. while (builtin_func_defs[idx].name) {
  3115. if (completion_class != builtin_func_defs[idx].tag) {
  3116. idx++;
  3117. continue;
  3118. }
  3119. if (name == builtin_func_defs[idx].name) {
  3120. failed_builtin = true;
  3121. bool fail = false;
  3122. for (int i = 0; i < argcount; i++) {
  3123. if (p_func->arguments[i + 1]->type == Node::NODE_TYPE_ARRAY) {
  3124. const ArrayNode *anode = static_cast<const ArrayNode *>(p_func->arguments[i + 1]);
  3125. if (anode->call_expression == nullptr && !anode->is_indexed()) {
  3126. fail = true;
  3127. break;
  3128. }
  3129. }
  3130. 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])) {
  3131. //all good, but needs implicit conversion later
  3132. } else if (args[i] != builtin_func_defs[idx].args[i]) {
  3133. fail = true;
  3134. break;
  3135. }
  3136. }
  3137. if (!fail) {
  3138. if (RenderingServer::get_singleton()->is_low_end()) {
  3139. if (builtin_func_defs[idx].high_end) {
  3140. fail = true;
  3141. unsupported_builtin = true;
  3142. builtin_idx = idx;
  3143. }
  3144. }
  3145. }
  3146. if (!fail && argcount < 4 && builtin_func_defs[idx].args[argcount] != TYPE_VOID) {
  3147. fail = true; //make sure the number of arguments matches
  3148. }
  3149. if (!fail) {
  3150. {
  3151. int constarg_idx = 0;
  3152. while (builtin_func_const_args[constarg_idx].name) {
  3153. if (String(name) == builtin_func_const_args[constarg_idx].name) {
  3154. int arg = builtin_func_const_args[constarg_idx].arg + 1;
  3155. if (p_func->arguments.size() <= arg) {
  3156. break;
  3157. }
  3158. int min = builtin_func_const_args[constarg_idx].min;
  3159. int max = builtin_func_const_args[constarg_idx].max;
  3160. bool error = false;
  3161. Vector<Scalar> values = _get_node_values(p_block, p_func->arguments[arg]);
  3162. if (p_func->arguments[arg]->get_datatype() == TYPE_INT && !values.is_empty()) {
  3163. if (values[0].sint < min || values[0].sint > max) {
  3164. error = true;
  3165. }
  3166. } else {
  3167. error = true;
  3168. }
  3169. if (error) {
  3170. _set_error(vformat(RTR("Expected integer constant within [%d..%d] range."), min, max));
  3171. return false;
  3172. }
  3173. }
  3174. constarg_idx++;
  3175. }
  3176. }
  3177. //make sure its not an out argument used in the wrong way
  3178. int outarg_idx = 0;
  3179. while (builtin_func_out_args[outarg_idx].name) {
  3180. if (String(name) == builtin_func_out_args[outarg_idx].name) {
  3181. for (int arg = 0; arg < BuiltinFuncOutArgs::MAX_ARGS; arg++) {
  3182. int arg_idx = builtin_func_out_args[outarg_idx].arguments[arg];
  3183. if (arg_idx == -1) {
  3184. break;
  3185. }
  3186. if (arg_idx < argcount) {
  3187. 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) {
  3188. _set_error(vformat(RTR("Argument %d of function '%s' is not a variable, array, or member."), arg_idx + 1, String(name)));
  3189. return false;
  3190. }
  3191. if (p_func->arguments[arg_idx + 1]->type == Node::NODE_TYPE_ARRAY) {
  3192. ArrayNode *mn = static_cast<ArrayNode *>(p_func->arguments[arg_idx + 1]);
  3193. if (mn->is_const) {
  3194. fail = true;
  3195. }
  3196. } else if (p_func->arguments[arg_idx + 1]->type == Node::NODE_TYPE_MEMBER) {
  3197. MemberNode *mn = static_cast<MemberNode *>(p_func->arguments[arg_idx + 1]);
  3198. if (mn->basetype_const) {
  3199. fail = true;
  3200. }
  3201. } else { // TYPE_VARIABLE
  3202. VariableNode *vn = static_cast<VariableNode *>(p_func->arguments[arg_idx + 1]);
  3203. if (vn->is_const) {
  3204. fail = true;
  3205. } else {
  3206. StringName varname = vn->name;
  3207. if (shader->uniforms.has(varname)) {
  3208. fail = true;
  3209. } else {
  3210. if (shader->varyings.has(varname)) {
  3211. _set_error(vformat(RTR("Varyings cannot be passed for the '%s' parameter."), "out"));
  3212. return false;
  3213. }
  3214. if (p_function_info.built_ins.has(varname)) {
  3215. BuiltInInfo info = p_function_info.built_ins[varname];
  3216. if (info.constant) {
  3217. fail = true;
  3218. }
  3219. }
  3220. }
  3221. }
  3222. }
  3223. if (fail) {
  3224. _set_error(vformat(RTR("A constant value cannot be passed for the '%s' parameter."), "out"));
  3225. return false;
  3226. }
  3227. StringName var_name;
  3228. if (p_func->arguments[arg_idx + 1]->type == Node::NODE_TYPE_ARRAY) {
  3229. var_name = static_cast<const ArrayNode *>(p_func->arguments[arg_idx + 1])->name;
  3230. } else if (p_func->arguments[arg_idx + 1]->type == Node::NODE_TYPE_MEMBER) {
  3231. Node *n = static_cast<const MemberNode *>(p_func->arguments[arg_idx + 1])->owner;
  3232. while (n->type == Node::NODE_TYPE_MEMBER) {
  3233. n = static_cast<const MemberNode *>(n)->owner;
  3234. }
  3235. if (n->type != Node::NODE_TYPE_VARIABLE && n->type != Node::NODE_TYPE_ARRAY) {
  3236. _set_error(vformat(RTR("Argument %d of function '%s' is not a variable, array, or member."), arg_idx + 1, String(name)));
  3237. return false;
  3238. }
  3239. if (n->type == Node::NODE_TYPE_VARIABLE) {
  3240. var_name = static_cast<const VariableNode *>(n)->name;
  3241. } else { // TYPE_ARRAY
  3242. var_name = static_cast<const ArrayNode *>(n)->name;
  3243. }
  3244. } else { // TYPE_VARIABLE
  3245. var_name = static_cast<const VariableNode *>(p_func->arguments[arg_idx + 1])->name;
  3246. }
  3247. const BlockNode *b = p_block;
  3248. bool valid = false;
  3249. while (b) {
  3250. if (b->variables.has(var_name) || p_function_info.built_ins.has(var_name)) {
  3251. valid = true;
  3252. break;
  3253. }
  3254. if (b->parent_function) {
  3255. for (int i = 0; i < b->parent_function->arguments.size(); i++) {
  3256. if (b->parent_function->arguments[i].name == var_name) {
  3257. valid = true;
  3258. break;
  3259. }
  3260. }
  3261. }
  3262. b = b->parent_block;
  3263. }
  3264. if (!valid) {
  3265. _set_error(vformat(RTR("Argument %d of function '%s' can only take a local variable, array, or member."), arg_idx + 1, String(name)));
  3266. return false;
  3267. }
  3268. }
  3269. }
  3270. }
  3271. outarg_idx++;
  3272. }
  3273. //implicitly convert values if possible
  3274. for (int i = 0; i < argcount; i++) {
  3275. 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) {
  3276. //can't do implicit conversion here
  3277. continue;
  3278. }
  3279. //this is an implicit conversion
  3280. ConstantNode *constant = static_cast<ConstantNode *>(p_func->arguments[i + 1]);
  3281. ConstantNode *conversion = alloc_node<ConstantNode>();
  3282. conversion->datatype = builtin_func_defs[idx].args[i];
  3283. conversion->values.resize(1);
  3284. convert_constant(constant, builtin_func_defs[idx].args[i], conversion->values.ptrw());
  3285. p_func->arguments.write[i + 1] = conversion;
  3286. }
  3287. if (r_ret_type) {
  3288. *r_ret_type = builtin_func_defs[idx].rettype;
  3289. }
  3290. return true;
  3291. }
  3292. }
  3293. idx++;
  3294. }
  3295. }
  3296. if (unsupported_builtin) {
  3297. String arglist = "";
  3298. for (int i = 0; i < argcount; i++) {
  3299. if (i > 0) {
  3300. arglist += ", ";
  3301. }
  3302. arglist += get_datatype_name(builtin_func_defs[builtin_idx].args[i]);
  3303. }
  3304. _set_error(vformat(RTR("Built-in function \"%s(%s)\" is only supported on high-end platforms."), String(name), arglist));
  3305. return false;
  3306. }
  3307. if (failed_builtin) {
  3308. String arg_list;
  3309. for (int i = 0; i < argcount; i++) {
  3310. if (i > 0) {
  3311. arg_list += ",";
  3312. }
  3313. String arg_name;
  3314. if (args[i] == TYPE_STRUCT) {
  3315. arg_name = args2[i];
  3316. } else {
  3317. arg_name = get_datatype_name(args[i]);
  3318. }
  3319. if (args3[i] > 0) {
  3320. arg_name += "[";
  3321. arg_name += itos(args3[i]);
  3322. arg_name += "]";
  3323. }
  3324. arg_list += arg_name;
  3325. }
  3326. _set_error(vformat(RTR("Invalid arguments for the built-in function: \"%s(%s)\"."), String(name), arg_list));
  3327. return false;
  3328. }
  3329. // try existing functions..
  3330. StringName exclude_function;
  3331. BlockNode *block = p_block;
  3332. while (block) {
  3333. if (block->parent_function) {
  3334. exclude_function = block->parent_function->name;
  3335. }
  3336. block = block->parent_block;
  3337. }
  3338. if (name == exclude_function) {
  3339. _set_error(RTR("Recursion is not allowed."));
  3340. return false;
  3341. }
  3342. int last_arg_count = 0;
  3343. bool exists = false;
  3344. String arg_list = "";
  3345. for (int i = 0; i < shader->vfunctions.size(); i++) {
  3346. if (name != shader->vfunctions[i].name) {
  3347. continue;
  3348. }
  3349. exists = true;
  3350. if (!shader->vfunctions[i].callable) {
  3351. _set_error(vformat(RTR("Function '%s' can't be called from source code."), String(name)));
  3352. return false;
  3353. }
  3354. FunctionNode *pfunc = shader->vfunctions[i].function;
  3355. if (arg_list.is_empty()) {
  3356. for (int j = 0; j < pfunc->arguments.size(); j++) {
  3357. if (j > 0) {
  3358. arg_list += ", ";
  3359. }
  3360. String func_arg_name;
  3361. if (pfunc->arguments[j].type == TYPE_STRUCT) {
  3362. func_arg_name = pfunc->arguments[j].struct_name;
  3363. } else {
  3364. func_arg_name = get_datatype_name(pfunc->arguments[j].type);
  3365. }
  3366. if (pfunc->arguments[j].array_size > 0) {
  3367. func_arg_name += "[";
  3368. func_arg_name += itos(pfunc->arguments[j].array_size);
  3369. func_arg_name += "]";
  3370. }
  3371. arg_list += func_arg_name;
  3372. }
  3373. }
  3374. if (pfunc->arguments.size() != args.size()) {
  3375. last_arg_count = pfunc->arguments.size();
  3376. continue;
  3377. }
  3378. bool fail = false;
  3379. for (int j = 0; j < args.size(); j++) {
  3380. 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)) {
  3381. //all good, but it needs implicit conversion later
  3382. } 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) {
  3383. String func_arg_name;
  3384. if (pfunc->arguments[j].type == TYPE_STRUCT) {
  3385. func_arg_name = pfunc->arguments[j].struct_name;
  3386. } else {
  3387. func_arg_name = get_datatype_name(pfunc->arguments[j].type);
  3388. }
  3389. if (pfunc->arguments[j].array_size > 0) {
  3390. func_arg_name += "[";
  3391. func_arg_name += itos(pfunc->arguments[j].array_size);
  3392. func_arg_name += "]";
  3393. }
  3394. String arg_name;
  3395. if (args[j] == TYPE_STRUCT) {
  3396. arg_name = args2[j];
  3397. } else {
  3398. arg_name = get_datatype_name(args[j]);
  3399. }
  3400. if (args3[j] > 0) {
  3401. arg_name += "[";
  3402. arg_name += itos(args3[j]);
  3403. arg_name += "]";
  3404. }
  3405. _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));
  3406. fail = true;
  3407. break;
  3408. }
  3409. }
  3410. if (!fail) {
  3411. //implicitly convert values if possible
  3412. for (int k = 0; k < args.size(); k++) {
  3413. if (get_scalar_type(args[k]) != args[k] || args[k] == pfunc->arguments[k].type || p_func->arguments[k + 1]->type != Node::NODE_TYPE_CONSTANT) {
  3414. //can't do implicit conversion here
  3415. continue;
  3416. }
  3417. //this is an implicit conversion
  3418. ConstantNode *constant = static_cast<ConstantNode *>(p_func->arguments[k + 1]);
  3419. ConstantNode *conversion = alloc_node<ConstantNode>();
  3420. conversion->datatype = pfunc->arguments[k].type;
  3421. conversion->values.resize(1);
  3422. convert_constant(constant, pfunc->arguments[k].type, conversion->values.ptrw());
  3423. p_func->arguments.write[k + 1] = conversion;
  3424. }
  3425. if (r_ret_type) {
  3426. *r_ret_type = pfunc->return_type;
  3427. if (pfunc->return_type == TYPE_STRUCT) {
  3428. *r_ret_type_str = pfunc->return_struct_name;
  3429. }
  3430. }
  3431. if (r_is_custom_function) {
  3432. *r_is_custom_function = true;
  3433. }
  3434. return true;
  3435. }
  3436. }
  3437. if (exists) {
  3438. if (last_arg_count > args.size()) {
  3439. _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()));
  3440. } else if (last_arg_count < args.size()) {
  3441. _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()));
  3442. }
  3443. }
  3444. return false;
  3445. }
  3446. 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) {
  3447. bool result = true;
  3448. if (p_datatype_a == TYPE_STRUCT || p_datatype_b == TYPE_STRUCT) {
  3449. if (p_datatype_name_a != p_datatype_name_b) {
  3450. result = false;
  3451. }
  3452. } else {
  3453. if (p_datatype_a != p_datatype_b) {
  3454. result = false;
  3455. }
  3456. }
  3457. if (p_array_size_a != p_array_size_b) {
  3458. result = false;
  3459. }
  3460. if (!result) {
  3461. String type_name = p_datatype_a == TYPE_STRUCT ? p_datatype_name_a : get_datatype_name(p_datatype_a);
  3462. if (p_array_size_a > 0) {
  3463. type_name += "[";
  3464. type_name += itos(p_array_size_a);
  3465. type_name += "]";
  3466. }
  3467. String type_name2 = p_datatype_b == TYPE_STRUCT ? p_datatype_name_b : get_datatype_name(p_datatype_b);
  3468. if (p_array_size_b > 0) {
  3469. type_name2 += "[";
  3470. type_name2 += itos(p_array_size_b);
  3471. type_name2 += "]";
  3472. }
  3473. _set_error(vformat(RTR("Invalid assignment of '%s' to '%s'."), type_name2, type_name));
  3474. }
  3475. return result;
  3476. }
  3477. bool ShaderLanguage::_compare_datatypes_in_nodes(Node *a, Node *b) {
  3478. 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());
  3479. }
  3480. bool ShaderLanguage::_parse_function_arguments(BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_func, int *r_complete_arg) {
  3481. TkPos pos = _get_tkpos();
  3482. Token tk = _get_token();
  3483. if (tk.type == TK_PARENTHESIS_CLOSE) {
  3484. return true;
  3485. }
  3486. _set_tkpos(pos);
  3487. while (true) {
  3488. if (r_complete_arg) {
  3489. pos = _get_tkpos();
  3490. tk = _get_token();
  3491. if (tk.type == TK_CURSOR) {
  3492. *r_complete_arg = p_func->arguments.size() - 1;
  3493. } else {
  3494. _set_tkpos(pos);
  3495. }
  3496. }
  3497. Node *arg = _parse_and_reduce_expression(p_block, p_function_info);
  3498. if (!arg) {
  3499. return false;
  3500. }
  3501. if (is_const_decl && arg->type == Node::NODE_TYPE_VARIABLE) {
  3502. const VariableNode *var = static_cast<const VariableNode *>(arg);
  3503. if (!var->is_const) {
  3504. _set_error(RTR("Expected constant expression."));
  3505. return false;
  3506. }
  3507. }
  3508. p_func->arguments.push_back(arg);
  3509. tk = _get_token();
  3510. if (tk.type == TK_PARENTHESIS_CLOSE) {
  3511. return true;
  3512. } else if (tk.type != TK_COMMA) {
  3513. // something is broken
  3514. _set_error(RTR("Expected ',' or ')' after argument."));
  3515. return false;
  3516. }
  3517. }
  3518. return true;
  3519. }
  3520. bool ShaderLanguage::is_token_operator(TokenType p_type) {
  3521. return (p_type == TK_OP_EQUAL ||
  3522. p_type == TK_OP_NOT_EQUAL ||
  3523. p_type == TK_OP_LESS ||
  3524. p_type == TK_OP_LESS_EQUAL ||
  3525. p_type == TK_OP_GREATER ||
  3526. p_type == TK_OP_GREATER_EQUAL ||
  3527. p_type == TK_OP_AND ||
  3528. p_type == TK_OP_OR ||
  3529. p_type == TK_OP_NOT ||
  3530. p_type == TK_OP_ADD ||
  3531. p_type == TK_OP_SUB ||
  3532. p_type == TK_OP_MUL ||
  3533. p_type == TK_OP_DIV ||
  3534. p_type == TK_OP_MOD ||
  3535. p_type == TK_OP_SHIFT_LEFT ||
  3536. p_type == TK_OP_SHIFT_RIGHT ||
  3537. p_type == TK_OP_ASSIGN ||
  3538. p_type == TK_OP_ASSIGN_ADD ||
  3539. p_type == TK_OP_ASSIGN_SUB ||
  3540. p_type == TK_OP_ASSIGN_MUL ||
  3541. p_type == TK_OP_ASSIGN_DIV ||
  3542. p_type == TK_OP_ASSIGN_MOD ||
  3543. p_type == TK_OP_ASSIGN_SHIFT_LEFT ||
  3544. p_type == TK_OP_ASSIGN_SHIFT_RIGHT ||
  3545. p_type == TK_OP_ASSIGN_BIT_AND ||
  3546. p_type == TK_OP_ASSIGN_BIT_OR ||
  3547. p_type == TK_OP_ASSIGN_BIT_XOR ||
  3548. p_type == TK_OP_BIT_AND ||
  3549. p_type == TK_OP_BIT_OR ||
  3550. p_type == TK_OP_BIT_XOR ||
  3551. p_type == TK_OP_BIT_INVERT ||
  3552. p_type == TK_OP_INCREMENT ||
  3553. p_type == TK_OP_DECREMENT ||
  3554. p_type == TK_QUESTION ||
  3555. p_type == TK_COLON);
  3556. }
  3557. bool ShaderLanguage::is_token_operator_assign(TokenType p_type) {
  3558. return (p_type == TK_OP_ASSIGN ||
  3559. p_type == TK_OP_ASSIGN_ADD ||
  3560. p_type == TK_OP_ASSIGN_SUB ||
  3561. p_type == TK_OP_ASSIGN_MUL ||
  3562. p_type == TK_OP_ASSIGN_DIV ||
  3563. p_type == TK_OP_ASSIGN_MOD ||
  3564. p_type == TK_OP_ASSIGN_SHIFT_LEFT ||
  3565. p_type == TK_OP_ASSIGN_SHIFT_RIGHT ||
  3566. p_type == TK_OP_ASSIGN_BIT_AND ||
  3567. p_type == TK_OP_ASSIGN_BIT_OR ||
  3568. p_type == TK_OP_ASSIGN_BIT_XOR);
  3569. }
  3570. bool ShaderLanguage::is_token_hint(TokenType p_type) {
  3571. return int(p_type) > int(TK_RENDER_MODE) && int(p_type) < int(TK_SHADER_TYPE);
  3572. }
  3573. bool ShaderLanguage::convert_constant(ConstantNode *p_constant, DataType p_to_type, Scalar *p_value) {
  3574. if (p_constant->datatype == p_to_type) {
  3575. if (p_value) {
  3576. for (int i = 0; i < p_constant->values.size(); i++) {
  3577. p_value[i] = p_constant->values[i];
  3578. }
  3579. }
  3580. return true;
  3581. } else if (p_constant->datatype == TYPE_INT && p_to_type == TYPE_FLOAT) {
  3582. if (p_value) {
  3583. p_value->real = p_constant->values[0].sint;
  3584. }
  3585. return true;
  3586. } else if (p_constant->datatype == TYPE_UINT && p_to_type == TYPE_FLOAT) {
  3587. if (p_value) {
  3588. p_value->real = p_constant->values[0].uint;
  3589. }
  3590. return true;
  3591. } else if (p_constant->datatype == TYPE_INT && p_to_type == TYPE_UINT) {
  3592. if (p_constant->values[0].sint < 0) {
  3593. return false;
  3594. }
  3595. if (p_value) {
  3596. p_value->uint = p_constant->values[0].sint;
  3597. }
  3598. return true;
  3599. } else if (p_constant->datatype == TYPE_UINT && p_to_type == TYPE_INT) {
  3600. if (p_constant->values[0].uint > 0x7FFFFFFF) {
  3601. return false;
  3602. }
  3603. if (p_value) {
  3604. p_value->sint = p_constant->values[0].uint;
  3605. }
  3606. return true;
  3607. } else {
  3608. return false;
  3609. }
  3610. }
  3611. bool ShaderLanguage::is_scalar_type(DataType p_type) {
  3612. return p_type == TYPE_BOOL || p_type == TYPE_INT || p_type == TYPE_UINT || p_type == TYPE_FLOAT;
  3613. }
  3614. bool ShaderLanguage::is_float_type(DataType p_type) {
  3615. switch (p_type) {
  3616. case TYPE_FLOAT:
  3617. case TYPE_VEC2:
  3618. case TYPE_VEC3:
  3619. case TYPE_VEC4:
  3620. case TYPE_MAT2:
  3621. case TYPE_MAT3:
  3622. case TYPE_MAT4:
  3623. case TYPE_SAMPLER2D:
  3624. case TYPE_SAMPLER2DARRAY:
  3625. case TYPE_SAMPLER3D:
  3626. case TYPE_SAMPLERCUBE:
  3627. case TYPE_SAMPLERCUBEARRAY: {
  3628. return true;
  3629. }
  3630. default: {
  3631. return false;
  3632. }
  3633. }
  3634. }
  3635. bool ShaderLanguage::is_sampler_type(DataType p_type) {
  3636. return p_type > TYPE_MAT4 && p_type < TYPE_STRUCT;
  3637. }
  3638. Variant ShaderLanguage::constant_value_to_variant(const Vector<Scalar> &p_value, DataType p_type, int p_array_size, ShaderLanguage::ShaderNode::Uniform::Hint p_hint) {
  3639. int array_size = p_array_size;
  3640. if (p_value.size() > 0) {
  3641. Variant value;
  3642. switch (p_type) {
  3643. case ShaderLanguage::TYPE_BOOL:
  3644. if (array_size > 0) {
  3645. PackedInt32Array array;
  3646. for (int i = 0; i < array_size; i++) {
  3647. array.push_back(p_value[i].boolean);
  3648. }
  3649. value = Variant(array);
  3650. } else {
  3651. value = Variant(p_value[0].boolean);
  3652. }
  3653. break;
  3654. case ShaderLanguage::TYPE_BVEC2:
  3655. array_size *= 2;
  3656. if (array_size > 0) {
  3657. PackedInt32Array array;
  3658. for (int i = 0; i < array_size; i++) {
  3659. array.push_back(p_value[i].boolean);
  3660. }
  3661. value = Variant(array);
  3662. } else {
  3663. value = Variant(p_value[0].boolean);
  3664. }
  3665. break;
  3666. case ShaderLanguage::TYPE_BVEC3:
  3667. array_size *= 3;
  3668. if (array_size > 0) {
  3669. PackedInt32Array array;
  3670. for (int i = 0; i < array_size; i++) {
  3671. array.push_back(p_value[i].boolean);
  3672. }
  3673. value = Variant(array);
  3674. } else {
  3675. value = Variant(p_value[0].boolean);
  3676. }
  3677. break;
  3678. case ShaderLanguage::TYPE_BVEC4:
  3679. array_size *= 4;
  3680. if (array_size > 0) {
  3681. PackedInt32Array array;
  3682. for (int i = 0; i < array_size; i++) {
  3683. array.push_back(p_value[i].boolean);
  3684. }
  3685. value = Variant(array);
  3686. } else {
  3687. value = Variant(p_value[0].boolean);
  3688. }
  3689. break;
  3690. case ShaderLanguage::TYPE_INT:
  3691. if (array_size > 0) {
  3692. PackedInt32Array array;
  3693. for (int i = 0; i < array_size; i++) {
  3694. array.push_back(p_value[i].sint);
  3695. }
  3696. value = Variant(array);
  3697. } else {
  3698. value = Variant(p_value[0].sint);
  3699. }
  3700. break;
  3701. case ShaderLanguage::TYPE_IVEC2:
  3702. if (array_size > 0) {
  3703. array_size *= 2;
  3704. PackedInt32Array array;
  3705. for (int i = 0; i < array_size; i++) {
  3706. array.push_back(p_value[i].sint);
  3707. }
  3708. value = Variant(array);
  3709. } else {
  3710. value = Variant(Vector2i(p_value[0].sint, p_value[1].sint));
  3711. }
  3712. break;
  3713. case ShaderLanguage::TYPE_IVEC3:
  3714. if (array_size > 0) {
  3715. array_size *= 3;
  3716. PackedInt32Array array;
  3717. for (int i = 0; i < array_size; i++) {
  3718. array.push_back(p_value[i].sint);
  3719. }
  3720. value = Variant(array);
  3721. } else {
  3722. value = Variant(Vector3i(p_value[0].sint, p_value[1].sint, p_value[2].sint));
  3723. }
  3724. break;
  3725. case ShaderLanguage::TYPE_IVEC4:
  3726. if (array_size > 0) {
  3727. array_size *= 4;
  3728. PackedInt32Array array;
  3729. for (int i = 0; i < array_size; i++) {
  3730. array.push_back(p_value[i].sint);
  3731. }
  3732. value = Variant(array);
  3733. } else {
  3734. value = Variant(Vector4i(p_value[0].sint, p_value[1].sint, p_value[2].sint, p_value[3].sint));
  3735. }
  3736. break;
  3737. case ShaderLanguage::TYPE_UINT:
  3738. if (array_size > 0) {
  3739. PackedInt32Array array;
  3740. for (int i = 0; i < array_size; i++) {
  3741. array.push_back(p_value[i].uint);
  3742. }
  3743. value = Variant(array);
  3744. } else {
  3745. value = Variant(p_value[0].uint);
  3746. }
  3747. break;
  3748. case ShaderLanguage::TYPE_UVEC2:
  3749. if (array_size > 0) {
  3750. array_size *= 2;
  3751. PackedInt32Array array;
  3752. for (int i = 0; i < array_size; i++) {
  3753. array.push_back(p_value[i].uint);
  3754. }
  3755. value = Variant(array);
  3756. } else {
  3757. value = Variant(Vector2i(p_value[0].uint, p_value[1].uint));
  3758. }
  3759. break;
  3760. case ShaderLanguage::TYPE_UVEC3:
  3761. if (array_size > 0) {
  3762. array_size *= 3;
  3763. PackedInt32Array array;
  3764. for (int i = 0; i < array_size; i++) {
  3765. array.push_back(p_value[i].uint);
  3766. }
  3767. value = Variant(array);
  3768. } else {
  3769. value = Variant(Vector3i(p_value[0].uint, p_value[1].uint, p_value[2].uint));
  3770. }
  3771. break;
  3772. case ShaderLanguage::TYPE_UVEC4:
  3773. if (array_size > 0) {
  3774. array_size *= 4;
  3775. PackedInt32Array array;
  3776. for (int i = 0; i < array_size; i++) {
  3777. array.push_back(p_value[i].uint);
  3778. }
  3779. value = Variant(array);
  3780. } else {
  3781. value = Variant(Vector4i(p_value[0].uint, p_value[1].uint, p_value[2].uint, p_value[3].uint));
  3782. }
  3783. break;
  3784. case ShaderLanguage::TYPE_FLOAT:
  3785. if (array_size > 0) {
  3786. PackedFloat32Array array;
  3787. for (int i = 0; i < array_size; i++) {
  3788. array.push_back(p_value[i].real);
  3789. }
  3790. value = Variant(array);
  3791. } else {
  3792. value = Variant(p_value[0].real);
  3793. }
  3794. break;
  3795. case ShaderLanguage::TYPE_VEC2:
  3796. if (array_size > 0) {
  3797. array_size *= 2;
  3798. PackedVector2Array array;
  3799. for (int i = 0; i < array_size; i += 2) {
  3800. array.push_back(Vector2(p_value[i].real, p_value[i + 1].real));
  3801. }
  3802. value = Variant(array);
  3803. } else {
  3804. value = Variant(Vector2(p_value[0].real, p_value[1].real));
  3805. }
  3806. break;
  3807. case ShaderLanguage::TYPE_VEC3:
  3808. if (array_size > 0) {
  3809. array_size *= 3;
  3810. if (p_hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3811. PackedColorArray array;
  3812. for (int i = 0; i < array_size; i += 3) {
  3813. array.push_back(Color(p_value[i].real, p_value[i + 1].real, p_value[i + 2].real));
  3814. }
  3815. value = Variant(array);
  3816. } else {
  3817. PackedVector3Array array;
  3818. for (int i = 0; i < array_size; i += 3) {
  3819. array.push_back(Vector3(p_value[i].real, p_value[i + 1].real, p_value[i + 2].real));
  3820. }
  3821. value = Variant(array);
  3822. }
  3823. } else {
  3824. if (p_hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3825. value = Variant(Color(p_value[0].real, p_value[1].real, p_value[2].real));
  3826. } else {
  3827. value = Variant(Vector3(p_value[0].real, p_value[1].real, p_value[2].real));
  3828. }
  3829. }
  3830. break;
  3831. case ShaderLanguage::TYPE_VEC4:
  3832. if (array_size > 0) {
  3833. array_size *= 4;
  3834. if (p_hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3835. PackedColorArray array;
  3836. for (int i = 0; i < array_size; i += 4) {
  3837. array.push_back(Color(p_value[i].real, p_value[i + 1].real, p_value[i + 2].real, p_value[i + 3].real));
  3838. }
  3839. value = Variant(array);
  3840. } else {
  3841. PackedVector4Array array;
  3842. for (int i = 0; i < array_size; i += 4) {
  3843. array.push_back(Vector4(p_value[i].real, p_value[i + 1].real, p_value[i + 2].real, p_value[i + 3].real));
  3844. }
  3845. value = Variant(array);
  3846. }
  3847. } else {
  3848. if (p_hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3849. value = Variant(Color(p_value[0].real, p_value[1].real, p_value[2].real, p_value[3].real));
  3850. } else {
  3851. value = Variant(Vector4(p_value[0].real, p_value[1].real, p_value[2].real, p_value[3].real));
  3852. }
  3853. }
  3854. break;
  3855. case ShaderLanguage::TYPE_MAT2:
  3856. if (array_size > 0) {
  3857. array_size *= 4;
  3858. PackedFloat32Array array;
  3859. for (int i = 0; i < array_size; i += 4) {
  3860. array.push_back(p_value[i].real);
  3861. array.push_back(p_value[i + 1].real);
  3862. array.push_back(p_value[i + 2].real);
  3863. array.push_back(p_value[i + 3].real);
  3864. }
  3865. value = Variant(array);
  3866. } else {
  3867. value = Variant(Transform2D(p_value[0].real, p_value[2].real, p_value[1].real, p_value[3].real, 0.0, 0.0));
  3868. }
  3869. break;
  3870. case ShaderLanguage::TYPE_MAT3: {
  3871. if (array_size > 0) {
  3872. array_size *= 9;
  3873. PackedFloat32Array array;
  3874. for (int i = 0; i < array_size; i += 9) {
  3875. for (int j = 0; j < 9; j++) {
  3876. array.push_back(p_value[i + j].real);
  3877. }
  3878. }
  3879. value = Variant(array);
  3880. } else {
  3881. Basis p;
  3882. p[0][0] = p_value[0].real;
  3883. p[0][1] = p_value[1].real;
  3884. p[0][2] = p_value[2].real;
  3885. p[1][0] = p_value[3].real;
  3886. p[1][1] = p_value[4].real;
  3887. p[1][2] = p_value[5].real;
  3888. p[2][0] = p_value[6].real;
  3889. p[2][1] = p_value[7].real;
  3890. p[2][2] = p_value[8].real;
  3891. value = Variant(p);
  3892. }
  3893. break;
  3894. }
  3895. case ShaderLanguage::TYPE_MAT4: {
  3896. if (array_size > 0) {
  3897. array_size *= 16;
  3898. PackedFloat32Array array;
  3899. for (int i = 0; i < array_size; i += 16) {
  3900. for (int j = 0; j < 16; j++) {
  3901. array.push_back(p_value[i + j].real);
  3902. }
  3903. }
  3904. value = Variant(array);
  3905. } else {
  3906. Projection p = Projection(Vector4(p_value[0].real, p_value[1].real, p_value[2].real, p_value[3].real),
  3907. Vector4(p_value[4].real, p_value[5].real, p_value[6].real, p_value[7].real),
  3908. Vector4(p_value[8].real, p_value[9].real, p_value[10].real, p_value[11].real),
  3909. Vector4(p_value[12].real, p_value[13].real, p_value[14].real, p_value[15].real));
  3910. value = Variant(p);
  3911. }
  3912. break;
  3913. }
  3914. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  3915. case ShaderLanguage::TYPE_ISAMPLER2D:
  3916. case ShaderLanguage::TYPE_ISAMPLER3D:
  3917. case ShaderLanguage::TYPE_SAMPLER2DARRAY:
  3918. case ShaderLanguage::TYPE_SAMPLER2D:
  3919. case ShaderLanguage::TYPE_SAMPLER3D:
  3920. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  3921. case ShaderLanguage::TYPE_USAMPLER2D:
  3922. case ShaderLanguage::TYPE_USAMPLER3D:
  3923. case ShaderLanguage::TYPE_SAMPLERCUBE:
  3924. case ShaderLanguage::TYPE_SAMPLERCUBEARRAY: {
  3925. // Texture types, likely not relevant here.
  3926. break;
  3927. }
  3928. case ShaderLanguage::TYPE_STRUCT:
  3929. break;
  3930. case ShaderLanguage::TYPE_VOID:
  3931. break;
  3932. case ShaderLanguage::TYPE_MAX:
  3933. break;
  3934. }
  3935. return value;
  3936. }
  3937. return Variant();
  3938. }
  3939. PropertyInfo ShaderLanguage::uniform_to_property_info(const ShaderNode::Uniform &p_uniform) {
  3940. PropertyInfo pi;
  3941. switch (p_uniform.type) {
  3942. case ShaderLanguage::TYPE_VOID:
  3943. pi.type = Variant::NIL;
  3944. break;
  3945. case ShaderLanguage::TYPE_BOOL:
  3946. if (p_uniform.array_size > 0) {
  3947. pi.type = Variant::PACKED_INT32_ARRAY;
  3948. pi.hint = PROPERTY_HINT_TYPE_STRING;
  3949. pi.hint_string = itos(Variant::INT) + "/" + itos(PROPERTY_HINT_FLAGS) + ":" + RTR("On");
  3950. } else {
  3951. pi.type = Variant::BOOL;
  3952. }
  3953. break;
  3954. case ShaderLanguage::TYPE_BVEC2:
  3955. if (p_uniform.array_size > 0) {
  3956. pi.type = Variant::PACKED_INT32_ARRAY;
  3957. pi.hint = PROPERTY_HINT_TYPE_STRING;
  3958. pi.hint_string = itos(Variant::INT) + "/" + itos(PROPERTY_HINT_FLAGS) + ":x,y";
  3959. } else {
  3960. pi.type = Variant::INT;
  3961. pi.hint = PROPERTY_HINT_FLAGS;
  3962. pi.hint_string = "x,y";
  3963. }
  3964. break;
  3965. case ShaderLanguage::TYPE_BVEC3:
  3966. if (p_uniform.array_size > 0) {
  3967. pi.type = Variant::PACKED_INT32_ARRAY;
  3968. pi.hint = PROPERTY_HINT_TYPE_STRING;
  3969. pi.hint_string = itos(Variant::INT) + "/" + itos(PROPERTY_HINT_FLAGS) + ":x,y,z";
  3970. } else {
  3971. pi.type = Variant::INT;
  3972. pi.hint = PROPERTY_HINT_FLAGS;
  3973. pi.hint_string = "x,y,z";
  3974. }
  3975. break;
  3976. case ShaderLanguage::TYPE_BVEC4:
  3977. if (p_uniform.array_size > 0) {
  3978. pi.type = Variant::PACKED_INT32_ARRAY;
  3979. pi.hint = PROPERTY_HINT_TYPE_STRING;
  3980. pi.hint_string = itos(Variant::INT) + "/" + itos(PROPERTY_HINT_FLAGS) + ":x,y,z,w";
  3981. } else {
  3982. pi.type = Variant::INT;
  3983. pi.hint = PROPERTY_HINT_FLAGS;
  3984. pi.hint_string = "x,y,z,w";
  3985. }
  3986. break;
  3987. case ShaderLanguage::TYPE_UINT:
  3988. case ShaderLanguage::TYPE_INT: {
  3989. if (p_uniform.array_size > 0) {
  3990. pi.type = Variant::PACKED_INT32_ARRAY;
  3991. // TODO: Handle range and encoding for for unsigned values.
  3992. } else if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_ENUM) {
  3993. pi.type = Variant::INT;
  3994. pi.hint = PROPERTY_HINT_ENUM;
  3995. String hint_string;
  3996. pi.hint_string = String(",").join(p_uniform.hint_enum_names);
  3997. } else {
  3998. pi.type = Variant::INT;
  3999. pi.hint = PROPERTY_HINT_RANGE;
  4000. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_RANGE) {
  4001. pi.hint_string = rtos(p_uniform.hint_range[0]) + "," + rtos(p_uniform.hint_range[1]) + "," + rtos(p_uniform.hint_range[2]);
  4002. } else if (p_uniform.type == ShaderLanguage::TYPE_UINT) {
  4003. pi.hint_string = "0," + itos(UINT32_MAX);
  4004. } else {
  4005. pi.hint_string = itos(INT32_MIN) + "," + itos(INT32_MAX);
  4006. }
  4007. }
  4008. } break;
  4009. case ShaderLanguage::TYPE_UVEC2:
  4010. case ShaderLanguage::TYPE_IVEC2: {
  4011. if (p_uniform.array_size > 0) {
  4012. pi.type = Variant::PACKED_INT32_ARRAY;
  4013. // TODO: Handle vector pairs?
  4014. } else {
  4015. pi.type = Variant::VECTOR2I;
  4016. }
  4017. } break;
  4018. case ShaderLanguage::TYPE_UVEC3:
  4019. case ShaderLanguage::TYPE_IVEC3: {
  4020. if (p_uniform.array_size > 0) {
  4021. pi.type = Variant::PACKED_INT32_ARRAY;
  4022. // TODO: Handle vector pairs?
  4023. } else {
  4024. pi.type = Variant::VECTOR3I;
  4025. }
  4026. } break;
  4027. case ShaderLanguage::TYPE_UVEC4:
  4028. case ShaderLanguage::TYPE_IVEC4: {
  4029. if (p_uniform.array_size > 0) {
  4030. pi.type = Variant::PACKED_INT32_ARRAY;
  4031. // TODO: Handle vector pairs?
  4032. } else {
  4033. pi.type = Variant::VECTOR4I;
  4034. }
  4035. } break;
  4036. case ShaderLanguage::TYPE_FLOAT: {
  4037. if (p_uniform.array_size > 0) {
  4038. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  4039. } else {
  4040. pi.type = Variant::FLOAT;
  4041. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_RANGE) {
  4042. pi.hint = PROPERTY_HINT_RANGE;
  4043. pi.hint_string = rtos(p_uniform.hint_range[0]) + "," + rtos(p_uniform.hint_range[1]) + "," + rtos(p_uniform.hint_range[2]);
  4044. }
  4045. }
  4046. } break;
  4047. case ShaderLanguage::TYPE_VEC2:
  4048. if (p_uniform.array_size > 0) {
  4049. pi.type = Variant::PACKED_VECTOR2_ARRAY;
  4050. } else {
  4051. pi.type = Variant::VECTOR2;
  4052. }
  4053. break;
  4054. case ShaderLanguage::TYPE_VEC3:
  4055. if (p_uniform.array_size > 0) {
  4056. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  4057. pi.hint = PROPERTY_HINT_COLOR_NO_ALPHA;
  4058. pi.type = Variant::PACKED_COLOR_ARRAY;
  4059. } else {
  4060. pi.type = Variant::PACKED_VECTOR3_ARRAY;
  4061. }
  4062. } else {
  4063. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  4064. pi.hint = PROPERTY_HINT_COLOR_NO_ALPHA;
  4065. pi.type = Variant::COLOR;
  4066. } else {
  4067. pi.type = Variant::VECTOR3;
  4068. }
  4069. }
  4070. break;
  4071. case ShaderLanguage::TYPE_VEC4: {
  4072. if (p_uniform.array_size > 0) {
  4073. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  4074. pi.type = Variant::PACKED_COLOR_ARRAY;
  4075. } else {
  4076. pi.type = Variant::PACKED_VECTOR4_ARRAY;
  4077. }
  4078. } else {
  4079. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  4080. pi.type = Variant::COLOR;
  4081. } else {
  4082. pi.type = Variant::VECTOR4;
  4083. }
  4084. }
  4085. } break;
  4086. case ShaderLanguage::TYPE_MAT2:
  4087. if (p_uniform.array_size > 0) {
  4088. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  4089. } else {
  4090. pi.type = Variant::TRANSFORM2D;
  4091. }
  4092. break;
  4093. case ShaderLanguage::TYPE_MAT3:
  4094. if (p_uniform.array_size > 0) {
  4095. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  4096. } else {
  4097. pi.type = Variant::BASIS;
  4098. }
  4099. break;
  4100. case ShaderLanguage::TYPE_MAT4:
  4101. if (p_uniform.array_size > 0) {
  4102. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  4103. } else {
  4104. pi.type = Variant::PROJECTION;
  4105. }
  4106. break;
  4107. case ShaderLanguage::TYPE_SAMPLER2D:
  4108. case ShaderLanguage::TYPE_ISAMPLER2D:
  4109. case ShaderLanguage::TYPE_USAMPLER2D: {
  4110. if (p_uniform.array_size > 0) {
  4111. pi.type = Variant::ARRAY;
  4112. pi.hint = PROPERTY_HINT_ARRAY_TYPE;
  4113. pi.hint_string = MAKE_RESOURCE_TYPE_HINT("Texture2D");
  4114. } else {
  4115. pi.type = Variant::OBJECT;
  4116. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  4117. pi.hint_string = "Texture2D";
  4118. }
  4119. } break;
  4120. case ShaderLanguage::TYPE_SAMPLER2DARRAY:
  4121. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  4122. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  4123. case ShaderLanguage::TYPE_SAMPLERCUBE:
  4124. case ShaderLanguage::TYPE_SAMPLERCUBEARRAY: {
  4125. if (p_uniform.array_size > 0) {
  4126. pi.type = Variant::ARRAY;
  4127. pi.hint = PROPERTY_HINT_ARRAY_TYPE;
  4128. pi.hint_string = MAKE_RESOURCE_TYPE_HINT("TextureLayered");
  4129. } else {
  4130. pi.type = Variant::OBJECT;
  4131. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  4132. pi.hint_string = "TextureLayered";
  4133. }
  4134. } break;
  4135. case ShaderLanguage::TYPE_SAMPLER3D:
  4136. case ShaderLanguage::TYPE_ISAMPLER3D:
  4137. case ShaderLanguage::TYPE_USAMPLER3D: {
  4138. if (p_uniform.array_size > 0) {
  4139. pi.type = Variant::ARRAY;
  4140. pi.hint = PROPERTY_HINT_ARRAY_TYPE;
  4141. pi.hint_string = MAKE_RESOURCE_TYPE_HINT("Texture3D");
  4142. } else {
  4143. pi.type = Variant::OBJECT;
  4144. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  4145. pi.hint_string = "Texture3D";
  4146. }
  4147. } break;
  4148. case ShaderLanguage::TYPE_STRUCT: {
  4149. // FIXME: Implement this.
  4150. } break;
  4151. case ShaderLanguage::TYPE_MAX:
  4152. break;
  4153. }
  4154. return pi;
  4155. }
  4156. uint32_t ShaderLanguage::get_datatype_size(ShaderLanguage::DataType p_type) {
  4157. switch (p_type) {
  4158. case TYPE_VOID:
  4159. return 0;
  4160. case TYPE_BOOL:
  4161. return 4;
  4162. case TYPE_BVEC2:
  4163. return 8;
  4164. case TYPE_BVEC3:
  4165. return 12;
  4166. case TYPE_BVEC4:
  4167. return 16;
  4168. case TYPE_INT:
  4169. return 4;
  4170. case TYPE_IVEC2:
  4171. return 8;
  4172. case TYPE_IVEC3:
  4173. return 12;
  4174. case TYPE_IVEC4:
  4175. return 16;
  4176. case TYPE_UINT:
  4177. return 4;
  4178. case TYPE_UVEC2:
  4179. return 8;
  4180. case TYPE_UVEC3:
  4181. return 12;
  4182. case TYPE_UVEC4:
  4183. return 16;
  4184. case TYPE_FLOAT:
  4185. return 4;
  4186. case TYPE_VEC2:
  4187. return 8;
  4188. case TYPE_VEC3:
  4189. return 12;
  4190. case TYPE_VEC4:
  4191. return 16;
  4192. case TYPE_MAT2:
  4193. return 32; // 4 * 4 + 4 * 4
  4194. case TYPE_MAT3:
  4195. return 48; // 4 * 4 + 4 * 4 + 4 * 4
  4196. case TYPE_MAT4:
  4197. return 64;
  4198. case TYPE_SAMPLER2D:
  4199. return 16;
  4200. case TYPE_ISAMPLER2D:
  4201. return 16;
  4202. case TYPE_USAMPLER2D:
  4203. return 16;
  4204. case TYPE_SAMPLER2DARRAY:
  4205. return 16;
  4206. case TYPE_ISAMPLER2DARRAY:
  4207. return 16;
  4208. case TYPE_USAMPLER2DARRAY:
  4209. return 16;
  4210. case TYPE_SAMPLER3D:
  4211. return 16;
  4212. case TYPE_ISAMPLER3D:
  4213. return 16;
  4214. case TYPE_USAMPLER3D:
  4215. return 16;
  4216. case TYPE_SAMPLERCUBE:
  4217. return 16;
  4218. case TYPE_SAMPLERCUBEARRAY:
  4219. return 16;
  4220. case TYPE_STRUCT:
  4221. return 0;
  4222. case TYPE_MAX: {
  4223. ERR_FAIL_V(0);
  4224. };
  4225. }
  4226. ERR_FAIL_V(0);
  4227. }
  4228. uint32_t ShaderLanguage::get_datatype_component_count(ShaderLanguage::DataType p_type) {
  4229. switch (p_type) {
  4230. case TYPE_BOOL:
  4231. return 1U;
  4232. case TYPE_BVEC2:
  4233. return 2U;
  4234. case TYPE_BVEC3:
  4235. return 3U;
  4236. case TYPE_BVEC4:
  4237. return 4U;
  4238. case TYPE_INT:
  4239. return 1U;
  4240. case TYPE_IVEC2:
  4241. return 2U;
  4242. case TYPE_IVEC3:
  4243. return 3U;
  4244. case TYPE_IVEC4:
  4245. return 4U;
  4246. case TYPE_UINT:
  4247. return 1U;
  4248. case TYPE_UVEC2:
  4249. return 2U;
  4250. case TYPE_UVEC3:
  4251. return 3U;
  4252. case TYPE_UVEC4:
  4253. return 4U;
  4254. case TYPE_FLOAT:
  4255. return 1U;
  4256. case TYPE_VEC2:
  4257. return 2U;
  4258. case TYPE_VEC3:
  4259. return 3U;
  4260. case TYPE_VEC4:
  4261. return 4U;
  4262. case TYPE_MAT2:
  4263. return 4U;
  4264. case TYPE_MAT3:
  4265. return 9U;
  4266. case TYPE_MAT4:
  4267. return 16U;
  4268. default:
  4269. break;
  4270. }
  4271. return 0U;
  4272. }
  4273. void ShaderLanguage::get_keyword_list(List<String> *r_keywords) {
  4274. HashSet<String> kws;
  4275. int idx = 0;
  4276. while (keyword_list[idx].text) {
  4277. kws.insert(keyword_list[idx].text);
  4278. idx++;
  4279. }
  4280. idx = 0;
  4281. while (builtin_func_defs[idx].name) {
  4282. kws.insert(builtin_func_defs[idx].name);
  4283. idx++;
  4284. }
  4285. for (const String &E : kws) {
  4286. r_keywords->push_back(E);
  4287. }
  4288. }
  4289. bool ShaderLanguage::is_control_flow_keyword(String p_keyword) {
  4290. return p_keyword == "break" ||
  4291. p_keyword == "case" ||
  4292. p_keyword == "continue" ||
  4293. p_keyword == "default" ||
  4294. p_keyword == "do" ||
  4295. p_keyword == "else" ||
  4296. p_keyword == "for" ||
  4297. p_keyword == "if" ||
  4298. p_keyword == "return" ||
  4299. p_keyword == "switch" ||
  4300. p_keyword == "while";
  4301. }
  4302. void ShaderLanguage::get_builtin_funcs(List<String> *r_keywords) {
  4303. HashSet<String> kws;
  4304. int idx = 0;
  4305. while (builtin_func_defs[idx].name) {
  4306. kws.insert(builtin_func_defs[idx].name);
  4307. idx++;
  4308. }
  4309. for (const String &E : kws) {
  4310. r_keywords->push_back(E);
  4311. }
  4312. }
  4313. ShaderLanguage::DataType ShaderLanguage::get_scalar_type(DataType p_type) {
  4314. static const DataType scalar_types[] = {
  4315. TYPE_VOID,
  4316. TYPE_BOOL,
  4317. TYPE_BOOL,
  4318. TYPE_BOOL,
  4319. TYPE_BOOL,
  4320. TYPE_INT,
  4321. TYPE_INT,
  4322. TYPE_INT,
  4323. TYPE_INT,
  4324. TYPE_UINT,
  4325. TYPE_UINT,
  4326. TYPE_UINT,
  4327. TYPE_UINT,
  4328. TYPE_FLOAT,
  4329. TYPE_FLOAT,
  4330. TYPE_FLOAT,
  4331. TYPE_FLOAT,
  4332. TYPE_FLOAT,
  4333. TYPE_FLOAT,
  4334. TYPE_FLOAT,
  4335. TYPE_FLOAT,
  4336. TYPE_INT,
  4337. TYPE_UINT,
  4338. TYPE_FLOAT,
  4339. TYPE_INT,
  4340. TYPE_UINT,
  4341. TYPE_FLOAT,
  4342. TYPE_INT,
  4343. TYPE_UINT,
  4344. TYPE_FLOAT,
  4345. TYPE_FLOAT,
  4346. TYPE_VOID,
  4347. };
  4348. static_assert(sizeof(scalar_types) / sizeof(*scalar_types) == TYPE_MAX);
  4349. return scalar_types[p_type];
  4350. }
  4351. int ShaderLanguage::get_cardinality(DataType p_type) {
  4352. static const int cardinality_table[] = {
  4353. 0,
  4354. 1,
  4355. 2,
  4356. 3,
  4357. 4,
  4358. 1,
  4359. 2,
  4360. 3,
  4361. 4,
  4362. 1,
  4363. 2,
  4364. 3,
  4365. 4,
  4366. 1,
  4367. 2,
  4368. 3,
  4369. 4,
  4370. 4,
  4371. 9,
  4372. 16,
  4373. 1,
  4374. 1,
  4375. 1,
  4376. 1,
  4377. 1,
  4378. 1,
  4379. 1,
  4380. 1,
  4381. 1,
  4382. 1,
  4383. 1,
  4384. 1,
  4385. };
  4386. static_assert(sizeof(cardinality_table) / sizeof(*cardinality_table) == TYPE_MAX);
  4387. return cardinality_table[p_type];
  4388. }
  4389. bool ShaderLanguage::_get_completable_identifier(BlockNode *p_block, CompletionType p_type, StringName &identifier) {
  4390. identifier = StringName();
  4391. TkPos pos = { 0, 0 };
  4392. Token tk = _get_token();
  4393. if (tk.type == TK_IDENTIFIER) {
  4394. identifier = tk.text;
  4395. pos = _get_tkpos();
  4396. tk = _get_token();
  4397. }
  4398. if (tk.type == TK_CURSOR) {
  4399. completion_type = p_type;
  4400. completion_line = tk_line;
  4401. completion_block = p_block;
  4402. pos = _get_tkpos();
  4403. tk = _get_token();
  4404. if (tk.type == TK_IDENTIFIER) {
  4405. identifier = identifier.operator String() + tk.text.operator String();
  4406. } else {
  4407. _set_tkpos(pos);
  4408. }
  4409. return true;
  4410. } else if (identifier != StringName()) {
  4411. _set_tkpos(pos);
  4412. }
  4413. return false;
  4414. }
  4415. bool ShaderLanguage::_is_operator_assign(Operator p_op) const {
  4416. switch (p_op) {
  4417. case OP_ASSIGN:
  4418. case OP_ASSIGN_ADD:
  4419. case OP_ASSIGN_SUB:
  4420. case OP_ASSIGN_MUL:
  4421. case OP_ASSIGN_DIV:
  4422. case OP_ASSIGN_MOD:
  4423. case OP_ASSIGN_SHIFT_LEFT:
  4424. case OP_ASSIGN_SHIFT_RIGHT:
  4425. case OP_ASSIGN_BIT_AND:
  4426. case OP_ASSIGN_BIT_OR:
  4427. case OP_ASSIGN_BIT_XOR:
  4428. return true;
  4429. default:
  4430. return false;
  4431. }
  4432. }
  4433. bool ShaderLanguage::_validate_varying_assign(ShaderNode::Varying &p_varying, String *r_message) {
  4434. if (current_function != "vertex" && current_function != "fragment") {
  4435. *r_message = vformat(RTR("Varying may not be assigned in the '%s' function."), current_function);
  4436. return false;
  4437. }
  4438. switch (p_varying.stage) {
  4439. case ShaderNode::Varying::STAGE_UNKNOWN: // first assign
  4440. if (current_function == varying_function_names.vertex) {
  4441. if (p_varying.type < TYPE_INT) {
  4442. *r_message = vformat(RTR("Varying with '%s' data type may only be assigned in the 'fragment' function."), get_datatype_name(p_varying.type));
  4443. return false;
  4444. }
  4445. p_varying.stage = ShaderNode::Varying::STAGE_VERTEX;
  4446. } else if (current_function == varying_function_names.fragment) {
  4447. p_varying.stage = ShaderNode::Varying::STAGE_FRAGMENT;
  4448. }
  4449. break;
  4450. case ShaderNode::Varying::STAGE_VERTEX_TO_FRAGMENT_LIGHT:
  4451. case ShaderNode::Varying::STAGE_VERTEX:
  4452. if (current_function == varying_function_names.fragment) {
  4453. *r_message = RTR("Varyings which assigned in 'vertex' function may not be reassigned in 'fragment' or 'light'.");
  4454. return false;
  4455. }
  4456. break;
  4457. case ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT:
  4458. case ShaderNode::Varying::STAGE_FRAGMENT:
  4459. if (current_function == varying_function_names.vertex) {
  4460. *r_message = RTR("Varyings which assigned in 'fragment' function may not be reassigned in 'vertex' or 'light'.");
  4461. return false;
  4462. }
  4463. break;
  4464. default:
  4465. break;
  4466. }
  4467. return true;
  4468. }
  4469. bool ShaderLanguage::_check_node_constness(const Node *p_node) const {
  4470. switch (p_node->type) {
  4471. case Node::NODE_TYPE_OPERATOR: {
  4472. const OperatorNode *op_node = static_cast<const OperatorNode *>(p_node);
  4473. for (int i = int(op_node->op == OP_CALL); i < op_node->arguments.size(); i++) {
  4474. if (!_check_node_constness(op_node->arguments[i])) {
  4475. return false;
  4476. }
  4477. }
  4478. } break;
  4479. case Node::NODE_TYPE_CONSTANT:
  4480. break;
  4481. case Node::NODE_TYPE_VARIABLE: {
  4482. const VariableNode *var_node = static_cast<const VariableNode *>(p_node);
  4483. if (!var_node->is_const) {
  4484. return false;
  4485. }
  4486. } break;
  4487. case Node::NODE_TYPE_ARRAY: {
  4488. const ArrayNode *arr_node = static_cast<const ArrayNode *>(p_node);
  4489. if (!arr_node->is_const) {
  4490. return false;
  4491. }
  4492. } break;
  4493. default:
  4494. return false;
  4495. }
  4496. return true;
  4497. }
  4498. bool ShaderLanguage::_check_restricted_func(const StringName &p_name, const StringName &p_current_function) const {
  4499. int idx = 0;
  4500. while (frag_only_func_defs[idx].name) {
  4501. if (StringName(frag_only_func_defs[idx].name) == p_name) {
  4502. if (is_supported_frag_only_funcs) {
  4503. if (p_current_function == "vertex" && stages->has(p_current_function)) {
  4504. return true;
  4505. }
  4506. } else {
  4507. return true;
  4508. }
  4509. break;
  4510. }
  4511. idx++;
  4512. }
  4513. return false;
  4514. }
  4515. bool ShaderLanguage::_validate_restricted_func(const StringName &p_name, const CallInfo *p_func_info, bool p_is_builtin_hint) {
  4516. const bool is_in_restricted_function = p_func_info->name == "vertex";
  4517. // No need to check up the hierarchy if it's a built-in.
  4518. if (!p_is_builtin_hint) {
  4519. for (const CallInfo *func_info : p_func_info->calls) {
  4520. if (is_in_restricted_function && func_info->name != p_name) {
  4521. // Skips check for non-called method.
  4522. continue;
  4523. }
  4524. if (!_validate_restricted_func(p_name, func_info)) {
  4525. return false;
  4526. }
  4527. }
  4528. }
  4529. if (!p_func_info->uses_restricted_items.is_empty()) {
  4530. const Pair<StringName, CallInfo::Item> &first_element = p_func_info->uses_restricted_items.get(0);
  4531. if (first_element.second.type == CallInfo::Item::ITEM_TYPE_VARYING) {
  4532. const ShaderNode::Varying &varying = shader->varyings[first_element.first];
  4533. if (varying.stage == ShaderNode::Varying::STAGE_VERTEX) {
  4534. return true;
  4535. }
  4536. }
  4537. _set_tkpos(first_element.second.pos);
  4538. if (is_in_restricted_function) {
  4539. _set_error(vformat(RTR("'%s' cannot be used within the '%s' processor function."), first_element.first, "vertex"));
  4540. } else {
  4541. _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"));
  4542. }
  4543. return false;
  4544. }
  4545. return true;
  4546. }
  4547. bool ShaderLanguage::_validate_assign(Node *p_node, const FunctionInfo &p_function_info, String *r_message) {
  4548. if (p_node->type == Node::NODE_TYPE_OPERATOR) {
  4549. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  4550. if (op->op == OP_INDEX) {
  4551. return _validate_assign(op->arguments[0], p_function_info, r_message);
  4552. } else if (_is_operator_assign(op->op)) {
  4553. //chained assignment
  4554. return _validate_assign(op->arguments[1], p_function_info, r_message);
  4555. } else if (op->op == OP_CALL) {
  4556. if (r_message) {
  4557. *r_message = RTR("Assignment to function.");
  4558. }
  4559. return false;
  4560. }
  4561. } else if (p_node->type == Node::NODE_TYPE_MEMBER) {
  4562. MemberNode *member = static_cast<MemberNode *>(p_node);
  4563. if (member->has_swizzling_duplicates) {
  4564. if (r_message) {
  4565. *r_message = RTR("Swizzling assignment contains duplicates.");
  4566. }
  4567. return false;
  4568. }
  4569. return _validate_assign(member->owner, p_function_info, r_message);
  4570. } else if (p_node->type == Node::NODE_TYPE_VARIABLE) {
  4571. VariableNode *var = static_cast<VariableNode *>(p_node);
  4572. if (shader->uniforms.has(var->name)) {
  4573. if (r_message) {
  4574. *r_message = RTR("Assignment to uniform.");
  4575. }
  4576. return false;
  4577. }
  4578. if (shader->constants.has(var->name) || var->is_const) {
  4579. if (r_message) {
  4580. *r_message = RTR("Constants cannot be modified.");
  4581. }
  4582. return false;
  4583. }
  4584. if (shader->varyings.has(var->name)) {
  4585. return _validate_varying_assign(shader->varyings[var->name], r_message);
  4586. }
  4587. if (!(p_function_info.built_ins.has(var->name) && p_function_info.built_ins[var->name].constant)) {
  4588. return true;
  4589. }
  4590. } else if (p_node->type == Node::NODE_TYPE_ARRAY) {
  4591. ArrayNode *arr = static_cast<ArrayNode *>(p_node);
  4592. if (shader->constants.has(arr->name) || arr->is_const) {
  4593. if (r_message) {
  4594. *r_message = RTR("Constants cannot be modified.");
  4595. }
  4596. return false;
  4597. }
  4598. return true;
  4599. }
  4600. if (r_message) {
  4601. *r_message = "Assignment to constant expression.";
  4602. }
  4603. return false;
  4604. }
  4605. ShaderLanguage::ShaderNode::Uniform::Hint ShaderLanguage::_sanitize_hint(ShaderNode::Uniform::Hint p_hint) {
  4606. if (p_hint == ShaderNode::Uniform::HINT_SCREEN_TEXTURE ||
  4607. p_hint == ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE ||
  4608. p_hint == ShaderNode::Uniform::HINT_DEPTH_TEXTURE) {
  4609. return p_hint;
  4610. }
  4611. return ShaderNode::Uniform::HINT_NONE;
  4612. }
  4613. 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) {
  4614. for (int i = 0; i < shader->vfunctions.size(); i++) {
  4615. if (shader->vfunctions[i].name == p_name) {
  4616. ERR_FAIL_INDEX_V(p_argument, shader->vfunctions[i].function->arguments.size(), false);
  4617. FunctionNode::Argument *arg = &shader->vfunctions[i].function->arguments.write[p_argument];
  4618. if (arg->tex_builtin_check) {
  4619. _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)));
  4620. return false;
  4621. } else if (arg->tex_argument_check) {
  4622. // Was checked, verify that filter, repeat, and hint are the same.
  4623. if (arg->tex_argument_filter == p_filter && arg->tex_argument_repeat == p_repeat && arg->tex_hint == _sanitize_hint(p_hint)) {
  4624. return true;
  4625. } else {
  4626. _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)));
  4627. return false;
  4628. }
  4629. } else {
  4630. arg->tex_argument_check = true;
  4631. arg->tex_argument_filter = p_filter;
  4632. arg->tex_argument_repeat = p_repeat;
  4633. arg->tex_hint = _sanitize_hint(p_hint);
  4634. for (KeyValue<StringName, HashSet<int>> &E : arg->tex_argument_connect) {
  4635. for (const int &F : E.value) {
  4636. if (!_propagate_function_call_sampler_uniform_settings(E.key, F, p_filter, p_repeat, p_hint)) {
  4637. return false;
  4638. }
  4639. }
  4640. }
  4641. return true;
  4642. }
  4643. }
  4644. }
  4645. ERR_FAIL_V(false); //bug? function not found
  4646. }
  4647. bool ShaderLanguage::_propagate_function_call_sampler_builtin_reference(const StringName &p_name, int p_argument, const StringName &p_builtin) {
  4648. for (int i = 0; i < shader->vfunctions.size(); i++) {
  4649. if (shader->vfunctions[i].name == p_name) {
  4650. ERR_FAIL_INDEX_V(p_argument, shader->vfunctions[i].function->arguments.size(), false);
  4651. FunctionNode::Argument *arg = &shader->vfunctions[i].function->arguments.write[p_argument];
  4652. if (arg->tex_argument_check) {
  4653. _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)));
  4654. return false;
  4655. } else if (arg->tex_builtin_check) {
  4656. //was checked, verify that the built-in is the same
  4657. if (arg->tex_builtin == p_builtin) {
  4658. return true;
  4659. } else {
  4660. _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)));
  4661. return false;
  4662. }
  4663. } else {
  4664. arg->tex_builtin_check = true;
  4665. arg->tex_builtin = p_builtin;
  4666. for (KeyValue<StringName, HashSet<int>> &E : arg->tex_argument_connect) {
  4667. for (const int &F : E.value) {
  4668. if (!_propagate_function_call_sampler_builtin_reference(E.key, F, p_builtin)) {
  4669. return false;
  4670. }
  4671. }
  4672. }
  4673. return true;
  4674. }
  4675. }
  4676. }
  4677. ERR_FAIL_V(false); //bug? function not found
  4678. }
  4679. 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) {
  4680. bool error = false;
  4681. if (r_array_size != nullptr && *r_array_size > 0) {
  4682. error = true;
  4683. }
  4684. if (r_unknown_size != nullptr && *r_unknown_size) {
  4685. error = true;
  4686. }
  4687. if (error) {
  4688. _set_error(vformat(RTR("Array size is already defined.")));
  4689. return ERR_PARSE_ERROR;
  4690. }
  4691. TkPos pos = _get_tkpos();
  4692. Token tk = _get_token();
  4693. if (tk.type == TK_BRACKET_CLOSE) {
  4694. if (p_forbid_unknown_size) {
  4695. _set_error(vformat(RTR("Unknown array size is forbidden in that context.")));
  4696. return ERR_PARSE_ERROR;
  4697. }
  4698. if (r_unknown_size != nullptr) {
  4699. *r_unknown_size = true;
  4700. }
  4701. } else {
  4702. _set_tkpos(pos);
  4703. int array_size = 0;
  4704. Node *expr = _parse_and_reduce_expression(p_block, p_function_info);
  4705. if (expr) {
  4706. Vector<Scalar> values = _get_node_values(p_block, expr);
  4707. if (!values.is_empty()) {
  4708. switch (expr->get_datatype()) {
  4709. case TYPE_INT: {
  4710. array_size = values[0].sint;
  4711. } break;
  4712. case TYPE_UINT: {
  4713. array_size = (int)values[0].uint;
  4714. } break;
  4715. default: {
  4716. } break;
  4717. }
  4718. }
  4719. if (r_size_expression != nullptr) {
  4720. *r_size_expression = expr;
  4721. }
  4722. }
  4723. if (array_size <= 0) {
  4724. _set_error(RTR("Expected a positive integer constant."));
  4725. return ERR_PARSE_ERROR;
  4726. }
  4727. tk = _get_token();
  4728. if (tk.type != TK_BRACKET_CLOSE) {
  4729. _set_expected_error("]");
  4730. return ERR_PARSE_ERROR;
  4731. }
  4732. if (r_array_size != nullptr) {
  4733. *r_array_size = array_size;
  4734. }
  4735. }
  4736. return OK;
  4737. }
  4738. ShaderLanguage::Node *ShaderLanguage::_parse_array_constructor(BlockNode *p_block, const FunctionInfo &p_function_info) {
  4739. DataType type = TYPE_VOID;
  4740. String struct_name = "";
  4741. int array_size = 0;
  4742. bool auto_size = false;
  4743. bool undefined_size = false;
  4744. Token tk = _get_token();
  4745. if (tk.type == TK_CURLY_BRACKET_OPEN) {
  4746. auto_size = true;
  4747. } else {
  4748. if (shader->structs.has(tk.text)) {
  4749. type = TYPE_STRUCT;
  4750. struct_name = tk.text;
  4751. } else {
  4752. if (!is_token_variable_datatype(tk.type)) {
  4753. _set_error(RTR("Invalid data type for the array."));
  4754. return nullptr;
  4755. }
  4756. type = get_token_datatype(tk.type);
  4757. }
  4758. tk = _get_token();
  4759. if (tk.type == TK_BRACKET_OPEN) {
  4760. Error error = _parse_array_size(p_block, p_function_info, false, nullptr, &array_size, &undefined_size);
  4761. if (error != OK) {
  4762. return nullptr;
  4763. }
  4764. tk = _get_token();
  4765. } else {
  4766. _set_expected_error("[");
  4767. return nullptr;
  4768. }
  4769. }
  4770. ArrayConstructNode *an = alloc_node<ArrayConstructNode>();
  4771. if (tk.type == TK_PARENTHESIS_OPEN || auto_size) { // initialization
  4772. int idx = 0;
  4773. while (true) {
  4774. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  4775. if (!n) {
  4776. return nullptr;
  4777. }
  4778. // define type by using the first member
  4779. if (auto_size && idx == 0) {
  4780. type = n->get_datatype();
  4781. if (type == TYPE_STRUCT) {
  4782. struct_name = n->get_datatype_name();
  4783. }
  4784. } else {
  4785. if (!_compare_datatypes(type, struct_name, 0, n->get_datatype(), n->get_datatype_name(), 0)) {
  4786. return nullptr;
  4787. }
  4788. }
  4789. tk = _get_token();
  4790. if (tk.type == TK_COMMA) {
  4791. an->initializer.push_back(n);
  4792. } else if (!auto_size && tk.type == TK_PARENTHESIS_CLOSE) {
  4793. an->initializer.push_back(n);
  4794. break;
  4795. } else if (auto_size && tk.type == TK_CURLY_BRACKET_CLOSE) {
  4796. an->initializer.push_back(n);
  4797. break;
  4798. } else {
  4799. if (auto_size) {
  4800. _set_expected_error("}", ",");
  4801. } else {
  4802. _set_expected_error(")", ",");
  4803. }
  4804. return nullptr;
  4805. }
  4806. idx++;
  4807. }
  4808. if (!auto_size && !undefined_size && an->initializer.size() != array_size) {
  4809. _set_error(vformat(RTR("Array size mismatch. Expected %d elements (found %d)."), array_size, an->initializer.size()));
  4810. return nullptr;
  4811. }
  4812. } else {
  4813. _set_error(RTR("Expected array initialization."));
  4814. return nullptr;
  4815. }
  4816. an->datatype = type;
  4817. an->struct_name = struct_name;
  4818. return an;
  4819. }
  4820. 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) {
  4821. DataType type = TYPE_VOID;
  4822. String struct_name = "";
  4823. int array_size = 0;
  4824. bool auto_size = false;
  4825. TkPos prev_pos = _get_tkpos();
  4826. Token tk = _get_token();
  4827. if (tk.type == TK_CURLY_BRACKET_OPEN) {
  4828. auto_size = true;
  4829. } else {
  4830. if (shader->structs.has(tk.text)) {
  4831. type = TYPE_STRUCT;
  4832. struct_name = tk.text;
  4833. } else {
  4834. if (!is_token_variable_datatype(tk.type)) {
  4835. _set_tkpos(prev_pos);
  4836. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  4837. if (!n) {
  4838. _set_error(RTR("Invalid data type for the array."));
  4839. return nullptr;
  4840. }
  4841. if (!_compare_datatypes(p_type, p_struct_name, p_array_size, n->get_datatype(), n->get_datatype_name(), n->get_array_size())) {
  4842. return nullptr;
  4843. }
  4844. return n;
  4845. }
  4846. type = get_token_datatype(tk.type);
  4847. }
  4848. tk = _get_token();
  4849. if (tk.type == TK_BRACKET_OPEN) {
  4850. bool is_unknown_size = false;
  4851. Error error = _parse_array_size(p_block, p_function_info, false, nullptr, &array_size, &is_unknown_size);
  4852. if (error != OK) {
  4853. return nullptr;
  4854. }
  4855. if (is_unknown_size) {
  4856. array_size = p_array_size;
  4857. }
  4858. tk = _get_token();
  4859. } else {
  4860. _set_expected_error("[");
  4861. return nullptr;
  4862. }
  4863. if (type != p_type || struct_name != p_struct_name || array_size != p_array_size) {
  4864. String from;
  4865. if (type == TYPE_STRUCT) {
  4866. from += struct_name;
  4867. } else {
  4868. from += get_datatype_name(type);
  4869. }
  4870. from += "[";
  4871. from += itos(array_size);
  4872. from += "]'";
  4873. String to;
  4874. if (type == TYPE_STRUCT) {
  4875. to += p_struct_name;
  4876. } else {
  4877. to += get_datatype_name(p_type);
  4878. }
  4879. to += "[";
  4880. to += itos(p_array_size);
  4881. to += "]'";
  4882. _set_error(vformat(RTR("Cannot convert from '%s' to '%s'."), from, to));
  4883. return nullptr;
  4884. }
  4885. }
  4886. ArrayConstructNode *an = alloc_node<ArrayConstructNode>();
  4887. an->datatype = p_type;
  4888. an->struct_name = p_struct_name;
  4889. if (tk.type == TK_PARENTHESIS_OPEN || auto_size) { // initialization
  4890. while (true) {
  4891. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  4892. if (!n) {
  4893. return nullptr;
  4894. }
  4895. if (!_compare_datatypes(p_type, p_struct_name, 0, n->get_datatype(), n->get_datatype_name(), n->get_array_size())) {
  4896. return nullptr;
  4897. }
  4898. tk = _get_token();
  4899. if (tk.type == TK_COMMA) {
  4900. an->initializer.push_back(n);
  4901. } else if (!auto_size && tk.type == TK_PARENTHESIS_CLOSE) {
  4902. an->initializer.push_back(n);
  4903. break;
  4904. } else if (auto_size && tk.type == TK_CURLY_BRACKET_CLOSE) {
  4905. an->initializer.push_back(n);
  4906. break;
  4907. } else {
  4908. if (auto_size) {
  4909. _set_expected_error("}", ",");
  4910. } else {
  4911. _set_expected_error(")", ",");
  4912. }
  4913. return nullptr;
  4914. }
  4915. }
  4916. if (an->initializer.size() != p_array_size) {
  4917. _set_error(vformat(RTR("Array size mismatch. Expected %d elements (found %d)."), p_array_size, an->initializer.size()));
  4918. return nullptr;
  4919. }
  4920. } else {
  4921. _set_error(RTR("Expected array initialization."));
  4922. return nullptr;
  4923. }
  4924. return an;
  4925. }
  4926. ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, const FunctionInfo &p_function_info, const ExpressionInfo *p_previous_expression_info) {
  4927. Vector<Expression> expression;
  4928. //Vector<TokenType> operators;
  4929. #ifdef DEBUG_ENABLED
  4930. bool check_position_write = check_warnings && HAS_WARNING(ShaderWarning::MAGIC_POSITION_WRITE_FLAG);
  4931. check_position_write = check_position_write && String(shader_type_identifier) == "spatial" && current_function == "vertex";
  4932. #endif
  4933. while (true) {
  4934. Node *expr = nullptr;
  4935. TkPos prepos = _get_tkpos();
  4936. Token tk = _get_token();
  4937. TkPos pos = _get_tkpos();
  4938. bool is_const = false;
  4939. if (tk.type == TK_PARENTHESIS_OPEN) {
  4940. //handle subexpression
  4941. expr = _parse_and_reduce_expression(p_block, p_function_info);
  4942. if (!expr) {
  4943. return nullptr;
  4944. }
  4945. tk = _get_token();
  4946. if (tk.type != TK_PARENTHESIS_CLOSE) {
  4947. _set_error(RTR("Expected ')' in expression."));
  4948. return nullptr;
  4949. }
  4950. } else if (tk.type == TK_FLOAT_CONSTANT) {
  4951. ConstantNode *constant = alloc_node<ConstantNode>();
  4952. Scalar v;
  4953. v.real = tk.constant;
  4954. constant->values.push_back(v);
  4955. constant->datatype = TYPE_FLOAT;
  4956. expr = constant;
  4957. } else if (tk.type == TK_INT_CONSTANT) {
  4958. ConstantNode *constant = alloc_node<ConstantNode>();
  4959. Scalar v;
  4960. v.sint = tk.constant;
  4961. constant->values.push_back(v);
  4962. constant->datatype = TYPE_INT;
  4963. expr = constant;
  4964. } else if (tk.type == TK_UINT_CONSTANT) {
  4965. ConstantNode *constant = alloc_node<ConstantNode>();
  4966. Scalar v;
  4967. v.uint = tk.constant;
  4968. constant->values.push_back(v);
  4969. constant->datatype = TYPE_UINT;
  4970. expr = constant;
  4971. } else if (tk.type == TK_TRUE) {
  4972. //handle true constant
  4973. ConstantNode *constant = alloc_node<ConstantNode>();
  4974. Scalar v;
  4975. v.boolean = true;
  4976. constant->values.push_back(v);
  4977. constant->datatype = TYPE_BOOL;
  4978. expr = constant;
  4979. } else if (tk.type == TK_FALSE) {
  4980. //handle false constant
  4981. ConstantNode *constant = alloc_node<ConstantNode>();
  4982. Scalar v;
  4983. v.boolean = false;
  4984. constant->values.push_back(v);
  4985. constant->datatype = TYPE_BOOL;
  4986. expr = constant;
  4987. } else if (tk.type == TK_TYPE_VOID) {
  4988. //make sure void is not used in expression
  4989. _set_error(RTR("Void value not allowed in expression."));
  4990. return nullptr;
  4991. } else if (is_token_nonvoid_datatype(tk.type) || tk.type == TK_CURLY_BRACKET_OPEN) {
  4992. if (tk.type == TK_CURLY_BRACKET_OPEN) {
  4993. //array constructor
  4994. _set_tkpos(prepos);
  4995. expr = _parse_array_constructor(p_block, p_function_info);
  4996. } else {
  4997. DataType datatype;
  4998. DataPrecision precision = PRECISION_DEFAULT;
  4999. if (is_token_precision(tk.type)) {
  5000. precision = get_token_precision(tk.type);
  5001. tk = _get_token();
  5002. }
  5003. datatype = get_token_datatype(tk.type);
  5004. if (precision != PRECISION_DEFAULT && _validate_precision(datatype, precision) != OK) {
  5005. return nullptr;
  5006. }
  5007. tk = _get_token();
  5008. if (tk.type == TK_BRACKET_OPEN) {
  5009. //array constructor
  5010. _set_tkpos(prepos);
  5011. expr = _parse_array_constructor(p_block, p_function_info);
  5012. } else {
  5013. if (tk.type != TK_PARENTHESIS_OPEN) {
  5014. _set_error(RTR("Expected '(' after the type name."));
  5015. return nullptr;
  5016. }
  5017. //basic type constructor
  5018. OperatorNode *func = alloc_node<OperatorNode>();
  5019. func->op = OP_CONSTRUCT;
  5020. if (precision != PRECISION_DEFAULT) {
  5021. func->return_precision_cache = precision;
  5022. }
  5023. VariableNode *funcname = alloc_node<VariableNode>();
  5024. funcname->name = get_datatype_name(datatype);
  5025. func->arguments.push_back(funcname);
  5026. int carg = -1;
  5027. bool ok = _parse_function_arguments(p_block, p_function_info, func, &carg);
  5028. if (carg >= 0) {
  5029. completion_type = COMPLETION_CALL_ARGUMENTS;
  5030. completion_line = tk_line;
  5031. completion_block = p_block;
  5032. completion_function = funcname->name;
  5033. completion_argument = carg;
  5034. }
  5035. if (!ok) {
  5036. return nullptr;
  5037. }
  5038. if (!_validate_function_call(p_block, p_function_info, func, &func->return_cache, &func->struct_name)) {
  5039. _set_error(vformat(RTR("No matching constructor found for: '%s'."), String(funcname->name)));
  5040. return nullptr;
  5041. }
  5042. expr = _reduce_expression(p_block, func);
  5043. }
  5044. }
  5045. } else if (tk.type == TK_IDENTIFIER) {
  5046. _set_tkpos(prepos);
  5047. StringName identifier;
  5048. StructNode *pstruct = nullptr;
  5049. bool struct_init = false;
  5050. _get_completable_identifier(p_block, COMPLETION_IDENTIFIER, identifier);
  5051. if (shader->structs.has(identifier)) {
  5052. pstruct = shader->structs[identifier].shader_struct;
  5053. struct_init = true;
  5054. }
  5055. tk = _get_token();
  5056. if (tk.type == TK_PARENTHESIS_OPEN) {
  5057. if (struct_init) { //a struct constructor
  5058. const StringName &name = identifier;
  5059. OperatorNode *func = alloc_node<OperatorNode>();
  5060. func->op = OP_STRUCT;
  5061. func->struct_name = name;
  5062. func->return_cache = TYPE_STRUCT;
  5063. VariableNode *funcname = alloc_node<VariableNode>();
  5064. funcname->name = name;
  5065. func->arguments.push_back(funcname);
  5066. for (List<ShaderLanguage::MemberNode *>::Element *E = pstruct->members.front(); E; E = E->next()) {
  5067. Node *nexpr;
  5068. if (E->get()->array_size != 0) {
  5069. nexpr = _parse_array_constructor(p_block, p_function_info, E->get()->get_datatype(), E->get()->struct_name, E->get()->array_size);
  5070. if (!nexpr) {
  5071. return nullptr;
  5072. }
  5073. } else {
  5074. nexpr = _parse_and_reduce_expression(p_block, p_function_info);
  5075. if (!nexpr) {
  5076. return nullptr;
  5077. }
  5078. if (!_compare_datatypes_in_nodes(E->get(), nexpr)) {
  5079. return nullptr;
  5080. }
  5081. }
  5082. if (E->next()) {
  5083. tk = _get_token();
  5084. if (tk.type != TK_COMMA) {
  5085. _set_expected_error(",");
  5086. return nullptr;
  5087. }
  5088. }
  5089. func->arguments.push_back(nexpr);
  5090. }
  5091. tk = _get_token();
  5092. if (tk.type != TK_PARENTHESIS_CLOSE) {
  5093. _set_expected_error(")");
  5094. return nullptr;
  5095. }
  5096. expr = func;
  5097. } else { //a function call
  5098. // Non-builtin function call is forbidden for constant declaration.
  5099. if (is_const_decl) {
  5100. if (shader->functions.has(identifier)) {
  5101. _set_error(RTR("Expected constant expression."));
  5102. return nullptr;
  5103. }
  5104. }
  5105. const StringName &name = identifier;
  5106. if (name != current_function) { // Recursion is not allowed.
  5107. // Register call.
  5108. if (calls_info.has(name)) {
  5109. calls_info[current_function].calls.push_back(&calls_info[name]);
  5110. }
  5111. int idx = 0;
  5112. bool is_builtin = false;
  5113. while (frag_only_func_defs[idx].name) {
  5114. if (frag_only_func_defs[idx].name == name) {
  5115. // If a built-in function not found for the current shader type, then it shouldn't be parsed further.
  5116. if (!is_supported_frag_only_funcs) {
  5117. _set_error(vformat(RTR("Built-in function '%s' is not supported for the '%s' shader type."), name, shader_type_identifier));
  5118. return nullptr;
  5119. }
  5120. // Register usage of the restricted function.
  5121. calls_info[current_function].uses_restricted_items.push_back(Pair<StringName, CallInfo::Item>(name, CallInfo::Item(CallInfo::Item::ITEM_TYPE_BUILTIN, _get_tkpos())));
  5122. is_builtin = true;
  5123. break;
  5124. }
  5125. idx++;
  5126. }
  5127. // Recursively checks for the restricted function call.
  5128. if (is_supported_frag_only_funcs && current_function == "vertex" && stages->has(current_function) && !_validate_restricted_func(name, &calls_info[current_function], is_builtin)) {
  5129. return nullptr;
  5130. }
  5131. }
  5132. OperatorNode *func = alloc_node<OperatorNode>();
  5133. func->op = OP_CALL;
  5134. VariableNode *funcname = alloc_node<VariableNode>();
  5135. funcname->name = name;
  5136. func->arguments.push_back(funcname);
  5137. int carg = -1;
  5138. bool ok = _parse_function_arguments(p_block, p_function_info, func, &carg);
  5139. // Check if block has a variable with the same name as function to prevent shader crash.
  5140. ShaderLanguage::BlockNode *bnode = p_block;
  5141. while (bnode) {
  5142. if (bnode->variables.has(name)) {
  5143. _set_error(RTR("Expected a function name."));
  5144. return nullptr;
  5145. }
  5146. bnode = bnode->parent_block;
  5147. }
  5148. //test if function was parsed first
  5149. int function_index = -1;
  5150. for (int i = 0; i < shader->vfunctions.size(); i++) {
  5151. if (shader->vfunctions[i].name == name) {
  5152. //add to current function as dependency
  5153. for (int j = 0; j < shader->vfunctions.size(); j++) {
  5154. if (shader->vfunctions[j].name == current_function) {
  5155. shader->vfunctions.write[j].uses_function.insert(name);
  5156. break;
  5157. }
  5158. }
  5159. //see if texture arguments must connect
  5160. function_index = i;
  5161. break;
  5162. }
  5163. }
  5164. if (carg >= 0) {
  5165. completion_type = COMPLETION_CALL_ARGUMENTS;
  5166. completion_line = tk_line;
  5167. completion_block = p_block;
  5168. completion_function = funcname->name;
  5169. completion_argument = carg;
  5170. }
  5171. if (!ok) {
  5172. return nullptr;
  5173. }
  5174. bool is_custom_func = false;
  5175. if (!_validate_function_call(p_block, p_function_info, func, &func->return_cache, &func->struct_name, &is_custom_func)) {
  5176. _set_error(vformat(RTR("No matching function found for: '%s'."), String(funcname->name)));
  5177. return nullptr;
  5178. }
  5179. completion_class = TAG_GLOBAL; // reset sub-class
  5180. if (function_index >= 0) {
  5181. //connect texture arguments, so we can cache in the
  5182. //argument what type of filter and repeat to use
  5183. FunctionNode *call_function = shader->vfunctions[function_index].function;
  5184. if (call_function) {
  5185. func->return_cache = call_function->get_datatype();
  5186. func->struct_name = call_function->get_datatype_name();
  5187. func->return_array_size = call_function->get_array_size();
  5188. //get current base function
  5189. FunctionNode *base_function = nullptr;
  5190. {
  5191. BlockNode *b = p_block;
  5192. while (b) {
  5193. if (b->parent_function) {
  5194. base_function = b->parent_function;
  5195. break;
  5196. } else {
  5197. b = b->parent_block;
  5198. }
  5199. }
  5200. }
  5201. ERR_FAIL_NULL_V(base_function, nullptr); // Bug, wtf.
  5202. for (int i = 0; i < call_function->arguments.size(); i++) {
  5203. int argidx = i + 1;
  5204. if (argidx < func->arguments.size()) {
  5205. bool error = false;
  5206. Node *n = func->arguments[argidx];
  5207. ArgumentQualifier arg_qual = call_function->arguments[i].qualifier;
  5208. bool is_out_arg = arg_qual != ArgumentQualifier::ARGUMENT_QUALIFIER_IN;
  5209. if (n->type == Node::NODE_TYPE_VARIABLE || n->type == Node::NODE_TYPE_ARRAY) {
  5210. StringName varname;
  5211. if (n->type == Node::NODE_TYPE_VARIABLE) {
  5212. VariableNode *vn = static_cast<VariableNode *>(n);
  5213. varname = vn->name;
  5214. } else { // TYPE_ARRAY
  5215. ArrayNode *an = static_cast<ArrayNode *>(n);
  5216. varname = an->name;
  5217. }
  5218. if (shader->varyings.has(varname)) {
  5219. switch (shader->varyings[varname].stage) {
  5220. case ShaderNode::Varying::STAGE_UNKNOWN:
  5221. if (is_out_arg) {
  5222. error = true;
  5223. }
  5224. break;
  5225. case ShaderNode::Varying::STAGE_VERTEX_TO_FRAGMENT_LIGHT:
  5226. [[fallthrough]];
  5227. case ShaderNode::Varying::STAGE_VERTEX:
  5228. if (is_out_arg && current_function != varying_function_names.vertex) { // inout/out
  5229. error = true;
  5230. }
  5231. break;
  5232. case ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT:
  5233. [[fallthrough]];
  5234. case ShaderNode::Varying::STAGE_FRAGMENT:
  5235. if (!is_out_arg) {
  5236. if (current_function != varying_function_names.fragment && current_function != varying_function_names.light) {
  5237. error = true;
  5238. }
  5239. } else if (current_function != varying_function_names.fragment) { // inout/out
  5240. error = true;
  5241. }
  5242. break;
  5243. default:
  5244. break;
  5245. }
  5246. if (error) {
  5247. _set_error(vformat(RTR("Varying '%s' cannot be passed for the '%s' parameter in that context."), varname, _get_qualifier_str(arg_qual)));
  5248. return nullptr;
  5249. }
  5250. }
  5251. }
  5252. bool is_const_arg = call_function->arguments[i].is_const;
  5253. if (is_const_arg || is_out_arg) {
  5254. StringName varname;
  5255. if (n->type == Node::NODE_TYPE_CONSTANT || n->type == Node::NODE_TYPE_OPERATOR || n->type == Node::NODE_TYPE_ARRAY_CONSTRUCT) {
  5256. if (!is_const_arg) {
  5257. error = true;
  5258. }
  5259. } else if (n->type == Node::NODE_TYPE_ARRAY) {
  5260. ArrayNode *an = static_cast<ArrayNode *>(n);
  5261. if (!is_const_arg && (an->call_expression != nullptr || an->is_const)) {
  5262. error = true;
  5263. }
  5264. varname = an->name;
  5265. } else if (n->type == Node::NODE_TYPE_VARIABLE) {
  5266. VariableNode *vn = static_cast<VariableNode *>(n);
  5267. if (vn->is_const && !is_const_arg) {
  5268. error = true;
  5269. }
  5270. varname = vn->name;
  5271. } else if (n->type == Node::NODE_TYPE_MEMBER) {
  5272. MemberNode *mn = static_cast<MemberNode *>(n);
  5273. if (mn->basetype_const && is_out_arg) {
  5274. error = true;
  5275. }
  5276. }
  5277. if (!error && varname != StringName()) {
  5278. if (shader->constants.has(varname)) {
  5279. error = true;
  5280. } else if (shader->uniforms.has(varname)) {
  5281. error = true;
  5282. } else if (p_function_info.built_ins.has(varname)) {
  5283. BuiltInInfo info = p_function_info.built_ins[varname];
  5284. if (info.constant) {
  5285. error = true;
  5286. }
  5287. }
  5288. }
  5289. if (error) {
  5290. _set_error(vformat(RTR("A constant value cannot be passed for the '%s' parameter."), _get_qualifier_str(arg_qual)));
  5291. return nullptr;
  5292. }
  5293. }
  5294. if (is_sampler_type(call_function->arguments[i].type)) {
  5295. // Let's see where our argument comes from.
  5296. StringName varname;
  5297. if (n->type == Node::NODE_TYPE_VARIABLE) {
  5298. VariableNode *vn = static_cast<VariableNode *>(n);
  5299. varname = vn->name;
  5300. } else if (n->type == Node::NODE_TYPE_ARRAY) {
  5301. ArrayNode *an = static_cast<ArrayNode *>(n);
  5302. varname = an->name;
  5303. }
  5304. if (shader->uniforms.has(varname)) {
  5305. //being sampler, this either comes from a uniform
  5306. ShaderNode::Uniform *u = &shader->uniforms[varname];
  5307. ERR_CONTINUE(u->type != call_function->arguments[i].type); //this should have been validated previously
  5308. if (RendererCompositor::get_singleton()->is_xr_enabled() && is_custom_func) {
  5309. ShaderNode::Uniform::Hint hint = u->hint;
  5310. if (hint == ShaderNode::Uniform::HINT_DEPTH_TEXTURE || hint == ShaderNode::Uniform::HINT_SCREEN_TEXTURE || hint == ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE) {
  5311. _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)));
  5312. return nullptr;
  5313. }
  5314. }
  5315. //propagate
  5316. if (!_propagate_function_call_sampler_uniform_settings(name, i, u->filter, u->repeat, u->hint)) {
  5317. return nullptr;
  5318. }
  5319. } else if (p_function_info.built_ins.has(varname)) {
  5320. //a built-in
  5321. if (!_propagate_function_call_sampler_builtin_reference(name, i, varname)) {
  5322. return nullptr;
  5323. }
  5324. } else {
  5325. //or this comes from an argument, but nothing else can be a sampler
  5326. bool found = false;
  5327. for (int j = 0; j < base_function->arguments.size(); j++) {
  5328. if (base_function->arguments[j].name == varname) {
  5329. if (!base_function->arguments[j].tex_argument_connect.has(call_function->name)) {
  5330. base_function->arguments.write[j].tex_argument_connect[call_function->name] = HashSet<int>();
  5331. }
  5332. base_function->arguments.write[j].tex_argument_connect[call_function->name].insert(i);
  5333. found = true;
  5334. break;
  5335. }
  5336. }
  5337. ERR_CONTINUE(!found);
  5338. }
  5339. }
  5340. } else {
  5341. break;
  5342. }
  5343. }
  5344. }
  5345. }
  5346. expr = func;
  5347. #ifdef DEBUG_ENABLED
  5348. if (check_warnings) {
  5349. StringName func_name;
  5350. if (p_block && p_block->parent_function) {
  5351. func_name = p_block->parent_function->name;
  5352. }
  5353. _parse_used_identifier(name, IdentifierType::IDENTIFIER_FUNCTION, func_name);
  5354. }
  5355. #endif // DEBUG_ENABLED
  5356. }
  5357. } else {
  5358. //an identifier
  5359. last_name = identifier;
  5360. last_type = IDENTIFIER_MAX;
  5361. _set_tkpos(pos);
  5362. DataType data_type = TYPE_MAX;
  5363. IdentifierType ident_type = IDENTIFIER_MAX;
  5364. int array_size = 0;
  5365. StringName struct_name;
  5366. bool is_local = false;
  5367. if (p_block && p_block->block_tag != SubClassTag::TAG_GLOBAL) {
  5368. int idx = 0;
  5369. bool found = false;
  5370. while (builtin_func_defs[idx].name) {
  5371. if (builtin_func_defs[idx].tag == p_block->block_tag && builtin_func_defs[idx].name == identifier) {
  5372. found = true;
  5373. break;
  5374. }
  5375. idx++;
  5376. }
  5377. if (!found) {
  5378. _set_error(vformat(RTR("Unknown identifier in expression: '%s'."), String(identifier)));
  5379. return nullptr;
  5380. }
  5381. } else {
  5382. if (!_find_identifier(p_block, false, p_function_info, identifier, &data_type, &ident_type, &is_const, &array_size, &struct_name)) {
  5383. if (identifier == "SCREEN_TEXTURE" || identifier == "DEPTH_TEXTURE" || identifier == "NORMAL_ROUGHNESS_TEXTURE") {
  5384. String name = String(identifier);
  5385. String name_lower = name.to_lower();
  5386. _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));
  5387. return nullptr;
  5388. }
  5389. _set_error(vformat(RTR("Unknown identifier in expression: '%s'."), String(identifier)));
  5390. return nullptr;
  5391. }
  5392. if (is_const_decl && !is_const) {
  5393. _set_error(RTR("Expected constant expression."));
  5394. return nullptr;
  5395. }
  5396. if (ident_type == IDENTIFIER_VARYING) {
  5397. TkPos prev_pos = _get_tkpos();
  5398. Token next_token = _get_token();
  5399. // An array of varyings.
  5400. if (next_token.type == TK_BRACKET_OPEN) {
  5401. _get_token(); // Pass constant.
  5402. _get_token(); // Pass TK_BRACKET_CLOSE.
  5403. next_token = _get_token();
  5404. }
  5405. _set_tkpos(prev_pos);
  5406. ShaderNode::Varying &var = shader->varyings[identifier];
  5407. calls_info[current_function].uses_restricted_items.push_back(Pair<StringName, CallInfo::Item>(identifier, CallInfo::Item(CallInfo::Item::ITEM_TYPE_VARYING, prev_pos)));
  5408. String error;
  5409. if (is_token_operator_assign(next_token.type)) {
  5410. if (!_validate_varying_assign(shader->varyings[identifier], &error)) {
  5411. _set_error(error);
  5412. return nullptr;
  5413. }
  5414. } else {
  5415. switch (var.stage) {
  5416. case ShaderNode::Varying::STAGE_UNKNOWN: {
  5417. if (var.type < TYPE_INT) {
  5418. if (current_function == varying_function_names.vertex) {
  5419. _set_error(vformat(RTR("Varying with '%s' data type may only be used in the 'fragment' function."), get_datatype_name(var.type)));
  5420. } else {
  5421. _set_error(vformat(RTR("Varying '%s' must be assigned in the 'fragment' function first."), identifier));
  5422. }
  5423. return nullptr;
  5424. }
  5425. } break;
  5426. case ShaderNode::Varying::STAGE_VERTEX:
  5427. if (current_function == varying_function_names.fragment || current_function == varying_function_names.light) {
  5428. var.stage = ShaderNode::Varying::STAGE_VERTEX_TO_FRAGMENT_LIGHT;
  5429. }
  5430. break;
  5431. case ShaderNode::Varying::STAGE_FRAGMENT:
  5432. if (current_function == varying_function_names.light) {
  5433. var.stage = ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT;
  5434. }
  5435. break;
  5436. default:
  5437. break;
  5438. }
  5439. }
  5440. if ((var.stage != ShaderNode::Varying::STAGE_FRAGMENT && var.stage != ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT) && var.type < TYPE_FLOAT && var.interpolation != INTERPOLATION_FLAT) {
  5441. _set_tkpos(var.tkpos);
  5442. _set_error(RTR("Varying with integer data type must be declared with `flat` interpolation qualifier."));
  5443. return nullptr;
  5444. }
  5445. }
  5446. if (ident_type == IDENTIFIER_FUNCTION) {
  5447. _set_error(vformat(RTR("Can't use function as identifier: '%s'."), String(identifier)));
  5448. return nullptr;
  5449. }
  5450. #ifdef DEBUG_ENABLED
  5451. if (check_position_write && ident_type == IDENTIFIER_BUILTIN_VAR) {
  5452. if (String(identifier) == "POSITION") {
  5453. // Check if the user wrote "POSITION = vec4(VERTEX," and warn if they did.
  5454. TkPos prev_pos = _get_tkpos();
  5455. if (_get_token().type == TK_OP_ASSIGN &&
  5456. _get_token().type == TK_TYPE_VEC4 &&
  5457. _get_token().type == TK_PARENTHESIS_OPEN &&
  5458. _get_token().text == "VERTEX" &&
  5459. _get_token().type == TK_COMMA) {
  5460. _add_line_warning(ShaderWarning::MAGIC_POSITION_WRITE);
  5461. }
  5462. // Reset the position so compiling can continue as normal.
  5463. _set_tkpos(prev_pos);
  5464. }
  5465. }
  5466. #endif
  5467. if (is_const) {
  5468. last_type = IDENTIFIER_CONSTANT;
  5469. } else {
  5470. last_type = ident_type;
  5471. }
  5472. is_local = ident_type == IDENTIFIER_LOCAL_VAR || ident_type == IDENTIFIER_FUNCTION_ARGUMENT;
  5473. }
  5474. Node *index_expression = nullptr;
  5475. Node *call_expression = nullptr;
  5476. Node *assign_expression = nullptr;
  5477. if (array_size > 0) {
  5478. prepos = _get_tkpos();
  5479. tk = _get_token();
  5480. if (tk.type == TK_OP_ASSIGN) {
  5481. if (is_const) {
  5482. _set_error(RTR("Constants cannot be modified."));
  5483. return nullptr;
  5484. }
  5485. assign_expression = _parse_array_constructor(p_block, p_function_info, data_type, struct_name, array_size);
  5486. if (!assign_expression) {
  5487. return nullptr;
  5488. }
  5489. } else if (tk.type == TK_PERIOD) {
  5490. completion_class = TAG_ARRAY;
  5491. if (p_block != nullptr) {
  5492. p_block->block_tag = SubClassTag::TAG_ARRAY;
  5493. }
  5494. call_expression = _parse_and_reduce_expression(p_block, p_function_info);
  5495. if (p_block != nullptr) {
  5496. p_block->block_tag = SubClassTag::TAG_GLOBAL;
  5497. }
  5498. if (!call_expression) {
  5499. return nullptr;
  5500. }
  5501. } else if (tk.type == TK_BRACKET_OPEN) { // indexing
  5502. index_expression = _parse_and_reduce_expression(p_block, p_function_info);
  5503. if (!index_expression) {
  5504. return nullptr;
  5505. }
  5506. if (index_expression->get_array_size() != 0 || (index_expression->get_datatype() != TYPE_INT && index_expression->get_datatype() != TYPE_UINT)) {
  5507. _set_error(RTR("Only integer expressions are allowed for indexing."));
  5508. return nullptr;
  5509. }
  5510. if (index_expression->type == Node::NODE_TYPE_CONSTANT) {
  5511. ConstantNode *cnode = static_cast<ConstantNode *>(index_expression);
  5512. if (cnode) {
  5513. if (!cnode->values.is_empty()) {
  5514. int value = cnode->values[0].sint;
  5515. if (value < 0 || value >= array_size) {
  5516. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), value, 0, array_size - 1));
  5517. return nullptr;
  5518. }
  5519. }
  5520. }
  5521. }
  5522. tk = _get_token();
  5523. if (tk.type != TK_BRACKET_CLOSE) {
  5524. _set_expected_error("]");
  5525. return nullptr;
  5526. }
  5527. } else {
  5528. _set_tkpos(prepos);
  5529. }
  5530. ArrayNode *arrname = alloc_node<ArrayNode>();
  5531. arrname->name = identifier;
  5532. arrname->datatype_cache = data_type;
  5533. arrname->struct_name = struct_name;
  5534. arrname->index_expression = index_expression;
  5535. arrname->call_expression = call_expression;
  5536. arrname->assign_expression = assign_expression;
  5537. arrname->is_const = is_const;
  5538. arrname->array_size = array_size;
  5539. arrname->is_local = is_local;
  5540. expr = arrname;
  5541. } else {
  5542. VariableNode *varname = alloc_node<VariableNode>();
  5543. varname->name = identifier;
  5544. varname->datatype_cache = data_type;
  5545. varname->is_const = is_const;
  5546. varname->struct_name = struct_name;
  5547. varname->is_local = is_local;
  5548. expr = varname;
  5549. }
  5550. #ifdef DEBUG_ENABLED
  5551. if (check_warnings) {
  5552. StringName func_name;
  5553. BlockNode *b = p_block;
  5554. while (b) {
  5555. if (b->parent_function) {
  5556. func_name = b->parent_function->name;
  5557. break;
  5558. } else {
  5559. b = b->parent_block;
  5560. }
  5561. }
  5562. _parse_used_identifier(identifier, ident_type, func_name);
  5563. }
  5564. #endif // DEBUG_ENABLED
  5565. }
  5566. } else if (tk.type == TK_OP_ADD) {
  5567. continue; //this one does nothing
  5568. } 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) {
  5569. Expression e;
  5570. e.is_op = true;
  5571. switch (tk.type) {
  5572. case TK_OP_SUB:
  5573. e.op = OP_NEGATE;
  5574. break;
  5575. case TK_OP_NOT:
  5576. e.op = OP_NOT;
  5577. break;
  5578. case TK_OP_BIT_INVERT:
  5579. e.op = OP_BIT_INVERT;
  5580. break;
  5581. case TK_OP_INCREMENT:
  5582. e.op = OP_INCREMENT;
  5583. break;
  5584. case TK_OP_DECREMENT:
  5585. e.op = OP_DECREMENT;
  5586. break;
  5587. default:
  5588. ERR_FAIL_V(nullptr);
  5589. }
  5590. expression.push_back(e);
  5591. continue;
  5592. } else {
  5593. bool valid = false;
  5594. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_FOR_EXPRESSION && tk.type == TK_PARENTHESIS_CLOSE) {
  5595. valid = true;
  5596. _set_tkpos(prepos);
  5597. OperatorNode *func = alloc_node<OperatorNode>();
  5598. func->op = OP_EMPTY;
  5599. expr = func;
  5600. }
  5601. if (!valid) {
  5602. if (tk.type != TK_SEMICOLON) {
  5603. _set_error(vformat(RTR("Expected expression, found: '%s'."), get_token_text(tk)));
  5604. return nullptr;
  5605. } else {
  5606. #ifdef DEBUG_ENABLED
  5607. if (!p_block || (p_block->block_type != BlockNode::BLOCK_TYPE_FOR_INIT && p_block->block_type != BlockNode::BLOCK_TYPE_FOR_CONDITION)) {
  5608. if (check_warnings && HAS_WARNING(ShaderWarning::FORMATTING_ERROR_FLAG)) {
  5609. _add_line_warning(ShaderWarning::FORMATTING_ERROR, RTR("Empty statement. Remove ';' to fix this warning."));
  5610. }
  5611. }
  5612. #endif // DEBUG_ENABLED
  5613. _set_tkpos(prepos);
  5614. OperatorNode *func = alloc_node<OperatorNode>();
  5615. func->op = OP_EMPTY;
  5616. expr = func;
  5617. }
  5618. }
  5619. }
  5620. ERR_FAIL_NULL_V(expr, nullptr);
  5621. /* OK now see what's NEXT to the operator.. */
  5622. while (true) {
  5623. TkPos pos2 = _get_tkpos();
  5624. tk = _get_token();
  5625. if (tk.type == TK_CURSOR) {
  5626. //do nothing
  5627. } else if (tk.type == TK_PERIOD) {
  5628. #ifdef DEBUG_ENABLED
  5629. uint32_t prev_keyword_completion_context = keyword_completion_context;
  5630. keyword_completion_context = CF_UNSPECIFIED;
  5631. #endif
  5632. DataType dt = expr->get_datatype();
  5633. String st = expr->get_datatype_name();
  5634. if (!expr->is_indexed() && expr->get_array_size() > 0) {
  5635. completion_class = TAG_ARRAY;
  5636. if (p_block != nullptr) {
  5637. p_block->block_tag = SubClassTag::TAG_ARRAY;
  5638. }
  5639. Node *call_expression = _parse_and_reduce_expression(p_block, p_function_info);
  5640. if (p_block != nullptr) {
  5641. p_block->block_tag = SubClassTag::TAG_GLOBAL;
  5642. }
  5643. if (!call_expression) {
  5644. return nullptr;
  5645. }
  5646. expr = call_expression;
  5647. break;
  5648. }
  5649. StringName identifier;
  5650. if (_get_completable_identifier(p_block, dt == TYPE_STRUCT ? COMPLETION_STRUCT : COMPLETION_INDEX, identifier)) {
  5651. if (dt == TYPE_STRUCT) {
  5652. completion_struct = st;
  5653. } else {
  5654. completion_base = dt;
  5655. }
  5656. }
  5657. if (identifier == StringName()) {
  5658. _set_error(RTR("Expected an identifier as a member."));
  5659. return nullptr;
  5660. }
  5661. String ident = identifier;
  5662. bool ok = true;
  5663. bool repeated = false;
  5664. DataType member_type = TYPE_VOID;
  5665. StringName member_struct_name = "";
  5666. int array_size = 0;
  5667. RBSet<char> position_symbols;
  5668. RBSet<char> color_symbols;
  5669. RBSet<char> texture_symbols;
  5670. bool mix_error = false;
  5671. switch (dt) {
  5672. case TYPE_STRUCT: {
  5673. ok = false;
  5674. String member_name = String(ident.ptr());
  5675. if (shader->structs.has(st)) {
  5676. StructNode *n = shader->structs[st].shader_struct;
  5677. for (const MemberNode *E : n->members) {
  5678. if (String(E->name) == member_name) {
  5679. member_type = E->datatype;
  5680. array_size = E->array_size;
  5681. if (member_type == TYPE_STRUCT) {
  5682. member_struct_name = E->struct_name;
  5683. }
  5684. ok = true;
  5685. break;
  5686. }
  5687. }
  5688. }
  5689. } break;
  5690. case TYPE_BVEC2:
  5691. case TYPE_IVEC2:
  5692. case TYPE_UVEC2:
  5693. case TYPE_VEC2: {
  5694. int l = ident.length();
  5695. if (l == 1) {
  5696. member_type = DataType(dt - 1);
  5697. } else if (l == 2) {
  5698. member_type = dt;
  5699. } else if (l == 3) {
  5700. member_type = DataType(dt + 1);
  5701. } else if (l == 4) {
  5702. member_type = DataType(dt + 2);
  5703. } else {
  5704. ok = false;
  5705. break;
  5706. }
  5707. const char32_t *c = ident.ptr();
  5708. for (int i = 0; i < l; i++) {
  5709. switch (c[i]) {
  5710. case 'r':
  5711. case 'g':
  5712. if (position_symbols.size() > 0 || texture_symbols.size() > 0) {
  5713. mix_error = true;
  5714. break;
  5715. }
  5716. if (!color_symbols.has(c[i])) {
  5717. color_symbols.insert(c[i]);
  5718. } else {
  5719. repeated = true;
  5720. }
  5721. break;
  5722. case 'x':
  5723. case 'y':
  5724. if (color_symbols.size() > 0 || texture_symbols.size() > 0) {
  5725. mix_error = true;
  5726. break;
  5727. }
  5728. if (!position_symbols.has(c[i])) {
  5729. position_symbols.insert(c[i]);
  5730. } else {
  5731. repeated = true;
  5732. }
  5733. break;
  5734. case 's':
  5735. case 't':
  5736. if (color_symbols.size() > 0 || position_symbols.size() > 0) {
  5737. mix_error = true;
  5738. break;
  5739. }
  5740. if (!texture_symbols.has(c[i])) {
  5741. texture_symbols.insert(c[i]);
  5742. } else {
  5743. repeated = true;
  5744. }
  5745. break;
  5746. default:
  5747. ok = false;
  5748. break;
  5749. }
  5750. }
  5751. } break;
  5752. case TYPE_BVEC3:
  5753. case TYPE_IVEC3:
  5754. case TYPE_UVEC3:
  5755. case TYPE_VEC3: {
  5756. int l = ident.length();
  5757. if (l == 1) {
  5758. member_type = DataType(dt - 2);
  5759. } else if (l == 2) {
  5760. member_type = DataType(dt - 1);
  5761. } else if (l == 3) {
  5762. member_type = dt;
  5763. } else if (l == 4) {
  5764. member_type = DataType(dt + 1);
  5765. } else {
  5766. ok = false;
  5767. break;
  5768. }
  5769. const char32_t *c = ident.ptr();
  5770. for (int i = 0; i < l; i++) {
  5771. switch (c[i]) {
  5772. case 'r':
  5773. case 'g':
  5774. case 'b':
  5775. if (position_symbols.size() > 0 || texture_symbols.size() > 0) {
  5776. mix_error = true;
  5777. break;
  5778. }
  5779. if (!color_symbols.has(c[i])) {
  5780. color_symbols.insert(c[i]);
  5781. } else {
  5782. repeated = true;
  5783. }
  5784. break;
  5785. case 'x':
  5786. case 'y':
  5787. case 'z':
  5788. if (color_symbols.size() > 0 || texture_symbols.size() > 0) {
  5789. mix_error = true;
  5790. break;
  5791. }
  5792. if (!position_symbols.has(c[i])) {
  5793. position_symbols.insert(c[i]);
  5794. } else {
  5795. repeated = true;
  5796. }
  5797. break;
  5798. case 's':
  5799. case 't':
  5800. case 'p':
  5801. if (color_symbols.size() > 0 || position_symbols.size() > 0) {
  5802. mix_error = true;
  5803. break;
  5804. }
  5805. if (!texture_symbols.has(c[i])) {
  5806. texture_symbols.insert(c[i]);
  5807. } else {
  5808. repeated = true;
  5809. }
  5810. break;
  5811. default:
  5812. ok = false;
  5813. break;
  5814. }
  5815. }
  5816. } break;
  5817. case TYPE_BVEC4:
  5818. case TYPE_IVEC4:
  5819. case TYPE_UVEC4:
  5820. case TYPE_VEC4: {
  5821. int l = ident.length();
  5822. if (l == 1) {
  5823. member_type = DataType(dt - 3);
  5824. } else if (l == 2) {
  5825. member_type = DataType(dt - 2);
  5826. } else if (l == 3) {
  5827. member_type = DataType(dt - 1);
  5828. } else if (l == 4) {
  5829. member_type = dt;
  5830. } else {
  5831. ok = false;
  5832. break;
  5833. }
  5834. const char32_t *c = ident.ptr();
  5835. for (int i = 0; i < l; i++) {
  5836. switch (c[i]) {
  5837. case 'r':
  5838. case 'g':
  5839. case 'b':
  5840. case 'a':
  5841. if (position_symbols.size() > 0 || texture_symbols.size() > 0) {
  5842. mix_error = true;
  5843. break;
  5844. }
  5845. if (!color_symbols.has(c[i])) {
  5846. color_symbols.insert(c[i]);
  5847. } else {
  5848. repeated = true;
  5849. }
  5850. break;
  5851. case 'x':
  5852. case 'y':
  5853. case 'z':
  5854. case 'w':
  5855. if (color_symbols.size() > 0 || texture_symbols.size() > 0) {
  5856. mix_error = true;
  5857. break;
  5858. }
  5859. if (!position_symbols.has(c[i])) {
  5860. position_symbols.insert(c[i]);
  5861. } else {
  5862. repeated = true;
  5863. }
  5864. break;
  5865. case 's':
  5866. case 't':
  5867. case 'p':
  5868. case 'q':
  5869. if (color_symbols.size() > 0 || position_symbols.size() > 0) {
  5870. mix_error = true;
  5871. break;
  5872. }
  5873. if (!texture_symbols.has(c[i])) {
  5874. texture_symbols.insert(c[i]);
  5875. } else {
  5876. repeated = true;
  5877. }
  5878. break;
  5879. default:
  5880. ok = false;
  5881. break;
  5882. }
  5883. }
  5884. } break;
  5885. default: {
  5886. ok = false;
  5887. }
  5888. }
  5889. if (mix_error) {
  5890. _set_error(vformat(RTR("Cannot combine symbols from different sets in expression '.%s'."), ident));
  5891. return nullptr;
  5892. }
  5893. if (!ok) {
  5894. _set_error(vformat(RTR("Invalid member for '%s' expression: '.%s'."), (dt == TYPE_STRUCT ? st : get_datatype_name(dt)), ident));
  5895. return nullptr;
  5896. }
  5897. MemberNode *mn = alloc_node<MemberNode>();
  5898. mn->basetype = dt;
  5899. mn->basetype_const = is_const;
  5900. mn->datatype = member_type;
  5901. mn->base_struct_name = st;
  5902. mn->struct_name = member_struct_name;
  5903. mn->array_size = array_size;
  5904. mn->name = ident;
  5905. mn->owner = expr;
  5906. mn->has_swizzling_duplicates = repeated;
  5907. if (array_size > 0) {
  5908. TkPos prev_pos = _get_tkpos();
  5909. tk = _get_token();
  5910. if (tk.type == TK_OP_ASSIGN) {
  5911. if (last_type == IDENTIFIER_CONSTANT) {
  5912. _set_error(RTR("Constants cannot be modified."));
  5913. return nullptr;
  5914. }
  5915. Node *assign_expression = _parse_array_constructor(p_block, p_function_info, member_type, member_struct_name, array_size);
  5916. if (!assign_expression) {
  5917. return nullptr;
  5918. }
  5919. mn->assign_expression = assign_expression;
  5920. } else if (tk.type == TK_PERIOD) {
  5921. completion_class = TAG_ARRAY;
  5922. if (p_block != nullptr) {
  5923. p_block->block_tag = SubClassTag::TAG_ARRAY;
  5924. }
  5925. mn->call_expression = _parse_and_reduce_expression(p_block, p_function_info);
  5926. if (p_block != nullptr) {
  5927. p_block->block_tag = SubClassTag::TAG_GLOBAL;
  5928. }
  5929. if (!mn->call_expression) {
  5930. return nullptr;
  5931. }
  5932. } else if (tk.type == TK_BRACKET_OPEN) {
  5933. Node *index_expression = _parse_and_reduce_expression(p_block, p_function_info);
  5934. if (!index_expression) {
  5935. return nullptr;
  5936. }
  5937. if (index_expression->get_array_size() != 0 || (index_expression->get_datatype() != TYPE_INT && index_expression->get_datatype() != TYPE_UINT)) {
  5938. _set_error(RTR("Only integer expressions are allowed for indexing."));
  5939. return nullptr;
  5940. }
  5941. if (index_expression->type == Node::NODE_TYPE_CONSTANT) {
  5942. ConstantNode *cnode = static_cast<ConstantNode *>(index_expression);
  5943. if (cnode) {
  5944. if (!cnode->values.is_empty()) {
  5945. int value = cnode->values[0].sint;
  5946. if (value < 0 || value >= array_size) {
  5947. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), value, 0, array_size - 1));
  5948. return nullptr;
  5949. }
  5950. }
  5951. }
  5952. }
  5953. tk = _get_token();
  5954. if (tk.type != TK_BRACKET_CLOSE) {
  5955. _set_expected_error("]");
  5956. return nullptr;
  5957. }
  5958. mn->index_expression = index_expression;
  5959. } else {
  5960. _set_tkpos(prev_pos);
  5961. }
  5962. }
  5963. expr = mn;
  5964. #ifdef DEBUG_ENABLED
  5965. keyword_completion_context = prev_keyword_completion_context;
  5966. #endif
  5967. //todo
  5968. //member (period) has priority over any operator
  5969. //creates a subindexing expression in place
  5970. /*} else if (tk.type==TK_BRACKET_OPEN) {
  5971. //todo
  5972. //subindexing has priority over any operator
  5973. //creates a subindexing expression in place
  5974. */
  5975. } else if (tk.type == TK_BRACKET_OPEN) {
  5976. Node *index = _parse_and_reduce_expression(p_block, p_function_info);
  5977. if (!index) {
  5978. return nullptr;
  5979. }
  5980. if (index->get_array_size() != 0 || (index->get_datatype() != TYPE_INT && index->get_datatype() != TYPE_UINT)) {
  5981. _set_error(RTR("Only integer expressions are allowed for indexing."));
  5982. return nullptr;
  5983. }
  5984. DataType member_type = TYPE_VOID;
  5985. String member_struct_name;
  5986. if (expr->get_array_size() > 0) {
  5987. if (index->type == Node::NODE_TYPE_CONSTANT) {
  5988. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  5989. if (index_constant >= (uint32_t)expr->get_array_size()) {
  5990. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), index_constant, 0, expr->get_array_size() - 1));
  5991. return nullptr;
  5992. }
  5993. }
  5994. member_type = expr->get_datatype();
  5995. if (member_type == TYPE_STRUCT) {
  5996. member_struct_name = expr->get_datatype_name();
  5997. }
  5998. } else {
  5999. switch (expr->get_datatype()) {
  6000. case TYPE_BVEC2:
  6001. case TYPE_VEC2:
  6002. case TYPE_IVEC2:
  6003. case TYPE_UVEC2:
  6004. case TYPE_MAT2:
  6005. if (index->type == Node::NODE_TYPE_CONSTANT) {
  6006. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  6007. if (index_constant >= 2) {
  6008. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), index_constant, 0, 1));
  6009. return nullptr;
  6010. }
  6011. }
  6012. switch (expr->get_datatype()) {
  6013. case TYPE_BVEC2:
  6014. member_type = TYPE_BOOL;
  6015. break;
  6016. case TYPE_VEC2:
  6017. member_type = TYPE_FLOAT;
  6018. break;
  6019. case TYPE_IVEC2:
  6020. member_type = TYPE_INT;
  6021. break;
  6022. case TYPE_UVEC2:
  6023. member_type = TYPE_UINT;
  6024. break;
  6025. case TYPE_MAT2:
  6026. member_type = TYPE_VEC2;
  6027. break;
  6028. default:
  6029. break;
  6030. }
  6031. break;
  6032. case TYPE_BVEC3:
  6033. case TYPE_VEC3:
  6034. case TYPE_IVEC3:
  6035. case TYPE_UVEC3:
  6036. case TYPE_MAT3:
  6037. if (index->type == Node::NODE_TYPE_CONSTANT) {
  6038. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  6039. if (index_constant >= 3) {
  6040. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), index_constant, 0, 2));
  6041. return nullptr;
  6042. }
  6043. }
  6044. switch (expr->get_datatype()) {
  6045. case TYPE_BVEC3:
  6046. member_type = TYPE_BOOL;
  6047. break;
  6048. case TYPE_VEC3:
  6049. member_type = TYPE_FLOAT;
  6050. break;
  6051. case TYPE_IVEC3:
  6052. member_type = TYPE_INT;
  6053. break;
  6054. case TYPE_UVEC3:
  6055. member_type = TYPE_UINT;
  6056. break;
  6057. case TYPE_MAT3:
  6058. member_type = TYPE_VEC3;
  6059. break;
  6060. default:
  6061. break;
  6062. }
  6063. break;
  6064. case TYPE_BVEC4:
  6065. case TYPE_VEC4:
  6066. case TYPE_IVEC4:
  6067. case TYPE_UVEC4:
  6068. case TYPE_MAT4:
  6069. if (index->type == Node::NODE_TYPE_CONSTANT) {
  6070. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  6071. if (index_constant >= 4) {
  6072. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), index_constant, 0, 3));
  6073. return nullptr;
  6074. }
  6075. }
  6076. switch (expr->get_datatype()) {
  6077. case TYPE_BVEC4:
  6078. member_type = TYPE_BOOL;
  6079. break;
  6080. case TYPE_VEC4:
  6081. member_type = TYPE_FLOAT;
  6082. break;
  6083. case TYPE_IVEC4:
  6084. member_type = TYPE_INT;
  6085. break;
  6086. case TYPE_UVEC4:
  6087. member_type = TYPE_UINT;
  6088. break;
  6089. case TYPE_MAT4:
  6090. member_type = TYPE_VEC4;
  6091. break;
  6092. default:
  6093. break;
  6094. }
  6095. break;
  6096. default: {
  6097. _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()))));
  6098. return nullptr;
  6099. }
  6100. }
  6101. }
  6102. OperatorNode *op = alloc_node<OperatorNode>();
  6103. op->op = OP_INDEX;
  6104. op->return_cache = member_type;
  6105. op->struct_name = member_struct_name;
  6106. op->arguments.push_back(expr);
  6107. op->arguments.push_back(index);
  6108. expr = op;
  6109. tk = _get_token();
  6110. if (tk.type != TK_BRACKET_CLOSE) {
  6111. _set_expected_error("]");
  6112. return nullptr;
  6113. }
  6114. } else if (tk.type == TK_OP_INCREMENT || tk.type == TK_OP_DECREMENT) {
  6115. OperatorNode *op = alloc_node<OperatorNode>();
  6116. op->op = tk.type == TK_OP_DECREMENT ? OP_POST_DECREMENT : OP_POST_INCREMENT;
  6117. op->arguments.push_back(expr);
  6118. if (!_validate_operator(p_block, op, &op->return_cache, &op->return_array_size)) {
  6119. _set_error(RTR("Invalid base type for increment/decrement operator."));
  6120. return nullptr;
  6121. }
  6122. if (!_validate_assign(expr, p_function_info)) {
  6123. _set_error(RTR("Invalid use of increment/decrement operator in a constant expression."));
  6124. return nullptr;
  6125. }
  6126. expr = op;
  6127. } else {
  6128. _set_tkpos(pos2);
  6129. break;
  6130. }
  6131. }
  6132. Expression e;
  6133. e.is_op = false;
  6134. e.node = expr;
  6135. expression.push_back(e);
  6136. pos = _get_tkpos();
  6137. tk = _get_token();
  6138. if (p_previous_expression_info != nullptr && tk.type == p_previous_expression_info->tt_break && !p_previous_expression_info->is_last_expr) {
  6139. break;
  6140. }
  6141. if (is_token_operator(tk.type)) {
  6142. Expression o;
  6143. o.is_op = true;
  6144. switch (tk.type) {
  6145. case TK_OP_EQUAL:
  6146. o.op = OP_EQUAL;
  6147. break;
  6148. case TK_OP_NOT_EQUAL:
  6149. o.op = OP_NOT_EQUAL;
  6150. break;
  6151. case TK_OP_LESS:
  6152. o.op = OP_LESS;
  6153. break;
  6154. case TK_OP_LESS_EQUAL:
  6155. o.op = OP_LESS_EQUAL;
  6156. break;
  6157. case TK_OP_GREATER:
  6158. o.op = OP_GREATER;
  6159. break;
  6160. case TK_OP_GREATER_EQUAL:
  6161. o.op = OP_GREATER_EQUAL;
  6162. break;
  6163. case TK_OP_AND:
  6164. o.op = OP_AND;
  6165. break;
  6166. case TK_OP_OR:
  6167. o.op = OP_OR;
  6168. break;
  6169. case TK_OP_ADD:
  6170. o.op = OP_ADD;
  6171. break;
  6172. case TK_OP_SUB:
  6173. o.op = OP_SUB;
  6174. break;
  6175. case TK_OP_MUL:
  6176. o.op = OP_MUL;
  6177. break;
  6178. case TK_OP_DIV:
  6179. o.op = OP_DIV;
  6180. break;
  6181. case TK_OP_MOD:
  6182. o.op = OP_MOD;
  6183. break;
  6184. case TK_OP_SHIFT_LEFT:
  6185. o.op = OP_SHIFT_LEFT;
  6186. break;
  6187. case TK_OP_SHIFT_RIGHT:
  6188. o.op = OP_SHIFT_RIGHT;
  6189. break;
  6190. case TK_OP_ASSIGN:
  6191. o.op = OP_ASSIGN;
  6192. break;
  6193. case TK_OP_ASSIGN_ADD:
  6194. o.op = OP_ASSIGN_ADD;
  6195. break;
  6196. case TK_OP_ASSIGN_SUB:
  6197. o.op = OP_ASSIGN_SUB;
  6198. break;
  6199. case TK_OP_ASSIGN_MUL:
  6200. o.op = OP_ASSIGN_MUL;
  6201. break;
  6202. case TK_OP_ASSIGN_DIV:
  6203. o.op = OP_ASSIGN_DIV;
  6204. break;
  6205. case TK_OP_ASSIGN_MOD:
  6206. o.op = OP_ASSIGN_MOD;
  6207. break;
  6208. case TK_OP_ASSIGN_SHIFT_LEFT:
  6209. o.op = OP_ASSIGN_SHIFT_LEFT;
  6210. break;
  6211. case TK_OP_ASSIGN_SHIFT_RIGHT:
  6212. o.op = OP_ASSIGN_SHIFT_RIGHT;
  6213. break;
  6214. case TK_OP_ASSIGN_BIT_AND:
  6215. o.op = OP_ASSIGN_BIT_AND;
  6216. break;
  6217. case TK_OP_ASSIGN_BIT_OR:
  6218. o.op = OP_ASSIGN_BIT_OR;
  6219. break;
  6220. case TK_OP_ASSIGN_BIT_XOR:
  6221. o.op = OP_ASSIGN_BIT_XOR;
  6222. break;
  6223. case TK_OP_BIT_AND:
  6224. o.op = OP_BIT_AND;
  6225. break;
  6226. case TK_OP_BIT_OR:
  6227. o.op = OP_BIT_OR;
  6228. break;
  6229. case TK_OP_BIT_XOR:
  6230. o.op = OP_BIT_XOR;
  6231. break;
  6232. case TK_QUESTION:
  6233. o.op = OP_SELECT_IF;
  6234. break;
  6235. case TK_COLON:
  6236. o.op = OP_SELECT_ELSE;
  6237. break;
  6238. default: {
  6239. _set_error(vformat(RTR("Invalid token for the operator: '%s'."), get_token_text(tk)));
  6240. return nullptr;
  6241. }
  6242. }
  6243. expression.push_back(o);
  6244. if (o.op == OP_SELECT_IF) {
  6245. ExpressionInfo info;
  6246. info.expression = &expression;
  6247. info.tt_break = TK_COLON;
  6248. expr = _parse_and_reduce_expression(p_block, p_function_info, &info);
  6249. if (!expr) {
  6250. return nullptr;
  6251. }
  6252. expression.push_back({ true, { OP_SELECT_ELSE } });
  6253. if (p_previous_expression_info != nullptr) {
  6254. info.is_last_expr = p_previous_expression_info->is_last_expr;
  6255. } else {
  6256. info.is_last_expr = true;
  6257. }
  6258. expr = _parse_and_reduce_expression(p_block, p_function_info, &info);
  6259. if (!expr) {
  6260. return nullptr;
  6261. }
  6262. break;
  6263. }
  6264. } else {
  6265. _set_tkpos(pos); //something else, so rollback and end
  6266. break;
  6267. }
  6268. }
  6269. /* Reduce the set set of expressions and place them in an operator tree, respecting precedence */
  6270. while (expression.size() > 1) {
  6271. int next_op = -1;
  6272. int min_priority = 0xFFFFF;
  6273. bool is_unary = false;
  6274. bool is_ternary = false;
  6275. for (int i = 0; i < expression.size(); i++) {
  6276. if (!expression[i].is_op) {
  6277. continue;
  6278. }
  6279. bool unary = false;
  6280. bool ternary = false;
  6281. Operator op = expression[i].op;
  6282. int priority;
  6283. switch (op) {
  6284. case OP_EQUAL:
  6285. priority = 8;
  6286. break;
  6287. case OP_NOT_EQUAL:
  6288. priority = 8;
  6289. break;
  6290. case OP_LESS:
  6291. priority = 7;
  6292. break;
  6293. case OP_LESS_EQUAL:
  6294. priority = 7;
  6295. break;
  6296. case OP_GREATER:
  6297. priority = 7;
  6298. break;
  6299. case OP_GREATER_EQUAL:
  6300. priority = 7;
  6301. break;
  6302. case OP_AND:
  6303. priority = 12;
  6304. break;
  6305. case OP_OR:
  6306. priority = 14;
  6307. break;
  6308. case OP_NOT:
  6309. priority = 3;
  6310. unary = true;
  6311. break;
  6312. case OP_NEGATE:
  6313. priority = 3;
  6314. unary = true;
  6315. break;
  6316. case OP_ADD:
  6317. priority = 5;
  6318. break;
  6319. case OP_SUB:
  6320. priority = 5;
  6321. break;
  6322. case OP_MUL:
  6323. priority = 4;
  6324. break;
  6325. case OP_DIV:
  6326. priority = 4;
  6327. break;
  6328. case OP_MOD:
  6329. priority = 4;
  6330. break;
  6331. case OP_SHIFT_LEFT:
  6332. priority = 6;
  6333. break;
  6334. case OP_SHIFT_RIGHT:
  6335. priority = 6;
  6336. break;
  6337. case OP_ASSIGN:
  6338. priority = 16;
  6339. break;
  6340. case OP_ASSIGN_ADD:
  6341. priority = 16;
  6342. break;
  6343. case OP_ASSIGN_SUB:
  6344. priority = 16;
  6345. break;
  6346. case OP_ASSIGN_MUL:
  6347. priority = 16;
  6348. break;
  6349. case OP_ASSIGN_DIV:
  6350. priority = 16;
  6351. break;
  6352. case OP_ASSIGN_MOD:
  6353. priority = 16;
  6354. break;
  6355. case OP_ASSIGN_SHIFT_LEFT:
  6356. priority = 16;
  6357. break;
  6358. case OP_ASSIGN_SHIFT_RIGHT:
  6359. priority = 16;
  6360. break;
  6361. case OP_ASSIGN_BIT_AND:
  6362. priority = 16;
  6363. break;
  6364. case OP_ASSIGN_BIT_OR:
  6365. priority = 16;
  6366. break;
  6367. case OP_ASSIGN_BIT_XOR:
  6368. priority = 16;
  6369. break;
  6370. case OP_BIT_AND:
  6371. priority = 9;
  6372. break;
  6373. case OP_BIT_OR:
  6374. priority = 11;
  6375. break;
  6376. case OP_BIT_XOR:
  6377. priority = 10;
  6378. break;
  6379. case OP_BIT_INVERT:
  6380. priority = 3;
  6381. unary = true;
  6382. break;
  6383. case OP_INCREMENT:
  6384. priority = 3;
  6385. unary = true;
  6386. break;
  6387. case OP_DECREMENT:
  6388. priority = 3;
  6389. unary = true;
  6390. break;
  6391. case OP_SELECT_IF:
  6392. priority = 15;
  6393. ternary = true;
  6394. break;
  6395. case OP_SELECT_ELSE:
  6396. priority = 15;
  6397. ternary = true;
  6398. break;
  6399. default:
  6400. ERR_FAIL_V(nullptr); //unexpected operator
  6401. }
  6402. #ifdef DEBUG_ENABLED
  6403. if (check_warnings && HAS_WARNING(ShaderWarning::FLOAT_COMPARISON_FLAG) && (op == OP_EQUAL || op == OP_NOT_EQUAL) &&
  6404. (!expression[i - 1].is_op && !expression[i + 1].is_op) &&
  6405. (expression[i - 1].node->get_datatype() == TYPE_FLOAT && expression[i + 1].node->get_datatype() == TYPE_FLOAT)) {
  6406. _add_line_warning(ShaderWarning::FLOAT_COMPARISON);
  6407. }
  6408. #endif // DEBUG_ENABLED
  6409. if (priority < min_priority) {
  6410. // < is used for left to right (default)
  6411. // <= is used for right to left
  6412. next_op = i;
  6413. min_priority = priority;
  6414. is_unary = unary;
  6415. is_ternary = ternary;
  6416. }
  6417. }
  6418. ERR_FAIL_COND_V(next_op == -1, nullptr);
  6419. // OK! create operator..
  6420. if (is_unary) {
  6421. int expr_pos = next_op;
  6422. while (expression[expr_pos].is_op) {
  6423. expr_pos++;
  6424. if (expr_pos == expression.size()) {
  6425. //can happen..
  6426. _set_error(RTR("Unexpected end of expression."));
  6427. return nullptr;
  6428. }
  6429. }
  6430. //consecutively do unary operators
  6431. for (int i = expr_pos - 1; i >= next_op; i--) {
  6432. OperatorNode *op = alloc_node<OperatorNode>();
  6433. op->op = expression[i].op;
  6434. if ((op->op == OP_INCREMENT || op->op == OP_DECREMENT) && !_validate_assign(expression[i + 1].node, p_function_info)) {
  6435. _set_error(RTR("Invalid use of increment/decrement operator in a constant expression."));
  6436. return nullptr;
  6437. }
  6438. op->arguments.push_back(expression[i + 1].node);
  6439. expression.write[i].is_op = false;
  6440. expression.write[i].node = op;
  6441. if (!_validate_operator(p_block, op, &op->return_cache, &op->return_array_size)) {
  6442. if (error_set) {
  6443. return nullptr;
  6444. }
  6445. String at;
  6446. for (int j = 0; j < op->arguments.size(); j++) {
  6447. if (j > 0) {
  6448. at += ", ";
  6449. }
  6450. at += get_datatype_name(op->arguments[j]->get_datatype());
  6451. if (!op->arguments[j]->is_indexed() && op->arguments[j]->get_array_size() > 0) {
  6452. at += "[";
  6453. at += itos(op->arguments[j]->get_array_size());
  6454. at += "]";
  6455. }
  6456. }
  6457. _set_error(vformat(RTR("Invalid arguments to unary operator '%s': %s."), get_operator_text(op->op), at));
  6458. return nullptr;
  6459. }
  6460. expression.remove_at(i + 1);
  6461. }
  6462. } else if (is_ternary) {
  6463. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  6464. _set_parsing_error();
  6465. ERR_FAIL_V(nullptr);
  6466. }
  6467. if (next_op + 2 >= expression.size() || !expression[next_op + 2].is_op || expression[next_op + 2].op != OP_SELECT_ELSE) {
  6468. _set_error(RTR("Missing matching ':' for select operator."));
  6469. return nullptr;
  6470. }
  6471. OperatorNode *op = alloc_node<OperatorNode>();
  6472. op->op = expression[next_op].op;
  6473. op->arguments.push_back(expression[next_op - 1].node);
  6474. op->arguments.push_back(expression[next_op + 1].node);
  6475. op->arguments.push_back(expression[next_op + 3].node);
  6476. expression.write[next_op - 1].is_op = false;
  6477. expression.write[next_op - 1].node = op;
  6478. if (!_validate_operator(p_block, op, &op->return_cache, &op->return_array_size)) {
  6479. if (error_set) {
  6480. return nullptr;
  6481. }
  6482. String at;
  6483. for (int i = 0; i < op->arguments.size(); i++) {
  6484. if (i > 0) {
  6485. at += ", ";
  6486. }
  6487. at += get_datatype_name(op->arguments[i]->get_datatype());
  6488. if (!op->arguments[i]->is_indexed() && op->arguments[i]->get_array_size() > 0) {
  6489. at += "[";
  6490. at += itos(op->arguments[i]->get_array_size());
  6491. at += "]";
  6492. }
  6493. }
  6494. _set_error(vformat(RTR("Invalid argument to ternary operator: '%s'."), at));
  6495. return nullptr;
  6496. }
  6497. for (int i = 0; i < 4; i++) {
  6498. expression.remove_at(next_op);
  6499. }
  6500. } else {
  6501. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  6502. _set_parsing_error();
  6503. ERR_FAIL_V(nullptr);
  6504. }
  6505. OperatorNode *op = alloc_node<OperatorNode>();
  6506. op->op = expression[next_op].op;
  6507. if (expression[next_op - 1].is_op) {
  6508. _set_parsing_error();
  6509. ERR_FAIL_V(nullptr);
  6510. }
  6511. if (_is_operator_assign(op->op)) {
  6512. if (p_block && expression[next_op - 1].node->type == Node::NODE_TYPE_VARIABLE) {
  6513. VariableNode *vn = static_cast<VariableNode *>(expression[next_op - 1].node);
  6514. p_block->use_op_eval = vn->is_const;
  6515. }
  6516. String assign_message;
  6517. if (!_validate_assign(expression[next_op - 1].node, p_function_info, &assign_message)) {
  6518. _set_error(assign_message);
  6519. return nullptr;
  6520. }
  6521. }
  6522. if (expression[next_op + 1].is_op) {
  6523. // this is not invalid and can really appear
  6524. // but it becomes invalid anyway because no binary op
  6525. // can be followed by a unary op in a valid combination,
  6526. // due to how precedence works, unaries will always disappear first
  6527. _set_parsing_error();
  6528. }
  6529. op->arguments.push_back(expression[next_op - 1].node); //expression goes as left
  6530. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as right
  6531. expression.write[next_op - 1].node = op;
  6532. //replace all 3 nodes by this operator and make it an expression
  6533. if (!_validate_operator(p_block, op, &op->return_cache, &op->return_array_size)) {
  6534. if (error_set) {
  6535. return nullptr;
  6536. }
  6537. String at;
  6538. for (int i = 0; i < op->arguments.size(); i++) {
  6539. if (i > 0) {
  6540. at += ", ";
  6541. }
  6542. if (op->arguments[i]->get_datatype() == TYPE_STRUCT) {
  6543. at += op->arguments[i]->get_datatype_name();
  6544. } else {
  6545. at += get_datatype_name(op->arguments[i]->get_datatype());
  6546. }
  6547. if (!op->arguments[i]->is_indexed() && op->arguments[i]->get_array_size() > 0) {
  6548. at += "[";
  6549. at += itos(op->arguments[i]->get_array_size());
  6550. at += "]";
  6551. }
  6552. }
  6553. _set_error(vformat(RTR("Invalid arguments to operator '%s': '%s'."), get_operator_text(op->op), at));
  6554. return nullptr;
  6555. }
  6556. expression.remove_at(next_op);
  6557. expression.remove_at(next_op);
  6558. }
  6559. }
  6560. if (p_block) {
  6561. p_block->use_op_eval = true;
  6562. }
  6563. if (p_previous_expression_info != nullptr) {
  6564. p_previous_expression_info->expression->push_back(expression[0]);
  6565. }
  6566. return expression[0].node;
  6567. }
  6568. ShaderLanguage::Node *ShaderLanguage::_reduce_expression(BlockNode *p_block, ShaderLanguage::Node *p_node) {
  6569. if (p_node->type != Node::NODE_TYPE_OPERATOR) {
  6570. return p_node;
  6571. }
  6572. //for now only reduce simple constructors
  6573. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  6574. if (op->op == OP_CONSTRUCT) {
  6575. ERR_FAIL_COND_V(op->arguments[0]->type != Node::NODE_TYPE_VARIABLE, p_node);
  6576. DataType type = op->get_datatype();
  6577. DataType base = get_scalar_type(type);
  6578. int cardinality = get_cardinality(type);
  6579. Vector<Scalar> values;
  6580. for (int i = 1; i < op->arguments.size(); i++) {
  6581. op->arguments.write[i] = _reduce_expression(p_block, op->arguments[i]);
  6582. if (op->arguments[i]->type == Node::NODE_TYPE_CONSTANT) {
  6583. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[i]);
  6584. if (get_scalar_type(cn->datatype) == base) {
  6585. for (int j = 0; j < cn->values.size(); j++) {
  6586. values.push_back(cn->values[j]);
  6587. }
  6588. } else if (get_scalar_type(cn->datatype) == cn->datatype) {
  6589. Scalar v;
  6590. if (!convert_constant(cn, base, &v)) {
  6591. return p_node;
  6592. }
  6593. values.push_back(v);
  6594. } else {
  6595. return p_node;
  6596. }
  6597. } else {
  6598. return p_node;
  6599. }
  6600. }
  6601. if (values.size() == 1) {
  6602. if (type >= TYPE_MAT2 && type <= TYPE_MAT4) {
  6603. Scalar value = values[0];
  6604. Scalar zero;
  6605. zero.real = 0.0f;
  6606. int size = 2 + (type - TYPE_MAT2);
  6607. values.clear();
  6608. for (int i = 0; i < size; i++) {
  6609. for (int j = 0; j < size; j++) {
  6610. values.push_back(i == j ? value : zero);
  6611. }
  6612. }
  6613. } else {
  6614. Scalar value = values[0];
  6615. for (int i = 1; i < cardinality; i++) {
  6616. values.push_back(value);
  6617. }
  6618. }
  6619. } else if (values.size() != cardinality) {
  6620. ERR_PRINT("Failed to reduce expression, values and cardinality mismatch.");
  6621. return p_node;
  6622. }
  6623. ConstantNode *cn = alloc_node<ConstantNode>();
  6624. cn->datatype = op->get_datatype();
  6625. cn->values = values;
  6626. return cn;
  6627. } else if (op->op == OP_NEGATE) {
  6628. op->arguments.write[0] = _reduce_expression(p_block, op->arguments[0]);
  6629. if (op->arguments[0]->type == Node::NODE_TYPE_CONSTANT) {
  6630. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[0]);
  6631. DataType base = get_scalar_type(cn->datatype);
  6632. Vector<Scalar> values;
  6633. for (int i = 0; i < cn->values.size(); i++) {
  6634. Scalar nv;
  6635. switch (base) {
  6636. case TYPE_BOOL: {
  6637. nv.boolean = !cn->values[i].boolean;
  6638. } break;
  6639. case TYPE_INT: {
  6640. nv.sint = -cn->values[i].sint;
  6641. } break;
  6642. case TYPE_UINT: {
  6643. // Intentionally wrap the unsigned int value, because GLSL does.
  6644. nv.uint = 0 - cn->values[i].uint;
  6645. } break;
  6646. case TYPE_FLOAT: {
  6647. nv.real = -cn->values[i].real;
  6648. } break;
  6649. default: {
  6650. }
  6651. }
  6652. values.push_back(nv);
  6653. }
  6654. cn->values = values;
  6655. return cn;
  6656. }
  6657. }
  6658. return p_node;
  6659. }
  6660. ShaderLanguage::Node *ShaderLanguage::_parse_and_reduce_expression(BlockNode *p_block, const FunctionInfo &p_function_info, const ExpressionInfo *p_previous_expression_info) {
  6661. ShaderLanguage::Node *expr = _parse_expression(p_block, p_function_info, p_previous_expression_info);
  6662. if (!expr) { //errored
  6663. return nullptr;
  6664. }
  6665. expr = _reduce_expression(p_block, expr);
  6666. return expr;
  6667. }
  6668. Error ShaderLanguage::_parse_block(BlockNode *p_block, const FunctionInfo &p_function_info, bool p_just_one, bool p_can_break, bool p_can_continue) {
  6669. while (true) {
  6670. TkPos pos = _get_tkpos();
  6671. Token tk = _get_token();
  6672. #ifdef DEBUG_ENABLED
  6673. Token next;
  6674. #endif // DEBUG_ENABLED
  6675. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_SWITCH) {
  6676. if (tk.type != TK_CF_CASE && tk.type != TK_CF_DEFAULT && tk.type != TK_CURLY_BRACKET_CLOSE) {
  6677. _set_error(vformat(RTR("A switch may only contain '%s' and '%s' blocks."), "case", "default"));
  6678. return ERR_PARSE_ERROR;
  6679. }
  6680. }
  6681. bool is_struct = shader->structs.has(tk.text);
  6682. bool is_var_init = false;
  6683. bool is_condition = false;
  6684. if (tk.type == TK_CURLY_BRACKET_CLOSE) { //end of block
  6685. if (p_just_one) {
  6686. _set_expected_error("}");
  6687. return ERR_PARSE_ERROR;
  6688. }
  6689. return OK;
  6690. } else if (tk.type == TK_CONST || is_token_precision(tk.type) || is_token_nonvoid_datatype(tk.type) || is_struct) {
  6691. is_var_init = true;
  6692. String struct_name = "";
  6693. if (is_struct) {
  6694. struct_name = tk.text;
  6695. #ifdef DEBUG_ENABLED
  6696. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_STRUCT_FLAG) && used_structs.has(struct_name)) {
  6697. used_structs[struct_name].used = true;
  6698. }
  6699. #endif // DEBUG_ENABLED
  6700. }
  6701. #ifdef DEBUG_ENABLED
  6702. uint32_t precision_flag = CF_PRECISION_MODIFIER;
  6703. keyword_completion_context = CF_DATATYPE;
  6704. if (!is_token_precision(tk.type)) {
  6705. if (!is_struct) {
  6706. keyword_completion_context |= precision_flag;
  6707. }
  6708. }
  6709. #endif // DEBUG_ENABLED
  6710. bool is_const = false;
  6711. if (tk.type == TK_CONST) {
  6712. is_const = true;
  6713. tk = _get_token();
  6714. if (!is_struct) {
  6715. is_struct = shader->structs.has(tk.text); // check again.
  6716. struct_name = tk.text;
  6717. }
  6718. }
  6719. DataPrecision precision = PRECISION_DEFAULT;
  6720. if (is_token_precision(tk.type)) {
  6721. precision = get_token_precision(tk.type);
  6722. tk = _get_token();
  6723. if (!is_struct) {
  6724. is_struct = shader->structs.has(tk.text); // check again.
  6725. }
  6726. #ifdef DEBUG_ENABLED
  6727. if (keyword_completion_context & precision_flag) {
  6728. keyword_completion_context ^= precision_flag;
  6729. }
  6730. #endif // DEBUG_ENABLED
  6731. }
  6732. #ifdef DEBUG_ENABLED
  6733. if (is_const && _lookup_next(next)) {
  6734. if (is_token_precision(next.type)) {
  6735. keyword_completion_context = CF_UNSPECIFIED;
  6736. }
  6737. if (is_token_datatype(next.type)) {
  6738. keyword_completion_context ^= CF_DATATYPE;
  6739. }
  6740. }
  6741. #endif // DEBUG_ENABLED
  6742. if (precision != PRECISION_DEFAULT) {
  6743. if (!is_token_nonvoid_datatype(tk.type)) {
  6744. _set_error(RTR("Expected variable type after precision modifier."));
  6745. return ERR_PARSE_ERROR;
  6746. }
  6747. }
  6748. if (!is_struct) {
  6749. if (!is_token_variable_datatype(tk.type)) {
  6750. _set_error(RTR("Invalid variable type (samplers are not allowed)."));
  6751. return ERR_PARSE_ERROR;
  6752. }
  6753. }
  6754. DataType type = is_struct ? TYPE_STRUCT : get_token_datatype(tk.type);
  6755. if (precision != PRECISION_DEFAULT && _validate_precision(type, precision) != OK) {
  6756. return ERR_PARSE_ERROR;
  6757. }
  6758. #ifdef DEBUG_ENABLED
  6759. keyword_completion_context = CF_UNSPECIFIED;
  6760. #endif // DEBUG_ENABLED
  6761. int array_size = 0;
  6762. bool fixed_array_size = false;
  6763. bool first = true;
  6764. VariableDeclarationNode *vdnode = alloc_node<VariableDeclarationNode>();
  6765. vdnode->precision = precision;
  6766. if (is_struct) {
  6767. vdnode->struct_name = struct_name;
  6768. vdnode->datatype = TYPE_STRUCT;
  6769. } else {
  6770. vdnode->datatype = type;
  6771. };
  6772. vdnode->is_const = is_const;
  6773. do {
  6774. bool unknown_size = false;
  6775. VariableDeclarationNode::Declaration decl;
  6776. tk = _get_token();
  6777. if (first) {
  6778. first = false;
  6779. if (tk.type != TK_IDENTIFIER && tk.type != TK_BRACKET_OPEN) {
  6780. _set_error(RTR("Expected an identifier or '[' after type."));
  6781. return ERR_PARSE_ERROR;
  6782. }
  6783. if (tk.type == TK_BRACKET_OPEN) {
  6784. Error error = _parse_array_size(p_block, p_function_info, false, &decl.size_expression, &array_size, &unknown_size);
  6785. if (error != OK) {
  6786. return error;
  6787. }
  6788. decl.size = array_size;
  6789. fixed_array_size = true;
  6790. tk = _get_token();
  6791. }
  6792. }
  6793. if (tk.type != TK_IDENTIFIER) {
  6794. _set_error(RTR("Expected an identifier."));
  6795. return ERR_PARSE_ERROR;
  6796. }
  6797. StringName name = tk.text;
  6798. ShaderLanguage::IdentifierType itype;
  6799. if (_find_identifier(p_block, true, p_function_info, name, (ShaderLanguage::DataType *)nullptr, &itype)) {
  6800. if (itype != IDENTIFIER_FUNCTION) {
  6801. _set_redefinition_error(String(name));
  6802. return ERR_PARSE_ERROR;
  6803. }
  6804. }
  6805. decl.name = name;
  6806. #ifdef DEBUG_ENABLED
  6807. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_LOCAL_VARIABLE_FLAG) && p_block) {
  6808. FunctionNode *parent_function = nullptr;
  6809. {
  6810. BlockNode *block = p_block;
  6811. while (block && !block->parent_function) {
  6812. block = block->parent_block;
  6813. }
  6814. parent_function = block->parent_function;
  6815. }
  6816. if (parent_function) {
  6817. StringName func_name = parent_function->name;
  6818. if (!used_local_vars.has(func_name)) {
  6819. used_local_vars.insert(func_name, HashMap<StringName, Usage>());
  6820. }
  6821. used_local_vars[func_name].insert(name, Usage(tk_line));
  6822. }
  6823. }
  6824. #endif // DEBUG_ENABLED
  6825. is_const_decl = is_const;
  6826. BlockNode::Variable var;
  6827. var.type = type;
  6828. var.precision = precision;
  6829. var.line = tk_line;
  6830. var.array_size = array_size;
  6831. var.is_const = is_const;
  6832. var.struct_name = struct_name;
  6833. tk = _get_token();
  6834. if (tk.type == TK_BRACKET_OPEN) {
  6835. Error error = _parse_array_size(p_block, p_function_info, false, &decl.size_expression, &var.array_size, &unknown_size);
  6836. if (error != OK) {
  6837. return error;
  6838. }
  6839. decl.size = var.array_size;
  6840. array_size = var.array_size;
  6841. tk = _get_token();
  6842. }
  6843. #ifdef DEBUG_ENABLED
  6844. if (var.type == DataType::TYPE_BOOL) {
  6845. keyword_completion_context = CF_BOOLEAN;
  6846. }
  6847. #endif // DEBUG_ENABLED
  6848. if (var.array_size > 0 || unknown_size) {
  6849. bool full_def = false;
  6850. if (tk.type == TK_OP_ASSIGN) {
  6851. TkPos prev_pos = _get_tkpos();
  6852. tk = _get_token();
  6853. if (tk.type == TK_IDENTIFIER) { // a function call array initialization
  6854. _set_tkpos(prev_pos);
  6855. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  6856. if (!n) {
  6857. _set_error(RTR("Expected array initializer."));
  6858. return ERR_PARSE_ERROR;
  6859. } else {
  6860. if (unknown_size) {
  6861. decl.size = n->get_array_size();
  6862. var.array_size = n->get_array_size();
  6863. }
  6864. if (!_compare_datatypes(var.type, var.struct_name, var.array_size, n->get_datatype(), n->get_datatype_name(), n->get_array_size())) {
  6865. return ERR_PARSE_ERROR;
  6866. }
  6867. decl.single_expression = true;
  6868. decl.initializer.push_back(n);
  6869. }
  6870. tk = _get_token();
  6871. } else {
  6872. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  6873. if (unknown_size) {
  6874. _set_expected_error("{");
  6875. return ERR_PARSE_ERROR;
  6876. }
  6877. full_def = true;
  6878. DataPrecision precision2 = PRECISION_DEFAULT;
  6879. if (is_token_precision(tk.type)) {
  6880. precision2 = get_token_precision(tk.type);
  6881. tk = _get_token();
  6882. if (!is_token_nonvoid_datatype(tk.type)) {
  6883. _set_error(RTR("Expected data type after precision modifier."));
  6884. return ERR_PARSE_ERROR;
  6885. }
  6886. }
  6887. DataType type2;
  6888. StringName struct_name2 = "";
  6889. if (shader->structs.has(tk.text)) {
  6890. type2 = TYPE_STRUCT;
  6891. struct_name2 = tk.text;
  6892. } else {
  6893. if (!is_token_variable_datatype(tk.type)) {
  6894. _set_error(RTR("Invalid data type for the array."));
  6895. return ERR_PARSE_ERROR;
  6896. }
  6897. type2 = get_token_datatype(tk.type);
  6898. }
  6899. if (precision2 != PRECISION_DEFAULT && _validate_precision(type2, precision2) != OK) {
  6900. return ERR_PARSE_ERROR;
  6901. }
  6902. int array_size2 = 0;
  6903. tk = _get_token();
  6904. if (tk.type == TK_BRACKET_OPEN) {
  6905. bool is_unknown_size = false;
  6906. Error error = _parse_array_size(p_block, p_function_info, false, nullptr, &array_size2, &is_unknown_size);
  6907. if (error != OK) {
  6908. return error;
  6909. }
  6910. if (is_unknown_size) {
  6911. array_size2 = var.array_size;
  6912. }
  6913. tk = _get_token();
  6914. } else {
  6915. _set_expected_error("[");
  6916. return ERR_PARSE_ERROR;
  6917. }
  6918. if (precision != precision2 || type != type2 || struct_name != struct_name2 || var.array_size != array_size2) {
  6919. String from;
  6920. if (precision2 != PRECISION_DEFAULT) {
  6921. from += get_precision_name(precision2);
  6922. from += " ";
  6923. }
  6924. if (type2 == TYPE_STRUCT) {
  6925. from += struct_name2;
  6926. } else {
  6927. from += get_datatype_name(type2);
  6928. }
  6929. from += "[";
  6930. from += itos(array_size2);
  6931. from += "]'";
  6932. String to;
  6933. if (precision != PRECISION_DEFAULT) {
  6934. to += get_precision_name(precision);
  6935. to += " ";
  6936. }
  6937. if (type == TYPE_STRUCT) {
  6938. to += struct_name;
  6939. } else {
  6940. to += get_datatype_name(type);
  6941. }
  6942. to += "[";
  6943. to += itos(var.array_size);
  6944. to += "]'";
  6945. _set_error(vformat(RTR("Cannot convert from '%s' to '%s'."), from, to));
  6946. return ERR_PARSE_ERROR;
  6947. }
  6948. }
  6949. bool curly = tk.type == TK_CURLY_BRACKET_OPEN;
  6950. if (unknown_size) {
  6951. if (!curly) {
  6952. _set_expected_error("{");
  6953. return ERR_PARSE_ERROR;
  6954. }
  6955. } else {
  6956. if (full_def) {
  6957. if (curly) {
  6958. _set_expected_error("(");
  6959. return ERR_PARSE_ERROR;
  6960. }
  6961. }
  6962. }
  6963. if (tk.type == TK_PARENTHESIS_OPEN || curly) { // initialization
  6964. while (true) {
  6965. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  6966. if (!n) {
  6967. return ERR_PARSE_ERROR;
  6968. }
  6969. if (is_const && n->type == Node::NODE_TYPE_OPERATOR && static_cast<OperatorNode *>(n)->op == OP_CALL) {
  6970. _set_error(RTR("Expected a constant expression."));
  6971. return ERR_PARSE_ERROR;
  6972. }
  6973. if (!_compare_datatypes(var.type, struct_name, 0, n->get_datatype(), n->get_datatype_name(), 0)) {
  6974. return ERR_PARSE_ERROR;
  6975. }
  6976. tk = _get_token();
  6977. if (tk.type == TK_COMMA) {
  6978. decl.initializer.push_back(n);
  6979. continue;
  6980. } else if (!curly && tk.type == TK_PARENTHESIS_CLOSE) {
  6981. decl.initializer.push_back(n);
  6982. break;
  6983. } else if (curly && tk.type == TK_CURLY_BRACKET_CLOSE) {
  6984. decl.initializer.push_back(n);
  6985. break;
  6986. } else {
  6987. if (curly) {
  6988. _set_expected_error("}", ",");
  6989. } else {
  6990. _set_expected_error(")", ",");
  6991. }
  6992. return ERR_PARSE_ERROR;
  6993. }
  6994. }
  6995. if (unknown_size) {
  6996. decl.size = decl.initializer.size();
  6997. var.array_size = decl.initializer.size();
  6998. } else if (decl.initializer.size() != var.array_size) {
  6999. _set_error(vformat(RTR("Array size mismatch. Expected %d elements (found %d)."), var.array_size, decl.initializer.size()));
  7000. return ERR_PARSE_ERROR;
  7001. }
  7002. tk = _get_token();
  7003. } else {
  7004. _set_expected_error("(");
  7005. return ERR_PARSE_ERROR;
  7006. }
  7007. }
  7008. } else {
  7009. if (unknown_size) {
  7010. _set_error(RTR("Expected array initialization."));
  7011. return ERR_PARSE_ERROR;
  7012. }
  7013. if (is_const) {
  7014. _set_error(RTR("Expected initialization of constant."));
  7015. return ERR_PARSE_ERROR;
  7016. }
  7017. }
  7018. array_size = var.array_size;
  7019. } else if (tk.type == TK_OP_ASSIGN) {
  7020. p_block->use_op_eval = is_const;
  7021. // Variable created with assignment! Must parse an expression.
  7022. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  7023. if (!n) {
  7024. return ERR_PARSE_ERROR;
  7025. }
  7026. if (is_const && n->type == Node::NODE_TYPE_OPERATOR && static_cast<OperatorNode *>(n)->op == OP_CALL) {
  7027. OperatorNode *op = static_cast<OperatorNode *>(n);
  7028. for (int i = 1; i < op->arguments.size(); i++) {
  7029. if (!_check_node_constness(op->arguments[i])) {
  7030. _set_error(vformat(RTR("Expected constant expression for argument %d of function call after '='."), i - 1));
  7031. return ERR_PARSE_ERROR;
  7032. }
  7033. }
  7034. }
  7035. if (is_const) {
  7036. var.values = n->get_values();
  7037. }
  7038. if (!_compare_datatypes(var.type, var.struct_name, var.array_size, n->get_datatype(), n->get_datatype_name(), n->get_array_size())) {
  7039. return ERR_PARSE_ERROR;
  7040. }
  7041. decl.initializer.push_back(n);
  7042. tk = _get_token();
  7043. } else {
  7044. if (is_const) {
  7045. _set_error(RTR("Expected initialization of constant."));
  7046. return ERR_PARSE_ERROR;
  7047. }
  7048. }
  7049. vdnode->declarations.push_back(decl);
  7050. p_block->variables[name] = var;
  7051. is_const_decl = false;
  7052. if (!fixed_array_size) {
  7053. array_size = 0;
  7054. }
  7055. if (tk.type == TK_SEMICOLON) {
  7056. break;
  7057. } else if (tk.type != TK_COMMA) {
  7058. _set_expected_error(",", ";");
  7059. return ERR_PARSE_ERROR;
  7060. }
  7061. } while (tk.type == TK_COMMA); //another variable
  7062. #ifdef DEBUG_ENABLED
  7063. keyword_completion_context = CF_BLOCK;
  7064. #endif // DEBUG_ENABLED
  7065. p_block->statements.push_back(static_cast<Node *>(vdnode));
  7066. } else if (tk.type == TK_CURLY_BRACKET_OPEN) {
  7067. //a sub block, just because..
  7068. BlockNode *block = alloc_node<BlockNode>();
  7069. block->parent_block = p_block;
  7070. if (_parse_block(block, p_function_info, false, p_can_break, p_can_continue) != OK) {
  7071. return ERR_PARSE_ERROR;
  7072. }
  7073. p_block->statements.push_back(block);
  7074. } else if (tk.type == TK_CF_IF) {
  7075. //if () {}
  7076. tk = _get_token();
  7077. if (tk.type != TK_PARENTHESIS_OPEN) {
  7078. _set_expected_after_error("(", "if");
  7079. return ERR_PARSE_ERROR;
  7080. }
  7081. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  7082. cf->flow_op = FLOW_OP_IF;
  7083. #ifdef DEBUG_ENABLED
  7084. keyword_completion_context = CF_IF_DECL;
  7085. #endif // DEBUG_ENABLED
  7086. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  7087. if (!n) {
  7088. return ERR_PARSE_ERROR;
  7089. }
  7090. #ifdef DEBUG_ENABLED
  7091. keyword_completion_context = CF_BLOCK;
  7092. #endif // DEBUG_ENABLED
  7093. if (n->get_datatype() != TYPE_BOOL) {
  7094. _set_error(RTR("Expected a boolean expression."));
  7095. return ERR_PARSE_ERROR;
  7096. }
  7097. tk = _get_token();
  7098. if (tk.type != TK_PARENTHESIS_CLOSE) {
  7099. _set_expected_error(")");
  7100. return ERR_PARSE_ERROR;
  7101. }
  7102. BlockNode *block = alloc_node<BlockNode>();
  7103. block->parent_block = p_block;
  7104. cf->expressions.push_back(n);
  7105. cf->blocks.push_back(block);
  7106. p_block->statements.push_back(cf);
  7107. Error err = _parse_block(block, p_function_info, true, p_can_break, p_can_continue);
  7108. if (err) {
  7109. return err;
  7110. }
  7111. pos = _get_tkpos();
  7112. tk = _get_token();
  7113. if (tk.type == TK_CF_ELSE) {
  7114. block = alloc_node<BlockNode>();
  7115. block->parent_block = p_block;
  7116. cf->blocks.push_back(block);
  7117. err = _parse_block(block, p_function_info, true, p_can_break, p_can_continue);
  7118. if (err) {
  7119. return err;
  7120. }
  7121. } else {
  7122. _set_tkpos(pos); //rollback
  7123. }
  7124. } else if (tk.type == TK_CF_SWITCH) {
  7125. // switch() {}
  7126. tk = _get_token();
  7127. if (tk.type != TK_PARENTHESIS_OPEN) {
  7128. _set_expected_after_error("(", "switch");
  7129. return ERR_PARSE_ERROR;
  7130. }
  7131. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  7132. cf->flow_op = FLOW_OP_SWITCH;
  7133. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  7134. if (!n) {
  7135. return ERR_PARSE_ERROR;
  7136. }
  7137. {
  7138. const ShaderLanguage::DataType switch_type = n->get_datatype();
  7139. if (switch_type != TYPE_INT && switch_type != TYPE_UINT) {
  7140. _set_error(RTR("Expected an integer expression."));
  7141. return ERR_PARSE_ERROR;
  7142. }
  7143. }
  7144. tk = _get_token();
  7145. if (tk.type != TK_PARENTHESIS_CLOSE) {
  7146. _set_expected_error(")");
  7147. return ERR_PARSE_ERROR;
  7148. }
  7149. tk = _get_token();
  7150. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  7151. _set_expected_after_error("{", "switch");
  7152. return ERR_PARSE_ERROR;
  7153. }
  7154. BlockNode *switch_block = alloc_node<BlockNode>();
  7155. switch_block->block_type = BlockNode::BLOCK_TYPE_SWITCH;
  7156. switch_block->parent_block = p_block;
  7157. cf->expressions.push_back(n);
  7158. cf->blocks.push_back(switch_block);
  7159. p_block->statements.push_back(cf);
  7160. int prev_type = TK_CF_CASE;
  7161. while (true) { // Go-through multiple cases.
  7162. if (_parse_block(switch_block, p_function_info, true, true, false) != OK) {
  7163. return ERR_PARSE_ERROR;
  7164. }
  7165. pos = _get_tkpos();
  7166. tk = _get_token();
  7167. if (tk.type == TK_CF_CASE || tk.type == TK_CF_DEFAULT) {
  7168. if (prev_type == TK_CF_DEFAULT) {
  7169. if (tk.type == TK_CF_CASE) {
  7170. _set_error(RTR("Cases must be defined before default case."));
  7171. return ERR_PARSE_ERROR;
  7172. } else if (prev_type == TK_CF_DEFAULT) {
  7173. _set_error(RTR("Default case must be defined only once."));
  7174. return ERR_PARSE_ERROR;
  7175. }
  7176. }
  7177. prev_type = tk.type;
  7178. _set_tkpos(pos);
  7179. continue;
  7180. } else {
  7181. HashSet<int> constants;
  7182. for (ShaderLanguage::Node *statement : switch_block->statements) { // Checks for duplicates.
  7183. ControlFlowNode *flow = static_cast<ControlFlowNode *>(statement);
  7184. if (flow) {
  7185. if (flow->flow_op == FLOW_OP_CASE) {
  7186. if (flow->expressions[0]->type == Node::NODE_TYPE_CONSTANT) {
  7187. ConstantNode *cn = static_cast<ConstantNode *>(flow->expressions[0]);
  7188. if (!cn || cn->values.is_empty()) {
  7189. return ERR_PARSE_ERROR;
  7190. }
  7191. if (constants.has(cn->values[0].sint)) {
  7192. _set_error(vformat(RTR("Duplicated case label: %d."), cn->values[0].sint));
  7193. return ERR_PARSE_ERROR;
  7194. }
  7195. constants.insert(cn->values[0].sint);
  7196. } else if (flow->expressions[0]->type == Node::NODE_TYPE_VARIABLE) {
  7197. VariableNode *vn = static_cast<VariableNode *>(flow->expressions[0]);
  7198. if (!vn) {
  7199. return ERR_PARSE_ERROR;
  7200. }
  7201. Vector<Scalar> v = { Scalar() };
  7202. _find_identifier(p_block, false, p_function_info, vn->name, nullptr, nullptr, nullptr, nullptr, nullptr, &v);
  7203. if (constants.has(v[0].sint)) {
  7204. _set_error(vformat(RTR("Duplicated case label: %d."), v[0].sint));
  7205. return ERR_PARSE_ERROR;
  7206. }
  7207. constants.insert(v[0].sint);
  7208. }
  7209. } else if (flow->flow_op == FLOW_OP_DEFAULT) {
  7210. continue;
  7211. } else {
  7212. return ERR_PARSE_ERROR;
  7213. }
  7214. } else {
  7215. return ERR_PARSE_ERROR;
  7216. }
  7217. }
  7218. break;
  7219. }
  7220. }
  7221. } else if (tk.type == TK_CF_CASE) {
  7222. // case x : break; | return;
  7223. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_CASE) {
  7224. _set_tkpos(pos);
  7225. return OK;
  7226. }
  7227. if (!p_block || (p_block->block_type != BlockNode::BLOCK_TYPE_SWITCH)) {
  7228. _set_error(vformat(RTR("'%s' must be placed within a '%s' block."), "case", "switch"));
  7229. return ERR_PARSE_ERROR;
  7230. }
  7231. tk = _get_token();
  7232. int sign = 1;
  7233. if (tk.type == TK_OP_SUB) {
  7234. sign = -1;
  7235. tk = _get_token();
  7236. }
  7237. Node *n = nullptr;
  7238. if (!tk.is_integer_constant()) {
  7239. bool correct_constant_expression = false;
  7240. DataType data_type;
  7241. if (tk.type == TK_IDENTIFIER) {
  7242. bool is_const;
  7243. _find_identifier(p_block, false, p_function_info, tk.text, &data_type, nullptr, &is_const);
  7244. if (is_const) {
  7245. if (data_type == TYPE_INT) {
  7246. correct_constant_expression = true;
  7247. }
  7248. }
  7249. }
  7250. if (!correct_constant_expression) {
  7251. _set_error(RTR("Expected an integer constant."));
  7252. return ERR_PARSE_ERROR;
  7253. }
  7254. VariableNode *vn = alloc_node<VariableNode>();
  7255. vn->name = tk.text;
  7256. n = vn;
  7257. } else {
  7258. Scalar v;
  7259. if (tk.type == TK_UINT_CONSTANT) {
  7260. v.uint = (uint32_t)tk.constant;
  7261. } else {
  7262. v.sint = (int)tk.constant * sign;
  7263. }
  7264. ConstantNode *cn = alloc_node<ConstantNode>();
  7265. cn->values.push_back(v);
  7266. cn->datatype = (tk.type == TK_UINT_CONSTANT ? TYPE_UINT : TYPE_INT);
  7267. n = cn;
  7268. }
  7269. tk = _get_token();
  7270. if (tk.type != TK_COLON) {
  7271. _set_expected_error(":");
  7272. return ERR_PARSE_ERROR;
  7273. }
  7274. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  7275. cf->flow_op = FLOW_OP_CASE;
  7276. BlockNode *case_block = alloc_node<BlockNode>();
  7277. case_block->block_type = BlockNode::BLOCK_TYPE_CASE;
  7278. case_block->parent_block = p_block;
  7279. cf->expressions.push_back(n);
  7280. cf->blocks.push_back(case_block);
  7281. p_block->statements.push_back(cf);
  7282. Error err = _parse_block(case_block, p_function_info, false, true, false);
  7283. if (err) {
  7284. return err;
  7285. }
  7286. return OK;
  7287. } else if (tk.type == TK_CF_DEFAULT) {
  7288. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_CASE) {
  7289. _set_tkpos(pos);
  7290. return OK;
  7291. }
  7292. if (!p_block || (p_block->block_type != BlockNode::BLOCK_TYPE_SWITCH)) {
  7293. _set_error(vformat(RTR("'%s' must be placed within a '%s' block."), "default", "switch"));
  7294. return ERR_PARSE_ERROR;
  7295. }
  7296. tk = _get_token();
  7297. if (tk.type != TK_COLON) {
  7298. _set_expected_error(":");
  7299. return ERR_PARSE_ERROR;
  7300. }
  7301. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  7302. cf->flow_op = FLOW_OP_DEFAULT;
  7303. BlockNode *default_block = alloc_node<BlockNode>();
  7304. default_block->block_type = BlockNode::BLOCK_TYPE_DEFAULT;
  7305. default_block->parent_block = p_block;
  7306. cf->blocks.push_back(default_block);
  7307. p_block->statements.push_back(cf);
  7308. Error err = _parse_block(default_block, p_function_info, false, true, false);
  7309. if (err) {
  7310. return err;
  7311. }
  7312. return OK;
  7313. } else if (tk.type == TK_CF_DO || tk.type == TK_CF_WHILE) {
  7314. // do {} while()
  7315. // while() {}
  7316. bool is_do = tk.type == TK_CF_DO;
  7317. BlockNode *do_block = nullptr;
  7318. if (is_do) {
  7319. do_block = alloc_node<BlockNode>();
  7320. do_block->parent_block = p_block;
  7321. Error err = _parse_block(do_block, p_function_info, true, true, true);
  7322. if (err) {
  7323. return err;
  7324. }
  7325. tk = _get_token();
  7326. if (tk.type != TK_CF_WHILE) {
  7327. _set_expected_after_error("while", "do");
  7328. return ERR_PARSE_ERROR;
  7329. }
  7330. }
  7331. tk = _get_token();
  7332. if (tk.type != TK_PARENTHESIS_OPEN) {
  7333. _set_expected_after_error("(", "while");
  7334. return ERR_PARSE_ERROR;
  7335. }
  7336. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  7337. if (is_do) {
  7338. cf->flow_op = FLOW_OP_DO;
  7339. } else {
  7340. cf->flow_op = FLOW_OP_WHILE;
  7341. }
  7342. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  7343. if (!n) {
  7344. return ERR_PARSE_ERROR;
  7345. }
  7346. tk = _get_token();
  7347. if (tk.type != TK_PARENTHESIS_CLOSE) {
  7348. _set_expected_error(")");
  7349. return ERR_PARSE_ERROR;
  7350. }
  7351. if (!is_do) {
  7352. BlockNode *block = alloc_node<BlockNode>();
  7353. block->parent_block = p_block;
  7354. cf->expressions.push_back(n);
  7355. cf->blocks.push_back(block);
  7356. p_block->statements.push_back(cf);
  7357. Error err = _parse_block(block, p_function_info, true, true, true);
  7358. if (err) {
  7359. return err;
  7360. }
  7361. } else {
  7362. cf->expressions.push_back(n);
  7363. cf->blocks.push_back(do_block);
  7364. p_block->statements.push_back(cf);
  7365. tk = _get_token();
  7366. if (tk.type != TK_SEMICOLON) {
  7367. _set_expected_error(";");
  7368. return ERR_PARSE_ERROR;
  7369. }
  7370. }
  7371. } else if (tk.type == TK_CF_FOR) {
  7372. // for() {}
  7373. tk = _get_token();
  7374. if (tk.type != TK_PARENTHESIS_OPEN) {
  7375. _set_expected_after_error("(", "for");
  7376. return ERR_PARSE_ERROR;
  7377. }
  7378. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  7379. cf->flow_op = FLOW_OP_FOR;
  7380. BlockNode *init_block = alloc_node<BlockNode>();
  7381. init_block->block_type = BlockNode::BLOCK_TYPE_FOR_INIT;
  7382. init_block->parent_block = p_block;
  7383. init_block->single_statement = true;
  7384. cf->blocks.push_back(init_block);
  7385. #ifdef DEBUG_ENABLED
  7386. keyword_completion_context = CF_DATATYPE;
  7387. #endif // DEBUG_ENABLED
  7388. Error err = _parse_block(init_block, p_function_info, true, false, false);
  7389. if (err != OK) {
  7390. return err;
  7391. }
  7392. #ifdef DEBUG_ENABLED
  7393. keyword_completion_context = CF_UNSPECIFIED;
  7394. #endif // DEBUG_ENABLED
  7395. BlockNode *condition_block = alloc_node<BlockNode>();
  7396. condition_block->block_type = BlockNode::BLOCK_TYPE_FOR_CONDITION;
  7397. condition_block->parent_block = init_block;
  7398. condition_block->single_statement = true;
  7399. condition_block->use_comma_between_statements = true;
  7400. cf->blocks.push_back(condition_block);
  7401. err = _parse_block(condition_block, p_function_info, true, false, false);
  7402. if (err != OK) {
  7403. return err;
  7404. }
  7405. BlockNode *expression_block = alloc_node<BlockNode>();
  7406. expression_block->block_type = BlockNode::BLOCK_TYPE_FOR_EXPRESSION;
  7407. expression_block->parent_block = init_block;
  7408. expression_block->single_statement = true;
  7409. expression_block->use_comma_between_statements = true;
  7410. cf->blocks.push_back(expression_block);
  7411. err = _parse_block(expression_block, p_function_info, true, false, false);
  7412. if (err != OK) {
  7413. return err;
  7414. }
  7415. BlockNode *block = alloc_node<BlockNode>();
  7416. block->parent_block = init_block;
  7417. cf->blocks.push_back(block);
  7418. p_block->statements.push_back(cf);
  7419. #ifdef DEBUG_ENABLED
  7420. keyword_completion_context = CF_BLOCK;
  7421. #endif // DEBUG_ENABLED
  7422. err = _parse_block(block, p_function_info, true, true, true);
  7423. if (err != OK) {
  7424. return err;
  7425. }
  7426. } else if (tk.type == TK_CF_RETURN) {
  7427. //check return type
  7428. BlockNode *b = p_block;
  7429. while (b && !b->parent_function) {
  7430. b = b->parent_block;
  7431. }
  7432. if (!b) {
  7433. _set_parsing_error();
  7434. return ERR_BUG;
  7435. }
  7436. if (b->parent_function && p_function_info.main_function) {
  7437. _set_error(vformat(RTR("Using '%s' in the '%s' processor function is incorrect."), "return", b->parent_function->name));
  7438. return ERR_PARSE_ERROR;
  7439. }
  7440. String return_struct_name = String(b->parent_function->return_struct_name);
  7441. String array_size_string;
  7442. if (b->parent_function->return_array_size > 0) {
  7443. array_size_string = "[" + itos(b->parent_function->return_array_size) + "]";
  7444. }
  7445. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  7446. flow->flow_op = FLOW_OP_RETURN;
  7447. pos = _get_tkpos();
  7448. tk = _get_token();
  7449. if (tk.type == TK_SEMICOLON) {
  7450. //all is good
  7451. if (b->parent_function->return_type != TYPE_VOID) {
  7452. _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));
  7453. return ERR_PARSE_ERROR;
  7454. }
  7455. } else {
  7456. _set_tkpos(pos); //rollback, wants expression
  7457. #ifdef DEBUG_ENABLED
  7458. if (b->parent_function->return_type == DataType::TYPE_BOOL) {
  7459. keyword_completion_context = CF_BOOLEAN;
  7460. }
  7461. #endif // DEBUG_ENABLED
  7462. Node *expr = _parse_and_reduce_expression(p_block, p_function_info);
  7463. if (!expr) {
  7464. return ERR_PARSE_ERROR;
  7465. }
  7466. 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()) {
  7467. _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));
  7468. return ERR_PARSE_ERROR;
  7469. }
  7470. tk = _get_token();
  7471. if (tk.type != TK_SEMICOLON) {
  7472. _set_expected_after_error(";", "return");
  7473. return ERR_PARSE_ERROR;
  7474. }
  7475. #ifdef DEBUG_ENABLED
  7476. if (b->parent_function->return_type == DataType::TYPE_BOOL) {
  7477. keyword_completion_context = CF_BLOCK;
  7478. }
  7479. #endif // DEBUG_ENABLED
  7480. flow->expressions.push_back(expr);
  7481. }
  7482. p_block->statements.push_back(flow);
  7483. BlockNode *block = p_block;
  7484. while (block) {
  7485. if (block->block_type == BlockNode::BLOCK_TYPE_CASE || block->block_type == BlockNode::BLOCK_TYPE_DEFAULT) {
  7486. return OK;
  7487. }
  7488. block = block->parent_block;
  7489. }
  7490. } else if (tk.type == TK_CF_DISCARD) {
  7491. //check return type
  7492. BlockNode *b = p_block;
  7493. while (b && !b->parent_function) {
  7494. b = b->parent_block;
  7495. }
  7496. if (!b) {
  7497. _set_parsing_error();
  7498. return ERR_BUG;
  7499. }
  7500. if (!b->parent_function->can_discard) {
  7501. _set_error(vformat(RTR("Use of '%s' is not allowed here."), "discard"));
  7502. return ERR_PARSE_ERROR;
  7503. }
  7504. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  7505. flow->flow_op = FLOW_OP_DISCARD;
  7506. pos = _get_tkpos();
  7507. tk = _get_token();
  7508. if (tk.type != TK_SEMICOLON) {
  7509. _set_expected_after_error(";", "discard");
  7510. return ERR_PARSE_ERROR;
  7511. }
  7512. p_block->statements.push_back(flow);
  7513. } else if (tk.type == TK_CF_BREAK) {
  7514. if (!p_can_break) {
  7515. _set_error(vformat(RTR("'%s' is not allowed outside of a loop or '%s' statement."), "break", "switch"));
  7516. return ERR_PARSE_ERROR;
  7517. }
  7518. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  7519. flow->flow_op = FLOW_OP_BREAK;
  7520. pos = _get_tkpos();
  7521. tk = _get_token();
  7522. if (tk.type != TK_SEMICOLON) {
  7523. _set_expected_after_error(";", "break");
  7524. return ERR_PARSE_ERROR;
  7525. }
  7526. p_block->statements.push_back(flow);
  7527. BlockNode *block = p_block;
  7528. while (block) {
  7529. if (block->block_type == BlockNode::BLOCK_TYPE_CASE || block->block_type == BlockNode::BLOCK_TYPE_DEFAULT) {
  7530. return OK;
  7531. }
  7532. block = block->parent_block;
  7533. }
  7534. } else if (tk.type == TK_CF_CONTINUE) {
  7535. if (!p_can_continue) {
  7536. _set_error(vformat(RTR("'%s' is not allowed outside of a loop."), "continue"));
  7537. return ERR_PARSE_ERROR;
  7538. }
  7539. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  7540. flow->flow_op = FLOW_OP_CONTINUE;
  7541. pos = _get_tkpos();
  7542. tk = _get_token();
  7543. if (tk.type != TK_SEMICOLON) {
  7544. //all is good
  7545. _set_expected_after_error(";", "continue");
  7546. return ERR_PARSE_ERROR;
  7547. }
  7548. p_block->statements.push_back(flow);
  7549. } else {
  7550. //nothing else, so expression
  7551. _set_tkpos(pos); //rollback
  7552. Node *expr = _parse_and_reduce_expression(p_block, p_function_info);
  7553. if (!expr) {
  7554. return ERR_PARSE_ERROR;
  7555. }
  7556. is_condition = expr->get_datatype() == TYPE_BOOL;
  7557. if (expr->type == Node::NODE_TYPE_OPERATOR) {
  7558. OperatorNode *op = static_cast<OperatorNode *>(expr);
  7559. if (op->op == OP_EMPTY) {
  7560. is_var_init = true;
  7561. is_condition = true;
  7562. }
  7563. }
  7564. p_block->statements.push_back(expr);
  7565. tk = _get_token();
  7566. if (p_block->block_type == BlockNode::BLOCK_TYPE_FOR_CONDITION) {
  7567. if (tk.type == TK_COMMA) {
  7568. if (!is_condition) {
  7569. _set_error(RTR("The middle expression is expected to have a boolean data type."));
  7570. return ERR_PARSE_ERROR;
  7571. }
  7572. tk = _peek();
  7573. if (tk.type == TK_SEMICOLON) {
  7574. _set_error(vformat(RTR("Expected expression, found: '%s'."), get_token_text(tk)));
  7575. return ERR_PARSE_ERROR;
  7576. }
  7577. continue;
  7578. }
  7579. if (tk.type != TK_SEMICOLON) {
  7580. _set_expected_error(",", ";");
  7581. return ERR_PARSE_ERROR;
  7582. }
  7583. } else if (p_block->block_type == BlockNode::BLOCK_TYPE_FOR_EXPRESSION) {
  7584. if (tk.type == TK_COMMA) {
  7585. tk = _peek();
  7586. if (tk.type == TK_PARENTHESIS_CLOSE) {
  7587. _set_error(vformat(RTR("Expected expression, found: '%s'."), get_token_text(tk)));
  7588. return ERR_PARSE_ERROR;
  7589. }
  7590. continue;
  7591. }
  7592. if (tk.type != TK_PARENTHESIS_CLOSE) {
  7593. _set_expected_error(",", ")");
  7594. return ERR_PARSE_ERROR;
  7595. }
  7596. } else if (tk.type != TK_SEMICOLON) {
  7597. _set_expected_error(";");
  7598. return ERR_PARSE_ERROR;
  7599. }
  7600. }
  7601. if (p_block) {
  7602. if (p_block->block_type == BlockNode::BLOCK_TYPE_FOR_INIT && !is_var_init) {
  7603. _set_error(RTR("The left expression is expected to be a variable declaration."));
  7604. return ERR_PARSE_ERROR;
  7605. }
  7606. if (p_block->block_type == BlockNode::BLOCK_TYPE_FOR_CONDITION && !is_condition) {
  7607. _set_error(RTR("The middle expression is expected to have a boolean data type."));
  7608. return ERR_PARSE_ERROR;
  7609. }
  7610. }
  7611. if (p_just_one) {
  7612. break;
  7613. }
  7614. }
  7615. return OK;
  7616. }
  7617. String ShaderLanguage::_get_shader_type_list(const HashSet<String> &p_shader_types) const {
  7618. // Return a list of shader types as an human-readable string
  7619. String valid_types;
  7620. for (const String &E : p_shader_types) {
  7621. if (!valid_types.is_empty()) {
  7622. valid_types += ", ";
  7623. }
  7624. valid_types += "'" + E + "'";
  7625. }
  7626. return valid_types;
  7627. }
  7628. String ShaderLanguage::_get_qualifier_str(ArgumentQualifier p_qualifier) const {
  7629. switch (p_qualifier) {
  7630. case ArgumentQualifier::ARGUMENT_QUALIFIER_IN:
  7631. return "in";
  7632. case ArgumentQualifier::ARGUMENT_QUALIFIER_OUT:
  7633. return "out";
  7634. case ArgumentQualifier::ARGUMENT_QUALIFIER_INOUT:
  7635. return "inout";
  7636. }
  7637. return "";
  7638. }
  7639. Error ShaderLanguage::_validate_precision(DataType p_type, DataPrecision p_precision) {
  7640. switch (p_type) {
  7641. case TYPE_STRUCT: {
  7642. _set_error(RTR("The precision modifier cannot be used on structs."));
  7643. return FAILED;
  7644. } break;
  7645. case TYPE_BOOL:
  7646. case TYPE_BVEC2:
  7647. case TYPE_BVEC3:
  7648. case TYPE_BVEC4: {
  7649. _set_error(RTR("The precision modifier cannot be used on boolean types."));
  7650. return FAILED;
  7651. } break;
  7652. default:
  7653. break;
  7654. }
  7655. return OK;
  7656. }
  7657. Error ShaderLanguage::_parse_shader(const HashMap<StringName, FunctionInfo> &p_functions, const Vector<ModeInfo> &p_render_modes, const HashSet<String> &p_shader_types) {
  7658. Token tk;
  7659. TkPos prev_pos;
  7660. Token next;
  7661. if (!is_shader_inc) {
  7662. #ifdef DEBUG_ENABLED
  7663. keyword_completion_context = CF_SHADER_TYPE;
  7664. #endif // DEBUG_ENABLED
  7665. tk = _get_token();
  7666. if (tk.type != TK_SHADER_TYPE) {
  7667. _set_error(vformat(RTR("Expected '%s' at the beginning of shader. Valid types are: %s."), "shader_type", _get_shader_type_list(p_shader_types)));
  7668. return ERR_PARSE_ERROR;
  7669. }
  7670. #ifdef DEBUG_ENABLED
  7671. keyword_completion_context = CF_UNSPECIFIED;
  7672. #endif // DEBUG_ENABLED
  7673. _get_completable_identifier(nullptr, COMPLETION_SHADER_TYPE, shader_type_identifier);
  7674. if (shader_type_identifier == StringName()) {
  7675. _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)));
  7676. return ERR_PARSE_ERROR;
  7677. }
  7678. if (!p_shader_types.has(shader_type_identifier)) {
  7679. _set_error(vformat(RTR("Invalid shader type. Valid types are: %s"), _get_shader_type_list(p_shader_types)));
  7680. return ERR_PARSE_ERROR;
  7681. }
  7682. prev_pos = _get_tkpos();
  7683. tk = _get_token();
  7684. if (tk.type != TK_SEMICOLON) {
  7685. _set_tkpos(prev_pos);
  7686. _set_expected_after_error(";", "shader_type " + String(shader_type_identifier));
  7687. return ERR_PARSE_ERROR;
  7688. }
  7689. }
  7690. #ifdef DEBUG_ENABLED
  7691. keyword_completion_context = CF_GLOBAL_SPACE;
  7692. #endif // DEBUG_ENABLED
  7693. tk = _get_token();
  7694. int texture_uniforms = 0;
  7695. int texture_binding = 0;
  7696. int uniforms = 0;
  7697. int instance_index = 0;
  7698. int prop_index = 0;
  7699. #ifdef DEBUG_ENABLED
  7700. uint64_t uniform_buffer_size = 0;
  7701. uint64_t max_uniform_buffer_size = 0;
  7702. int uniform_buffer_exceeded_line = -1;
  7703. bool check_device_limit_warnings = false;
  7704. {
  7705. RenderingDevice *device = RenderingDevice::get_singleton();
  7706. if (device != nullptr) {
  7707. check_device_limit_warnings = check_warnings && HAS_WARNING(ShaderWarning::DEVICE_LIMIT_EXCEEDED_FLAG);
  7708. max_uniform_buffer_size = device->limit_get(RenderingDevice::LIMIT_MAX_UNIFORM_BUFFER_SIZE);
  7709. }
  7710. }
  7711. #endif // DEBUG_ENABLED
  7712. ShaderNode::Uniform::Scope uniform_scope = ShaderNode::Uniform::SCOPE_LOCAL;
  7713. stages = &p_functions;
  7714. is_supported_frag_only_funcs = shader_type_identifier == "canvas_item" || shader_type_identifier == "spatial" || shader_type_identifier == "sky";
  7715. const FunctionInfo &constants = p_functions.has("constants") ? p_functions["constants"] : FunctionInfo();
  7716. HashMap<String, String> defined_modes;
  7717. while (tk.type != TK_EOF) {
  7718. switch (tk.type) {
  7719. case TK_RENDER_MODE: {
  7720. #ifdef DEBUG_ENABLED
  7721. keyword_completion_context = CF_UNSPECIFIED;
  7722. #endif // DEBUG_ENABLED
  7723. while (true) {
  7724. StringName mode;
  7725. _get_completable_identifier(nullptr, COMPLETION_RENDER_MODE, mode);
  7726. if (mode == StringName()) {
  7727. _set_error(RTR("Expected an identifier for render mode."));
  7728. return ERR_PARSE_ERROR;
  7729. }
  7730. const String smode = String(mode);
  7731. if (shader->render_modes.has(mode)) {
  7732. _set_error(vformat(RTR("Duplicated render mode: '%s'."), smode));
  7733. return ERR_PARSE_ERROR;
  7734. }
  7735. bool found = false;
  7736. if (is_shader_inc) {
  7737. for (int i = 0; i < RenderingServer::SHADER_MAX; i++) {
  7738. const Vector<ModeInfo> modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(i));
  7739. for (int j = 0; j < modes.size(); j++) {
  7740. const ModeInfo &info = modes[j];
  7741. const String name = String(info.name);
  7742. if (smode.begins_with(name)) {
  7743. if (!info.options.is_empty()) {
  7744. if (info.options.has(smode.substr(name.length() + 1))) {
  7745. found = true;
  7746. if (defined_modes.has(name)) {
  7747. _set_error(vformat(RTR("Redefinition of render mode: '%s'. The '%s' mode has already been set to '%s'."), smode, name, defined_modes[name]));
  7748. return ERR_PARSE_ERROR;
  7749. }
  7750. defined_modes.insert(name, smode);
  7751. break;
  7752. }
  7753. } else {
  7754. found = true;
  7755. break;
  7756. }
  7757. }
  7758. }
  7759. }
  7760. } else {
  7761. for (int i = 0; i < p_render_modes.size(); i++) {
  7762. const ModeInfo &info = p_render_modes[i];
  7763. const String name = String(info.name);
  7764. if (smode.begins_with(name)) {
  7765. if (!info.options.is_empty()) {
  7766. if (info.options.has(smode.substr(name.length() + 1))) {
  7767. found = true;
  7768. if (defined_modes.has(name)) {
  7769. _set_error(vformat(RTR("Redefinition of render mode: '%s'. The '%s' mode has already been set to '%s'."), smode, name, defined_modes[name]));
  7770. return ERR_PARSE_ERROR;
  7771. }
  7772. defined_modes.insert(name, smode);
  7773. break;
  7774. }
  7775. } else {
  7776. found = true;
  7777. break;
  7778. }
  7779. }
  7780. }
  7781. }
  7782. if (!found) {
  7783. _set_error(vformat(RTR("Invalid render mode: '%s'."), smode));
  7784. return ERR_PARSE_ERROR;
  7785. }
  7786. shader->render_modes.push_back(mode);
  7787. tk = _get_token();
  7788. if (tk.type == TK_COMMA) {
  7789. //all good, do nothing
  7790. } else if (tk.type == TK_SEMICOLON) {
  7791. break; //done
  7792. } else {
  7793. _set_error(vformat(RTR("Unexpected token: '%s'."), get_token_text(tk)));
  7794. return ERR_PARSE_ERROR;
  7795. }
  7796. }
  7797. #ifdef DEBUG_ENABLED
  7798. keyword_completion_context = CF_GLOBAL_SPACE;
  7799. #endif // DEBUG_ENABLED
  7800. } break;
  7801. case TK_STRUCT: {
  7802. ShaderNode::Struct st;
  7803. DataType type;
  7804. #ifdef DEBUG_ENABLED
  7805. keyword_completion_context = CF_UNSPECIFIED;
  7806. #endif // DEBUG_ENABLED
  7807. tk = _get_token();
  7808. if (tk.type == TK_IDENTIFIER) {
  7809. st.name = tk.text;
  7810. if (shader->constants.has(st.name) || shader->structs.has(st.name)) {
  7811. _set_redefinition_error(String(st.name));
  7812. return ERR_PARSE_ERROR;
  7813. }
  7814. tk = _get_token();
  7815. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  7816. _set_expected_error("{");
  7817. return ERR_PARSE_ERROR;
  7818. }
  7819. } else {
  7820. _set_error(RTR("Expected a struct identifier."));
  7821. return ERR_PARSE_ERROR;
  7822. }
  7823. StructNode *st_node = alloc_node<StructNode>();
  7824. st.shader_struct = st_node;
  7825. int member_count = 0;
  7826. HashSet<String> member_names;
  7827. while (true) { // variables list
  7828. #ifdef DEBUG_ENABLED
  7829. keyword_completion_context = CF_DATATYPE | CF_PRECISION_MODIFIER;
  7830. #endif // DEBUG_ENABLED
  7831. tk = _get_token();
  7832. if (tk.type == TK_CURLY_BRACKET_CLOSE) {
  7833. break;
  7834. }
  7835. StringName struct_name = "";
  7836. bool struct_dt = false;
  7837. DataPrecision precision = PRECISION_DEFAULT;
  7838. if (tk.type == TK_STRUCT) {
  7839. _set_error(RTR("Nested structs are not allowed."));
  7840. return ERR_PARSE_ERROR;
  7841. }
  7842. if (is_token_precision(tk.type)) {
  7843. precision = get_token_precision(tk.type);
  7844. tk = _get_token();
  7845. #ifdef DEBUG_ENABLED
  7846. keyword_completion_context ^= CF_PRECISION_MODIFIER;
  7847. #endif // DEBUG_ENABLED
  7848. }
  7849. if (shader->structs.has(tk.text)) {
  7850. struct_name = tk.text;
  7851. #ifdef DEBUG_ENABLED
  7852. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_STRUCT_FLAG) && used_structs.has(struct_name)) {
  7853. used_structs[struct_name].used = true;
  7854. }
  7855. #endif // DEBUG_ENABLED
  7856. struct_dt = true;
  7857. }
  7858. if (!is_token_datatype(tk.type) && !struct_dt) {
  7859. _set_error(RTR("Expected data type."));
  7860. return ERR_PARSE_ERROR;
  7861. } else {
  7862. type = struct_dt ? TYPE_STRUCT : get_token_datatype(tk.type);
  7863. if (precision != PRECISION_DEFAULT && _validate_precision(type, precision) != OK) {
  7864. return ERR_PARSE_ERROR;
  7865. }
  7866. if (type == TYPE_VOID || is_sampler_type(type)) {
  7867. _set_error(vformat(RTR("A '%s' data type is not allowed here."), get_datatype_name(type)));
  7868. return ERR_PARSE_ERROR;
  7869. }
  7870. #ifdef DEBUG_ENABLED
  7871. keyword_completion_context = CF_UNSPECIFIED;
  7872. #endif // DEBUG_ENABLED
  7873. bool first = true;
  7874. bool fixed_array_size = false;
  7875. int array_size = 0;
  7876. do {
  7877. tk = _get_token();
  7878. if (first) {
  7879. first = false;
  7880. if (tk.type != TK_IDENTIFIER && tk.type != TK_BRACKET_OPEN) {
  7881. _set_error(RTR("Expected an identifier or '['."));
  7882. return ERR_PARSE_ERROR;
  7883. }
  7884. if (tk.type == TK_BRACKET_OPEN) {
  7885. Error error = _parse_array_size(nullptr, constants, true, nullptr, &array_size, nullptr);
  7886. if (error != OK) {
  7887. return error;
  7888. }
  7889. fixed_array_size = true;
  7890. tk = _get_token();
  7891. }
  7892. }
  7893. if (tk.type != TK_IDENTIFIER) {
  7894. _set_error(RTR("Expected an identifier."));
  7895. return ERR_PARSE_ERROR;
  7896. }
  7897. MemberNode *member = alloc_node<MemberNode>();
  7898. member->precision = precision;
  7899. member->datatype = type;
  7900. member->struct_name = struct_name;
  7901. member->name = tk.text;
  7902. member->array_size = array_size;
  7903. if (member_names.has(member->name)) {
  7904. _set_redefinition_error(String(member->name));
  7905. return ERR_PARSE_ERROR;
  7906. }
  7907. member_names.insert(member->name);
  7908. tk = _get_token();
  7909. if (tk.type == TK_BRACKET_OPEN) {
  7910. Error error = _parse_array_size(nullptr, constants, true, nullptr, &member->array_size, nullptr);
  7911. if (error != OK) {
  7912. return error;
  7913. }
  7914. tk = _get_token();
  7915. }
  7916. if (!fixed_array_size) {
  7917. array_size = 0;
  7918. }
  7919. if (tk.type != TK_SEMICOLON && tk.type != TK_COMMA) {
  7920. _set_expected_error(",", ";");
  7921. return ERR_PARSE_ERROR;
  7922. }
  7923. st_node->members.push_back(member);
  7924. member_count++;
  7925. } while (tk.type == TK_COMMA); // another member
  7926. }
  7927. }
  7928. if (member_count == 0) {
  7929. _set_error(RTR("Empty structs are not allowed."));
  7930. return ERR_PARSE_ERROR;
  7931. }
  7932. #ifdef DEBUG_ENABLED
  7933. keyword_completion_context = CF_UNSPECIFIED;
  7934. #endif // DEBUG_ENABLED
  7935. tk = _get_token();
  7936. if (tk.type != TK_SEMICOLON) {
  7937. _set_expected_error(";");
  7938. return ERR_PARSE_ERROR;
  7939. }
  7940. #ifdef DEBUG_ENABLED
  7941. keyword_completion_context = CF_GLOBAL_SPACE;
  7942. #endif // DEBUG_ENABLED
  7943. shader->structs[st.name] = st;
  7944. shader->vstructs.push_back(st); // struct's order is important!
  7945. #ifdef DEBUG_ENABLED
  7946. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_STRUCT_FLAG)) {
  7947. used_structs.insert(st.name, Usage(tk_line));
  7948. }
  7949. #endif // DEBUG_ENABLED
  7950. } break;
  7951. case TK_GLOBAL: {
  7952. #ifdef DEBUG_ENABLED
  7953. keyword_completion_context = CF_UNIFORM_KEYWORD;
  7954. if (_lookup_next(next)) {
  7955. if (next.type == TK_UNIFORM) {
  7956. keyword_completion_context ^= CF_UNIFORM_KEYWORD;
  7957. }
  7958. }
  7959. #endif // DEBUG_ENABLED
  7960. tk = _get_token();
  7961. if (tk.type != TK_UNIFORM) {
  7962. _set_expected_after_error("uniform", "global");
  7963. return ERR_PARSE_ERROR;
  7964. }
  7965. uniform_scope = ShaderNode::Uniform::SCOPE_GLOBAL;
  7966. };
  7967. [[fallthrough]];
  7968. case TK_INSTANCE: {
  7969. if (tk.type == TK_INSTANCE) {
  7970. #ifdef DEBUG_ENABLED
  7971. keyword_completion_context = CF_UNIFORM_KEYWORD;
  7972. if (_lookup_next(next)) {
  7973. if (next.type == TK_UNIFORM) {
  7974. keyword_completion_context ^= CF_UNIFORM_KEYWORD;
  7975. }
  7976. }
  7977. #endif // DEBUG_ENABLED
  7978. if (shader_type_identifier != StringName() && String(shader_type_identifier) != "spatial") {
  7979. _set_error(vformat(RTR("Uniform instances are not yet implemented for '%s' shaders."), shader_type_identifier));
  7980. return ERR_PARSE_ERROR;
  7981. }
  7982. if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  7983. _set_error(RTR("Uniform instances are not supported in gl_compatibility shaders."));
  7984. return ERR_PARSE_ERROR;
  7985. }
  7986. if (uniform_scope == ShaderNode::Uniform::SCOPE_LOCAL) {
  7987. tk = _get_token();
  7988. if (tk.type != TK_UNIFORM) {
  7989. _set_expected_after_error("uniform", "instance");
  7990. return ERR_PARSE_ERROR;
  7991. }
  7992. uniform_scope = ShaderNode::Uniform::SCOPE_INSTANCE;
  7993. }
  7994. }
  7995. };
  7996. [[fallthrough]];
  7997. case TK_UNIFORM:
  7998. case TK_VARYING: {
  7999. bool is_uniform = tk.type == TK_UNIFORM;
  8000. #ifdef DEBUG_ENABLED
  8001. keyword_completion_context = CF_UNSPECIFIED;
  8002. #endif // DEBUG_ENABLED
  8003. if (!is_uniform) {
  8004. if (shader_type_identifier == "particles" || shader_type_identifier == "sky" || shader_type_identifier == "fog") {
  8005. _set_error(vformat(RTR("Varyings cannot be used in '%s' shaders."), shader_type_identifier));
  8006. return ERR_PARSE_ERROR;
  8007. }
  8008. }
  8009. DataPrecision precision = PRECISION_DEFAULT;
  8010. DataInterpolation interpolation = INTERPOLATION_DEFAULT;
  8011. DataType type;
  8012. StringName name;
  8013. int array_size = 0;
  8014. tk = _get_token();
  8015. #ifdef DEBUG_ENABLED
  8016. bool temp_error = false;
  8017. uint32_t datatype_flag;
  8018. if (!is_uniform) {
  8019. datatype_flag = CF_VARYING_TYPE;
  8020. keyword_completion_context = CF_INTERPOLATION_QUALIFIER | CF_PRECISION_MODIFIER | datatype_flag;
  8021. if (_lookup_next(next)) {
  8022. if (is_token_interpolation(next.type)) {
  8023. keyword_completion_context ^= (CF_INTERPOLATION_QUALIFIER | datatype_flag);
  8024. } else if (is_token_precision(next.type)) {
  8025. keyword_completion_context ^= (CF_PRECISION_MODIFIER | datatype_flag);
  8026. } else if (is_token_datatype(next.type)) {
  8027. keyword_completion_context ^= datatype_flag;
  8028. }
  8029. }
  8030. } else {
  8031. datatype_flag = CF_UNIFORM_TYPE;
  8032. keyword_completion_context = CF_PRECISION_MODIFIER | datatype_flag;
  8033. if (_lookup_next(next)) {
  8034. if (is_token_precision(next.type)) {
  8035. keyword_completion_context ^= (CF_PRECISION_MODIFIER | datatype_flag);
  8036. } else if (is_token_datatype(next.type)) {
  8037. keyword_completion_context ^= datatype_flag;
  8038. }
  8039. }
  8040. }
  8041. #endif // DEBUG_ENABLED
  8042. if (is_token_interpolation(tk.type)) {
  8043. if (is_uniform) {
  8044. _set_error(RTR("Interpolation qualifiers are not supported for uniforms."));
  8045. #ifdef DEBUG_ENABLED
  8046. temp_error = true;
  8047. #else
  8048. return ERR_PARSE_ERROR;
  8049. #endif // DEBUG_ENABLED
  8050. }
  8051. interpolation = get_token_interpolation(tk.type);
  8052. tk = _get_token();
  8053. #ifdef DEBUG_ENABLED
  8054. if (keyword_completion_context & CF_INTERPOLATION_QUALIFIER) {
  8055. keyword_completion_context ^= CF_INTERPOLATION_QUALIFIER;
  8056. }
  8057. if (_lookup_next(next)) {
  8058. if (is_token_precision(next.type)) {
  8059. keyword_completion_context ^= CF_PRECISION_MODIFIER;
  8060. } else if (is_token_datatype(next.type)) {
  8061. keyword_completion_context ^= datatype_flag;
  8062. }
  8063. }
  8064. if (temp_error) {
  8065. return ERR_PARSE_ERROR;
  8066. }
  8067. #endif // DEBUG_ENABLED
  8068. }
  8069. if (is_token_precision(tk.type)) {
  8070. precision = get_token_precision(tk.type);
  8071. tk = _get_token();
  8072. #ifdef DEBUG_ENABLED
  8073. if (keyword_completion_context & CF_INTERPOLATION_QUALIFIER) {
  8074. keyword_completion_context ^= CF_INTERPOLATION_QUALIFIER;
  8075. }
  8076. if (keyword_completion_context & CF_PRECISION_MODIFIER) {
  8077. keyword_completion_context ^= CF_PRECISION_MODIFIER;
  8078. }
  8079. if (_lookup_next(next)) {
  8080. if (is_token_datatype(next.type)) {
  8081. keyword_completion_context = CF_UNSPECIFIED;
  8082. }
  8083. }
  8084. #endif // DEBUG_ENABLED
  8085. }
  8086. if (shader->structs.has(tk.text)) {
  8087. if (is_uniform) {
  8088. _set_error(vformat(RTR("The '%s' data type is not supported for uniforms."), "struct"));
  8089. return ERR_PARSE_ERROR;
  8090. } else {
  8091. _set_error(vformat(RTR("The '%s' data type is not allowed here."), "struct"));
  8092. return ERR_PARSE_ERROR;
  8093. }
  8094. }
  8095. if (!is_token_datatype(tk.type)) {
  8096. _set_error(RTR("Expected data type."));
  8097. return ERR_PARSE_ERROR;
  8098. }
  8099. type = get_token_datatype(tk.type);
  8100. if (precision != PRECISION_DEFAULT && _validate_precision(type, precision) != OK) {
  8101. return ERR_PARSE_ERROR;
  8102. }
  8103. if (type == TYPE_VOID) {
  8104. _set_error(vformat(RTR("The '%s' data type is not allowed here."), "void"));
  8105. return ERR_PARSE_ERROR;
  8106. }
  8107. if (!is_uniform && interpolation != INTERPOLATION_DEFAULT && type < TYPE_INT) {
  8108. _set_error(vformat(RTR("Interpolation modifier '%s' cannot be used with boolean types."), get_interpolation_name(interpolation)));
  8109. return ERR_PARSE_ERROR;
  8110. }
  8111. if (!is_uniform && type > TYPE_MAT4) {
  8112. _set_error(RTR("Invalid data type for varying."));
  8113. return ERR_PARSE_ERROR;
  8114. }
  8115. #ifdef DEBUG_ENABLED
  8116. keyword_completion_context = CF_UNSPECIFIED;
  8117. #endif // DEBUG_ENABLED
  8118. tk = _get_token();
  8119. if (tk.type != TK_IDENTIFIER && tk.type != TK_BRACKET_OPEN) {
  8120. _set_error(RTR("Expected an identifier or '['."));
  8121. return ERR_PARSE_ERROR;
  8122. }
  8123. if (tk.type == TK_BRACKET_OPEN) {
  8124. Error error = _parse_array_size(nullptr, constants, true, nullptr, &array_size, nullptr);
  8125. if (error != OK) {
  8126. return error;
  8127. }
  8128. tk = _get_token();
  8129. }
  8130. if (tk.type != TK_IDENTIFIER) {
  8131. _set_error(RTR("Expected an identifier."));
  8132. return ERR_PARSE_ERROR;
  8133. }
  8134. prev_pos = _get_tkpos();
  8135. name = tk.text;
  8136. if (_find_identifier(nullptr, false, constants, name)) {
  8137. _set_redefinition_error(String(name));
  8138. return ERR_PARSE_ERROR;
  8139. }
  8140. if (has_builtin(p_functions, name)) {
  8141. _set_redefinition_error(String(name));
  8142. return ERR_PARSE_ERROR;
  8143. }
  8144. if (is_uniform) {
  8145. if (uniform_scope == ShaderNode::Uniform::SCOPE_GLOBAL && Engine::get_singleton()->is_editor_hint()) { // Type checking for global uniforms is not allowed outside the editor.
  8146. //validate global uniform
  8147. DataType gvtype = global_shader_uniform_get_type_func(name);
  8148. if (gvtype == TYPE_MAX) {
  8149. _set_error(vformat(RTR("Global uniform '%s' does not exist. Create it in Project Settings."), String(name)));
  8150. return ERR_PARSE_ERROR;
  8151. }
  8152. if (type != gvtype) {
  8153. _set_error(vformat(RTR("Global uniform '%s' must be of type '%s'."), String(name), get_datatype_name(gvtype)));
  8154. return ERR_PARSE_ERROR;
  8155. }
  8156. }
  8157. ShaderNode::Uniform uniform;
  8158. uniform.type = type;
  8159. uniform.scope = uniform_scope;
  8160. uniform.precision = precision;
  8161. uniform.array_size = array_size;
  8162. uniform.group = current_uniform_group_name;
  8163. uniform.subgroup = current_uniform_subgroup_name;
  8164. tk = _get_token();
  8165. if (tk.type == TK_BRACKET_OPEN) {
  8166. Error error = _parse_array_size(nullptr, constants, true, nullptr, &uniform.array_size, nullptr);
  8167. if (error != OK) {
  8168. return error;
  8169. }
  8170. tk = _get_token();
  8171. }
  8172. if (is_sampler_type(type)) {
  8173. if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE) {
  8174. _set_error(vformat(RTR("The '%s' qualifier is not supported for sampler types."), "SCOPE_INSTANCE"));
  8175. return ERR_PARSE_ERROR;
  8176. }
  8177. uniform.texture_order = texture_uniforms++;
  8178. uniform.texture_binding = texture_binding;
  8179. if (uniform.array_size > 0) {
  8180. texture_binding += uniform.array_size;
  8181. } else {
  8182. ++texture_binding;
  8183. }
  8184. uniform.order = -1;
  8185. uniform.prop_order = prop_index++;
  8186. } else {
  8187. if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE && (type == TYPE_MAT2 || type == TYPE_MAT3 || type == TYPE_MAT4)) {
  8188. _set_error(vformat(RTR("The '%s' qualifier is not supported for matrix types."), "SCOPE_INSTANCE"));
  8189. return ERR_PARSE_ERROR;
  8190. }
  8191. uniform.texture_order = -1;
  8192. if (uniform_scope != ShaderNode::Uniform::SCOPE_INSTANCE) {
  8193. uniform.order = uniforms++;
  8194. uniform.prop_order = prop_index++;
  8195. #ifdef DEBUG_ENABLED
  8196. if (check_device_limit_warnings) {
  8197. if (uniform.array_size > 0) {
  8198. int size = get_datatype_size(uniform.type) * uniform.array_size;
  8199. int m = (16 * uniform.array_size);
  8200. if ((size % m) != 0U) {
  8201. size += m - (size % m);
  8202. }
  8203. uniform_buffer_size += size;
  8204. } else {
  8205. uniform_buffer_size += get_datatype_size(uniform.type);
  8206. }
  8207. if (uniform_buffer_exceeded_line == -1 && uniform_buffer_size > max_uniform_buffer_size) {
  8208. uniform_buffer_exceeded_line = tk_line;
  8209. }
  8210. }
  8211. #endif // DEBUG_ENABLED
  8212. }
  8213. }
  8214. if (uniform.array_size > 0) {
  8215. if (uniform_scope == ShaderNode::Uniform::SCOPE_GLOBAL) {
  8216. _set_error(vformat(RTR("The '%s' qualifier is not supported for uniform arrays."), "SCOPE_GLOBAL"));
  8217. return ERR_PARSE_ERROR;
  8218. }
  8219. if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE) {
  8220. _set_error(vformat(RTR("The '%s' qualifier is not supported for uniform arrays."), "SCOPE_INSTANCE"));
  8221. return ERR_PARSE_ERROR;
  8222. }
  8223. }
  8224. int custom_instance_index = -1;
  8225. if (tk.type == TK_COLON) {
  8226. completion_type = COMPLETION_HINT;
  8227. completion_base = type;
  8228. completion_base_array = uniform.array_size > 0;
  8229. //hint
  8230. do {
  8231. tk = _get_token();
  8232. completion_line = tk.line;
  8233. if (!is_token_hint(tk.type)) {
  8234. _set_error(RTR("Expected valid type hint after ':'."));
  8235. return ERR_PARSE_ERROR;
  8236. }
  8237. if (uniform.array_size > 0) {
  8238. static Vector<int> supported_hints = {
  8239. TK_HINT_SOURCE_COLOR, TK_REPEAT_DISABLE, TK_REPEAT_ENABLE,
  8240. TK_FILTER_LINEAR, TK_FILTER_LINEAR_MIPMAP, TK_FILTER_LINEAR_MIPMAP_ANISOTROPIC,
  8241. TK_FILTER_NEAREST, TK_FILTER_NEAREST_MIPMAP, TK_FILTER_NEAREST_MIPMAP_ANISOTROPIC
  8242. };
  8243. if (!supported_hints.has(tk.type)) {
  8244. _set_error(RTR("This hint is not supported for uniform arrays."));
  8245. return ERR_PARSE_ERROR;
  8246. }
  8247. }
  8248. ShaderNode::Uniform::Hint new_hint = ShaderNode::Uniform::HINT_NONE;
  8249. TextureFilter new_filter = FILTER_DEFAULT;
  8250. TextureRepeat new_repeat = REPEAT_DEFAULT;
  8251. switch (tk.type) {
  8252. case TK_HINT_SOURCE_COLOR: {
  8253. if (type != TYPE_VEC3 && type != TYPE_VEC4 && !is_sampler_type(type)) {
  8254. _set_error(vformat(RTR("Source color hint is for '%s', '%s' or sampler types only."), "vec3", "vec4"));
  8255. return ERR_PARSE_ERROR;
  8256. }
  8257. if (is_sampler_type(type)) {
  8258. if (uniform.use_color) {
  8259. _set_error(vformat(RTR("Duplicated hint: '%s'."), "source_color"));
  8260. return ERR_PARSE_ERROR;
  8261. }
  8262. uniform.use_color = true;
  8263. } else {
  8264. new_hint = ShaderNode::Uniform::HINT_SOURCE_COLOR;
  8265. }
  8266. } break;
  8267. case TK_HINT_DEFAULT_BLACK_TEXTURE: {
  8268. new_hint = ShaderNode::Uniform::HINT_DEFAULT_BLACK;
  8269. } break;
  8270. case TK_HINT_DEFAULT_WHITE_TEXTURE: {
  8271. new_hint = ShaderNode::Uniform::HINT_DEFAULT_WHITE;
  8272. } break;
  8273. case TK_HINT_DEFAULT_TRANSPARENT_TEXTURE: {
  8274. new_hint = ShaderNode::Uniform::HINT_DEFAULT_TRANSPARENT;
  8275. } break;
  8276. case TK_HINT_NORMAL_TEXTURE: {
  8277. new_hint = ShaderNode::Uniform::HINT_NORMAL;
  8278. } break;
  8279. case TK_HINT_ROUGHNESS_NORMAL_TEXTURE: {
  8280. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL;
  8281. } break;
  8282. case TK_HINT_ROUGHNESS_R: {
  8283. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_R;
  8284. } break;
  8285. case TK_HINT_ROUGHNESS_G: {
  8286. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_G;
  8287. } break;
  8288. case TK_HINT_ROUGHNESS_B: {
  8289. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_B;
  8290. } break;
  8291. case TK_HINT_ROUGHNESS_A: {
  8292. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_A;
  8293. } break;
  8294. case TK_HINT_ROUGHNESS_GRAY: {
  8295. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_GRAY;
  8296. } break;
  8297. case TK_HINT_ANISOTROPY_TEXTURE: {
  8298. new_hint = ShaderNode::Uniform::HINT_ANISOTROPY;
  8299. } break;
  8300. case TK_HINT_RANGE: {
  8301. if (type != TYPE_FLOAT && type != TYPE_INT) {
  8302. _set_error(vformat(RTR("Range hint is for '%s' and '%s' only."), "float", "int"));
  8303. return ERR_PARSE_ERROR;
  8304. }
  8305. tk = _get_token();
  8306. if (tk.type != TK_PARENTHESIS_OPEN) {
  8307. _set_expected_after_error("(", "hint_range");
  8308. return ERR_PARSE_ERROR;
  8309. }
  8310. tk = _get_token();
  8311. float sign = 1.0;
  8312. if (tk.type == TK_OP_SUB) {
  8313. sign = -1.0;
  8314. tk = _get_token();
  8315. }
  8316. if (tk.type != TK_FLOAT_CONSTANT && !tk.is_integer_constant()) {
  8317. _set_error(RTR("Expected an integer constant."));
  8318. return ERR_PARSE_ERROR;
  8319. }
  8320. uniform.hint_range[0] = tk.constant;
  8321. uniform.hint_range[0] *= sign;
  8322. tk = _get_token();
  8323. if (tk.type != TK_COMMA) {
  8324. _set_error(RTR("Expected ',' after integer constant."));
  8325. return ERR_PARSE_ERROR;
  8326. }
  8327. tk = _get_token();
  8328. sign = 1.0;
  8329. if (tk.type == TK_OP_SUB) {
  8330. sign = -1.0;
  8331. tk = _get_token();
  8332. }
  8333. if (tk.type != TK_FLOAT_CONSTANT && !tk.is_integer_constant()) {
  8334. _set_error(RTR("Expected an integer constant after ','."));
  8335. return ERR_PARSE_ERROR;
  8336. }
  8337. uniform.hint_range[1] = tk.constant;
  8338. uniform.hint_range[1] *= sign;
  8339. tk = _get_token();
  8340. if (tk.type == TK_COMMA) {
  8341. tk = _get_token();
  8342. if (tk.type != TK_FLOAT_CONSTANT && !tk.is_integer_constant()) {
  8343. _set_error(RTR("Expected an integer constant after ','."));
  8344. return ERR_PARSE_ERROR;
  8345. }
  8346. uniform.hint_range[2] = tk.constant;
  8347. tk = _get_token();
  8348. } else {
  8349. if (type == TYPE_INT) {
  8350. uniform.hint_range[2] = 1;
  8351. } else {
  8352. uniform.hint_range[2] = 0.001;
  8353. }
  8354. }
  8355. if (tk.type != TK_PARENTHESIS_CLOSE) {
  8356. _set_expected_error(")");
  8357. return ERR_PARSE_ERROR;
  8358. }
  8359. new_hint = ShaderNode::Uniform::HINT_RANGE;
  8360. } break;
  8361. case TK_HINT_ENUM: {
  8362. if (type != TYPE_INT) {
  8363. _set_error(vformat(RTR("Enum hint is for '%s' only."), "int"));
  8364. return ERR_PARSE_ERROR;
  8365. }
  8366. tk = _get_token();
  8367. if (tk.type != TK_PARENTHESIS_OPEN) {
  8368. _set_expected_after_error("(", "hint_enum");
  8369. return ERR_PARSE_ERROR;
  8370. }
  8371. while (true) {
  8372. tk = _get_token();
  8373. if (tk.type != TK_STRING_CONSTANT) {
  8374. _set_error(RTR("Expected a string constant."));
  8375. return ERR_PARSE_ERROR;
  8376. }
  8377. uniform.hint_enum_names.push_back(tk.text);
  8378. tk = _get_token();
  8379. if (tk.type == TK_PARENTHESIS_CLOSE) {
  8380. break;
  8381. } else if (tk.type != TK_COMMA) {
  8382. _set_error(RTR("Expected ',' or ')' after string constant."));
  8383. return ERR_PARSE_ERROR;
  8384. }
  8385. }
  8386. new_hint = ShaderNode::Uniform::HINT_ENUM;
  8387. } break;
  8388. case TK_HINT_INSTANCE_INDEX: {
  8389. if (custom_instance_index != -1) {
  8390. _set_error(vformat(RTR("Can only specify '%s' once."), "instance_index"));
  8391. return ERR_PARSE_ERROR;
  8392. }
  8393. tk = _get_token();
  8394. if (tk.type != TK_PARENTHESIS_OPEN) {
  8395. _set_expected_after_error("(", "instance_index");
  8396. return ERR_PARSE_ERROR;
  8397. }
  8398. tk = _get_token();
  8399. if (tk.type == TK_OP_SUB) {
  8400. _set_error(RTR("The instance index can't be negative."));
  8401. return ERR_PARSE_ERROR;
  8402. }
  8403. if (!tk.is_integer_constant()) {
  8404. _set_error(RTR("Expected an integer constant."));
  8405. return ERR_PARSE_ERROR;
  8406. }
  8407. custom_instance_index = tk.constant;
  8408. current_uniform_instance_index_defined = true;
  8409. if (custom_instance_index >= MAX_INSTANCE_UNIFORM_INDICES) {
  8410. _set_error(vformat(RTR("Allowed instance uniform indices must be within [0..%d] range."), MAX_INSTANCE_UNIFORM_INDICES - 1));
  8411. return ERR_PARSE_ERROR;
  8412. }
  8413. tk = _get_token();
  8414. if (tk.type != TK_PARENTHESIS_CLOSE) {
  8415. _set_expected_error(")");
  8416. return ERR_PARSE_ERROR;
  8417. }
  8418. } break;
  8419. case TK_HINT_SCREEN_TEXTURE: {
  8420. new_hint = ShaderNode::Uniform::HINT_SCREEN_TEXTURE;
  8421. --texture_uniforms;
  8422. --texture_binding;
  8423. } break;
  8424. case TK_HINT_NORMAL_ROUGHNESS_TEXTURE: {
  8425. new_hint = ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE;
  8426. --texture_uniforms;
  8427. --texture_binding;
  8428. if (OS::get_singleton()->get_current_rendering_method() != "forward_plus") {
  8429. _set_error(RTR("'hint_normal_roughness_texture' is only available when using the Forward+ backend."));
  8430. return ERR_PARSE_ERROR;
  8431. }
  8432. if (shader_type_identifier != StringName() && String(shader_type_identifier) != "spatial") {
  8433. _set_error(vformat(RTR("'hint_normal_roughness_texture' is not supported in '%s' shaders."), shader_type_identifier));
  8434. return ERR_PARSE_ERROR;
  8435. }
  8436. } break;
  8437. case TK_HINT_DEPTH_TEXTURE: {
  8438. new_hint = ShaderNode::Uniform::HINT_DEPTH_TEXTURE;
  8439. --texture_uniforms;
  8440. --texture_binding;
  8441. if (shader_type_identifier != StringName() && String(shader_type_identifier) != "spatial") {
  8442. _set_error(vformat(RTR("'hint_depth_texture' is not supported in '%s' shaders."), shader_type_identifier));
  8443. return ERR_PARSE_ERROR;
  8444. }
  8445. } break;
  8446. case TK_FILTER_NEAREST: {
  8447. new_filter = FILTER_NEAREST;
  8448. } break;
  8449. case TK_FILTER_LINEAR: {
  8450. new_filter = FILTER_LINEAR;
  8451. } break;
  8452. case TK_FILTER_NEAREST_MIPMAP: {
  8453. new_filter = FILTER_NEAREST_MIPMAP;
  8454. } break;
  8455. case TK_FILTER_LINEAR_MIPMAP: {
  8456. new_filter = FILTER_LINEAR_MIPMAP;
  8457. } break;
  8458. case TK_FILTER_NEAREST_MIPMAP_ANISOTROPIC: {
  8459. new_filter = FILTER_NEAREST_MIPMAP_ANISOTROPIC;
  8460. } break;
  8461. case TK_FILTER_LINEAR_MIPMAP_ANISOTROPIC: {
  8462. new_filter = FILTER_LINEAR_MIPMAP_ANISOTROPIC;
  8463. } break;
  8464. case TK_REPEAT_DISABLE: {
  8465. new_repeat = REPEAT_DISABLE;
  8466. } break;
  8467. case TK_REPEAT_ENABLE: {
  8468. new_repeat = REPEAT_ENABLE;
  8469. } break;
  8470. default:
  8471. break;
  8472. }
  8473. bool is_sampler_hint = new_hint != ShaderNode::Uniform::HINT_NONE && new_hint != ShaderNode::Uniform::HINT_SOURCE_COLOR && new_hint != ShaderNode::Uniform::HINT_RANGE && new_hint != ShaderNode::Uniform::HINT_ENUM;
  8474. if (((new_filter != FILTER_DEFAULT || new_repeat != REPEAT_DEFAULT) || is_sampler_hint) && !is_sampler_type(type)) {
  8475. _set_error(RTR("This hint is only for sampler types."));
  8476. return ERR_PARSE_ERROR;
  8477. }
  8478. if (new_hint != ShaderNode::Uniform::HINT_NONE) {
  8479. if (uniform.hint != ShaderNode::Uniform::HINT_NONE) {
  8480. if (uniform.hint == new_hint) {
  8481. _set_error(vformat(RTR("Duplicated hint: '%s'."), get_uniform_hint_name(new_hint)));
  8482. } else {
  8483. _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)));
  8484. }
  8485. return ERR_PARSE_ERROR;
  8486. } else {
  8487. uniform.hint = new_hint;
  8488. current_uniform_hint = new_hint;
  8489. }
  8490. }
  8491. if (new_filter != FILTER_DEFAULT) {
  8492. if (uniform.filter != FILTER_DEFAULT) {
  8493. if (uniform.filter == new_filter) {
  8494. _set_error(vformat(RTR("Duplicated filter mode: '%s'."), get_texture_filter_name(new_filter)));
  8495. } else {
  8496. _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)));
  8497. }
  8498. return ERR_PARSE_ERROR;
  8499. } else {
  8500. uniform.filter = new_filter;
  8501. current_uniform_filter = new_filter;
  8502. }
  8503. }
  8504. if (new_repeat != REPEAT_DEFAULT) {
  8505. if (uniform.repeat != REPEAT_DEFAULT) {
  8506. if (uniform.repeat == new_repeat) {
  8507. _set_error(vformat(RTR("Duplicated repeat mode: '%s'."), get_texture_repeat_name(new_repeat)));
  8508. } else {
  8509. _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)));
  8510. }
  8511. return ERR_PARSE_ERROR;
  8512. } else {
  8513. uniform.repeat = new_repeat;
  8514. current_uniform_repeat = new_repeat;
  8515. }
  8516. }
  8517. tk = _get_token();
  8518. } while (tk.type == TK_COMMA);
  8519. }
  8520. if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE) {
  8521. if (custom_instance_index >= 0) {
  8522. uniform.instance_index = custom_instance_index;
  8523. } else {
  8524. uniform.instance_index = instance_index++;
  8525. if (instance_index > MAX_INSTANCE_UNIFORM_INDICES) {
  8526. _set_error(vformat(RTR("Too many '%s' uniforms in shader, maximum supported is %d."), "instance", MAX_INSTANCE_UNIFORM_INDICES));
  8527. return ERR_PARSE_ERROR;
  8528. }
  8529. }
  8530. }
  8531. //reset scope for next uniform
  8532. if (tk.type == TK_OP_ASSIGN) {
  8533. if (uniform.array_size > 0) {
  8534. _set_error(RTR("Setting default values to uniform arrays is not supported."));
  8535. return ERR_PARSE_ERROR;
  8536. }
  8537. Node *expr = _parse_and_reduce_expression(nullptr, constants);
  8538. if (!expr) {
  8539. return ERR_PARSE_ERROR;
  8540. }
  8541. if (expr->type != Node::NODE_TYPE_CONSTANT) {
  8542. _set_error(RTR("Expected constant expression after '='."));
  8543. return ERR_PARSE_ERROR;
  8544. }
  8545. ConstantNode *cn = static_cast<ConstantNode *>(expr);
  8546. uniform.default_value.resize(cn->values.size());
  8547. if (!convert_constant(cn, uniform.type, uniform.default_value.ptrw())) {
  8548. _set_error(vformat(RTR("Can't convert constant to '%s'."), get_datatype_name(uniform.type)));
  8549. return ERR_PARSE_ERROR;
  8550. }
  8551. tk = _get_token();
  8552. }
  8553. shader->uniforms[name] = uniform;
  8554. #ifdef DEBUG_ENABLED
  8555. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_UNIFORM_FLAG)) {
  8556. used_uniforms.insert(name, Usage(tk_line));
  8557. }
  8558. #endif // DEBUG_ENABLED
  8559. //reset scope for next uniform
  8560. uniform_scope = ShaderNode::Uniform::SCOPE_LOCAL;
  8561. if (tk.type != TK_SEMICOLON) {
  8562. _set_expected_error(";");
  8563. return ERR_PARSE_ERROR;
  8564. }
  8565. #ifdef DEBUG_ENABLED
  8566. keyword_completion_context = CF_GLOBAL_SPACE;
  8567. #endif // DEBUG_ENABLED
  8568. completion_type = COMPLETION_NONE;
  8569. current_uniform_hint = ShaderNode::Uniform::HINT_NONE;
  8570. current_uniform_filter = FILTER_DEFAULT;
  8571. current_uniform_repeat = REPEAT_DEFAULT;
  8572. current_uniform_instance_index_defined = false;
  8573. } else { // varying
  8574. ShaderNode::Varying varying;
  8575. varying.type = type;
  8576. varying.precision = precision;
  8577. varying.interpolation = interpolation;
  8578. varying.tkpos = prev_pos;
  8579. varying.array_size = array_size;
  8580. tk = _get_token();
  8581. if (tk.type != TK_SEMICOLON && tk.type != TK_BRACKET_OPEN) {
  8582. if (array_size == 0) {
  8583. _set_expected_error(";", "[");
  8584. } else {
  8585. _set_expected_error(";");
  8586. }
  8587. return ERR_PARSE_ERROR;
  8588. }
  8589. if (tk.type == TK_BRACKET_OPEN) {
  8590. Error error = _parse_array_size(nullptr, constants, true, nullptr, &varying.array_size, nullptr);
  8591. if (error != OK) {
  8592. return error;
  8593. }
  8594. tk = _get_token();
  8595. }
  8596. shader->varyings[name] = varying;
  8597. #ifdef DEBUG_ENABLED
  8598. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_VARYING_FLAG)) {
  8599. used_varyings.insert(name, Usage(tk_line));
  8600. }
  8601. #endif // DEBUG_ENABLED
  8602. }
  8603. } break;
  8604. case TK_UNIFORM_GROUP: {
  8605. tk = _get_token();
  8606. if (tk.type == TK_IDENTIFIER) {
  8607. current_uniform_group_name = tk.text;
  8608. current_uniform_subgroup_name = "";
  8609. tk = _get_token();
  8610. if (tk.type == TK_PERIOD) {
  8611. tk = _get_token();
  8612. if (tk.type == TK_IDENTIFIER) {
  8613. current_uniform_subgroup_name = tk.text;
  8614. tk = _get_token();
  8615. if (tk.type != TK_SEMICOLON) {
  8616. _set_expected_error(";");
  8617. return ERR_PARSE_ERROR;
  8618. }
  8619. } else {
  8620. _set_error(RTR("Expected an uniform subgroup identifier."));
  8621. return ERR_PARSE_ERROR;
  8622. }
  8623. } else if (tk.type != TK_SEMICOLON) {
  8624. _set_expected_error(";", ".");
  8625. return ERR_PARSE_ERROR;
  8626. }
  8627. } else {
  8628. if (tk.type != TK_SEMICOLON) {
  8629. if (current_uniform_group_name.is_empty()) {
  8630. _set_error(RTR("Expected an uniform group identifier."));
  8631. } else {
  8632. _set_error(RTR("Expected an uniform group identifier or `;`."));
  8633. }
  8634. return ERR_PARSE_ERROR;
  8635. } else if (current_uniform_group_name.is_empty()) {
  8636. _set_error(RTR("Group needs to be opened before."));
  8637. return ERR_PARSE_ERROR;
  8638. } else {
  8639. current_uniform_group_name = "";
  8640. current_uniform_subgroup_name = "";
  8641. }
  8642. }
  8643. } break;
  8644. case TK_SHADER_TYPE: {
  8645. _set_error(RTR("Shader type is already defined."));
  8646. return ERR_PARSE_ERROR;
  8647. } break;
  8648. default: {
  8649. //function or constant variable
  8650. bool is_constant = false;
  8651. bool is_struct = false;
  8652. StringName struct_name;
  8653. DataPrecision precision = PRECISION_DEFAULT;
  8654. DataType type;
  8655. StringName name;
  8656. int array_size = 0;
  8657. if (tk.type == TK_CONST) {
  8658. is_constant = true;
  8659. tk = _get_token();
  8660. }
  8661. if (is_token_precision(tk.type)) {
  8662. precision = get_token_precision(tk.type);
  8663. tk = _get_token();
  8664. }
  8665. if (shader->structs.has(tk.text)) {
  8666. is_struct = true;
  8667. struct_name = tk.text;
  8668. } else {
  8669. #ifdef DEBUG_ENABLED
  8670. if (_lookup_next(next)) {
  8671. if (next.type == TK_UNIFORM) {
  8672. keyword_completion_context = CF_UNIFORM_QUALIFIER;
  8673. }
  8674. }
  8675. #endif // DEBUG_ENABLED
  8676. if (!is_token_datatype(tk.type)) {
  8677. _set_error(RTR("Expected constant, function, uniform or varying."));
  8678. return ERR_PARSE_ERROR;
  8679. }
  8680. if (!is_token_variable_datatype(tk.type)) {
  8681. if (is_constant) {
  8682. _set_error(RTR("Invalid constant type (samplers are not allowed)."));
  8683. } else {
  8684. _set_error(RTR("Invalid function type (samplers are not allowed)."));
  8685. }
  8686. return ERR_PARSE_ERROR;
  8687. }
  8688. }
  8689. if (is_struct) {
  8690. type = TYPE_STRUCT;
  8691. } else {
  8692. type = get_token_datatype(tk.type);
  8693. }
  8694. if (precision != PRECISION_DEFAULT && _validate_precision(type, precision) != OK) {
  8695. return ERR_PARSE_ERROR;
  8696. }
  8697. prev_pos = _get_tkpos();
  8698. tk = _get_token();
  8699. #ifdef DEBUG_ENABLED
  8700. keyword_completion_context = CF_UNSPECIFIED;
  8701. #endif // DEBUG_ENABLED
  8702. bool unknown_size = false;
  8703. bool fixed_array_size = false;
  8704. if (tk.type == TK_BRACKET_OPEN) {
  8705. Error error = _parse_array_size(nullptr, constants, !is_constant, nullptr, &array_size, &unknown_size);
  8706. if (error != OK) {
  8707. return error;
  8708. }
  8709. fixed_array_size = true;
  8710. prev_pos = _get_tkpos();
  8711. }
  8712. _set_tkpos(prev_pos);
  8713. _get_completable_identifier(nullptr, COMPLETION_MAIN_FUNCTION, name);
  8714. if (name == StringName()) {
  8715. if (is_constant) {
  8716. _set_error(RTR("Expected an identifier or '[' after type."));
  8717. } else {
  8718. _set_error(RTR("Expected a function name after type."));
  8719. }
  8720. return ERR_PARSE_ERROR;
  8721. }
  8722. if (shader->structs.has(name) || _find_identifier(nullptr, false, constants, name) || has_builtin(p_functions, name, !is_constant)) {
  8723. _set_redefinition_error(String(name));
  8724. return ERR_PARSE_ERROR;
  8725. }
  8726. tk = _get_token();
  8727. if (tk.type != TK_PARENTHESIS_OPEN) {
  8728. if (type == TYPE_VOID) {
  8729. _set_error(RTR("Expected '(' after function identifier."));
  8730. return ERR_PARSE_ERROR;
  8731. }
  8732. //variable
  8733. while (true) {
  8734. ShaderNode::Constant constant;
  8735. constant.name = name;
  8736. constant.type = is_struct ? TYPE_STRUCT : type;
  8737. constant.struct_name = struct_name;
  8738. constant.precision = precision;
  8739. constant.initializer = nullptr;
  8740. constant.array_size = array_size;
  8741. is_const_decl = true;
  8742. if (tk.type == TK_BRACKET_OPEN) {
  8743. Error error = _parse_array_size(nullptr, constants, false, nullptr, &constant.array_size, &unknown_size);
  8744. if (error != OK) {
  8745. return error;
  8746. }
  8747. tk = _get_token();
  8748. }
  8749. if (tk.type == TK_OP_ASSIGN) {
  8750. if (!is_constant) {
  8751. _set_error(vformat(RTR("Global non-constant variables are not supported. Expected '%s' keyword before constant definition."), "const"));
  8752. return ERR_PARSE_ERROR;
  8753. }
  8754. if (constant.array_size > 0 || unknown_size) {
  8755. bool full_def = false;
  8756. VariableDeclarationNode::Declaration decl;
  8757. decl.name = name;
  8758. decl.size = constant.array_size;
  8759. tk = _get_token();
  8760. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  8761. if (unknown_size) {
  8762. _set_expected_error("{");
  8763. return ERR_PARSE_ERROR;
  8764. }
  8765. full_def = true;
  8766. DataPrecision precision2 = PRECISION_DEFAULT;
  8767. if (is_token_precision(tk.type)) {
  8768. precision2 = get_token_precision(tk.type);
  8769. tk = _get_token();
  8770. if (!is_token_nonvoid_datatype(tk.type)) {
  8771. _set_error(RTR("Expected data type after precision modifier."));
  8772. return ERR_PARSE_ERROR;
  8773. }
  8774. }
  8775. StringName struct_name2;
  8776. DataType type2;
  8777. if (shader->structs.has(tk.text)) {
  8778. type2 = TYPE_STRUCT;
  8779. struct_name2 = tk.text;
  8780. } else {
  8781. if (!is_token_variable_datatype(tk.type)) {
  8782. _set_error(RTR("Invalid data type for the array."));
  8783. return ERR_PARSE_ERROR;
  8784. }
  8785. type2 = get_token_datatype(tk.type);
  8786. }
  8787. int array_size2 = 0;
  8788. tk = _get_token();
  8789. if (tk.type == TK_BRACKET_OPEN) {
  8790. bool is_unknown_size = false;
  8791. Error error = _parse_array_size(nullptr, constants, false, nullptr, &array_size2, &is_unknown_size);
  8792. if (error != OK) {
  8793. return error;
  8794. }
  8795. if (is_unknown_size) {
  8796. array_size2 = constant.array_size;
  8797. }
  8798. tk = _get_token();
  8799. } else {
  8800. _set_expected_error("[");
  8801. return ERR_PARSE_ERROR;
  8802. }
  8803. if (constant.precision != precision2 || constant.type != type2 || struct_name != struct_name2 || constant.array_size != array_size2) {
  8804. String from;
  8805. if (type2 == TYPE_STRUCT) {
  8806. from += struct_name2;
  8807. } else {
  8808. if (precision2 != PRECISION_DEFAULT) {
  8809. from += get_precision_name(precision2);
  8810. from += " ";
  8811. }
  8812. from += get_datatype_name(type2);
  8813. }
  8814. from += "[";
  8815. from += itos(array_size2);
  8816. from += "]'";
  8817. String to;
  8818. if (type == TYPE_STRUCT) {
  8819. to += struct_name;
  8820. } else {
  8821. if (precision != PRECISION_DEFAULT) {
  8822. to += get_precision_name(precision);
  8823. to += " ";
  8824. }
  8825. to += get_datatype_name(type);
  8826. }
  8827. to += "[";
  8828. to += itos(constant.array_size);
  8829. to += "]'";
  8830. _set_error(vformat(RTR("Cannot convert from '%s' to '%s'."), from, to));
  8831. return ERR_PARSE_ERROR;
  8832. }
  8833. }
  8834. bool curly = tk.type == TK_CURLY_BRACKET_OPEN;
  8835. if (unknown_size) {
  8836. if (!curly) {
  8837. _set_expected_error("{");
  8838. return ERR_PARSE_ERROR;
  8839. }
  8840. } else {
  8841. if (full_def) {
  8842. if (curly) {
  8843. _set_expected_error("(");
  8844. return ERR_PARSE_ERROR;
  8845. }
  8846. }
  8847. }
  8848. if (tk.type == TK_PARENTHESIS_OPEN || curly) { // initialization
  8849. while (true) {
  8850. Node *n = _parse_and_reduce_expression(nullptr, constants);
  8851. if (!n) {
  8852. return ERR_PARSE_ERROR;
  8853. }
  8854. if (n->type == Node::NODE_TYPE_OPERATOR && static_cast<OperatorNode *>(n)->op == OP_CALL) {
  8855. _set_error(RTR("Expected constant expression."));
  8856. return ERR_PARSE_ERROR;
  8857. }
  8858. if (!_compare_datatypes(constant.type, struct_name, 0, n->get_datatype(), n->get_datatype_name(), 0)) {
  8859. return ERR_PARSE_ERROR;
  8860. }
  8861. tk = _get_token();
  8862. if (tk.type == TK_COMMA) {
  8863. decl.initializer.push_back(n);
  8864. continue;
  8865. } else if (!curly && tk.type == TK_PARENTHESIS_CLOSE) {
  8866. decl.initializer.push_back(n);
  8867. break;
  8868. } else if (curly && tk.type == TK_CURLY_BRACKET_CLOSE) {
  8869. decl.initializer.push_back(n);
  8870. break;
  8871. } else {
  8872. if (curly) {
  8873. _set_expected_error("}", ",");
  8874. } else {
  8875. _set_expected_error(")", ",");
  8876. }
  8877. return ERR_PARSE_ERROR;
  8878. }
  8879. }
  8880. if (unknown_size) {
  8881. decl.size = decl.initializer.size();
  8882. constant.array_size = decl.initializer.size();
  8883. } else if (decl.initializer.size() != constant.array_size) {
  8884. _set_error(vformat(RTR("Array size mismatch. Expected %d elements (found %d)."), constant.array_size, decl.initializer.size()));
  8885. return ERR_PARSE_ERROR;
  8886. }
  8887. } else {
  8888. _set_expected_error("(");
  8889. return ERR_PARSE_ERROR;
  8890. }
  8891. array_size = constant.array_size;
  8892. ConstantNode *expr = memnew(ConstantNode);
  8893. expr->datatype = constant.type;
  8894. expr->struct_name = constant.struct_name;
  8895. expr->array_size = constant.array_size;
  8896. expr->array_declarations.push_back(decl);
  8897. constant.initializer = static_cast<ConstantNode *>(expr);
  8898. } else {
  8899. #ifdef DEBUG_ENABLED
  8900. if (constant.type == DataType::TYPE_BOOL) {
  8901. keyword_completion_context = CF_BOOLEAN;
  8902. }
  8903. #endif // DEBUG_ENABLED
  8904. //variable created with assignment! must parse an expression
  8905. Node *expr = _parse_and_reduce_expression(nullptr, constants);
  8906. if (!expr) {
  8907. return ERR_PARSE_ERROR;
  8908. }
  8909. #ifdef DEBUG_ENABLED
  8910. if (constant.type == DataType::TYPE_BOOL) {
  8911. keyword_completion_context = CF_GLOBAL_SPACE;
  8912. }
  8913. #endif // DEBUG_ENABLED
  8914. if (expr->type == Node::NODE_TYPE_OPERATOR && static_cast<OperatorNode *>(expr)->op == OP_CALL) {
  8915. OperatorNode *op = static_cast<OperatorNode *>(expr);
  8916. for (int i = 1; i < op->arguments.size(); i++) {
  8917. if (!_check_node_constness(op->arguments[i])) {
  8918. _set_error(vformat(RTR("Expected constant expression for argument %d of function call after '='."), i - 1));
  8919. return ERR_PARSE_ERROR;
  8920. }
  8921. }
  8922. }
  8923. constant.initializer = expr;
  8924. if (!_compare_datatypes(type, struct_name, 0, expr->get_datatype(), expr->get_datatype_name(), expr->get_array_size())) {
  8925. return ERR_PARSE_ERROR;
  8926. }
  8927. }
  8928. tk = _get_token();
  8929. } else {
  8930. if (constant.array_size > 0 || unknown_size) {
  8931. _set_error(RTR("Expected array initialization."));
  8932. return ERR_PARSE_ERROR;
  8933. } else {
  8934. _set_error(RTR("Expected initialization of constant."));
  8935. return ERR_PARSE_ERROR;
  8936. }
  8937. }
  8938. shader->constants[name] = constant;
  8939. shader->vconstants.push_back(constant);
  8940. #ifdef DEBUG_ENABLED
  8941. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_CONSTANT_FLAG)) {
  8942. used_constants.insert(name, Usage(tk_line));
  8943. }
  8944. #endif // DEBUG_ENABLED
  8945. if (tk.type == TK_COMMA) {
  8946. tk = _get_token();
  8947. if (tk.type != TK_IDENTIFIER) {
  8948. _set_error(RTR("Expected an identifier after type."));
  8949. return ERR_PARSE_ERROR;
  8950. }
  8951. name = tk.text;
  8952. if (_find_identifier(nullptr, false, constants, name)) {
  8953. _set_redefinition_error(String(name));
  8954. return ERR_PARSE_ERROR;
  8955. }
  8956. if (has_builtin(p_functions, name)) {
  8957. _set_redefinition_error(String(name));
  8958. return ERR_PARSE_ERROR;
  8959. }
  8960. tk = _get_token();
  8961. if (!fixed_array_size) {
  8962. array_size = 0;
  8963. }
  8964. unknown_size = false;
  8965. } else if (tk.type == TK_SEMICOLON) {
  8966. is_const_decl = false;
  8967. break;
  8968. } else {
  8969. _set_expected_error(",", ";");
  8970. return ERR_PARSE_ERROR;
  8971. }
  8972. }
  8973. break;
  8974. }
  8975. if (is_constant) {
  8976. _set_error(vformat(RTR("'%s' qualifier cannot be used with a function return type."), "const"));
  8977. return ERR_PARSE_ERROR;
  8978. }
  8979. FunctionInfo builtins;
  8980. if (p_functions.has(name)) {
  8981. builtins = p_functions[name];
  8982. }
  8983. if (p_functions.has("global")) { // Adds global variables: 'TIME'
  8984. for (const KeyValue<StringName, BuiltInInfo> &E : p_functions["global"].built_ins) {
  8985. builtins.built_ins.insert(E.key, E.value);
  8986. }
  8987. }
  8988. if (p_functions.has("constants")) { // Adds global constants: 'PI', 'TAU', 'E'
  8989. for (const KeyValue<StringName, BuiltInInfo> &E : p_functions["constants"].built_ins) {
  8990. builtins.built_ins.insert(E.key, E.value);
  8991. }
  8992. }
  8993. for (int i = 0; i < shader->vfunctions.size(); i++) {
  8994. if (!shader->vfunctions[i].callable && shader->vfunctions[i].name == name) {
  8995. _set_redefinition_error(String(name));
  8996. return ERR_PARSE_ERROR;
  8997. }
  8998. }
  8999. ShaderNode::Function function;
  9000. function.callable = !p_functions.has(name);
  9001. function.name = name;
  9002. FunctionNode *func_node = alloc_node<FunctionNode>();
  9003. function.function = func_node;
  9004. shader->functions.insert(name, function);
  9005. shader->vfunctions.push_back(function);
  9006. CallInfo call_info;
  9007. call_info.name = name;
  9008. calls_info.insert(name, call_info);
  9009. func_node->name = name;
  9010. func_node->return_type = type;
  9011. func_node->return_struct_name = struct_name;
  9012. func_node->return_precision = precision;
  9013. func_node->return_array_size = array_size;
  9014. if (p_functions.has(name)) {
  9015. func_node->can_discard = p_functions[name].can_discard;
  9016. } else {
  9017. #ifdef DEBUG_ENABLED
  9018. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_FUNCTION_FLAG)) {
  9019. used_functions.insert(name, Usage(tk_line));
  9020. }
  9021. #endif // DEBUG_ENABLED
  9022. }
  9023. func_node->body = alloc_node<BlockNode>();
  9024. func_node->body->parent_function = func_node;
  9025. tk = _get_token();
  9026. while (true) {
  9027. if (tk.type == TK_PARENTHESIS_CLOSE) {
  9028. break;
  9029. }
  9030. #ifdef DEBUG_ENABLED
  9031. keyword_completion_context = CF_CONST_KEYWORD | CF_FUNC_DECL_PARAM_SPEC | CF_PRECISION_MODIFIER | CF_FUNC_DECL_PARAM_TYPE; // eg. const in mediump float
  9032. if (_lookup_next(next)) {
  9033. if (next.type == TK_CONST) {
  9034. keyword_completion_context = CF_UNSPECIFIED;
  9035. } else if (is_token_arg_qual(next.type)) {
  9036. keyword_completion_context = CF_CONST_KEYWORD;
  9037. } else if (is_token_precision(next.type)) {
  9038. keyword_completion_context = (CF_CONST_KEYWORD | CF_FUNC_DECL_PARAM_SPEC | CF_FUNC_DECL_PARAM_TYPE);
  9039. } else if (is_token_datatype(next.type)) {
  9040. keyword_completion_context = (CF_CONST_KEYWORD | CF_FUNC_DECL_PARAM_SPEC | CF_PRECISION_MODIFIER);
  9041. }
  9042. }
  9043. #endif // DEBUG_ENABLED
  9044. bool param_is_const = false;
  9045. if (tk.type == TK_CONST) {
  9046. param_is_const = true;
  9047. tk = _get_token();
  9048. #ifdef DEBUG_ENABLED
  9049. if (keyword_completion_context & CF_CONST_KEYWORD) {
  9050. keyword_completion_context ^= CF_CONST_KEYWORD;
  9051. }
  9052. if (_lookup_next(next)) {
  9053. if (is_token_arg_qual(next.type)) {
  9054. keyword_completion_context = CF_UNSPECIFIED;
  9055. } else if (is_token_precision(next.type)) {
  9056. keyword_completion_context = (CF_FUNC_DECL_PARAM_SPEC | CF_FUNC_DECL_PARAM_TYPE);
  9057. } else if (is_token_datatype(next.type)) {
  9058. keyword_completion_context = (CF_FUNC_DECL_PARAM_SPEC | CF_PRECISION_MODIFIER);
  9059. }
  9060. }
  9061. #endif // DEBUG_ENABLED
  9062. }
  9063. ArgumentQualifier param_qualifier = ARGUMENT_QUALIFIER_IN;
  9064. if (is_token_arg_qual(tk.type)) {
  9065. bool error = false;
  9066. switch (tk.type) {
  9067. case TK_ARG_IN: {
  9068. param_qualifier = ARGUMENT_QUALIFIER_IN;
  9069. } break;
  9070. case TK_ARG_OUT: {
  9071. if (param_is_const) {
  9072. _set_error(vformat(RTR("The '%s' qualifier cannot be used within a function parameter declared with '%s'."), "out", "const"));
  9073. error = true;
  9074. }
  9075. param_qualifier = ARGUMENT_QUALIFIER_OUT;
  9076. } break;
  9077. case TK_ARG_INOUT: {
  9078. if (param_is_const) {
  9079. _set_error(vformat(RTR("The '%s' qualifier cannot be used within a function parameter declared with '%s'."), "inout", "const"));
  9080. error = true;
  9081. }
  9082. param_qualifier = ARGUMENT_QUALIFIER_INOUT;
  9083. } break;
  9084. default:
  9085. error = true;
  9086. break;
  9087. }
  9088. tk = _get_token();
  9089. #ifdef DEBUG_ENABLED
  9090. if (keyword_completion_context & CF_CONST_KEYWORD) {
  9091. keyword_completion_context ^= CF_CONST_KEYWORD;
  9092. }
  9093. if (keyword_completion_context & CF_FUNC_DECL_PARAM_SPEC) {
  9094. keyword_completion_context ^= CF_FUNC_DECL_PARAM_SPEC;
  9095. }
  9096. if (_lookup_next(next)) {
  9097. if (is_token_precision(next.type)) {
  9098. keyword_completion_context = CF_FUNC_DECL_PARAM_TYPE;
  9099. } else if (is_token_datatype(next.type)) {
  9100. keyword_completion_context = CF_PRECISION_MODIFIER;
  9101. }
  9102. }
  9103. #endif // DEBUG_ENABLED
  9104. if (error) {
  9105. return ERR_PARSE_ERROR;
  9106. }
  9107. }
  9108. DataType param_type;
  9109. StringName param_name;
  9110. StringName param_struct_name;
  9111. DataPrecision param_precision = PRECISION_DEFAULT;
  9112. int arg_array_size = 0;
  9113. if (is_token_precision(tk.type)) {
  9114. param_precision = get_token_precision(tk.type);
  9115. tk = _get_token();
  9116. #ifdef DEBUG_ENABLED
  9117. if (keyword_completion_context & CF_CONST_KEYWORD) {
  9118. keyword_completion_context ^= CF_CONST_KEYWORD;
  9119. }
  9120. if (keyword_completion_context & CF_FUNC_DECL_PARAM_SPEC) {
  9121. keyword_completion_context ^= CF_FUNC_DECL_PARAM_SPEC;
  9122. }
  9123. if (keyword_completion_context & CF_PRECISION_MODIFIER) {
  9124. keyword_completion_context ^= CF_PRECISION_MODIFIER;
  9125. }
  9126. if (_lookup_next(next)) {
  9127. if (is_token_datatype(next.type)) {
  9128. keyword_completion_context = CF_UNSPECIFIED;
  9129. }
  9130. }
  9131. #endif // DEBUG_ENABLED
  9132. }
  9133. is_struct = false;
  9134. if (shader->structs.has(tk.text)) {
  9135. is_struct = true;
  9136. param_struct_name = tk.text;
  9137. #ifdef DEBUG_ENABLED
  9138. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_STRUCT_FLAG) && used_structs.has(param_struct_name)) {
  9139. used_structs[param_struct_name].used = true;
  9140. }
  9141. #endif // DEBUG_ENABLED
  9142. }
  9143. if (!is_struct && !is_token_datatype(tk.type)) {
  9144. _set_error(RTR("Expected a valid data type for argument."));
  9145. return ERR_PARSE_ERROR;
  9146. }
  9147. if (param_qualifier == ARGUMENT_QUALIFIER_OUT || param_qualifier == ARGUMENT_QUALIFIER_INOUT) {
  9148. if (is_sampler_type(get_token_datatype(tk.type))) {
  9149. _set_error(RTR("Opaque types cannot be output parameters."));
  9150. return ERR_PARSE_ERROR;
  9151. }
  9152. }
  9153. if (is_struct) {
  9154. param_type = TYPE_STRUCT;
  9155. } else {
  9156. param_type = get_token_datatype(tk.type);
  9157. if (param_type == TYPE_VOID) {
  9158. _set_error(RTR("Void type not allowed as argument."));
  9159. return ERR_PARSE_ERROR;
  9160. }
  9161. }
  9162. if (param_precision != PRECISION_DEFAULT && _validate_precision(param_type, param_precision) != OK) {
  9163. return ERR_PARSE_ERROR;
  9164. }
  9165. #ifdef DEBUG_ENABLED
  9166. keyword_completion_context = CF_UNSPECIFIED;
  9167. #endif // DEBUG_ENABLED
  9168. tk = _get_token();
  9169. if (tk.type == TK_BRACKET_OPEN) {
  9170. Error error = _parse_array_size(nullptr, constants, true, nullptr, &arg_array_size, nullptr);
  9171. if (error != OK) {
  9172. return error;
  9173. }
  9174. tk = _get_token();
  9175. }
  9176. if (tk.type != TK_IDENTIFIER) {
  9177. _set_error(RTR("Expected an identifier for argument name."));
  9178. return ERR_PARSE_ERROR;
  9179. }
  9180. param_name = tk.text;
  9181. ShaderLanguage::IdentifierType itype;
  9182. if (_find_identifier(func_node->body, false, builtins, param_name, (ShaderLanguage::DataType *)nullptr, &itype)) {
  9183. if (itype != IDENTIFIER_FUNCTION) {
  9184. _set_redefinition_error(String(param_name));
  9185. return ERR_PARSE_ERROR;
  9186. }
  9187. }
  9188. if (has_builtin(p_functions, param_name)) {
  9189. _set_redefinition_error(String(param_name));
  9190. return ERR_PARSE_ERROR;
  9191. }
  9192. FunctionNode::Argument arg;
  9193. arg.type = param_type;
  9194. arg.name = param_name;
  9195. arg.struct_name = param_struct_name;
  9196. arg.precision = param_precision;
  9197. arg.qualifier = param_qualifier;
  9198. arg.tex_argument_check = false;
  9199. arg.tex_builtin_check = false;
  9200. arg.tex_argument_filter = FILTER_DEFAULT;
  9201. arg.tex_argument_repeat = REPEAT_DEFAULT;
  9202. arg.is_const = param_is_const;
  9203. tk = _get_token();
  9204. if (tk.type == TK_BRACKET_OPEN) {
  9205. Error error = _parse_array_size(nullptr, constants, true, nullptr, &arg_array_size, nullptr);
  9206. if (error != OK) {
  9207. return error;
  9208. }
  9209. tk = _get_token();
  9210. }
  9211. arg.array_size = arg_array_size;
  9212. func_node->arguments.push_back(arg);
  9213. if (tk.type == TK_COMMA) {
  9214. tk = _get_token();
  9215. //do none and go on
  9216. } else if (tk.type != TK_PARENTHESIS_CLOSE) {
  9217. _set_expected_error(",", ")");
  9218. return ERR_PARSE_ERROR;
  9219. }
  9220. }
  9221. if (p_functions.has(name)) {
  9222. //if one of the core functions, make sure they are of the correct form
  9223. if (func_node->arguments.size() > 0) {
  9224. _set_error(vformat(RTR("Function '%s' expects no arguments."), String(name)));
  9225. return ERR_PARSE_ERROR;
  9226. }
  9227. if (func_node->return_type != TYPE_VOID) {
  9228. _set_error(vformat(RTR("Function '%s' must be of '%s' return type."), String(name), "void"));
  9229. return ERR_PARSE_ERROR;
  9230. }
  9231. }
  9232. //all good let's parse inside the function!
  9233. tk = _get_token();
  9234. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  9235. _set_error(RTR("Expected a '{' to begin function."));
  9236. return ERR_PARSE_ERROR;
  9237. }
  9238. current_function = name;
  9239. #ifdef DEBUG_ENABLED
  9240. keyword_completion_context = CF_BLOCK;
  9241. #endif // DEBUG_ENABLED
  9242. Error err = _parse_block(func_node->body, builtins);
  9243. if (err) {
  9244. return err;
  9245. }
  9246. #ifdef DEBUG_ENABLED
  9247. keyword_completion_context = CF_GLOBAL_SPACE;
  9248. #endif // DEBUG_ENABLED
  9249. if (func_node->return_type != DataType::TYPE_VOID) {
  9250. BlockNode *block = func_node->body;
  9251. if (_find_last_flow_op_in_block(block, FlowOperation::FLOW_OP_RETURN) != OK) {
  9252. _set_error(vformat(RTR("Expected at least one '%s' statement in a non-void function."), "return"));
  9253. return ERR_PARSE_ERROR;
  9254. }
  9255. }
  9256. current_function = StringName();
  9257. }
  9258. }
  9259. tk = _get_token();
  9260. }
  9261. #ifdef DEBUG_ENABLED
  9262. if (check_device_limit_warnings && uniform_buffer_exceeded_line != -1) {
  9263. _add_warning(ShaderWarning::DEVICE_LIMIT_EXCEEDED, uniform_buffer_exceeded_line, RTR("uniform buffer"), { uniform_buffer_size, max_uniform_buffer_size });
  9264. }
  9265. #endif // DEBUG_ENABLED
  9266. return OK;
  9267. }
  9268. bool ShaderLanguage::has_builtin(const HashMap<StringName, ShaderLanguage::FunctionInfo> &p_functions, const StringName &p_name, bool p_check_global_funcs) {
  9269. if (p_check_global_funcs && global_func_set.has(p_name)) {
  9270. return true;
  9271. }
  9272. for (const KeyValue<StringName, ShaderLanguage::FunctionInfo> &E : p_functions) {
  9273. if (E.value.built_ins.has(p_name)) {
  9274. return true;
  9275. }
  9276. }
  9277. return false;
  9278. }
  9279. Error ShaderLanguage::_find_last_flow_op_in_op(ControlFlowNode *p_flow, FlowOperation p_op) {
  9280. bool found = false;
  9281. for (int i = p_flow->blocks.size() - 1; i >= 0; i--) {
  9282. if (p_flow->blocks[i]->type == Node::NODE_TYPE_BLOCK) {
  9283. BlockNode *last_block = static_cast<BlockNode *>(p_flow->blocks[i]);
  9284. if (_find_last_flow_op_in_block(last_block, p_op) == OK) {
  9285. found = true;
  9286. break;
  9287. }
  9288. }
  9289. }
  9290. if (found) {
  9291. return OK;
  9292. }
  9293. return FAILED;
  9294. }
  9295. Error ShaderLanguage::_find_last_flow_op_in_block(BlockNode *p_block, FlowOperation p_op) {
  9296. bool found = false;
  9297. for (List<ShaderLanguage::Node *>::Element *E = p_block->statements.back(); E; E = E->prev()) {
  9298. if (E->get()->type == Node::NODE_TYPE_CONTROL_FLOW) {
  9299. ControlFlowNode *flow = static_cast<ControlFlowNode *>(E->get());
  9300. if (flow->flow_op == p_op) {
  9301. found = true;
  9302. break;
  9303. } else {
  9304. if (_find_last_flow_op_in_op(flow, p_op) == OK) {
  9305. found = true;
  9306. break;
  9307. }
  9308. }
  9309. } else if (E->get()->type == Node::NODE_TYPE_BLOCK) {
  9310. BlockNode *block = static_cast<BlockNode *>(E->get());
  9311. if (_find_last_flow_op_in_block(block, p_op) == OK) {
  9312. found = true;
  9313. break;
  9314. }
  9315. }
  9316. }
  9317. if (found) {
  9318. return OK;
  9319. }
  9320. return FAILED;
  9321. }
  9322. // skips over whitespace and /* */ and // comments
  9323. static int _get_first_ident_pos(const String &p_code) {
  9324. int idx = 0;
  9325. #define GETCHAR(m_idx) (((idx + m_idx) < p_code.length()) ? p_code[idx + m_idx] : char32_t(0))
  9326. while (true) {
  9327. if (GETCHAR(0) == '/' && GETCHAR(1) == '/') {
  9328. idx += 2;
  9329. while (true) {
  9330. if (GETCHAR(0) == 0) {
  9331. return 0;
  9332. }
  9333. if (GETCHAR(0) == '\n') {
  9334. idx++;
  9335. break; // loop
  9336. }
  9337. idx++;
  9338. }
  9339. } else if (GETCHAR(0) == '/' && GETCHAR(1) == '*') {
  9340. idx += 2;
  9341. while (true) {
  9342. if (GETCHAR(0) == 0) {
  9343. return 0;
  9344. }
  9345. if (GETCHAR(0) == '*' && GETCHAR(1) == '/') {
  9346. idx += 2;
  9347. break; // loop
  9348. }
  9349. idx++;
  9350. }
  9351. } else {
  9352. switch (GETCHAR(0)) {
  9353. case ' ':
  9354. case '\t':
  9355. case '\r':
  9356. case '\n': {
  9357. idx++;
  9358. } break; // switch
  9359. default:
  9360. return idx;
  9361. }
  9362. }
  9363. }
  9364. #undef GETCHAR
  9365. }
  9366. String ShaderLanguage::get_shader_type(const String &p_code) {
  9367. bool reading_type = false;
  9368. String cur_identifier;
  9369. for (int i = _get_first_ident_pos(p_code); i < p_code.length(); i++) {
  9370. if (p_code[i] == ';') {
  9371. break;
  9372. } else if (p_code[i] <= 32) {
  9373. if (!cur_identifier.is_empty()) {
  9374. if (!reading_type) {
  9375. if (cur_identifier != "shader_type") {
  9376. return String();
  9377. }
  9378. reading_type = true;
  9379. cur_identifier = String();
  9380. } else {
  9381. return cur_identifier;
  9382. }
  9383. }
  9384. } else {
  9385. cur_identifier += String::chr(p_code[i]);
  9386. }
  9387. }
  9388. if (reading_type) {
  9389. return cur_identifier;
  9390. }
  9391. return String();
  9392. }
  9393. bool ShaderLanguage::is_builtin_func_out_parameter(const String &p_name, int p_param) {
  9394. int i = 0;
  9395. while (builtin_func_out_args[i].name) {
  9396. if (p_name == builtin_func_out_args[i].name) {
  9397. for (int j = 0; j < BuiltinFuncOutArgs::MAX_ARGS; j++) {
  9398. int arg = builtin_func_out_args[i].arguments[j];
  9399. if (arg == p_param) {
  9400. return true;
  9401. }
  9402. if (arg < 0) {
  9403. return false;
  9404. }
  9405. }
  9406. }
  9407. i++;
  9408. }
  9409. return false;
  9410. }
  9411. #ifdef DEBUG_ENABLED
  9412. void ShaderLanguage::_check_warning_accums() {
  9413. for (const KeyValue<ShaderWarning::Code, HashMap<StringName, HashMap<StringName, Usage>> *> &E : warnings_check_map2) {
  9414. for (const KeyValue<StringName, HashMap<StringName, Usage>> &T : *E.value) {
  9415. for (const KeyValue<StringName, Usage> &U : T.value) {
  9416. if (!U.value.used) {
  9417. _add_warning(E.key, U.value.decl_line, U.key);
  9418. }
  9419. }
  9420. }
  9421. }
  9422. for (const KeyValue<ShaderWarning::Code, HashMap<StringName, Usage> *> &E : warnings_check_map) {
  9423. for (const KeyValue<StringName, Usage> &U : (*E.value)) {
  9424. if (!U.value.used) {
  9425. _add_warning(E.key, U.value.decl_line, U.key);
  9426. }
  9427. }
  9428. }
  9429. }
  9430. List<ShaderWarning>::Element *ShaderLanguage::get_warnings_ptr() {
  9431. return warnings.front();
  9432. }
  9433. void ShaderLanguage::enable_warning_checking(bool p_enabled) {
  9434. check_warnings = p_enabled;
  9435. }
  9436. bool ShaderLanguage::is_warning_checking_enabled() const {
  9437. return check_warnings;
  9438. }
  9439. void ShaderLanguage::set_warning_flags(uint32_t p_flags) {
  9440. warning_flags = p_flags;
  9441. }
  9442. uint32_t ShaderLanguage::get_warning_flags() const {
  9443. return warning_flags;
  9444. }
  9445. #endif // DEBUG_ENABLED
  9446. Error ShaderLanguage::compile(const String &p_code, const ShaderCompileInfo &p_info) {
  9447. clear();
  9448. is_shader_inc = p_info.is_include;
  9449. code = p_code;
  9450. global_shader_uniform_get_type_func = p_info.global_shader_uniform_type_func;
  9451. varying_function_names = p_info.varying_function_names;
  9452. nodes = nullptr;
  9453. shader = alloc_node<ShaderNode>();
  9454. Error err = _parse_shader(p_info.functions, p_info.render_modes, p_info.shader_types);
  9455. #ifdef DEBUG_ENABLED
  9456. if (check_warnings) {
  9457. _check_warning_accums();
  9458. }
  9459. #endif // DEBUG_ENABLED
  9460. if (err != OK) {
  9461. return err;
  9462. }
  9463. return OK;
  9464. }
  9465. Error ShaderLanguage::complete(const String &p_code, const ShaderCompileInfo &p_info, List<ScriptLanguage::CodeCompletionOption> *r_options, String &r_call_hint) {
  9466. clear();
  9467. is_shader_inc = p_info.is_include;
  9468. code = p_code;
  9469. varying_function_names = p_info.varying_function_names;
  9470. nodes = nullptr;
  9471. global_shader_uniform_get_type_func = p_info.global_shader_uniform_type_func;
  9472. shader = alloc_node<ShaderNode>();
  9473. _parse_shader(p_info.functions, p_info.render_modes, p_info.shader_types);
  9474. #ifdef DEBUG_ENABLED
  9475. // Adds context keywords.
  9476. if (keyword_completion_context != CF_UNSPECIFIED) {
  9477. int sz = sizeof(keyword_list) / sizeof(KeyWord);
  9478. for (int i = 0; i < sz; i++) {
  9479. if (keyword_list[i].flags == CF_UNSPECIFIED) {
  9480. break; // Ignore hint keywords (parsed below).
  9481. }
  9482. if (keyword_list[i].flags & keyword_completion_context) {
  9483. if (keyword_list[i].excluded_shader_types.has(shader_type_identifier)) {
  9484. continue;
  9485. }
  9486. if (!keyword_list[i].functions.is_empty() && !keyword_list[i].functions.has(current_function)) {
  9487. continue;
  9488. }
  9489. ScriptLanguage::CodeCompletionOption option(keyword_list[i].text, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9490. r_options->push_back(option);
  9491. }
  9492. }
  9493. }
  9494. #endif // DEBUG_ENABLED
  9495. switch (completion_type) {
  9496. case COMPLETION_NONE: {
  9497. //do nothing
  9498. return OK;
  9499. } break;
  9500. case COMPLETION_SHADER_TYPE: {
  9501. for (const String &shader_type : p_info.shader_types) {
  9502. ScriptLanguage::CodeCompletionOption option(shader_type, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9503. r_options->push_back(option);
  9504. }
  9505. return OK;
  9506. } break;
  9507. case COMPLETION_RENDER_MODE: {
  9508. if (is_shader_inc) {
  9509. for (int i = 0; i < RenderingServer::SHADER_MAX; i++) {
  9510. const Vector<ModeInfo> modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(i));
  9511. for (int j = 0; j < modes.size(); j++) {
  9512. const ModeInfo &info = modes[j];
  9513. if (!info.options.is_empty()) {
  9514. bool found = false;
  9515. for (int k = 0; k < info.options.size(); k++) {
  9516. if (shader->render_modes.has(String(info.name) + "_" + String(info.options[k]))) {
  9517. found = true;
  9518. }
  9519. }
  9520. if (!found) {
  9521. for (int k = 0; k < info.options.size(); k++) {
  9522. ScriptLanguage::CodeCompletionOption option(String(info.name) + "_" + String(info.options[k]), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9523. r_options->push_back(option);
  9524. }
  9525. }
  9526. } else {
  9527. const String name = String(info.name);
  9528. if (!shader->render_modes.has(name)) {
  9529. ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9530. r_options->push_back(option);
  9531. }
  9532. }
  9533. }
  9534. }
  9535. } else {
  9536. for (int i = 0; i < p_info.render_modes.size(); i++) {
  9537. const ModeInfo &info = p_info.render_modes[i];
  9538. if (!info.options.is_empty()) {
  9539. bool found = false;
  9540. for (int j = 0; j < info.options.size(); j++) {
  9541. if (shader->render_modes.has(String(info.name) + "_" + String(info.options[j]))) {
  9542. found = true;
  9543. }
  9544. }
  9545. if (!found) {
  9546. for (int j = 0; j < info.options.size(); j++) {
  9547. ScriptLanguage::CodeCompletionOption option(String(info.name) + "_" + String(info.options[j]), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9548. r_options->push_back(option);
  9549. }
  9550. }
  9551. } else {
  9552. const String name = String(info.name);
  9553. if (!shader->render_modes.has(name)) {
  9554. ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9555. r_options->push_back(option);
  9556. }
  9557. }
  9558. }
  9559. }
  9560. return OK;
  9561. } break;
  9562. case COMPLETION_STRUCT: {
  9563. if (shader->structs.has(completion_struct)) {
  9564. StructNode *node = shader->structs[completion_struct].shader_struct;
  9565. for (ShaderLanguage::MemberNode *member : node->members) {
  9566. ScriptLanguage::CodeCompletionOption option(member->name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER);
  9567. r_options->push_back(option);
  9568. }
  9569. }
  9570. return OK;
  9571. } break;
  9572. case COMPLETION_MAIN_FUNCTION: {
  9573. for (const KeyValue<StringName, FunctionInfo> &E : p_info.functions) {
  9574. if (!E.value.main_function) {
  9575. continue;
  9576. }
  9577. bool found = false;
  9578. for (int i = 0; i < shader->vfunctions.size(); i++) {
  9579. if (shader->vfunctions[i].name == E.key) {
  9580. found = true;
  9581. break;
  9582. }
  9583. }
  9584. if (found) {
  9585. continue;
  9586. }
  9587. ScriptLanguage::CodeCompletionOption option(E.key, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  9588. r_options->push_back(option);
  9589. }
  9590. return OK;
  9591. } break;
  9592. case COMPLETION_IDENTIFIER:
  9593. case COMPLETION_FUNCTION_CALL: {
  9594. bool comp_ident = completion_type == COMPLETION_IDENTIFIER;
  9595. HashMap<String, ScriptLanguage::CodeCompletionKind> matches;
  9596. StringName skip_function;
  9597. BlockNode *block = completion_block;
  9598. if (completion_class == TAG_GLOBAL) {
  9599. while (block) {
  9600. if (comp_ident) {
  9601. for (const KeyValue<StringName, BlockNode::Variable> &E : block->variables) {
  9602. if (E.value.line < completion_line) {
  9603. matches.insert(E.key, ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE);
  9604. }
  9605. }
  9606. }
  9607. if (block->parent_function) {
  9608. if (comp_ident) {
  9609. for (int i = 0; i < block->parent_function->arguments.size(); i++) {
  9610. matches.insert(block->parent_function->arguments[i].name, ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE);
  9611. }
  9612. }
  9613. skip_function = block->parent_function->name;
  9614. }
  9615. block = block->parent_block;
  9616. }
  9617. if (comp_ident) {
  9618. if (is_shader_inc) {
  9619. for (int i = 0; i < RenderingServer::SHADER_MAX; i++) {
  9620. const HashMap<StringName, ShaderLanguage::FunctionInfo> info = ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(i));
  9621. if (info.has("global")) {
  9622. for (const KeyValue<StringName, BuiltInInfo> &E : info["global"].built_ins) {
  9623. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  9624. if (E.value.constant) {
  9625. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  9626. }
  9627. matches.insert(E.key, kind);
  9628. }
  9629. }
  9630. if (info.has("constants")) {
  9631. for (const KeyValue<StringName, BuiltInInfo> &E : info["constants"].built_ins) {
  9632. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  9633. if (E.value.constant) {
  9634. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  9635. }
  9636. matches.insert(E.key, kind);
  9637. }
  9638. }
  9639. if (skip_function != StringName() && info.has(skip_function)) {
  9640. for (const KeyValue<StringName, BuiltInInfo> &E : info[skip_function].built_ins) {
  9641. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  9642. if (E.value.constant) {
  9643. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  9644. }
  9645. matches.insert(E.key, kind);
  9646. }
  9647. }
  9648. }
  9649. } else {
  9650. if (p_info.functions.has("global")) {
  9651. for (const KeyValue<StringName, BuiltInInfo> &E : p_info.functions["global"].built_ins) {
  9652. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  9653. if (E.value.constant) {
  9654. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  9655. }
  9656. matches.insert(E.key, kind);
  9657. }
  9658. }
  9659. if (p_info.functions.has("constants")) {
  9660. for (const KeyValue<StringName, BuiltInInfo> &E : p_info.functions["constants"].built_ins) {
  9661. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  9662. if (E.value.constant) {
  9663. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  9664. }
  9665. matches.insert(E.key, kind);
  9666. }
  9667. }
  9668. if (skip_function != StringName() && p_info.functions.has(skip_function)) {
  9669. for (const KeyValue<StringName, BuiltInInfo> &E : p_info.functions[skip_function].built_ins) {
  9670. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  9671. if (E.value.constant) {
  9672. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  9673. }
  9674. matches.insert(E.key, kind);
  9675. }
  9676. }
  9677. }
  9678. for (const KeyValue<StringName, ShaderNode::Constant> &E : shader->constants) {
  9679. matches.insert(E.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  9680. }
  9681. for (const KeyValue<StringName, ShaderNode::Varying> &E : shader->varyings) {
  9682. matches.insert(E.key, ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE);
  9683. }
  9684. for (const KeyValue<StringName, ShaderNode::Uniform> &E : shader->uniforms) {
  9685. matches.insert(E.key, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER);
  9686. }
  9687. }
  9688. for (int i = 0; i < shader->vfunctions.size(); i++) {
  9689. if (!shader->vfunctions[i].callable || shader->vfunctions[i].name == skip_function) {
  9690. continue;
  9691. }
  9692. matches.insert(String(shader->vfunctions[i].name), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  9693. }
  9694. int idx = 0;
  9695. bool low_end = RenderingServer::get_singleton()->is_low_end();
  9696. if (stages && stages->has(skip_function)) {
  9697. for (const KeyValue<StringName, StageFunctionInfo> &E : (*stages)[skip_function].stage_functions) {
  9698. matches.insert(String(E.key), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  9699. }
  9700. }
  9701. while (builtin_func_defs[idx].name) {
  9702. if ((low_end && builtin_func_defs[idx].high_end) || _check_restricted_func(builtin_func_defs[idx].name, skip_function)) {
  9703. idx++;
  9704. continue;
  9705. }
  9706. matches.insert(String(builtin_func_defs[idx].name), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  9707. idx++;
  9708. }
  9709. } else { // sub-class
  9710. int idx = 0;
  9711. bool low_end = RenderingServer::get_singleton()->is_low_end();
  9712. while (builtin_func_defs[idx].name) {
  9713. if (low_end && builtin_func_defs[idx].high_end) {
  9714. idx++;
  9715. continue;
  9716. }
  9717. if (builtin_func_defs[idx].tag == completion_class) {
  9718. matches.insert(String(builtin_func_defs[idx].name), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  9719. }
  9720. idx++;
  9721. }
  9722. }
  9723. for (const KeyValue<String, ScriptLanguage::CodeCompletionKind> &E : matches) {
  9724. ScriptLanguage::CodeCompletionOption option(E.key, E.value);
  9725. if (E.value == ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION) {
  9726. option.insert_text += "(";
  9727. }
  9728. r_options->push_back(option);
  9729. }
  9730. return OK;
  9731. } break;
  9732. case COMPLETION_CALL_ARGUMENTS: {
  9733. StringName block_function;
  9734. BlockNode *block = completion_block;
  9735. while (block) {
  9736. if (block->parent_function) {
  9737. block_function = block->parent_function->name;
  9738. }
  9739. block = block->parent_block;
  9740. }
  9741. for (int i = 0; i < shader->vfunctions.size(); i++) {
  9742. if (!shader->vfunctions[i].callable) {
  9743. continue;
  9744. }
  9745. if (shader->vfunctions[i].name == completion_function) {
  9746. String calltip;
  9747. if (shader->vfunctions[i].function->return_type == TYPE_STRUCT) {
  9748. calltip += String(shader->vfunctions[i].function->return_struct_name);
  9749. } else {
  9750. calltip += get_datatype_name(shader->vfunctions[i].function->return_type);
  9751. }
  9752. if (shader->vfunctions[i].function->return_array_size > 0) {
  9753. calltip += "[";
  9754. calltip += itos(shader->vfunctions[i].function->return_array_size);
  9755. calltip += "]";
  9756. }
  9757. calltip += " ";
  9758. calltip += shader->vfunctions[i].name;
  9759. calltip += "(";
  9760. for (int j = 0; j < shader->vfunctions[i].function->arguments.size(); j++) {
  9761. if (j > 0) {
  9762. calltip += ", ";
  9763. } else {
  9764. calltip += " ";
  9765. }
  9766. if (j == completion_argument) {
  9767. calltip += char32_t(0xFFFF);
  9768. }
  9769. if (shader->vfunctions[i].function->arguments[j].is_const) {
  9770. calltip += "const ";
  9771. }
  9772. if (shader->vfunctions[i].function->arguments[j].qualifier != ArgumentQualifier::ARGUMENT_QUALIFIER_IN) {
  9773. if (shader->vfunctions[i].function->arguments[j].qualifier == ArgumentQualifier::ARGUMENT_QUALIFIER_OUT) {
  9774. calltip += "out ";
  9775. } else { // ArgumentQualifier::ARGUMENT_QUALIFIER_INOUT
  9776. calltip += "inout ";
  9777. }
  9778. }
  9779. if (shader->vfunctions[i].function->arguments[j].type == TYPE_STRUCT) {
  9780. calltip += String(shader->vfunctions[i].function->arguments[j].struct_name);
  9781. } else {
  9782. calltip += get_datatype_name(shader->vfunctions[i].function->arguments[j].type);
  9783. }
  9784. calltip += " ";
  9785. calltip += shader->vfunctions[i].function->arguments[j].name;
  9786. if (shader->vfunctions[i].function->arguments[j].array_size > 0) {
  9787. calltip += "[";
  9788. calltip += itos(shader->vfunctions[i].function->arguments[j].array_size);
  9789. calltip += "]";
  9790. }
  9791. if (j == completion_argument) {
  9792. calltip += char32_t(0xFFFF);
  9793. }
  9794. }
  9795. if (shader->vfunctions[i].function->arguments.size()) {
  9796. calltip += " ";
  9797. }
  9798. calltip += ")";
  9799. r_call_hint = calltip;
  9800. return OK;
  9801. }
  9802. }
  9803. int idx = 0;
  9804. String calltip;
  9805. bool low_end = RenderingServer::get_singleton()->is_low_end();
  9806. if (stages && stages->has(block_function)) {
  9807. for (const KeyValue<StringName, StageFunctionInfo> &E : (*stages)[block_function].stage_functions) {
  9808. if (completion_function == E.key) {
  9809. calltip += get_datatype_name(E.value.return_type);
  9810. calltip += " ";
  9811. calltip += E.key;
  9812. calltip += "(";
  9813. for (int i = 0; i < E.value.arguments.size(); i++) {
  9814. if (i > 0) {
  9815. calltip += ", ";
  9816. } else {
  9817. calltip += " ";
  9818. }
  9819. if (i == completion_argument) {
  9820. calltip += char32_t(0xFFFF);
  9821. }
  9822. calltip += get_datatype_name(E.value.arguments[i].type);
  9823. calltip += " ";
  9824. calltip += E.value.arguments[i].name;
  9825. if (i == completion_argument) {
  9826. calltip += char32_t(0xFFFF);
  9827. }
  9828. }
  9829. if (E.value.arguments.size()) {
  9830. calltip += " ";
  9831. }
  9832. calltip += ")";
  9833. r_call_hint = calltip;
  9834. return OK;
  9835. }
  9836. }
  9837. }
  9838. while (builtin_func_defs[idx].name) {
  9839. if ((low_end && builtin_func_defs[idx].high_end) || _check_restricted_func(builtin_func_defs[idx].name, block_function)) {
  9840. idx++;
  9841. continue;
  9842. }
  9843. int idx2 = 0;
  9844. HashSet<int> out_args;
  9845. while (builtin_func_out_args[idx2].name != nullptr) {
  9846. if (builtin_func_out_args[idx2].name == builtin_func_defs[idx].name) {
  9847. for (int i = 0; i < BuiltinFuncOutArgs::MAX_ARGS; i++) {
  9848. int arg = builtin_func_out_args[idx2].arguments[i];
  9849. if (arg == -1) {
  9850. break;
  9851. }
  9852. out_args.insert(arg);
  9853. }
  9854. break;
  9855. }
  9856. idx2++;
  9857. }
  9858. if (completion_function == builtin_func_defs[idx].name) {
  9859. if (builtin_func_defs[idx].tag != completion_class) {
  9860. idx++;
  9861. continue;
  9862. }
  9863. if (calltip.length()) {
  9864. calltip += "\n";
  9865. }
  9866. calltip += get_datatype_name(builtin_func_defs[idx].rettype);
  9867. calltip += " ";
  9868. calltip += builtin_func_defs[idx].name;
  9869. calltip += "(";
  9870. bool found_arg = false;
  9871. for (int i = 0; i < BuiltinFuncDef::MAX_ARGS - 1; i++) {
  9872. if (builtin_func_defs[idx].args[i] == TYPE_VOID) {
  9873. break;
  9874. }
  9875. if (i > 0) {
  9876. calltip += ", ";
  9877. } else {
  9878. calltip += " ";
  9879. }
  9880. if (i == completion_argument) {
  9881. calltip += char32_t(0xFFFF);
  9882. }
  9883. if (out_args.has(i)) {
  9884. calltip += "out ";
  9885. }
  9886. calltip += get_datatype_name(builtin_func_defs[idx].args[i]);
  9887. String arg_name = (String)builtin_func_defs[idx].args_names[i];
  9888. if (!arg_name.is_empty()) {
  9889. calltip += " ";
  9890. calltip += arg_name;
  9891. }
  9892. if (i == completion_argument) {
  9893. calltip += char32_t(0xFFFF);
  9894. }
  9895. found_arg = true;
  9896. }
  9897. if (found_arg) {
  9898. calltip += " ";
  9899. }
  9900. calltip += ")";
  9901. }
  9902. idx++;
  9903. }
  9904. r_call_hint = calltip;
  9905. return OK;
  9906. } break;
  9907. case COMPLETION_INDEX: {
  9908. const char colv[4] = { 'r', 'g', 'b', 'a' };
  9909. const char coordv[4] = { 'x', 'y', 'z', 'w' };
  9910. const char coordt[4] = { 's', 't', 'p', 'q' };
  9911. const String theme_color_names[4] = { "axis_x_color", "axis_y_color", "axis_z_color", "axis_w_color" };
  9912. int limit = 0;
  9913. switch (completion_base) {
  9914. case TYPE_BVEC2:
  9915. case TYPE_IVEC2:
  9916. case TYPE_UVEC2:
  9917. case TYPE_VEC2: {
  9918. limit = 2;
  9919. } break;
  9920. case TYPE_BVEC3:
  9921. case TYPE_IVEC3:
  9922. case TYPE_UVEC3:
  9923. case TYPE_VEC3: {
  9924. limit = 3;
  9925. } break;
  9926. case TYPE_BVEC4:
  9927. case TYPE_IVEC4:
  9928. case TYPE_UVEC4:
  9929. case TYPE_VEC4: {
  9930. limit = 4;
  9931. } break;
  9932. default: {
  9933. }
  9934. }
  9935. for (int i = 0; i < limit; i++) {
  9936. r_options->push_back(ScriptLanguage::CodeCompletionOption(String::chr(colv[i]), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT, ScriptLanguage::LOCATION_OTHER, theme_color_names[i]));
  9937. r_options->push_back(ScriptLanguage::CodeCompletionOption(String::chr(coordv[i]), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT, ScriptLanguage::LOCATION_OTHER, theme_color_names[i]));
  9938. r_options->push_back(ScriptLanguage::CodeCompletionOption(String::chr(coordt[i]), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT, ScriptLanguage::LOCATION_OTHER, theme_color_names[i]));
  9939. }
  9940. } break;
  9941. case COMPLETION_HINT: {
  9942. if (completion_base == DataType::TYPE_VEC3 || completion_base == DataType::TYPE_VEC4) {
  9943. if (current_uniform_hint == ShaderNode::Uniform::HINT_NONE) {
  9944. ScriptLanguage::CodeCompletionOption option("source_color", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9945. r_options->push_back(option);
  9946. }
  9947. } else if ((completion_base == DataType::TYPE_INT || completion_base == DataType::TYPE_FLOAT) && !completion_base_array) {
  9948. if (current_uniform_hint == ShaderNode::Uniform::HINT_NONE) {
  9949. Vector<String> options;
  9950. if (completion_base == DataType::TYPE_INT) {
  9951. options.push_back("hint_range(0, 100, 1)");
  9952. options.push_back("hint_enum(\"Zero\", \"One\", \"Two\")");
  9953. } else {
  9954. options.push_back("hint_range(0.0, 1.0, 0.1)");
  9955. }
  9956. for (const String &option_text : options) {
  9957. String hint_name = option_text.substr(0, option_text.find_char(char32_t('(')));
  9958. ScriptLanguage::CodeCompletionOption option(hint_name, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9959. option.insert_text = option_text;
  9960. r_options->push_back(option);
  9961. }
  9962. }
  9963. } else if ((int(completion_base) > int(TYPE_MAT4) && int(completion_base) < int(TYPE_STRUCT))) {
  9964. Vector<String> options;
  9965. if (current_uniform_filter == FILTER_DEFAULT) {
  9966. options.push_back("filter_linear");
  9967. options.push_back("filter_linear_mipmap");
  9968. options.push_back("filter_linear_mipmap_anisotropic");
  9969. options.push_back("filter_nearest");
  9970. options.push_back("filter_nearest_mipmap");
  9971. options.push_back("filter_nearest_mipmap_anisotropic");
  9972. }
  9973. if (current_uniform_repeat == REPEAT_DEFAULT) {
  9974. options.push_back("repeat_enable");
  9975. options.push_back("repeat_disable");
  9976. }
  9977. if (completion_base_array) {
  9978. if (current_uniform_hint == ShaderNode::Uniform::HINT_NONE) {
  9979. options.push_back("source_color");
  9980. }
  9981. } else {
  9982. if (current_uniform_hint == ShaderNode::Uniform::HINT_NONE) {
  9983. options.push_back("hint_anisotropy");
  9984. options.push_back("hint_default_black");
  9985. options.push_back("hint_default_white");
  9986. options.push_back("hint_default_transparent");
  9987. options.push_back("hint_normal");
  9988. options.push_back("hint_roughness_a");
  9989. options.push_back("hint_roughness_b");
  9990. options.push_back("hint_roughness_g");
  9991. options.push_back("hint_roughness_gray");
  9992. options.push_back("hint_roughness_normal");
  9993. options.push_back("hint_roughness_r");
  9994. options.push_back("hint_screen_texture");
  9995. options.push_back("hint_normal_roughness_texture");
  9996. options.push_back("hint_depth_texture");
  9997. options.push_back("source_color");
  9998. }
  9999. }
  10000. for (int i = 0; i < options.size(); i++) {
  10001. ScriptLanguage::CodeCompletionOption option(options[i], ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  10002. r_options->push_back(option);
  10003. }
  10004. }
  10005. if (!completion_base_array && !current_uniform_instance_index_defined) {
  10006. ScriptLanguage::CodeCompletionOption option("instance_index", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  10007. option.insert_text = "instance_index(0)";
  10008. r_options->push_back(option);
  10009. }
  10010. } break;
  10011. }
  10012. return ERR_PARSE_ERROR;
  10013. }
  10014. String ShaderLanguage::get_error_text() {
  10015. return error_str;
  10016. }
  10017. Vector<ShaderLanguage::FilePosition> ShaderLanguage::get_include_positions() {
  10018. return include_positions;
  10019. }
  10020. int ShaderLanguage::get_error_line() {
  10021. return error_line;
  10022. }
  10023. ShaderLanguage::ShaderNode *ShaderLanguage::get_shader() {
  10024. return shader;
  10025. }
  10026. ShaderLanguage::ShaderLanguage() {
  10027. nodes = nullptr;
  10028. completion_class = TAG_GLOBAL;
  10029. if (instance_counter == 0) {
  10030. int idx = 0;
  10031. while (builtin_func_defs[idx].name) {
  10032. if (builtin_func_defs[idx].tag == SubClassTag::TAG_GLOBAL) {
  10033. global_func_set.insert(builtin_func_defs[idx].name);
  10034. }
  10035. idx++;
  10036. }
  10037. }
  10038. instance_counter++;
  10039. #ifdef DEBUG_ENABLED
  10040. warnings_check_map.insert(ShaderWarning::UNUSED_CONSTANT, &used_constants);
  10041. warnings_check_map.insert(ShaderWarning::UNUSED_FUNCTION, &used_functions);
  10042. warnings_check_map.insert(ShaderWarning::UNUSED_STRUCT, &used_structs);
  10043. warnings_check_map.insert(ShaderWarning::UNUSED_UNIFORM, &used_uniforms);
  10044. warnings_check_map.insert(ShaderWarning::UNUSED_VARYING, &used_varyings);
  10045. warnings_check_map2.insert(ShaderWarning::UNUSED_LOCAL_VARIABLE, &used_local_vars);
  10046. #endif // DEBUG_ENABLED
  10047. }
  10048. ShaderLanguage::~ShaderLanguage() {
  10049. clear();
  10050. instance_counter--;
  10051. if (instance_counter == 0) {
  10052. global_func_set.clear();
  10053. }
  10054. }