shader_language.cpp 348 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625
  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. String ShaderLanguage::get_operator_text(Operator p_op) {
  39. static const char *op_names[OP_MAX] = { "==",
  40. "!=",
  41. "<",
  42. "<=",
  43. ">",
  44. ">=",
  45. "&&",
  46. "||",
  47. "!",
  48. "-",
  49. "+",
  50. "-",
  51. "*",
  52. "/",
  53. "%",
  54. "<<",
  55. ">>",
  56. "=",
  57. "+=",
  58. "-=",
  59. "*=",
  60. "/=",
  61. "%=",
  62. "<<=",
  63. ">>=",
  64. "&=",
  65. "|=",
  66. "^=",
  67. "&",
  68. "|",
  69. "^",
  70. "~",
  71. "++",
  72. "--",
  73. "?",
  74. ":",
  75. "++",
  76. "--",
  77. "()",
  78. "construct",
  79. "index",
  80. "empty" };
  81. return op_names[p_op];
  82. }
  83. const char *ShaderLanguage::token_names[TK_MAX] = {
  84. "EMPTY",
  85. "IDENTIFIER",
  86. "TRUE",
  87. "FALSE",
  88. "FLOAT_CONSTANT",
  89. "INT_CONSTANT",
  90. "UINT_CONSTANT",
  91. "TYPE_VOID",
  92. "TYPE_BOOL",
  93. "TYPE_BVEC2",
  94. "TYPE_BVEC3",
  95. "TYPE_BVEC4",
  96. "TYPE_INT",
  97. "TYPE_IVEC2",
  98. "TYPE_IVEC3",
  99. "TYPE_IVEC4",
  100. "TYPE_UINT",
  101. "TYPE_UVEC2",
  102. "TYPE_UVEC3",
  103. "TYPE_UVEC4",
  104. "TYPE_FLOAT",
  105. "TYPE_VEC2",
  106. "TYPE_VEC3",
  107. "TYPE_VEC4",
  108. "TYPE_MAT2",
  109. "TYPE_MAT3",
  110. "TYPE_MAT4",
  111. "TYPE_SAMPLER2D",
  112. "TYPE_ISAMPLER2D",
  113. "TYPE_USAMPLER2D",
  114. "TYPE_SAMPLER2DARRAY",
  115. "TYPE_ISAMPLER2DARRAY",
  116. "TYPE_USAMPLER2DARRAY",
  117. "TYPE_SAMPLER3D",
  118. "TYPE_ISAMPLER3D",
  119. "TYPE_USAMPLER3D",
  120. "TYPE_SAMPLERCUBE",
  121. "TYPE_SAMPLERCUBEARRAY",
  122. "INTERPOLATION_FLAT",
  123. "INTERPOLATION_SMOOTH",
  124. "CONST",
  125. "STRUCT",
  126. "PRECISION_LOW",
  127. "PRECISION_MID",
  128. "PRECISION_HIGH",
  129. "OP_EQUAL",
  130. "OP_NOT_EQUAL",
  131. "OP_LESS",
  132. "OP_LESS_EQUAL",
  133. "OP_GREATER",
  134. "OP_GREATER_EQUAL",
  135. "OP_AND",
  136. "OP_OR",
  137. "OP_NOT",
  138. "OP_ADD",
  139. "OP_SUB",
  140. "OP_MUL",
  141. "OP_DIV",
  142. "OP_MOD",
  143. "OP_SHIFT_LEFT",
  144. "OP_SHIFT_RIGHT",
  145. "OP_ASSIGN",
  146. "OP_ASSIGN_ADD",
  147. "OP_ASSIGN_SUB",
  148. "OP_ASSIGN_MUL",
  149. "OP_ASSIGN_DIV",
  150. "OP_ASSIGN_MOD",
  151. "OP_ASSIGN_SHIFT_LEFT",
  152. "OP_ASSIGN_SHIFT_RIGHT",
  153. "OP_ASSIGN_BIT_AND",
  154. "OP_ASSIGN_BIT_OR",
  155. "OP_ASSIGN_BIT_XOR",
  156. "OP_BIT_AND",
  157. "OP_BIT_OR",
  158. "OP_BIT_XOR",
  159. "OP_BIT_INVERT",
  160. "OP_INCREMENT",
  161. "OP_DECREMENT",
  162. "CF_IF",
  163. "CF_ELSE",
  164. "CF_FOR",
  165. "CF_WHILE",
  166. "CF_DO",
  167. "CF_SWITCH",
  168. "CF_CASE",
  169. "CF_DEFAULT",
  170. "CF_BREAK",
  171. "CF_CONTINUE",
  172. "CF_RETURN",
  173. "CF_DISCARD",
  174. "BRACKET_OPEN",
  175. "BRACKET_CLOSE",
  176. "CURLY_BRACKET_OPEN",
  177. "CURLY_BRACKET_CLOSE",
  178. "PARENTHESIS_OPEN",
  179. "PARENTHESIS_CLOSE",
  180. "QUESTION",
  181. "COMMA",
  182. "COLON",
  183. "SEMICOLON",
  184. "PERIOD",
  185. "UNIFORM",
  186. "UNIFORM_GROUP",
  187. "INSTANCE",
  188. "GLOBAL",
  189. "VARYING",
  190. "ARG_IN",
  191. "ARG_OUT",
  192. "ARG_INOUT",
  193. "RENDER_MODE",
  194. "HINT_DEFAULT_WHITE_TEXTURE",
  195. "HINT_DEFAULT_BLACK_TEXTURE",
  196. "HINT_DEFAULT_TRANSPARENT_TEXTURE",
  197. "HINT_NORMAL_TEXTURE",
  198. "HINT_ROUGHNESS_NORMAL_TEXTURE",
  199. "HINT_ROUGHNESS_R",
  200. "HINT_ROUGHNESS_G",
  201. "HINT_ROUGHNESS_B",
  202. "HINT_ROUGHNESS_A",
  203. "HINT_ROUGHNESS_GRAY",
  204. "HINT_ANISOTROPY_TEXTURE",
  205. "HINT_SOURCE_COLOR",
  206. "HINT_RANGE",
  207. "HINT_INSTANCE_INDEX",
  208. "HINT_SCREEN_TEXTURE",
  209. "HINT_NORMAL_ROUGHNESS_TEXTURE",
  210. "HINT_DEPTH_TEXTURE",
  211. "FILTER_NEAREST",
  212. "FILTER_LINEAR",
  213. "FILTER_NEAREST_MIPMAP",
  214. "FILTER_LINEAR_MIPMAP",
  215. "FILTER_NEAREST_MIPMAP_ANISOTROPIC",
  216. "FILTER_LINEAR_MIPMAP_ANISOTROPIC",
  217. "REPEAT_ENABLE",
  218. "REPEAT_DISABLE",
  219. "SHADER_TYPE",
  220. "CURSOR",
  221. "ERROR",
  222. "EOF",
  223. };
  224. String ShaderLanguage::get_token_text(Token p_token) {
  225. String name = token_names[p_token.type];
  226. if (p_token.is_integer_constant() || p_token.type == TK_FLOAT_CONSTANT) {
  227. name += "(" + rtos(p_token.constant) + ")";
  228. } else if (p_token.type == TK_IDENTIFIER) {
  229. name += "(" + String(p_token.text) + ")";
  230. } else if (p_token.type == TK_ERROR) {
  231. name += "(" + String(p_token.text) + ")";
  232. }
  233. return name;
  234. }
  235. ShaderLanguage::Token ShaderLanguage::_make_token(TokenType p_type, const StringName &p_text) {
  236. Token tk;
  237. tk.type = p_type;
  238. tk.text = p_text;
  239. tk.line = tk_line;
  240. if (tk.type == TK_ERROR) {
  241. _set_error(p_text);
  242. }
  243. return tk;
  244. }
  245. enum ContextFlag : uint32_t {
  246. CF_UNSPECIFIED = 0U,
  247. CF_BLOCK = 1U, // "void test() { <x> }"
  248. CF_FUNC_DECL_PARAM_SPEC = 2U, // "void test(<x> int param) {}"
  249. CF_FUNC_DECL_PARAM_TYPE = 4U, // "void test(<x> param) {}"
  250. CF_IF_DECL = 8U, // "if(<x>) {}"
  251. CF_BOOLEAN = 16U, // "bool t = <x>;"
  252. CF_GLOBAL_SPACE = 32U, // "struct", "const", "void" etc.
  253. CF_DATATYPE = 64U, // "<x> value;"
  254. CF_UNIFORM_TYPE = 128U, // "uniform <x> myUniform;"
  255. CF_VARYING_TYPE = 256U, // "varying <x> myVarying;"
  256. CF_PRECISION_MODIFIER = 512U, // "<x> vec4 a = vec4(0.0, 1.0, 2.0, 3.0);"
  257. CF_INTERPOLATION_QUALIFIER = 1024U, // "varying <x> vec3 myColor;"
  258. CF_UNIFORM_KEYWORD = 2048U, // "uniform"
  259. CF_CONST_KEYWORD = 4096U, // "const"
  260. CF_UNIFORM_QUALIFIER = 8192U, // "<x> uniform float t;"
  261. CF_SHADER_TYPE = 16384U, // "shader_type"
  262. };
  263. const uint32_t KCF_DATATYPE = CF_BLOCK | CF_GLOBAL_SPACE | CF_DATATYPE | CF_FUNC_DECL_PARAM_TYPE | CF_UNIFORM_TYPE;
  264. const uint32_t KCF_SAMPLER_DATATYPE = CF_FUNC_DECL_PARAM_TYPE | CF_UNIFORM_TYPE;
  265. const ShaderLanguage::KeyWord ShaderLanguage::keyword_list[] = {
  266. { TK_TRUE, "true", CF_BLOCK | CF_IF_DECL | CF_BOOLEAN, {}, {} },
  267. { TK_FALSE, "false", CF_BLOCK | CF_IF_DECL | CF_BOOLEAN, {}, {} },
  268. // data types
  269. { TK_TYPE_VOID, "void", CF_GLOBAL_SPACE, {}, {} },
  270. { TK_TYPE_BOOL, "bool", KCF_DATATYPE, {}, {} },
  271. { TK_TYPE_BVEC2, "bvec2", KCF_DATATYPE, {}, {} },
  272. { TK_TYPE_BVEC3, "bvec3", KCF_DATATYPE, {}, {} },
  273. { TK_TYPE_BVEC4, "bvec4", KCF_DATATYPE, {}, {} },
  274. { TK_TYPE_INT, "int", KCF_DATATYPE, {}, {} },
  275. { TK_TYPE_IVEC2, "ivec2", KCF_DATATYPE, {}, {} },
  276. { TK_TYPE_IVEC3, "ivec3", KCF_DATATYPE, {}, {} },
  277. { TK_TYPE_IVEC4, "ivec4", KCF_DATATYPE, {}, {} },
  278. { TK_TYPE_UINT, "uint", KCF_DATATYPE, {}, {} },
  279. { TK_TYPE_UVEC2, "uvec2", KCF_DATATYPE, {}, {} },
  280. { TK_TYPE_UVEC3, "uvec3", KCF_DATATYPE, {}, {} },
  281. { TK_TYPE_UVEC4, "uvec4", KCF_DATATYPE, {}, {} },
  282. { TK_TYPE_FLOAT, "float", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  283. { TK_TYPE_VEC2, "vec2", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  284. { TK_TYPE_VEC3, "vec3", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  285. { TK_TYPE_VEC4, "vec4", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  286. { TK_TYPE_MAT2, "mat2", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  287. { TK_TYPE_MAT3, "mat3", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  288. { TK_TYPE_MAT4, "mat4", KCF_DATATYPE | CF_VARYING_TYPE, {}, {} },
  289. { TK_TYPE_SAMPLER2D, "sampler2D", KCF_SAMPLER_DATATYPE, {}, {} },
  290. { TK_TYPE_ISAMPLER2D, "isampler2D", KCF_SAMPLER_DATATYPE, {}, {} },
  291. { TK_TYPE_USAMPLER2D, "usampler2D", KCF_SAMPLER_DATATYPE, {}, {} },
  292. { TK_TYPE_SAMPLER2DARRAY, "sampler2DArray", KCF_SAMPLER_DATATYPE, {}, {} },
  293. { TK_TYPE_ISAMPLER2DARRAY, "isampler2DArray", KCF_SAMPLER_DATATYPE, {}, {} },
  294. { TK_TYPE_USAMPLER2DARRAY, "usampler2DArray", KCF_SAMPLER_DATATYPE, {}, {} },
  295. { TK_TYPE_SAMPLER3D, "sampler3D", KCF_SAMPLER_DATATYPE, {}, {} },
  296. { TK_TYPE_ISAMPLER3D, "isampler3D", KCF_SAMPLER_DATATYPE, {}, {} },
  297. { TK_TYPE_USAMPLER3D, "usampler3D", KCF_SAMPLER_DATATYPE, {}, {} },
  298. { TK_TYPE_SAMPLERCUBE, "samplerCube", KCF_SAMPLER_DATATYPE, {}, {} },
  299. { TK_TYPE_SAMPLERCUBEARRAY, "samplerCubeArray", KCF_SAMPLER_DATATYPE, {}, {} },
  300. // interpolation qualifiers
  301. { TK_INTERPOLATION_FLAT, "flat", CF_INTERPOLATION_QUALIFIER, {}, {} },
  302. { TK_INTERPOLATION_SMOOTH, "smooth", CF_INTERPOLATION_QUALIFIER, {}, {} },
  303. // precision modifiers
  304. { TK_PRECISION_LOW, "lowp", CF_BLOCK | CF_PRECISION_MODIFIER, {}, {} },
  305. { TK_PRECISION_MID, "mediump", CF_BLOCK | CF_PRECISION_MODIFIER, {}, {} },
  306. { TK_PRECISION_HIGH, "highp", CF_BLOCK | CF_PRECISION_MODIFIER, {}, {} },
  307. // global space keywords
  308. { TK_UNIFORM, "uniform", CF_GLOBAL_SPACE | CF_UNIFORM_KEYWORD, {}, {} },
  309. { TK_UNIFORM_GROUP, "group_uniforms", CF_GLOBAL_SPACE, {}, {} },
  310. { TK_VARYING, "varying", CF_GLOBAL_SPACE, { "particles", "sky", "fog" }, {} },
  311. { TK_CONST, "const", CF_BLOCK | CF_GLOBAL_SPACE | CF_CONST_KEYWORD, {}, {} },
  312. { TK_STRUCT, "struct", CF_GLOBAL_SPACE, {}, {} },
  313. { TK_SHADER_TYPE, "shader_type", CF_SHADER_TYPE, {}, {} },
  314. { TK_RENDER_MODE, "render_mode", CF_GLOBAL_SPACE, {}, {} },
  315. // uniform qualifiers
  316. { TK_INSTANCE, "instance", CF_GLOBAL_SPACE | CF_UNIFORM_QUALIFIER, {}, {} },
  317. { TK_GLOBAL, "global", CF_GLOBAL_SPACE | CF_UNIFORM_QUALIFIER, {}, {} },
  318. // block keywords
  319. { TK_CF_IF, "if", CF_BLOCK, {}, {} },
  320. { TK_CF_ELSE, "else", CF_BLOCK, {}, {} },
  321. { TK_CF_FOR, "for", CF_BLOCK, {}, {} },
  322. { TK_CF_WHILE, "while", CF_BLOCK, {}, {} },
  323. { TK_CF_DO, "do", CF_BLOCK, {}, {} },
  324. { TK_CF_SWITCH, "switch", CF_BLOCK, {}, {} },
  325. { TK_CF_CASE, "case", CF_BLOCK, {}, {} },
  326. { TK_CF_DEFAULT, "default", CF_BLOCK, {}, {} },
  327. { TK_CF_BREAK, "break", CF_BLOCK, {}, {} },
  328. { TK_CF_CONTINUE, "continue", CF_BLOCK, {}, {} },
  329. { TK_CF_RETURN, "return", CF_BLOCK, {}, {} },
  330. { TK_CF_DISCARD, "discard", CF_BLOCK, { "particles", "sky", "fog" }, { "fragment" } },
  331. // function specifier keywords
  332. { TK_ARG_IN, "in", CF_FUNC_DECL_PARAM_SPEC, {}, {} },
  333. { TK_ARG_OUT, "out", CF_FUNC_DECL_PARAM_SPEC, {}, {} },
  334. { TK_ARG_INOUT, "inout", CF_FUNC_DECL_PARAM_SPEC, {}, {} },
  335. // hints
  336. { TK_HINT_SOURCE_COLOR, "source_color", CF_UNSPECIFIED, {}, {} },
  337. { TK_HINT_RANGE, "hint_range", CF_UNSPECIFIED, {}, {} },
  338. { TK_HINT_INSTANCE_INDEX, "instance_index", CF_UNSPECIFIED, {}, {} },
  339. // sampler hints
  340. { TK_HINT_NORMAL_TEXTURE, "hint_normal", CF_UNSPECIFIED, {}, {} },
  341. { TK_HINT_DEFAULT_WHITE_TEXTURE, "hint_default_white", CF_UNSPECIFIED, {}, {} },
  342. { TK_HINT_DEFAULT_BLACK_TEXTURE, "hint_default_black", CF_UNSPECIFIED, {}, {} },
  343. { TK_HINT_DEFAULT_TRANSPARENT_TEXTURE, "hint_default_transparent", CF_UNSPECIFIED, {}, {} },
  344. { TK_HINT_ANISOTROPY_TEXTURE, "hint_anisotropy", CF_UNSPECIFIED, {}, {} },
  345. { TK_HINT_ROUGHNESS_R, "hint_roughness_r", CF_UNSPECIFIED, {}, {} },
  346. { TK_HINT_ROUGHNESS_G, "hint_roughness_g", CF_UNSPECIFIED, {}, {} },
  347. { TK_HINT_ROUGHNESS_B, "hint_roughness_b", CF_UNSPECIFIED, {}, {} },
  348. { TK_HINT_ROUGHNESS_A, "hint_roughness_a", CF_UNSPECIFIED, {}, {} },
  349. { TK_HINT_ROUGHNESS_NORMAL_TEXTURE, "hint_roughness_normal", CF_UNSPECIFIED, {}, {} },
  350. { TK_HINT_ROUGHNESS_GRAY, "hint_roughness_gray", CF_UNSPECIFIED, {}, {} },
  351. { TK_HINT_SCREEN_TEXTURE, "hint_screen_texture", CF_UNSPECIFIED, {}, {} },
  352. { TK_HINT_NORMAL_ROUGHNESS_TEXTURE, "hint_normal_roughness_texture", CF_UNSPECIFIED, {}, {} },
  353. { TK_HINT_DEPTH_TEXTURE, "hint_depth_texture", CF_UNSPECIFIED, {}, {} },
  354. { TK_FILTER_NEAREST, "filter_nearest", CF_UNSPECIFIED, {}, {} },
  355. { TK_FILTER_LINEAR, "filter_linear", CF_UNSPECIFIED, {}, {} },
  356. { TK_FILTER_NEAREST_MIPMAP, "filter_nearest_mipmap", CF_UNSPECIFIED, {}, {} },
  357. { TK_FILTER_LINEAR_MIPMAP, "filter_linear_mipmap", CF_UNSPECIFIED, {}, {} },
  358. { TK_FILTER_NEAREST_MIPMAP_ANISOTROPIC, "filter_nearest_mipmap_anisotropic", CF_UNSPECIFIED, {}, {} },
  359. { TK_FILTER_LINEAR_MIPMAP_ANISOTROPIC, "filter_linear_mipmap_anisotropic", CF_UNSPECIFIED, {}, {} },
  360. { TK_REPEAT_ENABLE, "repeat_enable", CF_UNSPECIFIED, {}, {} },
  361. { TK_REPEAT_DISABLE, "repeat_disable", CF_UNSPECIFIED, {}, {} },
  362. { TK_ERROR, nullptr, CF_UNSPECIFIED, {}, {} }
  363. };
  364. ShaderLanguage::Token ShaderLanguage::_get_token() {
  365. #define GETCHAR(m_idx) (((char_idx + m_idx) < code.length()) ? code[char_idx + m_idx] : char32_t(0))
  366. while (true) {
  367. char_idx++;
  368. switch (GETCHAR(-1)) {
  369. case 0:
  370. return _make_token(TK_EOF);
  371. case 0xFFFF:
  372. return _make_token(TK_CURSOR); //for completion
  373. case '\t':
  374. case '\r':
  375. case ' ':
  376. continue;
  377. case '\n':
  378. tk_line++;
  379. continue;
  380. case '/': {
  381. switch (GETCHAR(0)) {
  382. case '*': { // block comment
  383. char_idx++;
  384. while (true) {
  385. if (GETCHAR(0) == 0) {
  386. return _make_token(TK_EOF);
  387. }
  388. if (GETCHAR(0) == '*' && GETCHAR(1) == '/') {
  389. char_idx += 2;
  390. break;
  391. } else if (GETCHAR(0) == '\n') {
  392. tk_line++;
  393. }
  394. char_idx++;
  395. }
  396. } break;
  397. case '/': { // line comment skip
  398. while (true) {
  399. if (GETCHAR(0) == '\n') {
  400. tk_line++;
  401. char_idx++;
  402. break;
  403. }
  404. if (GETCHAR(0) == 0) {
  405. return _make_token(TK_EOF);
  406. }
  407. char_idx++;
  408. }
  409. } break;
  410. case '=': { // diveq
  411. char_idx++;
  412. return _make_token(TK_OP_ASSIGN_DIV);
  413. } break;
  414. default:
  415. return _make_token(TK_OP_DIV);
  416. }
  417. continue; //a comment, continue to next token
  418. } break;
  419. case '=': {
  420. if (GETCHAR(0) == '=') {
  421. char_idx++;
  422. return _make_token(TK_OP_EQUAL);
  423. }
  424. return _make_token(TK_OP_ASSIGN);
  425. } break;
  426. case '<': {
  427. if (GETCHAR(0) == '=') {
  428. char_idx++;
  429. return _make_token(TK_OP_LESS_EQUAL);
  430. } else if (GETCHAR(0) == '<') {
  431. char_idx++;
  432. if (GETCHAR(0) == '=') {
  433. char_idx++;
  434. return _make_token(TK_OP_ASSIGN_SHIFT_LEFT);
  435. }
  436. return _make_token(TK_OP_SHIFT_LEFT);
  437. }
  438. return _make_token(TK_OP_LESS);
  439. } break;
  440. case '>': {
  441. if (GETCHAR(0) == '=') {
  442. char_idx++;
  443. return _make_token(TK_OP_GREATER_EQUAL);
  444. } else if (GETCHAR(0) == '>') {
  445. char_idx++;
  446. if (GETCHAR(0) == '=') {
  447. char_idx++;
  448. return _make_token(TK_OP_ASSIGN_SHIFT_RIGHT);
  449. }
  450. return _make_token(TK_OP_SHIFT_RIGHT);
  451. }
  452. return _make_token(TK_OP_GREATER);
  453. } break;
  454. case '!': {
  455. if (GETCHAR(0) == '=') {
  456. char_idx++;
  457. return _make_token(TK_OP_NOT_EQUAL);
  458. }
  459. return _make_token(TK_OP_NOT);
  460. } break;
  461. //case '"' //string - no strings in shader
  462. //case '\'' //string - no strings in shader
  463. case '{':
  464. return _make_token(TK_CURLY_BRACKET_OPEN);
  465. case '}':
  466. return _make_token(TK_CURLY_BRACKET_CLOSE);
  467. case '[':
  468. return _make_token(TK_BRACKET_OPEN);
  469. case ']':
  470. return _make_token(TK_BRACKET_CLOSE);
  471. case '(':
  472. return _make_token(TK_PARENTHESIS_OPEN);
  473. case ')':
  474. return _make_token(TK_PARENTHESIS_CLOSE);
  475. case ',':
  476. return _make_token(TK_COMMA);
  477. case ';':
  478. return _make_token(TK_SEMICOLON);
  479. case '?':
  480. return _make_token(TK_QUESTION);
  481. case ':':
  482. return _make_token(TK_COLON);
  483. case '^':
  484. if (GETCHAR(0) == '=') {
  485. char_idx++;
  486. return _make_token(TK_OP_ASSIGN_BIT_XOR);
  487. }
  488. return _make_token(TK_OP_BIT_XOR);
  489. case '~':
  490. return _make_token(TK_OP_BIT_INVERT);
  491. case '&': {
  492. if (GETCHAR(0) == '=') {
  493. char_idx++;
  494. return _make_token(TK_OP_ASSIGN_BIT_AND);
  495. } else if (GETCHAR(0) == '&') {
  496. char_idx++;
  497. return _make_token(TK_OP_AND);
  498. }
  499. return _make_token(TK_OP_BIT_AND);
  500. } break;
  501. case '|': {
  502. if (GETCHAR(0) == '=') {
  503. char_idx++;
  504. return _make_token(TK_OP_ASSIGN_BIT_OR);
  505. } else if (GETCHAR(0) == '|') {
  506. char_idx++;
  507. return _make_token(TK_OP_OR);
  508. }
  509. return _make_token(TK_OP_BIT_OR);
  510. } break;
  511. case '*': {
  512. if (GETCHAR(0) == '=') {
  513. char_idx++;
  514. return _make_token(TK_OP_ASSIGN_MUL);
  515. }
  516. return _make_token(TK_OP_MUL);
  517. } break;
  518. case '+': {
  519. if (GETCHAR(0) == '=') {
  520. char_idx++;
  521. return _make_token(TK_OP_ASSIGN_ADD);
  522. } else if (GETCHAR(0) == '+') {
  523. char_idx++;
  524. return _make_token(TK_OP_INCREMENT);
  525. }
  526. return _make_token(TK_OP_ADD);
  527. } break;
  528. case '-': {
  529. if (GETCHAR(0) == '=') {
  530. char_idx++;
  531. return _make_token(TK_OP_ASSIGN_SUB);
  532. } else if (GETCHAR(0) == '-') {
  533. char_idx++;
  534. return _make_token(TK_OP_DECREMENT);
  535. }
  536. return _make_token(TK_OP_SUB);
  537. } break;
  538. case '%': {
  539. if (GETCHAR(0) == '=') {
  540. char_idx++;
  541. return _make_token(TK_OP_ASSIGN_MOD);
  542. }
  543. return _make_token(TK_OP_MOD);
  544. } break;
  545. case '@': {
  546. if (GETCHAR(0) == '@' && GETCHAR(1) == '>') {
  547. char_idx += 2;
  548. LocalVector<char32_t> incp;
  549. while (GETCHAR(0) != '\n') {
  550. incp.push_back(GETCHAR(0));
  551. char_idx++;
  552. }
  553. incp.push_back(0); // Zero end it.
  554. String include_path(incp.ptr());
  555. include_positions.write[include_positions.size() - 1].line = tk_line;
  556. FilePosition fp;
  557. fp.file = include_path;
  558. fp.line = 0;
  559. tk_line = 0;
  560. include_positions.push_back(fp);
  561. } else if (GETCHAR(0) == '@' && GETCHAR(1) == '<') {
  562. if (include_positions.size() == 1) {
  563. return _make_token(TK_ERROR, "Invalid include exit hint @@< without matching enter hint.");
  564. }
  565. char_idx += 2;
  566. include_positions.resize(include_positions.size() - 1); // Pop back.
  567. tk_line = include_positions[include_positions.size() - 1].line - 1; // Restore line.
  568. } else {
  569. return _make_token(TK_ERROR, "Invalid include enter/exit hint token (@@> and @@<)");
  570. }
  571. } break;
  572. default: {
  573. char_idx--; //go back one, since we have no idea what this is
  574. if (is_digit(GETCHAR(0)) || (GETCHAR(0) == '.' && is_digit(GETCHAR(1)))) {
  575. // parse number
  576. bool hexa_found = false;
  577. bool period_found = false;
  578. bool exponent_found = false;
  579. bool float_suffix_found = false;
  580. bool uint_suffix_found = false;
  581. bool end_suffix_found = false;
  582. enum {
  583. CASE_ALL,
  584. CASE_HEXA_PERIOD,
  585. CASE_EXPONENT,
  586. CASE_SIGN_AFTER_EXPONENT,
  587. CASE_NONE,
  588. CASE_MAX,
  589. } lut_case = CASE_ALL;
  590. static bool suffix_lut[CASE_MAX][127];
  591. if (!is_const_suffix_lut_initialized) {
  592. is_const_suffix_lut_initialized = true;
  593. for (int i = 0; i < 127; i++) {
  594. char t = char(i);
  595. suffix_lut[CASE_ALL][i] = t == '.' || t == 'x' || t == 'e' || t == 'f' || t == 'u' || t == '-' || t == '+';
  596. suffix_lut[CASE_HEXA_PERIOD][i] = t == 'e' || t == 'f';
  597. suffix_lut[CASE_EXPONENT][i] = t == 'f' || t == '-' || t == '+';
  598. suffix_lut[CASE_SIGN_AFTER_EXPONENT][i] = t == 'f';
  599. suffix_lut[CASE_NONE][i] = false;
  600. }
  601. }
  602. String str;
  603. int i = 0;
  604. bool digit_after_exp = false;
  605. while (true) {
  606. const char32_t symbol = String::char_lowercase(GETCHAR(i));
  607. bool error = false;
  608. if (is_digit(symbol)) {
  609. if (exponent_found) {
  610. digit_after_exp = true;
  611. }
  612. if (end_suffix_found) {
  613. error = true;
  614. }
  615. } else {
  616. if (symbol < 0x7F && suffix_lut[lut_case][symbol]) {
  617. if (symbol == 'x') {
  618. hexa_found = true;
  619. lut_case = CASE_HEXA_PERIOD;
  620. } else if (symbol == '.') {
  621. period_found = true;
  622. lut_case = CASE_HEXA_PERIOD;
  623. } else if (symbol == 'e' && !hexa_found) {
  624. exponent_found = true;
  625. lut_case = CASE_EXPONENT;
  626. } else if (symbol == 'f' && !hexa_found) {
  627. if (!period_found && !exponent_found) {
  628. error = true;
  629. }
  630. float_suffix_found = true;
  631. end_suffix_found = true;
  632. lut_case = CASE_NONE;
  633. } else if (symbol == 'u') {
  634. uint_suffix_found = true;
  635. end_suffix_found = true;
  636. lut_case = CASE_NONE;
  637. } else if (symbol == '-' || symbol == '+') {
  638. if (exponent_found) {
  639. lut_case = CASE_SIGN_AFTER_EXPONENT;
  640. } else {
  641. break;
  642. }
  643. }
  644. } else if (!hexa_found || !is_hex_digit(symbol)) {
  645. if (is_ascii_identifier_char(symbol)) {
  646. error = true;
  647. } else {
  648. break;
  649. }
  650. }
  651. }
  652. if (error) {
  653. if (hexa_found) {
  654. return _make_token(TK_ERROR, "Invalid (hexadecimal) numeric constant");
  655. }
  656. if (period_found || exponent_found || float_suffix_found) {
  657. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  658. }
  659. if (uint_suffix_found) {
  660. return _make_token(TK_ERROR, "Invalid (unsigned integer) numeric constant");
  661. }
  662. return _make_token(TK_ERROR, "Invalid (integer) numeric constant");
  663. }
  664. str += symbol;
  665. i++;
  666. }
  667. char32_t last_char = str[str.length() - 1];
  668. if (hexa_found) { // Integer (hex).
  669. if (str.size() > 11 || !str.is_valid_hex_number(true)) { // > 0xFFFFFFFF
  670. return _make_token(TK_ERROR, "Invalid (hexadecimal) numeric constant");
  671. }
  672. } else if (period_found || exponent_found || float_suffix_found) { // Float
  673. 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).
  674. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  675. }
  676. if (period_found) {
  677. if (float_suffix_found) {
  678. //checks for eg "1.f" or "1.99f" notations
  679. if (last_char != 'f') {
  680. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  681. }
  682. } else {
  683. //checks for eg. "1." or "1.99" notations
  684. if (last_char != '.' && !is_digit(last_char)) {
  685. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  686. }
  687. }
  688. } else if (float_suffix_found) {
  689. // if no period found the float suffix must be the last character, like in "2f" for "2.0"
  690. if (last_char != 'f') {
  691. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  692. }
  693. }
  694. if (float_suffix_found) {
  695. // Strip the suffix.
  696. str = str.left(str.length() - 1);
  697. // Compensate reading cursor position.
  698. char_idx += 1;
  699. }
  700. if (!str.is_valid_float()) {
  701. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  702. }
  703. } else { // Integer
  704. if (uint_suffix_found) {
  705. // Strip the suffix.
  706. str = str.left(str.length() - 1);
  707. // Compensate reading cursor position.
  708. char_idx += 1;
  709. }
  710. if (!str.is_valid_int()) {
  711. if (uint_suffix_found) {
  712. return _make_token(TK_ERROR, "Invalid (unsigned integer) numeric constant");
  713. } else {
  714. return _make_token(TK_ERROR, "Invalid (integer) numeric constant");
  715. }
  716. }
  717. }
  718. char_idx += str.length();
  719. Token tk;
  720. if (period_found || exponent_found || float_suffix_found) {
  721. tk.type = TK_FLOAT_CONSTANT;
  722. } else if (uint_suffix_found) {
  723. tk.type = TK_UINT_CONSTANT;
  724. } else {
  725. tk.type = TK_INT_CONSTANT;
  726. }
  727. if (hexa_found) {
  728. tk.constant = (double)str.hex_to_int();
  729. } else {
  730. tk.constant = str.to_float();
  731. }
  732. tk.line = tk_line;
  733. return tk;
  734. }
  735. if (GETCHAR(0) == '.') {
  736. //parse period
  737. char_idx++;
  738. return _make_token(TK_PERIOD);
  739. }
  740. if (is_ascii_identifier_char(GETCHAR(0))) {
  741. // parse identifier
  742. String str;
  743. while (is_ascii_identifier_char(GETCHAR(0))) {
  744. str += char32_t(GETCHAR(0));
  745. char_idx++;
  746. }
  747. //see if keyword
  748. //should be converted to a static map
  749. int idx = 0;
  750. while (keyword_list[idx].text) {
  751. if (str == keyword_list[idx].text) {
  752. return _make_token(keyword_list[idx].token);
  753. }
  754. idx++;
  755. }
  756. str = str.replace("dus_", "_");
  757. return _make_token(TK_IDENTIFIER, str);
  758. }
  759. if (GETCHAR(0) > 32) {
  760. return _make_token(TK_ERROR, "Tokenizer: Unknown character #" + itos(GETCHAR(0)) + ": '" + String::chr(GETCHAR(0)) + "'");
  761. } else {
  762. return _make_token(TK_ERROR, "Tokenizer: Unknown character #" + itos(GETCHAR(0)));
  763. }
  764. } break;
  765. }
  766. }
  767. ERR_PRINT("BUG");
  768. return Token();
  769. #undef GETCHAR
  770. }
  771. bool ShaderLanguage::_lookup_next(Token &r_tk) {
  772. TkPos pre_pos = _get_tkpos();
  773. int line = pre_pos.tk_line;
  774. _get_token();
  775. Token tk = _get_token();
  776. _set_tkpos(pre_pos);
  777. if (tk.line == line) {
  778. r_tk = tk;
  779. return true;
  780. }
  781. return false;
  782. }
  783. String ShaderLanguage::token_debug(const String &p_code) {
  784. clear();
  785. code = p_code;
  786. String output;
  787. Token tk = _get_token();
  788. while (tk.type != TK_EOF && tk.type != TK_ERROR) {
  789. output += itos(tk_line) + ": " + get_token_text(tk) + "\n";
  790. tk = _get_token();
  791. }
  792. return output;
  793. }
  794. bool ShaderLanguage::is_token_variable_datatype(TokenType p_type) {
  795. return (
  796. p_type == TK_TYPE_VOID ||
  797. p_type == TK_TYPE_BOOL ||
  798. p_type == TK_TYPE_BVEC2 ||
  799. p_type == TK_TYPE_BVEC3 ||
  800. p_type == TK_TYPE_BVEC4 ||
  801. p_type == TK_TYPE_INT ||
  802. p_type == TK_TYPE_IVEC2 ||
  803. p_type == TK_TYPE_IVEC3 ||
  804. p_type == TK_TYPE_IVEC4 ||
  805. p_type == TK_TYPE_UINT ||
  806. p_type == TK_TYPE_UVEC2 ||
  807. p_type == TK_TYPE_UVEC3 ||
  808. p_type == TK_TYPE_UVEC4 ||
  809. p_type == TK_TYPE_FLOAT ||
  810. p_type == TK_TYPE_VEC2 ||
  811. p_type == TK_TYPE_VEC3 ||
  812. p_type == TK_TYPE_VEC4 ||
  813. p_type == TK_TYPE_MAT2 ||
  814. p_type == TK_TYPE_MAT3 ||
  815. p_type == TK_TYPE_MAT4);
  816. }
  817. bool ShaderLanguage::is_token_datatype(TokenType p_type) {
  818. return (
  819. p_type == TK_TYPE_VOID ||
  820. p_type == TK_TYPE_BOOL ||
  821. p_type == TK_TYPE_BVEC2 ||
  822. p_type == TK_TYPE_BVEC3 ||
  823. p_type == TK_TYPE_BVEC4 ||
  824. p_type == TK_TYPE_INT ||
  825. p_type == TK_TYPE_IVEC2 ||
  826. p_type == TK_TYPE_IVEC3 ||
  827. p_type == TK_TYPE_IVEC4 ||
  828. p_type == TK_TYPE_UINT ||
  829. p_type == TK_TYPE_UVEC2 ||
  830. p_type == TK_TYPE_UVEC3 ||
  831. p_type == TK_TYPE_UVEC4 ||
  832. p_type == TK_TYPE_FLOAT ||
  833. p_type == TK_TYPE_VEC2 ||
  834. p_type == TK_TYPE_VEC3 ||
  835. p_type == TK_TYPE_VEC4 ||
  836. p_type == TK_TYPE_MAT2 ||
  837. p_type == TK_TYPE_MAT3 ||
  838. p_type == TK_TYPE_MAT4 ||
  839. p_type == TK_TYPE_SAMPLER2D ||
  840. p_type == TK_TYPE_ISAMPLER2D ||
  841. p_type == TK_TYPE_USAMPLER2D ||
  842. p_type == TK_TYPE_SAMPLER2DARRAY ||
  843. p_type == TK_TYPE_ISAMPLER2DARRAY ||
  844. p_type == TK_TYPE_USAMPLER2DARRAY ||
  845. p_type == TK_TYPE_SAMPLER3D ||
  846. p_type == TK_TYPE_ISAMPLER3D ||
  847. p_type == TK_TYPE_USAMPLER3D ||
  848. p_type == TK_TYPE_SAMPLERCUBE ||
  849. p_type == TK_TYPE_SAMPLERCUBEARRAY);
  850. }
  851. ShaderLanguage::DataType ShaderLanguage::get_token_datatype(TokenType p_type) {
  852. return DataType(p_type - TK_TYPE_VOID);
  853. }
  854. bool ShaderLanguage::is_token_interpolation(TokenType p_type) {
  855. return (
  856. p_type == TK_INTERPOLATION_FLAT ||
  857. p_type == TK_INTERPOLATION_SMOOTH);
  858. }
  859. ShaderLanguage::DataInterpolation ShaderLanguage::get_token_interpolation(TokenType p_type) {
  860. if (p_type == TK_INTERPOLATION_FLAT) {
  861. return INTERPOLATION_FLAT;
  862. } else {
  863. return INTERPOLATION_SMOOTH;
  864. }
  865. }
  866. bool ShaderLanguage::is_token_precision(TokenType p_type) {
  867. return (
  868. p_type == TK_PRECISION_LOW ||
  869. p_type == TK_PRECISION_MID ||
  870. p_type == TK_PRECISION_HIGH);
  871. }
  872. bool ShaderLanguage::is_token_arg_qual(TokenType p_type) {
  873. return (
  874. p_type == TK_ARG_IN ||
  875. p_type == TK_ARG_OUT ||
  876. p_type == TK_ARG_INOUT);
  877. }
  878. ShaderLanguage::DataPrecision ShaderLanguage::get_token_precision(TokenType p_type) {
  879. if (p_type == TK_PRECISION_LOW) {
  880. return PRECISION_LOWP;
  881. } else if (p_type == TK_PRECISION_HIGH) {
  882. return PRECISION_HIGHP;
  883. } else {
  884. return PRECISION_MEDIUMP;
  885. }
  886. }
  887. String ShaderLanguage::get_precision_name(DataPrecision p_type) {
  888. switch (p_type) {
  889. case PRECISION_LOWP:
  890. return "lowp";
  891. case PRECISION_MEDIUMP:
  892. return "mediump";
  893. case PRECISION_HIGHP:
  894. return "highp";
  895. default:
  896. break;
  897. }
  898. return "";
  899. }
  900. String ShaderLanguage::get_interpolation_name(DataInterpolation p_interpolation) {
  901. switch (p_interpolation) {
  902. case INTERPOLATION_FLAT:
  903. return "flat";
  904. case INTERPOLATION_SMOOTH:
  905. return "smooth";
  906. default:
  907. break;
  908. }
  909. return "";
  910. }
  911. String ShaderLanguage::get_datatype_name(DataType p_type) {
  912. switch (p_type) {
  913. case TYPE_VOID:
  914. return "void";
  915. case TYPE_BOOL:
  916. return "bool";
  917. case TYPE_BVEC2:
  918. return "bvec2";
  919. case TYPE_BVEC3:
  920. return "bvec3";
  921. case TYPE_BVEC4:
  922. return "bvec4";
  923. case TYPE_INT:
  924. return "int";
  925. case TYPE_IVEC2:
  926. return "ivec2";
  927. case TYPE_IVEC3:
  928. return "ivec3";
  929. case TYPE_IVEC4:
  930. return "ivec4";
  931. case TYPE_UINT:
  932. return "uint";
  933. case TYPE_UVEC2:
  934. return "uvec2";
  935. case TYPE_UVEC3:
  936. return "uvec3";
  937. case TYPE_UVEC4:
  938. return "uvec4";
  939. case TYPE_FLOAT:
  940. return "float";
  941. case TYPE_VEC2:
  942. return "vec2";
  943. case TYPE_VEC3:
  944. return "vec3";
  945. case TYPE_VEC4:
  946. return "vec4";
  947. case TYPE_MAT2:
  948. return "mat2";
  949. case TYPE_MAT3:
  950. return "mat3";
  951. case TYPE_MAT4:
  952. return "mat4";
  953. case TYPE_SAMPLER2D:
  954. return "sampler2D";
  955. case TYPE_ISAMPLER2D:
  956. return "isampler2D";
  957. case TYPE_USAMPLER2D:
  958. return "usampler2D";
  959. case TYPE_SAMPLER2DARRAY:
  960. return "sampler2DArray";
  961. case TYPE_ISAMPLER2DARRAY:
  962. return "isampler2DArray";
  963. case TYPE_USAMPLER2DARRAY:
  964. return "usampler2DArray";
  965. case TYPE_SAMPLER3D:
  966. return "sampler3D";
  967. case TYPE_ISAMPLER3D:
  968. return "isampler3D";
  969. case TYPE_USAMPLER3D:
  970. return "usampler3D";
  971. case TYPE_SAMPLERCUBE:
  972. return "samplerCube";
  973. case TYPE_SAMPLERCUBEARRAY:
  974. return "samplerCubeArray";
  975. case TYPE_STRUCT:
  976. return "struct";
  977. case TYPE_MAX:
  978. return "invalid";
  979. }
  980. return "";
  981. }
  982. String ShaderLanguage::get_uniform_hint_name(ShaderNode::Uniform::Hint p_hint) {
  983. String result;
  984. switch (p_hint) {
  985. case ShaderNode::Uniform::HINT_RANGE: {
  986. result = "hint_range";
  987. } break;
  988. case ShaderNode::Uniform::HINT_SOURCE_COLOR: {
  989. result = "source_color";
  990. } break;
  991. case ShaderNode::Uniform::HINT_NORMAL: {
  992. result = "hint_normal";
  993. } break;
  994. case ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL: {
  995. result = "hint_roughness_normal";
  996. } break;
  997. case ShaderNode::Uniform::HINT_ROUGHNESS_R: {
  998. result = "hint_roughness_r";
  999. } break;
  1000. case ShaderNode::Uniform::HINT_ROUGHNESS_G: {
  1001. result = "hint_roughness_g";
  1002. } break;
  1003. case ShaderNode::Uniform::HINT_ROUGHNESS_B: {
  1004. result = "hint_roughness_b";
  1005. } break;
  1006. case ShaderNode::Uniform::HINT_ROUGHNESS_A: {
  1007. result = "hint_roughness_a";
  1008. } break;
  1009. case ShaderNode::Uniform::HINT_ROUGHNESS_GRAY: {
  1010. result = "hint_roughness_gray";
  1011. } break;
  1012. case ShaderNode::Uniform::HINT_DEFAULT_BLACK: {
  1013. result = "hint_default_black";
  1014. } break;
  1015. case ShaderNode::Uniform::HINT_DEFAULT_WHITE: {
  1016. result = "hint_default_white";
  1017. } break;
  1018. case ShaderNode::Uniform::HINT_DEFAULT_TRANSPARENT: {
  1019. result = "hint_default_transparent";
  1020. } break;
  1021. case ShaderNode::Uniform::HINT_ANISOTROPY: {
  1022. result = "hint_anisotropy";
  1023. } break;
  1024. case ShaderNode::Uniform::HINT_SCREEN_TEXTURE: {
  1025. result = "hint_screen_texture";
  1026. } break;
  1027. case ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE: {
  1028. result = "hint_normal_roughness_texture";
  1029. } break;
  1030. case ShaderNode::Uniform::HINT_DEPTH_TEXTURE: {
  1031. result = "hint_depth_texture";
  1032. } break;
  1033. default:
  1034. break;
  1035. }
  1036. return result;
  1037. }
  1038. String ShaderLanguage::get_texture_filter_name(TextureFilter p_filter) {
  1039. String result;
  1040. switch (p_filter) {
  1041. case FILTER_NEAREST: {
  1042. result = "filter_nearest";
  1043. } break;
  1044. case FILTER_LINEAR: {
  1045. result = "filter_linear";
  1046. } break;
  1047. case FILTER_NEAREST_MIPMAP: {
  1048. result = "filter_nearest_mipmap";
  1049. } break;
  1050. case FILTER_LINEAR_MIPMAP: {
  1051. result = "filter_linear_mipmap";
  1052. } break;
  1053. case FILTER_NEAREST_MIPMAP_ANISOTROPIC: {
  1054. result = "filter_nearest_mipmap_anisotropic";
  1055. } break;
  1056. case FILTER_LINEAR_MIPMAP_ANISOTROPIC: {
  1057. result = "filter_linear_mipmap_anisotropic";
  1058. } break;
  1059. default: {
  1060. } break;
  1061. }
  1062. return result;
  1063. }
  1064. String ShaderLanguage::get_texture_repeat_name(TextureRepeat p_repeat) {
  1065. String result;
  1066. switch (p_repeat) {
  1067. case REPEAT_DISABLE: {
  1068. result = "repeat_disable";
  1069. } break;
  1070. case REPEAT_ENABLE: {
  1071. result = "repeat_enable";
  1072. } break;
  1073. default: {
  1074. } break;
  1075. }
  1076. return result;
  1077. }
  1078. bool ShaderLanguage::is_token_nonvoid_datatype(TokenType p_type) {
  1079. return is_token_datatype(p_type) && p_type != TK_TYPE_VOID;
  1080. }
  1081. void ShaderLanguage::clear() {
  1082. current_function = StringName();
  1083. last_name = StringName();
  1084. last_type = IDENTIFIER_MAX;
  1085. current_uniform_group_name = "";
  1086. current_uniform_subgroup_name = "";
  1087. current_uniform_hint = ShaderNode::Uniform::HINT_NONE;
  1088. current_uniform_filter = FILTER_DEFAULT;
  1089. current_uniform_repeat = REPEAT_DEFAULT;
  1090. current_uniform_instance_index_defined = false;
  1091. completion_type = COMPLETION_NONE;
  1092. completion_block = nullptr;
  1093. completion_function = StringName();
  1094. completion_class = TAG_GLOBAL;
  1095. completion_struct = StringName();
  1096. completion_base = TYPE_VOID;
  1097. completion_base_array = false;
  1098. include_positions.clear();
  1099. include_positions.push_back(FilePosition());
  1100. #ifdef DEBUG_ENABLED
  1101. keyword_completion_context = CF_UNSPECIFIED;
  1102. used_constants.clear();
  1103. used_varyings.clear();
  1104. used_uniforms.clear();
  1105. used_functions.clear();
  1106. used_structs.clear();
  1107. used_local_vars.clear();
  1108. warnings.clear();
  1109. #endif // DEBUG_ENABLED
  1110. error_line = 0;
  1111. tk_line = 1;
  1112. char_idx = 0;
  1113. error_set = false;
  1114. error_str = "";
  1115. is_const_decl = false;
  1116. while (nodes) {
  1117. Node *n = nodes;
  1118. nodes = nodes->next;
  1119. memdelete(n);
  1120. }
  1121. }
  1122. #ifdef DEBUG_ENABLED
  1123. void ShaderLanguage::_parse_used_identifier(const StringName &p_identifier, IdentifierType p_type, const StringName &p_function) {
  1124. switch (p_type) {
  1125. case IdentifierType::IDENTIFIER_CONSTANT:
  1126. if (HAS_WARNING(ShaderWarning::UNUSED_CONSTANT_FLAG) && used_constants.has(p_identifier)) {
  1127. used_constants[p_identifier].used = true;
  1128. }
  1129. break;
  1130. case IdentifierType::IDENTIFIER_VARYING:
  1131. if (HAS_WARNING(ShaderWarning::UNUSED_VARYING_FLAG) && used_varyings.has(p_identifier)) {
  1132. if (shader->varyings[p_identifier].stage != ShaderNode::Varying::STAGE_VERTEX && shader->varyings[p_identifier].stage != ShaderNode::Varying::STAGE_FRAGMENT) {
  1133. used_varyings[p_identifier].used = true;
  1134. }
  1135. }
  1136. break;
  1137. case IdentifierType::IDENTIFIER_UNIFORM:
  1138. if (HAS_WARNING(ShaderWarning::UNUSED_UNIFORM_FLAG) && used_uniforms.has(p_identifier)) {
  1139. used_uniforms[p_identifier].used = true;
  1140. }
  1141. break;
  1142. case IdentifierType::IDENTIFIER_FUNCTION:
  1143. if (HAS_WARNING(ShaderWarning::UNUSED_FUNCTION_FLAG) && used_functions.has(p_identifier)) {
  1144. used_functions[p_identifier].used = true;
  1145. }
  1146. break;
  1147. case IdentifierType::IDENTIFIER_LOCAL_VAR:
  1148. if (HAS_WARNING(ShaderWarning::UNUSED_LOCAL_VARIABLE_FLAG) && used_local_vars.has(p_function) && used_local_vars[p_function].has(p_identifier)) {
  1149. used_local_vars[p_function][p_identifier].used = true;
  1150. }
  1151. break;
  1152. default:
  1153. break;
  1154. }
  1155. }
  1156. #endif // DEBUG_ENABLED
  1157. bool ShaderLanguage::_find_identifier(const BlockNode *p_block, bool p_allow_reassign, const FunctionInfo &p_function_info, const StringName &p_identifier, DataType *r_data_type, IdentifierType *r_type, bool *r_is_const, int *r_array_size, StringName *r_struct_name, ConstantNode::Value *r_constant_value) {
  1158. if (is_shader_inc) {
  1159. for (int i = 0; i < RenderingServer::SHADER_MAX; i++) {
  1160. for (const KeyValue<StringName, FunctionInfo> &E : ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(i))) {
  1161. if ((current_function == E.key || E.key == "global" || E.key == "constants") && E.value.built_ins.has(p_identifier)) {
  1162. if (r_data_type) {
  1163. *r_data_type = E.value.built_ins[p_identifier].type;
  1164. }
  1165. if (r_is_const) {
  1166. *r_is_const = E.value.built_ins[p_identifier].constant;
  1167. }
  1168. if (r_type) {
  1169. *r_type = IDENTIFIER_BUILTIN_VAR;
  1170. }
  1171. return true;
  1172. }
  1173. }
  1174. }
  1175. } else {
  1176. if (p_function_info.built_ins.has(p_identifier)) {
  1177. if (r_data_type) {
  1178. *r_data_type = p_function_info.built_ins[p_identifier].type;
  1179. }
  1180. if (r_is_const) {
  1181. *r_is_const = p_function_info.built_ins[p_identifier].constant;
  1182. }
  1183. if (r_type) {
  1184. *r_type = IDENTIFIER_BUILTIN_VAR;
  1185. }
  1186. return true;
  1187. }
  1188. }
  1189. if (p_function_info.stage_functions.has(p_identifier)) {
  1190. if (r_data_type) {
  1191. *r_data_type = p_function_info.stage_functions[p_identifier].return_type;
  1192. }
  1193. if (r_is_const) {
  1194. *r_is_const = true;
  1195. }
  1196. if (r_type) {
  1197. *r_type = IDENTIFIER_FUNCTION;
  1198. }
  1199. return true;
  1200. }
  1201. FunctionNode *function = nullptr;
  1202. while (p_block) {
  1203. if (p_block->variables.has(p_identifier)) {
  1204. if (r_data_type) {
  1205. *r_data_type = p_block->variables[p_identifier].type;
  1206. }
  1207. if (r_is_const) {
  1208. *r_is_const = p_block->variables[p_identifier].is_const;
  1209. }
  1210. if (r_array_size) {
  1211. *r_array_size = p_block->variables[p_identifier].array_size;
  1212. }
  1213. if (r_struct_name) {
  1214. *r_struct_name = p_block->variables[p_identifier].struct_name;
  1215. }
  1216. if (r_constant_value) {
  1217. *r_constant_value = p_block->variables[p_identifier].value;
  1218. }
  1219. if (r_type) {
  1220. *r_type = IDENTIFIER_LOCAL_VAR;
  1221. }
  1222. return true;
  1223. }
  1224. if (p_block->parent_function) {
  1225. function = p_block->parent_function;
  1226. break;
  1227. } else {
  1228. if (p_allow_reassign) {
  1229. break;
  1230. }
  1231. ERR_FAIL_COND_V(!p_block->parent_block, false);
  1232. p_block = p_block->parent_block;
  1233. }
  1234. }
  1235. if (function) {
  1236. for (int i = 0; i < function->arguments.size(); i++) {
  1237. if (function->arguments[i].name == p_identifier) {
  1238. if (r_data_type) {
  1239. *r_data_type = function->arguments[i].type;
  1240. }
  1241. if (r_struct_name) {
  1242. *r_struct_name = function->arguments[i].type_str;
  1243. }
  1244. if (r_array_size) {
  1245. *r_array_size = function->arguments[i].array_size;
  1246. }
  1247. if (r_is_const) {
  1248. *r_is_const = function->arguments[i].is_const;
  1249. }
  1250. if (r_type) {
  1251. *r_type = IDENTIFIER_FUNCTION_ARGUMENT;
  1252. }
  1253. return true;
  1254. }
  1255. }
  1256. }
  1257. if (shader->varyings.has(p_identifier)) {
  1258. if (r_data_type) {
  1259. *r_data_type = shader->varyings[p_identifier].type;
  1260. }
  1261. if (r_array_size) {
  1262. *r_array_size = shader->varyings[p_identifier].array_size;
  1263. }
  1264. if (r_type) {
  1265. *r_type = IDENTIFIER_VARYING;
  1266. }
  1267. return true;
  1268. }
  1269. if (shader->uniforms.has(p_identifier)) {
  1270. if (r_data_type) {
  1271. *r_data_type = shader->uniforms[p_identifier].type;
  1272. }
  1273. if (r_array_size) {
  1274. *r_array_size = shader->uniforms[p_identifier].array_size;
  1275. }
  1276. if (r_type) {
  1277. *r_type = IDENTIFIER_UNIFORM;
  1278. }
  1279. return true;
  1280. }
  1281. if (shader->constants.has(p_identifier)) {
  1282. if (r_is_const) {
  1283. *r_is_const = true;
  1284. }
  1285. if (r_data_type) {
  1286. *r_data_type = shader->constants[p_identifier].type;
  1287. }
  1288. if (r_array_size) {
  1289. *r_array_size = shader->constants[p_identifier].array_size;
  1290. }
  1291. if (r_struct_name) {
  1292. *r_struct_name = shader->constants[p_identifier].type_str;
  1293. }
  1294. if (r_constant_value) {
  1295. if (shader->constants[p_identifier].initializer && shader->constants[p_identifier].initializer->values.size() == 1) {
  1296. *r_constant_value = shader->constants[p_identifier].initializer->values[0];
  1297. }
  1298. }
  1299. if (r_type) {
  1300. *r_type = IDENTIFIER_CONSTANT;
  1301. }
  1302. return true;
  1303. }
  1304. for (int i = 0; i < shader->functions.size(); i++) {
  1305. if (!shader->functions[i].callable) {
  1306. continue;
  1307. }
  1308. if (shader->functions[i].name == p_identifier) {
  1309. if (r_data_type) {
  1310. *r_data_type = shader->functions[i].function->return_type;
  1311. }
  1312. if (r_array_size) {
  1313. *r_array_size = shader->functions[i].function->return_array_size;
  1314. }
  1315. if (r_type) {
  1316. *r_type = IDENTIFIER_FUNCTION;
  1317. }
  1318. return true;
  1319. }
  1320. }
  1321. return false;
  1322. }
  1323. bool ShaderLanguage::_validate_operator(OperatorNode *p_op, DataType *r_ret_type, int *r_ret_size) {
  1324. bool valid = false;
  1325. DataType ret_type = TYPE_VOID;
  1326. int ret_size = 0;
  1327. switch (p_op->op) {
  1328. case OP_EQUAL:
  1329. case OP_NOT_EQUAL: {
  1330. 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)) {
  1331. break; // don't accept arrays
  1332. }
  1333. DataType na = p_op->arguments[0]->get_datatype();
  1334. DataType nb = p_op->arguments[1]->get_datatype();
  1335. valid = na == nb;
  1336. ret_type = TYPE_BOOL;
  1337. } break;
  1338. case OP_LESS:
  1339. case OP_LESS_EQUAL:
  1340. case OP_GREATER:
  1341. case OP_GREATER_EQUAL: {
  1342. 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)) {
  1343. break; // don't accept arrays
  1344. }
  1345. DataType na = p_op->arguments[0]->get_datatype();
  1346. DataType nb = p_op->arguments[1]->get_datatype();
  1347. valid = na == nb && (na == TYPE_UINT || na == TYPE_INT || na == TYPE_FLOAT);
  1348. ret_type = TYPE_BOOL;
  1349. } break;
  1350. case OP_AND:
  1351. case OP_OR: {
  1352. 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)) {
  1353. break; // don't accept arrays
  1354. }
  1355. DataType na = p_op->arguments[0]->get_datatype();
  1356. DataType nb = p_op->arguments[1]->get_datatype();
  1357. valid = na == nb && na == TYPE_BOOL;
  1358. ret_type = TYPE_BOOL;
  1359. } break;
  1360. case OP_NOT: {
  1361. if (!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) {
  1362. break; // don't accept arrays
  1363. }
  1364. DataType na = p_op->arguments[0]->get_datatype();
  1365. valid = na == TYPE_BOOL;
  1366. ret_type = TYPE_BOOL;
  1367. } break;
  1368. case OP_INCREMENT:
  1369. case OP_DECREMENT:
  1370. case OP_POST_INCREMENT:
  1371. case OP_POST_DECREMENT:
  1372. case OP_NEGATE: {
  1373. if (!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) {
  1374. break; // don't accept arrays
  1375. }
  1376. DataType na = p_op->arguments[0]->get_datatype();
  1377. valid = na > TYPE_BOOL && na < TYPE_MAT2;
  1378. ret_type = na;
  1379. } break;
  1380. case OP_ADD:
  1381. case OP_SUB:
  1382. case OP_MUL:
  1383. case OP_DIV: {
  1384. 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)) {
  1385. break; // don't accept arrays
  1386. }
  1387. DataType na = p_op->arguments[0]->get_datatype();
  1388. DataType nb = p_op->arguments[1]->get_datatype();
  1389. if (na > nb) {
  1390. //make things easier;
  1391. SWAP(na, nb);
  1392. }
  1393. if (na == nb) {
  1394. valid = (na > TYPE_BOOL && na <= TYPE_MAT4);
  1395. ret_type = na;
  1396. } else if (na == TYPE_INT && nb == TYPE_IVEC2) {
  1397. valid = true;
  1398. ret_type = TYPE_IVEC2;
  1399. } else if (na == TYPE_INT && nb == TYPE_IVEC3) {
  1400. valid = true;
  1401. ret_type = TYPE_IVEC3;
  1402. } else if (na == TYPE_INT && nb == TYPE_IVEC4) {
  1403. valid = true;
  1404. ret_type = TYPE_IVEC4;
  1405. } else if (na == TYPE_UINT && nb == TYPE_UVEC2) {
  1406. valid = true;
  1407. ret_type = TYPE_UVEC2;
  1408. } else if (na == TYPE_UINT && nb == TYPE_UVEC3) {
  1409. valid = true;
  1410. ret_type = TYPE_UVEC3;
  1411. } else if (na == TYPE_UINT && nb == TYPE_UVEC4) {
  1412. valid = true;
  1413. ret_type = TYPE_UVEC4;
  1414. } else if (na == TYPE_FLOAT && nb == TYPE_VEC2) {
  1415. valid = true;
  1416. ret_type = TYPE_VEC2;
  1417. } else if (na == TYPE_FLOAT && nb == TYPE_VEC3) {
  1418. valid = true;
  1419. ret_type = TYPE_VEC3;
  1420. } else if (na == TYPE_FLOAT && nb == TYPE_VEC4) {
  1421. valid = true;
  1422. ret_type = TYPE_VEC4;
  1423. } else if (na == TYPE_FLOAT && nb == TYPE_MAT2) {
  1424. valid = true;
  1425. ret_type = TYPE_MAT2;
  1426. } else if (na == TYPE_FLOAT && nb == TYPE_MAT3) {
  1427. valid = true;
  1428. ret_type = TYPE_MAT3;
  1429. } else if (na == TYPE_FLOAT && nb == TYPE_MAT4) {
  1430. valid = true;
  1431. ret_type = TYPE_MAT4;
  1432. } else if (p_op->op == OP_MUL && na == TYPE_VEC2 && nb == TYPE_MAT2) {
  1433. valid = true;
  1434. ret_type = TYPE_VEC2;
  1435. } else if (p_op->op == OP_MUL && na == TYPE_VEC3 && nb == TYPE_MAT3) {
  1436. valid = true;
  1437. ret_type = TYPE_VEC3;
  1438. } else if (p_op->op == OP_MUL && na == TYPE_VEC4 && nb == TYPE_MAT4) {
  1439. valid = true;
  1440. ret_type = TYPE_VEC4;
  1441. }
  1442. } break;
  1443. case OP_ASSIGN_MOD:
  1444. case OP_MOD: {
  1445. /*
  1446. * The operator modulus (%) operates on signed or unsigned integers or integer vectors. The operand
  1447. * types must both be signed or both be unsigned. The operands cannot be vectors of differing size. If
  1448. * one operand is a scalar and the other vector, then the scalar is applied component-wise to the vector,
  1449. * resulting in the same type as the vector. If both are vectors of the same size, the result is computed
  1450. * component-wise.
  1451. */
  1452. 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)) {
  1453. break; // don't accept arrays
  1454. }
  1455. DataType na = p_op->arguments[0]->get_datatype();
  1456. DataType nb = p_op->arguments[1]->get_datatype();
  1457. if (na == TYPE_INT && nb == TYPE_INT) {
  1458. valid = true;
  1459. ret_type = TYPE_INT;
  1460. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1461. valid = true;
  1462. ret_type = TYPE_IVEC2;
  1463. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1464. valid = true;
  1465. ret_type = TYPE_IVEC3;
  1466. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1467. valid = true;
  1468. ret_type = TYPE_IVEC4;
  1469. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  1470. valid = true;
  1471. ret_type = TYPE_IVEC2;
  1472. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  1473. valid = true;
  1474. ret_type = TYPE_IVEC3;
  1475. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  1476. valid = true;
  1477. ret_type = TYPE_IVEC4;
  1478. /////
  1479. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1480. valid = true;
  1481. ret_type = TYPE_UINT;
  1482. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1483. valid = true;
  1484. ret_type = TYPE_UVEC2;
  1485. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1486. valid = true;
  1487. ret_type = TYPE_UVEC3;
  1488. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1489. valid = true;
  1490. ret_type = TYPE_UVEC4;
  1491. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1492. valid = true;
  1493. ret_type = TYPE_UVEC2;
  1494. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1495. valid = true;
  1496. ret_type = TYPE_UVEC3;
  1497. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1498. valid = true;
  1499. ret_type = TYPE_UVEC4;
  1500. }
  1501. } break;
  1502. case OP_ASSIGN_SHIFT_LEFT:
  1503. case OP_ASSIGN_SHIFT_RIGHT:
  1504. case OP_SHIFT_LEFT:
  1505. case OP_SHIFT_RIGHT: {
  1506. 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)) {
  1507. break; // don't accept arrays
  1508. }
  1509. DataType na = p_op->arguments[0]->get_datatype();
  1510. DataType nb = p_op->arguments[1]->get_datatype();
  1511. if (na == TYPE_INT && nb == TYPE_INT) {
  1512. valid = true;
  1513. ret_type = TYPE_INT;
  1514. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1515. valid = true;
  1516. ret_type = TYPE_IVEC2;
  1517. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1518. valid = true;
  1519. ret_type = TYPE_IVEC3;
  1520. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1521. valid = true;
  1522. ret_type = TYPE_IVEC4;
  1523. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  1524. valid = true;
  1525. ret_type = TYPE_IVEC2;
  1526. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  1527. valid = true;
  1528. ret_type = TYPE_IVEC3;
  1529. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  1530. valid = true;
  1531. ret_type = TYPE_IVEC4;
  1532. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1533. valid = true;
  1534. ret_type = TYPE_UINT;
  1535. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1536. valid = true;
  1537. ret_type = TYPE_UVEC2;
  1538. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1539. valid = true;
  1540. ret_type = TYPE_UVEC3;
  1541. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1542. valid = true;
  1543. ret_type = TYPE_UVEC4;
  1544. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1545. valid = true;
  1546. ret_type = TYPE_UVEC2;
  1547. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1548. valid = true;
  1549. ret_type = TYPE_UVEC3;
  1550. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1551. valid = true;
  1552. ret_type = TYPE_UVEC4;
  1553. }
  1554. } break;
  1555. case OP_ASSIGN: {
  1556. int sa = 0;
  1557. int sb = 0;
  1558. if (!p_op->arguments[0]->is_indexed()) {
  1559. sa = p_op->arguments[0]->get_array_size();
  1560. }
  1561. if (!p_op->arguments[1]->is_indexed()) {
  1562. sb = p_op->arguments[1]->get_array_size();
  1563. }
  1564. if (sa != sb) {
  1565. break; // don't accept arrays if their sizes are not equal
  1566. }
  1567. DataType na = p_op->arguments[0]->get_datatype();
  1568. DataType nb = p_op->arguments[1]->get_datatype();
  1569. if (na == TYPE_STRUCT || nb == TYPE_STRUCT) {
  1570. valid = p_op->arguments[0]->get_datatype_name() == p_op->arguments[1]->get_datatype_name();
  1571. } else {
  1572. valid = na == nb;
  1573. }
  1574. ret_type = na;
  1575. ret_size = sa;
  1576. } break;
  1577. case OP_ASSIGN_ADD:
  1578. case OP_ASSIGN_SUB:
  1579. case OP_ASSIGN_MUL:
  1580. case OP_ASSIGN_DIV: {
  1581. int sa = 0;
  1582. int sb = 0;
  1583. if (!p_op->arguments[0]->is_indexed()) {
  1584. sa = p_op->arguments[0]->get_array_size();
  1585. }
  1586. if (!p_op->arguments[1]->is_indexed()) {
  1587. sb = p_op->arguments[1]->get_array_size();
  1588. }
  1589. if (sa > 0 || sb > 0) {
  1590. break; // don't accept arrays
  1591. }
  1592. DataType na = p_op->arguments[0]->get_datatype();
  1593. DataType nb = p_op->arguments[1]->get_datatype();
  1594. if (na == nb) {
  1595. valid = (na > TYPE_BOOL && na <= TYPE_MAT4);
  1596. ret_type = na;
  1597. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1598. valid = true;
  1599. ret_type = TYPE_IVEC2;
  1600. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1601. valid = true;
  1602. ret_type = TYPE_IVEC3;
  1603. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1604. valid = true;
  1605. ret_type = TYPE_IVEC4;
  1606. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1607. valid = true;
  1608. ret_type = TYPE_UVEC2;
  1609. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1610. valid = true;
  1611. ret_type = TYPE_UVEC3;
  1612. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1613. valid = true;
  1614. ret_type = TYPE_UVEC4;
  1615. } else if (na == TYPE_VEC2 && nb == TYPE_FLOAT) {
  1616. valid = true;
  1617. ret_type = TYPE_VEC2;
  1618. } else if (na == TYPE_VEC3 && nb == TYPE_FLOAT) {
  1619. valid = true;
  1620. ret_type = TYPE_VEC3;
  1621. } else if (na == TYPE_VEC4 && nb == TYPE_FLOAT) {
  1622. valid = true;
  1623. ret_type = TYPE_VEC4;
  1624. } else if (na == TYPE_MAT2 && nb == TYPE_FLOAT) {
  1625. valid = true;
  1626. ret_type = TYPE_MAT2;
  1627. } else if (na == TYPE_MAT3 && nb == TYPE_FLOAT) {
  1628. valid = true;
  1629. ret_type = TYPE_MAT3;
  1630. } else if (na == TYPE_MAT4 && nb == TYPE_FLOAT) {
  1631. valid = true;
  1632. ret_type = TYPE_MAT4;
  1633. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC2 && nb == TYPE_MAT2) {
  1634. valid = true;
  1635. ret_type = TYPE_VEC2;
  1636. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC3 && nb == TYPE_MAT3) {
  1637. valid = true;
  1638. ret_type = TYPE_VEC3;
  1639. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_VEC4 && nb == TYPE_MAT4) {
  1640. valid = true;
  1641. ret_type = TYPE_VEC4;
  1642. }
  1643. } break;
  1644. case OP_ASSIGN_BIT_AND:
  1645. case OP_ASSIGN_BIT_OR:
  1646. case OP_ASSIGN_BIT_XOR:
  1647. case OP_BIT_AND:
  1648. case OP_BIT_OR:
  1649. case OP_BIT_XOR: {
  1650. /*
  1651. * The bitwise operators and (&), exclusive-or (^), and inclusive-or (|). The operands must be of type
  1652. * signed or unsigned integers or integer vectors. The operands cannot be vectors of differing size. If
  1653. * one operand is a scalar and the other a vector, the scalar is applied component-wise to the vector,
  1654. * resulting in the same type as the vector. The fundamental types of the operands (signed or unsigned)
  1655. * must match.
  1656. */
  1657. int sa = 0;
  1658. int sb = 0;
  1659. if (!p_op->arguments[0]->is_indexed()) {
  1660. sa = p_op->arguments[0]->get_array_size();
  1661. }
  1662. if (!p_op->arguments[1]->is_indexed()) {
  1663. sb = p_op->arguments[1]->get_array_size();
  1664. }
  1665. if (sa > 0 || sb > 0) {
  1666. break; // don't accept arrays
  1667. }
  1668. DataType na = p_op->arguments[0]->get_datatype();
  1669. DataType nb = p_op->arguments[1]->get_datatype();
  1670. if (na > nb && p_op->op >= OP_BIT_AND) {
  1671. //can swap for non assign
  1672. SWAP(na, nb);
  1673. }
  1674. if (na == TYPE_INT && nb == TYPE_INT) {
  1675. valid = true;
  1676. ret_type = TYPE_INT;
  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_IVEC2 && nb == TYPE_IVEC2) {
  1687. valid = true;
  1688. ret_type = TYPE_IVEC2;
  1689. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  1690. valid = true;
  1691. ret_type = TYPE_IVEC3;
  1692. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  1693. valid = true;
  1694. ret_type = TYPE_IVEC4;
  1695. /////
  1696. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1697. valid = true;
  1698. ret_type = TYPE_UINT;
  1699. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1700. valid = true;
  1701. ret_type = TYPE_UVEC2;
  1702. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1703. valid = true;
  1704. ret_type = TYPE_UVEC3;
  1705. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1706. valid = true;
  1707. ret_type = TYPE_UVEC4;
  1708. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1709. valid = true;
  1710. ret_type = TYPE_UVEC2;
  1711. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1712. valid = true;
  1713. ret_type = TYPE_UVEC3;
  1714. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1715. valid = true;
  1716. ret_type = TYPE_UVEC4;
  1717. }
  1718. } break;
  1719. case OP_BIT_INVERT: { //unaries
  1720. if (!p_op->arguments[0]->is_indexed() && p_op->arguments[0]->get_array_size() > 0) {
  1721. break; // don't accept arrays
  1722. }
  1723. DataType na = p_op->arguments[0]->get_datatype();
  1724. valid = na >= TYPE_INT && na < TYPE_FLOAT;
  1725. ret_type = na;
  1726. } break;
  1727. case OP_SELECT_IF: {
  1728. int sa = 0;
  1729. int sb = 0;
  1730. if (!p_op->arguments[1]->is_indexed()) {
  1731. sa = p_op->arguments[1]->get_array_size();
  1732. }
  1733. if (!p_op->arguments[2]->is_indexed()) {
  1734. sb = p_op->arguments[2]->get_array_size();
  1735. }
  1736. if (sa != sb) {
  1737. break; // don't accept arrays if their sizes are not equal
  1738. }
  1739. DataType na = p_op->arguments[0]->get_datatype();
  1740. DataType nb = p_op->arguments[1]->get_datatype();
  1741. DataType nc = p_op->arguments[2]->get_datatype();
  1742. valid = na == TYPE_BOOL && (nb == nc);
  1743. ret_type = nb;
  1744. ret_size = sa;
  1745. } break;
  1746. default: {
  1747. ERR_FAIL_V(false);
  1748. }
  1749. }
  1750. if (r_ret_type) {
  1751. *r_ret_type = ret_type;
  1752. }
  1753. if (r_ret_size) {
  1754. *r_ret_size = ret_size;
  1755. }
  1756. return valid;
  1757. }
  1758. const ShaderLanguage::BuiltinFuncDef ShaderLanguage::builtin_func_defs[] = {
  1759. // Constructors.
  1760. { "bool", TYPE_BOOL, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1761. { "bvec2", TYPE_BVEC2, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1762. { "bvec2", TYPE_BVEC2, { TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1763. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1764. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1765. { "bvec3", TYPE_BVEC3, { TYPE_BVEC2, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1766. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1767. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1768. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1769. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BVEC2, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1770. { "bvec4", TYPE_BVEC4, { TYPE_BVEC2, TYPE_BOOL, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1771. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BOOL, TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1772. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1773. { "bvec4", TYPE_BVEC4, { TYPE_BVEC3, TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1774. { "bvec4", TYPE_BVEC4, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1775. { "float", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1776. { "vec2", TYPE_VEC2, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1777. { "vec2", TYPE_VEC2, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1778. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1779. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1780. { "vec3", TYPE_VEC3, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1781. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1782. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1783. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1784. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1785. { "vec4", TYPE_VEC4, { TYPE_VEC2, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1786. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1787. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1788. { "vec4", TYPE_VEC4, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1789. { "vec4", TYPE_VEC4, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1790. { "int", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1791. { "ivec2", TYPE_IVEC2, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1792. { "ivec2", TYPE_IVEC2, { TYPE_INT, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1793. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1794. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1795. { "ivec3", TYPE_IVEC3, { TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1796. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1797. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1798. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1799. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1800. { "ivec4", TYPE_IVEC4, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1801. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_INT, TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1802. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1803. { "ivec4", TYPE_IVEC4, { TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1804. { "ivec4", TYPE_IVEC4, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1805. { "uint", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1806. { "uvec2", TYPE_UVEC2, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1807. { "uvec2", TYPE_UVEC2, { TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1808. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1809. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1810. { "uvec3", TYPE_UVEC3, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1811. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1812. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1813. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1814. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1815. { "uvec4", TYPE_UVEC4, { TYPE_UVEC2, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1816. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UINT, TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1817. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1818. { "uvec4", TYPE_UVEC4, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1819. { "uvec4", TYPE_UVEC4, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1820. { "mat2", TYPE_MAT2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1821. { "mat3", TYPE_MAT3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1822. { "mat4", TYPE_MAT4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1823. { "mat2", TYPE_MAT2, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1824. { "mat3", TYPE_MAT3, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1825. { "mat4", TYPE_MAT4, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1826. // Conversion scalars.
  1827. { "int", TYPE_INT, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1828. { "int", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1829. { "int", TYPE_INT, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1830. { "int", TYPE_INT, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1831. { "float", TYPE_FLOAT, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1832. { "float", TYPE_FLOAT, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1833. { "float", TYPE_FLOAT, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1834. { "float", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1835. { "uint", TYPE_UINT, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1836. { "uint", TYPE_UINT, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1837. { "uint", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1838. { "uint", TYPE_UINT, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1839. { "bool", TYPE_BOOL, { TYPE_BOOL, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1840. { "bool", TYPE_BOOL, { TYPE_INT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1841. { "bool", TYPE_BOOL, { TYPE_UINT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1842. { "bool", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1843. // Conversion vectors.
  1844. { "ivec2", TYPE_IVEC2, { TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1845. { "ivec2", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1846. { "ivec2", TYPE_IVEC2, { TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1847. { "ivec2", TYPE_IVEC2, { TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1848. { "vec2", TYPE_VEC2, { TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1849. { "vec2", TYPE_VEC2, { TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1850. { "vec2", TYPE_VEC2, { TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1851. { "vec2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1852. { "uvec2", TYPE_UVEC2, { TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1853. { "uvec2", TYPE_UVEC2, { TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1854. { "uvec2", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1855. { "uvec2", TYPE_UVEC2, { TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1856. { "bvec2", TYPE_BVEC2, { TYPE_BVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1857. { "bvec2", TYPE_BVEC2, { TYPE_IVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1858. { "bvec2", TYPE_BVEC2, { TYPE_UVEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1859. { "bvec2", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1860. { "ivec3", TYPE_IVEC3, { TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1861. { "ivec3", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1862. { "ivec3", TYPE_IVEC3, { TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1863. { "ivec3", TYPE_IVEC3, { TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1864. { "vec3", TYPE_VEC3, { TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1865. { "vec3", TYPE_VEC3, { TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1866. { "vec3", TYPE_VEC3, { TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1867. { "vec3", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1868. { "uvec3", TYPE_UVEC3, { TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1869. { "uvec3", TYPE_UVEC3, { TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1870. { "uvec3", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1871. { "uvec3", TYPE_UVEC3, { TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1872. { "bvec3", TYPE_BVEC3, { TYPE_BVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1873. { "bvec3", TYPE_BVEC3, { TYPE_IVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1874. { "bvec3", TYPE_BVEC3, { TYPE_UVEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1875. { "bvec3", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1876. { "ivec4", TYPE_IVEC4, { TYPE_BVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1877. { "ivec4", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1878. { "ivec4", TYPE_IVEC4, { TYPE_UVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1879. { "ivec4", TYPE_IVEC4, { TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1880. { "vec4", TYPE_VEC4, { TYPE_BVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1881. { "vec4", TYPE_VEC4, { TYPE_IVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1882. { "vec4", TYPE_VEC4, { TYPE_UVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1883. { "vec4", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1884. { "uvec4", TYPE_UVEC4, { TYPE_BVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1885. { "uvec4", TYPE_UVEC4, { TYPE_IVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1886. { "uvec4", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1887. { "uvec4", TYPE_UVEC4, { TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1888. { "bvec4", TYPE_BVEC4, { TYPE_BVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1889. { "bvec4", TYPE_BVEC4, { TYPE_IVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1890. { "bvec4", TYPE_BVEC4, { TYPE_UVEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1891. { "bvec4", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1892. // Conversion between matrixes.
  1893. { "mat2", TYPE_MAT2, { TYPE_MAT3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1894. { "mat2", TYPE_MAT2, { TYPE_MAT4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1895. { "mat3", TYPE_MAT3, { TYPE_MAT2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1896. { "mat3", TYPE_MAT3, { TYPE_MAT4, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1897. { "mat4", TYPE_MAT4, { TYPE_MAT2, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1898. { "mat4", TYPE_MAT4, { TYPE_MAT3, TYPE_VOID }, { "" }, TAG_GLOBAL, false },
  1899. // Built-ins - trigonometric functions.
  1900. // radians
  1901. { "radians", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "degrees" }, TAG_GLOBAL, false },
  1902. { "radians", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "degrees" }, TAG_GLOBAL, false },
  1903. { "radians", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "degrees" }, TAG_GLOBAL, false },
  1904. { "radians", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "degrees" }, TAG_GLOBAL, false },
  1905. // degrees
  1906. { "degrees", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "radians" }, TAG_GLOBAL, false },
  1907. { "degrees", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "radians" }, TAG_GLOBAL, false },
  1908. { "degrees", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "radians" }, TAG_GLOBAL, false },
  1909. { "degrees", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "radians" }, TAG_GLOBAL, false },
  1910. // sin
  1911. { "sin", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1912. { "sin", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1913. { "sin", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1914. { "sin", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1915. // cos
  1916. { "cos", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1917. { "cos", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1918. { "cos", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1919. { "cos", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1920. // tan
  1921. { "tan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1922. { "tan", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1923. { "tan", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1924. { "tan", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "angle" }, TAG_GLOBAL, false },
  1925. // asin
  1926. { "asin", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1927. { "asin", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1928. { "asin", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1929. { "asin", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1930. // acos
  1931. { "acos", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1932. { "acos", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1933. { "acos", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1934. { "acos", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1935. // atan
  1936. { "atan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "y_over_x" }, TAG_GLOBAL, false },
  1937. { "atan", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "y_over_x" }, TAG_GLOBAL, false },
  1938. { "atan", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "y_over_x" }, TAG_GLOBAL, false },
  1939. { "atan", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "y_over_x" }, TAG_GLOBAL, false },
  1940. { "atan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "y", "x" }, TAG_GLOBAL, false },
  1941. { "atan", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "y", "x" }, TAG_GLOBAL, false },
  1942. { "atan", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "y", "x" }, TAG_GLOBAL, false },
  1943. { "atan", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "y", "x" }, TAG_GLOBAL, false },
  1944. // sinh
  1945. { "sinh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1946. { "sinh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1947. { "sinh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1948. { "sinh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1949. // cosh
  1950. { "cosh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1951. { "cosh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1952. { "cosh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1953. { "cosh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1954. // tanh
  1955. { "tanh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1956. { "tanh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1957. { "tanh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1958. { "tanh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1959. // asinh
  1960. { "asinh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1961. { "asinh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1962. { "asinh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1963. { "asinh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1964. // acosh
  1965. { "acosh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1966. { "acosh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1967. { "acosh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1968. { "acosh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1969. // atanh
  1970. { "atanh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1971. { "atanh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1972. { "atanh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1973. { "atanh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1974. // Builtins - exponential functions.
  1975. // pow
  1976. { "pow", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  1977. { "pow", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  1978. { "pow", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  1979. { "pow", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  1980. // exp
  1981. { "exp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1982. { "exp", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1983. { "exp", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1984. { "exp", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1985. // log
  1986. { "log", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1987. { "log", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1988. { "log", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1989. { "log", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1990. // exp2
  1991. { "exp2", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1992. { "exp2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1993. { "exp2", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1994. { "exp2", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1995. // log2
  1996. { "log2", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1997. { "log2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1998. { "log2", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  1999. { "log2", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2000. // sqrt
  2001. { "sqrt", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2002. { "sqrt", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2003. { "sqrt", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2004. { "sqrt", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2005. // inversesqrt
  2006. { "inversesqrt", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2007. { "inversesqrt", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2008. { "inversesqrt", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2009. { "inversesqrt", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2010. // Built-ins - common functions.
  2011. // abs
  2012. { "abs", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2013. { "abs", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2014. { "abs", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2015. { "abs", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2016. { "abs", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2017. { "abs", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2018. { "abs", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2019. { "abs", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2020. // sign
  2021. { "sign", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2022. { "sign", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2023. { "sign", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2024. { "sign", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2025. { "sign", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2026. { "sign", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2027. { "sign", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2028. { "sign", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2029. // floor
  2030. { "floor", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2031. { "floor", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2032. { "floor", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2033. { "floor", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2034. // trunc
  2035. { "trunc", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2036. { "trunc", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2037. { "trunc", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2038. { "trunc", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2039. // round
  2040. { "round", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2041. { "round", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2042. { "round", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2043. { "round", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2044. // roundEven
  2045. { "roundEven", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2046. { "roundEven", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2047. { "roundEven", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2048. { "roundEven", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2049. // ceil
  2050. { "ceil", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2051. { "ceil", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2052. { "ceil", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2053. { "ceil", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2054. // fract
  2055. { "fract", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2056. { "fract", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2057. { "fract", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2058. { "fract", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2059. // mod
  2060. { "mod", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2061. { "mod", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2062. { "mod", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2063. { "mod", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2064. { "mod", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2065. { "mod", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2066. { "mod", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "x", "y" }, TAG_GLOBAL, false },
  2067. // modf
  2068. { "modf", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "i" }, TAG_GLOBAL, false },
  2069. { "modf", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "x", "i" }, TAG_GLOBAL, false },
  2070. { "modf", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "x", "i" }, TAG_GLOBAL, false },
  2071. { "modf", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "x", "i" }, TAG_GLOBAL, false },
  2072. // min
  2073. { "min", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2074. { "min", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2075. { "min", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2076. { "min", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2077. { "min", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2078. { "min", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2079. { "min", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2080. { "min", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2081. { "min", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2082. { "min", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2083. { "min", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2084. { "min", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2085. { "min", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2086. { "min", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2087. { "min", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2088. { "min", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2089. { "min", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2090. { "min", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2091. { "min", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2092. { "min", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2093. { "min", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2094. // max
  2095. { "max", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2096. { "max", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2097. { "max", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2098. { "max", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2099. { "max", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2100. { "max", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2101. { "max", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2102. { "max", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2103. { "max", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2104. { "max", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2105. { "max", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2106. { "max", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2107. { "max", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2108. { "max", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2109. { "max", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2110. { "max", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2111. { "max", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2112. { "max", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2113. { "max", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2114. { "max", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2115. { "max", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2116. // clamp
  2117. { "clamp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2118. { "clamp", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2119. { "clamp", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2120. { "clamp", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2121. { "clamp", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2122. { "clamp", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2123. { "clamp", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2124. { "clamp", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2125. { "clamp", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2126. { "clamp", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2127. { "clamp", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2128. { "clamp", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2129. { "clamp", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2130. { "clamp", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2131. { "clamp", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2132. { "clamp", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2133. { "clamp", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2134. { "clamp", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2135. { "clamp", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2136. { "clamp", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2137. { "clamp", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "minVal", "maxVal" }, TAG_GLOBAL, false },
  2138. // mix
  2139. { "mix", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2140. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2141. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_BVEC2, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2142. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2143. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2144. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_BVEC3, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2145. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2146. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2147. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_BVEC4, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2148. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b", "value" }, TAG_GLOBAL, false },
  2149. // step
  2150. { "step", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2151. { "step", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2152. { "step", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2153. { "step", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2154. { "step", TYPE_VEC2, { TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2155. { "step", TYPE_VEC3, { TYPE_FLOAT, TYPE_VEC3, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2156. { "step", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC4, TYPE_VOID }, { "edge", "x" }, TAG_GLOBAL, false },
  2157. // smoothstep
  2158. { "smoothstep", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2159. { "smoothstep", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2160. { "smoothstep", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2161. { "smoothstep", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2162. { "smoothstep", TYPE_VEC2, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC2, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2163. { "smoothstep", TYPE_VEC3, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC3, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2164. { "smoothstep", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC4, TYPE_VOID }, { "edge0", "edge1", "value" }, TAG_GLOBAL, false },
  2165. // isnan
  2166. { "isnan", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2167. { "isnan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2168. { "isnan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2169. { "isnan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2170. // isinf
  2171. { "isinf", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2172. { "isinf", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2173. { "isinf", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2174. { "isinf", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2175. // floatBitsToInt
  2176. { "floatBitsToInt", TYPE_INT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2177. { "floatBitsToInt", TYPE_IVEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2178. { "floatBitsToInt", TYPE_IVEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2179. { "floatBitsToInt", TYPE_IVEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2180. // floatBitsToUint
  2181. { "floatBitsToUint", TYPE_UINT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2182. { "floatBitsToUint", TYPE_UVEC2, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2183. { "floatBitsToUint", TYPE_UVEC3, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2184. { "floatBitsToUint", TYPE_UVEC4, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2185. // intBitsToFloat
  2186. { "intBitsToFloat", TYPE_FLOAT, { TYPE_INT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2187. { "intBitsToFloat", TYPE_VEC2, { TYPE_IVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2188. { "intBitsToFloat", TYPE_VEC3, { TYPE_IVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2189. { "intBitsToFloat", TYPE_VEC4, { TYPE_IVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2190. // uintBitsToFloat
  2191. { "uintBitsToFloat", TYPE_FLOAT, { TYPE_UINT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2192. { "uintBitsToFloat", TYPE_VEC2, { TYPE_UVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2193. { "uintBitsToFloat", TYPE_VEC3, { TYPE_UVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2194. { "uintBitsToFloat", TYPE_VEC4, { TYPE_UVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2195. // Built-ins - geometric functions.
  2196. // length
  2197. { "length", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2198. { "length", TYPE_FLOAT, { TYPE_VEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2199. { "length", TYPE_FLOAT, { TYPE_VEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2200. { "length", TYPE_FLOAT, { TYPE_VEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2201. // distance
  2202. { "distance", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2203. { "distance", TYPE_FLOAT, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2204. { "distance", TYPE_FLOAT, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2205. { "distance", TYPE_FLOAT, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2206. // dot
  2207. { "dot", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2208. { "dot", TYPE_FLOAT, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2209. { "dot", TYPE_FLOAT, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2210. { "dot", TYPE_FLOAT, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2211. // cross
  2212. { "cross", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2213. // normalize
  2214. { "normalize", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2215. { "normalize", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2216. { "normalize", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2217. { "normalize", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2218. // reflect
  2219. { "reflect", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "I", "N" }, TAG_GLOBAL, false },
  2220. { "reflect", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "I", "N" }, TAG_GLOBAL, false },
  2221. { "reflect", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "I", "N" }, TAG_GLOBAL, false },
  2222. // refract
  2223. { "refract", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "I", "N", "eta" }, TAG_GLOBAL, false },
  2224. { "refract", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "I", "N", "eta" }, TAG_GLOBAL, false },
  2225. { "refract", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "I", "N", "eta" }, TAG_GLOBAL, false },
  2226. // faceforward
  2227. { "faceforward", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "N", "I", "Nref" }, TAG_GLOBAL, false },
  2228. { "faceforward", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "N", "I", "Nref" }, TAG_GLOBAL, false },
  2229. { "faceforward", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "N", "I", "Nref" }, TAG_GLOBAL, false },
  2230. // matrixCompMult
  2231. { "matrixCompMult", TYPE_MAT2, { TYPE_MAT2, TYPE_MAT2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2232. { "matrixCompMult", TYPE_MAT3, { TYPE_MAT3, TYPE_MAT3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2233. { "matrixCompMult", TYPE_MAT4, { TYPE_MAT4, TYPE_MAT4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2234. // outerProduct
  2235. { "outerProduct", TYPE_MAT2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "c", "r" }, TAG_GLOBAL, false },
  2236. { "outerProduct", TYPE_MAT3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "c", "r" }, TAG_GLOBAL, false },
  2237. { "outerProduct", TYPE_MAT4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "c", "r" }, TAG_GLOBAL, false },
  2238. // transpose
  2239. { "transpose", TYPE_MAT2, { TYPE_MAT2, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2240. { "transpose", TYPE_MAT3, { TYPE_MAT3, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2241. { "transpose", TYPE_MAT4, { TYPE_MAT4, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2242. // determinant
  2243. { "determinant", TYPE_FLOAT, { TYPE_MAT2, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2244. { "determinant", TYPE_FLOAT, { TYPE_MAT3, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2245. { "determinant", TYPE_FLOAT, { TYPE_MAT4, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2246. // inverse
  2247. { "inverse", TYPE_MAT2, { TYPE_MAT2, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2248. { "inverse", TYPE_MAT3, { TYPE_MAT3, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2249. { "inverse", TYPE_MAT4, { TYPE_MAT4, TYPE_VOID }, { "m" }, TAG_GLOBAL, false },
  2250. // lessThan
  2251. { "lessThan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2252. { "lessThan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2253. { "lessThan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2254. { "lessThan", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2255. { "lessThan", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2256. { "lessThan", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2257. { "lessThan", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2258. { "lessThan", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2259. { "lessThan", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2260. // greaterThan
  2261. { "greaterThan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2262. { "greaterThan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2263. { "greaterThan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2264. { "greaterThan", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2265. { "greaterThan", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2266. { "greaterThan", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2267. { "greaterThan", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2268. { "greaterThan", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2269. { "greaterThan", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2270. // lessThanEqual
  2271. { "lessThanEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2272. { "lessThanEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2273. { "lessThanEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2274. { "lessThanEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2275. { "lessThanEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2276. { "lessThanEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2277. { "lessThanEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2278. { "lessThanEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2279. { "lessThanEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2280. // greaterThanEqual
  2281. { "greaterThanEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2282. { "greaterThanEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2283. { "greaterThanEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2284. { "greaterThanEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2285. { "greaterThanEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2286. { "greaterThanEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2287. { "greaterThanEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2288. { "greaterThanEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2289. { "greaterThanEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2290. // equal
  2291. { "equal", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2292. { "equal", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2293. { "equal", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2294. { "equal", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2295. { "equal", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2296. { "equal", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2297. { "equal", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2298. { "equal", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2299. { "equal", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2300. { "equal", TYPE_BVEC2, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2301. { "equal", TYPE_BVEC3, { TYPE_BVEC3, TYPE_BVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2302. { "equal", TYPE_BVEC4, { TYPE_BVEC4, TYPE_BVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2303. // notEqual
  2304. { "notEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2305. { "notEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2306. { "notEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2307. { "notEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2308. { "notEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2309. { "notEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2310. { "notEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2311. { "notEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2312. { "notEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2313. { "notEqual", TYPE_BVEC2, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2314. { "notEqual", TYPE_BVEC3, { TYPE_BVEC3, TYPE_BVEC3, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2315. { "notEqual", TYPE_BVEC4, { TYPE_BVEC4, TYPE_BVEC4, TYPE_VOID }, { "a", "b" }, TAG_GLOBAL, false },
  2316. // any
  2317. { "any", TYPE_BOOL, { TYPE_BVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2318. { "any", TYPE_BOOL, { TYPE_BVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2319. { "any", TYPE_BOOL, { TYPE_BVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2320. // all
  2321. { "all", TYPE_BOOL, { TYPE_BVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2322. { "all", TYPE_BOOL, { TYPE_BVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2323. { "all", TYPE_BOOL, { TYPE_BVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2324. // not
  2325. { "not", TYPE_BVEC2, { TYPE_BVEC2, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2326. { "not", TYPE_BVEC3, { TYPE_BVEC3, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2327. { "not", TYPE_BVEC4, { TYPE_BVEC4, TYPE_VOID }, { "x" }, TAG_GLOBAL, false },
  2328. // Built-ins: texture functions.
  2329. // textureSize
  2330. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLER2D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2331. { "textureSize", TYPE_IVEC2, { TYPE_ISAMPLER2D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2332. { "textureSize", TYPE_IVEC2, { TYPE_USAMPLER2D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2333. { "textureSize", TYPE_IVEC3, { TYPE_SAMPLER2DARRAY, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2334. { "textureSize", TYPE_IVEC3, { TYPE_ISAMPLER2DARRAY, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2335. { "textureSize", TYPE_IVEC3, { TYPE_USAMPLER2DARRAY, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2336. { "textureSize", TYPE_IVEC3, { TYPE_SAMPLER3D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2337. { "textureSize", TYPE_IVEC3, { TYPE_ISAMPLER3D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2338. { "textureSize", TYPE_IVEC3, { TYPE_USAMPLER3D, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2339. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLERCUBE, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2340. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLERCUBEARRAY, TYPE_INT, TYPE_VOID }, { "sampler", "lod" }, TAG_GLOBAL, false },
  2341. // texture
  2342. { "texture", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2343. { "texture", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2344. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2345. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2346. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2347. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2348. { "texture", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2349. { "texture", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2350. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2351. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2352. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2353. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2354. { "texture", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2355. { "texture", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2356. { "texture", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2357. { "texture", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2358. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2359. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2360. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2361. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2362. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBEARRAY, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2363. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBEARRAY, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2364. // textureProj
  2365. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2366. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2367. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2368. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2369. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2370. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2371. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2372. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2373. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2374. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2375. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2376. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2377. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2378. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2379. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2380. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2381. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2382. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "bias" }, TAG_GLOBAL, false },
  2383. // textureLod
  2384. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2385. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2386. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2387. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2388. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2389. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2390. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2391. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2392. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2393. { "textureLod", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2394. { "textureLod", TYPE_VEC4, { TYPE_SAMPLERCUBEARRAY, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2395. // texelFetch
  2396. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2397. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2398. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2399. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2400. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2401. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2402. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2403. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2404. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2405. // textureProjLod
  2406. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2407. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2408. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2409. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2410. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2411. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2412. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2413. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2414. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID }, { "sampler", "coords", "lod" }, TAG_GLOBAL, false },
  2415. // textureGrad
  2416. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2417. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2418. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2419. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2420. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2421. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2422. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2423. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2424. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2425. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2426. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLERCUBEARRAY, TYPE_VEC4, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2427. // textureProjGrad
  2428. { "textureProjGrad", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2429. { "textureProjGrad", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2430. { "textureProjGrad", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2431. { "textureProjGrad", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2432. { "textureProjGrad", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2433. { "textureProjGrad", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2434. { "textureProjGrad", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2435. { "textureProjGrad", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2436. { "textureProjGrad", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords", "dPdx", "dPdy" }, TAG_GLOBAL, false },
  2437. // textureGather
  2438. { "textureGather", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2439. { "textureGather", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2440. { "textureGather", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2441. { "textureGather", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2442. { "textureGather", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2443. { "textureGather", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2444. { "textureGather", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2445. { "textureGather", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2446. { "textureGather", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2447. { "textureGather", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2448. { "textureGather", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2449. { "textureGather", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2450. { "textureGather", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VOID }, { "sampler", "coords" }, TAG_GLOBAL, false },
  2451. { "textureGather", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_INT, TYPE_VOID }, { "sampler", "coords", "comp" }, TAG_GLOBAL, false },
  2452. // textureQueryLod
  2453. { "textureQueryLod", TYPE_VEC2, { TYPE_SAMPLER2D, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2454. { "textureQueryLod", TYPE_VEC2, { TYPE_ISAMPLER2D, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2455. { "textureQueryLod", TYPE_VEC2, { TYPE_USAMPLER2D, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2456. { "textureQueryLod", TYPE_VEC2, { TYPE_SAMPLER2DARRAY, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2457. { "textureQueryLod", TYPE_VEC2, { TYPE_ISAMPLER2DARRAY, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2458. { "textureQueryLod", TYPE_VEC2, { TYPE_USAMPLER2DARRAY, TYPE_VEC2 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2459. { "textureQueryLod", TYPE_VEC2, { TYPE_SAMPLER3D, TYPE_VEC3 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2460. { "textureQueryLod", TYPE_VEC2, { TYPE_ISAMPLER3D, TYPE_VEC3 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2461. { "textureQueryLod", TYPE_VEC2, { TYPE_USAMPLER3D, TYPE_VEC3 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2462. { "textureQueryLod", TYPE_VEC2, { TYPE_SAMPLERCUBE, TYPE_VEC3 }, { "sampler", "coords" }, TAG_GLOBAL, true },
  2463. // textureQueryLevels
  2464. { "textureQueryLevels", TYPE_INT, { TYPE_SAMPLER2D }, { "sampler" }, TAG_GLOBAL, true },
  2465. { "textureQueryLevels", TYPE_INT, { TYPE_ISAMPLER2D }, { "sampler" }, TAG_GLOBAL, true },
  2466. { "textureQueryLevels", TYPE_INT, { TYPE_USAMPLER2D }, { "sampler" }, TAG_GLOBAL, true },
  2467. { "textureQueryLevels", TYPE_INT, { TYPE_SAMPLER2DARRAY }, { "sampler" }, TAG_GLOBAL, true },
  2468. { "textureQueryLevels", TYPE_INT, { TYPE_ISAMPLER2DARRAY }, { "sampler" }, TAG_GLOBAL, true },
  2469. { "textureQueryLevels", TYPE_INT, { TYPE_USAMPLER2DARRAY }, { "sampler" }, TAG_GLOBAL, true },
  2470. { "textureQueryLevels", TYPE_INT, { TYPE_SAMPLER3D }, { "sampler" }, TAG_GLOBAL, true },
  2471. { "textureQueryLevels", TYPE_INT, { TYPE_ISAMPLER3D }, { "sampler" }, TAG_GLOBAL, true },
  2472. { "textureQueryLevels", TYPE_INT, { TYPE_USAMPLER3D }, { "sampler" }, TAG_GLOBAL, true },
  2473. { "textureQueryLevels", TYPE_INT, { TYPE_SAMPLERCUBE }, { "sampler" }, TAG_GLOBAL, true },
  2474. // dFdx
  2475. { "dFdx", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2476. { "dFdx", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2477. { "dFdx", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2478. { "dFdx", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2479. // dFdxCoarse
  2480. { "dFdxCoarse", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2481. { "dFdxCoarse", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2482. { "dFdxCoarse", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2483. { "dFdxCoarse", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2484. // dFdxFine
  2485. { "dFdxFine", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2486. { "dFdxFine", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2487. { "dFdxFine", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2488. { "dFdxFine", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2489. // dFdy
  2490. { "dFdy", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2491. { "dFdy", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2492. { "dFdy", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2493. { "dFdy", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2494. // dFdyCoarse
  2495. { "dFdyCoarse", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2496. { "dFdyCoarse", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2497. { "dFdyCoarse", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2498. { "dFdyCoarse", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2499. // dFdyFine
  2500. { "dFdyFine", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2501. { "dFdyFine", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2502. { "dFdyFine", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2503. { "dFdyFine", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2504. // fwidth
  2505. { "fwidth", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2506. { "fwidth", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2507. { "fwidth", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2508. { "fwidth", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, false },
  2509. // fwidthCoarse
  2510. { "fwidthCoarse", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2511. { "fwidthCoarse", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2512. { "fwidthCoarse", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2513. { "fwidthCoarse", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2514. // fwidthFine
  2515. { "fwidthFine", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2516. { "fwidthFine", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2517. { "fwidthFine", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2518. { "fwidthFine", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID }, { "p" }, TAG_GLOBAL, true },
  2519. // Sub-functions.
  2520. // array
  2521. { "length", TYPE_INT, { TYPE_VOID }, { "" }, TAG_ARRAY, false },
  2522. // Modern functions.
  2523. // fma
  2524. { "fma", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID }, { "a", "b", "c" }, TAG_GLOBAL, true },
  2525. { "fma", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID }, { "a", "b", "c" }, TAG_GLOBAL, true },
  2526. { "fma", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID }, { "a", "b", "c" }, TAG_GLOBAL, true },
  2527. { "fma", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID }, { "a", "b", "c" }, TAG_GLOBAL, true },
  2528. // Packing/Unpacking functions.
  2529. { "packHalf2x16", TYPE_UINT, { TYPE_VEC2, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2530. { "packUnorm2x16", TYPE_UINT, { TYPE_VEC2, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2531. { "packSnorm2x16", TYPE_UINT, { TYPE_VEC2, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2532. { "packUnorm4x8", TYPE_UINT, { TYPE_VEC4, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2533. { "packSnorm4x8", TYPE_UINT, { TYPE_VEC4, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2534. { "unpackHalf2x16", TYPE_VEC2, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2535. { "unpackUnorm2x16", TYPE_VEC2, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2536. { "unpackSnorm2x16", TYPE_VEC2, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2537. { "unpackUnorm4x8", TYPE_VEC4, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2538. { "unpackSnorm4x8", TYPE_VEC4, { TYPE_UINT, TYPE_VOID }, { "v" }, TAG_GLOBAL, false },
  2539. // bitfieldExtract
  2540. { "bitfieldExtract", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2541. { "bitfieldExtract", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2542. { "bitfieldExtract", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2543. { "bitfieldExtract", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2544. { "bitfieldExtract", TYPE_UINT, { TYPE_UINT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2545. { "bitfieldExtract", TYPE_UVEC2, { TYPE_UVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2546. { "bitfieldExtract", TYPE_UVEC3, { TYPE_UVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2547. { "bitfieldExtract", TYPE_UVEC4, { TYPE_UVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "value", "offset", "bits" }, TAG_GLOBAL, true },
  2548. // bitfieldInsert
  2549. { "bitfieldInsert", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2550. { "bitfieldInsert", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2551. { "bitfieldInsert", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2552. { "bitfieldInsert", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2553. { "bitfieldInsert", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2554. { "bitfieldInsert", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2555. { "bitfieldInsert", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2556. { "bitfieldInsert", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_INT, TYPE_INT, TYPE_VOID }, { "base", "insert", "offset", "bits" }, TAG_GLOBAL, true },
  2557. // bitfieldReverse
  2558. { "bitfieldReverse", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2559. { "bitfieldReverse", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2560. { "bitfieldReverse", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2561. { "bitfieldReverse", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2562. { "bitfieldReverse", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2563. { "bitfieldReverse", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2564. { "bitfieldReverse", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2565. { "bitfieldReverse", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2566. // bitCount
  2567. { "bitCount", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2568. { "bitCount", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2569. { "bitCount", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2570. { "bitCount", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2571. { "bitCount", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2572. { "bitCount", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2573. { "bitCount", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2574. { "bitCount", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2575. // findLSB
  2576. { "findLSB", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2577. { "findLSB", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2578. { "findLSB", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2579. { "findLSB", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2580. { "findLSB", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2581. { "findLSB", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2582. { "findLSB", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2583. { "findLSB", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2584. // findMSB
  2585. { "findMSB", TYPE_INT, { TYPE_INT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2586. { "findMSB", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2587. { "findMSB", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2588. { "findMSB", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2589. { "findMSB", TYPE_UINT, { TYPE_UINT, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2590. { "findMSB", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2591. { "findMSB", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2592. { "findMSB", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID }, { "value" }, TAG_GLOBAL, true },
  2593. // umulExtended
  2594. { "umulExtended", TYPE_VOID, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2595. { "umulExtended", TYPE_VOID, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2596. { "umulExtended", TYPE_VOID, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2597. { "umulExtended", TYPE_VOID, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2598. // imulExtended
  2599. { "imulExtended", TYPE_VOID, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2600. { "imulExtended", TYPE_VOID, { TYPE_IVEC2, TYPE_IVEC2, TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2601. { "imulExtended", TYPE_VOID, { TYPE_IVEC3, TYPE_IVEC3, TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2602. { "imulExtended", TYPE_VOID, { TYPE_IVEC4, TYPE_IVEC4, TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID }, { "x", "y", "msb", "lsb" }, TAG_GLOBAL, true },
  2603. // uaddCarry
  2604. { "uaddCarry", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "y", "carry" }, TAG_GLOBAL, true },
  2605. { "uaddCarry", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "x", "y", "carry" }, TAG_GLOBAL, true },
  2606. { "uaddCarry", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "x", "y", "carry" }, TAG_GLOBAL, true },
  2607. { "uaddCarry", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "x", "y", "carry" }, TAG_GLOBAL, true },
  2608. // usubBorrow
  2609. { "usubBorrow", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID }, { "x", "y", "borrow" }, TAG_GLOBAL, true },
  2610. { "usubBorrow", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID }, { "x", "y", "borrow" }, TAG_GLOBAL, true },
  2611. { "usubBorrow", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID }, { "x", "y", "borrow" }, TAG_GLOBAL, true },
  2612. { "usubBorrow", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID }, { "x", "y", "borrow" }, TAG_GLOBAL, true },
  2613. // ldexp
  2614. { "ldexp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_INT, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2615. { "ldexp", TYPE_VEC2, { TYPE_VEC2, TYPE_IVEC2, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2616. { "ldexp", TYPE_VEC3, { TYPE_VEC3, TYPE_IVEC3, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2617. { "ldexp", TYPE_VEC4, { TYPE_VEC4, TYPE_IVEC4, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2618. // frexp
  2619. { "frexp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_INT, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2620. { "frexp", TYPE_VEC2, { TYPE_VEC2, TYPE_IVEC2, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2621. { "frexp", TYPE_VEC3, { TYPE_VEC3, TYPE_IVEC3, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2622. { "frexp", TYPE_VEC4, { TYPE_VEC4, TYPE_IVEC4, TYPE_VOID }, { "x", "exp" }, TAG_GLOBAL, true },
  2623. { nullptr, TYPE_VOID, { TYPE_VOID }, { "" }, TAG_GLOBAL, false }
  2624. };
  2625. const ShaderLanguage::BuiltinFuncOutArgs ShaderLanguage::builtin_func_out_args[] = {
  2626. { "modf", { 1, -1 } },
  2627. { "umulExtended", { 2, 3 } },
  2628. { "imulExtended", { 2, 3 } },
  2629. { "uaddCarry", { 2, -1 } },
  2630. { "usubBorrow", { 2, -1 } },
  2631. { "ldexp", { 1, -1 } },
  2632. { "frexp", { 1, -1 } },
  2633. { nullptr, { 0, -1 } }
  2634. };
  2635. const ShaderLanguage::BuiltinFuncConstArgs ShaderLanguage::builtin_func_const_args[] = {
  2636. { "textureGather", 2, 0, 3 },
  2637. { nullptr, 0, 0, 0 }
  2638. };
  2639. bool ShaderLanguage::is_const_suffix_lut_initialized = false;
  2640. 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) {
  2641. ERR_FAIL_COND_V(p_func->op != OP_CALL && p_func->op != OP_CONSTRUCT, false);
  2642. Vector<DataType> args;
  2643. Vector<StringName> args2;
  2644. Vector<int> args3;
  2645. ERR_FAIL_COND_V(p_func->arguments[0]->type != Node::NODE_TYPE_VARIABLE, false);
  2646. StringName name = static_cast<VariableNode *>(p_func->arguments[0])->name.operator String();
  2647. for (int i = 1; i < p_func->arguments.size(); i++) {
  2648. args.push_back(p_func->arguments[i]->get_datatype());
  2649. args2.push_back(p_func->arguments[i]->get_datatype_name());
  2650. args3.push_back(p_func->arguments[i]->get_array_size());
  2651. }
  2652. int argcount = args.size();
  2653. if (p_function_info.stage_functions.has(name)) {
  2654. //stage based function
  2655. const StageFunctionInfo &sf = p_function_info.stage_functions[name];
  2656. if (argcount != sf.arguments.size()) {
  2657. _set_error(vformat(RTR("Invalid number of arguments when calling stage function '%s', which expects %d arguments."), String(name), sf.arguments.size()));
  2658. return false;
  2659. }
  2660. //validate arguments
  2661. for (int i = 0; i < argcount; i++) {
  2662. if (args[i] != sf.arguments[i].type) {
  2663. _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)));
  2664. return false;
  2665. }
  2666. }
  2667. if (r_ret_type) {
  2668. *r_ret_type = sf.return_type;
  2669. }
  2670. if (r_ret_type_str) {
  2671. *r_ret_type_str = "";
  2672. }
  2673. return true;
  2674. }
  2675. bool failed_builtin = false;
  2676. bool unsupported_builtin = false;
  2677. int builtin_idx = 0;
  2678. if (argcount <= 4) {
  2679. // test builtins
  2680. int idx = 0;
  2681. while (builtin_func_defs[idx].name) {
  2682. if (completion_class != builtin_func_defs[idx].tag) {
  2683. idx++;
  2684. continue;
  2685. }
  2686. if (name == builtin_func_defs[idx].name) {
  2687. failed_builtin = true;
  2688. bool fail = false;
  2689. for (int i = 0; i < argcount; i++) {
  2690. if (p_func->arguments[i + 1]->type == Node::NODE_TYPE_ARRAY) {
  2691. const ArrayNode *anode = static_cast<const ArrayNode *>(p_func->arguments[i + 1]);
  2692. if (anode->call_expression == nullptr && !anode->is_indexed()) {
  2693. fail = true;
  2694. break;
  2695. }
  2696. }
  2697. 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])) {
  2698. //all good, but needs implicit conversion later
  2699. } else if (args[i] != builtin_func_defs[idx].args[i]) {
  2700. fail = true;
  2701. break;
  2702. }
  2703. }
  2704. if (!fail) {
  2705. if (RenderingServer::get_singleton()->is_low_end()) {
  2706. if (builtin_func_defs[idx].high_end) {
  2707. fail = true;
  2708. unsupported_builtin = true;
  2709. builtin_idx = idx;
  2710. }
  2711. }
  2712. }
  2713. if (!fail && argcount < 4 && builtin_func_defs[idx].args[argcount] != TYPE_VOID) {
  2714. fail = true; //make sure the number of arguments matches
  2715. }
  2716. if (!fail) {
  2717. {
  2718. int constarg_idx = 0;
  2719. while (builtin_func_const_args[constarg_idx].name) {
  2720. if (String(name) == builtin_func_const_args[constarg_idx].name) {
  2721. int arg = builtin_func_const_args[constarg_idx].arg + 1;
  2722. if (p_func->arguments.size() <= arg) {
  2723. break;
  2724. }
  2725. int min = builtin_func_const_args[constarg_idx].min;
  2726. int max = builtin_func_const_args[constarg_idx].max;
  2727. bool error = false;
  2728. if (p_func->arguments[arg]->type == Node::NODE_TYPE_VARIABLE) {
  2729. const VariableNode *vn = static_cast<VariableNode *>(p_func->arguments[arg]);
  2730. bool is_const = false;
  2731. ConstantNode::Value value;
  2732. value.sint = -1;
  2733. _find_identifier(p_block, false, p_function_info, vn->name, nullptr, nullptr, &is_const, nullptr, nullptr, &value);
  2734. if (!is_const || value.sint < min || value.sint > max) {
  2735. error = true;
  2736. }
  2737. } else {
  2738. if (p_func->arguments[arg]->type == Node::NODE_TYPE_CONSTANT) {
  2739. const ConstantNode *cn = static_cast<ConstantNode *>(p_func->arguments[arg]);
  2740. if (cn->get_datatype() == TYPE_INT && cn->values.size() == 1) {
  2741. int value = cn->values[0].sint;
  2742. if (value < min || value > max) {
  2743. error = true;
  2744. }
  2745. } else {
  2746. error = true;
  2747. }
  2748. } else {
  2749. error = true;
  2750. }
  2751. }
  2752. if (error) {
  2753. _set_error(vformat(RTR("Expected integer constant within [%d..%d] range."), min, max));
  2754. return false;
  2755. }
  2756. }
  2757. constarg_idx++;
  2758. }
  2759. }
  2760. //make sure its not an out argument used in the wrong way
  2761. int outarg_idx = 0;
  2762. while (builtin_func_out_args[outarg_idx].name) {
  2763. if (String(name) == builtin_func_out_args[outarg_idx].name) {
  2764. for (int arg = 0; arg < BuiltinFuncOutArgs::MAX_ARGS; arg++) {
  2765. int arg_idx = builtin_func_out_args[outarg_idx].arguments[arg];
  2766. if (arg_idx == -1) {
  2767. break;
  2768. }
  2769. if (arg_idx < argcount) {
  2770. 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) {
  2771. _set_error(vformat(RTR("Argument %d of function '%s' is not a variable, array, or member."), arg_idx + 1, String(name)));
  2772. return false;
  2773. }
  2774. if (p_func->arguments[arg_idx + 1]->type == Node::NODE_TYPE_ARRAY) {
  2775. ArrayNode *mn = static_cast<ArrayNode *>(p_func->arguments[arg_idx + 1]);
  2776. if (mn->is_const) {
  2777. fail = true;
  2778. }
  2779. } else if (p_func->arguments[arg_idx + 1]->type == Node::NODE_TYPE_MEMBER) {
  2780. MemberNode *mn = static_cast<MemberNode *>(p_func->arguments[arg_idx + 1]);
  2781. if (mn->basetype_const) {
  2782. fail = true;
  2783. }
  2784. } else { // TYPE_VARIABLE
  2785. VariableNode *vn = static_cast<VariableNode *>(p_func->arguments[arg_idx + 1]);
  2786. if (vn->is_const) {
  2787. fail = true;
  2788. } else {
  2789. StringName varname = vn->name;
  2790. if (shader->uniforms.has(varname)) {
  2791. fail = true;
  2792. } else {
  2793. if (shader->varyings.has(varname)) {
  2794. _set_error(vformat(RTR("Varyings cannot be passed for the '%s' parameter."), "out"));
  2795. return false;
  2796. }
  2797. if (p_function_info.built_ins.has(varname)) {
  2798. BuiltInInfo info = p_function_info.built_ins[varname];
  2799. if (info.constant) {
  2800. fail = true;
  2801. }
  2802. }
  2803. }
  2804. }
  2805. }
  2806. if (fail) {
  2807. _set_error(vformat(RTR("A constant value cannot be passed for the '%s' parameter."), "out"));
  2808. return false;
  2809. }
  2810. StringName var_name;
  2811. if (p_func->arguments[arg_idx + 1]->type == Node::NODE_TYPE_ARRAY) {
  2812. var_name = static_cast<const ArrayNode *>(p_func->arguments[arg_idx + 1])->name;
  2813. } else if (p_func->arguments[arg_idx + 1]->type == Node::NODE_TYPE_MEMBER) {
  2814. Node *n = static_cast<const MemberNode *>(p_func->arguments[arg_idx + 1])->owner;
  2815. while (n->type == Node::NODE_TYPE_MEMBER) {
  2816. n = static_cast<const MemberNode *>(n)->owner;
  2817. }
  2818. if (n->type != Node::NODE_TYPE_VARIABLE && n->type != Node::NODE_TYPE_ARRAY) {
  2819. _set_error(vformat(RTR("Argument %d of function '%s' is not a variable, array, or member."), arg_idx + 1, String(name)));
  2820. return false;
  2821. }
  2822. if (n->type == Node::NODE_TYPE_VARIABLE) {
  2823. var_name = static_cast<const VariableNode *>(n)->name;
  2824. } else { // TYPE_ARRAY
  2825. var_name = static_cast<const ArrayNode *>(n)->name;
  2826. }
  2827. } else { // TYPE_VARIABLE
  2828. var_name = static_cast<const VariableNode *>(p_func->arguments[arg_idx + 1])->name;
  2829. }
  2830. const BlockNode *b = p_block;
  2831. bool valid = false;
  2832. while (b) {
  2833. if (b->variables.has(var_name) || p_function_info.built_ins.has(var_name)) {
  2834. valid = true;
  2835. break;
  2836. }
  2837. if (b->parent_function) {
  2838. for (int i = 0; i < b->parent_function->arguments.size(); i++) {
  2839. if (b->parent_function->arguments[i].name == var_name) {
  2840. valid = true;
  2841. break;
  2842. }
  2843. }
  2844. }
  2845. b = b->parent_block;
  2846. }
  2847. if (!valid) {
  2848. _set_error(vformat(RTR("Argument %d of function '%s' can only take a local variable, array, or member."), arg_idx + 1, String(name)));
  2849. return false;
  2850. }
  2851. }
  2852. }
  2853. }
  2854. outarg_idx++;
  2855. }
  2856. //implicitly convert values if possible
  2857. for (int i = 0; i < argcount; i++) {
  2858. 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) {
  2859. //can't do implicit conversion here
  2860. continue;
  2861. }
  2862. //this is an implicit conversion
  2863. ConstantNode *constant = static_cast<ConstantNode *>(p_func->arguments[i + 1]);
  2864. ConstantNode *conversion = alloc_node<ConstantNode>();
  2865. conversion->datatype = builtin_func_defs[idx].args[i];
  2866. conversion->values.resize(1);
  2867. convert_constant(constant, builtin_func_defs[idx].args[i], conversion->values.ptrw());
  2868. p_func->arguments.write[i + 1] = conversion;
  2869. }
  2870. if (r_ret_type) {
  2871. *r_ret_type = builtin_func_defs[idx].rettype;
  2872. }
  2873. return true;
  2874. }
  2875. }
  2876. idx++;
  2877. }
  2878. }
  2879. if (unsupported_builtin) {
  2880. String arglist = "";
  2881. for (int i = 0; i < argcount; i++) {
  2882. if (i > 0) {
  2883. arglist += ", ";
  2884. }
  2885. arglist += get_datatype_name(builtin_func_defs[builtin_idx].args[i]);
  2886. }
  2887. _set_error(vformat(RTR("Built-in function \"%s(%s)\" is only supported on high-end platforms."), String(name), arglist));
  2888. return false;
  2889. }
  2890. if (failed_builtin) {
  2891. String arg_list;
  2892. for (int i = 0; i < argcount; i++) {
  2893. if (i > 0) {
  2894. arg_list += ",";
  2895. }
  2896. String arg_name;
  2897. if (args[i] == TYPE_STRUCT) {
  2898. arg_name = args2[i];
  2899. } else {
  2900. arg_name = get_datatype_name(args[i]);
  2901. }
  2902. if (args3[i] > 0) {
  2903. arg_name += "[";
  2904. arg_name += itos(args3[i]);
  2905. arg_name += "]";
  2906. }
  2907. arg_list += arg_name;
  2908. }
  2909. _set_error(vformat(RTR("Invalid arguments for the built-in function: \"%s(%s)\"."), String(name), arg_list));
  2910. return false;
  2911. }
  2912. // try existing functions..
  2913. StringName exclude_function;
  2914. BlockNode *block = p_block;
  2915. while (block) {
  2916. if (block->parent_function) {
  2917. exclude_function = block->parent_function->name;
  2918. }
  2919. block = block->parent_block;
  2920. }
  2921. if (name == exclude_function) {
  2922. _set_error(RTR("Recursion is not allowed."));
  2923. return false;
  2924. }
  2925. int last_arg_count = 0;
  2926. String arg_list = "";
  2927. for (int i = 0; i < shader->functions.size(); i++) {
  2928. if (name != shader->functions[i].name) {
  2929. continue;
  2930. }
  2931. if (!shader->functions[i].callable) {
  2932. _set_error(vformat(RTR("Function '%s' can't be called from source code."), String(name)));
  2933. return false;
  2934. }
  2935. FunctionNode *pfunc = shader->functions[i].function;
  2936. if (arg_list.is_empty()) {
  2937. for (int j = 0; j < pfunc->arguments.size(); j++) {
  2938. if (j > 0) {
  2939. arg_list += ", ";
  2940. }
  2941. String func_arg_name;
  2942. if (pfunc->arguments[j].type == TYPE_STRUCT) {
  2943. func_arg_name = pfunc->arguments[j].type_str;
  2944. } else {
  2945. func_arg_name = get_datatype_name(pfunc->arguments[j].type);
  2946. }
  2947. if (pfunc->arguments[j].array_size > 0) {
  2948. func_arg_name += "[";
  2949. func_arg_name += itos(pfunc->arguments[j].array_size);
  2950. func_arg_name += "]";
  2951. }
  2952. arg_list += func_arg_name;
  2953. }
  2954. }
  2955. if (pfunc->arguments.size() != args.size()) {
  2956. last_arg_count = pfunc->arguments.size();
  2957. continue;
  2958. }
  2959. bool fail = false;
  2960. for (int j = 0; j < args.size(); j++) {
  2961. 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)) {
  2962. //all good, but it needs implicit conversion later
  2963. } else if (args[j] != pfunc->arguments[j].type || (args[j] == TYPE_STRUCT && args2[j] != pfunc->arguments[j].type_str) || args3[j] != pfunc->arguments[j].array_size) {
  2964. String func_arg_name;
  2965. if (pfunc->arguments[j].type == TYPE_STRUCT) {
  2966. func_arg_name = pfunc->arguments[j].type_str;
  2967. } else {
  2968. func_arg_name = get_datatype_name(pfunc->arguments[j].type);
  2969. }
  2970. if (pfunc->arguments[j].array_size > 0) {
  2971. func_arg_name += "[";
  2972. func_arg_name += itos(pfunc->arguments[j].array_size);
  2973. func_arg_name += "]";
  2974. }
  2975. String arg_name;
  2976. if (args[j] == TYPE_STRUCT) {
  2977. arg_name = args2[j];
  2978. } else {
  2979. arg_name = get_datatype_name(args[j]);
  2980. }
  2981. if (args3[j] > 0) {
  2982. arg_name += "[";
  2983. arg_name += itos(args3[j]);
  2984. arg_name += "]";
  2985. }
  2986. _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));
  2987. fail = true;
  2988. break;
  2989. }
  2990. }
  2991. if (!fail) {
  2992. //implicitly convert values if possible
  2993. for (int k = 0; k < args.size(); k++) {
  2994. if (get_scalar_type(args[k]) != args[k] || args[k] == pfunc->arguments[k].type || p_func->arguments[k + 1]->type != Node::NODE_TYPE_CONSTANT) {
  2995. //can't do implicit conversion here
  2996. continue;
  2997. }
  2998. //this is an implicit conversion
  2999. ConstantNode *constant = static_cast<ConstantNode *>(p_func->arguments[k + 1]);
  3000. ConstantNode *conversion = alloc_node<ConstantNode>();
  3001. conversion->datatype = pfunc->arguments[k].type;
  3002. conversion->values.resize(1);
  3003. convert_constant(constant, pfunc->arguments[k].type, conversion->values.ptrw());
  3004. p_func->arguments.write[k + 1] = conversion;
  3005. }
  3006. if (r_ret_type) {
  3007. *r_ret_type = pfunc->return_type;
  3008. if (pfunc->return_type == TYPE_STRUCT) {
  3009. *r_ret_type_str = pfunc->return_struct_name;
  3010. }
  3011. }
  3012. if (r_is_custom_function) {
  3013. *r_is_custom_function = true;
  3014. }
  3015. return true;
  3016. }
  3017. }
  3018. if (last_arg_count > args.size()) {
  3019. _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()));
  3020. } else if (last_arg_count < args.size()) {
  3021. _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()));
  3022. }
  3023. return false;
  3024. }
  3025. 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) {
  3026. bool result = true;
  3027. if (p_datatype_a == TYPE_STRUCT || p_datatype_b == TYPE_STRUCT) {
  3028. if (p_datatype_name_a != p_datatype_name_b) {
  3029. result = false;
  3030. }
  3031. } else {
  3032. if (p_datatype_a != p_datatype_b) {
  3033. result = false;
  3034. }
  3035. }
  3036. if (p_array_size_a != p_array_size_b) {
  3037. result = false;
  3038. }
  3039. if (!result) {
  3040. String type_name = p_datatype_a == TYPE_STRUCT ? p_datatype_name_a : get_datatype_name(p_datatype_a);
  3041. if (p_array_size_a > 0) {
  3042. type_name += "[";
  3043. type_name += itos(p_array_size_a);
  3044. type_name += "]";
  3045. }
  3046. String type_name2 = p_datatype_b == TYPE_STRUCT ? p_datatype_name_b : get_datatype_name(p_datatype_b);
  3047. if (p_array_size_b > 0) {
  3048. type_name2 += "[";
  3049. type_name2 += itos(p_array_size_b);
  3050. type_name2 += "]";
  3051. }
  3052. _set_error(vformat(RTR("Invalid assignment of '%s' to '%s'."), type_name2, type_name));
  3053. }
  3054. return result;
  3055. }
  3056. bool ShaderLanguage::_compare_datatypes_in_nodes(Node *a, Node *b) {
  3057. 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());
  3058. }
  3059. bool ShaderLanguage::_parse_function_arguments(BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_func, int *r_complete_arg) {
  3060. TkPos pos = _get_tkpos();
  3061. Token tk = _get_token();
  3062. if (tk.type == TK_PARENTHESIS_CLOSE) {
  3063. return true;
  3064. }
  3065. _set_tkpos(pos);
  3066. while (true) {
  3067. if (r_complete_arg) {
  3068. pos = _get_tkpos();
  3069. tk = _get_token();
  3070. if (tk.type == TK_CURSOR) {
  3071. *r_complete_arg = p_func->arguments.size() - 1;
  3072. } else {
  3073. _set_tkpos(pos);
  3074. }
  3075. }
  3076. Node *arg = _parse_and_reduce_expression(p_block, p_function_info);
  3077. if (!arg) {
  3078. return false;
  3079. }
  3080. if (is_const_decl && arg->type == Node::NODE_TYPE_VARIABLE) {
  3081. const VariableNode *var = static_cast<const VariableNode *>(arg);
  3082. if (!var->is_const) {
  3083. _set_error(RTR("Expected constant expression."));
  3084. return false;
  3085. }
  3086. }
  3087. p_func->arguments.push_back(arg);
  3088. tk = _get_token();
  3089. if (tk.type == TK_PARENTHESIS_CLOSE) {
  3090. return true;
  3091. } else if (tk.type != TK_COMMA) {
  3092. // something is broken
  3093. _set_error(RTR("Expected ',' or ')' after argument."));
  3094. return false;
  3095. }
  3096. }
  3097. return true;
  3098. }
  3099. bool ShaderLanguage::is_token_operator(TokenType p_type) {
  3100. return (p_type == TK_OP_EQUAL ||
  3101. p_type == TK_OP_NOT_EQUAL ||
  3102. p_type == TK_OP_LESS ||
  3103. p_type == TK_OP_LESS_EQUAL ||
  3104. p_type == TK_OP_GREATER ||
  3105. p_type == TK_OP_GREATER_EQUAL ||
  3106. p_type == TK_OP_AND ||
  3107. p_type == TK_OP_OR ||
  3108. p_type == TK_OP_NOT ||
  3109. p_type == TK_OP_ADD ||
  3110. p_type == TK_OP_SUB ||
  3111. p_type == TK_OP_MUL ||
  3112. p_type == TK_OP_DIV ||
  3113. p_type == TK_OP_MOD ||
  3114. p_type == TK_OP_SHIFT_LEFT ||
  3115. p_type == TK_OP_SHIFT_RIGHT ||
  3116. p_type == TK_OP_ASSIGN ||
  3117. p_type == TK_OP_ASSIGN_ADD ||
  3118. p_type == TK_OP_ASSIGN_SUB ||
  3119. p_type == TK_OP_ASSIGN_MUL ||
  3120. p_type == TK_OP_ASSIGN_DIV ||
  3121. p_type == TK_OP_ASSIGN_MOD ||
  3122. p_type == TK_OP_ASSIGN_SHIFT_LEFT ||
  3123. p_type == TK_OP_ASSIGN_SHIFT_RIGHT ||
  3124. p_type == TK_OP_ASSIGN_BIT_AND ||
  3125. p_type == TK_OP_ASSIGN_BIT_OR ||
  3126. p_type == TK_OP_ASSIGN_BIT_XOR ||
  3127. p_type == TK_OP_BIT_AND ||
  3128. p_type == TK_OP_BIT_OR ||
  3129. p_type == TK_OP_BIT_XOR ||
  3130. p_type == TK_OP_BIT_INVERT ||
  3131. p_type == TK_OP_INCREMENT ||
  3132. p_type == TK_OP_DECREMENT ||
  3133. p_type == TK_QUESTION ||
  3134. p_type == TK_COLON);
  3135. }
  3136. bool ShaderLanguage::is_token_operator_assign(TokenType p_type) {
  3137. return (p_type == TK_OP_ASSIGN ||
  3138. p_type == TK_OP_ASSIGN_ADD ||
  3139. p_type == TK_OP_ASSIGN_SUB ||
  3140. p_type == TK_OP_ASSIGN_MUL ||
  3141. p_type == TK_OP_ASSIGN_DIV ||
  3142. p_type == TK_OP_ASSIGN_MOD ||
  3143. p_type == TK_OP_ASSIGN_SHIFT_LEFT ||
  3144. p_type == TK_OP_ASSIGN_SHIFT_RIGHT ||
  3145. p_type == TK_OP_ASSIGN_BIT_AND ||
  3146. p_type == TK_OP_ASSIGN_BIT_OR ||
  3147. p_type == TK_OP_ASSIGN_BIT_XOR);
  3148. }
  3149. bool ShaderLanguage::is_token_hint(TokenType p_type) {
  3150. return int(p_type) > int(TK_RENDER_MODE) && int(p_type) < int(TK_SHADER_TYPE);
  3151. }
  3152. bool ShaderLanguage::convert_constant(ConstantNode *p_constant, DataType p_to_type, ConstantNode::Value *p_value) {
  3153. if (p_constant->datatype == p_to_type) {
  3154. if (p_value) {
  3155. for (int i = 0; i < p_constant->values.size(); i++) {
  3156. p_value[i] = p_constant->values[i];
  3157. }
  3158. }
  3159. return true;
  3160. } else if (p_constant->datatype == TYPE_INT && p_to_type == TYPE_FLOAT) {
  3161. if (p_value) {
  3162. p_value->real = p_constant->values[0].sint;
  3163. }
  3164. return true;
  3165. } else if (p_constant->datatype == TYPE_UINT && p_to_type == TYPE_FLOAT) {
  3166. if (p_value) {
  3167. p_value->real = p_constant->values[0].uint;
  3168. }
  3169. return true;
  3170. } else if (p_constant->datatype == TYPE_INT && p_to_type == TYPE_UINT) {
  3171. if (p_constant->values[0].sint < 0) {
  3172. return false;
  3173. }
  3174. if (p_value) {
  3175. p_value->uint = p_constant->values[0].sint;
  3176. }
  3177. return true;
  3178. } else if (p_constant->datatype == TYPE_UINT && p_to_type == TYPE_INT) {
  3179. if (p_constant->values[0].uint > 0x7FFFFFFF) {
  3180. return false;
  3181. }
  3182. if (p_value) {
  3183. p_value->sint = p_constant->values[0].uint;
  3184. }
  3185. return true;
  3186. } else {
  3187. return false;
  3188. }
  3189. }
  3190. bool ShaderLanguage::is_scalar_type(DataType p_type) {
  3191. return p_type == TYPE_BOOL || p_type == TYPE_INT || p_type == TYPE_UINT || p_type == TYPE_FLOAT;
  3192. }
  3193. bool ShaderLanguage::is_float_type(DataType p_type) {
  3194. switch (p_type) {
  3195. case TYPE_FLOAT:
  3196. case TYPE_VEC2:
  3197. case TYPE_VEC3:
  3198. case TYPE_VEC4:
  3199. case TYPE_MAT2:
  3200. case TYPE_MAT3:
  3201. case TYPE_MAT4:
  3202. case TYPE_SAMPLER2D:
  3203. case TYPE_SAMPLER2DARRAY:
  3204. case TYPE_SAMPLER3D:
  3205. case TYPE_SAMPLERCUBE:
  3206. case TYPE_SAMPLERCUBEARRAY: {
  3207. return true;
  3208. }
  3209. default: {
  3210. return false;
  3211. }
  3212. }
  3213. }
  3214. bool ShaderLanguage::is_sampler_type(DataType p_type) {
  3215. return p_type > TYPE_MAT4 && p_type < TYPE_STRUCT;
  3216. }
  3217. Variant ShaderLanguage::constant_value_to_variant(const Vector<ShaderLanguage::ConstantNode::Value> &p_value, DataType p_type, int p_array_size, ShaderLanguage::ShaderNode::Uniform::Hint p_hint) {
  3218. int array_size = p_array_size;
  3219. if (p_value.size() > 0) {
  3220. Variant value;
  3221. switch (p_type) {
  3222. case ShaderLanguage::TYPE_BOOL:
  3223. if (array_size > 0) {
  3224. PackedInt32Array array;
  3225. for (int i = 0; i < array_size; i++) {
  3226. array.push_back(p_value[i].boolean);
  3227. }
  3228. value = Variant(array);
  3229. } else {
  3230. value = Variant(p_value[0].boolean);
  3231. }
  3232. break;
  3233. case ShaderLanguage::TYPE_BVEC2:
  3234. array_size *= 2;
  3235. if (array_size > 0) {
  3236. PackedInt32Array array;
  3237. for (int i = 0; i < array_size; i++) {
  3238. array.push_back(p_value[i].boolean);
  3239. }
  3240. value = Variant(array);
  3241. } else {
  3242. value = Variant(p_value[0].boolean);
  3243. }
  3244. break;
  3245. case ShaderLanguage::TYPE_BVEC3:
  3246. array_size *= 3;
  3247. if (array_size > 0) {
  3248. PackedInt32Array array;
  3249. for (int i = 0; i < array_size; i++) {
  3250. array.push_back(p_value[i].boolean);
  3251. }
  3252. value = Variant(array);
  3253. } else {
  3254. value = Variant(p_value[0].boolean);
  3255. }
  3256. break;
  3257. case ShaderLanguage::TYPE_BVEC4:
  3258. array_size *= 4;
  3259. if (array_size > 0) {
  3260. PackedInt32Array array;
  3261. for (int i = 0; i < array_size; i++) {
  3262. array.push_back(p_value[i].boolean);
  3263. }
  3264. value = Variant(array);
  3265. } else {
  3266. value = Variant(p_value[0].boolean);
  3267. }
  3268. break;
  3269. case ShaderLanguage::TYPE_INT:
  3270. if (array_size > 0) {
  3271. PackedInt32Array array;
  3272. for (int i = 0; i < array_size; i++) {
  3273. array.push_back(p_value[i].sint);
  3274. }
  3275. value = Variant(array);
  3276. } else {
  3277. value = Variant(p_value[0].sint);
  3278. }
  3279. break;
  3280. case ShaderLanguage::TYPE_IVEC2:
  3281. if (array_size > 0) {
  3282. array_size *= 2;
  3283. PackedInt32Array array;
  3284. for (int i = 0; i < array_size; i++) {
  3285. array.push_back(p_value[i].sint);
  3286. }
  3287. value = Variant(array);
  3288. } else {
  3289. value = Variant(Vector2(p_value[0].sint, p_value[1].sint));
  3290. }
  3291. break;
  3292. case ShaderLanguage::TYPE_IVEC3:
  3293. if (array_size > 0) {
  3294. array_size *= 3;
  3295. PackedInt32Array array;
  3296. for (int i = 0; i < array_size; i++) {
  3297. array.push_back(p_value[i].sint);
  3298. }
  3299. value = Variant(array);
  3300. } else {
  3301. value = Variant(Vector3(p_value[0].sint, p_value[1].sint, p_value[2].sint));
  3302. }
  3303. break;
  3304. case ShaderLanguage::TYPE_IVEC4:
  3305. if (array_size > 0) {
  3306. array_size *= 4;
  3307. PackedInt32Array array;
  3308. for (int i = 0; i < array_size; i++) {
  3309. array.push_back(p_value[i].sint);
  3310. }
  3311. value = Variant(array);
  3312. } else {
  3313. value = Variant(Quaternion(p_value[0].sint, p_value[1].sint, p_value[2].sint, p_value[3].sint));
  3314. }
  3315. break;
  3316. case ShaderLanguage::TYPE_UINT:
  3317. if (array_size > 0) {
  3318. PackedInt32Array array;
  3319. for (int i = 0; i < array_size; i++) {
  3320. array.push_back(p_value[i].uint);
  3321. }
  3322. value = Variant(array);
  3323. } else {
  3324. value = Variant(p_value[0].uint);
  3325. }
  3326. break;
  3327. case ShaderLanguage::TYPE_UVEC2:
  3328. if (array_size > 0) {
  3329. array_size *= 2;
  3330. PackedInt32Array array;
  3331. for (int i = 0; i < array_size; i++) {
  3332. array.push_back(p_value[i].uint);
  3333. }
  3334. value = Variant(array);
  3335. } else {
  3336. value = Variant(Vector2(p_value[0].uint, p_value[1].uint));
  3337. }
  3338. break;
  3339. case ShaderLanguage::TYPE_UVEC3:
  3340. if (array_size > 0) {
  3341. array_size *= 3;
  3342. PackedInt32Array array;
  3343. for (int i = 0; i < array_size; i++) {
  3344. array.push_back(p_value[i].uint);
  3345. }
  3346. value = Variant(array);
  3347. } else {
  3348. value = Variant(Vector3(p_value[0].uint, p_value[1].uint, p_value[2].uint));
  3349. }
  3350. break;
  3351. case ShaderLanguage::TYPE_UVEC4:
  3352. if (array_size > 0) {
  3353. array_size *= 4;
  3354. PackedInt32Array array;
  3355. for (int i = 0; i < array_size; i++) {
  3356. array.push_back(p_value[i].uint);
  3357. }
  3358. value = Variant(array);
  3359. } else {
  3360. value = Variant(Quaternion(p_value[0].uint, p_value[1].uint, p_value[2].uint, p_value[3].uint));
  3361. }
  3362. break;
  3363. case ShaderLanguage::TYPE_FLOAT:
  3364. if (array_size > 0) {
  3365. PackedFloat32Array array;
  3366. for (int i = 0; i < array_size; i++) {
  3367. array.push_back(p_value[i].real);
  3368. }
  3369. value = Variant(array);
  3370. } else {
  3371. value = Variant(p_value[0].real);
  3372. }
  3373. break;
  3374. case ShaderLanguage::TYPE_VEC2:
  3375. if (array_size > 0) {
  3376. array_size *= 2;
  3377. PackedVector2Array array;
  3378. for (int i = 0; i < array_size; i += 2) {
  3379. array.push_back(Vector2(p_value[i].real, p_value[i + 1].real));
  3380. }
  3381. value = Variant(array);
  3382. } else {
  3383. value = Variant(Vector2(p_value[0].real, p_value[1].real));
  3384. }
  3385. break;
  3386. case ShaderLanguage::TYPE_VEC3:
  3387. if (array_size > 0) {
  3388. array_size *= 3;
  3389. if (p_hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3390. PackedColorArray array;
  3391. for (int i = 0; i < array_size; i += 3) {
  3392. array.push_back(Color(p_value[i].real, p_value[i + 1].real, p_value[i + 2].real));
  3393. }
  3394. value = Variant(array);
  3395. } else {
  3396. PackedVector3Array array;
  3397. for (int i = 0; i < array_size; i += 3) {
  3398. array.push_back(Vector3(p_value[i].real, p_value[i + 1].real, p_value[i + 2].real));
  3399. }
  3400. value = Variant(array);
  3401. }
  3402. } else {
  3403. if (p_hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3404. value = Variant(Color(p_value[0].real, p_value[1].real, p_value[2].real));
  3405. } else {
  3406. value = Variant(Vector3(p_value[0].real, p_value[1].real, p_value[2].real));
  3407. }
  3408. }
  3409. break;
  3410. case ShaderLanguage::TYPE_VEC4:
  3411. if (array_size > 0) {
  3412. array_size *= 4;
  3413. if (p_hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3414. PackedColorArray array;
  3415. for (int i = 0; i < array_size; i += 4) {
  3416. array.push_back(Color(p_value[i].real, p_value[i + 1].real, p_value[i + 2].real, p_value[i + 3].real));
  3417. }
  3418. value = Variant(array);
  3419. } else {
  3420. PackedFloat32Array array;
  3421. for (int i = 0; i < array_size; i += 4) {
  3422. array.push_back(p_value[i].real);
  3423. array.push_back(p_value[i + 1].real);
  3424. array.push_back(p_value[i + 2].real);
  3425. array.push_back(p_value[i + 3].real);
  3426. }
  3427. value = Variant(array);
  3428. }
  3429. } else {
  3430. if (p_hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3431. value = Variant(Color(p_value[0].real, p_value[1].real, p_value[2].real, p_value[3].real));
  3432. } else {
  3433. value = Variant(Quaternion(p_value[0].real, p_value[1].real, p_value[2].real, p_value[3].real));
  3434. }
  3435. }
  3436. break;
  3437. case ShaderLanguage::TYPE_MAT2:
  3438. if (array_size > 0) {
  3439. array_size *= 4;
  3440. PackedFloat32Array array;
  3441. for (int i = 0; i < array_size; i += 4) {
  3442. array.push_back(p_value[i].real);
  3443. array.push_back(p_value[i + 1].real);
  3444. array.push_back(p_value[i + 2].real);
  3445. array.push_back(p_value[i + 3].real);
  3446. }
  3447. value = Variant(array);
  3448. } else {
  3449. value = Variant(Transform2D(p_value[0].real, p_value[2].real, p_value[1].real, p_value[3].real, 0.0, 0.0));
  3450. }
  3451. break;
  3452. case ShaderLanguage::TYPE_MAT3: {
  3453. if (array_size > 0) {
  3454. array_size *= 9;
  3455. PackedFloat32Array array;
  3456. for (int i = 0; i < array_size; i += 9) {
  3457. for (int j = 0; j < 9; j++) {
  3458. array.push_back(p_value[i + j].real);
  3459. }
  3460. }
  3461. value = Variant(array);
  3462. } else {
  3463. Basis p;
  3464. p[0][0] = p_value[0].real;
  3465. p[0][1] = p_value[1].real;
  3466. p[0][2] = p_value[2].real;
  3467. p[1][0] = p_value[3].real;
  3468. p[1][1] = p_value[4].real;
  3469. p[1][2] = p_value[5].real;
  3470. p[2][0] = p_value[6].real;
  3471. p[2][1] = p_value[7].real;
  3472. p[2][2] = p_value[8].real;
  3473. value = Variant(p);
  3474. }
  3475. break;
  3476. }
  3477. case ShaderLanguage::TYPE_MAT4: {
  3478. if (array_size > 0) {
  3479. array_size *= 16;
  3480. PackedFloat32Array array;
  3481. for (int i = 0; i < array_size; i += 16) {
  3482. for (int j = 0; j < 16; j++) {
  3483. array.push_back(p_value[i + j].real);
  3484. }
  3485. }
  3486. value = Variant(array);
  3487. } else {
  3488. Projection p = Projection(Vector4(p_value[0].real, p_value[1].real, p_value[2].real, p_value[3].real),
  3489. Vector4(p_value[4].real, p_value[5].real, p_value[6].real, p_value[7].real),
  3490. Vector4(p_value[8].real, p_value[9].real, p_value[10].real, p_value[11].real),
  3491. Vector4(p_value[12].real, p_value[13].real, p_value[14].real, p_value[15].real));
  3492. value = Variant(p);
  3493. }
  3494. break;
  3495. }
  3496. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  3497. case ShaderLanguage::TYPE_ISAMPLER2D:
  3498. case ShaderLanguage::TYPE_ISAMPLER3D:
  3499. case ShaderLanguage::TYPE_SAMPLER2DARRAY:
  3500. case ShaderLanguage::TYPE_SAMPLER2D:
  3501. case ShaderLanguage::TYPE_SAMPLER3D:
  3502. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  3503. case ShaderLanguage::TYPE_USAMPLER2D:
  3504. case ShaderLanguage::TYPE_USAMPLER3D:
  3505. case ShaderLanguage::TYPE_SAMPLERCUBE:
  3506. case ShaderLanguage::TYPE_SAMPLERCUBEARRAY: {
  3507. // Texture types, likely not relevant here.
  3508. break;
  3509. }
  3510. case ShaderLanguage::TYPE_STRUCT:
  3511. break;
  3512. case ShaderLanguage::TYPE_VOID:
  3513. break;
  3514. case ShaderLanguage::TYPE_MAX:
  3515. break;
  3516. }
  3517. return value;
  3518. }
  3519. return Variant();
  3520. }
  3521. PropertyInfo ShaderLanguage::uniform_to_property_info(const ShaderNode::Uniform &p_uniform) {
  3522. PropertyInfo pi;
  3523. switch (p_uniform.type) {
  3524. case ShaderLanguage::TYPE_VOID:
  3525. pi.type = Variant::NIL;
  3526. break;
  3527. case ShaderLanguage::TYPE_BOOL:
  3528. if (p_uniform.array_size > 0) {
  3529. pi.type = Variant::PACKED_INT32_ARRAY;
  3530. } else {
  3531. pi.type = Variant::BOOL;
  3532. }
  3533. break;
  3534. case ShaderLanguage::TYPE_BVEC2:
  3535. if (p_uniform.array_size > 0) {
  3536. pi.type = Variant::PACKED_INT32_ARRAY;
  3537. } else {
  3538. pi.type = Variant::INT;
  3539. pi.hint = PROPERTY_HINT_FLAGS;
  3540. pi.hint_string = "x,y";
  3541. }
  3542. break;
  3543. case ShaderLanguage::TYPE_BVEC3:
  3544. if (p_uniform.array_size > 0) {
  3545. pi.type = Variant::PACKED_INT32_ARRAY;
  3546. } else {
  3547. pi.type = Variant::INT;
  3548. pi.hint = PROPERTY_HINT_FLAGS;
  3549. pi.hint_string = "x,y,z";
  3550. }
  3551. break;
  3552. case ShaderLanguage::TYPE_BVEC4:
  3553. if (p_uniform.array_size > 0) {
  3554. pi.type = Variant::PACKED_INT32_ARRAY;
  3555. } else {
  3556. pi.type = Variant::INT;
  3557. pi.hint = PROPERTY_HINT_FLAGS;
  3558. pi.hint_string = "x,y,z,w";
  3559. }
  3560. break;
  3561. case ShaderLanguage::TYPE_UINT:
  3562. case ShaderLanguage::TYPE_INT: {
  3563. if (p_uniform.array_size > 0) {
  3564. pi.type = Variant::PACKED_INT32_ARRAY;
  3565. } else {
  3566. pi.type = Variant::INT;
  3567. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_RANGE) {
  3568. pi.hint = PROPERTY_HINT_RANGE;
  3569. pi.hint_string = rtos(p_uniform.hint_range[0]) + "," + rtos(p_uniform.hint_range[1]) + "," + rtos(p_uniform.hint_range[2]);
  3570. }
  3571. }
  3572. } break;
  3573. case ShaderLanguage::TYPE_UVEC2:
  3574. case ShaderLanguage::TYPE_IVEC2: {
  3575. if (p_uniform.array_size > 0) {
  3576. pi.type = Variant::PACKED_INT32_ARRAY;
  3577. } else {
  3578. pi.type = Variant::VECTOR2I;
  3579. }
  3580. } break;
  3581. case ShaderLanguage::TYPE_UVEC3:
  3582. case ShaderLanguage::TYPE_IVEC3: {
  3583. if (p_uniform.array_size > 0) {
  3584. pi.type = Variant::PACKED_INT32_ARRAY;
  3585. } else {
  3586. pi.type = Variant::VECTOR3I;
  3587. }
  3588. } break;
  3589. case ShaderLanguage::TYPE_UVEC4:
  3590. case ShaderLanguage::TYPE_IVEC4: {
  3591. if (p_uniform.array_size > 0) {
  3592. pi.type = Variant::PACKED_INT32_ARRAY;
  3593. } else {
  3594. pi.type = Variant::VECTOR4I;
  3595. }
  3596. } break;
  3597. case ShaderLanguage::TYPE_FLOAT: {
  3598. if (p_uniform.array_size > 0) {
  3599. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  3600. } else {
  3601. pi.type = Variant::FLOAT;
  3602. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_RANGE) {
  3603. pi.hint = PROPERTY_HINT_RANGE;
  3604. pi.hint_string = rtos(p_uniform.hint_range[0]) + "," + rtos(p_uniform.hint_range[1]) + "," + rtos(p_uniform.hint_range[2]);
  3605. }
  3606. }
  3607. } break;
  3608. case ShaderLanguage::TYPE_VEC2:
  3609. if (p_uniform.array_size > 0) {
  3610. pi.type = Variant::PACKED_VECTOR2_ARRAY;
  3611. } else {
  3612. pi.type = Variant::VECTOR2;
  3613. }
  3614. break;
  3615. case ShaderLanguage::TYPE_VEC3:
  3616. if (p_uniform.array_size > 0) {
  3617. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3618. pi.hint = PROPERTY_HINT_COLOR_NO_ALPHA;
  3619. pi.type = Variant::PACKED_COLOR_ARRAY;
  3620. } else {
  3621. pi.type = Variant::PACKED_VECTOR3_ARRAY;
  3622. }
  3623. } else {
  3624. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3625. pi.hint = PROPERTY_HINT_COLOR_NO_ALPHA;
  3626. pi.type = Variant::COLOR;
  3627. } else {
  3628. pi.type = Variant::VECTOR3;
  3629. }
  3630. }
  3631. break;
  3632. case ShaderLanguage::TYPE_VEC4: {
  3633. if (p_uniform.array_size > 0) {
  3634. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3635. pi.type = Variant::PACKED_COLOR_ARRAY;
  3636. } else {
  3637. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  3638. }
  3639. } else {
  3640. if (p_uniform.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  3641. pi.type = Variant::COLOR;
  3642. } else {
  3643. pi.type = Variant::VECTOR4;
  3644. }
  3645. }
  3646. } break;
  3647. case ShaderLanguage::TYPE_MAT2:
  3648. if (p_uniform.array_size > 0) {
  3649. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  3650. } else {
  3651. pi.type = Variant::TRANSFORM2D;
  3652. }
  3653. break;
  3654. case ShaderLanguage::TYPE_MAT3:
  3655. if (p_uniform.array_size > 0) {
  3656. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  3657. } else {
  3658. pi.type = Variant::BASIS;
  3659. }
  3660. break;
  3661. case ShaderLanguage::TYPE_MAT4:
  3662. if (p_uniform.array_size > 0) {
  3663. pi.type = Variant::PACKED_FLOAT32_ARRAY;
  3664. } else {
  3665. pi.type = Variant::PROJECTION;
  3666. }
  3667. break;
  3668. case ShaderLanguage::TYPE_SAMPLER2D:
  3669. case ShaderLanguage::TYPE_ISAMPLER2D:
  3670. case ShaderLanguage::TYPE_USAMPLER2D: {
  3671. if (p_uniform.array_size > 0) {
  3672. pi.type = Variant::ARRAY;
  3673. pi.hint = PROPERTY_HINT_ARRAY_TYPE;
  3674. pi.hint_string = MAKE_RESOURCE_TYPE_HINT("Texture2D");
  3675. } else {
  3676. pi.type = Variant::OBJECT;
  3677. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3678. pi.hint_string = "Texture2D";
  3679. }
  3680. } break;
  3681. case ShaderLanguage::TYPE_SAMPLER2DARRAY:
  3682. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  3683. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  3684. case ShaderLanguage::TYPE_SAMPLERCUBE:
  3685. case ShaderLanguage::TYPE_SAMPLERCUBEARRAY: {
  3686. if (p_uniform.array_size > 0) {
  3687. pi.type = Variant::ARRAY;
  3688. pi.hint = PROPERTY_HINT_ARRAY_TYPE;
  3689. pi.hint_string = MAKE_RESOURCE_TYPE_HINT("TextureLayered");
  3690. } else {
  3691. pi.type = Variant::OBJECT;
  3692. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3693. pi.hint_string = "TextureLayered";
  3694. }
  3695. } break;
  3696. case ShaderLanguage::TYPE_SAMPLER3D:
  3697. case ShaderLanguage::TYPE_ISAMPLER3D:
  3698. case ShaderLanguage::TYPE_USAMPLER3D: {
  3699. if (p_uniform.array_size > 0) {
  3700. pi.type = Variant::ARRAY;
  3701. pi.hint = PROPERTY_HINT_ARRAY_TYPE;
  3702. pi.hint_string = MAKE_RESOURCE_TYPE_HINT("Texture3D");
  3703. } else {
  3704. pi.type = Variant::OBJECT;
  3705. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3706. pi.hint_string = "Texture3D";
  3707. }
  3708. } break;
  3709. case ShaderLanguage::TYPE_STRUCT: {
  3710. // FIXME: Implement this.
  3711. } break;
  3712. case ShaderLanguage::TYPE_MAX:
  3713. break;
  3714. }
  3715. return pi;
  3716. }
  3717. uint32_t ShaderLanguage::get_datatype_size(ShaderLanguage::DataType p_type) {
  3718. switch (p_type) {
  3719. case TYPE_VOID:
  3720. return 0;
  3721. case TYPE_BOOL:
  3722. return 4;
  3723. case TYPE_BVEC2:
  3724. return 8;
  3725. case TYPE_BVEC3:
  3726. return 12;
  3727. case TYPE_BVEC4:
  3728. return 16;
  3729. case TYPE_INT:
  3730. return 4;
  3731. case TYPE_IVEC2:
  3732. return 8;
  3733. case TYPE_IVEC3:
  3734. return 12;
  3735. case TYPE_IVEC4:
  3736. return 16;
  3737. case TYPE_UINT:
  3738. return 4;
  3739. case TYPE_UVEC2:
  3740. return 8;
  3741. case TYPE_UVEC3:
  3742. return 12;
  3743. case TYPE_UVEC4:
  3744. return 16;
  3745. case TYPE_FLOAT:
  3746. return 4;
  3747. case TYPE_VEC2:
  3748. return 8;
  3749. case TYPE_VEC3:
  3750. return 12;
  3751. case TYPE_VEC4:
  3752. return 16;
  3753. case TYPE_MAT2:
  3754. return 32; // 4 * 4 + 4 * 4
  3755. case TYPE_MAT3:
  3756. return 48; // 4 * 4 + 4 * 4 + 4 * 4
  3757. case TYPE_MAT4:
  3758. return 64;
  3759. case TYPE_SAMPLER2D:
  3760. return 16;
  3761. case TYPE_ISAMPLER2D:
  3762. return 16;
  3763. case TYPE_USAMPLER2D:
  3764. return 16;
  3765. case TYPE_SAMPLER2DARRAY:
  3766. return 16;
  3767. case TYPE_ISAMPLER2DARRAY:
  3768. return 16;
  3769. case TYPE_USAMPLER2DARRAY:
  3770. return 16;
  3771. case TYPE_SAMPLER3D:
  3772. return 16;
  3773. case TYPE_ISAMPLER3D:
  3774. return 16;
  3775. case TYPE_USAMPLER3D:
  3776. return 16;
  3777. case TYPE_SAMPLERCUBE:
  3778. return 16;
  3779. case TYPE_SAMPLERCUBEARRAY:
  3780. return 16;
  3781. case TYPE_STRUCT:
  3782. return 0;
  3783. case TYPE_MAX: {
  3784. ERR_FAIL_V(0);
  3785. };
  3786. }
  3787. ERR_FAIL_V(0);
  3788. }
  3789. void ShaderLanguage::get_keyword_list(List<String> *r_keywords) {
  3790. HashSet<String> kws;
  3791. int idx = 0;
  3792. while (keyword_list[idx].text) {
  3793. kws.insert(keyword_list[idx].text);
  3794. idx++;
  3795. }
  3796. idx = 0;
  3797. while (builtin_func_defs[idx].name) {
  3798. kws.insert(builtin_func_defs[idx].name);
  3799. idx++;
  3800. }
  3801. for (const String &E : kws) {
  3802. r_keywords->push_back(E);
  3803. }
  3804. }
  3805. bool ShaderLanguage::is_control_flow_keyword(String p_keyword) {
  3806. return p_keyword == "break" ||
  3807. p_keyword == "case" ||
  3808. p_keyword == "continue" ||
  3809. p_keyword == "default" ||
  3810. p_keyword == "do" ||
  3811. p_keyword == "else" ||
  3812. p_keyword == "for" ||
  3813. p_keyword == "if" ||
  3814. p_keyword == "return" ||
  3815. p_keyword == "switch" ||
  3816. p_keyword == "while";
  3817. }
  3818. void ShaderLanguage::get_builtin_funcs(List<String> *r_keywords) {
  3819. HashSet<String> kws;
  3820. int idx = 0;
  3821. while (builtin_func_defs[idx].name) {
  3822. kws.insert(builtin_func_defs[idx].name);
  3823. idx++;
  3824. }
  3825. for (const String &E : kws) {
  3826. r_keywords->push_back(E);
  3827. }
  3828. }
  3829. ShaderLanguage::DataType ShaderLanguage::get_scalar_type(DataType p_type) {
  3830. static const DataType scalar_types[] = {
  3831. TYPE_VOID,
  3832. TYPE_BOOL,
  3833. TYPE_BOOL,
  3834. TYPE_BOOL,
  3835. TYPE_BOOL,
  3836. TYPE_INT,
  3837. TYPE_INT,
  3838. TYPE_INT,
  3839. TYPE_INT,
  3840. TYPE_UINT,
  3841. TYPE_UINT,
  3842. TYPE_UINT,
  3843. TYPE_UINT,
  3844. TYPE_FLOAT,
  3845. TYPE_FLOAT,
  3846. TYPE_FLOAT,
  3847. TYPE_FLOAT,
  3848. TYPE_FLOAT,
  3849. TYPE_FLOAT,
  3850. TYPE_FLOAT,
  3851. TYPE_FLOAT,
  3852. TYPE_INT,
  3853. TYPE_UINT,
  3854. TYPE_FLOAT,
  3855. TYPE_INT,
  3856. TYPE_UINT,
  3857. TYPE_FLOAT,
  3858. TYPE_INT,
  3859. TYPE_UINT,
  3860. TYPE_FLOAT,
  3861. TYPE_FLOAT,
  3862. TYPE_VOID,
  3863. };
  3864. static_assert(sizeof(scalar_types) / sizeof(*scalar_types) == TYPE_MAX);
  3865. return scalar_types[p_type];
  3866. }
  3867. int ShaderLanguage::get_cardinality(DataType p_type) {
  3868. static const int cardinality_table[] = {
  3869. 0,
  3870. 1,
  3871. 2,
  3872. 3,
  3873. 4,
  3874. 1,
  3875. 2,
  3876. 3,
  3877. 4,
  3878. 1,
  3879. 2,
  3880. 3,
  3881. 4,
  3882. 1,
  3883. 2,
  3884. 3,
  3885. 4,
  3886. 4,
  3887. 9,
  3888. 16,
  3889. 1,
  3890. 1,
  3891. 1,
  3892. 1,
  3893. 1,
  3894. 1,
  3895. 1,
  3896. 1,
  3897. 1,
  3898. 1,
  3899. 1,
  3900. 1,
  3901. };
  3902. static_assert(sizeof(cardinality_table) / sizeof(*cardinality_table) == TYPE_MAX);
  3903. return cardinality_table[p_type];
  3904. }
  3905. bool ShaderLanguage::_get_completable_identifier(BlockNode *p_block, CompletionType p_type, StringName &identifier) {
  3906. identifier = StringName();
  3907. TkPos pos = { 0, 0 };
  3908. Token tk = _get_token();
  3909. if (tk.type == TK_IDENTIFIER) {
  3910. identifier = tk.text;
  3911. pos = _get_tkpos();
  3912. tk = _get_token();
  3913. }
  3914. if (tk.type == TK_CURSOR) {
  3915. completion_type = p_type;
  3916. completion_line = tk_line;
  3917. completion_block = p_block;
  3918. pos = _get_tkpos();
  3919. tk = _get_token();
  3920. if (tk.type == TK_IDENTIFIER) {
  3921. identifier = identifier.operator String() + tk.text.operator String();
  3922. } else {
  3923. _set_tkpos(pos);
  3924. }
  3925. return true;
  3926. } else if (identifier != StringName()) {
  3927. _set_tkpos(pos);
  3928. }
  3929. return false;
  3930. }
  3931. bool ShaderLanguage::_is_operator_assign(Operator p_op) const {
  3932. switch (p_op) {
  3933. case OP_ASSIGN:
  3934. case OP_ASSIGN_ADD:
  3935. case OP_ASSIGN_SUB:
  3936. case OP_ASSIGN_MUL:
  3937. case OP_ASSIGN_DIV:
  3938. case OP_ASSIGN_MOD:
  3939. case OP_ASSIGN_SHIFT_LEFT:
  3940. case OP_ASSIGN_SHIFT_RIGHT:
  3941. case OP_ASSIGN_BIT_AND:
  3942. case OP_ASSIGN_BIT_OR:
  3943. case OP_ASSIGN_BIT_XOR:
  3944. return true;
  3945. default:
  3946. return false;
  3947. }
  3948. }
  3949. bool ShaderLanguage::_validate_varying_assign(ShaderNode::Varying &p_varying, String *r_message) {
  3950. if (current_function != "vertex" && current_function != "fragment") {
  3951. *r_message = vformat(RTR("Varying may not be assigned in the '%s' function."), current_function);
  3952. return false;
  3953. }
  3954. switch (p_varying.stage) {
  3955. case ShaderNode::Varying::STAGE_UNKNOWN: // first assign
  3956. if (current_function == varying_function_names.vertex) {
  3957. if (p_varying.type < TYPE_INT) {
  3958. *r_message = vformat(RTR("Varying with '%s' data type may only be assigned in the 'fragment' function."), get_datatype_name(p_varying.type));
  3959. return false;
  3960. }
  3961. p_varying.stage = ShaderNode::Varying::STAGE_VERTEX;
  3962. } else if (current_function == varying_function_names.fragment) {
  3963. p_varying.stage = ShaderNode::Varying::STAGE_FRAGMENT;
  3964. }
  3965. break;
  3966. case ShaderNode::Varying::STAGE_VERTEX_TO_FRAGMENT_LIGHT:
  3967. case ShaderNode::Varying::STAGE_VERTEX:
  3968. if (current_function == varying_function_names.fragment) {
  3969. *r_message = RTR("Varyings which assigned in 'vertex' function may not be reassigned in 'fragment' or 'light'.");
  3970. return false;
  3971. }
  3972. break;
  3973. case ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT:
  3974. case ShaderNode::Varying::STAGE_FRAGMENT:
  3975. if (current_function == varying_function_names.vertex) {
  3976. *r_message = RTR("Varyings which assigned in 'fragment' function may not be reassigned in 'vertex' or 'light'.");
  3977. return false;
  3978. }
  3979. break;
  3980. default:
  3981. break;
  3982. }
  3983. return true;
  3984. }
  3985. bool ShaderLanguage::_check_node_constness(const Node *p_node) const {
  3986. switch (p_node->type) {
  3987. case Node::NODE_TYPE_OPERATOR: {
  3988. const OperatorNode *op_node = static_cast<const OperatorNode *>(p_node);
  3989. for (int i = int(op_node->op == OP_CALL); i < op_node->arguments.size(); i++) {
  3990. if (!_check_node_constness(op_node->arguments[i])) {
  3991. return false;
  3992. }
  3993. }
  3994. } break;
  3995. case Node::NODE_TYPE_CONSTANT:
  3996. break;
  3997. case Node::NODE_TYPE_VARIABLE: {
  3998. const VariableNode *var_node = static_cast<const VariableNode *>(p_node);
  3999. if (!var_node->is_const) {
  4000. return false;
  4001. }
  4002. } break;
  4003. case Node::NODE_TYPE_ARRAY: {
  4004. const ArrayNode *arr_node = static_cast<const ArrayNode *>(p_node);
  4005. if (!arr_node->is_const) {
  4006. return false;
  4007. }
  4008. } break;
  4009. default:
  4010. return false;
  4011. }
  4012. return true;
  4013. }
  4014. bool ShaderLanguage::_validate_assign(Node *p_node, const FunctionInfo &p_function_info, String *r_message) {
  4015. if (p_node->type == Node::NODE_TYPE_OPERATOR) {
  4016. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  4017. if (op->op == OP_INDEX) {
  4018. return _validate_assign(op->arguments[0], p_function_info, r_message);
  4019. } else if (_is_operator_assign(op->op)) {
  4020. //chained assignment
  4021. return _validate_assign(op->arguments[1], p_function_info, r_message);
  4022. } else if (op->op == OP_CALL) {
  4023. if (r_message) {
  4024. *r_message = RTR("Assignment to function.");
  4025. }
  4026. return false;
  4027. }
  4028. } else if (p_node->type == Node::NODE_TYPE_MEMBER) {
  4029. MemberNode *member = static_cast<MemberNode *>(p_node);
  4030. if (member->has_swizzling_duplicates) {
  4031. if (r_message) {
  4032. *r_message = RTR("Swizzling assignment contains duplicates.");
  4033. }
  4034. return false;
  4035. }
  4036. return _validate_assign(member->owner, p_function_info, r_message);
  4037. } else if (p_node->type == Node::NODE_TYPE_VARIABLE) {
  4038. VariableNode *var = static_cast<VariableNode *>(p_node);
  4039. if (shader->uniforms.has(var->name)) {
  4040. if (r_message) {
  4041. *r_message = RTR("Assignment to uniform.");
  4042. }
  4043. return false;
  4044. }
  4045. if (shader->constants.has(var->name) || var->is_const) {
  4046. if (r_message) {
  4047. *r_message = RTR("Constants cannot be modified.");
  4048. }
  4049. return false;
  4050. }
  4051. if (!(p_function_info.built_ins.has(var->name) && p_function_info.built_ins[var->name].constant)) {
  4052. return true;
  4053. }
  4054. } else if (p_node->type == Node::NODE_TYPE_ARRAY) {
  4055. ArrayNode *arr = static_cast<ArrayNode *>(p_node);
  4056. if (shader->constants.has(arr->name) || arr->is_const) {
  4057. if (r_message) {
  4058. *r_message = RTR("Constants cannot be modified.");
  4059. }
  4060. return false;
  4061. }
  4062. return true;
  4063. }
  4064. if (r_message) {
  4065. *r_message = "Assignment to constant expression.";
  4066. }
  4067. return false;
  4068. }
  4069. bool ShaderLanguage::_propagate_function_call_sampler_uniform_settings(StringName p_name, int p_argument, TextureFilter p_filter, TextureRepeat p_repeat) {
  4070. for (int i = 0; i < shader->functions.size(); i++) {
  4071. if (shader->functions[i].name == p_name) {
  4072. ERR_FAIL_INDEX_V(p_argument, shader->functions[i].function->arguments.size(), false);
  4073. FunctionNode::Argument *arg = &shader->functions[i].function->arguments.write[p_argument];
  4074. if (arg->tex_builtin_check) {
  4075. _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)));
  4076. return false;
  4077. } else if (arg->tex_argument_check) {
  4078. //was checked, verify that filter and repeat are the same
  4079. if (arg->tex_argument_filter == p_filter && arg->tex_argument_repeat == p_repeat) {
  4080. return true;
  4081. } else {
  4082. _set_error(vformat(RTR("Sampler argument %d of function '%s' called more than once using textures that differ in either filter or repeat setting."), p_argument, String(p_name)));
  4083. return false;
  4084. }
  4085. } else {
  4086. arg->tex_argument_check = true;
  4087. arg->tex_argument_filter = p_filter;
  4088. arg->tex_argument_repeat = p_repeat;
  4089. for (KeyValue<StringName, HashSet<int>> &E : arg->tex_argument_connect) {
  4090. for (const int &F : E.value) {
  4091. if (!_propagate_function_call_sampler_uniform_settings(E.key, F, p_filter, p_repeat)) {
  4092. return false;
  4093. }
  4094. }
  4095. }
  4096. return true;
  4097. }
  4098. }
  4099. }
  4100. ERR_FAIL_V(false); //bug? function not found
  4101. }
  4102. bool ShaderLanguage::_propagate_function_call_sampler_builtin_reference(StringName p_name, int p_argument, const StringName &p_builtin) {
  4103. for (int i = 0; i < shader->functions.size(); i++) {
  4104. if (shader->functions[i].name == p_name) {
  4105. ERR_FAIL_INDEX_V(p_argument, shader->functions[i].function->arguments.size(), false);
  4106. FunctionNode::Argument *arg = &shader->functions[i].function->arguments.write[p_argument];
  4107. if (arg->tex_argument_check) {
  4108. _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)));
  4109. return false;
  4110. } else if (arg->tex_builtin_check) {
  4111. //was checked, verify that the built-in is the same
  4112. if (arg->tex_builtin == p_builtin) {
  4113. return true;
  4114. } else {
  4115. _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)));
  4116. return false;
  4117. }
  4118. } else {
  4119. arg->tex_builtin_check = true;
  4120. arg->tex_builtin = p_builtin;
  4121. for (KeyValue<StringName, HashSet<int>> &E : arg->tex_argument_connect) {
  4122. for (const int &F : E.value) {
  4123. if (!_propagate_function_call_sampler_builtin_reference(E.key, F, p_builtin)) {
  4124. return false;
  4125. }
  4126. }
  4127. }
  4128. return true;
  4129. }
  4130. }
  4131. }
  4132. ERR_FAIL_V(false); //bug? function not found
  4133. }
  4134. 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) {
  4135. bool error = false;
  4136. if (r_array_size != nullptr && *r_array_size > 0) {
  4137. error = true;
  4138. }
  4139. if (r_unknown_size != nullptr && *r_unknown_size) {
  4140. error = true;
  4141. }
  4142. if (error) {
  4143. _set_error(vformat(RTR("Array size is already defined.")));
  4144. return ERR_PARSE_ERROR;
  4145. }
  4146. TkPos pos = _get_tkpos();
  4147. Token tk = _get_token();
  4148. if (tk.type == TK_BRACKET_CLOSE) {
  4149. if (p_forbid_unknown_size) {
  4150. _set_error(vformat(RTR("Unknown array size is forbidden in that context.")));
  4151. return ERR_PARSE_ERROR;
  4152. }
  4153. if (r_unknown_size != nullptr) {
  4154. *r_unknown_size = true;
  4155. }
  4156. } else {
  4157. int array_size = 0;
  4158. if (!tk.is_integer_constant() || ((int)tk.constant) <= 0) {
  4159. _set_tkpos(pos);
  4160. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  4161. if (n) {
  4162. if (n->type == Node::NODE_TYPE_VARIABLE) {
  4163. VariableNode *vn = static_cast<VariableNode *>(n);
  4164. if (vn) {
  4165. ConstantNode::Value v;
  4166. DataType data_type;
  4167. bool is_const = false;
  4168. _find_identifier(p_block, false, p_function_info, vn->name, &data_type, nullptr, &is_const, nullptr, nullptr, &v);
  4169. if (is_const) {
  4170. if (data_type == TYPE_INT) {
  4171. int32_t value = v.sint;
  4172. if (value > 0) {
  4173. array_size = value;
  4174. }
  4175. } else if (data_type == TYPE_UINT) {
  4176. uint32_t value = v.uint;
  4177. if (value > 0U) {
  4178. array_size = value;
  4179. }
  4180. }
  4181. }
  4182. }
  4183. } else if (n->type == Node::NODE_TYPE_OPERATOR) {
  4184. _set_error(vformat(RTR("Array size expressions are not supported.")));
  4185. return ERR_PARSE_ERROR;
  4186. }
  4187. if (r_size_expression != nullptr) {
  4188. *r_size_expression = n;
  4189. }
  4190. }
  4191. } else if (((int)tk.constant) > 0) {
  4192. array_size = (uint32_t)tk.constant;
  4193. }
  4194. if (array_size <= 0) {
  4195. _set_error(RTR("Expected a positive integer constant."));
  4196. return ERR_PARSE_ERROR;
  4197. }
  4198. tk = _get_token();
  4199. if (tk.type != TK_BRACKET_CLOSE) {
  4200. _set_expected_error("]");
  4201. return ERR_PARSE_ERROR;
  4202. }
  4203. if (r_array_size != nullptr) {
  4204. *r_array_size = array_size;
  4205. }
  4206. }
  4207. return OK;
  4208. }
  4209. ShaderLanguage::Node *ShaderLanguage::_parse_array_constructor(BlockNode *p_block, const FunctionInfo &p_function_info) {
  4210. DataType type = TYPE_VOID;
  4211. String struct_name = "";
  4212. int array_size = 0;
  4213. bool auto_size = false;
  4214. bool undefined_size = false;
  4215. Token tk = _get_token();
  4216. if (tk.type == TK_CURLY_BRACKET_OPEN) {
  4217. auto_size = true;
  4218. } else {
  4219. if (shader->structs.has(tk.text)) {
  4220. type = TYPE_STRUCT;
  4221. struct_name = tk.text;
  4222. } else {
  4223. if (!is_token_variable_datatype(tk.type)) {
  4224. _set_error(RTR("Invalid data type for the array."));
  4225. return nullptr;
  4226. }
  4227. type = get_token_datatype(tk.type);
  4228. }
  4229. tk = _get_token();
  4230. if (tk.type == TK_BRACKET_OPEN) {
  4231. Error error = _parse_array_size(p_block, p_function_info, false, nullptr, &array_size, &undefined_size);
  4232. if (error != OK) {
  4233. return nullptr;
  4234. }
  4235. tk = _get_token();
  4236. } else {
  4237. _set_expected_error("[");
  4238. return nullptr;
  4239. }
  4240. }
  4241. ArrayConstructNode *an = alloc_node<ArrayConstructNode>();
  4242. if (tk.type == TK_PARENTHESIS_OPEN || auto_size) { // initialization
  4243. int idx = 0;
  4244. while (true) {
  4245. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  4246. if (!n) {
  4247. return nullptr;
  4248. }
  4249. // define type by using the first member
  4250. if (auto_size && idx == 0) {
  4251. type = n->get_datatype();
  4252. if (type == TYPE_STRUCT) {
  4253. struct_name = n->get_datatype_name();
  4254. }
  4255. } else {
  4256. if (!_compare_datatypes(type, struct_name, 0, n->get_datatype(), n->get_datatype_name(), 0)) {
  4257. return nullptr;
  4258. }
  4259. }
  4260. tk = _get_token();
  4261. if (tk.type == TK_COMMA) {
  4262. an->initializer.push_back(n);
  4263. } else if (!auto_size && tk.type == TK_PARENTHESIS_CLOSE) {
  4264. an->initializer.push_back(n);
  4265. break;
  4266. } else if (auto_size && tk.type == TK_CURLY_BRACKET_CLOSE) {
  4267. an->initializer.push_back(n);
  4268. break;
  4269. } else {
  4270. if (auto_size) {
  4271. _set_expected_error("}", ",");
  4272. } else {
  4273. _set_expected_error(")", ",");
  4274. }
  4275. return nullptr;
  4276. }
  4277. idx++;
  4278. }
  4279. if (!auto_size && !undefined_size && an->initializer.size() != array_size) {
  4280. _set_error(RTR("Array size mismatch."));
  4281. return nullptr;
  4282. }
  4283. } else {
  4284. _set_error(RTR("Expected array initialization."));
  4285. return nullptr;
  4286. }
  4287. an->datatype = type;
  4288. an->struct_name = struct_name;
  4289. return an;
  4290. }
  4291. 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) {
  4292. DataType type = TYPE_VOID;
  4293. String struct_name = "";
  4294. int array_size = 0;
  4295. bool auto_size = false;
  4296. TkPos prev_pos = _get_tkpos();
  4297. Token tk = _get_token();
  4298. if (tk.type == TK_CURLY_BRACKET_OPEN) {
  4299. auto_size = true;
  4300. } else {
  4301. if (shader->structs.has(tk.text)) {
  4302. type = TYPE_STRUCT;
  4303. struct_name = tk.text;
  4304. } else {
  4305. if (!is_token_variable_datatype(tk.type)) {
  4306. _set_tkpos(prev_pos);
  4307. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  4308. if (!n) {
  4309. _set_error(RTR("Invalid data type for the array."));
  4310. return nullptr;
  4311. }
  4312. if (!_compare_datatypes(p_type, p_struct_name, p_array_size, n->get_datatype(), n->get_datatype_name(), n->get_array_size())) {
  4313. return nullptr;
  4314. }
  4315. return n;
  4316. }
  4317. type = get_token_datatype(tk.type);
  4318. }
  4319. tk = _get_token();
  4320. if (tk.type == TK_BRACKET_OPEN) {
  4321. bool is_unknown_size = false;
  4322. Error error = _parse_array_size(p_block, p_function_info, false, nullptr, &array_size, &is_unknown_size);
  4323. if (error != OK) {
  4324. return nullptr;
  4325. }
  4326. if (is_unknown_size) {
  4327. array_size = p_array_size;
  4328. }
  4329. tk = _get_token();
  4330. } else {
  4331. _set_expected_error("[");
  4332. return nullptr;
  4333. }
  4334. if (type != p_type || struct_name != p_struct_name || array_size != p_array_size) {
  4335. String from;
  4336. if (type == TYPE_STRUCT) {
  4337. from += struct_name;
  4338. } else {
  4339. from += get_datatype_name(type);
  4340. }
  4341. from += "[";
  4342. from += itos(array_size);
  4343. from += "]'";
  4344. String to;
  4345. if (type == TYPE_STRUCT) {
  4346. to += p_struct_name;
  4347. } else {
  4348. to += get_datatype_name(p_type);
  4349. }
  4350. to += "[";
  4351. to += itos(p_array_size);
  4352. to += "]'";
  4353. _set_error(vformat(RTR("Cannot convert from '%s' to '%s'."), from, to));
  4354. return nullptr;
  4355. }
  4356. }
  4357. ArrayConstructNode *an = alloc_node<ArrayConstructNode>();
  4358. an->datatype = p_type;
  4359. an->struct_name = p_struct_name;
  4360. if (tk.type == TK_PARENTHESIS_OPEN || auto_size) { // initialization
  4361. while (true) {
  4362. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  4363. if (!n) {
  4364. return nullptr;
  4365. }
  4366. if (!_compare_datatypes(p_type, p_struct_name, 0, n->get_datatype(), n->get_datatype_name(), n->get_array_size())) {
  4367. return nullptr;
  4368. }
  4369. tk = _get_token();
  4370. if (tk.type == TK_COMMA) {
  4371. an->initializer.push_back(n);
  4372. } else if (!auto_size && tk.type == TK_PARENTHESIS_CLOSE) {
  4373. an->initializer.push_back(n);
  4374. break;
  4375. } else if (auto_size && tk.type == TK_CURLY_BRACKET_CLOSE) {
  4376. an->initializer.push_back(n);
  4377. break;
  4378. } else {
  4379. if (auto_size) {
  4380. _set_expected_error("}", ",");
  4381. } else {
  4382. _set_expected_error(")", ",");
  4383. }
  4384. return nullptr;
  4385. }
  4386. }
  4387. if (an->initializer.size() != p_array_size) {
  4388. _set_error(RTR("Array size mismatch."));
  4389. return nullptr;
  4390. }
  4391. } else {
  4392. _set_error(RTR("Expected array initialization."));
  4393. return nullptr;
  4394. }
  4395. return an;
  4396. }
  4397. ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, const FunctionInfo &p_function_info) {
  4398. Vector<Expression> expression;
  4399. //Vector<TokenType> operators;
  4400. while (true) {
  4401. Node *expr = nullptr;
  4402. TkPos prepos = _get_tkpos();
  4403. Token tk = _get_token();
  4404. TkPos pos = _get_tkpos();
  4405. bool is_const = false;
  4406. if (tk.type == TK_PARENTHESIS_OPEN) {
  4407. //handle subexpression
  4408. expr = _parse_and_reduce_expression(p_block, p_function_info);
  4409. if (!expr) {
  4410. return nullptr;
  4411. }
  4412. tk = _get_token();
  4413. if (tk.type != TK_PARENTHESIS_CLOSE) {
  4414. _set_error(RTR("Expected ')' in expression."));
  4415. return nullptr;
  4416. }
  4417. } else if (tk.type == TK_FLOAT_CONSTANT) {
  4418. ConstantNode *constant = alloc_node<ConstantNode>();
  4419. ConstantNode::Value v;
  4420. v.real = tk.constant;
  4421. constant->values.push_back(v);
  4422. constant->datatype = TYPE_FLOAT;
  4423. expr = constant;
  4424. } else if (tk.type == TK_INT_CONSTANT) {
  4425. ConstantNode *constant = alloc_node<ConstantNode>();
  4426. ConstantNode::Value v;
  4427. v.sint = tk.constant;
  4428. constant->values.push_back(v);
  4429. constant->datatype = TYPE_INT;
  4430. expr = constant;
  4431. } else if (tk.type == TK_UINT_CONSTANT) {
  4432. ConstantNode *constant = alloc_node<ConstantNode>();
  4433. ConstantNode::Value v;
  4434. v.uint = tk.constant;
  4435. constant->values.push_back(v);
  4436. constant->datatype = TYPE_UINT;
  4437. expr = constant;
  4438. } else if (tk.type == TK_TRUE) {
  4439. //handle true constant
  4440. ConstantNode *constant = alloc_node<ConstantNode>();
  4441. ConstantNode::Value v;
  4442. v.boolean = true;
  4443. constant->values.push_back(v);
  4444. constant->datatype = TYPE_BOOL;
  4445. expr = constant;
  4446. } else if (tk.type == TK_FALSE) {
  4447. //handle false constant
  4448. ConstantNode *constant = alloc_node<ConstantNode>();
  4449. ConstantNode::Value v;
  4450. v.boolean = false;
  4451. constant->values.push_back(v);
  4452. constant->datatype = TYPE_BOOL;
  4453. expr = constant;
  4454. } else if (tk.type == TK_TYPE_VOID) {
  4455. //make sure void is not used in expression
  4456. _set_error(RTR("Void value not allowed in expression."));
  4457. return nullptr;
  4458. } else if (is_token_nonvoid_datatype(tk.type) || tk.type == TK_CURLY_BRACKET_OPEN) {
  4459. if (tk.type == TK_CURLY_BRACKET_OPEN) {
  4460. //array constructor
  4461. _set_tkpos(prepos);
  4462. expr = _parse_array_constructor(p_block, p_function_info);
  4463. } else {
  4464. DataType datatype;
  4465. DataPrecision precision = PRECISION_DEFAULT;
  4466. if (is_token_precision(tk.type)) {
  4467. precision = get_token_precision(tk.type);
  4468. tk = _get_token();
  4469. }
  4470. datatype = get_token_datatype(tk.type);
  4471. if (precision != PRECISION_DEFAULT && _validate_precision(datatype, precision) != OK) {
  4472. return nullptr;
  4473. }
  4474. tk = _get_token();
  4475. if (tk.type == TK_BRACKET_OPEN) {
  4476. //array constructor
  4477. _set_tkpos(prepos);
  4478. expr = _parse_array_constructor(p_block, p_function_info);
  4479. } else {
  4480. if (tk.type != TK_PARENTHESIS_OPEN) {
  4481. _set_error(RTR("Expected '(' after the type name."));
  4482. return nullptr;
  4483. }
  4484. //basic type constructor
  4485. OperatorNode *func = alloc_node<OperatorNode>();
  4486. func->op = OP_CONSTRUCT;
  4487. if (precision != PRECISION_DEFAULT) {
  4488. func->return_precision_cache = precision;
  4489. }
  4490. VariableNode *funcname = alloc_node<VariableNode>();
  4491. funcname->name = get_datatype_name(datatype);
  4492. func->arguments.push_back(funcname);
  4493. int carg = -1;
  4494. bool ok = _parse_function_arguments(p_block, p_function_info, func, &carg);
  4495. if (carg >= 0) {
  4496. completion_type = COMPLETION_CALL_ARGUMENTS;
  4497. completion_line = tk_line;
  4498. completion_block = p_block;
  4499. completion_function = funcname->name;
  4500. completion_argument = carg;
  4501. }
  4502. if (!ok) {
  4503. return nullptr;
  4504. }
  4505. if (!_validate_function_call(p_block, p_function_info, func, &func->return_cache, &func->struct_name)) {
  4506. _set_error(vformat(RTR("No matching constructor found for: '%s'."), String(funcname->name)));
  4507. return nullptr;
  4508. }
  4509. expr = _reduce_expression(p_block, func);
  4510. }
  4511. }
  4512. } else if (tk.type == TK_IDENTIFIER) {
  4513. _set_tkpos(prepos);
  4514. StringName identifier;
  4515. StructNode *pstruct = nullptr;
  4516. bool struct_init = false;
  4517. _get_completable_identifier(p_block, COMPLETION_IDENTIFIER, identifier);
  4518. if (shader->structs.has(identifier)) {
  4519. pstruct = shader->structs[identifier].shader_struct;
  4520. struct_init = true;
  4521. }
  4522. tk = _get_token();
  4523. if (tk.type == TK_PARENTHESIS_OPEN) {
  4524. if (struct_init) { //a struct constructor
  4525. const StringName &name = identifier;
  4526. OperatorNode *func = alloc_node<OperatorNode>();
  4527. func->op = OP_STRUCT;
  4528. func->struct_name = name;
  4529. func->return_cache = TYPE_STRUCT;
  4530. VariableNode *funcname = alloc_node<VariableNode>();
  4531. funcname->name = name;
  4532. func->arguments.push_back(funcname);
  4533. for (int i = 0; i < pstruct->members.size(); i++) {
  4534. Node *nexpr;
  4535. if (pstruct->members[i]->array_size != 0) {
  4536. nexpr = _parse_array_constructor(p_block, p_function_info, pstruct->members[i]->get_datatype(), pstruct->members[i]->struct_name, pstruct->members[i]->array_size);
  4537. if (!nexpr) {
  4538. return nullptr;
  4539. }
  4540. } else {
  4541. nexpr = _parse_and_reduce_expression(p_block, p_function_info);
  4542. if (!nexpr) {
  4543. return nullptr;
  4544. }
  4545. if (!_compare_datatypes_in_nodes(pstruct->members[i], nexpr)) {
  4546. return nullptr;
  4547. }
  4548. }
  4549. if (i + 1 < pstruct->members.size()) {
  4550. tk = _get_token();
  4551. if (tk.type != TK_COMMA) {
  4552. _set_expected_error(",");
  4553. return nullptr;
  4554. }
  4555. }
  4556. func->arguments.push_back(nexpr);
  4557. }
  4558. tk = _get_token();
  4559. if (tk.type != TK_PARENTHESIS_CLOSE) {
  4560. _set_expected_error(")");
  4561. return nullptr;
  4562. }
  4563. expr = func;
  4564. } else { //a function call
  4565. if (p_block == nullptr) { // Non-constructor function call in global space is forbidden.
  4566. if (is_const_decl) {
  4567. _set_error(RTR("Expected constant expression."));
  4568. }
  4569. return nullptr;
  4570. }
  4571. const StringName &name = identifier;
  4572. OperatorNode *func = alloc_node<OperatorNode>();
  4573. func->op = OP_CALL;
  4574. VariableNode *funcname = alloc_node<VariableNode>();
  4575. funcname->name = name;
  4576. func->arguments.push_back(funcname);
  4577. int carg = -1;
  4578. bool ok = _parse_function_arguments(p_block, p_function_info, func, &carg);
  4579. // Check if block has a variable with the same name as function to prevent shader crash.
  4580. ShaderLanguage::BlockNode *bnode = p_block;
  4581. while (bnode) {
  4582. if (bnode->variables.has(name)) {
  4583. _set_error(RTR("Expected a function name."));
  4584. return nullptr;
  4585. }
  4586. bnode = bnode->parent_block;
  4587. }
  4588. //test if function was parsed first
  4589. int function_index = -1;
  4590. for (int i = 0; i < shader->functions.size(); i++) {
  4591. if (shader->functions[i].name == name) {
  4592. //add to current function as dependency
  4593. for (int j = 0; j < shader->functions.size(); j++) {
  4594. if (shader->functions[j].name == current_function) {
  4595. shader->functions.write[j].uses_function.insert(name);
  4596. break;
  4597. }
  4598. }
  4599. //see if texture arguments must connect
  4600. function_index = i;
  4601. break;
  4602. }
  4603. }
  4604. if (carg >= 0) {
  4605. completion_type = COMPLETION_CALL_ARGUMENTS;
  4606. completion_line = tk_line;
  4607. completion_block = p_block;
  4608. completion_function = funcname->name;
  4609. completion_argument = carg;
  4610. }
  4611. if (!ok) {
  4612. return nullptr;
  4613. }
  4614. bool is_custom_func = false;
  4615. if (!_validate_function_call(p_block, p_function_info, func, &func->return_cache, &func->struct_name, &is_custom_func)) {
  4616. _set_error(vformat(RTR("No matching function found for: '%s'."), String(funcname->name)));
  4617. return nullptr;
  4618. }
  4619. completion_class = TAG_GLOBAL; // reset sub-class
  4620. if (function_index >= 0) {
  4621. //connect texture arguments, so we can cache in the
  4622. //argument what type of filter and repeat to use
  4623. FunctionNode *call_function = shader->functions[function_index].function;
  4624. if (call_function) {
  4625. func->return_cache = call_function->get_datatype();
  4626. func->struct_name = call_function->get_datatype_name();
  4627. func->return_array_size = call_function->get_array_size();
  4628. //get current base function
  4629. FunctionNode *base_function = nullptr;
  4630. {
  4631. BlockNode *b = p_block;
  4632. while (b) {
  4633. if (b->parent_function) {
  4634. base_function = b->parent_function;
  4635. break;
  4636. } else {
  4637. b = b->parent_block;
  4638. }
  4639. }
  4640. }
  4641. ERR_FAIL_COND_V(!base_function, nullptr); //bug, wtf
  4642. for (int i = 0; i < call_function->arguments.size(); i++) {
  4643. int argidx = i + 1;
  4644. if (argidx < func->arguments.size()) {
  4645. bool error = false;
  4646. Node *n = func->arguments[argidx];
  4647. ArgumentQualifier arg_qual = call_function->arguments[i].qualifier;
  4648. bool is_out_arg = arg_qual != ArgumentQualifier::ARGUMENT_QUALIFIER_IN;
  4649. if (n->type == Node::NODE_TYPE_VARIABLE || n->type == Node::NODE_TYPE_ARRAY) {
  4650. StringName varname;
  4651. if (n->type == Node::NODE_TYPE_VARIABLE) {
  4652. VariableNode *vn = static_cast<VariableNode *>(n);
  4653. varname = vn->name;
  4654. } else { // TYPE_ARRAY
  4655. ArrayNode *an = static_cast<ArrayNode *>(n);
  4656. varname = an->name;
  4657. }
  4658. if (shader->varyings.has(varname)) {
  4659. switch (shader->varyings[varname].stage) {
  4660. case ShaderNode::Varying::STAGE_UNKNOWN: {
  4661. _set_error(vformat(RTR("Varying '%s' must be assigned in the 'vertex' or 'fragment' function first."), varname));
  4662. return nullptr;
  4663. }
  4664. case ShaderNode::Varying::STAGE_VERTEX_TO_FRAGMENT_LIGHT:
  4665. [[fallthrough]];
  4666. case ShaderNode::Varying::STAGE_VERTEX:
  4667. if (is_out_arg && current_function != varying_function_names.vertex) { // inout/out
  4668. error = true;
  4669. }
  4670. break;
  4671. case ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT:
  4672. [[fallthrough]];
  4673. case ShaderNode::Varying::STAGE_FRAGMENT:
  4674. if (!is_out_arg) {
  4675. if (current_function != varying_function_names.fragment && current_function != varying_function_names.light) {
  4676. error = true;
  4677. }
  4678. } else if (current_function != varying_function_names.fragment) { // inout/out
  4679. error = true;
  4680. }
  4681. break;
  4682. default:
  4683. break;
  4684. }
  4685. if (error) {
  4686. _set_error(vformat(RTR("Varying '%s' cannot be passed for the '%s' parameter in that context."), varname, _get_qualifier_str(arg_qual)));
  4687. return nullptr;
  4688. }
  4689. }
  4690. }
  4691. bool is_const_arg = call_function->arguments[i].is_const;
  4692. if (is_const_arg || is_out_arg) {
  4693. StringName varname;
  4694. if (n->type == Node::NODE_TYPE_CONSTANT || n->type == Node::NODE_TYPE_OPERATOR || n->type == Node::NODE_TYPE_ARRAY_CONSTRUCT) {
  4695. if (!is_const_arg) {
  4696. error = true;
  4697. }
  4698. } else if (n->type == Node::NODE_TYPE_ARRAY) {
  4699. ArrayNode *an = static_cast<ArrayNode *>(n);
  4700. if (!is_const_arg && (an->call_expression != nullptr || an->is_const)) {
  4701. error = true;
  4702. }
  4703. varname = an->name;
  4704. } else if (n->type == Node::NODE_TYPE_VARIABLE) {
  4705. VariableNode *vn = static_cast<VariableNode *>(n);
  4706. if (vn->is_const && !is_const_arg) {
  4707. error = true;
  4708. }
  4709. varname = vn->name;
  4710. } else if (n->type == Node::NODE_TYPE_MEMBER) {
  4711. MemberNode *mn = static_cast<MemberNode *>(n);
  4712. if (mn->basetype_const && is_out_arg) {
  4713. error = true;
  4714. }
  4715. }
  4716. if (!error && varname != StringName()) {
  4717. if (shader->constants.has(varname)) {
  4718. error = true;
  4719. } else if (shader->uniforms.has(varname)) {
  4720. error = true;
  4721. } else if (p_function_info.built_ins.has(varname)) {
  4722. BuiltInInfo info = p_function_info.built_ins[varname];
  4723. if (info.constant) {
  4724. error = true;
  4725. }
  4726. }
  4727. }
  4728. if (error) {
  4729. _set_error(vformat(RTR("A constant value cannot be passed for '%s' parameter."), _get_qualifier_str(arg_qual)));
  4730. return nullptr;
  4731. }
  4732. }
  4733. if (is_sampler_type(call_function->arguments[i].type)) {
  4734. //let's see where our argument comes from
  4735. ERR_CONTINUE(n->type != Node::NODE_TYPE_VARIABLE); //bug? this should always be a variable
  4736. VariableNode *vn = static_cast<VariableNode *>(n);
  4737. StringName varname = vn->name;
  4738. if (shader->uniforms.has(varname)) {
  4739. //being sampler, this either comes from a uniform
  4740. ShaderNode::Uniform *u = &shader->uniforms[varname];
  4741. ERR_CONTINUE(u->type != call_function->arguments[i].type); //this should have been validated previously
  4742. if (RendererCompositor::get_singleton()->is_xr_enabled() && is_custom_func) {
  4743. ShaderNode::Uniform::Hint hint = u->hint;
  4744. if (hint == ShaderNode::Uniform::HINT_DEPTH_TEXTURE || hint == ShaderNode::Uniform::HINT_SCREEN_TEXTURE || hint == ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE) {
  4745. _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)));
  4746. return nullptr;
  4747. }
  4748. }
  4749. //propagate
  4750. if (!_propagate_function_call_sampler_uniform_settings(name, i, u->filter, u->repeat)) {
  4751. return nullptr;
  4752. }
  4753. } else if (p_function_info.built_ins.has(varname)) {
  4754. //a built-in
  4755. if (!_propagate_function_call_sampler_builtin_reference(name, i, varname)) {
  4756. return nullptr;
  4757. }
  4758. } else {
  4759. //or this comes from an argument, but nothing else can be a sampler
  4760. bool found = false;
  4761. for (int j = 0; j < base_function->arguments.size(); j++) {
  4762. if (base_function->arguments[j].name == varname) {
  4763. if (!base_function->arguments[j].tex_argument_connect.has(call_function->name)) {
  4764. base_function->arguments.write[j].tex_argument_connect[call_function->name] = HashSet<int>();
  4765. }
  4766. base_function->arguments.write[j].tex_argument_connect[call_function->name].insert(i);
  4767. found = true;
  4768. break;
  4769. }
  4770. }
  4771. ERR_CONTINUE(!found);
  4772. }
  4773. }
  4774. } else {
  4775. break;
  4776. }
  4777. }
  4778. }
  4779. }
  4780. expr = func;
  4781. #ifdef DEBUG_ENABLED
  4782. if (check_warnings) {
  4783. StringName func_name;
  4784. if (p_block && p_block->parent_function) {
  4785. func_name = p_block->parent_function->name;
  4786. }
  4787. _parse_used_identifier(name, IdentifierType::IDENTIFIER_FUNCTION, func_name);
  4788. }
  4789. #endif // DEBUG_ENABLED
  4790. }
  4791. } else {
  4792. //an identifier
  4793. last_name = identifier;
  4794. last_type = IDENTIFIER_MAX;
  4795. _set_tkpos(pos);
  4796. DataType data_type = TYPE_MAX;
  4797. IdentifierType ident_type = IDENTIFIER_MAX;
  4798. int array_size = 0;
  4799. StringName struct_name;
  4800. bool is_local = false;
  4801. if (p_block && p_block->block_tag != SubClassTag::TAG_GLOBAL) {
  4802. int idx = 0;
  4803. bool found = false;
  4804. while (builtin_func_defs[idx].name) {
  4805. if (builtin_func_defs[idx].tag == p_block->block_tag && builtin_func_defs[idx].name == identifier) {
  4806. found = true;
  4807. break;
  4808. }
  4809. idx++;
  4810. }
  4811. if (!found) {
  4812. _set_error(vformat(RTR("Unknown identifier in expression: '%s'."), String(identifier)));
  4813. return nullptr;
  4814. }
  4815. } else {
  4816. if (!_find_identifier(p_block, false, p_function_info, identifier, &data_type, &ident_type, &is_const, &array_size, &struct_name)) {
  4817. if (identifier == "SCREEN_TEXTURE" || identifier == "DEPTH_TEXTURE" || identifier == "NORMAL_ROUGHNESS_TEXTURE") {
  4818. String name = String(identifier);
  4819. String name_lower = name.to_lower();
  4820. _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));
  4821. return nullptr;
  4822. }
  4823. _set_error(vformat(RTR("Unknown identifier in expression: '%s'."), String(identifier)));
  4824. return nullptr;
  4825. }
  4826. if (is_const_decl && !is_const) {
  4827. _set_error(RTR("Expected constant expression."));
  4828. return nullptr;
  4829. }
  4830. if (ident_type == IDENTIFIER_VARYING) {
  4831. TkPos prev_pos = _get_tkpos();
  4832. Token next_token = _get_token();
  4833. // An array of varyings.
  4834. if (next_token.type == TK_BRACKET_OPEN) {
  4835. _get_token(); // Pass constant.
  4836. _get_token(); // Pass TK_BRACKET_CLOSE.
  4837. next_token = _get_token();
  4838. }
  4839. _set_tkpos(prev_pos);
  4840. ShaderNode::Varying &var = shader->varyings[identifier];
  4841. String error;
  4842. if (is_token_operator_assign(next_token.type)) {
  4843. if (!_validate_varying_assign(shader->varyings[identifier], &error)) {
  4844. _set_error(error);
  4845. return nullptr;
  4846. }
  4847. } else {
  4848. switch (var.stage) {
  4849. case ShaderNode::Varying::STAGE_UNKNOWN: {
  4850. if (var.type < TYPE_INT) {
  4851. if (current_function == varying_function_names.vertex) {
  4852. _set_error(vformat(RTR("Varying with '%s' data type may only be used in the 'fragment' function."), get_datatype_name(var.type)));
  4853. } else {
  4854. _set_error(vformat(RTR("Varying '%s' must be assigned in the 'fragment' function first."), identifier));
  4855. }
  4856. return nullptr;
  4857. }
  4858. } break;
  4859. case ShaderNode::Varying::STAGE_VERTEX:
  4860. if (current_function == varying_function_names.fragment || current_function == varying_function_names.light) {
  4861. var.stage = ShaderNode::Varying::STAGE_VERTEX_TO_FRAGMENT_LIGHT;
  4862. }
  4863. break;
  4864. case ShaderNode::Varying::STAGE_FRAGMENT:
  4865. if (current_function == varying_function_names.light) {
  4866. var.stage = ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT;
  4867. }
  4868. break;
  4869. default:
  4870. break;
  4871. }
  4872. }
  4873. if ((var.stage != ShaderNode::Varying::STAGE_FRAGMENT && var.stage != ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT) && var.type < TYPE_FLOAT && var.interpolation != INTERPOLATION_FLAT) {
  4874. _set_tkpos(var.tkpos);
  4875. _set_error(RTR("Varying with integer data type must be declared with `flat` interpolation qualifier."));
  4876. return nullptr;
  4877. }
  4878. }
  4879. if (ident_type == IDENTIFIER_FUNCTION) {
  4880. _set_error(vformat(RTR("Can't use function as identifier: '%s'."), String(identifier)));
  4881. return nullptr;
  4882. }
  4883. if (is_const) {
  4884. last_type = IDENTIFIER_CONSTANT;
  4885. } else {
  4886. last_type = ident_type;
  4887. }
  4888. is_local = ident_type == IDENTIFIER_LOCAL_VAR || ident_type == IDENTIFIER_FUNCTION_ARGUMENT;
  4889. }
  4890. Node *index_expression = nullptr;
  4891. Node *call_expression = nullptr;
  4892. Node *assign_expression = nullptr;
  4893. if (array_size > 0) {
  4894. prepos = _get_tkpos();
  4895. tk = _get_token();
  4896. if (tk.type == TK_OP_ASSIGN) {
  4897. if (is_const) {
  4898. _set_error(RTR("Constants cannot be modified."));
  4899. return nullptr;
  4900. }
  4901. assign_expression = _parse_array_constructor(p_block, p_function_info, data_type, struct_name, array_size);
  4902. if (!assign_expression) {
  4903. return nullptr;
  4904. }
  4905. } else if (tk.type == TK_PERIOD) {
  4906. completion_class = TAG_ARRAY;
  4907. if (p_block != nullptr) {
  4908. p_block->block_tag = SubClassTag::TAG_ARRAY;
  4909. }
  4910. call_expression = _parse_and_reduce_expression(p_block, p_function_info);
  4911. if (p_block != nullptr) {
  4912. p_block->block_tag = SubClassTag::TAG_GLOBAL;
  4913. }
  4914. if (!call_expression) {
  4915. return nullptr;
  4916. }
  4917. } else if (tk.type == TK_BRACKET_OPEN) { // indexing
  4918. index_expression = _parse_and_reduce_expression(p_block, p_function_info);
  4919. if (!index_expression) {
  4920. return nullptr;
  4921. }
  4922. if (index_expression->get_array_size() != 0 || (index_expression->get_datatype() != TYPE_INT && index_expression->get_datatype() != TYPE_UINT)) {
  4923. _set_error(RTR("Only integer expressions are allowed for indexing."));
  4924. return nullptr;
  4925. }
  4926. if (index_expression->type == Node::NODE_TYPE_CONSTANT) {
  4927. ConstantNode *cnode = static_cast<ConstantNode *>(index_expression);
  4928. if (cnode) {
  4929. if (!cnode->values.is_empty()) {
  4930. int value = cnode->values[0].sint;
  4931. if (value < 0 || value >= array_size) {
  4932. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), value, 0, array_size - 1));
  4933. return nullptr;
  4934. }
  4935. }
  4936. }
  4937. }
  4938. tk = _get_token();
  4939. if (tk.type != TK_BRACKET_CLOSE) {
  4940. _set_expected_error("]");
  4941. return nullptr;
  4942. }
  4943. } else {
  4944. _set_tkpos(prepos);
  4945. }
  4946. ArrayNode *arrname = alloc_node<ArrayNode>();
  4947. arrname->name = identifier;
  4948. arrname->datatype_cache = data_type;
  4949. arrname->struct_name = struct_name;
  4950. arrname->index_expression = index_expression;
  4951. arrname->call_expression = call_expression;
  4952. arrname->assign_expression = assign_expression;
  4953. arrname->is_const = is_const;
  4954. arrname->array_size = array_size;
  4955. arrname->is_local = is_local;
  4956. expr = arrname;
  4957. } else {
  4958. VariableNode *varname = alloc_node<VariableNode>();
  4959. varname->name = identifier;
  4960. varname->datatype_cache = data_type;
  4961. varname->is_const = is_const;
  4962. varname->struct_name = struct_name;
  4963. varname->is_local = is_local;
  4964. expr = varname;
  4965. }
  4966. #ifdef DEBUG_ENABLED
  4967. if (check_warnings) {
  4968. StringName func_name;
  4969. BlockNode *b = p_block;
  4970. while (b) {
  4971. if (b->parent_function) {
  4972. func_name = b->parent_function->name;
  4973. break;
  4974. } else {
  4975. b = b->parent_block;
  4976. }
  4977. }
  4978. _parse_used_identifier(identifier, ident_type, func_name);
  4979. }
  4980. #endif // DEBUG_ENABLED
  4981. }
  4982. } else if (tk.type == TK_OP_ADD) {
  4983. continue; //this one does nothing
  4984. } 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) {
  4985. Expression e;
  4986. e.is_op = true;
  4987. switch (tk.type) {
  4988. case TK_OP_SUB:
  4989. e.op = OP_NEGATE;
  4990. break;
  4991. case TK_OP_NOT:
  4992. e.op = OP_NOT;
  4993. break;
  4994. case TK_OP_BIT_INVERT:
  4995. e.op = OP_BIT_INVERT;
  4996. break;
  4997. case TK_OP_INCREMENT:
  4998. e.op = OP_INCREMENT;
  4999. break;
  5000. case TK_OP_DECREMENT:
  5001. e.op = OP_DECREMENT;
  5002. break;
  5003. default:
  5004. ERR_FAIL_V(nullptr);
  5005. }
  5006. expression.push_back(e);
  5007. continue;
  5008. } else {
  5009. bool valid = false;
  5010. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_FOR_EXPRESSION && tk.type == TK_PARENTHESIS_CLOSE) {
  5011. valid = true;
  5012. _set_tkpos(prepos);
  5013. OperatorNode *func = alloc_node<OperatorNode>();
  5014. func->op = OP_EMPTY;
  5015. expr = func;
  5016. }
  5017. if (!valid) {
  5018. if (tk.type != TK_SEMICOLON) {
  5019. _set_error(vformat(RTR("Expected expression, found: '%s'."), get_token_text(tk)));
  5020. return nullptr;
  5021. } else {
  5022. #ifdef DEBUG_ENABLED
  5023. if (!p_block || (p_block->block_type != BlockNode::BLOCK_TYPE_FOR_INIT && p_block->block_type != BlockNode::BLOCK_TYPE_FOR_CONDITION)) {
  5024. if (check_warnings && HAS_WARNING(ShaderWarning::FORMATTING_ERROR_FLAG)) {
  5025. _add_line_warning(ShaderWarning::FORMATTING_ERROR, RTR("Empty statement. Remove ';' to fix this warning."));
  5026. }
  5027. }
  5028. #endif // DEBUG_ENABLED
  5029. _set_tkpos(prepos);
  5030. OperatorNode *func = alloc_node<OperatorNode>();
  5031. func->op = OP_EMPTY;
  5032. expr = func;
  5033. }
  5034. }
  5035. }
  5036. ERR_FAIL_COND_V(!expr, nullptr);
  5037. /* OK now see what's NEXT to the operator.. */
  5038. while (true) {
  5039. TkPos pos2 = _get_tkpos();
  5040. tk = _get_token();
  5041. if (tk.type == TK_CURSOR) {
  5042. //do nothing
  5043. } else if (tk.type == TK_PERIOD) {
  5044. #ifdef DEBUG_ENABLED
  5045. uint32_t prev_keyword_completion_context = keyword_completion_context;
  5046. keyword_completion_context = CF_UNSPECIFIED;
  5047. #endif
  5048. DataType dt = expr->get_datatype();
  5049. String st = expr->get_datatype_name();
  5050. if (!expr->is_indexed() && expr->get_array_size() > 0) {
  5051. completion_class = TAG_ARRAY;
  5052. if (p_block != nullptr) {
  5053. p_block->block_tag = SubClassTag::TAG_ARRAY;
  5054. }
  5055. Node *call_expression = _parse_and_reduce_expression(p_block, p_function_info);
  5056. if (p_block != nullptr) {
  5057. p_block->block_tag = SubClassTag::TAG_GLOBAL;
  5058. }
  5059. if (!call_expression) {
  5060. return nullptr;
  5061. }
  5062. expr = call_expression;
  5063. break;
  5064. }
  5065. StringName identifier;
  5066. if (_get_completable_identifier(p_block, dt == TYPE_STRUCT ? COMPLETION_STRUCT : COMPLETION_INDEX, identifier)) {
  5067. if (dt == TYPE_STRUCT) {
  5068. completion_struct = st;
  5069. } else {
  5070. completion_base = dt;
  5071. }
  5072. }
  5073. if (identifier == StringName()) {
  5074. _set_error(RTR("Expected an identifier as a member."));
  5075. return nullptr;
  5076. }
  5077. String ident = identifier;
  5078. bool ok = true;
  5079. bool repeated = false;
  5080. DataType member_type = TYPE_VOID;
  5081. StringName member_struct_name = "";
  5082. int array_size = 0;
  5083. RBSet<char> position_symbols;
  5084. RBSet<char> color_symbols;
  5085. RBSet<char> texture_symbols;
  5086. bool mix_error = false;
  5087. switch (dt) {
  5088. case TYPE_STRUCT: {
  5089. ok = false;
  5090. String member_name = String(ident.ptr());
  5091. if (shader->structs.has(st)) {
  5092. StructNode *n = shader->structs[st].shader_struct;
  5093. for (const MemberNode *E : n->members) {
  5094. if (String(E->name) == member_name) {
  5095. member_type = E->datatype;
  5096. array_size = E->array_size;
  5097. if (member_type == TYPE_STRUCT) {
  5098. member_struct_name = E->struct_name;
  5099. }
  5100. ok = true;
  5101. break;
  5102. }
  5103. }
  5104. }
  5105. } break;
  5106. case TYPE_BVEC2:
  5107. case TYPE_IVEC2:
  5108. case TYPE_UVEC2:
  5109. case TYPE_VEC2: {
  5110. int l = ident.length();
  5111. if (l == 1) {
  5112. member_type = DataType(dt - 1);
  5113. } else if (l == 2) {
  5114. member_type = dt;
  5115. } else if (l == 3) {
  5116. member_type = DataType(dt + 1);
  5117. } else if (l == 4) {
  5118. member_type = DataType(dt + 2);
  5119. } else {
  5120. ok = false;
  5121. break;
  5122. }
  5123. const char32_t *c = ident.ptr();
  5124. for (int i = 0; i < l; i++) {
  5125. switch (c[i]) {
  5126. case 'r':
  5127. case 'g':
  5128. if (position_symbols.size() > 0 || texture_symbols.size() > 0) {
  5129. mix_error = true;
  5130. break;
  5131. }
  5132. if (!color_symbols.has(c[i])) {
  5133. color_symbols.insert(c[i]);
  5134. } else {
  5135. repeated = true;
  5136. }
  5137. break;
  5138. case 'x':
  5139. case 'y':
  5140. if (color_symbols.size() > 0 || texture_symbols.size() > 0) {
  5141. mix_error = true;
  5142. break;
  5143. }
  5144. if (!position_symbols.has(c[i])) {
  5145. position_symbols.insert(c[i]);
  5146. } else {
  5147. repeated = true;
  5148. }
  5149. break;
  5150. case 's':
  5151. case 't':
  5152. if (color_symbols.size() > 0 || position_symbols.size() > 0) {
  5153. mix_error = true;
  5154. break;
  5155. }
  5156. if (!texture_symbols.has(c[i])) {
  5157. texture_symbols.insert(c[i]);
  5158. } else {
  5159. repeated = true;
  5160. }
  5161. break;
  5162. default:
  5163. ok = false;
  5164. break;
  5165. }
  5166. }
  5167. } break;
  5168. case TYPE_BVEC3:
  5169. case TYPE_IVEC3:
  5170. case TYPE_UVEC3:
  5171. case TYPE_VEC3: {
  5172. int l = ident.length();
  5173. if (l == 1) {
  5174. member_type = DataType(dt - 2);
  5175. } else if (l == 2) {
  5176. member_type = DataType(dt - 1);
  5177. } else if (l == 3) {
  5178. member_type = dt;
  5179. } else if (l == 4) {
  5180. member_type = DataType(dt + 1);
  5181. } else {
  5182. ok = false;
  5183. break;
  5184. }
  5185. const char32_t *c = ident.ptr();
  5186. for (int i = 0; i < l; i++) {
  5187. switch (c[i]) {
  5188. case 'r':
  5189. case 'g':
  5190. case 'b':
  5191. if (position_symbols.size() > 0 || texture_symbols.size() > 0) {
  5192. mix_error = true;
  5193. break;
  5194. }
  5195. if (!color_symbols.has(c[i])) {
  5196. color_symbols.insert(c[i]);
  5197. } else {
  5198. repeated = true;
  5199. }
  5200. break;
  5201. case 'x':
  5202. case 'y':
  5203. case 'z':
  5204. if (color_symbols.size() > 0 || texture_symbols.size() > 0) {
  5205. mix_error = true;
  5206. break;
  5207. }
  5208. if (!position_symbols.has(c[i])) {
  5209. position_symbols.insert(c[i]);
  5210. } else {
  5211. repeated = true;
  5212. }
  5213. break;
  5214. case 's':
  5215. case 't':
  5216. case 'p':
  5217. if (color_symbols.size() > 0 || position_symbols.size() > 0) {
  5218. mix_error = true;
  5219. break;
  5220. }
  5221. if (!texture_symbols.has(c[i])) {
  5222. texture_symbols.insert(c[i]);
  5223. } else {
  5224. repeated = true;
  5225. }
  5226. break;
  5227. default:
  5228. ok = false;
  5229. break;
  5230. }
  5231. }
  5232. } break;
  5233. case TYPE_BVEC4:
  5234. case TYPE_IVEC4:
  5235. case TYPE_UVEC4:
  5236. case TYPE_VEC4: {
  5237. int l = ident.length();
  5238. if (l == 1) {
  5239. member_type = DataType(dt - 3);
  5240. } else if (l == 2) {
  5241. member_type = DataType(dt - 2);
  5242. } else if (l == 3) {
  5243. member_type = DataType(dt - 1);
  5244. } else if (l == 4) {
  5245. member_type = dt;
  5246. } else {
  5247. ok = false;
  5248. break;
  5249. }
  5250. const char32_t *c = ident.ptr();
  5251. for (int i = 0; i < l; i++) {
  5252. switch (c[i]) {
  5253. case 'r':
  5254. case 'g':
  5255. case 'b':
  5256. case 'a':
  5257. if (position_symbols.size() > 0 || texture_symbols.size() > 0) {
  5258. mix_error = true;
  5259. break;
  5260. }
  5261. if (!color_symbols.has(c[i])) {
  5262. color_symbols.insert(c[i]);
  5263. } else {
  5264. repeated = true;
  5265. }
  5266. break;
  5267. case 'x':
  5268. case 'y':
  5269. case 'z':
  5270. case 'w':
  5271. if (color_symbols.size() > 0 || texture_symbols.size() > 0) {
  5272. mix_error = true;
  5273. break;
  5274. }
  5275. if (!position_symbols.has(c[i])) {
  5276. position_symbols.insert(c[i]);
  5277. } else {
  5278. repeated = true;
  5279. }
  5280. break;
  5281. case 's':
  5282. case 't':
  5283. case 'p':
  5284. case 'q':
  5285. if (color_symbols.size() > 0 || position_symbols.size() > 0) {
  5286. mix_error = true;
  5287. break;
  5288. }
  5289. if (!texture_symbols.has(c[i])) {
  5290. texture_symbols.insert(c[i]);
  5291. } else {
  5292. repeated = true;
  5293. }
  5294. break;
  5295. default:
  5296. ok = false;
  5297. break;
  5298. }
  5299. }
  5300. } break;
  5301. default: {
  5302. ok = false;
  5303. }
  5304. }
  5305. if (mix_error) {
  5306. _set_error(vformat(RTR("Cannot combine symbols from different sets in expression '.%s'."), ident));
  5307. return nullptr;
  5308. }
  5309. if (!ok) {
  5310. _set_error(vformat(RTR("Invalid member for '%s' expression: '.%s'."), (dt == TYPE_STRUCT ? st : get_datatype_name(dt)), ident));
  5311. return nullptr;
  5312. }
  5313. MemberNode *mn = alloc_node<MemberNode>();
  5314. mn->basetype = dt;
  5315. mn->basetype_const = is_const;
  5316. mn->datatype = member_type;
  5317. mn->base_struct_name = st;
  5318. mn->struct_name = member_struct_name;
  5319. mn->array_size = array_size;
  5320. mn->name = ident;
  5321. mn->owner = expr;
  5322. mn->has_swizzling_duplicates = repeated;
  5323. if (array_size > 0) {
  5324. TkPos prev_pos = _get_tkpos();
  5325. tk = _get_token();
  5326. if (tk.type == TK_OP_ASSIGN) {
  5327. if (last_type == IDENTIFIER_CONSTANT) {
  5328. _set_error(RTR("Constants cannot be modified."));
  5329. return nullptr;
  5330. }
  5331. Node *assign_expression = _parse_array_constructor(p_block, p_function_info, member_type, member_struct_name, array_size);
  5332. if (!assign_expression) {
  5333. return nullptr;
  5334. }
  5335. mn->assign_expression = assign_expression;
  5336. } else if (tk.type == TK_PERIOD) {
  5337. completion_class = TAG_ARRAY;
  5338. if (p_block != nullptr) {
  5339. p_block->block_tag = SubClassTag::TAG_ARRAY;
  5340. }
  5341. mn->call_expression = _parse_and_reduce_expression(p_block, p_function_info);
  5342. if (p_block != nullptr) {
  5343. p_block->block_tag = SubClassTag::TAG_GLOBAL;
  5344. }
  5345. if (!mn->call_expression) {
  5346. return nullptr;
  5347. }
  5348. } else if (tk.type == TK_BRACKET_OPEN) {
  5349. Node *index_expression = _parse_and_reduce_expression(p_block, p_function_info);
  5350. if (!index_expression) {
  5351. return nullptr;
  5352. }
  5353. if (index_expression->get_array_size() != 0 || (index_expression->get_datatype() != TYPE_INT && index_expression->get_datatype() != TYPE_UINT)) {
  5354. _set_error(RTR("Only integer expressions are allowed for indexing."));
  5355. return nullptr;
  5356. }
  5357. if (index_expression->type == Node::NODE_TYPE_CONSTANT) {
  5358. ConstantNode *cnode = static_cast<ConstantNode *>(index_expression);
  5359. if (cnode) {
  5360. if (!cnode->values.is_empty()) {
  5361. int value = cnode->values[0].sint;
  5362. if (value < 0 || value >= array_size) {
  5363. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), value, 0, array_size - 1));
  5364. return nullptr;
  5365. }
  5366. }
  5367. }
  5368. }
  5369. tk = _get_token();
  5370. if (tk.type != TK_BRACKET_CLOSE) {
  5371. _set_expected_error("]");
  5372. return nullptr;
  5373. }
  5374. mn->index_expression = index_expression;
  5375. } else {
  5376. _set_tkpos(prev_pos);
  5377. }
  5378. }
  5379. expr = mn;
  5380. #ifdef DEBUG_ENABLED
  5381. keyword_completion_context = prev_keyword_completion_context;
  5382. #endif
  5383. //todo
  5384. //member (period) has priority over any operator
  5385. //creates a subindexing expression in place
  5386. /*} else if (tk.type==TK_BRACKET_OPEN) {
  5387. //todo
  5388. //subindexing has priority over any operator
  5389. //creates a subindexing expression in place
  5390. */
  5391. } else if (tk.type == TK_BRACKET_OPEN) {
  5392. Node *index = _parse_and_reduce_expression(p_block, p_function_info);
  5393. if (!index) {
  5394. return nullptr;
  5395. }
  5396. if (index->get_array_size() != 0 || (index->get_datatype() != TYPE_INT && index->get_datatype() != TYPE_UINT)) {
  5397. _set_error(RTR("Only integer expressions are allowed for indexing."));
  5398. return nullptr;
  5399. }
  5400. DataType member_type = TYPE_VOID;
  5401. String member_struct_name;
  5402. if (expr->get_array_size() > 0) {
  5403. if (index->type == Node::NODE_TYPE_CONSTANT) {
  5404. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  5405. if (index_constant >= (uint32_t)expr->get_array_size()) {
  5406. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), index_constant, 0, expr->get_array_size() - 1));
  5407. return nullptr;
  5408. }
  5409. }
  5410. member_type = expr->get_datatype();
  5411. if (member_type == TYPE_STRUCT) {
  5412. member_struct_name = expr->get_datatype_name();
  5413. }
  5414. } else {
  5415. switch (expr->get_datatype()) {
  5416. case TYPE_BVEC2:
  5417. case TYPE_VEC2:
  5418. case TYPE_IVEC2:
  5419. case TYPE_UVEC2:
  5420. case TYPE_MAT2:
  5421. if (index->type == Node::NODE_TYPE_CONSTANT) {
  5422. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  5423. if (index_constant >= 2) {
  5424. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), index_constant, 0, 1));
  5425. return nullptr;
  5426. }
  5427. }
  5428. switch (expr->get_datatype()) {
  5429. case TYPE_BVEC2:
  5430. member_type = TYPE_BOOL;
  5431. break;
  5432. case TYPE_VEC2:
  5433. member_type = TYPE_FLOAT;
  5434. break;
  5435. case TYPE_IVEC2:
  5436. member_type = TYPE_INT;
  5437. break;
  5438. case TYPE_UVEC2:
  5439. member_type = TYPE_UINT;
  5440. break;
  5441. case TYPE_MAT2:
  5442. member_type = TYPE_VEC2;
  5443. break;
  5444. default:
  5445. break;
  5446. }
  5447. break;
  5448. case TYPE_BVEC3:
  5449. case TYPE_VEC3:
  5450. case TYPE_IVEC3:
  5451. case TYPE_UVEC3:
  5452. case TYPE_MAT3:
  5453. if (index->type == Node::NODE_TYPE_CONSTANT) {
  5454. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  5455. if (index_constant >= 3) {
  5456. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), index_constant, 0, 2));
  5457. return nullptr;
  5458. }
  5459. }
  5460. switch (expr->get_datatype()) {
  5461. case TYPE_BVEC3:
  5462. member_type = TYPE_BOOL;
  5463. break;
  5464. case TYPE_VEC3:
  5465. member_type = TYPE_FLOAT;
  5466. break;
  5467. case TYPE_IVEC3:
  5468. member_type = TYPE_INT;
  5469. break;
  5470. case TYPE_UVEC3:
  5471. member_type = TYPE_UINT;
  5472. break;
  5473. case TYPE_MAT3:
  5474. member_type = TYPE_VEC3;
  5475. break;
  5476. default:
  5477. break;
  5478. }
  5479. break;
  5480. case TYPE_BVEC4:
  5481. case TYPE_VEC4:
  5482. case TYPE_IVEC4:
  5483. case TYPE_UVEC4:
  5484. case TYPE_MAT4:
  5485. if (index->type == Node::NODE_TYPE_CONSTANT) {
  5486. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  5487. if (index_constant >= 4) {
  5488. _set_error(vformat(RTR("Index [%d] out of range [%d..%d]."), index_constant, 0, 3));
  5489. return nullptr;
  5490. }
  5491. }
  5492. switch (expr->get_datatype()) {
  5493. case TYPE_BVEC4:
  5494. member_type = TYPE_BOOL;
  5495. break;
  5496. case TYPE_VEC4:
  5497. member_type = TYPE_FLOAT;
  5498. break;
  5499. case TYPE_IVEC4:
  5500. member_type = TYPE_INT;
  5501. break;
  5502. case TYPE_UVEC4:
  5503. member_type = TYPE_UINT;
  5504. break;
  5505. case TYPE_MAT4:
  5506. member_type = TYPE_VEC4;
  5507. break;
  5508. default:
  5509. break;
  5510. }
  5511. break;
  5512. default: {
  5513. _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()))));
  5514. return nullptr;
  5515. }
  5516. }
  5517. }
  5518. OperatorNode *op = alloc_node<OperatorNode>();
  5519. op->op = OP_INDEX;
  5520. op->return_cache = member_type;
  5521. op->struct_name = member_struct_name;
  5522. op->arguments.push_back(expr);
  5523. op->arguments.push_back(index);
  5524. expr = op;
  5525. tk = _get_token();
  5526. if (tk.type != TK_BRACKET_CLOSE) {
  5527. _set_expected_error("]");
  5528. return nullptr;
  5529. }
  5530. } else if (tk.type == TK_OP_INCREMENT || tk.type == TK_OP_DECREMENT) {
  5531. OperatorNode *op = alloc_node<OperatorNode>();
  5532. op->op = tk.type == TK_OP_DECREMENT ? OP_POST_DECREMENT : OP_POST_INCREMENT;
  5533. op->arguments.push_back(expr);
  5534. if (!_validate_operator(op, &op->return_cache, &op->return_array_size)) {
  5535. _set_error(RTR("Invalid base type for increment/decrement operator."));
  5536. return nullptr;
  5537. }
  5538. if (!_validate_assign(expr, p_function_info)) {
  5539. _set_error(RTR("Invalid use of increment/decrement operator in a constant expression."));
  5540. return nullptr;
  5541. }
  5542. expr = op;
  5543. } else {
  5544. _set_tkpos(pos2);
  5545. break;
  5546. }
  5547. }
  5548. Expression e;
  5549. e.is_op = false;
  5550. e.node = expr;
  5551. expression.push_back(e);
  5552. pos = _get_tkpos();
  5553. tk = _get_token();
  5554. if (is_token_operator(tk.type)) {
  5555. Expression o;
  5556. o.is_op = true;
  5557. switch (tk.type) {
  5558. case TK_OP_EQUAL:
  5559. o.op = OP_EQUAL;
  5560. break;
  5561. case TK_OP_NOT_EQUAL:
  5562. o.op = OP_NOT_EQUAL;
  5563. break;
  5564. case TK_OP_LESS:
  5565. o.op = OP_LESS;
  5566. break;
  5567. case TK_OP_LESS_EQUAL:
  5568. o.op = OP_LESS_EQUAL;
  5569. break;
  5570. case TK_OP_GREATER:
  5571. o.op = OP_GREATER;
  5572. break;
  5573. case TK_OP_GREATER_EQUAL:
  5574. o.op = OP_GREATER_EQUAL;
  5575. break;
  5576. case TK_OP_AND:
  5577. o.op = OP_AND;
  5578. break;
  5579. case TK_OP_OR:
  5580. o.op = OP_OR;
  5581. break;
  5582. case TK_OP_ADD:
  5583. o.op = OP_ADD;
  5584. break;
  5585. case TK_OP_SUB:
  5586. o.op = OP_SUB;
  5587. break;
  5588. case TK_OP_MUL:
  5589. o.op = OP_MUL;
  5590. break;
  5591. case TK_OP_DIV:
  5592. o.op = OP_DIV;
  5593. break;
  5594. case TK_OP_MOD:
  5595. o.op = OP_MOD;
  5596. break;
  5597. case TK_OP_SHIFT_LEFT:
  5598. o.op = OP_SHIFT_LEFT;
  5599. break;
  5600. case TK_OP_SHIFT_RIGHT:
  5601. o.op = OP_SHIFT_RIGHT;
  5602. break;
  5603. case TK_OP_ASSIGN:
  5604. o.op = OP_ASSIGN;
  5605. break;
  5606. case TK_OP_ASSIGN_ADD:
  5607. o.op = OP_ASSIGN_ADD;
  5608. break;
  5609. case TK_OP_ASSIGN_SUB:
  5610. o.op = OP_ASSIGN_SUB;
  5611. break;
  5612. case TK_OP_ASSIGN_MUL:
  5613. o.op = OP_ASSIGN_MUL;
  5614. break;
  5615. case TK_OP_ASSIGN_DIV:
  5616. o.op = OP_ASSIGN_DIV;
  5617. break;
  5618. case TK_OP_ASSIGN_MOD:
  5619. o.op = OP_ASSIGN_MOD;
  5620. break;
  5621. case TK_OP_ASSIGN_SHIFT_LEFT:
  5622. o.op = OP_ASSIGN_SHIFT_LEFT;
  5623. break;
  5624. case TK_OP_ASSIGN_SHIFT_RIGHT:
  5625. o.op = OP_ASSIGN_SHIFT_RIGHT;
  5626. break;
  5627. case TK_OP_ASSIGN_BIT_AND:
  5628. o.op = OP_ASSIGN_BIT_AND;
  5629. break;
  5630. case TK_OP_ASSIGN_BIT_OR:
  5631. o.op = OP_ASSIGN_BIT_OR;
  5632. break;
  5633. case TK_OP_ASSIGN_BIT_XOR:
  5634. o.op = OP_ASSIGN_BIT_XOR;
  5635. break;
  5636. case TK_OP_BIT_AND:
  5637. o.op = OP_BIT_AND;
  5638. break;
  5639. case TK_OP_BIT_OR:
  5640. o.op = OP_BIT_OR;
  5641. break;
  5642. case TK_OP_BIT_XOR:
  5643. o.op = OP_BIT_XOR;
  5644. break;
  5645. case TK_QUESTION:
  5646. o.op = OP_SELECT_IF;
  5647. break;
  5648. case TK_COLON:
  5649. o.op = OP_SELECT_ELSE;
  5650. break;
  5651. default: {
  5652. _set_error(vformat(RTR("Invalid token for the operator: '%s'."), get_token_text(tk)));
  5653. return nullptr;
  5654. }
  5655. }
  5656. expression.push_back(o);
  5657. } else {
  5658. _set_tkpos(pos); //something else, so rollback and end
  5659. break;
  5660. }
  5661. }
  5662. /* Reduce the set set of expressions and place them in an operator tree, respecting precedence */
  5663. while (expression.size() > 1) {
  5664. int next_op = -1;
  5665. int min_priority = 0xFFFFF;
  5666. bool is_unary = false;
  5667. bool is_ternary = false;
  5668. for (int i = 0; i < expression.size(); i++) {
  5669. if (!expression[i].is_op) {
  5670. continue;
  5671. }
  5672. bool unary = false;
  5673. bool ternary = false;
  5674. Operator op = expression[i].op;
  5675. int priority;
  5676. switch (op) {
  5677. case OP_EQUAL:
  5678. priority = 8;
  5679. break;
  5680. case OP_NOT_EQUAL:
  5681. priority = 8;
  5682. break;
  5683. case OP_LESS:
  5684. priority = 7;
  5685. break;
  5686. case OP_LESS_EQUAL:
  5687. priority = 7;
  5688. break;
  5689. case OP_GREATER:
  5690. priority = 7;
  5691. break;
  5692. case OP_GREATER_EQUAL:
  5693. priority = 7;
  5694. break;
  5695. case OP_AND:
  5696. priority = 12;
  5697. break;
  5698. case OP_OR:
  5699. priority = 14;
  5700. break;
  5701. case OP_NOT:
  5702. priority = 3;
  5703. unary = true;
  5704. break;
  5705. case OP_NEGATE:
  5706. priority = 3;
  5707. unary = true;
  5708. break;
  5709. case OP_ADD:
  5710. priority = 5;
  5711. break;
  5712. case OP_SUB:
  5713. priority = 5;
  5714. break;
  5715. case OP_MUL:
  5716. priority = 4;
  5717. break;
  5718. case OP_DIV:
  5719. priority = 4;
  5720. break;
  5721. case OP_MOD:
  5722. priority = 4;
  5723. break;
  5724. case OP_SHIFT_LEFT:
  5725. priority = 6;
  5726. break;
  5727. case OP_SHIFT_RIGHT:
  5728. priority = 6;
  5729. break;
  5730. case OP_ASSIGN:
  5731. priority = 16;
  5732. break;
  5733. case OP_ASSIGN_ADD:
  5734. priority = 16;
  5735. break;
  5736. case OP_ASSIGN_SUB:
  5737. priority = 16;
  5738. break;
  5739. case OP_ASSIGN_MUL:
  5740. priority = 16;
  5741. break;
  5742. case OP_ASSIGN_DIV:
  5743. priority = 16;
  5744. break;
  5745. case OP_ASSIGN_MOD:
  5746. priority = 16;
  5747. break;
  5748. case OP_ASSIGN_SHIFT_LEFT:
  5749. priority = 16;
  5750. break;
  5751. case OP_ASSIGN_SHIFT_RIGHT:
  5752. priority = 16;
  5753. break;
  5754. case OP_ASSIGN_BIT_AND:
  5755. priority = 16;
  5756. break;
  5757. case OP_ASSIGN_BIT_OR:
  5758. priority = 16;
  5759. break;
  5760. case OP_ASSIGN_BIT_XOR:
  5761. priority = 16;
  5762. break;
  5763. case OP_BIT_AND:
  5764. priority = 9;
  5765. break;
  5766. case OP_BIT_OR:
  5767. priority = 11;
  5768. break;
  5769. case OP_BIT_XOR:
  5770. priority = 10;
  5771. break;
  5772. case OP_BIT_INVERT:
  5773. priority = 3;
  5774. unary = true;
  5775. break;
  5776. case OP_INCREMENT:
  5777. priority = 3;
  5778. unary = true;
  5779. break;
  5780. case OP_DECREMENT:
  5781. priority = 3;
  5782. unary = true;
  5783. break;
  5784. case OP_SELECT_IF:
  5785. priority = 15;
  5786. ternary = true;
  5787. break;
  5788. case OP_SELECT_ELSE:
  5789. priority = 15;
  5790. ternary = true;
  5791. break;
  5792. default:
  5793. ERR_FAIL_V(nullptr); //unexpected operator
  5794. }
  5795. #ifdef DEBUG_ENABLED
  5796. if (check_warnings && HAS_WARNING(ShaderWarning::FLOAT_COMPARISON_FLAG) && (op == OP_EQUAL || op == OP_NOT_EQUAL) &&
  5797. (!expression[i - 1].is_op && !expression[i + 1].is_op) &&
  5798. (expression[i - 1].node->get_datatype() == TYPE_FLOAT && expression[i + 1].node->get_datatype() == TYPE_FLOAT)) {
  5799. _add_line_warning(ShaderWarning::FLOAT_COMPARISON);
  5800. }
  5801. #endif // DEBUG_ENABLED
  5802. if (priority < min_priority) {
  5803. // < is used for left to right (default)
  5804. // <= is used for right to left
  5805. next_op = i;
  5806. min_priority = priority;
  5807. is_unary = unary;
  5808. is_ternary = ternary;
  5809. }
  5810. }
  5811. ERR_FAIL_COND_V(next_op == -1, nullptr);
  5812. // OK! create operator..
  5813. if (is_unary) {
  5814. int expr_pos = next_op;
  5815. while (expression[expr_pos].is_op) {
  5816. expr_pos++;
  5817. if (expr_pos == expression.size()) {
  5818. //can happen..
  5819. _set_error(RTR("Unexpected end of expression."));
  5820. return nullptr;
  5821. }
  5822. }
  5823. //consecutively do unary operators
  5824. for (int i = expr_pos - 1; i >= next_op; i--) {
  5825. OperatorNode *op = alloc_node<OperatorNode>();
  5826. op->op = expression[i].op;
  5827. if ((op->op == OP_INCREMENT || op->op == OP_DECREMENT) && !_validate_assign(expression[i + 1].node, p_function_info)) {
  5828. _set_error(RTR("Invalid use of increment/decrement operator in a constant expression."));
  5829. return nullptr;
  5830. }
  5831. op->arguments.push_back(expression[i + 1].node);
  5832. expression.write[i].is_op = false;
  5833. expression.write[i].node = op;
  5834. if (!_validate_operator(op, &op->return_cache, &op->return_array_size)) {
  5835. String at;
  5836. for (int j = 0; j < op->arguments.size(); j++) {
  5837. if (j > 0) {
  5838. at += ", ";
  5839. }
  5840. at += get_datatype_name(op->arguments[j]->get_datatype());
  5841. if (!op->arguments[j]->is_indexed() && op->arguments[j]->get_array_size() > 0) {
  5842. at += "[";
  5843. at += itos(op->arguments[j]->get_array_size());
  5844. at += "]";
  5845. }
  5846. }
  5847. _set_error(vformat(RTR("Invalid arguments to unary operator '%s': %s."), get_operator_text(op->op), at));
  5848. return nullptr;
  5849. }
  5850. expression.remove_at(i + 1);
  5851. }
  5852. } else if (is_ternary) {
  5853. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  5854. _set_parsing_error();
  5855. ERR_FAIL_V(nullptr);
  5856. }
  5857. if (next_op + 2 >= expression.size() || !expression[next_op + 2].is_op || expression[next_op + 2].op != OP_SELECT_ELSE) {
  5858. _set_error(RTR("Missing matching ':' for select operator."));
  5859. return nullptr;
  5860. }
  5861. OperatorNode *op = alloc_node<OperatorNode>();
  5862. op->op = expression[next_op].op;
  5863. op->arguments.push_back(expression[next_op - 1].node);
  5864. op->arguments.push_back(expression[next_op + 1].node);
  5865. op->arguments.push_back(expression[next_op + 3].node);
  5866. expression.write[next_op - 1].is_op = false;
  5867. expression.write[next_op - 1].node = op;
  5868. if (!_validate_operator(op, &op->return_cache, &op->return_array_size)) {
  5869. String at;
  5870. for (int i = 0; i < op->arguments.size(); i++) {
  5871. if (i > 0) {
  5872. at += ", ";
  5873. }
  5874. at += get_datatype_name(op->arguments[i]->get_datatype());
  5875. if (!op->arguments[i]->is_indexed() && op->arguments[i]->get_array_size() > 0) {
  5876. at += "[";
  5877. at += itos(op->arguments[i]->get_array_size());
  5878. at += "]";
  5879. }
  5880. }
  5881. _set_error(vformat(RTR("Invalid argument to ternary operator: '%s'."), at));
  5882. return nullptr;
  5883. }
  5884. for (int i = 0; i < 4; i++) {
  5885. expression.remove_at(next_op);
  5886. }
  5887. } else {
  5888. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  5889. _set_parsing_error();
  5890. ERR_FAIL_V(nullptr);
  5891. }
  5892. OperatorNode *op = alloc_node<OperatorNode>();
  5893. op->op = expression[next_op].op;
  5894. if (expression[next_op - 1].is_op) {
  5895. _set_parsing_error();
  5896. ERR_FAIL_V(nullptr);
  5897. }
  5898. if (_is_operator_assign(op->op)) {
  5899. String assign_message;
  5900. if (!_validate_assign(expression[next_op - 1].node, p_function_info, &assign_message)) {
  5901. _set_error(assign_message);
  5902. return nullptr;
  5903. }
  5904. }
  5905. if (expression[next_op + 1].is_op) {
  5906. // this is not invalid and can really appear
  5907. // but it becomes invalid anyway because no binary op
  5908. // can be followed by a unary op in a valid combination,
  5909. // due to how precedence works, unaries will always disappear first
  5910. _set_parsing_error();
  5911. }
  5912. op->arguments.push_back(expression[next_op - 1].node); //expression goes as left
  5913. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as right
  5914. expression.write[next_op - 1].node = op;
  5915. //replace all 3 nodes by this operator and make it an expression
  5916. if (!_validate_operator(op, &op->return_cache, &op->return_array_size)) {
  5917. String at;
  5918. for (int i = 0; i < op->arguments.size(); i++) {
  5919. if (i > 0) {
  5920. at += ", ";
  5921. }
  5922. if (op->arguments[i]->get_datatype() == TYPE_STRUCT) {
  5923. at += op->arguments[i]->get_datatype_name();
  5924. } else {
  5925. at += get_datatype_name(op->arguments[i]->get_datatype());
  5926. }
  5927. if (!op->arguments[i]->is_indexed() && op->arguments[i]->get_array_size() > 0) {
  5928. at += "[";
  5929. at += itos(op->arguments[i]->get_array_size());
  5930. at += "]";
  5931. }
  5932. }
  5933. _set_error(vformat(RTR("Invalid arguments to operator '%s': '%s'."), get_operator_text(op->op), at));
  5934. return nullptr;
  5935. }
  5936. expression.remove_at(next_op);
  5937. expression.remove_at(next_op);
  5938. }
  5939. }
  5940. return expression[0].node;
  5941. }
  5942. ShaderLanguage::Node *ShaderLanguage::_reduce_expression(BlockNode *p_block, ShaderLanguage::Node *p_node) {
  5943. if (p_node->type != Node::NODE_TYPE_OPERATOR) {
  5944. return p_node;
  5945. }
  5946. //for now only reduce simple constructors
  5947. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  5948. if (op->op == OP_CONSTRUCT) {
  5949. ERR_FAIL_COND_V(op->arguments[0]->type != Node::NODE_TYPE_VARIABLE, p_node);
  5950. DataType type = op->get_datatype();
  5951. DataType base = get_scalar_type(type);
  5952. int cardinality = get_cardinality(type);
  5953. Vector<ConstantNode::Value> values;
  5954. for (int i = 1; i < op->arguments.size(); i++) {
  5955. op->arguments.write[i] = _reduce_expression(p_block, op->arguments[i]);
  5956. if (op->arguments[i]->type == Node::NODE_TYPE_CONSTANT) {
  5957. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[i]);
  5958. if (get_scalar_type(cn->datatype) == base) {
  5959. for (int j = 0; j < cn->values.size(); j++) {
  5960. values.push_back(cn->values[j]);
  5961. }
  5962. } else if (get_scalar_type(cn->datatype) == cn->datatype) {
  5963. ConstantNode::Value v;
  5964. if (!convert_constant(cn, base, &v)) {
  5965. return p_node;
  5966. }
  5967. values.push_back(v);
  5968. } else {
  5969. return p_node;
  5970. }
  5971. } else {
  5972. return p_node;
  5973. }
  5974. }
  5975. if (values.size() == 1) {
  5976. if (type >= TYPE_MAT2 && type <= TYPE_MAT4) {
  5977. ConstantNode::Value value = values[0];
  5978. ConstantNode::Value zero;
  5979. zero.real = 0.0f;
  5980. int size = 2 + (type - TYPE_MAT2);
  5981. values.clear();
  5982. for (int i = 0; i < size; i++) {
  5983. for (int j = 0; j < size; j++) {
  5984. values.push_back(i == j ? value : zero);
  5985. }
  5986. }
  5987. } else {
  5988. ConstantNode::Value value = values[0];
  5989. for (int i = 1; i < cardinality; i++) {
  5990. values.push_back(value);
  5991. }
  5992. }
  5993. } else if (values.size() != cardinality) {
  5994. ERR_PRINT("Failed to reduce expression, values and cardinality mismatch.");
  5995. return p_node;
  5996. }
  5997. ConstantNode *cn = alloc_node<ConstantNode>();
  5998. cn->datatype = op->get_datatype();
  5999. cn->values = values;
  6000. return cn;
  6001. } else if (op->op == OP_NEGATE) {
  6002. op->arguments.write[0] = _reduce_expression(p_block, op->arguments[0]);
  6003. if (op->arguments[0]->type == Node::NODE_TYPE_CONSTANT) {
  6004. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[0]);
  6005. DataType base = get_scalar_type(cn->datatype);
  6006. Vector<ConstantNode::Value> values;
  6007. for (int i = 0; i < cn->values.size(); i++) {
  6008. ConstantNode::Value nv;
  6009. switch (base) {
  6010. case TYPE_BOOL: {
  6011. nv.boolean = !cn->values[i].boolean;
  6012. } break;
  6013. case TYPE_INT: {
  6014. nv.sint = -cn->values[i].sint;
  6015. } break;
  6016. case TYPE_UINT: {
  6017. // Intentionally wrap the unsigned int value, because GLSL does.
  6018. nv.uint = 0 - cn->values[i].uint;
  6019. } break;
  6020. case TYPE_FLOAT: {
  6021. nv.real = -cn->values[i].real;
  6022. } break;
  6023. default: {
  6024. }
  6025. }
  6026. values.push_back(nv);
  6027. }
  6028. cn->values = values;
  6029. return cn;
  6030. }
  6031. }
  6032. return p_node;
  6033. }
  6034. ShaderLanguage::Node *ShaderLanguage::_parse_and_reduce_expression(BlockNode *p_block, const FunctionInfo &p_function_info) {
  6035. ShaderLanguage::Node *expr = _parse_expression(p_block, p_function_info);
  6036. if (!expr) { //errored
  6037. return nullptr;
  6038. }
  6039. expr = _reduce_expression(p_block, expr);
  6040. return expr;
  6041. }
  6042. Error ShaderLanguage::_parse_block(BlockNode *p_block, const FunctionInfo &p_function_info, bool p_just_one, bool p_can_break, bool p_can_continue) {
  6043. while (true) {
  6044. TkPos pos = _get_tkpos();
  6045. Token tk = _get_token();
  6046. #ifdef DEBUG_ENABLED
  6047. Token next;
  6048. #endif // DEBUG_ENABLED
  6049. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_SWITCH) {
  6050. if (tk.type != TK_CF_CASE && tk.type != TK_CF_DEFAULT && tk.type != TK_CURLY_BRACKET_CLOSE) {
  6051. _set_error(vformat(RTR("A switch may only contain '%s' and '%s' blocks."), "case", "default"));
  6052. return ERR_PARSE_ERROR;
  6053. }
  6054. }
  6055. bool is_struct = shader->structs.has(tk.text);
  6056. bool is_var_init = false;
  6057. bool is_condition = false;
  6058. if (tk.type == TK_CURLY_BRACKET_CLOSE) { //end of block
  6059. if (p_just_one) {
  6060. _set_expected_error("}");
  6061. return ERR_PARSE_ERROR;
  6062. }
  6063. return OK;
  6064. } else if (tk.type == TK_CONST || is_token_precision(tk.type) || is_token_nonvoid_datatype(tk.type) || is_struct) {
  6065. is_var_init = true;
  6066. String struct_name = "";
  6067. if (is_struct) {
  6068. struct_name = tk.text;
  6069. #ifdef DEBUG_ENABLED
  6070. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_STRUCT_FLAG) && used_structs.has(struct_name)) {
  6071. used_structs[struct_name].used = true;
  6072. }
  6073. #endif // DEBUG_ENABLED
  6074. }
  6075. #ifdef DEBUG_ENABLED
  6076. uint32_t precision_flag = CF_PRECISION_MODIFIER;
  6077. keyword_completion_context = CF_DATATYPE;
  6078. if (!is_token_precision(tk.type)) {
  6079. if (!is_struct) {
  6080. keyword_completion_context |= precision_flag;
  6081. }
  6082. }
  6083. #endif // DEBUG_ENABLED
  6084. bool is_const = false;
  6085. if (tk.type == TK_CONST) {
  6086. is_const = true;
  6087. tk = _get_token();
  6088. if (!is_struct) {
  6089. is_struct = shader->structs.has(tk.text); // check again.
  6090. struct_name = tk.text;
  6091. }
  6092. }
  6093. DataPrecision precision = PRECISION_DEFAULT;
  6094. if (is_token_precision(tk.type)) {
  6095. precision = get_token_precision(tk.type);
  6096. tk = _get_token();
  6097. if (!is_struct) {
  6098. is_struct = shader->structs.has(tk.text); // check again.
  6099. }
  6100. #ifdef DEBUG_ENABLED
  6101. if (keyword_completion_context & precision_flag) {
  6102. keyword_completion_context ^= precision_flag;
  6103. }
  6104. #endif // DEBUG_ENABLED
  6105. }
  6106. #ifdef DEBUG_ENABLED
  6107. if (is_const && _lookup_next(next)) {
  6108. if (is_token_precision(next.type)) {
  6109. keyword_completion_context = CF_UNSPECIFIED;
  6110. }
  6111. if (is_token_datatype(next.type)) {
  6112. keyword_completion_context ^= CF_DATATYPE;
  6113. }
  6114. }
  6115. #endif // DEBUG_ENABLED
  6116. if (precision != PRECISION_DEFAULT) {
  6117. if (!is_token_nonvoid_datatype(tk.type)) {
  6118. _set_error(RTR("Expected variable type after precision modifier."));
  6119. return ERR_PARSE_ERROR;
  6120. }
  6121. }
  6122. if (!is_struct) {
  6123. if (!is_token_variable_datatype(tk.type)) {
  6124. _set_error(RTR("Invalid variable type (samplers are not allowed)."));
  6125. return ERR_PARSE_ERROR;
  6126. }
  6127. }
  6128. DataType type = is_struct ? TYPE_STRUCT : get_token_datatype(tk.type);
  6129. if (precision != PRECISION_DEFAULT && _validate_precision(type, precision) != OK) {
  6130. return ERR_PARSE_ERROR;
  6131. }
  6132. #ifdef DEBUG_ENABLED
  6133. keyword_completion_context = CF_UNSPECIFIED;
  6134. #endif // DEBUG_ENABLED
  6135. int array_size = 0;
  6136. bool fixed_array_size = false;
  6137. bool first = true;
  6138. VariableDeclarationNode *vdnode = alloc_node<VariableDeclarationNode>();
  6139. vdnode->precision = precision;
  6140. if (is_struct) {
  6141. vdnode->struct_name = struct_name;
  6142. vdnode->datatype = TYPE_STRUCT;
  6143. } else {
  6144. vdnode->datatype = type;
  6145. };
  6146. vdnode->is_const = is_const;
  6147. do {
  6148. bool unknown_size = false;
  6149. VariableDeclarationNode::Declaration decl;
  6150. tk = _get_token();
  6151. if (first) {
  6152. first = false;
  6153. if (tk.type != TK_IDENTIFIER && tk.type != TK_BRACKET_OPEN) {
  6154. _set_error(RTR("Expected an identifier or '[' after type."));
  6155. return ERR_PARSE_ERROR;
  6156. }
  6157. if (tk.type == TK_BRACKET_OPEN) {
  6158. Error error = _parse_array_size(p_block, p_function_info, false, &decl.size_expression, &array_size, &unknown_size);
  6159. if (error != OK) {
  6160. return error;
  6161. }
  6162. decl.size = array_size;
  6163. fixed_array_size = true;
  6164. tk = _get_token();
  6165. }
  6166. }
  6167. if (tk.type != TK_IDENTIFIER) {
  6168. _set_error(RTR("Expected an identifier."));
  6169. return ERR_PARSE_ERROR;
  6170. }
  6171. StringName name = tk.text;
  6172. ShaderLanguage::IdentifierType itype;
  6173. if (_find_identifier(p_block, true, p_function_info, name, (ShaderLanguage::DataType *)nullptr, &itype)) {
  6174. if (itype != IDENTIFIER_FUNCTION) {
  6175. _set_redefinition_error(String(name));
  6176. return ERR_PARSE_ERROR;
  6177. }
  6178. }
  6179. decl.name = name;
  6180. #ifdef DEBUG_ENABLED
  6181. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_LOCAL_VARIABLE_FLAG) && p_block) {
  6182. FunctionNode *parent_function = nullptr;
  6183. {
  6184. BlockNode *block = p_block;
  6185. while (block && !block->parent_function) {
  6186. block = block->parent_block;
  6187. }
  6188. parent_function = block->parent_function;
  6189. }
  6190. if (parent_function) {
  6191. StringName func_name = parent_function->name;
  6192. if (!used_local_vars.has(func_name)) {
  6193. used_local_vars.insert(func_name, HashMap<StringName, Usage>());
  6194. }
  6195. used_local_vars[func_name].insert(name, Usage(tk_line));
  6196. }
  6197. }
  6198. #endif // DEBUG_ENABLED
  6199. is_const_decl = is_const;
  6200. BlockNode::Variable var;
  6201. var.type = type;
  6202. var.precision = precision;
  6203. var.line = tk_line;
  6204. var.array_size = array_size;
  6205. var.is_const = is_const;
  6206. var.struct_name = struct_name;
  6207. tk = _get_token();
  6208. if (tk.type == TK_BRACKET_OPEN) {
  6209. Error error = _parse_array_size(p_block, p_function_info, false, &decl.size_expression, &var.array_size, &unknown_size);
  6210. if (error != OK) {
  6211. return error;
  6212. }
  6213. decl.size = var.array_size;
  6214. array_size = var.array_size;
  6215. tk = _get_token();
  6216. }
  6217. #ifdef DEBUG_ENABLED
  6218. if (var.type == DataType::TYPE_BOOL) {
  6219. keyword_completion_context = CF_BOOLEAN;
  6220. }
  6221. #endif // DEBUG_ENABLED
  6222. if (var.array_size > 0 || unknown_size) {
  6223. bool full_def = false;
  6224. if (tk.type == TK_OP_ASSIGN) {
  6225. TkPos prev_pos = _get_tkpos();
  6226. tk = _get_token();
  6227. if (tk.type == TK_IDENTIFIER) { // a function call array initialization
  6228. _set_tkpos(prev_pos);
  6229. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  6230. if (!n) {
  6231. _set_error(RTR("Expected array initializer."));
  6232. return ERR_PARSE_ERROR;
  6233. } else {
  6234. if (unknown_size) {
  6235. decl.size = n->get_array_size();
  6236. var.array_size = n->get_array_size();
  6237. }
  6238. if (!_compare_datatypes(var.type, var.struct_name, var.array_size, n->get_datatype(), n->get_datatype_name(), n->get_array_size())) {
  6239. return ERR_PARSE_ERROR;
  6240. }
  6241. decl.single_expression = true;
  6242. decl.initializer.push_back(n);
  6243. }
  6244. tk = _get_token();
  6245. } else {
  6246. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  6247. if (unknown_size) {
  6248. _set_expected_error("{");
  6249. return ERR_PARSE_ERROR;
  6250. }
  6251. full_def = true;
  6252. DataPrecision precision2 = PRECISION_DEFAULT;
  6253. if (is_token_precision(tk.type)) {
  6254. precision2 = get_token_precision(tk.type);
  6255. tk = _get_token();
  6256. if (!is_token_nonvoid_datatype(tk.type)) {
  6257. _set_error(RTR("Expected data type after precision modifier."));
  6258. return ERR_PARSE_ERROR;
  6259. }
  6260. }
  6261. DataType type2;
  6262. StringName struct_name2 = "";
  6263. if (shader->structs.has(tk.text)) {
  6264. type2 = TYPE_STRUCT;
  6265. struct_name2 = tk.text;
  6266. } else {
  6267. if (!is_token_variable_datatype(tk.type)) {
  6268. _set_error(RTR("Invalid data type for the array."));
  6269. return ERR_PARSE_ERROR;
  6270. }
  6271. type2 = get_token_datatype(tk.type);
  6272. }
  6273. if (precision2 != PRECISION_DEFAULT && _validate_precision(type2, precision2) != OK) {
  6274. return ERR_PARSE_ERROR;
  6275. }
  6276. int array_size2 = 0;
  6277. tk = _get_token();
  6278. if (tk.type == TK_BRACKET_OPEN) {
  6279. bool is_unknown_size = false;
  6280. Error error = _parse_array_size(p_block, p_function_info, false, nullptr, &array_size2, &is_unknown_size);
  6281. if (error != OK) {
  6282. return error;
  6283. }
  6284. if (is_unknown_size) {
  6285. array_size2 = var.array_size;
  6286. }
  6287. tk = _get_token();
  6288. } else {
  6289. _set_expected_error("[");
  6290. return ERR_PARSE_ERROR;
  6291. }
  6292. if (precision != precision2 || type != type2 || struct_name != struct_name2 || var.array_size != array_size2) {
  6293. String from;
  6294. if (precision2 != PRECISION_DEFAULT) {
  6295. from += get_precision_name(precision2);
  6296. from += " ";
  6297. }
  6298. if (type2 == TYPE_STRUCT) {
  6299. from += struct_name2;
  6300. } else {
  6301. from += get_datatype_name(type2);
  6302. }
  6303. from += "[";
  6304. from += itos(array_size2);
  6305. from += "]'";
  6306. String to;
  6307. if (precision != PRECISION_DEFAULT) {
  6308. to += get_precision_name(precision);
  6309. to += " ";
  6310. }
  6311. if (type == TYPE_STRUCT) {
  6312. to += struct_name;
  6313. } else {
  6314. to += get_datatype_name(type);
  6315. }
  6316. to += "[";
  6317. to += itos(var.array_size);
  6318. to += "]'";
  6319. _set_error(vformat(RTR("Cannot convert from '%s' to '%s'."), from, to));
  6320. return ERR_PARSE_ERROR;
  6321. }
  6322. }
  6323. bool curly = tk.type == TK_CURLY_BRACKET_OPEN;
  6324. if (unknown_size) {
  6325. if (!curly) {
  6326. _set_expected_error("{");
  6327. return ERR_PARSE_ERROR;
  6328. }
  6329. } else {
  6330. if (full_def) {
  6331. if (curly) {
  6332. _set_expected_error("(");
  6333. return ERR_PARSE_ERROR;
  6334. }
  6335. }
  6336. }
  6337. if (tk.type == TK_PARENTHESIS_OPEN || curly) { // initialization
  6338. while (true) {
  6339. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  6340. if (!n) {
  6341. return ERR_PARSE_ERROR;
  6342. }
  6343. if (is_const && n->type == Node::NODE_TYPE_OPERATOR && static_cast<OperatorNode *>(n)->op == OP_CALL) {
  6344. _set_error(RTR("Expected a constant expression."));
  6345. return ERR_PARSE_ERROR;
  6346. }
  6347. if (!_compare_datatypes(var.type, struct_name, 0, n->get_datatype(), n->get_datatype_name(), 0)) {
  6348. return ERR_PARSE_ERROR;
  6349. }
  6350. tk = _get_token();
  6351. if (tk.type == TK_COMMA) {
  6352. decl.initializer.push_back(n);
  6353. continue;
  6354. } else if (!curly && tk.type == TK_PARENTHESIS_CLOSE) {
  6355. decl.initializer.push_back(n);
  6356. break;
  6357. } else if (curly && tk.type == TK_CURLY_BRACKET_CLOSE) {
  6358. decl.initializer.push_back(n);
  6359. break;
  6360. } else {
  6361. if (curly) {
  6362. _set_expected_error("}", ",");
  6363. } else {
  6364. _set_expected_error(")", ",");
  6365. }
  6366. return ERR_PARSE_ERROR;
  6367. }
  6368. }
  6369. if (unknown_size) {
  6370. decl.size = decl.initializer.size();
  6371. var.array_size = decl.initializer.size();
  6372. } else if (decl.initializer.size() != var.array_size) {
  6373. _set_error(RTR("Array size mismatch."));
  6374. return ERR_PARSE_ERROR;
  6375. }
  6376. tk = _get_token();
  6377. }
  6378. }
  6379. } else {
  6380. if (unknown_size) {
  6381. _set_error(RTR("Expected array initialization."));
  6382. return ERR_PARSE_ERROR;
  6383. }
  6384. if (is_const) {
  6385. _set_error(RTR("Expected initialization of constant."));
  6386. return ERR_PARSE_ERROR;
  6387. }
  6388. }
  6389. array_size = var.array_size;
  6390. } else if (tk.type == TK_OP_ASSIGN) {
  6391. //variable created with assignment! must parse an expression
  6392. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  6393. if (!n) {
  6394. return ERR_PARSE_ERROR;
  6395. }
  6396. if (is_const && n->type == Node::NODE_TYPE_OPERATOR && static_cast<OperatorNode *>(n)->op == OP_CALL) {
  6397. OperatorNode *op = static_cast<OperatorNode *>(n);
  6398. for (int i = 1; i < op->arguments.size(); i++) {
  6399. if (!_check_node_constness(op->arguments[i])) {
  6400. _set_error(vformat(RTR("Expected constant expression for argument %d of function call after '='."), i - 1));
  6401. return ERR_PARSE_ERROR;
  6402. }
  6403. }
  6404. }
  6405. if (n->type == Node::NODE_TYPE_CONSTANT) {
  6406. ConstantNode *const_node = static_cast<ConstantNode *>(n);
  6407. if (const_node && const_node->values.size() == 1) {
  6408. var.value = const_node->values[0];
  6409. }
  6410. }
  6411. if (!_compare_datatypes(var.type, var.struct_name, var.array_size, n->get_datatype(), n->get_datatype_name(), n->get_array_size())) {
  6412. return ERR_PARSE_ERROR;
  6413. }
  6414. decl.initializer.push_back(n);
  6415. tk = _get_token();
  6416. } else {
  6417. if (is_const) {
  6418. _set_error(RTR("Expected initialization of constant."));
  6419. return ERR_PARSE_ERROR;
  6420. }
  6421. }
  6422. vdnode->declarations.push_back(decl);
  6423. p_block->variables[name] = var;
  6424. is_const_decl = false;
  6425. if (!fixed_array_size) {
  6426. array_size = 0;
  6427. }
  6428. if (tk.type == TK_SEMICOLON) {
  6429. break;
  6430. } else if (tk.type != TK_COMMA) {
  6431. _set_expected_error(",", ";");
  6432. return ERR_PARSE_ERROR;
  6433. }
  6434. } while (tk.type == TK_COMMA); //another variable
  6435. #ifdef DEBUG_ENABLED
  6436. keyword_completion_context = CF_BLOCK;
  6437. #endif // DEBUG_ENABLED
  6438. p_block->statements.push_back(static_cast<Node *>(vdnode));
  6439. } else if (tk.type == TK_CURLY_BRACKET_OPEN) {
  6440. //a sub block, just because..
  6441. BlockNode *block = alloc_node<BlockNode>();
  6442. block->parent_block = p_block;
  6443. if (_parse_block(block, p_function_info, false, p_can_break, p_can_continue) != OK) {
  6444. return ERR_PARSE_ERROR;
  6445. }
  6446. p_block->statements.push_back(block);
  6447. } else if (tk.type == TK_CF_IF) {
  6448. //if () {}
  6449. tk = _get_token();
  6450. if (tk.type != TK_PARENTHESIS_OPEN) {
  6451. _set_expected_after_error("(", "if");
  6452. return ERR_PARSE_ERROR;
  6453. }
  6454. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6455. cf->flow_op = FLOW_OP_IF;
  6456. #ifdef DEBUG_ENABLED
  6457. keyword_completion_context = CF_IF_DECL;
  6458. #endif // DEBUG_ENABLED
  6459. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  6460. if (!n) {
  6461. return ERR_PARSE_ERROR;
  6462. }
  6463. #ifdef DEBUG_ENABLED
  6464. keyword_completion_context = CF_BLOCK;
  6465. #endif // DEBUG_ENABLED
  6466. if (n->get_datatype() != TYPE_BOOL) {
  6467. _set_error(RTR("Expected a boolean expression."));
  6468. return ERR_PARSE_ERROR;
  6469. }
  6470. tk = _get_token();
  6471. if (tk.type != TK_PARENTHESIS_CLOSE) {
  6472. _set_expected_error(")");
  6473. return ERR_PARSE_ERROR;
  6474. }
  6475. BlockNode *block = alloc_node<BlockNode>();
  6476. block->parent_block = p_block;
  6477. cf->expressions.push_back(n);
  6478. cf->blocks.push_back(block);
  6479. p_block->statements.push_back(cf);
  6480. Error err = _parse_block(block, p_function_info, true, p_can_break, p_can_continue);
  6481. if (err) {
  6482. return err;
  6483. }
  6484. pos = _get_tkpos();
  6485. tk = _get_token();
  6486. if (tk.type == TK_CF_ELSE) {
  6487. block = alloc_node<BlockNode>();
  6488. block->parent_block = p_block;
  6489. cf->blocks.push_back(block);
  6490. err = _parse_block(block, p_function_info, true, p_can_break, p_can_continue);
  6491. if (err) {
  6492. return err;
  6493. }
  6494. } else {
  6495. _set_tkpos(pos); //rollback
  6496. }
  6497. } else if (tk.type == TK_CF_SWITCH) {
  6498. // switch() {}
  6499. tk = _get_token();
  6500. if (tk.type != TK_PARENTHESIS_OPEN) {
  6501. _set_expected_after_error("(", "switch");
  6502. return ERR_PARSE_ERROR;
  6503. }
  6504. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6505. cf->flow_op = FLOW_OP_SWITCH;
  6506. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  6507. if (!n) {
  6508. return ERR_PARSE_ERROR;
  6509. }
  6510. {
  6511. const ShaderLanguage::DataType switch_type = n->get_datatype();
  6512. if (switch_type != TYPE_INT && switch_type != TYPE_UINT) {
  6513. _set_error(RTR("Expected an integer expression."));
  6514. return ERR_PARSE_ERROR;
  6515. }
  6516. }
  6517. tk = _get_token();
  6518. if (tk.type != TK_PARENTHESIS_CLOSE) {
  6519. _set_expected_error(")");
  6520. return ERR_PARSE_ERROR;
  6521. }
  6522. tk = _get_token();
  6523. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  6524. _set_expected_after_error("{", "switch");
  6525. return ERR_PARSE_ERROR;
  6526. }
  6527. BlockNode *switch_block = alloc_node<BlockNode>();
  6528. switch_block->block_type = BlockNode::BLOCK_TYPE_SWITCH;
  6529. switch_block->parent_block = p_block;
  6530. cf->expressions.push_back(n);
  6531. cf->blocks.push_back(switch_block);
  6532. p_block->statements.push_back(cf);
  6533. int prev_type = TK_CF_CASE;
  6534. while (true) { // Go-through multiple cases.
  6535. if (_parse_block(switch_block, p_function_info, true, true, false) != OK) {
  6536. return ERR_PARSE_ERROR;
  6537. }
  6538. pos = _get_tkpos();
  6539. tk = _get_token();
  6540. if (tk.type == TK_CF_CASE || tk.type == TK_CF_DEFAULT) {
  6541. if (prev_type == TK_CF_DEFAULT) {
  6542. if (tk.type == TK_CF_CASE) {
  6543. _set_error(RTR("Cases must be defined before default case."));
  6544. return ERR_PARSE_ERROR;
  6545. } else if (prev_type == TK_CF_DEFAULT) {
  6546. _set_error(RTR("Default case must be defined only once."));
  6547. return ERR_PARSE_ERROR;
  6548. }
  6549. }
  6550. prev_type = tk.type;
  6551. _set_tkpos(pos);
  6552. continue;
  6553. } else {
  6554. HashSet<int> constants;
  6555. for (int i = 0; i < switch_block->statements.size(); i++) { // Checks for duplicates.
  6556. ControlFlowNode *flow = static_cast<ControlFlowNode *>(switch_block->statements[i]);
  6557. if (flow) {
  6558. if (flow->flow_op == FLOW_OP_CASE) {
  6559. if (flow->expressions[0]->type == Node::NODE_TYPE_CONSTANT) {
  6560. ConstantNode *cn = static_cast<ConstantNode *>(flow->expressions[0]);
  6561. if (!cn || cn->values.is_empty()) {
  6562. return ERR_PARSE_ERROR;
  6563. }
  6564. if (constants.has(cn->values[0].sint)) {
  6565. _set_error(vformat(RTR("Duplicated case label: %d."), cn->values[0].sint));
  6566. return ERR_PARSE_ERROR;
  6567. }
  6568. constants.insert(cn->values[0].sint);
  6569. } else if (flow->expressions[0]->type == Node::NODE_TYPE_VARIABLE) {
  6570. VariableNode *vn = static_cast<VariableNode *>(flow->expressions[0]);
  6571. if (!vn) {
  6572. return ERR_PARSE_ERROR;
  6573. }
  6574. ConstantNode::Value v;
  6575. _find_identifier(p_block, false, p_function_info, vn->name, nullptr, nullptr, nullptr, nullptr, nullptr, &v);
  6576. if (constants.has(v.sint)) {
  6577. _set_error(vformat(RTR("Duplicated case label: %d."), v.sint));
  6578. return ERR_PARSE_ERROR;
  6579. }
  6580. constants.insert(v.sint);
  6581. }
  6582. } else if (flow->flow_op == FLOW_OP_DEFAULT) {
  6583. continue;
  6584. } else {
  6585. return ERR_PARSE_ERROR;
  6586. }
  6587. } else {
  6588. return ERR_PARSE_ERROR;
  6589. }
  6590. }
  6591. break;
  6592. }
  6593. }
  6594. } else if (tk.type == TK_CF_CASE) {
  6595. // case x : break; | return;
  6596. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_CASE) {
  6597. _set_tkpos(pos);
  6598. return OK;
  6599. }
  6600. if (!p_block || (p_block->block_type != BlockNode::BLOCK_TYPE_SWITCH)) {
  6601. _set_error(vformat(RTR("'%s' must be placed within a '%s' block."), "case", "switch"));
  6602. return ERR_PARSE_ERROR;
  6603. }
  6604. tk = _get_token();
  6605. int sign = 1;
  6606. if (tk.type == TK_OP_SUB) {
  6607. sign = -1;
  6608. tk = _get_token();
  6609. }
  6610. Node *n = nullptr;
  6611. if (!tk.is_integer_constant()) {
  6612. bool correct_constant_expression = false;
  6613. DataType data_type;
  6614. if (tk.type == TK_IDENTIFIER) {
  6615. bool is_const;
  6616. _find_identifier(p_block, false, p_function_info, tk.text, &data_type, nullptr, &is_const);
  6617. if (is_const) {
  6618. if (data_type == TYPE_INT) {
  6619. correct_constant_expression = true;
  6620. }
  6621. }
  6622. }
  6623. if (!correct_constant_expression) {
  6624. _set_error(RTR("Expected an integer constant."));
  6625. return ERR_PARSE_ERROR;
  6626. }
  6627. VariableNode *vn = alloc_node<VariableNode>();
  6628. vn->name = tk.text;
  6629. n = vn;
  6630. } else {
  6631. ConstantNode::Value v;
  6632. if (tk.type == TK_UINT_CONSTANT) {
  6633. v.uint = (uint32_t)tk.constant;
  6634. } else {
  6635. v.sint = (int)tk.constant * sign;
  6636. }
  6637. ConstantNode *cn = alloc_node<ConstantNode>();
  6638. cn->values.push_back(v);
  6639. cn->datatype = (tk.type == TK_UINT_CONSTANT ? TYPE_UINT : TYPE_INT);
  6640. n = cn;
  6641. }
  6642. tk = _get_token();
  6643. if (tk.type != TK_COLON) {
  6644. _set_expected_error(":");
  6645. return ERR_PARSE_ERROR;
  6646. }
  6647. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6648. cf->flow_op = FLOW_OP_CASE;
  6649. BlockNode *case_block = alloc_node<BlockNode>();
  6650. case_block->block_type = BlockNode::BLOCK_TYPE_CASE;
  6651. case_block->parent_block = p_block;
  6652. cf->expressions.push_back(n);
  6653. cf->blocks.push_back(case_block);
  6654. p_block->statements.push_back(cf);
  6655. Error err = _parse_block(case_block, p_function_info, false, true, false);
  6656. if (err) {
  6657. return err;
  6658. }
  6659. return OK;
  6660. } else if (tk.type == TK_CF_DEFAULT) {
  6661. if (p_block && p_block->block_type == BlockNode::BLOCK_TYPE_CASE) {
  6662. _set_tkpos(pos);
  6663. return OK;
  6664. }
  6665. if (!p_block || (p_block->block_type != BlockNode::BLOCK_TYPE_SWITCH)) {
  6666. _set_error(vformat(RTR("'%s' must be placed within a '%s' block."), "default", "switch"));
  6667. return ERR_PARSE_ERROR;
  6668. }
  6669. tk = _get_token();
  6670. if (tk.type != TK_COLON) {
  6671. _set_expected_error(":");
  6672. return ERR_PARSE_ERROR;
  6673. }
  6674. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6675. cf->flow_op = FLOW_OP_DEFAULT;
  6676. BlockNode *default_block = alloc_node<BlockNode>();
  6677. default_block->block_type = BlockNode::BLOCK_TYPE_DEFAULT;
  6678. default_block->parent_block = p_block;
  6679. cf->blocks.push_back(default_block);
  6680. p_block->statements.push_back(cf);
  6681. Error err = _parse_block(default_block, p_function_info, false, true, false);
  6682. if (err) {
  6683. return err;
  6684. }
  6685. return OK;
  6686. } else if (tk.type == TK_CF_DO || tk.type == TK_CF_WHILE) {
  6687. // do {} while()
  6688. // while() {}
  6689. bool is_do = tk.type == TK_CF_DO;
  6690. BlockNode *do_block = nullptr;
  6691. if (is_do) {
  6692. do_block = alloc_node<BlockNode>();
  6693. do_block->parent_block = p_block;
  6694. Error err = _parse_block(do_block, p_function_info, true, true, true);
  6695. if (err) {
  6696. return err;
  6697. }
  6698. tk = _get_token();
  6699. if (tk.type != TK_CF_WHILE) {
  6700. _set_expected_after_error("while", "do");
  6701. return ERR_PARSE_ERROR;
  6702. }
  6703. }
  6704. tk = _get_token();
  6705. if (tk.type != TK_PARENTHESIS_OPEN) {
  6706. _set_expected_after_error("(", "while");
  6707. return ERR_PARSE_ERROR;
  6708. }
  6709. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6710. if (is_do) {
  6711. cf->flow_op = FLOW_OP_DO;
  6712. } else {
  6713. cf->flow_op = FLOW_OP_WHILE;
  6714. }
  6715. Node *n = _parse_and_reduce_expression(p_block, p_function_info);
  6716. if (!n) {
  6717. return ERR_PARSE_ERROR;
  6718. }
  6719. tk = _get_token();
  6720. if (tk.type != TK_PARENTHESIS_CLOSE) {
  6721. _set_expected_error(")");
  6722. return ERR_PARSE_ERROR;
  6723. }
  6724. if (!is_do) {
  6725. BlockNode *block = alloc_node<BlockNode>();
  6726. block->parent_block = p_block;
  6727. cf->expressions.push_back(n);
  6728. cf->blocks.push_back(block);
  6729. p_block->statements.push_back(cf);
  6730. Error err = _parse_block(block, p_function_info, true, true, true);
  6731. if (err) {
  6732. return err;
  6733. }
  6734. } else {
  6735. cf->expressions.push_back(n);
  6736. cf->blocks.push_back(do_block);
  6737. p_block->statements.push_back(cf);
  6738. tk = _get_token();
  6739. if (tk.type != TK_SEMICOLON) {
  6740. _set_expected_error(";");
  6741. return ERR_PARSE_ERROR;
  6742. }
  6743. }
  6744. } else if (tk.type == TK_CF_FOR) {
  6745. // for() {}
  6746. tk = _get_token();
  6747. if (tk.type != TK_PARENTHESIS_OPEN) {
  6748. _set_expected_after_error("(", "for");
  6749. return ERR_PARSE_ERROR;
  6750. }
  6751. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  6752. cf->flow_op = FLOW_OP_FOR;
  6753. BlockNode *init_block = alloc_node<BlockNode>();
  6754. init_block->block_type = BlockNode::BLOCK_TYPE_FOR_INIT;
  6755. init_block->parent_block = p_block;
  6756. init_block->single_statement = true;
  6757. cf->blocks.push_back(init_block);
  6758. #ifdef DEBUG_ENABLED
  6759. keyword_completion_context = CF_DATATYPE;
  6760. #endif // DEBUG_ENABLED
  6761. Error err = _parse_block(init_block, p_function_info, true, false, false);
  6762. if (err != OK) {
  6763. return err;
  6764. }
  6765. #ifdef DEBUG_ENABLED
  6766. keyword_completion_context = CF_UNSPECIFIED;
  6767. #endif // DEBUG_ENABLED
  6768. BlockNode *condition_block = alloc_node<BlockNode>();
  6769. condition_block->block_type = BlockNode::BLOCK_TYPE_FOR_CONDITION;
  6770. condition_block->parent_block = init_block;
  6771. condition_block->single_statement = true;
  6772. condition_block->use_comma_between_statements = true;
  6773. cf->blocks.push_back(condition_block);
  6774. err = _parse_block(condition_block, p_function_info, true, false, false);
  6775. if (err != OK) {
  6776. return err;
  6777. }
  6778. BlockNode *expression_block = alloc_node<BlockNode>();
  6779. expression_block->block_type = BlockNode::BLOCK_TYPE_FOR_EXPRESSION;
  6780. expression_block->parent_block = init_block;
  6781. expression_block->single_statement = true;
  6782. expression_block->use_comma_between_statements = true;
  6783. cf->blocks.push_back(expression_block);
  6784. err = _parse_block(expression_block, p_function_info, true, false, false);
  6785. if (err != OK) {
  6786. return err;
  6787. }
  6788. BlockNode *block = alloc_node<BlockNode>();
  6789. block->parent_block = init_block;
  6790. cf->blocks.push_back(block);
  6791. p_block->statements.push_back(cf);
  6792. #ifdef DEBUG_ENABLED
  6793. keyword_completion_context = CF_BLOCK;
  6794. #endif // DEBUG_ENABLED
  6795. err = _parse_block(block, p_function_info, true, true, true);
  6796. if (err != OK) {
  6797. return err;
  6798. }
  6799. } else if (tk.type == TK_CF_RETURN) {
  6800. //check return type
  6801. BlockNode *b = p_block;
  6802. while (b && !b->parent_function) {
  6803. b = b->parent_block;
  6804. }
  6805. if (!b) {
  6806. _set_parsing_error();
  6807. return ERR_BUG;
  6808. }
  6809. if (b->parent_function && p_function_info.main_function) {
  6810. _set_error(vformat(RTR("Using '%s' in the '%s' processor function is incorrect."), "return", b->parent_function->name));
  6811. return ERR_PARSE_ERROR;
  6812. }
  6813. String return_struct_name = String(b->parent_function->return_struct_name);
  6814. String array_size_string;
  6815. if (b->parent_function->return_array_size > 0) {
  6816. array_size_string = "[" + itos(b->parent_function->return_array_size) + "]";
  6817. }
  6818. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  6819. flow->flow_op = FLOW_OP_RETURN;
  6820. pos = _get_tkpos();
  6821. tk = _get_token();
  6822. if (tk.type == TK_SEMICOLON) {
  6823. //all is good
  6824. if (b->parent_function->return_type != TYPE_VOID) {
  6825. _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));
  6826. return ERR_PARSE_ERROR;
  6827. }
  6828. } else {
  6829. _set_tkpos(pos); //rollback, wants expression
  6830. #ifdef DEBUG_ENABLED
  6831. if (b->parent_function->return_type == DataType::TYPE_BOOL) {
  6832. keyword_completion_context = CF_BOOLEAN;
  6833. }
  6834. #endif // DEBUG_ENABLED
  6835. Node *expr = _parse_and_reduce_expression(p_block, p_function_info);
  6836. if (!expr) {
  6837. return ERR_PARSE_ERROR;
  6838. }
  6839. 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()) {
  6840. _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));
  6841. return ERR_PARSE_ERROR;
  6842. }
  6843. tk = _get_token();
  6844. if (tk.type != TK_SEMICOLON) {
  6845. _set_expected_after_error(";", "return");
  6846. return ERR_PARSE_ERROR;
  6847. }
  6848. #ifdef DEBUG_ENABLED
  6849. if (b->parent_function->return_type == DataType::TYPE_BOOL) {
  6850. keyword_completion_context = CF_BLOCK;
  6851. }
  6852. #endif // DEBUG_ENABLED
  6853. flow->expressions.push_back(expr);
  6854. }
  6855. p_block->statements.push_back(flow);
  6856. BlockNode *block = p_block;
  6857. while (block) {
  6858. if (block->block_type == BlockNode::BLOCK_TYPE_CASE || block->block_type == BlockNode::BLOCK_TYPE_DEFAULT) {
  6859. return OK;
  6860. }
  6861. block = block->parent_block;
  6862. }
  6863. } else if (tk.type == TK_CF_DISCARD) {
  6864. //check return type
  6865. BlockNode *b = p_block;
  6866. while (b && !b->parent_function) {
  6867. b = b->parent_block;
  6868. }
  6869. if (!b) {
  6870. _set_parsing_error();
  6871. return ERR_BUG;
  6872. }
  6873. if (!b->parent_function->can_discard) {
  6874. _set_error(vformat(RTR("Use of '%s' is not allowed here."), "discard"));
  6875. return ERR_PARSE_ERROR;
  6876. }
  6877. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  6878. flow->flow_op = FLOW_OP_DISCARD;
  6879. pos = _get_tkpos();
  6880. tk = _get_token();
  6881. if (tk.type != TK_SEMICOLON) {
  6882. _set_expected_after_error(";", "discard");
  6883. return ERR_PARSE_ERROR;
  6884. }
  6885. p_block->statements.push_back(flow);
  6886. } else if (tk.type == TK_CF_BREAK) {
  6887. if (!p_can_break) {
  6888. _set_error(vformat(RTR("'%s' is not allowed outside of a loop or '%s' statement."), "break", "switch"));
  6889. return ERR_PARSE_ERROR;
  6890. }
  6891. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  6892. flow->flow_op = FLOW_OP_BREAK;
  6893. pos = _get_tkpos();
  6894. tk = _get_token();
  6895. if (tk.type != TK_SEMICOLON) {
  6896. _set_expected_after_error(";", "break");
  6897. return ERR_PARSE_ERROR;
  6898. }
  6899. p_block->statements.push_back(flow);
  6900. BlockNode *block = p_block;
  6901. while (block) {
  6902. if (block->block_type == BlockNode::BLOCK_TYPE_CASE || block->block_type == BlockNode::BLOCK_TYPE_DEFAULT) {
  6903. return OK;
  6904. }
  6905. block = block->parent_block;
  6906. }
  6907. } else if (tk.type == TK_CF_CONTINUE) {
  6908. if (!p_can_continue) {
  6909. _set_error(vformat(RTR("'%s' is not allowed outside of a loop."), "continue"));
  6910. return ERR_PARSE_ERROR;
  6911. }
  6912. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  6913. flow->flow_op = FLOW_OP_CONTINUE;
  6914. pos = _get_tkpos();
  6915. tk = _get_token();
  6916. if (tk.type != TK_SEMICOLON) {
  6917. //all is good
  6918. _set_expected_after_error(";", "continue");
  6919. return ERR_PARSE_ERROR;
  6920. }
  6921. p_block->statements.push_back(flow);
  6922. } else {
  6923. //nothing else, so expression
  6924. _set_tkpos(pos); //rollback
  6925. Node *expr = _parse_and_reduce_expression(p_block, p_function_info);
  6926. if (!expr) {
  6927. return ERR_PARSE_ERROR;
  6928. }
  6929. is_condition = expr->type == Node::NODE_TYPE_OPERATOR && expr->get_datatype() == TYPE_BOOL;
  6930. if (expr->type == Node::NODE_TYPE_OPERATOR) {
  6931. OperatorNode *op = static_cast<OperatorNode *>(expr);
  6932. if (op->op == OP_EMPTY) {
  6933. is_var_init = true;
  6934. is_condition = true;
  6935. }
  6936. }
  6937. p_block->statements.push_back(expr);
  6938. tk = _get_token();
  6939. if (p_block->block_type == BlockNode::BLOCK_TYPE_FOR_CONDITION) {
  6940. if (tk.type == TK_COMMA) {
  6941. if (!is_condition) {
  6942. _set_error(RTR("The middle expression is expected to be a boolean operator."));
  6943. return ERR_PARSE_ERROR;
  6944. }
  6945. continue;
  6946. }
  6947. if (tk.type != TK_SEMICOLON) {
  6948. _set_expected_error(",", ";");
  6949. return ERR_PARSE_ERROR;
  6950. }
  6951. } else if (p_block->block_type == BlockNode::BLOCK_TYPE_FOR_EXPRESSION) {
  6952. if (tk.type == TK_COMMA) {
  6953. continue;
  6954. }
  6955. if (tk.type != TK_PARENTHESIS_CLOSE) {
  6956. _set_expected_error(",", ")");
  6957. return ERR_PARSE_ERROR;
  6958. }
  6959. } else if (tk.type != TK_SEMICOLON) {
  6960. _set_expected_error(";");
  6961. return ERR_PARSE_ERROR;
  6962. }
  6963. }
  6964. if (p_block) {
  6965. if (p_block->block_type == BlockNode::BLOCK_TYPE_FOR_INIT && !is_var_init) {
  6966. _set_error(RTR("The left expression is expected to be a variable declaration."));
  6967. return ERR_PARSE_ERROR;
  6968. }
  6969. if (p_block->block_type == BlockNode::BLOCK_TYPE_FOR_CONDITION && !is_condition) {
  6970. _set_error(RTR("The middle expression is expected to be a boolean operator."));
  6971. return ERR_PARSE_ERROR;
  6972. }
  6973. }
  6974. if (p_just_one) {
  6975. break;
  6976. }
  6977. }
  6978. return OK;
  6979. }
  6980. String ShaderLanguage::_get_shader_type_list(const HashSet<String> &p_shader_types) const {
  6981. // Return a list of shader types as an human-readable string
  6982. String valid_types;
  6983. for (const String &E : p_shader_types) {
  6984. if (!valid_types.is_empty()) {
  6985. valid_types += ", ";
  6986. }
  6987. valid_types += "'" + E + "'";
  6988. }
  6989. return valid_types;
  6990. }
  6991. String ShaderLanguage::_get_qualifier_str(ArgumentQualifier p_qualifier) const {
  6992. switch (p_qualifier) {
  6993. case ArgumentQualifier::ARGUMENT_QUALIFIER_IN:
  6994. return "in";
  6995. case ArgumentQualifier::ARGUMENT_QUALIFIER_OUT:
  6996. return "out";
  6997. case ArgumentQualifier::ARGUMENT_QUALIFIER_INOUT:
  6998. return "inout";
  6999. }
  7000. return "";
  7001. }
  7002. Error ShaderLanguage::_validate_precision(DataType p_type, DataPrecision p_precision) {
  7003. switch (p_type) {
  7004. case TYPE_STRUCT: {
  7005. _set_error(RTR("The precision modifier cannot be used on structs."));
  7006. return FAILED;
  7007. } break;
  7008. case TYPE_BOOL:
  7009. case TYPE_BVEC2:
  7010. case TYPE_BVEC3:
  7011. case TYPE_BVEC4: {
  7012. _set_error(RTR("The precision modifier cannot be used on boolean types."));
  7013. return FAILED;
  7014. } break;
  7015. default:
  7016. break;
  7017. }
  7018. return OK;
  7019. }
  7020. Error ShaderLanguage::_parse_shader(const HashMap<StringName, FunctionInfo> &p_functions, const Vector<ModeInfo> &p_render_modes, const HashSet<String> &p_shader_types) {
  7021. Token tk;
  7022. TkPos prev_pos;
  7023. Token next;
  7024. if (!is_shader_inc) {
  7025. #ifdef DEBUG_ENABLED
  7026. keyword_completion_context = CF_SHADER_TYPE;
  7027. #endif // DEBUG_ENABLED
  7028. tk = _get_token();
  7029. if (tk.type != TK_SHADER_TYPE) {
  7030. _set_error(vformat(RTR("Expected '%s' at the beginning of shader. Valid types are: %s."), "shader_type", _get_shader_type_list(p_shader_types)));
  7031. return ERR_PARSE_ERROR;
  7032. }
  7033. #ifdef DEBUG_ENABLED
  7034. keyword_completion_context = CF_UNSPECIFIED;
  7035. #endif // DEBUG_ENABLED
  7036. _get_completable_identifier(nullptr, COMPLETION_SHADER_TYPE, shader_type_identifier);
  7037. if (shader_type_identifier == StringName()) {
  7038. _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)));
  7039. return ERR_PARSE_ERROR;
  7040. }
  7041. if (!p_shader_types.has(shader_type_identifier)) {
  7042. _set_error(vformat(RTR("Invalid shader type. Valid types are: %s"), _get_shader_type_list(p_shader_types)));
  7043. return ERR_PARSE_ERROR;
  7044. }
  7045. prev_pos = _get_tkpos();
  7046. tk = _get_token();
  7047. if (tk.type != TK_SEMICOLON) {
  7048. _set_tkpos(prev_pos);
  7049. _set_expected_after_error(";", "shader_type " + String(shader_type_identifier));
  7050. return ERR_PARSE_ERROR;
  7051. }
  7052. }
  7053. #ifdef DEBUG_ENABLED
  7054. keyword_completion_context = CF_GLOBAL_SPACE;
  7055. #endif // DEBUG_ENABLED
  7056. tk = _get_token();
  7057. int texture_uniforms = 0;
  7058. int texture_binding = 0;
  7059. int uniforms = 0;
  7060. int instance_index = 0;
  7061. #ifdef DEBUG_ENABLED
  7062. uint64_t uniform_buffer_size = 0;
  7063. uint64_t max_uniform_buffer_size = 0;
  7064. int uniform_buffer_exceeded_line = -1;
  7065. bool check_device_limit_warnings = false;
  7066. {
  7067. RenderingDevice *device = RenderingDevice::get_singleton();
  7068. if (device != nullptr) {
  7069. check_device_limit_warnings = check_warnings && HAS_WARNING(ShaderWarning::DEVICE_LIMIT_EXCEEDED_FLAG);
  7070. max_uniform_buffer_size = device->limit_get(RenderingDevice::LIMIT_MAX_UNIFORM_BUFFER_SIZE);
  7071. }
  7072. }
  7073. #endif // DEBUG_ENABLED
  7074. ShaderNode::Uniform::Scope uniform_scope = ShaderNode::Uniform::SCOPE_LOCAL;
  7075. stages = &p_functions;
  7076. const FunctionInfo &constants = p_functions.has("constants") ? p_functions["constants"] : FunctionInfo();
  7077. HashMap<String, String> defined_modes;
  7078. while (tk.type != TK_EOF) {
  7079. switch (tk.type) {
  7080. case TK_RENDER_MODE: {
  7081. #ifdef DEBUG_ENABLED
  7082. keyword_completion_context = CF_UNSPECIFIED;
  7083. #endif // DEBUG_ENABLED
  7084. while (true) {
  7085. StringName mode;
  7086. _get_completable_identifier(nullptr, COMPLETION_RENDER_MODE, mode);
  7087. if (mode == StringName()) {
  7088. _set_error(RTR("Expected an identifier for render mode."));
  7089. return ERR_PARSE_ERROR;
  7090. }
  7091. const String smode = String(mode);
  7092. if (shader->render_modes.has(mode)) {
  7093. _set_error(vformat(RTR("Duplicated render mode: '%s'."), smode));
  7094. return ERR_PARSE_ERROR;
  7095. }
  7096. bool found = false;
  7097. if (is_shader_inc) {
  7098. for (int i = 0; i < RenderingServer::SHADER_MAX; i++) {
  7099. const Vector<ModeInfo> modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(i));
  7100. for (int j = 0; j < modes.size(); j++) {
  7101. const ModeInfo &info = modes[j];
  7102. const String name = String(info.name);
  7103. if (smode.begins_with(name)) {
  7104. if (!info.options.is_empty()) {
  7105. if (info.options.has(smode.substr(name.length() + 1))) {
  7106. found = true;
  7107. if (defined_modes.has(name)) {
  7108. _set_error(vformat(RTR("Redefinition of render mode: '%s'. The '%s' mode has already been set to '%s'."), smode, name, defined_modes[name]));
  7109. return ERR_PARSE_ERROR;
  7110. }
  7111. defined_modes.insert(name, smode);
  7112. break;
  7113. }
  7114. } else {
  7115. found = true;
  7116. break;
  7117. }
  7118. }
  7119. }
  7120. }
  7121. } else {
  7122. for (int i = 0; i < p_render_modes.size(); i++) {
  7123. const ModeInfo &info = p_render_modes[i];
  7124. const String name = String(info.name);
  7125. if (smode.begins_with(name)) {
  7126. if (!info.options.is_empty()) {
  7127. if (info.options.has(smode.substr(name.length() + 1))) {
  7128. found = true;
  7129. if (defined_modes.has(name)) {
  7130. _set_error(vformat(RTR("Redefinition of render mode: '%s'. The '%s' mode has already been set to '%s'."), smode, name, defined_modes[name]));
  7131. return ERR_PARSE_ERROR;
  7132. }
  7133. defined_modes.insert(name, smode);
  7134. break;
  7135. }
  7136. } else {
  7137. found = true;
  7138. break;
  7139. }
  7140. }
  7141. }
  7142. }
  7143. if (!found) {
  7144. _set_error(vformat(RTR("Invalid render mode: '%s'."), smode));
  7145. return ERR_PARSE_ERROR;
  7146. }
  7147. shader->render_modes.push_back(mode);
  7148. tk = _get_token();
  7149. if (tk.type == TK_COMMA) {
  7150. //all good, do nothing
  7151. } else if (tk.type == TK_SEMICOLON) {
  7152. break; //done
  7153. } else {
  7154. _set_error(vformat(RTR("Unexpected token: '%s'."), get_token_text(tk)));
  7155. return ERR_PARSE_ERROR;
  7156. }
  7157. }
  7158. #ifdef DEBUG_ENABLED
  7159. keyword_completion_context = CF_GLOBAL_SPACE;
  7160. #endif // DEBUG_ENABLED
  7161. } break;
  7162. case TK_STRUCT: {
  7163. ShaderNode::Struct st;
  7164. DataType type;
  7165. #ifdef DEBUG_ENABLED
  7166. keyword_completion_context = CF_UNSPECIFIED;
  7167. #endif // DEBUG_ENABLED
  7168. tk = _get_token();
  7169. if (tk.type == TK_IDENTIFIER) {
  7170. st.name = tk.text;
  7171. if (shader->constants.has(st.name) || shader->structs.has(st.name)) {
  7172. _set_redefinition_error(String(st.name));
  7173. return ERR_PARSE_ERROR;
  7174. }
  7175. tk = _get_token();
  7176. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  7177. _set_expected_error("{");
  7178. return ERR_PARSE_ERROR;
  7179. }
  7180. } else {
  7181. _set_error(RTR("Expected a struct identifier."));
  7182. return ERR_PARSE_ERROR;
  7183. }
  7184. StructNode *st_node = alloc_node<StructNode>();
  7185. st.shader_struct = st_node;
  7186. int member_count = 0;
  7187. HashSet<String> member_names;
  7188. while (true) { // variables list
  7189. #ifdef DEBUG_ENABLED
  7190. keyword_completion_context = CF_DATATYPE | CF_PRECISION_MODIFIER;
  7191. #endif // DEBUG_ENABLED
  7192. tk = _get_token();
  7193. if (tk.type == TK_CURLY_BRACKET_CLOSE) {
  7194. break;
  7195. }
  7196. StringName struct_name = "";
  7197. bool struct_dt = false;
  7198. DataPrecision precision = PRECISION_DEFAULT;
  7199. if (tk.type == TK_STRUCT) {
  7200. _set_error(RTR("Nested structs are not allowed."));
  7201. return ERR_PARSE_ERROR;
  7202. }
  7203. if (is_token_precision(tk.type)) {
  7204. precision = get_token_precision(tk.type);
  7205. tk = _get_token();
  7206. #ifdef DEBUG_ENABLED
  7207. keyword_completion_context ^= CF_PRECISION_MODIFIER;
  7208. #endif // DEBUG_ENABLED
  7209. }
  7210. if (shader->structs.has(tk.text)) {
  7211. struct_name = tk.text;
  7212. #ifdef DEBUG_ENABLED
  7213. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_STRUCT_FLAG) && used_structs.has(struct_name)) {
  7214. used_structs[struct_name].used = true;
  7215. }
  7216. #endif // DEBUG_ENABLED
  7217. struct_dt = true;
  7218. }
  7219. if (!is_token_datatype(tk.type) && !struct_dt) {
  7220. _set_error(RTR("Expected data type."));
  7221. return ERR_PARSE_ERROR;
  7222. } else {
  7223. type = struct_dt ? TYPE_STRUCT : get_token_datatype(tk.type);
  7224. if (precision != PRECISION_DEFAULT && _validate_precision(type, precision) != OK) {
  7225. return ERR_PARSE_ERROR;
  7226. }
  7227. if (type == TYPE_VOID || is_sampler_type(type)) {
  7228. _set_error(vformat(RTR("A '%s' data type is not allowed here."), get_datatype_name(type)));
  7229. return ERR_PARSE_ERROR;
  7230. }
  7231. #ifdef DEBUG_ENABLED
  7232. keyword_completion_context = CF_UNSPECIFIED;
  7233. #endif // DEBUG_ENABLED
  7234. bool first = true;
  7235. bool fixed_array_size = false;
  7236. int array_size = 0;
  7237. do {
  7238. tk = _get_token();
  7239. if (first) {
  7240. first = false;
  7241. if (tk.type != TK_IDENTIFIER && tk.type != TK_BRACKET_OPEN) {
  7242. _set_error(RTR("Expected an identifier or '['."));
  7243. return ERR_PARSE_ERROR;
  7244. }
  7245. if (tk.type == TK_BRACKET_OPEN) {
  7246. Error error = _parse_array_size(nullptr, constants, true, nullptr, &array_size, nullptr);
  7247. if (error != OK) {
  7248. return error;
  7249. }
  7250. fixed_array_size = true;
  7251. tk = _get_token();
  7252. }
  7253. }
  7254. if (tk.type != TK_IDENTIFIER) {
  7255. _set_error(RTR("Expected an identifier."));
  7256. return ERR_PARSE_ERROR;
  7257. }
  7258. MemberNode *member = alloc_node<MemberNode>();
  7259. member->precision = precision;
  7260. member->datatype = type;
  7261. member->struct_name = struct_name;
  7262. member->name = tk.text;
  7263. member->array_size = array_size;
  7264. if (member_names.has(member->name)) {
  7265. _set_redefinition_error(String(member->name));
  7266. return ERR_PARSE_ERROR;
  7267. }
  7268. member_names.insert(member->name);
  7269. tk = _get_token();
  7270. if (tk.type == TK_BRACKET_OPEN) {
  7271. Error error = _parse_array_size(nullptr, constants, true, nullptr, &member->array_size, nullptr);
  7272. if (error != OK) {
  7273. return error;
  7274. }
  7275. tk = _get_token();
  7276. }
  7277. if (!fixed_array_size) {
  7278. array_size = 0;
  7279. }
  7280. if (tk.type != TK_SEMICOLON && tk.type != TK_COMMA) {
  7281. _set_expected_error(",", ";");
  7282. return ERR_PARSE_ERROR;
  7283. }
  7284. st_node->members.push_back(member);
  7285. member_count++;
  7286. } while (tk.type == TK_COMMA); // another member
  7287. }
  7288. }
  7289. if (member_count == 0) {
  7290. _set_error(RTR("Empty structs are not allowed."));
  7291. return ERR_PARSE_ERROR;
  7292. }
  7293. #ifdef DEBUG_ENABLED
  7294. keyword_completion_context = CF_UNSPECIFIED;
  7295. #endif // DEBUG_ENABLED
  7296. tk = _get_token();
  7297. if (tk.type != TK_SEMICOLON) {
  7298. _set_expected_error(";");
  7299. return ERR_PARSE_ERROR;
  7300. }
  7301. #ifdef DEBUG_ENABLED
  7302. keyword_completion_context = CF_GLOBAL_SPACE;
  7303. #endif // DEBUG_ENABLED
  7304. shader->structs[st.name] = st;
  7305. shader->vstructs.push_back(st); // struct's order is important!
  7306. #ifdef DEBUG_ENABLED
  7307. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_STRUCT_FLAG)) {
  7308. used_structs.insert(st.name, Usage(tk_line));
  7309. }
  7310. #endif // DEBUG_ENABLED
  7311. } break;
  7312. case TK_GLOBAL: {
  7313. #ifdef DEBUG_ENABLED
  7314. keyword_completion_context = CF_UNIFORM_KEYWORD;
  7315. if (_lookup_next(next)) {
  7316. if (next.type == TK_UNIFORM) {
  7317. keyword_completion_context ^= CF_UNIFORM_KEYWORD;
  7318. }
  7319. }
  7320. #endif // DEBUG_ENABLED
  7321. tk = _get_token();
  7322. if (tk.type != TK_UNIFORM) {
  7323. _set_expected_after_error("uniform", "global");
  7324. return ERR_PARSE_ERROR;
  7325. }
  7326. uniform_scope = ShaderNode::Uniform::SCOPE_GLOBAL;
  7327. };
  7328. [[fallthrough]];
  7329. case TK_INSTANCE: {
  7330. if (tk.type == TK_INSTANCE) {
  7331. #ifdef DEBUG_ENABLED
  7332. keyword_completion_context = CF_UNIFORM_KEYWORD;
  7333. if (_lookup_next(next)) {
  7334. if (next.type == TK_UNIFORM) {
  7335. keyword_completion_context ^= CF_UNIFORM_KEYWORD;
  7336. }
  7337. }
  7338. #endif // DEBUG_ENABLED
  7339. if (String(shader_type_identifier) != "spatial") {
  7340. _set_error(vformat(RTR("Uniform instances are not yet implemented for '%s' shaders."), shader_type_identifier));
  7341. return ERR_PARSE_ERROR;
  7342. }
  7343. if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  7344. _set_error(RTR("Uniform instances are not supported in gl_compatibility shaders."));
  7345. return ERR_PARSE_ERROR;
  7346. }
  7347. if (uniform_scope == ShaderNode::Uniform::SCOPE_LOCAL) {
  7348. tk = _get_token();
  7349. if (tk.type != TK_UNIFORM) {
  7350. _set_expected_after_error("uniform", "instance");
  7351. return ERR_PARSE_ERROR;
  7352. }
  7353. uniform_scope = ShaderNode::Uniform::SCOPE_INSTANCE;
  7354. }
  7355. }
  7356. };
  7357. [[fallthrough]];
  7358. case TK_UNIFORM:
  7359. case TK_VARYING: {
  7360. bool is_uniform = tk.type == TK_UNIFORM;
  7361. #ifdef DEBUG_ENABLED
  7362. keyword_completion_context = CF_UNSPECIFIED;
  7363. #endif // DEBUG_ENABLED
  7364. if (!is_uniform) {
  7365. if (shader_type_identifier == "particles" || shader_type_identifier == "sky" || shader_type_identifier == "fog") {
  7366. _set_error(vformat(RTR("Varyings cannot be used in '%s' shaders."), shader_type_identifier));
  7367. return ERR_PARSE_ERROR;
  7368. }
  7369. }
  7370. DataPrecision precision = PRECISION_DEFAULT;
  7371. DataInterpolation interpolation = INTERPOLATION_DEFAULT;
  7372. DataType type;
  7373. StringName name;
  7374. int array_size = 0;
  7375. tk = _get_token();
  7376. #ifdef DEBUG_ENABLED
  7377. bool temp_error = false;
  7378. uint32_t datatype_flag;
  7379. if (!is_uniform) {
  7380. datatype_flag = CF_VARYING_TYPE;
  7381. keyword_completion_context = CF_INTERPOLATION_QUALIFIER | CF_PRECISION_MODIFIER | datatype_flag;
  7382. if (_lookup_next(next)) {
  7383. if (is_token_interpolation(next.type)) {
  7384. keyword_completion_context ^= (CF_INTERPOLATION_QUALIFIER | datatype_flag);
  7385. } else if (is_token_precision(next.type)) {
  7386. keyword_completion_context ^= (CF_PRECISION_MODIFIER | datatype_flag);
  7387. } else if (is_token_datatype(next.type)) {
  7388. keyword_completion_context ^= datatype_flag;
  7389. }
  7390. }
  7391. } else {
  7392. datatype_flag = CF_UNIFORM_TYPE;
  7393. keyword_completion_context = CF_PRECISION_MODIFIER | datatype_flag;
  7394. if (_lookup_next(next)) {
  7395. if (is_token_precision(next.type)) {
  7396. keyword_completion_context ^= (CF_PRECISION_MODIFIER | datatype_flag);
  7397. } else if (is_token_datatype(next.type)) {
  7398. keyword_completion_context ^= datatype_flag;
  7399. }
  7400. }
  7401. }
  7402. #endif // DEBUG_ENABLED
  7403. if (is_token_interpolation(tk.type)) {
  7404. if (is_uniform) {
  7405. _set_error(RTR("Interpolation qualifiers are not supported for uniforms."));
  7406. #ifdef DEBUG_ENABLED
  7407. temp_error = true;
  7408. #else
  7409. return ERR_PARSE_ERROR;
  7410. #endif // DEBUG_ENABLED
  7411. }
  7412. interpolation = get_token_interpolation(tk.type);
  7413. tk = _get_token();
  7414. #ifdef DEBUG_ENABLED
  7415. if (keyword_completion_context & CF_INTERPOLATION_QUALIFIER) {
  7416. keyword_completion_context ^= CF_INTERPOLATION_QUALIFIER;
  7417. }
  7418. if (_lookup_next(next)) {
  7419. if (is_token_precision(next.type)) {
  7420. keyword_completion_context ^= CF_PRECISION_MODIFIER;
  7421. } else if (is_token_datatype(next.type)) {
  7422. keyword_completion_context ^= datatype_flag;
  7423. }
  7424. }
  7425. if (temp_error) {
  7426. return ERR_PARSE_ERROR;
  7427. }
  7428. #endif // DEBUG_ENABLED
  7429. }
  7430. if (is_token_precision(tk.type)) {
  7431. precision = get_token_precision(tk.type);
  7432. tk = _get_token();
  7433. #ifdef DEBUG_ENABLED
  7434. if (keyword_completion_context & CF_INTERPOLATION_QUALIFIER) {
  7435. keyword_completion_context ^= CF_INTERPOLATION_QUALIFIER;
  7436. }
  7437. if (keyword_completion_context & CF_PRECISION_MODIFIER) {
  7438. keyword_completion_context ^= CF_PRECISION_MODIFIER;
  7439. }
  7440. if (_lookup_next(next)) {
  7441. if (is_token_datatype(next.type)) {
  7442. keyword_completion_context = CF_UNSPECIFIED;
  7443. }
  7444. }
  7445. #endif // DEBUG_ENABLED
  7446. }
  7447. if (shader->structs.has(tk.text)) {
  7448. if (is_uniform) {
  7449. _set_error(vformat(RTR("The '%s' data type is not supported for uniforms."), "struct"));
  7450. return ERR_PARSE_ERROR;
  7451. } else {
  7452. _set_error(vformat(RTR("The '%s' data type is not allowed here."), "struct"));
  7453. return ERR_PARSE_ERROR;
  7454. }
  7455. }
  7456. if (!is_token_datatype(tk.type)) {
  7457. _set_error(RTR("Expected data type."));
  7458. return ERR_PARSE_ERROR;
  7459. }
  7460. type = get_token_datatype(tk.type);
  7461. if (precision != PRECISION_DEFAULT && _validate_precision(type, precision) != OK) {
  7462. return ERR_PARSE_ERROR;
  7463. }
  7464. if (type == TYPE_VOID) {
  7465. _set_error(vformat(RTR("The '%s' data type is not allowed here."), "void"));
  7466. return ERR_PARSE_ERROR;
  7467. }
  7468. if (!is_uniform && interpolation != INTERPOLATION_DEFAULT && type < TYPE_INT) {
  7469. _set_error(vformat(RTR("Interpolation modifier '%s' cannot be used with boolean types."), get_interpolation_name(interpolation)));
  7470. return ERR_PARSE_ERROR;
  7471. }
  7472. if (!is_uniform && type > TYPE_MAT4) {
  7473. _set_error(RTR("Invalid data type for varying."));
  7474. return ERR_PARSE_ERROR;
  7475. }
  7476. #ifdef DEBUG_ENABLED
  7477. keyword_completion_context = CF_UNSPECIFIED;
  7478. #endif // DEBUG_ENABLED
  7479. tk = _get_token();
  7480. if (tk.type != TK_IDENTIFIER && tk.type != TK_BRACKET_OPEN) {
  7481. _set_error(RTR("Expected an identifier or '['."));
  7482. return ERR_PARSE_ERROR;
  7483. }
  7484. if (tk.type == TK_BRACKET_OPEN) {
  7485. Error error = _parse_array_size(nullptr, constants, true, nullptr, &array_size, nullptr);
  7486. if (error != OK) {
  7487. return error;
  7488. }
  7489. tk = _get_token();
  7490. }
  7491. if (tk.type != TK_IDENTIFIER) {
  7492. _set_error(RTR("Expected an identifier."));
  7493. return ERR_PARSE_ERROR;
  7494. }
  7495. prev_pos = _get_tkpos();
  7496. name = tk.text;
  7497. if (_find_identifier(nullptr, false, constants, name)) {
  7498. _set_redefinition_error(String(name));
  7499. return ERR_PARSE_ERROR;
  7500. }
  7501. if (has_builtin(p_functions, name)) {
  7502. _set_redefinition_error(String(name));
  7503. return ERR_PARSE_ERROR;
  7504. }
  7505. if (is_uniform) {
  7506. if (uniform_scope == ShaderNode::Uniform::SCOPE_GLOBAL && Engine::get_singleton()->is_editor_hint()) { // Type checking for global uniforms is not allowed outside the editor.
  7507. //validate global uniform
  7508. DataType gvtype = global_shader_uniform_get_type_func(name);
  7509. if (gvtype == TYPE_MAX) {
  7510. _set_error(vformat(RTR("Global uniform '%s' does not exist. Create it in Project Settings."), String(name)));
  7511. return ERR_PARSE_ERROR;
  7512. }
  7513. if (type != gvtype) {
  7514. _set_error(vformat(RTR("Global uniform '%s' must be of type '%s'."), String(name), get_datatype_name(gvtype)));
  7515. return ERR_PARSE_ERROR;
  7516. }
  7517. }
  7518. ShaderNode::Uniform uniform;
  7519. uniform.type = type;
  7520. uniform.scope = uniform_scope;
  7521. uniform.precision = precision;
  7522. uniform.array_size = array_size;
  7523. uniform.group = current_uniform_group_name;
  7524. uniform.subgroup = current_uniform_subgroup_name;
  7525. tk = _get_token();
  7526. if (tk.type == TK_BRACKET_OPEN) {
  7527. Error error = _parse_array_size(nullptr, constants, true, nullptr, &uniform.array_size, nullptr);
  7528. if (error != OK) {
  7529. return error;
  7530. }
  7531. tk = _get_token();
  7532. }
  7533. if (is_sampler_type(type)) {
  7534. if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE) {
  7535. _set_error(vformat(RTR("The '%s' qualifier is not supported for sampler types."), "SCOPE_INSTANCE"));
  7536. return ERR_PARSE_ERROR;
  7537. }
  7538. uniform.texture_order = texture_uniforms++;
  7539. uniform.texture_binding = texture_binding;
  7540. if (uniform.array_size > 0) {
  7541. texture_binding += uniform.array_size;
  7542. } else {
  7543. ++texture_binding;
  7544. }
  7545. uniform.order = -1;
  7546. } else {
  7547. if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE && (type == TYPE_MAT2 || type == TYPE_MAT3 || type == TYPE_MAT4)) {
  7548. _set_error(vformat(RTR("The '%s' qualifier is not supported for matrix types."), "SCOPE_INSTANCE"));
  7549. return ERR_PARSE_ERROR;
  7550. }
  7551. uniform.texture_order = -1;
  7552. if (uniform_scope != ShaderNode::Uniform::SCOPE_INSTANCE) {
  7553. uniform.order = uniforms++;
  7554. #ifdef DEBUG_ENABLED
  7555. if (check_device_limit_warnings) {
  7556. if (uniform.array_size > 0) {
  7557. int size = get_datatype_size(uniform.type) * uniform.array_size;
  7558. int m = (16 * uniform.array_size);
  7559. if ((size % m) != 0U) {
  7560. size += m - (size % m);
  7561. }
  7562. uniform_buffer_size += size;
  7563. } else {
  7564. uniform_buffer_size += get_datatype_size(uniform.type);
  7565. }
  7566. if (uniform_buffer_exceeded_line == -1 && uniform_buffer_size > max_uniform_buffer_size) {
  7567. uniform_buffer_exceeded_line = tk_line;
  7568. }
  7569. }
  7570. #endif // DEBUG_ENABLED
  7571. }
  7572. }
  7573. if (uniform.array_size > 0) {
  7574. if (uniform_scope == ShaderNode::Uniform::SCOPE_GLOBAL) {
  7575. _set_error(vformat(RTR("The '%s' qualifier is not supported for uniform arrays."), "SCOPE_GLOBAL"));
  7576. return ERR_PARSE_ERROR;
  7577. }
  7578. if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE) {
  7579. _set_error(vformat(RTR("The '%s' qualifier is not supported for uniform arrays."), "SCOPE_INSTANCE"));
  7580. return ERR_PARSE_ERROR;
  7581. }
  7582. }
  7583. int custom_instance_index = -1;
  7584. if (tk.type == TK_COLON) {
  7585. completion_type = COMPLETION_HINT;
  7586. completion_base = type;
  7587. completion_base_array = uniform.array_size > 0;
  7588. //hint
  7589. do {
  7590. tk = _get_token();
  7591. completion_line = tk.line;
  7592. if (!is_token_hint(tk.type)) {
  7593. _set_error(RTR("Expected valid type hint after ':'."));
  7594. return ERR_PARSE_ERROR;
  7595. }
  7596. if (uniform.array_size > 0) {
  7597. if (tk.type != TK_HINT_SOURCE_COLOR) {
  7598. _set_error(RTR("This hint is not supported for uniform arrays."));
  7599. return ERR_PARSE_ERROR;
  7600. }
  7601. }
  7602. ShaderNode::Uniform::Hint new_hint = ShaderNode::Uniform::HINT_NONE;
  7603. TextureFilter new_filter = FILTER_DEFAULT;
  7604. TextureRepeat new_repeat = REPEAT_DEFAULT;
  7605. switch (tk.type) {
  7606. case TK_HINT_SOURCE_COLOR: {
  7607. if (type != TYPE_VEC3 && type != TYPE_VEC4 && !is_sampler_type(type)) {
  7608. _set_error(vformat(RTR("Source color hint is for '%s', '%s' or sampler types only."), "vec3", "vec4"));
  7609. return ERR_PARSE_ERROR;
  7610. }
  7611. if (is_sampler_type(type)) {
  7612. if (uniform.use_color) {
  7613. _set_error(vformat(RTR("Duplicated hint: '%s'."), "source_color"));
  7614. return ERR_PARSE_ERROR;
  7615. }
  7616. uniform.use_color = true;
  7617. } else {
  7618. new_hint = ShaderNode::Uniform::HINT_SOURCE_COLOR;
  7619. }
  7620. } break;
  7621. case TK_HINT_DEFAULT_BLACK_TEXTURE: {
  7622. new_hint = ShaderNode::Uniform::HINT_DEFAULT_BLACK;
  7623. } break;
  7624. case TK_HINT_DEFAULT_WHITE_TEXTURE: {
  7625. new_hint = ShaderNode::Uniform::HINT_DEFAULT_WHITE;
  7626. } break;
  7627. case TK_HINT_DEFAULT_TRANSPARENT_TEXTURE: {
  7628. new_hint = ShaderNode::Uniform::HINT_DEFAULT_TRANSPARENT;
  7629. } break;
  7630. case TK_HINT_NORMAL_TEXTURE: {
  7631. new_hint = ShaderNode::Uniform::HINT_NORMAL;
  7632. } break;
  7633. case TK_HINT_ROUGHNESS_NORMAL_TEXTURE: {
  7634. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL;
  7635. } break;
  7636. case TK_HINT_ROUGHNESS_R: {
  7637. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_R;
  7638. } break;
  7639. case TK_HINT_ROUGHNESS_G: {
  7640. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_G;
  7641. } break;
  7642. case TK_HINT_ROUGHNESS_B: {
  7643. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_B;
  7644. } break;
  7645. case TK_HINT_ROUGHNESS_A: {
  7646. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_A;
  7647. } break;
  7648. case TK_HINT_ROUGHNESS_GRAY: {
  7649. new_hint = ShaderNode::Uniform::HINT_ROUGHNESS_GRAY;
  7650. } break;
  7651. case TK_HINT_ANISOTROPY_TEXTURE: {
  7652. new_hint = ShaderNode::Uniform::HINT_ANISOTROPY;
  7653. } break;
  7654. case TK_HINT_RANGE: {
  7655. if (type != TYPE_FLOAT && type != TYPE_INT) {
  7656. _set_error(vformat(RTR("Range hint is for '%s' and '%s' only."), "float", "int"));
  7657. return ERR_PARSE_ERROR;
  7658. }
  7659. tk = _get_token();
  7660. if (tk.type != TK_PARENTHESIS_OPEN) {
  7661. _set_expected_after_error("(", "hint_range");
  7662. return ERR_PARSE_ERROR;
  7663. }
  7664. tk = _get_token();
  7665. float sign = 1.0;
  7666. if (tk.type == TK_OP_SUB) {
  7667. sign = -1.0;
  7668. tk = _get_token();
  7669. }
  7670. if (tk.type != TK_FLOAT_CONSTANT && !tk.is_integer_constant()) {
  7671. _set_error(RTR("Expected an integer constant."));
  7672. return ERR_PARSE_ERROR;
  7673. }
  7674. uniform.hint_range[0] = tk.constant;
  7675. uniform.hint_range[0] *= sign;
  7676. tk = _get_token();
  7677. if (tk.type != TK_COMMA) {
  7678. _set_error(RTR("Expected ',' after integer constant."));
  7679. return ERR_PARSE_ERROR;
  7680. }
  7681. tk = _get_token();
  7682. sign = 1.0;
  7683. if (tk.type == TK_OP_SUB) {
  7684. sign = -1.0;
  7685. tk = _get_token();
  7686. }
  7687. if (tk.type != TK_FLOAT_CONSTANT && !tk.is_integer_constant()) {
  7688. _set_error(RTR("Expected an integer constant after ','."));
  7689. return ERR_PARSE_ERROR;
  7690. }
  7691. uniform.hint_range[1] = tk.constant;
  7692. uniform.hint_range[1] *= sign;
  7693. tk = _get_token();
  7694. if (tk.type == TK_COMMA) {
  7695. tk = _get_token();
  7696. if (tk.type != TK_FLOAT_CONSTANT && !tk.is_integer_constant()) {
  7697. _set_error(RTR("Expected an integer constant after ','."));
  7698. return ERR_PARSE_ERROR;
  7699. }
  7700. uniform.hint_range[2] = tk.constant;
  7701. tk = _get_token();
  7702. } else {
  7703. if (type == TYPE_INT) {
  7704. uniform.hint_range[2] = 1;
  7705. } else {
  7706. uniform.hint_range[2] = 0.001;
  7707. }
  7708. }
  7709. if (tk.type != TK_PARENTHESIS_CLOSE) {
  7710. _set_expected_error(")");
  7711. return ERR_PARSE_ERROR;
  7712. }
  7713. new_hint = ShaderNode::Uniform::HINT_RANGE;
  7714. } break;
  7715. case TK_HINT_INSTANCE_INDEX: {
  7716. if (custom_instance_index != -1) {
  7717. _set_error(vformat(RTR("Can only specify '%s' once."), "instance_index"));
  7718. return ERR_PARSE_ERROR;
  7719. }
  7720. tk = _get_token();
  7721. if (tk.type != TK_PARENTHESIS_OPEN) {
  7722. _set_expected_after_error("(", "instance_index");
  7723. return ERR_PARSE_ERROR;
  7724. }
  7725. tk = _get_token();
  7726. if (tk.type == TK_OP_SUB) {
  7727. _set_error(RTR("The instance index can't be negative."));
  7728. return ERR_PARSE_ERROR;
  7729. }
  7730. if (!tk.is_integer_constant()) {
  7731. _set_error(RTR("Expected an integer constant."));
  7732. return ERR_PARSE_ERROR;
  7733. }
  7734. custom_instance_index = tk.constant;
  7735. current_uniform_instance_index_defined = true;
  7736. if (custom_instance_index >= MAX_INSTANCE_UNIFORM_INDICES) {
  7737. _set_error(vformat(RTR("Allowed instance uniform indices must be within [0..%d] range."), MAX_INSTANCE_UNIFORM_INDICES - 1));
  7738. return ERR_PARSE_ERROR;
  7739. }
  7740. tk = _get_token();
  7741. if (tk.type != TK_PARENTHESIS_CLOSE) {
  7742. _set_expected_error(")");
  7743. return ERR_PARSE_ERROR;
  7744. }
  7745. } break;
  7746. case TK_HINT_SCREEN_TEXTURE: {
  7747. new_hint = ShaderNode::Uniform::HINT_SCREEN_TEXTURE;
  7748. --texture_uniforms;
  7749. --texture_binding;
  7750. } break;
  7751. case TK_HINT_NORMAL_ROUGHNESS_TEXTURE: {
  7752. new_hint = ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE;
  7753. --texture_uniforms;
  7754. --texture_binding;
  7755. if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  7756. _set_error(RTR("'hint_normal_roughness_texture' is not supported in gl_compatibility shaders."));
  7757. return ERR_PARSE_ERROR;
  7758. }
  7759. if (String(shader_type_identifier) != "spatial") {
  7760. _set_error(vformat(RTR("'hint_normal_roughness_texture' is not supported in '%s' shaders."), shader_type_identifier));
  7761. return ERR_PARSE_ERROR;
  7762. }
  7763. } break;
  7764. case TK_HINT_DEPTH_TEXTURE: {
  7765. new_hint = ShaderNode::Uniform::HINT_DEPTH_TEXTURE;
  7766. --texture_uniforms;
  7767. --texture_binding;
  7768. if (String(shader_type_identifier) != "spatial") {
  7769. _set_error(vformat(RTR("'hint_depth_texture' is not supported in '%s' shaders."), shader_type_identifier));
  7770. return ERR_PARSE_ERROR;
  7771. }
  7772. } break;
  7773. case TK_FILTER_NEAREST: {
  7774. new_filter = FILTER_NEAREST;
  7775. } break;
  7776. case TK_FILTER_LINEAR: {
  7777. new_filter = FILTER_LINEAR;
  7778. } break;
  7779. case TK_FILTER_NEAREST_MIPMAP: {
  7780. new_filter = FILTER_NEAREST_MIPMAP;
  7781. } break;
  7782. case TK_FILTER_LINEAR_MIPMAP: {
  7783. new_filter = FILTER_LINEAR_MIPMAP;
  7784. } break;
  7785. case TK_FILTER_NEAREST_MIPMAP_ANISOTROPIC: {
  7786. new_filter = FILTER_NEAREST_MIPMAP_ANISOTROPIC;
  7787. } break;
  7788. case TK_FILTER_LINEAR_MIPMAP_ANISOTROPIC: {
  7789. new_filter = FILTER_LINEAR_MIPMAP_ANISOTROPIC;
  7790. } break;
  7791. case TK_REPEAT_DISABLE: {
  7792. new_repeat = REPEAT_DISABLE;
  7793. } break;
  7794. case TK_REPEAT_ENABLE: {
  7795. new_repeat = REPEAT_ENABLE;
  7796. } break;
  7797. default:
  7798. break;
  7799. }
  7800. if (((new_filter != FILTER_DEFAULT || new_repeat != REPEAT_DEFAULT) || (new_hint != ShaderNode::Uniform::HINT_NONE && new_hint != ShaderNode::Uniform::HINT_SOURCE_COLOR && new_hint != ShaderNode::Uniform::HINT_RANGE)) && !is_sampler_type(type)) {
  7801. _set_error(RTR("This hint is only for sampler types."));
  7802. return ERR_PARSE_ERROR;
  7803. }
  7804. if (new_hint != ShaderNode::Uniform::HINT_NONE) {
  7805. if (uniform.hint != ShaderNode::Uniform::HINT_NONE) {
  7806. if (uniform.hint == new_hint) {
  7807. _set_error(vformat(RTR("Duplicated hint: '%s'."), get_uniform_hint_name(new_hint)));
  7808. } else {
  7809. _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)));
  7810. }
  7811. return ERR_PARSE_ERROR;
  7812. } else {
  7813. uniform.hint = new_hint;
  7814. current_uniform_hint = new_hint;
  7815. }
  7816. }
  7817. if (new_filter != FILTER_DEFAULT) {
  7818. if (uniform.filter != FILTER_DEFAULT) {
  7819. if (uniform.filter == new_filter) {
  7820. _set_error(vformat(RTR("Duplicated filter mode: '%s'."), get_texture_filter_name(new_filter)));
  7821. } else {
  7822. _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)));
  7823. }
  7824. return ERR_PARSE_ERROR;
  7825. } else {
  7826. uniform.filter = new_filter;
  7827. current_uniform_filter = new_filter;
  7828. }
  7829. }
  7830. if (new_repeat != REPEAT_DEFAULT) {
  7831. if (uniform.repeat != REPEAT_DEFAULT) {
  7832. if (uniform.repeat == new_repeat) {
  7833. _set_error(vformat(RTR("Duplicated repeat mode: '%s'."), get_texture_repeat_name(new_repeat)));
  7834. } else {
  7835. _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)));
  7836. }
  7837. return ERR_PARSE_ERROR;
  7838. } else {
  7839. uniform.repeat = new_repeat;
  7840. current_uniform_repeat = new_repeat;
  7841. }
  7842. }
  7843. tk = _get_token();
  7844. } while (tk.type == TK_COMMA);
  7845. }
  7846. if (uniform_scope == ShaderNode::Uniform::SCOPE_INSTANCE) {
  7847. if (custom_instance_index >= 0) {
  7848. uniform.instance_index = custom_instance_index;
  7849. } else {
  7850. uniform.instance_index = instance_index++;
  7851. if (instance_index > MAX_INSTANCE_UNIFORM_INDICES) {
  7852. _set_error(vformat(RTR("Too many '%s' uniforms in shader, maximum supported is %d."), "instance", MAX_INSTANCE_UNIFORM_INDICES));
  7853. return ERR_PARSE_ERROR;
  7854. }
  7855. }
  7856. }
  7857. //reset scope for next uniform
  7858. if (tk.type == TK_OP_ASSIGN) {
  7859. if (uniform.array_size > 0) {
  7860. _set_error(RTR("Setting default values to uniform arrays is not supported."));
  7861. return ERR_PARSE_ERROR;
  7862. }
  7863. Node *expr = _parse_and_reduce_expression(nullptr, constants);
  7864. if (!expr) {
  7865. return ERR_PARSE_ERROR;
  7866. }
  7867. if (expr->type != Node::NODE_TYPE_CONSTANT) {
  7868. _set_error(RTR("Expected constant expression after '='."));
  7869. return ERR_PARSE_ERROR;
  7870. }
  7871. ConstantNode *cn = static_cast<ConstantNode *>(expr);
  7872. uniform.default_value.resize(cn->values.size());
  7873. if (!convert_constant(cn, uniform.type, uniform.default_value.ptrw())) {
  7874. _set_error(vformat(RTR("Can't convert constant to '%s'."), get_datatype_name(uniform.type)));
  7875. return ERR_PARSE_ERROR;
  7876. }
  7877. tk = _get_token();
  7878. }
  7879. shader->uniforms[name] = uniform;
  7880. #ifdef DEBUG_ENABLED
  7881. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_UNIFORM_FLAG)) {
  7882. used_uniforms.insert(name, Usage(tk_line));
  7883. }
  7884. #endif // DEBUG_ENABLED
  7885. //reset scope for next uniform
  7886. uniform_scope = ShaderNode::Uniform::SCOPE_LOCAL;
  7887. if (tk.type != TK_SEMICOLON) {
  7888. _set_expected_error(";");
  7889. return ERR_PARSE_ERROR;
  7890. }
  7891. #ifdef DEBUG_ENABLED
  7892. keyword_completion_context = CF_GLOBAL_SPACE;
  7893. #endif // DEBUG_ENABLED
  7894. completion_type = COMPLETION_NONE;
  7895. current_uniform_hint = ShaderNode::Uniform::HINT_NONE;
  7896. current_uniform_filter = FILTER_DEFAULT;
  7897. current_uniform_repeat = REPEAT_DEFAULT;
  7898. current_uniform_instance_index_defined = false;
  7899. } else { // varying
  7900. ShaderNode::Varying varying;
  7901. varying.type = type;
  7902. varying.precision = precision;
  7903. varying.interpolation = interpolation;
  7904. varying.tkpos = prev_pos;
  7905. varying.array_size = array_size;
  7906. tk = _get_token();
  7907. if (tk.type != TK_SEMICOLON && tk.type != TK_BRACKET_OPEN) {
  7908. if (array_size == 0) {
  7909. _set_expected_error(";", "[");
  7910. } else {
  7911. _set_expected_error(";");
  7912. }
  7913. return ERR_PARSE_ERROR;
  7914. }
  7915. if (tk.type == TK_BRACKET_OPEN) {
  7916. Error error = _parse_array_size(nullptr, constants, true, nullptr, &varying.array_size, nullptr);
  7917. if (error != OK) {
  7918. return error;
  7919. }
  7920. tk = _get_token();
  7921. }
  7922. shader->varyings[name] = varying;
  7923. #ifdef DEBUG_ENABLED
  7924. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_VARYING_FLAG)) {
  7925. used_varyings.insert(name, Usage(tk_line));
  7926. }
  7927. #endif // DEBUG_ENABLED
  7928. }
  7929. } break;
  7930. case TK_UNIFORM_GROUP: {
  7931. tk = _get_token();
  7932. if (tk.type == TK_IDENTIFIER) {
  7933. current_uniform_group_name = tk.text;
  7934. tk = _get_token();
  7935. if (tk.type == TK_PERIOD) {
  7936. tk = _get_token();
  7937. if (tk.type == TK_IDENTIFIER) {
  7938. current_uniform_subgroup_name = tk.text;
  7939. tk = _get_token();
  7940. if (tk.type != TK_SEMICOLON) {
  7941. _set_expected_error(";");
  7942. return ERR_PARSE_ERROR;
  7943. }
  7944. } else {
  7945. _set_error(RTR("Expected an uniform subgroup identifier."));
  7946. return ERR_PARSE_ERROR;
  7947. }
  7948. } else if (tk.type != TK_SEMICOLON) {
  7949. _set_expected_error(";", ".");
  7950. return ERR_PARSE_ERROR;
  7951. }
  7952. } else {
  7953. if (tk.type != TK_SEMICOLON) {
  7954. if (current_uniform_group_name.is_empty()) {
  7955. _set_error(RTR("Expected an uniform group identifier."));
  7956. } else {
  7957. _set_error(RTR("Expected an uniform group identifier or `;`."));
  7958. }
  7959. return ERR_PARSE_ERROR;
  7960. } else if (current_uniform_group_name.is_empty()) {
  7961. _set_error(RTR("Group needs to be opened before."));
  7962. return ERR_PARSE_ERROR;
  7963. } else {
  7964. current_uniform_group_name = "";
  7965. current_uniform_subgroup_name = "";
  7966. }
  7967. }
  7968. } break;
  7969. case TK_SHADER_TYPE: {
  7970. _set_error(RTR("Shader type is already defined."));
  7971. return ERR_PARSE_ERROR;
  7972. } break;
  7973. default: {
  7974. //function or constant variable
  7975. bool is_constant = false;
  7976. bool is_struct = false;
  7977. StringName struct_name;
  7978. DataPrecision precision = PRECISION_DEFAULT;
  7979. DataType type;
  7980. StringName name;
  7981. int array_size = 0;
  7982. if (tk.type == TK_CONST) {
  7983. is_constant = true;
  7984. tk = _get_token();
  7985. }
  7986. if (is_token_precision(tk.type)) {
  7987. precision = get_token_precision(tk.type);
  7988. tk = _get_token();
  7989. }
  7990. if (shader->structs.has(tk.text)) {
  7991. is_struct = true;
  7992. struct_name = tk.text;
  7993. } else {
  7994. #ifdef DEBUG_ENABLED
  7995. if (_lookup_next(next)) {
  7996. if (next.type == TK_UNIFORM) {
  7997. keyword_completion_context = CF_UNIFORM_QUALIFIER;
  7998. }
  7999. }
  8000. #endif // DEBUG_ENABLED
  8001. if (!is_token_datatype(tk.type)) {
  8002. _set_error(RTR("Expected constant, function, uniform or varying."));
  8003. return ERR_PARSE_ERROR;
  8004. }
  8005. if (!is_token_variable_datatype(tk.type)) {
  8006. if (is_constant) {
  8007. _set_error(RTR("Invalid constant type (samplers are not allowed)."));
  8008. } else {
  8009. _set_error(RTR("Invalid function type (samplers are not allowed)."));
  8010. }
  8011. return ERR_PARSE_ERROR;
  8012. }
  8013. }
  8014. if (is_struct) {
  8015. type = TYPE_STRUCT;
  8016. } else {
  8017. type = get_token_datatype(tk.type);
  8018. }
  8019. if (precision != PRECISION_DEFAULT && _validate_precision(type, precision) != OK) {
  8020. return ERR_PARSE_ERROR;
  8021. }
  8022. prev_pos = _get_tkpos();
  8023. tk = _get_token();
  8024. #ifdef DEBUG_ENABLED
  8025. keyword_completion_context = CF_UNSPECIFIED;
  8026. #endif // DEBUG_ENABLED
  8027. bool unknown_size = false;
  8028. bool fixed_array_size = false;
  8029. if (tk.type == TK_BRACKET_OPEN) {
  8030. Error error = _parse_array_size(nullptr, constants, !is_constant, nullptr, &array_size, &unknown_size);
  8031. if (error != OK) {
  8032. return error;
  8033. }
  8034. fixed_array_size = true;
  8035. prev_pos = _get_tkpos();
  8036. }
  8037. _set_tkpos(prev_pos);
  8038. _get_completable_identifier(nullptr, COMPLETION_MAIN_FUNCTION, name);
  8039. if (name == StringName()) {
  8040. if (is_constant) {
  8041. _set_error(RTR("Expected an identifier or '[' after type."));
  8042. } else {
  8043. _set_error(RTR("Expected a function name after type."));
  8044. }
  8045. return ERR_PARSE_ERROR;
  8046. }
  8047. if (shader->structs.has(name) || _find_identifier(nullptr, false, constants, name) || has_builtin(p_functions, name)) {
  8048. _set_redefinition_error(String(name));
  8049. return ERR_PARSE_ERROR;
  8050. }
  8051. tk = _get_token();
  8052. if (tk.type != TK_PARENTHESIS_OPEN) {
  8053. if (type == TYPE_VOID) {
  8054. _set_error(RTR("Expected '(' after function identifier."));
  8055. return ERR_PARSE_ERROR;
  8056. }
  8057. //variable
  8058. while (true) {
  8059. ShaderNode::Constant constant;
  8060. constant.name = name;
  8061. constant.type = is_struct ? TYPE_STRUCT : type;
  8062. constant.type_str = struct_name;
  8063. constant.precision = precision;
  8064. constant.initializer = nullptr;
  8065. constant.array_size = array_size;
  8066. is_const_decl = true;
  8067. if (tk.type == TK_BRACKET_OPEN) {
  8068. Error error = _parse_array_size(nullptr, constants, false, nullptr, &constant.array_size, &unknown_size);
  8069. if (error != OK) {
  8070. return error;
  8071. }
  8072. tk = _get_token();
  8073. }
  8074. if (tk.type == TK_OP_ASSIGN) {
  8075. if (!is_constant) {
  8076. _set_error(vformat(RTR("Global non-constant variables are not supported. Expected '%s' keyword before constant definition."), "const"));
  8077. return ERR_PARSE_ERROR;
  8078. }
  8079. if (constant.array_size > 0 || unknown_size) {
  8080. bool full_def = false;
  8081. VariableDeclarationNode::Declaration decl;
  8082. decl.name = name;
  8083. decl.size = constant.array_size;
  8084. tk = _get_token();
  8085. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  8086. if (unknown_size) {
  8087. _set_expected_error("{");
  8088. return ERR_PARSE_ERROR;
  8089. }
  8090. full_def = true;
  8091. DataPrecision precision2 = PRECISION_DEFAULT;
  8092. if (is_token_precision(tk.type)) {
  8093. precision2 = get_token_precision(tk.type);
  8094. tk = _get_token();
  8095. if (!is_token_nonvoid_datatype(tk.type)) {
  8096. _set_error(RTR("Expected data type after precision modifier."));
  8097. return ERR_PARSE_ERROR;
  8098. }
  8099. }
  8100. StringName struct_name2;
  8101. DataType type2;
  8102. if (shader->structs.has(tk.text)) {
  8103. type2 = TYPE_STRUCT;
  8104. struct_name2 = tk.text;
  8105. } else {
  8106. if (!is_token_variable_datatype(tk.type)) {
  8107. _set_error(RTR("Invalid data type for the array."));
  8108. return ERR_PARSE_ERROR;
  8109. }
  8110. type2 = get_token_datatype(tk.type);
  8111. }
  8112. int array_size2 = 0;
  8113. tk = _get_token();
  8114. if (tk.type == TK_BRACKET_OPEN) {
  8115. bool is_unknown_size = false;
  8116. Error error = _parse_array_size(nullptr, constants, false, nullptr, &array_size2, &is_unknown_size);
  8117. if (error != OK) {
  8118. return error;
  8119. }
  8120. if (is_unknown_size) {
  8121. array_size2 = constant.array_size;
  8122. }
  8123. tk = _get_token();
  8124. } else {
  8125. _set_expected_error("[");
  8126. return ERR_PARSE_ERROR;
  8127. }
  8128. if (constant.precision != precision2 || constant.type != type2 || struct_name != struct_name2 || constant.array_size != array_size2) {
  8129. String from;
  8130. if (type2 == TYPE_STRUCT) {
  8131. from += struct_name2;
  8132. } else {
  8133. if (precision2 != PRECISION_DEFAULT) {
  8134. from += get_precision_name(precision2);
  8135. from += " ";
  8136. }
  8137. from += get_datatype_name(type2);
  8138. }
  8139. from += "[";
  8140. from += itos(array_size2);
  8141. from += "]'";
  8142. String to;
  8143. if (type == TYPE_STRUCT) {
  8144. to += struct_name;
  8145. } else {
  8146. if (precision != PRECISION_DEFAULT) {
  8147. to += get_precision_name(precision);
  8148. to += " ";
  8149. }
  8150. to += get_datatype_name(type);
  8151. }
  8152. to += "[";
  8153. to += itos(constant.array_size);
  8154. to += "]'";
  8155. _set_error(vformat(RTR("Cannot convert from '%s' to '%s'."), from, to));
  8156. return ERR_PARSE_ERROR;
  8157. }
  8158. }
  8159. bool curly = tk.type == TK_CURLY_BRACKET_OPEN;
  8160. if (unknown_size) {
  8161. if (!curly) {
  8162. _set_expected_error("{");
  8163. return ERR_PARSE_ERROR;
  8164. }
  8165. } else {
  8166. if (full_def) {
  8167. if (curly) {
  8168. _set_expected_error("(");
  8169. return ERR_PARSE_ERROR;
  8170. }
  8171. }
  8172. }
  8173. if (tk.type == TK_PARENTHESIS_OPEN || curly) { // initialization
  8174. while (true) {
  8175. Node *n = _parse_and_reduce_expression(nullptr, constants);
  8176. if (!n) {
  8177. return ERR_PARSE_ERROR;
  8178. }
  8179. if (n->type == Node::NODE_TYPE_OPERATOR && static_cast<OperatorNode *>(n)->op == OP_CALL) {
  8180. _set_error(RTR("Expected constant expression."));
  8181. return ERR_PARSE_ERROR;
  8182. }
  8183. if (!_compare_datatypes(constant.type, struct_name, 0, n->get_datatype(), n->get_datatype_name(), 0)) {
  8184. return ERR_PARSE_ERROR;
  8185. }
  8186. tk = _get_token();
  8187. if (tk.type == TK_COMMA) {
  8188. decl.initializer.push_back(n);
  8189. continue;
  8190. } else if (!curly && tk.type == TK_PARENTHESIS_CLOSE) {
  8191. decl.initializer.push_back(n);
  8192. break;
  8193. } else if (curly && tk.type == TK_CURLY_BRACKET_CLOSE) {
  8194. decl.initializer.push_back(n);
  8195. break;
  8196. } else {
  8197. if (curly) {
  8198. _set_expected_error("}", ",");
  8199. } else {
  8200. _set_expected_error(")", ",");
  8201. }
  8202. return ERR_PARSE_ERROR;
  8203. }
  8204. }
  8205. if (unknown_size) {
  8206. decl.size = decl.initializer.size();
  8207. constant.array_size = decl.initializer.size();
  8208. } else if (decl.initializer.size() != constant.array_size) {
  8209. _set_error(RTR("Array size mismatch."));
  8210. return ERR_PARSE_ERROR;
  8211. }
  8212. }
  8213. array_size = constant.array_size;
  8214. ConstantNode *expr = memnew(ConstantNode);
  8215. expr->datatype = constant.type;
  8216. expr->struct_name = constant.type_str;
  8217. expr->array_size = constant.array_size;
  8218. expr->array_declarations.push_back(decl);
  8219. constant.initializer = static_cast<ConstantNode *>(expr);
  8220. } else {
  8221. #ifdef DEBUG_ENABLED
  8222. if (constant.type == DataType::TYPE_BOOL) {
  8223. keyword_completion_context = CF_BOOLEAN;
  8224. }
  8225. #endif // DEBUG_ENABLED
  8226. //variable created with assignment! must parse an expression
  8227. Node *expr = _parse_and_reduce_expression(nullptr, constants);
  8228. if (!expr) {
  8229. return ERR_PARSE_ERROR;
  8230. }
  8231. #ifdef DEBUG_ENABLED
  8232. if (constant.type == DataType::TYPE_BOOL) {
  8233. keyword_completion_context = CF_GLOBAL_SPACE;
  8234. }
  8235. #endif // DEBUG_ENABLED
  8236. if (expr->type == Node::NODE_TYPE_OPERATOR && static_cast<OperatorNode *>(expr)->op == OP_CALL) {
  8237. OperatorNode *op = static_cast<OperatorNode *>(expr);
  8238. for (int i = 1; i < op->arguments.size(); i++) {
  8239. if (!_check_node_constness(op->arguments[i])) {
  8240. _set_error(vformat(RTR("Expected constant expression for argument %d of function call after '='."), i - 1));
  8241. return ERR_PARSE_ERROR;
  8242. }
  8243. }
  8244. }
  8245. constant.initializer = static_cast<ConstantNode *>(expr);
  8246. if (!_compare_datatypes(type, struct_name, 0, expr->get_datatype(), expr->get_datatype_name(), expr->get_array_size())) {
  8247. return ERR_PARSE_ERROR;
  8248. }
  8249. }
  8250. tk = _get_token();
  8251. } else {
  8252. if (constant.array_size > 0 || unknown_size) {
  8253. _set_error(RTR("Expected array initialization."));
  8254. return ERR_PARSE_ERROR;
  8255. } else {
  8256. _set_error(RTR("Expected initialization of constant."));
  8257. return ERR_PARSE_ERROR;
  8258. }
  8259. }
  8260. shader->constants[name] = constant;
  8261. shader->vconstants.push_back(constant);
  8262. #ifdef DEBUG_ENABLED
  8263. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_CONSTANT_FLAG)) {
  8264. used_constants.insert(name, Usage(tk_line));
  8265. }
  8266. #endif // DEBUG_ENABLED
  8267. if (tk.type == TK_COMMA) {
  8268. tk = _get_token();
  8269. if (tk.type != TK_IDENTIFIER) {
  8270. _set_error(RTR("Expected an identifier after type."));
  8271. return ERR_PARSE_ERROR;
  8272. }
  8273. name = tk.text;
  8274. if (_find_identifier(nullptr, false, constants, name)) {
  8275. _set_redefinition_error(String(name));
  8276. return ERR_PARSE_ERROR;
  8277. }
  8278. if (has_builtin(p_functions, name)) {
  8279. _set_redefinition_error(String(name));
  8280. return ERR_PARSE_ERROR;
  8281. }
  8282. tk = _get_token();
  8283. if (!fixed_array_size) {
  8284. array_size = 0;
  8285. }
  8286. unknown_size = false;
  8287. } else if (tk.type == TK_SEMICOLON) {
  8288. is_const_decl = false;
  8289. break;
  8290. } else {
  8291. _set_expected_error(",", ";");
  8292. return ERR_PARSE_ERROR;
  8293. }
  8294. }
  8295. break;
  8296. }
  8297. FunctionInfo builtins;
  8298. if (p_functions.has(name)) {
  8299. builtins = p_functions[name];
  8300. }
  8301. if (p_functions.has("global")) { // Adds global variables: 'TIME'
  8302. for (const KeyValue<StringName, BuiltInInfo> &E : p_functions["global"].built_ins) {
  8303. builtins.built_ins.insert(E.key, E.value);
  8304. }
  8305. }
  8306. if (p_functions.has("constants")) { // Adds global constants: 'PI', 'TAU', 'E'
  8307. for (const KeyValue<StringName, BuiltInInfo> &E : p_functions["constants"].built_ins) {
  8308. builtins.built_ins.insert(E.key, E.value);
  8309. }
  8310. }
  8311. for (int i = 0; i < shader->functions.size(); i++) {
  8312. if (!shader->functions[i].callable && shader->functions[i].name == name) {
  8313. _set_redefinition_error(String(name));
  8314. return ERR_PARSE_ERROR;
  8315. }
  8316. }
  8317. ShaderNode::Function function;
  8318. function.callable = !p_functions.has(name);
  8319. function.name = name;
  8320. FunctionNode *func_node = alloc_node<FunctionNode>();
  8321. function.function = func_node;
  8322. shader->functions.push_back(function);
  8323. func_node->name = name;
  8324. func_node->return_type = type;
  8325. func_node->return_struct_name = struct_name;
  8326. func_node->return_precision = precision;
  8327. func_node->return_array_size = array_size;
  8328. if (p_functions.has(name)) {
  8329. func_node->can_discard = p_functions[name].can_discard;
  8330. } else {
  8331. #ifdef DEBUG_ENABLED
  8332. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_FUNCTION_FLAG)) {
  8333. used_functions.insert(name, Usage(tk_line));
  8334. }
  8335. #endif // DEBUG_ENABLED
  8336. }
  8337. func_node->body = alloc_node<BlockNode>();
  8338. func_node->body->parent_function = func_node;
  8339. tk = _get_token();
  8340. while (true) {
  8341. if (tk.type == TK_PARENTHESIS_CLOSE) {
  8342. break;
  8343. }
  8344. #ifdef DEBUG_ENABLED
  8345. keyword_completion_context = CF_CONST_KEYWORD | CF_FUNC_DECL_PARAM_SPEC | CF_PRECISION_MODIFIER | CF_FUNC_DECL_PARAM_TYPE; // eg. const in mediump float
  8346. if (_lookup_next(next)) {
  8347. if (next.type == TK_CONST) {
  8348. keyword_completion_context = CF_UNSPECIFIED;
  8349. } else if (is_token_arg_qual(next.type)) {
  8350. keyword_completion_context = CF_CONST_KEYWORD;
  8351. } else if (is_token_precision(next.type)) {
  8352. keyword_completion_context = (CF_CONST_KEYWORD | CF_FUNC_DECL_PARAM_SPEC | CF_FUNC_DECL_PARAM_TYPE);
  8353. } else if (is_token_datatype(next.type)) {
  8354. keyword_completion_context = (CF_CONST_KEYWORD | CF_FUNC_DECL_PARAM_SPEC | CF_PRECISION_MODIFIER);
  8355. }
  8356. }
  8357. #endif // DEBUG_ENABLED
  8358. bool param_is_const = false;
  8359. if (tk.type == TK_CONST) {
  8360. param_is_const = true;
  8361. tk = _get_token();
  8362. #ifdef DEBUG_ENABLED
  8363. if (keyword_completion_context & CF_CONST_KEYWORD) {
  8364. keyword_completion_context ^= CF_CONST_KEYWORD;
  8365. }
  8366. if (_lookup_next(next)) {
  8367. if (is_token_arg_qual(next.type)) {
  8368. keyword_completion_context = CF_UNSPECIFIED;
  8369. } else if (is_token_precision(next.type)) {
  8370. keyword_completion_context = (CF_FUNC_DECL_PARAM_SPEC | CF_FUNC_DECL_PARAM_TYPE);
  8371. } else if (is_token_datatype(next.type)) {
  8372. keyword_completion_context = (CF_FUNC_DECL_PARAM_SPEC | CF_PRECISION_MODIFIER);
  8373. }
  8374. }
  8375. #endif // DEBUG_ENABLED
  8376. }
  8377. ArgumentQualifier param_qualifier = ARGUMENT_QUALIFIER_IN;
  8378. if (is_token_arg_qual(tk.type)) {
  8379. bool error = false;
  8380. switch (tk.type) {
  8381. case TK_ARG_IN: {
  8382. param_qualifier = ARGUMENT_QUALIFIER_IN;
  8383. } break;
  8384. case TK_ARG_OUT: {
  8385. if (param_is_const) {
  8386. _set_error(vformat(RTR("The '%s' qualifier cannot be used within a function parameter declared with '%s'."), "out", "const"));
  8387. error = true;
  8388. }
  8389. param_qualifier = ARGUMENT_QUALIFIER_OUT;
  8390. } break;
  8391. case TK_ARG_INOUT: {
  8392. if (param_is_const) {
  8393. _set_error(vformat(RTR("The '%s' qualifier cannot be used within a function parameter declared with '%s'."), "inout", "const"));
  8394. error = true;
  8395. }
  8396. param_qualifier = ARGUMENT_QUALIFIER_INOUT;
  8397. } break;
  8398. default:
  8399. error = true;
  8400. break;
  8401. }
  8402. tk = _get_token();
  8403. #ifdef DEBUG_ENABLED
  8404. if (keyword_completion_context & CF_CONST_KEYWORD) {
  8405. keyword_completion_context ^= CF_CONST_KEYWORD;
  8406. }
  8407. if (keyword_completion_context & CF_FUNC_DECL_PARAM_SPEC) {
  8408. keyword_completion_context ^= CF_FUNC_DECL_PARAM_SPEC;
  8409. }
  8410. if (_lookup_next(next)) {
  8411. if (is_token_precision(next.type)) {
  8412. keyword_completion_context = CF_FUNC_DECL_PARAM_TYPE;
  8413. } else if (is_token_datatype(next.type)) {
  8414. keyword_completion_context = CF_PRECISION_MODIFIER;
  8415. }
  8416. }
  8417. #endif // DEBUG_ENABLED
  8418. if (error) {
  8419. return ERR_PARSE_ERROR;
  8420. }
  8421. }
  8422. DataType param_type;
  8423. StringName param_name;
  8424. StringName param_struct_name;
  8425. DataPrecision param_precision = PRECISION_DEFAULT;
  8426. int arg_array_size = 0;
  8427. if (is_token_precision(tk.type)) {
  8428. param_precision = get_token_precision(tk.type);
  8429. tk = _get_token();
  8430. #ifdef DEBUG_ENABLED
  8431. if (keyword_completion_context & CF_CONST_KEYWORD) {
  8432. keyword_completion_context ^= CF_CONST_KEYWORD;
  8433. }
  8434. if (keyword_completion_context & CF_FUNC_DECL_PARAM_SPEC) {
  8435. keyword_completion_context ^= CF_FUNC_DECL_PARAM_SPEC;
  8436. }
  8437. if (keyword_completion_context & CF_PRECISION_MODIFIER) {
  8438. keyword_completion_context ^= CF_PRECISION_MODIFIER;
  8439. }
  8440. if (_lookup_next(next)) {
  8441. if (is_token_datatype(next.type)) {
  8442. keyword_completion_context = CF_UNSPECIFIED;
  8443. }
  8444. }
  8445. #endif // DEBUG_ENABLED
  8446. }
  8447. is_struct = false;
  8448. if (shader->structs.has(tk.text)) {
  8449. is_struct = true;
  8450. param_struct_name = tk.text;
  8451. #ifdef DEBUG_ENABLED
  8452. if (check_warnings && HAS_WARNING(ShaderWarning::UNUSED_STRUCT_FLAG) && used_structs.has(param_struct_name)) {
  8453. used_structs[param_struct_name].used = true;
  8454. }
  8455. #endif // DEBUG_ENABLED
  8456. }
  8457. if (!is_struct && !is_token_datatype(tk.type)) {
  8458. _set_error(RTR("Expected a valid data type for argument."));
  8459. return ERR_PARSE_ERROR;
  8460. }
  8461. if (param_qualifier == ARGUMENT_QUALIFIER_OUT || param_qualifier == ARGUMENT_QUALIFIER_INOUT) {
  8462. if (is_sampler_type(get_token_datatype(tk.type))) {
  8463. _set_error(RTR("Opaque types cannot be output parameters."));
  8464. return ERR_PARSE_ERROR;
  8465. }
  8466. }
  8467. if (is_struct) {
  8468. param_type = TYPE_STRUCT;
  8469. } else {
  8470. param_type = get_token_datatype(tk.type);
  8471. if (param_type == TYPE_VOID) {
  8472. _set_error(RTR("Void type not allowed as argument."));
  8473. return ERR_PARSE_ERROR;
  8474. }
  8475. }
  8476. if (param_precision != PRECISION_DEFAULT && _validate_precision(param_type, param_precision) != OK) {
  8477. return ERR_PARSE_ERROR;
  8478. }
  8479. #ifdef DEBUG_ENABLED
  8480. keyword_completion_context = CF_UNSPECIFIED;
  8481. #endif // DEBUG_ENABLED
  8482. tk = _get_token();
  8483. if (tk.type == TK_BRACKET_OPEN) {
  8484. Error error = _parse_array_size(nullptr, constants, true, nullptr, &arg_array_size, nullptr);
  8485. if (error != OK) {
  8486. return error;
  8487. }
  8488. tk = _get_token();
  8489. }
  8490. if (tk.type != TK_IDENTIFIER) {
  8491. _set_error(RTR("Expected an identifier for argument name."));
  8492. return ERR_PARSE_ERROR;
  8493. }
  8494. param_name = tk.text;
  8495. ShaderLanguage::IdentifierType itype;
  8496. if (_find_identifier(func_node->body, false, builtins, param_name, (ShaderLanguage::DataType *)nullptr, &itype)) {
  8497. if (itype != IDENTIFIER_FUNCTION) {
  8498. _set_redefinition_error(String(param_name));
  8499. return ERR_PARSE_ERROR;
  8500. }
  8501. }
  8502. if (has_builtin(p_functions, param_name)) {
  8503. _set_redefinition_error(String(param_name));
  8504. return ERR_PARSE_ERROR;
  8505. }
  8506. FunctionNode::Argument arg;
  8507. arg.type = param_type;
  8508. arg.name = param_name;
  8509. arg.type_str = param_struct_name;
  8510. arg.precision = param_precision;
  8511. arg.qualifier = param_qualifier;
  8512. arg.tex_argument_check = false;
  8513. arg.tex_builtin_check = false;
  8514. arg.tex_argument_filter = FILTER_DEFAULT;
  8515. arg.tex_argument_repeat = REPEAT_DEFAULT;
  8516. arg.is_const = param_is_const;
  8517. tk = _get_token();
  8518. if (tk.type == TK_BRACKET_OPEN) {
  8519. Error error = _parse_array_size(nullptr, constants, true, nullptr, &arg_array_size, nullptr);
  8520. if (error != OK) {
  8521. return error;
  8522. }
  8523. tk = _get_token();
  8524. }
  8525. arg.array_size = arg_array_size;
  8526. func_node->arguments.push_back(arg);
  8527. if (tk.type == TK_COMMA) {
  8528. tk = _get_token();
  8529. //do none and go on
  8530. } else if (tk.type != TK_PARENTHESIS_CLOSE) {
  8531. _set_expected_error(",", ")");
  8532. return ERR_PARSE_ERROR;
  8533. }
  8534. }
  8535. if (p_functions.has(name)) {
  8536. //if one of the core functions, make sure they are of the correct form
  8537. if (func_node->arguments.size() > 0) {
  8538. _set_error(vformat(RTR("Function '%s' expects no arguments."), String(name)));
  8539. return ERR_PARSE_ERROR;
  8540. }
  8541. if (func_node->return_type != TYPE_VOID) {
  8542. _set_error(vformat(RTR("Function '%s' must be of '%s' return type."), String(name), "void"));
  8543. return ERR_PARSE_ERROR;
  8544. }
  8545. }
  8546. //all good let's parse inside the function!
  8547. tk = _get_token();
  8548. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  8549. _set_error(RTR("Expected a '{' to begin function."));
  8550. return ERR_PARSE_ERROR;
  8551. }
  8552. current_function = name;
  8553. #ifdef DEBUG_ENABLED
  8554. keyword_completion_context = CF_BLOCK;
  8555. #endif // DEBUG_ENABLED
  8556. Error err = _parse_block(func_node->body, builtins);
  8557. if (err) {
  8558. return err;
  8559. }
  8560. #ifdef DEBUG_ENABLED
  8561. keyword_completion_context = CF_GLOBAL_SPACE;
  8562. #endif // DEBUG_ENABLED
  8563. if (func_node->return_type != DataType::TYPE_VOID) {
  8564. BlockNode *block = func_node->body;
  8565. if (_find_last_flow_op_in_block(block, FlowOperation::FLOW_OP_RETURN) != OK) {
  8566. _set_error(vformat(RTR("Expected at least one '%s' statement in a non-void function."), "return"));
  8567. return ERR_PARSE_ERROR;
  8568. }
  8569. }
  8570. current_function = StringName();
  8571. }
  8572. }
  8573. tk = _get_token();
  8574. }
  8575. #ifdef DEBUG_ENABLED
  8576. if (check_device_limit_warnings && uniform_buffer_exceeded_line != -1) {
  8577. _add_warning(ShaderWarning::DEVICE_LIMIT_EXCEEDED, uniform_buffer_exceeded_line, RTR("uniform buffer"), { uniform_buffer_size, max_uniform_buffer_size });
  8578. }
  8579. #endif // DEBUG_ENABLED
  8580. return OK;
  8581. }
  8582. bool ShaderLanguage::has_builtin(const HashMap<StringName, ShaderLanguage::FunctionInfo> &p_functions, const StringName &p_name) {
  8583. for (const KeyValue<StringName, ShaderLanguage::FunctionInfo> &E : p_functions) {
  8584. if (E.value.built_ins.has(p_name)) {
  8585. return true;
  8586. }
  8587. }
  8588. return false;
  8589. }
  8590. Error ShaderLanguage::_find_last_flow_op_in_op(ControlFlowNode *p_flow, FlowOperation p_op) {
  8591. bool found = false;
  8592. for (int i = p_flow->blocks.size() - 1; i >= 0; i--) {
  8593. if (p_flow->blocks[i]->type == Node::NODE_TYPE_BLOCK) {
  8594. BlockNode *last_block = static_cast<BlockNode *>(p_flow->blocks[i]);
  8595. if (_find_last_flow_op_in_block(last_block, p_op) == OK) {
  8596. found = true;
  8597. break;
  8598. }
  8599. }
  8600. }
  8601. if (found) {
  8602. return OK;
  8603. }
  8604. return FAILED;
  8605. }
  8606. Error ShaderLanguage::_find_last_flow_op_in_block(BlockNode *p_block, FlowOperation p_op) {
  8607. bool found = false;
  8608. for (int i = p_block->statements.size() - 1; i >= 0; i--) {
  8609. if (p_block->statements[i]->type == Node::NODE_TYPE_CONTROL_FLOW) {
  8610. ControlFlowNode *flow = static_cast<ControlFlowNode *>(p_block->statements[i]);
  8611. if (flow->flow_op == p_op) {
  8612. found = true;
  8613. break;
  8614. } else {
  8615. if (_find_last_flow_op_in_op(flow, p_op) == OK) {
  8616. found = true;
  8617. break;
  8618. }
  8619. }
  8620. } else if (p_block->statements[i]->type == Node::NODE_TYPE_BLOCK) {
  8621. BlockNode *block = static_cast<BlockNode *>(p_block->statements[i]);
  8622. if (_find_last_flow_op_in_block(block, p_op) == OK) {
  8623. found = true;
  8624. break;
  8625. }
  8626. }
  8627. }
  8628. if (found) {
  8629. return OK;
  8630. }
  8631. return FAILED;
  8632. }
  8633. // skips over whitespace and /* */ and // comments
  8634. static int _get_first_ident_pos(const String &p_code) {
  8635. int idx = 0;
  8636. #define GETCHAR(m_idx) (((idx + m_idx) < p_code.length()) ? p_code[idx + m_idx] : char32_t(0))
  8637. while (true) {
  8638. if (GETCHAR(0) == '/' && GETCHAR(1) == '/') {
  8639. idx += 2;
  8640. while (true) {
  8641. if (GETCHAR(0) == 0) {
  8642. return 0;
  8643. }
  8644. if (GETCHAR(0) == '\n') {
  8645. idx++;
  8646. break; // loop
  8647. }
  8648. idx++;
  8649. }
  8650. } else if (GETCHAR(0) == '/' && GETCHAR(1) == '*') {
  8651. idx += 2;
  8652. while (true) {
  8653. if (GETCHAR(0) == 0) {
  8654. return 0;
  8655. }
  8656. if (GETCHAR(0) == '*' && GETCHAR(1) == '/') {
  8657. idx += 2;
  8658. break; // loop
  8659. }
  8660. idx++;
  8661. }
  8662. } else {
  8663. switch (GETCHAR(0)) {
  8664. case ' ':
  8665. case '\t':
  8666. case '\r':
  8667. case '\n': {
  8668. idx++;
  8669. } break; // switch
  8670. default:
  8671. return idx;
  8672. }
  8673. }
  8674. }
  8675. #undef GETCHAR
  8676. }
  8677. String ShaderLanguage::get_shader_type(const String &p_code) {
  8678. bool reading_type = false;
  8679. String cur_identifier;
  8680. for (int i = _get_first_ident_pos(p_code); i < p_code.length(); i++) {
  8681. if (p_code[i] == ';') {
  8682. break;
  8683. } else if (p_code[i] <= 32) {
  8684. if (!cur_identifier.is_empty()) {
  8685. if (!reading_type) {
  8686. if (cur_identifier != "shader_type") {
  8687. return String();
  8688. }
  8689. reading_type = true;
  8690. cur_identifier = String();
  8691. } else {
  8692. return cur_identifier;
  8693. }
  8694. }
  8695. } else {
  8696. cur_identifier += String::chr(p_code[i]);
  8697. }
  8698. }
  8699. if (reading_type) {
  8700. return cur_identifier;
  8701. }
  8702. return String();
  8703. }
  8704. bool ShaderLanguage::is_builtin_func_out_parameter(const String &p_name, int p_param) {
  8705. int i = 0;
  8706. while (builtin_func_out_args[i].name) {
  8707. if (p_name == builtin_func_out_args[i].name) {
  8708. for (int j = 0; j < BuiltinFuncOutArgs::MAX_ARGS; j++) {
  8709. int arg = builtin_func_out_args[i].arguments[j];
  8710. if (arg == p_param) {
  8711. return true;
  8712. }
  8713. if (arg < 0) {
  8714. return false;
  8715. }
  8716. }
  8717. }
  8718. i++;
  8719. }
  8720. return false;
  8721. }
  8722. #ifdef DEBUG_ENABLED
  8723. void ShaderLanguage::_check_warning_accums() {
  8724. for (const KeyValue<ShaderWarning::Code, HashMap<StringName, HashMap<StringName, Usage>> *> &E : warnings_check_map2) {
  8725. for (const KeyValue<StringName, HashMap<StringName, Usage>> &T : *E.value) {
  8726. for (const KeyValue<StringName, Usage> &U : T.value) {
  8727. if (!U.value.used) {
  8728. _add_warning(E.key, U.value.decl_line, U.key);
  8729. }
  8730. }
  8731. }
  8732. }
  8733. for (const KeyValue<ShaderWarning::Code, HashMap<StringName, Usage> *> &E : warnings_check_map) {
  8734. for (const KeyValue<StringName, Usage> &U : (*E.value)) {
  8735. if (!U.value.used) {
  8736. _add_warning(E.key, U.value.decl_line, U.key);
  8737. }
  8738. }
  8739. }
  8740. }
  8741. List<ShaderWarning>::Element *ShaderLanguage::get_warnings_ptr() {
  8742. return warnings.front();
  8743. }
  8744. void ShaderLanguage::enable_warning_checking(bool p_enabled) {
  8745. check_warnings = p_enabled;
  8746. }
  8747. bool ShaderLanguage::is_warning_checking_enabled() const {
  8748. return check_warnings;
  8749. }
  8750. void ShaderLanguage::set_warning_flags(uint32_t p_flags) {
  8751. warning_flags = p_flags;
  8752. }
  8753. uint32_t ShaderLanguage::get_warning_flags() const {
  8754. return warning_flags;
  8755. }
  8756. #endif // DEBUG_ENABLED
  8757. Error ShaderLanguage::compile(const String &p_code, const ShaderCompileInfo &p_info) {
  8758. clear();
  8759. is_shader_inc = p_info.is_include;
  8760. code = p_code;
  8761. global_shader_uniform_get_type_func = p_info.global_shader_uniform_type_func;
  8762. varying_function_names = p_info.varying_function_names;
  8763. nodes = nullptr;
  8764. shader = alloc_node<ShaderNode>();
  8765. Error err = _parse_shader(p_info.functions, p_info.render_modes, p_info.shader_types);
  8766. #ifdef DEBUG_ENABLED
  8767. if (check_warnings) {
  8768. _check_warning_accums();
  8769. }
  8770. #endif // DEBUG_ENABLED
  8771. if (err != OK) {
  8772. return err;
  8773. }
  8774. return OK;
  8775. }
  8776. Error ShaderLanguage::complete(const String &p_code, const ShaderCompileInfo &p_info, List<ScriptLanguage::CodeCompletionOption> *r_options, String &r_call_hint) {
  8777. clear();
  8778. is_shader_inc = p_info.is_include;
  8779. code = p_code;
  8780. varying_function_names = p_info.varying_function_names;
  8781. nodes = nullptr;
  8782. global_shader_uniform_get_type_func = p_info.global_shader_uniform_type_func;
  8783. shader = alloc_node<ShaderNode>();
  8784. _parse_shader(p_info.functions, p_info.render_modes, p_info.shader_types);
  8785. #ifdef DEBUG_ENABLED
  8786. // Adds context keywords.
  8787. if (keyword_completion_context != CF_UNSPECIFIED) {
  8788. int sz = sizeof(keyword_list) / sizeof(KeyWord);
  8789. for (int i = 0; i < sz; i++) {
  8790. if (keyword_list[i].flags == CF_UNSPECIFIED) {
  8791. break; // Ignore hint keywords (parsed below).
  8792. }
  8793. if (keyword_list[i].flags & keyword_completion_context) {
  8794. if (keyword_list[i].excluded_shader_types.has(shader_type_identifier)) {
  8795. continue;
  8796. }
  8797. if (!keyword_list[i].functions.is_empty() && !keyword_list[i].functions.has(current_function)) {
  8798. continue;
  8799. }
  8800. ScriptLanguage::CodeCompletionOption option(keyword_list[i].text, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  8801. r_options->push_back(option);
  8802. }
  8803. }
  8804. }
  8805. #endif // DEBUG_ENABLED
  8806. switch (completion_type) {
  8807. case COMPLETION_NONE: {
  8808. //do nothing
  8809. return OK;
  8810. } break;
  8811. case COMPLETION_SHADER_TYPE: {
  8812. for (const String &shader_type : p_info.shader_types) {
  8813. ScriptLanguage::CodeCompletionOption option(shader_type, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  8814. r_options->push_back(option);
  8815. }
  8816. return OK;
  8817. } break;
  8818. case COMPLETION_RENDER_MODE: {
  8819. if (is_shader_inc) {
  8820. for (int i = 0; i < RenderingServer::SHADER_MAX; i++) {
  8821. const Vector<ModeInfo> modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(i));
  8822. for (int j = 0; j < modes.size(); j++) {
  8823. const ModeInfo &info = modes[j];
  8824. if (!info.options.is_empty()) {
  8825. bool found = false;
  8826. for (int k = 0; k < info.options.size(); k++) {
  8827. if (shader->render_modes.has(String(info.name) + "_" + String(info.options[k]))) {
  8828. found = true;
  8829. }
  8830. }
  8831. if (!found) {
  8832. for (int k = 0; k < info.options.size(); k++) {
  8833. ScriptLanguage::CodeCompletionOption option(String(info.name) + "_" + String(info.options[k]), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  8834. r_options->push_back(option);
  8835. }
  8836. }
  8837. } else {
  8838. const String name = String(info.name);
  8839. if (!shader->render_modes.has(name)) {
  8840. ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  8841. r_options->push_back(option);
  8842. }
  8843. }
  8844. }
  8845. }
  8846. } else {
  8847. for (int i = 0; i < p_info.render_modes.size(); i++) {
  8848. const ModeInfo &info = p_info.render_modes[i];
  8849. if (!info.options.is_empty()) {
  8850. bool found = false;
  8851. for (int j = 0; j < info.options.size(); j++) {
  8852. if (shader->render_modes.has(String(info.name) + "_" + String(info.options[j]))) {
  8853. found = true;
  8854. }
  8855. }
  8856. if (!found) {
  8857. for (int j = 0; j < info.options.size(); j++) {
  8858. ScriptLanguage::CodeCompletionOption option(String(info.name) + "_" + String(info.options[j]), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  8859. r_options->push_back(option);
  8860. }
  8861. }
  8862. } else {
  8863. const String name = String(info.name);
  8864. if (!shader->render_modes.has(name)) {
  8865. ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  8866. r_options->push_back(option);
  8867. }
  8868. }
  8869. }
  8870. }
  8871. return OK;
  8872. } break;
  8873. case COMPLETION_STRUCT: {
  8874. if (shader->structs.has(completion_struct)) {
  8875. StructNode *node = shader->structs[completion_struct].shader_struct;
  8876. for (int i = 0; i < node->members.size(); i++) {
  8877. ScriptLanguage::CodeCompletionOption option(node->members[i]->name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER);
  8878. r_options->push_back(option);
  8879. }
  8880. }
  8881. return OK;
  8882. } break;
  8883. case COMPLETION_MAIN_FUNCTION: {
  8884. for (const KeyValue<StringName, FunctionInfo> &E : p_info.functions) {
  8885. if (!E.value.main_function) {
  8886. continue;
  8887. }
  8888. bool found = false;
  8889. for (int i = 0; i < shader->functions.size(); i++) {
  8890. if (shader->functions[i].name == E.key) {
  8891. found = true;
  8892. break;
  8893. }
  8894. }
  8895. if (found) {
  8896. continue;
  8897. }
  8898. ScriptLanguage::CodeCompletionOption option(E.key, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  8899. r_options->push_back(option);
  8900. }
  8901. return OK;
  8902. } break;
  8903. case COMPLETION_IDENTIFIER:
  8904. case COMPLETION_FUNCTION_CALL: {
  8905. bool comp_ident = completion_type == COMPLETION_IDENTIFIER;
  8906. HashMap<String, ScriptLanguage::CodeCompletionKind> matches;
  8907. StringName skip_function;
  8908. BlockNode *block = completion_block;
  8909. if (completion_class == TAG_GLOBAL) {
  8910. while (block) {
  8911. if (comp_ident) {
  8912. for (const KeyValue<StringName, BlockNode::Variable> &E : block->variables) {
  8913. if (E.value.line < completion_line) {
  8914. matches.insert(E.key, ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE);
  8915. }
  8916. }
  8917. }
  8918. if (block->parent_function) {
  8919. if (comp_ident) {
  8920. for (int i = 0; i < block->parent_function->arguments.size(); i++) {
  8921. matches.insert(block->parent_function->arguments[i].name, ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE);
  8922. }
  8923. }
  8924. skip_function = block->parent_function->name;
  8925. }
  8926. block = block->parent_block;
  8927. }
  8928. if (comp_ident) {
  8929. if (is_shader_inc) {
  8930. for (int i = 0; i < RenderingServer::SHADER_MAX; i++) {
  8931. const HashMap<StringName, ShaderLanguage::FunctionInfo> info = ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(i));
  8932. if (info.has("global")) {
  8933. for (const KeyValue<StringName, BuiltInInfo> &E : info["global"].built_ins) {
  8934. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  8935. if (E.value.constant) {
  8936. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  8937. }
  8938. matches.insert(E.key, kind);
  8939. }
  8940. }
  8941. if (info.has("constants")) {
  8942. for (const KeyValue<StringName, BuiltInInfo> &E : info["constants"].built_ins) {
  8943. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  8944. if (E.value.constant) {
  8945. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  8946. }
  8947. matches.insert(E.key, kind);
  8948. }
  8949. }
  8950. if (skip_function != StringName() && info.has(skip_function)) {
  8951. for (const KeyValue<StringName, BuiltInInfo> &E : info[skip_function].built_ins) {
  8952. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  8953. if (E.value.constant) {
  8954. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  8955. }
  8956. matches.insert(E.key, kind);
  8957. }
  8958. }
  8959. }
  8960. } else {
  8961. if (p_info.functions.has("global")) {
  8962. for (const KeyValue<StringName, BuiltInInfo> &E : p_info.functions["global"].built_ins) {
  8963. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  8964. if (E.value.constant) {
  8965. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  8966. }
  8967. matches.insert(E.key, kind);
  8968. }
  8969. }
  8970. if (p_info.functions.has("constants")) {
  8971. for (const KeyValue<StringName, BuiltInInfo> &E : p_info.functions["constants"].built_ins) {
  8972. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  8973. if (E.value.constant) {
  8974. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  8975. }
  8976. matches.insert(E.key, kind);
  8977. }
  8978. }
  8979. if (skip_function != StringName() && p_info.functions.has(skip_function)) {
  8980. for (const KeyValue<StringName, BuiltInInfo> &E : p_info.functions[skip_function].built_ins) {
  8981. ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_MEMBER;
  8982. if (E.value.constant) {
  8983. kind = ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT;
  8984. }
  8985. matches.insert(E.key, kind);
  8986. }
  8987. }
  8988. }
  8989. for (const KeyValue<StringName, ShaderNode::Constant> &E : shader->constants) {
  8990. matches.insert(E.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  8991. }
  8992. for (const KeyValue<StringName, ShaderNode::Varying> &E : shader->varyings) {
  8993. matches.insert(E.key, ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE);
  8994. }
  8995. for (const KeyValue<StringName, ShaderNode::Uniform> &E : shader->uniforms) {
  8996. matches.insert(E.key, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER);
  8997. }
  8998. }
  8999. for (int i = 0; i < shader->functions.size(); i++) {
  9000. if (!shader->functions[i].callable || shader->functions[i].name == skip_function) {
  9001. continue;
  9002. }
  9003. matches.insert(String(shader->functions[i].name), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  9004. }
  9005. int idx = 0;
  9006. bool low_end = RenderingServer::get_singleton()->is_low_end();
  9007. if (stages && stages->has(skip_function)) {
  9008. for (const KeyValue<StringName, StageFunctionInfo> &E : (*stages)[skip_function].stage_functions) {
  9009. matches.insert(String(E.key), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  9010. }
  9011. }
  9012. while (builtin_func_defs[idx].name) {
  9013. if (low_end && builtin_func_defs[idx].high_end) {
  9014. idx++;
  9015. continue;
  9016. }
  9017. matches.insert(String(builtin_func_defs[idx].name), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  9018. idx++;
  9019. }
  9020. } else { // sub-class
  9021. int idx = 0;
  9022. bool low_end = RenderingServer::get_singleton()->is_low_end();
  9023. while (builtin_func_defs[idx].name) {
  9024. if (low_end && builtin_func_defs[idx].high_end) {
  9025. idx++;
  9026. continue;
  9027. }
  9028. if (builtin_func_defs[idx].tag == completion_class) {
  9029. matches.insert(String(builtin_func_defs[idx].name), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  9030. }
  9031. idx++;
  9032. }
  9033. }
  9034. for (const KeyValue<String, ScriptLanguage::CodeCompletionKind> &E : matches) {
  9035. ScriptLanguage::CodeCompletionOption option(E.key, E.value);
  9036. if (E.value == ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION) {
  9037. option.insert_text += "(";
  9038. }
  9039. r_options->push_back(option);
  9040. }
  9041. return OK;
  9042. } break;
  9043. case COMPLETION_CALL_ARGUMENTS: {
  9044. StringName block_function;
  9045. BlockNode *block = completion_block;
  9046. while (block) {
  9047. if (block->parent_function) {
  9048. block_function = block->parent_function->name;
  9049. }
  9050. block = block->parent_block;
  9051. }
  9052. for (int i = 0; i < shader->functions.size(); i++) {
  9053. if (!shader->functions[i].callable) {
  9054. continue;
  9055. }
  9056. if (shader->functions[i].name == completion_function) {
  9057. String calltip;
  9058. calltip += get_datatype_name(shader->functions[i].function->return_type);
  9059. if (shader->functions[i].function->return_array_size > 0) {
  9060. calltip += "[";
  9061. calltip += itos(shader->functions[i].function->return_array_size);
  9062. calltip += "]";
  9063. }
  9064. calltip += " ";
  9065. calltip += shader->functions[i].name;
  9066. calltip += "(";
  9067. for (int j = 0; j < shader->functions[i].function->arguments.size(); j++) {
  9068. if (j > 0) {
  9069. calltip += ", ";
  9070. } else {
  9071. calltip += " ";
  9072. }
  9073. if (j == completion_argument) {
  9074. calltip += char32_t(0xFFFF);
  9075. }
  9076. if (shader->functions[i].function->arguments[j].is_const) {
  9077. calltip += "const ";
  9078. }
  9079. if (shader->functions[i].function->arguments[j].qualifier != ArgumentQualifier::ARGUMENT_QUALIFIER_IN) {
  9080. if (shader->functions[i].function->arguments[j].qualifier == ArgumentQualifier::ARGUMENT_QUALIFIER_OUT) {
  9081. calltip += "out ";
  9082. } else { // ArgumentQualifier::ARGUMENT_QUALIFIER_INOUT
  9083. calltip += "inout ";
  9084. }
  9085. }
  9086. calltip += get_datatype_name(shader->functions[i].function->arguments[j].type);
  9087. calltip += " ";
  9088. calltip += shader->functions[i].function->arguments[j].name;
  9089. if (shader->functions[i].function->arguments[j].array_size > 0) {
  9090. calltip += "[";
  9091. calltip += itos(shader->functions[i].function->arguments[j].array_size);
  9092. calltip += "]";
  9093. }
  9094. if (j == completion_argument) {
  9095. calltip += char32_t(0xFFFF);
  9096. }
  9097. }
  9098. if (shader->functions[i].function->arguments.size()) {
  9099. calltip += " ";
  9100. }
  9101. calltip += ")";
  9102. r_call_hint = calltip;
  9103. return OK;
  9104. }
  9105. }
  9106. int idx = 0;
  9107. String calltip;
  9108. bool low_end = RenderingServer::get_singleton()->is_low_end();
  9109. if (stages && stages->has(block_function)) {
  9110. for (const KeyValue<StringName, StageFunctionInfo> &E : (*stages)[block_function].stage_functions) {
  9111. if (completion_function == E.key) {
  9112. calltip += get_datatype_name(E.value.return_type);
  9113. calltip += " ";
  9114. calltip += E.key;
  9115. calltip += "(";
  9116. for (int i = 0; i < E.value.arguments.size(); i++) {
  9117. if (i > 0) {
  9118. calltip += ", ";
  9119. } else {
  9120. calltip += " ";
  9121. }
  9122. if (i == completion_argument) {
  9123. calltip += char32_t(0xFFFF);
  9124. }
  9125. calltip += get_datatype_name(E.value.arguments[i].type);
  9126. calltip += " ";
  9127. calltip += E.value.arguments[i].name;
  9128. if (i == completion_argument) {
  9129. calltip += char32_t(0xFFFF);
  9130. }
  9131. }
  9132. if (E.value.arguments.size()) {
  9133. calltip += " ";
  9134. }
  9135. calltip += ")";
  9136. r_call_hint = calltip;
  9137. return OK;
  9138. }
  9139. }
  9140. }
  9141. while (builtin_func_defs[idx].name) {
  9142. if (low_end && builtin_func_defs[idx].high_end) {
  9143. idx++;
  9144. continue;
  9145. }
  9146. int idx2 = 0;
  9147. HashSet<int> out_args;
  9148. while (builtin_func_out_args[idx2].name != nullptr) {
  9149. if (builtin_func_out_args[idx2].name == builtin_func_defs[idx].name) {
  9150. for (int i = 0; i < BuiltinFuncOutArgs::MAX_ARGS; i++) {
  9151. int arg = builtin_func_out_args[idx2].arguments[i];
  9152. if (arg == -1) {
  9153. break;
  9154. }
  9155. out_args.insert(arg);
  9156. }
  9157. break;
  9158. }
  9159. idx2++;
  9160. }
  9161. if (completion_function == builtin_func_defs[idx].name) {
  9162. if (builtin_func_defs[idx].tag != completion_class) {
  9163. idx++;
  9164. continue;
  9165. }
  9166. if (calltip.length()) {
  9167. calltip += "\n";
  9168. }
  9169. calltip += get_datatype_name(builtin_func_defs[idx].rettype);
  9170. calltip += " ";
  9171. calltip += builtin_func_defs[idx].name;
  9172. calltip += "(";
  9173. bool found_arg = false;
  9174. for (int i = 0; i < BuiltinFuncDef::MAX_ARGS - 1; i++) {
  9175. if (builtin_func_defs[idx].args[i] == TYPE_VOID) {
  9176. break;
  9177. }
  9178. if (i > 0) {
  9179. calltip += ", ";
  9180. } else {
  9181. calltip += " ";
  9182. }
  9183. if (i == completion_argument) {
  9184. calltip += char32_t(0xFFFF);
  9185. }
  9186. if (out_args.has(i)) {
  9187. calltip += "out ";
  9188. }
  9189. calltip += get_datatype_name(builtin_func_defs[idx].args[i]);
  9190. String arg_name = (String)builtin_func_defs[idx].args_names[i];
  9191. if (!arg_name.is_empty()) {
  9192. calltip += " ";
  9193. calltip += arg_name;
  9194. }
  9195. if (i == completion_argument) {
  9196. calltip += char32_t(0xFFFF);
  9197. }
  9198. found_arg = true;
  9199. }
  9200. if (found_arg) {
  9201. calltip += " ";
  9202. }
  9203. calltip += ")";
  9204. }
  9205. idx++;
  9206. }
  9207. r_call_hint = calltip;
  9208. return OK;
  9209. } break;
  9210. case COMPLETION_INDEX: {
  9211. const char colv[4] = { 'r', 'g', 'b', 'a' };
  9212. const char coordv[4] = { 'x', 'y', 'z', 'w' };
  9213. const char coordt[4] = { 's', 't', 'p', 'q' };
  9214. int limit = 0;
  9215. switch (completion_base) {
  9216. case TYPE_BVEC2:
  9217. case TYPE_IVEC2:
  9218. case TYPE_UVEC2:
  9219. case TYPE_VEC2: {
  9220. limit = 2;
  9221. } break;
  9222. case TYPE_BVEC3:
  9223. case TYPE_IVEC3:
  9224. case TYPE_UVEC3:
  9225. case TYPE_VEC3: {
  9226. limit = 3;
  9227. } break;
  9228. case TYPE_BVEC4:
  9229. case TYPE_IVEC4:
  9230. case TYPE_UVEC4:
  9231. case TYPE_VEC4: {
  9232. limit = 4;
  9233. } break;
  9234. default: {
  9235. }
  9236. }
  9237. for (int i = 0; i < limit; i++) {
  9238. r_options->push_back(ScriptLanguage::CodeCompletionOption(String::chr(colv[i]), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT));
  9239. r_options->push_back(ScriptLanguage::CodeCompletionOption(String::chr(coordv[i]), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT));
  9240. r_options->push_back(ScriptLanguage::CodeCompletionOption(String::chr(coordt[i]), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT));
  9241. }
  9242. } break;
  9243. case COMPLETION_HINT: {
  9244. if (completion_base == DataType::TYPE_VEC3 || completion_base == DataType::TYPE_VEC4) {
  9245. if (current_uniform_hint == ShaderNode::Uniform::HINT_NONE) {
  9246. ScriptLanguage::CodeCompletionOption option("source_color", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9247. r_options->push_back(option);
  9248. }
  9249. } else if ((completion_base == DataType::TYPE_INT || completion_base == DataType::TYPE_FLOAT) && !completion_base_array) {
  9250. if (current_uniform_hint == ShaderNode::Uniform::HINT_NONE) {
  9251. ScriptLanguage::CodeCompletionOption option("hint_range", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9252. if (completion_base == DataType::TYPE_INT) {
  9253. option.insert_text = "hint_range(0, 100, 1)";
  9254. } else {
  9255. option.insert_text = "hint_range(0.0, 1.0, 0.1)";
  9256. }
  9257. r_options->push_back(option);
  9258. }
  9259. } else if ((int(completion_base) > int(TYPE_MAT4) && int(completion_base) < int(TYPE_STRUCT))) {
  9260. Vector<String> options;
  9261. if (completion_base_array) {
  9262. if (current_uniform_hint == ShaderNode::Uniform::HINT_NONE) {
  9263. options.push_back("source_color");
  9264. }
  9265. } else {
  9266. if (current_uniform_filter == FILTER_DEFAULT) {
  9267. options.push_back("filter_linear");
  9268. options.push_back("filter_linear_mipmap");
  9269. options.push_back("filter_linear_mipmap_anisotropic");
  9270. options.push_back("filter_nearest");
  9271. options.push_back("filter_nearest_mipmap");
  9272. options.push_back("filter_nearest_mipmap_anisotropic");
  9273. }
  9274. if (current_uniform_hint == ShaderNode::Uniform::HINT_NONE) {
  9275. options.push_back("hint_anisotropy");
  9276. options.push_back("hint_default_black");
  9277. options.push_back("hint_default_white");
  9278. options.push_back("hint_default_transparent");
  9279. options.push_back("hint_normal");
  9280. options.push_back("hint_roughness_a");
  9281. options.push_back("hint_roughness_b");
  9282. options.push_back("hint_roughness_g");
  9283. options.push_back("hint_roughness_gray");
  9284. options.push_back("hint_roughness_normal");
  9285. options.push_back("hint_roughness_r");
  9286. options.push_back("hint_screen_texture");
  9287. options.push_back("hint_normal_roughness_texture");
  9288. options.push_back("hint_depth_texture");
  9289. options.push_back("source_color");
  9290. }
  9291. if (current_uniform_repeat == REPEAT_DEFAULT) {
  9292. options.push_back("repeat_enable");
  9293. options.push_back("repeat_disable");
  9294. }
  9295. }
  9296. for (int i = 0; i < options.size(); i++) {
  9297. ScriptLanguage::CodeCompletionOption option(options[i], ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9298. r_options->push_back(option);
  9299. }
  9300. }
  9301. if (!completion_base_array && !current_uniform_instance_index_defined) {
  9302. ScriptLanguage::CodeCompletionOption option("instance_index", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  9303. option.insert_text = "instance_index(0)";
  9304. r_options->push_back(option);
  9305. }
  9306. } break;
  9307. }
  9308. return ERR_PARSE_ERROR;
  9309. }
  9310. String ShaderLanguage::get_error_text() {
  9311. return error_str;
  9312. }
  9313. Vector<ShaderLanguage::FilePosition> ShaderLanguage::get_include_positions() {
  9314. return include_positions;
  9315. }
  9316. int ShaderLanguage::get_error_line() {
  9317. return error_line;
  9318. }
  9319. ShaderLanguage::ShaderNode *ShaderLanguage::get_shader() {
  9320. return shader;
  9321. }
  9322. ShaderLanguage::ShaderLanguage() {
  9323. nodes = nullptr;
  9324. completion_class = TAG_GLOBAL;
  9325. #ifdef DEBUG_ENABLED
  9326. warnings_check_map.insert(ShaderWarning::UNUSED_CONSTANT, &used_constants);
  9327. warnings_check_map.insert(ShaderWarning::UNUSED_FUNCTION, &used_functions);
  9328. warnings_check_map.insert(ShaderWarning::UNUSED_STRUCT, &used_structs);
  9329. warnings_check_map.insert(ShaderWarning::UNUSED_UNIFORM, &used_uniforms);
  9330. warnings_check_map.insert(ShaderWarning::UNUSED_VARYING, &used_varyings);
  9331. warnings_check_map2.insert(ShaderWarning::UNUSED_LOCAL_VARIABLE, &used_local_vars);
  9332. #endif // DEBUG_ENABLED
  9333. }
  9334. ShaderLanguage::~ShaderLanguage() {
  9335. clear();
  9336. }