rasterizer_storage_gles2.cpp 210 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623
  1. /**************************************************************************/
  2. /* rasterizer_storage_gles2.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_gles2.h"
  31. #include "core/math/transform.h"
  32. #include "core/project_settings.h"
  33. #include "rasterizer_canvas_gles2.h"
  34. #include "rasterizer_scene_gles2.h"
  35. #include "servers/visual/shader_language.h"
  36. GLuint RasterizerStorageGLES2::system_fbo = 0;
  37. /* TEXTURE API */
  38. #define _EXT_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
  39. #define _EXT_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
  40. #define _EXT_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
  41. #define _EXT_COMPRESSED_RED_RGTC1_EXT 0x8DBB
  42. #define _EXT_COMPRESSED_RED_RGTC1 0x8DBB
  43. #define _EXT_COMPRESSED_SIGNED_RED_RGTC1 0x8DBC
  44. #define _EXT_COMPRESSED_RG_RGTC2 0x8DBD
  45. #define _EXT_COMPRESSED_SIGNED_RG_RGTC2 0x8DBE
  46. #define _EXT_COMPRESSED_SIGNED_RED_RGTC1_EXT 0x8DBC
  47. #define _EXT_COMPRESSED_RED_GREEN_RGTC2_EXT 0x8DBD
  48. #define _EXT_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT 0x8DBE
  49. #define _EXT_ETC1_RGB8_OES 0x8D64
  50. #define _EXT_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
  51. #define _EXT_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
  52. #define _EXT_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
  53. #define _EXT_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
  54. #define _EXT_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT 0x8A54
  55. #define _EXT_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT 0x8A55
  56. #define _EXT_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT 0x8A56
  57. #define _EXT_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT 0x8A57
  58. #define _EXT_COMPRESSED_RGBA_BPTC_UNORM 0x8E8C
  59. #define _EXT_COMPRESSED_SRGB_ALPHA_BPTC_UNORM 0x8E8D
  60. #define _EXT_COMPRESSED_RGB_BPTC_SIGNED_FLOAT 0x8E8E
  61. #define _EXT_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT 0x8E8F
  62. #define _GL_TEXTURE_EXTERNAL_OES 0x8D65
  63. #ifdef GLES_OVER_GL
  64. #define _GL_HALF_FLOAT_OES 0x140B
  65. #else
  66. #define _GL_HALF_FLOAT_OES 0x8D61
  67. #endif
  68. #define _EXT_TEXTURE_CUBE_MAP_SEAMLESS 0x884F
  69. #define _GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
  70. #define _GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
  71. #define _RED_OES 0x1903
  72. #define _DEPTH_COMPONENT24_OES 0x81A6
  73. #ifndef GLES_OVER_GL
  74. #define glClearDepth glClearDepthf
  75. // enable extensions manually for android and ios
  76. #ifndef UWP_ENABLED
  77. #include <dlfcn.h> // needed to load extensions
  78. #endif
  79. #ifdef IPHONE_ENABLED
  80. #include <OpenGLES/ES2/glext.h>
  81. //void *glRenderbufferStorageMultisampleAPPLE;
  82. //void *glResolveMultisampleFramebufferAPPLE;
  83. #define glRenderbufferStorageMultisample glRenderbufferStorageMultisampleAPPLE
  84. #elif defined(ANDROID_ENABLED)
  85. #include <GLES2/gl2ext.h>
  86. PFNGLRENDERBUFFERSTORAGEMULTISAMPLEEXTPROC glRenderbufferStorageMultisampleEXT;
  87. PFNGLFRAMEBUFFERTEXTURE2DMULTISAMPLEEXTPROC glFramebufferTexture2DMultisampleEXT;
  88. #define glRenderbufferStorageMultisample glRenderbufferStorageMultisampleEXT
  89. #define glFramebufferTexture2DMultisample glFramebufferTexture2DMultisampleEXT
  90. #elif defined(UWP_ENABLED)
  91. #include <GLES2/gl2ext.h>
  92. #define glRenderbufferStorageMultisample glRenderbufferStorageMultisampleANGLE
  93. #define glFramebufferTexture2DMultisample glFramebufferTexture2DMultisampleANGLE
  94. #endif
  95. #define GL_TEXTURE_3D 0x806F
  96. #define GL_MAX_SAMPLES 0x8D57
  97. #endif //!GLES_OVER_GL
  98. void RasterizerStorageGLES2::bind_quad_array() const {
  99. glBindBuffer(GL_ARRAY_BUFFER, resources.quadie);
  100. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, nullptr);
  101. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, CAST_INT_TO_UCHAR_PTR(8));
  102. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  103. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  104. }
  105. Ref<Image> RasterizerStorageGLES2::_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 p_force_decompress) const {
  106. r_gl_format = 0;
  107. Ref<Image> image = p_image;
  108. r_compressed = false;
  109. r_real_format = p_format;
  110. bool need_decompress = false;
  111. switch (p_format) {
  112. case Image::FORMAT_L8: {
  113. r_gl_internal_format = GL_LUMINANCE;
  114. r_gl_format = GL_LUMINANCE;
  115. r_gl_type = GL_UNSIGNED_BYTE;
  116. } break;
  117. case Image::FORMAT_LA8: {
  118. r_gl_internal_format = GL_LUMINANCE_ALPHA;
  119. r_gl_format = GL_LUMINANCE_ALPHA;
  120. r_gl_type = GL_UNSIGNED_BYTE;
  121. } break;
  122. case Image::FORMAT_R8: {
  123. r_gl_internal_format = GL_ALPHA;
  124. r_gl_format = GL_ALPHA;
  125. r_gl_type = GL_UNSIGNED_BYTE;
  126. } break;
  127. case Image::FORMAT_RG8: {
  128. ERR_PRINT("RG texture not supported, converting to RGB8.");
  129. if (image.is_valid()) {
  130. image->convert(Image::FORMAT_RGB8);
  131. }
  132. r_real_format = Image::FORMAT_RGB8;
  133. r_gl_internal_format = GL_RGB;
  134. r_gl_format = GL_RGB;
  135. r_gl_type = GL_UNSIGNED_BYTE;
  136. } break;
  137. case Image::FORMAT_RGB8: {
  138. r_gl_internal_format = GL_RGB;
  139. r_gl_format = GL_RGB;
  140. r_gl_type = GL_UNSIGNED_BYTE;
  141. } break;
  142. case Image::FORMAT_RGBA8: {
  143. r_gl_format = GL_RGBA;
  144. r_gl_internal_format = GL_RGBA;
  145. r_gl_type = GL_UNSIGNED_BYTE;
  146. } break;
  147. case Image::FORMAT_RGBA4444: {
  148. r_gl_internal_format = GL_RGBA;
  149. r_gl_format = GL_RGBA;
  150. r_gl_type = GL_UNSIGNED_SHORT_4_4_4_4;
  151. } break;
  152. case Image::FORMAT_RGBA5551: {
  153. r_gl_internal_format = GL_RGB5_A1;
  154. r_gl_format = GL_RGBA;
  155. r_gl_type = GL_UNSIGNED_SHORT_5_5_5_1;
  156. } break;
  157. case Image::FORMAT_RF: {
  158. if (!config.float_texture_supported) {
  159. ERR_PRINT("R float texture not supported, converting to RGB8.");
  160. if (image.is_valid()) {
  161. image->convert(Image::FORMAT_RGB8);
  162. }
  163. r_real_format = Image::FORMAT_RGB8;
  164. r_gl_internal_format = GL_RGB;
  165. r_gl_format = GL_RGB;
  166. r_gl_type = GL_UNSIGNED_BYTE;
  167. } else {
  168. r_gl_internal_format = GL_ALPHA;
  169. r_gl_format = GL_ALPHA;
  170. r_gl_type = GL_FLOAT;
  171. }
  172. } break;
  173. case Image::FORMAT_RGF: {
  174. ERR_PRINT("RG float texture not supported, converting to RGB8.");
  175. if (image.is_valid()) {
  176. image->convert(Image::FORMAT_RGB8);
  177. }
  178. r_real_format = Image::FORMAT_RGB8;
  179. r_gl_internal_format = GL_RGB;
  180. r_gl_format = GL_RGB;
  181. r_gl_type = GL_UNSIGNED_BYTE;
  182. } break;
  183. case Image::FORMAT_RGBF: {
  184. if (!config.float_texture_supported) {
  185. ERR_PRINT("RGB float texture not supported, converting to RGB8.");
  186. if (image.is_valid()) {
  187. image->convert(Image::FORMAT_RGB8);
  188. }
  189. r_real_format = Image::FORMAT_RGB8;
  190. r_gl_internal_format = GL_RGB;
  191. r_gl_format = GL_RGB;
  192. r_gl_type = GL_UNSIGNED_BYTE;
  193. } else {
  194. r_gl_internal_format = GL_RGB;
  195. r_gl_format = GL_RGB;
  196. r_gl_type = GL_FLOAT;
  197. }
  198. } break;
  199. case Image::FORMAT_RGBAF: {
  200. if (!config.float_texture_supported) {
  201. ERR_PRINT("RGBA float texture not supported, converting to RGBA8.");
  202. if (image.is_valid()) {
  203. image->convert(Image::FORMAT_RGBA8);
  204. }
  205. r_real_format = Image::FORMAT_RGBA8;
  206. r_gl_internal_format = GL_RGBA;
  207. r_gl_format = GL_RGBA;
  208. r_gl_type = GL_UNSIGNED_BYTE;
  209. } else {
  210. r_gl_internal_format = GL_RGBA;
  211. r_gl_format = GL_RGBA;
  212. r_gl_type = GL_FLOAT;
  213. }
  214. } break;
  215. case Image::FORMAT_RH: {
  216. need_decompress = true;
  217. } break;
  218. case Image::FORMAT_RGH: {
  219. need_decompress = true;
  220. } break;
  221. case Image::FORMAT_RGBH: {
  222. need_decompress = true;
  223. } break;
  224. case Image::FORMAT_RGBAH: {
  225. need_decompress = true;
  226. } break;
  227. case Image::FORMAT_RGBE9995: {
  228. r_gl_internal_format = GL_RGB;
  229. r_gl_format = GL_RGB;
  230. r_gl_type = GL_UNSIGNED_BYTE;
  231. if (image.is_valid()) {
  232. image = image->rgbe_to_srgb();
  233. }
  234. return image;
  235. } break;
  236. case Image::FORMAT_DXT1: {
  237. if (config.s3tc_supported) {
  238. r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  239. r_gl_format = GL_RGBA;
  240. r_gl_type = GL_UNSIGNED_BYTE;
  241. r_compressed = true;
  242. } else {
  243. need_decompress = true;
  244. }
  245. } break;
  246. case Image::FORMAT_DXT3: {
  247. if (config.s3tc_supported) {
  248. r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  249. r_gl_format = GL_RGBA;
  250. r_gl_type = GL_UNSIGNED_BYTE;
  251. r_compressed = true;
  252. } else {
  253. need_decompress = true;
  254. }
  255. } break;
  256. case Image::FORMAT_DXT5: {
  257. if (config.s3tc_supported) {
  258. r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  259. r_gl_format = GL_RGBA;
  260. r_gl_type = GL_UNSIGNED_BYTE;
  261. r_compressed = true;
  262. } else {
  263. need_decompress = true;
  264. }
  265. } break;
  266. case Image::FORMAT_RGTC_R: {
  267. if (config.rgtc_supported) {
  268. r_gl_internal_format = _EXT_COMPRESSED_RED_RGTC1_EXT;
  269. r_gl_format = GL_RGBA;
  270. r_gl_type = GL_UNSIGNED_BYTE;
  271. r_compressed = true;
  272. } else {
  273. need_decompress = true;
  274. }
  275. } break;
  276. case Image::FORMAT_RGTC_RG: {
  277. if (config.rgtc_supported) {
  278. r_gl_internal_format = _EXT_COMPRESSED_RED_GREEN_RGTC2_EXT;
  279. r_gl_format = GL_RGBA;
  280. r_gl_type = GL_UNSIGNED_BYTE;
  281. r_compressed = true;
  282. } else {
  283. need_decompress = true;
  284. }
  285. } break;
  286. case Image::FORMAT_BPTC_RGBA: {
  287. if (config.bptc_supported) {
  288. r_gl_internal_format = _EXT_COMPRESSED_RGBA_BPTC_UNORM;
  289. r_gl_format = GL_RGBA;
  290. r_gl_type = GL_UNSIGNED_BYTE;
  291. r_compressed = true;
  292. } else {
  293. need_decompress = true;
  294. }
  295. } break;
  296. case Image::FORMAT_BPTC_RGBF: {
  297. if (config.bptc_supported) {
  298. r_gl_internal_format = _EXT_COMPRESSED_RGB_BPTC_SIGNED_FLOAT;
  299. r_gl_format = GL_RGB;
  300. r_gl_type = GL_FLOAT;
  301. r_compressed = true;
  302. } else {
  303. need_decompress = true;
  304. }
  305. } break;
  306. case Image::FORMAT_BPTC_RGBFU: {
  307. if (config.bptc_supported) {
  308. r_gl_internal_format = _EXT_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT;
  309. r_gl_format = GL_RGB;
  310. r_gl_type = GL_FLOAT;
  311. r_compressed = true;
  312. } else {
  313. need_decompress = true;
  314. }
  315. } break;
  316. case Image::FORMAT_PVRTC2: {
  317. if (config.pvrtc_supported) {
  318. r_gl_internal_format = _EXT_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  319. r_gl_format = GL_RGBA;
  320. r_gl_type = GL_UNSIGNED_BYTE;
  321. r_compressed = true;
  322. } else {
  323. need_decompress = true;
  324. }
  325. } break;
  326. case Image::FORMAT_PVRTC2A: {
  327. if (config.pvrtc_supported) {
  328. r_gl_internal_format = _EXT_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  329. r_gl_format = GL_RGBA;
  330. r_gl_type = GL_UNSIGNED_BYTE;
  331. r_compressed = true;
  332. } else {
  333. need_decompress = true;
  334. }
  335. } break;
  336. case Image::FORMAT_PVRTC4: {
  337. if (config.pvrtc_supported) {
  338. r_gl_internal_format = _EXT_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
  339. r_gl_format = GL_RGBA;
  340. r_gl_type = GL_UNSIGNED_BYTE;
  341. r_compressed = true;
  342. } else {
  343. need_decompress = true;
  344. }
  345. } break;
  346. case Image::FORMAT_PVRTC4A: {
  347. if (config.pvrtc_supported) {
  348. r_gl_internal_format = _EXT_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
  349. r_gl_format = GL_RGBA;
  350. r_gl_type = GL_UNSIGNED_BYTE;
  351. r_compressed = true;
  352. } else {
  353. need_decompress = true;
  354. }
  355. } break;
  356. case Image::FORMAT_ETC: {
  357. if (config.etc1_supported) {
  358. r_gl_internal_format = _EXT_ETC1_RGB8_OES;
  359. r_gl_format = GL_RGBA;
  360. r_gl_type = GL_UNSIGNED_BYTE;
  361. r_compressed = true;
  362. } else {
  363. need_decompress = true;
  364. }
  365. } break;
  366. case Image::FORMAT_ETC2_R11: {
  367. need_decompress = true;
  368. } break;
  369. case Image::FORMAT_ETC2_R11S: {
  370. need_decompress = true;
  371. } break;
  372. case Image::FORMAT_ETC2_RG11: {
  373. need_decompress = true;
  374. } break;
  375. case Image::FORMAT_ETC2_RG11S: {
  376. need_decompress = true;
  377. } break;
  378. case Image::FORMAT_ETC2_RGB8: {
  379. need_decompress = true;
  380. } break;
  381. case Image::FORMAT_ETC2_RGBA8: {
  382. need_decompress = true;
  383. } break;
  384. case Image::FORMAT_ETC2_RGB8A1: {
  385. need_decompress = true;
  386. } break;
  387. default: {
  388. ERR_FAIL_V(Ref<Image>());
  389. }
  390. }
  391. if (need_decompress || p_force_decompress) {
  392. if (!image.is_null()) {
  393. image = image->duplicate();
  394. image->decompress();
  395. ERR_FAIL_COND_V(image->is_compressed(), image);
  396. switch (image->get_format()) {
  397. case Image::FORMAT_RGB8: {
  398. r_gl_format = GL_RGB;
  399. r_gl_internal_format = GL_RGB;
  400. r_gl_type = GL_UNSIGNED_BYTE;
  401. r_real_format = Image::FORMAT_RGB8;
  402. r_compressed = false;
  403. } break;
  404. case Image::FORMAT_RGBA8: {
  405. r_gl_format = GL_RGBA;
  406. r_gl_internal_format = GL_RGBA;
  407. r_gl_type = GL_UNSIGNED_BYTE;
  408. r_real_format = Image::FORMAT_RGBA8;
  409. r_compressed = false;
  410. } break;
  411. default: {
  412. image->convert(Image::FORMAT_RGBA8);
  413. r_gl_format = GL_RGBA;
  414. r_gl_internal_format = GL_RGBA;
  415. r_gl_type = GL_UNSIGNED_BYTE;
  416. r_real_format = Image::FORMAT_RGBA8;
  417. r_compressed = false;
  418. } break;
  419. }
  420. }
  421. return image;
  422. }
  423. return p_image;
  424. }
  425. static const GLenum _cube_side_enum[6] = {
  426. GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
  427. GL_TEXTURE_CUBE_MAP_POSITIVE_X,
  428. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
  429. GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
  430. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
  431. GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
  432. };
  433. RID RasterizerStorageGLES2::texture_create() {
  434. Texture *texture = memnew(Texture);
  435. ERR_FAIL_COND_V(!texture, RID());
  436. glGenTextures(1, &texture->tex_id);
  437. texture->active = false;
  438. texture->total_data_size = 0;
  439. return texture_owner.make_rid(texture);
  440. }
  441. void RasterizerStorageGLES2::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) {
  442. GLenum format;
  443. GLenum internal_format;
  444. GLenum type;
  445. bool compressed = false;
  446. if (p_flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
  447. p_flags &= ~VS::TEXTURE_FLAG_MIPMAPS; // no mipies for video
  448. }
  449. Texture *texture = texture_owner.getornull(p_texture);
  450. ERR_FAIL_COND(!texture);
  451. texture->width = p_width;
  452. texture->height = p_height;
  453. texture->format = p_format;
  454. if (texture->width > config.max_texture_size || texture->height > config.max_texture_size) {
  455. WARN_PRINT("Cannot create texture larger than maximum hardware supported size of " + itos(config.max_texture_size) + ". Setting size to maximum.");
  456. texture->width = MIN(texture->width, config.max_texture_size);
  457. texture->height = MIN(texture->height, config.max_texture_size);
  458. }
  459. texture->flags = p_flags;
  460. texture->stored_cube_sides = 0;
  461. texture->type = p_type;
  462. switch (p_type) {
  463. case VS::TEXTURE_TYPE_2D: {
  464. texture->target = GL_TEXTURE_2D;
  465. texture->images.resize(1);
  466. } break;
  467. case VS::TEXTURE_TYPE_EXTERNAL: {
  468. #ifdef ANDROID_ENABLED
  469. texture->target = _GL_TEXTURE_EXTERNAL_OES;
  470. #else
  471. texture->target = GL_TEXTURE_2D;
  472. #endif
  473. texture->images.resize(0);
  474. } break;
  475. case VS::TEXTURE_TYPE_CUBEMAP: {
  476. texture->target = GL_TEXTURE_CUBE_MAP;
  477. texture->images.resize(6);
  478. } break;
  479. case VS::TEXTURE_TYPE_2D_ARRAY:
  480. case VS::TEXTURE_TYPE_3D: {
  481. texture->target = GL_TEXTURE_3D;
  482. ERR_PRINT("3D textures and Texture Arrays are not supported in GLES2. Please switch to the GLES3 backend.");
  483. return;
  484. } break;
  485. default: {
  486. ERR_PRINT("Unknown texture type!");
  487. return;
  488. }
  489. }
  490. if (p_type != VS::TEXTURE_TYPE_EXTERNAL) {
  491. texture->alloc_width = texture->width;
  492. texture->alloc_height = texture->height;
  493. texture->resize_to_po2 = false;
  494. if (!config.support_npot_repeat_mipmap) {
  495. int po2_width = next_power_of_2(p_width);
  496. int po2_height = next_power_of_2(p_height);
  497. bool is_po2 = p_width == po2_width && p_height == po2_height;
  498. if (!is_po2 && (p_flags & VS::TEXTURE_FLAG_REPEAT || p_flags & VS::TEXTURE_FLAG_MIPMAPS)) {
  499. if (p_flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
  500. //not supported
  501. ERR_PRINT("Streaming texture for non power of 2 or has mipmaps on this hardware: " + texture->path + "'. Mipmaps and repeat disabled.");
  502. texture->flags &= ~(VS::TEXTURE_FLAG_REPEAT | VS::TEXTURE_FLAG_MIPMAPS);
  503. } else {
  504. texture->alloc_height = po2_height;
  505. texture->alloc_width = po2_width;
  506. texture->resize_to_po2 = true;
  507. }
  508. }
  509. }
  510. Image::Format real_format;
  511. _get_gl_image_and_format(Ref<Image>(),
  512. texture->format,
  513. texture->flags,
  514. real_format,
  515. format,
  516. internal_format,
  517. type,
  518. compressed,
  519. texture->resize_to_po2);
  520. texture->gl_format_cache = format;
  521. texture->gl_type_cache = type;
  522. texture->gl_internal_format_cache = internal_format;
  523. texture->data_size = 0;
  524. texture->mipmaps = 1;
  525. texture->compressed = compressed;
  526. }
  527. glActiveTexture(GL_TEXTURE0);
  528. glBindTexture(texture->target, texture->tex_id);
  529. if (p_type == VS::TEXTURE_TYPE_EXTERNAL) {
  530. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  531. glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  532. glTexParameteri(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  533. glTexParameteri(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  534. } else if (p_flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
  535. //prealloc if video
  536. glTexImage2D(texture->target, 0, internal_format, texture->alloc_width, texture->alloc_height, 0, format, type, nullptr);
  537. }
  538. texture->active = true;
  539. }
  540. void RasterizerStorageGLES2::texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer) {
  541. Texture *texture = texture_owner.getornull(p_texture);
  542. ERR_FAIL_COND(!texture);
  543. if (texture->target == GL_TEXTURE_3D) {
  544. // Target is set to a 3D texture or array texture, exit early to avoid spamming errors
  545. return;
  546. }
  547. ERR_FAIL_COND(!texture->active);
  548. ERR_FAIL_COND(texture->render_target);
  549. ERR_FAIL_COND(texture->format != p_image->get_format());
  550. ERR_FAIL_COND(p_image.is_null());
  551. ERR_FAIL_COND(texture->type == VS::TEXTURE_TYPE_EXTERNAL);
  552. GLenum type;
  553. GLenum format;
  554. GLenum internal_format;
  555. bool compressed = false;
  556. if (config.keep_original_textures && !(texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING)) {
  557. texture->images.write[p_layer] = p_image;
  558. }
  559. Image::Format real_format;
  560. Ref<Image> img = _get_gl_image_and_format(p_image, p_image->get_format(), texture->flags, real_format, format, internal_format, type, compressed, texture->resize_to_po2);
  561. if (texture->resize_to_po2) {
  562. if (p_image->is_compressed()) {
  563. ERR_PRINT("Texture '" + texture->path + "' is required to be a power of 2 because it uses either mipmaps or repeat, so it was decompressed. This will hurt performance and memory usage.");
  564. }
  565. if (img == p_image) {
  566. img = img->duplicate();
  567. }
  568. img->resize_to_po2(false, texture->flags & VS::TEXTURE_FLAG_FILTER ? Image::INTERPOLATE_BILINEAR : Image::INTERPOLATE_NEAREST);
  569. }
  570. if (config.shrink_textures_x2 && (p_image->has_mipmaps() || !p_image->is_compressed()) && !(texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING)) {
  571. texture->alloc_height = MAX(1, texture->alloc_height / 2);
  572. texture->alloc_width = MAX(1, texture->alloc_width / 2);
  573. if (texture->alloc_width == img->get_width() / 2 && texture->alloc_height == img->get_height() / 2) {
  574. img->shrink_x2();
  575. } else if (img->get_format() <= Image::FORMAT_RGBA8) {
  576. img->resize(texture->alloc_width, texture->alloc_height, Image::INTERPOLATE_BILINEAR);
  577. }
  578. }
  579. GLenum blit_target = (texture->target == GL_TEXTURE_CUBE_MAP) ? _cube_side_enum[p_layer] : GL_TEXTURE_2D;
  580. texture->data_size = img->get_data().size();
  581. PoolVector<uint8_t>::Read read = img->get_data().read();
  582. ERR_FAIL_COND(!read.ptr());
  583. glActiveTexture(GL_TEXTURE0);
  584. glBindTexture(texture->target, texture->tex_id);
  585. texture->ignore_mipmaps = compressed && !img->has_mipmaps();
  586. if ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && !texture->ignore_mipmaps) {
  587. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  588. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, config.use_fast_texture_filter ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR_MIPMAP_LINEAR);
  589. } else {
  590. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, config.use_fast_texture_filter ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST_MIPMAP_LINEAR);
  591. }
  592. } else {
  593. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  594. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  595. } else {
  596. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  597. }
  598. }
  599. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  600. glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtering
  601. } else {
  602. glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // raw Filtering
  603. }
  604. if (((texture->flags & VS::TEXTURE_FLAG_REPEAT) || (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT)) && texture->target != GL_TEXTURE_CUBE_MAP) {
  605. if (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT) {
  606. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
  607. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
  608. } else {
  609. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  610. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  611. }
  612. } else {
  613. //glTexParameterf( texture->target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
  614. glTexParameterf(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  615. glTexParameterf(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  616. }
  617. if (config.use_anisotropic_filter) {
  618. if (texture->flags & VS::TEXTURE_FLAG_ANISOTROPIC_FILTER) {
  619. glTexParameterf(texture->target, _GL_TEXTURE_MAX_ANISOTROPY_EXT, config.anisotropic_level);
  620. } else {
  621. glTexParameterf(texture->target, _GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
  622. }
  623. }
  624. int mipmaps = ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && img->has_mipmaps()) ? img->get_mipmap_count() + 1 : 1;
  625. int w = img->get_width();
  626. int h = img->get_height();
  627. int tsize = 0;
  628. for (int i = 0; i < mipmaps; i++) {
  629. int size, ofs;
  630. img->get_mipmap_offset_and_size(i, ofs, size);
  631. if (compressed) {
  632. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  633. int bw = w;
  634. int bh = h;
  635. glCompressedTexImage2D(blit_target, i, internal_format, bw, bh, 0, size, &read[ofs]);
  636. } else {
  637. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  638. if (texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
  639. glTexSubImage2D(blit_target, i, 0, 0, w, h, format, type, &read[ofs]);
  640. } else {
  641. glTexImage2D(blit_target, i, internal_format, w, h, 0, format, type, &read[ofs]);
  642. }
  643. }
  644. tsize += size;
  645. w = MAX(1, w >> 1);
  646. h = MAX(1, h >> 1);
  647. }
  648. info.texture_mem -= texture->total_data_size;
  649. texture->total_data_size = tsize;
  650. info.texture_mem += texture->total_data_size;
  651. // printf("texture: %i x %i - size: %i - total: %i\n", texture->width, texture->height, tsize, info.texture_mem);
  652. texture->stored_cube_sides |= (1 << p_layer);
  653. if ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && mipmaps == 1 && !texture->ignore_mipmaps && (texture->type != VS::TEXTURE_TYPE_CUBEMAP || texture->stored_cube_sides == (1 << 6) - 1)) {
  654. //generate mipmaps if they were requested and the image does not contain them
  655. glGenerateMipmap(texture->target);
  656. }
  657. texture->mipmaps = mipmaps;
  658. }
  659. void RasterizerStorageGLES2::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) {
  660. // TODO
  661. ERR_PRINT("Not implemented (ask Karroffel to do it :p)");
  662. }
  663. Ref<Image> RasterizerStorageGLES2::texture_get_data(RID p_texture, int p_layer) const {
  664. Texture *texture = texture_owner.getornull(p_texture);
  665. ERR_FAIL_COND_V(!texture, Ref<Image>());
  666. ERR_FAIL_COND_V(!texture->active, Ref<Image>());
  667. ERR_FAIL_COND_V(texture->data_size == 0 && !texture->render_target, Ref<Image>());
  668. if (texture->type == VS::TEXTURE_TYPE_CUBEMAP && p_layer < 6 && p_layer >= 0 && !texture->images[p_layer].is_null()) {
  669. return texture->images[p_layer];
  670. }
  671. #ifdef GLES_OVER_GL
  672. Image::Format real_format;
  673. GLenum gl_format;
  674. GLenum gl_internal_format;
  675. GLenum gl_type;
  676. bool compressed;
  677. _get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, real_format, gl_format, gl_internal_format, gl_type, compressed, false);
  678. PoolVector<uint8_t> data;
  679. int data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, real_format, texture->mipmaps > 1);
  680. data.resize(data_size * 2); //add some memory at the end, just in case for buggy drivers
  681. PoolVector<uint8_t>::Write wb = data.write();
  682. glActiveTexture(GL_TEXTURE0);
  683. glBindTexture(texture->target, texture->tex_id);
  684. glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
  685. for (int i = 0; i < texture->mipmaps; i++) {
  686. int ofs = Image::get_image_mipmap_offset(texture->alloc_width, texture->alloc_height, real_format, i);
  687. if (texture->compressed) {
  688. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  689. glGetCompressedTexImage(texture->target, i, &wb[ofs]);
  690. } else {
  691. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  692. glGetTexImage(texture->target, i, texture->gl_format_cache, texture->gl_type_cache, &wb[ofs]);
  693. }
  694. }
  695. wb.release();
  696. data.resize(data_size);
  697. Image *img = memnew(Image(texture->alloc_width, texture->alloc_height, texture->mipmaps > 1, real_format, data));
  698. return Ref<Image>(img);
  699. #else
  700. Image::Format real_format;
  701. GLenum gl_format;
  702. GLenum gl_internal_format;
  703. GLenum gl_type;
  704. bool compressed;
  705. _get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, real_format, gl_format, gl_internal_format, gl_type, compressed, texture->resize_to_po2);
  706. PoolVector<uint8_t> data;
  707. int data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, Image::FORMAT_RGBA8, false);
  708. data.resize(data_size * 2); //add some memory at the end, just in case for buggy drivers
  709. PoolVector<uint8_t>::Write wb = data.write();
  710. GLuint temp_framebuffer;
  711. glGenFramebuffers(1, &temp_framebuffer);
  712. GLuint temp_color_texture;
  713. glGenTextures(1, &temp_color_texture);
  714. glBindFramebuffer(GL_FRAMEBUFFER, temp_framebuffer);
  715. glBindTexture(GL_TEXTURE_2D, temp_color_texture);
  716. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->alloc_width, texture->alloc_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  717. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  718. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  719. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, temp_color_texture, 0);
  720. glDepthMask(GL_FALSE);
  721. glDisable(GL_DEPTH_TEST);
  722. glDisable(GL_CULL_FACE);
  723. glDisable(GL_BLEND);
  724. glDepthFunc(GL_LEQUAL);
  725. glColorMask(1, 1, 1, 1);
  726. glActiveTexture(GL_TEXTURE0);
  727. glBindTexture(GL_TEXTURE_2D, texture->tex_id);
  728. glViewport(0, 0, texture->alloc_width, texture->alloc_height);
  729. shaders.copy.bind();
  730. glClearColor(0.0, 0.0, 0.0, 0.0);
  731. glClear(GL_COLOR_BUFFER_BIT);
  732. bind_quad_array();
  733. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  734. glBindBuffer(GL_ARRAY_BUFFER, 0);
  735. glReadPixels(0, 0, texture->alloc_width, texture->alloc_height, GL_RGBA, GL_UNSIGNED_BYTE, &wb[0]);
  736. glDeleteTextures(1, &temp_color_texture);
  737. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  738. glDeleteFramebuffers(1, &temp_framebuffer);
  739. wb.release();
  740. data.resize(data_size);
  741. Image *img = memnew(Image(texture->alloc_width, texture->alloc_height, false, Image::FORMAT_RGBA8, data));
  742. if (!texture->compressed) {
  743. img->convert(real_format);
  744. }
  745. return Ref<Image>(img);
  746. #endif
  747. }
  748. void RasterizerStorageGLES2::texture_set_flags(RID p_texture, uint32_t p_flags) {
  749. Texture *texture = texture_owner.getornull(p_texture);
  750. ERR_FAIL_COND(!texture);
  751. bool had_mipmaps = texture->flags & VS::TEXTURE_FLAG_MIPMAPS;
  752. texture->flags = p_flags;
  753. glActiveTexture(GL_TEXTURE0);
  754. glBindTexture(texture->target, texture->tex_id);
  755. if (((texture->flags & VS::TEXTURE_FLAG_REPEAT) || (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT)) && texture->target != GL_TEXTURE_CUBE_MAP) {
  756. if (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT) {
  757. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
  758. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
  759. } else {
  760. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  761. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  762. }
  763. } else {
  764. //glTexParameterf( texture->target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
  765. glTexParameterf(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  766. glTexParameterf(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  767. }
  768. if (config.use_anisotropic_filter) {
  769. if (texture->flags & VS::TEXTURE_FLAG_ANISOTROPIC_FILTER) {
  770. glTexParameterf(texture->target, _GL_TEXTURE_MAX_ANISOTROPY_EXT, config.anisotropic_level);
  771. } else {
  772. glTexParameterf(texture->target, _GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
  773. }
  774. }
  775. if ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && !texture->ignore_mipmaps) {
  776. if (!had_mipmaps && texture->mipmaps == 1) {
  777. glGenerateMipmap(texture->target);
  778. }
  779. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  780. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, config.use_fast_texture_filter ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR_MIPMAP_LINEAR);
  781. } else {
  782. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, config.use_fast_texture_filter ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST_MIPMAP_LINEAR);
  783. }
  784. } else {
  785. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  786. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  787. } else {
  788. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  789. }
  790. }
  791. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  792. glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtering
  793. } else {
  794. glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // raw Filtering
  795. }
  796. }
  797. uint32_t RasterizerStorageGLES2::texture_get_flags(RID p_texture) const {
  798. Texture *texture = texture_owner.getornull(p_texture);
  799. ERR_FAIL_COND_V(!texture, 0);
  800. return texture->flags;
  801. }
  802. Image::Format RasterizerStorageGLES2::texture_get_format(RID p_texture) const {
  803. Texture *texture = texture_owner.getornull(p_texture);
  804. ERR_FAIL_COND_V(!texture, Image::FORMAT_L8);
  805. return texture->format;
  806. }
  807. VisualServer::TextureType RasterizerStorageGLES2::texture_get_type(RID p_texture) const {
  808. Texture *texture = texture_owner.getornull(p_texture);
  809. ERR_FAIL_COND_V(!texture, VS::TEXTURE_TYPE_2D);
  810. return texture->type;
  811. }
  812. uint32_t RasterizerStorageGLES2::texture_get_texid(RID p_texture) const {
  813. Texture *texture = texture_owner.getornull(p_texture);
  814. ERR_FAIL_COND_V(!texture, 0);
  815. return texture->tex_id;
  816. }
  817. void RasterizerStorageGLES2::texture_bind(RID p_texture, uint32_t p_texture_no) {
  818. Texture *texture = texture_owner.getornull(p_texture);
  819. ERR_FAIL_COND(!texture);
  820. glActiveTexture(GL_TEXTURE0 + p_texture_no);
  821. glBindTexture(texture->target, texture->tex_id);
  822. }
  823. uint32_t RasterizerStorageGLES2::texture_get_width(RID p_texture) const {
  824. Texture *texture = texture_owner.getornull(p_texture);
  825. ERR_FAIL_COND_V(!texture, 0);
  826. return texture->width;
  827. }
  828. uint32_t RasterizerStorageGLES2::texture_get_height(RID p_texture) const {
  829. Texture *texture = texture_owner.getornull(p_texture);
  830. ERR_FAIL_COND_V(!texture, 0);
  831. return texture->height;
  832. }
  833. uint32_t RasterizerStorageGLES2::texture_get_depth(RID p_texture) const {
  834. Texture *texture = texture_owner.getornull(p_texture);
  835. ERR_FAIL_COND_V(!texture, 0);
  836. return texture->depth;
  837. }
  838. void RasterizerStorageGLES2::texture_set_size_override(RID p_texture, int p_width, int p_height, int p_depth) {
  839. Texture *texture = texture_owner.getornull(p_texture);
  840. ERR_FAIL_COND(!texture);
  841. ERR_FAIL_COND(texture->render_target);
  842. ERR_FAIL_COND(p_width <= 0 || p_width > 16384);
  843. ERR_FAIL_COND(p_height <= 0 || p_height > 16384);
  844. //real texture size is in alloc width and height
  845. texture->width = p_width;
  846. texture->height = p_height;
  847. }
  848. void RasterizerStorageGLES2::texture_set_path(RID p_texture, const String &p_path) {
  849. Texture *texture = texture_owner.getornull(p_texture);
  850. ERR_FAIL_COND(!texture);
  851. texture->path = p_path;
  852. }
  853. String RasterizerStorageGLES2::texture_get_path(RID p_texture) const {
  854. Texture *texture = texture_owner.getornull(p_texture);
  855. ERR_FAIL_COND_V(!texture, "");
  856. return texture->path;
  857. }
  858. void RasterizerStorageGLES2::texture_debug_usage(List<VS::TextureInfo> *r_info) {
  859. List<RID> textures;
  860. texture_owner.get_owned_list(&textures);
  861. for (List<RID>::Element *E = textures.front(); E; E = E->next()) {
  862. Texture *t = texture_owner.getornull(E->get());
  863. if (!t) {
  864. continue;
  865. }
  866. VS::TextureInfo tinfo;
  867. tinfo.texture = E->get();
  868. tinfo.path = t->path;
  869. tinfo.format = t->format;
  870. tinfo.width = t->alloc_width;
  871. tinfo.height = t->alloc_height;
  872. tinfo.depth = 0;
  873. tinfo.bytes = t->total_data_size;
  874. r_info->push_back(tinfo);
  875. }
  876. }
  877. void RasterizerStorageGLES2::texture_set_shrink_all_x2_on_set_data(bool p_enable) {
  878. config.shrink_textures_x2 = p_enable;
  879. }
  880. void RasterizerStorageGLES2::textures_keep_original(bool p_enable) {
  881. config.keep_original_textures = p_enable;
  882. }
  883. Size2 RasterizerStorageGLES2::texture_size_with_proxy(RID p_texture) const {
  884. const Texture *texture = texture_owner.getornull(p_texture);
  885. ERR_FAIL_COND_V(!texture, Size2());
  886. if (texture->proxy) {
  887. return Size2(texture->proxy->width, texture->proxy->height);
  888. } else {
  889. return Size2(texture->width, texture->height);
  890. }
  891. }
  892. void RasterizerStorageGLES2::texture_set_proxy(RID p_texture, RID p_proxy) {
  893. Texture *texture = texture_owner.getornull(p_texture);
  894. ERR_FAIL_COND(!texture);
  895. if (texture->proxy) {
  896. texture->proxy->proxy_owners.erase(texture);
  897. texture->proxy = nullptr;
  898. }
  899. if (p_proxy.is_valid()) {
  900. Texture *proxy = texture_owner.get(p_proxy);
  901. ERR_FAIL_COND(!proxy);
  902. ERR_FAIL_COND(proxy == texture);
  903. proxy->proxy_owners.insert(texture);
  904. texture->proxy = proxy;
  905. }
  906. }
  907. void RasterizerStorageGLES2::texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) {
  908. Texture *texture = texture_owner.getornull(p_texture);
  909. ERR_FAIL_COND(!texture);
  910. texture->redraw_if_visible = p_enable;
  911. }
  912. void RasterizerStorageGLES2::texture_set_detect_3d_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) {
  913. Texture *texture = texture_owner.get(p_texture);
  914. ERR_FAIL_COND(!texture);
  915. texture->detect_3d = p_callback;
  916. texture->detect_3d_ud = p_userdata;
  917. }
  918. void RasterizerStorageGLES2::texture_set_detect_srgb_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) {
  919. Texture *texture = texture_owner.get(p_texture);
  920. ERR_FAIL_COND(!texture);
  921. texture->detect_srgb = p_callback;
  922. texture->detect_srgb_ud = p_userdata;
  923. }
  924. void RasterizerStorageGLES2::texture_set_detect_normal_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) {
  925. Texture *texture = texture_owner.get(p_texture);
  926. ERR_FAIL_COND(!texture);
  927. texture->detect_normal = p_callback;
  928. texture->detect_normal_ud = p_userdata;
  929. }
  930. RID RasterizerStorageGLES2::texture_create_radiance_cubemap(RID p_source, int p_resolution) const {
  931. return RID();
  932. }
  933. RID RasterizerStorageGLES2::sky_create() {
  934. Sky *sky = memnew(Sky);
  935. sky->radiance = 0;
  936. return sky_owner.make_rid(sky);
  937. }
  938. void RasterizerStorageGLES2::sky_set_texture(RID p_sky, RID p_panorama, int p_radiance_size) {
  939. Sky *sky = sky_owner.getornull(p_sky);
  940. ERR_FAIL_COND(!sky);
  941. if (sky->panorama.is_valid()) {
  942. sky->panorama = RID();
  943. glDeleteTextures(1, &sky->radiance);
  944. sky->radiance = 0;
  945. }
  946. sky->panorama = p_panorama;
  947. if (!sky->panorama.is_valid()) {
  948. return; // the panorama was cleared
  949. }
  950. Texture *texture = texture_owner.getornull(sky->panorama);
  951. if (!texture) {
  952. sky->panorama = RID();
  953. ERR_FAIL_COND(!texture);
  954. }
  955. texture = texture->get_ptr(); //resolve for proxies
  956. // glBindVertexArray(0) and more
  957. {
  958. glBindBuffer(GL_ARRAY_BUFFER, 0);
  959. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  960. glDisable(GL_CULL_FACE);
  961. glDisable(GL_DEPTH_TEST);
  962. glDisable(GL_SCISSOR_TEST);
  963. glDisable(GL_BLEND);
  964. for (int i = 0; i < VS::ARRAY_MAX - 1; i++) {
  965. glDisableVertexAttribArray(i);
  966. }
  967. }
  968. glActiveTexture(GL_TEXTURE0);
  969. glBindTexture(texture->target, texture->tex_id);
  970. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  971. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  972. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  973. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //need this for proper sampling
  974. glActiveTexture(GL_TEXTURE1);
  975. glBindTexture(GL_TEXTURE_2D, resources.radical_inverse_vdc_cache_tex);
  976. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  977. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  978. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  979. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  980. // New cubemap that will hold the mipmaps with different roughness values
  981. glActiveTexture(GL_TEXTURE2);
  982. glGenTextures(1, &sky->radiance);
  983. glBindTexture(GL_TEXTURE_CUBE_MAP, sky->radiance);
  984. int size = p_radiance_size / 2; //divide by two because its a cubemap (this is an approximation because GLES3 uses a dual paraboloid)
  985. GLenum internal_format = GL_RGB;
  986. GLenum format = GL_RGB;
  987. GLenum type = GL_UNSIGNED_BYTE;
  988. // Set the initial (empty) mipmaps
  989. // Mobile hardware (PowerVR specially) prefers this approach,
  990. // the previous approach with manual lod levels kills the game.
  991. for (int i = 0; i < 6; i++) {
  992. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internal_format, size, size, 0, format, type, nullptr);
  993. }
  994. glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
  995. // No filters for now
  996. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  997. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  998. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  999. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1000. // Framebuffer
  1001. glBindFramebuffer(GL_FRAMEBUFFER, resources.mipmap_blur_fbo);
  1002. int mipmaps = 6;
  1003. int lod = 0;
  1004. int mm_level = mipmaps;
  1005. size = p_radiance_size / 2;
  1006. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::USE_SOURCE_PANORAMA, true);
  1007. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::USE_DIRECT_WRITE, true);
  1008. shaders.cubemap_filter.bind();
  1009. // third, render to the framebuffer using separate textures, then copy to mipmaps
  1010. while (size >= 1) {
  1011. //make framebuffer size the texture size, need to use a separate texture for compatibility
  1012. glActiveTexture(GL_TEXTURE3);
  1013. glBindTexture(GL_TEXTURE_2D, resources.mipmap_blur_color);
  1014. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
  1015. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, resources.mipmap_blur_color, 0);
  1016. if (lod == 1) {
  1017. // We set USE_DIRECT_WRITE to false for LOD levels 1 and up, so the shader will properly
  1018. // filter the roughness instead of just copying 1:1 from the source panorama.
  1019. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::USE_DIRECT_WRITE, false);
  1020. shaders.cubemap_filter.bind();
  1021. }
  1022. glViewport(0, 0, size, size);
  1023. bind_quad_array();
  1024. glActiveTexture(GL_TEXTURE2); //back to panorama
  1025. for (int i = 0; i < 6; i++) {
  1026. shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES2::FACE_ID, i);
  1027. float roughness = mm_level >= 0 ? lod / (float)(mipmaps - 1) : 1;
  1028. roughness = MIN(1.0, roughness); //keep max at 1
  1029. shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES2::ROUGHNESS, roughness);
  1030. shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES2::Z_FLIP, false);
  1031. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  1032. glCopyTexSubImage2D(_cube_side_enum[i], lod, 0, 0, 0, 0, size, size);
  1033. }
  1034. size >>= 1;
  1035. mm_level--;
  1036. lod++;
  1037. }
  1038. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::USE_SOURCE_PANORAMA, false);
  1039. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::USE_DIRECT_WRITE, false);
  1040. // restore ranges
  1041. glActiveTexture(GL_TEXTURE2); //back to panorama
  1042. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  1043. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1044. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1045. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1046. glBindTexture(GL_TEXTURE_2D, 0);
  1047. glActiveTexture(GL_TEXTURE3); //back to panorama
  1048. glBindTexture(GL_TEXTURE_2D, 0);
  1049. glActiveTexture(GL_TEXTURE1);
  1050. glBindTexture(GL_TEXTURE_2D, 0);
  1051. glActiveTexture(GL_TEXTURE0);
  1052. glBindTexture(GL_TEXTURE_2D, 0);
  1053. //reset flags on Sky Texture that may have changed
  1054. texture_set_flags(sky->panorama, texture->flags);
  1055. // Framebuffer did its job. thank mr framebuffer
  1056. glActiveTexture(GL_TEXTURE0); //back to panorama
  1057. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES2::system_fbo);
  1058. }
  1059. /* SHADER API */
  1060. RID RasterizerStorageGLES2::shader_create() {
  1061. Shader *shader = memnew(Shader);
  1062. shader->mode = VS::SHADER_SPATIAL;
  1063. shader->shader = &scene->state.scene_shader;
  1064. RID rid = shader_owner.make_rid(shader);
  1065. _shader_make_dirty(shader);
  1066. shader->self = rid;
  1067. return rid;
  1068. }
  1069. void RasterizerStorageGLES2::_shader_make_dirty(Shader *p_shader) {
  1070. if (p_shader->dirty_list.in_list()) {
  1071. return;
  1072. }
  1073. _shader_dirty_list.add(&p_shader->dirty_list);
  1074. }
  1075. void RasterizerStorageGLES2::shader_set_code(RID p_shader, const String &p_code) {
  1076. Shader *shader = shader_owner.getornull(p_shader);
  1077. ERR_FAIL_COND(!shader);
  1078. shader->code = p_code;
  1079. String mode_string = ShaderLanguage::get_shader_type(p_code);
  1080. VS::ShaderMode mode;
  1081. if (mode_string == "canvas_item") {
  1082. mode = VS::SHADER_CANVAS_ITEM;
  1083. } else if (mode_string == "particles") {
  1084. mode = VS::SHADER_PARTICLES;
  1085. } else {
  1086. mode = VS::SHADER_SPATIAL;
  1087. }
  1088. if (shader->custom_code_id && mode != shader->mode) {
  1089. shader->shader->free_custom_shader(shader->custom_code_id);
  1090. shader->custom_code_id = 0;
  1091. }
  1092. shader->mode = mode;
  1093. // TODO handle all shader types
  1094. if (mode == VS::SHADER_CANVAS_ITEM) {
  1095. shader->shader = &canvas->state.canvas_shader;
  1096. } else if (mode == VS::SHADER_SPATIAL) {
  1097. shader->shader = &scene->state.scene_shader;
  1098. } else {
  1099. return;
  1100. }
  1101. if (shader->custom_code_id == 0) {
  1102. shader->custom_code_id = shader->shader->create_custom_shader();
  1103. }
  1104. _shader_make_dirty(shader);
  1105. }
  1106. String RasterizerStorageGLES2::shader_get_code(RID p_shader) const {
  1107. const Shader *shader = shader_owner.get(p_shader);
  1108. ERR_FAIL_COND_V(!shader, "");
  1109. return shader->code;
  1110. }
  1111. void RasterizerStorageGLES2::_update_shader(Shader *p_shader) const {
  1112. _shader_dirty_list.remove(&p_shader->dirty_list);
  1113. p_shader->valid = false;
  1114. p_shader->uniforms.clear();
  1115. if (p_shader->code == String()) {
  1116. return; //just invalid, but no error
  1117. }
  1118. ShaderCompilerGLES2::GeneratedCode gen_code;
  1119. ShaderCompilerGLES2::IdentifierActions *actions = nullptr;
  1120. switch (p_shader->mode) {
  1121. case VS::SHADER_CANVAS_ITEM: {
  1122. p_shader->canvas_item.light_mode = Shader::CanvasItem::LIGHT_MODE_NORMAL;
  1123. p_shader->canvas_item.blend_mode = Shader::CanvasItem::BLEND_MODE_MIX;
  1124. p_shader->canvas_item.uses_screen_texture = false;
  1125. p_shader->canvas_item.uses_screen_uv = false;
  1126. p_shader->canvas_item.uses_time = false;
  1127. p_shader->canvas_item.uses_modulate = false;
  1128. p_shader->canvas_item.uses_color = false;
  1129. p_shader->canvas_item.uses_vertex = false;
  1130. p_shader->canvas_item.batch_flags = 0;
  1131. p_shader->canvas_item.uses_world_matrix = false;
  1132. p_shader->canvas_item.uses_extra_matrix = false;
  1133. p_shader->canvas_item.uses_projection_matrix = false;
  1134. p_shader->canvas_item.uses_instance_custom = false;
  1135. shaders.actions_canvas.render_mode_values["blend_add"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_ADD);
  1136. shaders.actions_canvas.render_mode_values["blend_mix"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_MIX);
  1137. shaders.actions_canvas.render_mode_values["blend_sub"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_SUB);
  1138. shaders.actions_canvas.render_mode_values["blend_mul"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_MUL);
  1139. shaders.actions_canvas.render_mode_values["blend_premul_alpha"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_PMALPHA);
  1140. shaders.actions_canvas.render_mode_values["unshaded"] = Pair<int *, int>(&p_shader->canvas_item.light_mode, Shader::CanvasItem::LIGHT_MODE_UNSHADED);
  1141. shaders.actions_canvas.render_mode_values["light_only"] = Pair<int *, int>(&p_shader->canvas_item.light_mode, Shader::CanvasItem::LIGHT_MODE_LIGHT_ONLY);
  1142. shaders.actions_canvas.usage_flag_pointers["SCREEN_UV"] = &p_shader->canvas_item.uses_screen_uv;
  1143. shaders.actions_canvas.usage_flag_pointers["SCREEN_PIXEL_SIZE"] = &p_shader->canvas_item.uses_screen_uv;
  1144. shaders.actions_canvas.usage_flag_pointers["SCREEN_TEXTURE"] = &p_shader->canvas_item.uses_screen_texture;
  1145. shaders.actions_canvas.usage_flag_pointers["TIME"] = &p_shader->canvas_item.uses_time;
  1146. shaders.actions_canvas.usage_flag_pointers["MODULATE"] = &p_shader->canvas_item.uses_modulate;
  1147. shaders.actions_canvas.usage_flag_pointers["COLOR"] = &p_shader->canvas_item.uses_color;
  1148. shaders.actions_canvas.usage_flag_pointers["VERTEX"] = &p_shader->canvas_item.uses_vertex;
  1149. shaders.actions_canvas.usage_flag_pointers["WORLD_MATRIX"] = &p_shader->canvas_item.uses_world_matrix;
  1150. shaders.actions_canvas.usage_flag_pointers["EXTRA_MATRIX"] = &p_shader->canvas_item.uses_extra_matrix;
  1151. shaders.actions_canvas.usage_flag_pointers["PROJECTION_MATRIX"] = &p_shader->canvas_item.uses_projection_matrix;
  1152. shaders.actions_canvas.usage_flag_pointers["INSTANCE_CUSTOM"] = &p_shader->canvas_item.uses_instance_custom;
  1153. actions = &shaders.actions_canvas;
  1154. actions->uniforms = &p_shader->uniforms;
  1155. } break;
  1156. case VS::SHADER_SPATIAL: {
  1157. p_shader->spatial.blend_mode = Shader::Spatial::BLEND_MODE_MIX;
  1158. p_shader->spatial.depth_draw_mode = Shader::Spatial::DEPTH_DRAW_OPAQUE;
  1159. p_shader->spatial.cull_mode = Shader::Spatial::CULL_MODE_BACK;
  1160. p_shader->spatial.uses_alpha = false;
  1161. p_shader->spatial.uses_alpha_scissor = false;
  1162. p_shader->spatial.uses_discard = false;
  1163. p_shader->spatial.unshaded = false;
  1164. p_shader->spatial.no_depth_test = false;
  1165. p_shader->spatial.uses_sss = false;
  1166. p_shader->spatial.uses_time = false;
  1167. p_shader->spatial.uses_vertex_lighting = false;
  1168. p_shader->spatial.uses_screen_texture = false;
  1169. p_shader->spatial.uses_depth_texture = false;
  1170. p_shader->spatial.uses_vertex = false;
  1171. p_shader->spatial.uses_tangent = false;
  1172. p_shader->spatial.uses_ensure_correct_normals = false;
  1173. p_shader->spatial.writes_modelview_or_projection = false;
  1174. p_shader->spatial.uses_world_coordinates = false;
  1175. shaders.actions_scene.render_mode_values["blend_add"] = Pair<int *, int>(&p_shader->spatial.blend_mode, Shader::Spatial::BLEND_MODE_ADD);
  1176. shaders.actions_scene.render_mode_values["blend_mix"] = Pair<int *, int>(&p_shader->spatial.blend_mode, Shader::Spatial::BLEND_MODE_MIX);
  1177. shaders.actions_scene.render_mode_values["blend_sub"] = Pair<int *, int>(&p_shader->spatial.blend_mode, Shader::Spatial::BLEND_MODE_SUB);
  1178. shaders.actions_scene.render_mode_values["blend_mul"] = Pair<int *, int>(&p_shader->spatial.blend_mode, Shader::Spatial::BLEND_MODE_MUL);
  1179. shaders.actions_scene.render_mode_values["depth_draw_opaque"] = Pair<int *, int>(&p_shader->spatial.depth_draw_mode, Shader::Spatial::DEPTH_DRAW_OPAQUE);
  1180. shaders.actions_scene.render_mode_values["depth_draw_always"] = Pair<int *, int>(&p_shader->spatial.depth_draw_mode, Shader::Spatial::DEPTH_DRAW_ALWAYS);
  1181. shaders.actions_scene.render_mode_values["depth_draw_never"] = Pair<int *, int>(&p_shader->spatial.depth_draw_mode, Shader::Spatial::DEPTH_DRAW_NEVER);
  1182. 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);
  1183. shaders.actions_scene.render_mode_values["cull_front"] = Pair<int *, int>(&p_shader->spatial.cull_mode, Shader::Spatial::CULL_MODE_FRONT);
  1184. shaders.actions_scene.render_mode_values["cull_back"] = Pair<int *, int>(&p_shader->spatial.cull_mode, Shader::Spatial::CULL_MODE_BACK);
  1185. shaders.actions_scene.render_mode_values["cull_disabled"] = Pair<int *, int>(&p_shader->spatial.cull_mode, Shader::Spatial::CULL_MODE_DISABLED);
  1186. shaders.actions_scene.render_mode_flags["unshaded"] = &p_shader->spatial.unshaded;
  1187. shaders.actions_scene.render_mode_flags["depth_test_disable"] = &p_shader->spatial.no_depth_test;
  1188. shaders.actions_scene.render_mode_flags["vertex_lighting"] = &p_shader->spatial.uses_vertex_lighting;
  1189. shaders.actions_scene.render_mode_flags["world_vertex_coords"] = &p_shader->spatial.uses_world_coordinates;
  1190. shaders.actions_scene.render_mode_flags["ensure_correct_normals"] = &p_shader->spatial.uses_ensure_correct_normals;
  1191. shaders.actions_scene.usage_flag_pointers["ALPHA"] = &p_shader->spatial.uses_alpha;
  1192. shaders.actions_scene.usage_flag_pointers["ALPHA_SCISSOR"] = &p_shader->spatial.uses_alpha_scissor;
  1193. shaders.actions_scene.usage_flag_pointers["SSS_STRENGTH"] = &p_shader->spatial.uses_sss;
  1194. shaders.actions_scene.usage_flag_pointers["DISCARD"] = &p_shader->spatial.uses_discard;
  1195. shaders.actions_scene.usage_flag_pointers["SCREEN_TEXTURE"] = &p_shader->spatial.uses_screen_texture;
  1196. shaders.actions_scene.usage_flag_pointers["DEPTH_TEXTURE"] = &p_shader->spatial.uses_depth_texture;
  1197. shaders.actions_scene.usage_flag_pointers["TIME"] = &p_shader->spatial.uses_time;
  1198. // Use of any of these BUILTINS indicate the need for transformed tangents.
  1199. // This is needed to know when to transform tangents in software skinning.
  1200. shaders.actions_scene.usage_flag_pointers["TANGENT"] = &p_shader->spatial.uses_tangent;
  1201. shaders.actions_scene.usage_flag_pointers["NORMALMAP"] = &p_shader->spatial.uses_tangent;
  1202. shaders.actions_scene.write_flag_pointers["MODELVIEW_MATRIX"] = &p_shader->spatial.writes_modelview_or_projection;
  1203. shaders.actions_scene.write_flag_pointers["PROJECTION_MATRIX"] = &p_shader->spatial.writes_modelview_or_projection;
  1204. shaders.actions_scene.write_flag_pointers["VERTEX"] = &p_shader->spatial.uses_vertex;
  1205. actions = &shaders.actions_scene;
  1206. actions->uniforms = &p_shader->uniforms;
  1207. if (p_shader->spatial.uses_screen_texture && p_shader->spatial.uses_depth_texture) {
  1208. ERR_PRINT_ONCE("Using both SCREEN_TEXTURE and DEPTH_TEXTURE is not supported in GLES2");
  1209. }
  1210. if (p_shader->spatial.uses_depth_texture && !config.support_depth_texture) {
  1211. ERR_PRINT_ONCE("Using DEPTH_TEXTURE is not permitted on this hardware, operation will fail.");
  1212. }
  1213. } break;
  1214. default: {
  1215. return;
  1216. } break;
  1217. }
  1218. Error err = shaders.compiler.compile(p_shader->mode, p_shader->code, actions, p_shader->path, gen_code);
  1219. if (err != OK) {
  1220. return;
  1221. }
  1222. 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.custom_defines);
  1223. p_shader->texture_count = gen_code.texture_uniforms.size();
  1224. p_shader->texture_hints = gen_code.texture_hints;
  1225. p_shader->uses_vertex_time = gen_code.uses_vertex_time;
  1226. p_shader->uses_fragment_time = gen_code.uses_fragment_time;
  1227. // some logic for batching
  1228. if (p_shader->mode == VS::SHADER_CANVAS_ITEM) {
  1229. if (p_shader->canvas_item.uses_modulate | p_shader->canvas_item.uses_color) {
  1230. p_shader->canvas_item.batch_flags |= RasterizerStorageCommon::PREVENT_COLOR_BAKING;
  1231. }
  1232. if (p_shader->canvas_item.uses_vertex) {
  1233. p_shader->canvas_item.batch_flags |= RasterizerStorageCommon::PREVENT_VERTEX_BAKING;
  1234. }
  1235. 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) {
  1236. p_shader->canvas_item.batch_flags |= RasterizerStorageCommon::PREVENT_ITEM_JOINING;
  1237. }
  1238. }
  1239. p_shader->shader->set_custom_shader(p_shader->custom_code_id);
  1240. p_shader->shader->bind();
  1241. // cache uniform locations
  1242. for (SelfList<Material> *E = p_shader->materials.first(); E; E = E->next()) {
  1243. _material_make_dirty(E->self());
  1244. }
  1245. p_shader->valid = true;
  1246. p_shader->version++;
  1247. }
  1248. void RasterizerStorageGLES2::update_dirty_shaders() {
  1249. while (_shader_dirty_list.first()) {
  1250. _update_shader(_shader_dirty_list.first()->self());
  1251. }
  1252. }
  1253. void RasterizerStorageGLES2::shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const {
  1254. Shader *shader = shader_owner.get(p_shader);
  1255. ERR_FAIL_COND(!shader);
  1256. if (shader->dirty_list.in_list()) {
  1257. _update_shader(shader);
  1258. }
  1259. Map<int, StringName> order;
  1260. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = shader->uniforms.front(); E; E = E->next()) {
  1261. if (E->get().texture_order >= 0) {
  1262. order[E->get().texture_order + 100000] = E->key();
  1263. } else {
  1264. order[E->get().order] = E->key();
  1265. }
  1266. }
  1267. for (Map<int, StringName>::Element *E = order.front(); E; E = E->next()) {
  1268. PropertyInfo pi;
  1269. ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[E->get()];
  1270. pi.name = E->get();
  1271. switch (u.type) {
  1272. case ShaderLanguage::TYPE_STRUCT: {
  1273. pi.type = Variant::ARRAY;
  1274. } break;
  1275. case ShaderLanguage::TYPE_VOID: {
  1276. pi.type = Variant::NIL;
  1277. } break;
  1278. case ShaderLanguage::TYPE_BOOL: {
  1279. pi.type = Variant::BOOL;
  1280. } break;
  1281. // bool vectors
  1282. case ShaderLanguage::TYPE_BVEC2: {
  1283. pi.type = Variant::INT;
  1284. pi.hint = PROPERTY_HINT_FLAGS;
  1285. pi.hint_string = "x,y";
  1286. } break;
  1287. case ShaderLanguage::TYPE_BVEC3: {
  1288. pi.type = Variant::INT;
  1289. pi.hint = PROPERTY_HINT_FLAGS;
  1290. pi.hint_string = "x,y,z";
  1291. } break;
  1292. case ShaderLanguage::TYPE_BVEC4: {
  1293. pi.type = Variant::INT;
  1294. pi.hint = PROPERTY_HINT_FLAGS;
  1295. pi.hint_string = "x,y,z,w";
  1296. } break;
  1297. // int stuff
  1298. case ShaderLanguage::TYPE_UINT:
  1299. case ShaderLanguage::TYPE_INT: {
  1300. pi.type = Variant::INT;
  1301. if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_RANGE) {
  1302. pi.hint = PROPERTY_HINT_RANGE;
  1303. pi.hint_string = rtos(u.hint_range[0]) + "," + rtos(u.hint_range[1]) + "," + rtos(u.hint_range[2]);
  1304. }
  1305. } break;
  1306. case ShaderLanguage::TYPE_IVEC2:
  1307. case ShaderLanguage::TYPE_UVEC2:
  1308. case ShaderLanguage::TYPE_IVEC3:
  1309. case ShaderLanguage::TYPE_UVEC3:
  1310. case ShaderLanguage::TYPE_IVEC4:
  1311. case ShaderLanguage::TYPE_UVEC4: {
  1312. pi.type = Variant::POOL_INT_ARRAY;
  1313. } break;
  1314. case ShaderLanguage::TYPE_FLOAT: {
  1315. pi.type = Variant::REAL;
  1316. if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_RANGE) {
  1317. pi.hint = PROPERTY_HINT_RANGE;
  1318. pi.hint_string = rtos(u.hint_range[0]) + "," + rtos(u.hint_range[1]) + "," + rtos(u.hint_range[2]);
  1319. }
  1320. } break;
  1321. case ShaderLanguage::TYPE_VEC2: {
  1322. pi.type = Variant::VECTOR2;
  1323. } break;
  1324. case ShaderLanguage::TYPE_VEC3: {
  1325. pi.type = Variant::VECTOR3;
  1326. } break;
  1327. case ShaderLanguage::TYPE_VEC4: {
  1328. if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_COLOR) {
  1329. pi.type = Variant::COLOR;
  1330. } else {
  1331. pi.type = Variant::PLANE;
  1332. }
  1333. } break;
  1334. case ShaderLanguage::TYPE_MAT2: {
  1335. pi.type = Variant::TRANSFORM2D;
  1336. } break;
  1337. case ShaderLanguage::TYPE_MAT3: {
  1338. pi.type = Variant::BASIS;
  1339. } break;
  1340. case ShaderLanguage::TYPE_MAT4: {
  1341. pi.type = Variant::TRANSFORM;
  1342. } break;
  1343. case ShaderLanguage::TYPE_SAMPLER2D:
  1344. case ShaderLanguage::TYPE_SAMPLEREXT:
  1345. case ShaderLanguage::TYPE_ISAMPLER2D:
  1346. case ShaderLanguage::TYPE_USAMPLER2D: {
  1347. pi.type = Variant::OBJECT;
  1348. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  1349. pi.hint_string = "Texture";
  1350. } break;
  1351. case ShaderLanguage::TYPE_SAMPLERCUBE: {
  1352. pi.type = Variant::OBJECT;
  1353. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  1354. pi.hint_string = "CubeMap";
  1355. } break;
  1356. case ShaderLanguage::TYPE_SAMPLER2DARRAY:
  1357. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  1358. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  1359. case ShaderLanguage::TYPE_SAMPLER3D:
  1360. case ShaderLanguage::TYPE_ISAMPLER3D:
  1361. case ShaderLanguage::TYPE_USAMPLER3D: {
  1362. // Not implemented in GLES2
  1363. } break;
  1364. default: {
  1365. }
  1366. }
  1367. p_param_list->push_back(pi);
  1368. }
  1369. }
  1370. void RasterizerStorageGLES2::shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture) {
  1371. Shader *shader = shader_owner.get(p_shader);
  1372. ERR_FAIL_COND(!shader);
  1373. ERR_FAIL_COND(p_texture.is_valid() && !texture_owner.owns(p_texture));
  1374. if (p_texture.is_valid()) {
  1375. shader->default_textures[p_name] = p_texture;
  1376. } else {
  1377. shader->default_textures.erase(p_name);
  1378. }
  1379. _shader_make_dirty(shader);
  1380. }
  1381. RID RasterizerStorageGLES2::shader_get_default_texture_param(RID p_shader, const StringName &p_name) const {
  1382. const Shader *shader = shader_owner.get(p_shader);
  1383. ERR_FAIL_COND_V(!shader, RID());
  1384. const Map<StringName, RID>::Element *E = shader->default_textures.find(p_name);
  1385. if (!E) {
  1386. return RID();
  1387. }
  1388. return E->get();
  1389. }
  1390. void RasterizerStorageGLES2::shader_add_custom_define(RID p_shader, const String &p_define) {
  1391. Shader *shader = shader_owner.get(p_shader);
  1392. ERR_FAIL_COND(!shader);
  1393. shader->shader->add_custom_define(p_define);
  1394. _shader_make_dirty(shader);
  1395. }
  1396. void RasterizerStorageGLES2::shader_get_custom_defines(RID p_shader, Vector<String> *p_defines) const {
  1397. Shader *shader = shader_owner.get(p_shader);
  1398. ERR_FAIL_COND(!shader);
  1399. shader->shader->get_custom_defines(p_defines);
  1400. }
  1401. void RasterizerStorageGLES2::shader_remove_custom_define(RID p_shader, const String &p_define) {
  1402. Shader *shader = shader_owner.get(p_shader);
  1403. ERR_FAIL_COND(!shader);
  1404. shader->shader->remove_custom_define(p_define);
  1405. _shader_make_dirty(shader);
  1406. }
  1407. /* COMMON MATERIAL API */
  1408. void RasterizerStorageGLES2::_material_make_dirty(Material *p_material) const {
  1409. if (p_material->dirty_list.in_list()) {
  1410. return;
  1411. }
  1412. _material_dirty_list.add(&p_material->dirty_list);
  1413. }
  1414. RID RasterizerStorageGLES2::material_create() {
  1415. Material *material = memnew(Material);
  1416. return material_owner.make_rid(material);
  1417. }
  1418. void RasterizerStorageGLES2::material_set_shader(RID p_material, RID p_shader) {
  1419. Material *material = material_owner.get(p_material);
  1420. ERR_FAIL_COND(!material);
  1421. Shader *shader = shader_owner.getornull(p_shader);
  1422. if (material->shader) {
  1423. // if a shader is present, remove the old shader
  1424. material->shader->materials.remove(&material->list);
  1425. }
  1426. material->shader = shader;
  1427. if (shader) {
  1428. shader->materials.add(&material->list);
  1429. }
  1430. _material_make_dirty(material);
  1431. }
  1432. RID RasterizerStorageGLES2::material_get_shader(RID p_material) const {
  1433. const Material *material = material_owner.get(p_material);
  1434. ERR_FAIL_COND_V(!material, RID());
  1435. if (material->shader) {
  1436. return material->shader->self;
  1437. }
  1438. return RID();
  1439. }
  1440. void RasterizerStorageGLES2::material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) {
  1441. Material *material = material_owner.get(p_material);
  1442. ERR_FAIL_COND(!material);
  1443. if (p_value.get_type() == Variant::NIL) {
  1444. material->params.erase(p_param);
  1445. } else {
  1446. material->params[p_param] = p_value;
  1447. }
  1448. _material_make_dirty(material);
  1449. }
  1450. Variant RasterizerStorageGLES2::material_get_param(RID p_material, const StringName &p_param) const {
  1451. const Material *material = material_owner.get(p_material);
  1452. ERR_FAIL_COND_V(!material, RID());
  1453. if (material->params.has(p_param)) {
  1454. return material->params[p_param];
  1455. }
  1456. return material_get_param_default(p_material, p_param);
  1457. }
  1458. Variant RasterizerStorageGLES2::material_get_param_default(RID p_material, const StringName &p_param) const {
  1459. const Material *material = material_owner.get(p_material);
  1460. ERR_FAIL_COND_V(!material, Variant());
  1461. if (material->shader) {
  1462. if (material->shader->uniforms.has(p_param)) {
  1463. ShaderLanguage::ShaderNode::Uniform uniform = material->shader->uniforms[p_param];
  1464. Vector<ShaderLanguage::ConstantNode::Value> default_value = uniform.default_value;
  1465. return ShaderLanguage::constant_value_to_variant(default_value, uniform.type, uniform.hint);
  1466. }
  1467. }
  1468. return Variant();
  1469. }
  1470. void RasterizerStorageGLES2::material_set_line_width(RID p_material, float p_width) {
  1471. Material *material = material_owner.getornull(p_material);
  1472. ERR_FAIL_COND(!material);
  1473. material->line_width = p_width;
  1474. }
  1475. void RasterizerStorageGLES2::material_set_next_pass(RID p_material, RID p_next_material) {
  1476. Material *material = material_owner.get(p_material);
  1477. ERR_FAIL_COND(!material);
  1478. material->next_pass = p_next_material;
  1479. }
  1480. bool RasterizerStorageGLES2::material_is_animated(RID p_material) {
  1481. Material *material = material_owner.get(p_material);
  1482. ERR_FAIL_COND_V(!material, false);
  1483. if (material->dirty_list.in_list()) {
  1484. _update_material(material);
  1485. }
  1486. bool animated = material->is_animated_cache;
  1487. if (!animated && material->next_pass.is_valid()) {
  1488. animated = material_is_animated(material->next_pass);
  1489. }
  1490. return animated;
  1491. }
  1492. bool RasterizerStorageGLES2::material_casts_shadows(RID p_material) {
  1493. Material *material = material_owner.get(p_material);
  1494. ERR_FAIL_COND_V(!material, false);
  1495. if (material->dirty_list.in_list()) {
  1496. _update_material(material);
  1497. }
  1498. bool casts_shadows = material->can_cast_shadow_cache;
  1499. if (!casts_shadows && material->next_pass.is_valid()) {
  1500. casts_shadows = material_casts_shadows(material->next_pass);
  1501. }
  1502. return casts_shadows;
  1503. }
  1504. bool RasterizerStorageGLES2::material_uses_tangents(RID p_material) {
  1505. Material *material = material_owner.get(p_material);
  1506. ERR_FAIL_COND_V(!material, false);
  1507. if (!material->shader) {
  1508. return false;
  1509. }
  1510. if (material->shader->dirty_list.in_list()) {
  1511. _update_shader(material->shader);
  1512. }
  1513. return material->shader->spatial.uses_tangent;
  1514. }
  1515. bool RasterizerStorageGLES2::material_uses_ensure_correct_normals(RID p_material) {
  1516. Material *material = material_owner.get(p_material);
  1517. ERR_FAIL_COND_V(!material, false);
  1518. if (!material->shader) {
  1519. return false;
  1520. }
  1521. if (material->shader->dirty_list.in_list()) {
  1522. _update_shader(material->shader);
  1523. }
  1524. return material->shader->spatial.uses_ensure_correct_normals;
  1525. }
  1526. void RasterizerStorageGLES2::material_add_instance_owner(RID p_material, RasterizerScene::InstanceBase *p_instance) {
  1527. Material *material = material_owner.getornull(p_material);
  1528. ERR_FAIL_COND(!material);
  1529. Map<RasterizerScene::InstanceBase *, int>::Element *E = material->instance_owners.find(p_instance);
  1530. if (E) {
  1531. E->get()++;
  1532. } else {
  1533. material->instance_owners[p_instance] = 1;
  1534. }
  1535. }
  1536. void RasterizerStorageGLES2::material_remove_instance_owner(RID p_material, RasterizerScene::InstanceBase *p_instance) {
  1537. Material *material = material_owner.getornull(p_material);
  1538. ERR_FAIL_COND(!material);
  1539. Map<RasterizerScene::InstanceBase *, int>::Element *E = material->instance_owners.find(p_instance);
  1540. ERR_FAIL_COND(!E);
  1541. E->get()--;
  1542. if (E->get() == 0) {
  1543. material->instance_owners.erase(E);
  1544. }
  1545. }
  1546. void RasterizerStorageGLES2::material_set_render_priority(RID p_material, int priority) {
  1547. ERR_FAIL_COND(priority < VS::MATERIAL_RENDER_PRIORITY_MIN);
  1548. ERR_FAIL_COND(priority > VS::MATERIAL_RENDER_PRIORITY_MAX);
  1549. Material *material = material_owner.get(p_material);
  1550. ERR_FAIL_COND(!material);
  1551. material->render_priority = priority;
  1552. }
  1553. void RasterizerStorageGLES2::_update_material(Material *p_material) {
  1554. if (p_material->dirty_list.in_list()) {
  1555. _material_dirty_list.remove(&p_material->dirty_list);
  1556. }
  1557. if (p_material->shader && p_material->shader->dirty_list.in_list()) {
  1558. _update_shader(p_material->shader);
  1559. }
  1560. if (p_material->shader && !p_material->shader->valid) {
  1561. return;
  1562. }
  1563. {
  1564. bool can_cast_shadow = false;
  1565. bool is_animated = false;
  1566. if (p_material->shader && p_material->shader->mode == VS::SHADER_SPATIAL) {
  1567. if (p_material->shader->spatial.blend_mode == Shader::Spatial::BLEND_MODE_MIX &&
  1568. (!(p_material->shader->spatial.uses_alpha && !p_material->shader->spatial.uses_alpha_scissor) || p_material->shader->spatial.depth_draw_mode == Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS)) {
  1569. can_cast_shadow = true;
  1570. }
  1571. if (p_material->shader->spatial.uses_discard && p_material->shader->uses_fragment_time) {
  1572. is_animated = true;
  1573. }
  1574. if (p_material->shader->spatial.uses_vertex && p_material->shader->uses_vertex_time) {
  1575. is_animated = true;
  1576. }
  1577. if (can_cast_shadow != p_material->can_cast_shadow_cache || is_animated != p_material->is_animated_cache) {
  1578. p_material->can_cast_shadow_cache = can_cast_shadow;
  1579. p_material->is_animated_cache = is_animated;
  1580. for (Map<Geometry *, int>::Element *E = p_material->geometry_owners.front(); E; E = E->next()) {
  1581. E->key()->material_changed_notify();
  1582. }
  1583. for (Map<RasterizerScene::InstanceBase *, int>::Element *E = p_material->instance_owners.front(); E; E = E->next()) {
  1584. E->key()->base_changed(false, true);
  1585. }
  1586. }
  1587. }
  1588. }
  1589. // uniforms and other things will be set in the use_material method in ShaderGLES2
  1590. if (p_material->shader && p_material->shader->texture_count > 0) {
  1591. p_material->textures.resize(p_material->shader->texture_count);
  1592. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = p_material->shader->uniforms.front(); E; E = E->next()) {
  1593. if (E->get().texture_order < 0) {
  1594. continue; // not a texture, does not go here
  1595. }
  1596. RID texture;
  1597. Map<StringName, Variant>::Element *V = p_material->params.find(E->key());
  1598. if (V) {
  1599. texture = V->get();
  1600. }
  1601. if (!texture.is_valid()) {
  1602. Map<StringName, RID>::Element *W = p_material->shader->default_textures.find(E->key());
  1603. if (W) {
  1604. texture = W->get();
  1605. }
  1606. }
  1607. p_material->textures.write[E->get().texture_order] = Pair<StringName, RID>(E->key(), texture);
  1608. }
  1609. } else {
  1610. p_material->textures.clear();
  1611. }
  1612. }
  1613. void RasterizerStorageGLES2::_material_add_geometry(RID p_material, Geometry *p_geometry) {
  1614. Material *material = material_owner.getornull(p_material);
  1615. ERR_FAIL_COND(!material);
  1616. Map<Geometry *, int>::Element *I = material->geometry_owners.find(p_geometry);
  1617. if (I) {
  1618. I->get()++;
  1619. } else {
  1620. material->geometry_owners[p_geometry] = 1;
  1621. }
  1622. }
  1623. void RasterizerStorageGLES2::_material_remove_geometry(RID p_material, Geometry *p_geometry) {
  1624. Material *material = material_owner.getornull(p_material);
  1625. ERR_FAIL_COND(!material);
  1626. Map<Geometry *, int>::Element *I = material->geometry_owners.find(p_geometry);
  1627. ERR_FAIL_COND(!I);
  1628. I->get()--;
  1629. if (I->get() == 0) {
  1630. material->geometry_owners.erase(I);
  1631. }
  1632. }
  1633. void RasterizerStorageGLES2::update_dirty_materials() {
  1634. while (_material_dirty_list.first()) {
  1635. Material *material = _material_dirty_list.first()->self();
  1636. _update_material(material);
  1637. }
  1638. }
  1639. /* MESH API */
  1640. RID RasterizerStorageGLES2::mesh_create() {
  1641. Mesh *mesh = memnew(Mesh);
  1642. return mesh_owner.make_rid(mesh);
  1643. }
  1644. static PoolVector<uint8_t> _unpack_half_floats(const PoolVector<uint8_t> &array, uint32_t &format, int p_vertices) {
  1645. uint32_t p_format = format;
  1646. static int src_size[VS::ARRAY_MAX];
  1647. static int dst_size[VS::ARRAY_MAX];
  1648. static int to_convert[VS::ARRAY_MAX];
  1649. int src_stride = 0;
  1650. int dst_stride = 0;
  1651. for (int i = 0; i < VS::ARRAY_MAX; i++) {
  1652. to_convert[i] = 0;
  1653. if (!(p_format & (1 << i))) {
  1654. src_size[i] = 0;
  1655. dst_size[i] = 0;
  1656. continue;
  1657. }
  1658. switch (i) {
  1659. case VS::ARRAY_VERTEX: {
  1660. if (p_format & VS::ARRAY_COMPRESS_VERTEX) {
  1661. if (p_format & VS::ARRAY_FLAG_USE_2D_VERTICES) {
  1662. src_size[i] = 4;
  1663. dst_size[i] = 8;
  1664. to_convert[i] = 2;
  1665. } else {
  1666. src_size[i] = 8;
  1667. dst_size[i] = 12;
  1668. to_convert[i] = 3;
  1669. }
  1670. format &= ~VS::ARRAY_COMPRESS_VERTEX;
  1671. } else {
  1672. if (p_format & VS::ARRAY_FLAG_USE_2D_VERTICES) {
  1673. src_size[i] = 8;
  1674. dst_size[i] = 8;
  1675. } else {
  1676. src_size[i] = 12;
  1677. dst_size[i] = 12;
  1678. }
  1679. }
  1680. } break;
  1681. case VS::ARRAY_NORMAL: {
  1682. if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
  1683. src_size[i] = 4;
  1684. dst_size[i] = 4;
  1685. } else {
  1686. if (p_format & VS::ARRAY_COMPRESS_NORMAL) {
  1687. src_size[i] = 4;
  1688. dst_size[i] = 4;
  1689. } else {
  1690. src_size[i] = 12;
  1691. dst_size[i] = 12;
  1692. }
  1693. }
  1694. } break;
  1695. case VS::ARRAY_TANGENT: {
  1696. if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
  1697. if (!(p_format & VS::ARRAY_COMPRESS_TANGENT && p_format & VS::ARRAY_COMPRESS_NORMAL)) {
  1698. src_size[VS::ARRAY_NORMAL] = 8;
  1699. dst_size[VS::ARRAY_NORMAL] = 8;
  1700. // These must be incremented manually,
  1701. // as we are modifying a previous attribute size.
  1702. src_stride += 4;
  1703. dst_stride += 4;
  1704. }
  1705. src_size[i] = 0;
  1706. dst_size[i] = 0;
  1707. } else {
  1708. if (p_format & VS::ARRAY_COMPRESS_TANGENT) {
  1709. src_size[i] = 4;
  1710. dst_size[i] = 4;
  1711. } else {
  1712. src_size[i] = 16;
  1713. dst_size[i] = 16;
  1714. }
  1715. }
  1716. } break;
  1717. case VS::ARRAY_COLOR: {
  1718. if (p_format & VS::ARRAY_COMPRESS_COLOR) {
  1719. src_size[i] = 4;
  1720. dst_size[i] = 4;
  1721. } else {
  1722. src_size[i] = 16;
  1723. dst_size[i] = 16;
  1724. }
  1725. } break;
  1726. case VS::ARRAY_TEX_UV: {
  1727. if (p_format & VS::ARRAY_COMPRESS_TEX_UV) {
  1728. src_size[i] = 4;
  1729. to_convert[i] = 2;
  1730. format &= ~VS::ARRAY_COMPRESS_TEX_UV;
  1731. } else {
  1732. src_size[i] = 8;
  1733. }
  1734. dst_size[i] = 8;
  1735. } break;
  1736. case VS::ARRAY_TEX_UV2: {
  1737. if (p_format & VS::ARRAY_COMPRESS_TEX_UV2) {
  1738. src_size[i] = 4;
  1739. to_convert[i] = 2;
  1740. format &= ~VS::ARRAY_COMPRESS_TEX_UV2;
  1741. } else {
  1742. src_size[i] = 8;
  1743. }
  1744. dst_size[i] = 8;
  1745. } break;
  1746. case VS::ARRAY_BONES: {
  1747. if (p_format & VS::ARRAY_FLAG_USE_16_BIT_BONES) {
  1748. src_size[i] = 8;
  1749. dst_size[i] = 8;
  1750. } else {
  1751. src_size[i] = 4;
  1752. dst_size[i] = 4;
  1753. }
  1754. } break;
  1755. case VS::ARRAY_WEIGHTS: {
  1756. if (p_format & VS::ARRAY_COMPRESS_WEIGHTS) {
  1757. src_size[i] = 8;
  1758. dst_size[i] = 8;
  1759. } else {
  1760. src_size[i] = 16;
  1761. dst_size[i] = 16;
  1762. }
  1763. } break;
  1764. case VS::ARRAY_INDEX: {
  1765. src_size[i] = 0;
  1766. dst_size[i] = 0;
  1767. } break;
  1768. }
  1769. src_stride += src_size[i];
  1770. dst_stride += dst_size[i];
  1771. }
  1772. PoolVector<uint8_t> ret;
  1773. ret.resize(p_vertices * dst_stride);
  1774. PoolVector<uint8_t>::Read r = array.read();
  1775. PoolVector<uint8_t>::Write w = ret.write();
  1776. int src_offset = 0;
  1777. int dst_offset = 0;
  1778. for (int i = 0; i < VS::ARRAY_MAX; i++) {
  1779. if (src_size[i] == 0) {
  1780. continue; //no go
  1781. }
  1782. const uint8_t *rptr = r.ptr();
  1783. uint8_t *wptr = w.ptr();
  1784. if (to_convert[i]) { //converting
  1785. for (int j = 0; j < p_vertices; j++) {
  1786. const uint16_t *src = (const uint16_t *)&rptr[src_stride * j + src_offset];
  1787. float *dst = (float *)&wptr[dst_stride * j + dst_offset];
  1788. for (int k = 0; k < to_convert[i]; k++) {
  1789. dst[k] = Math::half_to_float(src[k]);
  1790. }
  1791. }
  1792. } else {
  1793. //just copy
  1794. for (int j = 0; j < p_vertices; j++) {
  1795. for (int k = 0; k < src_size[i]; k++) {
  1796. wptr[dst_stride * j + dst_offset + k] = rptr[src_stride * j + src_offset + k];
  1797. }
  1798. }
  1799. }
  1800. src_offset += src_size[i];
  1801. dst_offset += dst_size[i];
  1802. }
  1803. r.release();
  1804. w.release();
  1805. return ret;
  1806. }
  1807. void RasterizerStorageGLES2::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) {
  1808. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1809. ERR_FAIL_COND(!mesh);
  1810. ERR_FAIL_COND(!(p_format & VS::ARRAY_FORMAT_VERTEX));
  1811. //must have index and bones, both.
  1812. {
  1813. uint32_t bones_weight = VS::ARRAY_FORMAT_BONES | VS::ARRAY_FORMAT_WEIGHTS;
  1814. 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.");
  1815. }
  1816. //bool has_morph = p_blend_shapes.size();
  1817. bool use_split_stream = GLOBAL_GET("rendering/misc/mesh_storage/split_stream") && !(p_format & VS::ARRAY_FLAG_USE_DYNAMIC_UPDATE);
  1818. Surface::Attrib attribs[VS::ARRAY_MAX];
  1819. int attributes_base_offset = 0;
  1820. int attributes_stride = 0;
  1821. int positions_stride = 0;
  1822. bool uses_half_float = false;
  1823. for (int i = 0; i < VS::ARRAY_MAX; i++) {
  1824. attribs[i].index = i;
  1825. if (!(p_format & (1 << i))) {
  1826. attribs[i].enabled = false;
  1827. attribs[i].integer = false;
  1828. continue;
  1829. }
  1830. attribs[i].enabled = true;
  1831. attribs[i].offset = attributes_base_offset + attributes_stride;
  1832. attribs[i].integer = false;
  1833. switch (i) {
  1834. case VS::ARRAY_VERTEX: {
  1835. if (p_format & VS::ARRAY_FLAG_USE_2D_VERTICES) {
  1836. attribs[i].size = 2;
  1837. } else {
  1838. attribs[i].size = (p_format & VS::ARRAY_COMPRESS_VERTEX) ? 4 : 3;
  1839. }
  1840. if (p_format & VS::ARRAY_COMPRESS_VERTEX) {
  1841. attribs[i].type = _GL_HALF_FLOAT_OES;
  1842. positions_stride += attribs[i].size * 2;
  1843. uses_half_float = true;
  1844. } else {
  1845. attribs[i].type = GL_FLOAT;
  1846. positions_stride += attribs[i].size * 4;
  1847. }
  1848. attribs[i].normalized = GL_FALSE;
  1849. if (use_split_stream) {
  1850. attributes_base_offset = positions_stride * p_vertex_count;
  1851. } else {
  1852. attributes_base_offset = positions_stride;
  1853. }
  1854. } break;
  1855. case VS::ARRAY_NORMAL: {
  1856. if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
  1857. // Always pack normal and tangent into vec4
  1858. // normal will be xy tangent will be zw
  1859. // normal will always be oct32 encoded
  1860. // UNLESS tangent exists and is also compressed
  1861. // then it will be oct16 encoded along with tangent
  1862. attribs[i].normalized = GL_TRUE;
  1863. attribs[i].size = 2;
  1864. attribs[i].type = GL_SHORT;
  1865. attributes_stride += 4;
  1866. } else {
  1867. attribs[i].size = 3;
  1868. if (p_format & VS::ARRAY_COMPRESS_NORMAL) {
  1869. attribs[i].type = GL_BYTE;
  1870. attributes_stride += 4; //pad extra byte
  1871. attribs[i].normalized = GL_TRUE;
  1872. } else {
  1873. attribs[i].type = GL_FLOAT;
  1874. attributes_stride += 12;
  1875. attribs[i].normalized = GL_FALSE;
  1876. }
  1877. }
  1878. } break;
  1879. case VS::ARRAY_TANGENT: {
  1880. if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
  1881. attribs[i].enabled = false;
  1882. attribs[VS::ARRAY_NORMAL].size = 4;
  1883. if (p_format & VS::ARRAY_COMPRESS_TANGENT && p_format & VS::ARRAY_COMPRESS_NORMAL) {
  1884. // normal and tangent will each be oct16 (2 bytes each)
  1885. // pack into single vec4<GL_BYTE> for memory bandwidth
  1886. // savings while keeping 4 byte alignment
  1887. attribs[VS::ARRAY_NORMAL].type = GL_BYTE;
  1888. } else {
  1889. // normal and tangent will each be oct32 (4 bytes each)
  1890. attributes_stride += 4;
  1891. }
  1892. } else {
  1893. attribs[i].size = 4;
  1894. if (p_format & VS::ARRAY_COMPRESS_TANGENT) {
  1895. attribs[i].type = GL_BYTE;
  1896. attributes_stride += 4;
  1897. attribs[i].normalized = GL_TRUE;
  1898. } else {
  1899. attribs[i].type = GL_FLOAT;
  1900. attributes_stride += 16;
  1901. attribs[i].normalized = GL_FALSE;
  1902. }
  1903. }
  1904. } break;
  1905. case VS::ARRAY_COLOR: {
  1906. attribs[i].size = 4;
  1907. if (p_format & VS::ARRAY_COMPRESS_COLOR) {
  1908. attribs[i].type = GL_UNSIGNED_BYTE;
  1909. attributes_stride += 4;
  1910. attribs[i].normalized = GL_TRUE;
  1911. } else {
  1912. attribs[i].type = GL_FLOAT;
  1913. attributes_stride += 16;
  1914. attribs[i].normalized = GL_FALSE;
  1915. }
  1916. } break;
  1917. case VS::ARRAY_TEX_UV: {
  1918. attribs[i].size = 2;
  1919. if (p_format & VS::ARRAY_COMPRESS_TEX_UV) {
  1920. attribs[i].type = _GL_HALF_FLOAT_OES;
  1921. attributes_stride += 4;
  1922. uses_half_float = true;
  1923. } else {
  1924. attribs[i].type = GL_FLOAT;
  1925. attributes_stride += 8;
  1926. }
  1927. attribs[i].normalized = GL_FALSE;
  1928. } break;
  1929. case VS::ARRAY_TEX_UV2: {
  1930. attribs[i].size = 2;
  1931. if (p_format & VS::ARRAY_COMPRESS_TEX_UV2) {
  1932. attribs[i].type = _GL_HALF_FLOAT_OES;
  1933. attributes_stride += 4;
  1934. uses_half_float = true;
  1935. } else {
  1936. attribs[i].type = GL_FLOAT;
  1937. attributes_stride += 8;
  1938. }
  1939. attribs[i].normalized = GL_FALSE;
  1940. } break;
  1941. case VS::ARRAY_BONES: {
  1942. attribs[i].size = 4;
  1943. if (p_format & VS::ARRAY_FLAG_USE_16_BIT_BONES) {
  1944. attribs[i].type = GL_UNSIGNED_SHORT;
  1945. attributes_stride += 8;
  1946. } else {
  1947. attribs[i].type = GL_UNSIGNED_BYTE;
  1948. attributes_stride += 4;
  1949. }
  1950. attribs[i].normalized = GL_FALSE;
  1951. attribs[i].integer = true;
  1952. } break;
  1953. case VS::ARRAY_WEIGHTS: {
  1954. attribs[i].size = 4;
  1955. if (p_format & VS::ARRAY_COMPRESS_WEIGHTS) {
  1956. attribs[i].type = GL_UNSIGNED_SHORT;
  1957. attributes_stride += 8;
  1958. attribs[i].normalized = GL_TRUE;
  1959. } else {
  1960. attribs[i].type = GL_FLOAT;
  1961. attributes_stride += 16;
  1962. attribs[i].normalized = GL_FALSE;
  1963. }
  1964. } break;
  1965. case VS::ARRAY_INDEX: {
  1966. attribs[i].size = 1;
  1967. if (p_vertex_count >= (1 << 16)) {
  1968. attribs[i].type = GL_UNSIGNED_INT;
  1969. attribs[i].stride = 4;
  1970. } else {
  1971. attribs[i].type = GL_UNSIGNED_SHORT;
  1972. attribs[i].stride = 2;
  1973. }
  1974. attribs[i].normalized = GL_FALSE;
  1975. } break;
  1976. }
  1977. }
  1978. if (use_split_stream) {
  1979. attribs[VS::ARRAY_VERTEX].stride = positions_stride;
  1980. for (int i = 1; i < VS::ARRAY_MAX - 1; i++) {
  1981. attribs[i].stride = attributes_stride;
  1982. }
  1983. } else {
  1984. for (int i = 0; i < VS::ARRAY_MAX - 1; i++) {
  1985. attribs[i].stride = positions_stride + attributes_stride;
  1986. }
  1987. }
  1988. //validate sizes
  1989. PoolVector<uint8_t> array = p_array;
  1990. int stride = positions_stride + attributes_stride;
  1991. int array_size = stride * p_vertex_count;
  1992. int index_array_size = 0;
  1993. if (array.size() != array_size && array.size() + p_vertex_count * 2 == array_size) {
  1994. //old format, convert
  1995. array = PoolVector<uint8_t>();
  1996. array.resize(p_array.size() + p_vertex_count * 2);
  1997. PoolVector<uint8_t>::Write w = array.write();
  1998. PoolVector<uint8_t>::Read r = p_array.read();
  1999. uint16_t *w16 = (uint16_t *)w.ptr();
  2000. const uint16_t *r16 = (uint16_t *)r.ptr();
  2001. uint16_t one = Math::make_half_float(1);
  2002. for (int i = 0; i < p_vertex_count; i++) {
  2003. *w16++ = *r16++;
  2004. *w16++ = *r16++;
  2005. *w16++ = *r16++;
  2006. *w16++ = one;
  2007. for (int j = 0; j < (stride / 2) - 4; j++) {
  2008. *w16++ = *r16++;
  2009. }
  2010. }
  2011. }
  2012. ERR_FAIL_COND(array.size() != array_size);
  2013. if (!config.support_half_float_vertices && uses_half_float) {
  2014. uint32_t new_format = p_format;
  2015. PoolVector<uint8_t> unpacked_array = _unpack_half_floats(array, new_format, p_vertex_count);
  2016. Vector<PoolVector<uint8_t>> unpacked_blend_shapes;
  2017. for (int i = 0; i < p_blend_shapes.size(); i++) {
  2018. uint32_t temp_format = p_format; // Just throw this away as it will be the same as new_format
  2019. unpacked_blend_shapes.push_back(_unpack_half_floats(p_blend_shapes[i], temp_format, p_vertex_count));
  2020. }
  2021. mesh_add_surface(p_mesh, new_format, p_primitive, unpacked_array, p_vertex_count, p_index_array, p_index_count, p_aabb, unpacked_blend_shapes, p_bone_aabbs);
  2022. return; //do not go any further, above function used unpacked stuff will be used instead.
  2023. }
  2024. if (p_format & VS::ARRAY_FORMAT_INDEX) {
  2025. index_array_size = attribs[VS::ARRAY_INDEX].stride * p_index_count;
  2026. }
  2027. ERR_FAIL_COND(p_index_array.size() != index_array_size);
  2028. ERR_FAIL_COND(p_blend_shapes.size() != mesh->blend_shape_count);
  2029. for (int i = 0; i < p_blend_shapes.size(); i++) {
  2030. ERR_FAIL_COND(p_blend_shapes[i].size() != array_size);
  2031. }
  2032. // all valid, create stuff
  2033. Surface *surface = memnew(Surface);
  2034. surface->active = true;
  2035. surface->array_len = p_vertex_count;
  2036. surface->index_array_len = p_index_count;
  2037. surface->array_byte_size = array.size();
  2038. surface->index_array_byte_size = p_index_array.size();
  2039. surface->primitive = p_primitive;
  2040. surface->mesh = mesh;
  2041. surface->format = p_format;
  2042. surface->skeleton_bone_aabb = p_bone_aabbs;
  2043. surface->skeleton_bone_used.resize(surface->skeleton_bone_aabb.size());
  2044. surface->aabb = p_aabb;
  2045. surface->max_bone = p_bone_aabbs.size();
  2046. surface->blend_shape_data = p_blend_shapes;
  2047. surface->data = array;
  2048. surface->index_data = p_index_array;
  2049. surface->total_data_size += surface->array_byte_size + surface->index_array_byte_size;
  2050. for (int i = 0; i < surface->skeleton_bone_used.size(); i++) {
  2051. surface->skeleton_bone_used.write[i] = !(surface->skeleton_bone_aabb[i].size.x < 0 || surface->skeleton_bone_aabb[i].size.y < 0 || surface->skeleton_bone_aabb[i].size.z < 0);
  2052. }
  2053. for (int i = 0; i < VS::ARRAY_MAX; i++) {
  2054. surface->attribs[i] = attribs[i];
  2055. }
  2056. // Okay, now the OpenGL stuff, wheeeeey \o/
  2057. {
  2058. PoolVector<uint8_t>::Read vr = array.read();
  2059. glGenBuffers(1, &surface->vertex_id);
  2060. glBindBuffer(GL_ARRAY_BUFFER, surface->vertex_id);
  2061. glBufferData(GL_ARRAY_BUFFER, array_size, vr.ptr(), (p_format & VS::ARRAY_FLAG_USE_DYNAMIC_UPDATE) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  2062. glBindBuffer(GL_ARRAY_BUFFER, 0);
  2063. if (p_format & VS::ARRAY_FORMAT_INDEX) {
  2064. PoolVector<uint8_t>::Read ir = p_index_array.read();
  2065. glGenBuffers(1, &surface->index_id);
  2066. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, surface->index_id);
  2067. glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_array_size, ir.ptr(), GL_STATIC_DRAW);
  2068. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  2069. } else {
  2070. surface->index_id = 0;
  2071. }
  2072. // TODO generate wireframes
  2073. // Make one blend shape buffer per surface
  2074. {
  2075. surface->blend_shape_buffer_size = 0;
  2076. glGenBuffers(1, &surface->blend_shape_buffer_id);
  2077. }
  2078. }
  2079. mesh->surfaces.push_back(surface);
  2080. mesh->instance_change_notify(true, true);
  2081. info.vertex_mem += surface->total_data_size;
  2082. }
  2083. void RasterizerStorageGLES2::mesh_set_blend_shape_count(RID p_mesh, int p_amount) {
  2084. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2085. ERR_FAIL_COND(!mesh);
  2086. ERR_FAIL_COND(mesh->surfaces.size() != 0);
  2087. ERR_FAIL_COND(p_amount < 0);
  2088. mesh->blend_shape_count = p_amount;
  2089. mesh->instance_change_notify(true, false);
  2090. if (!mesh->update_list.in_list()) {
  2091. blend_shapes_update_list.add(&mesh->update_list);
  2092. }
  2093. }
  2094. int RasterizerStorageGLES2::mesh_get_blend_shape_count(RID p_mesh) const {
  2095. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2096. ERR_FAIL_COND_V(!mesh, 0);
  2097. return mesh->blend_shape_count;
  2098. }
  2099. void RasterizerStorageGLES2::mesh_set_blend_shape_mode(RID p_mesh, VS::BlendShapeMode p_mode) {
  2100. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2101. ERR_FAIL_COND(!mesh);
  2102. mesh->blend_shape_mode = p_mode;
  2103. if (!mesh->update_list.in_list()) {
  2104. blend_shapes_update_list.add(&mesh->update_list);
  2105. }
  2106. }
  2107. VS::BlendShapeMode RasterizerStorageGLES2::mesh_get_blend_shape_mode(RID p_mesh) const {
  2108. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2109. ERR_FAIL_COND_V(!mesh, VS::BLEND_SHAPE_MODE_NORMALIZED);
  2110. return mesh->blend_shape_mode;
  2111. }
  2112. void RasterizerStorageGLES2::mesh_set_blend_shape_values(RID p_mesh, PoolVector<float> p_values) {
  2113. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2114. ERR_FAIL_COND(!mesh);
  2115. mesh->blend_shape_values = p_values;
  2116. if (!mesh->update_list.in_list()) {
  2117. blend_shapes_update_list.add(&mesh->update_list);
  2118. }
  2119. }
  2120. PoolVector<float> RasterizerStorageGLES2::mesh_get_blend_shape_values(RID p_mesh) const {
  2121. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2122. ERR_FAIL_COND_V(!mesh, PoolVector<float>());
  2123. return mesh->blend_shape_values;
  2124. }
  2125. void RasterizerStorageGLES2::mesh_surface_update_region(RID p_mesh, int p_surface, int p_offset, const PoolVector<uint8_t> &p_data) {
  2126. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2127. ERR_FAIL_COND(!mesh);
  2128. ERR_FAIL_INDEX(p_surface, mesh->surfaces.size());
  2129. int total_size = p_data.size();
  2130. ERR_FAIL_COND(p_offset + total_size > mesh->surfaces[p_surface]->array_byte_size);
  2131. PoolVector<uint8_t>::Read r = p_data.read();
  2132. glBindBuffer(GL_ARRAY_BUFFER, mesh->surfaces[p_surface]->vertex_id);
  2133. glBufferSubData(GL_ARRAY_BUFFER, p_offset, total_size, r.ptr());
  2134. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  2135. }
  2136. void RasterizerStorageGLES2::mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) {
  2137. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2138. ERR_FAIL_COND(!mesh);
  2139. ERR_FAIL_INDEX(p_surface, mesh->surfaces.size());
  2140. if (mesh->surfaces[p_surface]->material == p_material) {
  2141. return;
  2142. }
  2143. if (mesh->surfaces[p_surface]->material.is_valid()) {
  2144. _material_remove_geometry(mesh->surfaces[p_surface]->material, mesh->surfaces[p_surface]);
  2145. }
  2146. mesh->surfaces[p_surface]->material = p_material;
  2147. if (mesh->surfaces[p_surface]->material.is_valid()) {
  2148. _material_add_geometry(mesh->surfaces[p_surface]->material, mesh->surfaces[p_surface]);
  2149. }
  2150. mesh->instance_change_notify(false, true);
  2151. }
  2152. RID RasterizerStorageGLES2::mesh_surface_get_material(RID p_mesh, int p_surface) const {
  2153. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2154. ERR_FAIL_COND_V(!mesh, RID());
  2155. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), RID());
  2156. return mesh->surfaces[p_surface]->material;
  2157. }
  2158. int RasterizerStorageGLES2::mesh_surface_get_array_len(RID p_mesh, int p_surface) const {
  2159. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2160. ERR_FAIL_COND_V(!mesh, 0);
  2161. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), 0);
  2162. return mesh->surfaces[p_surface]->array_len;
  2163. }
  2164. int RasterizerStorageGLES2::mesh_surface_get_array_index_len(RID p_mesh, int p_surface) const {
  2165. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2166. ERR_FAIL_COND_V(!mesh, 0);
  2167. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), 0);
  2168. return mesh->surfaces[p_surface]->index_array_len;
  2169. }
  2170. PoolVector<uint8_t> RasterizerStorageGLES2::mesh_surface_get_array(RID p_mesh, int p_surface) const {
  2171. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2172. ERR_FAIL_COND_V(!mesh, PoolVector<uint8_t>());
  2173. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), PoolVector<uint8_t>());
  2174. Surface *surface = mesh->surfaces[p_surface];
  2175. return surface->data;
  2176. }
  2177. PoolVector<uint8_t> RasterizerStorageGLES2::mesh_surface_get_index_array(RID p_mesh, int p_surface) const {
  2178. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2179. ERR_FAIL_COND_V(!mesh, PoolVector<uint8_t>());
  2180. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), PoolVector<uint8_t>());
  2181. Surface *surface = mesh->surfaces[p_surface];
  2182. return surface->index_data;
  2183. }
  2184. uint32_t RasterizerStorageGLES2::mesh_surface_get_format(RID p_mesh, int p_surface) const {
  2185. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2186. ERR_FAIL_COND_V(!mesh, 0);
  2187. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), 0);
  2188. return mesh->surfaces[p_surface]->format;
  2189. }
  2190. VS::PrimitiveType RasterizerStorageGLES2::mesh_surface_get_primitive_type(RID p_mesh, int p_surface) const {
  2191. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2192. ERR_FAIL_COND_V(!mesh, VS::PRIMITIVE_MAX);
  2193. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), VS::PRIMITIVE_MAX);
  2194. return mesh->surfaces[p_surface]->primitive;
  2195. }
  2196. AABB RasterizerStorageGLES2::mesh_surface_get_aabb(RID p_mesh, int p_surface) const {
  2197. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2198. ERR_FAIL_COND_V(!mesh, AABB());
  2199. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), AABB());
  2200. return mesh->surfaces[p_surface]->aabb;
  2201. }
  2202. Vector<PoolVector<uint8_t>> RasterizerStorageGLES2::mesh_surface_get_blend_shapes(RID p_mesh, int p_surface) const {
  2203. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2204. ERR_FAIL_COND_V(!mesh, Vector<PoolVector<uint8_t>>());
  2205. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), Vector<PoolVector<uint8_t>>());
  2206. return mesh->surfaces[p_surface]->blend_shape_data;
  2207. }
  2208. Vector<AABB> RasterizerStorageGLES2::mesh_surface_get_skeleton_aabb(RID p_mesh, int p_surface) const {
  2209. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2210. ERR_FAIL_COND_V(!mesh, Vector<AABB>());
  2211. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), Vector<AABB>());
  2212. return mesh->surfaces[p_surface]->skeleton_bone_aabb;
  2213. }
  2214. void RasterizerStorageGLES2::mesh_remove_surface(RID p_mesh, int p_surface) {
  2215. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2216. ERR_FAIL_COND(!mesh);
  2217. ERR_FAIL_INDEX(p_surface, mesh->surfaces.size());
  2218. Surface *surface = mesh->surfaces[p_surface];
  2219. if (surface->material.is_valid()) {
  2220. _material_remove_geometry(surface->material, mesh->surfaces[p_surface]);
  2221. }
  2222. glDeleteBuffers(1, &surface->vertex_id);
  2223. if (surface->index_id) {
  2224. glDeleteBuffers(1, &surface->index_id);
  2225. }
  2226. glDeleteBuffers(1, &surface->blend_shape_buffer_id);
  2227. info.vertex_mem -= surface->total_data_size;
  2228. memdelete(surface);
  2229. mesh->surfaces.remove(p_surface);
  2230. mesh->instance_change_notify(true, true);
  2231. }
  2232. int RasterizerStorageGLES2::mesh_get_surface_count(RID p_mesh) const {
  2233. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2234. ERR_FAIL_COND_V(!mesh, 0);
  2235. return mesh->surfaces.size();
  2236. }
  2237. void RasterizerStorageGLES2::mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) {
  2238. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2239. ERR_FAIL_COND(!mesh);
  2240. mesh->custom_aabb = p_aabb;
  2241. mesh->instance_change_notify(true, false);
  2242. }
  2243. AABB RasterizerStorageGLES2::mesh_get_custom_aabb(RID p_mesh) const {
  2244. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2245. ERR_FAIL_COND_V(!mesh, AABB());
  2246. return mesh->custom_aabb;
  2247. }
  2248. AABB RasterizerStorageGLES2::mesh_get_aabb(RID p_mesh, RID p_skeleton) const {
  2249. Mesh *mesh = mesh_owner.get(p_mesh);
  2250. ERR_FAIL_COND_V(!mesh, AABB());
  2251. if (mesh->custom_aabb != AABB()) {
  2252. return mesh->custom_aabb;
  2253. }
  2254. Skeleton *sk = nullptr;
  2255. if (p_skeleton.is_valid()) {
  2256. sk = skeleton_owner.get(p_skeleton);
  2257. }
  2258. AABB aabb;
  2259. if (sk && sk->size != 0) {
  2260. for (int i = 0; i < mesh->surfaces.size(); i++) {
  2261. AABB laabb;
  2262. if ((mesh->surfaces[i]->format & VS::ARRAY_FORMAT_BONES) && mesh->surfaces[i]->skeleton_bone_aabb.size()) {
  2263. int bs = mesh->surfaces[i]->skeleton_bone_aabb.size();
  2264. const AABB *skbones = mesh->surfaces[i]->skeleton_bone_aabb.ptr();
  2265. const bool *skused = mesh->surfaces[i]->skeleton_bone_used.ptr();
  2266. int sbs = sk->size;
  2267. ERR_CONTINUE(bs > sbs);
  2268. const float *texture = sk->bone_data.ptr();
  2269. bool first = true;
  2270. if (sk->use_2d) {
  2271. for (int j = 0; j < bs; j++) {
  2272. if (!skused[j]) {
  2273. continue;
  2274. }
  2275. int base_ofs = j * 2 * 4;
  2276. Transform mtx;
  2277. mtx.basis[0].x = texture[base_ofs + 0];
  2278. mtx.basis[0].y = texture[base_ofs + 1];
  2279. mtx.origin.x = texture[base_ofs + 3];
  2280. base_ofs += 4;
  2281. mtx.basis[1].x = texture[base_ofs + 0];
  2282. mtx.basis[1].y = texture[base_ofs + 1];
  2283. mtx.origin.y = texture[base_ofs + 3];
  2284. AABB baabb = mtx.xform(skbones[j]);
  2285. if (first) {
  2286. laabb = baabb;
  2287. first = false;
  2288. } else {
  2289. laabb.merge_with(baabb);
  2290. }
  2291. }
  2292. } else {
  2293. for (int j = 0; j < bs; j++) {
  2294. if (!skused[j]) {
  2295. continue;
  2296. }
  2297. int base_ofs = j * 3 * 4;
  2298. Transform mtx;
  2299. mtx.basis[0].x = texture[base_ofs + 0];
  2300. mtx.basis[0].y = texture[base_ofs + 1];
  2301. mtx.basis[0].z = texture[base_ofs + 2];
  2302. mtx.origin.x = texture[base_ofs + 3];
  2303. base_ofs += 4;
  2304. mtx.basis[1].x = texture[base_ofs + 0];
  2305. mtx.basis[1].y = texture[base_ofs + 1];
  2306. mtx.basis[1].z = texture[base_ofs + 2];
  2307. mtx.origin.y = texture[base_ofs + 3];
  2308. base_ofs += 4;
  2309. mtx.basis[2].x = texture[base_ofs + 0];
  2310. mtx.basis[2].y = texture[base_ofs + 1];
  2311. mtx.basis[2].z = texture[base_ofs + 2];
  2312. mtx.origin.z = texture[base_ofs + 3];
  2313. AABB baabb = mtx.xform(skbones[j]);
  2314. if (first) {
  2315. laabb = baabb;
  2316. first = false;
  2317. } else {
  2318. laabb.merge_with(baabb);
  2319. }
  2320. }
  2321. }
  2322. } else {
  2323. laabb = mesh->surfaces[i]->aabb;
  2324. }
  2325. if (i == 0) {
  2326. aabb = laabb;
  2327. } else {
  2328. aabb.merge_with(laabb);
  2329. }
  2330. }
  2331. } else {
  2332. for (int i = 0; i < mesh->surfaces.size(); i++) {
  2333. if (i == 0) {
  2334. aabb = mesh->surfaces[i]->aabb;
  2335. } else {
  2336. aabb.merge_with(mesh->surfaces[i]->aabb);
  2337. }
  2338. }
  2339. }
  2340. return aabb;
  2341. }
  2342. void RasterizerStorageGLES2::mesh_clear(RID p_mesh) {
  2343. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2344. ERR_FAIL_COND(!mesh);
  2345. while (mesh->surfaces.size()) {
  2346. mesh_remove_surface(p_mesh, 0);
  2347. }
  2348. }
  2349. /* MULTIMESH API */
  2350. RID RasterizerStorageGLES2::_multimesh_create() {
  2351. MultiMesh *multimesh = memnew(MultiMesh);
  2352. return multimesh_owner.make_rid(multimesh);
  2353. }
  2354. void RasterizerStorageGLES2::_multimesh_allocate(RID p_multimesh, int p_instances, VS::MultimeshTransformFormat p_transform_format, VS::MultimeshColorFormat p_color_format, VS::MultimeshCustomDataFormat p_data) {
  2355. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2356. ERR_FAIL_COND(!multimesh);
  2357. if (multimesh->size == p_instances && multimesh->transform_format == p_transform_format && multimesh->color_format == p_color_format && multimesh->custom_data_format == p_data) {
  2358. return;
  2359. }
  2360. multimesh->size = p_instances;
  2361. multimesh->color_format = p_color_format;
  2362. multimesh->transform_format = p_transform_format;
  2363. multimesh->custom_data_format = p_data;
  2364. if (multimesh->size) {
  2365. multimesh->data.resize(0);
  2366. }
  2367. if (multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D) {
  2368. multimesh->xform_floats = 8;
  2369. } else {
  2370. multimesh->xform_floats = 12;
  2371. }
  2372. if (multimesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
  2373. multimesh->color_floats = 1;
  2374. } else if (multimesh->color_format == VS::MULTIMESH_COLOR_FLOAT) {
  2375. multimesh->color_floats = 4;
  2376. } else {
  2377. multimesh->color_floats = 0;
  2378. }
  2379. if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
  2380. multimesh->custom_data_floats = 1;
  2381. } else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_FLOAT) {
  2382. multimesh->custom_data_floats = 4;
  2383. } else {
  2384. multimesh->custom_data_floats = 0;
  2385. }
  2386. int format_floats = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  2387. multimesh->data.resize(format_floats * p_instances);
  2388. for (int i = 0; i < p_instances * format_floats; i += format_floats) {
  2389. int color_from = 0;
  2390. int custom_data_from = 0;
  2391. if (multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D) {
  2392. multimesh->data.write[i + 0] = 1.0;
  2393. multimesh->data.write[i + 1] = 0.0;
  2394. multimesh->data.write[i + 2] = 0.0;
  2395. multimesh->data.write[i + 3] = 0.0;
  2396. multimesh->data.write[i + 4] = 0.0;
  2397. multimesh->data.write[i + 5] = 1.0;
  2398. multimesh->data.write[i + 6] = 0.0;
  2399. multimesh->data.write[i + 7] = 0.0;
  2400. color_from = 8;
  2401. custom_data_from = 8;
  2402. } else {
  2403. multimesh->data.write[i + 0] = 1.0;
  2404. multimesh->data.write[i + 1] = 0.0;
  2405. multimesh->data.write[i + 2] = 0.0;
  2406. multimesh->data.write[i + 3] = 0.0;
  2407. multimesh->data.write[i + 4] = 0.0;
  2408. multimesh->data.write[i + 5] = 1.0;
  2409. multimesh->data.write[i + 6] = 0.0;
  2410. multimesh->data.write[i + 7] = 0.0;
  2411. multimesh->data.write[i + 8] = 0.0;
  2412. multimesh->data.write[i + 9] = 0.0;
  2413. multimesh->data.write[i + 10] = 1.0;
  2414. multimesh->data.write[i + 11] = 0.0;
  2415. color_from = 12;
  2416. custom_data_from = 12;
  2417. }
  2418. if (multimesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
  2419. union {
  2420. uint32_t colu;
  2421. float colf;
  2422. } cu;
  2423. cu.colu = 0xFFFFFFFF;
  2424. multimesh->data.write[i + color_from + 0] = cu.colf;
  2425. custom_data_from = color_from + 1;
  2426. } else if (multimesh->color_format == VS::MULTIMESH_COLOR_FLOAT) {
  2427. multimesh->data.write[i + color_from + 0] = 1.0;
  2428. multimesh->data.write[i + color_from + 1] = 1.0;
  2429. multimesh->data.write[i + color_from + 2] = 1.0;
  2430. multimesh->data.write[i + color_from + 3] = 1.0;
  2431. custom_data_from = color_from + 4;
  2432. }
  2433. if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
  2434. union {
  2435. uint32_t colu;
  2436. float colf;
  2437. } cu;
  2438. cu.colu = 0;
  2439. multimesh->data.write[i + custom_data_from + 0] = cu.colf;
  2440. } else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_FLOAT) {
  2441. multimesh->data.write[i + custom_data_from + 0] = 0.0;
  2442. multimesh->data.write[i + custom_data_from + 1] = 0.0;
  2443. multimesh->data.write[i + custom_data_from + 2] = 0.0;
  2444. multimesh->data.write[i + custom_data_from + 3] = 0.0;
  2445. }
  2446. }
  2447. multimesh->dirty_aabb = true;
  2448. multimesh->dirty_data = true;
  2449. if (!multimesh->update_list.in_list()) {
  2450. multimesh_update_list.add(&multimesh->update_list);
  2451. }
  2452. }
  2453. int RasterizerStorageGLES2::_multimesh_get_instance_count(RID p_multimesh) const {
  2454. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2455. ERR_FAIL_COND_V(!multimesh, 0);
  2456. return multimesh->size;
  2457. }
  2458. void RasterizerStorageGLES2::_multimesh_set_mesh(RID p_multimesh, RID p_mesh) {
  2459. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2460. ERR_FAIL_COND(!multimesh);
  2461. if (multimesh->mesh.is_valid()) {
  2462. Mesh *mesh = mesh_owner.getornull(multimesh->mesh);
  2463. if (mesh) {
  2464. mesh->multimeshes.remove(&multimesh->mesh_list);
  2465. }
  2466. }
  2467. multimesh->mesh = p_mesh;
  2468. if (multimesh->mesh.is_valid()) {
  2469. Mesh *mesh = mesh_owner.getornull(multimesh->mesh);
  2470. if (mesh) {
  2471. mesh->multimeshes.add(&multimesh->mesh_list);
  2472. }
  2473. }
  2474. multimesh->dirty_aabb = true;
  2475. if (!multimesh->update_list.in_list()) {
  2476. multimesh_update_list.add(&multimesh->update_list);
  2477. }
  2478. }
  2479. void RasterizerStorageGLES2::_multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform) {
  2480. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2481. ERR_FAIL_COND(!multimesh);
  2482. ERR_FAIL_INDEX(p_index, multimesh->size);
  2483. ERR_FAIL_COND(multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D);
  2484. int stride = multimesh->color_floats + multimesh->custom_data_floats + multimesh->xform_floats;
  2485. float *dataptr = &multimesh->data.write[stride * p_index];
  2486. dataptr[0] = p_transform.basis.elements[0][0];
  2487. dataptr[1] = p_transform.basis.elements[0][1];
  2488. dataptr[2] = p_transform.basis.elements[0][2];
  2489. dataptr[3] = p_transform.origin.x;
  2490. dataptr[4] = p_transform.basis.elements[1][0];
  2491. dataptr[5] = p_transform.basis.elements[1][1];
  2492. dataptr[6] = p_transform.basis.elements[1][2];
  2493. dataptr[7] = p_transform.origin.y;
  2494. dataptr[8] = p_transform.basis.elements[2][0];
  2495. dataptr[9] = p_transform.basis.elements[2][1];
  2496. dataptr[10] = p_transform.basis.elements[2][2];
  2497. dataptr[11] = p_transform.origin.z;
  2498. multimesh->dirty_data = true;
  2499. multimesh->dirty_aabb = true;
  2500. if (!multimesh->update_list.in_list()) {
  2501. multimesh_update_list.add(&multimesh->update_list);
  2502. }
  2503. }
  2504. void RasterizerStorageGLES2::_multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) {
  2505. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2506. ERR_FAIL_COND(!multimesh);
  2507. ERR_FAIL_INDEX(p_index, multimesh->size);
  2508. ERR_FAIL_COND(multimesh->transform_format == VS::MULTIMESH_TRANSFORM_3D);
  2509. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  2510. float *dataptr = &multimesh->data.write[stride * p_index];
  2511. dataptr[0] = p_transform.elements[0][0];
  2512. dataptr[1] = p_transform.elements[1][0];
  2513. dataptr[2] = 0;
  2514. dataptr[3] = p_transform.elements[2][0];
  2515. dataptr[4] = p_transform.elements[0][1];
  2516. dataptr[5] = p_transform.elements[1][1];
  2517. dataptr[6] = 0;
  2518. dataptr[7] = p_transform.elements[2][1];
  2519. multimesh->dirty_data = true;
  2520. multimesh->dirty_aabb = true;
  2521. if (!multimesh->update_list.in_list()) {
  2522. multimesh_update_list.add(&multimesh->update_list);
  2523. }
  2524. }
  2525. void RasterizerStorageGLES2::_multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) {
  2526. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2527. ERR_FAIL_COND(!multimesh);
  2528. ERR_FAIL_INDEX(p_index, multimesh->size);
  2529. ERR_FAIL_COND(multimesh->color_format == VS::MULTIMESH_COLOR_NONE);
  2530. ERR_FAIL_INDEX(multimesh->color_format, VS::MULTIMESH_COLOR_MAX);
  2531. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  2532. float *dataptr = &multimesh->data.write[stride * p_index + multimesh->xform_floats];
  2533. if (multimesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
  2534. uint8_t *data8 = (uint8_t *)dataptr;
  2535. data8[0] = CLAMP(p_color.r * 255.0, 0, 255);
  2536. data8[1] = CLAMP(p_color.g * 255.0, 0, 255);
  2537. data8[2] = CLAMP(p_color.b * 255.0, 0, 255);
  2538. data8[3] = CLAMP(p_color.a * 255.0, 0, 255);
  2539. } else if (multimesh->color_format == VS::MULTIMESH_COLOR_FLOAT) {
  2540. dataptr[0] = p_color.r;
  2541. dataptr[1] = p_color.g;
  2542. dataptr[2] = p_color.b;
  2543. dataptr[3] = p_color.a;
  2544. }
  2545. multimesh->dirty_data = true;
  2546. multimesh->dirty_aabb = true;
  2547. if (!multimesh->update_list.in_list()) {
  2548. multimesh_update_list.add(&multimesh->update_list);
  2549. }
  2550. }
  2551. void RasterizerStorageGLES2::_multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_custom_data) {
  2552. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2553. ERR_FAIL_COND(!multimesh);
  2554. ERR_FAIL_INDEX(p_index, multimesh->size);
  2555. ERR_FAIL_COND(multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_NONE);
  2556. ERR_FAIL_INDEX(multimesh->custom_data_format, VS::MULTIMESH_CUSTOM_DATA_MAX);
  2557. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  2558. float *dataptr = &multimesh->data.write[stride * p_index + multimesh->xform_floats + multimesh->color_floats];
  2559. if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
  2560. uint8_t *data8 = (uint8_t *)dataptr;
  2561. data8[0] = CLAMP(p_custom_data.r * 255.0, 0, 255);
  2562. data8[1] = CLAMP(p_custom_data.g * 255.0, 0, 255);
  2563. data8[2] = CLAMP(p_custom_data.b * 255.0, 0, 255);
  2564. data8[3] = CLAMP(p_custom_data.a * 255.0, 0, 255);
  2565. } else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_FLOAT) {
  2566. dataptr[0] = p_custom_data.r;
  2567. dataptr[1] = p_custom_data.g;
  2568. dataptr[2] = p_custom_data.b;
  2569. dataptr[3] = p_custom_data.a;
  2570. }
  2571. multimesh->dirty_data = true;
  2572. multimesh->dirty_aabb = true;
  2573. if (!multimesh->update_list.in_list()) {
  2574. multimesh_update_list.add(&multimesh->update_list);
  2575. }
  2576. }
  2577. RID RasterizerStorageGLES2::_multimesh_get_mesh(RID p_multimesh) const {
  2578. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2579. ERR_FAIL_COND_V(!multimesh, RID());
  2580. return multimesh->mesh;
  2581. }
  2582. Transform RasterizerStorageGLES2::_multimesh_instance_get_transform(RID p_multimesh, int p_index) const {
  2583. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2584. ERR_FAIL_COND_V(!multimesh, Transform());
  2585. ERR_FAIL_INDEX_V(p_index, multimesh->size, Transform());
  2586. ERR_FAIL_COND_V(multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D, Transform());
  2587. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  2588. float *dataptr = &multimesh->data.write[stride * p_index];
  2589. Transform xform;
  2590. xform.basis.elements[0][0] = dataptr[0];
  2591. xform.basis.elements[0][1] = dataptr[1];
  2592. xform.basis.elements[0][2] = dataptr[2];
  2593. xform.origin.x = dataptr[3];
  2594. xform.basis.elements[1][0] = dataptr[4];
  2595. xform.basis.elements[1][1] = dataptr[5];
  2596. xform.basis.elements[1][2] = dataptr[6];
  2597. xform.origin.y = dataptr[7];
  2598. xform.basis.elements[2][0] = dataptr[8];
  2599. xform.basis.elements[2][1] = dataptr[9];
  2600. xform.basis.elements[2][2] = dataptr[10];
  2601. xform.origin.z = dataptr[11];
  2602. return xform;
  2603. }
  2604. Transform2D RasterizerStorageGLES2::_multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const {
  2605. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2606. ERR_FAIL_COND_V(!multimesh, Transform2D());
  2607. ERR_FAIL_INDEX_V(p_index, multimesh->size, Transform2D());
  2608. ERR_FAIL_COND_V(multimesh->transform_format == VS::MULTIMESH_TRANSFORM_3D, Transform2D());
  2609. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  2610. float *dataptr = &multimesh->data.write[stride * p_index];
  2611. Transform2D xform;
  2612. xform.elements[0][0] = dataptr[0];
  2613. xform.elements[1][0] = dataptr[1];
  2614. xform.elements[2][0] = dataptr[3];
  2615. xform.elements[0][1] = dataptr[4];
  2616. xform.elements[1][1] = dataptr[5];
  2617. xform.elements[2][1] = dataptr[7];
  2618. return xform;
  2619. }
  2620. Color RasterizerStorageGLES2::_multimesh_instance_get_color(RID p_multimesh, int p_index) const {
  2621. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2622. ERR_FAIL_COND_V(!multimesh, Color());
  2623. ERR_FAIL_INDEX_V(p_index, multimesh->size, Color());
  2624. ERR_FAIL_COND_V(multimesh->color_format == VS::MULTIMESH_COLOR_NONE, Color());
  2625. ERR_FAIL_INDEX_V(multimesh->color_format, VS::MULTIMESH_COLOR_MAX, Color());
  2626. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  2627. float *dataptr = &multimesh->data.write[stride * p_index + multimesh->xform_floats];
  2628. if (multimesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
  2629. union {
  2630. uint32_t colu;
  2631. float colf;
  2632. } cu;
  2633. cu.colf = dataptr[0];
  2634. return Color::hex(BSWAP32(cu.colu));
  2635. } else if (multimesh->color_format == VS::MULTIMESH_COLOR_FLOAT) {
  2636. Color c;
  2637. c.r = dataptr[0];
  2638. c.g = dataptr[1];
  2639. c.b = dataptr[2];
  2640. c.a = dataptr[3];
  2641. return c;
  2642. }
  2643. return Color();
  2644. }
  2645. Color RasterizerStorageGLES2::_multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const {
  2646. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2647. ERR_FAIL_COND_V(!multimesh, Color());
  2648. ERR_FAIL_INDEX_V(p_index, multimesh->size, Color());
  2649. ERR_FAIL_COND_V(multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_NONE, Color());
  2650. ERR_FAIL_INDEX_V(multimesh->custom_data_format, VS::MULTIMESH_CUSTOM_DATA_MAX, Color());
  2651. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  2652. float *dataptr = &multimesh->data.write[stride * p_index + multimesh->xform_floats + multimesh->color_floats];
  2653. if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
  2654. union {
  2655. uint32_t colu;
  2656. float colf;
  2657. } cu;
  2658. cu.colf = dataptr[0];
  2659. return Color::hex(BSWAP32(cu.colu));
  2660. } else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_FLOAT) {
  2661. Color c;
  2662. c.r = dataptr[0];
  2663. c.g = dataptr[1];
  2664. c.b = dataptr[2];
  2665. c.a = dataptr[3];
  2666. return c;
  2667. }
  2668. return Color();
  2669. }
  2670. void RasterizerStorageGLES2::_multimesh_set_as_bulk_array(RID p_multimesh, const PoolVector<float> &p_array) {
  2671. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2672. ERR_FAIL_COND(!multimesh);
  2673. ERR_FAIL_COND(!multimesh->data.ptr());
  2674. int dsize = multimesh->data.size();
  2675. ERR_FAIL_COND(dsize != p_array.size());
  2676. PoolVector<float>::Read r = p_array.read();
  2677. ERR_FAIL_COND(!r.ptr());
  2678. memcpy(multimesh->data.ptrw(), r.ptr(), dsize * sizeof(float));
  2679. multimesh->dirty_data = true;
  2680. multimesh->dirty_aabb = true;
  2681. if (!multimesh->update_list.in_list()) {
  2682. multimesh_update_list.add(&multimesh->update_list);
  2683. }
  2684. }
  2685. void RasterizerStorageGLES2::_multimesh_set_visible_instances(RID p_multimesh, int p_visible) {
  2686. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2687. ERR_FAIL_COND(!multimesh);
  2688. multimesh->visible_instances = p_visible;
  2689. }
  2690. int RasterizerStorageGLES2::_multimesh_get_visible_instances(RID p_multimesh) const {
  2691. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2692. ERR_FAIL_COND_V(!multimesh, -1);
  2693. return multimesh->visible_instances;
  2694. }
  2695. AABB RasterizerStorageGLES2::_multimesh_get_aabb(RID p_multimesh) const {
  2696. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2697. ERR_FAIL_COND_V(!multimesh, AABB());
  2698. const_cast<RasterizerStorageGLES2 *>(this)->update_dirty_multimeshes();
  2699. return multimesh->aabb;
  2700. }
  2701. RasterizerStorage::MMInterpolator *RasterizerStorageGLES2::_multimesh_get_interpolator(RID p_multimesh) const {
  2702. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2703. ERR_FAIL_COND_V(!multimesh, nullptr);
  2704. return &multimesh->interpolator;
  2705. }
  2706. void RasterizerStorageGLES2::update_dirty_multimeshes() {
  2707. while (multimesh_update_list.first()) {
  2708. MultiMesh *multimesh = multimesh_update_list.first()->self();
  2709. if (multimesh->size && multimesh->dirty_aabb) {
  2710. AABB mesh_aabb;
  2711. if (multimesh->mesh.is_valid()) {
  2712. mesh_aabb = mesh_get_aabb(multimesh->mesh, RID());
  2713. }
  2714. mesh_aabb.size += Vector3(0.001, 0.001, 0.001); //in case mesh is empty in one of the sides
  2715. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  2716. int count = multimesh->data.size();
  2717. float *data = multimesh->data.ptrw();
  2718. AABB aabb;
  2719. if (multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D) {
  2720. for (int i = 0; i < count; i += stride) {
  2721. float *dataptr = &data[i];
  2722. Transform xform;
  2723. xform.basis[0][0] = dataptr[0];
  2724. xform.basis[0][1] = dataptr[1];
  2725. xform.origin[0] = dataptr[3];
  2726. xform.basis[1][0] = dataptr[4];
  2727. xform.basis[1][1] = dataptr[5];
  2728. xform.origin[1] = dataptr[7];
  2729. AABB laabb = xform.xform(mesh_aabb);
  2730. if (i == 0) {
  2731. aabb = laabb;
  2732. } else {
  2733. aabb.merge_with(laabb);
  2734. }
  2735. }
  2736. } else {
  2737. for (int i = 0; i < count; i += stride) {
  2738. float *dataptr = &data[i];
  2739. Transform xform;
  2740. xform.basis.elements[0][0] = dataptr[0];
  2741. xform.basis.elements[0][1] = dataptr[1];
  2742. xform.basis.elements[0][2] = dataptr[2];
  2743. xform.origin.x = dataptr[3];
  2744. xform.basis.elements[1][0] = dataptr[4];
  2745. xform.basis.elements[1][1] = dataptr[5];
  2746. xform.basis.elements[1][2] = dataptr[6];
  2747. xform.origin.y = dataptr[7];
  2748. xform.basis.elements[2][0] = dataptr[8];
  2749. xform.basis.elements[2][1] = dataptr[9];
  2750. xform.basis.elements[2][2] = dataptr[10];
  2751. xform.origin.z = dataptr[11];
  2752. AABB laabb = xform.xform(mesh_aabb);
  2753. if (i == 0) {
  2754. aabb = laabb;
  2755. } else {
  2756. aabb.merge_with(laabb);
  2757. }
  2758. }
  2759. }
  2760. multimesh->aabb = aabb;
  2761. }
  2762. multimesh->dirty_aabb = false;
  2763. multimesh->dirty_data = false;
  2764. multimesh->instance_change_notify(true, false);
  2765. multimesh_update_list.remove(multimesh_update_list.first());
  2766. }
  2767. }
  2768. /* IMMEDIATE API */
  2769. RID RasterizerStorageGLES2::immediate_create() {
  2770. Immediate *im = memnew(Immediate);
  2771. return immediate_owner.make_rid(im);
  2772. }
  2773. void RasterizerStorageGLES2::immediate_begin(RID p_immediate, VS::PrimitiveType p_primitive, RID p_texture) {
  2774. Immediate *im = immediate_owner.get(p_immediate);
  2775. ERR_FAIL_COND(!im);
  2776. ERR_FAIL_COND(im->building);
  2777. Immediate::Chunk ic;
  2778. ic.texture = p_texture;
  2779. ic.primitive = p_primitive;
  2780. im->chunks.push_back(ic);
  2781. im->mask = 0;
  2782. im->building = true;
  2783. }
  2784. void RasterizerStorageGLES2::immediate_vertex(RID p_immediate, const Vector3 &p_vertex) {
  2785. Immediate *im = immediate_owner.get(p_immediate);
  2786. ERR_FAIL_COND(!im);
  2787. ERR_FAIL_COND(!im->building);
  2788. Immediate::Chunk *c = &im->chunks.back()->get();
  2789. if (c->vertices.empty() && im->chunks.size() == 1) {
  2790. im->aabb.position = p_vertex;
  2791. im->aabb.size = Vector3();
  2792. } else {
  2793. im->aabb.expand_to(p_vertex);
  2794. }
  2795. if (im->mask & VS::ARRAY_FORMAT_NORMAL) {
  2796. c->normals.push_back(chunk_normal);
  2797. }
  2798. if (im->mask & VS::ARRAY_FORMAT_TANGENT) {
  2799. c->tangents.push_back(chunk_tangent);
  2800. }
  2801. if (im->mask & VS::ARRAY_FORMAT_COLOR) {
  2802. c->colors.push_back(chunk_color);
  2803. }
  2804. if (im->mask & VS::ARRAY_FORMAT_TEX_UV) {
  2805. c->uvs.push_back(chunk_uv);
  2806. }
  2807. if (im->mask & VS::ARRAY_FORMAT_TEX_UV2) {
  2808. c->uv2s.push_back(chunk_uv2);
  2809. }
  2810. im->mask |= VS::ARRAY_FORMAT_VERTEX;
  2811. c->vertices.push_back(p_vertex);
  2812. }
  2813. void RasterizerStorageGLES2::immediate_normal(RID p_immediate, const Vector3 &p_normal) {
  2814. Immediate *im = immediate_owner.get(p_immediate);
  2815. ERR_FAIL_COND(!im);
  2816. ERR_FAIL_COND(!im->building);
  2817. im->mask |= VS::ARRAY_FORMAT_NORMAL;
  2818. chunk_normal = p_normal;
  2819. }
  2820. void RasterizerStorageGLES2::immediate_tangent(RID p_immediate, const Plane &p_tangent) {
  2821. Immediate *im = immediate_owner.get(p_immediate);
  2822. ERR_FAIL_COND(!im);
  2823. ERR_FAIL_COND(!im->building);
  2824. im->mask |= VS::ARRAY_FORMAT_TANGENT;
  2825. chunk_tangent = p_tangent;
  2826. }
  2827. void RasterizerStorageGLES2::immediate_color(RID p_immediate, const Color &p_color) {
  2828. Immediate *im = immediate_owner.get(p_immediate);
  2829. ERR_FAIL_COND(!im);
  2830. ERR_FAIL_COND(!im->building);
  2831. im->mask |= VS::ARRAY_FORMAT_COLOR;
  2832. chunk_color = p_color;
  2833. }
  2834. void RasterizerStorageGLES2::immediate_uv(RID p_immediate, const Vector2 &tex_uv) {
  2835. Immediate *im = immediate_owner.get(p_immediate);
  2836. ERR_FAIL_COND(!im);
  2837. ERR_FAIL_COND(!im->building);
  2838. im->mask |= VS::ARRAY_FORMAT_TEX_UV;
  2839. chunk_uv = tex_uv;
  2840. }
  2841. void RasterizerStorageGLES2::immediate_uv2(RID p_immediate, const Vector2 &tex_uv) {
  2842. Immediate *im = immediate_owner.get(p_immediate);
  2843. ERR_FAIL_COND(!im);
  2844. ERR_FAIL_COND(!im->building);
  2845. im->mask |= VS::ARRAY_FORMAT_TEX_UV2;
  2846. chunk_uv2 = tex_uv;
  2847. }
  2848. void RasterizerStorageGLES2::immediate_end(RID p_immediate) {
  2849. Immediate *im = immediate_owner.get(p_immediate);
  2850. ERR_FAIL_COND(!im);
  2851. ERR_FAIL_COND(!im->building);
  2852. im->building = false;
  2853. im->instance_change_notify(true, false);
  2854. }
  2855. void RasterizerStorageGLES2::immediate_clear(RID p_immediate) {
  2856. Immediate *im = immediate_owner.get(p_immediate);
  2857. ERR_FAIL_COND(!im);
  2858. ERR_FAIL_COND(im->building);
  2859. im->chunks.clear();
  2860. im->instance_change_notify(true, false);
  2861. }
  2862. AABB RasterizerStorageGLES2::immediate_get_aabb(RID p_immediate) const {
  2863. Immediate *im = immediate_owner.get(p_immediate);
  2864. ERR_FAIL_COND_V(!im, AABB());
  2865. return im->aabb;
  2866. }
  2867. void RasterizerStorageGLES2::immediate_set_material(RID p_immediate, RID p_material) {
  2868. Immediate *im = immediate_owner.get(p_immediate);
  2869. ERR_FAIL_COND(!im);
  2870. im->material = p_material;
  2871. im->instance_change_notify(false, true);
  2872. }
  2873. RID RasterizerStorageGLES2::immediate_get_material(RID p_immediate) const {
  2874. const Immediate *im = immediate_owner.get(p_immediate);
  2875. ERR_FAIL_COND_V(!im, RID());
  2876. return im->material;
  2877. }
  2878. /* SKELETON API */
  2879. RID RasterizerStorageGLES2::skeleton_create() {
  2880. Skeleton *skeleton = memnew(Skeleton);
  2881. glGenTextures(1, &skeleton->tex_id);
  2882. return skeleton_owner.make_rid(skeleton);
  2883. }
  2884. void RasterizerStorageGLES2::skeleton_allocate(RID p_skeleton, int p_bones, bool p_2d_skeleton) {
  2885. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2886. ERR_FAIL_COND(!skeleton);
  2887. ERR_FAIL_COND(p_bones < 0);
  2888. if (skeleton->size == p_bones && skeleton->use_2d == p_2d_skeleton) {
  2889. return;
  2890. }
  2891. skeleton->size = p_bones;
  2892. skeleton->use_2d = p_2d_skeleton;
  2893. if (!config.use_skeleton_software) {
  2894. glActiveTexture(GL_TEXTURE0);
  2895. glBindTexture(GL_TEXTURE_2D, skeleton->tex_id);
  2896. #ifdef GLES_OVER_GL
  2897. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, p_bones * (skeleton->use_2d ? 2 : 3), 1, 0, GL_RGBA, GL_FLOAT, nullptr);
  2898. #else
  2899. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, p_bones * (skeleton->use_2d ? 2 : 3), 1, 0, GL_RGBA, GL_FLOAT, NULL);
  2900. #endif
  2901. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  2902. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  2903. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  2904. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  2905. glBindTexture(GL_TEXTURE_2D, 0);
  2906. }
  2907. if (skeleton->use_2d) {
  2908. skeleton->bone_data.resize(p_bones * 4 * 2);
  2909. } else {
  2910. skeleton->bone_data.resize(p_bones * 4 * 3);
  2911. }
  2912. }
  2913. int RasterizerStorageGLES2::skeleton_get_bone_count(RID p_skeleton) const {
  2914. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2915. ERR_FAIL_COND_V(!skeleton, 0);
  2916. return skeleton->size;
  2917. }
  2918. void RasterizerStorageGLES2::skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform &p_transform) {
  2919. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2920. ERR_FAIL_COND(!skeleton);
  2921. ERR_FAIL_INDEX(p_bone, skeleton->size);
  2922. ERR_FAIL_COND(skeleton->use_2d);
  2923. float *bone_data = skeleton->bone_data.ptrw();
  2924. int base_offset = p_bone * 4 * 3;
  2925. bone_data[base_offset + 0] = p_transform.basis[0].x;
  2926. bone_data[base_offset + 1] = p_transform.basis[0].y;
  2927. bone_data[base_offset + 2] = p_transform.basis[0].z;
  2928. bone_data[base_offset + 3] = p_transform.origin.x;
  2929. bone_data[base_offset + 4] = p_transform.basis[1].x;
  2930. bone_data[base_offset + 5] = p_transform.basis[1].y;
  2931. bone_data[base_offset + 6] = p_transform.basis[1].z;
  2932. bone_data[base_offset + 7] = p_transform.origin.y;
  2933. bone_data[base_offset + 8] = p_transform.basis[2].x;
  2934. bone_data[base_offset + 9] = p_transform.basis[2].y;
  2935. bone_data[base_offset + 10] = p_transform.basis[2].z;
  2936. bone_data[base_offset + 11] = p_transform.origin.z;
  2937. if (!skeleton->update_list.in_list()) {
  2938. skeleton_update_list.add(&skeleton->update_list);
  2939. }
  2940. }
  2941. Transform RasterizerStorageGLES2::skeleton_bone_get_transform(RID p_skeleton, int p_bone) const {
  2942. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2943. ERR_FAIL_COND_V(!skeleton, Transform());
  2944. ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform());
  2945. ERR_FAIL_COND_V(skeleton->use_2d, Transform());
  2946. const float *bone_data = skeleton->bone_data.ptr();
  2947. Transform ret;
  2948. int base_offset = p_bone * 4 * 3;
  2949. ret.basis[0].x = bone_data[base_offset + 0];
  2950. ret.basis[0].y = bone_data[base_offset + 1];
  2951. ret.basis[0].z = bone_data[base_offset + 2];
  2952. ret.origin.x = bone_data[base_offset + 3];
  2953. ret.basis[1].x = bone_data[base_offset + 4];
  2954. ret.basis[1].y = bone_data[base_offset + 5];
  2955. ret.basis[1].z = bone_data[base_offset + 6];
  2956. ret.origin.y = bone_data[base_offset + 7];
  2957. ret.basis[2].x = bone_data[base_offset + 8];
  2958. ret.basis[2].y = bone_data[base_offset + 9];
  2959. ret.basis[2].z = bone_data[base_offset + 10];
  2960. ret.origin.z = bone_data[base_offset + 11];
  2961. return ret;
  2962. }
  2963. void RasterizerStorageGLES2::skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) {
  2964. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2965. ERR_FAIL_COND(!skeleton);
  2966. ERR_FAIL_INDEX(p_bone, skeleton->size);
  2967. ERR_FAIL_COND(!skeleton->use_2d);
  2968. float *bone_data = skeleton->bone_data.ptrw();
  2969. int base_offset = p_bone * 4 * 2;
  2970. bone_data[base_offset + 0] = p_transform[0][0];
  2971. bone_data[base_offset + 1] = p_transform[1][0];
  2972. bone_data[base_offset + 2] = 0;
  2973. bone_data[base_offset + 3] = p_transform[2][0];
  2974. bone_data[base_offset + 4] = p_transform[0][1];
  2975. bone_data[base_offset + 5] = p_transform[1][1];
  2976. bone_data[base_offset + 6] = 0;
  2977. bone_data[base_offset + 7] = p_transform[2][1];
  2978. if (!skeleton->update_list.in_list()) {
  2979. skeleton_update_list.add(&skeleton->update_list);
  2980. }
  2981. skeleton->revision++;
  2982. }
  2983. Transform2D RasterizerStorageGLES2::skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const {
  2984. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2985. ERR_FAIL_COND_V(!skeleton, Transform2D());
  2986. ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform2D());
  2987. ERR_FAIL_COND_V(!skeleton->use_2d, Transform2D());
  2988. const float *bone_data = skeleton->bone_data.ptr();
  2989. Transform2D ret;
  2990. int base_offset = p_bone * 4 * 2;
  2991. ret[0][0] = bone_data[base_offset + 0];
  2992. ret[1][0] = bone_data[base_offset + 1];
  2993. ret[2][0] = bone_data[base_offset + 3];
  2994. ret[0][1] = bone_data[base_offset + 4];
  2995. ret[1][1] = bone_data[base_offset + 5];
  2996. ret[2][1] = bone_data[base_offset + 7];
  2997. return ret;
  2998. }
  2999. void RasterizerStorageGLES2::skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) {
  3000. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  3001. ERR_FAIL_COND(!skeleton);
  3002. skeleton->base_transform_2d = p_base_transform;
  3003. }
  3004. uint32_t RasterizerStorageGLES2::skeleton_get_revision(RID p_skeleton) const {
  3005. const Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  3006. ERR_FAIL_COND_V(!skeleton, 0);
  3007. return skeleton->revision;
  3008. }
  3009. void RasterizerStorageGLES2::update_dirty_blend_shapes() {
  3010. while (blend_shapes_update_list.first()) {
  3011. Mesh *mesh = blend_shapes_update_list.first()->self();
  3012. for (int is = 0; is < mesh->surfaces.size(); is++) {
  3013. RasterizerStorageGLES2::Surface *s = mesh->surfaces[is];
  3014. if (!s->blend_shape_data.empty()) {
  3015. PoolVector<float> &transform_buffer = resources.blend_shape_transform_cpu_buffer;
  3016. size_t buffer_size = s->array_len * 8 * 4;
  3017. if (resources.blend_shape_transform_cpu_buffer_size < buffer_size) {
  3018. resources.blend_shape_transform_cpu_buffer_size = buffer_size;
  3019. transform_buffer.resize(buffer_size);
  3020. }
  3021. PoolVector<uint8_t>::Read read = s->data.read();
  3022. PoolVector<float>::Write write = transform_buffer.write();
  3023. float base_weight = 1.0;
  3024. if (s->mesh->blend_shape_mode == VS::BLEND_SHAPE_MODE_NORMALIZED) {
  3025. for (int ti = 0; ti < mesh->blend_shape_values.size(); ti++) {
  3026. base_weight -= mesh->blend_shape_values.get(ti);
  3027. }
  3028. }
  3029. for (int i = 0; i < VS::ARRAY_MAX - 1; i++) {
  3030. if (s->attribs[i].enabled) {
  3031. // Read all attributes
  3032. for (int j = 0; j < s->array_len; j++) {
  3033. size_t offset = s->attribs[i].offset + (j * s->attribs[i].stride);
  3034. const float *rd = (const float *)(read.ptr() + offset);
  3035. size_t offset_write = i * 4 + (j * 8 * 4);
  3036. float *wr = (float *)(write.ptr() + offset_write);
  3037. // Set the base
  3038. switch (i) {
  3039. case VS::ARRAY_VERTEX: {
  3040. if (s->format & VS::ARRAY_COMPRESS_VERTEX) {
  3041. wr[0] = Math::halfptr_to_float(&((uint16_t *)rd)[0]) * base_weight;
  3042. wr[1] = Math::halfptr_to_float(&((uint16_t *)rd)[1]) * base_weight;
  3043. wr[2] = Math::halfptr_to_float(&((uint16_t *)rd)[2]) * base_weight;
  3044. wr[3] = 1.0f;
  3045. } else {
  3046. float a[3] = { 0 };
  3047. a[0] = wr[0] = rd[0] * base_weight;
  3048. a[1] = wr[1] = rd[1] * base_weight;
  3049. a[2] = wr[2] = rd[2] * base_weight;
  3050. memcpy(&write[offset_write], a, sizeof(float) * s->attribs[i].size);
  3051. }
  3052. } break;
  3053. case VS::ARRAY_NORMAL: {
  3054. if (s->format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
  3055. if (s->format & VS::ARRAY_COMPRESS_NORMAL && s->format & VS::ARRAY_FORMAT_TANGENT && s->format & VS::ARRAY_COMPRESS_TANGENT) {
  3056. Vector2 oct(((int8_t *)rd)[0] / 127.0, ((int8_t *)rd)[1] / 127.0);
  3057. Vector3 vec = VS::oct_to_norm(oct);
  3058. wr[0] = vec.x * base_weight;
  3059. wr[1] = vec.y * base_weight;
  3060. wr[2] = vec.z * base_weight;
  3061. } else {
  3062. Vector2 oct(((int16_t *)rd)[0] / 32767.0, ((int16_t *)rd)[1] / 32767.0);
  3063. Vector3 vec = VS::oct_to_norm(oct);
  3064. wr[0] = vec.x * base_weight;
  3065. wr[1] = vec.y * base_weight;
  3066. wr[2] = vec.z * base_weight;
  3067. }
  3068. } else {
  3069. if (s->format & VS::ARRAY_COMPRESS_NORMAL) {
  3070. wr[0] = (((int8_t *)rd)[0] / 127.0) * base_weight;
  3071. wr[1] = (((int8_t *)rd)[1] / 127.0) * base_weight;
  3072. wr[2] = (((int8_t *)rd)[2] / 127.0) * base_weight;
  3073. } else {
  3074. wr[0] = rd[0] * base_weight;
  3075. wr[1] = rd[1] * base_weight;
  3076. wr[2] = rd[2] * base_weight;
  3077. }
  3078. }
  3079. } break;
  3080. case VS::ARRAY_TANGENT: {
  3081. if (s->format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
  3082. if (s->format & VS::ARRAY_COMPRESS_TANGENT && s->format & VS::ARRAY_FORMAT_NORMAL && s->format & VS::ARRAY_COMPRESS_NORMAL) {
  3083. Vector2 oct(((int8_t *)rd)[0] / 127.0, ((int8_t *)rd)[1] / 127.0);
  3084. float sign;
  3085. Vector3 vec = VS::oct_to_tangent(oct, &sign);
  3086. wr[0] = vec.x * base_weight;
  3087. wr[1] = vec.y * base_weight;
  3088. wr[2] = vec.z * base_weight;
  3089. wr[3] = sign * base_weight;
  3090. } else {
  3091. Vector2 oct(((int16_t *)rd)[0] / 32767.0, ((int16_t *)rd)[1] / 32767.0);
  3092. float sign;
  3093. Vector3 vec = VS::oct_to_tangent(oct, &sign);
  3094. wr[0] = vec.x * base_weight;
  3095. wr[1] = vec.y * base_weight;
  3096. wr[2] = vec.z * base_weight;
  3097. wr[3] = sign * base_weight;
  3098. }
  3099. } else {
  3100. if (s->format & VS::ARRAY_COMPRESS_TANGENT) {
  3101. wr[0] = (((int8_t *)rd)[0] / 127.0) * base_weight;
  3102. wr[1] = (((int8_t *)rd)[1] / 127.0) * base_weight;
  3103. wr[2] = (((int8_t *)rd)[2] / 127.0) * base_weight;
  3104. wr[3] = (((int8_t *)rd)[3] / 127.0) * base_weight;
  3105. } else {
  3106. wr[0] = rd[0] * base_weight;
  3107. wr[1] = rd[1] * base_weight;
  3108. wr[2] = rd[2] * base_weight;
  3109. wr[3] = rd[3] * base_weight;
  3110. }
  3111. }
  3112. } break;
  3113. case VS::ARRAY_COLOR: {
  3114. if (s->format & VS::ARRAY_COMPRESS_COLOR) {
  3115. wr[0] = (((uint8_t *)rd)[0] / 255.0) * base_weight;
  3116. wr[1] = (((uint8_t *)rd)[1] / 255.0) * base_weight;
  3117. wr[2] = (((uint8_t *)rd)[2] / 255.0) * base_weight;
  3118. wr[3] = (((uint8_t *)rd)[3] / 255.0) * base_weight;
  3119. } else {
  3120. wr[0] = rd[0] * base_weight;
  3121. wr[1] = rd[1] * base_weight;
  3122. wr[2] = rd[2] * base_weight;
  3123. wr[3] = rd[3] * base_weight;
  3124. }
  3125. } break;
  3126. case VS::ARRAY_TEX_UV: {
  3127. if (s->format & VS::ARRAY_COMPRESS_TEX_UV) {
  3128. wr[0] = Math::halfptr_to_float(&((uint16_t *)rd)[0]) * base_weight;
  3129. wr[1] = Math::halfptr_to_float(&((uint16_t *)rd)[1]) * base_weight;
  3130. } else {
  3131. wr[0] = rd[0] * base_weight;
  3132. wr[1] = rd[1] * base_weight;
  3133. }
  3134. } break;
  3135. case VS::ARRAY_TEX_UV2: {
  3136. if (s->format & VS::ARRAY_COMPRESS_TEX_UV2) {
  3137. wr[0] = Math::halfptr_to_float(&((uint16_t *)rd)[0]) * base_weight;
  3138. wr[1] = Math::halfptr_to_float(&((uint16_t *)rd)[1]) * base_weight;
  3139. } else {
  3140. wr[0] = rd[0] * base_weight;
  3141. wr[1] = rd[1] * base_weight;
  3142. }
  3143. } break;
  3144. case VS::ARRAY_WEIGHTS: {
  3145. if (s->format & VS::ARRAY_COMPRESS_WEIGHTS) {
  3146. wr[0] = (((uint16_t *)rd)[0] / 65535.0) * base_weight;
  3147. wr[1] = (((uint16_t *)rd)[1] / 65535.0) * base_weight;
  3148. wr[2] = (((uint16_t *)rd)[2] / 65535.0) * base_weight;
  3149. wr[3] = (((uint16_t *)rd)[3] / 65535.0) * base_weight;
  3150. } else {
  3151. wr[0] = rd[0] * base_weight;
  3152. wr[1] = rd[1] * base_weight;
  3153. wr[2] = rd[2] * base_weight;
  3154. wr[3] = rd[3] * base_weight;
  3155. }
  3156. } break;
  3157. }
  3158. // Add all blend shapes
  3159. for (int ti = 0; ti < mesh->blend_shape_values.size(); ti++) {
  3160. PoolVector<uint8_t>::Read blend = s->blend_shape_data[ti].read();
  3161. const float *br = (const float *)(blend.ptr() + offset);
  3162. float weight = mesh->blend_shape_values.get(ti);
  3163. if (Math::is_zero_approx(weight)) {
  3164. continue;
  3165. }
  3166. switch (i) {
  3167. case VS::ARRAY_VERTEX: {
  3168. if (s->format & VS::ARRAY_COMPRESS_VERTEX) {
  3169. wr[0] += Math::halfptr_to_float(&((uint16_t *)br)[0]) * weight;
  3170. wr[1] += Math::halfptr_to_float(&((uint16_t *)br)[1]) * weight;
  3171. wr[2] += Math::halfptr_to_float(&((uint16_t *)br)[2]) * weight;
  3172. wr[3] = 1.0f;
  3173. } else {
  3174. wr[0] += br[0] * weight;
  3175. wr[1] += br[1] * weight;
  3176. wr[2] += br[2] * weight;
  3177. }
  3178. } break;
  3179. case VS::ARRAY_NORMAL: {
  3180. if (s->format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
  3181. if (s->format & VS::ARRAY_COMPRESS_NORMAL && s->format & VS::ARRAY_FORMAT_TANGENT && s->format & VS::ARRAY_COMPRESS_TANGENT) {
  3182. Vector2 oct(((int8_t *)br)[0] / 127.0, ((int8_t *)br)[1] / 127.0);
  3183. Vector3 vec = VS::oct_to_norm(oct);
  3184. wr[0] += vec.x * weight;
  3185. wr[1] += vec.y * weight;
  3186. wr[2] += vec.z * weight;
  3187. } else {
  3188. Vector2 oct(((int16_t *)br)[0] / 32767.0, ((int16_t *)br)[1] / 32767.0);
  3189. Vector3 vec = VS::oct_to_norm(oct);
  3190. wr[0] += vec.x * weight;
  3191. wr[1] += vec.y * weight;
  3192. wr[2] += vec.z * weight;
  3193. }
  3194. } else {
  3195. if (s->format & VS::ARRAY_COMPRESS_NORMAL) {
  3196. wr[0] += (float(((int8_t *)br)[0]) / 127.0) * weight;
  3197. wr[1] += (float(((int8_t *)br)[1]) / 127.0) * weight;
  3198. wr[2] += (float(((int8_t *)br)[2]) / 127.0) * weight;
  3199. } else {
  3200. wr[0] += br[0] * weight;
  3201. wr[1] += br[1] * weight;
  3202. wr[2] += br[2] * weight;
  3203. }
  3204. }
  3205. } break;
  3206. case VS::ARRAY_TANGENT: {
  3207. if (s->format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
  3208. if (s->format & VS::ARRAY_COMPRESS_TANGENT && s->format & VS::ARRAY_FORMAT_NORMAL && s->format & VS::ARRAY_COMPRESS_NORMAL) {
  3209. Vector2 oct(((int8_t *)br)[0] / 127.0, ((int8_t *)br)[1] / 127.0);
  3210. float sign;
  3211. Vector3 vec = VS::oct_to_tangent(oct, &sign);
  3212. wr[0] += vec.x * weight;
  3213. wr[1] += vec.y * weight;
  3214. wr[2] += vec.z * weight;
  3215. wr[3] = sign * weight;
  3216. } else {
  3217. Vector2 oct(((int16_t *)rd)[0] / 32767.0, ((int16_t *)rd)[1] / 32767.0);
  3218. float sign;
  3219. Vector3 vec = VS::oct_to_tangent(oct, &sign);
  3220. wr[0] += vec.x * weight;
  3221. wr[1] += vec.y * weight;
  3222. wr[2] += vec.z * weight;
  3223. wr[3] = sign * weight;
  3224. }
  3225. } else {
  3226. if (s->format & VS::ARRAY_COMPRESS_TANGENT) {
  3227. wr[0] += (float(((int8_t *)br)[0]) / 127.0) * weight;
  3228. wr[1] += (float(((int8_t *)br)[1]) / 127.0) * weight;
  3229. wr[2] += (float(((int8_t *)br)[2]) / 127.0) * weight;
  3230. wr[3] = (float(((int8_t *)br)[3]) / 127.0);
  3231. } else {
  3232. wr[0] += br[0] * weight;
  3233. wr[1] += br[1] * weight;
  3234. wr[2] += br[2] * weight;
  3235. wr[3] = br[3];
  3236. }
  3237. }
  3238. } break;
  3239. case VS::ARRAY_COLOR: {
  3240. if (s->format & VS::ARRAY_COMPRESS_COLOR) {
  3241. wr[0] += (((uint8_t *)br)[0] / 255.0) * weight;
  3242. wr[1] += (((uint8_t *)br)[1] / 255.0) * weight;
  3243. wr[2] += (((uint8_t *)br)[2] / 255.0) * weight;
  3244. wr[3] += (((uint8_t *)br)[3] / 255.0) * weight;
  3245. } else {
  3246. wr[0] += br[0] * weight;
  3247. wr[1] += br[1] * weight;
  3248. wr[2] += br[2] * weight;
  3249. wr[3] += br[3] * weight;
  3250. }
  3251. } break;
  3252. case VS::ARRAY_TEX_UV: {
  3253. if (s->format & VS::ARRAY_COMPRESS_TEX_UV) {
  3254. wr[0] += Math::halfptr_to_float(&((uint16_t *)br)[0]) * weight;
  3255. wr[1] += Math::halfptr_to_float(&((uint16_t *)br)[1]) * weight;
  3256. } else {
  3257. wr[0] += br[0] * weight;
  3258. wr[1] += br[1] * weight;
  3259. }
  3260. } break;
  3261. case VS::ARRAY_TEX_UV2: {
  3262. if (s->format & VS::ARRAY_COMPRESS_TEX_UV2) {
  3263. wr[0] += Math::halfptr_to_float(&((uint16_t *)br)[0]) * weight;
  3264. wr[1] += Math::halfptr_to_float(&((uint16_t *)br)[1]) * weight;
  3265. } else {
  3266. wr[0] += br[0] * weight;
  3267. wr[1] += br[1] * weight;
  3268. }
  3269. } break;
  3270. case VS::ARRAY_WEIGHTS: {
  3271. if (s->format & VS::ARRAY_COMPRESS_WEIGHTS) {
  3272. wr[0] += (((uint16_t *)br)[0] / 65535.0) * weight;
  3273. wr[1] += (((uint16_t *)br)[1] / 65535.0) * weight;
  3274. wr[2] += (((uint16_t *)br)[2] / 65535.0) * weight;
  3275. wr[3] += (((uint16_t *)br)[3] / 65535.0) * weight;
  3276. } else {
  3277. wr[0] += br[0] * weight;
  3278. wr[1] += br[1] * weight;
  3279. wr[2] += br[2] * weight;
  3280. wr[3] += br[3] * weight;
  3281. }
  3282. } break;
  3283. }
  3284. }
  3285. }
  3286. }
  3287. }
  3288. // Store size and send changed blend shape render to GL
  3289. glBindBuffer(GL_ARRAY_BUFFER, s->blend_shape_buffer_id);
  3290. if (buffer_size > s->blend_shape_buffer_size) {
  3291. s->blend_shape_buffer_size = buffer_size;
  3292. glBufferData(GL_ARRAY_BUFFER, buffer_size * sizeof(float), transform_buffer.read().ptr(), GL_DYNAMIC_DRAW);
  3293. } else {
  3294. buffer_orphan_and_upload(s->blend_shape_buffer_size * sizeof(float), 0, buffer_size * sizeof(float), transform_buffer.read().ptr(), GL_ARRAY_BUFFER, true);
  3295. }
  3296. glBindBuffer(GL_ARRAY_BUFFER, 0);
  3297. }
  3298. }
  3299. blend_shapes_update_list.remove(blend_shapes_update_list.first());
  3300. }
  3301. }
  3302. void RasterizerStorageGLES2::_update_skeleton_transform_buffer(const PoolVector<float> &p_data, size_t p_size) {
  3303. glBindBuffer(GL_ARRAY_BUFFER, resources.skeleton_transform_buffer);
  3304. uint32_t buffer_size = p_size * sizeof(float);
  3305. if (p_size > resources.skeleton_transform_buffer_size) {
  3306. // new requested buffer is bigger, so resizing the GPU buffer
  3307. resources.skeleton_transform_buffer_size = p_size;
  3308. glBufferData(GL_ARRAY_BUFFER, buffer_size, p_data.read().ptr(), GL_DYNAMIC_DRAW);
  3309. } else {
  3310. // this may not be best, it could be better to use glBufferData in both cases.
  3311. buffer_orphan_and_upload(resources.skeleton_transform_buffer_size * sizeof(float), 0, buffer_size, p_data.read().ptr(), GL_ARRAY_BUFFER, true);
  3312. }
  3313. glBindBuffer(GL_ARRAY_BUFFER, 0);
  3314. }
  3315. void RasterizerStorageGLES2::update_dirty_skeletons() {
  3316. if (config.use_skeleton_software) {
  3317. return;
  3318. }
  3319. glActiveTexture(GL_TEXTURE0);
  3320. while (skeleton_update_list.first()) {
  3321. Skeleton *skeleton = skeleton_update_list.first()->self();
  3322. if (skeleton->size) {
  3323. glBindTexture(GL_TEXTURE_2D, skeleton->tex_id);
  3324. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, skeleton->size * (skeleton->use_2d ? 2 : 3), 1, GL_RGBA, GL_FLOAT, skeleton->bone_data.ptr());
  3325. }
  3326. for (Set<RasterizerScene::InstanceBase *>::Element *E = skeleton->instances.front(); E; E = E->next()) {
  3327. E->get()->base_changed(true, false);
  3328. }
  3329. skeleton_update_list.remove(skeleton_update_list.first());
  3330. }
  3331. }
  3332. /* Light API */
  3333. RID RasterizerStorageGLES2::light_create(VS::LightType p_type) {
  3334. Light *light = memnew(Light);
  3335. light->type = p_type;
  3336. light->param[VS::LIGHT_PARAM_ENERGY] = 1.0;
  3337. light->param[VS::LIGHT_PARAM_INDIRECT_ENERGY] = 1.0;
  3338. light->param[VS::LIGHT_PARAM_SIZE] = 0.0;
  3339. light->param[VS::LIGHT_PARAM_SPECULAR] = 0.5;
  3340. light->param[VS::LIGHT_PARAM_RANGE] = 1.0;
  3341. light->param[VS::LIGHT_PARAM_SPOT_ANGLE] = 45;
  3342. light->param[VS::LIGHT_PARAM_CONTACT_SHADOW_SIZE] = 45;
  3343. light->param[VS::LIGHT_PARAM_SHADOW_MAX_DISTANCE] = 0;
  3344. light->param[VS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET] = 0.1;
  3345. light->param[VS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET] = 0.3;
  3346. light->param[VS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET] = 0.6;
  3347. light->param[VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] = 0.1;
  3348. light->param[VS::LIGHT_PARAM_SHADOW_BIAS_SPLIT_SCALE] = 0.1;
  3349. light->color = Color(1, 1, 1, 1);
  3350. light->shadow = false;
  3351. light->negative = false;
  3352. light->cull_mask = 0xFFFFFFFF;
  3353. light->directional_shadow_mode = VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL;
  3354. light->omni_shadow_mode = VS::LIGHT_OMNI_SHADOW_DUAL_PARABOLOID;
  3355. light->omni_shadow_detail = VS::LIGHT_OMNI_SHADOW_DETAIL_VERTICAL;
  3356. light->directional_blend_splits = false;
  3357. light->directional_range_mode = VS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE;
  3358. light->reverse_cull = false;
  3359. light->bake_mode = VS::LIGHT_BAKE_INDIRECT;
  3360. light->version = 0;
  3361. return light_owner.make_rid(light);
  3362. }
  3363. void RasterizerStorageGLES2::light_set_color(RID p_light, const Color &p_color) {
  3364. Light *light = light_owner.getornull(p_light);
  3365. ERR_FAIL_COND(!light);
  3366. light->color = p_color;
  3367. }
  3368. void RasterizerStorageGLES2::light_set_param(RID p_light, VS::LightParam p_param, float p_value) {
  3369. Light *light = light_owner.getornull(p_light);
  3370. ERR_FAIL_COND(!light);
  3371. ERR_FAIL_INDEX(p_param, VS::LIGHT_PARAM_MAX);
  3372. switch (p_param) {
  3373. case VS::LIGHT_PARAM_RANGE:
  3374. case VS::LIGHT_PARAM_SPOT_ANGLE:
  3375. case VS::LIGHT_PARAM_SHADOW_MAX_DISTANCE:
  3376. case VS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET:
  3377. case VS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET:
  3378. case VS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET:
  3379. case VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS:
  3380. case VS::LIGHT_PARAM_SHADOW_BIAS: {
  3381. light->version++;
  3382. light->instance_change_notify(true, false);
  3383. } break;
  3384. default: {
  3385. }
  3386. }
  3387. light->param[p_param] = p_value;
  3388. }
  3389. void RasterizerStorageGLES2::light_set_shadow(RID p_light, bool p_enabled) {
  3390. Light *light = light_owner.getornull(p_light);
  3391. ERR_FAIL_COND(!light);
  3392. light->shadow = p_enabled;
  3393. light->version++;
  3394. light->instance_change_notify(true, false);
  3395. }
  3396. void RasterizerStorageGLES2::light_set_shadow_color(RID p_light, const Color &p_color) {
  3397. Light *light = light_owner.getornull(p_light);
  3398. ERR_FAIL_COND(!light);
  3399. light->shadow_color = p_color;
  3400. }
  3401. void RasterizerStorageGLES2::light_set_projector(RID p_light, RID p_texture) {
  3402. Light *light = light_owner.getornull(p_light);
  3403. ERR_FAIL_COND(!light);
  3404. light->projector = p_texture;
  3405. }
  3406. void RasterizerStorageGLES2::light_set_negative(RID p_light, bool p_enable) {
  3407. Light *light = light_owner.getornull(p_light);
  3408. ERR_FAIL_COND(!light);
  3409. light->negative = p_enable;
  3410. }
  3411. void RasterizerStorageGLES2::light_set_cull_mask(RID p_light, uint32_t p_mask) {
  3412. Light *light = light_owner.getornull(p_light);
  3413. ERR_FAIL_COND(!light);
  3414. light->cull_mask = p_mask;
  3415. light->version++;
  3416. light->instance_change_notify(true, false);
  3417. }
  3418. void RasterizerStorageGLES2::light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) {
  3419. Light *light = light_owner.getornull(p_light);
  3420. ERR_FAIL_COND(!light);
  3421. light->reverse_cull = p_enabled;
  3422. light->version++;
  3423. light->instance_change_notify(true, false);
  3424. }
  3425. void RasterizerStorageGLES2::light_set_use_gi(RID p_light, bool p_enabled) {
  3426. 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.");
  3427. light_set_bake_mode(p_light, p_enabled ? VS::LightBakeMode::LIGHT_BAKE_INDIRECT : VS::LightBakeMode::LIGHT_BAKE_DISABLED);
  3428. }
  3429. void RasterizerStorageGLES2::light_set_bake_mode(RID p_light, VS::LightBakeMode p_bake_mode) {
  3430. Light *light = light_owner.getornull(p_light);
  3431. ERR_FAIL_COND(!light);
  3432. light->bake_mode = p_bake_mode;
  3433. light->version++;
  3434. light->instance_change_notify(true, false);
  3435. }
  3436. void RasterizerStorageGLES2::light_omni_set_shadow_mode(RID p_light, VS::LightOmniShadowMode p_mode) {
  3437. Light *light = light_owner.getornull(p_light);
  3438. ERR_FAIL_COND(!light);
  3439. light->omni_shadow_mode = p_mode;
  3440. light->version++;
  3441. light->instance_change_notify(true, false);
  3442. }
  3443. VS::LightOmniShadowMode RasterizerStorageGLES2::light_omni_get_shadow_mode(RID p_light) {
  3444. Light *light = light_owner.getornull(p_light);
  3445. ERR_FAIL_COND_V(!light, VS::LIGHT_OMNI_SHADOW_CUBE);
  3446. return light->omni_shadow_mode;
  3447. }
  3448. void RasterizerStorageGLES2::light_omni_set_shadow_detail(RID p_light, VS::LightOmniShadowDetail p_detail) {
  3449. Light *light = light_owner.getornull(p_light);
  3450. ERR_FAIL_COND(!light);
  3451. light->omni_shadow_detail = p_detail;
  3452. light->version++;
  3453. light->instance_change_notify(true, false);
  3454. }
  3455. void RasterizerStorageGLES2::light_directional_set_shadow_mode(RID p_light, VS::LightDirectionalShadowMode p_mode) {
  3456. Light *light = light_owner.getornull(p_light);
  3457. ERR_FAIL_COND(!light);
  3458. light->directional_shadow_mode = p_mode;
  3459. light->version++;
  3460. light->instance_change_notify(true, false);
  3461. }
  3462. void RasterizerStorageGLES2::light_directional_set_blend_splits(RID p_light, bool p_enable) {
  3463. Light *light = light_owner.getornull(p_light);
  3464. ERR_FAIL_COND(!light);
  3465. light->directional_blend_splits = p_enable;
  3466. light->version++;
  3467. light->instance_change_notify(true, false);
  3468. }
  3469. bool RasterizerStorageGLES2::light_directional_get_blend_splits(RID p_light) const {
  3470. Light *light = light_owner.getornull(p_light);
  3471. ERR_FAIL_COND_V(!light, false);
  3472. return light->directional_blend_splits;
  3473. }
  3474. VS::LightDirectionalShadowMode RasterizerStorageGLES2::light_directional_get_shadow_mode(RID p_light) {
  3475. Light *light = light_owner.getornull(p_light);
  3476. ERR_FAIL_COND_V(!light, VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL);
  3477. return light->directional_shadow_mode;
  3478. }
  3479. void RasterizerStorageGLES2::light_directional_set_shadow_depth_range_mode(RID p_light, VS::LightDirectionalShadowDepthRangeMode p_range_mode) {
  3480. Light *light = light_owner.getornull(p_light);
  3481. ERR_FAIL_COND(!light);
  3482. light->directional_range_mode = p_range_mode;
  3483. }
  3484. VS::LightDirectionalShadowDepthRangeMode RasterizerStorageGLES2::light_directional_get_shadow_depth_range_mode(RID p_light) const {
  3485. Light *light = light_owner.getornull(p_light);
  3486. ERR_FAIL_COND_V(!light, VS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE);
  3487. return light->directional_range_mode;
  3488. }
  3489. VS::LightType RasterizerStorageGLES2::light_get_type(RID p_light) const {
  3490. Light *light = light_owner.getornull(p_light);
  3491. ERR_FAIL_COND_V(!light, VS::LIGHT_DIRECTIONAL);
  3492. return light->type;
  3493. }
  3494. float RasterizerStorageGLES2::light_get_param(RID p_light, VS::LightParam p_param) {
  3495. Light *light = light_owner.getornull(p_light);
  3496. ERR_FAIL_COND_V(!light, 0.0);
  3497. ERR_FAIL_INDEX_V(p_param, VS::LIGHT_PARAM_MAX, 0.0);
  3498. return light->param[p_param];
  3499. }
  3500. Color RasterizerStorageGLES2::light_get_color(RID p_light) {
  3501. Light *light = light_owner.getornull(p_light);
  3502. ERR_FAIL_COND_V(!light, Color());
  3503. return light->color;
  3504. }
  3505. bool RasterizerStorageGLES2::light_get_use_gi(RID p_light) {
  3506. return light_get_bake_mode(p_light) != VS::LightBakeMode::LIGHT_BAKE_DISABLED;
  3507. }
  3508. VS::LightBakeMode RasterizerStorageGLES2::light_get_bake_mode(RID p_light) {
  3509. Light *light = light_owner.getornull(p_light);
  3510. ERR_FAIL_COND_V(!light, VS::LightBakeMode::LIGHT_BAKE_DISABLED);
  3511. return light->bake_mode;
  3512. }
  3513. bool RasterizerStorageGLES2::light_has_shadow(RID p_light) const {
  3514. Light *light = light_owner.getornull(p_light);
  3515. ERR_FAIL_COND_V(!light, false);
  3516. return light->shadow;
  3517. }
  3518. uint64_t RasterizerStorageGLES2::light_get_version(RID p_light) const {
  3519. Light *light = light_owner.getornull(p_light);
  3520. ERR_FAIL_COND_V(!light, 0);
  3521. return light->version;
  3522. }
  3523. AABB RasterizerStorageGLES2::light_get_aabb(RID p_light) const {
  3524. Light *light = light_owner.getornull(p_light);
  3525. ERR_FAIL_COND_V(!light, AABB());
  3526. switch (light->type) {
  3527. case VS::LIGHT_SPOT: {
  3528. float len = light->param[VS::LIGHT_PARAM_RANGE];
  3529. float size = Math::tan(Math::deg2rad(light->param[VS::LIGHT_PARAM_SPOT_ANGLE])) * len;
  3530. return AABB(Vector3(-size, -size, -len), Vector3(size * 2, size * 2, len));
  3531. };
  3532. case VS::LIGHT_OMNI: {
  3533. float r = light->param[VS::LIGHT_PARAM_RANGE];
  3534. return AABB(-Vector3(r, r, r), Vector3(r, r, r) * 2);
  3535. };
  3536. case VS::LIGHT_DIRECTIONAL: {
  3537. return AABB();
  3538. };
  3539. }
  3540. ERR_FAIL_V(AABB());
  3541. }
  3542. /* PROBE API */
  3543. RID RasterizerStorageGLES2::reflection_probe_create() {
  3544. ReflectionProbe *reflection_probe = memnew(ReflectionProbe);
  3545. reflection_probe->intensity = 1.0;
  3546. reflection_probe->interior_ambient = Color();
  3547. reflection_probe->interior_ambient_energy = 1.0;
  3548. reflection_probe->interior_ambient_probe_contrib = 0.0;
  3549. reflection_probe->max_distance = 0;
  3550. reflection_probe->extents = Vector3(1, 1, 1);
  3551. reflection_probe->origin_offset = Vector3(0, 0, 0);
  3552. reflection_probe->interior = false;
  3553. reflection_probe->box_projection = false;
  3554. reflection_probe->enable_shadows = false;
  3555. reflection_probe->cull_mask = (1 << 20) - 1;
  3556. reflection_probe->update_mode = VS::REFLECTION_PROBE_UPDATE_ONCE;
  3557. reflection_probe->resolution = 128;
  3558. return reflection_probe_owner.make_rid(reflection_probe);
  3559. }
  3560. void RasterizerStorageGLES2::reflection_probe_set_update_mode(RID p_probe, VS::ReflectionProbeUpdateMode p_mode) {
  3561. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3562. ERR_FAIL_COND(!reflection_probe);
  3563. reflection_probe->update_mode = p_mode;
  3564. reflection_probe->instance_change_notify(true, false);
  3565. }
  3566. void RasterizerStorageGLES2::reflection_probe_set_intensity(RID p_probe, float p_intensity) {
  3567. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3568. ERR_FAIL_COND(!reflection_probe);
  3569. reflection_probe->intensity = p_intensity;
  3570. }
  3571. void RasterizerStorageGLES2::reflection_probe_set_interior_ambient(RID p_probe, const Color &p_ambient) {
  3572. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3573. ERR_FAIL_COND(!reflection_probe);
  3574. reflection_probe->interior_ambient = p_ambient;
  3575. }
  3576. void RasterizerStorageGLES2::reflection_probe_set_interior_ambient_energy(RID p_probe, float p_energy) {
  3577. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3578. ERR_FAIL_COND(!reflection_probe);
  3579. reflection_probe->interior_ambient_energy = p_energy;
  3580. }
  3581. void RasterizerStorageGLES2::reflection_probe_set_interior_ambient_probe_contribution(RID p_probe, float p_contrib) {
  3582. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3583. ERR_FAIL_COND(!reflection_probe);
  3584. reflection_probe->interior_ambient_probe_contrib = p_contrib;
  3585. }
  3586. void RasterizerStorageGLES2::reflection_probe_set_max_distance(RID p_probe, float p_distance) {
  3587. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3588. ERR_FAIL_COND(!reflection_probe);
  3589. reflection_probe->max_distance = p_distance;
  3590. reflection_probe->instance_change_notify(true, false);
  3591. }
  3592. void RasterizerStorageGLES2::reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents) {
  3593. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3594. ERR_FAIL_COND(!reflection_probe);
  3595. reflection_probe->extents = p_extents;
  3596. reflection_probe->instance_change_notify(true, false);
  3597. }
  3598. void RasterizerStorageGLES2::reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) {
  3599. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3600. ERR_FAIL_COND(!reflection_probe);
  3601. reflection_probe->origin_offset = p_offset;
  3602. reflection_probe->instance_change_notify(true, false);
  3603. }
  3604. void RasterizerStorageGLES2::reflection_probe_set_as_interior(RID p_probe, bool p_enable) {
  3605. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3606. ERR_FAIL_COND(!reflection_probe);
  3607. reflection_probe->interior = p_enable;
  3608. reflection_probe->instance_change_notify(true, false);
  3609. }
  3610. void RasterizerStorageGLES2::reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) {
  3611. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3612. ERR_FAIL_COND(!reflection_probe);
  3613. reflection_probe->box_projection = p_enable;
  3614. }
  3615. void RasterizerStorageGLES2::reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) {
  3616. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3617. ERR_FAIL_COND(!reflection_probe);
  3618. reflection_probe->enable_shadows = p_enable;
  3619. reflection_probe->instance_change_notify(true, false);
  3620. }
  3621. void RasterizerStorageGLES2::reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) {
  3622. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3623. ERR_FAIL_COND(!reflection_probe);
  3624. reflection_probe->cull_mask = p_layers;
  3625. reflection_probe->instance_change_notify(true, false);
  3626. }
  3627. void RasterizerStorageGLES2::reflection_probe_set_resolution(RID p_probe, int p_resolution) {
  3628. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3629. ERR_FAIL_COND(!reflection_probe);
  3630. reflection_probe->resolution = p_resolution;
  3631. }
  3632. AABB RasterizerStorageGLES2::reflection_probe_get_aabb(RID p_probe) const {
  3633. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3634. ERR_FAIL_COND_V(!reflection_probe, AABB());
  3635. AABB aabb;
  3636. aabb.position = -reflection_probe->extents;
  3637. aabb.size = reflection_probe->extents * 2.0;
  3638. return aabb;
  3639. }
  3640. VS::ReflectionProbeUpdateMode RasterizerStorageGLES2::reflection_probe_get_update_mode(RID p_probe) const {
  3641. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3642. ERR_FAIL_COND_V(!reflection_probe, VS::REFLECTION_PROBE_UPDATE_ALWAYS);
  3643. return reflection_probe->update_mode;
  3644. }
  3645. uint32_t RasterizerStorageGLES2::reflection_probe_get_cull_mask(RID p_probe) const {
  3646. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3647. ERR_FAIL_COND_V(!reflection_probe, 0);
  3648. return reflection_probe->cull_mask;
  3649. }
  3650. Vector3 RasterizerStorageGLES2::reflection_probe_get_extents(RID p_probe) const {
  3651. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3652. ERR_FAIL_COND_V(!reflection_probe, Vector3());
  3653. return reflection_probe->extents;
  3654. }
  3655. Vector3 RasterizerStorageGLES2::reflection_probe_get_origin_offset(RID p_probe) const {
  3656. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3657. ERR_FAIL_COND_V(!reflection_probe, Vector3());
  3658. return reflection_probe->origin_offset;
  3659. }
  3660. bool RasterizerStorageGLES2::reflection_probe_renders_shadows(RID p_probe) const {
  3661. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3662. ERR_FAIL_COND_V(!reflection_probe, false);
  3663. return reflection_probe->enable_shadows;
  3664. }
  3665. float RasterizerStorageGLES2::reflection_probe_get_origin_max_distance(RID p_probe) const {
  3666. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3667. ERR_FAIL_COND_V(!reflection_probe, 0);
  3668. return reflection_probe->max_distance;
  3669. }
  3670. int RasterizerStorageGLES2::reflection_probe_get_resolution(RID p_probe) const {
  3671. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3672. ERR_FAIL_COND_V(!reflection_probe, 0);
  3673. return reflection_probe->resolution;
  3674. }
  3675. RID RasterizerStorageGLES2::gi_probe_create() {
  3676. return RID();
  3677. }
  3678. void RasterizerStorageGLES2::gi_probe_set_bounds(RID p_probe, const AABB &p_bounds) {
  3679. }
  3680. AABB RasterizerStorageGLES2::gi_probe_get_bounds(RID p_probe) const {
  3681. return AABB();
  3682. }
  3683. void RasterizerStorageGLES2::gi_probe_set_cell_size(RID p_probe, float p_size) {
  3684. }
  3685. float RasterizerStorageGLES2::gi_probe_get_cell_size(RID p_probe) const {
  3686. return 0.0;
  3687. }
  3688. void RasterizerStorageGLES2::gi_probe_set_to_cell_xform(RID p_probe, const Transform &p_xform) {
  3689. }
  3690. Transform RasterizerStorageGLES2::gi_probe_get_to_cell_xform(RID p_probe) const {
  3691. return Transform();
  3692. }
  3693. void RasterizerStorageGLES2::gi_probe_set_dynamic_data(RID p_probe, const PoolVector<int> &p_data) {
  3694. }
  3695. PoolVector<int> RasterizerStorageGLES2::gi_probe_get_dynamic_data(RID p_probe) const {
  3696. return PoolVector<int>();
  3697. }
  3698. void RasterizerStorageGLES2::gi_probe_set_dynamic_range(RID p_probe, int p_range) {
  3699. }
  3700. int RasterizerStorageGLES2::gi_probe_get_dynamic_range(RID p_probe) const {
  3701. return 0;
  3702. }
  3703. void RasterizerStorageGLES2::gi_probe_set_energy(RID p_probe, float p_range) {
  3704. }
  3705. void RasterizerStorageGLES2::gi_probe_set_bias(RID p_probe, float p_range) {
  3706. }
  3707. void RasterizerStorageGLES2::gi_probe_set_normal_bias(RID p_probe, float p_range) {
  3708. }
  3709. void RasterizerStorageGLES2::gi_probe_set_propagation(RID p_probe, float p_range) {
  3710. }
  3711. void RasterizerStorageGLES2::gi_probe_set_interior(RID p_probe, bool p_enable) {
  3712. }
  3713. bool RasterizerStorageGLES2::gi_probe_is_interior(RID p_probe) const {
  3714. return false;
  3715. }
  3716. void RasterizerStorageGLES2::gi_probe_set_compress(RID p_probe, bool p_enable) {
  3717. }
  3718. bool RasterizerStorageGLES2::gi_probe_is_compressed(RID p_probe) const {
  3719. return false;
  3720. }
  3721. float RasterizerStorageGLES2::gi_probe_get_energy(RID p_probe) const {
  3722. return 0;
  3723. }
  3724. float RasterizerStorageGLES2::gi_probe_get_bias(RID p_probe) const {
  3725. return 0;
  3726. }
  3727. float RasterizerStorageGLES2::gi_probe_get_normal_bias(RID p_probe) const {
  3728. return 0;
  3729. }
  3730. float RasterizerStorageGLES2::gi_probe_get_propagation(RID p_probe) const {
  3731. return 0;
  3732. }
  3733. uint32_t RasterizerStorageGLES2::gi_probe_get_version(RID p_probe) {
  3734. return 0;
  3735. }
  3736. RID RasterizerStorageGLES2::gi_probe_dynamic_data_create(int p_width, int p_height, int p_depth, GIProbeCompression p_compression) {
  3737. return RID();
  3738. }
  3739. void RasterizerStorageGLES2::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) {
  3740. }
  3741. ///////
  3742. RID RasterizerStorageGLES2::lightmap_capture_create() {
  3743. LightmapCapture *capture = memnew(LightmapCapture);
  3744. return lightmap_capture_data_owner.make_rid(capture);
  3745. }
  3746. void RasterizerStorageGLES2::lightmap_capture_set_bounds(RID p_capture, const AABB &p_bounds) {
  3747. LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  3748. ERR_FAIL_COND(!capture);
  3749. capture->bounds = p_bounds;
  3750. capture->instance_change_notify(true, false);
  3751. }
  3752. AABB RasterizerStorageGLES2::lightmap_capture_get_bounds(RID p_capture) const {
  3753. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  3754. ERR_FAIL_COND_V(!capture, AABB());
  3755. return capture->bounds;
  3756. }
  3757. void RasterizerStorageGLES2::lightmap_capture_set_octree(RID p_capture, const PoolVector<uint8_t> &p_octree) {
  3758. LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  3759. ERR_FAIL_COND(!capture);
  3760. ERR_FAIL_COND(p_octree.size() == 0 || (p_octree.size() % sizeof(LightmapCaptureOctree)) != 0);
  3761. capture->octree.resize(p_octree.size() / sizeof(LightmapCaptureOctree));
  3762. if (p_octree.size()) {
  3763. PoolVector<LightmapCaptureOctree>::Write w = capture->octree.write();
  3764. PoolVector<uint8_t>::Read r = p_octree.read();
  3765. memcpy(w.ptr(), r.ptr(), p_octree.size());
  3766. }
  3767. capture->instance_change_notify(true, false);
  3768. }
  3769. PoolVector<uint8_t> RasterizerStorageGLES2::lightmap_capture_get_octree(RID p_capture) const {
  3770. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  3771. ERR_FAIL_COND_V(!capture, PoolVector<uint8_t>());
  3772. if (capture->octree.size() == 0) {
  3773. return PoolVector<uint8_t>();
  3774. }
  3775. PoolVector<uint8_t> ret;
  3776. ret.resize(capture->octree.size() * sizeof(LightmapCaptureOctree));
  3777. {
  3778. PoolVector<LightmapCaptureOctree>::Read r = capture->octree.read();
  3779. PoolVector<uint8_t>::Write w = ret.write();
  3780. memcpy(w.ptr(), r.ptr(), ret.size());
  3781. }
  3782. return ret;
  3783. }
  3784. void RasterizerStorageGLES2::lightmap_capture_set_octree_cell_transform(RID p_capture, const Transform &p_xform) {
  3785. LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  3786. ERR_FAIL_COND(!capture);
  3787. capture->cell_xform = p_xform;
  3788. }
  3789. Transform RasterizerStorageGLES2::lightmap_capture_get_octree_cell_transform(RID p_capture) const {
  3790. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  3791. ERR_FAIL_COND_V(!capture, Transform());
  3792. return capture->cell_xform;
  3793. }
  3794. void RasterizerStorageGLES2::lightmap_capture_set_octree_cell_subdiv(RID p_capture, int p_subdiv) {
  3795. LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  3796. ERR_FAIL_COND(!capture);
  3797. capture->cell_subdiv = p_subdiv;
  3798. }
  3799. int RasterizerStorageGLES2::lightmap_capture_get_octree_cell_subdiv(RID p_capture) const {
  3800. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  3801. ERR_FAIL_COND_V(!capture, 0);
  3802. return capture->cell_subdiv;
  3803. }
  3804. void RasterizerStorageGLES2::lightmap_capture_set_energy(RID p_capture, float p_energy) {
  3805. LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  3806. ERR_FAIL_COND(!capture);
  3807. capture->energy = p_energy;
  3808. if (!capture->update_list.in_list()) {
  3809. capture_update_list.add(&capture->update_list);
  3810. }
  3811. }
  3812. float RasterizerStorageGLES2::lightmap_capture_get_energy(RID p_capture) const {
  3813. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  3814. ERR_FAIL_COND_V(!capture, 0);
  3815. return capture->energy;
  3816. }
  3817. void RasterizerStorageGLES2::lightmap_capture_set_interior(RID p_capture, bool p_interior) {
  3818. LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  3819. ERR_FAIL_COND(!capture);
  3820. capture->interior = p_interior;
  3821. if (!capture->update_list.in_list()) {
  3822. capture_update_list.add(&capture->update_list);
  3823. }
  3824. }
  3825. bool RasterizerStorageGLES2::lightmap_capture_is_interior(RID p_capture) const {
  3826. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  3827. ERR_FAIL_COND_V(!capture, false);
  3828. return capture->interior;
  3829. }
  3830. void RasterizerStorageGLES2::update_dirty_captures() {
  3831. while (capture_update_list.first()) {
  3832. LightmapCapture *capture = capture_update_list.first()->self();
  3833. capture->instance_change_notify(false, true);
  3834. capture_update_list.remove(capture_update_list.first());
  3835. }
  3836. }
  3837. const PoolVector<RasterizerStorage::LightmapCaptureOctree> *RasterizerStorageGLES2::lightmap_capture_get_octree_ptr(RID p_capture) const {
  3838. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  3839. ERR_FAIL_COND_V(!capture, nullptr);
  3840. return &capture->octree;
  3841. }
  3842. ///////
  3843. RID RasterizerStorageGLES2::particles_create() {
  3844. return RID();
  3845. }
  3846. void RasterizerStorageGLES2::particles_set_emitting(RID p_particles, bool p_emitting) {
  3847. }
  3848. bool RasterizerStorageGLES2::particles_get_emitting(RID p_particles) {
  3849. return false;
  3850. }
  3851. void RasterizerStorageGLES2::particles_set_amount(RID p_particles, int p_amount) {
  3852. }
  3853. void RasterizerStorageGLES2::particles_set_lifetime(RID p_particles, float p_lifetime) {
  3854. }
  3855. void RasterizerStorageGLES2::particles_set_one_shot(RID p_particles, bool p_one_shot) {
  3856. }
  3857. void RasterizerStorageGLES2::particles_set_pre_process_time(RID p_particles, float p_time) {
  3858. }
  3859. void RasterizerStorageGLES2::particles_set_explosiveness_ratio(RID p_particles, float p_ratio) {
  3860. }
  3861. void RasterizerStorageGLES2::particles_set_randomness_ratio(RID p_particles, float p_ratio) {
  3862. }
  3863. void RasterizerStorageGLES2::particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) {
  3864. }
  3865. void RasterizerStorageGLES2::particles_set_speed_scale(RID p_particles, float p_scale) {
  3866. }
  3867. void RasterizerStorageGLES2::particles_set_use_local_coordinates(RID p_particles, bool p_enable) {
  3868. }
  3869. void RasterizerStorageGLES2::particles_set_fixed_fps(RID p_particles, int p_fps) {
  3870. }
  3871. void RasterizerStorageGLES2::particles_set_fractional_delta(RID p_particles, bool p_enable) {
  3872. }
  3873. void RasterizerStorageGLES2::particles_set_process_material(RID p_particles, RID p_material) {
  3874. }
  3875. void RasterizerStorageGLES2::particles_set_draw_order(RID p_particles, VS::ParticlesDrawOrder p_order) {
  3876. }
  3877. void RasterizerStorageGLES2::particles_set_draw_passes(RID p_particles, int p_passes) {
  3878. }
  3879. void RasterizerStorageGLES2::particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) {
  3880. }
  3881. void RasterizerStorageGLES2::particles_restart(RID p_particles) {
  3882. }
  3883. void RasterizerStorageGLES2::particles_request_process(RID p_particles) {
  3884. }
  3885. AABB RasterizerStorageGLES2::particles_get_current_aabb(RID p_particles) {
  3886. return AABB();
  3887. }
  3888. AABB RasterizerStorageGLES2::particles_get_aabb(RID p_particles) const {
  3889. return AABB();
  3890. }
  3891. void RasterizerStorageGLES2::particles_set_emission_transform(RID p_particles, const Transform &p_transform) {
  3892. }
  3893. int RasterizerStorageGLES2::particles_get_draw_passes(RID p_particles) const {
  3894. return 0;
  3895. }
  3896. RID RasterizerStorageGLES2::particles_get_draw_pass_mesh(RID p_particles, int p_pass) const {
  3897. return RID();
  3898. }
  3899. void RasterizerStorageGLES2::update_particles() {
  3900. }
  3901. bool RasterizerStorageGLES2::particles_is_inactive(RID p_particles) const {
  3902. return true;
  3903. }
  3904. ////////
  3905. void RasterizerStorageGLES2::instance_add_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance) {
  3906. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  3907. ERR_FAIL_COND(!skeleton);
  3908. skeleton->instances.insert(p_instance);
  3909. }
  3910. void RasterizerStorageGLES2::instance_remove_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance) {
  3911. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  3912. ERR_FAIL_COND(!skeleton);
  3913. skeleton->instances.erase(p_instance);
  3914. }
  3915. void RasterizerStorageGLES2::instance_add_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) {
  3916. Instantiable *inst = nullptr;
  3917. switch (p_instance->base_type) {
  3918. case VS::INSTANCE_MESH: {
  3919. inst = mesh_owner.getornull(p_base);
  3920. ERR_FAIL_COND(!inst);
  3921. } break;
  3922. case VS::INSTANCE_MULTIMESH: {
  3923. inst = multimesh_owner.getornull(p_base);
  3924. ERR_FAIL_COND(!inst);
  3925. } break;
  3926. case VS::INSTANCE_IMMEDIATE: {
  3927. inst = immediate_owner.getornull(p_base);
  3928. ERR_FAIL_COND(!inst);
  3929. } break;
  3930. /*case VS::INSTANCE_PARTICLES: {
  3931. inst = particles_owner.getornull(p_base);
  3932. ERR_FAIL_COND(!inst);
  3933. } break;*/
  3934. case VS::INSTANCE_REFLECTION_PROBE: {
  3935. inst = reflection_probe_owner.getornull(p_base);
  3936. ERR_FAIL_COND(!inst);
  3937. } break;
  3938. case VS::INSTANCE_LIGHT: {
  3939. inst = light_owner.getornull(p_base);
  3940. ERR_FAIL_COND(!inst);
  3941. } break;
  3942. /*case VS::INSTANCE_GI_PROBE: {
  3943. inst = gi_probe_owner.getornull(p_base);
  3944. ERR_FAIL_COND(!inst);
  3945. } break;*/
  3946. case VS::INSTANCE_LIGHTMAP_CAPTURE: {
  3947. inst = lightmap_capture_data_owner.getornull(p_base);
  3948. ERR_FAIL_COND(!inst);
  3949. } break;
  3950. default: {
  3951. ERR_FAIL();
  3952. }
  3953. }
  3954. inst->instance_list.add(&p_instance->dependency_item);
  3955. }
  3956. void RasterizerStorageGLES2::instance_remove_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) {
  3957. Instantiable *inst = nullptr;
  3958. switch (p_instance->base_type) {
  3959. case VS::INSTANCE_MESH: {
  3960. inst = mesh_owner.getornull(p_base);
  3961. ERR_FAIL_COND(!inst);
  3962. } break;
  3963. case VS::INSTANCE_MULTIMESH: {
  3964. inst = multimesh_owner.getornull(p_base);
  3965. ERR_FAIL_COND(!inst);
  3966. } break;
  3967. case VS::INSTANCE_IMMEDIATE: {
  3968. inst = immediate_owner.getornull(p_base);
  3969. ERR_FAIL_COND(!inst);
  3970. } break;
  3971. /*case VS::INSTANCE_PARTICLES: {
  3972. inst = particles_owner.getornull(p_base);
  3973. ERR_FAIL_COND(!inst);
  3974. } break;*/
  3975. case VS::INSTANCE_REFLECTION_PROBE: {
  3976. inst = reflection_probe_owner.getornull(p_base);
  3977. ERR_FAIL_COND(!inst);
  3978. } break;
  3979. case VS::INSTANCE_LIGHT: {
  3980. inst = light_owner.getornull(p_base);
  3981. ERR_FAIL_COND(!inst);
  3982. } break;
  3983. /*case VS::INSTANCE_GI_PROBE: {
  3984. inst = gi_probe_owner.getornull(p_base);
  3985. ERR_FAIL_COND(!inst);
  3986. } break; */
  3987. case VS::INSTANCE_LIGHTMAP_CAPTURE: {
  3988. inst = lightmap_capture_data_owner.getornull(p_base);
  3989. ERR_FAIL_COND(!inst);
  3990. } break;
  3991. default: {
  3992. ERR_FAIL();
  3993. }
  3994. }
  3995. inst->instance_list.remove(&p_instance->dependency_item);
  3996. }
  3997. /* RENDER TARGET */
  3998. void RasterizerStorageGLES2::_render_target_allocate(RenderTarget *rt) {
  3999. // do not allocate a render target with no size
  4000. if (rt->width <= 0 || rt->height <= 0) {
  4001. return;
  4002. }
  4003. // do not allocate a render target that is attached to the screen
  4004. if (rt->flags[RENDER_TARGET_DIRECT_TO_SCREEN]) {
  4005. rt->fbo = RasterizerStorageGLES2::system_fbo;
  4006. return;
  4007. }
  4008. if (rt->width > config.max_viewport_dimensions[0] || rt->height > config.max_viewport_dimensions[1]) {
  4009. WARN_PRINT("Cannot create render target larger than maximum hardware supported size of (" + itos(config.max_viewport_dimensions[0]) + ", " + itos(config.max_viewport_dimensions[1]) + "). Setting size to maximum.");
  4010. rt->width = MIN(rt->width, config.max_viewport_dimensions[0]);
  4011. rt->height = MIN(rt->height, config.max_viewport_dimensions[1]);
  4012. }
  4013. GLuint color_internal_format;
  4014. GLuint color_format;
  4015. GLuint color_type = GL_UNSIGNED_BYTE;
  4016. Image::Format image_format;
  4017. if (rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  4018. #ifdef GLES_OVER_GL
  4019. color_internal_format = GL_RGBA8;
  4020. #else
  4021. color_internal_format = GL_RGBA;
  4022. #endif
  4023. color_format = GL_RGBA;
  4024. image_format = Image::FORMAT_RGBA8;
  4025. } else {
  4026. #ifdef GLES_OVER_GL
  4027. color_internal_format = GL_RGB8;
  4028. #else
  4029. color_internal_format = GL_RGB;
  4030. #endif
  4031. color_format = GL_RGB;
  4032. image_format = Image::FORMAT_RGB8;
  4033. }
  4034. rt->used_dof_blur_near = false;
  4035. rt->mip_maps_allocated = false;
  4036. {
  4037. /* Front FBO */
  4038. Texture *texture = texture_owner.getornull(rt->texture);
  4039. ERR_FAIL_COND(!texture);
  4040. // framebuffer
  4041. glGenFramebuffers(1, &rt->fbo);
  4042. glBindFramebuffer(GL_FRAMEBUFFER, rt->fbo);
  4043. // color
  4044. glGenTextures(1, &rt->color);
  4045. glBindTexture(GL_TEXTURE_2D, rt->color);
  4046. glTexImage2D(GL_TEXTURE_2D, 0, color_internal_format, rt->width, rt->height, 0, color_format, color_type, nullptr);
  4047. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  4048. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  4049. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  4050. } else {
  4051. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  4052. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  4053. }
  4054. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  4055. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  4056. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color, 0);
  4057. // depth
  4058. if (config.support_depth_texture) {
  4059. glGenTextures(1, &rt->depth);
  4060. glBindTexture(GL_TEXTURE_2D, rt->depth);
  4061. glTexImage2D(GL_TEXTURE_2D, 0, config.depth_internalformat, rt->width, rt->height, 0, GL_DEPTH_COMPONENT, config.depth_type, nullptr);
  4062. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  4063. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  4064. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  4065. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  4066. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rt->depth, 0);
  4067. } else {
  4068. glGenRenderbuffers(1, &rt->depth);
  4069. glBindRenderbuffer(GL_RENDERBUFFER, rt->depth);
  4070. glRenderbufferStorage(GL_RENDERBUFFER, config.depth_buffer_internalformat, rt->width, rt->height);
  4071. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth);
  4072. }
  4073. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  4074. if (status != GL_FRAMEBUFFER_COMPLETE) {
  4075. glDeleteFramebuffers(1, &rt->fbo);
  4076. if (config.support_depth_texture) {
  4077. glDeleteTextures(1, &rt->depth);
  4078. } else {
  4079. glDeleteRenderbuffers(1, &rt->depth);
  4080. }
  4081. glDeleteTextures(1, &rt->color);
  4082. rt->fbo = 0;
  4083. rt->width = 0;
  4084. rt->height = 0;
  4085. rt->color = 0;
  4086. rt->depth = 0;
  4087. texture->tex_id = 0;
  4088. texture->active = false;
  4089. WARN_PRINT("Could not create framebuffer!!");
  4090. return;
  4091. }
  4092. texture->format = image_format;
  4093. texture->gl_format_cache = color_format;
  4094. texture->gl_type_cache = GL_UNSIGNED_BYTE;
  4095. texture->gl_internal_format_cache = color_internal_format;
  4096. texture->tex_id = rt->color;
  4097. texture->width = rt->width;
  4098. texture->alloc_width = rt->width;
  4099. texture->height = rt->height;
  4100. texture->alloc_height = rt->height;
  4101. texture->active = true;
  4102. texture_set_flags(rt->texture, texture->flags);
  4103. }
  4104. /* BACK FBO */
  4105. /* For MSAA */
  4106. #ifndef JAVASCRIPT_ENABLED
  4107. if (rt->msaa >= VS::VIEWPORT_MSAA_2X && rt->msaa <= VS::VIEWPORT_MSAA_16X && config.multisample_supported) {
  4108. rt->multisample_active = true;
  4109. static const int msaa_value[] = { 0, 2, 4, 8, 16 };
  4110. int msaa = msaa_value[rt->msaa];
  4111. int max_samples = 0;
  4112. glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
  4113. if (msaa > max_samples) {
  4114. WARN_PRINT("MSAA must be <= GL_MAX_SAMPLES, falling-back to GL_MAX_SAMPLES = " + itos(max_samples));
  4115. msaa = max_samples;
  4116. }
  4117. //regular fbo
  4118. glGenFramebuffers(1, &rt->multisample_fbo);
  4119. glBindFramebuffer(GL_FRAMEBUFFER, rt->multisample_fbo);
  4120. glGenRenderbuffers(1, &rt->multisample_depth);
  4121. glBindRenderbuffer(GL_RENDERBUFFER, rt->multisample_depth);
  4122. glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, config.depth_buffer_internalformat, rt->width, rt->height);
  4123. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->multisample_depth);
  4124. #if defined(GLES_OVER_GL) || defined(IPHONE_ENABLED)
  4125. glGenRenderbuffers(1, &rt->multisample_color);
  4126. glBindRenderbuffer(GL_RENDERBUFFER, rt->multisample_color);
  4127. glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, color_internal_format, rt->width, rt->height);
  4128. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rt->multisample_color);
  4129. #elif ANDROID_ENABLED
  4130. // Render to a texture in android
  4131. glGenTextures(1, &rt->multisample_color);
  4132. glBindTexture(GL_TEXTURE_2D, rt->multisample_color);
  4133. glTexImage2D(GL_TEXTURE_2D, 0, color_internal_format, rt->width, rt->height, 0, color_format, color_type, NULL);
  4134. // multisample buffer is same size as front buffer, so just use nearest
  4135. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  4136. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  4137. glFramebufferTexture2DMultisample(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->multisample_color, 0, msaa);
  4138. #endif
  4139. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  4140. if (status != GL_FRAMEBUFFER_COMPLETE) {
  4141. // Delete allocated resources and default to no MSAA
  4142. WARN_PRINT_ONCE("Cannot allocate back framebuffer for MSAA");
  4143. printf("err status: %x\n", status);
  4144. config.multisample_supported = false;
  4145. rt->multisample_active = false;
  4146. glDeleteFramebuffers(1, &rt->multisample_fbo);
  4147. rt->multisample_fbo = 0;
  4148. glDeleteRenderbuffers(1, &rt->multisample_depth);
  4149. rt->multisample_depth = 0;
  4150. #ifdef ANDROID_ENABLED
  4151. glDeleteTextures(1, &rt->multisample_color);
  4152. #else
  4153. glDeleteRenderbuffers(1, &rt->multisample_color);
  4154. #endif
  4155. rt->multisample_color = 0;
  4156. }
  4157. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  4158. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  4159. #ifdef ANDROID_ENABLED
  4160. glBindTexture(GL_TEXTURE_2D, 0);
  4161. #endif
  4162. } else
  4163. #endif // JAVASCRIPT_ENABLED
  4164. {
  4165. rt->multisample_active = false;
  4166. }
  4167. glClearColor(0, 0, 0, 0);
  4168. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  4169. // copy texscreen buffers
  4170. if (!(rt->flags[RasterizerStorage::RENDER_TARGET_NO_SAMPLING])) {
  4171. glGenTextures(1, &rt->copy_screen_effect.color);
  4172. glBindTexture(GL_TEXTURE_2D, rt->copy_screen_effect.color);
  4173. if (rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  4174. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, rt->width, rt->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  4175. } else {
  4176. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, rt->width, rt->height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
  4177. }
  4178. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  4179. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  4180. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  4181. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  4182. glGenFramebuffers(1, &rt->copy_screen_effect.fbo);
  4183. glBindFramebuffer(GL_FRAMEBUFFER, rt->copy_screen_effect.fbo);
  4184. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->copy_screen_effect.color, 0);
  4185. glClearColor(0, 0, 0, 0);
  4186. glClear(GL_COLOR_BUFFER_BIT);
  4187. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  4188. if (status != GL_FRAMEBUFFER_COMPLETE) {
  4189. _render_target_clear(rt);
  4190. ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE);
  4191. }
  4192. }
  4193. // Allocate mipmap chains for post_process effects
  4194. if (!rt->flags[RasterizerStorage::RENDER_TARGET_NO_3D] && rt->width >= 2 && rt->height >= 2) {
  4195. for (int i = 0; i < 2; i++) {
  4196. ERR_FAIL_COND(rt->mip_maps[i].sizes.size());
  4197. int w = rt->width;
  4198. int h = rt->height;
  4199. if (i > 0) {
  4200. w >>= 1;
  4201. h >>= 1;
  4202. }
  4203. int level = 0;
  4204. int fb_w = w;
  4205. int fb_h = h;
  4206. while (true) {
  4207. RenderTarget::MipMaps::Size mm;
  4208. mm.width = w;
  4209. mm.height = h;
  4210. rt->mip_maps[i].sizes.push_back(mm);
  4211. w >>= 1;
  4212. h >>= 1;
  4213. if (w < 2 || h < 2) {
  4214. break;
  4215. }
  4216. level++;
  4217. }
  4218. GLsizei width = fb_w;
  4219. GLsizei height = fb_h;
  4220. if (config.render_to_mipmap_supported) {
  4221. glGenTextures(1, &rt->mip_maps[i].color);
  4222. glBindTexture(GL_TEXTURE_2D, rt->mip_maps[i].color);
  4223. for (int l = 0; l < level + 1; l++) {
  4224. glTexImage2D(GL_TEXTURE_2D, l, color_internal_format, width, height, 0, color_format, color_type, nullptr);
  4225. width = MAX(1, (width / 2));
  4226. height = MAX(1, (height / 2));
  4227. }
  4228. #ifdef GLES_OVER_GL
  4229. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  4230. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, level);
  4231. #endif
  4232. } else {
  4233. // Can't render to specific levels of a mipmap in ES 2.0 or Webgl so create a texture for each level
  4234. for (int l = 0; l < level + 1; l++) {
  4235. glGenTextures(1, &rt->mip_maps[i].sizes.write[l].color);
  4236. glBindTexture(GL_TEXTURE_2D, rt->mip_maps[i].sizes[l].color);
  4237. glTexImage2D(GL_TEXTURE_2D, 0, color_internal_format, width, height, 0, color_format, color_type, nullptr);
  4238. width = MAX(1, (width / 2));
  4239. height = MAX(1, (height / 2));
  4240. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  4241. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  4242. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  4243. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  4244. }
  4245. }
  4246. glDisable(GL_SCISSOR_TEST);
  4247. glColorMask(1, 1, 1, 1);
  4248. glDepthMask(GL_TRUE);
  4249. for (int j = 0; j < rt->mip_maps[i].sizes.size(); j++) {
  4250. RenderTarget::MipMaps::Size &mm = rt->mip_maps[i].sizes.write[j];
  4251. glGenFramebuffers(1, &mm.fbo);
  4252. glBindFramebuffer(GL_FRAMEBUFFER, mm.fbo);
  4253. if (config.render_to_mipmap_supported) {
  4254. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->mip_maps[i].color, j);
  4255. } else {
  4256. glBindTexture(GL_TEXTURE_2D, rt->mip_maps[i].sizes[j].color);
  4257. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->mip_maps[i].sizes[j].color, 0);
  4258. }
  4259. bool used_depth = false;
  4260. if (j == 0 && i == 0) { //use always
  4261. if (config.support_depth_texture) {
  4262. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rt->depth, 0);
  4263. } else {
  4264. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth);
  4265. }
  4266. used_depth = true;
  4267. }
  4268. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  4269. if (status != GL_FRAMEBUFFER_COMPLETE) {
  4270. WARN_PRINT_ONCE("Cannot allocate mipmaps for 3D post processing effects");
  4271. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES2::system_fbo);
  4272. return;
  4273. }
  4274. glClearColor(1.0, 0.0, 1.0, 0.0);
  4275. glClear(GL_COLOR_BUFFER_BIT);
  4276. if (used_depth) {
  4277. glClearDepth(1.0);
  4278. glClear(GL_DEPTH_BUFFER_BIT);
  4279. }
  4280. }
  4281. rt->mip_maps[i].levels = level;
  4282. if (config.render_to_mipmap_supported) {
  4283. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  4284. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  4285. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  4286. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  4287. }
  4288. }
  4289. rt->mip_maps_allocated = true;
  4290. }
  4291. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES2::system_fbo);
  4292. }
  4293. void RasterizerStorageGLES2::_render_target_clear(RenderTarget *rt) {
  4294. // there is nothing to clear when DIRECT_TO_SCREEN is used
  4295. if (rt->flags[RENDER_TARGET_DIRECT_TO_SCREEN]) {
  4296. return;
  4297. }
  4298. if (rt->fbo) {
  4299. glDeleteFramebuffers(1, &rt->fbo);
  4300. glDeleteTextures(1, &rt->color);
  4301. rt->fbo = 0;
  4302. }
  4303. Texture *tex = texture_owner.get(rt->texture);
  4304. tex->alloc_height = 0;
  4305. tex->alloc_width = 0;
  4306. tex->width = 0;
  4307. tex->height = 0;
  4308. tex->active = false;
  4309. if (rt->external.fbo != 0) {
  4310. // free this
  4311. glDeleteFramebuffers(1, &rt->external.fbo);
  4312. // reset our texture back to the original
  4313. tex->tex_id = rt->color;
  4314. if (rt->external.depth != 0 && rt->external.depth_owned) {
  4315. glDeleteRenderbuffers(1, &rt->external.depth);
  4316. }
  4317. rt->external.fbo = 0;
  4318. rt->external.color = 0;
  4319. rt->external.depth = 0;
  4320. rt->external.depth_owned = false;
  4321. }
  4322. if (rt->depth) {
  4323. if (config.support_depth_texture) {
  4324. glDeleteTextures(1, &rt->depth);
  4325. } else {
  4326. glDeleteRenderbuffers(1, &rt->depth);
  4327. }
  4328. rt->depth = 0;
  4329. }
  4330. if (rt->copy_screen_effect.color) {
  4331. glDeleteFramebuffers(1, &rt->copy_screen_effect.fbo);
  4332. rt->copy_screen_effect.fbo = 0;
  4333. glDeleteTextures(1, &rt->copy_screen_effect.color);
  4334. rt->copy_screen_effect.color = 0;
  4335. }
  4336. for (int i = 0; i < 2; i++) {
  4337. if (rt->mip_maps[i].sizes.size()) {
  4338. for (int j = 0; j < rt->mip_maps[i].sizes.size(); j++) {
  4339. glDeleteFramebuffers(1, &rt->mip_maps[i].sizes[j].fbo);
  4340. glDeleteTextures(1, &rt->mip_maps[i].sizes[j].color);
  4341. }
  4342. glDeleteTextures(1, &rt->mip_maps[i].color);
  4343. rt->mip_maps[i].sizes.clear();
  4344. rt->mip_maps[i].levels = 0;
  4345. rt->mip_maps[i].color = 0;
  4346. }
  4347. }
  4348. if (rt->multisample_active) {
  4349. glDeleteFramebuffers(1, &rt->multisample_fbo);
  4350. rt->multisample_fbo = 0;
  4351. glDeleteRenderbuffers(1, &rt->multisample_depth);
  4352. rt->multisample_depth = 0;
  4353. #ifdef ANDROID_ENABLED
  4354. glDeleteTextures(1, &rt->multisample_color);
  4355. #else
  4356. glDeleteRenderbuffers(1, &rt->multisample_color);
  4357. #endif
  4358. rt->multisample_color = 0;
  4359. }
  4360. }
  4361. RID RasterizerStorageGLES2::render_target_create() {
  4362. RenderTarget *rt = memnew(RenderTarget);
  4363. Texture *t = memnew(Texture);
  4364. t->type = VS::TEXTURE_TYPE_2D;
  4365. t->flags = 0;
  4366. t->width = 0;
  4367. t->height = 0;
  4368. t->alloc_height = 0;
  4369. t->alloc_width = 0;
  4370. t->format = Image::FORMAT_R8;
  4371. t->target = GL_TEXTURE_2D;
  4372. t->gl_format_cache = 0;
  4373. t->gl_internal_format_cache = 0;
  4374. t->gl_type_cache = 0;
  4375. t->data_size = 0;
  4376. t->total_data_size = 0;
  4377. t->ignore_mipmaps = false;
  4378. t->compressed = false;
  4379. t->mipmaps = 1;
  4380. t->active = true;
  4381. t->tex_id = 0;
  4382. t->render_target = rt;
  4383. rt->texture = texture_owner.make_rid(t);
  4384. return render_target_owner.make_rid(rt);
  4385. }
  4386. void RasterizerStorageGLES2::render_target_set_position(RID p_render_target, int p_x, int p_y) {
  4387. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  4388. ERR_FAIL_COND(!rt);
  4389. rt->x = p_x;
  4390. rt->y = p_y;
  4391. }
  4392. void RasterizerStorageGLES2::render_target_set_size(RID p_render_target, int p_width, int p_height) {
  4393. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  4394. ERR_FAIL_COND(!rt);
  4395. if (p_width == rt->width && p_height == rt->height) {
  4396. return;
  4397. }
  4398. _render_target_clear(rt);
  4399. rt->width = p_width;
  4400. rt->height = p_height;
  4401. _render_target_allocate(rt);
  4402. }
  4403. RID RasterizerStorageGLES2::render_target_get_texture(RID p_render_target) const {
  4404. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  4405. ERR_FAIL_COND_V(!rt, RID());
  4406. return rt->texture;
  4407. }
  4408. uint32_t RasterizerStorageGLES2::render_target_get_depth_texture_id(RID p_render_target) const {
  4409. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  4410. ERR_FAIL_COND_V(!rt, 0);
  4411. if (rt->external.depth == 0) {
  4412. return rt->depth;
  4413. } else {
  4414. return rt->external.depth;
  4415. }
  4416. }
  4417. void RasterizerStorageGLES2::render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id, unsigned int p_depth_id) {
  4418. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  4419. ERR_FAIL_COND(!rt);
  4420. if (p_texture_id == 0) {
  4421. if (rt->external.fbo != 0) {
  4422. // free this
  4423. glDeleteFramebuffers(1, &rt->external.fbo);
  4424. // and this
  4425. if (rt->external.depth != 0 && rt->external.depth_owned) {
  4426. glDeleteRenderbuffers(1, &rt->external.depth);
  4427. }
  4428. // reset our texture back to the original
  4429. Texture *t = texture_owner.get(rt->texture);
  4430. t->tex_id = rt->color;
  4431. t->width = rt->width;
  4432. t->alloc_width = rt->width;
  4433. t->height = rt->height;
  4434. t->alloc_height = rt->height;
  4435. rt->external.fbo = 0;
  4436. rt->external.color = 0;
  4437. rt->external.depth = 0;
  4438. }
  4439. } else {
  4440. if (rt->external.fbo == 0) {
  4441. // create our fbo
  4442. glGenFramebuffers(1, &rt->external.fbo);
  4443. }
  4444. // bind our frame buffer
  4445. glBindFramebuffer(GL_FRAMEBUFFER, rt->external.fbo);
  4446. rt->external.color = p_texture_id;
  4447. // Set our texture to the new image, note that we expect formats to be the same (or compatible) so we don't change those
  4448. Texture *t = texture_owner.get(rt->texture);
  4449. t->tex_id = p_texture_id;
  4450. t->width = rt->width;
  4451. t->height = rt->height;
  4452. t->alloc_height = rt->width;
  4453. t->alloc_width = rt->height;
  4454. // Switch our texture on our frame buffer
  4455. #if ANDROID_ENABLED
  4456. if (rt->msaa >= VS::VIEWPORT_MSAA_EXT_2X && rt->msaa <= VS::VIEWPORT_MSAA_EXT_4X) {
  4457. // This code only applies to the Oculus Go and Oculus Quest. Due to the the tiled nature
  4458. // of the GPU we can do a single render pass by rendering directly into our texture chains
  4459. // texture and apply MSAA as we render.
  4460. // On any other hardware these two modes are ignored and we do not have any MSAA,
  4461. // the normal MSAA modes need to be used to enable our two pass approach
  4462. // If we created a depth buffer before and we're now passed one, we need to clear it out
  4463. if (rt->external.depth != 0 && rt->external.depth_owned && p_depth_id != 0) {
  4464. glDeleteRenderbuffers(1, &rt->external.depth);
  4465. rt->external.depth_owned = false;
  4466. rt->external.depth = 0;
  4467. }
  4468. if (!rt->external.depth_owned) {
  4469. rt->external.depth = p_depth_id;
  4470. }
  4471. static const int msaa_value[] = { 2, 4 };
  4472. int msaa = msaa_value[rt->msaa - VS::VIEWPORT_MSAA_EXT_2X];
  4473. if (rt->external.depth == 0) {
  4474. rt->external.depth_owned = true;
  4475. // create a multisample depth buffer, we're not reusing Godots because Godot's didn't get created..
  4476. glGenRenderbuffers(1, &rt->external.depth);
  4477. glBindRenderbuffer(GL_RENDERBUFFER, rt->external.depth);
  4478. glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, config.depth_buffer_internalformat, rt->width, rt->height);
  4479. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->external.depth);
  4480. } else if (!rt->external.depth_owned) {
  4481. // we make an exception here, external plugin MUST make sure this is a proper multisample render buffer!
  4482. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->external.depth);
  4483. }
  4484. // and set our external texture as the texture...
  4485. glFramebufferTexture2DMultisample(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_texture_id, 0, msaa);
  4486. } else
  4487. #endif
  4488. {
  4489. // if MSAA as on before, clear our render buffer
  4490. if (rt->external.depth != 0 && rt->external.depth_owned) {
  4491. glDeleteRenderbuffers(1, &rt->external.depth);
  4492. }
  4493. rt->external.depth_owned = false;
  4494. rt->external.depth = p_depth_id;
  4495. // set our texture as the destination for our framebuffer
  4496. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_texture_id, 0);
  4497. // seeing we're rendering into this directly, better also use our depth buffer, just use our existing one :)
  4498. if (rt->external.depth != 0) {
  4499. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rt->external.depth, 0);
  4500. } else if (config.support_depth_texture) {
  4501. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rt->depth, 0);
  4502. } else {
  4503. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth);
  4504. }
  4505. }
  4506. // check status and unbind
  4507. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  4508. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES2::system_fbo);
  4509. if (status != GL_FRAMEBUFFER_COMPLETE) {
  4510. printf("framebuffer fail, status: %x\n", status);
  4511. }
  4512. ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE);
  4513. }
  4514. }
  4515. void RasterizerStorageGLES2::render_target_set_flag(RID p_render_target, RenderTargetFlags p_flag, bool p_value) {
  4516. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  4517. ERR_FAIL_COND(!rt);
  4518. // When setting DIRECT_TO_SCREEN, you need to clear before the value is set, but allocate after as
  4519. // those functions change how they operate depending on the value of DIRECT_TO_SCREEN
  4520. if (p_flag == RENDER_TARGET_DIRECT_TO_SCREEN && p_value != rt->flags[RENDER_TARGET_DIRECT_TO_SCREEN]) {
  4521. _render_target_clear(rt);
  4522. rt->flags[p_flag] = p_value;
  4523. _render_target_allocate(rt);
  4524. }
  4525. rt->flags[p_flag] = p_value;
  4526. switch (p_flag) {
  4527. case RENDER_TARGET_TRANSPARENT:
  4528. case RENDER_TARGET_HDR:
  4529. case RENDER_TARGET_NO_3D:
  4530. case RENDER_TARGET_NO_SAMPLING:
  4531. case RENDER_TARGET_NO_3D_EFFECTS: {
  4532. //must reset for these formats
  4533. _render_target_clear(rt);
  4534. _render_target_allocate(rt);
  4535. } break;
  4536. default: {
  4537. }
  4538. }
  4539. }
  4540. bool RasterizerStorageGLES2::render_target_was_used(RID p_render_target) {
  4541. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  4542. ERR_FAIL_COND_V(!rt, false);
  4543. return rt->used_in_frame;
  4544. }
  4545. void RasterizerStorageGLES2::render_target_clear_used(RID p_render_target) {
  4546. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  4547. ERR_FAIL_COND(!rt);
  4548. rt->used_in_frame = false;
  4549. }
  4550. void RasterizerStorageGLES2::render_target_set_msaa(RID p_render_target, VS::ViewportMSAA p_msaa) {
  4551. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  4552. ERR_FAIL_COND(!rt);
  4553. if (rt->msaa == p_msaa) {
  4554. return;
  4555. }
  4556. if (!config.multisample_supported) {
  4557. ERR_PRINT("MSAA not supported on this hardware.");
  4558. return;
  4559. }
  4560. _render_target_clear(rt);
  4561. rt->msaa = p_msaa;
  4562. _render_target_allocate(rt);
  4563. }
  4564. void RasterizerStorageGLES2::render_target_set_use_fxaa(RID p_render_target, bool p_fxaa) {
  4565. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  4566. ERR_FAIL_COND(!rt);
  4567. rt->use_fxaa = p_fxaa;
  4568. }
  4569. void RasterizerStorageGLES2::render_target_set_use_debanding(RID p_render_target, bool p_debanding) {
  4570. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  4571. ERR_FAIL_COND(!rt);
  4572. if (p_debanding) {
  4573. WARN_PRINT_ONCE("Debanding is not supported in the GLES2 backend. To use debanding, switch to the GLES3 backend and make sure HDR is enabled.");
  4574. }
  4575. rt->use_debanding = p_debanding;
  4576. }
  4577. void RasterizerStorageGLES2::render_target_set_sharpen_intensity(RID p_render_target, float p_intensity) {
  4578. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  4579. ERR_FAIL_COND(!rt);
  4580. if (p_intensity >= 0.001) {
  4581. WARN_PRINT_ONCE("Sharpening is not supported in the GLES2 backend. To use sharpening, switch to the GLES3 backend.");
  4582. }
  4583. rt->sharpen_intensity = p_intensity;
  4584. }
  4585. /* CANVAS SHADOW */
  4586. RID RasterizerStorageGLES2::canvas_light_shadow_buffer_create(int p_width) {
  4587. CanvasLightShadow *cls = memnew(CanvasLightShadow);
  4588. if (p_width > config.max_texture_size) {
  4589. p_width = config.max_texture_size;
  4590. }
  4591. cls->size = p_width;
  4592. cls->height = 16;
  4593. glActiveTexture(GL_TEXTURE0);
  4594. glGenFramebuffers(1, &cls->fbo);
  4595. glBindFramebuffer(GL_FRAMEBUFFER, cls->fbo);
  4596. glGenRenderbuffers(1, &cls->depth);
  4597. glBindRenderbuffer(GL_RENDERBUFFER, cls->depth);
  4598. glRenderbufferStorage(GL_RENDERBUFFER, config.depth_buffer_internalformat, cls->size, cls->height);
  4599. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, cls->depth);
  4600. glGenTextures(1, &cls->distance);
  4601. glBindTexture(GL_TEXTURE_2D, cls->distance);
  4602. if (config.use_rgba_2d_shadows) {
  4603. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cls->size, cls->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  4604. } else {
  4605. #ifdef GLES_OVER_GL
  4606. glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, cls->size, cls->height, 0, _RED_OES, GL_FLOAT, nullptr);
  4607. #else
  4608. glTexImage2D(GL_TEXTURE_2D, 0, GL_FLOAT, cls->size, cls->height, 0, _RED_OES, GL_FLOAT, NULL);
  4609. #endif
  4610. }
  4611. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  4612. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  4613. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  4614. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  4615. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, cls->distance, 0);
  4616. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  4617. //printf("errnum: %x\n",status);
  4618. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES2::system_fbo);
  4619. if (status != GL_FRAMEBUFFER_COMPLETE) {
  4620. memdelete(cls);
  4621. ERR_FAIL_COND_V(status != GL_FRAMEBUFFER_COMPLETE, RID());
  4622. }
  4623. return canvas_light_shadow_owner.make_rid(cls);
  4624. }
  4625. /* LIGHT SHADOW MAPPING */
  4626. RID RasterizerStorageGLES2::canvas_light_occluder_create() {
  4627. CanvasOccluder *co = memnew(CanvasOccluder);
  4628. co->index_id = 0;
  4629. co->vertex_id = 0;
  4630. co->len = 0;
  4631. return canvas_occluder_owner.make_rid(co);
  4632. }
  4633. void RasterizerStorageGLES2::canvas_light_occluder_set_polylines(RID p_occluder, const PoolVector<Vector2> &p_lines) {
  4634. CanvasOccluder *co = canvas_occluder_owner.get(p_occluder);
  4635. ERR_FAIL_COND(!co);
  4636. co->lines = p_lines;
  4637. if (p_lines.size() != co->len) {
  4638. if (co->index_id) {
  4639. glDeleteBuffers(1, &co->index_id);
  4640. }
  4641. if (co->vertex_id) {
  4642. glDeleteBuffers(1, &co->vertex_id);
  4643. }
  4644. co->index_id = 0;
  4645. co->vertex_id = 0;
  4646. co->len = 0;
  4647. }
  4648. if (p_lines.size()) {
  4649. PoolVector<float> geometry;
  4650. PoolVector<uint16_t> indices;
  4651. int lc = p_lines.size();
  4652. geometry.resize(lc * 6);
  4653. indices.resize(lc * 3);
  4654. PoolVector<float>::Write vw = geometry.write();
  4655. PoolVector<uint16_t>::Write iw = indices.write();
  4656. PoolVector<Vector2>::Read lr = p_lines.read();
  4657. const int POLY_HEIGHT = 16384;
  4658. for (int i = 0; i < lc / 2; i++) {
  4659. vw[i * 12 + 0] = lr[i * 2 + 0].x;
  4660. vw[i * 12 + 1] = lr[i * 2 + 0].y;
  4661. vw[i * 12 + 2] = POLY_HEIGHT;
  4662. vw[i * 12 + 3] = lr[i * 2 + 1].x;
  4663. vw[i * 12 + 4] = lr[i * 2 + 1].y;
  4664. vw[i * 12 + 5] = POLY_HEIGHT;
  4665. vw[i * 12 + 6] = lr[i * 2 + 1].x;
  4666. vw[i * 12 + 7] = lr[i * 2 + 1].y;
  4667. vw[i * 12 + 8] = -POLY_HEIGHT;
  4668. vw[i * 12 + 9] = lr[i * 2 + 0].x;
  4669. vw[i * 12 + 10] = lr[i * 2 + 0].y;
  4670. vw[i * 12 + 11] = -POLY_HEIGHT;
  4671. iw[i * 6 + 0] = i * 4 + 0;
  4672. iw[i * 6 + 1] = i * 4 + 1;
  4673. iw[i * 6 + 2] = i * 4 + 2;
  4674. iw[i * 6 + 3] = i * 4 + 2;
  4675. iw[i * 6 + 4] = i * 4 + 3;
  4676. iw[i * 6 + 5] = i * 4 + 0;
  4677. }
  4678. //if same buffer len is being set, just use BufferSubData to avoid a pipeline flush
  4679. if (!co->vertex_id) {
  4680. glGenBuffers(1, &co->vertex_id);
  4681. glBindBuffer(GL_ARRAY_BUFFER, co->vertex_id);
  4682. glBufferData(GL_ARRAY_BUFFER, lc * 6 * sizeof(real_t), vw.ptr(), GL_STATIC_DRAW);
  4683. } else {
  4684. glBindBuffer(GL_ARRAY_BUFFER, co->vertex_id);
  4685. glBufferSubData(GL_ARRAY_BUFFER, 0, lc * 6 * sizeof(real_t), vw.ptr());
  4686. }
  4687. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  4688. if (!co->index_id) {
  4689. glGenBuffers(1, &co->index_id);
  4690. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, co->index_id);
  4691. glBufferData(GL_ELEMENT_ARRAY_BUFFER, lc * 3 * sizeof(uint16_t), iw.ptr(), GL_DYNAMIC_DRAW);
  4692. } else {
  4693. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, co->index_id);
  4694. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, lc * 3 * sizeof(uint16_t), iw.ptr());
  4695. }
  4696. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); //unbind
  4697. co->len = lc;
  4698. }
  4699. }
  4700. VS::InstanceType RasterizerStorageGLES2::get_base_type(RID p_rid) const {
  4701. if (mesh_owner.owns(p_rid)) {
  4702. return VS::INSTANCE_MESH;
  4703. } else if (light_owner.owns(p_rid)) {
  4704. return VS::INSTANCE_LIGHT;
  4705. } else if (multimesh_owner.owns(p_rid)) {
  4706. return VS::INSTANCE_MULTIMESH;
  4707. } else if (immediate_owner.owns(p_rid)) {
  4708. return VS::INSTANCE_IMMEDIATE;
  4709. } else if (reflection_probe_owner.owns(p_rid)) {
  4710. return VS::INSTANCE_REFLECTION_PROBE;
  4711. } else if (lightmap_capture_data_owner.owns(p_rid)) {
  4712. return VS::INSTANCE_LIGHTMAP_CAPTURE;
  4713. } else {
  4714. return VS::INSTANCE_NONE;
  4715. }
  4716. }
  4717. bool RasterizerStorageGLES2::free(RID p_rid) {
  4718. if (render_target_owner.owns(p_rid)) {
  4719. RenderTarget *rt = render_target_owner.getornull(p_rid);
  4720. _render_target_clear(rt);
  4721. Texture *t = texture_owner.get(rt->texture);
  4722. texture_owner.free(rt->texture);
  4723. memdelete(t);
  4724. render_target_owner.free(p_rid);
  4725. memdelete(rt);
  4726. return true;
  4727. } else if (texture_owner.owns(p_rid)) {
  4728. Texture *t = texture_owner.get(p_rid);
  4729. // can't free a render target texture
  4730. ERR_FAIL_COND_V(t->render_target, true);
  4731. info.texture_mem -= t->total_data_size;
  4732. texture_owner.free(p_rid);
  4733. memdelete(t);
  4734. return true;
  4735. } else if (sky_owner.owns(p_rid)) {
  4736. Sky *sky = sky_owner.get(p_rid);
  4737. sky_set_texture(p_rid, RID(), 256);
  4738. sky_owner.free(p_rid);
  4739. memdelete(sky);
  4740. return true;
  4741. } else if (shader_owner.owns(p_rid)) {
  4742. Shader *shader = shader_owner.get(p_rid);
  4743. if (shader->shader && shader->custom_code_id) {
  4744. shader->shader->free_custom_shader(shader->custom_code_id);
  4745. }
  4746. if (shader->dirty_list.in_list()) {
  4747. _shader_dirty_list.remove(&shader->dirty_list);
  4748. }
  4749. while (shader->materials.first()) {
  4750. Material *m = shader->materials.first()->self();
  4751. m->shader = nullptr;
  4752. _material_make_dirty(m);
  4753. shader->materials.remove(shader->materials.first());
  4754. }
  4755. shader_owner.free(p_rid);
  4756. memdelete(shader);
  4757. return true;
  4758. } else if (material_owner.owns(p_rid)) {
  4759. Material *m = material_owner.get(p_rid);
  4760. if (m->shader) {
  4761. m->shader->materials.remove(&m->list);
  4762. }
  4763. for (Map<Geometry *, int>::Element *E = m->geometry_owners.front(); E; E = E->next()) {
  4764. Geometry *g = E->key();
  4765. g->material = RID();
  4766. }
  4767. for (Map<RasterizerScene::InstanceBase *, int>::Element *E = m->instance_owners.front(); E; E = E->next()) {
  4768. RasterizerScene::InstanceBase *ins = E->key();
  4769. if (ins->material_override == p_rid) {
  4770. ins->material_override = RID();
  4771. }
  4772. if (ins->material_overlay == p_rid) {
  4773. ins->material_overlay = RID();
  4774. }
  4775. for (int i = 0; i < ins->materials.size(); i++) {
  4776. if (ins->materials[i] == p_rid) {
  4777. ins->materials.write[i] = RID();
  4778. }
  4779. }
  4780. }
  4781. material_owner.free(p_rid);
  4782. memdelete(m);
  4783. return true;
  4784. } else if (skeleton_owner.owns(p_rid)) {
  4785. Skeleton *s = skeleton_owner.get(p_rid);
  4786. if (s->update_list.in_list()) {
  4787. skeleton_update_list.remove(&s->update_list);
  4788. }
  4789. for (Set<RasterizerScene::InstanceBase *>::Element *E = s->instances.front(); E; E = E->next()) {
  4790. E->get()->skeleton = RID();
  4791. }
  4792. skeleton_allocate(p_rid, 0, false);
  4793. if (s->tex_id) {
  4794. glDeleteTextures(1, &s->tex_id);
  4795. }
  4796. skeleton_owner.free(p_rid);
  4797. memdelete(s);
  4798. return true;
  4799. } else if (mesh_owner.owns(p_rid)) {
  4800. Mesh *mesh = mesh_owner.get(p_rid);
  4801. mesh->instance_remove_deps();
  4802. mesh_clear(p_rid);
  4803. while (mesh->multimeshes.first()) {
  4804. MultiMesh *multimesh = mesh->multimeshes.first()->self();
  4805. multimesh->mesh = RID();
  4806. multimesh->dirty_aabb = true;
  4807. mesh->multimeshes.remove(mesh->multimeshes.first());
  4808. if (!multimesh->update_list.in_list()) {
  4809. multimesh_update_list.add(&multimesh->update_list);
  4810. }
  4811. }
  4812. mesh_owner.free(p_rid);
  4813. memdelete(mesh);
  4814. return true;
  4815. } else if (multimesh_owner.owns(p_rid)) {
  4816. // remove from interpolator
  4817. _interpolation_data.notify_free_multimesh(p_rid);
  4818. MultiMesh *multimesh = multimesh_owner.get(p_rid);
  4819. multimesh->instance_remove_deps();
  4820. if (multimesh->mesh.is_valid()) {
  4821. Mesh *mesh = mesh_owner.getornull(multimesh->mesh);
  4822. if (mesh) {
  4823. mesh->multimeshes.remove(&multimesh->mesh_list);
  4824. }
  4825. }
  4826. multimesh_allocate(p_rid, 0, VS::MULTIMESH_TRANSFORM_3D, VS::MULTIMESH_COLOR_NONE);
  4827. update_dirty_multimeshes();
  4828. multimesh_owner.free(p_rid);
  4829. memdelete(multimesh);
  4830. return true;
  4831. } else if (immediate_owner.owns(p_rid)) {
  4832. Immediate *im = immediate_owner.get(p_rid);
  4833. im->instance_remove_deps();
  4834. immediate_owner.free(p_rid);
  4835. memdelete(im);
  4836. return true;
  4837. } else if (light_owner.owns(p_rid)) {
  4838. Light *light = light_owner.get(p_rid);
  4839. light->instance_remove_deps();
  4840. light_owner.free(p_rid);
  4841. memdelete(light);
  4842. return true;
  4843. } else if (reflection_probe_owner.owns(p_rid)) {
  4844. // delete the texture
  4845. ReflectionProbe *reflection_probe = reflection_probe_owner.get(p_rid);
  4846. reflection_probe->instance_remove_deps();
  4847. reflection_probe_owner.free(p_rid);
  4848. memdelete(reflection_probe);
  4849. return true;
  4850. } else if (lightmap_capture_data_owner.owns(p_rid)) {
  4851. // delete the texture
  4852. LightmapCapture *lightmap_capture = lightmap_capture_data_owner.get(p_rid);
  4853. lightmap_capture->instance_remove_deps();
  4854. lightmap_capture_data_owner.free(p_rid);
  4855. memdelete(lightmap_capture);
  4856. return true;
  4857. } else if (canvas_occluder_owner.owns(p_rid)) {
  4858. CanvasOccluder *co = canvas_occluder_owner.get(p_rid);
  4859. if (co->index_id) {
  4860. glDeleteBuffers(1, &co->index_id);
  4861. }
  4862. if (co->vertex_id) {
  4863. glDeleteBuffers(1, &co->vertex_id);
  4864. }
  4865. canvas_occluder_owner.free(p_rid);
  4866. memdelete(co);
  4867. return true;
  4868. } else if (canvas_light_shadow_owner.owns(p_rid)) {
  4869. CanvasLightShadow *cls = canvas_light_shadow_owner.get(p_rid);
  4870. glDeleteFramebuffers(1, &cls->fbo);
  4871. glDeleteRenderbuffers(1, &cls->depth);
  4872. glDeleteTextures(1, &cls->distance);
  4873. canvas_light_shadow_owner.free(p_rid);
  4874. memdelete(cls);
  4875. return true;
  4876. } else {
  4877. return false;
  4878. }
  4879. }
  4880. bool RasterizerStorageGLES2::has_os_feature(const String &p_feature) const {
  4881. if (p_feature == "pvrtc") {
  4882. return config.pvrtc_supported;
  4883. }
  4884. if (p_feature == "s3tc") {
  4885. return config.s3tc_supported;
  4886. }
  4887. if (p_feature == "etc") {
  4888. return config.etc1_supported;
  4889. }
  4890. if (p_feature == "skinning_fallback") {
  4891. return config.use_skeleton_software;
  4892. }
  4893. return false;
  4894. }
  4895. ////////////////////////////////////////////
  4896. void RasterizerStorageGLES2::set_debug_generate_wireframes(bool p_generate) {
  4897. }
  4898. void RasterizerStorageGLES2::render_info_begin_capture() {
  4899. info.snap = info.render;
  4900. }
  4901. void RasterizerStorageGLES2::render_info_end_capture() {
  4902. info.snap.object_count = info.render.object_count - info.snap.object_count;
  4903. info.snap.draw_call_count = info.render.draw_call_count - info.snap.draw_call_count;
  4904. info.snap.material_switch_count = info.render.material_switch_count - info.snap.material_switch_count;
  4905. info.snap.surface_switch_count = info.render.surface_switch_count - info.snap.surface_switch_count;
  4906. info.snap.shader_rebind_count = info.render.shader_rebind_count - info.snap.shader_rebind_count;
  4907. info.snap.vertices_count = info.render.vertices_count - info.snap.vertices_count;
  4908. info.snap._2d_item_count = info.render._2d_item_count - info.snap._2d_item_count;
  4909. info.snap._2d_draw_call_count = info.render._2d_draw_call_count - info.snap._2d_draw_call_count;
  4910. }
  4911. int RasterizerStorageGLES2::get_captured_render_info(VS::RenderInfo p_info) {
  4912. switch (p_info) {
  4913. case VS::INFO_OBJECTS_IN_FRAME: {
  4914. return info.snap.object_count;
  4915. } break;
  4916. case VS::INFO_VERTICES_IN_FRAME: {
  4917. return info.snap.vertices_count;
  4918. } break;
  4919. case VS::INFO_MATERIAL_CHANGES_IN_FRAME: {
  4920. return info.snap.material_switch_count;
  4921. } break;
  4922. case VS::INFO_SHADER_CHANGES_IN_FRAME: {
  4923. return info.snap.shader_rebind_count;
  4924. } break;
  4925. case VS::INFO_SURFACE_CHANGES_IN_FRAME: {
  4926. return info.snap.surface_switch_count;
  4927. } break;
  4928. case VS::INFO_DRAW_CALLS_IN_FRAME: {
  4929. return info.snap.draw_call_count;
  4930. } break;
  4931. case VS::INFO_2D_ITEMS_IN_FRAME: {
  4932. return info.snap._2d_item_count;
  4933. } break;
  4934. case VS::INFO_2D_DRAW_CALLS_IN_FRAME: {
  4935. return info.snap._2d_draw_call_count;
  4936. } break;
  4937. default: {
  4938. return get_render_info(p_info);
  4939. }
  4940. }
  4941. }
  4942. uint64_t RasterizerStorageGLES2::get_render_info(VS::RenderInfo p_info) {
  4943. switch (p_info) {
  4944. case VS::INFO_OBJECTS_IN_FRAME:
  4945. return info.render_final.object_count;
  4946. case VS::INFO_VERTICES_IN_FRAME:
  4947. return info.render_final.vertices_count;
  4948. case VS::INFO_MATERIAL_CHANGES_IN_FRAME:
  4949. return info.render_final.material_switch_count;
  4950. case VS::INFO_SHADER_CHANGES_IN_FRAME:
  4951. return info.render_final.shader_rebind_count;
  4952. case VS::INFO_SURFACE_CHANGES_IN_FRAME:
  4953. return info.render_final.surface_switch_count;
  4954. case VS::INFO_DRAW_CALLS_IN_FRAME:
  4955. return info.render_final.draw_call_count;
  4956. case VS::INFO_2D_ITEMS_IN_FRAME:
  4957. return info.render_final._2d_item_count;
  4958. case VS::INFO_2D_DRAW_CALLS_IN_FRAME:
  4959. return info.render_final._2d_draw_call_count;
  4960. case VS::INFO_USAGE_VIDEO_MEM_TOTAL:
  4961. return 0; //no idea
  4962. case VS::INFO_VIDEO_MEM_USED:
  4963. return info.vertex_mem + info.texture_mem;
  4964. case VS::INFO_TEXTURE_MEM_USED:
  4965. return info.texture_mem;
  4966. case VS::INFO_VERTEX_MEM_USED:
  4967. return info.vertex_mem;
  4968. default:
  4969. return 0; //no idea either
  4970. }
  4971. }
  4972. String RasterizerStorageGLES2::get_video_adapter_name() const {
  4973. return (const char *)glGetString(GL_RENDERER);
  4974. }
  4975. String RasterizerStorageGLES2::get_video_adapter_vendor() const {
  4976. return (const char *)glGetString(GL_VENDOR);
  4977. }
  4978. void RasterizerStorageGLES2::initialize() {
  4979. RasterizerStorageGLES2::system_fbo = 0;
  4980. {
  4981. const GLubyte *extension_string = glGetString(GL_EXTENSIONS);
  4982. Vector<String> extensions = String((const char *)extension_string).split(" ");
  4983. for (int i = 0; i < extensions.size(); i++) {
  4984. config.extensions.insert(extensions[i]);
  4985. }
  4986. }
  4987. config.keep_original_textures = false;
  4988. config.shrink_textures_x2 = false;
  4989. config.depth_internalformat = GL_DEPTH_COMPONENT;
  4990. config.depth_type = GL_UNSIGNED_INT;
  4991. #ifdef GLES_OVER_GL
  4992. config.float_texture_supported = true;
  4993. config.s3tc_supported = true;
  4994. config.pvrtc_supported = false;
  4995. config.etc1_supported = false;
  4996. config.support_npot_repeat_mipmap = true;
  4997. config.depth_buffer_internalformat = GL_DEPTH_COMPONENT24;
  4998. #else
  4999. config.float_texture_supported = config.extensions.has("GL_ARB_texture_float") || config.extensions.has("GL_OES_texture_float");
  5000. config.s3tc_supported = config.extensions.has("GL_EXT_texture_compression_s3tc") || config.extensions.has("WEBGL_compressed_texture_s3tc");
  5001. config.etc1_supported = config.extensions.has("GL_OES_compressed_ETC1_RGB8_texture") || config.extensions.has("WEBGL_compressed_texture_etc1");
  5002. config.pvrtc_supported = config.extensions.has("GL_IMG_texture_compression_pvrtc") || config.extensions.has("WEBGL_compressed_texture_pvrtc");
  5003. config.support_npot_repeat_mipmap = config.extensions.has("GL_OES_texture_npot");
  5004. // If the desktop build is using S3TC, and you export / run from the IDE for android, if the device supports
  5005. // S3TC it will crash trying to load these textures, as they are not exported in the APK. This is a simple way
  5006. // to prevent Android devices trying to load S3TC, by faking lack of hardware support.
  5007. #if defined(ANDROID_ENABLED) || defined(IPHONE_ENABLED)
  5008. config.s3tc_supported = false;
  5009. #endif
  5010. #ifdef JAVASCRIPT_ENABLED
  5011. // RenderBuffer internal format must be 16 bits in WebGL,
  5012. // but depth_texture should default to 32 always
  5013. // if the implementation doesn't support 32, it should just quietly use 16 instead
  5014. // https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/
  5015. config.depth_buffer_internalformat = GL_DEPTH_COMPONENT16;
  5016. config.depth_type = GL_UNSIGNED_INT;
  5017. #else
  5018. // on mobile check for 24 bit depth support for RenderBufferStorage
  5019. if (config.extensions.has("GL_OES_depth24")) {
  5020. config.depth_buffer_internalformat = _DEPTH_COMPONENT24_OES;
  5021. config.depth_type = GL_UNSIGNED_INT;
  5022. } else {
  5023. config.depth_buffer_internalformat = GL_DEPTH_COMPONENT16;
  5024. config.depth_type = GL_UNSIGNED_SHORT;
  5025. }
  5026. #endif
  5027. #endif
  5028. #ifndef GLES_OVER_GL
  5029. //Manually load extensions for android and ios
  5030. #ifdef IPHONE_ENABLED
  5031. // appears that IPhone doesn't need to dlopen TODO: test this rigorously before removing
  5032. //void *gles2_lib = dlopen(NULL, RTLD_LAZY);
  5033. //glRenderbufferStorageMultisampleAPPLE = dlsym(gles2_lib, "glRenderbufferStorageMultisampleAPPLE");
  5034. //glResolveMultisampleFramebufferAPPLE = dlsym(gles2_lib, "glResolveMultisampleFramebufferAPPLE");
  5035. #elif ANDROID_ENABLED
  5036. void *gles2_lib = dlopen("libGLESv2.so", RTLD_LAZY);
  5037. glRenderbufferStorageMultisampleEXT = (PFNGLRENDERBUFFERSTORAGEMULTISAMPLEEXTPROC)dlsym(gles2_lib, "glRenderbufferStorageMultisampleEXT");
  5038. glFramebufferTexture2DMultisampleEXT = (PFNGLFRAMEBUFFERTEXTURE2DMULTISAMPLEEXTPROC)dlsym(gles2_lib, "glFramebufferTexture2DMultisampleEXT");
  5039. #endif
  5040. #endif
  5041. // Check for multisample support
  5042. config.multisample_supported = config.extensions.has("GL_EXT_framebuffer_multisample") || config.extensions.has("GL_EXT_multisampled_render_to_texture") || config.extensions.has("GL_APPLE_framebuffer_multisample");
  5043. #ifdef GLES_OVER_GL
  5044. //TODO: causes huge problems with desktop video drivers. Making false for now, needs to be true to render SCREEN_TEXTURE mipmaps
  5045. config.render_to_mipmap_supported = false;
  5046. #else
  5047. //check if mipmaps can be used for SCREEN_TEXTURE and Glow on Mobile and web platforms
  5048. config.render_to_mipmap_supported = config.extensions.has("GL_OES_fbo_render_mipmap") && config.extensions.has("GL_EXT_texture_lod");
  5049. #endif
  5050. #ifdef GLES_OVER_GL
  5051. config.use_rgba_2d_shadows = false;
  5052. config.support_depth_texture = true;
  5053. config.use_rgba_3d_shadows = false;
  5054. config.support_depth_cubemaps = true;
  5055. #else
  5056. config.use_rgba_2d_shadows = !(config.float_texture_supported && config.extensions.has("GL_EXT_texture_rg"));
  5057. config.support_depth_texture = config.extensions.has("GL_OES_depth_texture") || config.extensions.has("WEBGL_depth_texture");
  5058. config.use_rgba_3d_shadows = !config.support_depth_texture;
  5059. config.support_depth_cubemaps = config.extensions.has("GL_OES_depth_texture_cube_map");
  5060. #endif
  5061. #ifdef GLES_OVER_GL
  5062. config.support_32_bits_indices = true;
  5063. #else
  5064. config.support_32_bits_indices = config.extensions.has("GL_OES_element_index_uint");
  5065. #endif
  5066. #ifdef GLES_OVER_GL
  5067. config.support_write_depth = true;
  5068. #elif defined(JAVASCRIPT_ENABLED)
  5069. config.support_write_depth = false;
  5070. #else
  5071. config.support_write_depth = config.extensions.has("GL_EXT_frag_depth");
  5072. #endif
  5073. config.support_half_float_vertices = true;
  5074. //every platform should support this except web, iOS has issues with their support, so add option to disable
  5075. #ifdef JAVASCRIPT_ENABLED
  5076. config.support_half_float_vertices = false;
  5077. #endif
  5078. bool disable_half_float = GLOBAL_GET("rendering/gles2/compatibility/disable_half_float");
  5079. if (disable_half_float) {
  5080. config.support_half_float_vertices = false;
  5081. }
  5082. 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");
  5083. config.bptc_supported = config.extensions.has("GL_ARB_texture_compression_bptc") || config.extensions.has("EXT_texture_compression_bptc");
  5084. config.anisotropic_level = 1.0;
  5085. config.use_anisotropic_filter = config.extensions.has("GL_EXT_texture_filter_anisotropic");
  5086. if (config.use_anisotropic_filter) {
  5087. glGetFloatv(_GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &config.anisotropic_level);
  5088. config.anisotropic_level = MIN(int(ProjectSettings::get_singleton()->get("rendering/quality/filters/anisotropic_filter_level")), config.anisotropic_level);
  5089. }
  5090. //determine formats for depth textures (or renderbuffers)
  5091. if (config.support_depth_texture) {
  5092. // Will use texture for depth
  5093. // have to manually see if we can create a valid framebuffer texture using UNSIGNED_INT,
  5094. // as there is no extension to test for this.
  5095. GLuint fbo;
  5096. glGenFramebuffers(1, &fbo);
  5097. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  5098. GLuint depth;
  5099. glGenTextures(1, &depth);
  5100. glBindTexture(GL_TEXTURE_2D, depth);
  5101. glTexImage2D(GL_TEXTURE_2D, 0, config.depth_internalformat, 32, 32, 0, GL_DEPTH_COMPONENT, config.depth_type, nullptr);
  5102. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  5103. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  5104. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  5105. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  5106. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth, 0);
  5107. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  5108. glBindFramebuffer(GL_FRAMEBUFFER, system_fbo);
  5109. glDeleteFramebuffers(1, &fbo);
  5110. glBindTexture(GL_TEXTURE_2D, 0);
  5111. glDeleteTextures(1, &depth);
  5112. if (status != GL_FRAMEBUFFER_COMPLETE) {
  5113. // If it fails, test to see if it supports a framebuffer texture using UNSIGNED_SHORT
  5114. // This is needed because many OSX devices don't support either UNSIGNED_INT or UNSIGNED_SHORT
  5115. #ifdef GLES_OVER_GL
  5116. config.depth_internalformat = GL_DEPTH_COMPONENT16;
  5117. #else
  5118. // OES_depth_texture extension only specifies GL_DEPTH_COMPONENT.
  5119. config.depth_internalformat = GL_DEPTH_COMPONENT;
  5120. #endif
  5121. config.depth_type = GL_UNSIGNED_SHORT;
  5122. glGenFramebuffers(1, &fbo);
  5123. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  5124. glGenTextures(1, &depth);
  5125. glBindTexture(GL_TEXTURE_2D, depth);
  5126. glTexImage2D(GL_TEXTURE_2D, 0, config.depth_internalformat, 32, 32, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, nullptr);
  5127. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  5128. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  5129. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  5130. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  5131. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth, 0);
  5132. status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  5133. if (status != GL_FRAMEBUFFER_COMPLETE) {
  5134. //if it fails again depth textures aren't supported, use rgba shadows and renderbuffer for depth
  5135. config.support_depth_texture = false;
  5136. config.use_rgba_3d_shadows = true;
  5137. }
  5138. glBindFramebuffer(GL_FRAMEBUFFER, system_fbo);
  5139. glDeleteFramebuffers(1, &fbo);
  5140. glBindTexture(GL_TEXTURE_2D, 0);
  5141. glDeleteTextures(1, &depth);
  5142. }
  5143. }
  5144. //picky requirements for these
  5145. config.support_shadow_cubemaps = config.support_depth_texture && config.support_write_depth && config.support_depth_cubemaps;
  5146. if (!config.support_shadow_cubemaps) {
  5147. print_verbose("OmniLight cubemap shadows are not supported by this GPU. Falling back to dual paraboloid shadows for all omni lights (faster but less precise).");
  5148. }
  5149. frame.count = 0;
  5150. frame.delta = 0;
  5151. frame.current_rt = nullptr;
  5152. frame.clear_request = false;
  5153. glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &config.max_vertex_texture_image_units);
  5154. glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &config.max_texture_image_units);
  5155. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &config.max_texture_size);
  5156. glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &config.max_cubemap_texture_size);
  5157. glGetIntegerv(GL_MAX_VIEWPORT_DIMS, config.max_viewport_dimensions);
  5158. // the use skeleton software path should be used if either float texture is not supported,
  5159. // OR max_vertex_texture_image_units is zero
  5160. config.use_skeleton_software = (config.float_texture_supported == false) || (config.max_vertex_texture_image_units == 0);
  5161. shaders.copy.init();
  5162. shaders.cubemap_filter.init();
  5163. bool ggx_hq = GLOBAL_GET("rendering/quality/reflections/high_quality_ggx");
  5164. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::LOW_QUALITY, !ggx_hq);
  5165. {
  5166. // quad for copying stuff
  5167. glGenBuffers(1, &resources.quadie);
  5168. glBindBuffer(GL_ARRAY_BUFFER, resources.quadie);
  5169. {
  5170. const float qv[16] = {
  5171. -1,
  5172. -1,
  5173. 0,
  5174. 0,
  5175. -1,
  5176. 1,
  5177. 0,
  5178. 1,
  5179. 1,
  5180. 1,
  5181. 1,
  5182. 1,
  5183. 1,
  5184. -1,
  5185. 1,
  5186. 0,
  5187. };
  5188. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 16, qv, GL_STATIC_DRAW);
  5189. }
  5190. glBindBuffer(GL_ARRAY_BUFFER, 0);
  5191. }
  5192. {
  5193. //default textures
  5194. glGenTextures(1, &resources.white_tex);
  5195. unsigned char whitetexdata[8 * 8 * 3];
  5196. for (int i = 0; i < 8 * 8 * 3; i++) {
  5197. whitetexdata[i] = 255;
  5198. }
  5199. glActiveTexture(GL_TEXTURE0);
  5200. glBindTexture(GL_TEXTURE_2D, resources.white_tex);
  5201. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, whitetexdata);
  5202. glGenerateMipmap(GL_TEXTURE_2D);
  5203. glBindTexture(GL_TEXTURE_2D, 0);
  5204. glGenTextures(1, &resources.black_tex);
  5205. unsigned char blacktexdata[8 * 8 * 3];
  5206. for (int i = 0; i < 8 * 8 * 3; i++) {
  5207. blacktexdata[i] = 0;
  5208. }
  5209. glActiveTexture(GL_TEXTURE0);
  5210. glBindTexture(GL_TEXTURE_2D, resources.black_tex);
  5211. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, blacktexdata);
  5212. glGenerateMipmap(GL_TEXTURE_2D);
  5213. glBindTexture(GL_TEXTURE_2D, 0);
  5214. glGenTextures(1, &resources.normal_tex);
  5215. unsigned char normaltexdata[8 * 8 * 3];
  5216. for (int i = 0; i < 8 * 8 * 3; i += 3) {
  5217. normaltexdata[i + 0] = 128;
  5218. normaltexdata[i + 1] = 128;
  5219. normaltexdata[i + 2] = 255;
  5220. }
  5221. glActiveTexture(GL_TEXTURE0);
  5222. glBindTexture(GL_TEXTURE_2D, resources.normal_tex);
  5223. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, normaltexdata);
  5224. glGenerateMipmap(GL_TEXTURE_2D);
  5225. glBindTexture(GL_TEXTURE_2D, 0);
  5226. glGenTextures(1, &resources.aniso_tex);
  5227. unsigned char anisotexdata[8 * 8 * 3];
  5228. for (int i = 0; i < 8 * 8 * 3; i += 3) {
  5229. anisotexdata[i + 0] = 255;
  5230. anisotexdata[i + 1] = 128;
  5231. anisotexdata[i + 2] = 0;
  5232. }
  5233. glActiveTexture(GL_TEXTURE0);
  5234. glBindTexture(GL_TEXTURE_2D, resources.aniso_tex);
  5235. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, anisotexdata);
  5236. glGenerateMipmap(GL_TEXTURE_2D);
  5237. glBindTexture(GL_TEXTURE_2D, 0);
  5238. }
  5239. // skeleton buffer
  5240. {
  5241. resources.skeleton_transform_buffer_size = 0;
  5242. glGenBuffers(1, &resources.skeleton_transform_buffer);
  5243. }
  5244. // blend buffer
  5245. {
  5246. resources.blend_shape_transform_cpu_buffer_size = 0;
  5247. }
  5248. // radical inverse vdc cache texture
  5249. // used for cubemap filtering
  5250. if (true /*||config.float_texture_supported*/) { //uint8 is similar and works everywhere
  5251. glGenTextures(1, &resources.radical_inverse_vdc_cache_tex);
  5252. glActiveTexture(GL_TEXTURE0);
  5253. glBindTexture(GL_TEXTURE_2D, resources.radical_inverse_vdc_cache_tex);
  5254. uint8_t radical_inverse[512];
  5255. for (uint32_t i = 0; i < 512; i++) {
  5256. uint32_t bits = i;
  5257. bits = (bits << 16) | (bits >> 16);
  5258. bits = ((bits & 0x55555555) << 1) | ((bits & 0xAAAAAAAA) >> 1);
  5259. bits = ((bits & 0x33333333) << 2) | ((bits & 0xCCCCCCCC) >> 2);
  5260. bits = ((bits & 0x0F0F0F0F) << 4) | ((bits & 0xF0F0F0F0) >> 4);
  5261. bits = ((bits & 0x00FF00FF) << 8) | ((bits & 0xFF00FF00) >> 8);
  5262. float value = float(bits) * 2.3283064365386963e-10;
  5263. radical_inverse[i] = uint8_t(CLAMP(value * 255.0, 0, 255));
  5264. }
  5265. glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 512, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, radical_inverse);
  5266. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  5267. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  5268. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  5269. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //need this for proper sampling
  5270. glBindTexture(GL_TEXTURE_2D, 0);
  5271. }
  5272. {
  5273. glGenFramebuffers(1, &resources.mipmap_blur_fbo);
  5274. glGenTextures(1, &resources.mipmap_blur_color);
  5275. }
  5276. #ifdef GLES_OVER_GL
  5277. //this needs to be enabled manually in OpenGL 2.1
  5278. if (config.extensions.has("GL_ARB_seamless_cube_map")) {
  5279. glEnable(_EXT_TEXTURE_CUBE_MAP_SEAMLESS);
  5280. }
  5281. glEnable(GL_POINT_SPRITE);
  5282. glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
  5283. #endif
  5284. config.force_vertex_shading = GLOBAL_GET("rendering/quality/shading/force_vertex_shading");
  5285. config.use_fast_texture_filter = GLOBAL_GET("rendering/quality/filters/use_nearest_mipmap_filter");
  5286. GLOBAL_DEF_RST("rendering/quality/lightmapping/use_bicubic_sampling", true);
  5287. GLOBAL_DEF_RST("rendering/quality/lightmapping/use_bicubic_sampling.mobile", false);
  5288. config.use_lightmap_filter_bicubic = GLOBAL_GET("rendering/quality/lightmapping/use_bicubic_sampling");
  5289. config.use_physical_light_attenuation = GLOBAL_GET("rendering/quality/shading/use_physical_light_attenuation");
  5290. int orphan_mode = GLOBAL_GET("rendering/2d/opengl/legacy_orphan_buffers");
  5291. switch (orphan_mode) {
  5292. default: {
  5293. config.should_orphan = true;
  5294. } break;
  5295. case 1: {
  5296. config.should_orphan = false;
  5297. } break;
  5298. case 2: {
  5299. config.should_orphan = true;
  5300. } break;
  5301. }
  5302. }
  5303. void RasterizerStorageGLES2::finalize() {
  5304. }
  5305. void RasterizerStorageGLES2::_copy_screen() {
  5306. bind_quad_array();
  5307. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  5308. }
  5309. void RasterizerStorageGLES2::update_dirty_resources() {
  5310. update_dirty_shaders();
  5311. update_dirty_materials();
  5312. update_dirty_blend_shapes();
  5313. update_dirty_skeletons();
  5314. update_dirty_multimeshes();
  5315. update_dirty_captures();
  5316. }
  5317. RasterizerStorageGLES2::RasterizerStorageGLES2() {
  5318. RasterizerStorageGLES2::system_fbo = 0;
  5319. config.should_orphan = true;
  5320. }