shader_language.cpp 322 KB

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