rasterizer_storage_gles3.cpp 249 KB

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