shader_language.cpp 340 KB

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