shader_language.cpp 274 KB

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