2
0

rasterizer_storage_gles3.cpp 237 KB

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