rasterizer_storage_gles3.cpp 273 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565
  1. /**************************************************************************/
  2. /* rasterizer_storage_gles3.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "rasterizer_storage_gles3.h"
  31. #include "core/engine.h"
  32. #include "core/os/os.h"
  33. #include "core/project_settings.h"
  34. #include "core/threaded_callable_queue.h"
  35. #include "main/main.h"
  36. #include "rasterizer_canvas_gles3.h"
  37. #include "rasterizer_scene_gles3.h"
  38. #include "servers/visual/visual_server_canvas.h"
  39. #include "servers/visual/visual_server_globals.h"
  40. #include "servers/visual_server.h"
  41. #if defined(IPHONE_ENABLED) || defined(ANDROID_ENABLED)
  42. #include <dlfcn.h>
  43. #endif
  44. #ifdef TOOLS_ENABLED
  45. #include "editor/editor_settings.h"
  46. #endif
  47. /* TEXTURE API */
  48. #define _EXT_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
  49. #define _EXT_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
  50. #define _EXT_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
  51. #define _EXT_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
  52. #define _EXT_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT 0x8A54
  53. #define _EXT_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT 0x8A55
  54. #define _EXT_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT 0x8A56
  55. #define _EXT_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT 0x8A57
  56. #define _EXT_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
  57. #define _EXT_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
  58. #define _EXT_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
  59. #define _EXT_COMPRESSED_LUMINANCE_LATC1_EXT 0x8C70
  60. #define _EXT_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT 0x8C71
  61. #define _EXT_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT 0x8C72
  62. #define _EXT_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT 0x8C73
  63. #define _EXT_COMPRESSED_RED_RGTC1_EXT 0x8DBB
  64. #define _EXT_COMPRESSED_RED_RGTC1 0x8DBB
  65. #define _EXT_COMPRESSED_SIGNED_RED_RGTC1 0x8DBC
  66. #define _EXT_COMPRESSED_RG_RGTC2 0x8DBD
  67. #define _EXT_COMPRESSED_SIGNED_RG_RGTC2 0x8DBE
  68. #define _EXT_COMPRESSED_SIGNED_RED_RGTC1_EXT 0x8DBC
  69. #define _EXT_COMPRESSED_RED_GREEN_RGTC2_EXT 0x8DBD
  70. #define _EXT_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT 0x8DBE
  71. #define _EXT_ETC1_RGB8_OES 0x8D64
  72. #define _EXT_SLUMINANCE_NV 0x8C46
  73. #define _EXT_SLUMINANCE_ALPHA_NV 0x8C44
  74. #define _EXT_SRGB8_NV 0x8C41
  75. #define _EXT_SLUMINANCE8_NV 0x8C47
  76. #define _EXT_SLUMINANCE8_ALPHA8_NV 0x8C45
  77. #define _EXT_COMPRESSED_SRGB_S3TC_DXT1_NV 0x8C4C
  78. #define _EXT_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_NV 0x8C4D
  79. #define _EXT_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_NV 0x8C4E
  80. #define _EXT_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_NV 0x8C4F
  81. #define _EXT_ATC_RGB_AMD 0x8C92
  82. #define _EXT_ATC_RGBA_EXPLICIT_ALPHA_AMD 0x8C93
  83. #define _EXT_ATC_RGBA_INTERPOLATED_ALPHA_AMD 0x87EE
  84. #define _EXT_TEXTURE_CUBE_MAP_SEAMLESS 0x884F
  85. #define _GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
  86. #define _GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
  87. #define _EXT_COMPRESSED_R11_EAC 0x9270
  88. #define _EXT_COMPRESSED_SIGNED_R11_EAC 0x9271
  89. #define _EXT_COMPRESSED_RG11_EAC 0x9272
  90. #define _EXT_COMPRESSED_SIGNED_RG11_EAC 0x9273
  91. #define _EXT_COMPRESSED_RGB8_ETC2 0x9274
  92. #define _EXT_COMPRESSED_SRGB8_ETC2 0x9275
  93. #define _EXT_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
  94. #define _EXT_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
  95. #define _EXT_COMPRESSED_RGBA8_ETC2_EAC 0x9278
  96. #define _EXT_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
  97. #define _EXT_COMPRESSED_RGBA_BPTC_UNORM 0x8E8C
  98. #define _EXT_COMPRESSED_SRGB_ALPHA_BPTC_UNORM 0x8E8D
  99. #define _EXT_COMPRESSED_RGB_BPTC_SIGNED_FLOAT 0x8E8E
  100. #define _EXT_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT 0x8E8F
  101. #define _GL_TEXTURE_EXTERNAL_OES 0x8D65
  102. #ifndef GLES_OVER_GL
  103. #define glClearDepth glClearDepthf
  104. #endif
  105. void glTexStorage2DCustom(GLenum target, GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height, GLenum format, GLenum type) {
  106. #ifdef GLES_OVER_GL
  107. for (int i = 0; i < levels; i++) {
  108. glTexImage2D(target, i, internalformat, width, height, 0, format, type, nullptr);
  109. width = MAX(1, (width / 2));
  110. height = MAX(1, (height / 2));
  111. }
  112. #else
  113. glTexStorage2D(target, levels, internalformat, width, height);
  114. #endif
  115. }
  116. GLuint RasterizerStorageGLES3::system_fbo = 0;
  117. void RasterizerStorageGLES3::GLWrapper::initialize(int p_max_texture_image_units) {
  118. texture_unit_table.create(p_max_texture_image_units);
  119. }
  120. void RasterizerStorageGLES3::GLWrapper::reset() {
  121. for (uint32_t i = 0; i < texture_units_bound.size(); i++) {
  122. ::glActiveTexture(GL_TEXTURE0 + texture_units_bound[i]);
  123. glBindTexture(GL_TEXTURE_2D, 0);
  124. }
  125. texture_units_bound.clear();
  126. texture_unit_table.blank();
  127. }
  128. int32_t RasterizerStorageGLES3::safe_gl_get_integer(unsigned int p_gl_param_name, int32_t p_max_accepted) {
  129. int64_t temp;
  130. glGetInteger64v(p_gl_param_name, &temp);
  131. temp = MIN(temp, (int64_t)p_max_accepted);
  132. return temp;
  133. }
  134. Ref<Image> RasterizerStorageGLES3::_get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, uint32_t p_flags, Image::Format &r_real_format, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed, bool &r_srgb, bool p_force_decompress) const {
  135. r_compressed = false;
  136. r_gl_format = 0;
  137. r_real_format = p_format;
  138. Ref<Image> image = p_image;
  139. r_srgb = false;
  140. bool need_decompress = false;
  141. switch (p_format) {
  142. case Image::FORMAT_L8: {
  143. #ifdef GLES_OVER_GL
  144. r_gl_internal_format = GL_R8;
  145. r_gl_format = GL_RED;
  146. r_gl_type = GL_UNSIGNED_BYTE;
  147. #else
  148. r_gl_internal_format = GL_LUMINANCE;
  149. r_gl_format = GL_LUMINANCE;
  150. r_gl_type = GL_UNSIGNED_BYTE;
  151. #endif
  152. } break;
  153. case Image::FORMAT_LA8: {
  154. #ifdef GLES_OVER_GL
  155. r_gl_internal_format = GL_RG8;
  156. r_gl_format = GL_RG;
  157. r_gl_type = GL_UNSIGNED_BYTE;
  158. #else
  159. r_gl_internal_format = GL_LUMINANCE_ALPHA;
  160. r_gl_format = GL_LUMINANCE_ALPHA;
  161. r_gl_type = GL_UNSIGNED_BYTE;
  162. #endif
  163. } break;
  164. case Image::FORMAT_R8: {
  165. r_gl_internal_format = GL_R8;
  166. r_gl_format = GL_RED;
  167. r_gl_type = GL_UNSIGNED_BYTE;
  168. } break;
  169. case Image::FORMAT_RG8: {
  170. r_gl_internal_format = GL_RG8;
  171. r_gl_format = GL_RG;
  172. r_gl_type = GL_UNSIGNED_BYTE;
  173. } break;
  174. case Image::FORMAT_RGB8: {
  175. r_gl_internal_format = (config.srgb_decode_supported || (p_flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) ? GL_SRGB8 : GL_RGB8;
  176. r_gl_format = GL_RGB;
  177. r_gl_type = GL_UNSIGNED_BYTE;
  178. r_srgb = true;
  179. } break;
  180. case Image::FORMAT_RGBA8: {
  181. r_gl_format = GL_RGBA;
  182. r_gl_internal_format = (config.srgb_decode_supported || (p_flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) ? GL_SRGB8_ALPHA8 : GL_RGBA8;
  183. r_gl_type = GL_UNSIGNED_BYTE;
  184. r_srgb = true;
  185. } break;
  186. case Image::FORMAT_RGBA4444: {
  187. r_gl_internal_format = GL_RGBA4;
  188. r_gl_format = GL_RGBA;
  189. r_gl_type = GL_UNSIGNED_SHORT_4_4_4_4;
  190. } break;
  191. case Image::FORMAT_RGBA5551: {
  192. r_gl_internal_format = GL_RGB5_A1;
  193. r_gl_format = GL_RGBA;
  194. r_gl_type = GL_UNSIGNED_SHORT_5_5_5_1;
  195. } break;
  196. case Image::FORMAT_RF: {
  197. r_gl_internal_format = GL_R32F;
  198. r_gl_format = GL_RED;
  199. r_gl_type = GL_FLOAT;
  200. } break;
  201. case Image::FORMAT_RGF: {
  202. r_gl_internal_format = GL_RG32F;
  203. r_gl_format = GL_RG;
  204. r_gl_type = GL_FLOAT;
  205. } break;
  206. case Image::FORMAT_RGBF: {
  207. r_gl_internal_format = GL_RGB32F;
  208. r_gl_format = GL_RGB;
  209. r_gl_type = GL_FLOAT;
  210. } break;
  211. case Image::FORMAT_RGBAF: {
  212. r_gl_internal_format = GL_RGBA32F;
  213. r_gl_format = GL_RGBA;
  214. r_gl_type = GL_FLOAT;
  215. } break;
  216. case Image::FORMAT_RH: {
  217. r_gl_internal_format = GL_R16F;
  218. r_gl_format = GL_RED;
  219. r_gl_type = GL_HALF_FLOAT;
  220. } break;
  221. case Image::FORMAT_RGH: {
  222. r_gl_internal_format = GL_RG16F;
  223. r_gl_format = GL_RG;
  224. r_gl_type = GL_HALF_FLOAT;
  225. } break;
  226. case Image::FORMAT_RGBH: {
  227. r_gl_internal_format = GL_RGB16F;
  228. r_gl_format = GL_RGB;
  229. r_gl_type = GL_HALF_FLOAT;
  230. } break;
  231. case Image::FORMAT_RGBAH: {
  232. r_gl_internal_format = GL_RGBA16F;
  233. r_gl_format = GL_RGBA;
  234. r_gl_type = GL_HALF_FLOAT;
  235. } break;
  236. case Image::FORMAT_RGBE9995: {
  237. r_gl_internal_format = GL_RGB9_E5;
  238. r_gl_format = GL_RGB;
  239. r_gl_type = GL_UNSIGNED_INT_5_9_9_9_REV;
  240. } break;
  241. case Image::FORMAT_DXT1: {
  242. if (config.s3tc_supported) {
  243. r_gl_internal_format = (config.srgb_decode_supported || (p_flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) ? _EXT_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_NV : _EXT_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  244. r_gl_format = GL_RGBA;
  245. r_gl_type = GL_UNSIGNED_BYTE;
  246. r_compressed = true;
  247. r_srgb = true;
  248. } else {
  249. need_decompress = true;
  250. }
  251. } break;
  252. case Image::FORMAT_DXT3: {
  253. if (config.s3tc_supported) {
  254. r_gl_internal_format = (config.srgb_decode_supported || (p_flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) ? _EXT_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_NV : _EXT_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  255. r_gl_format = GL_RGBA;
  256. r_gl_type = GL_UNSIGNED_BYTE;
  257. r_compressed = true;
  258. r_srgb = true;
  259. } else {
  260. need_decompress = true;
  261. }
  262. } break;
  263. case Image::FORMAT_DXT5: {
  264. if (config.s3tc_supported) {
  265. r_gl_internal_format = (config.srgb_decode_supported || (p_flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) ? _EXT_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_NV : _EXT_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  266. r_gl_format = GL_RGBA;
  267. r_gl_type = GL_UNSIGNED_BYTE;
  268. r_compressed = true;
  269. r_srgb = true;
  270. } else {
  271. need_decompress = true;
  272. }
  273. } break;
  274. case Image::FORMAT_RGTC_R: {
  275. if (config.rgtc_supported) {
  276. r_gl_internal_format = _EXT_COMPRESSED_RED_RGTC1_EXT;
  277. r_gl_format = GL_RGBA;
  278. r_gl_type = GL_UNSIGNED_BYTE;
  279. r_compressed = true;
  280. } else {
  281. need_decompress = true;
  282. }
  283. } break;
  284. case Image::FORMAT_RGTC_RG: {
  285. if (config.rgtc_supported) {
  286. r_gl_internal_format = _EXT_COMPRESSED_RED_GREEN_RGTC2_EXT;
  287. r_gl_format = GL_RGBA;
  288. r_gl_type = GL_UNSIGNED_BYTE;
  289. r_compressed = true;
  290. } else {
  291. need_decompress = true;
  292. }
  293. } break;
  294. case Image::FORMAT_BPTC_RGBA: {
  295. if (config.bptc_supported) {
  296. r_gl_internal_format = (config.srgb_decode_supported || (p_flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) ? _EXT_COMPRESSED_SRGB_ALPHA_BPTC_UNORM : _EXT_COMPRESSED_RGBA_BPTC_UNORM;
  297. r_gl_format = GL_RGBA;
  298. r_gl_type = GL_UNSIGNED_BYTE;
  299. r_compressed = true;
  300. r_srgb = true;
  301. } else {
  302. need_decompress = true;
  303. }
  304. } break;
  305. case Image::FORMAT_BPTC_RGBF: {
  306. if (config.bptc_supported) {
  307. r_gl_internal_format = _EXT_COMPRESSED_RGB_BPTC_SIGNED_FLOAT;
  308. r_gl_format = GL_RGB;
  309. r_gl_type = GL_FLOAT;
  310. r_compressed = true;
  311. } else {
  312. need_decompress = true;
  313. }
  314. } break;
  315. case Image::FORMAT_BPTC_RGBFU: {
  316. if (config.bptc_supported) {
  317. r_gl_internal_format = _EXT_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT;
  318. r_gl_format = GL_RGB;
  319. r_gl_type = GL_FLOAT;
  320. r_compressed = true;
  321. } else {
  322. need_decompress = true;
  323. }
  324. } break;
  325. case Image::FORMAT_PVRTC2: {
  326. if (config.pvrtc_supported) {
  327. r_gl_internal_format = (config.srgb_decode_supported || (p_flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) ? _EXT_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT : _EXT_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  328. r_gl_format = GL_RGBA;
  329. r_gl_type = GL_UNSIGNED_BYTE;
  330. r_compressed = true;
  331. r_srgb = true;
  332. } else {
  333. need_decompress = true;
  334. }
  335. } break;
  336. case Image::FORMAT_PVRTC2A: {
  337. if (config.pvrtc_supported) {
  338. r_gl_internal_format = (config.srgb_decode_supported || (p_flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) ? _EXT_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT : _EXT_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  339. r_gl_format = GL_RGBA;
  340. r_gl_type = GL_UNSIGNED_BYTE;
  341. r_compressed = true;
  342. r_srgb = true;
  343. } else {
  344. need_decompress = true;
  345. }
  346. } break;
  347. case Image::FORMAT_PVRTC4: {
  348. if (config.pvrtc_supported) {
  349. r_gl_internal_format = (config.srgb_decode_supported || (p_flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) ? _EXT_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT : _EXT_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
  350. r_gl_format = GL_RGBA;
  351. r_gl_type = GL_UNSIGNED_BYTE;
  352. r_compressed = true;
  353. r_srgb = true;
  354. } else {
  355. need_decompress = true;
  356. }
  357. } break;
  358. case Image::FORMAT_PVRTC4A: {
  359. if (config.pvrtc_supported) {
  360. r_gl_internal_format = (config.srgb_decode_supported || (p_flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) ? _EXT_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT : _EXT_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
  361. r_gl_format = GL_RGBA;
  362. r_gl_type = GL_UNSIGNED_BYTE;
  363. r_compressed = true;
  364. r_srgb = true;
  365. } else {
  366. need_decompress = true;
  367. }
  368. } break;
  369. case Image::FORMAT_ETC: {
  370. if (config.etc_supported) {
  371. r_gl_internal_format = _EXT_ETC1_RGB8_OES;
  372. r_gl_format = GL_RGBA;
  373. r_gl_type = GL_UNSIGNED_BYTE;
  374. r_compressed = true;
  375. } else {
  376. need_decompress = true;
  377. }
  378. } break;
  379. case Image::FORMAT_ETC2_R11: {
  380. if (config.etc2_supported) {
  381. r_gl_internal_format = _EXT_COMPRESSED_R11_EAC;
  382. r_gl_format = GL_RED;
  383. r_gl_type = GL_UNSIGNED_BYTE;
  384. r_compressed = true;
  385. } else {
  386. need_decompress = true;
  387. }
  388. } break;
  389. case Image::FORMAT_ETC2_R11S: {
  390. if (config.etc2_supported) {
  391. r_gl_internal_format = _EXT_COMPRESSED_SIGNED_R11_EAC;
  392. r_gl_format = GL_RED;
  393. r_gl_type = GL_UNSIGNED_BYTE;
  394. r_compressed = true;
  395. } else {
  396. need_decompress = true;
  397. }
  398. } break;
  399. case Image::FORMAT_ETC2_RG11: {
  400. if (config.etc2_supported) {
  401. r_gl_internal_format = _EXT_COMPRESSED_RG11_EAC;
  402. r_gl_format = GL_RG;
  403. r_gl_type = GL_UNSIGNED_BYTE;
  404. r_compressed = true;
  405. } else {
  406. need_decompress = true;
  407. }
  408. } break;
  409. case Image::FORMAT_ETC2_RG11S: {
  410. if (config.etc2_supported) {
  411. r_gl_internal_format = _EXT_COMPRESSED_SIGNED_RG11_EAC;
  412. r_gl_format = GL_RG;
  413. r_gl_type = GL_UNSIGNED_BYTE;
  414. r_compressed = true;
  415. } else {
  416. need_decompress = true;
  417. }
  418. } break;
  419. case Image::FORMAT_ETC2_RGB8: {
  420. if (config.etc2_supported) {
  421. r_gl_internal_format = (config.srgb_decode_supported || (p_flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) ? _EXT_COMPRESSED_SRGB8_ETC2 : _EXT_COMPRESSED_RGB8_ETC2;
  422. r_gl_format = GL_RGB;
  423. r_gl_type = GL_UNSIGNED_BYTE;
  424. r_compressed = true;
  425. r_srgb = true;
  426. } else {
  427. need_decompress = true;
  428. }
  429. } break;
  430. case Image::FORMAT_ETC2_RGBA8: {
  431. if (config.etc2_supported) {
  432. r_gl_internal_format = (config.srgb_decode_supported || (p_flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) ? _EXT_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC : _EXT_COMPRESSED_RGBA8_ETC2_EAC;
  433. r_gl_format = GL_RGBA;
  434. r_gl_type = GL_UNSIGNED_BYTE;
  435. r_compressed = true;
  436. r_srgb = true;
  437. } else {
  438. need_decompress = true;
  439. }
  440. } break;
  441. case Image::FORMAT_ETC2_RGB8A1: {
  442. if (config.etc2_supported) {
  443. r_gl_internal_format = (config.srgb_decode_supported || (p_flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) ? _EXT_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 : _EXT_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
  444. r_gl_format = GL_RGBA;
  445. r_gl_type = GL_UNSIGNED_BYTE;
  446. r_compressed = true;
  447. r_srgb = true;
  448. } else {
  449. need_decompress = true;
  450. }
  451. } break;
  452. default: {
  453. ERR_FAIL_V(Ref<Image>());
  454. }
  455. }
  456. if (need_decompress || p_force_decompress) {
  457. if (!image.is_null()) {
  458. image = image->duplicate();
  459. image->decompress();
  460. ERR_FAIL_COND_V(image->is_compressed(), image);
  461. image->convert(Image::FORMAT_RGBA8);
  462. }
  463. r_gl_format = GL_RGBA;
  464. r_gl_internal_format = (config.srgb_decode_supported || (p_flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) ? GL_SRGB8_ALPHA8 : GL_RGBA8;
  465. r_gl_type = GL_UNSIGNED_BYTE;
  466. r_compressed = false;
  467. r_real_format = Image::FORMAT_RGBA8;
  468. r_srgb = true;
  469. return image;
  470. }
  471. return image;
  472. }
  473. static const GLenum _cube_side_enum[6] = {
  474. GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
  475. GL_TEXTURE_CUBE_MAP_POSITIVE_X,
  476. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
  477. GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
  478. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
  479. GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
  480. };
  481. RID RasterizerStorageGLES3::texture_create() {
  482. Texture *texture = memnew(Texture);
  483. ERR_FAIL_COND_V(!texture, RID());
  484. glGenTextures(1, &texture->tex_id);
  485. texture->active = false;
  486. texture->total_data_size = 0;
  487. return texture_owner.make_rid(texture);
  488. }
  489. void RasterizerStorageGLES3::texture_allocate(RID p_texture, int p_width, int p_height, int p_depth_3d, Image::Format p_format, VisualServer::TextureType p_type, uint32_t p_flags) {
  490. GLenum format;
  491. GLenum internal_format;
  492. GLenum type;
  493. bool compressed;
  494. bool srgb;
  495. if (p_flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
  496. p_flags &= ~VS::TEXTURE_FLAG_MIPMAPS; // no mipies for video
  497. }
  498. #ifndef GLES_OVER_GL
  499. switch (p_format) {
  500. case Image::FORMAT_RF:
  501. case Image::FORMAT_RGF:
  502. case Image::FORMAT_RGBF:
  503. case Image::FORMAT_RGBAF:
  504. case Image::FORMAT_RH:
  505. case Image::FORMAT_RGH:
  506. case Image::FORMAT_RGBH:
  507. case Image::FORMAT_RGBAH: {
  508. if (!config.texture_float_linear_supported) {
  509. // disable linear texture filtering when not supported for float format on some devices (issue #24295)
  510. p_flags &= ~VS::TEXTURE_FLAG_FILTER;
  511. }
  512. } break;
  513. default: {
  514. }
  515. }
  516. #endif
  517. Texture *texture = texture_owner.get(p_texture);
  518. ERR_FAIL_COND(!texture);
  519. texture->width = p_width;
  520. texture->height = p_height;
  521. texture->depth = p_depth_3d;
  522. texture->format = p_format;
  523. texture->flags = p_flags;
  524. texture->stored_cube_sides = 0;
  525. texture->type = p_type;
  526. switch (p_type) {
  527. case VS::TEXTURE_TYPE_2D: {
  528. texture->target = GL_TEXTURE_2D;
  529. texture->images.resize(1);
  530. } break;
  531. case VS::TEXTURE_TYPE_EXTERNAL: {
  532. #ifdef ANDROID_ENABLED
  533. texture->target = _GL_TEXTURE_EXTERNAL_OES;
  534. #else
  535. texture->target = GL_TEXTURE_2D;
  536. #endif
  537. texture->images.resize(0);
  538. } break;
  539. case VS::TEXTURE_TYPE_CUBEMAP: {
  540. texture->target = GL_TEXTURE_CUBE_MAP;
  541. texture->images.resize(6);
  542. } break;
  543. case VS::TEXTURE_TYPE_2D_ARRAY: {
  544. texture->target = GL_TEXTURE_2D_ARRAY;
  545. texture->images.resize(p_depth_3d);
  546. } break;
  547. case VS::TEXTURE_TYPE_3D: {
  548. texture->target = GL_TEXTURE_3D;
  549. texture->images.resize(p_depth_3d);
  550. } break;
  551. }
  552. if (p_type != VS::TEXTURE_TYPE_EXTERNAL) {
  553. texture->is_npot_repeat_mipmap = false;
  554. #ifdef JAVASCRIPT_ENABLED
  555. // WebGL 2.0 on browsers does not seem to properly support compressed non power-of-two (NPOT)
  556. // textures with repeat/mipmaps, even though NPOT textures should be supported as per the spec.
  557. // Force decompressing them to work it around on WebGL 2.0 at a performance cost (GH-33058).
  558. int po2_width = next_power_of_2(p_width);
  559. int po2_height = next_power_of_2(p_height);
  560. bool is_po2 = p_width == po2_width && p_height == po2_height;
  561. if (!is_po2 && (p_flags & VS::TEXTURE_FLAG_REPEAT || p_flags & VS::TEXTURE_FLAG_MIPMAPS)) {
  562. texture->is_npot_repeat_mipmap = true;
  563. }
  564. #endif // JAVASCRIPT_ENABLED
  565. Image::Format real_format;
  566. _get_gl_image_and_format(Ref<Image>(),
  567. texture->format,
  568. texture->flags,
  569. real_format,
  570. format,
  571. internal_format,
  572. type,
  573. compressed,
  574. srgb,
  575. texture->is_npot_repeat_mipmap);
  576. texture->alloc_width = texture->width;
  577. texture->alloc_height = texture->height;
  578. texture->alloc_depth = texture->depth;
  579. texture->gl_format_cache = format;
  580. texture->gl_type_cache = type;
  581. texture->gl_internal_format_cache = internal_format;
  582. texture->compressed = compressed;
  583. texture->srgb = srgb;
  584. texture->data_size = 0;
  585. texture->mipmaps = 1;
  586. }
  587. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  588. glBindTexture(texture->target, texture->tex_id);
  589. if (p_type == VS::TEXTURE_TYPE_EXTERNAL) {
  590. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  591. glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  592. glTexParameteri(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  593. glTexParameteri(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  594. } else if (p_type == VS::TEXTURE_TYPE_3D || p_type == VS::TEXTURE_TYPE_2D_ARRAY) {
  595. int width = p_width;
  596. int height = p_height;
  597. int depth = p_depth_3d;
  598. int mipmaps = 0;
  599. while (width > 0 || height > 0 || (p_type == VS::TEXTURE_TYPE_3D && depth > 0)) {
  600. width = MAX(1, width);
  601. height = MAX(1, height);
  602. depth = MAX(1, depth);
  603. glTexImage3D(texture->target, mipmaps, internal_format, width, height, depth, 0, format, type, nullptr);
  604. width /= 2;
  605. height /= 2;
  606. if (p_type == VS::TEXTURE_TYPE_3D) {
  607. depth /= 2;
  608. }
  609. mipmaps++;
  610. if (!(p_flags & VS::TEXTURE_FLAG_MIPMAPS)) {
  611. break;
  612. }
  613. }
  614. glTexParameteri(texture->target, GL_TEXTURE_BASE_LEVEL, 0);
  615. glTexParameteri(texture->target, GL_TEXTURE_MAX_LEVEL, mipmaps - 1);
  616. } else if (p_flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
  617. //prealloc if video
  618. glTexImage2D(texture->target, 0, internal_format, p_width, p_height, 0, format, type, nullptr);
  619. }
  620. texture->active = true;
  621. }
  622. void RasterizerStorageGLES3::texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer) {
  623. Texture *texture = texture_owner.get(p_texture);
  624. ERR_FAIL_COND(!texture);
  625. ERR_FAIL_COND(!texture->active);
  626. ERR_FAIL_COND(texture->render_target);
  627. ERR_FAIL_COND(texture->format != p_image->get_format());
  628. ERR_FAIL_COND(p_image.is_null());
  629. ERR_FAIL_COND(texture->type == VS::TEXTURE_TYPE_EXTERNAL);
  630. GLenum type;
  631. GLenum format;
  632. GLenum internal_format;
  633. bool compressed;
  634. bool srgb;
  635. if (config.keep_original_textures && !(texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING)) {
  636. texture->images.write[p_layer] = p_image;
  637. }
  638. Image::Format real_format;
  639. Ref<Image> img = _get_gl_image_and_format(p_image, p_image->get_format(), texture->flags, real_format, format, internal_format, type, compressed, srgb, texture->is_npot_repeat_mipmap);
  640. if (config.shrink_textures_x2 && (p_image->has_mipmaps() || !p_image->is_compressed()) && !(texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING)) {
  641. texture->alloc_height = MAX(1, texture->alloc_height / 2);
  642. texture->alloc_width = MAX(1, texture->alloc_width / 2);
  643. if (texture->alloc_width == img->get_width() / 2 && texture->alloc_height == img->get_height() / 2) {
  644. img->shrink_x2();
  645. } else if (img->get_format() <= Image::FORMAT_RGBA8) {
  646. img->resize(texture->alloc_width, texture->alloc_height, Image::INTERPOLATE_BILINEAR);
  647. }
  648. };
  649. GLenum blit_target = GL_TEXTURE_2D;
  650. switch (texture->type) {
  651. case VS::TEXTURE_TYPE_2D:
  652. case VS::TEXTURE_TYPE_EXTERNAL: {
  653. blit_target = GL_TEXTURE_2D;
  654. } break;
  655. case VS::TEXTURE_TYPE_CUBEMAP: {
  656. ERR_FAIL_INDEX(p_layer, 6);
  657. blit_target = _cube_side_enum[p_layer];
  658. } break;
  659. case VS::TEXTURE_TYPE_2D_ARRAY: {
  660. blit_target = GL_TEXTURE_2D_ARRAY;
  661. } break;
  662. case VS::TEXTURE_TYPE_3D: {
  663. blit_target = GL_TEXTURE_3D;
  664. } break;
  665. }
  666. texture->data_size = img->get_data().size();
  667. PoolVector<uint8_t>::Read read = img->get_data().read();
  668. ERR_FAIL_COND(!read.ptr());
  669. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  670. glBindTexture(texture->target, texture->tex_id);
  671. texture->ignore_mipmaps = compressed && !img->has_mipmaps();
  672. if ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && !texture->ignore_mipmaps) {
  673. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  674. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, config.use_fast_texture_filter ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR_MIPMAP_LINEAR);
  675. } else {
  676. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, config.use_fast_texture_filter ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST_MIPMAP_LINEAR);
  677. }
  678. } else {
  679. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  680. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  681. } else {
  682. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  683. }
  684. }
  685. if (config.srgb_decode_supported && srgb) {
  686. if (texture->flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR) {
  687. glTexParameteri(texture->target, _TEXTURE_SRGB_DECODE_EXT, _DECODE_EXT);
  688. texture->using_srgb = true;
  689. } else {
  690. glTexParameteri(texture->target, _TEXTURE_SRGB_DECODE_EXT, _SKIP_DECODE_EXT);
  691. texture->using_srgb = false;
  692. }
  693. }
  694. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  695. glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtering
  696. } else {
  697. glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // raw Filtering
  698. }
  699. if (((texture->flags & VS::TEXTURE_FLAG_REPEAT) || (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT)) && texture->target != GL_TEXTURE_CUBE_MAP) {
  700. if (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT) {
  701. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
  702. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
  703. } else {
  704. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  705. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  706. }
  707. } else {
  708. //glTexParameterf( texture->target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
  709. glTexParameterf(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  710. glTexParameterf(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  711. }
  712. //set swizle for older format compatibility
  713. #ifdef GLES_OVER_GL
  714. switch (texture->format) {
  715. case Image::FORMAT_L8: {
  716. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_R, GL_RED);
  717. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_G, GL_RED);
  718. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_B, GL_RED);
  719. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_A, GL_ONE);
  720. } break;
  721. case Image::FORMAT_LA8: {
  722. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_R, GL_RED);
  723. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_G, GL_RED);
  724. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_B, GL_RED);
  725. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_A, GL_GREEN);
  726. } break;
  727. default: {
  728. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_R, GL_RED);
  729. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
  730. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
  731. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
  732. } break;
  733. }
  734. #endif
  735. if (config.use_anisotropic_filter) {
  736. if (texture->flags & VS::TEXTURE_FLAG_ANISOTROPIC_FILTER) {
  737. glTexParameterf(texture->target, _GL_TEXTURE_MAX_ANISOTROPY_EXT, config.anisotropic_level);
  738. } else {
  739. glTexParameterf(texture->target, _GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
  740. }
  741. }
  742. int mipmaps = ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && img->has_mipmaps()) ? img->get_mipmap_count() + 1 : 1;
  743. int w = img->get_width();
  744. int h = img->get_height();
  745. int tsize = 0;
  746. for (int i = 0; i < mipmaps; i++) {
  747. int size, ofs;
  748. img->get_mipmap_offset_and_size(i, ofs, size);
  749. if (texture->type == VS::TEXTURE_TYPE_2D || texture->type == VS::TEXTURE_TYPE_CUBEMAP) {
  750. if (texture->compressed) {
  751. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  752. int bw = w;
  753. int bh = h;
  754. glCompressedTexImage2D(blit_target, i, internal_format, bw, bh, 0, size, &read[ofs]);
  755. } else {
  756. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  757. if (texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
  758. glTexSubImage2D(blit_target, i, 0, 0, w, h, format, type, &read[ofs]);
  759. } else {
  760. glTexImage2D(blit_target, i, internal_format, w, h, 0, format, type, &read[ofs]);
  761. }
  762. }
  763. } else {
  764. if (texture->compressed) {
  765. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  766. int bw = w;
  767. int bh = h;
  768. glCompressedTexSubImage3D(blit_target, i, 0, 0, p_layer, bw, bh, 1, internal_format, size, &read[ofs]);
  769. } else {
  770. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  771. glTexSubImage3D(blit_target, i, 0, 0, p_layer, w, h, 1, format, type, &read[ofs]);
  772. }
  773. }
  774. tsize += size;
  775. w = MAX(1, w >> 1);
  776. h = MAX(1, h >> 1);
  777. }
  778. // Handle array and 3D textures, as those set their data per layer.
  779. tsize *= MAX(texture->alloc_depth, 1);
  780. info.texture_mem -= texture->total_data_size;
  781. texture->total_data_size = tsize;
  782. info.texture_mem += texture->total_data_size;
  783. //printf("texture: %i x %i - size: %i - total: %i\n",texture->width,texture->height,tsize,_rinfo.texture_mem);
  784. texture->stored_cube_sides |= (1 << p_layer);
  785. if ((texture->type == VS::TEXTURE_TYPE_2D || texture->type == VS::TEXTURE_TYPE_CUBEMAP) && (texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && mipmaps == 1 && !texture->ignore_mipmaps && (texture->type != VS::TEXTURE_TYPE_CUBEMAP || texture->stored_cube_sides == (1 << 6) - 1)) {
  786. //generate mipmaps if they were requested and the image does not contain them
  787. glGenerateMipmap(texture->target);
  788. } else if (mipmaps > 1) {
  789. glTexParameteri(texture->target, GL_TEXTURE_BASE_LEVEL, 0);
  790. glTexParameteri(texture->target, GL_TEXTURE_MAX_LEVEL, mipmaps - 1);
  791. } else {
  792. glTexParameteri(texture->target, GL_TEXTURE_BASE_LEVEL, 0);
  793. glTexParameteri(texture->target, GL_TEXTURE_MAX_LEVEL, 0);
  794. }
  795. texture->mipmaps = mipmaps;
  796. //texture_set_flags(p_texture,texture->flags);
  797. }
  798. // Uploads pixel data to a sub-region of a texture, for the specified mipmap.
  799. // The texture pixels must have been allocated before, because most features seen in texture_set_data() make no sense in a partial update.
  800. // TODO If we want this to be usable without pre-filling pixels with a full image, we have to call glTexImage2D() with null data.
  801. void RasterizerStorageGLES3::texture_set_data_partial(RID p_texture, const Ref<Image> &p_image, int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, int p_dst_mip, int p_layer) {
  802. Texture *texture = texture_owner.get(p_texture);
  803. ERR_FAIL_COND(!texture);
  804. ERR_FAIL_COND(!texture->active);
  805. ERR_FAIL_COND(texture->render_target);
  806. ERR_FAIL_COND(texture->format != p_image->get_format());
  807. ERR_FAIL_COND(p_image.is_null());
  808. ERR_FAIL_COND(src_w <= 0 || src_h <= 0);
  809. ERR_FAIL_COND(src_x < 0 || src_y < 0 || src_x + src_w > p_image->get_width() || src_y + src_h > p_image->get_height());
  810. ERR_FAIL_COND(dst_x < 0 || dst_y < 0 || dst_x + src_w > texture->alloc_width || dst_y + src_h > texture->alloc_height);
  811. ERR_FAIL_COND(p_dst_mip < 0 || p_dst_mip >= texture->mipmaps);
  812. ERR_FAIL_COND(texture->type == VS::TEXTURE_TYPE_EXTERNAL);
  813. GLenum type;
  814. GLenum format;
  815. GLenum internal_format;
  816. bool compressed;
  817. bool srgb;
  818. // Because OpenGL wants data as a dense array, we have to extract the sub-image if the source rect isn't the full image
  819. Ref<Image> p_sub_img = p_image;
  820. if (src_x > 0 || src_y > 0 || src_w != p_image->get_width() || src_h != p_image->get_height()) {
  821. p_sub_img = p_image->get_rect(Rect2(src_x, src_y, src_w, src_h));
  822. }
  823. Image::Format real_format;
  824. Ref<Image> img = _get_gl_image_and_format(p_sub_img, p_sub_img->get_format(), texture->flags, real_format, format, internal_format, type, compressed, srgb, texture->is_npot_repeat_mipmap);
  825. GLenum blit_target = GL_TEXTURE_2D;
  826. switch (texture->type) {
  827. case VS::TEXTURE_TYPE_2D:
  828. case VS::TEXTURE_TYPE_EXTERNAL: {
  829. blit_target = GL_TEXTURE_2D;
  830. } break;
  831. case VS::TEXTURE_TYPE_CUBEMAP: {
  832. ERR_FAIL_INDEX(p_layer, 6);
  833. blit_target = _cube_side_enum[p_layer];
  834. } break;
  835. case VS::TEXTURE_TYPE_2D_ARRAY: {
  836. blit_target = GL_TEXTURE_2D_ARRAY;
  837. } break;
  838. case VS::TEXTURE_TYPE_3D: {
  839. blit_target = GL_TEXTURE_3D;
  840. } break;
  841. }
  842. PoolVector<uint8_t>::Read read = img->get_data().read();
  843. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  844. glBindTexture(texture->target, texture->tex_id);
  845. int src_data_size = img->get_data().size();
  846. int src_ofs = 0;
  847. if (texture->type == VS::TEXTURE_TYPE_2D || texture->type == VS::TEXTURE_TYPE_CUBEMAP) {
  848. if (texture->compressed) {
  849. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  850. glCompressedTexSubImage2D(blit_target, p_dst_mip, dst_x, dst_y, src_w, src_h, internal_format, src_data_size, &read[src_ofs]);
  851. } else {
  852. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  853. // `format` has to match the internal_format used when the texture was created
  854. glTexSubImage2D(blit_target, p_dst_mip, dst_x, dst_y, src_w, src_h, format, type, &read[src_ofs]);
  855. }
  856. } else {
  857. if (texture->compressed) {
  858. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  859. glCompressedTexSubImage3D(blit_target, p_dst_mip, dst_x, dst_y, p_layer, src_w, src_h, 1, format, src_data_size, &read[src_ofs]);
  860. } else {
  861. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  862. // `format` has to match the internal_format used when the texture was created
  863. glTexSubImage3D(blit_target, p_dst_mip, dst_x, dst_y, p_layer, src_w, src_h, 1, format, type, &read[src_ofs]);
  864. }
  865. }
  866. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  867. glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtering
  868. } else {
  869. glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // raw Filtering
  870. }
  871. }
  872. Ref<Image> RasterizerStorageGLES3::texture_get_data(RID p_texture, int p_layer) const {
  873. Texture *texture = texture_owner.get(p_texture);
  874. ERR_FAIL_COND_V(!texture, Ref<Image>());
  875. ERR_FAIL_COND_V(!texture->active, Ref<Image>());
  876. ERR_FAIL_COND_V(texture->data_size == 0 && !texture->render_target, Ref<Image>());
  877. if (texture->type == VS::TEXTURE_TYPE_CUBEMAP && p_layer < 6 && !texture->images[p_layer].is_null()) {
  878. return texture->images[p_layer];
  879. }
  880. // 3D textures and 2D texture arrays need special treatment, as the glGetTexImage reads **the whole**
  881. // texture to host-memory. 3D textures and 2D texture arrays are potentially very big, so reading
  882. // everything just to throw everything but one layer away is A Bad Idea.
  883. //
  884. // Unfortunately, to solve this, the copy shader has to read the data out via a shader and store it
  885. // in a temporary framebuffer. The data from the framebuffer can then be read using glReadPixels.
  886. if (texture->type == VS::TEXTURE_TYPE_2D_ARRAY || texture->type == VS::TEXTURE_TYPE_3D) {
  887. // can't read a layer that doesn't exist
  888. ERR_FAIL_INDEX_V(p_layer, texture->alloc_depth, Ref<Image>());
  889. // get some information about the texture
  890. Image::Format real_format;
  891. GLenum gl_format;
  892. GLenum gl_internal_format;
  893. GLenum gl_type;
  894. bool compressed;
  895. bool srgb;
  896. _get_gl_image_and_format(
  897. Ref<Image>(),
  898. texture->format,
  899. texture->flags,
  900. real_format,
  901. gl_format,
  902. gl_internal_format,
  903. gl_type,
  904. compressed,
  905. srgb,
  906. texture->is_npot_repeat_mipmap);
  907. PoolVector<uint8_t> data;
  908. // TODO need to decide between RgbaUnorm and RgbaFloat32 for output
  909. int data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, Image::FORMAT_RGBA8, false);
  910. data.resize(data_size * 2); // add some more memory at the end, just in case for buggy drivers
  911. PoolVector<uint8_t>::Write wb = data.write();
  912. // generate temporary resources
  913. GLuint tmp_fbo;
  914. glGenFramebuffers(1, &tmp_fbo);
  915. GLuint tmp_color_attachment;
  916. glGenTextures(1, &tmp_color_attachment);
  917. // now bring the OpenGL context into the correct state
  918. {
  919. glBindFramebuffer(GL_FRAMEBUFFER, tmp_fbo);
  920. // back color attachment with memory, then set properties
  921. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  922. glBindTexture(GL_TEXTURE_2D, tmp_color_attachment);
  923. // TODO support HDR properly
  924. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->alloc_width, texture->alloc_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  925. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  926. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  927. // use the color texture as color attachment for this render pass
  928. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tmp_color_attachment, 0);
  929. // more GL state, wheeeey
  930. glDepthMask(GL_FALSE);
  931. glDisable(GL_DEPTH_TEST);
  932. glDisable(GL_CULL_FACE);
  933. glDisable(GL_BLEND);
  934. glDepthFunc(GL_LEQUAL);
  935. glColorMask(1, 1, 1, 1);
  936. // use volume tex for reading
  937. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  938. glBindTexture(texture->target, texture->tex_id);
  939. glViewport(0, 0, texture->alloc_width, texture->alloc_height);
  940. // set up copy shader for proper use
  941. shaders.copy.set_conditional(CopyShaderGLES3::LINEAR_TO_SRGB, !srgb);
  942. shaders.copy.set_conditional(CopyShaderGLES3::USE_TEXTURE3D, texture->type == VS::TEXTURE_TYPE_3D);
  943. shaders.copy.set_conditional(CopyShaderGLES3::USE_TEXTURE2DARRAY, texture->type == VS::TEXTURE_TYPE_2D_ARRAY);
  944. shaders.copy.bind();
  945. float layer;
  946. if (texture->type == VS::TEXTURE_TYPE_2D_ARRAY) {
  947. layer = (float)p_layer;
  948. } else {
  949. // calculate the normalized z coordinate for the layer
  950. layer = (float)p_layer / (float)texture->alloc_depth;
  951. }
  952. shaders.copy.set_uniform(CopyShaderGLES3::LAYER, layer);
  953. glBindVertexArray(resources.quadie_array);
  954. }
  955. // clear color attachment, then perform copy
  956. glClearColor(0.0, 0.0, 0.0, 0.0);
  957. glClear(GL_COLOR_BUFFER_BIT);
  958. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  959. // read the image into the host buffer
  960. glReadPixels(0, 0, texture->alloc_width, texture->alloc_height, GL_RGBA, GL_UNSIGNED_BYTE, &wb[0]);
  961. // remove temp resources and unset some GL state
  962. {
  963. shaders.copy.set_conditional(CopyShaderGLES3::USE_TEXTURE3D, false);
  964. shaders.copy.set_conditional(CopyShaderGLES3::USE_TEXTURE2DARRAY, false);
  965. shaders.copy.set_conditional(CopyShaderGLES3::LINEAR_TO_SRGB, false);
  966. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  967. glDeleteTextures(1, &tmp_color_attachment);
  968. glDeleteFramebuffers(1, &tmp_fbo);
  969. }
  970. wb.release();
  971. data.resize(data_size);
  972. Image *img = memnew(Image(texture->alloc_width, texture->alloc_height, false, Image::FORMAT_RGBA8, data));
  973. if (!texture->compressed) {
  974. img->convert(real_format);
  975. }
  976. return Ref<Image>(img);
  977. }
  978. #ifdef GLES_OVER_GL
  979. Image::Format real_format;
  980. GLenum gl_format;
  981. GLenum gl_internal_format;
  982. GLenum gl_type;
  983. bool compressed;
  984. bool srgb;
  985. _get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, real_format, gl_format, gl_internal_format, gl_type, compressed, srgb, false);
  986. PoolVector<uint8_t> data;
  987. int data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, real_format, texture->mipmaps > 1);
  988. data.resize(data_size * 2); //add some memory at the end, just in case for buggy drivers
  989. PoolVector<uint8_t>::Write wb = data.write();
  990. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  991. glBindTexture(texture->target, texture->tex_id);
  992. glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
  993. for (int i = 0; i < texture->mipmaps; i++) {
  994. int ofs = Image::get_image_mipmap_offset(texture->alloc_width, texture->alloc_height, real_format, i);
  995. if (texture->compressed) {
  996. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  997. glGetCompressedTexImage(texture->target, i, &wb[ofs]);
  998. } else {
  999. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  1000. glGetTexImage(texture->target, i, texture->gl_format_cache, texture->gl_type_cache, &wb[ofs]);
  1001. }
  1002. }
  1003. Image::Format img_format;
  1004. //convert special case RGB10_A2 to RGBA8 because it's not a supported image format
  1005. if (texture->gl_internal_format_cache == GL_RGB10_A2) {
  1006. img_format = Image::FORMAT_RGBA8;
  1007. uint32_t *ptr = (uint32_t *)wb.ptr();
  1008. uint32_t num_pixels = data_size / 4;
  1009. for (uint32_t ofs = 0; ofs < num_pixels; ofs++) {
  1010. uint32_t px = ptr[ofs];
  1011. uint32_t a = px >> 30 & 0xFF;
  1012. ptr[ofs] = (px >> 2 & 0xFF) |
  1013. (px >> 12 & 0xFF) << 8 |
  1014. (px >> 22 & 0xFF) << 16 |
  1015. (a | a << 2 | a << 4 | a << 6) << 24;
  1016. }
  1017. } else {
  1018. img_format = real_format;
  1019. }
  1020. wb.release();
  1021. data.resize(data_size);
  1022. Image *img = memnew(Image(texture->alloc_width, texture->alloc_height, texture->mipmaps > 1, img_format, data));
  1023. return Ref<Image>(img);
  1024. #else
  1025. Image::Format real_format;
  1026. GLenum gl_format;
  1027. GLenum gl_internal_format;
  1028. GLenum gl_type;
  1029. bool compressed;
  1030. bool srgb;
  1031. _get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, real_format, gl_format, gl_internal_format, gl_type, compressed, srgb, texture->is_npot_repeat_mipmap);
  1032. PoolVector<uint8_t> data;
  1033. int data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, Image::FORMAT_RGBA8, false);
  1034. data.resize(data_size * 2); //add some memory at the end, just in case for buggy drivers
  1035. PoolVector<uint8_t>::Write wb = data.write();
  1036. GLuint temp_framebuffer;
  1037. glGenFramebuffers(1, &temp_framebuffer);
  1038. GLuint temp_color_texture;
  1039. glGenTextures(1, &temp_color_texture);
  1040. glBindFramebuffer(GL_FRAMEBUFFER, temp_framebuffer);
  1041. glBindTexture(GL_TEXTURE_2D, temp_color_texture);
  1042. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->alloc_width, texture->alloc_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  1043. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1044. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1045. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, temp_color_texture, 0);
  1046. glDepthMask(GL_FALSE);
  1047. glDisable(GL_DEPTH_TEST);
  1048. glDisable(GL_CULL_FACE);
  1049. glDisable(GL_BLEND);
  1050. glDepthFunc(GL_LEQUAL);
  1051. glColorMask(1, 1, 1, 1);
  1052. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  1053. glBindTexture(GL_TEXTURE_2D, texture->tex_id);
  1054. glViewport(0, 0, texture->alloc_width, texture->alloc_height);
  1055. shaders.copy.set_conditional(CopyShaderGLES3::LINEAR_TO_SRGB, !srgb);
  1056. shaders.copy.bind();
  1057. glClearColor(0.0, 0.0, 0.0, 0.0);
  1058. glClear(GL_COLOR_BUFFER_BIT);
  1059. glBindVertexArray(resources.quadie_array);
  1060. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  1061. glBindVertexArray(0);
  1062. glReadPixels(0, 0, texture->alloc_width, texture->alloc_height, GL_RGBA, GL_UNSIGNED_BYTE, &wb[0]);
  1063. shaders.copy.set_conditional(CopyShaderGLES3::LINEAR_TO_SRGB, false);
  1064. glDeleteTextures(1, &temp_color_texture);
  1065. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  1066. glDeleteFramebuffers(1, &temp_framebuffer);
  1067. wb.release();
  1068. data.resize(data_size);
  1069. Image *img = memnew(Image(texture->alloc_width, texture->alloc_height, false, Image::FORMAT_RGBA8, data));
  1070. if (!texture->compressed) {
  1071. img->convert(real_format);
  1072. }
  1073. return Ref<Image>(img);
  1074. #endif
  1075. }
  1076. void RasterizerStorageGLES3::texture_set_flags(RID p_texture, uint32_t p_flags) {
  1077. Texture *texture = texture_owner.get(p_texture);
  1078. ERR_FAIL_COND(!texture);
  1079. if (texture->render_target) {
  1080. // only allow filter and repeat flags for render target (ie. viewport) textures
  1081. p_flags &= (VS::TEXTURE_FLAG_FILTER | VS::TEXTURE_FLAG_REPEAT);
  1082. }
  1083. bool had_mipmaps = texture->flags & VS::TEXTURE_FLAG_MIPMAPS;
  1084. texture->flags = p_flags;
  1085. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  1086. glBindTexture(texture->target, texture->tex_id);
  1087. if (((texture->flags & VS::TEXTURE_FLAG_REPEAT) || (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT)) && texture->target != GL_TEXTURE_CUBE_MAP) {
  1088. if (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT) {
  1089. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
  1090. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
  1091. } else {
  1092. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  1093. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  1094. }
  1095. } else {
  1096. //glTexParameterf( texture->target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
  1097. glTexParameterf(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1098. glTexParameterf(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1099. }
  1100. if (config.use_anisotropic_filter) {
  1101. if (texture->flags & VS::TEXTURE_FLAG_ANISOTROPIC_FILTER) {
  1102. glTexParameterf(texture->target, _GL_TEXTURE_MAX_ANISOTROPY_EXT, config.anisotropic_level);
  1103. } else {
  1104. glTexParameterf(texture->target, _GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
  1105. }
  1106. }
  1107. if ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && !texture->ignore_mipmaps) {
  1108. if (!had_mipmaps && texture->mipmaps == 1) {
  1109. glGenerateMipmap(texture->target);
  1110. }
  1111. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  1112. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, config.use_fast_texture_filter ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR_MIPMAP_LINEAR);
  1113. } else {
  1114. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, config.use_fast_texture_filter ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST_MIPMAP_LINEAR);
  1115. }
  1116. } else {
  1117. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  1118. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1119. } else {
  1120. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  1121. }
  1122. }
  1123. if (config.srgb_decode_supported && texture->srgb) {
  1124. if (texture->flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR) {
  1125. glTexParameteri(texture->target, _TEXTURE_SRGB_DECODE_EXT, _DECODE_EXT);
  1126. texture->using_srgb = true;
  1127. } else {
  1128. glTexParameteri(texture->target, _TEXTURE_SRGB_DECODE_EXT, _SKIP_DECODE_EXT);
  1129. texture->using_srgb = false;
  1130. }
  1131. }
  1132. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  1133. glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtering
  1134. } else {
  1135. glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // raw Filtering
  1136. }
  1137. }
  1138. uint32_t RasterizerStorageGLES3::texture_get_flags(RID p_texture) const {
  1139. Texture *texture = texture_owner.get(p_texture);
  1140. ERR_FAIL_COND_V(!texture, 0);
  1141. return texture->flags;
  1142. }
  1143. Image::Format RasterizerStorageGLES3::texture_get_format(RID p_texture) const {
  1144. Texture *texture = texture_owner.get(p_texture);
  1145. ERR_FAIL_COND_V(!texture, Image::FORMAT_L8);
  1146. return texture->format;
  1147. }
  1148. VisualServer::TextureType RasterizerStorageGLES3::texture_get_type(RID p_texture) const {
  1149. Texture *texture = texture_owner.get(p_texture);
  1150. ERR_FAIL_COND_V(!texture, VS::TEXTURE_TYPE_2D);
  1151. return texture->type;
  1152. }
  1153. uint32_t RasterizerStorageGLES3::texture_get_texid(RID p_texture) const {
  1154. Texture *texture = texture_owner.get(p_texture);
  1155. ERR_FAIL_COND_V(!texture, 0);
  1156. return texture->tex_id;
  1157. }
  1158. void RasterizerStorageGLES3::texture_bind(RID p_texture, uint32_t p_texture_no) {
  1159. Texture *texture = texture_owner.getornull(p_texture);
  1160. ERR_FAIL_COND(!texture);
  1161. gl_wrapper.gl_active_texture(GL_TEXTURE0 + p_texture_no);
  1162. glBindTexture(texture->target, texture->tex_id);
  1163. }
  1164. uint32_t RasterizerStorageGLES3::texture_get_width(RID p_texture) const {
  1165. Texture *texture = texture_owner.get(p_texture);
  1166. ERR_FAIL_COND_V(!texture, 0);
  1167. return texture->width;
  1168. }
  1169. uint32_t RasterizerStorageGLES3::texture_get_height(RID p_texture) const {
  1170. Texture *texture = texture_owner.get(p_texture);
  1171. ERR_FAIL_COND_V(!texture, 0);
  1172. return texture->height;
  1173. }
  1174. uint32_t RasterizerStorageGLES3::texture_get_depth(RID p_texture) const {
  1175. Texture *texture = texture_owner.get(p_texture);
  1176. ERR_FAIL_COND_V(!texture, 0);
  1177. return texture->depth;
  1178. }
  1179. void RasterizerStorageGLES3::texture_set_size_override(RID p_texture, int p_width, int p_height, int p_depth) {
  1180. Texture *texture = texture_owner.get(p_texture);
  1181. ERR_FAIL_COND(!texture);
  1182. ERR_FAIL_COND(texture->render_target);
  1183. ERR_FAIL_COND(p_width <= 0 || p_width > 16384);
  1184. ERR_FAIL_COND(p_height <= 0 || p_height > 16384);
  1185. //real texture size is in alloc width and height
  1186. texture->width = p_width;
  1187. texture->height = p_height;
  1188. }
  1189. void RasterizerStorageGLES3::texture_set_path(RID p_texture, const String &p_path) {
  1190. Texture *texture = texture_owner.get(p_texture);
  1191. ERR_FAIL_COND(!texture);
  1192. texture->path = p_path;
  1193. }
  1194. String RasterizerStorageGLES3::texture_get_path(RID p_texture) const {
  1195. Texture *texture = texture_owner.get(p_texture);
  1196. ERR_FAIL_COND_V(!texture, String());
  1197. return texture->path;
  1198. }
  1199. void RasterizerStorageGLES3::texture_debug_usage(List<VS::TextureInfo> *r_info) {
  1200. List<RID> textures;
  1201. texture_owner.get_owned_list(&textures);
  1202. for (List<RID>::Element *E = textures.front(); E; E = E->next()) {
  1203. Texture *t = texture_owner.get(E->get());
  1204. if (!t) {
  1205. continue;
  1206. }
  1207. VS::TextureInfo tinfo;
  1208. tinfo.texture = E->get();
  1209. tinfo.path = t->path;
  1210. tinfo.format = t->format;
  1211. tinfo.width = t->alloc_width;
  1212. tinfo.height = t->alloc_height;
  1213. tinfo.depth = t->alloc_depth;
  1214. tinfo.bytes = t->total_data_size;
  1215. r_info->push_back(tinfo);
  1216. }
  1217. }
  1218. void RasterizerStorageGLES3::texture_set_shrink_all_x2_on_set_data(bool p_enable) {
  1219. config.shrink_textures_x2 = p_enable;
  1220. }
  1221. void RasterizerStorageGLES3::textures_keep_original(bool p_enable) {
  1222. config.keep_original_textures = p_enable;
  1223. }
  1224. void RasterizerStorageGLES3::texture_set_detect_3d_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) {
  1225. Texture *texture = texture_owner.get(p_texture);
  1226. ERR_FAIL_COND(!texture);
  1227. texture->detect_3d = p_callback;
  1228. texture->detect_3d_ud = p_userdata;
  1229. }
  1230. void RasterizerStorageGLES3::texture_set_detect_srgb_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) {
  1231. Texture *texture = texture_owner.get(p_texture);
  1232. ERR_FAIL_COND(!texture);
  1233. texture->detect_srgb = p_callback;
  1234. texture->detect_srgb_ud = p_userdata;
  1235. }
  1236. void RasterizerStorageGLES3::texture_set_detect_normal_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) {
  1237. Texture *texture = texture_owner.get(p_texture);
  1238. ERR_FAIL_COND(!texture);
  1239. texture->detect_normal = p_callback;
  1240. texture->detect_normal_ud = p_userdata;
  1241. }
  1242. RID RasterizerStorageGLES3::texture_create_radiance_cubemap(RID p_source, int p_resolution) const {
  1243. Texture *texture = texture_owner.get(p_source);
  1244. ERR_FAIL_COND_V(!texture, RID());
  1245. ERR_FAIL_COND_V(texture->type != VS::TEXTURE_TYPE_CUBEMAP, RID());
  1246. bool use_float = config.framebuffer_half_float_supported;
  1247. if (p_resolution < 0) {
  1248. p_resolution = texture->width;
  1249. }
  1250. glBindVertexArray(0);
  1251. glDisable(GL_CULL_FACE);
  1252. glDisable(GL_DEPTH_TEST);
  1253. glDisable(GL_SCISSOR_TEST);
  1254. glDisable(GL_BLEND);
  1255. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  1256. glBindTexture(texture->target, texture->tex_id);
  1257. if (config.srgb_decode_supported && texture->srgb && !texture->using_srgb) {
  1258. glTexParameteri(texture->target, _TEXTURE_SRGB_DECODE_EXT, _DECODE_EXT);
  1259. texture->using_srgb = true;
  1260. #ifdef TOOLS_ENABLED
  1261. if (!(texture->flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) {
  1262. texture->flags |= VS::TEXTURE_FLAG_CONVERT_TO_LINEAR;
  1263. //notify that texture must be set to linear beforehand, so it works in other platforms when exported
  1264. }
  1265. #endif
  1266. }
  1267. gl_wrapper.gl_active_texture(GL_TEXTURE1);
  1268. GLuint new_cubemap;
  1269. glGenTextures(1, &new_cubemap);
  1270. glBindTexture(GL_TEXTURE_CUBE_MAP, new_cubemap);
  1271. GLuint tmp_fb;
  1272. glGenFramebuffers(1, &tmp_fb);
  1273. glBindFramebuffer(GL_FRAMEBUFFER, tmp_fb);
  1274. int size = p_resolution;
  1275. int lod = 0;
  1276. shaders.cubemap_filter.bind();
  1277. int mipmaps = 6;
  1278. int mm_level = mipmaps;
  1279. GLenum internal_format = use_float ? GL_RGBA16F : GL_RGB10_A2;
  1280. GLenum format = GL_RGBA;
  1281. GLenum type = use_float ? GL_HALF_FLOAT : GL_UNSIGNED_INT_2_10_10_10_REV;
  1282. while (mm_level) {
  1283. for (int i = 0; i < 6; i++) {
  1284. glTexImage2D(_cube_side_enum[i], lod, internal_format, size, size, 0, format, type, nullptr);
  1285. }
  1286. lod++;
  1287. mm_level--;
  1288. if (size > 1) {
  1289. size >>= 1;
  1290. }
  1291. }
  1292. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
  1293. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, lod - 1);
  1294. lod = 0;
  1295. mm_level = mipmaps;
  1296. size = p_resolution;
  1297. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DUAL_PARABOLOID, false);
  1298. while (mm_level) {
  1299. for (int i = 0; i < 6; i++) {
  1300. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, _cube_side_enum[i], new_cubemap, lod);
  1301. glViewport(0, 0, size, size);
  1302. glBindVertexArray(resources.quadie_array);
  1303. shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::FACE_ID, i);
  1304. shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::ROUGHNESS, lod / float(mipmaps - 1));
  1305. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  1306. glBindVertexArray(0);
  1307. #ifdef DEBUG_ENABLED
  1308. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  1309. ERR_CONTINUE(status != GL_FRAMEBUFFER_COMPLETE);
  1310. #endif
  1311. }
  1312. if (size > 1) {
  1313. size >>= 1;
  1314. }
  1315. lod++;
  1316. mm_level--;
  1317. }
  1318. //restore ranges
  1319. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
  1320. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, lod - 1);
  1321. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  1322. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1323. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1324. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1325. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  1326. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
  1327. glDeleteFramebuffers(1, &tmp_fb);
  1328. Texture *ctex = memnew(Texture);
  1329. ctex->type = VS::TEXTURE_TYPE_CUBEMAP;
  1330. ctex->flags = VS::TEXTURE_FLAG_MIPMAPS | VS::TEXTURE_FLAG_FILTER;
  1331. ctex->width = p_resolution;
  1332. ctex->height = p_resolution;
  1333. ctex->alloc_width = p_resolution;
  1334. ctex->alloc_height = p_resolution;
  1335. ctex->format = use_float ? Image::FORMAT_RGBAH : Image::FORMAT_RGBA8;
  1336. ctex->target = GL_TEXTURE_CUBE_MAP;
  1337. ctex->gl_format_cache = format;
  1338. ctex->gl_internal_format_cache = internal_format;
  1339. ctex->gl_type_cache = type;
  1340. ctex->data_size = 0;
  1341. ctex->compressed = false;
  1342. ctex->srgb = false;
  1343. ctex->total_data_size = 0;
  1344. ctex->ignore_mipmaps = false;
  1345. ctex->mipmaps = mipmaps;
  1346. ctex->active = true;
  1347. ctex->tex_id = new_cubemap;
  1348. ctex->stored_cube_sides = (1 << 6) - 1;
  1349. ctex->render_target = nullptr;
  1350. return texture_owner.make_rid(ctex);
  1351. }
  1352. Size2 RasterizerStorageGLES3::texture_size_with_proxy(RID p_texture) const {
  1353. const Texture *texture = texture_owner.getornull(p_texture);
  1354. ERR_FAIL_COND_V(!texture, Size2());
  1355. if (texture->proxy) {
  1356. return Size2(texture->proxy->width, texture->proxy->height);
  1357. } else {
  1358. return Size2(texture->width, texture->height);
  1359. }
  1360. }
  1361. void RasterizerStorageGLES3::texture_set_proxy(RID p_texture, RID p_proxy) {
  1362. Texture *texture = texture_owner.get(p_texture);
  1363. ERR_FAIL_COND(!texture);
  1364. if (texture->proxy) {
  1365. texture->proxy->proxy_owners.erase(texture);
  1366. texture->proxy = nullptr;
  1367. }
  1368. if (p_proxy.is_valid()) {
  1369. Texture *proxy = texture_owner.get(p_proxy);
  1370. ERR_FAIL_COND(!proxy);
  1371. ERR_FAIL_COND(proxy == texture);
  1372. proxy->proxy_owners.insert(texture);
  1373. texture->proxy = proxy;
  1374. }
  1375. }
  1376. void RasterizerStorageGLES3::texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) {
  1377. Texture *texture = texture_owner.get(p_texture);
  1378. ERR_FAIL_COND(!texture);
  1379. texture->redraw_if_visible = p_enable;
  1380. }
  1381. RID RasterizerStorageGLES3::sky_create() {
  1382. Sky *sky = memnew(Sky);
  1383. sky->radiance = 0;
  1384. sky->irradiance = 0;
  1385. return sky_owner.make_rid(sky);
  1386. }
  1387. void RasterizerStorageGLES3::sky_set_texture(RID p_sky, RID p_panorama, int p_radiance_size) {
  1388. Sky *sky = sky_owner.getornull(p_sky);
  1389. ERR_FAIL_COND(!sky);
  1390. if (sky->panorama.is_valid()) {
  1391. sky->panorama = RID();
  1392. glDeleteTextures(1, &sky->radiance);
  1393. glDeleteTextures(1, &sky->irradiance);
  1394. sky->radiance = 0;
  1395. sky->irradiance = 0;
  1396. }
  1397. sky->panorama = p_panorama;
  1398. if (!sky->panorama.is_valid()) {
  1399. return; //cleared
  1400. }
  1401. Texture *texture = texture_owner.getornull(sky->panorama);
  1402. if (!texture) {
  1403. sky->panorama = RID();
  1404. ERR_FAIL_COND(!texture);
  1405. }
  1406. texture = texture->get_ptr(); //resolve for proxies
  1407. glBindVertexArray(0);
  1408. glDisable(GL_CULL_FACE);
  1409. glDisable(GL_DEPTH_TEST);
  1410. glDisable(GL_SCISSOR_TEST);
  1411. glDisable(GL_BLEND);
  1412. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  1413. glBindTexture(texture->target, texture->tex_id);
  1414. glTexParameteri(texture->target, GL_TEXTURE_BASE_LEVEL, 0);
  1415. #ifdef GLES_OVER_GL
  1416. glTexParameteri(texture->target, GL_TEXTURE_MAX_LEVEL, int(Math::floor(Math::log(float(texture->width)) / Math::log(2.0f))));
  1417. glGenerateMipmap(texture->target);
  1418. #else
  1419. glTexParameteri(texture->target, GL_TEXTURE_MAX_LEVEL, 0);
  1420. #endif
  1421. // Need Mipmaps regardless of whether they are set in import by user
  1422. glTexParameterf(texture->target, GL_TEXTURE_WRAP_S, GL_REPEAT);
  1423. glTexParameterf(texture->target, GL_TEXTURE_WRAP_T, GL_REPEAT);
  1424. #ifdef GLES_OVER_GL
  1425. glTexParameterf(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  1426. #else
  1427. glTexParameterf(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1428. #endif
  1429. glTexParameterf(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1430. if (config.srgb_decode_supported && texture->srgb && !texture->using_srgb) {
  1431. glTexParameteri(texture->target, _TEXTURE_SRGB_DECODE_EXT, _DECODE_EXT);
  1432. texture->using_srgb = true;
  1433. #ifdef TOOLS_ENABLED
  1434. if (!(texture->flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) {
  1435. texture->flags |= VS::TEXTURE_FLAG_CONVERT_TO_LINEAR;
  1436. //notify that texture must be set to linear beforehand, so it works in other platforms when exported
  1437. }
  1438. #endif
  1439. }
  1440. {
  1441. //Irradiance map
  1442. gl_wrapper.gl_active_texture(GL_TEXTURE1);
  1443. glGenTextures(1, &sky->irradiance);
  1444. glBindTexture(GL_TEXTURE_2D, sky->irradiance);
  1445. GLuint tmp_fb;
  1446. glGenFramebuffers(1, &tmp_fb);
  1447. glBindFramebuffer(GL_FRAMEBUFFER, tmp_fb);
  1448. int size = 32;
  1449. bool use_float = config.framebuffer_half_float_supported;
  1450. GLenum internal_format = use_float ? GL_RGBA16F : GL_RGB10_A2;
  1451. GLenum format = GL_RGBA;
  1452. GLenum type = use_float ? GL_HALF_FLOAT : GL_UNSIGNED_INT_2_10_10_10_REV;
  1453. glTexImage2D(GL_TEXTURE_2D, 0, internal_format, size, size * 2, 0, format, type, nullptr);
  1454. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  1455. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
  1456. glTexParameterf(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1457. glTexParameterf(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1458. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1459. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1460. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, sky->irradiance, 0);
  1461. int irradiance_size = GLOBAL_GET("rendering/quality/reflections/irradiance_max_size");
  1462. int upscale_size = MIN(int(previous_power_of_2(irradiance_size)), p_radiance_size);
  1463. GLuint tmp_fb2;
  1464. GLuint tmp_tex;
  1465. {
  1466. //generate another one for rendering, as can't read and write from a single texarray it seems
  1467. glGenFramebuffers(1, &tmp_fb2);
  1468. glBindFramebuffer(GL_FRAMEBUFFER, tmp_fb2);
  1469. glGenTextures(1, &tmp_tex);
  1470. glBindTexture(GL_TEXTURE_2D, tmp_tex);
  1471. glTexImage2D(GL_TEXTURE_2D, 0, internal_format, upscale_size, 2.0 * upscale_size, 0, format, type, nullptr);
  1472. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tmp_tex, 0);
  1473. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  1474. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1475. #ifdef DEBUG_ENABLED
  1476. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  1477. ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE);
  1478. #endif
  1479. }
  1480. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DUAL_PARABOLOID, true);
  1481. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_SOURCE_PANORAMA, true);
  1482. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::COMPUTE_IRRADIANCE, true);
  1483. shaders.cubemap_filter.bind();
  1484. // Very large Panoramas require way too much effort to compute irradiance so use a mipmap
  1485. // level that corresponds to a panorama of 1024x512
  1486. shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::SOURCE_MIP_LEVEL, MAX(Math::floor(Math::log(float(texture->width)) / Math::log(2.0f)) - 10.0f, 0.0f));
  1487. // Compute Irradiance for a large texture, specified by radiance size and then pull out a low mipmap corresponding to 32x32
  1488. for (int i = 0; i < 2; i++) {
  1489. glViewport(0, i * upscale_size, upscale_size, upscale_size);
  1490. glBindVertexArray(resources.quadie_array);
  1491. shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::Z_FLIP, i > 0);
  1492. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  1493. glBindVertexArray(0);
  1494. }
  1495. glGenerateMipmap(GL_TEXTURE_2D);
  1496. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  1497. glBindTexture(GL_TEXTURE_2D, tmp_tex);
  1498. glBindFramebuffer(GL_FRAMEBUFFER, tmp_fb);
  1499. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DUAL_PARABOLOID, false);
  1500. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_SOURCE_PANORAMA, false);
  1501. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::COMPUTE_IRRADIANCE, false);
  1502. shaders.copy.set_conditional(CopyShaderGLES3::USE_LOD, true);
  1503. shaders.copy.bind();
  1504. shaders.copy.set_uniform(CopyShaderGLES3::MIP_LEVEL, MAX(Math::floor(Math::log(float(upscale_size)) / Math::log(2.0f)) - 5.0f, 0.0f)); // Mip level that corresponds to a 32x32 texture
  1505. glViewport(0, 0, size, size * 2.0);
  1506. glBindVertexArray(resources.quadie_array);
  1507. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  1508. glBindVertexArray(0);
  1509. shaders.copy.set_conditional(CopyShaderGLES3::USE_LOD, false);
  1510. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
  1511. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  1512. glBindTexture(texture->target, texture->tex_id);
  1513. glDeleteFramebuffers(1, &tmp_fb);
  1514. glDeleteFramebuffers(1, &tmp_fb2);
  1515. glDeleteTextures(1, &tmp_tex);
  1516. }
  1517. // Now compute radiance
  1518. gl_wrapper.gl_active_texture(GL_TEXTURE1);
  1519. glGenTextures(1, &sky->radiance);
  1520. if (config.use_texture_array_environment) {
  1521. //texture3D
  1522. glBindTexture(GL_TEXTURE_2D_ARRAY, sky->radiance);
  1523. GLuint tmp_fb;
  1524. glGenFramebuffers(1, &tmp_fb);
  1525. glBindFramebuffer(GL_FRAMEBUFFER, tmp_fb);
  1526. int size = p_radiance_size;
  1527. int array_level = 6;
  1528. bool use_float = config.framebuffer_half_float_supported;
  1529. GLenum internal_format = use_float ? GL_RGBA16F : GL_RGB10_A2;
  1530. GLenum format = GL_RGBA;
  1531. GLenum type = use_float ? GL_HALF_FLOAT : GL_UNSIGNED_INT_2_10_10_10_REV;
  1532. glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, internal_format, size, size * 2, array_level, 0, format, type, nullptr);
  1533. glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1534. glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1535. GLuint tmp_fb2;
  1536. GLuint tmp_tex;
  1537. {
  1538. //generate another one for rendering, as can't read and write from a single texarray it seems
  1539. glGenFramebuffers(1, &tmp_fb2);
  1540. glBindFramebuffer(GL_FRAMEBUFFER, tmp_fb2);
  1541. glGenTextures(1, &tmp_tex);
  1542. glBindTexture(GL_TEXTURE_2D, tmp_tex);
  1543. glTexImage2D(GL_TEXTURE_2D, 0, internal_format, size, size * 2, 0, format, type, nullptr);
  1544. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tmp_tex, 0);
  1545. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  1546. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  1547. #ifdef DEBUG_ENABLED
  1548. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  1549. ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE);
  1550. #endif
  1551. }
  1552. for (int j = 0; j < array_level; j++) {
  1553. glBindFramebuffer(GL_FRAMEBUFFER, tmp_fb2);
  1554. #ifdef GLES_OVER_GL
  1555. if (j < 3) {
  1556. #else
  1557. if (j == 0) {
  1558. #endif
  1559. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DUAL_PARABOLOID, true);
  1560. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_SOURCE_PANORAMA, true);
  1561. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_SOURCE_DUAL_PARABOLOID_ARRAY, false);
  1562. shaders.cubemap_filter.bind();
  1563. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  1564. glBindTexture(texture->target, texture->tex_id);
  1565. shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::SOURCE_RESOLUTION, float(texture->width / 4));
  1566. } else {
  1567. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DUAL_PARABOLOID, true);
  1568. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_SOURCE_PANORAMA, false);
  1569. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_SOURCE_DUAL_PARABOLOID_ARRAY, true);
  1570. shaders.cubemap_filter.bind();
  1571. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  1572. glBindTexture(GL_TEXTURE_2D_ARRAY, sky->radiance);
  1573. shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::SOURCE_ARRAY_INDEX, j - 1); //read from previous to ensure better blur
  1574. }
  1575. for (int i = 0; i < 2; i++) {
  1576. glViewport(0, i * size, size, size);
  1577. glBindVertexArray(resources.quadie_array);
  1578. shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::Z_FLIP, i > 0);
  1579. shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::ROUGHNESS, j / float(array_level - 1));
  1580. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  1581. glBindVertexArray(0);
  1582. }
  1583. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, tmp_fb);
  1584. glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, sky->radiance, 0, j);
  1585. glBindFramebuffer(GL_READ_FRAMEBUFFER, tmp_fb2);
  1586. glReadBuffer(GL_COLOR_ATTACHMENT0);
  1587. glBlitFramebuffer(0, 0, size, size * 2, 0, 0, size, size * 2, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  1588. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  1589. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  1590. }
  1591. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_SOURCE_PANORAMA, false);
  1592. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DUAL_PARABOLOID, false);
  1593. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_SOURCE_DUAL_PARABOLOID_ARRAY, false);
  1594. //restore ranges
  1595. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  1596. glBindTexture(GL_TEXTURE_2D_ARRAY, sky->radiance);
  1597. glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
  1598. glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  1599. glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1600. glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1601. glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1602. //reset flags on Sky Texture that may have changed
  1603. texture_set_flags(sky->panorama, texture->flags);
  1604. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
  1605. glDeleteFramebuffers(1, &tmp_fb);
  1606. glDeleteFramebuffers(1, &tmp_fb2);
  1607. glDeleteTextures(1, &tmp_tex);
  1608. } else {
  1609. //regular single texture with mipmaps
  1610. glBindTexture(GL_TEXTURE_2D, sky->radiance);
  1611. GLuint tmp_fb;
  1612. glGenFramebuffers(1, &tmp_fb);
  1613. glBindFramebuffer(GL_FRAMEBUFFER, tmp_fb);
  1614. int size = p_radiance_size;
  1615. int lod = 0;
  1616. int mipmaps = 6;
  1617. int mm_level = mipmaps;
  1618. bool use_float = config.framebuffer_half_float_supported;
  1619. GLenum internal_format = use_float ? GL_RGBA16F : GL_RGB10_A2;
  1620. GLenum format = GL_RGBA;
  1621. GLenum type = use_float ? GL_HALF_FLOAT : GL_UNSIGNED_INT_2_10_10_10_REV;
  1622. glTexStorage2DCustom(GL_TEXTURE_2D, mipmaps, internal_format, size, size * 2.0, format, type);
  1623. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  1624. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, mipmaps - 1);
  1625. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1626. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1627. GLuint tmp_fb2;
  1628. GLuint tmp_tex;
  1629. {
  1630. // Need a temporary framebuffer for rendering so we can read from previous iterations
  1631. glGenFramebuffers(1, &tmp_fb2);
  1632. glBindFramebuffer(GL_FRAMEBUFFER, tmp_fb2);
  1633. glGenTextures(1, &tmp_tex);
  1634. glBindTexture(GL_TEXTURE_2D, tmp_tex);
  1635. glTexImage2D(GL_TEXTURE_2D, 0, internal_format, size, size * 2, 0, format, type, nullptr);
  1636. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tmp_tex, 0);
  1637. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1638. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1639. #ifdef DEBUG_ENABLED
  1640. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  1641. ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE);
  1642. #endif
  1643. }
  1644. lod = 0;
  1645. mm_level = mipmaps;
  1646. size = p_radiance_size;
  1647. while (mm_level) {
  1648. glBindFramebuffer(GL_FRAMEBUFFER, tmp_fb);
  1649. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, sky->radiance, lod);
  1650. #ifdef DEBUG_ENABLED
  1651. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  1652. ERR_CONTINUE(status != GL_FRAMEBUFFER_COMPLETE);
  1653. #endif
  1654. glBindTexture(GL_TEXTURE_2D, tmp_tex);
  1655. glTexImage2D(GL_TEXTURE_2D, 0, internal_format, size, size * 2, 0, format, type, nullptr);
  1656. glBindFramebuffer(GL_FRAMEBUFFER, tmp_fb2);
  1657. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tmp_tex, 0);
  1658. #ifdef GLES_OVER_GL
  1659. if (lod < 3) {
  1660. #else
  1661. if (lod == 0) {
  1662. #endif
  1663. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DUAL_PARABOLOID, true);
  1664. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_SOURCE_PANORAMA, true);
  1665. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_SOURCE_DUAL_PARABOLOID, false);
  1666. shaders.cubemap_filter.bind();
  1667. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  1668. glBindTexture(texture->target, texture->tex_id);
  1669. shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::SOURCE_RESOLUTION, float(texture->width / 4));
  1670. } else {
  1671. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DUAL_PARABOLOID, true);
  1672. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_SOURCE_PANORAMA, false);
  1673. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_SOURCE_DUAL_PARABOLOID, true);
  1674. shaders.cubemap_filter.bind();
  1675. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  1676. glBindTexture(GL_TEXTURE_2D, sky->radiance);
  1677. shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::SOURCE_MIP_LEVEL, float(lod - 1)); //read from previous to ensure better blur
  1678. }
  1679. for (int i = 0; i < 2; i++) {
  1680. glViewport(0, i * size, size, size);
  1681. glBindVertexArray(resources.quadie_array);
  1682. shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::Z_FLIP, i > 0);
  1683. shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::ROUGHNESS, lod / float(mipmaps - 1));
  1684. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  1685. glBindVertexArray(0);
  1686. }
  1687. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, tmp_fb);
  1688. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, sky->radiance, lod);
  1689. glBindFramebuffer(GL_READ_FRAMEBUFFER, tmp_fb2);
  1690. glReadBuffer(GL_COLOR_ATTACHMENT0);
  1691. glBlitFramebuffer(0, 0, size, size * 2, 0, 0, size, size * 2, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  1692. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  1693. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  1694. if (size > 1) {
  1695. size >>= 1;
  1696. }
  1697. lod++;
  1698. mm_level--;
  1699. }
  1700. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DUAL_PARABOLOID, false);
  1701. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_SOURCE_PANORAMA, false);
  1702. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_SOURCE_DUAL_PARABOLOID, false);
  1703. //restore ranges
  1704. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  1705. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, lod - 1);
  1706. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  1707. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1708. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1709. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1710. //reset flags on Sky Texture that may have changed
  1711. texture_set_flags(sky->panorama, texture->flags);
  1712. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
  1713. glDeleteFramebuffers(1, &tmp_fb);
  1714. glDeleteFramebuffers(1, &tmp_fb2);
  1715. glDeleteTextures(1, &tmp_tex);
  1716. }
  1717. }
  1718. /* SHADER API */
  1719. RID RasterizerStorageGLES3::shader_create() {
  1720. Shader *shader = memnew(Shader);
  1721. shader->mode = VS::SHADER_SPATIAL;
  1722. shader->shader = &scene->state.scene_shader;
  1723. RID rid = shader_owner.make_rid(shader);
  1724. _shader_make_dirty(shader);
  1725. shader->self = rid;
  1726. return rid;
  1727. }
  1728. void RasterizerStorageGLES3::_shader_make_dirty(Shader *p_shader) {
  1729. if (p_shader->dirty_list.in_list()) {
  1730. return;
  1731. }
  1732. _shader_dirty_list.add(&p_shader->dirty_list);
  1733. }
  1734. void RasterizerStorageGLES3::shader_set_code(RID p_shader, const String &p_code) {
  1735. Shader *shader = shader_owner.get(p_shader);
  1736. ERR_FAIL_COND(!shader);
  1737. shader->code = p_code;
  1738. String mode_string = ShaderLanguage::get_shader_type(p_code);
  1739. VS::ShaderMode mode;
  1740. if (mode_string == "canvas_item") {
  1741. mode = VS::SHADER_CANVAS_ITEM;
  1742. } else if (mode_string == "particles") {
  1743. mode = VS::SHADER_PARTICLES;
  1744. } else {
  1745. mode = VS::SHADER_SPATIAL;
  1746. }
  1747. if (shader->custom_code_id && mode != shader->mode) {
  1748. shader->shader->free_custom_shader(shader->custom_code_id);
  1749. shader->custom_code_id = 0;
  1750. }
  1751. shader->mode = mode;
  1752. ShaderGLES3 *shaders[VS::SHADER_MAX] = {
  1753. &scene->state.scene_shader,
  1754. &canvas->state.canvas_shader,
  1755. &this->shaders.particles,
  1756. };
  1757. shader->shader = shaders[mode];
  1758. if (shader->custom_code_id == 0) {
  1759. shader->custom_code_id = shader->shader->create_custom_shader();
  1760. }
  1761. _shader_make_dirty(shader);
  1762. }
  1763. String RasterizerStorageGLES3::shader_get_code(RID p_shader) const {
  1764. const Shader *shader = shader_owner.get(p_shader);
  1765. ERR_FAIL_COND_V(!shader, String());
  1766. return shader->code;
  1767. }
  1768. void RasterizerStorageGLES3::_update_shader(Shader *p_shader) const {
  1769. _shader_dirty_list.remove(&p_shader->dirty_list);
  1770. p_shader->valid = false;
  1771. p_shader->ubo_size = 0;
  1772. p_shader->uniforms.clear();
  1773. if (p_shader->code == String()) {
  1774. return; //just invalid, but no error
  1775. }
  1776. ShaderCompilerGLES3::GeneratedCode gen_code;
  1777. ShaderCompilerGLES3::IdentifierActions *actions = nullptr;
  1778. int async_mode = (int)ShaderGLES3::ASYNC_MODE_VISIBLE;
  1779. switch (p_shader->mode) {
  1780. case VS::SHADER_CANVAS_ITEM: {
  1781. p_shader->canvas_item.light_mode = Shader::CanvasItem::LIGHT_MODE_NORMAL;
  1782. p_shader->canvas_item.blend_mode = Shader::CanvasItem::BLEND_MODE_MIX;
  1783. p_shader->canvas_item.uses_screen_texture = false;
  1784. p_shader->canvas_item.uses_screen_uv = false;
  1785. p_shader->canvas_item.uses_time = false;
  1786. p_shader->canvas_item.uses_modulate = false;
  1787. p_shader->canvas_item.uses_color = false;
  1788. p_shader->canvas_item.uses_vertex = false;
  1789. p_shader->canvas_item.batch_flags = 0;
  1790. p_shader->canvas_item.uses_world_matrix = false;
  1791. p_shader->canvas_item.uses_extra_matrix = false;
  1792. p_shader->canvas_item.uses_projection_matrix = false;
  1793. p_shader->canvas_item.uses_instance_custom = false;
  1794. shaders.actions_canvas.render_mode_values["blend_add"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_ADD);
  1795. shaders.actions_canvas.render_mode_values["blend_mix"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_MIX);
  1796. shaders.actions_canvas.render_mode_values["blend_sub"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_SUB);
  1797. shaders.actions_canvas.render_mode_values["blend_mul"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_MUL);
  1798. shaders.actions_canvas.render_mode_values["blend_premul_alpha"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_PMALPHA);
  1799. shaders.actions_canvas.render_mode_values["blend_disabled"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_DISABLED);
  1800. shaders.actions_canvas.render_mode_values["unshaded"] = Pair<int *, int>(&p_shader->canvas_item.light_mode, Shader::CanvasItem::LIGHT_MODE_UNSHADED);
  1801. shaders.actions_canvas.render_mode_values["light_only"] = Pair<int *, int>(&p_shader->canvas_item.light_mode, Shader::CanvasItem::LIGHT_MODE_LIGHT_ONLY);
  1802. shaders.actions_canvas.usage_flag_pointers["SCREEN_UV"] = &p_shader->canvas_item.uses_screen_uv;
  1803. shaders.actions_canvas.usage_flag_pointers["SCREEN_PIXEL_SIZE"] = &p_shader->canvas_item.uses_screen_uv;
  1804. shaders.actions_canvas.usage_flag_pointers["SCREEN_TEXTURE"] = &p_shader->canvas_item.uses_screen_texture;
  1805. shaders.actions_canvas.usage_flag_pointers["TIME"] = &p_shader->canvas_item.uses_time;
  1806. shaders.actions_canvas.usage_flag_pointers["MODULATE"] = &p_shader->canvas_item.uses_modulate;
  1807. shaders.actions_canvas.usage_flag_pointers["COLOR"] = &p_shader->canvas_item.uses_color;
  1808. shaders.actions_canvas.usage_flag_pointers["VERTEX"] = &p_shader->canvas_item.uses_vertex;
  1809. shaders.actions_canvas.usage_flag_pointers["WORLD_MATRIX"] = &p_shader->canvas_item.uses_world_matrix;
  1810. shaders.actions_canvas.usage_flag_pointers["EXTRA_MATRIX"] = &p_shader->canvas_item.uses_extra_matrix;
  1811. shaders.actions_canvas.usage_flag_pointers["PROJECTION_MATRIX"] = &p_shader->canvas_item.uses_projection_matrix;
  1812. shaders.actions_canvas.usage_flag_pointers["INSTANCE_CUSTOM"] = &p_shader->canvas_item.uses_instance_custom;
  1813. actions = &shaders.actions_canvas;
  1814. actions->uniforms = &p_shader->uniforms;
  1815. } break;
  1816. case VS::SHADER_SPATIAL: {
  1817. p_shader->spatial.blend_mode = Shader::Spatial::BLEND_MODE_MIX;
  1818. p_shader->spatial.depth_draw_mode = Shader::Spatial::DEPTH_DRAW_OPAQUE;
  1819. p_shader->spatial.cull_mode = Shader::Spatial::CULL_MODE_BACK;
  1820. p_shader->spatial.uses_alpha = false;
  1821. p_shader->spatial.uses_alpha_scissor = false;
  1822. p_shader->spatial.uses_discard = false;
  1823. p_shader->spatial.unshaded = false;
  1824. p_shader->spatial.no_depth_test = false;
  1825. p_shader->spatial.uses_sss = false;
  1826. p_shader->spatial.uses_time = false;
  1827. p_shader->spatial.uses_vertex_lighting = false;
  1828. p_shader->spatial.uses_screen_texture = false;
  1829. p_shader->spatial.uses_depth_texture = false;
  1830. p_shader->spatial.uses_vertex = false;
  1831. p_shader->spatial.uses_tangent = false;
  1832. p_shader->spatial.uses_ensure_correct_normals = false;
  1833. p_shader->spatial.writes_modelview_or_projection = false;
  1834. p_shader->spatial.uses_world_coordinates = false;
  1835. shaders.actions_scene.render_mode_values["blend_add"] = Pair<int *, int>(&p_shader->spatial.blend_mode, Shader::Spatial::BLEND_MODE_ADD);
  1836. shaders.actions_scene.render_mode_values["blend_mix"] = Pair<int *, int>(&p_shader->spatial.blend_mode, Shader::Spatial::BLEND_MODE_MIX);
  1837. shaders.actions_scene.render_mode_values["blend_sub"] = Pair<int *, int>(&p_shader->spatial.blend_mode, Shader::Spatial::BLEND_MODE_SUB);
  1838. shaders.actions_scene.render_mode_values["blend_mul"] = Pair<int *, int>(&p_shader->spatial.blend_mode, Shader::Spatial::BLEND_MODE_MUL);
  1839. shaders.actions_scene.render_mode_values["depth_draw_opaque"] = Pair<int *, int>(&p_shader->spatial.depth_draw_mode, Shader::Spatial::DEPTH_DRAW_OPAQUE);
  1840. shaders.actions_scene.render_mode_values["depth_draw_always"] = Pair<int *, int>(&p_shader->spatial.depth_draw_mode, Shader::Spatial::DEPTH_DRAW_ALWAYS);
  1841. shaders.actions_scene.render_mode_values["depth_draw_never"] = Pair<int *, int>(&p_shader->spatial.depth_draw_mode, Shader::Spatial::DEPTH_DRAW_NEVER);
  1842. shaders.actions_scene.render_mode_values["depth_draw_alpha_prepass"] = Pair<int *, int>(&p_shader->spatial.depth_draw_mode, Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS);
  1843. shaders.actions_scene.render_mode_values["cull_front"] = Pair<int *, int>(&p_shader->spatial.cull_mode, Shader::Spatial::CULL_MODE_FRONT);
  1844. shaders.actions_scene.render_mode_values["cull_back"] = Pair<int *, int>(&p_shader->spatial.cull_mode, Shader::Spatial::CULL_MODE_BACK);
  1845. shaders.actions_scene.render_mode_values["cull_disabled"] = Pair<int *, int>(&p_shader->spatial.cull_mode, Shader::Spatial::CULL_MODE_DISABLED);
  1846. shaders.actions_scene.render_mode_values["async_visible"] = Pair<int *, int>(&async_mode, (int)ShaderGLES3::ASYNC_MODE_VISIBLE);
  1847. shaders.actions_scene.render_mode_values["async_hidden"] = Pair<int *, int>(&async_mode, (int)ShaderGLES3::ASYNC_MODE_HIDDEN);
  1848. shaders.actions_scene.render_mode_flags["unshaded"] = &p_shader->spatial.unshaded;
  1849. shaders.actions_scene.render_mode_flags["depth_test_disable"] = &p_shader->spatial.no_depth_test;
  1850. shaders.actions_scene.render_mode_flags["vertex_lighting"] = &p_shader->spatial.uses_vertex_lighting;
  1851. shaders.actions_scene.render_mode_flags["world_vertex_coords"] = &p_shader->spatial.uses_world_coordinates;
  1852. shaders.actions_scene.render_mode_flags["ensure_correct_normals"] = &p_shader->spatial.uses_ensure_correct_normals;
  1853. shaders.actions_scene.usage_flag_pointers["ALPHA"] = &p_shader->spatial.uses_alpha;
  1854. shaders.actions_scene.usage_flag_pointers["ALPHA_SCISSOR"] = &p_shader->spatial.uses_alpha_scissor;
  1855. shaders.actions_scene.usage_flag_pointers["SSS_STRENGTH"] = &p_shader->spatial.uses_sss;
  1856. shaders.actions_scene.usage_flag_pointers["DISCARD"] = &p_shader->spatial.uses_discard;
  1857. shaders.actions_scene.usage_flag_pointers["SCREEN_TEXTURE"] = &p_shader->spatial.uses_screen_texture;
  1858. shaders.actions_scene.usage_flag_pointers["DEPTH_TEXTURE"] = &p_shader->spatial.uses_depth_texture;
  1859. shaders.actions_scene.usage_flag_pointers["TIME"] = &p_shader->spatial.uses_time;
  1860. // Use of any of these BUILTINS indicate the need for transformed tangents.
  1861. // This is needed to know when to transform tangents in software skinning.
  1862. shaders.actions_scene.usage_flag_pointers["TANGENT"] = &p_shader->spatial.uses_tangent;
  1863. shaders.actions_scene.usage_flag_pointers["NORMALMAP"] = &p_shader->spatial.uses_tangent;
  1864. shaders.actions_scene.write_flag_pointers["MODELVIEW_MATRIX"] = &p_shader->spatial.writes_modelview_or_projection;
  1865. shaders.actions_scene.write_flag_pointers["PROJECTION_MATRIX"] = &p_shader->spatial.writes_modelview_or_projection;
  1866. shaders.actions_scene.write_flag_pointers["VERTEX"] = &p_shader->spatial.uses_vertex;
  1867. actions = &shaders.actions_scene;
  1868. actions->uniforms = &p_shader->uniforms;
  1869. } break;
  1870. case VS::SHADER_PARTICLES: {
  1871. actions = &shaders.actions_particles;
  1872. actions->uniforms = &p_shader->uniforms;
  1873. } break;
  1874. case VS::SHADER_MAX:
  1875. break; // Can't happen, but silences warning
  1876. }
  1877. Error err = shaders.compiler.compile(p_shader->mode, p_shader->code, actions, p_shader->path, gen_code);
  1878. if (err != OK) {
  1879. return;
  1880. }
  1881. p_shader->ubo_size = gen_code.uniform_total_size;
  1882. p_shader->ubo_offsets = gen_code.uniform_offsets;
  1883. p_shader->texture_count = gen_code.texture_uniforms.size();
  1884. p_shader->texture_hints = gen_code.texture_hints;
  1885. p_shader->texture_types = gen_code.texture_types;
  1886. p_shader->uses_vertex_time = gen_code.uses_vertex_time;
  1887. p_shader->uses_fragment_time = gen_code.uses_fragment_time;
  1888. // some logic for batching
  1889. if (p_shader->mode == VS::SHADER_CANVAS_ITEM) {
  1890. if (p_shader->canvas_item.uses_modulate | p_shader->canvas_item.uses_color) {
  1891. p_shader->canvas_item.batch_flags |= RasterizerStorageCommon::PREVENT_COLOR_BAKING;
  1892. }
  1893. if (p_shader->canvas_item.uses_vertex) {
  1894. p_shader->canvas_item.batch_flags |= RasterizerStorageCommon::PREVENT_VERTEX_BAKING;
  1895. }
  1896. if (p_shader->canvas_item.uses_world_matrix | p_shader->canvas_item.uses_extra_matrix | p_shader->canvas_item.uses_projection_matrix | p_shader->canvas_item.uses_instance_custom) {
  1897. p_shader->canvas_item.batch_flags |= RasterizerStorageCommon::PREVENT_ITEM_JOINING;
  1898. }
  1899. }
  1900. p_shader->shader->set_custom_shader_code(p_shader->custom_code_id, gen_code.vertex, gen_code.vertex_global, gen_code.fragment, gen_code.light, gen_code.fragment_global, gen_code.uniforms, gen_code.texture_uniforms, gen_code.defines, (ShaderGLES3::AsyncMode)async_mode);
  1901. //all materials using this shader will have to be invalidated, unfortunately
  1902. for (SelfList<Material> *E = p_shader->materials.first(); E; E = E->next()) {
  1903. _material_make_dirty(E->self());
  1904. }
  1905. p_shader->valid = true;
  1906. p_shader->version++;
  1907. }
  1908. void RasterizerStorageGLES3::update_dirty_shaders() {
  1909. while (_shader_dirty_list.first()) {
  1910. _update_shader(_shader_dirty_list.first()->self());
  1911. }
  1912. }
  1913. void RasterizerStorageGLES3::shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const {
  1914. Shader *shader = shader_owner.get(p_shader);
  1915. ERR_FAIL_COND(!shader);
  1916. if (shader->dirty_list.in_list()) {
  1917. _update_shader(shader); // ok should be not anymore dirty
  1918. }
  1919. Map<int, StringName> order;
  1920. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = shader->uniforms.front(); E; E = E->next()) {
  1921. if (E->get().texture_order >= 0) {
  1922. order[E->get().texture_order + 100000] = E->key();
  1923. } else {
  1924. order[E->get().order] = E->key();
  1925. }
  1926. }
  1927. for (Map<int, StringName>::Element *E = order.front(); E; E = E->next()) {
  1928. PropertyInfo pi;
  1929. ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[E->get()];
  1930. pi.name = E->get();
  1931. switch (u.type) {
  1932. case ShaderLanguage::TYPE_STRUCT:
  1933. pi.type = Variant::ARRAY;
  1934. break;
  1935. case ShaderLanguage::TYPE_VOID:
  1936. pi.type = Variant::NIL;
  1937. break;
  1938. case ShaderLanguage::TYPE_BOOL:
  1939. pi.type = Variant::BOOL;
  1940. break;
  1941. case ShaderLanguage::TYPE_BVEC2:
  1942. pi.type = Variant::INT;
  1943. pi.hint = PROPERTY_HINT_FLAGS;
  1944. pi.hint_string = "x,y";
  1945. break;
  1946. case ShaderLanguage::TYPE_BVEC3:
  1947. pi.type = Variant::INT;
  1948. pi.hint = PROPERTY_HINT_FLAGS;
  1949. pi.hint_string = "x,y,z";
  1950. break;
  1951. case ShaderLanguage::TYPE_BVEC4:
  1952. pi.type = Variant::INT;
  1953. pi.hint = PROPERTY_HINT_FLAGS;
  1954. pi.hint_string = "x,y,z,w";
  1955. break;
  1956. case ShaderLanguage::TYPE_UINT:
  1957. case ShaderLanguage::TYPE_INT: {
  1958. pi.type = Variant::INT;
  1959. if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_RANGE) {
  1960. pi.hint = PROPERTY_HINT_RANGE;
  1961. pi.hint_string = rtos(u.hint_range[0]) + "," + rtos(u.hint_range[1]) + "," + rtos(u.hint_range[2]);
  1962. }
  1963. } break;
  1964. case ShaderLanguage::TYPE_IVEC2:
  1965. case ShaderLanguage::TYPE_IVEC3:
  1966. case ShaderLanguage::TYPE_IVEC4:
  1967. case ShaderLanguage::TYPE_UVEC2:
  1968. case ShaderLanguage::TYPE_UVEC3:
  1969. case ShaderLanguage::TYPE_UVEC4: {
  1970. pi.type = Variant::POOL_INT_ARRAY;
  1971. } break;
  1972. case ShaderLanguage::TYPE_FLOAT: {
  1973. pi.type = Variant::REAL;
  1974. if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_RANGE) {
  1975. pi.hint = PROPERTY_HINT_RANGE;
  1976. pi.hint_string = rtos(u.hint_range[0]) + "," + rtos(u.hint_range[1]) + "," + rtos(u.hint_range[2]);
  1977. }
  1978. } break;
  1979. case ShaderLanguage::TYPE_VEC2:
  1980. pi.type = Variant::VECTOR2;
  1981. break;
  1982. case ShaderLanguage::TYPE_VEC3:
  1983. pi.type = Variant::VECTOR3;
  1984. break;
  1985. case ShaderLanguage::TYPE_VEC4: {
  1986. if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_COLOR) {
  1987. pi.type = Variant::COLOR;
  1988. } else {
  1989. pi.type = Variant::PLANE;
  1990. }
  1991. } break;
  1992. case ShaderLanguage::TYPE_MAT2:
  1993. pi.type = Variant::TRANSFORM2D;
  1994. break;
  1995. case ShaderLanguage::TYPE_MAT3:
  1996. pi.type = Variant::BASIS;
  1997. break;
  1998. case ShaderLanguage::TYPE_MAT4:
  1999. pi.type = Variant::TRANSFORM;
  2000. break;
  2001. case ShaderLanguage::TYPE_SAMPLER2D:
  2002. case ShaderLanguage::TYPE_SAMPLEREXT:
  2003. case ShaderLanguage::TYPE_ISAMPLER2D:
  2004. case ShaderLanguage::TYPE_USAMPLER2D: {
  2005. pi.type = Variant::OBJECT;
  2006. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  2007. pi.hint_string = "Texture";
  2008. } break;
  2009. case ShaderLanguage::TYPE_SAMPLER2DARRAY:
  2010. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  2011. case ShaderLanguage::TYPE_USAMPLER2DARRAY: {
  2012. pi.type = Variant::OBJECT;
  2013. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  2014. pi.hint_string = "TextureArray";
  2015. } break;
  2016. case ShaderLanguage::TYPE_SAMPLER3D:
  2017. case ShaderLanguage::TYPE_ISAMPLER3D:
  2018. case ShaderLanguage::TYPE_USAMPLER3D: {
  2019. pi.type = Variant::OBJECT;
  2020. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  2021. pi.hint_string = "Texture3D";
  2022. } break;
  2023. case ShaderLanguage::TYPE_SAMPLERCUBE: {
  2024. pi.type = Variant::OBJECT;
  2025. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  2026. pi.hint_string = "CubeMap";
  2027. } break;
  2028. default: {
  2029. }
  2030. };
  2031. p_param_list->push_back(pi);
  2032. }
  2033. }
  2034. void RasterizerStorageGLES3::shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture) {
  2035. Shader *shader = shader_owner.get(p_shader);
  2036. ERR_FAIL_COND(!shader);
  2037. ERR_FAIL_COND(p_texture.is_valid() && !texture_owner.owns(p_texture));
  2038. if (p_texture.is_valid()) {
  2039. shader->default_textures[p_name] = p_texture;
  2040. } else {
  2041. shader->default_textures.erase(p_name);
  2042. }
  2043. _shader_make_dirty(shader);
  2044. }
  2045. RID RasterizerStorageGLES3::shader_get_default_texture_param(RID p_shader, const StringName &p_name) const {
  2046. const Shader *shader = shader_owner.get(p_shader);
  2047. ERR_FAIL_COND_V(!shader, RID());
  2048. const Map<StringName, RID>::Element *E = shader->default_textures.find(p_name);
  2049. if (!E) {
  2050. return RID();
  2051. }
  2052. return E->get();
  2053. }
  2054. void RasterizerStorageGLES3::shader_add_custom_define(RID p_shader, const String &p_define) {
  2055. Shader *shader = shader_owner.get(p_shader);
  2056. ERR_FAIL_COND(!shader);
  2057. shader->shader->add_custom_define(p_define);
  2058. _shader_make_dirty(shader);
  2059. }
  2060. void RasterizerStorageGLES3::shader_get_custom_defines(RID p_shader, Vector<String> *p_defines) const {
  2061. Shader *shader = shader_owner.get(p_shader);
  2062. ERR_FAIL_COND(!shader);
  2063. shader->shader->get_custom_defines(p_defines);
  2064. }
  2065. void RasterizerStorageGLES3::shader_remove_custom_define(RID p_shader, const String &p_define) {
  2066. Shader *shader = shader_owner.get(p_shader);
  2067. ERR_FAIL_COND(!shader);
  2068. shader->shader->remove_custom_define(p_define);
  2069. _shader_make_dirty(shader);
  2070. }
  2071. void RasterizerStorageGLES3::set_shader_async_hidden_forbidden(bool p_forbidden) {
  2072. ShaderGLES3::async_hidden_forbidden = p_forbidden;
  2073. }
  2074. bool RasterizerStorageGLES3::is_shader_async_hidden_forbidden() {
  2075. return ShaderGLES3::async_hidden_forbidden;
  2076. }
  2077. /* COMMON MATERIAL API */
  2078. void RasterizerStorageGLES3::_material_make_dirty(Material *p_material) const {
  2079. if (p_material->dirty_list.in_list()) {
  2080. return;
  2081. }
  2082. _material_dirty_list.add(&p_material->dirty_list);
  2083. }
  2084. RID RasterizerStorageGLES3::material_create() {
  2085. Material *material = memnew(Material);
  2086. return material_owner.make_rid(material);
  2087. }
  2088. void RasterizerStorageGLES3::material_set_shader(RID p_material, RID p_shader) {
  2089. Material *material = material_owner.get(p_material);
  2090. ERR_FAIL_COND(!material);
  2091. Shader *shader = shader_owner.getornull(p_shader);
  2092. if (material->shader) {
  2093. //if shader, remove from previous shader material list
  2094. material->shader->materials.remove(&material->list);
  2095. }
  2096. material->shader = shader;
  2097. if (shader) {
  2098. shader->materials.add(&material->list);
  2099. }
  2100. _material_make_dirty(material);
  2101. }
  2102. RID RasterizerStorageGLES3::material_get_shader(RID p_material) const {
  2103. const Material *material = material_owner.get(p_material);
  2104. ERR_FAIL_COND_V(!material, RID());
  2105. if (material->shader) {
  2106. return material->shader->self;
  2107. }
  2108. return RID();
  2109. }
  2110. void RasterizerStorageGLES3::material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) {
  2111. Material *material = material_owner.get(p_material);
  2112. ERR_FAIL_COND(!material);
  2113. if (p_value.get_type() == Variant::NIL) {
  2114. material->params.erase(p_param);
  2115. } else {
  2116. material->params[p_param] = p_value;
  2117. }
  2118. _material_make_dirty(material);
  2119. }
  2120. Variant RasterizerStorageGLES3::material_get_param(RID p_material, const StringName &p_param) const {
  2121. const Material *material = material_owner.get(p_material);
  2122. ERR_FAIL_COND_V(!material, Variant());
  2123. if (material->params.has(p_param)) {
  2124. return material->params[p_param];
  2125. }
  2126. return material_get_param_default(p_material, p_param);
  2127. }
  2128. Variant RasterizerStorageGLES3::material_get_param_default(RID p_material, const StringName &p_param) const {
  2129. const Material *material = material_owner.get(p_material);
  2130. ERR_FAIL_COND_V(!material, Variant());
  2131. if (material->shader) {
  2132. if (material->shader->uniforms.has(p_param)) {
  2133. ShaderLanguage::ShaderNode::Uniform uniform = material->shader->uniforms[p_param];
  2134. Vector<ShaderLanguage::ConstantNode::Value> default_value = uniform.default_value;
  2135. return ShaderLanguage::constant_value_to_variant(default_value, uniform.type, uniform.hint);
  2136. }
  2137. }
  2138. return Variant();
  2139. }
  2140. void RasterizerStorageGLES3::material_set_line_width(RID p_material, float p_width) {
  2141. Material *material = material_owner.get(p_material);
  2142. ERR_FAIL_COND(!material);
  2143. material->line_width = p_width;
  2144. }
  2145. void RasterizerStorageGLES3::material_set_next_pass(RID p_material, RID p_next_material) {
  2146. Material *material = material_owner.get(p_material);
  2147. ERR_FAIL_COND(!material);
  2148. material->next_pass = p_next_material;
  2149. }
  2150. bool RasterizerStorageGLES3::material_is_animated(RID p_material) {
  2151. Material *material = material_owner.get(p_material);
  2152. ERR_FAIL_COND_V(!material, false);
  2153. if (material->dirty_list.in_list()) {
  2154. _update_material(material);
  2155. }
  2156. bool animated = material->is_animated_cache;
  2157. if (!animated && material->next_pass.is_valid()) {
  2158. animated = material_is_animated(material->next_pass);
  2159. }
  2160. return animated;
  2161. }
  2162. bool RasterizerStorageGLES3::material_casts_shadows(RID p_material) {
  2163. Material *material = material_owner.get(p_material);
  2164. ERR_FAIL_COND_V(!material, false);
  2165. if (material->dirty_list.in_list()) {
  2166. _update_material(material);
  2167. }
  2168. bool casts_shadows = material->can_cast_shadow_cache;
  2169. if (!casts_shadows && material->next_pass.is_valid()) {
  2170. casts_shadows = material_casts_shadows(material->next_pass);
  2171. }
  2172. return casts_shadows;
  2173. }
  2174. bool RasterizerStorageGLES3::material_uses_tangents(RID p_material) {
  2175. Material *material = material_owner.get(p_material);
  2176. ERR_FAIL_COND_V(!material, false);
  2177. if (!material->shader) {
  2178. return false;
  2179. }
  2180. if (material->shader->dirty_list.in_list()) {
  2181. _update_shader(material->shader);
  2182. }
  2183. return material->shader->spatial.uses_tangent;
  2184. }
  2185. bool RasterizerStorageGLES3::material_uses_ensure_correct_normals(RID p_material) {
  2186. Material *material = material_owner.get(p_material);
  2187. ERR_FAIL_COND_V(!material, false);
  2188. if (!material->shader) {
  2189. return false;
  2190. }
  2191. if (material->shader->dirty_list.in_list()) {
  2192. _update_shader(material->shader);
  2193. }
  2194. return material->shader->spatial.uses_ensure_correct_normals;
  2195. }
  2196. void RasterizerStorageGLES3::material_add_instance_owner(RID p_material, RasterizerScene::InstanceBase *p_instance) {
  2197. Material *material = material_owner.get(p_material);
  2198. ERR_FAIL_COND(!material);
  2199. Map<RasterizerScene::InstanceBase *, int>::Element *E = material->instance_owners.find(p_instance);
  2200. if (E) {
  2201. E->get()++;
  2202. } else {
  2203. material->instance_owners[p_instance] = 1;
  2204. }
  2205. }
  2206. void RasterizerStorageGLES3::material_remove_instance_owner(RID p_material, RasterizerScene::InstanceBase *p_instance) {
  2207. Material *material = material_owner.get(p_material);
  2208. ERR_FAIL_COND(!material);
  2209. Map<RasterizerScene::InstanceBase *, int>::Element *E = material->instance_owners.find(p_instance);
  2210. ERR_FAIL_COND(!E);
  2211. E->get()--;
  2212. if (E->get() == 0) {
  2213. material->instance_owners.erase(E);
  2214. }
  2215. }
  2216. void RasterizerStorageGLES3::material_set_render_priority(RID p_material, int priority) {
  2217. ERR_FAIL_COND(priority < VS::MATERIAL_RENDER_PRIORITY_MIN);
  2218. ERR_FAIL_COND(priority > VS::MATERIAL_RENDER_PRIORITY_MAX);
  2219. Material *material = material_owner.get(p_material);
  2220. ERR_FAIL_COND(!material);
  2221. material->render_priority = priority;
  2222. }
  2223. _FORCE_INLINE_ static void _fill_std140_variant_ubo_value(ShaderLanguage::DataType type, const Variant &value, uint8_t *data, bool p_linear_color) {
  2224. switch (type) {
  2225. case ShaderLanguage::TYPE_BOOL: {
  2226. bool v = value;
  2227. GLuint *gui = (GLuint *)data;
  2228. *gui = v ? GL_TRUE : GL_FALSE;
  2229. } break;
  2230. case ShaderLanguage::TYPE_BVEC2: {
  2231. int v = value;
  2232. GLuint *gui = (GLuint *)data;
  2233. gui[0] = (v & 1) ? GL_TRUE : GL_FALSE;
  2234. gui[1] = (v & 2) ? GL_TRUE : GL_FALSE;
  2235. } break;
  2236. case ShaderLanguage::TYPE_BVEC3: {
  2237. int v = value;
  2238. GLuint *gui = (GLuint *)data;
  2239. gui[0] = (v & 1) ? GL_TRUE : GL_FALSE;
  2240. gui[1] = (v & 2) ? GL_TRUE : GL_FALSE;
  2241. gui[2] = (v & 4) ? GL_TRUE : GL_FALSE;
  2242. } break;
  2243. case ShaderLanguage::TYPE_BVEC4: {
  2244. int v = value;
  2245. GLuint *gui = (GLuint *)data;
  2246. gui[0] = (v & 1) ? GL_TRUE : GL_FALSE;
  2247. gui[1] = (v & 2) ? GL_TRUE : GL_FALSE;
  2248. gui[2] = (v & 4) ? GL_TRUE : GL_FALSE;
  2249. gui[3] = (v & 8) ? GL_TRUE : GL_FALSE;
  2250. } break;
  2251. case ShaderLanguage::TYPE_INT: {
  2252. int v = value;
  2253. GLint *gui = (GLint *)data;
  2254. gui[0] = v;
  2255. } break;
  2256. case ShaderLanguage::TYPE_IVEC2: {
  2257. PoolVector<int> iv = value;
  2258. int s = iv.size();
  2259. GLint *gui = (GLint *)data;
  2260. PoolVector<int>::Read r = iv.read();
  2261. for (int i = 0; i < 2; i++) {
  2262. if (i < s) {
  2263. gui[i] = r[i];
  2264. } else {
  2265. gui[i] = 0;
  2266. }
  2267. }
  2268. } break;
  2269. case ShaderLanguage::TYPE_IVEC3: {
  2270. PoolVector<int> iv = value;
  2271. int s = iv.size();
  2272. GLint *gui = (GLint *)data;
  2273. PoolVector<int>::Read r = iv.read();
  2274. for (int i = 0; i < 3; i++) {
  2275. if (i < s) {
  2276. gui[i] = r[i];
  2277. } else {
  2278. gui[i] = 0;
  2279. }
  2280. }
  2281. } break;
  2282. case ShaderLanguage::TYPE_IVEC4: {
  2283. PoolVector<int> iv = value;
  2284. int s = iv.size();
  2285. GLint *gui = (GLint *)data;
  2286. PoolVector<int>::Read r = iv.read();
  2287. for (int i = 0; i < 4; i++) {
  2288. if (i < s) {
  2289. gui[i] = r[i];
  2290. } else {
  2291. gui[i] = 0;
  2292. }
  2293. }
  2294. } break;
  2295. case ShaderLanguage::TYPE_UINT: {
  2296. int v = value;
  2297. GLuint *gui = (GLuint *)data;
  2298. gui[0] = v;
  2299. } break;
  2300. case ShaderLanguage::TYPE_UVEC2: {
  2301. PoolVector<int> iv = value;
  2302. int s = iv.size();
  2303. GLuint *gui = (GLuint *)data;
  2304. PoolVector<int>::Read r = iv.read();
  2305. for (int i = 0; i < 2; i++) {
  2306. if (i < s) {
  2307. gui[i] = r[i];
  2308. } else {
  2309. gui[i] = 0;
  2310. }
  2311. }
  2312. } break;
  2313. case ShaderLanguage::TYPE_UVEC3: {
  2314. PoolVector<int> iv = value;
  2315. int s = iv.size();
  2316. GLuint *gui = (GLuint *)data;
  2317. PoolVector<int>::Read r = iv.read();
  2318. for (int i = 0; i < 3; i++) {
  2319. if (i < s) {
  2320. gui[i] = r[i];
  2321. } else {
  2322. gui[i] = 0;
  2323. }
  2324. }
  2325. } break;
  2326. case ShaderLanguage::TYPE_UVEC4: {
  2327. PoolVector<int> iv = value;
  2328. int s = iv.size();
  2329. GLuint *gui = (GLuint *)data;
  2330. PoolVector<int>::Read r = iv.read();
  2331. for (int i = 0; i < 4; i++) {
  2332. if (i < s) {
  2333. gui[i] = r[i];
  2334. } else {
  2335. gui[i] = 0;
  2336. }
  2337. }
  2338. } break;
  2339. case ShaderLanguage::TYPE_FLOAT: {
  2340. float v = value;
  2341. GLfloat *gui = (GLfloat *)data;
  2342. gui[0] = v;
  2343. } break;
  2344. case ShaderLanguage::TYPE_VEC2: {
  2345. Vector2 v = value;
  2346. GLfloat *gui = (GLfloat *)data;
  2347. gui[0] = v.x;
  2348. gui[1] = v.y;
  2349. } break;
  2350. case ShaderLanguage::TYPE_VEC3: {
  2351. Vector3 v = value;
  2352. GLfloat *gui = (GLfloat *)data;
  2353. gui[0] = v.x;
  2354. gui[1] = v.y;
  2355. gui[2] = v.z;
  2356. } break;
  2357. case ShaderLanguage::TYPE_VEC4: {
  2358. GLfloat *gui = (GLfloat *)data;
  2359. if (value.get_type() == Variant::COLOR) {
  2360. Color v = value;
  2361. if (p_linear_color) {
  2362. v = v.to_linear();
  2363. }
  2364. gui[0] = v.r;
  2365. gui[1] = v.g;
  2366. gui[2] = v.b;
  2367. gui[3] = v.a;
  2368. } else if (value.get_type() == Variant::RECT2) {
  2369. Rect2 v = value;
  2370. gui[0] = v.position.x;
  2371. gui[1] = v.position.y;
  2372. gui[2] = v.size.x;
  2373. gui[3] = v.size.y;
  2374. } else if (value.get_type() == Variant::QUAT) {
  2375. Quat v = value;
  2376. gui[0] = v.x;
  2377. gui[1] = v.y;
  2378. gui[2] = v.z;
  2379. gui[3] = v.w;
  2380. } else {
  2381. Plane v = value;
  2382. gui[0] = v.normal.x;
  2383. gui[1] = v.normal.y;
  2384. gui[2] = v.normal.z;
  2385. gui[3] = v.d;
  2386. }
  2387. } break;
  2388. case ShaderLanguage::TYPE_MAT2: {
  2389. Transform2D v = value;
  2390. GLfloat *gui = (GLfloat *)data;
  2391. //in std140 members of mat2 are treated as vec4s
  2392. gui[0] = v.elements[0][0];
  2393. gui[1] = v.elements[0][1];
  2394. gui[2] = 0;
  2395. gui[3] = 0;
  2396. gui[4] = v.elements[1][0];
  2397. gui[5] = v.elements[1][1];
  2398. gui[6] = 0;
  2399. gui[7] = 0;
  2400. } break;
  2401. case ShaderLanguage::TYPE_MAT3: {
  2402. Basis v = value;
  2403. GLfloat *gui = (GLfloat *)data;
  2404. gui[0] = v.elements[0][0];
  2405. gui[1] = v.elements[1][0];
  2406. gui[2] = v.elements[2][0];
  2407. gui[3] = 0;
  2408. gui[4] = v.elements[0][1];
  2409. gui[5] = v.elements[1][1];
  2410. gui[6] = v.elements[2][1];
  2411. gui[7] = 0;
  2412. gui[8] = v.elements[0][2];
  2413. gui[9] = v.elements[1][2];
  2414. gui[10] = v.elements[2][2];
  2415. gui[11] = 0;
  2416. } break;
  2417. case ShaderLanguage::TYPE_MAT4: {
  2418. Transform v = value;
  2419. GLfloat *gui = (GLfloat *)data;
  2420. gui[0] = v.basis.elements[0][0];
  2421. gui[1] = v.basis.elements[1][0];
  2422. gui[2] = v.basis.elements[2][0];
  2423. gui[3] = 0;
  2424. gui[4] = v.basis.elements[0][1];
  2425. gui[5] = v.basis.elements[1][1];
  2426. gui[6] = v.basis.elements[2][1];
  2427. gui[7] = 0;
  2428. gui[8] = v.basis.elements[0][2];
  2429. gui[9] = v.basis.elements[1][2];
  2430. gui[10] = v.basis.elements[2][2];
  2431. gui[11] = 0;
  2432. gui[12] = v.origin.x;
  2433. gui[13] = v.origin.y;
  2434. gui[14] = v.origin.z;
  2435. gui[15] = 1;
  2436. } break;
  2437. default: {
  2438. }
  2439. }
  2440. }
  2441. _FORCE_INLINE_ static void _fill_std140_ubo_value(ShaderLanguage::DataType type, const Vector<ShaderLanguage::ConstantNode::Value> &value, uint8_t *data) {
  2442. switch (type) {
  2443. case ShaderLanguage::TYPE_BOOL: {
  2444. GLuint *gui = (GLuint *)data;
  2445. *gui = value[0].boolean ? GL_TRUE : GL_FALSE;
  2446. } break;
  2447. case ShaderLanguage::TYPE_BVEC2: {
  2448. GLuint *gui = (GLuint *)data;
  2449. gui[0] = value[0].boolean ? GL_TRUE : GL_FALSE;
  2450. gui[1] = value[1].boolean ? GL_TRUE : GL_FALSE;
  2451. } break;
  2452. case ShaderLanguage::TYPE_BVEC3: {
  2453. GLuint *gui = (GLuint *)data;
  2454. gui[0] = value[0].boolean ? GL_TRUE : GL_FALSE;
  2455. gui[1] = value[1].boolean ? GL_TRUE : GL_FALSE;
  2456. gui[2] = value[2].boolean ? GL_TRUE : GL_FALSE;
  2457. } break;
  2458. case ShaderLanguage::TYPE_BVEC4: {
  2459. GLuint *gui = (GLuint *)data;
  2460. gui[0] = value[0].boolean ? GL_TRUE : GL_FALSE;
  2461. gui[1] = value[1].boolean ? GL_TRUE : GL_FALSE;
  2462. gui[2] = value[2].boolean ? GL_TRUE : GL_FALSE;
  2463. gui[3] = value[3].boolean ? GL_TRUE : GL_FALSE;
  2464. } break;
  2465. case ShaderLanguage::TYPE_INT: {
  2466. GLint *gui = (GLint *)data;
  2467. gui[0] = value[0].sint;
  2468. } break;
  2469. case ShaderLanguage::TYPE_IVEC2: {
  2470. GLint *gui = (GLint *)data;
  2471. for (int i = 0; i < 2; i++) {
  2472. gui[i] = value[i].sint;
  2473. }
  2474. } break;
  2475. case ShaderLanguage::TYPE_IVEC3: {
  2476. GLint *gui = (GLint *)data;
  2477. for (int i = 0; i < 3; i++) {
  2478. gui[i] = value[i].sint;
  2479. }
  2480. } break;
  2481. case ShaderLanguage::TYPE_IVEC4: {
  2482. GLint *gui = (GLint *)data;
  2483. for (int i = 0; i < 4; i++) {
  2484. gui[i] = value[i].sint;
  2485. }
  2486. } break;
  2487. case ShaderLanguage::TYPE_UINT: {
  2488. GLuint *gui = (GLuint *)data;
  2489. gui[0] = value[0].uint;
  2490. } break;
  2491. case ShaderLanguage::TYPE_UVEC2: {
  2492. GLint *gui = (GLint *)data;
  2493. for (int i = 0; i < 2; i++) {
  2494. gui[i] = value[i].uint;
  2495. }
  2496. } break;
  2497. case ShaderLanguage::TYPE_UVEC3: {
  2498. GLint *gui = (GLint *)data;
  2499. for (int i = 0; i < 3; i++) {
  2500. gui[i] = value[i].uint;
  2501. }
  2502. } break;
  2503. case ShaderLanguage::TYPE_UVEC4: {
  2504. GLint *gui = (GLint *)data;
  2505. for (int i = 0; i < 4; i++) {
  2506. gui[i] = value[i].uint;
  2507. }
  2508. } break;
  2509. case ShaderLanguage::TYPE_FLOAT: {
  2510. GLfloat *gui = (GLfloat *)data;
  2511. gui[0] = value[0].real;
  2512. } break;
  2513. case ShaderLanguage::TYPE_VEC2: {
  2514. GLfloat *gui = (GLfloat *)data;
  2515. for (int i = 0; i < 2; i++) {
  2516. gui[i] = value[i].real;
  2517. }
  2518. } break;
  2519. case ShaderLanguage::TYPE_VEC3: {
  2520. GLfloat *gui = (GLfloat *)data;
  2521. for (int i = 0; i < 3; i++) {
  2522. gui[i] = value[i].real;
  2523. }
  2524. } break;
  2525. case ShaderLanguage::TYPE_VEC4: {
  2526. GLfloat *gui = (GLfloat *)data;
  2527. for (int i = 0; i < 4; i++) {
  2528. gui[i] = value[i].real;
  2529. }
  2530. } break;
  2531. case ShaderLanguage::TYPE_MAT2: {
  2532. GLfloat *gui = (GLfloat *)data;
  2533. //in std140 members of mat2 are treated as vec4s
  2534. gui[0] = value[0].real;
  2535. gui[1] = value[1].real;
  2536. gui[2] = 0;
  2537. gui[3] = 0;
  2538. gui[4] = value[2].real;
  2539. gui[5] = value[3].real;
  2540. gui[6] = 0;
  2541. gui[7] = 0;
  2542. } break;
  2543. case ShaderLanguage::TYPE_MAT3: {
  2544. GLfloat *gui = (GLfloat *)data;
  2545. gui[0] = value[0].real;
  2546. gui[1] = value[1].real;
  2547. gui[2] = value[2].real;
  2548. gui[3] = 0;
  2549. gui[4] = value[3].real;
  2550. gui[5] = value[4].real;
  2551. gui[6] = value[5].real;
  2552. gui[7] = 0;
  2553. gui[8] = value[6].real;
  2554. gui[9] = value[7].real;
  2555. gui[10] = value[8].real;
  2556. gui[11] = 0;
  2557. } break;
  2558. case ShaderLanguage::TYPE_MAT4: {
  2559. GLfloat *gui = (GLfloat *)data;
  2560. for (int i = 0; i < 16; i++) {
  2561. gui[i] = value[i].real;
  2562. }
  2563. } break;
  2564. default: {
  2565. }
  2566. }
  2567. }
  2568. _FORCE_INLINE_ static void _fill_std140_ubo_empty(ShaderLanguage::DataType type, uint8_t *data) {
  2569. switch (type) {
  2570. case ShaderLanguage::TYPE_BOOL:
  2571. case ShaderLanguage::TYPE_INT:
  2572. case ShaderLanguage::TYPE_UINT:
  2573. case ShaderLanguage::TYPE_FLOAT: {
  2574. memset(data, 0, 4);
  2575. } break;
  2576. case ShaderLanguage::TYPE_BVEC2:
  2577. case ShaderLanguage::TYPE_IVEC2:
  2578. case ShaderLanguage::TYPE_UVEC2:
  2579. case ShaderLanguage::TYPE_VEC2: {
  2580. memset(data, 0, 8);
  2581. } break;
  2582. case ShaderLanguage::TYPE_BVEC3:
  2583. case ShaderLanguage::TYPE_IVEC3:
  2584. case ShaderLanguage::TYPE_UVEC3:
  2585. case ShaderLanguage::TYPE_VEC3: {
  2586. memset(data, 0, 12);
  2587. } break;
  2588. case ShaderLanguage::TYPE_BVEC4:
  2589. case ShaderLanguage::TYPE_IVEC4:
  2590. case ShaderLanguage::TYPE_UVEC4:
  2591. case ShaderLanguage::TYPE_VEC4: {
  2592. memset(data, 0, 16);
  2593. } break;
  2594. case ShaderLanguage::TYPE_MAT2: {
  2595. memset(data, 0, 32);
  2596. } break;
  2597. case ShaderLanguage::TYPE_MAT3: {
  2598. memset(data, 0, 48);
  2599. } break;
  2600. case ShaderLanguage::TYPE_MAT4: {
  2601. memset(data, 0, 64);
  2602. } break;
  2603. default: {
  2604. }
  2605. }
  2606. }
  2607. void RasterizerStorageGLES3::_update_material(Material *material) {
  2608. if (material->dirty_list.in_list()) {
  2609. _material_dirty_list.remove(&material->dirty_list);
  2610. }
  2611. if (material->shader && material->shader->dirty_list.in_list()) {
  2612. _update_shader(material->shader);
  2613. }
  2614. if (material->shader && !material->shader->valid) {
  2615. return;
  2616. }
  2617. //update caches
  2618. {
  2619. bool can_cast_shadow = false;
  2620. bool is_animated = false;
  2621. if (material->shader && material->shader->mode == VS::SHADER_SPATIAL) {
  2622. if (material->shader->spatial.blend_mode == Shader::Spatial::BLEND_MODE_MIX &&
  2623. (!(material->shader->spatial.uses_alpha && !material->shader->spatial.uses_alpha_scissor) || material->shader->spatial.depth_draw_mode == Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS)) {
  2624. can_cast_shadow = true;
  2625. }
  2626. if (material->shader->spatial.uses_discard && material->shader->uses_fragment_time) {
  2627. is_animated = true;
  2628. }
  2629. if (material->shader->spatial.uses_vertex && material->shader->uses_vertex_time) {
  2630. is_animated = true;
  2631. }
  2632. if (can_cast_shadow != material->can_cast_shadow_cache || is_animated != material->is_animated_cache) {
  2633. material->can_cast_shadow_cache = can_cast_shadow;
  2634. material->is_animated_cache = is_animated;
  2635. for (Map<Geometry *, int>::Element *E = material->geometry_owners.front(); E; E = E->next()) {
  2636. E->key()->material_changed_notify();
  2637. }
  2638. for (Map<RasterizerScene::InstanceBase *, int>::Element *E = material->instance_owners.front(); E; E = E->next()) {
  2639. E->key()->base_changed(false, true);
  2640. }
  2641. }
  2642. }
  2643. }
  2644. //clear ubo if it needs to be cleared
  2645. if (material->ubo_size) {
  2646. if (!material->shader || material->shader->ubo_size != material->ubo_size) {
  2647. //by by ubo
  2648. glDeleteBuffers(1, &material->ubo_id);
  2649. material->ubo_id = 0;
  2650. material->ubo_size = 0;
  2651. }
  2652. }
  2653. //create ubo if it needs to be created
  2654. if (material->ubo_size == 0 && material->shader && material->shader->ubo_size) {
  2655. glGenBuffers(1, &material->ubo_id);
  2656. glBindBuffer(GL_UNIFORM_BUFFER, material->ubo_id);
  2657. glBufferData(GL_UNIFORM_BUFFER, material->shader->ubo_size, nullptr, GL_STATIC_DRAW);
  2658. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2659. material->ubo_size = material->shader->ubo_size;
  2660. }
  2661. //fill up the UBO if it needs to be filled
  2662. if (material->shader && material->ubo_size) {
  2663. uint8_t *local_ubo = (uint8_t *)alloca(material->ubo_size);
  2664. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = material->shader->uniforms.front(); E; E = E->next()) {
  2665. if (E->get().order < 0) {
  2666. continue; // texture, does not go here
  2667. }
  2668. //regular uniform
  2669. uint8_t *data = &local_ubo[material->shader->ubo_offsets[E->get().order]];
  2670. Map<StringName, Variant>::Element *V = material->params.find(E->key());
  2671. if (V) {
  2672. //user provided
  2673. _fill_std140_variant_ubo_value(E->get().type, V->get(), data, material->shader->mode == VS::SHADER_SPATIAL);
  2674. } else if (E->get().default_value.size()) {
  2675. //default value
  2676. _fill_std140_ubo_value(E->get().type, E->get().default_value, data);
  2677. //value=E->get().default_value;
  2678. } else {
  2679. //zero because it was not provided
  2680. if (E->get().type == ShaderLanguage::TYPE_VEC4 && E->get().hint == ShaderLanguage::ShaderNode::Uniform::HINT_COLOR) {
  2681. //colors must be set as black, with alpha as 1.0
  2682. _fill_std140_variant_ubo_value(E->get().type, Color(0, 0, 0, 1), data, material->shader->mode == VS::SHADER_SPATIAL);
  2683. } else {
  2684. //else just zero it out
  2685. _fill_std140_ubo_empty(E->get().type, data);
  2686. }
  2687. }
  2688. }
  2689. glBindBuffer(GL_UNIFORM_BUFFER, material->ubo_id);
  2690. glBufferData(GL_UNIFORM_BUFFER, material->ubo_size, local_ubo, GL_STATIC_DRAW);
  2691. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2692. }
  2693. //set up the texture array, for easy access when it needs to be drawn
  2694. if (material->shader && material->shader->texture_count) {
  2695. material->texture_is_3d.resize(material->shader->texture_count);
  2696. material->textures.resize(material->shader->texture_count);
  2697. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = material->shader->uniforms.front(); E; E = E->next()) {
  2698. if (E->get().texture_order < 0) {
  2699. continue; // not a texture, does not go here
  2700. }
  2701. RID texture;
  2702. switch (E->get().type) {
  2703. case ShaderLanguage::TYPE_SAMPLER3D:
  2704. case ShaderLanguage::TYPE_SAMPLER2DARRAY: {
  2705. material->texture_is_3d.write[E->get().texture_order] = true;
  2706. } break;
  2707. default: {
  2708. material->texture_is_3d.write[E->get().texture_order] = false;
  2709. } break;
  2710. }
  2711. Map<StringName, Variant>::Element *V = material->params.find(E->key());
  2712. if (V) {
  2713. texture = V->get();
  2714. }
  2715. if (!texture.is_valid()) {
  2716. Map<StringName, RID>::Element *W = material->shader->default_textures.find(E->key());
  2717. if (W) {
  2718. texture = W->get();
  2719. }
  2720. }
  2721. material->textures.write[E->get().texture_order] = texture;
  2722. }
  2723. } else {
  2724. material->textures.clear();
  2725. material->texture_is_3d.clear();
  2726. }
  2727. }
  2728. void RasterizerStorageGLES3::_material_add_geometry(RID p_material, Geometry *p_geometry) {
  2729. Material *material = material_owner.getornull(p_material);
  2730. ERR_FAIL_COND(!material);
  2731. Map<Geometry *, int>::Element *I = material->geometry_owners.find(p_geometry);
  2732. if (I) {
  2733. I->get()++;
  2734. } else {
  2735. material->geometry_owners[p_geometry] = 1;
  2736. }
  2737. }
  2738. void RasterizerStorageGLES3::_material_remove_geometry(RID p_material, Geometry *p_geometry) {
  2739. Material *material = material_owner.getornull(p_material);
  2740. ERR_FAIL_COND(!material);
  2741. Map<Geometry *, int>::Element *I = material->geometry_owners.find(p_geometry);
  2742. ERR_FAIL_COND(!I);
  2743. I->get()--;
  2744. if (I->get() == 0) {
  2745. material->geometry_owners.erase(I);
  2746. }
  2747. }
  2748. void RasterizerStorageGLES3::update_dirty_materials() {
  2749. while (_material_dirty_list.first()) {
  2750. Material *material = _material_dirty_list.first()->self();
  2751. _update_material(material);
  2752. }
  2753. }
  2754. /* MESH API */
  2755. RID RasterizerStorageGLES3::mesh_create() {
  2756. Mesh *mesh = memnew(Mesh);
  2757. return mesh_owner.make_rid(mesh);
  2758. }
  2759. void RasterizerStorageGLES3::mesh_add_surface(RID p_mesh, uint32_t p_format, VS::PrimitiveType p_primitive, const PoolVector<uint8_t> &p_array, int p_vertex_count, const PoolVector<uint8_t> &p_index_array, int p_index_count, const AABB &p_aabb, const Vector<PoolVector<uint8_t>> &p_blend_shapes, const Vector<AABB> &p_bone_aabbs) {
  2760. PoolVector<uint8_t> array = p_array;
  2761. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2762. ERR_FAIL_COND(!mesh);
  2763. ERR_FAIL_COND(!(p_format & VS::ARRAY_FORMAT_VERTEX));
  2764. //must have index and bones, both.
  2765. {
  2766. uint32_t bones_weight = VS::ARRAY_FORMAT_BONES | VS::ARRAY_FORMAT_WEIGHTS;
  2767. ERR_FAIL_COND_MSG((p_format & bones_weight) && (p_format & bones_weight) != bones_weight, "Array must have both bones and weights in format or none.");
  2768. }
  2769. //bool has_morph = p_blend_shapes.size();
  2770. bool use_split_stream = GLOBAL_GET("rendering/misc/mesh_storage/split_stream") && !(p_format & VS::ARRAY_FLAG_USE_DYNAMIC_UPDATE);
  2771. Surface::Attrib attribs[VS::ARRAY_MAX];
  2772. int attributes_base_offset = 0;
  2773. int attributes_stride = 0;
  2774. int positions_stride = 0;
  2775. for (int i = 0; i < VS::ARRAY_MAX; i++) {
  2776. attribs[i].index = i;
  2777. if (!(p_format & (1 << i))) {
  2778. attribs[i].enabled = false;
  2779. attribs[i].integer = false;
  2780. continue;
  2781. }
  2782. attribs[i].enabled = true;
  2783. attribs[i].offset = attributes_base_offset + attributes_stride;
  2784. attribs[i].integer = false;
  2785. switch (i) {
  2786. case VS::ARRAY_VERTEX: {
  2787. if (p_format & VS::ARRAY_FLAG_USE_2D_VERTICES) {
  2788. attribs[i].size = 2;
  2789. } else {
  2790. attribs[i].size = (p_format & VS::ARRAY_COMPRESS_VERTEX) ? 4 : 3;
  2791. }
  2792. if (p_format & VS::ARRAY_COMPRESS_VERTEX) {
  2793. attribs[i].type = GL_HALF_FLOAT;
  2794. positions_stride += attribs[i].size * 2;
  2795. } else {
  2796. attribs[i].type = GL_FLOAT;
  2797. positions_stride += attribs[i].size * 4;
  2798. }
  2799. attribs[i].normalized = GL_FALSE;
  2800. if (use_split_stream) {
  2801. attributes_base_offset = positions_stride * p_vertex_count;
  2802. } else {
  2803. attributes_base_offset = positions_stride;
  2804. }
  2805. } break;
  2806. case VS::ARRAY_NORMAL: {
  2807. if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
  2808. // Always pack normal and tangent into vec4
  2809. // normal will be xy tangent will be zw
  2810. // normal will always be oct32 (4 byte) encoded
  2811. // UNLESS tangent exists and is also compressed
  2812. // then it will be oct16 encoded along with tangent
  2813. attribs[i].normalized = GL_TRUE;
  2814. attribs[i].size = 2;
  2815. attribs[i].type = GL_SHORT;
  2816. attributes_stride += 4;
  2817. // Storing normal/tangent in the tangent attrib makes it easier to ubershaderify the scene shader
  2818. attribs[i].index = VS::ARRAY_TANGENT;
  2819. } else {
  2820. attribs[i].size = 3;
  2821. if (p_format & VS::ARRAY_COMPRESS_NORMAL) {
  2822. attribs[i].type = GL_BYTE;
  2823. attributes_stride += 4; //pad extra byte
  2824. attribs[i].normalized = GL_TRUE;
  2825. } else {
  2826. attribs[i].type = GL_FLOAT;
  2827. attributes_stride += 12;
  2828. attribs[i].normalized = GL_FALSE;
  2829. }
  2830. }
  2831. } break;
  2832. case VS::ARRAY_TANGENT: {
  2833. if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
  2834. attribs[i].enabled = false;
  2835. attribs[VS::ARRAY_NORMAL].size = 4;
  2836. if (p_format & VS::ARRAY_COMPRESS_TANGENT && p_format & VS::ARRAY_COMPRESS_NORMAL) {
  2837. // normal and tangent will each be oct16 (2 bytes each)
  2838. // pack into single vec4<GL_BYTE> for memory bandwidth
  2839. // savings while keeping 4 byte alignment
  2840. attribs[VS::ARRAY_NORMAL].type = GL_BYTE;
  2841. } else {
  2842. // normal and tangent will each be oct32 (4 bytes each)
  2843. attributes_stride += 4;
  2844. }
  2845. } else {
  2846. attribs[i].size = 4;
  2847. if (p_format & VS::ARRAY_COMPRESS_TANGENT) {
  2848. attribs[i].type = GL_BYTE;
  2849. attributes_stride += 4;
  2850. attribs[i].normalized = GL_TRUE;
  2851. } else {
  2852. attribs[i].type = GL_FLOAT;
  2853. attributes_stride += 16;
  2854. attribs[i].normalized = GL_FALSE;
  2855. }
  2856. }
  2857. } break;
  2858. case VS::ARRAY_COLOR: {
  2859. attribs[i].size = 4;
  2860. if (p_format & VS::ARRAY_COMPRESS_COLOR) {
  2861. attribs[i].type = GL_UNSIGNED_BYTE;
  2862. attributes_stride += 4;
  2863. attribs[i].normalized = GL_TRUE;
  2864. } else {
  2865. attribs[i].type = GL_FLOAT;
  2866. attributes_stride += 16;
  2867. attribs[i].normalized = GL_FALSE;
  2868. }
  2869. } break;
  2870. case VS::ARRAY_TEX_UV: {
  2871. attribs[i].size = 2;
  2872. if (p_format & VS::ARRAY_COMPRESS_TEX_UV) {
  2873. attribs[i].type = GL_HALF_FLOAT;
  2874. attributes_stride += 4;
  2875. } else {
  2876. attribs[i].type = GL_FLOAT;
  2877. attributes_stride += 8;
  2878. }
  2879. attribs[i].normalized = GL_FALSE;
  2880. } break;
  2881. case VS::ARRAY_TEX_UV2: {
  2882. attribs[i].size = 2;
  2883. if (p_format & VS::ARRAY_COMPRESS_TEX_UV2) {
  2884. attribs[i].type = GL_HALF_FLOAT;
  2885. attributes_stride += 4;
  2886. } else {
  2887. attribs[i].type = GL_FLOAT;
  2888. attributes_stride += 8;
  2889. }
  2890. attribs[i].normalized = GL_FALSE;
  2891. } break;
  2892. case VS::ARRAY_BONES: {
  2893. attribs[i].size = 4;
  2894. if (p_format & VS::ARRAY_FLAG_USE_16_BIT_BONES) {
  2895. attribs[i].type = GL_UNSIGNED_SHORT;
  2896. attributes_stride += 8;
  2897. } else {
  2898. attribs[i].type = GL_UNSIGNED_BYTE;
  2899. attributes_stride += 4;
  2900. }
  2901. attribs[i].normalized = GL_FALSE;
  2902. attribs[i].integer = true;
  2903. } break;
  2904. case VS::ARRAY_WEIGHTS: {
  2905. attribs[i].size = 4;
  2906. if (p_format & VS::ARRAY_COMPRESS_WEIGHTS) {
  2907. attribs[i].type = GL_UNSIGNED_SHORT;
  2908. attributes_stride += 8;
  2909. attribs[i].normalized = GL_TRUE;
  2910. } else {
  2911. attribs[i].type = GL_FLOAT;
  2912. attributes_stride += 16;
  2913. attribs[i].normalized = GL_FALSE;
  2914. }
  2915. } break;
  2916. case VS::ARRAY_INDEX: {
  2917. attribs[i].size = 1;
  2918. if (p_vertex_count >= (1 << 16)) {
  2919. attribs[i].type = GL_UNSIGNED_INT;
  2920. attribs[i].stride = 4;
  2921. } else {
  2922. attribs[i].type = GL_UNSIGNED_SHORT;
  2923. attribs[i].stride = 2;
  2924. }
  2925. attribs[i].normalized = GL_FALSE;
  2926. } break;
  2927. }
  2928. }
  2929. if (use_split_stream) {
  2930. attribs[VS::ARRAY_VERTEX].stride = positions_stride;
  2931. for (int i = 1; i < VS::ARRAY_MAX - 1; i++) {
  2932. attribs[i].stride = attributes_stride;
  2933. }
  2934. } else {
  2935. for (int i = 0; i < VS::ARRAY_MAX - 1; i++) {
  2936. attribs[i].stride = positions_stride + attributes_stride;
  2937. }
  2938. }
  2939. //validate sizes
  2940. int stride = positions_stride + attributes_stride;
  2941. int array_size = stride * p_vertex_count;
  2942. int index_array_size = 0;
  2943. if (array.size() != array_size && array.size() + p_vertex_count * 2 == array_size) {
  2944. //old format, convert
  2945. array = PoolVector<uint8_t>();
  2946. array.resize(p_array.size() + p_vertex_count * 2);
  2947. PoolVector<uint8_t>::Write w = array.write();
  2948. PoolVector<uint8_t>::Read r = p_array.read();
  2949. uint16_t *w16 = (uint16_t *)w.ptr();
  2950. const uint16_t *r16 = (uint16_t *)r.ptr();
  2951. uint16_t one = Math::make_half_float(1);
  2952. for (int i = 0; i < p_vertex_count; i++) {
  2953. *w16++ = *r16++;
  2954. *w16++ = *r16++;
  2955. *w16++ = *r16++;
  2956. *w16++ = one;
  2957. for (int j = 0; j < (stride / 2) - 4; j++) {
  2958. *w16++ = *r16++;
  2959. }
  2960. }
  2961. }
  2962. ERR_FAIL_COND(array.size() != array_size);
  2963. if (p_format & VS::ARRAY_FORMAT_INDEX) {
  2964. index_array_size = attribs[VS::ARRAY_INDEX].stride * p_index_count;
  2965. }
  2966. ERR_FAIL_COND(p_index_array.size() != index_array_size);
  2967. ERR_FAIL_COND(p_blend_shapes.size() != mesh->blend_shape_count);
  2968. for (int i = 0; i < p_blend_shapes.size(); i++) {
  2969. ERR_FAIL_COND(p_blend_shapes[i].size() != array_size);
  2970. }
  2971. //ok all valid, create stuff
  2972. Surface *surface = memnew(Surface);
  2973. surface->active = true;
  2974. surface->array_len = p_vertex_count;
  2975. surface->index_array_len = p_index_count;
  2976. surface->array_byte_size = array.size();
  2977. surface->index_array_byte_size = p_index_array.size();
  2978. surface->primitive = p_primitive;
  2979. surface->mesh = mesh;
  2980. surface->format = p_format;
  2981. surface->skeleton_bone_aabb = p_bone_aabbs;
  2982. surface->skeleton_bone_used.resize(surface->skeleton_bone_aabb.size());
  2983. surface->aabb = p_aabb;
  2984. surface->max_bone = p_bone_aabbs.size();
  2985. surface->total_data_size += surface->array_byte_size + surface->index_array_byte_size;
  2986. for (int i = 0; i < surface->skeleton_bone_used.size(); i++) {
  2987. if (surface->skeleton_bone_aabb[i].size.x < 0 || surface->skeleton_bone_aabb[i].size.y < 0 || surface->skeleton_bone_aabb[i].size.z < 0) {
  2988. surface->skeleton_bone_used.write[i] = false;
  2989. } else {
  2990. surface->skeleton_bone_used.write[i] = true;
  2991. }
  2992. }
  2993. for (int i = 0; i < VS::ARRAY_MAX; i++) {
  2994. surface->attribs[i] = attribs[i];
  2995. }
  2996. {
  2997. PoolVector<uint8_t>::Read vr = array.read();
  2998. glGenBuffers(1, &surface->vertex_id);
  2999. glBindBuffer(GL_ARRAY_BUFFER, surface->vertex_id);
  3000. glBufferData(GL_ARRAY_BUFFER, array_size, vr.ptr(), (p_format & VS::ARRAY_FLAG_USE_DYNAMIC_UPDATE) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  3001. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  3002. if (p_format & VS::ARRAY_FORMAT_INDEX) {
  3003. PoolVector<uint8_t>::Read ir = p_index_array.read();
  3004. glGenBuffers(1, &surface->index_id);
  3005. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, surface->index_id);
  3006. glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_array_size, ir.ptr(), GL_STATIC_DRAW);
  3007. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); //unbind
  3008. }
  3009. //generate arrays for faster state switching
  3010. for (int ai = 0; ai < 2; ai++) {
  3011. if (ai == 0) {
  3012. //for normal draw
  3013. glGenVertexArrays(1, &surface->array_id);
  3014. glBindVertexArray(surface->array_id);
  3015. glBindBuffer(GL_ARRAY_BUFFER, surface->vertex_id);
  3016. } else if (ai == 1) {
  3017. //for instancing draw (can be changed and no one cares)
  3018. glGenVertexArrays(1, &surface->instancing_array_id);
  3019. glBindVertexArray(surface->instancing_array_id);
  3020. glBindBuffer(GL_ARRAY_BUFFER, surface->vertex_id);
  3021. }
  3022. for (int i = 0; i < VS::ARRAY_MAX - 1; i++) {
  3023. if (!attribs[i].enabled) {
  3024. continue;
  3025. }
  3026. if (attribs[i].integer) {
  3027. glVertexAttribIPointer(attribs[i].index, attribs[i].size, attribs[i].type, attribs[i].stride, CAST_INT_TO_UCHAR_PTR(attribs[i].offset));
  3028. } else {
  3029. glVertexAttribPointer(attribs[i].index, attribs[i].size, attribs[i].type, attribs[i].normalized, attribs[i].stride, CAST_INT_TO_UCHAR_PTR(attribs[i].offset));
  3030. }
  3031. glEnableVertexAttribArray(attribs[i].index);
  3032. }
  3033. if (surface->index_id) {
  3034. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, surface->index_id);
  3035. }
  3036. glBindVertexArray(0);
  3037. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  3038. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  3039. }
  3040. #ifdef DEBUG_ENABLED
  3041. if (config.generate_wireframes && p_primitive == VS::PRIMITIVE_TRIANGLES) {
  3042. //generate wireframes, this is used mostly by editor
  3043. PoolVector<uint32_t> wf_indices;
  3044. int index_count;
  3045. if (p_format & VS::ARRAY_FORMAT_INDEX) {
  3046. index_count = p_index_count * 2;
  3047. wf_indices.resize(index_count);
  3048. PoolVector<uint8_t>::Read ir = p_index_array.read();
  3049. PoolVector<uint32_t>::Write wr = wf_indices.write();
  3050. if (p_vertex_count < (1 << 16)) {
  3051. //read 16 bit indices
  3052. const uint16_t *src_idx = (const uint16_t *)ir.ptr();
  3053. for (int i = 0; i + 5 < index_count; i += 6) {
  3054. wr[i + 0] = src_idx[i / 2];
  3055. wr[i + 1] = src_idx[i / 2 + 1];
  3056. wr[i + 2] = src_idx[i / 2 + 1];
  3057. wr[i + 3] = src_idx[i / 2 + 2];
  3058. wr[i + 4] = src_idx[i / 2 + 2];
  3059. wr[i + 5] = src_idx[i / 2];
  3060. }
  3061. } else {
  3062. //read 16 bit indices
  3063. const uint32_t *src_idx = (const uint32_t *)ir.ptr();
  3064. for (int i = 0; i + 5 < index_count; i += 6) {
  3065. wr[i + 0] = src_idx[i / 2];
  3066. wr[i + 1] = src_idx[i / 2 + 1];
  3067. wr[i + 2] = src_idx[i / 2 + 1];
  3068. wr[i + 3] = src_idx[i / 2 + 2];
  3069. wr[i + 4] = src_idx[i / 2 + 2];
  3070. wr[i + 5] = src_idx[i / 2];
  3071. }
  3072. }
  3073. } else {
  3074. index_count = p_vertex_count * 2;
  3075. wf_indices.resize(index_count);
  3076. PoolVector<uint32_t>::Write wr = wf_indices.write();
  3077. for (int i = 0; i + 5 < index_count; i += 6) {
  3078. wr[i + 0] = i / 2;
  3079. wr[i + 1] = i / 2 + 1;
  3080. wr[i + 2] = i / 2 + 1;
  3081. wr[i + 3] = i / 2 + 2;
  3082. wr[i + 4] = i / 2 + 2;
  3083. wr[i + 5] = i / 2;
  3084. }
  3085. }
  3086. {
  3087. PoolVector<uint32_t>::Read ir = wf_indices.read();
  3088. glGenBuffers(1, &surface->index_wireframe_id);
  3089. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, surface->index_wireframe_id);
  3090. glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_count * sizeof(uint32_t), ir.ptr(), GL_STATIC_DRAW);
  3091. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); //unbind
  3092. surface->index_wireframe_len = index_count;
  3093. }
  3094. for (int ai = 0; ai < 2; ai++) {
  3095. if (ai == 0) {
  3096. //for normal draw
  3097. glGenVertexArrays(1, &surface->array_wireframe_id);
  3098. glBindVertexArray(surface->array_wireframe_id);
  3099. glBindBuffer(GL_ARRAY_BUFFER, surface->vertex_id);
  3100. } else if (ai == 1) {
  3101. //for instancing draw (can be changed and no one cares)
  3102. glGenVertexArrays(1, &surface->instancing_array_wireframe_id);
  3103. glBindVertexArray(surface->instancing_array_wireframe_id);
  3104. glBindBuffer(GL_ARRAY_BUFFER, surface->vertex_id);
  3105. }
  3106. for (int i = 0; i < VS::ARRAY_MAX - 1; i++) {
  3107. if (!attribs[i].enabled) {
  3108. continue;
  3109. }
  3110. if (attribs[i].integer) {
  3111. glVertexAttribIPointer(attribs[i].index, attribs[i].size, attribs[i].type, attribs[i].stride, CAST_INT_TO_UCHAR_PTR(attribs[i].offset));
  3112. } else {
  3113. glVertexAttribPointer(attribs[i].index, attribs[i].size, attribs[i].type, attribs[i].normalized, attribs[i].stride, CAST_INT_TO_UCHAR_PTR(attribs[i].offset));
  3114. }
  3115. glEnableVertexAttribArray(attribs[i].index);
  3116. }
  3117. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, surface->index_wireframe_id);
  3118. glBindVertexArray(0);
  3119. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  3120. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  3121. }
  3122. }
  3123. #endif
  3124. }
  3125. {
  3126. //blend shapes
  3127. for (int i = 0; i < p_blend_shapes.size(); i++) {
  3128. Surface::BlendShape mt;
  3129. PoolVector<uint8_t>::Read vr = p_blend_shapes[i].read();
  3130. surface->total_data_size += array_size;
  3131. glGenBuffers(1, &mt.vertex_id);
  3132. glBindBuffer(GL_ARRAY_BUFFER, mt.vertex_id);
  3133. glBufferData(GL_ARRAY_BUFFER, array_size, vr.ptr(), GL_STATIC_DRAW);
  3134. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  3135. glGenVertexArrays(1, &mt.array_id);
  3136. glBindVertexArray(mt.array_id);
  3137. glBindBuffer(GL_ARRAY_BUFFER, mt.vertex_id);
  3138. for (int j = 0; j < VS::ARRAY_MAX - 1; j++) {
  3139. if (!attribs[j].enabled) {
  3140. continue;
  3141. }
  3142. if (attribs[j].integer) {
  3143. glVertexAttribIPointer(attribs[j].index, attribs[j].size, attribs[j].type, attribs[j].stride, CAST_INT_TO_UCHAR_PTR(attribs[j].offset));
  3144. } else {
  3145. glVertexAttribPointer(attribs[j].index, attribs[j].size, attribs[j].type, attribs[j].normalized, attribs[j].stride, CAST_INT_TO_UCHAR_PTR(attribs[j].offset));
  3146. }
  3147. glEnableVertexAttribArray(attribs[j].index);
  3148. }
  3149. glBindVertexArray(0);
  3150. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  3151. surface->blend_shapes.push_back(mt);
  3152. }
  3153. }
  3154. mesh->surfaces.push_back(surface);
  3155. mesh->instance_change_notify(true, true);
  3156. info.vertex_mem += surface->total_data_size;
  3157. }
  3158. void RasterizerStorageGLES3::mesh_set_blend_shape_count(RID p_mesh, int p_amount) {
  3159. Mesh *mesh = mesh_owner.getornull(p_mesh);
  3160. ERR_FAIL_COND(!mesh);
  3161. ERR_FAIL_COND(mesh->surfaces.size() != 0);
  3162. ERR_FAIL_COND(p_amount < 0);
  3163. mesh->blend_shape_count = p_amount;
  3164. mesh->instance_change_notify(true, false);
  3165. }
  3166. int RasterizerStorageGLES3::mesh_get_blend_shape_count(RID p_mesh) const {
  3167. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  3168. ERR_FAIL_COND_V(!mesh, 0);
  3169. return mesh->blend_shape_count;
  3170. }
  3171. void RasterizerStorageGLES3::mesh_set_blend_shape_mode(RID p_mesh, VS::BlendShapeMode p_mode) {
  3172. Mesh *mesh = mesh_owner.getornull(p_mesh);
  3173. ERR_FAIL_COND(!mesh);
  3174. mesh->blend_shape_mode = p_mode;
  3175. }
  3176. VS::BlendShapeMode RasterizerStorageGLES3::mesh_get_blend_shape_mode(RID p_mesh) const {
  3177. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  3178. ERR_FAIL_COND_V(!mesh, VS::BLEND_SHAPE_MODE_NORMALIZED);
  3179. return mesh->blend_shape_mode;
  3180. }
  3181. void RasterizerStorageGLES3::mesh_set_blend_shape_values(RID p_mesh, PoolVector<float> p_values) {
  3182. Mesh *mesh = mesh_owner.getornull(p_mesh);
  3183. ERR_FAIL_COND(!mesh);
  3184. mesh->blend_shape_values = p_values;
  3185. }
  3186. PoolVector<float> RasterizerStorageGLES3::mesh_get_blend_shape_values(RID p_mesh) const {
  3187. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  3188. ERR_FAIL_COND_V(!mesh, PoolVector<float>());
  3189. return mesh->blend_shape_values;
  3190. }
  3191. void RasterizerStorageGLES3::mesh_surface_update_region(RID p_mesh, int p_surface, int p_offset, const PoolVector<uint8_t> &p_data) {
  3192. Mesh *mesh = mesh_owner.getornull(p_mesh);
  3193. ERR_FAIL_COND(!mesh);
  3194. ERR_FAIL_INDEX(p_surface, mesh->surfaces.size());
  3195. int total_size = p_data.size();
  3196. ERR_FAIL_COND(p_offset + total_size > mesh->surfaces[p_surface]->array_byte_size);
  3197. PoolVector<uint8_t>::Read r = p_data.read();
  3198. glBindBuffer(GL_ARRAY_BUFFER, mesh->surfaces[p_surface]->vertex_id);
  3199. glBufferSubData(GL_ARRAY_BUFFER, p_offset, total_size, r.ptr());
  3200. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  3201. }
  3202. void RasterizerStorageGLES3::mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) {
  3203. Mesh *mesh = mesh_owner.getornull(p_mesh);
  3204. ERR_FAIL_COND(!mesh);
  3205. ERR_FAIL_INDEX(p_surface, mesh->surfaces.size());
  3206. if (mesh->surfaces[p_surface]->material == p_material) {
  3207. return;
  3208. }
  3209. if (mesh->surfaces[p_surface]->material.is_valid()) {
  3210. _material_remove_geometry(mesh->surfaces[p_surface]->material, mesh->surfaces[p_surface]);
  3211. }
  3212. mesh->surfaces[p_surface]->material = p_material;
  3213. if (mesh->surfaces[p_surface]->material.is_valid()) {
  3214. _material_add_geometry(mesh->surfaces[p_surface]->material, mesh->surfaces[p_surface]);
  3215. }
  3216. mesh->instance_change_notify(false, true);
  3217. }
  3218. RID RasterizerStorageGLES3::mesh_surface_get_material(RID p_mesh, int p_surface) const {
  3219. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  3220. ERR_FAIL_COND_V(!mesh, RID());
  3221. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), RID());
  3222. return mesh->surfaces[p_surface]->material;
  3223. }
  3224. int RasterizerStorageGLES3::mesh_surface_get_array_len(RID p_mesh, int p_surface) const {
  3225. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  3226. ERR_FAIL_COND_V(!mesh, 0);
  3227. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), 0);
  3228. return mesh->surfaces[p_surface]->array_len;
  3229. }
  3230. int RasterizerStorageGLES3::mesh_surface_get_array_index_len(RID p_mesh, int p_surface) const {
  3231. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  3232. ERR_FAIL_COND_V(!mesh, 0);
  3233. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), 0);
  3234. return mesh->surfaces[p_surface]->index_array_len;
  3235. }
  3236. PoolVector<uint8_t> RasterizerStorageGLES3::mesh_surface_get_array(RID p_mesh, int p_surface) const {
  3237. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  3238. ERR_FAIL_COND_V(!mesh, PoolVector<uint8_t>());
  3239. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), PoolVector<uint8_t>());
  3240. Surface *surface = mesh->surfaces[p_surface];
  3241. PoolVector<uint8_t> ret;
  3242. ret.resize(surface->array_byte_size);
  3243. glBindBuffer(GL_ARRAY_BUFFER, surface->vertex_id);
  3244. #if defined(GLES_OVER_GL) || defined(__EMSCRIPTEN__)
  3245. {
  3246. PoolVector<uint8_t>::Write w = ret.write();
  3247. glGetBufferSubData(GL_ARRAY_BUFFER, 0, surface->array_byte_size, w.ptr());
  3248. }
  3249. #else
  3250. void *data = glMapBufferRange(GL_ARRAY_BUFFER, 0, surface->array_byte_size, GL_MAP_READ_BIT);
  3251. ERR_FAIL_NULL_V(data, PoolVector<uint8_t>());
  3252. {
  3253. PoolVector<uint8_t>::Write w = ret.write();
  3254. memcpy(w.ptr(), data, surface->array_byte_size);
  3255. }
  3256. glUnmapBuffer(GL_ARRAY_BUFFER);
  3257. #endif
  3258. glBindBuffer(GL_ARRAY_BUFFER, 0);
  3259. return ret;
  3260. }
  3261. PoolVector<uint8_t> RasterizerStorageGLES3::mesh_surface_get_index_array(RID p_mesh, int p_surface) const {
  3262. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  3263. ERR_FAIL_COND_V(!mesh, PoolVector<uint8_t>());
  3264. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), PoolVector<uint8_t>());
  3265. Surface *surface = mesh->surfaces[p_surface];
  3266. PoolVector<uint8_t> ret;
  3267. ret.resize(surface->index_array_byte_size);
  3268. if (surface->index_array_byte_size > 0) {
  3269. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, surface->index_id);
  3270. #if defined(GLES_OVER_GL) || defined(__EMSCRIPTEN__)
  3271. {
  3272. PoolVector<uint8_t>::Write w = ret.write();
  3273. glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, surface->index_array_byte_size, w.ptr());
  3274. }
  3275. #else
  3276. void *data = glMapBufferRange(GL_ELEMENT_ARRAY_BUFFER, 0, surface->index_array_byte_size, GL_MAP_READ_BIT);
  3277. ERR_FAIL_NULL_V(data, PoolVector<uint8_t>());
  3278. {
  3279. PoolVector<uint8_t>::Write w = ret.write();
  3280. memcpy(w.ptr(), data, surface->index_array_byte_size);
  3281. }
  3282. glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
  3283. #endif
  3284. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  3285. }
  3286. return ret;
  3287. }
  3288. uint32_t RasterizerStorageGLES3::mesh_surface_get_format(RID p_mesh, int p_surface) const {
  3289. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  3290. ERR_FAIL_COND_V(!mesh, 0);
  3291. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), 0);
  3292. return mesh->surfaces[p_surface]->format;
  3293. }
  3294. VS::PrimitiveType RasterizerStorageGLES3::mesh_surface_get_primitive_type(RID p_mesh, int p_surface) const {
  3295. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  3296. ERR_FAIL_COND_V(!mesh, VS::PRIMITIVE_MAX);
  3297. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), VS::PRIMITIVE_MAX);
  3298. return mesh->surfaces[p_surface]->primitive;
  3299. }
  3300. AABB RasterizerStorageGLES3::mesh_surface_get_aabb(RID p_mesh, int p_surface) const {
  3301. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  3302. ERR_FAIL_COND_V(!mesh, AABB());
  3303. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), AABB());
  3304. return mesh->surfaces[p_surface]->aabb;
  3305. }
  3306. Vector<PoolVector<uint8_t>> RasterizerStorageGLES3::mesh_surface_get_blend_shapes(RID p_mesh, int p_surface) const {
  3307. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  3308. ERR_FAIL_COND_V(!mesh, Vector<PoolVector<uint8_t>>());
  3309. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), Vector<PoolVector<uint8_t>>());
  3310. Vector<PoolVector<uint8_t>> bsarr;
  3311. for (int i = 0; i < mesh->surfaces[p_surface]->blend_shapes.size(); i++) {
  3312. PoolVector<uint8_t> ret;
  3313. ret.resize(mesh->surfaces[p_surface]->array_byte_size);
  3314. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->surfaces[p_surface]->blend_shapes[i].vertex_id);
  3315. #if defined(GLES_OVER_GL) || defined(__EMSCRIPTEN__)
  3316. {
  3317. PoolVector<uint8_t>::Write w = ret.write();
  3318. glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, mesh->surfaces[p_surface]->array_byte_size, w.ptr());
  3319. }
  3320. #else
  3321. void *data = glMapBufferRange(GL_ELEMENT_ARRAY_BUFFER, 0, mesh->surfaces[p_surface]->array_byte_size, GL_MAP_READ_BIT);
  3322. ERR_FAIL_COND_V(!data, Vector<PoolVector<uint8_t>>());
  3323. {
  3324. PoolVector<uint8_t>::Write w = ret.write();
  3325. memcpy(w.ptr(), data, mesh->surfaces[p_surface]->array_byte_size);
  3326. }
  3327. glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
  3328. #endif
  3329. bsarr.push_back(ret);
  3330. }
  3331. return bsarr;
  3332. }
  3333. Vector<AABB> RasterizerStorageGLES3::mesh_surface_get_skeleton_aabb(RID p_mesh, int p_surface) const {
  3334. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  3335. ERR_FAIL_COND_V(!mesh, Vector<AABB>());
  3336. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), Vector<AABB>());
  3337. return mesh->surfaces[p_surface]->skeleton_bone_aabb;
  3338. }
  3339. void RasterizerStorageGLES3::mesh_remove_surface(RID p_mesh, int p_surface) {
  3340. Mesh *mesh = mesh_owner.getornull(p_mesh);
  3341. ERR_FAIL_COND(!mesh);
  3342. ERR_FAIL_INDEX(p_surface, mesh->surfaces.size());
  3343. Surface *surface = mesh->surfaces[p_surface];
  3344. if (surface->material.is_valid()) {
  3345. _material_remove_geometry(surface->material, mesh->surfaces[p_surface]);
  3346. }
  3347. glDeleteBuffers(1, &surface->vertex_id);
  3348. if (surface->index_id) {
  3349. glDeleteBuffers(1, &surface->index_id);
  3350. }
  3351. glDeleteVertexArrays(1, &surface->array_id);
  3352. glDeleteVertexArrays(1, &surface->instancing_array_id);
  3353. for (int i = 0; i < surface->blend_shapes.size(); i++) {
  3354. glDeleteBuffers(1, &surface->blend_shapes[i].vertex_id);
  3355. glDeleteVertexArrays(1, &surface->blend_shapes[i].array_id);
  3356. }
  3357. if (surface->index_wireframe_id) {
  3358. glDeleteBuffers(1, &surface->index_wireframe_id);
  3359. glDeleteVertexArrays(1, &surface->array_wireframe_id);
  3360. glDeleteVertexArrays(1, &surface->instancing_array_wireframe_id);
  3361. }
  3362. info.vertex_mem -= surface->total_data_size;
  3363. memdelete(surface);
  3364. mesh->surfaces.remove(p_surface);
  3365. mesh->instance_change_notify(true, true);
  3366. }
  3367. int RasterizerStorageGLES3::mesh_get_surface_count(RID p_mesh) const {
  3368. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  3369. ERR_FAIL_COND_V(!mesh, 0);
  3370. return mesh->surfaces.size();
  3371. }
  3372. void RasterizerStorageGLES3::mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) {
  3373. Mesh *mesh = mesh_owner.getornull(p_mesh);
  3374. ERR_FAIL_COND(!mesh);
  3375. mesh->custom_aabb = p_aabb;
  3376. mesh->instance_change_notify(true, false);
  3377. }
  3378. AABB RasterizerStorageGLES3::mesh_get_custom_aabb(RID p_mesh) const {
  3379. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  3380. ERR_FAIL_COND_V(!mesh, AABB());
  3381. return mesh->custom_aabb;
  3382. }
  3383. AABB RasterizerStorageGLES3::mesh_get_aabb(RID p_mesh, RID p_skeleton) const {
  3384. Mesh *mesh = mesh_owner.get(p_mesh);
  3385. ERR_FAIL_COND_V(!mesh, AABB());
  3386. if (mesh->custom_aabb != AABB()) {
  3387. return mesh->custom_aabb;
  3388. }
  3389. Skeleton *sk = nullptr;
  3390. if (p_skeleton.is_valid()) {
  3391. sk = skeleton_owner.get(p_skeleton);
  3392. }
  3393. AABB aabb;
  3394. if (sk && sk->size != 0) {
  3395. for (int i = 0; i < mesh->surfaces.size(); i++) {
  3396. AABB laabb;
  3397. if ((mesh->surfaces[i]->format & VS::ARRAY_FORMAT_BONES) && mesh->surfaces[i]->skeleton_bone_aabb.size()) {
  3398. int bs = mesh->surfaces[i]->skeleton_bone_aabb.size();
  3399. const AABB *skbones = mesh->surfaces[i]->skeleton_bone_aabb.ptr();
  3400. const bool *skused = mesh->surfaces[i]->skeleton_bone_used.ptr();
  3401. int sbs = sk->size;
  3402. ERR_CONTINUE(bs > sbs);
  3403. const float *texture = sk->skel_texture.ptr();
  3404. bool first = true;
  3405. if (sk->use_2d) {
  3406. for (int j = 0; j < bs; j++) {
  3407. if (!skused[j]) {
  3408. continue;
  3409. }
  3410. int base_ofs = ((j / 256) * 256) * 2 * 4 + (j % 256) * 4;
  3411. Transform mtx;
  3412. mtx.basis[0].x = texture[base_ofs + 0];
  3413. mtx.basis[0].y = texture[base_ofs + 1];
  3414. mtx.origin.x = texture[base_ofs + 3];
  3415. base_ofs += 256 * 4;
  3416. mtx.basis[1].x = texture[base_ofs + 0];
  3417. mtx.basis[1].y = texture[base_ofs + 1];
  3418. mtx.origin.y = texture[base_ofs + 3];
  3419. AABB baabb = mtx.xform(skbones[j]);
  3420. if (first) {
  3421. laabb = baabb;
  3422. first = false;
  3423. } else {
  3424. laabb.merge_with(baabb);
  3425. }
  3426. }
  3427. } else {
  3428. for (int j = 0; j < bs; j++) {
  3429. if (!skused[j]) {
  3430. continue;
  3431. }
  3432. int base_ofs = ((j / 256) * 256) * 3 * 4 + (j % 256) * 4;
  3433. Transform mtx;
  3434. mtx.basis[0].x = texture[base_ofs + 0];
  3435. mtx.basis[0].y = texture[base_ofs + 1];
  3436. mtx.basis[0].z = texture[base_ofs + 2];
  3437. mtx.origin.x = texture[base_ofs + 3];
  3438. base_ofs += 256 * 4;
  3439. mtx.basis[1].x = texture[base_ofs + 0];
  3440. mtx.basis[1].y = texture[base_ofs + 1];
  3441. mtx.basis[1].z = texture[base_ofs + 2];
  3442. mtx.origin.y = texture[base_ofs + 3];
  3443. base_ofs += 256 * 4;
  3444. mtx.basis[2].x = texture[base_ofs + 0];
  3445. mtx.basis[2].y = texture[base_ofs + 1];
  3446. mtx.basis[2].z = texture[base_ofs + 2];
  3447. mtx.origin.z = texture[base_ofs + 3];
  3448. AABB baabb = mtx.xform(skbones[j]);
  3449. if (first) {
  3450. laabb = baabb;
  3451. first = false;
  3452. } else {
  3453. laabb.merge_with(baabb);
  3454. }
  3455. }
  3456. }
  3457. } else {
  3458. laabb = mesh->surfaces[i]->aabb;
  3459. }
  3460. if (i == 0) {
  3461. aabb = laabb;
  3462. } else {
  3463. aabb.merge_with(laabb);
  3464. }
  3465. }
  3466. } else {
  3467. for (int i = 0; i < mesh->surfaces.size(); i++) {
  3468. if (i == 0) {
  3469. aabb = mesh->surfaces[i]->aabb;
  3470. } else {
  3471. aabb.merge_with(mesh->surfaces[i]->aabb);
  3472. }
  3473. }
  3474. }
  3475. return aabb;
  3476. }
  3477. void RasterizerStorageGLES3::mesh_clear(RID p_mesh) {
  3478. Mesh *mesh = mesh_owner.getornull(p_mesh);
  3479. ERR_FAIL_COND(!mesh);
  3480. while (mesh->surfaces.size()) {
  3481. mesh_remove_surface(p_mesh, 0);
  3482. }
  3483. }
  3484. void RasterizerStorageGLES3::mesh_render_blend_shapes(Surface *s, const float *p_weights) {
  3485. glBindVertexArray(s->array_id);
  3486. BlendShapeShaderGLES3::Conditionals cond[VS::ARRAY_MAX - 1] = {
  3487. BlendShapeShaderGLES3::ENABLE_NORMAL, //will be ignored
  3488. BlendShapeShaderGLES3::ENABLE_NORMAL,
  3489. BlendShapeShaderGLES3::ENABLE_TANGENT,
  3490. BlendShapeShaderGLES3::ENABLE_COLOR,
  3491. BlendShapeShaderGLES3::ENABLE_UV,
  3492. BlendShapeShaderGLES3::ENABLE_UV2,
  3493. BlendShapeShaderGLES3::ENABLE_SKELETON,
  3494. BlendShapeShaderGLES3::ENABLE_SKELETON,
  3495. };
  3496. int stride = 0;
  3497. if (s->format & VS::ARRAY_FLAG_USE_2D_VERTICES) {
  3498. stride = 2 * 4;
  3499. } else {
  3500. stride = 3 * 4;
  3501. }
  3502. static const int sizes[VS::ARRAY_MAX - 1] = {
  3503. 3 * 4,
  3504. 3 * 4,
  3505. 4 * 4,
  3506. 4 * 4,
  3507. 2 * 4,
  3508. 2 * 4,
  3509. 4 * 4,
  3510. 4 * 4
  3511. };
  3512. for (int i = 1; i < VS::ARRAY_MAX - 1; i++) {
  3513. shaders.blend_shapes.set_conditional(cond[i], s->format & (1 << i)); //enable conditional for format
  3514. if (s->format & (1 << i)) {
  3515. stride += sizes[i];
  3516. }
  3517. }
  3518. //copy all first
  3519. float base_weight = 1.0;
  3520. int mtc = s->blend_shapes.size();
  3521. if (s->mesh->blend_shape_mode == VS::BLEND_SHAPE_MODE_NORMALIZED) {
  3522. for (int i = 0; i < mtc; i++) {
  3523. base_weight -= p_weights[i];
  3524. }
  3525. }
  3526. shaders.blend_shapes.set_conditional(BlendShapeShaderGLES3::ENABLE_BLEND, false); //first pass does not blend
  3527. shaders.blend_shapes.set_conditional(BlendShapeShaderGLES3::USE_2D_VERTEX, s->format & VS::ARRAY_FLAG_USE_2D_VERTICES); //use 2D vertices if needed
  3528. shaders.blend_shapes.set_conditional(BlendShapeShaderGLES3::ENABLE_OCTAHEDRAL_COMPRESSION, s->format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION); //use octahedral normal compression
  3529. shaders.blend_shapes.bind();
  3530. shaders.blend_shapes.set_uniform(BlendShapeShaderGLES3::BLEND_AMOUNT, base_weight);
  3531. glEnable(GL_RASTERIZER_DISCARD);
  3532. glBindBuffer(GL_ARRAY_BUFFER, 0);
  3533. glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, resources.transform_feedback_buffers[0]);
  3534. glBeginTransformFeedback(GL_POINTS);
  3535. glDrawArrays(GL_POINTS, 0, s->array_len);
  3536. glEndTransformFeedback();
  3537. shaders.blend_shapes.set_conditional(BlendShapeShaderGLES3::ENABLE_BLEND, true); //first pass does not blend
  3538. shaders.blend_shapes.bind();
  3539. for (int ti = 0; ti < mtc; ti++) {
  3540. float weight = p_weights[ti];
  3541. if (Math::is_zero_approx(weight)) {
  3542. //not bother with this one
  3543. continue;
  3544. }
  3545. glBindVertexArray(s->blend_shapes[ti].array_id);
  3546. glBindBuffer(GL_ARRAY_BUFFER, resources.transform_feedback_buffers[0]);
  3547. glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, resources.transform_feedback_buffers[1]);
  3548. shaders.blend_shapes.set_uniform(BlendShapeShaderGLES3::BLEND_AMOUNT, weight);
  3549. int ofs = 0;
  3550. for (int i = 0; i < VS::ARRAY_MAX - 1; i++) {
  3551. if (s->format & (1 << i)) {
  3552. glEnableVertexAttribArray(i + 8);
  3553. switch (i) {
  3554. case VS::ARRAY_VERTEX: {
  3555. if (s->format & VS::ARRAY_FLAG_USE_2D_VERTICES) {
  3556. glVertexAttribPointer(i + 8, 2, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3557. ofs += 2 * 4;
  3558. } else {
  3559. glVertexAttribPointer(i + 8, 3, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3560. ofs += 3 * 4;
  3561. }
  3562. } break;
  3563. case VS::ARRAY_NORMAL: {
  3564. glVertexAttribPointer(i + 8, 3, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3565. ofs += 3 * 4;
  3566. } break;
  3567. case VS::ARRAY_TANGENT: {
  3568. glVertexAttribPointer(i + 8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3569. ofs += 4 * 4;
  3570. } break;
  3571. case VS::ARRAY_COLOR: {
  3572. glVertexAttribPointer(i + 8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3573. ofs += 4 * 4;
  3574. } break;
  3575. case VS::ARRAY_TEX_UV: {
  3576. glVertexAttribPointer(i + 8, 2, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3577. ofs += 2 * 4;
  3578. } break;
  3579. case VS::ARRAY_TEX_UV2: {
  3580. glVertexAttribPointer(i + 8, 2, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3581. ofs += 2 * 4;
  3582. } break;
  3583. case VS::ARRAY_BONES: {
  3584. glVertexAttribIPointer(i + 8, 4, GL_UNSIGNED_INT, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3585. ofs += 4 * 4;
  3586. } break;
  3587. case VS::ARRAY_WEIGHTS: {
  3588. glVertexAttribPointer(i + 8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3589. ofs += 4 * 4;
  3590. } break;
  3591. }
  3592. } else {
  3593. glDisableVertexAttribArray(i + 8);
  3594. }
  3595. }
  3596. glBeginTransformFeedback(GL_POINTS);
  3597. glDrawArrays(GL_POINTS, 0, s->array_len);
  3598. glEndTransformFeedback();
  3599. SWAP(resources.transform_feedback_buffers[0], resources.transform_feedback_buffers[1]);
  3600. }
  3601. glDisable(GL_RASTERIZER_DISCARD);
  3602. glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
  3603. glBindVertexArray(resources.transform_feedback_array);
  3604. glBindBuffer(GL_ARRAY_BUFFER, resources.transform_feedback_buffers[0]);
  3605. int ofs = 0;
  3606. for (int i = 0; i < VS::ARRAY_MAX - 1; i++) {
  3607. if (s->format & (1 << i)) {
  3608. glEnableVertexAttribArray(i);
  3609. switch (i) {
  3610. case VS::ARRAY_VERTEX: {
  3611. if (s->format & VS::ARRAY_FLAG_USE_2D_VERTICES) {
  3612. glVertexAttribPointer(i, 2, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3613. ofs += 2 * 4;
  3614. } else {
  3615. glVertexAttribPointer(i, 3, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3616. ofs += 3 * 4;
  3617. }
  3618. } break;
  3619. case VS::ARRAY_NORMAL: {
  3620. glVertexAttribPointer(i, 3, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3621. ofs += 3 * 4;
  3622. } break;
  3623. case VS::ARRAY_TANGENT: {
  3624. glVertexAttribPointer(i, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3625. ofs += 4 * 4;
  3626. } break;
  3627. case VS::ARRAY_COLOR: {
  3628. glVertexAttribPointer(i, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3629. ofs += 4 * 4;
  3630. } break;
  3631. case VS::ARRAY_TEX_UV: {
  3632. glVertexAttribPointer(i, 2, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3633. ofs += 2 * 4;
  3634. } break;
  3635. case VS::ARRAY_TEX_UV2: {
  3636. glVertexAttribPointer(i, 2, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3637. ofs += 2 * 4;
  3638. } break;
  3639. case VS::ARRAY_BONES: {
  3640. glVertexAttribIPointer(i, 4, GL_UNSIGNED_INT, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3641. ofs += 4 * 4;
  3642. } break;
  3643. case VS::ARRAY_WEIGHTS: {
  3644. glVertexAttribPointer(i, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(ofs));
  3645. ofs += 4 * 4;
  3646. } break;
  3647. }
  3648. } else {
  3649. glDisableVertexAttribArray(i);
  3650. }
  3651. }
  3652. if (s->index_array_len) {
  3653. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->index_id);
  3654. }
  3655. }
  3656. /* MULTIMESH API */
  3657. RID RasterizerStorageGLES3::_multimesh_create() {
  3658. MultiMesh *multimesh = memnew(MultiMesh);
  3659. return multimesh_owner.make_rid(multimesh);
  3660. }
  3661. void RasterizerStorageGLES3::_multimesh_allocate(RID p_multimesh, int p_instances, VS::MultimeshTransformFormat p_transform_format, VS::MultimeshColorFormat p_color_format, VS::MultimeshCustomDataFormat p_data_format) {
  3662. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3663. ERR_FAIL_COND(!multimesh);
  3664. if (multimesh->size == p_instances && multimesh->transform_format == p_transform_format && multimesh->color_format == p_color_format && multimesh->custom_data_format == p_data_format) {
  3665. return;
  3666. }
  3667. if (multimesh->buffer) {
  3668. glDeleteBuffers(1, &multimesh->buffer);
  3669. multimesh->data.resize(0);
  3670. multimesh->buffer = 0;
  3671. }
  3672. multimesh->size = p_instances;
  3673. multimesh->transform_format = p_transform_format;
  3674. multimesh->color_format = p_color_format;
  3675. multimesh->custom_data_format = p_data_format;
  3676. if (multimesh->size) {
  3677. if (multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D) {
  3678. multimesh->xform_floats = 8;
  3679. } else {
  3680. multimesh->xform_floats = 12;
  3681. }
  3682. if (multimesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
  3683. multimesh->color_floats = 1;
  3684. } else if (multimesh->color_format == VS::MULTIMESH_COLOR_FLOAT) {
  3685. multimesh->color_floats = 4;
  3686. } else {
  3687. multimesh->color_floats = 0;
  3688. }
  3689. if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
  3690. multimesh->custom_data_floats = 1;
  3691. } else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_FLOAT) {
  3692. multimesh->custom_data_floats = 4;
  3693. } else {
  3694. multimesh->custom_data_floats = 0;
  3695. }
  3696. int format_floats = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  3697. multimesh->data.resize(format_floats * p_instances);
  3698. float *dataptr = multimesh->data.ptrw();
  3699. for (int i = 0; i < p_instances * format_floats; i += format_floats) {
  3700. int color_from = 0;
  3701. int custom_data_from = 0;
  3702. if (multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D) {
  3703. dataptr[i + 0] = 1.0;
  3704. dataptr[i + 1] = 0.0;
  3705. dataptr[i + 2] = 0.0;
  3706. dataptr[i + 3] = 0.0;
  3707. dataptr[i + 4] = 0.0;
  3708. dataptr[i + 5] = 1.0;
  3709. dataptr[i + 6] = 0.0;
  3710. dataptr[i + 7] = 0.0;
  3711. color_from = 8;
  3712. custom_data_from = 8;
  3713. } else {
  3714. dataptr[i + 0] = 1.0;
  3715. dataptr[i + 1] = 0.0;
  3716. dataptr[i + 2] = 0.0;
  3717. dataptr[i + 3] = 0.0;
  3718. dataptr[i + 4] = 0.0;
  3719. dataptr[i + 5] = 1.0;
  3720. dataptr[i + 6] = 0.0;
  3721. dataptr[i + 7] = 0.0;
  3722. dataptr[i + 8] = 0.0;
  3723. dataptr[i + 9] = 0.0;
  3724. dataptr[i + 10] = 1.0;
  3725. dataptr[i + 11] = 0.0;
  3726. color_from = 12;
  3727. custom_data_from = 12;
  3728. }
  3729. if (multimesh->color_format == VS::MULTIMESH_COLOR_NONE) {
  3730. //none
  3731. } else if (multimesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
  3732. union {
  3733. uint32_t colu;
  3734. float colf;
  3735. } cu;
  3736. cu.colu = 0xFFFFFFFF;
  3737. dataptr[i + color_from + 0] = cu.colf;
  3738. custom_data_from = color_from + 1;
  3739. } else if (multimesh->color_format == VS::MULTIMESH_COLOR_FLOAT) {
  3740. dataptr[i + color_from + 0] = 1.0;
  3741. dataptr[i + color_from + 1] = 1.0;
  3742. dataptr[i + color_from + 2] = 1.0;
  3743. dataptr[i + color_from + 3] = 1.0;
  3744. custom_data_from = color_from + 4;
  3745. }
  3746. if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_NONE) {
  3747. //none
  3748. } else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
  3749. union {
  3750. uint32_t colu;
  3751. float colf;
  3752. } cu;
  3753. cu.colu = 0;
  3754. dataptr[i + custom_data_from + 0] = cu.colf;
  3755. } else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_FLOAT) {
  3756. dataptr[i + custom_data_from + 0] = 0.0;
  3757. dataptr[i + custom_data_from + 1] = 0.0;
  3758. dataptr[i + custom_data_from + 2] = 0.0;
  3759. dataptr[i + custom_data_from + 3] = 0.0;
  3760. }
  3761. }
  3762. glGenBuffers(1, &multimesh->buffer);
  3763. glBindBuffer(GL_ARRAY_BUFFER, multimesh->buffer);
  3764. glBufferData(GL_ARRAY_BUFFER, multimesh->data.size() * sizeof(float), nullptr, GL_STATIC_DRAW);
  3765. glBindBuffer(GL_ARRAY_BUFFER, 0);
  3766. }
  3767. multimesh->dirty_data = true;
  3768. multimesh->dirty_aabb = true;
  3769. if (!multimesh->update_list.in_list()) {
  3770. multimesh_update_list.add(&multimesh->update_list);
  3771. }
  3772. }
  3773. int RasterizerStorageGLES3::_multimesh_get_instance_count(RID p_multimesh) const {
  3774. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3775. ERR_FAIL_COND_V(!multimesh, 0);
  3776. return multimesh->size;
  3777. }
  3778. void RasterizerStorageGLES3::_multimesh_set_mesh(RID p_multimesh, RID p_mesh) {
  3779. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3780. ERR_FAIL_COND(!multimesh);
  3781. if (multimesh->mesh.is_valid()) {
  3782. Mesh *mesh = mesh_owner.getornull(multimesh->mesh);
  3783. if (mesh) {
  3784. mesh->multimeshes.remove(&multimesh->mesh_list);
  3785. }
  3786. }
  3787. multimesh->mesh = p_mesh;
  3788. if (multimesh->mesh.is_valid()) {
  3789. Mesh *mesh = mesh_owner.getornull(multimesh->mesh);
  3790. if (mesh) {
  3791. mesh->multimeshes.add(&multimesh->mesh_list);
  3792. }
  3793. }
  3794. multimesh->dirty_aabb = true;
  3795. if (!multimesh->update_list.in_list()) {
  3796. multimesh_update_list.add(&multimesh->update_list);
  3797. }
  3798. }
  3799. void RasterizerStorageGLES3::_multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform) {
  3800. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3801. ERR_FAIL_COND(!multimesh);
  3802. ERR_FAIL_INDEX(p_index, multimesh->size);
  3803. ERR_FAIL_COND(multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D);
  3804. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  3805. float *dataptr = &multimesh->data.write[stride * p_index];
  3806. dataptr[0] = p_transform.basis.elements[0][0];
  3807. dataptr[1] = p_transform.basis.elements[0][1];
  3808. dataptr[2] = p_transform.basis.elements[0][2];
  3809. dataptr[3] = p_transform.origin.x;
  3810. dataptr[4] = p_transform.basis.elements[1][0];
  3811. dataptr[5] = p_transform.basis.elements[1][1];
  3812. dataptr[6] = p_transform.basis.elements[1][2];
  3813. dataptr[7] = p_transform.origin.y;
  3814. dataptr[8] = p_transform.basis.elements[2][0];
  3815. dataptr[9] = p_transform.basis.elements[2][1];
  3816. dataptr[10] = p_transform.basis.elements[2][2];
  3817. dataptr[11] = p_transform.origin.z;
  3818. multimesh->dirty_data = true;
  3819. multimesh->dirty_aabb = true;
  3820. if (!multimesh->update_list.in_list()) {
  3821. multimesh_update_list.add(&multimesh->update_list);
  3822. }
  3823. }
  3824. void RasterizerStorageGLES3::_multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) {
  3825. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3826. ERR_FAIL_COND(!multimesh);
  3827. ERR_FAIL_INDEX(p_index, multimesh->size);
  3828. ERR_FAIL_COND(multimesh->transform_format == VS::MULTIMESH_TRANSFORM_3D);
  3829. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  3830. float *dataptr = &multimesh->data.write[stride * p_index];
  3831. dataptr[0] = p_transform.elements[0][0];
  3832. dataptr[1] = p_transform.elements[1][0];
  3833. dataptr[2] = 0;
  3834. dataptr[3] = p_transform.elements[2][0];
  3835. dataptr[4] = p_transform.elements[0][1];
  3836. dataptr[5] = p_transform.elements[1][1];
  3837. dataptr[6] = 0;
  3838. dataptr[7] = p_transform.elements[2][1];
  3839. multimesh->dirty_data = true;
  3840. multimesh->dirty_aabb = true;
  3841. if (!multimesh->update_list.in_list()) {
  3842. multimesh_update_list.add(&multimesh->update_list);
  3843. }
  3844. }
  3845. void RasterizerStorageGLES3::_multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) {
  3846. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3847. ERR_FAIL_COND(!multimesh);
  3848. ERR_FAIL_INDEX(p_index, multimesh->size);
  3849. ERR_FAIL_COND(multimesh->color_format == VS::MULTIMESH_COLOR_NONE);
  3850. ERR_FAIL_INDEX(multimesh->color_format, VS::MULTIMESH_COLOR_MAX);
  3851. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  3852. float *dataptr = &multimesh->data.write[stride * p_index + multimesh->xform_floats];
  3853. if (multimesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
  3854. uint8_t *data8 = (uint8_t *)dataptr;
  3855. data8[0] = CLAMP(p_color.r * 255.0, 0, 255);
  3856. data8[1] = CLAMP(p_color.g * 255.0, 0, 255);
  3857. data8[2] = CLAMP(p_color.b * 255.0, 0, 255);
  3858. data8[3] = CLAMP(p_color.a * 255.0, 0, 255);
  3859. } else if (multimesh->color_format == VS::MULTIMESH_COLOR_FLOAT) {
  3860. dataptr[0] = p_color.r;
  3861. dataptr[1] = p_color.g;
  3862. dataptr[2] = p_color.b;
  3863. dataptr[3] = p_color.a;
  3864. }
  3865. multimesh->dirty_data = true;
  3866. multimesh->dirty_aabb = true;
  3867. if (!multimesh->update_list.in_list()) {
  3868. multimesh_update_list.add(&multimesh->update_list);
  3869. }
  3870. }
  3871. void RasterizerStorageGLES3::_multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_custom_data) {
  3872. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3873. ERR_FAIL_COND(!multimesh);
  3874. ERR_FAIL_INDEX(p_index, multimesh->size);
  3875. ERR_FAIL_COND(multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_NONE);
  3876. ERR_FAIL_INDEX(multimesh->custom_data_format, VS::MULTIMESH_CUSTOM_DATA_MAX);
  3877. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  3878. float *dataptr = &multimesh->data.write[stride * p_index + multimesh->xform_floats + multimesh->color_floats];
  3879. if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
  3880. uint8_t *data8 = (uint8_t *)dataptr;
  3881. data8[0] = CLAMP(p_custom_data.r * 255.0, 0, 255);
  3882. data8[1] = CLAMP(p_custom_data.g * 255.0, 0, 255);
  3883. data8[2] = CLAMP(p_custom_data.b * 255.0, 0, 255);
  3884. data8[3] = CLAMP(p_custom_data.a * 255.0, 0, 255);
  3885. } else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_FLOAT) {
  3886. dataptr[0] = p_custom_data.r;
  3887. dataptr[1] = p_custom_data.g;
  3888. dataptr[2] = p_custom_data.b;
  3889. dataptr[3] = p_custom_data.a;
  3890. }
  3891. multimesh->dirty_data = true;
  3892. multimesh->dirty_aabb = true;
  3893. if (!multimesh->update_list.in_list()) {
  3894. multimesh_update_list.add(&multimesh->update_list);
  3895. }
  3896. }
  3897. RID RasterizerStorageGLES3::_multimesh_get_mesh(RID p_multimesh) const {
  3898. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3899. ERR_FAIL_COND_V(!multimesh, RID());
  3900. return multimesh->mesh;
  3901. }
  3902. Transform RasterizerStorageGLES3::_multimesh_instance_get_transform(RID p_multimesh, int p_index) const {
  3903. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3904. ERR_FAIL_COND_V(!multimesh, Transform());
  3905. ERR_FAIL_INDEX_V(p_index, multimesh->size, Transform());
  3906. ERR_FAIL_COND_V(multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D, Transform());
  3907. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  3908. float *dataptr = &multimesh->data.write[stride * p_index];
  3909. Transform xform;
  3910. xform.basis.elements[0][0] = dataptr[0];
  3911. xform.basis.elements[0][1] = dataptr[1];
  3912. xform.basis.elements[0][2] = dataptr[2];
  3913. xform.origin.x = dataptr[3];
  3914. xform.basis.elements[1][0] = dataptr[4];
  3915. xform.basis.elements[1][1] = dataptr[5];
  3916. xform.basis.elements[1][2] = dataptr[6];
  3917. xform.origin.y = dataptr[7];
  3918. xform.basis.elements[2][0] = dataptr[8];
  3919. xform.basis.elements[2][1] = dataptr[9];
  3920. xform.basis.elements[2][2] = dataptr[10];
  3921. xform.origin.z = dataptr[11];
  3922. return xform;
  3923. }
  3924. Transform2D RasterizerStorageGLES3::_multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const {
  3925. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3926. ERR_FAIL_COND_V(!multimesh, Transform2D());
  3927. ERR_FAIL_INDEX_V(p_index, multimesh->size, Transform2D());
  3928. ERR_FAIL_COND_V(multimesh->transform_format == VS::MULTIMESH_TRANSFORM_3D, Transform2D());
  3929. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  3930. float *dataptr = &multimesh->data.write[stride * p_index];
  3931. Transform2D xform;
  3932. xform.elements[0][0] = dataptr[0];
  3933. xform.elements[1][0] = dataptr[1];
  3934. xform.elements[2][0] = dataptr[3];
  3935. xform.elements[0][1] = dataptr[4];
  3936. xform.elements[1][1] = dataptr[5];
  3937. xform.elements[2][1] = dataptr[7];
  3938. return xform;
  3939. }
  3940. Color RasterizerStorageGLES3::_multimesh_instance_get_color(RID p_multimesh, int p_index) const {
  3941. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3942. ERR_FAIL_COND_V(!multimesh, Color());
  3943. ERR_FAIL_INDEX_V(p_index, multimesh->size, Color());
  3944. ERR_FAIL_COND_V(multimesh->color_format == VS::MULTIMESH_COLOR_NONE, Color());
  3945. ERR_FAIL_INDEX_V(multimesh->color_format, VS::MULTIMESH_COLOR_MAX, Color());
  3946. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  3947. float *dataptr = &multimesh->data.write[stride * p_index + multimesh->xform_floats];
  3948. if (multimesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
  3949. union {
  3950. uint32_t colu;
  3951. float colf;
  3952. } cu;
  3953. cu.colf = dataptr[0];
  3954. return Color::hex(BSWAP32(cu.colu));
  3955. } else if (multimesh->color_format == VS::MULTIMESH_COLOR_FLOAT) {
  3956. Color c;
  3957. c.r = dataptr[0];
  3958. c.g = dataptr[1];
  3959. c.b = dataptr[2];
  3960. c.a = dataptr[3];
  3961. return c;
  3962. }
  3963. return Color();
  3964. }
  3965. Color RasterizerStorageGLES3::_multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const {
  3966. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3967. ERR_FAIL_COND_V(!multimesh, Color());
  3968. ERR_FAIL_INDEX_V(p_index, multimesh->size, Color());
  3969. ERR_FAIL_COND_V(multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_NONE, Color());
  3970. ERR_FAIL_INDEX_V(multimesh->custom_data_format, VS::MULTIMESH_CUSTOM_DATA_MAX, Color());
  3971. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  3972. float *dataptr = &multimesh->data.write[stride * p_index + multimesh->xform_floats + multimesh->color_floats];
  3973. if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
  3974. union {
  3975. uint32_t colu;
  3976. float colf;
  3977. } cu;
  3978. cu.colf = dataptr[0];
  3979. return Color::hex(BSWAP32(cu.colu));
  3980. } else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_FLOAT) {
  3981. Color c;
  3982. c.r = dataptr[0];
  3983. c.g = dataptr[1];
  3984. c.b = dataptr[2];
  3985. c.a = dataptr[3];
  3986. return c;
  3987. }
  3988. return Color();
  3989. }
  3990. void RasterizerStorageGLES3::_multimesh_set_as_bulk_array(RID p_multimesh, const PoolVector<float> &p_array) {
  3991. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3992. ERR_FAIL_COND(!multimesh);
  3993. ERR_FAIL_COND(!multimesh->data.ptr());
  3994. int dsize = multimesh->data.size();
  3995. ERR_FAIL_COND(dsize != p_array.size());
  3996. PoolVector<float>::Read r = p_array.read();
  3997. memcpy(multimesh->data.ptrw(), r.ptr(), dsize * sizeof(float));
  3998. multimesh->dirty_data = true;
  3999. multimesh->dirty_aabb = true;
  4000. if (!multimesh->update_list.in_list()) {
  4001. multimesh_update_list.add(&multimesh->update_list);
  4002. }
  4003. }
  4004. void RasterizerStorageGLES3::_multimesh_set_visible_instances(RID p_multimesh, int p_visible) {
  4005. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  4006. ERR_FAIL_COND(!multimesh);
  4007. multimesh->visible_instances = p_visible;
  4008. }
  4009. int RasterizerStorageGLES3::_multimesh_get_visible_instances(RID p_multimesh) const {
  4010. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  4011. ERR_FAIL_COND_V(!multimesh, -1);
  4012. return multimesh->visible_instances;
  4013. }
  4014. AABB RasterizerStorageGLES3::_multimesh_get_aabb(RID p_multimesh) const {
  4015. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  4016. ERR_FAIL_COND_V(!multimesh, AABB());
  4017. const_cast<RasterizerStorageGLES3 *>(this)->update_dirty_multimeshes(); //update pending AABBs
  4018. return multimesh->aabb;
  4019. }
  4020. RasterizerStorage::MMInterpolator *RasterizerStorageGLES3::_multimesh_get_interpolator(RID p_multimesh) const {
  4021. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  4022. ERR_FAIL_COND_V_MSG(!multimesh, nullptr, "Multimesh not found: " + itos(p_multimesh.get_id()));
  4023. return &multimesh->interpolator;
  4024. }
  4025. void RasterizerStorageGLES3::multimesh_attach_canvas_item(RID p_multimesh, RID p_canvas_item, bool p_attach) {
  4026. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  4027. ERR_FAIL_NULL(multimesh);
  4028. ERR_FAIL_COND(!p_canvas_item.is_valid());
  4029. if (p_attach) {
  4030. int64_t found = multimesh->linked_canvas_items.find(p_canvas_item);
  4031. if (found == -1) {
  4032. multimesh->linked_canvas_items.push_back(p_canvas_item);
  4033. }
  4034. } else {
  4035. int64_t found = multimesh->linked_canvas_items.find(p_canvas_item);
  4036. if (found != -1) {
  4037. multimesh->linked_canvas_items.remove_unordered(found);
  4038. }
  4039. }
  4040. }
  4041. void RasterizerStorageGLES3::update_dirty_multimeshes() {
  4042. while (multimesh_update_list.first()) {
  4043. MultiMesh *multimesh = multimesh_update_list.first()->self();
  4044. if (multimesh->size && multimesh->dirty_data) {
  4045. glBindBuffer(GL_ARRAY_BUFFER, multimesh->buffer);
  4046. uint32_t buffer_size = multimesh->data.size() * sizeof(float);
  4047. // this could potentially have a project setting for API options as with 2d
  4048. // if (config.should_orphan) {
  4049. glBufferData(GL_ARRAY_BUFFER, buffer_size, multimesh->data.ptr(), GL_DYNAMIC_DRAW);
  4050. // } else {
  4051. // glBufferSubData(GL_ARRAY_BUFFER, 0, buffer_size, multimesh->data.ptr());
  4052. // }
  4053. glBindBuffer(GL_ARRAY_BUFFER, 0);
  4054. }
  4055. if (multimesh->size && multimesh->dirty_aabb) {
  4056. AABB mesh_aabb;
  4057. if (multimesh->mesh.is_valid()) {
  4058. mesh_aabb = mesh_get_aabb(multimesh->mesh, RID());
  4059. } else {
  4060. mesh_aabb.size += Vector3(0.001, 0.001, 0.001);
  4061. }
  4062. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  4063. int count = multimesh->data.size();
  4064. float *data = multimesh->data.ptrw();
  4065. AABB aabb;
  4066. if (multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D) {
  4067. for (int i = 0; i < count; i += stride) {
  4068. float *dataptr = &data[i];
  4069. Transform xform;
  4070. xform.basis[0][0] = dataptr[0];
  4071. xform.basis[0][1] = dataptr[1];
  4072. xform.origin[0] = dataptr[3];
  4073. xform.basis[1][0] = dataptr[4];
  4074. xform.basis[1][1] = dataptr[5];
  4075. xform.origin[1] = dataptr[7];
  4076. AABB laabb = xform.xform(mesh_aabb);
  4077. if (i == 0) {
  4078. aabb = laabb;
  4079. } else {
  4080. aabb.merge_with(laabb);
  4081. }
  4082. }
  4083. } else {
  4084. for (int i = 0; i < count; i += stride) {
  4085. float *dataptr = &data[i];
  4086. Transform xform;
  4087. xform.basis.elements[0][0] = dataptr[0];
  4088. xform.basis.elements[0][1] = dataptr[1];
  4089. xform.basis.elements[0][2] = dataptr[2];
  4090. xform.origin.x = dataptr[3];
  4091. xform.basis.elements[1][0] = dataptr[4];
  4092. xform.basis.elements[1][1] = dataptr[5];
  4093. xform.basis.elements[1][2] = dataptr[6];
  4094. xform.origin.y = dataptr[7];
  4095. xform.basis.elements[2][0] = dataptr[8];
  4096. xform.basis.elements[2][1] = dataptr[9];
  4097. xform.basis.elements[2][2] = dataptr[10];
  4098. xform.origin.z = dataptr[11];
  4099. AABB laabb = xform.xform(mesh_aabb);
  4100. if (i == 0) {
  4101. aabb = laabb;
  4102. } else {
  4103. aabb.merge_with(laabb);
  4104. }
  4105. }
  4106. }
  4107. multimesh->aabb = aabb;
  4108. // Inform any linked canvas items that bounds have changed
  4109. // (for hierarchical culling).
  4110. int num_linked = multimesh->linked_canvas_items.size();
  4111. for (int n = 0; n < num_linked; n++) {
  4112. const RID &rid = multimesh->linked_canvas_items[n];
  4113. VSG::canvas->_canvas_item_invalidate_local_bound(rid);
  4114. }
  4115. }
  4116. multimesh->dirty_aabb = false;
  4117. multimesh->dirty_data = false;
  4118. multimesh->instance_change_notify(true, false);
  4119. multimesh_update_list.remove(multimesh_update_list.first());
  4120. }
  4121. }
  4122. /* IMMEDIATE API */
  4123. RID RasterizerStorageGLES3::immediate_create() {
  4124. Immediate *im = memnew(Immediate);
  4125. return immediate_owner.make_rid(im);
  4126. }
  4127. void RasterizerStorageGLES3::immediate_begin(RID p_immediate, VS::PrimitiveType p_primitive, RID p_texture) {
  4128. ERR_FAIL_INDEX(p_primitive, (int)VS::PRIMITIVE_MAX);
  4129. Immediate *im = immediate_owner.get(p_immediate);
  4130. ERR_FAIL_COND(!im);
  4131. ERR_FAIL_COND(im->building);
  4132. Immediate::Chunk ic;
  4133. ic.texture = p_texture;
  4134. ic.primitive = p_primitive;
  4135. im->chunks.push_back(ic);
  4136. im->mask = 0;
  4137. im->building = true;
  4138. }
  4139. void RasterizerStorageGLES3::immediate_vertex(RID p_immediate, const Vector3 &p_vertex) {
  4140. Immediate *im = immediate_owner.get(p_immediate);
  4141. ERR_FAIL_COND(!im);
  4142. ERR_FAIL_COND(!im->building);
  4143. Immediate::Chunk *c = &im->chunks.back()->get();
  4144. if (c->vertices.empty() && im->chunks.size() == 1) {
  4145. im->aabb.position = p_vertex;
  4146. im->aabb.size = Vector3();
  4147. } else {
  4148. im->aabb.expand_to(p_vertex);
  4149. }
  4150. if (im->mask & VS::ARRAY_FORMAT_NORMAL) {
  4151. c->normals.push_back(chunk_normal);
  4152. }
  4153. if (im->mask & VS::ARRAY_FORMAT_TANGENT) {
  4154. c->tangents.push_back(chunk_tangent);
  4155. }
  4156. if (im->mask & VS::ARRAY_FORMAT_COLOR) {
  4157. c->colors.push_back(chunk_color);
  4158. }
  4159. if (im->mask & VS::ARRAY_FORMAT_TEX_UV) {
  4160. c->uvs.push_back(chunk_uv);
  4161. }
  4162. if (im->mask & VS::ARRAY_FORMAT_TEX_UV2) {
  4163. c->uvs2.push_back(chunk_uv2);
  4164. }
  4165. im->mask |= VS::ARRAY_FORMAT_VERTEX;
  4166. c->vertices.push_back(p_vertex);
  4167. }
  4168. void RasterizerStorageGLES3::immediate_normal(RID p_immediate, const Vector3 &p_normal) {
  4169. Immediate *im = immediate_owner.get(p_immediate);
  4170. ERR_FAIL_COND(!im);
  4171. ERR_FAIL_COND(!im->building);
  4172. im->mask |= VS::ARRAY_FORMAT_NORMAL;
  4173. chunk_normal = p_normal;
  4174. }
  4175. void RasterizerStorageGLES3::immediate_tangent(RID p_immediate, const Plane &p_tangent) {
  4176. Immediate *im = immediate_owner.get(p_immediate);
  4177. ERR_FAIL_COND(!im);
  4178. ERR_FAIL_COND(!im->building);
  4179. im->mask |= VS::ARRAY_FORMAT_TANGENT;
  4180. chunk_tangent = p_tangent;
  4181. }
  4182. void RasterizerStorageGLES3::immediate_color(RID p_immediate, const Color &p_color) {
  4183. Immediate *im = immediate_owner.get(p_immediate);
  4184. ERR_FAIL_COND(!im);
  4185. ERR_FAIL_COND(!im->building);
  4186. im->mask |= VS::ARRAY_FORMAT_COLOR;
  4187. chunk_color = p_color;
  4188. }
  4189. void RasterizerStorageGLES3::immediate_uv(RID p_immediate, const Vector2 &tex_uv) {
  4190. Immediate *im = immediate_owner.get(p_immediate);
  4191. ERR_FAIL_COND(!im);
  4192. ERR_FAIL_COND(!im->building);
  4193. im->mask |= VS::ARRAY_FORMAT_TEX_UV;
  4194. chunk_uv = tex_uv;
  4195. }
  4196. void RasterizerStorageGLES3::immediate_uv2(RID p_immediate, const Vector2 &tex_uv) {
  4197. Immediate *im = immediate_owner.get(p_immediate);
  4198. ERR_FAIL_COND(!im);
  4199. ERR_FAIL_COND(!im->building);
  4200. im->mask |= VS::ARRAY_FORMAT_TEX_UV2;
  4201. chunk_uv2 = tex_uv;
  4202. }
  4203. void RasterizerStorageGLES3::immediate_end(RID p_immediate) {
  4204. Immediate *im = immediate_owner.get(p_immediate);
  4205. ERR_FAIL_COND(!im);
  4206. ERR_FAIL_COND(!im->building);
  4207. im->building = false;
  4208. im->instance_change_notify(true, false);
  4209. }
  4210. void RasterizerStorageGLES3::immediate_clear(RID p_immediate) {
  4211. Immediate *im = immediate_owner.get(p_immediate);
  4212. ERR_FAIL_COND(!im);
  4213. ERR_FAIL_COND(im->building);
  4214. im->chunks.clear();
  4215. im->instance_change_notify(true, false);
  4216. }
  4217. AABB RasterizerStorageGLES3::immediate_get_aabb(RID p_immediate) const {
  4218. Immediate *im = immediate_owner.get(p_immediate);
  4219. ERR_FAIL_COND_V(!im, AABB());
  4220. return im->aabb;
  4221. }
  4222. void RasterizerStorageGLES3::immediate_set_material(RID p_immediate, RID p_material) {
  4223. Immediate *im = immediate_owner.get(p_immediate);
  4224. ERR_FAIL_COND(!im);
  4225. im->material = p_material;
  4226. im->instance_change_notify(false, true);
  4227. }
  4228. RID RasterizerStorageGLES3::immediate_get_material(RID p_immediate) const {
  4229. const Immediate *im = immediate_owner.get(p_immediate);
  4230. ERR_FAIL_COND_V(!im, RID());
  4231. return im->material;
  4232. }
  4233. /* SKELETON API */
  4234. RID RasterizerStorageGLES3::skeleton_create() {
  4235. Skeleton *skeleton = memnew(Skeleton);
  4236. glGenTextures(1, &skeleton->texture);
  4237. return skeleton_owner.make_rid(skeleton);
  4238. }
  4239. void RasterizerStorageGLES3::skeleton_allocate(RID p_skeleton, int p_bones, bool p_2d_skeleton) {
  4240. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4241. ERR_FAIL_COND(!skeleton);
  4242. ERR_FAIL_COND(p_bones < 0);
  4243. if (skeleton->size == p_bones && skeleton->use_2d == p_2d_skeleton) {
  4244. return;
  4245. }
  4246. skeleton->size = p_bones;
  4247. skeleton->use_2d = p_2d_skeleton;
  4248. int height = p_bones / 256;
  4249. if (p_bones % 256) {
  4250. height++;
  4251. }
  4252. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  4253. glBindTexture(GL_TEXTURE_2D, skeleton->texture);
  4254. if (skeleton->use_2d) {
  4255. skeleton->skel_texture.resize(256 * height * 2 * 4);
  4256. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 256, height * 2, 0, GL_RGBA, GL_FLOAT, nullptr);
  4257. } else {
  4258. skeleton->skel_texture.resize(256 * height * 3 * 4);
  4259. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 256, height * 3, 0, GL_RGBA, GL_FLOAT, nullptr);
  4260. }
  4261. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  4262. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  4263. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  4264. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  4265. if (!skeleton->update_list.in_list()) {
  4266. skeleton_update_list.add(&skeleton->update_list);
  4267. }
  4268. }
  4269. int RasterizerStorageGLES3::skeleton_get_bone_count(RID p_skeleton) const {
  4270. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4271. ERR_FAIL_COND_V(!skeleton, 0);
  4272. return skeleton->size;
  4273. }
  4274. void RasterizerStorageGLES3::skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform &p_transform) {
  4275. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4276. ERR_FAIL_COND(!skeleton);
  4277. ERR_FAIL_INDEX(p_bone, skeleton->size);
  4278. ERR_FAIL_COND(skeleton->use_2d);
  4279. float *texture = skeleton->skel_texture.ptrw();
  4280. int base_ofs = ((p_bone / 256) * 256) * 3 * 4 + (p_bone % 256) * 4;
  4281. texture[base_ofs + 0] = p_transform.basis[0].x;
  4282. texture[base_ofs + 1] = p_transform.basis[0].y;
  4283. texture[base_ofs + 2] = p_transform.basis[0].z;
  4284. texture[base_ofs + 3] = p_transform.origin.x;
  4285. base_ofs += 256 * 4;
  4286. texture[base_ofs + 0] = p_transform.basis[1].x;
  4287. texture[base_ofs + 1] = p_transform.basis[1].y;
  4288. texture[base_ofs + 2] = p_transform.basis[1].z;
  4289. texture[base_ofs + 3] = p_transform.origin.y;
  4290. base_ofs += 256 * 4;
  4291. texture[base_ofs + 0] = p_transform.basis[2].x;
  4292. texture[base_ofs + 1] = p_transform.basis[2].y;
  4293. texture[base_ofs + 2] = p_transform.basis[2].z;
  4294. texture[base_ofs + 3] = p_transform.origin.z;
  4295. if (!skeleton->update_list.in_list()) {
  4296. skeleton_update_list.add(&skeleton->update_list);
  4297. }
  4298. }
  4299. Transform RasterizerStorageGLES3::skeleton_bone_get_transform(RID p_skeleton, int p_bone) const {
  4300. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4301. ERR_FAIL_COND_V(!skeleton, Transform());
  4302. ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform());
  4303. ERR_FAIL_COND_V(skeleton->use_2d, Transform());
  4304. const float *texture = skeleton->skel_texture.ptr();
  4305. Transform ret;
  4306. int base_ofs = ((p_bone / 256) * 256) * 3 * 4 + (p_bone % 256) * 4;
  4307. ret.basis[0].x = texture[base_ofs + 0];
  4308. ret.basis[0].y = texture[base_ofs + 1];
  4309. ret.basis[0].z = texture[base_ofs + 2];
  4310. ret.origin.x = texture[base_ofs + 3];
  4311. base_ofs += 256 * 4;
  4312. ret.basis[1].x = texture[base_ofs + 0];
  4313. ret.basis[1].y = texture[base_ofs + 1];
  4314. ret.basis[1].z = texture[base_ofs + 2];
  4315. ret.origin.y = texture[base_ofs + 3];
  4316. base_ofs += 256 * 4;
  4317. ret.basis[2].x = texture[base_ofs + 0];
  4318. ret.basis[2].y = texture[base_ofs + 1];
  4319. ret.basis[2].z = texture[base_ofs + 2];
  4320. ret.origin.z = texture[base_ofs + 3];
  4321. return ret;
  4322. }
  4323. void RasterizerStorageGLES3::skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) {
  4324. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4325. ERR_FAIL_COND(!skeleton);
  4326. ERR_FAIL_INDEX(p_bone, skeleton->size);
  4327. ERR_FAIL_COND(!skeleton->use_2d);
  4328. float *texture = skeleton->skel_texture.ptrw();
  4329. int base_ofs = ((p_bone / 256) * 256) * 2 * 4 + (p_bone % 256) * 4;
  4330. texture[base_ofs + 0] = p_transform[0][0];
  4331. texture[base_ofs + 1] = p_transform[1][0];
  4332. texture[base_ofs + 2] = 0;
  4333. texture[base_ofs + 3] = p_transform[2][0];
  4334. base_ofs += 256 * 4;
  4335. texture[base_ofs + 0] = p_transform[0][1];
  4336. texture[base_ofs + 1] = p_transform[1][1];
  4337. texture[base_ofs + 2] = 0;
  4338. texture[base_ofs + 3] = p_transform[2][1];
  4339. if (!skeleton->update_list.in_list()) {
  4340. skeleton_update_list.add(&skeleton->update_list);
  4341. }
  4342. skeleton->revision++;
  4343. }
  4344. Transform2D RasterizerStorageGLES3::skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const {
  4345. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4346. ERR_FAIL_COND_V(!skeleton, Transform2D());
  4347. ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform2D());
  4348. ERR_FAIL_COND_V(!skeleton->use_2d, Transform2D());
  4349. const float *texture = skeleton->skel_texture.ptr();
  4350. Transform2D ret;
  4351. int base_ofs = ((p_bone / 256) * 256) * 2 * 4 + (p_bone % 256) * 4;
  4352. ret[0][0] = texture[base_ofs + 0];
  4353. ret[1][0] = texture[base_ofs + 1];
  4354. ret[2][0] = texture[base_ofs + 3];
  4355. base_ofs += 256 * 4;
  4356. ret[0][1] = texture[base_ofs + 0];
  4357. ret[1][1] = texture[base_ofs + 1];
  4358. ret[2][1] = texture[base_ofs + 3];
  4359. return ret;
  4360. }
  4361. void RasterizerStorageGLES3::skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) {
  4362. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4363. ERR_FAIL_COND(!skeleton->use_2d);
  4364. skeleton->base_transform_2d = p_base_transform;
  4365. }
  4366. void RasterizerStorageGLES3::skeleton_attach_canvas_item(RID p_skeleton, RID p_canvas_item, bool p_attach) {
  4367. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4368. ERR_FAIL_NULL(skeleton);
  4369. ERR_FAIL_COND(!p_canvas_item.is_valid());
  4370. if (p_attach) {
  4371. #ifdef DEV_ENABLED
  4372. // skeleton_attach_canvas_item() is not bound,
  4373. // and checks in canvas_item_attach_skeleton() should prevent this,
  4374. // but there isn't much harm in a DEV_ENABLED check here.
  4375. int64_t found = skeleton->linked_canvas_items.find(p_canvas_item);
  4376. ERR_FAIL_COND(found != -1);
  4377. #endif
  4378. skeleton->linked_canvas_items.push_back(p_canvas_item);
  4379. } else {
  4380. int64_t found = skeleton->linked_canvas_items.find(p_canvas_item);
  4381. ERR_FAIL_COND(found == -1);
  4382. skeleton->linked_canvas_items.remove_unordered(found);
  4383. }
  4384. }
  4385. uint32_t RasterizerStorageGLES3::skeleton_get_revision(RID p_skeleton) const {
  4386. const Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4387. ERR_FAIL_COND_V(!skeleton, 0);
  4388. return skeleton->revision;
  4389. }
  4390. void RasterizerStorageGLES3::update_dirty_skeletons() {
  4391. // 2D Skeletons always need to update the polygons so they
  4392. // know the bounds have changed.
  4393. // TODO : Could we have a separate list for 2D only?
  4394. SelfList<Skeleton> *ele = skeleton_update_list.first();
  4395. while (ele) {
  4396. Skeleton *skeleton = ele->self();
  4397. int num_linked = skeleton->linked_canvas_items.size();
  4398. for (int n = 0; n < num_linked; n++) {
  4399. const RID &rid = skeleton->linked_canvas_items[n];
  4400. VSG::canvas->_canvas_item_invalidate_local_bound(rid);
  4401. }
  4402. ele = ele->next();
  4403. }
  4404. // TODO : Is this update necessary for 2D software skinning?
  4405. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  4406. while (skeleton_update_list.first()) {
  4407. Skeleton *skeleton = skeleton_update_list.first()->self();
  4408. if (skeleton->size) {
  4409. int height = skeleton->size / 256;
  4410. if (skeleton->size % 256) {
  4411. height++;
  4412. }
  4413. glBindTexture(GL_TEXTURE_2D, skeleton->texture);
  4414. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 256, height * (skeleton->use_2d ? 2 : 3), GL_RGBA, GL_FLOAT, skeleton->skel_texture.ptr());
  4415. }
  4416. for (Set<RasterizerScene::InstanceBase *>::Element *E = skeleton->instances.front(); E; E = E->next()) {
  4417. E->get()->base_changed(true, false);
  4418. }
  4419. skeleton_update_list.remove(skeleton_update_list.first());
  4420. }
  4421. }
  4422. /* Light API */
  4423. RID RasterizerStorageGLES3::light_create(VS::LightType p_type) {
  4424. Light *light = memnew(Light);
  4425. light->type = p_type;
  4426. light->param[VS::LIGHT_PARAM_ENERGY] = 1.0;
  4427. light->param[VS::LIGHT_PARAM_INDIRECT_ENERGY] = 1.0;
  4428. light->param[VS::LIGHT_PARAM_SIZE] = 0.0;
  4429. light->param[VS::LIGHT_PARAM_SPECULAR] = 0.5;
  4430. light->param[VS::LIGHT_PARAM_RANGE] = 1.0;
  4431. light->param[VS::LIGHT_PARAM_SPOT_ANGLE] = 45;
  4432. light->param[VS::LIGHT_PARAM_CONTACT_SHADOW_SIZE] = 45;
  4433. light->param[VS::LIGHT_PARAM_SHADOW_MAX_DISTANCE] = 0;
  4434. light->param[VS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET] = 0.1;
  4435. light->param[VS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET] = 0.3;
  4436. light->param[VS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET] = 0.6;
  4437. light->param[VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] = 0.1;
  4438. light->param[VS::LIGHT_PARAM_SHADOW_BIAS_SPLIT_SCALE] = 0.1;
  4439. light->param[VS::LIGHT_PARAM_SHADOW_FADE_START] = 0.8;
  4440. light->color = Color(1, 1, 1, 1);
  4441. light->shadow = false;
  4442. light->negative = false;
  4443. light->cull_mask = 0xFFFFFFFF;
  4444. light->directional_shadow_mode = VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL;
  4445. light->omni_shadow_mode = VS::LIGHT_OMNI_SHADOW_DUAL_PARABOLOID;
  4446. light->omni_shadow_detail = VS::LIGHT_OMNI_SHADOW_DETAIL_VERTICAL;
  4447. light->directional_blend_splits = false;
  4448. light->directional_range_mode = VS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE;
  4449. light->reverse_cull = false;
  4450. light->bake_mode = VS::LIGHT_BAKE_INDIRECT;
  4451. light->version = 0;
  4452. return light_owner.make_rid(light);
  4453. }
  4454. void RasterizerStorageGLES3::light_set_color(RID p_light, const Color &p_color) {
  4455. Light *light = light_owner.getornull(p_light);
  4456. ERR_FAIL_COND(!light);
  4457. light->color = p_color;
  4458. }
  4459. void RasterizerStorageGLES3::light_set_param(RID p_light, VS::LightParam p_param, float p_value) {
  4460. Light *light = light_owner.getornull(p_light);
  4461. ERR_FAIL_COND(!light);
  4462. ERR_FAIL_INDEX(p_param, VS::LIGHT_PARAM_MAX);
  4463. switch (p_param) {
  4464. case VS::LIGHT_PARAM_RANGE:
  4465. case VS::LIGHT_PARAM_SPOT_ANGLE:
  4466. case VS::LIGHT_PARAM_SHADOW_MAX_DISTANCE:
  4467. case VS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET:
  4468. case VS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET:
  4469. case VS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET:
  4470. case VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS:
  4471. case VS::LIGHT_PARAM_SHADOW_BIAS:
  4472. case VS::LIGHT_PARAM_SHADOW_FADE_START: {
  4473. light->version++;
  4474. light->instance_change_notify(true, false);
  4475. } break;
  4476. default: {
  4477. }
  4478. }
  4479. light->param[p_param] = p_value;
  4480. }
  4481. void RasterizerStorageGLES3::light_set_shadow(RID p_light, bool p_enabled) {
  4482. Light *light = light_owner.getornull(p_light);
  4483. ERR_FAIL_COND(!light);
  4484. light->shadow = p_enabled;
  4485. light->version++;
  4486. light->instance_change_notify(true, false);
  4487. }
  4488. void RasterizerStorageGLES3::light_set_shadow_color(RID p_light, const Color &p_color) {
  4489. Light *light = light_owner.getornull(p_light);
  4490. ERR_FAIL_COND(!light);
  4491. light->shadow_color = p_color;
  4492. }
  4493. void RasterizerStorageGLES3::light_set_projector(RID p_light, RID p_texture) {
  4494. Light *light = light_owner.getornull(p_light);
  4495. ERR_FAIL_COND(!light);
  4496. light->projector = p_texture;
  4497. }
  4498. void RasterizerStorageGLES3::light_set_negative(RID p_light, bool p_enable) {
  4499. Light *light = light_owner.getornull(p_light);
  4500. ERR_FAIL_COND(!light);
  4501. light->negative = p_enable;
  4502. }
  4503. void RasterizerStorageGLES3::light_set_cull_mask(RID p_light, uint32_t p_mask) {
  4504. Light *light = light_owner.getornull(p_light);
  4505. ERR_FAIL_COND(!light);
  4506. light->cull_mask = p_mask;
  4507. light->version++;
  4508. light->instance_change_notify(true, false);
  4509. }
  4510. void RasterizerStorageGLES3::light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) {
  4511. Light *light = light_owner.getornull(p_light);
  4512. ERR_FAIL_COND(!light);
  4513. light->reverse_cull = p_enabled;
  4514. light->version++;
  4515. light->instance_change_notify(true, false);
  4516. }
  4517. void RasterizerStorageGLES3::light_set_use_gi(RID p_light, bool p_enabled) {
  4518. WARN_DEPRECATED_MSG("'VisualServer.light_set_use_gi' is deprecated and will be removed in a future version. Use 'VisualServer.light_set_bake_mode' instead.");
  4519. light_set_bake_mode(p_light, p_enabled ? VS::LightBakeMode::LIGHT_BAKE_INDIRECT : VS::LightBakeMode::LIGHT_BAKE_DISABLED);
  4520. }
  4521. void RasterizerStorageGLES3::light_set_bake_mode(RID p_light, VS::LightBakeMode p_bake_mode) {
  4522. Light *light = light_owner.getornull(p_light);
  4523. ERR_FAIL_COND(!light);
  4524. light->bake_mode = p_bake_mode;
  4525. light->version++;
  4526. light->instance_change_notify(true, false);
  4527. }
  4528. void RasterizerStorageGLES3::light_omni_set_shadow_mode(RID p_light, VS::LightOmniShadowMode p_mode) {
  4529. Light *light = light_owner.getornull(p_light);
  4530. ERR_FAIL_COND(!light);
  4531. light->omni_shadow_mode = p_mode;
  4532. light->version++;
  4533. light->instance_change_notify(true, false);
  4534. }
  4535. VS::LightOmniShadowMode RasterizerStorageGLES3::light_omni_get_shadow_mode(RID p_light) {
  4536. const Light *light = light_owner.getornull(p_light);
  4537. ERR_FAIL_COND_V(!light, VS::LIGHT_OMNI_SHADOW_CUBE);
  4538. return light->omni_shadow_mode;
  4539. }
  4540. void RasterizerStorageGLES3::light_omni_set_shadow_detail(RID p_light, VS::LightOmniShadowDetail p_detail) {
  4541. Light *light = light_owner.getornull(p_light);
  4542. ERR_FAIL_COND(!light);
  4543. light->omni_shadow_detail = p_detail;
  4544. light->version++;
  4545. light->instance_change_notify(true, false);
  4546. }
  4547. void RasterizerStorageGLES3::light_directional_set_shadow_mode(RID p_light, VS::LightDirectionalShadowMode p_mode) {
  4548. Light *light = light_owner.getornull(p_light);
  4549. ERR_FAIL_COND(!light);
  4550. light->directional_shadow_mode = p_mode;
  4551. light->version++;
  4552. light->instance_change_notify(true, false);
  4553. }
  4554. void RasterizerStorageGLES3::light_directional_set_blend_splits(RID p_light, bool p_enable) {
  4555. Light *light = light_owner.getornull(p_light);
  4556. ERR_FAIL_COND(!light);
  4557. light->directional_blend_splits = p_enable;
  4558. light->version++;
  4559. light->instance_change_notify(true, false);
  4560. }
  4561. bool RasterizerStorageGLES3::light_directional_get_blend_splits(RID p_light) const {
  4562. const Light *light = light_owner.getornull(p_light);
  4563. ERR_FAIL_COND_V(!light, false);
  4564. return light->directional_blend_splits;
  4565. }
  4566. VS::LightDirectionalShadowMode RasterizerStorageGLES3::light_directional_get_shadow_mode(RID p_light) {
  4567. const Light *light = light_owner.getornull(p_light);
  4568. ERR_FAIL_COND_V(!light, VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL);
  4569. return light->directional_shadow_mode;
  4570. }
  4571. void RasterizerStorageGLES3::light_directional_set_shadow_depth_range_mode(RID p_light, VS::LightDirectionalShadowDepthRangeMode p_range_mode) {
  4572. Light *light = light_owner.getornull(p_light);
  4573. ERR_FAIL_COND(!light);
  4574. light->directional_range_mode = p_range_mode;
  4575. }
  4576. VS::LightDirectionalShadowDepthRangeMode RasterizerStorageGLES3::light_directional_get_shadow_depth_range_mode(RID p_light) const {
  4577. const Light *light = light_owner.getornull(p_light);
  4578. ERR_FAIL_COND_V(!light, VS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE);
  4579. return light->directional_range_mode;
  4580. }
  4581. VS::LightType RasterizerStorageGLES3::light_get_type(RID p_light) const {
  4582. const Light *light = light_owner.getornull(p_light);
  4583. ERR_FAIL_COND_V(!light, VS::LIGHT_DIRECTIONAL);
  4584. return light->type;
  4585. }
  4586. float RasterizerStorageGLES3::light_get_param(RID p_light, VS::LightParam p_param) {
  4587. const Light *light = light_owner.getornull(p_light);
  4588. ERR_FAIL_COND_V(!light, VS::LIGHT_DIRECTIONAL);
  4589. return light->param[p_param];
  4590. }
  4591. Color RasterizerStorageGLES3::light_get_color(RID p_light) {
  4592. const Light *light = light_owner.getornull(p_light);
  4593. ERR_FAIL_COND_V(!light, Color());
  4594. return light->color;
  4595. }
  4596. bool RasterizerStorageGLES3::light_get_use_gi(RID p_light) {
  4597. return light_get_bake_mode(p_light) != VS::LightBakeMode::LIGHT_BAKE_DISABLED;
  4598. }
  4599. VS::LightBakeMode RasterizerStorageGLES3::light_get_bake_mode(RID p_light) {
  4600. Light *light = light_owner.getornull(p_light);
  4601. ERR_FAIL_COND_V(!light, VS::LightBakeMode::LIGHT_BAKE_DISABLED);
  4602. return light->bake_mode;
  4603. }
  4604. bool RasterizerStorageGLES3::light_has_shadow(RID p_light) const {
  4605. const Light *light = light_owner.getornull(p_light);
  4606. ERR_FAIL_COND_V(!light, VS::LIGHT_DIRECTIONAL);
  4607. return light->shadow;
  4608. }
  4609. uint64_t RasterizerStorageGLES3::light_get_version(RID p_light) const {
  4610. const Light *light = light_owner.getornull(p_light);
  4611. ERR_FAIL_COND_V(!light, 0);
  4612. return light->version;
  4613. }
  4614. AABB RasterizerStorageGLES3::light_get_aabb(RID p_light) const {
  4615. const Light *light = light_owner.getornull(p_light);
  4616. ERR_FAIL_COND_V(!light, AABB());
  4617. switch (light->type) {
  4618. case VS::LIGHT_SPOT: {
  4619. float len = light->param[VS::LIGHT_PARAM_RANGE];
  4620. float size = Math::tan(Math::deg2rad(light->param[VS::LIGHT_PARAM_SPOT_ANGLE])) * len;
  4621. return AABB(Vector3(-size, -size, -len), Vector3(size * 2, size * 2, len));
  4622. };
  4623. case VS::LIGHT_OMNI: {
  4624. float r = light->param[VS::LIGHT_PARAM_RANGE];
  4625. return AABB(-Vector3(r, r, r), Vector3(r, r, r) * 2);
  4626. };
  4627. case VS::LIGHT_DIRECTIONAL: {
  4628. return AABB();
  4629. };
  4630. }
  4631. ERR_FAIL_V(AABB());
  4632. }
  4633. /* PROBE API */
  4634. RID RasterizerStorageGLES3::reflection_probe_create() {
  4635. ReflectionProbe *reflection_probe = memnew(ReflectionProbe);
  4636. reflection_probe->intensity = 1.0;
  4637. reflection_probe->interior_ambient = Color();
  4638. reflection_probe->interior_ambient_energy = 1.0;
  4639. reflection_probe->interior_ambient_probe_contrib = 0.0;
  4640. reflection_probe->max_distance = 0;
  4641. reflection_probe->extents = Vector3(1, 1, 1);
  4642. reflection_probe->origin_offset = Vector3(0, 0, 0);
  4643. reflection_probe->interior = false;
  4644. reflection_probe->box_projection = false;
  4645. reflection_probe->enable_shadows = false;
  4646. reflection_probe->cull_mask = (1 << 20) - 1;
  4647. reflection_probe->update_mode = VS::REFLECTION_PROBE_UPDATE_ONCE;
  4648. return reflection_probe_owner.make_rid(reflection_probe);
  4649. }
  4650. void RasterizerStorageGLES3::reflection_probe_set_update_mode(RID p_probe, VS::ReflectionProbeUpdateMode p_mode) {
  4651. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4652. ERR_FAIL_COND(!reflection_probe);
  4653. reflection_probe->update_mode = p_mode;
  4654. reflection_probe->instance_change_notify(true, false);
  4655. }
  4656. void RasterizerStorageGLES3::reflection_probe_set_intensity(RID p_probe, float p_intensity) {
  4657. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4658. ERR_FAIL_COND(!reflection_probe);
  4659. reflection_probe->intensity = p_intensity;
  4660. }
  4661. void RasterizerStorageGLES3::reflection_probe_set_interior_ambient(RID p_probe, const Color &p_ambient) {
  4662. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4663. ERR_FAIL_COND(!reflection_probe);
  4664. reflection_probe->interior_ambient = p_ambient;
  4665. }
  4666. void RasterizerStorageGLES3::reflection_probe_set_interior_ambient_energy(RID p_probe, float p_energy) {
  4667. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4668. ERR_FAIL_COND(!reflection_probe);
  4669. reflection_probe->interior_ambient_energy = p_energy;
  4670. }
  4671. void RasterizerStorageGLES3::reflection_probe_set_interior_ambient_probe_contribution(RID p_probe, float p_contrib) {
  4672. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4673. ERR_FAIL_COND(!reflection_probe);
  4674. reflection_probe->interior_ambient_probe_contrib = p_contrib;
  4675. }
  4676. void RasterizerStorageGLES3::reflection_probe_set_max_distance(RID p_probe, float p_distance) {
  4677. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4678. ERR_FAIL_COND(!reflection_probe);
  4679. reflection_probe->max_distance = p_distance;
  4680. reflection_probe->instance_change_notify(true, false);
  4681. }
  4682. void RasterizerStorageGLES3::reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents) {
  4683. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4684. ERR_FAIL_COND(!reflection_probe);
  4685. reflection_probe->extents = p_extents;
  4686. reflection_probe->instance_change_notify(true, false);
  4687. }
  4688. void RasterizerStorageGLES3::reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) {
  4689. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4690. ERR_FAIL_COND(!reflection_probe);
  4691. reflection_probe->origin_offset = p_offset;
  4692. reflection_probe->instance_change_notify(true, false);
  4693. }
  4694. void RasterizerStorageGLES3::reflection_probe_set_as_interior(RID p_probe, bool p_enable) {
  4695. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4696. ERR_FAIL_COND(!reflection_probe);
  4697. reflection_probe->interior = p_enable;
  4698. reflection_probe->instance_change_notify(true, false);
  4699. }
  4700. void RasterizerStorageGLES3::reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) {
  4701. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4702. ERR_FAIL_COND(!reflection_probe);
  4703. reflection_probe->box_projection = p_enable;
  4704. }
  4705. void RasterizerStorageGLES3::reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) {
  4706. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4707. ERR_FAIL_COND(!reflection_probe);
  4708. reflection_probe->enable_shadows = p_enable;
  4709. reflection_probe->instance_change_notify(true, false);
  4710. }
  4711. void RasterizerStorageGLES3::reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) {
  4712. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4713. ERR_FAIL_COND(!reflection_probe);
  4714. reflection_probe->cull_mask = p_layers;
  4715. reflection_probe->instance_change_notify(true, false);
  4716. }
  4717. void RasterizerStorageGLES3::reflection_probe_set_resolution(RID p_probe, int p_resolution) {
  4718. }
  4719. AABB RasterizerStorageGLES3::reflection_probe_get_aabb(RID p_probe) const {
  4720. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4721. ERR_FAIL_COND_V(!reflection_probe, AABB());
  4722. AABB aabb;
  4723. aabb.position = -reflection_probe->extents;
  4724. aabb.size = reflection_probe->extents * 2.0;
  4725. return aabb;
  4726. }
  4727. VS::ReflectionProbeUpdateMode RasterizerStorageGLES3::reflection_probe_get_update_mode(RID p_probe) const {
  4728. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4729. ERR_FAIL_COND_V(!reflection_probe, VS::REFLECTION_PROBE_UPDATE_ALWAYS);
  4730. return reflection_probe->update_mode;
  4731. }
  4732. uint32_t RasterizerStorageGLES3::reflection_probe_get_cull_mask(RID p_probe) const {
  4733. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4734. ERR_FAIL_COND_V(!reflection_probe, 0);
  4735. return reflection_probe->cull_mask;
  4736. }
  4737. Vector3 RasterizerStorageGLES3::reflection_probe_get_extents(RID p_probe) const {
  4738. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4739. ERR_FAIL_COND_V(!reflection_probe, Vector3());
  4740. return reflection_probe->extents;
  4741. }
  4742. Vector3 RasterizerStorageGLES3::reflection_probe_get_origin_offset(RID p_probe) const {
  4743. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4744. ERR_FAIL_COND_V(!reflection_probe, Vector3());
  4745. return reflection_probe->origin_offset;
  4746. }
  4747. bool RasterizerStorageGLES3::reflection_probe_renders_shadows(RID p_probe) const {
  4748. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4749. ERR_FAIL_COND_V(!reflection_probe, false);
  4750. return reflection_probe->enable_shadows;
  4751. }
  4752. float RasterizerStorageGLES3::reflection_probe_get_origin_max_distance(RID p_probe) const {
  4753. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4754. ERR_FAIL_COND_V(!reflection_probe, 0);
  4755. return reflection_probe->max_distance;
  4756. }
  4757. RID RasterizerStorageGLES3::gi_probe_create() {
  4758. GIProbe *gip = memnew(GIProbe);
  4759. gip->bounds = AABB(Vector3(), Vector3(1, 1, 1));
  4760. gip->dynamic_range = 1.0;
  4761. gip->energy = 1.0;
  4762. gip->propagation = 1.0;
  4763. gip->bias = 0.4;
  4764. gip->normal_bias = 0.4;
  4765. gip->interior = false;
  4766. gip->compress = false;
  4767. gip->version = 1;
  4768. gip->cell_size = 1.0;
  4769. return gi_probe_owner.make_rid(gip);
  4770. }
  4771. void RasterizerStorageGLES3::gi_probe_set_bounds(RID p_probe, const AABB &p_bounds) {
  4772. GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4773. ERR_FAIL_COND(!gip);
  4774. gip->bounds = p_bounds;
  4775. gip->version++;
  4776. gip->instance_change_notify(true, false);
  4777. }
  4778. AABB RasterizerStorageGLES3::gi_probe_get_bounds(RID p_probe) const {
  4779. const GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4780. ERR_FAIL_COND_V(!gip, AABB());
  4781. return gip->bounds;
  4782. }
  4783. void RasterizerStorageGLES3::gi_probe_set_cell_size(RID p_probe, float p_size) {
  4784. GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4785. ERR_FAIL_COND(!gip);
  4786. gip->cell_size = p_size;
  4787. gip->version++;
  4788. gip->instance_change_notify(true, false);
  4789. }
  4790. float RasterizerStorageGLES3::gi_probe_get_cell_size(RID p_probe) const {
  4791. const GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4792. ERR_FAIL_COND_V(!gip, 0);
  4793. return gip->cell_size;
  4794. }
  4795. void RasterizerStorageGLES3::gi_probe_set_to_cell_xform(RID p_probe, const Transform &p_xform) {
  4796. GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4797. ERR_FAIL_COND(!gip);
  4798. gip->to_cell = p_xform;
  4799. }
  4800. Transform RasterizerStorageGLES3::gi_probe_get_to_cell_xform(RID p_probe) const {
  4801. const GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4802. ERR_FAIL_COND_V(!gip, Transform());
  4803. return gip->to_cell;
  4804. }
  4805. void RasterizerStorageGLES3::gi_probe_set_dynamic_data(RID p_probe, const PoolVector<int> &p_data) {
  4806. GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4807. ERR_FAIL_COND(!gip);
  4808. gip->dynamic_data = p_data;
  4809. gip->version++;
  4810. gip->instance_change_notify(true, false);
  4811. }
  4812. PoolVector<int> RasterizerStorageGLES3::gi_probe_get_dynamic_data(RID p_probe) const {
  4813. const GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4814. ERR_FAIL_COND_V(!gip, PoolVector<int>());
  4815. return gip->dynamic_data;
  4816. }
  4817. void RasterizerStorageGLES3::gi_probe_set_dynamic_range(RID p_probe, int p_range) {
  4818. GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4819. ERR_FAIL_COND(!gip);
  4820. gip->dynamic_range = p_range;
  4821. }
  4822. int RasterizerStorageGLES3::gi_probe_get_dynamic_range(RID p_probe) const {
  4823. const GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4824. ERR_FAIL_COND_V(!gip, 0);
  4825. return gip->dynamic_range;
  4826. }
  4827. void RasterizerStorageGLES3::gi_probe_set_energy(RID p_probe, float p_range) {
  4828. GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4829. ERR_FAIL_COND(!gip);
  4830. gip->energy = p_range;
  4831. }
  4832. void RasterizerStorageGLES3::gi_probe_set_bias(RID p_probe, float p_range) {
  4833. GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4834. ERR_FAIL_COND(!gip);
  4835. gip->bias = p_range;
  4836. }
  4837. void RasterizerStorageGLES3::gi_probe_set_normal_bias(RID p_probe, float p_range) {
  4838. GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4839. ERR_FAIL_COND(!gip);
  4840. gip->normal_bias = p_range;
  4841. }
  4842. void RasterizerStorageGLES3::gi_probe_set_propagation(RID p_probe, float p_range) {
  4843. GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4844. ERR_FAIL_COND(!gip);
  4845. gip->propagation = p_range;
  4846. }
  4847. void RasterizerStorageGLES3::gi_probe_set_interior(RID p_probe, bool p_enable) {
  4848. GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4849. ERR_FAIL_COND(!gip);
  4850. gip->interior = p_enable;
  4851. }
  4852. bool RasterizerStorageGLES3::gi_probe_is_interior(RID p_probe) const {
  4853. const GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4854. ERR_FAIL_COND_V(!gip, false);
  4855. return gip->interior;
  4856. }
  4857. void RasterizerStorageGLES3::gi_probe_set_compress(RID p_probe, bool p_enable) {
  4858. if (p_enable) {
  4859. WARN_DEPRECATED_MSG("GIProbe's Compress property has been deprecated due to known bugs and will be removed in Godot 4.0.");
  4860. }
  4861. GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4862. ERR_FAIL_COND(!gip);
  4863. gip->compress = p_enable;
  4864. }
  4865. bool RasterizerStorageGLES3::gi_probe_is_compressed(RID p_probe) const {
  4866. const GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4867. ERR_FAIL_COND_V(!gip, false);
  4868. return gip->compress;
  4869. }
  4870. float RasterizerStorageGLES3::gi_probe_get_energy(RID p_probe) const {
  4871. const GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4872. ERR_FAIL_COND_V(!gip, 0);
  4873. return gip->energy;
  4874. }
  4875. float RasterizerStorageGLES3::gi_probe_get_bias(RID p_probe) const {
  4876. const GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4877. ERR_FAIL_COND_V(!gip, 0);
  4878. return gip->bias;
  4879. }
  4880. float RasterizerStorageGLES3::gi_probe_get_normal_bias(RID p_probe) const {
  4881. const GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4882. ERR_FAIL_COND_V(!gip, 0);
  4883. return gip->normal_bias;
  4884. }
  4885. float RasterizerStorageGLES3::gi_probe_get_propagation(RID p_probe) const {
  4886. const GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4887. ERR_FAIL_COND_V(!gip, 0);
  4888. return gip->propagation;
  4889. }
  4890. uint32_t RasterizerStorageGLES3::gi_probe_get_version(RID p_probe) {
  4891. const GIProbe *gip = gi_probe_owner.getornull(p_probe);
  4892. ERR_FAIL_COND_V(!gip, 0);
  4893. return gip->version;
  4894. }
  4895. RID RasterizerStorageGLES3::gi_probe_dynamic_data_create(int p_width, int p_height, int p_depth, GIProbeCompression p_compression) {
  4896. GIProbeData *gipd = memnew(GIProbeData);
  4897. gipd->width = p_width;
  4898. gipd->height = p_height;
  4899. gipd->depth = p_depth;
  4900. gipd->compression = GI_PROBE_UNCOMPRESSED;
  4901. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  4902. glGenTextures(1, &gipd->tex_id);
  4903. glBindTexture(GL_TEXTURE_3D, gipd->tex_id);
  4904. int level = 0;
  4905. int min_size = 1;
  4906. if (gipd->compression == GI_PROBE_S3TC) {
  4907. min_size = 4;
  4908. }
  4909. while (true) {
  4910. glTexImage3D(GL_TEXTURE_3D, level, GL_RGBA8, p_width, p_height, p_depth, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  4911. if (p_width <= min_size || p_height <= min_size || p_depth <= min_size) {
  4912. break;
  4913. }
  4914. p_width >>= 1;
  4915. p_height >>= 1;
  4916. p_depth >>= 1;
  4917. level++;
  4918. }
  4919. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  4920. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  4921. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  4922. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  4923. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  4924. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_BASE_LEVEL, 0);
  4925. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, level);
  4926. gipd->levels = level + 1;
  4927. return gi_probe_data_owner.make_rid(gipd);
  4928. }
  4929. void RasterizerStorageGLES3::gi_probe_dynamic_data_update(RID p_gi_probe_data, int p_depth_slice, int p_slice_count, int p_mipmap, const void *p_data) {
  4930. GIProbeData *gipd = gi_probe_data_owner.getornull(p_gi_probe_data);
  4931. ERR_FAIL_COND(!gipd);
  4932. /*
  4933. Vector<uint8_t> data;
  4934. data.resize((gipd->width>>p_mipmap)*(gipd->height>>p_mipmap)*(gipd->depth>>p_mipmap)*4);
  4935. for(int i=0;i<(gipd->width>>p_mipmap);i++) {
  4936. for(int j=0;j<(gipd->height>>p_mipmap);j++) {
  4937. for(int k=0;k<(gipd->depth>>p_mipmap);k++) {
  4938. int ofs = (k*(gipd->height>>p_mipmap)*(gipd->width>>p_mipmap)) + j *(gipd->width>>p_mipmap) + i;
  4939. ofs*=4;
  4940. data[ofs+0]=i*0xFF/(gipd->width>>p_mipmap);
  4941. data[ofs+1]=j*0xFF/(gipd->height>>p_mipmap);
  4942. data[ofs+2]=k*0xFF/(gipd->depth>>p_mipmap);
  4943. data[ofs+3]=0xFF;
  4944. }
  4945. }
  4946. }
  4947. */
  4948. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  4949. glBindTexture(GL_TEXTURE_3D, gipd->tex_id);
  4950. glTexSubImage3D(GL_TEXTURE_3D, p_mipmap, 0, 0, p_depth_slice, gipd->width >> p_mipmap, gipd->height >> p_mipmap, p_slice_count, GL_RGBA, GL_UNSIGNED_BYTE, p_data);
  4951. //glTexImage3D(GL_TEXTURE_3D,p_mipmap,GL_RGBA8,gipd->width>>p_mipmap,gipd->height>>p_mipmap,gipd->depth>>p_mipmap,0,GL_RGBA,GL_UNSIGNED_BYTE,p_data);
  4952. //glTexImage3D(GL_TEXTURE_3D,p_mipmap,GL_RGBA8,gipd->width>>p_mipmap,gipd->height>>p_mipmap,gipd->depth>>p_mipmap,0,GL_RGBA,GL_UNSIGNED_BYTE,data.ptr());
  4953. }
  4954. /////////////////////////////
  4955. RID RasterizerStorageGLES3::lightmap_capture_create() {
  4956. LightmapCapture *capture = memnew(LightmapCapture);
  4957. return lightmap_capture_data_owner.make_rid(capture);
  4958. }
  4959. void RasterizerStorageGLES3::lightmap_capture_set_bounds(RID p_capture, const AABB &p_bounds) {
  4960. LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  4961. ERR_FAIL_COND(!capture);
  4962. capture->bounds = p_bounds;
  4963. capture->instance_change_notify(true, false);
  4964. }
  4965. AABB RasterizerStorageGLES3::lightmap_capture_get_bounds(RID p_capture) const {
  4966. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  4967. ERR_FAIL_COND_V(!capture, AABB());
  4968. return capture->bounds;
  4969. }
  4970. void RasterizerStorageGLES3::lightmap_capture_set_octree(RID p_capture, const PoolVector<uint8_t> &p_octree) {
  4971. LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  4972. ERR_FAIL_COND(!capture);
  4973. ERR_FAIL_COND(p_octree.size() == 0 || (p_octree.size() % sizeof(LightmapCaptureOctree)) != 0);
  4974. capture->octree.resize(p_octree.size() / sizeof(LightmapCaptureOctree));
  4975. if (p_octree.size()) {
  4976. PoolVector<LightmapCaptureOctree>::Write w = capture->octree.write();
  4977. PoolVector<uint8_t>::Read r = p_octree.read();
  4978. memcpy(w.ptr(), r.ptr(), p_octree.size());
  4979. }
  4980. capture->instance_change_notify(true, false);
  4981. }
  4982. PoolVector<uint8_t> RasterizerStorageGLES3::lightmap_capture_get_octree(RID p_capture) const {
  4983. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  4984. ERR_FAIL_COND_V(!capture, PoolVector<uint8_t>());
  4985. if (capture->octree.size() == 0) {
  4986. return PoolVector<uint8_t>();
  4987. }
  4988. PoolVector<uint8_t> ret;
  4989. ret.resize(capture->octree.size() * sizeof(LightmapCaptureOctree));
  4990. {
  4991. PoolVector<LightmapCaptureOctree>::Read r = capture->octree.read();
  4992. PoolVector<uint8_t>::Write w = ret.write();
  4993. memcpy(w.ptr(), r.ptr(), ret.size());
  4994. }
  4995. return ret;
  4996. }
  4997. void RasterizerStorageGLES3::lightmap_capture_set_octree_cell_transform(RID p_capture, const Transform &p_xform) {
  4998. LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  4999. ERR_FAIL_COND(!capture);
  5000. capture->cell_xform = p_xform;
  5001. }
  5002. Transform RasterizerStorageGLES3::lightmap_capture_get_octree_cell_transform(RID p_capture) const {
  5003. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  5004. ERR_FAIL_COND_V(!capture, Transform());
  5005. return capture->cell_xform;
  5006. }
  5007. void RasterizerStorageGLES3::lightmap_capture_set_octree_cell_subdiv(RID p_capture, int p_subdiv) {
  5008. LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  5009. ERR_FAIL_COND(!capture);
  5010. capture->cell_subdiv = p_subdiv;
  5011. }
  5012. int RasterizerStorageGLES3::lightmap_capture_get_octree_cell_subdiv(RID p_capture) const {
  5013. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  5014. ERR_FAIL_COND_V(!capture, 0);
  5015. return capture->cell_subdiv;
  5016. }
  5017. void RasterizerStorageGLES3::lightmap_capture_set_energy(RID p_capture, float p_energy) {
  5018. LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  5019. ERR_FAIL_COND(!capture);
  5020. capture->energy = p_energy;
  5021. if (!capture->update_list.in_list()) {
  5022. capture_update_list.add(&capture->update_list);
  5023. }
  5024. }
  5025. float RasterizerStorageGLES3::lightmap_capture_get_energy(RID p_capture) const {
  5026. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  5027. ERR_FAIL_COND_V(!capture, 0);
  5028. return capture->energy;
  5029. }
  5030. void RasterizerStorageGLES3::lightmap_capture_set_interior(RID p_capture, bool p_interior) {
  5031. LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  5032. ERR_FAIL_COND(!capture);
  5033. capture->interior = p_interior;
  5034. if (!capture->update_list.in_list()) {
  5035. capture_update_list.add(&capture->update_list);
  5036. }
  5037. }
  5038. bool RasterizerStorageGLES3::lightmap_capture_is_interior(RID p_capture) const {
  5039. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  5040. ERR_FAIL_COND_V(!capture, false);
  5041. return capture->interior;
  5042. }
  5043. const PoolVector<RasterizerStorage::LightmapCaptureOctree> *RasterizerStorageGLES3::lightmap_capture_get_octree_ptr(RID p_capture) const {
  5044. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  5045. ERR_FAIL_COND_V(!capture, nullptr);
  5046. return &capture->octree;
  5047. }
  5048. void RasterizerStorageGLES3::update_dirty_captures() {
  5049. while (capture_update_list.first()) {
  5050. LightmapCapture *capture = capture_update_list.first()->self();
  5051. capture->instance_change_notify(false, true);
  5052. capture_update_list.remove(capture_update_list.first());
  5053. }
  5054. }
  5055. ///////
  5056. RID RasterizerStorageGLES3::particles_create() {
  5057. Particles *particles = memnew(Particles);
  5058. return particles_owner.make_rid(particles);
  5059. }
  5060. void RasterizerStorageGLES3::particles_set_emitting(RID p_particles, bool p_emitting) {
  5061. Particles *particles = particles_owner.getornull(p_particles);
  5062. ERR_FAIL_COND(!particles);
  5063. particles->emitting = p_emitting;
  5064. }
  5065. bool RasterizerStorageGLES3::particles_get_emitting(RID p_particles) {
  5066. Particles *particles = particles_owner.getornull(p_particles);
  5067. ERR_FAIL_COND_V(!particles, false);
  5068. return particles->emitting;
  5069. }
  5070. void RasterizerStorageGLES3::particles_set_amount(RID p_particles, int p_amount) {
  5071. Particles *particles = particles_owner.getornull(p_particles);
  5072. ERR_FAIL_COND(!particles);
  5073. particles->amount = p_amount;
  5074. int floats = p_amount * 24;
  5075. float *data = memnew_arr(float, floats);
  5076. for (int i = 0; i < floats; i++) {
  5077. data[i] = 0;
  5078. }
  5079. for (int i = 0; i < 2; i++) {
  5080. glBindVertexArray(particles->particle_vaos[i]);
  5081. glBindBuffer(GL_ARRAY_BUFFER, particles->particle_buffers[i]);
  5082. glBufferData(GL_ARRAY_BUFFER, floats * sizeof(float), data, GL_STATIC_DRAW);
  5083. for (int j = 0; j < 6; j++) {
  5084. glEnableVertexAttribArray(j);
  5085. glVertexAttribPointer(j, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 4 * 6, CAST_INT_TO_UCHAR_PTR(j * 16));
  5086. }
  5087. }
  5088. if (particles->histories_enabled) {
  5089. for (int i = 0; i < 2; i++) {
  5090. glBindVertexArray(particles->particle_vao_histories[i]);
  5091. glBindBuffer(GL_ARRAY_BUFFER, particles->particle_buffer_histories[i]);
  5092. glBufferData(GL_ARRAY_BUFFER, floats * sizeof(float), data, GL_DYNAMIC_COPY);
  5093. for (int j = 0; j < 6; j++) {
  5094. glEnableVertexAttribArray(j);
  5095. glVertexAttribPointer(j, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 4 * 6, CAST_INT_TO_UCHAR_PTR(j * 16));
  5096. }
  5097. particles->particle_valid_histories[i] = false;
  5098. }
  5099. }
  5100. glBindVertexArray(0);
  5101. particles->prev_ticks = 0;
  5102. particles->phase = 0;
  5103. particles->prev_phase = 0;
  5104. particles->clear = true;
  5105. memdelete_arr(data);
  5106. }
  5107. void RasterizerStorageGLES3::particles_set_lifetime(RID p_particles, float p_lifetime) {
  5108. Particles *particles = particles_owner.getornull(p_particles);
  5109. ERR_FAIL_COND(!particles);
  5110. particles->lifetime = p_lifetime;
  5111. }
  5112. void RasterizerStorageGLES3::particles_set_one_shot(RID p_particles, bool p_one_shot) {
  5113. Particles *particles = particles_owner.getornull(p_particles);
  5114. ERR_FAIL_COND(!particles);
  5115. particles->one_shot = p_one_shot;
  5116. }
  5117. void RasterizerStorageGLES3::particles_set_pre_process_time(RID p_particles, float p_time) {
  5118. Particles *particles = particles_owner.getornull(p_particles);
  5119. ERR_FAIL_COND(!particles);
  5120. particles->pre_process_time = p_time;
  5121. }
  5122. void RasterizerStorageGLES3::particles_set_explosiveness_ratio(RID p_particles, float p_ratio) {
  5123. Particles *particles = particles_owner.getornull(p_particles);
  5124. ERR_FAIL_COND(!particles);
  5125. particles->explosiveness = p_ratio;
  5126. }
  5127. void RasterizerStorageGLES3::particles_set_randomness_ratio(RID p_particles, float p_ratio) {
  5128. Particles *particles = particles_owner.getornull(p_particles);
  5129. ERR_FAIL_COND(!particles);
  5130. particles->randomness = p_ratio;
  5131. }
  5132. void RasterizerStorageGLES3::_particles_update_histories(Particles *particles) {
  5133. bool needs_histories = particles->draw_order == VS::PARTICLES_DRAW_ORDER_VIEW_DEPTH;
  5134. if (needs_histories == particles->histories_enabled) {
  5135. return;
  5136. }
  5137. particles->histories_enabled = needs_histories;
  5138. int floats = particles->amount * 24;
  5139. if (!needs_histories) {
  5140. glDeleteBuffers(2, particles->particle_buffer_histories);
  5141. glDeleteVertexArrays(2, particles->particle_vao_histories);
  5142. } else {
  5143. glGenBuffers(2, particles->particle_buffer_histories);
  5144. glGenVertexArrays(2, particles->particle_vao_histories);
  5145. for (int i = 0; i < 2; i++) {
  5146. glBindVertexArray(particles->particle_vao_histories[i]);
  5147. glBindBuffer(GL_ARRAY_BUFFER, particles->particle_buffer_histories[i]);
  5148. glBufferData(GL_ARRAY_BUFFER, floats * sizeof(float), nullptr, GL_DYNAMIC_COPY);
  5149. for (int j = 0; j < 6; j++) {
  5150. glEnableVertexAttribArray(j);
  5151. glVertexAttribPointer(j, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 4 * 6, CAST_INT_TO_UCHAR_PTR(j * 16));
  5152. }
  5153. particles->particle_valid_histories[i] = false;
  5154. }
  5155. }
  5156. particles->clear = true;
  5157. }
  5158. void RasterizerStorageGLES3::particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) {
  5159. Particles *particles = particles_owner.getornull(p_particles);
  5160. ERR_FAIL_COND(!particles);
  5161. particles->custom_aabb = p_aabb;
  5162. _particles_update_histories(particles);
  5163. particles->instance_change_notify(true, false);
  5164. }
  5165. void RasterizerStorageGLES3::particles_set_speed_scale(RID p_particles, float p_scale) {
  5166. Particles *particles = particles_owner.getornull(p_particles);
  5167. ERR_FAIL_COND(!particles);
  5168. particles->speed_scale = p_scale;
  5169. }
  5170. void RasterizerStorageGLES3::particles_set_use_local_coordinates(RID p_particles, bool p_enable) {
  5171. Particles *particles = particles_owner.getornull(p_particles);
  5172. ERR_FAIL_COND(!particles);
  5173. particles->use_local_coords = p_enable;
  5174. }
  5175. void RasterizerStorageGLES3::particles_set_fixed_fps(RID p_particles, int p_fps) {
  5176. Particles *particles = particles_owner.getornull(p_particles);
  5177. ERR_FAIL_COND(!particles);
  5178. particles->fixed_fps = p_fps;
  5179. }
  5180. void RasterizerStorageGLES3::particles_set_fractional_delta(RID p_particles, bool p_enable) {
  5181. Particles *particles = particles_owner.getornull(p_particles);
  5182. ERR_FAIL_COND(!particles);
  5183. particles->fractional_delta = p_enable;
  5184. }
  5185. void RasterizerStorageGLES3::particles_set_process_material(RID p_particles, RID p_material) {
  5186. Particles *particles = particles_owner.getornull(p_particles);
  5187. ERR_FAIL_COND(!particles);
  5188. particles->process_material = p_material;
  5189. }
  5190. void RasterizerStorageGLES3::particles_set_draw_order(RID p_particles, VS::ParticlesDrawOrder p_order) {
  5191. Particles *particles = particles_owner.getornull(p_particles);
  5192. ERR_FAIL_COND(!particles);
  5193. particles->draw_order = p_order;
  5194. _particles_update_histories(particles);
  5195. }
  5196. void RasterizerStorageGLES3::particles_set_draw_passes(RID p_particles, int p_passes) {
  5197. Particles *particles = particles_owner.getornull(p_particles);
  5198. ERR_FAIL_COND(!particles);
  5199. particles->draw_passes.resize(p_passes);
  5200. }
  5201. void RasterizerStorageGLES3::particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) {
  5202. Particles *particles = particles_owner.getornull(p_particles);
  5203. ERR_FAIL_COND(!particles);
  5204. ERR_FAIL_INDEX(p_pass, particles->draw_passes.size());
  5205. particles->draw_passes.write[p_pass] = p_mesh;
  5206. }
  5207. void RasterizerStorageGLES3::particles_restart(RID p_particles) {
  5208. Particles *particles = particles_owner.getornull(p_particles);
  5209. ERR_FAIL_COND(!particles);
  5210. particles->restart_request = true;
  5211. }
  5212. void RasterizerStorageGLES3::particles_request_process(RID p_particles) {
  5213. Particles *particles = particles_owner.getornull(p_particles);
  5214. ERR_FAIL_COND(!particles);
  5215. if (!particles->particle_element.in_list()) {
  5216. particle_update_list.add(&particles->particle_element);
  5217. }
  5218. }
  5219. AABB RasterizerStorageGLES3::particles_get_current_aabb(RID p_particles) {
  5220. const Particles *particles = particles_owner.getornull(p_particles);
  5221. ERR_FAIL_COND_V(!particles, AABB());
  5222. const float *data;
  5223. glBindBuffer(GL_ARRAY_BUFFER, particles->particle_buffers[0]);
  5224. #if defined(GLES_OVER_GL) || defined(__EMSCRIPTEN__)
  5225. PoolVector<uint8_t> vector;
  5226. vector.resize(particles->amount * 16 * 6);
  5227. {
  5228. PoolVector<uint8_t>::Write w = vector.write();
  5229. glGetBufferSubData(GL_ARRAY_BUFFER, 0, particles->amount * 16 * 6, w.ptr());
  5230. }
  5231. PoolVector<uint8_t>::Read r = vector.read();
  5232. data = reinterpret_cast<const float *>(r.ptr());
  5233. #else
  5234. data = (float *)glMapBufferRange(GL_ARRAY_BUFFER, 0, particles->amount * 16 * 6, GL_MAP_READ_BIT);
  5235. #endif
  5236. AABB aabb;
  5237. Transform inv = particles->emission_transform.affine_inverse();
  5238. for (int i = 0; i < particles->amount; i++) {
  5239. int ofs = i * 24;
  5240. Vector3 pos = Vector3(data[ofs + 15], data[ofs + 19], data[ofs + 23]);
  5241. if (!particles->use_local_coords) {
  5242. pos = inv.xform(pos);
  5243. }
  5244. if (i == 0) {
  5245. aabb.position = pos;
  5246. } else {
  5247. aabb.expand_to(pos);
  5248. }
  5249. }
  5250. #if defined(GLES_OVER_GL) || defined(__EMSCRIPTEN__)
  5251. r.release();
  5252. vector = PoolVector<uint8_t>();
  5253. #else
  5254. glUnmapBuffer(GL_ARRAY_BUFFER);
  5255. #endif
  5256. glBindBuffer(GL_ARRAY_BUFFER, 0);
  5257. float longest_axis = 0;
  5258. for (int i = 0; i < particles->draw_passes.size(); i++) {
  5259. if (particles->draw_passes[i].is_valid()) {
  5260. AABB maabb = mesh_get_aabb(particles->draw_passes[i], RID());
  5261. longest_axis = MAX(maabb.get_longest_axis_size(), longest_axis);
  5262. }
  5263. }
  5264. aabb.grow_by(longest_axis);
  5265. return aabb;
  5266. }
  5267. AABB RasterizerStorageGLES3::particles_get_aabb(RID p_particles) const {
  5268. const Particles *particles = particles_owner.getornull(p_particles);
  5269. ERR_FAIL_COND_V(!particles, AABB());
  5270. return particles->custom_aabb;
  5271. }
  5272. void RasterizerStorageGLES3::particles_set_emission_transform(RID p_particles, const Transform &p_transform) {
  5273. Particles *particles = particles_owner.getornull(p_particles);
  5274. ERR_FAIL_COND(!particles);
  5275. particles->emission_transform = p_transform;
  5276. }
  5277. int RasterizerStorageGLES3::particles_get_draw_passes(RID p_particles) const {
  5278. const Particles *particles = particles_owner.getornull(p_particles);
  5279. ERR_FAIL_COND_V(!particles, 0);
  5280. return particles->draw_passes.size();
  5281. }
  5282. RID RasterizerStorageGLES3::particles_get_draw_pass_mesh(RID p_particles, int p_pass) const {
  5283. const Particles *particles = particles_owner.getornull(p_particles);
  5284. ERR_FAIL_COND_V(!particles, RID());
  5285. ERR_FAIL_INDEX_V(p_pass, particles->draw_passes.size(), RID());
  5286. return particles->draw_passes[p_pass];
  5287. }
  5288. void RasterizerStorageGLES3::_particles_process(Particles *p_particles, float p_delta) {
  5289. float new_phase = Math::fmod((float)p_particles->phase + (p_delta / p_particles->lifetime) * p_particles->speed_scale, (float)1.0);
  5290. if (p_particles->clear) {
  5291. p_particles->cycle_number = 0;
  5292. p_particles->random_seed = Math::rand();
  5293. } else if (new_phase < p_particles->phase) {
  5294. if (p_particles->one_shot) {
  5295. p_particles->emitting = false;
  5296. shaders.particles.set_uniform(ParticlesShaderGLES3::EMITTING, false);
  5297. }
  5298. p_particles->cycle_number++;
  5299. }
  5300. shaders.particles.set_uniform(ParticlesShaderGLES3::SYSTEM_PHASE, new_phase);
  5301. shaders.particles.set_uniform(ParticlesShaderGLES3::PREV_SYSTEM_PHASE, p_particles->phase);
  5302. p_particles->phase = new_phase;
  5303. shaders.particles.set_uniform(ParticlesShaderGLES3::DELTA, p_delta * p_particles->speed_scale);
  5304. shaders.particles.set_uniform(ParticlesShaderGLES3::CLEAR, p_particles->clear);
  5305. glUniform1ui(shaders.particles.get_uniform_location(ParticlesShaderGLES3::RANDOM_SEED), p_particles->random_seed);
  5306. if (p_particles->use_local_coords) {
  5307. shaders.particles.set_uniform(ParticlesShaderGLES3::EMISSION_TRANSFORM, Transform());
  5308. } else {
  5309. shaders.particles.set_uniform(ParticlesShaderGLES3::EMISSION_TRANSFORM, p_particles->emission_transform);
  5310. }
  5311. glUniform1ui(shaders.particles.get_uniform(ParticlesShaderGLES3::CYCLE), p_particles->cycle_number);
  5312. p_particles->clear = false;
  5313. glBindVertexArray(p_particles->particle_vaos[0]);
  5314. glBindBuffer(GL_ARRAY_BUFFER, 0); // ensure this is unbound per WebGL2 spec
  5315. glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, p_particles->particle_buffers[1]);
  5316. // GLint size = 0;
  5317. // glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
  5318. glBeginTransformFeedback(GL_POINTS);
  5319. glDrawArrays(GL_POINTS, 0, p_particles->amount);
  5320. glEndTransformFeedback();
  5321. SWAP(p_particles->particle_buffers[0], p_particles->particle_buffers[1]);
  5322. SWAP(p_particles->particle_vaos[0], p_particles->particle_vaos[1]);
  5323. glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
  5324. glBindVertexArray(0);
  5325. /* //debug particles :D
  5326. glBindBuffer(GL_ARRAY_BUFFER, p_particles->particle_buffers[0]);
  5327. float *data = (float *)glMapBufferRange(GL_ARRAY_BUFFER, 0, p_particles->amount * 16 * 6, GL_MAP_READ_BIT);
  5328. for (int i = 0; i < p_particles->amount; i++) {
  5329. int ofs = i * 24;
  5330. print_line(itos(i) + ":");
  5331. print_line("\tColor: " + Color(data[ofs + 0], data[ofs + 1], data[ofs + 2], data[ofs + 3]));
  5332. print_line("\tVelocity: " + Vector3(data[ofs + 4], data[ofs + 5], data[ofs + 6]));
  5333. print_line("\tActive: " + itos(data[ofs + 7]));
  5334. print_line("\tCustom: " + Color(data[ofs + 8], data[ofs + 9], data[ofs + 10], data[ofs + 11]));
  5335. print_line("\tXF X: " + Color(data[ofs + 12], data[ofs + 13], data[ofs + 14], data[ofs + 15]));
  5336. print_line("\tXF Y: " + Color(data[ofs + 16], data[ofs + 17], data[ofs + 18], data[ofs + 19]));
  5337. print_line("\tXF Z: " + Color(data[ofs + 20], data[ofs + 21], data[ofs + 22], data[ofs + 23]));
  5338. }
  5339. glUnmapBuffer(GL_ARRAY_BUFFER);
  5340. glBindBuffer(GL_ARRAY_BUFFER, 0);
  5341. //*/
  5342. }
  5343. void RasterizerStorageGLES3::update_particles() {
  5344. glEnable(GL_RASTERIZER_DISCARD);
  5345. while (particle_update_list.first()) {
  5346. //use transform feedback to process particles
  5347. Particles *particles = particle_update_list.first()->self();
  5348. if (particles->restart_request) {
  5349. particles->prev_ticks = 0;
  5350. particles->phase = 0;
  5351. particles->prev_phase = 0;
  5352. particles->clear = true;
  5353. particles->particle_valid_histories[0] = false;
  5354. particles->particle_valid_histories[1] = false;
  5355. particles->restart_request = false;
  5356. }
  5357. if (particles->inactive && !particles->emitting) {
  5358. particle_update_list.remove(particle_update_list.first());
  5359. continue;
  5360. }
  5361. if (particles->emitting) {
  5362. if (particles->inactive) {
  5363. //restart system from scratch
  5364. particles->prev_ticks = 0;
  5365. particles->phase = 0;
  5366. particles->prev_phase = 0;
  5367. particles->clear = true;
  5368. particles->particle_valid_histories[0] = false;
  5369. particles->particle_valid_histories[1] = false;
  5370. }
  5371. particles->inactive = false;
  5372. particles->inactive_time = 0;
  5373. } else {
  5374. particles->inactive_time += particles->speed_scale * frame.delta;
  5375. if (particles->inactive_time > particles->lifetime * 1.2) {
  5376. particles->inactive = true;
  5377. particle_update_list.remove(particle_update_list.first());
  5378. continue;
  5379. }
  5380. }
  5381. Material *material = material_owner.getornull(particles->process_material);
  5382. if (!material || !material->shader || material->shader->mode != VS::SHADER_PARTICLES) {
  5383. shaders.particles.set_custom_shader(0);
  5384. } else {
  5385. shaders.particles.set_custom_shader(material->shader->custom_code_id);
  5386. if (material->ubo_id) {
  5387. glBindBufferBase(GL_UNIFORM_BUFFER, 0, material->ubo_id);
  5388. }
  5389. int tc = material->textures.size();
  5390. RID *textures = material->textures.ptrw();
  5391. ShaderLanguage::ShaderNode::Uniform::Hint *texture_hints = material->shader->texture_hints.ptrw();
  5392. for (int i = 0; i < tc; i++) {
  5393. gl_wrapper.gl_active_texture(GL_TEXTURE0 + i);
  5394. GLenum target;
  5395. GLuint tex;
  5396. RasterizerStorageGLES3::Texture *t = texture_owner.getornull(textures[i]);
  5397. if (!t) {
  5398. //check hints
  5399. target = GL_TEXTURE_2D;
  5400. switch (texture_hints[i]) {
  5401. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO:
  5402. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
  5403. tex = resources.black_tex;
  5404. } break;
  5405. case ShaderLanguage::ShaderNode::Uniform::HINT_TRANSPARENT: {
  5406. tex = resources.transparent_tex;
  5407. } break;
  5408. case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
  5409. tex = resources.aniso_tex;
  5410. } break;
  5411. case ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL: {
  5412. tex = resources.normal_tex;
  5413. } break;
  5414. default: {
  5415. tex = resources.white_tex;
  5416. } break;
  5417. }
  5418. } else {
  5419. t = t->get_ptr(); //resolve for proxies
  5420. target = t->target;
  5421. tex = t->tex_id;
  5422. }
  5423. glBindTexture(target, tex);
  5424. }
  5425. }
  5426. shaders.particles.set_conditional(ParticlesShaderGLES3::USE_FRACTIONAL_DELTA, particles->fractional_delta);
  5427. shaders.particles.bind();
  5428. shaders.particles.set_uniform(ParticlesShaderGLES3::TOTAL_PARTICLES, particles->amount);
  5429. shaders.particles.set_uniform(ParticlesShaderGLES3::TIME, frame.time[0]);
  5430. shaders.particles.set_uniform(ParticlesShaderGLES3::EXPLOSIVENESS, particles->explosiveness);
  5431. shaders.particles.set_uniform(ParticlesShaderGLES3::LIFETIME, particles->lifetime);
  5432. shaders.particles.set_uniform(ParticlesShaderGLES3::ATTRACTOR_COUNT, 0);
  5433. shaders.particles.set_uniform(ParticlesShaderGLES3::EMITTING, particles->emitting);
  5434. shaders.particles.set_uniform(ParticlesShaderGLES3::RANDOMNESS, particles->randomness);
  5435. bool zero_time_scale = Engine::get_singleton()->get_time_scale() <= 0.0;
  5436. if (particles->clear && particles->pre_process_time > 0.0) {
  5437. float frame_time;
  5438. if (particles->fixed_fps > 0) {
  5439. frame_time = 1.0 / particles->fixed_fps;
  5440. } else {
  5441. frame_time = 1.0 / 30.0;
  5442. }
  5443. float todo = particles->pre_process_time;
  5444. while (todo >= 0) {
  5445. _particles_process(particles, frame_time);
  5446. todo -= frame_time;
  5447. }
  5448. }
  5449. if (particles->fixed_fps > 0) {
  5450. float frame_time;
  5451. float decr;
  5452. if (zero_time_scale) {
  5453. frame_time = 0.0;
  5454. decr = 1.0 / particles->fixed_fps;
  5455. } else {
  5456. frame_time = 1.0 / particles->fixed_fps;
  5457. decr = frame_time;
  5458. }
  5459. float delta = frame.delta;
  5460. if (delta > 0.1) { //avoid recursive stalls if fps goes below 10
  5461. delta = 0.1;
  5462. } else if (delta <= 0.0) { //unlikely but..
  5463. delta = 0.001;
  5464. }
  5465. float todo = particles->frame_remainder + delta;
  5466. while (todo >= frame_time) {
  5467. _particles_process(particles, frame_time);
  5468. todo -= decr;
  5469. }
  5470. particles->frame_remainder = todo;
  5471. } else {
  5472. if (zero_time_scale) {
  5473. _particles_process(particles, 0.0);
  5474. } else {
  5475. _particles_process(particles, frame.delta);
  5476. }
  5477. }
  5478. particle_update_list.remove(particle_update_list.first());
  5479. if (particles->histories_enabled) {
  5480. SWAP(particles->particle_buffer_histories[0], particles->particle_buffer_histories[1]);
  5481. SWAP(particles->particle_vao_histories[0], particles->particle_vao_histories[1]);
  5482. SWAP(particles->particle_valid_histories[0], particles->particle_valid_histories[1]);
  5483. //copy
  5484. glBindBuffer(GL_COPY_READ_BUFFER, particles->particle_buffers[0]);
  5485. glBindBuffer(GL_COPY_WRITE_BUFFER, particles->particle_buffer_histories[0]);
  5486. glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, particles->amount * 24 * sizeof(float));
  5487. particles->particle_valid_histories[0] = true;
  5488. }
  5489. particles->instance_change_notify(true, false); //make sure shadows are updated
  5490. }
  5491. glDisable(GL_RASTERIZER_DISCARD);
  5492. }
  5493. bool RasterizerStorageGLES3::particles_is_inactive(RID p_particles) const {
  5494. const Particles *particles = particles_owner.getornull(p_particles);
  5495. ERR_FAIL_COND_V(!particles, false);
  5496. return !particles->emitting && particles->inactive;
  5497. }
  5498. ////////
  5499. void RasterizerStorageGLES3::instance_add_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance) {
  5500. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  5501. ERR_FAIL_COND(!skeleton);
  5502. skeleton->instances.insert(p_instance);
  5503. }
  5504. void RasterizerStorageGLES3::instance_remove_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance) {
  5505. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  5506. ERR_FAIL_COND(!skeleton);
  5507. skeleton->instances.erase(p_instance);
  5508. }
  5509. void RasterizerStorageGLES3::instance_add_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) {
  5510. Instantiable *inst = nullptr;
  5511. switch (p_instance->base_type) {
  5512. case VS::INSTANCE_MESH: {
  5513. inst = mesh_owner.getornull(p_base);
  5514. ERR_FAIL_COND(!inst);
  5515. } break;
  5516. case VS::INSTANCE_MULTIMESH: {
  5517. inst = multimesh_owner.getornull(p_base);
  5518. ERR_FAIL_COND(!inst);
  5519. } break;
  5520. case VS::INSTANCE_IMMEDIATE: {
  5521. inst = immediate_owner.getornull(p_base);
  5522. ERR_FAIL_COND(!inst);
  5523. } break;
  5524. case VS::INSTANCE_PARTICLES: {
  5525. inst = particles_owner.getornull(p_base);
  5526. ERR_FAIL_COND(!inst);
  5527. } break;
  5528. case VS::INSTANCE_REFLECTION_PROBE: {
  5529. inst = reflection_probe_owner.getornull(p_base);
  5530. ERR_FAIL_COND(!inst);
  5531. } break;
  5532. case VS::INSTANCE_LIGHT: {
  5533. inst = light_owner.getornull(p_base);
  5534. ERR_FAIL_COND(!inst);
  5535. } break;
  5536. case VS::INSTANCE_GI_PROBE: {
  5537. inst = gi_probe_owner.getornull(p_base);
  5538. ERR_FAIL_COND(!inst);
  5539. } break;
  5540. case VS::INSTANCE_LIGHTMAP_CAPTURE: {
  5541. inst = lightmap_capture_data_owner.getornull(p_base);
  5542. ERR_FAIL_COND(!inst);
  5543. } break;
  5544. default: {
  5545. ERR_FAIL();
  5546. }
  5547. }
  5548. inst->instance_list.add(&p_instance->dependency_item);
  5549. }
  5550. void RasterizerStorageGLES3::instance_remove_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) {
  5551. Instantiable *inst = nullptr;
  5552. switch (p_instance->base_type) {
  5553. case VS::INSTANCE_MESH: {
  5554. inst = mesh_owner.getornull(p_base);
  5555. ERR_FAIL_COND(!inst);
  5556. } break;
  5557. case VS::INSTANCE_MULTIMESH: {
  5558. inst = multimesh_owner.getornull(p_base);
  5559. ERR_FAIL_COND(!inst);
  5560. } break;
  5561. case VS::INSTANCE_IMMEDIATE: {
  5562. inst = immediate_owner.getornull(p_base);
  5563. ERR_FAIL_COND(!inst);
  5564. } break;
  5565. case VS::INSTANCE_PARTICLES: {
  5566. inst = particles_owner.getornull(p_base);
  5567. ERR_FAIL_COND(!inst);
  5568. } break;
  5569. case VS::INSTANCE_REFLECTION_PROBE: {
  5570. inst = reflection_probe_owner.getornull(p_base);
  5571. ERR_FAIL_COND(!inst);
  5572. } break;
  5573. case VS::INSTANCE_LIGHT: {
  5574. inst = light_owner.getornull(p_base);
  5575. ERR_FAIL_COND(!inst);
  5576. } break;
  5577. case VS::INSTANCE_GI_PROBE: {
  5578. inst = gi_probe_owner.getornull(p_base);
  5579. ERR_FAIL_COND(!inst);
  5580. } break;
  5581. case VS::INSTANCE_LIGHTMAP_CAPTURE: {
  5582. inst = lightmap_capture_data_owner.getornull(p_base);
  5583. ERR_FAIL_COND(!inst);
  5584. } break;
  5585. default: {
  5586. ERR_FAIL();
  5587. }
  5588. }
  5589. inst->instance_list.remove(&p_instance->dependency_item);
  5590. }
  5591. /* RENDER TARGET */
  5592. void RasterizerStorageGLES3::_render_target_clear(RenderTarget *rt) {
  5593. if (rt->fbo) {
  5594. glDeleteFramebuffers(1, &rt->fbo);
  5595. glDeleteTextures(1, &rt->color);
  5596. rt->fbo = 0;
  5597. }
  5598. if (rt->buffers.active) {
  5599. glDeleteFramebuffers(1, &rt->buffers.fbo);
  5600. glDeleteRenderbuffers(1, &rt->buffers.depth);
  5601. glDeleteRenderbuffers(1, &rt->buffers.diffuse);
  5602. if (rt->buffers.effects_active) {
  5603. glDeleteRenderbuffers(1, &rt->buffers.specular);
  5604. glDeleteRenderbuffers(1, &rt->buffers.normal_rough);
  5605. glDeleteRenderbuffers(1, &rt->buffers.sss);
  5606. glDeleteFramebuffers(1, &rt->buffers.effect_fbo);
  5607. glDeleteTextures(1, &rt->buffers.effect);
  5608. }
  5609. rt->buffers.effects_active = false;
  5610. rt->buffers.active = false;
  5611. }
  5612. if (rt->depth) {
  5613. glDeleteTextures(1, &rt->depth);
  5614. rt->depth = 0;
  5615. }
  5616. if (rt->effects.ssao.blur_fbo[0]) {
  5617. glDeleteFramebuffers(1, &rt->effects.ssao.blur_fbo[0]);
  5618. glDeleteTextures(1, &rt->effects.ssao.blur_red[0]);
  5619. glDeleteFramebuffers(1, &rt->effects.ssao.blur_fbo[1]);
  5620. glDeleteTextures(1, &rt->effects.ssao.blur_red[1]);
  5621. for (int i = 0; i < rt->effects.ssao.depth_mipmap_fbos.size(); i++) {
  5622. glDeleteFramebuffers(1, &rt->effects.ssao.depth_mipmap_fbos[i]);
  5623. }
  5624. rt->effects.ssao.depth_mipmap_fbos.clear();
  5625. glDeleteTextures(1, &rt->effects.ssao.linear_depth);
  5626. rt->effects.ssao.blur_fbo[0] = 0;
  5627. rt->effects.ssao.blur_fbo[1] = 0;
  5628. }
  5629. if (rt->exposure.fbo) {
  5630. glDeleteFramebuffers(1, &rt->exposure.fbo);
  5631. glDeleteTextures(1, &rt->exposure.color);
  5632. rt->exposure.fbo = 0;
  5633. }
  5634. Texture *tex = texture_owner.get(rt->texture);
  5635. tex->alloc_height = 0;
  5636. tex->alloc_width = 0;
  5637. tex->width = 0;
  5638. tex->height = 0;
  5639. tex->active = false;
  5640. if (rt->external.fbo != 0) {
  5641. // free this
  5642. glDeleteFramebuffers(1, &rt->external.fbo);
  5643. // reset our texture back to the original
  5644. tex->tex_id = rt->color;
  5645. rt->external.fbo = 0;
  5646. rt->external.color = 0;
  5647. rt->external.depth = 0;
  5648. }
  5649. for (int i = 0; i < 2; i++) {
  5650. if (rt->effects.mip_maps[i].color) {
  5651. for (int j = 0; j < rt->effects.mip_maps[i].sizes.size(); j++) {
  5652. glDeleteFramebuffers(1, &rt->effects.mip_maps[i].sizes[j].fbo);
  5653. }
  5654. glDeleteTextures(1, &rt->effects.mip_maps[i].color);
  5655. rt->effects.mip_maps[i].sizes.clear();
  5656. rt->effects.mip_maps[i].levels = 0;
  5657. rt->effects.mip_maps[i].color = 0;
  5658. }
  5659. }
  5660. /*
  5661. if (rt->effects.screen_space_depth) {
  5662. glDeleteTextures(1,&rt->effects.screen_space_depth);
  5663. rt->effects.screen_space_depth=0;
  5664. }
  5665. */
  5666. }
  5667. void RasterizerStorageGLES3::_render_target_allocate(RenderTarget *rt) {
  5668. if (rt->width <= 0 || rt->height <= 0) {
  5669. return;
  5670. }
  5671. GLuint color_internal_format;
  5672. GLuint color_format;
  5673. GLuint color_type;
  5674. Image::Format image_format;
  5675. const bool hdr = rt->flags[RENDER_TARGET_HDR] && config.framebuffer_half_float_supported;
  5676. if (!hdr || rt->flags[RENDER_TARGET_NO_3D]) {
  5677. if (rt->flags[RENDER_TARGET_NO_3D_EFFECTS] && !rt->flags[RENDER_TARGET_TRANSPARENT]) {
  5678. //if this is not used, linear colorspace looks pretty bad
  5679. //this is the default mode used for mobile
  5680. color_internal_format = GL_RGB10_A2;
  5681. color_format = GL_RGBA;
  5682. color_type = GL_UNSIGNED_INT_2_10_10_10_REV;
  5683. image_format = Image::FORMAT_RGBA8;
  5684. } else {
  5685. color_internal_format = GL_RGBA8;
  5686. color_format = GL_RGBA;
  5687. color_type = GL_UNSIGNED_BYTE;
  5688. image_format = Image::FORMAT_RGBA8;
  5689. }
  5690. } else {
  5691. // HDR enabled.
  5692. if (rt->flags[RENDER_TARGET_USE_32_BPC_DEPTH]) {
  5693. // 32 bpc. Can be useful for advanced shaders, but should not be used
  5694. // for general-purpose rendering as it's slower.
  5695. color_internal_format = GL_RGBA32F;
  5696. color_format = GL_RGBA;
  5697. color_type = GL_FLOAT;
  5698. image_format = Image::FORMAT_RGBAF;
  5699. } else {
  5700. // 16 bpc. This is the default HDR mode.
  5701. color_internal_format = GL_RGBA16F;
  5702. color_format = GL_RGBA;
  5703. color_type = GL_HALF_FLOAT;
  5704. image_format = Image::FORMAT_RGBAH;
  5705. }
  5706. }
  5707. {
  5708. /* FRONT FBO */
  5709. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  5710. glGenFramebuffers(1, &rt->fbo);
  5711. glBindFramebuffer(GL_FRAMEBUFFER, rt->fbo);
  5712. glGenTextures(1, &rt->depth);
  5713. glBindTexture(GL_TEXTURE_2D, rt->depth);
  5714. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, rt->width, rt->height, 0,
  5715. GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
  5716. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  5717. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  5718. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  5719. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  5720. if (rt->external.depth == 0) {
  5721. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
  5722. GL_TEXTURE_2D, rt->depth, 0);
  5723. } else {
  5724. // Use our external depth texture instead.
  5725. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
  5726. GL_TEXTURE_2D, rt->external.depth, 0);
  5727. }
  5728. glGenTextures(1, &rt->color);
  5729. glBindTexture(GL_TEXTURE_2D, rt->color);
  5730. glTexImage2D(GL_TEXTURE_2D, 0, color_internal_format, rt->width, rt->height, 0, color_format, color_type, nullptr);
  5731. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  5732. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  5733. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  5734. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  5735. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color, 0);
  5736. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  5737. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
  5738. if (status != GL_FRAMEBUFFER_COMPLETE) {
  5739. printf("framebuffer fail, status: %x\n", status);
  5740. }
  5741. ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE);
  5742. Texture *tex = texture_owner.get(rt->texture);
  5743. tex->format = image_format;
  5744. tex->gl_format_cache = color_format;
  5745. tex->gl_type_cache = color_type;
  5746. tex->gl_internal_format_cache = color_internal_format;
  5747. tex->tex_id = rt->color;
  5748. tex->width = rt->width;
  5749. tex->alloc_width = rt->width;
  5750. tex->height = rt->height;
  5751. tex->alloc_height = rt->height;
  5752. tex->active = true;
  5753. texture_set_flags(rt->texture, tex->flags);
  5754. }
  5755. /* BACK FBO */
  5756. if (!rt->flags[RENDER_TARGET_NO_3D] && (!rt->flags[RENDER_TARGET_NO_3D_EFFECTS] || rt->msaa != VS::VIEWPORT_MSAA_DISABLED)) {
  5757. rt->buffers.active = true;
  5758. static const int msaa_value[] = { 0, 2, 4, 8, 16, 4, 16 }; // MSAA_EXT_nX is a GLES2 temporary hack ignored in GLES3 for now...
  5759. int msaa = msaa_value[rt->msaa];
  5760. int max_samples = 0;
  5761. glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
  5762. if (msaa > max_samples) {
  5763. WARN_PRINT("MSAA must be <= GL_MAX_SAMPLES, falling-back to GL_MAX_SAMPLES = " + itos(max_samples));
  5764. msaa = max_samples;
  5765. }
  5766. //regular fbo
  5767. glGenFramebuffers(1, &rt->buffers.fbo);
  5768. glBindFramebuffer(GL_FRAMEBUFFER, rt->buffers.fbo);
  5769. glGenRenderbuffers(1, &rt->buffers.depth);
  5770. glBindRenderbuffer(GL_RENDERBUFFER, rt->buffers.depth);
  5771. if (msaa == 0) {
  5772. glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, rt->width, rt->height);
  5773. } else {
  5774. glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, GL_DEPTH_COMPONENT24, rt->width, rt->height);
  5775. }
  5776. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->buffers.depth);
  5777. glGenRenderbuffers(1, &rt->buffers.diffuse);
  5778. glBindRenderbuffer(GL_RENDERBUFFER, rt->buffers.diffuse);
  5779. if (msaa == 0) {
  5780. glRenderbufferStorage(GL_RENDERBUFFER, color_internal_format, rt->width, rt->height);
  5781. } else {
  5782. glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, color_internal_format, rt->width, rt->height);
  5783. }
  5784. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rt->buffers.diffuse);
  5785. if (!rt->flags[RENDER_TARGET_NO_3D_EFFECTS]) {
  5786. rt->buffers.effects_active = true;
  5787. glGenRenderbuffers(1, &rt->buffers.specular);
  5788. glBindRenderbuffer(GL_RENDERBUFFER, rt->buffers.specular);
  5789. if (msaa == 0) {
  5790. glRenderbufferStorage(GL_RENDERBUFFER, color_internal_format, rt->width, rt->height);
  5791. } else {
  5792. glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, color_internal_format, rt->width, rt->height);
  5793. }
  5794. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_RENDERBUFFER, rt->buffers.specular);
  5795. glGenRenderbuffers(1, &rt->buffers.normal_rough);
  5796. glBindRenderbuffer(GL_RENDERBUFFER, rt->buffers.normal_rough);
  5797. if (msaa == 0) {
  5798. glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, rt->width, rt->height);
  5799. } else {
  5800. glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, GL_RGBA8, rt->width, rt->height);
  5801. }
  5802. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_RENDERBUFFER, rt->buffers.normal_rough);
  5803. glGenRenderbuffers(1, &rt->buffers.sss);
  5804. glBindRenderbuffer(GL_RENDERBUFFER, rt->buffers.sss);
  5805. if (msaa == 0) {
  5806. glRenderbufferStorage(GL_RENDERBUFFER, GL_R8, rt->width, rt->height);
  5807. } else {
  5808. glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, GL_R8, rt->width, rt->height);
  5809. }
  5810. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_RENDERBUFFER, rt->buffers.sss);
  5811. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  5812. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
  5813. if (status != GL_FRAMEBUFFER_COMPLETE) {
  5814. printf("err status: %x\n", status);
  5815. _render_target_clear(rt);
  5816. ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE);
  5817. }
  5818. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  5819. // effect resolver
  5820. glGenFramebuffers(1, &rt->buffers.effect_fbo);
  5821. glBindFramebuffer(GL_FRAMEBUFFER, rt->buffers.effect_fbo);
  5822. glGenTextures(1, &rt->buffers.effect);
  5823. glBindTexture(GL_TEXTURE_2D, rt->buffers.effect);
  5824. glTexImage2D(GL_TEXTURE_2D, 0, color_internal_format, rt->width, rt->height, 0,
  5825. color_format, color_type, nullptr);
  5826. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  5827. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  5828. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  5829. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  5830. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
  5831. GL_TEXTURE_2D, rt->buffers.effect, 0);
  5832. status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  5833. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
  5834. if (status != GL_FRAMEBUFFER_COMPLETE) {
  5835. printf("err status: %x\n", status);
  5836. _render_target_clear(rt);
  5837. ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE);
  5838. }
  5839. ///////////////// ssao
  5840. //AO strength textures
  5841. for (int i = 0; i < 2; i++) {
  5842. glGenFramebuffers(1, &rt->effects.ssao.blur_fbo[i]);
  5843. glBindFramebuffer(GL_FRAMEBUFFER, rt->effects.ssao.blur_fbo[i]);
  5844. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
  5845. GL_TEXTURE_2D, rt->depth, 0);
  5846. glGenTextures(1, &rt->effects.ssao.blur_red[i]);
  5847. glBindTexture(GL_TEXTURE_2D, rt->effects.ssao.blur_red[i]);
  5848. glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, rt->width, rt->height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
  5849. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  5850. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  5851. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  5852. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  5853. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->effects.ssao.blur_red[i], 0);
  5854. status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  5855. if (status != GL_FRAMEBUFFER_COMPLETE) {
  5856. _render_target_clear(rt);
  5857. ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE);
  5858. }
  5859. }
  5860. //5 mip levels for depth texture, but base is read separately
  5861. glGenTextures(1, &rt->effects.ssao.linear_depth);
  5862. glBindTexture(GL_TEXTURE_2D, rt->effects.ssao.linear_depth);
  5863. int ssao_w = rt->width / 2;
  5864. int ssao_h = rt->height / 2;
  5865. for (int i = 0; i < 4; i++) { //5, but 4 mips, base is read directly to save bw
  5866. glTexImage2D(GL_TEXTURE_2D, i, GL_R16UI, ssao_w, ssao_h, 0, GL_RED_INTEGER, GL_UNSIGNED_SHORT, nullptr);
  5867. ssao_w >>= 1;
  5868. ssao_h >>= 1;
  5869. }
  5870. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  5871. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
  5872. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  5873. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  5874. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  5875. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 3);
  5876. for (int i = 0; i < 4; i++) { //5, but 4 mips, base is read directly to save bw
  5877. GLuint fbo;
  5878. glGenFramebuffers(1, &fbo);
  5879. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  5880. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->effects.ssao.linear_depth, i);
  5881. rt->effects.ssao.depth_mipmap_fbos.push_back(fbo);
  5882. }
  5883. //////Exposure
  5884. glGenFramebuffers(1, &rt->exposure.fbo);
  5885. glBindFramebuffer(GL_FRAMEBUFFER, rt->exposure.fbo);
  5886. glGenTextures(1, &rt->exposure.color);
  5887. glBindTexture(GL_TEXTURE_2D, rt->exposure.color);
  5888. if (config.framebuffer_float_supported) {
  5889. glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, 1, 1, 0, GL_RED, GL_FLOAT, nullptr);
  5890. } else if (config.framebuffer_half_float_supported) {
  5891. glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, 1, 1, 0, GL_RED, GL_HALF_FLOAT, nullptr);
  5892. } else {
  5893. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
  5894. }
  5895. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->exposure.color, 0);
  5896. status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  5897. if (status != GL_FRAMEBUFFER_COMPLETE) {
  5898. _render_target_clear(rt);
  5899. ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE);
  5900. }
  5901. } else {
  5902. rt->buffers.effects_active = false;
  5903. }
  5904. } else {
  5905. rt->buffers.active = false;
  5906. rt->buffers.effects_active = true;
  5907. }
  5908. if (!rt->flags[RENDER_TARGET_NO_SAMPLING] && rt->width >= 2 && rt->height >= 2) {
  5909. for (int i = 0; i < 2; i++) {
  5910. ERR_FAIL_COND(rt->effects.mip_maps[i].sizes.size());
  5911. int w = rt->width;
  5912. int h = rt->height;
  5913. if (i > 0) {
  5914. w >>= 1;
  5915. h >>= 1;
  5916. }
  5917. glGenTextures(1, &rt->effects.mip_maps[i].color);
  5918. glBindTexture(GL_TEXTURE_2D, rt->effects.mip_maps[i].color);
  5919. int level = 0;
  5920. int fb_w = w;
  5921. int fb_h = h;
  5922. while (true) {
  5923. RenderTarget::Effects::MipMaps::Size mm;
  5924. mm.width = w;
  5925. mm.height = h;
  5926. rt->effects.mip_maps[i].sizes.push_back(mm);
  5927. w >>= 1;
  5928. h >>= 1;
  5929. if (w < 2 || h < 2) {
  5930. break;
  5931. }
  5932. level++;
  5933. }
  5934. glTexStorage2DCustom(GL_TEXTURE_2D, level + 1, color_internal_format, fb_w, fb_h, color_format, color_type);
  5935. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  5936. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, level);
  5937. glDisable(GL_SCISSOR_TEST);
  5938. glColorMask(1, 1, 1, 1);
  5939. if (!rt->buffers.active) {
  5940. glDepthMask(GL_TRUE);
  5941. }
  5942. for (int j = 0; j < rt->effects.mip_maps[i].sizes.size(); j++) {
  5943. RenderTarget::Effects::MipMaps::Size &mm = rt->effects.mip_maps[i].sizes.write[j];
  5944. glGenFramebuffers(1, &mm.fbo);
  5945. glBindFramebuffer(GL_FRAMEBUFFER, mm.fbo);
  5946. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->effects.mip_maps[i].color, j);
  5947. bool used_depth = false;
  5948. if (j == 0 && i == 0) { //use always
  5949. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rt->depth, 0);
  5950. used_depth = true;
  5951. }
  5952. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  5953. if (status != GL_FRAMEBUFFER_COMPLETE) {
  5954. _render_target_clear(rt);
  5955. ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE);
  5956. }
  5957. float zero[4] = { 1, 0, 1, 0 };
  5958. glViewport(0, 0, rt->effects.mip_maps[i].sizes[j].width, rt->effects.mip_maps[i].sizes[j].height);
  5959. glClearBufferfv(GL_COLOR, 0, zero);
  5960. if (used_depth) {
  5961. glClearDepth(1.0);
  5962. glClear(GL_DEPTH_BUFFER_BIT);
  5963. }
  5964. }
  5965. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
  5966. rt->effects.mip_maps[i].levels = level;
  5967. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  5968. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  5969. //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  5970. //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  5971. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  5972. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  5973. }
  5974. }
  5975. }
  5976. RID RasterizerStorageGLES3::render_target_create() {
  5977. RenderTarget *rt = memnew(RenderTarget);
  5978. Texture *t = memnew(Texture);
  5979. t->type = VS::TEXTURE_TYPE_2D;
  5980. t->flags = 0;
  5981. t->width = 0;
  5982. t->height = 0;
  5983. t->alloc_height = 0;
  5984. t->alloc_width = 0;
  5985. t->format = Image::FORMAT_R8;
  5986. t->target = GL_TEXTURE_2D;
  5987. t->gl_format_cache = 0;
  5988. t->gl_internal_format_cache = 0;
  5989. t->gl_type_cache = 0;
  5990. t->data_size = 0;
  5991. t->compressed = false;
  5992. t->srgb = false;
  5993. t->total_data_size = 0;
  5994. t->ignore_mipmaps = false;
  5995. t->mipmaps = 1;
  5996. t->active = true;
  5997. t->tex_id = 0;
  5998. t->render_target = rt;
  5999. rt->texture = texture_owner.make_rid(t);
  6000. return render_target_owner.make_rid(rt);
  6001. }
  6002. void RasterizerStorageGLES3::render_target_set_position(RID p_render_target, int p_x, int p_y) {
  6003. //only used in GLES2
  6004. }
  6005. void RasterizerStorageGLES3::render_target_set_size(RID p_render_target, int p_width, int p_height) {
  6006. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  6007. ERR_FAIL_COND(!rt);
  6008. if (rt->width == p_width && rt->height == p_height) {
  6009. return;
  6010. }
  6011. _render_target_clear(rt);
  6012. rt->width = p_width;
  6013. rt->height = p_height;
  6014. _render_target_allocate(rt);
  6015. }
  6016. RID RasterizerStorageGLES3::render_target_get_texture(RID p_render_target) const {
  6017. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  6018. ERR_FAIL_COND_V(!rt, RID());
  6019. return rt->texture;
  6020. }
  6021. uint32_t RasterizerStorageGLES3::render_target_get_depth_texture_id(RID p_render_target) const {
  6022. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  6023. ERR_FAIL_COND_V(!rt, 0);
  6024. if (rt->external.depth == 0) {
  6025. return rt->depth;
  6026. } else {
  6027. return rt->external.depth;
  6028. }
  6029. }
  6030. void RasterizerStorageGLES3::render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id, unsigned int p_depth_id) {
  6031. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  6032. ERR_FAIL_COND(!rt);
  6033. if (p_texture_id == 0) {
  6034. if (rt->external.fbo != 0) {
  6035. // return to our original depth buffer
  6036. if (rt->external.depth != 0 && rt->fbo != 0) {
  6037. glBindFramebuffer(GL_FRAMEBUFFER, rt->fbo);
  6038. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rt->depth, 0);
  6039. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
  6040. }
  6041. // free this
  6042. glDeleteFramebuffers(1, &rt->external.fbo);
  6043. // reset our texture back to the original
  6044. Texture *t = texture_owner.get(rt->texture);
  6045. t->tex_id = rt->color;
  6046. t->width = rt->width;
  6047. t->alloc_width = rt->width;
  6048. t->height = rt->height;
  6049. t->alloc_height = rt->height;
  6050. rt->external.fbo = 0;
  6051. rt->external.color = 0;
  6052. rt->external.depth = 0;
  6053. }
  6054. } else {
  6055. if (rt->external.fbo == 0) {
  6056. // create our fbo
  6057. glGenFramebuffers(1, &rt->external.fbo);
  6058. }
  6059. // bind our frame buffer
  6060. glBindFramebuffer(GL_FRAMEBUFFER, rt->external.fbo);
  6061. rt->external.color = p_texture_id;
  6062. // Set our texture to the new image, note that we expect formats to be the same (or compatible) so we don't change those
  6063. Texture *t = texture_owner.get(rt->texture);
  6064. t->tex_id = p_texture_id;
  6065. t->width = rt->width;
  6066. t->height = rt->height;
  6067. t->alloc_height = rt->width;
  6068. t->alloc_width = rt->height;
  6069. // set our texture as the destination for our framebuffer
  6070. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_texture_id, 0);
  6071. // check status
  6072. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  6073. if (status != GL_FRAMEBUFFER_COMPLETE) {
  6074. printf("framebuffer fail, status: %x\n", status);
  6075. }
  6076. // Copy our depth texture id,
  6077. // if it's 0 then we don't use it,
  6078. // else we use it instead of our normal depth buffer
  6079. rt->external.depth = p_depth_id;
  6080. if (rt->external.depth != 0 && rt->fbo != 0) {
  6081. // Use our external depth texture instead.
  6082. glBindFramebuffer(GL_FRAMEBUFFER, rt->fbo);
  6083. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rt->external.depth, 0);
  6084. // check status
  6085. GLenum status2 = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  6086. if (status2 != GL_FRAMEBUFFER_COMPLETE) {
  6087. printf("framebuffer fail, status: %x\n", status2);
  6088. }
  6089. }
  6090. // and unbind
  6091. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
  6092. ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE);
  6093. }
  6094. }
  6095. void RasterizerStorageGLES3::render_target_set_flag(RID p_render_target, RenderTargetFlags p_flag, bool p_value) {
  6096. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  6097. ERR_FAIL_COND(!rt);
  6098. rt->flags[p_flag] = p_value;
  6099. switch (p_flag) {
  6100. case RENDER_TARGET_HDR:
  6101. case RENDER_TARGET_USE_32_BPC_DEPTH:
  6102. case RENDER_TARGET_NO_3D:
  6103. case RENDER_TARGET_NO_SAMPLING:
  6104. case RENDER_TARGET_NO_3D_EFFECTS: {
  6105. //must reset for these formats
  6106. _render_target_clear(rt);
  6107. _render_target_allocate(rt);
  6108. } break;
  6109. default: {
  6110. }
  6111. }
  6112. }
  6113. bool RasterizerStorageGLES3::render_target_was_used(RID p_render_target) {
  6114. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  6115. ERR_FAIL_COND_V(!rt, false);
  6116. return rt->used_in_frame;
  6117. }
  6118. void RasterizerStorageGLES3::render_target_clear_used(RID p_render_target) {
  6119. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  6120. ERR_FAIL_COND(!rt);
  6121. rt->used_in_frame = false;
  6122. }
  6123. void RasterizerStorageGLES3::render_target_set_msaa(RID p_render_target, VS::ViewportMSAA p_msaa) {
  6124. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  6125. ERR_FAIL_COND(!rt);
  6126. if (rt->msaa == p_msaa) {
  6127. return;
  6128. }
  6129. _render_target_clear(rt);
  6130. rt->msaa = p_msaa;
  6131. _render_target_allocate(rt);
  6132. }
  6133. void RasterizerStorageGLES3::render_target_set_use_fxaa(RID p_render_target, bool p_fxaa) {
  6134. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  6135. ERR_FAIL_COND(!rt);
  6136. rt->use_fxaa = p_fxaa;
  6137. }
  6138. void RasterizerStorageGLES3::render_target_set_use_debanding(RID p_render_target, bool p_debanding) {
  6139. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  6140. ERR_FAIL_COND(!rt);
  6141. rt->use_debanding = p_debanding;
  6142. }
  6143. void RasterizerStorageGLES3::render_target_set_sharpen_intensity(RID p_render_target, float p_intensity) {
  6144. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  6145. ERR_FAIL_COND(!rt);
  6146. rt->sharpen_intensity = p_intensity;
  6147. }
  6148. /* CANVAS SHADOW */
  6149. RID RasterizerStorageGLES3::canvas_light_shadow_buffer_create(int p_width) {
  6150. CanvasLightShadow *cls = memnew(CanvasLightShadow);
  6151. if (p_width > config.max_texture_size) {
  6152. p_width = config.max_texture_size;
  6153. }
  6154. cls->size = p_width;
  6155. cls->height = 16;
  6156. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  6157. glGenFramebuffers(1, &cls->fbo);
  6158. glBindFramebuffer(GL_FRAMEBUFFER, cls->fbo);
  6159. glGenRenderbuffers(1, &cls->depth);
  6160. glBindRenderbuffer(GL_RENDERBUFFER, cls->depth);
  6161. glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, cls->size, cls->height);
  6162. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, cls->depth);
  6163. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  6164. glGenTextures(1, &cls->distance);
  6165. glBindTexture(GL_TEXTURE_2D, cls->distance);
  6166. if (config.use_rgba_2d_shadows) {
  6167. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, cls->size, cls->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  6168. } else {
  6169. glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, cls->size, cls->height, 0, GL_RED, GL_FLOAT, nullptr);
  6170. }
  6171. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  6172. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  6173. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  6174. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  6175. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, cls->distance, 0);
  6176. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  6177. //printf("errnum: %x\n",status);
  6178. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
  6179. if (status != GL_FRAMEBUFFER_COMPLETE) {
  6180. memdelete(cls);
  6181. ERR_FAIL_COND_V(status != GL_FRAMEBUFFER_COMPLETE, RID());
  6182. }
  6183. return canvas_light_shadow_owner.make_rid(cls);
  6184. }
  6185. /* LIGHT SHADOW MAPPING */
  6186. RID RasterizerStorageGLES3::canvas_light_occluder_create() {
  6187. CanvasOccluder *co = memnew(CanvasOccluder);
  6188. co->index_id = 0;
  6189. co->vertex_id = 0;
  6190. co->len = 0;
  6191. glGenVertexArrays(1, &co->array_id);
  6192. return canvas_occluder_owner.make_rid(co);
  6193. }
  6194. void RasterizerStorageGLES3::canvas_light_occluder_set_polylines(RID p_occluder, const PoolVector<Vector2> &p_lines) {
  6195. CanvasOccluder *co = canvas_occluder_owner.get(p_occluder);
  6196. ERR_FAIL_COND(!co);
  6197. co->lines = p_lines;
  6198. if (p_lines.size() != co->len) {
  6199. if (co->index_id) {
  6200. glDeleteBuffers(1, &co->index_id);
  6201. }
  6202. if (co->vertex_id) {
  6203. glDeleteBuffers(1, &co->vertex_id);
  6204. }
  6205. co->index_id = 0;
  6206. co->vertex_id = 0;
  6207. co->len = 0;
  6208. }
  6209. if (p_lines.size()) {
  6210. PoolVector<float> geometry;
  6211. PoolVector<uint16_t> indices;
  6212. int lc = p_lines.size();
  6213. geometry.resize(lc * 6);
  6214. indices.resize(lc * 3);
  6215. PoolVector<float>::Write vw = geometry.write();
  6216. PoolVector<uint16_t>::Write iw = indices.write();
  6217. PoolVector<Vector2>::Read lr = p_lines.read();
  6218. const int POLY_HEIGHT = 16384;
  6219. for (int i = 0; i < lc / 2; i++) {
  6220. vw[i * 12 + 0] = lr[i * 2 + 0].x;
  6221. vw[i * 12 + 1] = lr[i * 2 + 0].y;
  6222. vw[i * 12 + 2] = POLY_HEIGHT;
  6223. vw[i * 12 + 3] = lr[i * 2 + 1].x;
  6224. vw[i * 12 + 4] = lr[i * 2 + 1].y;
  6225. vw[i * 12 + 5] = POLY_HEIGHT;
  6226. vw[i * 12 + 6] = lr[i * 2 + 1].x;
  6227. vw[i * 12 + 7] = lr[i * 2 + 1].y;
  6228. vw[i * 12 + 8] = -POLY_HEIGHT;
  6229. vw[i * 12 + 9] = lr[i * 2 + 0].x;
  6230. vw[i * 12 + 10] = lr[i * 2 + 0].y;
  6231. vw[i * 12 + 11] = -POLY_HEIGHT;
  6232. iw[i * 6 + 0] = i * 4 + 0;
  6233. iw[i * 6 + 1] = i * 4 + 1;
  6234. iw[i * 6 + 2] = i * 4 + 2;
  6235. iw[i * 6 + 3] = i * 4 + 2;
  6236. iw[i * 6 + 4] = i * 4 + 3;
  6237. iw[i * 6 + 5] = i * 4 + 0;
  6238. }
  6239. //if same buffer len is being set, just use BufferSubData to avoid a pipeline flush
  6240. if (!co->vertex_id) {
  6241. glGenBuffers(1, &co->vertex_id);
  6242. glBindBuffer(GL_ARRAY_BUFFER, co->vertex_id);
  6243. glBufferData(GL_ARRAY_BUFFER, lc * 6 * sizeof(real_t), vw.ptr(), GL_STATIC_DRAW);
  6244. } else {
  6245. glBindBuffer(GL_ARRAY_BUFFER, co->vertex_id);
  6246. glBufferSubData(GL_ARRAY_BUFFER, 0, lc * 6 * sizeof(real_t), vw.ptr());
  6247. }
  6248. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  6249. if (!co->index_id) {
  6250. glGenBuffers(1, &co->index_id);
  6251. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, co->index_id);
  6252. glBufferData(GL_ELEMENT_ARRAY_BUFFER, lc * 3 * sizeof(uint16_t), iw.ptr(), GL_DYNAMIC_DRAW);
  6253. } else {
  6254. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, co->index_id);
  6255. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, lc * 3 * sizeof(uint16_t), iw.ptr());
  6256. }
  6257. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); //unbind
  6258. co->len = lc;
  6259. glBindVertexArray(co->array_id);
  6260. glBindBuffer(GL_ARRAY_BUFFER, co->vertex_id);
  6261. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  6262. glVertexAttribPointer(VS::ARRAY_VERTEX, 3, GL_FLOAT, false, 0, nullptr);
  6263. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, co->index_id);
  6264. glBindVertexArray(0);
  6265. }
  6266. }
  6267. VS::InstanceType RasterizerStorageGLES3::get_base_type(RID p_rid) const {
  6268. if (mesh_owner.owns(p_rid)) {
  6269. return VS::INSTANCE_MESH;
  6270. }
  6271. if (multimesh_owner.owns(p_rid)) {
  6272. return VS::INSTANCE_MULTIMESH;
  6273. }
  6274. if (immediate_owner.owns(p_rid)) {
  6275. return VS::INSTANCE_IMMEDIATE;
  6276. }
  6277. if (particles_owner.owns(p_rid)) {
  6278. return VS::INSTANCE_PARTICLES;
  6279. }
  6280. if (light_owner.owns(p_rid)) {
  6281. return VS::INSTANCE_LIGHT;
  6282. }
  6283. if (reflection_probe_owner.owns(p_rid)) {
  6284. return VS::INSTANCE_REFLECTION_PROBE;
  6285. }
  6286. if (gi_probe_owner.owns(p_rid)) {
  6287. return VS::INSTANCE_GI_PROBE;
  6288. }
  6289. if (lightmap_capture_data_owner.owns(p_rid)) {
  6290. return VS::INSTANCE_LIGHTMAP_CAPTURE;
  6291. }
  6292. return VS::INSTANCE_NONE;
  6293. }
  6294. bool RasterizerStorageGLES3::free(RID p_rid) {
  6295. if (render_target_owner.owns(p_rid)) {
  6296. RenderTarget *rt = render_target_owner.getornull(p_rid);
  6297. _render_target_clear(rt);
  6298. Texture *t = texture_owner.get(rt->texture);
  6299. texture_owner.free(rt->texture);
  6300. memdelete(t);
  6301. render_target_owner.free(p_rid);
  6302. memdelete(rt);
  6303. } else if (texture_owner.owns(p_rid)) {
  6304. // delete the texture
  6305. Texture *texture = texture_owner.get(p_rid);
  6306. ERR_FAIL_COND_V(texture->render_target, true); //can't free the render target texture, dude
  6307. info.texture_mem -= texture->total_data_size;
  6308. texture_owner.free(p_rid);
  6309. memdelete(texture);
  6310. } else if (sky_owner.owns(p_rid)) {
  6311. // delete the sky
  6312. Sky *sky = sky_owner.get(p_rid);
  6313. sky_set_texture(p_rid, RID(), 256);
  6314. sky_owner.free(p_rid);
  6315. memdelete(sky);
  6316. } else if (shader_owner.owns(p_rid)) {
  6317. // delete the texture
  6318. Shader *shader = shader_owner.get(p_rid);
  6319. if (shader->shader && shader->custom_code_id) {
  6320. shader->shader->free_custom_shader(shader->custom_code_id);
  6321. }
  6322. if (shader->dirty_list.in_list()) {
  6323. _shader_dirty_list.remove(&shader->dirty_list);
  6324. }
  6325. while (shader->materials.first()) {
  6326. Material *mat = shader->materials.first()->self();
  6327. mat->shader = nullptr;
  6328. _material_make_dirty(mat);
  6329. shader->materials.remove(shader->materials.first());
  6330. }
  6331. //material_shader.free_custom_shader(shader->custom_code_id);
  6332. shader_owner.free(p_rid);
  6333. memdelete(shader);
  6334. } else if (material_owner.owns(p_rid)) {
  6335. // delete the texture
  6336. Material *material = material_owner.get(p_rid);
  6337. if (material->shader) {
  6338. material->shader->materials.remove(&material->list);
  6339. }
  6340. if (material->ubo_id) {
  6341. glDeleteBuffers(1, &material->ubo_id);
  6342. }
  6343. //remove from owners
  6344. for (Map<Geometry *, int>::Element *E = material->geometry_owners.front(); E; E = E->next()) {
  6345. Geometry *g = E->key();
  6346. g->material = RID();
  6347. }
  6348. for (Map<RasterizerScene::InstanceBase *, int>::Element *E = material->instance_owners.front(); E; E = E->next()) {
  6349. RasterizerScene::InstanceBase *ins = E->key();
  6350. if (ins->material_override == p_rid) {
  6351. ins->material_override = RID();
  6352. }
  6353. if (ins->material_overlay == p_rid) {
  6354. ins->material_overlay = RID();
  6355. }
  6356. for (int i = 0; i < ins->materials.size(); i++) {
  6357. if (ins->materials[i] == p_rid) {
  6358. ins->materials.write[i] = RID();
  6359. }
  6360. }
  6361. }
  6362. material_owner.free(p_rid);
  6363. memdelete(material);
  6364. } else if (skeleton_owner.owns(p_rid)) {
  6365. // delete the texture
  6366. Skeleton *skeleton = skeleton_owner.get(p_rid);
  6367. if (skeleton->update_list.in_list()) {
  6368. skeleton_update_list.remove(&skeleton->update_list);
  6369. }
  6370. for (Set<RasterizerScene::InstanceBase *>::Element *E = skeleton->instances.front(); E; E = E->next()) {
  6371. E->get()->skeleton = RID();
  6372. }
  6373. skeleton_allocate(p_rid, 0, false);
  6374. glDeleteTextures(1, &skeleton->texture);
  6375. skeleton_owner.free(p_rid);
  6376. memdelete(skeleton);
  6377. } else if (mesh_owner.owns(p_rid)) {
  6378. // delete the texture
  6379. Mesh *mesh = mesh_owner.get(p_rid);
  6380. mesh->instance_remove_deps();
  6381. mesh_clear(p_rid);
  6382. while (mesh->multimeshes.first()) {
  6383. MultiMesh *multimesh = mesh->multimeshes.first()->self();
  6384. multimesh->mesh = RID();
  6385. multimesh->dirty_aabb = true;
  6386. mesh->multimeshes.remove(mesh->multimeshes.first());
  6387. if (!multimesh->update_list.in_list()) {
  6388. multimesh_update_list.add(&multimesh->update_list);
  6389. }
  6390. }
  6391. mesh_owner.free(p_rid);
  6392. memdelete(mesh);
  6393. } else if (multimesh_owner.owns(p_rid)) {
  6394. // remove from interpolator
  6395. _interpolation_data.notify_free_multimesh(p_rid);
  6396. MultiMesh *multimesh = multimesh_owner.get(p_rid);
  6397. // remove any references in linked canvas items
  6398. int num_linked = multimesh->linked_canvas_items.size();
  6399. for (int n = 0; n < num_linked; n++) {
  6400. const RID &rid = multimesh->linked_canvas_items[n];
  6401. VSG::canvas->_canvas_item_remove_references(rid, p_rid);
  6402. }
  6403. // delete the texture
  6404. multimesh->instance_remove_deps();
  6405. if (multimesh->mesh.is_valid()) {
  6406. Mesh *mesh = mesh_owner.getornull(multimesh->mesh);
  6407. if (mesh) {
  6408. mesh->multimeshes.remove(&multimesh->mesh_list);
  6409. }
  6410. }
  6411. multimesh_allocate(p_rid, 0, VS::MULTIMESH_TRANSFORM_2D, VS::MULTIMESH_COLOR_NONE); //frees multimesh
  6412. update_dirty_multimeshes();
  6413. multimesh_owner.free(p_rid);
  6414. memdelete(multimesh);
  6415. } else if (immediate_owner.owns(p_rid)) {
  6416. Immediate *immediate = immediate_owner.get(p_rid);
  6417. immediate->instance_remove_deps();
  6418. immediate_owner.free(p_rid);
  6419. memdelete(immediate);
  6420. } else if (light_owner.owns(p_rid)) {
  6421. // delete the texture
  6422. Light *light = light_owner.get(p_rid);
  6423. light->instance_remove_deps();
  6424. light_owner.free(p_rid);
  6425. memdelete(light);
  6426. } else if (reflection_probe_owner.owns(p_rid)) {
  6427. // delete the texture
  6428. ReflectionProbe *reflection_probe = reflection_probe_owner.get(p_rid);
  6429. reflection_probe->instance_remove_deps();
  6430. reflection_probe_owner.free(p_rid);
  6431. memdelete(reflection_probe);
  6432. } else if (gi_probe_owner.owns(p_rid)) {
  6433. // delete the texture
  6434. GIProbe *gi_probe = gi_probe_owner.get(p_rid);
  6435. gi_probe->instance_remove_deps();
  6436. gi_probe_owner.free(p_rid);
  6437. memdelete(gi_probe);
  6438. } else if (gi_probe_data_owner.owns(p_rid)) {
  6439. // delete the texture
  6440. GIProbeData *gi_probe_data = gi_probe_data_owner.get(p_rid);
  6441. glDeleteTextures(1, &gi_probe_data->tex_id);
  6442. gi_probe_data_owner.free(p_rid);
  6443. memdelete(gi_probe_data);
  6444. } else if (lightmap_capture_data_owner.owns(p_rid)) {
  6445. // delete the texture
  6446. LightmapCapture *lightmap_capture = lightmap_capture_data_owner.get(p_rid);
  6447. lightmap_capture->instance_remove_deps();
  6448. lightmap_capture_data_owner.free(p_rid);
  6449. memdelete(lightmap_capture);
  6450. } else if (canvas_occluder_owner.owns(p_rid)) {
  6451. CanvasOccluder *co = canvas_occluder_owner.get(p_rid);
  6452. if (co->index_id) {
  6453. glDeleteBuffers(1, &co->index_id);
  6454. }
  6455. if (co->vertex_id) {
  6456. glDeleteBuffers(1, &co->vertex_id);
  6457. }
  6458. glDeleteVertexArrays(1, &co->array_id);
  6459. canvas_occluder_owner.free(p_rid);
  6460. memdelete(co);
  6461. } else if (canvas_light_shadow_owner.owns(p_rid)) {
  6462. CanvasLightShadow *cls = canvas_light_shadow_owner.get(p_rid);
  6463. glDeleteFramebuffers(1, &cls->fbo);
  6464. glDeleteRenderbuffers(1, &cls->depth);
  6465. glDeleteTextures(1, &cls->distance);
  6466. canvas_light_shadow_owner.free(p_rid);
  6467. memdelete(cls);
  6468. } else if (particles_owner.owns(p_rid)) {
  6469. Particles *particles = particles_owner.get(p_rid);
  6470. particles->instance_remove_deps();
  6471. particles_owner.free(p_rid);
  6472. memdelete(particles);
  6473. } else {
  6474. return false;
  6475. }
  6476. return true;
  6477. }
  6478. bool RasterizerStorageGLES3::has_os_feature(const String &p_feature) const {
  6479. if (p_feature == "bptc") {
  6480. return config.bptc_supported;
  6481. }
  6482. if (p_feature == "s3tc") {
  6483. return config.s3tc_supported;
  6484. }
  6485. if (p_feature == "etc") {
  6486. return config.etc_supported;
  6487. }
  6488. if (p_feature == "etc2") {
  6489. return config.etc2_supported;
  6490. }
  6491. if (p_feature == "pvrtc") {
  6492. return config.pvrtc_supported;
  6493. }
  6494. return false;
  6495. }
  6496. ////////////////////////////////////////////
  6497. void RasterizerStorageGLES3::set_debug_generate_wireframes(bool p_generate) {
  6498. config.generate_wireframes = p_generate;
  6499. }
  6500. void RasterizerStorageGLES3::render_info_begin_capture() {
  6501. info.snap = info.render;
  6502. }
  6503. void RasterizerStorageGLES3::render_info_end_capture() {
  6504. info.snap.object_count = info.render.object_count - info.snap.object_count;
  6505. info.snap.draw_call_count = info.render.draw_call_count - info.snap.draw_call_count;
  6506. info.snap.material_switch_count = info.render.material_switch_count - info.snap.material_switch_count;
  6507. info.snap.surface_switch_count = info.render.surface_switch_count - info.snap.surface_switch_count;
  6508. info.snap.shader_rebind_count = info.render.shader_rebind_count - info.snap.shader_rebind_count;
  6509. info.snap.shader_compiles_started_count = info.render.shader_compiles_started_count - info.snap.shader_compiles_started_count;
  6510. info.snap.shader_compiles_in_progress_count = info.render.shader_compiles_in_progress_count - info.snap.shader_compiles_in_progress_count;
  6511. info.snap.vertices_count = info.render.vertices_count - info.snap.vertices_count;
  6512. info.snap._2d_item_count = info.render._2d_item_count - info.snap._2d_item_count;
  6513. info.snap._2d_draw_call_count = info.render._2d_draw_call_count - info.snap._2d_draw_call_count;
  6514. }
  6515. int RasterizerStorageGLES3::get_captured_render_info(VS::RenderInfo p_info) {
  6516. switch (p_info) {
  6517. case VS::INFO_OBJECTS_IN_FRAME: {
  6518. return info.snap.object_count;
  6519. } break;
  6520. case VS::INFO_VERTICES_IN_FRAME: {
  6521. return info.snap.vertices_count;
  6522. } break;
  6523. case VS::INFO_MATERIAL_CHANGES_IN_FRAME: {
  6524. return info.snap.material_switch_count;
  6525. } break;
  6526. case VS::INFO_SHADER_CHANGES_IN_FRAME: {
  6527. return info.snap.shader_rebind_count;
  6528. } break;
  6529. case VS::INFO_SHADER_COMPILES_IN_FRAME: {
  6530. return info.snap.shader_compiles_in_progress_count;
  6531. } break;
  6532. case VS::INFO_SURFACE_CHANGES_IN_FRAME: {
  6533. return info.snap.surface_switch_count;
  6534. } break;
  6535. case VS::INFO_DRAW_CALLS_IN_FRAME: {
  6536. return info.snap.draw_call_count;
  6537. } break;
  6538. case VS::INFO_2D_ITEMS_IN_FRAME: {
  6539. return info.snap._2d_item_count;
  6540. } break;
  6541. case VS::INFO_2D_DRAW_CALLS_IN_FRAME: {
  6542. return info.snap._2d_draw_call_count;
  6543. } break;
  6544. default: {
  6545. return get_render_info(p_info);
  6546. }
  6547. }
  6548. }
  6549. uint64_t RasterizerStorageGLES3::get_render_info(VS::RenderInfo p_info) {
  6550. switch (p_info) {
  6551. case VS::INFO_OBJECTS_IN_FRAME:
  6552. return info.render_final.object_count;
  6553. case VS::INFO_VERTICES_IN_FRAME:
  6554. return info.render_final.vertices_count;
  6555. case VS::INFO_MATERIAL_CHANGES_IN_FRAME:
  6556. return info.render_final.material_switch_count;
  6557. case VS::INFO_SHADER_CHANGES_IN_FRAME:
  6558. return info.render_final.shader_rebind_count;
  6559. case VS::INFO_SHADER_COMPILES_IN_FRAME:
  6560. return info.render.shader_compiles_in_progress_count;
  6561. case VS::INFO_SURFACE_CHANGES_IN_FRAME:
  6562. return info.render_final.surface_switch_count;
  6563. case VS::INFO_DRAW_CALLS_IN_FRAME:
  6564. return info.render_final.draw_call_count;
  6565. case VS::INFO_2D_ITEMS_IN_FRAME:
  6566. return info.render_final._2d_item_count;
  6567. case VS::INFO_2D_DRAW_CALLS_IN_FRAME:
  6568. return info.render_final._2d_draw_call_count;
  6569. case VS::INFO_USAGE_VIDEO_MEM_TOTAL:
  6570. return 0; //no idea
  6571. case VS::INFO_VIDEO_MEM_USED:
  6572. return info.vertex_mem + info.texture_mem;
  6573. case VS::INFO_TEXTURE_MEM_USED:
  6574. return info.texture_mem;
  6575. case VS::INFO_VERTEX_MEM_USED:
  6576. return info.vertex_mem;
  6577. default:
  6578. return 0; //no idea either
  6579. }
  6580. }
  6581. String RasterizerStorageGLES3::get_video_adapter_name() const {
  6582. return (const char *)glGetString(GL_RENDERER);
  6583. }
  6584. String RasterizerStorageGLES3::get_video_adapter_vendor() const {
  6585. return (const char *)glGetString(GL_VENDOR);
  6586. }
  6587. void RasterizerStorageGLES3::initialize() {
  6588. RasterizerStorageGLES3::system_fbo = 0;
  6589. //// extensions config
  6590. ///
  6591. {
  6592. int max_extensions = safe_gl_get_integer(GL_NUM_EXTENSIONS);
  6593. for (int i = 0; i < max_extensions; i++) {
  6594. const GLubyte *s = glGetStringi(GL_EXTENSIONS, i);
  6595. if (!s) {
  6596. break;
  6597. }
  6598. config.extensions.insert((const char *)s);
  6599. }
  6600. }
  6601. config.shrink_textures_x2 = false;
  6602. config.use_fast_texture_filter = int(ProjectSettings::get_singleton()->get("rendering/quality/filters/use_nearest_mipmap_filter"));
  6603. // Cap max_texture_image_units as we don't need large numbers of units,
  6604. // just in case an implementation provides a large number, as we want to keep
  6605. // the table in gl_wrapper small.
  6606. config.max_texture_image_units = safe_gl_get_integer(GL_MAX_TEXTURE_IMAGE_UNITS, Config::max_desired_texture_image_units);
  6607. // Initialize GLWrapper early on, as required for any calls to glActiveTexture.
  6608. gl_wrapper.initialize(config.max_texture_image_units);
  6609. config.etc_supported = config.extensions.has("GL_OES_compressed_ETC1_RGB8_texture");
  6610. config.latc_supported = config.extensions.has("GL_EXT_texture_compression_latc");
  6611. config.bptc_supported = config.extensions.has("GL_ARB_texture_compression_bptc");
  6612. #ifdef GLES_OVER_GL
  6613. config.etc2_supported = false;
  6614. config.s3tc_supported = true;
  6615. config.rgtc_supported = true; //RGTC - core since OpenGL version 3.0
  6616. config.texture_float_linear_supported = true;
  6617. config.framebuffer_float_supported = true;
  6618. config.framebuffer_half_float_supported = true;
  6619. #else
  6620. config.etc2_supported = true;
  6621. config.s3tc_supported = config.extensions.has("GL_EXT_texture_compression_dxt1") || config.extensions.has("GL_EXT_texture_compression_s3tc") || config.extensions.has("WEBGL_compressed_texture_s3tc");
  6622. config.rgtc_supported = config.extensions.has("GL_EXT_texture_compression_rgtc") || config.extensions.has("GL_ARB_texture_compression_rgtc") || config.extensions.has("EXT_texture_compression_rgtc");
  6623. config.texture_float_linear_supported = config.extensions.has("GL_OES_texture_float_linear");
  6624. config.framebuffer_float_supported = config.extensions.has("GL_EXT_color_buffer_float");
  6625. config.framebuffer_half_float_supported = config.extensions.has("GL_EXT_color_buffer_half_float") || config.framebuffer_float_supported;
  6626. // If the desktop build is using S3TC, and you export / run from the IDE for android, if the device supports
  6627. // S3TC it will crash trying to load these textures, as they are not exported in the APK. This is a simple way
  6628. // to prevent Android devices trying to load S3TC, by faking lack of hardware support.
  6629. #if defined(ANDROID_ENABLED) || defined(IPHONE_ENABLED)
  6630. config.s3tc_supported = false;
  6631. #endif
  6632. #endif
  6633. // not yet detected on GLES3 (is this mandated?)
  6634. config.support_npot_repeat_mipmap = true;
  6635. config.pvrtc_supported = config.extensions.has("GL_IMG_texture_compression_pvrtc");
  6636. config.srgb_decode_supported = config.extensions.has("GL_EXT_texture_sRGB_decode");
  6637. config.anisotropic_level = 1.0;
  6638. config.use_anisotropic_filter = config.extensions.has("GL_EXT_texture_filter_anisotropic");
  6639. if (config.use_anisotropic_filter) {
  6640. glGetFloatv(_GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &config.anisotropic_level);
  6641. config.anisotropic_level = MIN(int(ProjectSettings::get_singleton()->get("rendering/quality/filters/anisotropic_filter_level")), config.anisotropic_level);
  6642. }
  6643. #ifdef GLES_OVER_GL
  6644. config.program_binary_supported = GLAD_GL_ARB_get_program_binary;
  6645. config.parallel_shader_compile_supported = GLAD_GL_ARB_parallel_shader_compile || GLAD_GL_KHR_parallel_shader_compile;
  6646. #else
  6647. #ifdef JAVASCRIPT_ENABLED
  6648. config.program_binary_supported = false;
  6649. #else
  6650. config.program_binary_supported = true;
  6651. #endif
  6652. config.parallel_shader_compile_supported = config.extensions.has("GL_KHR_parallel_shader_compile") || config.extensions.has("GL_ARB_parallel_shader_compile");
  6653. #endif
  6654. int compilation_mode = 0;
  6655. if (!(Engine::get_singleton()->is_editor_hint() || Main::is_project_manager())) {
  6656. compilation_mode = ProjectSettings::get_singleton()->get("rendering/gles3/shaders/shader_compilation_mode");
  6657. }
  6658. config.async_compilation_enabled = compilation_mode >= 1;
  6659. config.shader_cache_enabled = compilation_mode == 2;
  6660. if (config.async_compilation_enabled) {
  6661. ShaderGLES3::max_simultaneous_compiles = MAX(1, (int)ProjectSettings::get_singleton()->get("rendering/gles3/shaders/max_simultaneous_compiles"));
  6662. #ifdef GLES_OVER_GL
  6663. if (GLAD_GL_ARB_parallel_shader_compile) {
  6664. glMaxShaderCompilerThreadsARB(ShaderGLES3::max_simultaneous_compiles);
  6665. } else if (GLAD_GL_KHR_parallel_shader_compile) {
  6666. glMaxShaderCompilerThreadsKHR(ShaderGLES3::max_simultaneous_compiles);
  6667. }
  6668. #else
  6669. #if defined(IPHONE_ENABLED) || defined(ANDROID_ENABLED) // TODO: Consider more platforms?
  6670. void *gles3_lib = nullptr;
  6671. void (*MaxShaderCompilerThreads)(GLuint) = nullptr;
  6672. #if defined(IPHONE_ENABLED)
  6673. gles3_lib = dlopen(nullptr, RTLD_LAZY);
  6674. #elif defined(ANDROID_ENABLED)
  6675. gles3_lib = dlopen("libGLESv3.so", RTLD_LAZY);
  6676. #endif
  6677. if (gles3_lib) {
  6678. MaxShaderCompilerThreads = (void (*)(GLuint))dlsym(gles3_lib, "glMaxShaderCompilerThreadsARB");
  6679. if (!MaxShaderCompilerThreads) {
  6680. MaxShaderCompilerThreads = (void (*)(GLuint))dlsym(gles3_lib, "glMaxShaderCompilerThreadsKHR");
  6681. }
  6682. }
  6683. if (MaxShaderCompilerThreads) {
  6684. MaxShaderCompilerThreads(ShaderGLES3::max_simultaneous_compiles);
  6685. } else {
  6686. #ifdef DEBUG_ENABLED
  6687. print_line("Async. shader compilation: No MaxShaderCompilerThreads function found.");
  6688. #endif
  6689. }
  6690. #endif
  6691. #endif
  6692. } else {
  6693. ShaderGLES3::max_simultaneous_compiles = 0;
  6694. }
  6695. #ifdef DEBUG_ENABLED
  6696. ShaderGLES3::log_active_async_compiles_count = (bool)ProjectSettings::get_singleton()->get("rendering/gles3/shaders/log_active_async_compiles_count");
  6697. #endif
  6698. frame.clear_request = false;
  6699. shaders.compile_queue = nullptr;
  6700. shaders.cache = nullptr;
  6701. shaders.cache_write_queue = nullptr;
  6702. bool effectively_on = false;
  6703. if (config.async_compilation_enabled) {
  6704. if (config.parallel_shader_compile_supported) {
  6705. print_line("Async. shader compilation: ON (full native support)");
  6706. effectively_on = true;
  6707. } else if (config.program_binary_supported && OS::get_singleton()->is_offscreen_gl_available()) {
  6708. shaders.compile_queue = memnew(ThreadedCallableQueue<GLuint>());
  6709. shaders.compile_queue->enqueue(0, []() { OS::get_singleton()->set_offscreen_gl_current(true); });
  6710. print_line("Async. shader compilation: ON (via secondary context)");
  6711. effectively_on = true;
  6712. } else {
  6713. print_line("Async. shader compilation: OFF (enabled for " + String(Engine::get_singleton()->is_editor_hint() ? "editor" : "project") + ", but not supported)");
  6714. }
  6715. if (effectively_on) {
  6716. if (config.shader_cache_enabled) {
  6717. if (config.program_binary_supported) {
  6718. print_line("Shader cache: ON");
  6719. shaders.cache = memnew(ShaderCacheGLES3);
  6720. shaders.cache_write_queue = memnew(ThreadedCallableQueue<GLuint>());
  6721. } else {
  6722. print_line("Shader cache: OFF (enabled, but not supported)");
  6723. }
  6724. } else {
  6725. print_line("Shader cache: OFF");
  6726. }
  6727. }
  6728. } else {
  6729. print_line("Async. shader compilation: OFF");
  6730. }
  6731. ShaderGLES3::compile_queue = shaders.compile_queue;
  6732. ShaderGLES3::parallel_compile_supported = config.parallel_shader_compile_supported;
  6733. ShaderGLES3::shader_cache = shaders.cache;
  6734. ShaderGLES3::cache_write_queue = shaders.cache_write_queue;
  6735. shaders.copy.init();
  6736. {
  6737. // Generate default textures.
  6738. // Opaque white color.
  6739. glGenTextures(1, &resources.white_tex);
  6740. unsigned char whitetexdata[8 * 8 * 3];
  6741. for (int i = 0; i < 8 * 8 * 3; i++) {
  6742. whitetexdata[i] = 255;
  6743. }
  6744. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  6745. glBindTexture(GL_TEXTURE_2D, resources.white_tex);
  6746. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, whitetexdata);
  6747. glGenerateMipmap(GL_TEXTURE_2D);
  6748. glBindTexture(GL_TEXTURE_2D, 0);
  6749. // Opaque black color.
  6750. glGenTextures(1, &resources.black_tex);
  6751. unsigned char blacktexdata[8 * 8 * 3];
  6752. for (int i = 0; i < 8 * 8 * 3; i++) {
  6753. blacktexdata[i] = 0;
  6754. }
  6755. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  6756. glBindTexture(GL_TEXTURE_2D, resources.black_tex);
  6757. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, blacktexdata);
  6758. glGenerateMipmap(GL_TEXTURE_2D);
  6759. glBindTexture(GL_TEXTURE_2D, 0);
  6760. // Transparent black color.
  6761. glGenTextures(1, &resources.transparent_tex);
  6762. unsigned char transparenttexdata[8 * 8 * 4];
  6763. for (int i = 0; i < 8 * 8 * 4; i++) {
  6764. transparenttexdata[i] = 0;
  6765. }
  6766. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  6767. glBindTexture(GL_TEXTURE_2D, resources.transparent_tex);
  6768. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, transparenttexdata);
  6769. glGenerateMipmap(GL_TEXTURE_2D);
  6770. glBindTexture(GL_TEXTURE_2D, 0);
  6771. // Opaque "flat" normal map color.
  6772. glGenTextures(1, &resources.normal_tex);
  6773. unsigned char normaltexdata[8 * 8 * 3];
  6774. for (int i = 0; i < 8 * 8 * 3; i += 3) {
  6775. normaltexdata[i + 0] = 128;
  6776. normaltexdata[i + 1] = 128;
  6777. normaltexdata[i + 2] = 255;
  6778. }
  6779. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  6780. glBindTexture(GL_TEXTURE_2D, resources.normal_tex);
  6781. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, normaltexdata);
  6782. glGenerateMipmap(GL_TEXTURE_2D);
  6783. glBindTexture(GL_TEXTURE_2D, 0);
  6784. // Opaque "flat" flowmap color.
  6785. glGenTextures(1, &resources.aniso_tex);
  6786. unsigned char anisotexdata[8 * 8 * 3];
  6787. for (int i = 0; i < 8 * 8 * 3; i += 3) {
  6788. anisotexdata[i + 0] = 255;
  6789. anisotexdata[i + 1] = 128;
  6790. anisotexdata[i + 2] = 0;
  6791. }
  6792. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  6793. glBindTexture(GL_TEXTURE_2D, resources.aniso_tex);
  6794. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, anisotexdata);
  6795. glGenerateMipmap(GL_TEXTURE_2D);
  6796. glBindTexture(GL_TEXTURE_2D, 0);
  6797. glGenTextures(1, &resources.depth_tex);
  6798. unsigned char depthtexdata[8 * 8 * 2] = {};
  6799. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  6800. glBindTexture(GL_TEXTURE_2D, resources.depth_tex);
  6801. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, 8, 8, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, depthtexdata);
  6802. glBindTexture(GL_TEXTURE_2D, 0);
  6803. // Opaque white color for 3D texture.
  6804. glGenTextures(1, &resources.white_tex_3d);
  6805. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  6806. glBindTexture(GL_TEXTURE_3D, resources.white_tex_3d);
  6807. glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, 2, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, whitetexdata);
  6808. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_BASE_LEVEL, 0);
  6809. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 0);
  6810. // Opaque white color for texture array.
  6811. glGenTextures(1, &resources.white_tex_array);
  6812. gl_wrapper.gl_active_texture(GL_TEXTURE0);
  6813. glBindTexture(GL_TEXTURE_2D_ARRAY, resources.white_tex_array);
  6814. glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGB, 8, 8, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
  6815. glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, 8, 8, 1, GL_RGB, GL_UNSIGNED_BYTE, whitetexdata);
  6816. glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
  6817. glBindTexture(GL_TEXTURE_2D, 0);
  6818. }
  6819. config.max_texture_size = safe_gl_get_integer(GL_MAX_TEXTURE_SIZE);
  6820. config.max_cubemap_texture_size = safe_gl_get_integer(GL_MAX_CUBE_MAP_TEXTURE_SIZE);
  6821. config.use_rgba_2d_shadows = !config.framebuffer_float_supported;
  6822. //generic quadie for copying
  6823. {
  6824. //quad buffers
  6825. glGenBuffers(1, &resources.quadie);
  6826. glBindBuffer(GL_ARRAY_BUFFER, resources.quadie);
  6827. {
  6828. const float qv[16] = {
  6829. -1,
  6830. -1,
  6831. 0,
  6832. 0,
  6833. -1,
  6834. 1,
  6835. 0,
  6836. 1,
  6837. 1,
  6838. 1,
  6839. 1,
  6840. 1,
  6841. 1,
  6842. -1,
  6843. 1,
  6844. 0,
  6845. };
  6846. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 16, qv, GL_STATIC_DRAW);
  6847. }
  6848. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  6849. glGenVertexArrays(1, &resources.quadie_array);
  6850. glBindVertexArray(resources.quadie_array);
  6851. glBindBuffer(GL_ARRAY_BUFFER, resources.quadie);
  6852. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, nullptr);
  6853. glEnableVertexAttribArray(0);
  6854. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, CAST_INT_TO_UCHAR_PTR(8));
  6855. glEnableVertexAttribArray(4);
  6856. glBindVertexArray(0);
  6857. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  6858. }
  6859. //generic quadie for copying without touching sky
  6860. {
  6861. //transform feedback buffers
  6862. uint32_t xf_feedback_size = GLOBAL_DEF_RST("rendering/limits/buffers/blend_shape_max_buffer_size_kb", 4096);
  6863. ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/buffers/blend_shape_max_buffer_size_kb", PropertyInfo(Variant::INT, "rendering/limits/buffers/blend_shape_max_buffer_size_kb", PROPERTY_HINT_RANGE, "0,8192,1,or_greater"));
  6864. for (int i = 0; i < 2; i++) {
  6865. glGenBuffers(1, &resources.transform_feedback_buffers[i]);
  6866. glBindBuffer(GL_ARRAY_BUFFER, resources.transform_feedback_buffers[i]);
  6867. glBufferData(GL_ARRAY_BUFFER, xf_feedback_size * 1024, nullptr, GL_STREAM_DRAW);
  6868. }
  6869. shaders.blend_shapes.init();
  6870. glGenVertexArrays(1, &resources.transform_feedback_array);
  6871. }
  6872. shaders.cubemap_filter.init();
  6873. bool ggx_hq = GLOBAL_GET("rendering/quality/reflections/high_quality_ggx");
  6874. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::LOW_QUALITY, !ggx_hq);
  6875. shaders.particles.init();
  6876. if (config.async_compilation_enabled) {
  6877. shaders.particles.init_async_compilation();
  6878. }
  6879. #ifdef GLES_OVER_GL
  6880. glEnable(_EXT_TEXTURE_CUBE_MAP_SEAMLESS);
  6881. #endif
  6882. frame.count = 0;
  6883. frame.delta = 0;
  6884. frame.current_rt = nullptr;
  6885. config.keep_original_textures = false;
  6886. config.generate_wireframes = false;
  6887. config.use_texture_array_environment = GLOBAL_GET("rendering/quality/reflections/texture_array_reflections");
  6888. config.force_vertex_shading = GLOBAL_GET("rendering/quality/shading/force_vertex_shading");
  6889. String renderer = (const char *)glGetString(GL_RENDERER);
  6890. GLOBAL_DEF("rendering/quality/lightmapping/use_bicubic_sampling", true);
  6891. GLOBAL_DEF("rendering/quality/lightmapping/use_bicubic_sampling.mobile", false);
  6892. config.use_lightmap_filter_bicubic = GLOBAL_GET("rendering/quality/lightmapping/use_bicubic_sampling");
  6893. config.use_physical_light_attenuation = GLOBAL_GET("rendering/quality/shading/use_physical_light_attenuation");
  6894. config.use_depth_prepass = bool(GLOBAL_GET("rendering/quality/depth_prepass/enable"));
  6895. if (config.use_depth_prepass) {
  6896. String vendors = GLOBAL_GET("rendering/quality/depth_prepass/disable_for_vendors");
  6897. Vector<String> vendor_match = vendors.split(",");
  6898. for (int i = 0; i < vendor_match.size(); i++) {
  6899. String v = vendor_match[i].strip_edges();
  6900. if (v == String()) {
  6901. continue;
  6902. }
  6903. if (renderer.findn(v) != -1) {
  6904. config.use_depth_prepass = false;
  6905. }
  6906. }
  6907. }
  6908. int orphan_mode = GLOBAL_GET("rendering/2d/opengl/legacy_orphan_buffers");
  6909. switch (orphan_mode) {
  6910. default: {
  6911. config.should_orphan = true;
  6912. } break;
  6913. case 1: {
  6914. config.should_orphan = false;
  6915. } break;
  6916. case 2: {
  6917. config.should_orphan = true;
  6918. } break;
  6919. }
  6920. }
  6921. void RasterizerStorageGLES3::finalize() {
  6922. glDeleteTextures(1, &resources.white_tex);
  6923. glDeleteTextures(1, &resources.black_tex);
  6924. glDeleteTextures(1, &resources.transparent_tex);
  6925. glDeleteTextures(1, &resources.normal_tex);
  6926. glDeleteTextures(1, &resources.depth_tex);
  6927. }
  6928. void RasterizerStorageGLES3::update_dirty_resources() {
  6929. update_dirty_multimeshes();
  6930. update_dirty_skeletons();
  6931. update_dirty_shaders();
  6932. update_dirty_materials();
  6933. update_particles();
  6934. update_dirty_captures();
  6935. }
  6936. RasterizerStorageGLES3::RasterizerStorageGLES3() {
  6937. config.should_orphan = true;
  6938. }
  6939. RasterizerStorageGLES3::~RasterizerStorageGLES3() {
  6940. if (shaders.cache) {
  6941. memdelete(shaders.cache);
  6942. }
  6943. if (shaders.cache_write_queue) {
  6944. memdelete(shaders.cache_write_queue);
  6945. }
  6946. if (shaders.compile_queue) {
  6947. shaders.compile_queue->enqueue(0, []() { OS::get_singleton()->set_offscreen_gl_current(false); });
  6948. memdelete(shaders.compile_queue);
  6949. }
  6950. }