rasterizer_storage_gles2.cpp 185 KB

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