shader_language.cpp 262 KB

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