rasterizer_storage_gles3.cpp 230 KB

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