rasterizer_storage_gles3.cpp 231 KB

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