renderer_storage_rd.cpp 281 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560
  1. /*************************************************************************/
  2. /* renderer_storage_rd.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 "renderer_storage_rd.h"
  31. #include "core/config/engine.h"
  32. #include "core/config/project_settings.h"
  33. #include "core/io/resource_loader.h"
  34. #include "renderer_compositor_rd.h"
  35. #include "servers/rendering/shader_language.h"
  36. Ref<Image> RendererStorageRD::_validate_texture_format(const Ref<Image> &p_image, TextureToRDFormat &r_format) {
  37. Ref<Image> image = p_image->duplicate();
  38. switch (p_image->get_format()) {
  39. case Image::FORMAT_L8: {
  40. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  41. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  42. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_R;
  43. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  44. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  45. } break; //luminance
  46. case Image::FORMAT_LA8: {
  47. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  48. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  49. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_R;
  50. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  51. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_G;
  52. } break; //luminance-alpha
  53. case Image::FORMAT_R8: {
  54. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  55. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  56. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  57. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  58. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  59. } break;
  60. case Image::FORMAT_RG8: {
  61. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  62. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  63. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  64. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  65. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  66. } break;
  67. case Image::FORMAT_RGB8: {
  68. //this format is not mandatory for specification, check if supported first
  69. if (false && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_R8G8B8_UNORM, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT) && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_R8G8B8_SRGB, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  70. r_format.format = RD::DATA_FORMAT_R8G8B8_UNORM;
  71. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8_SRGB;
  72. } else {
  73. //not supported, reconvert
  74. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  75. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  76. image->convert(Image::FORMAT_RGBA8);
  77. }
  78. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  79. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  80. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  81. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  82. } break;
  83. case Image::FORMAT_RGBA8: {
  84. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  85. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  86. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  87. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  88. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  89. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  90. } break;
  91. case Image::FORMAT_RGBA4444: {
  92. r_format.format = RD::DATA_FORMAT_B4G4R4A4_UNORM_PACK16;
  93. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_B; //needs swizzle
  94. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  95. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  96. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  97. } break;
  98. case Image::FORMAT_RGB565: {
  99. r_format.format = RD::DATA_FORMAT_B5G6R5_UNORM_PACK16;
  100. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_B;
  101. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  102. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  103. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  104. } break;
  105. case Image::FORMAT_RF: {
  106. r_format.format = RD::DATA_FORMAT_R32_SFLOAT;
  107. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  108. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  109. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  110. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  111. } break; //float
  112. case Image::FORMAT_RGF: {
  113. r_format.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  114. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  115. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  116. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  117. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  118. } break;
  119. case Image::FORMAT_RGBF: {
  120. //this format is not mandatory for specification, check if supported first
  121. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_R32G32B32_SFLOAT, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  122. r_format.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  123. } else {
  124. //not supported, reconvert
  125. r_format.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  126. image->convert(Image::FORMAT_RGBAF);
  127. }
  128. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  129. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  130. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  131. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  132. } break;
  133. case Image::FORMAT_RGBAF: {
  134. r_format.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  135. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  136. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  137. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  138. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  139. } break;
  140. case Image::FORMAT_RH: {
  141. r_format.format = RD::DATA_FORMAT_R16_SFLOAT;
  142. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  143. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  144. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  145. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  146. } break; //half float
  147. case Image::FORMAT_RGH: {
  148. r_format.format = RD::DATA_FORMAT_R16G16_SFLOAT;
  149. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  150. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  151. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  152. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  153. } break;
  154. case Image::FORMAT_RGBH: {
  155. //this format is not mandatory for specification, check if supported first
  156. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_R16G16B16_SFLOAT, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  157. r_format.format = RD::DATA_FORMAT_R16G16B16_SFLOAT;
  158. } else {
  159. //not supported, reconvert
  160. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  161. image->convert(Image::FORMAT_RGBAH);
  162. }
  163. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  164. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  165. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  166. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  167. } break;
  168. case Image::FORMAT_RGBAH: {
  169. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  170. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  171. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  172. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  173. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  174. } break;
  175. case Image::FORMAT_RGBE9995: {
  176. r_format.format = RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32;
  177. #ifndef _MSC_VER
  178. #warning TODO need to make a function in Image to swap bits for this
  179. #endif
  180. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_IDENTITY;
  181. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_IDENTITY;
  182. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_IDENTITY;
  183. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_IDENTITY;
  184. } break;
  185. case Image::FORMAT_DXT1: {
  186. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC1_RGB_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  187. r_format.format = RD::DATA_FORMAT_BC1_RGB_UNORM_BLOCK;
  188. r_format.format_srgb = RD::DATA_FORMAT_BC1_RGB_SRGB_BLOCK;
  189. } else {
  190. //not supported, reconvert
  191. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  192. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  193. image->decompress();
  194. image->convert(Image::FORMAT_RGBA8);
  195. }
  196. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  197. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  198. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  199. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  200. } break; //s3tc bc1
  201. case Image::FORMAT_DXT3: {
  202. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC2_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  203. r_format.format = RD::DATA_FORMAT_BC2_UNORM_BLOCK;
  204. r_format.format_srgb = RD::DATA_FORMAT_BC2_SRGB_BLOCK;
  205. } else {
  206. //not supported, reconvert
  207. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  208. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  209. image->decompress();
  210. image->convert(Image::FORMAT_RGBA8);
  211. }
  212. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  213. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  214. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  215. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  216. } break; //bc2
  217. case Image::FORMAT_DXT5: {
  218. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC3_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  219. r_format.format = RD::DATA_FORMAT_BC3_UNORM_BLOCK;
  220. r_format.format_srgb = RD::DATA_FORMAT_BC3_SRGB_BLOCK;
  221. } else {
  222. //not supported, reconvert
  223. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  224. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  225. image->decompress();
  226. image->convert(Image::FORMAT_RGBA8);
  227. }
  228. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  229. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  230. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  231. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  232. } break; //bc3
  233. case Image::FORMAT_RGTC_R: {
  234. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC4_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  235. r_format.format = RD::DATA_FORMAT_BC4_UNORM_BLOCK;
  236. } else {
  237. //not supported, reconvert
  238. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  239. image->decompress();
  240. image->convert(Image::FORMAT_R8);
  241. }
  242. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  243. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  244. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  245. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  246. } break;
  247. case Image::FORMAT_RGTC_RG: {
  248. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC5_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  249. r_format.format = RD::DATA_FORMAT_BC5_UNORM_BLOCK;
  250. } else {
  251. //not supported, reconvert
  252. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  253. image->decompress();
  254. image->convert(Image::FORMAT_RG8);
  255. }
  256. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  257. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  258. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  259. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  260. } break;
  261. case Image::FORMAT_BPTC_RGBA: {
  262. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC7_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  263. r_format.format = RD::DATA_FORMAT_BC7_UNORM_BLOCK;
  264. r_format.format_srgb = RD::DATA_FORMAT_BC7_SRGB_BLOCK;
  265. } else {
  266. //not supported, reconvert
  267. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  268. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  269. image->decompress();
  270. image->convert(Image::FORMAT_RGBA8);
  271. }
  272. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  273. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  274. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  275. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  276. } break; //btpc bc7
  277. case Image::FORMAT_BPTC_RGBF: {
  278. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC6H_SFLOAT_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  279. r_format.format = RD::DATA_FORMAT_BC6H_SFLOAT_BLOCK;
  280. } else {
  281. //not supported, reconvert
  282. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  283. image->decompress();
  284. image->convert(Image::FORMAT_RGBAH);
  285. }
  286. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  287. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  288. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  289. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  290. } break; //float bc6h
  291. case Image::FORMAT_BPTC_RGBFU: {
  292. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC6H_UFLOAT_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  293. r_format.format = RD::DATA_FORMAT_BC6H_UFLOAT_BLOCK;
  294. } else {
  295. //not supported, reconvert
  296. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  297. image->decompress();
  298. image->convert(Image::FORMAT_RGBAH);
  299. }
  300. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  301. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  302. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  303. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  304. } break; //unsigned float bc6hu
  305. case Image::FORMAT_PVRTC2: {
  306. //this is not properly supported by MoltekVK it seems, so best to use ETC2
  307. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  308. r_format.format = RD::DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG;
  309. r_format.format_srgb = RD::DATA_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG;
  310. } else {
  311. //not supported, reconvert
  312. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  313. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  314. image->decompress();
  315. image->convert(Image::FORMAT_RGBA8);
  316. }
  317. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  318. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  319. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  320. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  321. } break; //pvrtc
  322. case Image::FORMAT_PVRTC2A: {
  323. //this is not properly supported by MoltekVK it seems, so best to use ETC2
  324. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  325. r_format.format = RD::DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG;
  326. r_format.format_srgb = RD::DATA_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG;
  327. } else {
  328. //not supported, reconvert
  329. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  330. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  331. image->decompress();
  332. image->convert(Image::FORMAT_RGBA8);
  333. }
  334. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  335. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  336. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  337. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  338. } break;
  339. case Image::FORMAT_PVRTC4: {
  340. //this is not properly supported by MoltekVK it seems, so best to use ETC2
  341. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  342. r_format.format = RD::DATA_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG;
  343. r_format.format_srgb = RD::DATA_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG;
  344. } else {
  345. //not supported, reconvert
  346. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  347. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  348. image->decompress();
  349. image->convert(Image::FORMAT_RGBA8);
  350. }
  351. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  352. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  353. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  354. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  355. } break;
  356. case Image::FORMAT_PVRTC4A: {
  357. //this is not properly supported by MoltekVK it seems, so best to use ETC2
  358. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  359. r_format.format = RD::DATA_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG;
  360. r_format.format_srgb = RD::DATA_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG;
  361. } else {
  362. //not supported, reconvert
  363. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  364. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  365. image->decompress();
  366. image->convert(Image::FORMAT_RGBA8);
  367. }
  368. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  369. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  370. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  371. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  372. } break;
  373. case Image::FORMAT_ETC2_R11: {
  374. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_EAC_R11_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  375. r_format.format = RD::DATA_FORMAT_EAC_R11_UNORM_BLOCK;
  376. } else {
  377. //not supported, reconvert
  378. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  379. image->decompress();
  380. image->convert(Image::FORMAT_R8);
  381. }
  382. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  383. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  384. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  385. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  386. } break; //etc2
  387. case Image::FORMAT_ETC2_R11S: {
  388. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_EAC_R11_SNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  389. r_format.format = RD::DATA_FORMAT_EAC_R11_SNORM_BLOCK;
  390. } else {
  391. //not supported, reconvert
  392. r_format.format = RD::DATA_FORMAT_R8_SNORM;
  393. image->decompress();
  394. image->convert(Image::FORMAT_R8);
  395. }
  396. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  397. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  398. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  399. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  400. } break; //signed: {} break; NOT srgb.
  401. case Image::FORMAT_ETC2_RG11: {
  402. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_EAC_R11G11_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  403. r_format.format = RD::DATA_FORMAT_EAC_R11G11_UNORM_BLOCK;
  404. } else {
  405. //not supported, reconvert
  406. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  407. image->decompress();
  408. image->convert(Image::FORMAT_RG8);
  409. }
  410. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  411. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  412. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  413. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  414. } break;
  415. case Image::FORMAT_ETC2_RG11S: {
  416. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_EAC_R11G11_SNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  417. r_format.format = RD::DATA_FORMAT_EAC_R11G11_SNORM_BLOCK;
  418. } else {
  419. //not supported, reconvert
  420. r_format.format = RD::DATA_FORMAT_R8G8_SNORM;
  421. image->decompress();
  422. image->convert(Image::FORMAT_RG8);
  423. }
  424. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  425. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  426. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  427. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  428. } break;
  429. case Image::FORMAT_ETC:
  430. case Image::FORMAT_ETC2_RGB8: {
  431. //ETC2 is backwards compatible with ETC1, and all modern platforms support it
  432. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  433. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
  434. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8_SRGB_BLOCK;
  435. } else {
  436. //not supported, reconvert
  437. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  438. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  439. image->decompress();
  440. image->convert(Image::FORMAT_RGBA8);
  441. }
  442. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  443. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  444. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  445. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  446. } break;
  447. case Image::FORMAT_ETC2_RGBA8: {
  448. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  449. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
  450. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK;
  451. } else {
  452. //not supported, reconvert
  453. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  454. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  455. image->decompress();
  456. image->convert(Image::FORMAT_RGBA8);
  457. }
  458. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  459. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  460. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  461. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  462. } break;
  463. case Image::FORMAT_ETC2_RGB8A1: {
  464. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  465. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK;
  466. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK;
  467. } else {
  468. //not supported, reconvert
  469. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  470. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  471. image->decompress();
  472. image->convert(Image::FORMAT_RGBA8);
  473. }
  474. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  475. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  476. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  477. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  478. } break;
  479. case Image::FORMAT_ETC2_RA_AS_RG: {
  480. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  481. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
  482. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK;
  483. } else {
  484. //not supported, reconvert
  485. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  486. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  487. image->decompress();
  488. image->convert(Image::FORMAT_RGBA8);
  489. }
  490. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  491. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_A;
  492. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  493. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  494. } break;
  495. case Image::FORMAT_DXT5_RA_AS_RG: {
  496. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC3_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  497. r_format.format = RD::DATA_FORMAT_BC3_UNORM_BLOCK;
  498. r_format.format_srgb = RD::DATA_FORMAT_BC3_SRGB_BLOCK;
  499. } else {
  500. //not supported, reconvert
  501. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  502. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  503. image->decompress();
  504. image->convert(Image::FORMAT_RGBA8);
  505. }
  506. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  507. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_A;
  508. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  509. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  510. } break;
  511. default: {
  512. }
  513. }
  514. return image;
  515. }
  516. RID RendererStorageRD::texture_2d_create(const Ref<Image> &p_image) {
  517. ERR_FAIL_COND_V(p_image.is_null(), RID());
  518. ERR_FAIL_COND_V(p_image->empty(), RID());
  519. TextureToRDFormat ret_format;
  520. Ref<Image> image = _validate_texture_format(p_image, ret_format);
  521. Texture texture;
  522. texture.type = Texture::TYPE_2D;
  523. texture.width = p_image->get_width();
  524. texture.height = p_image->get_height();
  525. texture.layers = 1;
  526. texture.mipmaps = p_image->get_mipmap_count() + 1;
  527. texture.depth = 1;
  528. texture.format = p_image->get_format();
  529. texture.validated_format = image->get_format();
  530. texture.rd_type = RD::TEXTURE_TYPE_2D;
  531. texture.rd_format = ret_format.format;
  532. texture.rd_format_srgb = ret_format.format_srgb;
  533. RD::TextureFormat rd_format;
  534. RD::TextureView rd_view;
  535. { //attempt register
  536. rd_format.format = texture.rd_format;
  537. rd_format.width = texture.width;
  538. rd_format.height = texture.height;
  539. rd_format.depth = 1;
  540. rd_format.array_layers = 1;
  541. rd_format.mipmaps = texture.mipmaps;
  542. rd_format.type = texture.rd_type;
  543. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  544. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  545. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  546. rd_format.shareable_formats.push_back(texture.rd_format);
  547. rd_format.shareable_formats.push_back(texture.rd_format_srgb);
  548. }
  549. }
  550. {
  551. rd_view.swizzle_r = ret_format.swizzle_r;
  552. rd_view.swizzle_g = ret_format.swizzle_g;
  553. rd_view.swizzle_b = ret_format.swizzle_b;
  554. rd_view.swizzle_a = ret_format.swizzle_a;
  555. }
  556. Vector<uint8_t> data = image->get_data(); //use image data
  557. Vector<Vector<uint8_t>> data_slices;
  558. data_slices.push_back(data);
  559. texture.rd_texture = RD::get_singleton()->texture_create(rd_format, rd_view, data_slices);
  560. ERR_FAIL_COND_V(texture.rd_texture.is_null(), RID());
  561. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  562. rd_view.format_override = texture.rd_format_srgb;
  563. texture.rd_texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, texture.rd_texture);
  564. if (texture.rd_texture_srgb.is_null()) {
  565. RD::get_singleton()->free(texture.rd_texture);
  566. ERR_FAIL_COND_V(texture.rd_texture_srgb.is_null(), RID());
  567. }
  568. }
  569. //used for 2D, overridable
  570. texture.width_2d = texture.width;
  571. texture.height_2d = texture.height;
  572. texture.is_render_target = false;
  573. texture.rd_view = rd_view;
  574. texture.is_proxy = false;
  575. return texture_owner.make_rid(texture);
  576. }
  577. RID RendererStorageRD::texture_2d_layered_create(const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type) {
  578. ERR_FAIL_COND_V(p_layers.size() == 0, RID());
  579. ERR_FAIL_COND_V(p_layered_type == RS::TEXTURE_LAYERED_CUBEMAP && p_layers.size() != 6, RID());
  580. ERR_FAIL_COND_V(p_layered_type == RS::TEXTURE_LAYERED_CUBEMAP_ARRAY && (p_layers.size() < 6 || (p_layers.size() % 6) != 0), RID());
  581. TextureToRDFormat ret_format;
  582. Vector<Ref<Image>> images;
  583. {
  584. int valid_width = 0;
  585. int valid_height = 0;
  586. bool valid_mipmaps = false;
  587. Image::Format valid_format = Image::FORMAT_MAX;
  588. for (int i = 0; i < p_layers.size(); i++) {
  589. ERR_FAIL_COND_V(p_layers[i]->empty(), RID());
  590. if (i == 0) {
  591. valid_width = p_layers[i]->get_width();
  592. valid_height = p_layers[i]->get_height();
  593. valid_format = p_layers[i]->get_format();
  594. valid_mipmaps = p_layers[i]->has_mipmaps();
  595. } else {
  596. ERR_FAIL_COND_V(p_layers[i]->get_width() != valid_width, RID());
  597. ERR_FAIL_COND_V(p_layers[i]->get_height() != valid_height, RID());
  598. ERR_FAIL_COND_V(p_layers[i]->get_format() != valid_format, RID());
  599. ERR_FAIL_COND_V(p_layers[i]->has_mipmaps() != valid_mipmaps, RID());
  600. }
  601. images.push_back(_validate_texture_format(p_layers[i], ret_format));
  602. }
  603. }
  604. Texture texture;
  605. texture.type = Texture::TYPE_LAYERED;
  606. texture.layered_type = p_layered_type;
  607. texture.width = p_layers[0]->get_width();
  608. texture.height = p_layers[0]->get_height();
  609. texture.layers = p_layers.size();
  610. texture.mipmaps = p_layers[0]->get_mipmap_count() + 1;
  611. texture.depth = 1;
  612. texture.format = p_layers[0]->get_format();
  613. texture.validated_format = images[0]->get_format();
  614. switch (p_layered_type) {
  615. case RS::TEXTURE_LAYERED_2D_ARRAY: {
  616. texture.rd_type = RD::TEXTURE_TYPE_2D_ARRAY;
  617. } break;
  618. case RS::TEXTURE_LAYERED_CUBEMAP: {
  619. texture.rd_type = RD::TEXTURE_TYPE_CUBE;
  620. } break;
  621. case RS::TEXTURE_LAYERED_CUBEMAP_ARRAY: {
  622. texture.rd_type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  623. } break;
  624. }
  625. texture.rd_format = ret_format.format;
  626. texture.rd_format_srgb = ret_format.format_srgb;
  627. RD::TextureFormat rd_format;
  628. RD::TextureView rd_view;
  629. { //attempt register
  630. rd_format.format = texture.rd_format;
  631. rd_format.width = texture.width;
  632. rd_format.height = texture.height;
  633. rd_format.depth = 1;
  634. rd_format.array_layers = texture.layers;
  635. rd_format.mipmaps = texture.mipmaps;
  636. rd_format.type = texture.rd_type;
  637. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  638. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  639. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  640. rd_format.shareable_formats.push_back(texture.rd_format);
  641. rd_format.shareable_formats.push_back(texture.rd_format_srgb);
  642. }
  643. }
  644. {
  645. rd_view.swizzle_r = ret_format.swizzle_r;
  646. rd_view.swizzle_g = ret_format.swizzle_g;
  647. rd_view.swizzle_b = ret_format.swizzle_b;
  648. rd_view.swizzle_a = ret_format.swizzle_a;
  649. }
  650. Vector<Vector<uint8_t>> data_slices;
  651. for (int i = 0; i < images.size(); i++) {
  652. Vector<uint8_t> data = images[i]->get_data(); //use image data
  653. data_slices.push_back(data);
  654. }
  655. texture.rd_texture = RD::get_singleton()->texture_create(rd_format, rd_view, data_slices);
  656. ERR_FAIL_COND_V(texture.rd_texture.is_null(), RID());
  657. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  658. rd_view.format_override = texture.rd_format_srgb;
  659. texture.rd_texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, texture.rd_texture);
  660. if (texture.rd_texture_srgb.is_null()) {
  661. RD::get_singleton()->free(texture.rd_texture);
  662. ERR_FAIL_COND_V(texture.rd_texture_srgb.is_null(), RID());
  663. }
  664. }
  665. //used for 2D, overridable
  666. texture.width_2d = texture.width;
  667. texture.height_2d = texture.height;
  668. texture.is_render_target = false;
  669. texture.rd_view = rd_view;
  670. texture.is_proxy = false;
  671. return texture_owner.make_rid(texture);
  672. }
  673. RID RendererStorageRD::texture_3d_create(Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_data) {
  674. ERR_FAIL_COND_V(p_data.size() == 0, RID());
  675. Image::Image3DValidateError verr = Image::validate_3d_image(p_format, p_width, p_height, p_depth, p_mipmaps, p_data);
  676. if (verr != Image::VALIDATE_3D_OK) {
  677. ERR_FAIL_V_MSG(RID(), Image::get_3d_image_validation_error_text(verr));
  678. }
  679. TextureToRDFormat ret_format;
  680. Image::Format validated_format = Image::FORMAT_MAX;
  681. Vector<uint8_t> all_data;
  682. uint32_t mipmap_count = 0;
  683. Vector<Texture::BufferSlice3D> slices;
  684. {
  685. Vector<Ref<Image>> images;
  686. uint32_t all_data_size = 0;
  687. images.resize(p_data.size());
  688. for (int i = 0; i < p_data.size(); i++) {
  689. TextureToRDFormat f;
  690. images.write[i] = _validate_texture_format(p_data[i], f);
  691. if (i == 0) {
  692. ret_format = f;
  693. validated_format = images[0]->get_format();
  694. }
  695. all_data_size += images[i]->get_data().size();
  696. }
  697. all_data.resize(all_data_size); //consolidate all data here
  698. uint32_t offset = 0;
  699. Size2i prev_size;
  700. for (int i = 0; i < p_data.size(); i++) {
  701. uint32_t s = images[i]->get_data().size();
  702. copymem(&all_data.write[offset], images[i]->get_data().ptr(), s);
  703. {
  704. Texture::BufferSlice3D slice;
  705. slice.size.width = images[i]->get_width();
  706. slice.size.height = images[i]->get_height();
  707. slice.offset = offset;
  708. slice.buffer_size = s;
  709. slices.push_back(slice);
  710. }
  711. offset += s;
  712. Size2i img_size(images[i]->get_width(), images[i]->get_height());
  713. if (img_size != prev_size) {
  714. mipmap_count++;
  715. }
  716. prev_size = img_size;
  717. }
  718. }
  719. Texture texture;
  720. texture.type = Texture::TYPE_3D;
  721. texture.width = p_width;
  722. texture.height = p_height;
  723. texture.depth = p_depth;
  724. texture.mipmaps = mipmap_count;
  725. texture.format = p_data[0]->get_format();
  726. texture.validated_format = validated_format;
  727. texture.buffer_size_3d = all_data.size();
  728. texture.buffer_slices_3d = slices;
  729. texture.rd_type = RD::TEXTURE_TYPE_3D;
  730. texture.rd_format = ret_format.format;
  731. texture.rd_format_srgb = ret_format.format_srgb;
  732. RD::TextureFormat rd_format;
  733. RD::TextureView rd_view;
  734. { //attempt register
  735. rd_format.format = texture.rd_format;
  736. rd_format.width = texture.width;
  737. rd_format.height = texture.height;
  738. rd_format.depth = texture.depth;
  739. rd_format.array_layers = 1;
  740. rd_format.mipmaps = texture.mipmaps;
  741. rd_format.type = texture.rd_type;
  742. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  743. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  744. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  745. rd_format.shareable_formats.push_back(texture.rd_format);
  746. rd_format.shareable_formats.push_back(texture.rd_format_srgb);
  747. }
  748. }
  749. {
  750. rd_view.swizzle_r = ret_format.swizzle_r;
  751. rd_view.swizzle_g = ret_format.swizzle_g;
  752. rd_view.swizzle_b = ret_format.swizzle_b;
  753. rd_view.swizzle_a = ret_format.swizzle_a;
  754. }
  755. Vector<Vector<uint8_t>> data_slices;
  756. data_slices.push_back(all_data); //one slice
  757. texture.rd_texture = RD::get_singleton()->texture_create(rd_format, rd_view, data_slices);
  758. ERR_FAIL_COND_V(texture.rd_texture.is_null(), RID());
  759. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  760. rd_view.format_override = texture.rd_format_srgb;
  761. texture.rd_texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, texture.rd_texture);
  762. if (texture.rd_texture_srgb.is_null()) {
  763. RD::get_singleton()->free(texture.rd_texture);
  764. ERR_FAIL_COND_V(texture.rd_texture_srgb.is_null(), RID());
  765. }
  766. }
  767. //used for 2D, overridable
  768. texture.width_2d = texture.width;
  769. texture.height_2d = texture.height;
  770. texture.is_render_target = false;
  771. texture.rd_view = rd_view;
  772. texture.is_proxy = false;
  773. return texture_owner.make_rid(texture);
  774. }
  775. RID RendererStorageRD::texture_proxy_create(RID p_base) {
  776. Texture *tex = texture_owner.getornull(p_base);
  777. ERR_FAIL_COND_V(!tex, RID());
  778. Texture proxy_tex = *tex;
  779. proxy_tex.rd_view.format_override = tex->rd_format;
  780. proxy_tex.rd_texture = RD::get_singleton()->texture_create_shared(proxy_tex.rd_view, tex->rd_texture);
  781. if (proxy_tex.rd_texture_srgb.is_valid()) {
  782. proxy_tex.rd_view.format_override = tex->rd_format_srgb;
  783. proxy_tex.rd_texture_srgb = RD::get_singleton()->texture_create_shared(proxy_tex.rd_view, tex->rd_texture);
  784. }
  785. proxy_tex.proxy_to = p_base;
  786. proxy_tex.is_render_target = false;
  787. proxy_tex.is_proxy = true;
  788. proxy_tex.proxies.clear();
  789. RID rid = texture_owner.make_rid(proxy_tex);
  790. tex->proxies.push_back(rid);
  791. return rid;
  792. }
  793. void RendererStorageRD::_texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer, bool p_immediate) {
  794. ERR_FAIL_COND(p_image.is_null() || p_image->empty());
  795. Texture *tex = texture_owner.getornull(p_texture);
  796. ERR_FAIL_COND(!tex);
  797. ERR_FAIL_COND(tex->is_render_target);
  798. ERR_FAIL_COND(p_image->get_width() != tex->width || p_image->get_height() != tex->height);
  799. ERR_FAIL_COND(p_image->get_format() != tex->format);
  800. if (tex->type == Texture::TYPE_LAYERED) {
  801. ERR_FAIL_INDEX(p_layer, tex->layers);
  802. }
  803. #ifdef TOOLS_ENABLED
  804. tex->image_cache_2d.unref();
  805. #endif
  806. TextureToRDFormat f;
  807. Ref<Image> validated = _validate_texture_format(p_image, f);
  808. RD::get_singleton()->texture_update(tex->rd_texture, p_layer, validated->get_data(), !p_immediate);
  809. }
  810. void RendererStorageRD::texture_2d_update_immediate(RID p_texture, const Ref<Image> &p_image, int p_layer) {
  811. _texture_2d_update(p_texture, p_image, p_layer, true);
  812. }
  813. void RendererStorageRD::texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer) {
  814. _texture_2d_update(p_texture, p_image, p_layer, false);
  815. }
  816. void RendererStorageRD::texture_3d_update(RID p_texture, const Vector<Ref<Image>> &p_data) {
  817. Texture *tex = texture_owner.getornull(p_texture);
  818. ERR_FAIL_COND(!tex);
  819. ERR_FAIL_COND(tex->type != Texture::TYPE_3D);
  820. Image::Image3DValidateError verr = Image::validate_3d_image(tex->format, tex->width, tex->height, tex->depth, tex->mipmaps > 1, p_data);
  821. if (verr != Image::VALIDATE_3D_OK) {
  822. ERR_FAIL_MSG(Image::get_3d_image_validation_error_text(verr));
  823. }
  824. Vector<uint8_t> all_data;
  825. {
  826. Vector<Ref<Image>> images;
  827. uint32_t all_data_size = 0;
  828. images.resize(p_data.size());
  829. for (int i = 0; i < p_data.size(); i++) {
  830. Ref<Image> image = p_data[i];
  831. if (image->get_format() != tex->validated_format) {
  832. image = image->duplicate();
  833. image->convert(tex->validated_format);
  834. }
  835. all_data_size += images[i]->get_data().size();
  836. images.push_back(image);
  837. }
  838. all_data.resize(all_data_size); //consolidate all data here
  839. uint32_t offset = 0;
  840. for (int i = 0; i < p_data.size(); i++) {
  841. uint32_t s = images[i]->get_data().size();
  842. copymem(&all_data.write[offset], images[i]->get_data().ptr(), s);
  843. offset += s;
  844. }
  845. }
  846. RD::get_singleton()->texture_update(tex->rd_texture, 0, all_data, true);
  847. }
  848. void RendererStorageRD::texture_proxy_update(RID p_texture, RID p_proxy_to) {
  849. Texture *tex = texture_owner.getornull(p_texture);
  850. ERR_FAIL_COND(!tex);
  851. ERR_FAIL_COND(!tex->is_proxy);
  852. Texture *proxy_to = texture_owner.getornull(p_proxy_to);
  853. ERR_FAIL_COND(!proxy_to);
  854. ERR_FAIL_COND(proxy_to->is_proxy);
  855. if (tex->proxy_to.is_valid()) {
  856. //unlink proxy
  857. if (RD::get_singleton()->texture_is_valid(tex->rd_texture)) {
  858. RD::get_singleton()->free(tex->rd_texture);
  859. tex->rd_texture = RID();
  860. }
  861. if (RD::get_singleton()->texture_is_valid(tex->rd_texture_srgb)) {
  862. RD::get_singleton()->free(tex->rd_texture_srgb);
  863. tex->rd_texture_srgb = RID();
  864. }
  865. Texture *prev_tex = texture_owner.getornull(tex->proxy_to);
  866. ERR_FAIL_COND(!prev_tex);
  867. prev_tex->proxies.erase(p_texture);
  868. }
  869. *tex = *proxy_to;
  870. tex->proxy_to = p_proxy_to;
  871. tex->is_render_target = false;
  872. tex->is_proxy = true;
  873. tex->proxies.clear();
  874. proxy_to->proxies.push_back(p_texture);
  875. tex->rd_view.format_override = tex->rd_format;
  876. tex->rd_texture = RD::get_singleton()->texture_create_shared(tex->rd_view, proxy_to->rd_texture);
  877. if (tex->rd_texture_srgb.is_valid()) {
  878. tex->rd_view.format_override = tex->rd_format_srgb;
  879. tex->rd_texture_srgb = RD::get_singleton()->texture_create_shared(tex->rd_view, proxy_to->rd_texture);
  880. }
  881. }
  882. //these two APIs can be used together or in combination with the others.
  883. RID RendererStorageRD::texture_2d_placeholder_create() {
  884. //this could be better optimized to reuse an existing image , done this way
  885. //for now to get it working
  886. Ref<Image> image;
  887. image.instance();
  888. image->create(4, 4, false, Image::FORMAT_RGBA8);
  889. for (int i = 0; i < 4; i++) {
  890. for (int j = 0; j < 4; j++) {
  891. image->set_pixel(i, j, Color(1, 0, 1, 1));
  892. }
  893. }
  894. return texture_2d_create(image);
  895. }
  896. RID RendererStorageRD::texture_2d_layered_placeholder_create(RS::TextureLayeredType p_layered_type) {
  897. //this could be better optimized to reuse an existing image , done this way
  898. //for now to get it working
  899. Ref<Image> image;
  900. image.instance();
  901. image->create(4, 4, false, Image::FORMAT_RGBA8);
  902. for (int i = 0; i < 4; i++) {
  903. for (int j = 0; j < 4; j++) {
  904. image->set_pixel(i, j, Color(1, 0, 1, 1));
  905. }
  906. }
  907. Vector<Ref<Image>> images;
  908. if (p_layered_type == RS::TEXTURE_LAYERED_2D_ARRAY) {
  909. images.push_back(image);
  910. } else {
  911. //cube
  912. for (int i = 0; i < 6; i++) {
  913. images.push_back(image);
  914. }
  915. }
  916. return texture_2d_layered_create(images, p_layered_type);
  917. }
  918. RID RendererStorageRD::texture_3d_placeholder_create() {
  919. //this could be better optimized to reuse an existing image , done this way
  920. //for now to get it working
  921. Ref<Image> image;
  922. image.instance();
  923. image->create(4, 4, false, Image::FORMAT_RGBA8);
  924. for (int i = 0; i < 4; i++) {
  925. for (int j = 0; j < 4; j++) {
  926. image->set_pixel(i, j, Color(1, 0, 1, 1));
  927. }
  928. }
  929. Vector<Ref<Image>> images;
  930. //cube
  931. for (int i = 0; i < 4; i++) {
  932. images.push_back(image);
  933. }
  934. return texture_3d_create(Image::FORMAT_RGBA8, 4, 4, 4, false, images);
  935. }
  936. Ref<Image> RendererStorageRD::texture_2d_get(RID p_texture) const {
  937. Texture *tex = texture_owner.getornull(p_texture);
  938. ERR_FAIL_COND_V(!tex, Ref<Image>());
  939. #ifdef TOOLS_ENABLED
  940. if (tex->image_cache_2d.is_valid()) {
  941. return tex->image_cache_2d;
  942. }
  943. #endif
  944. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(tex->rd_texture, 0);
  945. ERR_FAIL_COND_V(data.size() == 0, Ref<Image>());
  946. Ref<Image> image;
  947. image.instance();
  948. image->create(tex->width, tex->height, tex->mipmaps > 1, tex->validated_format, data);
  949. ERR_FAIL_COND_V(image->empty(), Ref<Image>());
  950. if (tex->format != tex->validated_format) {
  951. image->convert(tex->format);
  952. }
  953. #ifdef TOOLS_ENABLED
  954. if (Engine::get_singleton()->is_editor_hint()) {
  955. tex->image_cache_2d = image;
  956. }
  957. #endif
  958. return image;
  959. }
  960. Ref<Image> RendererStorageRD::texture_2d_layer_get(RID p_texture, int p_layer) const {
  961. Texture *tex = texture_owner.getornull(p_texture);
  962. ERR_FAIL_COND_V(!tex, Ref<Image>());
  963. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(tex->rd_texture, p_layer);
  964. ERR_FAIL_COND_V(data.size() == 0, Ref<Image>());
  965. Ref<Image> image;
  966. image.instance();
  967. image->create(tex->width, tex->height, tex->mipmaps > 1, tex->validated_format, data);
  968. ERR_FAIL_COND_V(image->empty(), Ref<Image>());
  969. if (tex->format != tex->validated_format) {
  970. image->convert(tex->format);
  971. }
  972. return image;
  973. }
  974. Vector<Ref<Image>> RendererStorageRD::texture_3d_get(RID p_texture) const {
  975. Texture *tex = texture_owner.getornull(p_texture);
  976. ERR_FAIL_COND_V(!tex, Vector<Ref<Image>>());
  977. ERR_FAIL_COND_V(tex->type != Texture::TYPE_3D, Vector<Ref<Image>>());
  978. Vector<uint8_t> all_data = RD::get_singleton()->texture_get_data(tex->rd_texture, 0);
  979. ERR_FAIL_COND_V(all_data.size() != (int)tex->buffer_size_3d, Vector<Ref<Image>>());
  980. Vector<Ref<Image>> ret;
  981. for (int i = 0; i < tex->buffer_slices_3d.size(); i++) {
  982. const Texture::BufferSlice3D &bs = tex->buffer_slices_3d[i];
  983. ERR_FAIL_COND_V(bs.offset >= (uint32_t)all_data.size(), Vector<Ref<Image>>());
  984. ERR_FAIL_COND_V(bs.offset + bs.buffer_size > (uint32_t)all_data.size(), Vector<Ref<Image>>());
  985. Vector<uint8_t> sub_region = all_data.subarray(bs.offset, bs.offset + bs.buffer_size - 1);
  986. Ref<Image> img;
  987. img.instance();
  988. img->create(bs.size.width, bs.size.height, false, tex->validated_format, sub_region);
  989. ERR_FAIL_COND_V(img->empty(), Vector<Ref<Image>>());
  990. if (tex->format != tex->validated_format) {
  991. img->convert(tex->format);
  992. }
  993. ret.push_back(img);
  994. }
  995. return ret;
  996. }
  997. void RendererStorageRD::texture_replace(RID p_texture, RID p_by_texture) {
  998. Texture *tex = texture_owner.getornull(p_texture);
  999. ERR_FAIL_COND(!tex);
  1000. ERR_FAIL_COND(tex->proxy_to.is_valid()); //can't replace proxy
  1001. Texture *by_tex = texture_owner.getornull(p_by_texture);
  1002. ERR_FAIL_COND(!by_tex);
  1003. ERR_FAIL_COND(by_tex->proxy_to.is_valid()); //can't replace proxy
  1004. if (tex == by_tex) {
  1005. return;
  1006. }
  1007. if (tex->rd_texture_srgb.is_valid()) {
  1008. RD::get_singleton()->free(tex->rd_texture_srgb);
  1009. }
  1010. RD::get_singleton()->free(tex->rd_texture);
  1011. if (tex->canvas_texture) {
  1012. memdelete(tex->canvas_texture);
  1013. tex->canvas_texture = nullptr;
  1014. }
  1015. Vector<RID> proxies_to_update = tex->proxies;
  1016. Vector<RID> proxies_to_redirect = by_tex->proxies;
  1017. *tex = *by_tex;
  1018. tex->proxies = proxies_to_update; //restore proxies, so they can be updated
  1019. if (tex->canvas_texture) {
  1020. tex->canvas_texture->diffuse = p_texture; //update
  1021. }
  1022. for (int i = 0; i < proxies_to_update.size(); i++) {
  1023. texture_proxy_update(proxies_to_update[i], p_texture);
  1024. }
  1025. for (int i = 0; i < proxies_to_redirect.size(); i++) {
  1026. texture_proxy_update(proxies_to_redirect[i], p_texture);
  1027. }
  1028. //delete last, so proxies can be updated
  1029. texture_owner.free(p_by_texture);
  1030. if (decal_atlas.textures.has(p_texture)) {
  1031. //belongs to decal atlas..
  1032. decal_atlas.dirty = true; //mark it dirty since it was most likely modified
  1033. }
  1034. }
  1035. void RendererStorageRD::texture_set_size_override(RID p_texture, int p_width, int p_height) {
  1036. Texture *tex = texture_owner.getornull(p_texture);
  1037. ERR_FAIL_COND(!tex);
  1038. ERR_FAIL_COND(tex->type != Texture::TYPE_2D);
  1039. tex->width_2d = p_width;
  1040. tex->height_2d = p_height;
  1041. }
  1042. void RendererStorageRD::texture_set_path(RID p_texture, const String &p_path) {
  1043. Texture *tex = texture_owner.getornull(p_texture);
  1044. ERR_FAIL_COND(!tex);
  1045. tex->path = p_path;
  1046. }
  1047. String RendererStorageRD::texture_get_path(RID p_texture) const {
  1048. return String();
  1049. }
  1050. void RendererStorageRD::texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) {
  1051. Texture *tex = texture_owner.getornull(p_texture);
  1052. ERR_FAIL_COND(!tex);
  1053. tex->detect_3d_callback_ud = p_userdata;
  1054. tex->detect_3d_callback = p_callback;
  1055. }
  1056. void RendererStorageRD::texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) {
  1057. Texture *tex = texture_owner.getornull(p_texture);
  1058. ERR_FAIL_COND(!tex);
  1059. tex->detect_normal_callback_ud = p_userdata;
  1060. tex->detect_normal_callback = p_callback;
  1061. }
  1062. void RendererStorageRD::texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata) {
  1063. Texture *tex = texture_owner.getornull(p_texture);
  1064. ERR_FAIL_COND(!tex);
  1065. tex->detect_roughness_callback_ud = p_userdata;
  1066. tex->detect_roughness_callback = p_callback;
  1067. }
  1068. void RendererStorageRD::texture_debug_usage(List<RS::TextureInfo> *r_info) {
  1069. }
  1070. void RendererStorageRD::texture_set_proxy(RID p_proxy, RID p_base) {
  1071. }
  1072. void RendererStorageRD::texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) {
  1073. }
  1074. Size2 RendererStorageRD::texture_size_with_proxy(RID p_proxy) {
  1075. return texture_2d_get_size(p_proxy);
  1076. }
  1077. /* CANVAS TEXTURE */
  1078. void RendererStorageRD::CanvasTexture::clear_sets() {
  1079. if (cleared_cache) {
  1080. return;
  1081. }
  1082. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  1083. for (int j = 1; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  1084. if (RD::get_singleton()->uniform_set_is_valid(uniform_sets[i][j])) {
  1085. RD::get_singleton()->free(uniform_sets[i][j]);
  1086. uniform_sets[i][j] = RID();
  1087. }
  1088. }
  1089. }
  1090. cleared_cache = true;
  1091. }
  1092. RendererStorageRD::CanvasTexture::~CanvasTexture() {
  1093. clear_sets();
  1094. }
  1095. RID RendererStorageRD::canvas_texture_create() {
  1096. return canvas_texture_owner.make_rid(memnew(CanvasTexture));
  1097. }
  1098. void RendererStorageRD::canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture) {
  1099. CanvasTexture *ct = canvas_texture_owner.getornull(p_canvas_texture);
  1100. switch (p_channel) {
  1101. case RS::CANVAS_TEXTURE_CHANNEL_DIFFUSE: {
  1102. ct->diffuse = p_texture;
  1103. } break;
  1104. case RS::CANVAS_TEXTURE_CHANNEL_NORMAL: {
  1105. ct->normalmap = p_texture;
  1106. } break;
  1107. case RS::CANVAS_TEXTURE_CHANNEL_SPECULAR: {
  1108. ct->specular = p_texture;
  1109. } break;
  1110. }
  1111. ct->clear_sets();
  1112. }
  1113. void RendererStorageRD::canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_specular_color, float p_shininess) {
  1114. CanvasTexture *ct = canvas_texture_owner.getornull(p_canvas_texture);
  1115. ct->specular_color.r = p_specular_color.r;
  1116. ct->specular_color.g = p_specular_color.g;
  1117. ct->specular_color.b = p_specular_color.b;
  1118. ct->specular_color.a = p_shininess;
  1119. ct->clear_sets();
  1120. }
  1121. void RendererStorageRD::canvas_texture_set_texture_filter(RID p_canvas_texture, RS::CanvasItemTextureFilter p_filter) {
  1122. CanvasTexture *ct = canvas_texture_owner.getornull(p_canvas_texture);
  1123. ct->texture_filter = p_filter;
  1124. ct->clear_sets();
  1125. }
  1126. void RendererStorageRD::canvas_texture_set_texture_repeat(RID p_canvas_texture, RS::CanvasItemTextureRepeat p_repeat) {
  1127. CanvasTexture *ct = canvas_texture_owner.getornull(p_canvas_texture);
  1128. ct->texture_repeat = p_repeat;
  1129. ct->clear_sets();
  1130. }
  1131. bool RendererStorageRD::canvas_texture_get_uniform_set(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, RID p_base_shader, int p_base_set, RID &r_uniform_set, Size2i &r_size, Color &r_specular_shininess, bool &r_use_normal, bool &r_use_specular) {
  1132. CanvasTexture *ct = nullptr;
  1133. Texture *t = texture_owner.getornull(p_texture);
  1134. if (t) {
  1135. //regular texture
  1136. if (!t->canvas_texture) {
  1137. t->canvas_texture = memnew(CanvasTexture);
  1138. t->canvas_texture->diffuse = p_texture;
  1139. }
  1140. ct = t->canvas_texture;
  1141. } else {
  1142. ct = canvas_texture_owner.getornull(p_texture);
  1143. }
  1144. if (!ct) {
  1145. return false; //invalid texture RID
  1146. }
  1147. RS::CanvasItemTextureFilter filter = ct->texture_filter != RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT ? ct->texture_filter : p_base_filter;
  1148. ERR_FAIL_COND_V(filter == RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT, false);
  1149. RS::CanvasItemTextureRepeat repeat = ct->texture_repeat != RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT ? ct->texture_repeat : p_base_repeat;
  1150. ERR_FAIL_COND_V(repeat == RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT, false);
  1151. RID uniform_set = ct->uniform_sets[filter][repeat];
  1152. if (!RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  1153. //create and update
  1154. Vector<RD::Uniform> uniforms;
  1155. { //diffuse
  1156. RD::Uniform u;
  1157. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1158. u.binding = 0;
  1159. t = texture_owner.getornull(ct->diffuse);
  1160. if (!t) {
  1161. u.ids.push_back(texture_rd_get_default(DEFAULT_RD_TEXTURE_WHITE));
  1162. ct->size_cache = Size2i(1, 1);
  1163. } else {
  1164. u.ids.push_back(t->rd_texture);
  1165. ct->size_cache = Size2i(t->width_2d, t->height_2d);
  1166. }
  1167. uniforms.push_back(u);
  1168. }
  1169. { //normal
  1170. RD::Uniform u;
  1171. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1172. u.binding = 1;
  1173. t = texture_owner.getornull(ct->normalmap);
  1174. if (!t) {
  1175. u.ids.push_back(texture_rd_get_default(DEFAULT_RD_TEXTURE_NORMAL));
  1176. ct->use_normal_cache = false;
  1177. } else {
  1178. u.ids.push_back(t->rd_texture);
  1179. ct->use_normal_cache = true;
  1180. }
  1181. uniforms.push_back(u);
  1182. }
  1183. { //specular
  1184. RD::Uniform u;
  1185. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1186. u.binding = 2;
  1187. t = texture_owner.getornull(ct->specular);
  1188. if (!t) {
  1189. u.ids.push_back(texture_rd_get_default(DEFAULT_RD_TEXTURE_WHITE));
  1190. ct->use_specular_cache = false;
  1191. } else {
  1192. u.ids.push_back(t->rd_texture);
  1193. ct->use_specular_cache = true;
  1194. }
  1195. uniforms.push_back(u);
  1196. }
  1197. { //sampler
  1198. RD::Uniform u;
  1199. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  1200. u.binding = 3;
  1201. u.ids.push_back(sampler_rd_get_default(filter, repeat));
  1202. uniforms.push_back(u);
  1203. }
  1204. uniform_set = RD::get_singleton()->uniform_set_create(uniforms, p_base_shader, p_base_set);
  1205. ct->uniform_sets[filter][repeat] = uniform_set;
  1206. ct->cleared_cache = false;
  1207. }
  1208. r_uniform_set = uniform_set;
  1209. r_size = ct->size_cache;
  1210. r_specular_shininess = ct->specular_color;
  1211. r_use_normal = ct->use_normal_cache;
  1212. r_use_specular = ct->use_specular_cache;
  1213. return true;
  1214. }
  1215. /* SHADER API */
  1216. RID RendererStorageRD::shader_create() {
  1217. Shader shader;
  1218. shader.data = nullptr;
  1219. shader.type = SHADER_TYPE_MAX;
  1220. return shader_owner.make_rid(shader);
  1221. }
  1222. void RendererStorageRD::shader_set_code(RID p_shader, const String &p_code) {
  1223. Shader *shader = shader_owner.getornull(p_shader);
  1224. ERR_FAIL_COND(!shader);
  1225. shader->code = p_code;
  1226. String mode_string = ShaderLanguage::get_shader_type(p_code);
  1227. ShaderType new_type;
  1228. if (mode_string == "canvas_item") {
  1229. new_type = SHADER_TYPE_2D;
  1230. } else if (mode_string == "particles") {
  1231. new_type = SHADER_TYPE_PARTICLES;
  1232. } else if (mode_string == "spatial") {
  1233. new_type = SHADER_TYPE_3D;
  1234. } else if (mode_string == "sky") {
  1235. new_type = SHADER_TYPE_SKY;
  1236. } else {
  1237. new_type = SHADER_TYPE_MAX;
  1238. }
  1239. if (new_type != shader->type) {
  1240. if (shader->data) {
  1241. memdelete(shader->data);
  1242. shader->data = nullptr;
  1243. }
  1244. for (Set<Material *>::Element *E = shader->owners.front(); E; E = E->next()) {
  1245. Material *material = E->get();
  1246. material->shader_type = new_type;
  1247. if (material->data) {
  1248. memdelete(material->data);
  1249. material->data = nullptr;
  1250. }
  1251. }
  1252. shader->type = new_type;
  1253. if (new_type < SHADER_TYPE_MAX && shader_data_request_func[new_type]) {
  1254. shader->data = shader_data_request_func[new_type]();
  1255. } else {
  1256. shader->type = SHADER_TYPE_MAX; //invalid
  1257. }
  1258. for (Set<Material *>::Element *E = shader->owners.front(); E; E = E->next()) {
  1259. Material *material = E->get();
  1260. if (shader->data) {
  1261. material->data = material_data_request_func[new_type](shader->data);
  1262. material->data->self = material->self;
  1263. material->data->set_next_pass(material->next_pass);
  1264. material->data->set_render_priority(material->priority);
  1265. }
  1266. material->shader_type = new_type;
  1267. }
  1268. for (Map<StringName, RID>::Element *E = shader->default_texture_parameter.front(); E; E = E->next()) {
  1269. shader->data->set_default_texture_param(E->key(), E->get());
  1270. }
  1271. }
  1272. if (shader->data) {
  1273. shader->data->set_code(p_code);
  1274. }
  1275. for (Set<Material *>::Element *E = shader->owners.front(); E; E = E->next()) {
  1276. Material *material = E->get();
  1277. material->instance_dependency.instance_notify_changed(false, true);
  1278. _material_queue_update(material, true, true);
  1279. }
  1280. }
  1281. String RendererStorageRD::shader_get_code(RID p_shader) const {
  1282. Shader *shader = shader_owner.getornull(p_shader);
  1283. ERR_FAIL_COND_V(!shader, String());
  1284. return shader->code;
  1285. }
  1286. void RendererStorageRD::shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const {
  1287. Shader *shader = shader_owner.getornull(p_shader);
  1288. ERR_FAIL_COND(!shader);
  1289. if (shader->data) {
  1290. return shader->data->get_param_list(p_param_list);
  1291. }
  1292. }
  1293. void RendererStorageRD::shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture) {
  1294. Shader *shader = shader_owner.getornull(p_shader);
  1295. ERR_FAIL_COND(!shader);
  1296. if (p_texture.is_valid() && texture_owner.owns(p_texture)) {
  1297. shader->default_texture_parameter[p_name] = p_texture;
  1298. } else {
  1299. shader->default_texture_parameter.erase(p_name);
  1300. }
  1301. if (shader->data) {
  1302. shader->data->set_default_texture_param(p_name, p_texture);
  1303. }
  1304. for (Set<Material *>::Element *E = shader->owners.front(); E; E = E->next()) {
  1305. Material *material = E->get();
  1306. _material_queue_update(material, false, true);
  1307. }
  1308. }
  1309. RID RendererStorageRD::shader_get_default_texture_param(RID p_shader, const StringName &p_name) const {
  1310. Shader *shader = shader_owner.getornull(p_shader);
  1311. ERR_FAIL_COND_V(!shader, RID());
  1312. if (shader->default_texture_parameter.has(p_name)) {
  1313. return shader->default_texture_parameter[p_name];
  1314. }
  1315. return RID();
  1316. }
  1317. Variant RendererStorageRD::shader_get_param_default(RID p_shader, const StringName &p_param) const {
  1318. Shader *shader = shader_owner.getornull(p_shader);
  1319. ERR_FAIL_COND_V(!shader, Variant());
  1320. if (shader->data) {
  1321. return shader->data->get_default_parameter(p_param);
  1322. }
  1323. return Variant();
  1324. }
  1325. void RendererStorageRD::shader_set_data_request_function(ShaderType p_shader_type, ShaderDataRequestFunction p_function) {
  1326. ERR_FAIL_INDEX(p_shader_type, SHADER_TYPE_MAX);
  1327. shader_data_request_func[p_shader_type] = p_function;
  1328. }
  1329. /* COMMON MATERIAL API */
  1330. RID RendererStorageRD::material_create() {
  1331. Material material;
  1332. material.data = nullptr;
  1333. material.shader = nullptr;
  1334. material.shader_type = SHADER_TYPE_MAX;
  1335. material.update_next = nullptr;
  1336. material.update_requested = false;
  1337. material.uniform_dirty = false;
  1338. material.texture_dirty = false;
  1339. material.priority = 0;
  1340. RID id = material_owner.make_rid(material);
  1341. {
  1342. Material *material_ptr = material_owner.getornull(id);
  1343. material_ptr->self = id;
  1344. }
  1345. return id;
  1346. }
  1347. void RendererStorageRD::_material_queue_update(Material *material, bool p_uniform, bool p_texture) {
  1348. if (material->update_requested) {
  1349. return;
  1350. }
  1351. material->update_next = material_update_list;
  1352. material_update_list = material;
  1353. material->update_requested = true;
  1354. material->uniform_dirty = material->uniform_dirty || p_uniform;
  1355. material->texture_dirty = material->texture_dirty || p_texture;
  1356. }
  1357. void RendererStorageRD::material_set_shader(RID p_material, RID p_shader) {
  1358. Material *material = material_owner.getornull(p_material);
  1359. ERR_FAIL_COND(!material);
  1360. if (material->data) {
  1361. memdelete(material->data);
  1362. material->data = nullptr;
  1363. }
  1364. if (material->shader) {
  1365. material->shader->owners.erase(material);
  1366. material->shader = nullptr;
  1367. material->shader_type = SHADER_TYPE_MAX;
  1368. }
  1369. if (p_shader.is_null()) {
  1370. material->instance_dependency.instance_notify_changed(false, true);
  1371. return;
  1372. }
  1373. Shader *shader = shader_owner.getornull(p_shader);
  1374. ERR_FAIL_COND(!shader);
  1375. material->shader = shader;
  1376. material->shader_type = shader->type;
  1377. shader->owners.insert(material);
  1378. if (shader->type == SHADER_TYPE_MAX) {
  1379. return;
  1380. }
  1381. ERR_FAIL_COND(shader->data == nullptr);
  1382. material->data = material_data_request_func[shader->type](shader->data);
  1383. material->data->self = p_material;
  1384. material->data->set_next_pass(material->next_pass);
  1385. material->data->set_render_priority(material->priority);
  1386. //updating happens later
  1387. material->instance_dependency.instance_notify_changed(false, true);
  1388. _material_queue_update(material, true, true);
  1389. }
  1390. void RendererStorageRD::material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) {
  1391. Material *material = material_owner.getornull(p_material);
  1392. ERR_FAIL_COND(!material);
  1393. if (p_value.get_type() == Variant::NIL) {
  1394. material->params.erase(p_param);
  1395. } else {
  1396. material->params[p_param] = p_value;
  1397. }
  1398. if (material->shader && material->shader->data) { //shader is valid
  1399. bool is_texture = material->shader->data->is_param_texture(p_param);
  1400. _material_queue_update(material, !is_texture, is_texture);
  1401. } else {
  1402. _material_queue_update(material, true, true);
  1403. }
  1404. }
  1405. Variant RendererStorageRD::material_get_param(RID p_material, const StringName &p_param) const {
  1406. Material *material = material_owner.getornull(p_material);
  1407. ERR_FAIL_COND_V(!material, Variant());
  1408. if (material->params.has(p_param)) {
  1409. return material->params[p_param];
  1410. } else {
  1411. return Variant();
  1412. }
  1413. }
  1414. void RendererStorageRD::material_set_next_pass(RID p_material, RID p_next_material) {
  1415. Material *material = material_owner.getornull(p_material);
  1416. ERR_FAIL_COND(!material);
  1417. if (material->next_pass == p_next_material) {
  1418. return;
  1419. }
  1420. material->next_pass = p_next_material;
  1421. if (material->data) {
  1422. material->data->set_next_pass(p_next_material);
  1423. }
  1424. material->instance_dependency.instance_notify_changed(false, true);
  1425. }
  1426. void RendererStorageRD::material_set_render_priority(RID p_material, int priority) {
  1427. Material *material = material_owner.getornull(p_material);
  1428. ERR_FAIL_COND(!material);
  1429. material->priority = priority;
  1430. if (material->data) {
  1431. material->data->set_render_priority(priority);
  1432. }
  1433. }
  1434. bool RendererStorageRD::material_is_animated(RID p_material) {
  1435. Material *material = material_owner.getornull(p_material);
  1436. ERR_FAIL_COND_V(!material, false);
  1437. if (material->shader && material->shader->data) {
  1438. if (material->shader->data->is_animated()) {
  1439. return true;
  1440. } else if (material->next_pass.is_valid()) {
  1441. return material_is_animated(material->next_pass);
  1442. }
  1443. }
  1444. return false; //by default nothing is animated
  1445. }
  1446. bool RendererStorageRD::material_casts_shadows(RID p_material) {
  1447. Material *material = material_owner.getornull(p_material);
  1448. ERR_FAIL_COND_V(!material, true);
  1449. if (material->shader && material->shader->data) {
  1450. if (material->shader->data->casts_shadows()) {
  1451. return true;
  1452. } else if (material->next_pass.is_valid()) {
  1453. return material_casts_shadows(material->next_pass);
  1454. }
  1455. }
  1456. return true; //by default everything casts shadows
  1457. }
  1458. void RendererStorageRD::material_get_instance_shader_parameters(RID p_material, List<InstanceShaderParam> *r_parameters) {
  1459. Material *material = material_owner.getornull(p_material);
  1460. ERR_FAIL_COND(!material);
  1461. if (material->shader && material->shader->data) {
  1462. material->shader->data->get_instance_param_list(r_parameters);
  1463. if (material->next_pass.is_valid()) {
  1464. material_get_instance_shader_parameters(material->next_pass, r_parameters);
  1465. }
  1466. }
  1467. }
  1468. void RendererStorageRD::material_update_dependency(RID p_material, InstanceBaseDependency *p_instance) {
  1469. Material *material = material_owner.getornull(p_material);
  1470. ERR_FAIL_COND(!material);
  1471. p_instance->update_dependency(&material->instance_dependency);
  1472. if (material->next_pass.is_valid()) {
  1473. material_update_dependency(material->next_pass, p_instance);
  1474. }
  1475. }
  1476. void RendererStorageRD::material_set_data_request_function(ShaderType p_shader_type, MaterialDataRequestFunction p_function) {
  1477. ERR_FAIL_INDEX(p_shader_type, SHADER_TYPE_MAX);
  1478. material_data_request_func[p_shader_type] = p_function;
  1479. }
  1480. _FORCE_INLINE_ static void _fill_std140_variant_ubo_value(ShaderLanguage::DataType type, const Variant &value, uint8_t *data, bool p_linear_color) {
  1481. switch (type) {
  1482. case ShaderLanguage::TYPE_BOOL: {
  1483. bool v = value;
  1484. uint32_t *gui = (uint32_t *)data;
  1485. *gui = v ? 1 : 0;
  1486. } break;
  1487. case ShaderLanguage::TYPE_BVEC2: {
  1488. int v = value;
  1489. uint32_t *gui = (uint32_t *)data;
  1490. gui[0] = v & 1 ? 1 : 0;
  1491. gui[1] = v & 2 ? 1 : 0;
  1492. } break;
  1493. case ShaderLanguage::TYPE_BVEC3: {
  1494. int v = value;
  1495. uint32_t *gui = (uint32_t *)data;
  1496. gui[0] = (v & 1) ? 1 : 0;
  1497. gui[1] = (v & 2) ? 1 : 0;
  1498. gui[2] = (v & 4) ? 1 : 0;
  1499. } break;
  1500. case ShaderLanguage::TYPE_BVEC4: {
  1501. int v = value;
  1502. uint32_t *gui = (uint32_t *)data;
  1503. gui[0] = (v & 1) ? 1 : 0;
  1504. gui[1] = (v & 2) ? 1 : 0;
  1505. gui[2] = (v & 4) ? 1 : 0;
  1506. gui[3] = (v & 8) ? 1 : 0;
  1507. } break;
  1508. case ShaderLanguage::TYPE_INT: {
  1509. int v = value;
  1510. int32_t *gui = (int32_t *)data;
  1511. gui[0] = v;
  1512. } break;
  1513. case ShaderLanguage::TYPE_IVEC2: {
  1514. Vector<int> iv = value;
  1515. int s = iv.size();
  1516. int32_t *gui = (int32_t *)data;
  1517. const int *r = iv.ptr();
  1518. for (int i = 0; i < 2; i++) {
  1519. if (i < s) {
  1520. gui[i] = r[i];
  1521. } else {
  1522. gui[i] = 0;
  1523. }
  1524. }
  1525. } break;
  1526. case ShaderLanguage::TYPE_IVEC3: {
  1527. Vector<int> iv = value;
  1528. int s = iv.size();
  1529. int32_t *gui = (int32_t *)data;
  1530. const int *r = iv.ptr();
  1531. for (int i = 0; i < 3; i++) {
  1532. if (i < s) {
  1533. gui[i] = r[i];
  1534. } else {
  1535. gui[i] = 0;
  1536. }
  1537. }
  1538. } break;
  1539. case ShaderLanguage::TYPE_IVEC4: {
  1540. Vector<int> iv = value;
  1541. int s = iv.size();
  1542. int32_t *gui = (int32_t *)data;
  1543. const int *r = iv.ptr();
  1544. for (int i = 0; i < 4; i++) {
  1545. if (i < s) {
  1546. gui[i] = r[i];
  1547. } else {
  1548. gui[i] = 0;
  1549. }
  1550. }
  1551. } break;
  1552. case ShaderLanguage::TYPE_UINT: {
  1553. int v = value;
  1554. uint32_t *gui = (uint32_t *)data;
  1555. gui[0] = v;
  1556. } break;
  1557. case ShaderLanguage::TYPE_UVEC2: {
  1558. Vector<int> iv = value;
  1559. int s = iv.size();
  1560. uint32_t *gui = (uint32_t *)data;
  1561. const int *r = iv.ptr();
  1562. for (int i = 0; i < 2; i++) {
  1563. if (i < s) {
  1564. gui[i] = r[i];
  1565. } else {
  1566. gui[i] = 0;
  1567. }
  1568. }
  1569. } break;
  1570. case ShaderLanguage::TYPE_UVEC3: {
  1571. Vector<int> iv = value;
  1572. int s = iv.size();
  1573. uint32_t *gui = (uint32_t *)data;
  1574. const int *r = iv.ptr();
  1575. for (int i = 0; i < 3; i++) {
  1576. if (i < s) {
  1577. gui[i] = r[i];
  1578. } else {
  1579. gui[i] = 0;
  1580. }
  1581. }
  1582. } break;
  1583. case ShaderLanguage::TYPE_UVEC4: {
  1584. Vector<int> iv = value;
  1585. int s = iv.size();
  1586. uint32_t *gui = (uint32_t *)data;
  1587. const int *r = iv.ptr();
  1588. for (int i = 0; i < 4; i++) {
  1589. if (i < s) {
  1590. gui[i] = r[i];
  1591. } else {
  1592. gui[i] = 0;
  1593. }
  1594. }
  1595. } break;
  1596. case ShaderLanguage::TYPE_FLOAT: {
  1597. float v = value;
  1598. float *gui = (float *)data;
  1599. gui[0] = v;
  1600. } break;
  1601. case ShaderLanguage::TYPE_VEC2: {
  1602. Vector2 v = value;
  1603. float *gui = (float *)data;
  1604. gui[0] = v.x;
  1605. gui[1] = v.y;
  1606. } break;
  1607. case ShaderLanguage::TYPE_VEC3: {
  1608. Vector3 v = value;
  1609. float *gui = (float *)data;
  1610. gui[0] = v.x;
  1611. gui[1] = v.y;
  1612. gui[2] = v.z;
  1613. } break;
  1614. case ShaderLanguage::TYPE_VEC4: {
  1615. float *gui = (float *)data;
  1616. if (value.get_type() == Variant::COLOR) {
  1617. Color v = value;
  1618. if (p_linear_color) {
  1619. v = v.to_linear();
  1620. }
  1621. gui[0] = v.r;
  1622. gui[1] = v.g;
  1623. gui[2] = v.b;
  1624. gui[3] = v.a;
  1625. } else if (value.get_type() == Variant::RECT2) {
  1626. Rect2 v = value;
  1627. gui[0] = v.position.x;
  1628. gui[1] = v.position.y;
  1629. gui[2] = v.size.x;
  1630. gui[3] = v.size.y;
  1631. } else if (value.get_type() == Variant::QUAT) {
  1632. Quat v = value;
  1633. gui[0] = v.x;
  1634. gui[1] = v.y;
  1635. gui[2] = v.z;
  1636. gui[3] = v.w;
  1637. } else {
  1638. Plane v = value;
  1639. gui[0] = v.normal.x;
  1640. gui[1] = v.normal.y;
  1641. gui[2] = v.normal.z;
  1642. gui[3] = v.d;
  1643. }
  1644. } break;
  1645. case ShaderLanguage::TYPE_MAT2: {
  1646. Transform2D v = value;
  1647. float *gui = (float *)data;
  1648. //in std140 members of mat2 are treated as vec4s
  1649. gui[0] = v.elements[0][0];
  1650. gui[1] = v.elements[0][1];
  1651. gui[2] = 0;
  1652. gui[3] = 0;
  1653. gui[4] = v.elements[1][0];
  1654. gui[5] = v.elements[1][1];
  1655. gui[6] = 0;
  1656. gui[7] = 0;
  1657. } break;
  1658. case ShaderLanguage::TYPE_MAT3: {
  1659. Basis v = value;
  1660. float *gui = (float *)data;
  1661. gui[0] = v.elements[0][0];
  1662. gui[1] = v.elements[1][0];
  1663. gui[2] = v.elements[2][0];
  1664. gui[3] = 0;
  1665. gui[4] = v.elements[0][1];
  1666. gui[5] = v.elements[1][1];
  1667. gui[6] = v.elements[2][1];
  1668. gui[7] = 0;
  1669. gui[8] = v.elements[0][2];
  1670. gui[9] = v.elements[1][2];
  1671. gui[10] = v.elements[2][2];
  1672. gui[11] = 0;
  1673. } break;
  1674. case ShaderLanguage::TYPE_MAT4: {
  1675. Transform v = value;
  1676. float *gui = (float *)data;
  1677. gui[0] = v.basis.elements[0][0];
  1678. gui[1] = v.basis.elements[1][0];
  1679. gui[2] = v.basis.elements[2][0];
  1680. gui[3] = 0;
  1681. gui[4] = v.basis.elements[0][1];
  1682. gui[5] = v.basis.elements[1][1];
  1683. gui[6] = v.basis.elements[2][1];
  1684. gui[7] = 0;
  1685. gui[8] = v.basis.elements[0][2];
  1686. gui[9] = v.basis.elements[1][2];
  1687. gui[10] = v.basis.elements[2][2];
  1688. gui[11] = 0;
  1689. gui[12] = v.origin.x;
  1690. gui[13] = v.origin.y;
  1691. gui[14] = v.origin.z;
  1692. gui[15] = 1;
  1693. } break;
  1694. default: {
  1695. }
  1696. }
  1697. }
  1698. _FORCE_INLINE_ static void _fill_std140_ubo_value(ShaderLanguage::DataType type, const Vector<ShaderLanguage::ConstantNode::Value> &value, uint8_t *data) {
  1699. switch (type) {
  1700. case ShaderLanguage::TYPE_BOOL: {
  1701. uint32_t *gui = (uint32_t *)data;
  1702. *gui = value[0].boolean ? 1 : 0;
  1703. } break;
  1704. case ShaderLanguage::TYPE_BVEC2: {
  1705. uint32_t *gui = (uint32_t *)data;
  1706. gui[0] = value[0].boolean ? 1 : 0;
  1707. gui[1] = value[1].boolean ? 1 : 0;
  1708. } break;
  1709. case ShaderLanguage::TYPE_BVEC3: {
  1710. uint32_t *gui = (uint32_t *)data;
  1711. gui[0] = value[0].boolean ? 1 : 0;
  1712. gui[1] = value[1].boolean ? 1 : 0;
  1713. gui[2] = value[2].boolean ? 1 : 0;
  1714. } break;
  1715. case ShaderLanguage::TYPE_BVEC4: {
  1716. uint32_t *gui = (uint32_t *)data;
  1717. gui[0] = value[0].boolean ? 1 : 0;
  1718. gui[1] = value[1].boolean ? 1 : 0;
  1719. gui[2] = value[2].boolean ? 1 : 0;
  1720. gui[3] = value[3].boolean ? 1 : 0;
  1721. } break;
  1722. case ShaderLanguage::TYPE_INT: {
  1723. int32_t *gui = (int32_t *)data;
  1724. gui[0] = value[0].sint;
  1725. } break;
  1726. case ShaderLanguage::TYPE_IVEC2: {
  1727. int32_t *gui = (int32_t *)data;
  1728. for (int i = 0; i < 2; i++) {
  1729. gui[i] = value[i].sint;
  1730. }
  1731. } break;
  1732. case ShaderLanguage::TYPE_IVEC3: {
  1733. int32_t *gui = (int32_t *)data;
  1734. for (int i = 0; i < 3; i++) {
  1735. gui[i] = value[i].sint;
  1736. }
  1737. } break;
  1738. case ShaderLanguage::TYPE_IVEC4: {
  1739. int32_t *gui = (int32_t *)data;
  1740. for (int i = 0; i < 4; i++) {
  1741. gui[i] = value[i].sint;
  1742. }
  1743. } break;
  1744. case ShaderLanguage::TYPE_UINT: {
  1745. uint32_t *gui = (uint32_t *)data;
  1746. gui[0] = value[0].uint;
  1747. } break;
  1748. case ShaderLanguage::TYPE_UVEC2: {
  1749. int32_t *gui = (int32_t *)data;
  1750. for (int i = 0; i < 2; i++) {
  1751. gui[i] = value[i].uint;
  1752. }
  1753. } break;
  1754. case ShaderLanguage::TYPE_UVEC3: {
  1755. int32_t *gui = (int32_t *)data;
  1756. for (int i = 0; i < 3; i++) {
  1757. gui[i] = value[i].uint;
  1758. }
  1759. } break;
  1760. case ShaderLanguage::TYPE_UVEC4: {
  1761. int32_t *gui = (int32_t *)data;
  1762. for (int i = 0; i < 4; i++) {
  1763. gui[i] = value[i].uint;
  1764. }
  1765. } break;
  1766. case ShaderLanguage::TYPE_FLOAT: {
  1767. float *gui = (float *)data;
  1768. gui[0] = value[0].real;
  1769. } break;
  1770. case ShaderLanguage::TYPE_VEC2: {
  1771. float *gui = (float *)data;
  1772. for (int i = 0; i < 2; i++) {
  1773. gui[i] = value[i].real;
  1774. }
  1775. } break;
  1776. case ShaderLanguage::TYPE_VEC3: {
  1777. float *gui = (float *)data;
  1778. for (int i = 0; i < 3; i++) {
  1779. gui[i] = value[i].real;
  1780. }
  1781. } break;
  1782. case ShaderLanguage::TYPE_VEC4: {
  1783. float *gui = (float *)data;
  1784. for (int i = 0; i < 4; i++) {
  1785. gui[i] = value[i].real;
  1786. }
  1787. } break;
  1788. case ShaderLanguage::TYPE_MAT2: {
  1789. float *gui = (float *)data;
  1790. //in std140 members of mat2 are treated as vec4s
  1791. gui[0] = value[0].real;
  1792. gui[1] = value[1].real;
  1793. gui[2] = 0;
  1794. gui[3] = 0;
  1795. gui[4] = value[2].real;
  1796. gui[5] = value[3].real;
  1797. gui[6] = 0;
  1798. gui[7] = 0;
  1799. } break;
  1800. case ShaderLanguage::TYPE_MAT3: {
  1801. float *gui = (float *)data;
  1802. gui[0] = value[0].real;
  1803. gui[1] = value[1].real;
  1804. gui[2] = value[2].real;
  1805. gui[3] = 0;
  1806. gui[4] = value[3].real;
  1807. gui[5] = value[4].real;
  1808. gui[6] = value[5].real;
  1809. gui[7] = 0;
  1810. gui[8] = value[6].real;
  1811. gui[9] = value[7].real;
  1812. gui[10] = value[8].real;
  1813. gui[11] = 0;
  1814. } break;
  1815. case ShaderLanguage::TYPE_MAT4: {
  1816. float *gui = (float *)data;
  1817. for (int i = 0; i < 16; i++) {
  1818. gui[i] = value[i].real;
  1819. }
  1820. } break;
  1821. default: {
  1822. }
  1823. }
  1824. }
  1825. _FORCE_INLINE_ static void _fill_std140_ubo_empty(ShaderLanguage::DataType type, uint8_t *data) {
  1826. switch (type) {
  1827. case ShaderLanguage::TYPE_BOOL:
  1828. case ShaderLanguage::TYPE_INT:
  1829. case ShaderLanguage::TYPE_UINT:
  1830. case ShaderLanguage::TYPE_FLOAT: {
  1831. zeromem(data, 4);
  1832. } break;
  1833. case ShaderLanguage::TYPE_BVEC2:
  1834. case ShaderLanguage::TYPE_IVEC2:
  1835. case ShaderLanguage::TYPE_UVEC2:
  1836. case ShaderLanguage::TYPE_VEC2: {
  1837. zeromem(data, 8);
  1838. } break;
  1839. case ShaderLanguage::TYPE_BVEC3:
  1840. case ShaderLanguage::TYPE_IVEC3:
  1841. case ShaderLanguage::TYPE_UVEC3:
  1842. case ShaderLanguage::TYPE_VEC3:
  1843. case ShaderLanguage::TYPE_BVEC4:
  1844. case ShaderLanguage::TYPE_IVEC4:
  1845. case ShaderLanguage::TYPE_UVEC4:
  1846. case ShaderLanguage::TYPE_VEC4: {
  1847. zeromem(data, 16);
  1848. } break;
  1849. case ShaderLanguage::TYPE_MAT2: {
  1850. zeromem(data, 32);
  1851. } break;
  1852. case ShaderLanguage::TYPE_MAT3: {
  1853. zeromem(data, 48);
  1854. } break;
  1855. case ShaderLanguage::TYPE_MAT4: {
  1856. zeromem(data, 64);
  1857. } break;
  1858. default: {
  1859. }
  1860. }
  1861. }
  1862. void RendererStorageRD::MaterialData::update_uniform_buffer(const Map<StringName, ShaderLanguage::ShaderNode::Uniform> &p_uniforms, const uint32_t *p_uniform_offsets, const Map<StringName, Variant> &p_parameters, uint8_t *p_buffer, uint32_t p_buffer_size, bool p_use_linear_color) {
  1863. bool uses_global_buffer = false;
  1864. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = p_uniforms.front(); E; E = E->next()) {
  1865. if (E->get().order < 0) {
  1866. continue; // texture, does not go here
  1867. }
  1868. if (E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  1869. continue; //instance uniforms don't appear in the bufferr
  1870. }
  1871. if (E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  1872. //this is a global variable, get the index to it
  1873. RendererStorageRD *rs = base_singleton;
  1874. GlobalVariables::Variable *gv = rs->global_variables.variables.getptr(E->key());
  1875. uint32_t index = 0;
  1876. if (gv) {
  1877. index = gv->buffer_index;
  1878. } else {
  1879. WARN_PRINT("Shader uses global uniform '" + E->key() + "', but it was removed at some point. Material will not display correctly.");
  1880. }
  1881. uint32_t offset = p_uniform_offsets[E->get().order];
  1882. uint32_t *intptr = (uint32_t *)&p_buffer[offset];
  1883. *intptr = index;
  1884. uses_global_buffer = true;
  1885. continue;
  1886. }
  1887. //regular uniform
  1888. uint32_t offset = p_uniform_offsets[E->get().order];
  1889. #ifdef DEBUG_ENABLED
  1890. uint32_t size = ShaderLanguage::get_type_size(E->get().type);
  1891. ERR_CONTINUE(offset + size > p_buffer_size);
  1892. #endif
  1893. uint8_t *data = &p_buffer[offset];
  1894. const Map<StringName, Variant>::Element *V = p_parameters.find(E->key());
  1895. if (V) {
  1896. //user provided
  1897. _fill_std140_variant_ubo_value(E->get().type, V->get(), data, p_use_linear_color);
  1898. } else if (E->get().default_value.size()) {
  1899. //default value
  1900. _fill_std140_ubo_value(E->get().type, E->get().default_value, data);
  1901. //value=E->get().default_value;
  1902. } else {
  1903. //zero because it was not provided
  1904. if (E->get().type == ShaderLanguage::TYPE_VEC4 && E->get().hint == ShaderLanguage::ShaderNode::Uniform::HINT_COLOR) {
  1905. //colors must be set as black, with alpha as 1.0
  1906. _fill_std140_variant_ubo_value(E->get().type, Color(0, 0, 0, 1), data, p_use_linear_color);
  1907. } else {
  1908. //else just zero it out
  1909. _fill_std140_ubo_empty(E->get().type, data);
  1910. }
  1911. }
  1912. }
  1913. if (uses_global_buffer != (global_buffer_E != nullptr)) {
  1914. RendererStorageRD *rs = base_singleton;
  1915. if (uses_global_buffer) {
  1916. global_buffer_E = rs->global_variables.materials_using_buffer.push_back(self);
  1917. } else {
  1918. rs->global_variables.materials_using_buffer.erase(global_buffer_E);
  1919. global_buffer_E = nullptr;
  1920. }
  1921. }
  1922. }
  1923. RendererStorageRD::MaterialData::~MaterialData() {
  1924. if (global_buffer_E) {
  1925. //unregister global buffers
  1926. RendererStorageRD *rs = base_singleton;
  1927. rs->global_variables.materials_using_buffer.erase(global_buffer_E);
  1928. }
  1929. if (global_texture_E) {
  1930. //unregister global textures
  1931. RendererStorageRD *rs = base_singleton;
  1932. for (Map<StringName, uint64_t>::Element *E = used_global_textures.front(); E; E = E->next()) {
  1933. GlobalVariables::Variable *v = rs->global_variables.variables.getptr(E->key());
  1934. if (v) {
  1935. v->texture_materials.erase(self);
  1936. }
  1937. }
  1938. //unregister material from those using global textures
  1939. rs->global_variables.materials_using_texture.erase(global_texture_E);
  1940. }
  1941. }
  1942. void RendererStorageRD::MaterialData::update_textures(const Map<StringName, Variant> &p_parameters, const Map<StringName, RID> &p_default_textures, const Vector<ShaderCompilerRD::GeneratedCode::Texture> &p_texture_uniforms, RID *p_textures, bool p_use_linear_color) {
  1943. RendererStorageRD *singleton = (RendererStorageRD *)RendererStorage::base_singleton;
  1944. #ifdef TOOLS_ENABLED
  1945. Texture *roughness_detect_texture = nullptr;
  1946. RS::TextureDetectRoughnessChannel roughness_channel = RS::TEXTURE_DETECT_ROUGNHESS_R;
  1947. Texture *normal_detect_texture = nullptr;
  1948. #endif
  1949. bool uses_global_textures = false;
  1950. global_textures_pass++;
  1951. for (int i = 0; i < p_texture_uniforms.size(); i++) {
  1952. const StringName &uniform_name = p_texture_uniforms[i].name;
  1953. RID texture;
  1954. if (p_texture_uniforms[i].global) {
  1955. RendererStorageRD *rs = base_singleton;
  1956. uses_global_textures = true;
  1957. GlobalVariables::Variable *v = rs->global_variables.variables.getptr(uniform_name);
  1958. if (v) {
  1959. if (v->buffer_index >= 0) {
  1960. WARN_PRINT("Shader uses global uniform texture '" + String(uniform_name) + "', but it changed type and is no longer a texture!.");
  1961. } else {
  1962. Map<StringName, uint64_t>::Element *E = used_global_textures.find(uniform_name);
  1963. if (!E) {
  1964. E = used_global_textures.insert(uniform_name, global_textures_pass);
  1965. v->texture_materials.insert(self);
  1966. } else {
  1967. E->get() = global_textures_pass;
  1968. }
  1969. texture = v->override.get_type() != Variant::NIL ? v->override : v->value;
  1970. }
  1971. } else {
  1972. WARN_PRINT("Shader uses global uniform texture '" + String(uniform_name) + "', but it was removed at some point. Material will not display correctly.");
  1973. }
  1974. } else {
  1975. if (!texture.is_valid()) {
  1976. const Map<StringName, Variant>::Element *V = p_parameters.find(uniform_name);
  1977. if (V) {
  1978. texture = V->get();
  1979. }
  1980. }
  1981. if (!texture.is_valid()) {
  1982. const Map<StringName, RID>::Element *W = p_default_textures.find(uniform_name);
  1983. if (W) {
  1984. texture = W->get();
  1985. }
  1986. }
  1987. }
  1988. RID rd_texture;
  1989. if (texture.is_null()) {
  1990. //check default usage
  1991. switch (p_texture_uniforms[i].hint) {
  1992. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK:
  1993. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO: {
  1994. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_BLACK);
  1995. } break;
  1996. case ShaderLanguage::ShaderNode::Uniform::HINT_NONE: {
  1997. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_NORMAL);
  1998. } break;
  1999. case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
  2000. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_ANISO);
  2001. } break;
  2002. default: {
  2003. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_WHITE);
  2004. } break;
  2005. }
  2006. } else {
  2007. bool srgb = p_use_linear_color && (p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_ALBEDO || p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO);
  2008. Texture *tex = singleton->texture_owner.getornull(texture);
  2009. if (tex) {
  2010. rd_texture = (srgb && tex->rd_texture_srgb.is_valid()) ? tex->rd_texture_srgb : tex->rd_texture;
  2011. #ifdef TOOLS_ENABLED
  2012. if (tex->detect_3d_callback && p_use_linear_color) {
  2013. tex->detect_3d_callback(tex->detect_3d_callback_ud);
  2014. }
  2015. if (tex->detect_normal_callback && (p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL || p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL)) {
  2016. if (p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL) {
  2017. normal_detect_texture = tex;
  2018. }
  2019. tex->detect_normal_callback(tex->detect_normal_callback_ud);
  2020. }
  2021. if (tex->detect_roughness_callback && (p_texture_uniforms[i].hint >= ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_R || p_texture_uniforms[i].hint <= ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_GRAY)) {
  2022. //find the normal texture
  2023. roughness_detect_texture = tex;
  2024. roughness_channel = RS::TextureDetectRoughnessChannel(p_texture_uniforms[i].hint - ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_R);
  2025. }
  2026. #endif
  2027. }
  2028. if (rd_texture.is_null()) {
  2029. //wtf
  2030. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_WHITE);
  2031. }
  2032. }
  2033. p_textures[i] = rd_texture;
  2034. }
  2035. #ifdef TOOLS_ENABLED
  2036. if (roughness_detect_texture && normal_detect_texture && normal_detect_texture->path != String()) {
  2037. roughness_detect_texture->detect_roughness_callback(roughness_detect_texture->detect_roughness_callback_ud, normal_detect_texture->path, roughness_channel);
  2038. }
  2039. #endif
  2040. {
  2041. //for textures no longer used, unregister them
  2042. List<Map<StringName, uint64_t>::Element *> to_delete;
  2043. RendererStorageRD *rs = base_singleton;
  2044. for (Map<StringName, uint64_t>::Element *E = used_global_textures.front(); E; E = E->next()) {
  2045. if (E->get() != global_textures_pass) {
  2046. to_delete.push_back(E);
  2047. GlobalVariables::Variable *v = rs->global_variables.variables.getptr(E->key());
  2048. if (v) {
  2049. v->texture_materials.erase(self);
  2050. }
  2051. }
  2052. }
  2053. while (to_delete.front()) {
  2054. used_global_textures.erase(to_delete.front()->get());
  2055. to_delete.pop_front();
  2056. }
  2057. //handle registering/unregistering global textures
  2058. if (uses_global_textures != (global_texture_E != nullptr)) {
  2059. if (uses_global_textures) {
  2060. global_texture_E = rs->global_variables.materials_using_texture.push_back(self);
  2061. } else {
  2062. rs->global_variables.materials_using_texture.erase(global_texture_E);
  2063. global_texture_E = nullptr;
  2064. }
  2065. }
  2066. }
  2067. }
  2068. void RendererStorageRD::material_force_update_textures(RID p_material, ShaderType p_shader_type) {
  2069. Material *material = material_owner.getornull(p_material);
  2070. if (material->shader_type != p_shader_type) {
  2071. return;
  2072. }
  2073. if (material->data) {
  2074. material->data->update_parameters(material->params, false, true);
  2075. }
  2076. }
  2077. void RendererStorageRD::_update_queued_materials() {
  2078. Material *material = material_update_list;
  2079. while (material) {
  2080. Material *next = material->update_next;
  2081. if (material->data) {
  2082. material->data->update_parameters(material->params, material->uniform_dirty, material->texture_dirty);
  2083. }
  2084. material->update_requested = false;
  2085. material->texture_dirty = false;
  2086. material->uniform_dirty = false;
  2087. material->update_next = nullptr;
  2088. material = next;
  2089. }
  2090. material_update_list = nullptr;
  2091. }
  2092. /* MESH API */
  2093. RID RendererStorageRD::mesh_create() {
  2094. return mesh_owner.make_rid(Mesh());
  2095. }
  2096. /// Returns stride
  2097. void RendererStorageRD::mesh_add_surface(RID p_mesh, const RS::SurfaceData &p_surface) {
  2098. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2099. ERR_FAIL_COND(!mesh);
  2100. //ensure blend shape consistency
  2101. ERR_FAIL_COND(mesh->blend_shape_count && p_surface.blend_shape_count != mesh->blend_shape_count);
  2102. ERR_FAIL_COND(mesh->blend_shape_count && p_surface.bone_aabbs.size() != mesh->bone_aabbs.size());
  2103. #ifdef DEBUG_ENABLED
  2104. //do a validation, to catch errors first
  2105. {
  2106. uint32_t stride = 0;
  2107. uint32_t attrib_stride = 0;
  2108. uint32_t skin_stride = 0;
  2109. for (int i = 0; i < RS::ARRAY_WEIGHTS; i++) {
  2110. if ((p_surface.format & (1 << i))) {
  2111. switch (i) {
  2112. case RS::ARRAY_VERTEX: {
  2113. if (p_surface.format & RS::ARRAY_FLAG_USE_2D_VERTICES) {
  2114. stride += sizeof(float) * 2;
  2115. } else {
  2116. stride += sizeof(float) * 3;
  2117. }
  2118. } break;
  2119. case RS::ARRAY_NORMAL: {
  2120. stride += sizeof(int32_t);
  2121. } break;
  2122. case RS::ARRAY_TANGENT: {
  2123. stride += sizeof(int32_t);
  2124. } break;
  2125. case RS::ARRAY_COLOR: {
  2126. attrib_stride += sizeof(int16_t) * 4;
  2127. } break;
  2128. case RS::ARRAY_TEX_UV: {
  2129. attrib_stride += sizeof(float) * 2;
  2130. } break;
  2131. case RS::ARRAY_TEX_UV2: {
  2132. attrib_stride += sizeof(float) * 2;
  2133. } break;
  2134. case RS::ARRAY_CUSTOM0:
  2135. case RS::ARRAY_CUSTOM1:
  2136. case RS::ARRAY_CUSTOM2:
  2137. case RS::ARRAY_CUSTOM3: {
  2138. int idx = i - RS::ARRAY_CUSTOM0;
  2139. uint32_t fmt_shift[RS::ARRAY_CUSTOM_COUNT] = { RS::ARRAY_FORMAT_CUSTOM0_SHIFT, RS::ARRAY_FORMAT_CUSTOM1_SHIFT, RS::ARRAY_FORMAT_CUSTOM2_SHIFT, RS::ARRAY_FORMAT_CUSTOM3_SHIFT };
  2140. uint32_t fmt = (p_surface.format >> fmt_shift[idx]) & RS::ARRAY_FORMAT_CUSTOM_MASK;
  2141. uint32_t fmtsize[RS::ARRAY_CUSTOM_MAX] = { 4, 4, 4, 8, 4, 8, 12, 16 };
  2142. attrib_stride += fmtsize[fmt];
  2143. } break;
  2144. case RS::ARRAY_WEIGHTS:
  2145. case RS::ARRAY_BONES: {
  2146. //uses a separate array
  2147. bool use_8 = p_surface.format & RS::ARRAY_FLAG_USE_8_BONE_WEIGHTS;
  2148. skin_stride += sizeof(int16_t) * (use_8 ? 8 : 4);
  2149. } break;
  2150. }
  2151. }
  2152. }
  2153. int expected_size = stride * p_surface.vertex_count;
  2154. ERR_FAIL_COND_MSG(expected_size != p_surface.vertex_data.size(), "Size of vertex data provided (" + itos(p_surface.vertex_data.size()) + ") does not match expected (" + itos(expected_size) + ")");
  2155. int expected_attrib_size = attrib_stride * p_surface.vertex_count;
  2156. ERR_FAIL_COND_MSG(expected_attrib_size != p_surface.attribute_data.size(), "Size of attribute data provided (" + itos(p_surface.attribute_data.size()) + ") does not match expected (" + itos(expected_attrib_size) + ")");
  2157. if ((p_surface.format & RS::ARRAY_FORMAT_WEIGHTS) && (p_surface.format & RS::ARRAY_FORMAT_BONES)) {
  2158. expected_size = skin_stride * p_surface.vertex_count;
  2159. ERR_FAIL_COND_MSG(expected_size != p_surface.skin_data.size(), "Size of skin data provided (" + itos(p_surface.skin_data.size()) + ") does not match expected (" + itos(expected_size) + ")");
  2160. }
  2161. }
  2162. #endif
  2163. Mesh::Surface *s = memnew(Mesh::Surface);
  2164. s->format = p_surface.format;
  2165. s->primitive = p_surface.primitive;
  2166. s->vertex_buffer = RD::get_singleton()->vertex_buffer_create(p_surface.vertex_data.size(), p_surface.vertex_data);
  2167. if (p_surface.attribute_data.size()) {
  2168. s->attribute_buffer = RD::get_singleton()->vertex_buffer_create(p_surface.attribute_data.size(), p_surface.attribute_data);
  2169. }
  2170. if (p_surface.skin_data.size()) {
  2171. s->skin_buffer = RD::get_singleton()->vertex_buffer_create(p_surface.skin_data.size(), p_surface.skin_data);
  2172. }
  2173. s->vertex_count = p_surface.vertex_count;
  2174. if (p_surface.index_count) {
  2175. bool is_index_16 = p_surface.vertex_count <= 65536;
  2176. s->index_buffer = RD::get_singleton()->index_buffer_create(p_surface.index_count, is_index_16 ? RD::INDEX_BUFFER_FORMAT_UINT16 : RD::INDEX_BUFFER_FORMAT_UINT32, p_surface.index_data, false);
  2177. s->index_count = p_surface.index_count;
  2178. s->index_array = RD::get_singleton()->index_array_create(s->index_buffer, 0, s->index_count);
  2179. if (p_surface.lods.size()) {
  2180. s->lods = memnew_arr(Mesh::Surface::LOD, p_surface.lods.size());
  2181. s->lod_count = p_surface.lods.size();
  2182. for (int i = 0; i < p_surface.lods.size(); i++) {
  2183. uint32_t indices = p_surface.lods[i].index_data.size() / (is_index_16 ? 2 : 4);
  2184. s->lods[i].index_buffer = RD::get_singleton()->index_buffer_create(indices, is_index_16 ? RD::INDEX_BUFFER_FORMAT_UINT16 : RD::INDEX_BUFFER_FORMAT_UINT32, p_surface.lods[i].index_data);
  2185. s->lods[i].index_array = RD::get_singleton()->index_array_create(s->lods[i].index_buffer, 0, indices);
  2186. s->lods[i].edge_length = p_surface.lods[i].edge_length;
  2187. }
  2188. }
  2189. }
  2190. s->aabb = p_surface.aabb;
  2191. s->bone_aabbs = p_surface.bone_aabbs; //only really useful for returning them.
  2192. #if 0
  2193. for (int i = 0; i < p_surface.blend_shapes.size(); i++) {
  2194. if (p_surface.blend_shapes[i].size() != p_surface.vertex_data.size()) {
  2195. memdelete(s);
  2196. ERR_FAIL_COND(p_surface.blend_shapes[i].size() != p_surface.vertex_data.size());
  2197. }
  2198. RID vertex_buffer = RD::get_singleton()->vertex_buffer_create(p_surface.blend_shapes[i].size(), p_surface.blend_shapes[i]);
  2199. s->blend_shapes.push_back(vertex_buffer);
  2200. }
  2201. #endif
  2202. mesh->blend_shape_count = p_surface.blend_shape_count;
  2203. if (mesh->surface_count == 0) {
  2204. mesh->bone_aabbs = p_surface.bone_aabbs;
  2205. mesh->aabb = p_surface.aabb;
  2206. } else {
  2207. for (int i = 0; i < p_surface.bone_aabbs.size(); i++) {
  2208. mesh->bone_aabbs.write[i].merge_with(p_surface.bone_aabbs[i]);
  2209. }
  2210. mesh->aabb.merge_with(p_surface.aabb);
  2211. }
  2212. s->material = p_surface.material;
  2213. mesh->surfaces = (Mesh::Surface **)memrealloc(mesh->surfaces, sizeof(Mesh::Surface *) * (mesh->surface_count + 1));
  2214. mesh->surfaces[mesh->surface_count] = s;
  2215. mesh->surface_count++;
  2216. mesh->instance_dependency.instance_notify_changed(true, true);
  2217. mesh->material_cache.clear();
  2218. }
  2219. int RendererStorageRD::mesh_get_blend_shape_count(RID p_mesh) const {
  2220. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2221. ERR_FAIL_COND_V(!mesh, -1);
  2222. return mesh->blend_shape_count;
  2223. }
  2224. void RendererStorageRD::mesh_set_blend_shape_mode(RID p_mesh, RS::BlendShapeMode p_mode) {
  2225. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2226. ERR_FAIL_COND(!mesh);
  2227. ERR_FAIL_INDEX((int)p_mode, 2);
  2228. mesh->blend_shape_mode = p_mode;
  2229. }
  2230. RS::BlendShapeMode RendererStorageRD::mesh_get_blend_shape_mode(RID p_mesh) const {
  2231. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2232. ERR_FAIL_COND_V(!mesh, RS::BLEND_SHAPE_MODE_NORMALIZED);
  2233. return mesh->blend_shape_mode;
  2234. }
  2235. void RendererStorageRD::mesh_surface_update_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
  2236. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2237. ERR_FAIL_COND(!mesh);
  2238. ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
  2239. ERR_FAIL_COND(p_data.size() == 0);
  2240. uint64_t data_size = p_data.size();
  2241. const uint8_t *r = p_data.ptr();
  2242. RD::get_singleton()->buffer_update(mesh->surfaces[p_surface]->vertex_buffer, p_offset, data_size, r);
  2243. }
  2244. void RendererStorageRD::mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) {
  2245. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2246. ERR_FAIL_COND(!mesh);
  2247. ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
  2248. mesh->surfaces[p_surface]->material = p_material;
  2249. mesh->instance_dependency.instance_notify_changed(false, true);
  2250. mesh->material_cache.clear();
  2251. }
  2252. RID RendererStorageRD::mesh_surface_get_material(RID p_mesh, int p_surface) const {
  2253. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2254. ERR_FAIL_COND_V(!mesh, RID());
  2255. ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_surface, mesh->surface_count, RID());
  2256. return mesh->surfaces[p_surface]->material;
  2257. }
  2258. RS::SurfaceData RendererStorageRD::mesh_get_surface(RID p_mesh, int p_surface) const {
  2259. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2260. ERR_FAIL_COND_V(!mesh, RS::SurfaceData());
  2261. ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_surface, mesh->surface_count, RS::SurfaceData());
  2262. Mesh::Surface &s = *mesh->surfaces[p_surface];
  2263. RS::SurfaceData sd;
  2264. sd.format = s.format;
  2265. sd.vertex_data = RD::get_singleton()->buffer_get_data(s.vertex_buffer);
  2266. if (s.attribute_buffer.is_valid()) {
  2267. sd.attribute_data = RD::get_singleton()->buffer_get_data(s.attribute_buffer);
  2268. }
  2269. if (s.skin_buffer.is_valid()) {
  2270. sd.skin_data = RD::get_singleton()->buffer_get_data(s.skin_buffer);
  2271. }
  2272. sd.vertex_count = s.vertex_count;
  2273. sd.index_count = s.index_count;
  2274. sd.primitive = s.primitive;
  2275. if (sd.index_count) {
  2276. sd.index_data = RD::get_singleton()->buffer_get_data(s.index_buffer);
  2277. }
  2278. sd.aabb = s.aabb;
  2279. for (uint32_t i = 0; i < s.lod_count; i++) {
  2280. RS::SurfaceData::LOD lod;
  2281. lod.edge_length = s.lods[i].edge_length;
  2282. lod.index_data = RD::get_singleton()->buffer_get_data(s.lods[i].index_buffer);
  2283. sd.lods.push_back(lod);
  2284. }
  2285. sd.bone_aabbs = s.bone_aabbs;
  2286. if (s.blend_shape_buffer.is_valid()) {
  2287. sd.blend_shape_data = RD::get_singleton()->buffer_get_data(s.blend_shape_buffer);
  2288. }
  2289. return sd;
  2290. }
  2291. int RendererStorageRD::mesh_get_surface_count(RID p_mesh) const {
  2292. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2293. ERR_FAIL_COND_V(!mesh, 0);
  2294. return mesh->surface_count;
  2295. }
  2296. void RendererStorageRD::mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) {
  2297. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2298. ERR_FAIL_COND(!mesh);
  2299. mesh->custom_aabb = p_aabb;
  2300. }
  2301. AABB RendererStorageRD::mesh_get_custom_aabb(RID p_mesh) const {
  2302. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2303. ERR_FAIL_COND_V(!mesh, AABB());
  2304. return mesh->custom_aabb;
  2305. }
  2306. AABB RendererStorageRD::mesh_get_aabb(RID p_mesh, RID p_skeleton) {
  2307. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2308. ERR_FAIL_COND_V(!mesh, AABB());
  2309. if (mesh->custom_aabb != AABB()) {
  2310. return mesh->custom_aabb;
  2311. }
  2312. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2313. if (!skeleton || skeleton->size == 0) {
  2314. return mesh->aabb;
  2315. }
  2316. AABB aabb;
  2317. for (uint32_t i = 0; i < mesh->surface_count; i++) {
  2318. AABB laabb;
  2319. if ((mesh->surfaces[i]->format & RS::ARRAY_FORMAT_BONES) && mesh->surfaces[i]->bone_aabbs.size()) {
  2320. int bs = mesh->surfaces[i]->bone_aabbs.size();
  2321. const AABB *skbones = mesh->surfaces[i]->bone_aabbs.ptr();
  2322. int sbs = skeleton->size;
  2323. ERR_CONTINUE(bs > sbs);
  2324. const float *baseptr = skeleton->data.ptr();
  2325. bool first = true;
  2326. if (skeleton->use_2d) {
  2327. for (int j = 0; j < bs; j++) {
  2328. if (skbones[0].size == Vector3()) {
  2329. continue; //bone is unused
  2330. }
  2331. const float *dataptr = baseptr + j * 8;
  2332. Transform mtx;
  2333. mtx.basis.elements[0].x = dataptr[0];
  2334. mtx.basis.elements[1].x = dataptr[1];
  2335. mtx.origin.x = dataptr[3];
  2336. mtx.basis.elements[0].y = dataptr[4];
  2337. mtx.basis.elements[1].y = dataptr[5];
  2338. mtx.origin.y = dataptr[7];
  2339. AABB baabb = mtx.xform(skbones[j]);
  2340. if (first) {
  2341. laabb = baabb;
  2342. first = false;
  2343. } else {
  2344. laabb.merge_with(baabb);
  2345. }
  2346. }
  2347. } else {
  2348. for (int j = 0; j < bs; j++) {
  2349. if (skbones[0].size == Vector3()) {
  2350. continue; //bone is unused
  2351. }
  2352. const float *dataptr = baseptr + j * 12;
  2353. Transform mtx;
  2354. mtx.basis.elements[0][0] = dataptr[0];
  2355. mtx.basis.elements[0][1] = dataptr[1];
  2356. mtx.basis.elements[0][2] = dataptr[2];
  2357. mtx.origin.x = dataptr[3];
  2358. mtx.basis.elements[1][0] = dataptr[4];
  2359. mtx.basis.elements[1][1] = dataptr[5];
  2360. mtx.basis.elements[1][2] = dataptr[6];
  2361. mtx.origin.y = dataptr[7];
  2362. mtx.basis.elements[2][0] = dataptr[8];
  2363. mtx.basis.elements[2][1] = dataptr[9];
  2364. mtx.basis.elements[2][2] = dataptr[10];
  2365. mtx.origin.z = dataptr[11];
  2366. AABB baabb = mtx.xform(skbones[j]);
  2367. if (first) {
  2368. laabb = baabb;
  2369. first = false;
  2370. } else {
  2371. laabb.merge_with(baabb);
  2372. }
  2373. }
  2374. }
  2375. if (laabb.size == Vector3()) {
  2376. laabb = mesh->surfaces[i]->aabb;
  2377. }
  2378. } else {
  2379. laabb = mesh->surfaces[i]->aabb;
  2380. }
  2381. if (i == 0) {
  2382. aabb = laabb;
  2383. } else {
  2384. aabb.merge_with(laabb);
  2385. }
  2386. }
  2387. return aabb;
  2388. }
  2389. void RendererStorageRD::mesh_clear(RID p_mesh) {
  2390. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2391. ERR_FAIL_COND(!mesh);
  2392. for (uint32_t i = 0; i < mesh->surface_count; i++) {
  2393. Mesh::Surface &s = *mesh->surfaces[i];
  2394. RD::get_singleton()->free(s.vertex_buffer); //clears arrays as dependency automatically, including all versions
  2395. if (s.attribute_buffer.is_valid()) {
  2396. RD::get_singleton()->free(s.attribute_buffer);
  2397. }
  2398. if (s.skin_buffer.is_valid()) {
  2399. RD::get_singleton()->free(s.skin_buffer);
  2400. }
  2401. if (s.versions) {
  2402. memfree(s.versions); //reallocs, so free with memfree.
  2403. }
  2404. if (s.index_buffer.is_valid()) {
  2405. RD::get_singleton()->free(s.index_buffer);
  2406. }
  2407. if (s.lod_count) {
  2408. for (uint32_t j = 0; j < s.lod_count; j++) {
  2409. RD::get_singleton()->free(s.lods[j].index_buffer);
  2410. }
  2411. memdelete_arr(s.lods);
  2412. }
  2413. if (s.blend_shape_buffer.is_valid()) {
  2414. RD::get_singleton()->free(s.blend_shape_buffer);
  2415. }
  2416. memdelete(mesh->surfaces[i]);
  2417. }
  2418. if (mesh->surfaces) {
  2419. memfree(mesh->surfaces);
  2420. }
  2421. mesh->surfaces = nullptr;
  2422. mesh->surface_count = 0;
  2423. mesh->material_cache.clear();
  2424. mesh->instance_dependency.instance_notify_changed(true, true);
  2425. }
  2426. void RendererStorageRD::_mesh_surface_generate_version_for_input_mask(Mesh::Surface *s, uint32_t p_input_mask) {
  2427. uint32_t version = s->version_count;
  2428. s->version_count++;
  2429. s->versions = (Mesh::Surface::Version *)memrealloc(s->versions, sizeof(Mesh::Surface::Version) * s->version_count);
  2430. Mesh::Surface::Version &v = s->versions[version];
  2431. Vector<RD::VertexAttribute> attributes;
  2432. Vector<RID> buffers;
  2433. uint32_t stride = 0;
  2434. uint32_t attribute_stride = 0;
  2435. uint32_t skin_stride = 0;
  2436. for (int i = 0; i < RS::ARRAY_INDEX; i++) {
  2437. RD::VertexAttribute vd;
  2438. RID buffer;
  2439. vd.location = i;
  2440. if (!(s->format & (1 << i))) {
  2441. // Not supplied by surface, use default value
  2442. buffer = mesh_default_rd_buffers[i];
  2443. vd.stride = 0;
  2444. switch (i) {
  2445. case RS::ARRAY_VERTEX: {
  2446. vd.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  2447. } break;
  2448. case RS::ARRAY_NORMAL: {
  2449. vd.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  2450. } break;
  2451. case RS::ARRAY_TANGENT: {
  2452. vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  2453. } break;
  2454. case RS::ARRAY_COLOR: {
  2455. vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  2456. } break;
  2457. case RS::ARRAY_TEX_UV: {
  2458. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2459. } break;
  2460. case RS::ARRAY_TEX_UV2: {
  2461. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2462. } break;
  2463. case RS::ARRAY_CUSTOM0:
  2464. case RS::ARRAY_CUSTOM1:
  2465. case RS::ARRAY_CUSTOM2:
  2466. case RS::ARRAY_CUSTOM3: {
  2467. //assumed weights too
  2468. vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  2469. } break;
  2470. case RS::ARRAY_BONES: {
  2471. //assumed weights too
  2472. vd.format = RD::DATA_FORMAT_R32G32B32A32_UINT;
  2473. } break;
  2474. case RS::ARRAY_WEIGHTS: {
  2475. //assumed weights too
  2476. vd.format = RD::DATA_FORMAT_R32G32B32A32_UINT;
  2477. } break;
  2478. }
  2479. } else {
  2480. //Supplied, use it
  2481. vd.stride = 1; //mark that it needs a stride set (default uses 0)
  2482. switch (i) {
  2483. case RS::ARRAY_VERTEX: {
  2484. vd.offset = stride;
  2485. if (s->format & RS::ARRAY_FLAG_USE_2D_VERTICES) {
  2486. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2487. stride += sizeof(float) * 2;
  2488. } else {
  2489. vd.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  2490. stride += sizeof(float) * 3;
  2491. }
  2492. buffer = s->vertex_buffer;
  2493. } break;
  2494. case RS::ARRAY_NORMAL: {
  2495. vd.offset = stride;
  2496. vd.format = RD::DATA_FORMAT_A2B10G10R10_UNORM_PACK32;
  2497. stride += sizeof(uint32_t);
  2498. buffer = s->vertex_buffer;
  2499. } break;
  2500. case RS::ARRAY_TANGENT: {
  2501. vd.offset = stride;
  2502. vd.format = RD::DATA_FORMAT_A2B10G10R10_UNORM_PACK32;
  2503. stride += sizeof(uint32_t);
  2504. buffer = s->vertex_buffer;
  2505. } break;
  2506. case RS::ARRAY_COLOR: {
  2507. vd.offset = attribute_stride;
  2508. vd.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  2509. attribute_stride += sizeof(int16_t) * 4;
  2510. buffer = s->attribute_buffer;
  2511. } break;
  2512. case RS::ARRAY_TEX_UV: {
  2513. vd.offset = attribute_stride;
  2514. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2515. attribute_stride += sizeof(float) * 2;
  2516. buffer = s->attribute_buffer;
  2517. } break;
  2518. case RS::ARRAY_TEX_UV2: {
  2519. vd.offset = attribute_stride;
  2520. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2521. attribute_stride += sizeof(float) * 2;
  2522. buffer = s->attribute_buffer;
  2523. } break;
  2524. case RS::ARRAY_CUSTOM0:
  2525. case RS::ARRAY_CUSTOM1:
  2526. case RS::ARRAY_CUSTOM2:
  2527. case RS::ARRAY_CUSTOM3: {
  2528. vd.offset = attribute_stride;
  2529. int idx = i - RS::ARRAY_CUSTOM0;
  2530. uint32_t fmt_shift[RS::ARRAY_CUSTOM_COUNT] = { RS::ARRAY_FORMAT_CUSTOM0_SHIFT, RS::ARRAY_FORMAT_CUSTOM1_SHIFT, RS::ARRAY_FORMAT_CUSTOM2_SHIFT, RS::ARRAY_FORMAT_CUSTOM3_SHIFT };
  2531. uint32_t fmt = (s->format >> fmt_shift[idx]) & RS::ARRAY_FORMAT_CUSTOM_MASK;
  2532. uint32_t fmtsize[RS::ARRAY_CUSTOM_MAX] = { 4, 4, 4, 8, 4, 8, 12, 16 };
  2533. RD::DataFormat fmtrd[RS::ARRAY_CUSTOM_MAX] = { RD::DATA_FORMAT_R8G8B8A8_UNORM, RD::DATA_FORMAT_R8G8B8A8_SNORM, RD::DATA_FORMAT_R16G16_SFLOAT, RD::DATA_FORMAT_R16G16B16A16_SFLOAT, RD::DATA_FORMAT_R32_SFLOAT, RD::DATA_FORMAT_R32G32_SFLOAT, RD::DATA_FORMAT_R32G32B32_SFLOAT, RD::DATA_FORMAT_R32G32B32A32_SFLOAT };
  2534. vd.format = fmtrd[fmt];
  2535. attribute_stride += fmtsize[fmt];
  2536. buffer = s->attribute_buffer;
  2537. } break;
  2538. case RS::ARRAY_BONES: {
  2539. vd.offset = skin_stride;
  2540. vd.format = RD::DATA_FORMAT_R16G16B16A16_UINT;
  2541. skin_stride += sizeof(int16_t) * 4;
  2542. buffer = s->skin_buffer;
  2543. } break;
  2544. case RS::ARRAY_WEIGHTS: {
  2545. vd.offset = skin_stride;
  2546. vd.format = RD::DATA_FORMAT_R16G16B16A16_UNORM;
  2547. skin_stride += sizeof(int16_t) * 4;
  2548. buffer = s->skin_buffer;
  2549. } break;
  2550. }
  2551. }
  2552. if (!(p_input_mask & (1 << i))) {
  2553. continue; // Shader does not need this, skip it (but computing stride was important anyway)
  2554. }
  2555. attributes.push_back(vd);
  2556. buffers.push_back(buffer);
  2557. }
  2558. //update final stride
  2559. for (int i = 0; i < attributes.size(); i++) {
  2560. if (attributes[i].stride == 0) {
  2561. continue; //default location
  2562. }
  2563. int loc = attributes[i].location;
  2564. if (loc < RS::ARRAY_COLOR) {
  2565. attributes.write[i].stride = stride;
  2566. } else if (loc < RS::ARRAY_BONES) {
  2567. attributes.write[i].stride = attribute_stride;
  2568. } else {
  2569. attributes.write[i].stride = skin_stride;
  2570. }
  2571. }
  2572. v.input_mask = p_input_mask;
  2573. v.vertex_format = RD::get_singleton()->vertex_format_create(attributes);
  2574. v.vertex_array = RD::get_singleton()->vertex_array_create(s->vertex_count, v.vertex_format, buffers);
  2575. }
  2576. ////////////////// MULTIMESH
  2577. RID RendererStorageRD::multimesh_create() {
  2578. return multimesh_owner.make_rid(MultiMesh());
  2579. }
  2580. void RendererStorageRD::multimesh_allocate(RID p_multimesh, int p_instances, RS::MultimeshTransformFormat p_transform_format, bool p_use_colors, bool p_use_custom_data) {
  2581. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2582. ERR_FAIL_COND(!multimesh);
  2583. if (multimesh->instances == p_instances && multimesh->xform_format == p_transform_format && multimesh->uses_colors == p_use_colors && multimesh->uses_custom_data == p_use_custom_data) {
  2584. return;
  2585. }
  2586. if (multimesh->buffer.is_valid()) {
  2587. RD::get_singleton()->free(multimesh->buffer);
  2588. multimesh->buffer = RID();
  2589. multimesh->uniform_set_3d = RID(); //cleared by dependency
  2590. }
  2591. if (multimesh->data_cache_dirty_regions) {
  2592. memdelete_arr(multimesh->data_cache_dirty_regions);
  2593. multimesh->data_cache_dirty_regions = nullptr;
  2594. multimesh->data_cache_used_dirty_regions = 0;
  2595. }
  2596. multimesh->instances = p_instances;
  2597. multimesh->xform_format = p_transform_format;
  2598. multimesh->uses_colors = p_use_colors;
  2599. multimesh->color_offset_cache = p_transform_format == RS::MULTIMESH_TRANSFORM_2D ? 8 : 12;
  2600. multimesh->uses_custom_data = p_use_custom_data;
  2601. multimesh->custom_data_offset_cache = multimesh->color_offset_cache + (p_use_colors ? 4 : 0);
  2602. multimesh->stride_cache = multimesh->custom_data_offset_cache + (p_use_custom_data ? 4 : 0);
  2603. multimesh->buffer_set = false;
  2604. //print_line("allocate, elements: " + itos(p_instances) + " 2D: " + itos(p_transform_format == RS::MULTIMESH_TRANSFORM_2D) + " colors " + itos(multimesh->uses_colors) + " data " + itos(multimesh->uses_custom_data) + " stride " + itos(multimesh->stride_cache) + " total size " + itos(multimesh->stride_cache * multimesh->instances));
  2605. multimesh->data_cache = Vector<float>();
  2606. multimesh->aabb = AABB();
  2607. multimesh->aabb_dirty = false;
  2608. multimesh->visible_instances = MIN(multimesh->visible_instances, multimesh->instances);
  2609. if (multimesh->instances) {
  2610. multimesh->buffer = RD::get_singleton()->storage_buffer_create(multimesh->instances * multimesh->stride_cache * 4);
  2611. }
  2612. }
  2613. int RendererStorageRD::multimesh_get_instance_count(RID p_multimesh) const {
  2614. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2615. ERR_FAIL_COND_V(!multimesh, 0);
  2616. return multimesh->instances;
  2617. }
  2618. void RendererStorageRD::multimesh_set_mesh(RID p_multimesh, RID p_mesh) {
  2619. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2620. ERR_FAIL_COND(!multimesh);
  2621. if (multimesh->mesh == p_mesh) {
  2622. return;
  2623. }
  2624. multimesh->mesh = p_mesh;
  2625. if (multimesh->instances == 0) {
  2626. return;
  2627. }
  2628. if (multimesh->data_cache.size()) {
  2629. //we have a data cache, just mark it dirt
  2630. _multimesh_mark_all_dirty(multimesh, false, true);
  2631. } else if (multimesh->instances) {
  2632. //need to re-create AABB unfortunately, calling this has a penalty
  2633. if (multimesh->buffer_set) {
  2634. Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(multimesh->buffer);
  2635. const uint8_t *r = buffer.ptr();
  2636. const float *data = (const float *)r;
  2637. _multimesh_re_create_aabb(multimesh, data, multimesh->instances);
  2638. }
  2639. }
  2640. multimesh->instance_dependency.instance_notify_changed(true, true);
  2641. }
  2642. #define MULTIMESH_DIRTY_REGION_SIZE 512
  2643. void RendererStorageRD::_multimesh_make_local(MultiMesh *multimesh) const {
  2644. if (multimesh->data_cache.size() > 0) {
  2645. return; //already local
  2646. }
  2647. ERR_FAIL_COND(multimesh->data_cache.size() > 0);
  2648. // this means that the user wants to load/save individual elements,
  2649. // for this, the data must reside on CPU, so just copy it there.
  2650. multimesh->data_cache.resize(multimesh->instances * multimesh->stride_cache);
  2651. {
  2652. float *w = multimesh->data_cache.ptrw();
  2653. if (multimesh->buffer_set) {
  2654. Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(multimesh->buffer);
  2655. {
  2656. const uint8_t *r = buffer.ptr();
  2657. copymem(w, r, buffer.size());
  2658. }
  2659. } else {
  2660. zeromem(w, multimesh->instances * multimesh->stride_cache * sizeof(float));
  2661. }
  2662. }
  2663. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2664. multimesh->data_cache_dirty_regions = memnew_arr(bool, data_cache_dirty_region_count);
  2665. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  2666. multimesh->data_cache_dirty_regions[i] = false;
  2667. }
  2668. multimesh->data_cache_used_dirty_regions = 0;
  2669. }
  2670. void RendererStorageRD::_multimesh_mark_dirty(MultiMesh *multimesh, int p_index, bool p_aabb) {
  2671. uint32_t region_index = p_index / MULTIMESH_DIRTY_REGION_SIZE;
  2672. #ifdef DEBUG_ENABLED
  2673. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2674. ERR_FAIL_UNSIGNED_INDEX(region_index, data_cache_dirty_region_count); //bug
  2675. #endif
  2676. if (!multimesh->data_cache_dirty_regions[region_index]) {
  2677. multimesh->data_cache_dirty_regions[region_index] = true;
  2678. multimesh->data_cache_used_dirty_regions++;
  2679. }
  2680. if (p_aabb) {
  2681. multimesh->aabb_dirty = true;
  2682. }
  2683. if (!multimesh->dirty) {
  2684. multimesh->dirty_list = multimesh_dirty_list;
  2685. multimesh_dirty_list = multimesh;
  2686. multimesh->dirty = true;
  2687. }
  2688. }
  2689. void RendererStorageRD::_multimesh_mark_all_dirty(MultiMesh *multimesh, bool p_data, bool p_aabb) {
  2690. if (p_data) {
  2691. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2692. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  2693. if (!multimesh->data_cache_dirty_regions[i]) {
  2694. multimesh->data_cache_dirty_regions[i] = true;
  2695. multimesh->data_cache_used_dirty_regions++;
  2696. }
  2697. }
  2698. }
  2699. if (p_aabb) {
  2700. multimesh->aabb_dirty = true;
  2701. }
  2702. if (!multimesh->dirty) {
  2703. multimesh->dirty_list = multimesh_dirty_list;
  2704. multimesh_dirty_list = multimesh;
  2705. multimesh->dirty = true;
  2706. }
  2707. }
  2708. void RendererStorageRD::_multimesh_re_create_aabb(MultiMesh *multimesh, const float *p_data, int p_instances) {
  2709. ERR_FAIL_COND(multimesh->mesh.is_null());
  2710. AABB aabb;
  2711. AABB mesh_aabb = mesh_get_aabb(multimesh->mesh);
  2712. for (int i = 0; i < p_instances; i++) {
  2713. const float *data = p_data + multimesh->stride_cache * i;
  2714. Transform t;
  2715. if (multimesh->xform_format == RS::MULTIMESH_TRANSFORM_3D) {
  2716. t.basis.elements[0][0] = data[0];
  2717. t.basis.elements[0][1] = data[1];
  2718. t.basis.elements[0][2] = data[2];
  2719. t.origin.x = data[3];
  2720. t.basis.elements[1][0] = data[4];
  2721. t.basis.elements[1][1] = data[5];
  2722. t.basis.elements[1][2] = data[6];
  2723. t.origin.y = data[7];
  2724. t.basis.elements[2][0] = data[8];
  2725. t.basis.elements[2][1] = data[9];
  2726. t.basis.elements[2][2] = data[10];
  2727. t.origin.z = data[11];
  2728. } else {
  2729. t.basis.elements[0].x = data[0];
  2730. t.basis.elements[1].x = data[1];
  2731. t.origin.x = data[3];
  2732. t.basis.elements[0].y = data[4];
  2733. t.basis.elements[1].y = data[5];
  2734. t.origin.y = data[7];
  2735. }
  2736. if (i == 0) {
  2737. aabb = t.xform(mesh_aabb);
  2738. } else {
  2739. aabb.merge_with(t.xform(mesh_aabb));
  2740. }
  2741. }
  2742. multimesh->aabb = aabb;
  2743. }
  2744. void RendererStorageRD::multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform) {
  2745. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2746. ERR_FAIL_COND(!multimesh);
  2747. ERR_FAIL_INDEX(p_index, multimesh->instances);
  2748. ERR_FAIL_COND(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_3D);
  2749. _multimesh_make_local(multimesh);
  2750. {
  2751. float *w = multimesh->data_cache.ptrw();
  2752. float *dataptr = w + p_index * multimesh->stride_cache;
  2753. dataptr[0] = p_transform.basis.elements[0][0];
  2754. dataptr[1] = p_transform.basis.elements[0][1];
  2755. dataptr[2] = p_transform.basis.elements[0][2];
  2756. dataptr[3] = p_transform.origin.x;
  2757. dataptr[4] = p_transform.basis.elements[1][0];
  2758. dataptr[5] = p_transform.basis.elements[1][1];
  2759. dataptr[6] = p_transform.basis.elements[1][2];
  2760. dataptr[7] = p_transform.origin.y;
  2761. dataptr[8] = p_transform.basis.elements[2][0];
  2762. dataptr[9] = p_transform.basis.elements[2][1];
  2763. dataptr[10] = p_transform.basis.elements[2][2];
  2764. dataptr[11] = p_transform.origin.z;
  2765. }
  2766. _multimesh_mark_dirty(multimesh, p_index, true);
  2767. }
  2768. void RendererStorageRD::multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) {
  2769. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2770. ERR_FAIL_COND(!multimesh);
  2771. ERR_FAIL_INDEX(p_index, multimesh->instances);
  2772. ERR_FAIL_COND(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_2D);
  2773. _multimesh_make_local(multimesh);
  2774. {
  2775. float *w = multimesh->data_cache.ptrw();
  2776. float *dataptr = w + p_index * multimesh->stride_cache;
  2777. dataptr[0] = p_transform.elements[0][0];
  2778. dataptr[1] = p_transform.elements[1][0];
  2779. dataptr[2] = 0;
  2780. dataptr[3] = p_transform.elements[2][0];
  2781. dataptr[4] = p_transform.elements[0][1];
  2782. dataptr[5] = p_transform.elements[1][1];
  2783. dataptr[6] = 0;
  2784. dataptr[7] = p_transform.elements[2][1];
  2785. }
  2786. _multimesh_mark_dirty(multimesh, p_index, true);
  2787. }
  2788. void RendererStorageRD::multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) {
  2789. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2790. ERR_FAIL_COND(!multimesh);
  2791. ERR_FAIL_INDEX(p_index, multimesh->instances);
  2792. ERR_FAIL_COND(!multimesh->uses_colors);
  2793. _multimesh_make_local(multimesh);
  2794. {
  2795. float *w = multimesh->data_cache.ptrw();
  2796. float *dataptr = w + p_index * multimesh->stride_cache + multimesh->color_offset_cache;
  2797. dataptr[0] = p_color.r;
  2798. dataptr[1] = p_color.g;
  2799. dataptr[2] = p_color.b;
  2800. dataptr[3] = p_color.a;
  2801. }
  2802. _multimesh_mark_dirty(multimesh, p_index, false);
  2803. }
  2804. void RendererStorageRD::multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) {
  2805. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2806. ERR_FAIL_COND(!multimesh);
  2807. ERR_FAIL_INDEX(p_index, multimesh->instances);
  2808. ERR_FAIL_COND(!multimesh->uses_custom_data);
  2809. _multimesh_make_local(multimesh);
  2810. {
  2811. float *w = multimesh->data_cache.ptrw();
  2812. float *dataptr = w + p_index * multimesh->stride_cache + multimesh->custom_data_offset_cache;
  2813. dataptr[0] = p_color.r;
  2814. dataptr[1] = p_color.g;
  2815. dataptr[2] = p_color.b;
  2816. dataptr[3] = p_color.a;
  2817. }
  2818. _multimesh_mark_dirty(multimesh, p_index, false);
  2819. }
  2820. RID RendererStorageRD::multimesh_get_mesh(RID p_multimesh) const {
  2821. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2822. ERR_FAIL_COND_V(!multimesh, RID());
  2823. return multimesh->mesh;
  2824. }
  2825. Transform RendererStorageRD::multimesh_instance_get_transform(RID p_multimesh, int p_index) const {
  2826. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2827. ERR_FAIL_COND_V(!multimesh, Transform());
  2828. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Transform());
  2829. ERR_FAIL_COND_V(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_3D, Transform());
  2830. _multimesh_make_local(multimesh);
  2831. Transform t;
  2832. {
  2833. const float *r = multimesh->data_cache.ptr();
  2834. const float *dataptr = r + p_index * multimesh->stride_cache;
  2835. t.basis.elements[0][0] = dataptr[0];
  2836. t.basis.elements[0][1] = dataptr[1];
  2837. t.basis.elements[0][2] = dataptr[2];
  2838. t.origin.x = dataptr[3];
  2839. t.basis.elements[1][0] = dataptr[4];
  2840. t.basis.elements[1][1] = dataptr[5];
  2841. t.basis.elements[1][2] = dataptr[6];
  2842. t.origin.y = dataptr[7];
  2843. t.basis.elements[2][0] = dataptr[8];
  2844. t.basis.elements[2][1] = dataptr[9];
  2845. t.basis.elements[2][2] = dataptr[10];
  2846. t.origin.z = dataptr[11];
  2847. }
  2848. return t;
  2849. }
  2850. Transform2D RendererStorageRD::multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const {
  2851. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2852. ERR_FAIL_COND_V(!multimesh, Transform2D());
  2853. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Transform2D());
  2854. ERR_FAIL_COND_V(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_2D, Transform2D());
  2855. _multimesh_make_local(multimesh);
  2856. Transform2D t;
  2857. {
  2858. const float *r = multimesh->data_cache.ptr();
  2859. const float *dataptr = r + p_index * multimesh->stride_cache;
  2860. t.elements[0][0] = dataptr[0];
  2861. t.elements[1][0] = dataptr[1];
  2862. t.elements[2][0] = dataptr[3];
  2863. t.elements[0][1] = dataptr[4];
  2864. t.elements[1][1] = dataptr[5];
  2865. t.elements[2][1] = dataptr[7];
  2866. }
  2867. return t;
  2868. }
  2869. Color RendererStorageRD::multimesh_instance_get_color(RID p_multimesh, int p_index) const {
  2870. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2871. ERR_FAIL_COND_V(!multimesh, Color());
  2872. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Color());
  2873. ERR_FAIL_COND_V(!multimesh->uses_colors, Color());
  2874. _multimesh_make_local(multimesh);
  2875. Color c;
  2876. {
  2877. const float *r = multimesh->data_cache.ptr();
  2878. const float *dataptr = r + p_index * multimesh->stride_cache + multimesh->color_offset_cache;
  2879. c.r = dataptr[0];
  2880. c.g = dataptr[1];
  2881. c.b = dataptr[2];
  2882. c.a = dataptr[3];
  2883. }
  2884. return c;
  2885. }
  2886. Color RendererStorageRD::multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const {
  2887. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2888. ERR_FAIL_COND_V(!multimesh, Color());
  2889. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Color());
  2890. ERR_FAIL_COND_V(!multimesh->uses_custom_data, Color());
  2891. _multimesh_make_local(multimesh);
  2892. Color c;
  2893. {
  2894. const float *r = multimesh->data_cache.ptr();
  2895. const float *dataptr = r + p_index * multimesh->stride_cache + multimesh->custom_data_offset_cache;
  2896. c.r = dataptr[0];
  2897. c.g = dataptr[1];
  2898. c.b = dataptr[2];
  2899. c.a = dataptr[3];
  2900. }
  2901. return c;
  2902. }
  2903. void RendererStorageRD::multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer) {
  2904. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2905. ERR_FAIL_COND(!multimesh);
  2906. ERR_FAIL_COND(p_buffer.size() != (multimesh->instances * (int)multimesh->stride_cache));
  2907. {
  2908. const float *r = p_buffer.ptr();
  2909. RD::get_singleton()->buffer_update(multimesh->buffer, 0, p_buffer.size() * sizeof(float), r, false);
  2910. multimesh->buffer_set = true;
  2911. }
  2912. if (multimesh->data_cache.size()) {
  2913. //if we have a data cache, just update it
  2914. multimesh->data_cache = p_buffer;
  2915. {
  2916. //clear dirty since nothing will be dirty anymore
  2917. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2918. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  2919. multimesh->data_cache_dirty_regions[i] = false;
  2920. }
  2921. multimesh->data_cache_used_dirty_regions = 0;
  2922. }
  2923. _multimesh_mark_all_dirty(multimesh, false, true); //update AABB
  2924. } else if (multimesh->mesh.is_valid()) {
  2925. //if we have a mesh set, we need to re-generate the AABB from the new data
  2926. const float *data = p_buffer.ptr();
  2927. _multimesh_re_create_aabb(multimesh, data, multimesh->instances);
  2928. multimesh->instance_dependency.instance_notify_changed(true, false);
  2929. }
  2930. }
  2931. Vector<float> RendererStorageRD::multimesh_get_buffer(RID p_multimesh) const {
  2932. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2933. ERR_FAIL_COND_V(!multimesh, Vector<float>());
  2934. if (multimesh->buffer.is_null()) {
  2935. return Vector<float>();
  2936. } else if (multimesh->data_cache.size()) {
  2937. return multimesh->data_cache;
  2938. } else {
  2939. //get from memory
  2940. Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(multimesh->buffer);
  2941. Vector<float> ret;
  2942. ret.resize(multimesh->instances * multimesh->stride_cache);
  2943. {
  2944. float *w = ret.ptrw();
  2945. const uint8_t *r = buffer.ptr();
  2946. copymem(w, r, buffer.size());
  2947. }
  2948. return ret;
  2949. }
  2950. }
  2951. void RendererStorageRD::multimesh_set_visible_instances(RID p_multimesh, int p_visible) {
  2952. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2953. ERR_FAIL_COND(!multimesh);
  2954. ERR_FAIL_COND(p_visible < -1 || p_visible > multimesh->instances);
  2955. if (multimesh->visible_instances == p_visible) {
  2956. return;
  2957. }
  2958. if (multimesh->data_cache.size()) {
  2959. //there is a data cache..
  2960. _multimesh_mark_all_dirty(multimesh, false, true);
  2961. }
  2962. multimesh->visible_instances = p_visible;
  2963. }
  2964. int RendererStorageRD::multimesh_get_visible_instances(RID p_multimesh) const {
  2965. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2966. ERR_FAIL_COND_V(!multimesh, 0);
  2967. return multimesh->visible_instances;
  2968. }
  2969. AABB RendererStorageRD::multimesh_get_aabb(RID p_multimesh) const {
  2970. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2971. ERR_FAIL_COND_V(!multimesh, AABB());
  2972. if (multimesh->aabb_dirty) {
  2973. const_cast<RendererStorageRD *>(this)->_update_dirty_multimeshes();
  2974. }
  2975. return multimesh->aabb;
  2976. }
  2977. void RendererStorageRD::_update_dirty_multimeshes() {
  2978. while (multimesh_dirty_list) {
  2979. MultiMesh *multimesh = multimesh_dirty_list;
  2980. if (multimesh->data_cache.size()) { //may have been cleared, so only process if it exists
  2981. const float *data = multimesh->data_cache.ptr();
  2982. uint32_t visible_instances = multimesh->visible_instances >= 0 ? multimesh->visible_instances : multimesh->instances;
  2983. if (multimesh->data_cache_used_dirty_regions) {
  2984. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2985. uint32_t visible_region_count = (visible_instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2986. uint32_t region_size = multimesh->stride_cache * MULTIMESH_DIRTY_REGION_SIZE * sizeof(float);
  2987. if (multimesh->data_cache_used_dirty_regions > 32 || multimesh->data_cache_used_dirty_regions > visible_region_count / 2) {
  2988. //if there too many dirty regions, or represent the majority of regions, just copy all, else transfer cost piles up too much
  2989. RD::get_singleton()->buffer_update(multimesh->buffer, 0, MIN(visible_region_count * region_size, multimesh->instances * multimesh->stride_cache * sizeof(float)), data, false);
  2990. } else {
  2991. //not that many regions? update them all
  2992. for (uint32_t i = 0; i < visible_region_count; i++) {
  2993. if (multimesh->data_cache_dirty_regions[i]) {
  2994. uint64_t offset = i * region_size;
  2995. uint64_t size = multimesh->stride_cache * multimesh->instances * sizeof(float);
  2996. RD::get_singleton()->buffer_update(multimesh->buffer, offset, MIN(region_size, size - offset), &data[i * region_size], false);
  2997. }
  2998. }
  2999. }
  3000. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  3001. multimesh->data_cache_dirty_regions[i] = false;
  3002. }
  3003. multimesh->data_cache_used_dirty_regions = 0;
  3004. }
  3005. if (multimesh->aabb_dirty) {
  3006. //aabb is dirty..
  3007. _multimesh_re_create_aabb(multimesh, data, visible_instances);
  3008. multimesh->aabb_dirty = false;
  3009. multimesh->instance_dependency.instance_notify_changed(true, false);
  3010. }
  3011. }
  3012. multimesh_dirty_list = multimesh->dirty_list;
  3013. multimesh->dirty_list = nullptr;
  3014. multimesh->dirty = false;
  3015. }
  3016. multimesh_dirty_list = nullptr;
  3017. }
  3018. /* PARTICLES */
  3019. RID RendererStorageRD::particles_create() {
  3020. return particles_owner.make_rid(Particles());
  3021. }
  3022. void RendererStorageRD::particles_set_emitting(RID p_particles, bool p_emitting) {
  3023. Particles *particles = particles_owner.getornull(p_particles);
  3024. ERR_FAIL_COND(!particles);
  3025. particles->emitting = p_emitting;
  3026. }
  3027. bool RendererStorageRD::particles_get_emitting(RID p_particles) {
  3028. Particles *particles = particles_owner.getornull(p_particles);
  3029. ERR_FAIL_COND_V(!particles, false);
  3030. return particles->emitting;
  3031. }
  3032. void RendererStorageRD::_particles_free_data(Particles *particles) {
  3033. if (!particles->particle_buffer.is_valid()) {
  3034. return;
  3035. }
  3036. RD::get_singleton()->free(particles->particle_buffer);
  3037. RD::get_singleton()->free(particles->frame_params_buffer);
  3038. RD::get_singleton()->free(particles->particle_instance_buffer);
  3039. particles->particles_transforms_buffer_uniform_set = RID();
  3040. particles->particle_buffer = RID();
  3041. if (RD::get_singleton()->uniform_set_is_valid(particles->collision_textures_uniform_set)) {
  3042. RD::get_singleton()->free(particles->collision_textures_uniform_set);
  3043. }
  3044. if (particles->particles_sort_buffer.is_valid()) {
  3045. RD::get_singleton()->free(particles->particles_sort_buffer);
  3046. particles->particles_sort_buffer = RID();
  3047. }
  3048. if (particles->emission_buffer != nullptr) {
  3049. particles->emission_buffer = nullptr;
  3050. particles->emission_buffer_data.clear();
  3051. RD::get_singleton()->free(particles->emission_storage_buffer);
  3052. particles->emission_storage_buffer = RID();
  3053. }
  3054. }
  3055. void RendererStorageRD::particles_set_amount(RID p_particles, int p_amount) {
  3056. Particles *particles = particles_owner.getornull(p_particles);
  3057. ERR_FAIL_COND(!particles);
  3058. if (particles->amount == p_amount) {
  3059. return;
  3060. }
  3061. _particles_free_data(particles);
  3062. particles->amount = p_amount;
  3063. if (particles->amount > 0) {
  3064. particles->particle_buffer = RD::get_singleton()->storage_buffer_create(sizeof(ParticleData) * p_amount);
  3065. particles->frame_params_buffer = RD::get_singleton()->storage_buffer_create(sizeof(ParticlesFrameParams) * 1);
  3066. particles->particle_instance_buffer = RD::get_singleton()->storage_buffer_create(sizeof(float) * 4 * (3 + 1 + 1) * p_amount);
  3067. //needs to clear it
  3068. {
  3069. Vector<RD::Uniform> uniforms;
  3070. {
  3071. RD::Uniform u;
  3072. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3073. u.binding = 1;
  3074. u.ids.push_back(particles->particle_buffer);
  3075. uniforms.push_back(u);
  3076. }
  3077. {
  3078. RD::Uniform u;
  3079. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3080. u.binding = 2;
  3081. u.ids.push_back(particles->particle_instance_buffer);
  3082. uniforms.push_back(u);
  3083. }
  3084. particles->particles_copy_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.copy_shader.version_get_shader(particles_shader.copy_shader_version, 0), 0);
  3085. }
  3086. }
  3087. particles->prev_ticks = 0;
  3088. particles->phase = 0;
  3089. particles->prev_phase = 0;
  3090. particles->clear = true;
  3091. }
  3092. void RendererStorageRD::particles_set_lifetime(RID p_particles, float p_lifetime) {
  3093. Particles *particles = particles_owner.getornull(p_particles);
  3094. ERR_FAIL_COND(!particles);
  3095. particles->lifetime = p_lifetime;
  3096. }
  3097. void RendererStorageRD::particles_set_one_shot(RID p_particles, bool p_one_shot) {
  3098. Particles *particles = particles_owner.getornull(p_particles);
  3099. ERR_FAIL_COND(!particles);
  3100. particles->one_shot = p_one_shot;
  3101. }
  3102. void RendererStorageRD::particles_set_pre_process_time(RID p_particles, float p_time) {
  3103. Particles *particles = particles_owner.getornull(p_particles);
  3104. ERR_FAIL_COND(!particles);
  3105. particles->pre_process_time = p_time;
  3106. }
  3107. void RendererStorageRD::particles_set_explosiveness_ratio(RID p_particles, float p_ratio) {
  3108. Particles *particles = particles_owner.getornull(p_particles);
  3109. ERR_FAIL_COND(!particles);
  3110. particles->explosiveness = p_ratio;
  3111. }
  3112. void RendererStorageRD::particles_set_randomness_ratio(RID p_particles, float p_ratio) {
  3113. Particles *particles = particles_owner.getornull(p_particles);
  3114. ERR_FAIL_COND(!particles);
  3115. particles->randomness = p_ratio;
  3116. }
  3117. void RendererStorageRD::particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) {
  3118. Particles *particles = particles_owner.getornull(p_particles);
  3119. ERR_FAIL_COND(!particles);
  3120. particles->custom_aabb = p_aabb;
  3121. particles->instance_dependency.instance_notify_changed(true, false);
  3122. }
  3123. void RendererStorageRD::particles_set_speed_scale(RID p_particles, float p_scale) {
  3124. Particles *particles = particles_owner.getornull(p_particles);
  3125. ERR_FAIL_COND(!particles);
  3126. particles->speed_scale = p_scale;
  3127. }
  3128. void RendererStorageRD::particles_set_use_local_coordinates(RID p_particles, bool p_enable) {
  3129. Particles *particles = particles_owner.getornull(p_particles);
  3130. ERR_FAIL_COND(!particles);
  3131. particles->use_local_coords = p_enable;
  3132. }
  3133. void RendererStorageRD::particles_set_fixed_fps(RID p_particles, int p_fps) {
  3134. Particles *particles = particles_owner.getornull(p_particles);
  3135. ERR_FAIL_COND(!particles);
  3136. particles->fixed_fps = p_fps;
  3137. }
  3138. void RendererStorageRD::particles_set_fractional_delta(RID p_particles, bool p_enable) {
  3139. Particles *particles = particles_owner.getornull(p_particles);
  3140. ERR_FAIL_COND(!particles);
  3141. particles->fractional_delta = p_enable;
  3142. }
  3143. void RendererStorageRD::particles_set_collision_base_size(RID p_particles, float p_size) {
  3144. Particles *particles = particles_owner.getornull(p_particles);
  3145. ERR_FAIL_COND(!particles);
  3146. particles->collision_base_size = p_size;
  3147. }
  3148. void RendererStorageRD::particles_set_process_material(RID p_particles, RID p_material) {
  3149. Particles *particles = particles_owner.getornull(p_particles);
  3150. ERR_FAIL_COND(!particles);
  3151. particles->process_material = p_material;
  3152. }
  3153. void RendererStorageRD::particles_set_draw_order(RID p_particles, RS::ParticlesDrawOrder p_order) {
  3154. Particles *particles = particles_owner.getornull(p_particles);
  3155. ERR_FAIL_COND(!particles);
  3156. particles->draw_order = p_order;
  3157. }
  3158. void RendererStorageRD::particles_set_draw_passes(RID p_particles, int p_passes) {
  3159. Particles *particles = particles_owner.getornull(p_particles);
  3160. ERR_FAIL_COND(!particles);
  3161. particles->draw_passes.resize(p_passes);
  3162. }
  3163. void RendererStorageRD::particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) {
  3164. Particles *particles = particles_owner.getornull(p_particles);
  3165. ERR_FAIL_COND(!particles);
  3166. ERR_FAIL_INDEX(p_pass, particles->draw_passes.size());
  3167. particles->draw_passes.write[p_pass] = p_mesh;
  3168. }
  3169. void RendererStorageRD::particles_restart(RID p_particles) {
  3170. Particles *particles = particles_owner.getornull(p_particles);
  3171. ERR_FAIL_COND(!particles);
  3172. particles->restart_request = true;
  3173. }
  3174. void RendererStorageRD::_particles_allocate_emission_buffer(Particles *particles) {
  3175. ERR_FAIL_COND(particles->emission_buffer != nullptr);
  3176. particles->emission_buffer_data.resize(sizeof(ParticleEmissionBuffer::Data) * particles->amount + sizeof(uint32_t) * 4);
  3177. zeromem(particles->emission_buffer_data.ptrw(), particles->emission_buffer_data.size());
  3178. particles->emission_buffer = (ParticleEmissionBuffer *)particles->emission_buffer_data.ptrw();
  3179. particles->emission_buffer->particle_max = particles->amount;
  3180. particles->emission_storage_buffer = RD::get_singleton()->storage_buffer_create(particles->emission_buffer_data.size(), particles->emission_buffer_data);
  3181. if (RD::get_singleton()->uniform_set_is_valid(particles->particles_material_uniform_set)) {
  3182. //will need to be re-created
  3183. RD::get_singleton()->free(particles->particles_material_uniform_set);
  3184. particles->particles_material_uniform_set = RID();
  3185. }
  3186. }
  3187. void RendererStorageRD::particles_set_subemitter(RID p_particles, RID p_subemitter_particles) {
  3188. Particles *particles = particles_owner.getornull(p_particles);
  3189. ERR_FAIL_COND(!particles);
  3190. ERR_FAIL_COND(p_particles == p_subemitter_particles);
  3191. particles->sub_emitter = p_subemitter_particles;
  3192. if (RD::get_singleton()->uniform_set_is_valid(particles->particles_material_uniform_set)) {
  3193. RD::get_singleton()->free(particles->particles_material_uniform_set);
  3194. particles->particles_material_uniform_set = RID(); //clear and force to re create sub emitting
  3195. }
  3196. }
  3197. void RendererStorageRD::particles_emit(RID p_particles, const Transform &p_transform, const Vector3 &p_velocity, const Color &p_color, const Color &p_custom, uint32_t p_emit_flags) {
  3198. Particles *particles = particles_owner.getornull(p_particles);
  3199. ERR_FAIL_COND(!particles);
  3200. ERR_FAIL_COND(particles->amount == 0);
  3201. if (particles->emitting) {
  3202. particles->clear = true;
  3203. particles->emitting = false;
  3204. }
  3205. if (particles->emission_buffer == nullptr) {
  3206. _particles_allocate_emission_buffer(particles);
  3207. }
  3208. if (particles->inactive) {
  3209. //in case it was inactive, make active again
  3210. particles->inactive = false;
  3211. particles->inactive_time = 0;
  3212. }
  3213. int32_t idx = particles->emission_buffer->particle_count;
  3214. if (idx < particles->emission_buffer->particle_max) {
  3215. store_transform(p_transform, particles->emission_buffer->data[idx].xform);
  3216. particles->emission_buffer->data[idx].velocity[0] = p_velocity.x;
  3217. particles->emission_buffer->data[idx].velocity[1] = p_velocity.y;
  3218. particles->emission_buffer->data[idx].velocity[2] = p_velocity.z;
  3219. particles->emission_buffer->data[idx].custom[0] = p_custom.r;
  3220. particles->emission_buffer->data[idx].custom[1] = p_custom.g;
  3221. particles->emission_buffer->data[idx].custom[2] = p_custom.b;
  3222. particles->emission_buffer->data[idx].custom[3] = p_custom.a;
  3223. particles->emission_buffer->data[idx].color[0] = p_color.r;
  3224. particles->emission_buffer->data[idx].color[1] = p_color.g;
  3225. particles->emission_buffer->data[idx].color[2] = p_color.b;
  3226. particles->emission_buffer->data[idx].color[3] = p_color.a;
  3227. particles->emission_buffer->data[idx].flags = p_emit_flags;
  3228. particles->emission_buffer->particle_count++;
  3229. }
  3230. }
  3231. void RendererStorageRD::particles_request_process(RID p_particles) {
  3232. Particles *particles = particles_owner.getornull(p_particles);
  3233. ERR_FAIL_COND(!particles);
  3234. if (!particles->dirty) {
  3235. particles->dirty = true;
  3236. particles->update_list = particle_update_list;
  3237. particle_update_list = particles;
  3238. }
  3239. }
  3240. AABB RendererStorageRD::particles_get_current_aabb(RID p_particles) {
  3241. const Particles *particles = particles_owner.getornull(p_particles);
  3242. ERR_FAIL_COND_V(!particles, AABB());
  3243. Vector<ParticleData> data;
  3244. data.resize(particles->amount);
  3245. Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(particles->particle_buffer);
  3246. Transform inv = particles->emission_transform.affine_inverse();
  3247. AABB aabb;
  3248. if (buffer.size()) {
  3249. bool first = true;
  3250. const ParticleData *particle_data = (const ParticleData *)data.ptr();
  3251. for (int i = 0; i < particles->amount; i++) {
  3252. if (particle_data[i].active) {
  3253. Vector3 pos = Vector3(particle_data[i].xform[12], particle_data[i].xform[13], particle_data[i].xform[14]);
  3254. if (!particles->use_local_coords) {
  3255. pos = inv.xform(pos);
  3256. }
  3257. if (first) {
  3258. aabb.position = pos;
  3259. first = false;
  3260. } else {
  3261. aabb.expand_to(pos);
  3262. }
  3263. }
  3264. }
  3265. }
  3266. float longest_axis_size = 0;
  3267. for (int i = 0; i < particles->draw_passes.size(); i++) {
  3268. if (particles->draw_passes[i].is_valid()) {
  3269. AABB maabb = mesh_get_aabb(particles->draw_passes[i], RID());
  3270. longest_axis_size = MAX(maabb.get_longest_axis_size(), longest_axis_size);
  3271. }
  3272. }
  3273. aabb.grow_by(longest_axis_size);
  3274. return aabb;
  3275. }
  3276. AABB RendererStorageRD::particles_get_aabb(RID p_particles) const {
  3277. const Particles *particles = particles_owner.getornull(p_particles);
  3278. ERR_FAIL_COND_V(!particles, AABB());
  3279. return particles->custom_aabb;
  3280. }
  3281. void RendererStorageRD::particles_set_emission_transform(RID p_particles, const Transform &p_transform) {
  3282. Particles *particles = particles_owner.getornull(p_particles);
  3283. ERR_FAIL_COND(!particles);
  3284. particles->emission_transform = p_transform;
  3285. }
  3286. int RendererStorageRD::particles_get_draw_passes(RID p_particles) const {
  3287. const Particles *particles = particles_owner.getornull(p_particles);
  3288. ERR_FAIL_COND_V(!particles, 0);
  3289. return particles->draw_passes.size();
  3290. }
  3291. RID RendererStorageRD::particles_get_draw_pass_mesh(RID p_particles, int p_pass) const {
  3292. const Particles *particles = particles_owner.getornull(p_particles);
  3293. ERR_FAIL_COND_V(!particles, RID());
  3294. ERR_FAIL_INDEX_V(p_pass, particles->draw_passes.size(), RID());
  3295. return particles->draw_passes[p_pass];
  3296. }
  3297. void RendererStorageRD::particles_add_collision(RID p_particles, InstanceBaseDependency *p_instance) {
  3298. RendererSceneRender::InstanceBase *instance = static_cast<RendererSceneRender::InstanceBase *>(p_instance);
  3299. Particles *particles = particles_owner.getornull(p_particles);
  3300. ERR_FAIL_COND(!particles);
  3301. ERR_FAIL_COND(instance->base_type != RS::INSTANCE_PARTICLES_COLLISION);
  3302. particles->collisions.insert(instance);
  3303. }
  3304. void RendererStorageRD::particles_remove_collision(RID p_particles, InstanceBaseDependency *p_instance) {
  3305. RendererSceneRender::InstanceBase *instance = static_cast<RendererSceneRender::InstanceBase *>(p_instance);
  3306. Particles *particles = particles_owner.getornull(p_particles);
  3307. ERR_FAIL_COND(!particles);
  3308. particles->collisions.erase(instance);
  3309. }
  3310. void RendererStorageRD::_particles_process(Particles *p_particles, float p_delta) {
  3311. if (p_particles->particles_material_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(p_particles->particles_material_uniform_set)) {
  3312. Vector<RD::Uniform> uniforms;
  3313. {
  3314. RD::Uniform u;
  3315. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3316. u.binding = 0;
  3317. u.ids.push_back(p_particles->frame_params_buffer);
  3318. uniforms.push_back(u);
  3319. }
  3320. {
  3321. RD::Uniform u;
  3322. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3323. u.binding = 1;
  3324. u.ids.push_back(p_particles->particle_buffer);
  3325. uniforms.push_back(u);
  3326. }
  3327. {
  3328. RD::Uniform u;
  3329. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3330. u.binding = 2;
  3331. if (p_particles->emission_storage_buffer.is_valid()) {
  3332. u.ids.push_back(p_particles->emission_storage_buffer);
  3333. } else {
  3334. u.ids.push_back(default_rd_storage_buffer);
  3335. }
  3336. uniforms.push_back(u);
  3337. }
  3338. {
  3339. RD::Uniform u;
  3340. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3341. u.binding = 3;
  3342. Particles *sub_emitter = particles_owner.getornull(p_particles->sub_emitter);
  3343. if (sub_emitter) {
  3344. if (sub_emitter->emission_buffer == nullptr) { //no emission buffer, allocate emission buffer
  3345. _particles_allocate_emission_buffer(sub_emitter);
  3346. }
  3347. u.ids.push_back(sub_emitter->emission_storage_buffer);
  3348. } else {
  3349. u.ids.push_back(default_rd_storage_buffer);
  3350. }
  3351. uniforms.push_back(u);
  3352. }
  3353. p_particles->particles_material_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, 1);
  3354. }
  3355. float new_phase = Math::fmod((float)p_particles->phase + (p_delta / p_particles->lifetime) * p_particles->speed_scale, (float)1.0);
  3356. ParticlesFrameParams &frame_params = p_particles->frame_params;
  3357. if (p_particles->clear) {
  3358. p_particles->cycle_number = 0;
  3359. p_particles->random_seed = Math::rand();
  3360. } else if (new_phase < p_particles->phase) {
  3361. if (p_particles->one_shot) {
  3362. p_particles->emitting = false;
  3363. }
  3364. p_particles->cycle_number++;
  3365. }
  3366. frame_params.emitting = p_particles->emitting;
  3367. frame_params.system_phase = new_phase;
  3368. frame_params.prev_system_phase = p_particles->phase;
  3369. p_particles->phase = new_phase;
  3370. frame_params.time = RendererCompositorRD::singleton->get_total_time();
  3371. frame_params.delta = p_delta * p_particles->speed_scale;
  3372. frame_params.random_seed = p_particles->random_seed;
  3373. frame_params.explosiveness = p_particles->explosiveness;
  3374. frame_params.randomness = p_particles->randomness;
  3375. if (p_particles->use_local_coords) {
  3376. store_transform(Transform(), frame_params.emission_transform);
  3377. } else {
  3378. store_transform(p_particles->emission_transform, frame_params.emission_transform);
  3379. }
  3380. frame_params.cycle = p_particles->cycle_number;
  3381. { //collision and attractors
  3382. frame_params.collider_count = 0;
  3383. frame_params.attractor_count = 0;
  3384. frame_params.particle_size = p_particles->collision_base_size;
  3385. RID collision_3d_textures[ParticlesFrameParams::MAX_3D_TEXTURES];
  3386. RID collision_heightmap_texture;
  3387. Transform to_particles;
  3388. if (p_particles->use_local_coords) {
  3389. to_particles = p_particles->emission_transform.affine_inverse();
  3390. }
  3391. uint32_t collision_3d_textures_used = 0;
  3392. for (const Set<RendererSceneRender::InstanceBase *>::Element *E = p_particles->collisions.front(); E; E = E->next()) {
  3393. ParticlesCollision *pc = particles_collision_owner.getornull(E->get()->base);
  3394. Transform to_collider = E->get()->transform;
  3395. if (p_particles->use_local_coords) {
  3396. to_collider = to_particles * to_collider;
  3397. }
  3398. Vector3 scale = to_collider.basis.get_scale();
  3399. to_collider.basis.orthonormalize();
  3400. if (pc->type <= RS::PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT) {
  3401. //attractor
  3402. if (frame_params.attractor_count >= ParticlesFrameParams::MAX_ATTRACTORS) {
  3403. continue;
  3404. }
  3405. ParticlesFrameParams::Attractor &attr = frame_params.attractors[frame_params.attractor_count];
  3406. store_transform(to_collider, attr.transform);
  3407. attr.strength = pc->attractor_strength;
  3408. attr.attenuation = pc->attractor_attenuation;
  3409. attr.directionality = pc->attractor_directionality;
  3410. switch (pc->type) {
  3411. case RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT: {
  3412. attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_SPHERE;
  3413. float radius = pc->radius;
  3414. radius *= (scale.x + scale.y + scale.z) / 3.0;
  3415. attr.extents[0] = radius;
  3416. attr.extents[1] = radius;
  3417. attr.extents[2] = radius;
  3418. } break;
  3419. case RS::PARTICLES_COLLISION_TYPE_BOX_ATTRACT: {
  3420. attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_BOX;
  3421. Vector3 extents = pc->extents * scale;
  3422. attr.extents[0] = extents.x;
  3423. attr.extents[1] = extents.y;
  3424. attr.extents[2] = extents.z;
  3425. } break;
  3426. case RS::PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT: {
  3427. if (collision_3d_textures_used >= ParticlesFrameParams::MAX_3D_TEXTURES) {
  3428. continue;
  3429. }
  3430. attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_VECTOR_FIELD;
  3431. Vector3 extents = pc->extents * scale;
  3432. attr.extents[0] = extents.x;
  3433. attr.extents[1] = extents.y;
  3434. attr.extents[2] = extents.z;
  3435. attr.texture_index = collision_3d_textures_used;
  3436. collision_3d_textures[collision_3d_textures_used] = pc->field_texture;
  3437. collision_3d_textures_used++;
  3438. } break;
  3439. default: {
  3440. }
  3441. }
  3442. frame_params.attractor_count++;
  3443. } else {
  3444. //collider
  3445. if (frame_params.collider_count >= ParticlesFrameParams::MAX_COLLIDERS) {
  3446. continue;
  3447. }
  3448. ParticlesFrameParams::Collider &col = frame_params.colliders[frame_params.collider_count];
  3449. store_transform(to_collider, col.transform);
  3450. switch (pc->type) {
  3451. case RS::PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE: {
  3452. col.type = ParticlesFrameParams::COLLISION_TYPE_SPHERE;
  3453. float radius = pc->radius;
  3454. radius *= (scale.x + scale.y + scale.z) / 3.0;
  3455. col.extents[0] = radius;
  3456. col.extents[1] = radius;
  3457. col.extents[2] = radius;
  3458. } break;
  3459. case RS::PARTICLES_COLLISION_TYPE_BOX_COLLIDE: {
  3460. col.type = ParticlesFrameParams::COLLISION_TYPE_BOX;
  3461. Vector3 extents = pc->extents * scale;
  3462. col.extents[0] = extents.x;
  3463. col.extents[1] = extents.y;
  3464. col.extents[2] = extents.z;
  3465. } break;
  3466. case RS::PARTICLES_COLLISION_TYPE_SDF_COLLIDE: {
  3467. if (collision_3d_textures_used >= ParticlesFrameParams::MAX_3D_TEXTURES) {
  3468. continue;
  3469. }
  3470. col.type = ParticlesFrameParams::COLLISION_TYPE_SDF;
  3471. Vector3 extents = pc->extents * scale;
  3472. col.extents[0] = extents.x;
  3473. col.extents[1] = extents.y;
  3474. col.extents[2] = extents.z;
  3475. col.texture_index = collision_3d_textures_used;
  3476. col.scale = (scale.x + scale.y + scale.z) * 0.333333333333; //non uniform scale non supported
  3477. collision_3d_textures[collision_3d_textures_used] = pc->field_texture;
  3478. collision_3d_textures_used++;
  3479. } break;
  3480. case RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE: {
  3481. if (collision_heightmap_texture != RID()) { //already taken
  3482. continue;
  3483. }
  3484. col.type = ParticlesFrameParams::COLLISION_TYPE_HEIGHT_FIELD;
  3485. Vector3 extents = pc->extents * scale;
  3486. col.extents[0] = extents.x;
  3487. col.extents[1] = extents.y;
  3488. col.extents[2] = extents.z;
  3489. collision_heightmap_texture = pc->heightfield_texture;
  3490. } break;
  3491. default: {
  3492. }
  3493. }
  3494. frame_params.collider_count++;
  3495. }
  3496. }
  3497. bool different = false;
  3498. if (collision_3d_textures_used == p_particles->collision_3d_textures_used) {
  3499. for (int i = 0; i < ParticlesFrameParams::MAX_3D_TEXTURES; i++) {
  3500. if (p_particles->collision_3d_textures[i] != collision_3d_textures[i]) {
  3501. different = true;
  3502. break;
  3503. }
  3504. }
  3505. }
  3506. if (collision_heightmap_texture != p_particles->collision_heightmap_texture) {
  3507. different = true;
  3508. }
  3509. bool uniform_set_valid = RD::get_singleton()->uniform_set_is_valid(p_particles->collision_textures_uniform_set);
  3510. if (different || !uniform_set_valid) {
  3511. if (uniform_set_valid) {
  3512. RD::get_singleton()->free(p_particles->collision_textures_uniform_set);
  3513. }
  3514. Vector<RD::Uniform> uniforms;
  3515. {
  3516. RD::Uniform u;
  3517. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  3518. u.binding = 0;
  3519. for (uint32_t i = 0; i < ParticlesFrameParams::MAX_3D_TEXTURES; i++) {
  3520. RID rd_tex;
  3521. if (i < collision_3d_textures_used) {
  3522. Texture *t = texture_owner.getornull(collision_3d_textures[i]);
  3523. if (t && t->type == Texture::TYPE_3D) {
  3524. rd_tex = t->rd_texture;
  3525. }
  3526. }
  3527. if (rd_tex == RID()) {
  3528. rd_tex = default_rd_textures[DEFAULT_RD_TEXTURE_3D_WHITE];
  3529. }
  3530. u.ids.push_back(rd_tex);
  3531. }
  3532. uniforms.push_back(u);
  3533. }
  3534. {
  3535. RD::Uniform u;
  3536. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  3537. u.binding = 1;
  3538. if (collision_heightmap_texture.is_valid()) {
  3539. u.ids.push_back(collision_heightmap_texture);
  3540. } else {
  3541. u.ids.push_back(default_rd_textures[DEFAULT_RD_TEXTURE_BLACK]);
  3542. }
  3543. uniforms.push_back(u);
  3544. }
  3545. p_particles->collision_textures_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, 2);
  3546. }
  3547. }
  3548. ParticlesShader::PushConstant push_constant;
  3549. push_constant.clear = p_particles->clear;
  3550. push_constant.total_particles = p_particles->amount;
  3551. push_constant.lifetime = p_particles->lifetime;
  3552. push_constant.trail_size = 1;
  3553. push_constant.use_fractional_delta = p_particles->fractional_delta;
  3554. push_constant.sub_emitter_mode = !p_particles->emitting && p_particles->emission_buffer && (p_particles->emission_buffer->particle_count > 0 || p_particles->force_sub_emit);
  3555. p_particles->force_sub_emit = false; //reset
  3556. Particles *sub_emitter = particles_owner.getornull(p_particles->sub_emitter);
  3557. if (sub_emitter && sub_emitter->emission_storage_buffer.is_valid()) {
  3558. // print_line("updating subemitter buffer");
  3559. int32_t zero[4] = { 0, sub_emitter->amount, 0, 0 };
  3560. RD::get_singleton()->buffer_update(sub_emitter->emission_storage_buffer, 0, sizeof(uint32_t) * 4, zero, true);
  3561. push_constant.can_emit = true;
  3562. if (sub_emitter->emitting) {
  3563. sub_emitter->emitting = false;
  3564. sub_emitter->clear = true; //will need to clear if it was emitting, sorry
  3565. }
  3566. //make sure the sub emitter processes particles too
  3567. sub_emitter->inactive = false;
  3568. sub_emitter->inactive_time = 0;
  3569. sub_emitter->force_sub_emit = true;
  3570. } else {
  3571. push_constant.can_emit = false;
  3572. }
  3573. if (p_particles->emission_buffer && p_particles->emission_buffer->particle_count) {
  3574. RD::get_singleton()->buffer_update(p_particles->emission_storage_buffer, 0, sizeof(uint32_t) * 4 + sizeof(ParticleEmissionBuffer::Data) * p_particles->emission_buffer->particle_count, p_particles->emission_buffer, true);
  3575. p_particles->emission_buffer->particle_count = 0;
  3576. }
  3577. p_particles->clear = false;
  3578. RD::get_singleton()->buffer_update(p_particles->frame_params_buffer, 0, sizeof(ParticlesFrameParams), &frame_params, true);
  3579. ParticlesMaterialData *m = (ParticlesMaterialData *)material_get_data(p_particles->process_material, SHADER_TYPE_PARTICLES);
  3580. if (!m) {
  3581. m = (ParticlesMaterialData *)material_get_data(particles_shader.default_material, SHADER_TYPE_PARTICLES);
  3582. }
  3583. ERR_FAIL_COND(!m);
  3584. //todo should maybe compute all particle systems together?
  3585. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  3586. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, m->shader_data->pipeline);
  3587. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles_shader.base_uniform_set, 0);
  3588. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_particles->particles_material_uniform_set, 1);
  3589. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_particles->collision_textures_uniform_set, 2);
  3590. if (m->uniform_set.is_valid()) {
  3591. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, m->uniform_set, 3);
  3592. }
  3593. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ParticlesShader::PushConstant));
  3594. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_particles->amount, 1, 1, 64, 1, 1);
  3595. RD::get_singleton()->compute_list_end();
  3596. }
  3597. void RendererStorageRD::particles_set_view_axis(RID p_particles, const Vector3 &p_axis) {
  3598. Particles *particles = particles_owner.getornull(p_particles);
  3599. ERR_FAIL_COND(!particles);
  3600. if (particles->draw_order != RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH) {
  3601. return; //uninteresting for other modes
  3602. }
  3603. //copy to sort buffer
  3604. if (particles->particles_sort_buffer == RID()) {
  3605. uint32_t size = particles->amount;
  3606. if (size & 1) {
  3607. size++; //make multiple of 16
  3608. }
  3609. size *= sizeof(float) * 2;
  3610. particles->particles_sort_buffer = RD::get_singleton()->storage_buffer_create(size);
  3611. {
  3612. Vector<RD::Uniform> uniforms;
  3613. {
  3614. RD::Uniform u;
  3615. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3616. u.binding = 0;
  3617. u.ids.push_back(particles->particles_sort_buffer);
  3618. uniforms.push_back(u);
  3619. }
  3620. particles->particles_sort_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.copy_shader.version_get_shader(particles_shader.copy_shader_version, ParticlesShader::COPY_MODE_FILL_SORT_BUFFER), 1);
  3621. }
  3622. }
  3623. Vector3 axis = -p_axis; // cameras look to z negative
  3624. if (particles->use_local_coords) {
  3625. axis = particles->emission_transform.basis.xform_inv(axis).normalized();
  3626. }
  3627. ParticlesShader::CopyPushConstant copy_push_constant;
  3628. copy_push_constant.total_particles = particles->amount;
  3629. copy_push_constant.sort_direction[0] = axis.x;
  3630. copy_push_constant.sort_direction[1] = axis.y;
  3631. copy_push_constant.sort_direction[2] = axis.z;
  3632. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  3633. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[ParticlesShader::COPY_MODE_FILL_SORT_BUFFER]);
  3634. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0);
  3635. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_sort_uniform_set, 1);
  3636. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy_push_constant, sizeof(ParticlesShader::CopyPushConstant));
  3637. RD::get_singleton()->compute_list_dispatch_threads(compute_list, particles->amount, 1, 1, 64, 1, 1);
  3638. RD::get_singleton()->compute_list_end();
  3639. effects.sort_buffer(particles->particles_sort_uniform_set, particles->amount);
  3640. compute_list = RD::get_singleton()->compute_list_begin();
  3641. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[ParticlesShader::COPY_MODE_FILL_INSTANCES_WITH_SORT_BUFFER]);
  3642. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0);
  3643. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_sort_uniform_set, 1);
  3644. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy_push_constant, sizeof(ParticlesShader::CopyPushConstant));
  3645. RD::get_singleton()->compute_list_dispatch_threads(compute_list, particles->amount, 1, 1, 64, 1, 1);
  3646. RD::get_singleton()->compute_list_end();
  3647. }
  3648. void RendererStorageRD::update_particles() {
  3649. while (particle_update_list) {
  3650. //use transform feedback to process particles
  3651. Particles *particles = particle_update_list;
  3652. //take and remove
  3653. particle_update_list = particles->update_list;
  3654. particles->update_list = nullptr;
  3655. particles->dirty = false;
  3656. if (particles->restart_request) {
  3657. particles->prev_ticks = 0;
  3658. particles->phase = 0;
  3659. particles->prev_phase = 0;
  3660. particles->clear = true;
  3661. particles->restart_request = false;
  3662. }
  3663. if (particles->inactive && !particles->emitting) {
  3664. //go next
  3665. continue;
  3666. }
  3667. if (particles->emitting) {
  3668. if (particles->inactive) {
  3669. //restart system from scratch
  3670. particles->prev_ticks = 0;
  3671. particles->phase = 0;
  3672. particles->prev_phase = 0;
  3673. particles->clear = true;
  3674. }
  3675. particles->inactive = false;
  3676. particles->inactive_time = 0;
  3677. } else {
  3678. particles->inactive_time += particles->speed_scale * RendererCompositorRD::singleton->get_frame_delta_time();
  3679. if (particles->inactive_time > particles->lifetime * 1.2) {
  3680. particles->inactive = true;
  3681. continue;
  3682. }
  3683. }
  3684. bool zero_time_scale = Engine::get_singleton()->get_time_scale() <= 0.0;
  3685. if (particles->clear && particles->pre_process_time > 0.0) {
  3686. float frame_time;
  3687. if (particles->fixed_fps > 0)
  3688. frame_time = 1.0 / particles->fixed_fps;
  3689. else
  3690. frame_time = 1.0 / 30.0;
  3691. float todo = particles->pre_process_time;
  3692. while (todo >= 0) {
  3693. _particles_process(particles, frame_time);
  3694. todo -= frame_time;
  3695. }
  3696. }
  3697. if (particles->fixed_fps > 0) {
  3698. float frame_time;
  3699. float decr;
  3700. if (zero_time_scale) {
  3701. frame_time = 0.0;
  3702. decr = 1.0 / particles->fixed_fps;
  3703. } else {
  3704. frame_time = 1.0 / particles->fixed_fps;
  3705. decr = frame_time;
  3706. }
  3707. float delta = RendererCompositorRD::singleton->get_frame_delta_time();
  3708. if (delta > 0.1) { //avoid recursive stalls if fps goes below 10
  3709. delta = 0.1;
  3710. } else if (delta <= 0.0) { //unlikely but..
  3711. delta = 0.001;
  3712. }
  3713. float todo = particles->frame_remainder + delta;
  3714. while (todo >= frame_time) {
  3715. _particles_process(particles, frame_time);
  3716. todo -= decr;
  3717. }
  3718. particles->frame_remainder = todo;
  3719. } else {
  3720. if (zero_time_scale)
  3721. _particles_process(particles, 0.0);
  3722. else
  3723. _particles_process(particles, RendererCompositorRD::singleton->get_frame_delta_time());
  3724. }
  3725. //copy particles to instance buffer
  3726. if (particles->draw_order != RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH) {
  3727. ParticlesShader::CopyPushConstant copy_push_constant;
  3728. copy_push_constant.total_particles = particles->amount;
  3729. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  3730. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[ParticlesShader::COPY_MODE_FILL_INSTANCES]);
  3731. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0);
  3732. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy_push_constant, sizeof(ParticlesShader::CopyPushConstant));
  3733. RD::get_singleton()->compute_list_dispatch_threads(compute_list, particles->amount, 1, 1, 64, 1, 1);
  3734. RD::get_singleton()->compute_list_end();
  3735. }
  3736. particles->instance_dependency.instance_notify_changed(true, false); //make sure shadows are updated
  3737. }
  3738. }
  3739. bool RendererStorageRD::particles_is_inactive(RID p_particles) const {
  3740. const Particles *particles = particles_owner.getornull(p_particles);
  3741. ERR_FAIL_COND_V(!particles, false);
  3742. return !particles->emitting && particles->inactive;
  3743. }
  3744. /* SKY SHADER */
  3745. void RendererStorageRD::ParticlesShaderData::set_code(const String &p_code) {
  3746. //compile
  3747. code = p_code;
  3748. valid = false;
  3749. ubo_size = 0;
  3750. uniforms.clear();
  3751. if (code == String()) {
  3752. return; //just invalid, but no error
  3753. }
  3754. ShaderCompilerRD::GeneratedCode gen_code;
  3755. ShaderCompilerRD::IdentifierActions actions;
  3756. /*
  3757. uses_time = false;
  3758. actions.render_mode_flags["use_half_res_pass"] = &uses_half_res;
  3759. actions.render_mode_flags["use_quarter_res_pass"] = &uses_quarter_res;
  3760. actions.usage_flag_pointers["TIME"] = &uses_time;
  3761. */
  3762. actions.uniforms = &uniforms;
  3763. Error err = base_singleton->particles_shader.compiler.compile(RS::SHADER_PARTICLES, code, &actions, path, gen_code);
  3764. ERR_FAIL_COND(err != OK);
  3765. if (version.is_null()) {
  3766. version = base_singleton->particles_shader.shader.version_create();
  3767. }
  3768. base_singleton->particles_shader.shader.version_set_compute_code(version, gen_code.uniforms, gen_code.compute_global, gen_code.compute, gen_code.defines);
  3769. ERR_FAIL_COND(!base_singleton->particles_shader.shader.version_is_valid(version));
  3770. ubo_size = gen_code.uniform_total_size;
  3771. ubo_offsets = gen_code.uniform_offsets;
  3772. texture_uniforms = gen_code.texture_uniforms;
  3773. //update pipelines
  3774. pipeline = RD::get_singleton()->compute_pipeline_create(base_singleton->particles_shader.shader.version_get_shader(version, 0));
  3775. valid = true;
  3776. }
  3777. void RendererStorageRD::ParticlesShaderData::set_default_texture_param(const StringName &p_name, RID p_texture) {
  3778. if (!p_texture.is_valid()) {
  3779. default_texture_params.erase(p_name);
  3780. } else {
  3781. default_texture_params[p_name] = p_texture;
  3782. }
  3783. }
  3784. void RendererStorageRD::ParticlesShaderData::get_param_list(List<PropertyInfo> *p_param_list) const {
  3785. Map<int, StringName> order;
  3786. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = uniforms.front(); E; E = E->next()) {
  3787. if (E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL || E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  3788. continue;
  3789. }
  3790. if (E->get().texture_order >= 0) {
  3791. order[E->get().texture_order + 100000] = E->key();
  3792. } else {
  3793. order[E->get().order] = E->key();
  3794. }
  3795. }
  3796. for (Map<int, StringName>::Element *E = order.front(); E; E = E->next()) {
  3797. PropertyInfo pi = ShaderLanguage::uniform_to_property_info(uniforms[E->get()]);
  3798. pi.name = E->get();
  3799. p_param_list->push_back(pi);
  3800. }
  3801. }
  3802. void RendererStorageRD::ParticlesShaderData::get_instance_param_list(List<RendererStorage::InstanceShaderParam> *p_param_list) const {
  3803. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = uniforms.front(); E; E = E->next()) {
  3804. if (E->get().scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  3805. continue;
  3806. }
  3807. RendererStorage::InstanceShaderParam p;
  3808. p.info = ShaderLanguage::uniform_to_property_info(E->get());
  3809. p.info.name = E->key(); //supply name
  3810. p.index = E->get().instance_index;
  3811. p.default_value = ShaderLanguage::constant_value_to_variant(E->get().default_value, E->get().type, E->get().hint);
  3812. p_param_list->push_back(p);
  3813. }
  3814. }
  3815. bool RendererStorageRD::ParticlesShaderData::is_param_texture(const StringName &p_param) const {
  3816. if (!uniforms.has(p_param)) {
  3817. return false;
  3818. }
  3819. return uniforms[p_param].texture_order >= 0;
  3820. }
  3821. bool RendererStorageRD::ParticlesShaderData::is_animated() const {
  3822. return false;
  3823. }
  3824. bool RendererStorageRD::ParticlesShaderData::casts_shadows() const {
  3825. return false;
  3826. }
  3827. Variant RendererStorageRD::ParticlesShaderData::get_default_parameter(const StringName &p_parameter) const {
  3828. if (uniforms.has(p_parameter)) {
  3829. ShaderLanguage::ShaderNode::Uniform uniform = uniforms[p_parameter];
  3830. Vector<ShaderLanguage::ConstantNode::Value> default_value = uniform.default_value;
  3831. return ShaderLanguage::constant_value_to_variant(default_value, uniform.type, uniform.hint);
  3832. }
  3833. return Variant();
  3834. }
  3835. RendererStorageRD::ParticlesShaderData::ParticlesShaderData() {
  3836. valid = false;
  3837. }
  3838. RendererStorageRD::ParticlesShaderData::~ParticlesShaderData() {
  3839. //pipeline variants will clear themselves if shader is gone
  3840. if (version.is_valid()) {
  3841. base_singleton->particles_shader.shader.version_free(version);
  3842. }
  3843. }
  3844. RendererStorageRD::ShaderData *RendererStorageRD::_create_particles_shader_func() {
  3845. ParticlesShaderData *shader_data = memnew(ParticlesShaderData);
  3846. return shader_data;
  3847. }
  3848. void RendererStorageRD::ParticlesMaterialData::update_parameters(const Map<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) {
  3849. uniform_set_updated = true;
  3850. if ((uint32_t)ubo_data.size() != shader_data->ubo_size) {
  3851. p_uniform_dirty = true;
  3852. if (uniform_buffer.is_valid()) {
  3853. RD::get_singleton()->free(uniform_buffer);
  3854. uniform_buffer = RID();
  3855. }
  3856. ubo_data.resize(shader_data->ubo_size);
  3857. if (ubo_data.size()) {
  3858. uniform_buffer = RD::get_singleton()->uniform_buffer_create(ubo_data.size());
  3859. memset(ubo_data.ptrw(), 0, ubo_data.size()); //clear
  3860. }
  3861. //clear previous uniform set
  3862. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  3863. RD::get_singleton()->free(uniform_set);
  3864. uniform_set = RID();
  3865. }
  3866. }
  3867. //check whether buffer changed
  3868. if (p_uniform_dirty && ubo_data.size()) {
  3869. update_uniform_buffer(shader_data->uniforms, shader_data->ubo_offsets.ptr(), p_parameters, ubo_data.ptrw(), ubo_data.size(), false);
  3870. RD::get_singleton()->buffer_update(uniform_buffer, 0, ubo_data.size(), ubo_data.ptrw());
  3871. }
  3872. uint32_t tex_uniform_count = shader_data->texture_uniforms.size();
  3873. if ((uint32_t)texture_cache.size() != tex_uniform_count) {
  3874. texture_cache.resize(tex_uniform_count);
  3875. p_textures_dirty = true;
  3876. //clear previous uniform set
  3877. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  3878. RD::get_singleton()->free(uniform_set);
  3879. uniform_set = RID();
  3880. }
  3881. }
  3882. if (p_textures_dirty && tex_uniform_count) {
  3883. update_textures(p_parameters, shader_data->default_texture_params, shader_data->texture_uniforms, texture_cache.ptrw(), true);
  3884. }
  3885. if (shader_data->ubo_size == 0 && shader_data->texture_uniforms.size() == 0) {
  3886. // This material does not require an uniform set, so don't create it.
  3887. return;
  3888. }
  3889. if (!p_textures_dirty && uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  3890. //no reason to update uniform set, only UBO (or nothing) was needed to update
  3891. return;
  3892. }
  3893. Vector<RD::Uniform> uniforms;
  3894. {
  3895. if (shader_data->ubo_size) {
  3896. RD::Uniform u;
  3897. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  3898. u.binding = 0;
  3899. u.ids.push_back(uniform_buffer);
  3900. uniforms.push_back(u);
  3901. }
  3902. const RID *textures = texture_cache.ptrw();
  3903. for (uint32_t i = 0; i < tex_uniform_count; i++) {
  3904. RD::Uniform u;
  3905. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  3906. u.binding = 1 + i;
  3907. u.ids.push_back(textures[i]);
  3908. uniforms.push_back(u);
  3909. }
  3910. }
  3911. uniform_set = RD::get_singleton()->uniform_set_create(uniforms, base_singleton->particles_shader.shader.version_get_shader(shader_data->version, 0), 3);
  3912. }
  3913. RendererStorageRD::ParticlesMaterialData::~ParticlesMaterialData() {
  3914. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  3915. RD::get_singleton()->free(uniform_set);
  3916. }
  3917. if (uniform_buffer.is_valid()) {
  3918. RD::get_singleton()->free(uniform_buffer);
  3919. }
  3920. }
  3921. RendererStorageRD::MaterialData *RendererStorageRD::_create_particles_material_func(ParticlesShaderData *p_shader) {
  3922. ParticlesMaterialData *material_data = memnew(ParticlesMaterialData);
  3923. material_data->shader_data = p_shader;
  3924. material_data->last_frame = false;
  3925. //update will happen later anyway so do nothing.
  3926. return material_data;
  3927. }
  3928. ////////
  3929. /* PARTICLES COLLISION API */
  3930. RID RendererStorageRD::particles_collision_create() {
  3931. return particles_collision_owner.make_rid(ParticlesCollision());
  3932. }
  3933. RID RendererStorageRD::particles_collision_get_heightfield_framebuffer(RID p_particles_collision) const {
  3934. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3935. ERR_FAIL_COND_V(!particles_collision, RID());
  3936. ERR_FAIL_COND_V(particles_collision->type != RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE, RID());
  3937. if (particles_collision->heightfield_texture == RID()) {
  3938. //create
  3939. int resolutions[RS::PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_MAX] = { 256, 512, 1024, 2048, 4096, 8192 };
  3940. Size2i size;
  3941. if (particles_collision->extents.x > particles_collision->extents.z) {
  3942. size.x = resolutions[particles_collision->heightfield_resolution];
  3943. size.y = int32_t(particles_collision->extents.z / particles_collision->extents.x * size.x);
  3944. } else {
  3945. size.y = resolutions[particles_collision->heightfield_resolution];
  3946. size.x = int32_t(particles_collision->extents.x / particles_collision->extents.z * size.y);
  3947. }
  3948. RD::TextureFormat tf;
  3949. tf.format = RD::DATA_FORMAT_D32_SFLOAT;
  3950. tf.width = size.x;
  3951. tf.height = size.y;
  3952. tf.type = RD::TEXTURE_TYPE_2D;
  3953. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  3954. particles_collision->heightfield_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  3955. Vector<RID> fb_tex;
  3956. fb_tex.push_back(particles_collision->heightfield_texture);
  3957. particles_collision->heightfield_fb = RD::get_singleton()->framebuffer_create(fb_tex);
  3958. particles_collision->heightfield_fb_size = size;
  3959. }
  3960. return particles_collision->heightfield_fb;
  3961. }
  3962. void RendererStorageRD::particles_collision_set_collision_type(RID p_particles_collision, RS::ParticlesCollisionType p_type) {
  3963. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3964. ERR_FAIL_COND(!particles_collision);
  3965. if (p_type == particles_collision->type) {
  3966. return;
  3967. }
  3968. if (particles_collision->heightfield_texture.is_valid()) {
  3969. RD::get_singleton()->free(particles_collision->heightfield_texture);
  3970. particles_collision->heightfield_texture = RID();
  3971. }
  3972. particles_collision->type = p_type;
  3973. particles_collision->instance_dependency.instance_notify_changed(true, false);
  3974. }
  3975. void RendererStorageRD::particles_collision_set_cull_mask(RID p_particles_collision, uint32_t p_cull_mask) {
  3976. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3977. ERR_FAIL_COND(!particles_collision);
  3978. particles_collision->cull_mask = p_cull_mask;
  3979. }
  3980. void RendererStorageRD::particles_collision_set_sphere_radius(RID p_particles_collision, float p_radius) {
  3981. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3982. ERR_FAIL_COND(!particles_collision);
  3983. particles_collision->radius = p_radius;
  3984. particles_collision->instance_dependency.instance_notify_changed(true, false);
  3985. }
  3986. void RendererStorageRD::particles_collision_set_box_extents(RID p_particles_collision, const Vector3 &p_extents) {
  3987. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3988. ERR_FAIL_COND(!particles_collision);
  3989. particles_collision->extents = p_extents;
  3990. particles_collision->instance_dependency.instance_notify_changed(true, false);
  3991. }
  3992. void RendererStorageRD::particles_collision_set_attractor_strength(RID p_particles_collision, float p_strength) {
  3993. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3994. ERR_FAIL_COND(!particles_collision);
  3995. particles_collision->attractor_strength = p_strength;
  3996. }
  3997. void RendererStorageRD::particles_collision_set_attractor_directionality(RID p_particles_collision, float p_directionality) {
  3998. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3999. ERR_FAIL_COND(!particles_collision);
  4000. particles_collision->attractor_directionality = p_directionality;
  4001. }
  4002. void RendererStorageRD::particles_collision_set_attractor_attenuation(RID p_particles_collision, float p_curve) {
  4003. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4004. ERR_FAIL_COND(!particles_collision);
  4005. particles_collision->attractor_attenuation = p_curve;
  4006. }
  4007. void RendererStorageRD::particles_collision_set_field_texture(RID p_particles_collision, RID p_texture) {
  4008. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4009. ERR_FAIL_COND(!particles_collision);
  4010. particles_collision->field_texture = p_texture;
  4011. }
  4012. void RendererStorageRD::particles_collision_height_field_update(RID p_particles_collision) {
  4013. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4014. ERR_FAIL_COND(!particles_collision);
  4015. particles_collision->instance_dependency.instance_notify_changed(true, false);
  4016. }
  4017. void RendererStorageRD::particles_collision_set_height_field_resolution(RID p_particles_collision, RS::ParticlesCollisionHeightfieldResolution p_resolution) {
  4018. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4019. ERR_FAIL_COND(!particles_collision);
  4020. if (particles_collision->heightfield_resolution == p_resolution) {
  4021. return;
  4022. }
  4023. particles_collision->heightfield_resolution = p_resolution;
  4024. if (particles_collision->heightfield_texture.is_valid()) {
  4025. RD::get_singleton()->free(particles_collision->heightfield_texture);
  4026. particles_collision->heightfield_texture = RID();
  4027. }
  4028. }
  4029. AABB RendererStorageRD::particles_collision_get_aabb(RID p_particles_collision) const {
  4030. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4031. ERR_FAIL_COND_V(!particles_collision, AABB());
  4032. switch (particles_collision->type) {
  4033. case RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT:
  4034. case RS::PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE: {
  4035. AABB aabb;
  4036. aabb.position = -Vector3(1, 1, 1) * particles_collision->radius;
  4037. aabb.size = Vector3(2, 2, 2) * particles_collision->radius;
  4038. return aabb;
  4039. }
  4040. default: {
  4041. AABB aabb;
  4042. aabb.position = -particles_collision->extents;
  4043. aabb.size = particles_collision->extents * 2;
  4044. return aabb;
  4045. }
  4046. }
  4047. return AABB();
  4048. }
  4049. Vector3 RendererStorageRD::particles_collision_get_extents(RID p_particles_collision) const {
  4050. const ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4051. ERR_FAIL_COND_V(!particles_collision, Vector3());
  4052. return particles_collision->extents;
  4053. }
  4054. bool RendererStorageRD::particles_collision_is_heightfield(RID p_particles_collision) const {
  4055. const ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4056. ERR_FAIL_COND_V(!particles_collision, false);
  4057. return particles_collision->type == RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE;
  4058. }
  4059. /* SKELETON API */
  4060. RID RendererStorageRD::skeleton_create() {
  4061. return skeleton_owner.make_rid(Skeleton());
  4062. }
  4063. void RendererStorageRD::_skeleton_make_dirty(Skeleton *skeleton) {
  4064. if (!skeleton->dirty) {
  4065. skeleton->dirty = true;
  4066. skeleton->dirty_list = skeleton_dirty_list;
  4067. skeleton_dirty_list = skeleton;
  4068. }
  4069. }
  4070. void RendererStorageRD::skeleton_allocate(RID p_skeleton, int p_bones, bool p_2d_skeleton) {
  4071. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4072. ERR_FAIL_COND(!skeleton);
  4073. ERR_FAIL_COND(p_bones < 0);
  4074. if (skeleton->size == p_bones && skeleton->use_2d == p_2d_skeleton) {
  4075. return;
  4076. }
  4077. skeleton->size = p_bones;
  4078. skeleton->use_2d = p_2d_skeleton;
  4079. skeleton->uniform_set_3d = RID();
  4080. if (skeleton->buffer.is_valid()) {
  4081. RD::get_singleton()->free(skeleton->buffer);
  4082. skeleton->buffer = RID();
  4083. skeleton->data.resize(0);
  4084. }
  4085. if (skeleton->size) {
  4086. skeleton->data.resize(skeleton->size * (skeleton->use_2d ? 8 : 12));
  4087. skeleton->buffer = RD::get_singleton()->storage_buffer_create(skeleton->data.size() * sizeof(float));
  4088. zeromem(skeleton->data.ptrw(), skeleton->data.size() * sizeof(float));
  4089. _skeleton_make_dirty(skeleton);
  4090. }
  4091. }
  4092. int RendererStorageRD::skeleton_get_bone_count(RID p_skeleton) const {
  4093. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4094. ERR_FAIL_COND_V(!skeleton, 0);
  4095. return skeleton->size;
  4096. }
  4097. void RendererStorageRD::skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform &p_transform) {
  4098. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4099. ERR_FAIL_COND(!skeleton);
  4100. ERR_FAIL_INDEX(p_bone, skeleton->size);
  4101. ERR_FAIL_COND(skeleton->use_2d);
  4102. float *dataptr = skeleton->data.ptrw() + p_bone * 12;
  4103. dataptr[0] = p_transform.basis.elements[0][0];
  4104. dataptr[1] = p_transform.basis.elements[0][1];
  4105. dataptr[2] = p_transform.basis.elements[0][2];
  4106. dataptr[3] = p_transform.origin.x;
  4107. dataptr[4] = p_transform.basis.elements[1][0];
  4108. dataptr[5] = p_transform.basis.elements[1][1];
  4109. dataptr[6] = p_transform.basis.elements[1][2];
  4110. dataptr[7] = p_transform.origin.y;
  4111. dataptr[8] = p_transform.basis.elements[2][0];
  4112. dataptr[9] = p_transform.basis.elements[2][1];
  4113. dataptr[10] = p_transform.basis.elements[2][2];
  4114. dataptr[11] = p_transform.origin.z;
  4115. _skeleton_make_dirty(skeleton);
  4116. }
  4117. Transform RendererStorageRD::skeleton_bone_get_transform(RID p_skeleton, int p_bone) const {
  4118. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4119. ERR_FAIL_COND_V(!skeleton, Transform());
  4120. ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform());
  4121. ERR_FAIL_COND_V(skeleton->use_2d, Transform());
  4122. const float *dataptr = skeleton->data.ptr() + p_bone * 12;
  4123. Transform t;
  4124. t.basis.elements[0][0] = dataptr[0];
  4125. t.basis.elements[0][1] = dataptr[1];
  4126. t.basis.elements[0][2] = dataptr[2];
  4127. t.origin.x = dataptr[3];
  4128. t.basis.elements[1][0] = dataptr[4];
  4129. t.basis.elements[1][1] = dataptr[5];
  4130. t.basis.elements[1][2] = dataptr[6];
  4131. t.origin.y = dataptr[7];
  4132. t.basis.elements[2][0] = dataptr[8];
  4133. t.basis.elements[2][1] = dataptr[9];
  4134. t.basis.elements[2][2] = dataptr[10];
  4135. t.origin.z = dataptr[11];
  4136. return t;
  4137. }
  4138. void RendererStorageRD::skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) {
  4139. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4140. ERR_FAIL_COND(!skeleton);
  4141. ERR_FAIL_INDEX(p_bone, skeleton->size);
  4142. ERR_FAIL_COND(!skeleton->use_2d);
  4143. float *dataptr = skeleton->data.ptrw() + p_bone * 8;
  4144. dataptr[0] = p_transform.elements[0][0];
  4145. dataptr[1] = p_transform.elements[1][0];
  4146. dataptr[2] = 0;
  4147. dataptr[3] = p_transform.elements[2][0];
  4148. dataptr[4] = p_transform.elements[0][1];
  4149. dataptr[5] = p_transform.elements[1][1];
  4150. dataptr[6] = 0;
  4151. dataptr[7] = p_transform.elements[2][1];
  4152. _skeleton_make_dirty(skeleton);
  4153. }
  4154. Transform2D RendererStorageRD::skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const {
  4155. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4156. ERR_FAIL_COND_V(!skeleton, Transform2D());
  4157. ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform2D());
  4158. ERR_FAIL_COND_V(!skeleton->use_2d, Transform2D());
  4159. const float *dataptr = skeleton->data.ptr() + p_bone * 8;
  4160. Transform2D t;
  4161. t.elements[0][0] = dataptr[0];
  4162. t.elements[1][0] = dataptr[1];
  4163. t.elements[2][0] = dataptr[3];
  4164. t.elements[0][1] = dataptr[4];
  4165. t.elements[1][1] = dataptr[5];
  4166. t.elements[2][1] = dataptr[7];
  4167. return t;
  4168. }
  4169. void RendererStorageRD::skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) {
  4170. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4171. ERR_FAIL_COND(!skeleton->use_2d);
  4172. skeleton->base_transform_2d = p_base_transform;
  4173. }
  4174. void RendererStorageRD::_update_dirty_skeletons() {
  4175. while (skeleton_dirty_list) {
  4176. Skeleton *skeleton = skeleton_dirty_list;
  4177. if (skeleton->size) {
  4178. RD::get_singleton()->buffer_update(skeleton->buffer, 0, skeleton->data.size() * sizeof(float), skeleton->data.ptr(), false);
  4179. }
  4180. skeleton_dirty_list = skeleton->dirty_list;
  4181. skeleton->instance_dependency.instance_notify_changed(true, false);
  4182. skeleton->dirty = false;
  4183. skeleton->dirty_list = nullptr;
  4184. }
  4185. skeleton_dirty_list = nullptr;
  4186. }
  4187. /* LIGHT */
  4188. RID RendererStorageRD::light_create(RS::LightType p_type) {
  4189. Light light;
  4190. light.type = p_type;
  4191. light.param[RS::LIGHT_PARAM_ENERGY] = 1.0;
  4192. light.param[RS::LIGHT_PARAM_INDIRECT_ENERGY] = 1.0;
  4193. light.param[RS::LIGHT_PARAM_SPECULAR] = 0.5;
  4194. light.param[RS::LIGHT_PARAM_RANGE] = 1.0;
  4195. light.param[RS::LIGHT_PARAM_SIZE] = 0.0;
  4196. light.param[RS::LIGHT_PARAM_SPOT_ANGLE] = 45;
  4197. light.param[RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE] = 0;
  4198. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET] = 0.1;
  4199. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET] = 0.3;
  4200. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET] = 0.6;
  4201. light.param[RS::LIGHT_PARAM_SHADOW_FADE_START] = 0.8;
  4202. light.param[RS::LIGHT_PARAM_SHADOW_BIAS] = 0.02;
  4203. light.param[RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] = 1.0;
  4204. light.param[RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE] = 20.0;
  4205. light.param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS] = 0.05;
  4206. light.param[RS::LIGHT_PARAM_SHADOW_VOLUMETRIC_FOG_FADE] = 1.0;
  4207. return light_owner.make_rid(light);
  4208. }
  4209. void RendererStorageRD::light_set_color(RID p_light, const Color &p_color) {
  4210. Light *light = light_owner.getornull(p_light);
  4211. ERR_FAIL_COND(!light);
  4212. light->color = p_color;
  4213. }
  4214. void RendererStorageRD::light_set_param(RID p_light, RS::LightParam p_param, float p_value) {
  4215. Light *light = light_owner.getornull(p_light);
  4216. ERR_FAIL_COND(!light);
  4217. ERR_FAIL_INDEX(p_param, RS::LIGHT_PARAM_MAX);
  4218. switch (p_param) {
  4219. case RS::LIGHT_PARAM_RANGE:
  4220. case RS::LIGHT_PARAM_SPOT_ANGLE:
  4221. case RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE:
  4222. case RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET:
  4223. case RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET:
  4224. case RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET:
  4225. case RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS:
  4226. case RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE:
  4227. case RS::LIGHT_PARAM_SHADOW_BIAS: {
  4228. light->version++;
  4229. light->instance_dependency.instance_notify_changed(true, false);
  4230. } break;
  4231. default: {
  4232. }
  4233. }
  4234. light->param[p_param] = p_value;
  4235. }
  4236. void RendererStorageRD::light_set_shadow(RID p_light, bool p_enabled) {
  4237. Light *light = light_owner.getornull(p_light);
  4238. ERR_FAIL_COND(!light);
  4239. light->shadow = p_enabled;
  4240. light->version++;
  4241. light->instance_dependency.instance_notify_changed(true, false);
  4242. }
  4243. void RendererStorageRD::light_set_shadow_color(RID p_light, const Color &p_color) {
  4244. Light *light = light_owner.getornull(p_light);
  4245. ERR_FAIL_COND(!light);
  4246. light->shadow_color = p_color;
  4247. }
  4248. void RendererStorageRD::light_set_projector(RID p_light, RID p_texture) {
  4249. Light *light = light_owner.getornull(p_light);
  4250. ERR_FAIL_COND(!light);
  4251. if (light->projector == p_texture) {
  4252. return;
  4253. }
  4254. if (light->type != RS::LIGHT_DIRECTIONAL && light->projector.is_valid()) {
  4255. texture_remove_from_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
  4256. }
  4257. light->projector = p_texture;
  4258. if (light->type != RS::LIGHT_DIRECTIONAL && light->projector.is_valid()) {
  4259. texture_add_to_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
  4260. }
  4261. }
  4262. void RendererStorageRD::light_set_negative(RID p_light, bool p_enable) {
  4263. Light *light = light_owner.getornull(p_light);
  4264. ERR_FAIL_COND(!light);
  4265. light->negative = p_enable;
  4266. }
  4267. void RendererStorageRD::light_set_cull_mask(RID p_light, uint32_t p_mask) {
  4268. Light *light = light_owner.getornull(p_light);
  4269. ERR_FAIL_COND(!light);
  4270. light->cull_mask = p_mask;
  4271. light->version++;
  4272. light->instance_dependency.instance_notify_changed(true, false);
  4273. }
  4274. void RendererStorageRD::light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) {
  4275. Light *light = light_owner.getornull(p_light);
  4276. ERR_FAIL_COND(!light);
  4277. light->reverse_cull = p_enabled;
  4278. light->version++;
  4279. light->instance_dependency.instance_notify_changed(true, false);
  4280. }
  4281. void RendererStorageRD::light_set_bake_mode(RID p_light, RS::LightBakeMode p_bake_mode) {
  4282. Light *light = light_owner.getornull(p_light);
  4283. ERR_FAIL_COND(!light);
  4284. light->bake_mode = p_bake_mode;
  4285. light->version++;
  4286. light->instance_dependency.instance_notify_changed(true, false);
  4287. }
  4288. void RendererStorageRD::light_set_max_sdfgi_cascade(RID p_light, uint32_t p_cascade) {
  4289. Light *light = light_owner.getornull(p_light);
  4290. ERR_FAIL_COND(!light);
  4291. light->max_sdfgi_cascade = p_cascade;
  4292. light->version++;
  4293. light->instance_dependency.instance_notify_changed(true, false);
  4294. }
  4295. void RendererStorageRD::light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode) {
  4296. Light *light = light_owner.getornull(p_light);
  4297. ERR_FAIL_COND(!light);
  4298. light->omni_shadow_mode = p_mode;
  4299. light->version++;
  4300. light->instance_dependency.instance_notify_changed(true, false);
  4301. }
  4302. RS::LightOmniShadowMode RendererStorageRD::light_omni_get_shadow_mode(RID p_light) {
  4303. const Light *light = light_owner.getornull(p_light);
  4304. ERR_FAIL_COND_V(!light, RS::LIGHT_OMNI_SHADOW_CUBE);
  4305. return light->omni_shadow_mode;
  4306. }
  4307. void RendererStorageRD::light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode) {
  4308. Light *light = light_owner.getornull(p_light);
  4309. ERR_FAIL_COND(!light);
  4310. light->directional_shadow_mode = p_mode;
  4311. light->version++;
  4312. light->instance_dependency.instance_notify_changed(true, false);
  4313. }
  4314. void RendererStorageRD::light_directional_set_blend_splits(RID p_light, bool p_enable) {
  4315. Light *light = light_owner.getornull(p_light);
  4316. ERR_FAIL_COND(!light);
  4317. light->directional_blend_splits = p_enable;
  4318. light->version++;
  4319. light->instance_dependency.instance_notify_changed(true, false);
  4320. }
  4321. bool RendererStorageRD::light_directional_get_blend_splits(RID p_light) const {
  4322. const Light *light = light_owner.getornull(p_light);
  4323. ERR_FAIL_COND_V(!light, false);
  4324. return light->directional_blend_splits;
  4325. }
  4326. void RendererStorageRD::light_directional_set_sky_only(RID p_light, bool p_sky_only) {
  4327. Light *light = light_owner.getornull(p_light);
  4328. ERR_FAIL_COND(!light);
  4329. light->directional_sky_only = p_sky_only;
  4330. }
  4331. bool RendererStorageRD::light_directional_is_sky_only(RID p_light) const {
  4332. const Light *light = light_owner.getornull(p_light);
  4333. ERR_FAIL_COND_V(!light, false);
  4334. return light->directional_sky_only;
  4335. }
  4336. RS::LightDirectionalShadowMode RendererStorageRD::light_directional_get_shadow_mode(RID p_light) {
  4337. const Light *light = light_owner.getornull(p_light);
  4338. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL);
  4339. return light->directional_shadow_mode;
  4340. }
  4341. void RendererStorageRD::light_directional_set_shadow_depth_range_mode(RID p_light, RS::LightDirectionalShadowDepthRangeMode p_range_mode) {
  4342. Light *light = light_owner.getornull(p_light);
  4343. ERR_FAIL_COND(!light);
  4344. light->directional_range_mode = p_range_mode;
  4345. }
  4346. RS::LightDirectionalShadowDepthRangeMode RendererStorageRD::light_directional_get_shadow_depth_range_mode(RID p_light) const {
  4347. const Light *light = light_owner.getornull(p_light);
  4348. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE);
  4349. return light->directional_range_mode;
  4350. }
  4351. uint32_t RendererStorageRD::light_get_max_sdfgi_cascade(RID p_light) {
  4352. const Light *light = light_owner.getornull(p_light);
  4353. ERR_FAIL_COND_V(!light, 0);
  4354. return light->max_sdfgi_cascade;
  4355. }
  4356. RS::LightBakeMode RendererStorageRD::light_get_bake_mode(RID p_light) {
  4357. const Light *light = light_owner.getornull(p_light);
  4358. ERR_FAIL_COND_V(!light, RS::LIGHT_BAKE_DISABLED);
  4359. return light->bake_mode;
  4360. }
  4361. uint64_t RendererStorageRD::light_get_version(RID p_light) const {
  4362. const Light *light = light_owner.getornull(p_light);
  4363. ERR_FAIL_COND_V(!light, 0);
  4364. return light->version;
  4365. }
  4366. AABB RendererStorageRD::light_get_aabb(RID p_light) const {
  4367. const Light *light = light_owner.getornull(p_light);
  4368. ERR_FAIL_COND_V(!light, AABB());
  4369. switch (light->type) {
  4370. case RS::LIGHT_SPOT: {
  4371. float len = light->param[RS::LIGHT_PARAM_RANGE];
  4372. float size = Math::tan(Math::deg2rad(light->param[RS::LIGHT_PARAM_SPOT_ANGLE])) * len;
  4373. return AABB(Vector3(-size, -size, -len), Vector3(size * 2, size * 2, len));
  4374. };
  4375. case RS::LIGHT_OMNI: {
  4376. float r = light->param[RS::LIGHT_PARAM_RANGE];
  4377. return AABB(-Vector3(r, r, r), Vector3(r, r, r) * 2);
  4378. };
  4379. case RS::LIGHT_DIRECTIONAL: {
  4380. return AABB();
  4381. };
  4382. }
  4383. ERR_FAIL_V(AABB());
  4384. }
  4385. /* REFLECTION PROBE */
  4386. RID RendererStorageRD::reflection_probe_create() {
  4387. return reflection_probe_owner.make_rid(ReflectionProbe());
  4388. }
  4389. void RendererStorageRD::reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode) {
  4390. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4391. ERR_FAIL_COND(!reflection_probe);
  4392. reflection_probe->update_mode = p_mode;
  4393. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  4394. }
  4395. void RendererStorageRD::reflection_probe_set_intensity(RID p_probe, float p_intensity) {
  4396. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4397. ERR_FAIL_COND(!reflection_probe);
  4398. reflection_probe->intensity = p_intensity;
  4399. }
  4400. void RendererStorageRD::reflection_probe_set_ambient_mode(RID p_probe, RS::ReflectionProbeAmbientMode p_mode) {
  4401. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4402. ERR_FAIL_COND(!reflection_probe);
  4403. reflection_probe->ambient_mode = p_mode;
  4404. }
  4405. void RendererStorageRD::reflection_probe_set_ambient_color(RID p_probe, const Color &p_color) {
  4406. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4407. ERR_FAIL_COND(!reflection_probe);
  4408. reflection_probe->ambient_color = p_color;
  4409. }
  4410. void RendererStorageRD::reflection_probe_set_ambient_energy(RID p_probe, float p_energy) {
  4411. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4412. ERR_FAIL_COND(!reflection_probe);
  4413. reflection_probe->ambient_color_energy = p_energy;
  4414. }
  4415. void RendererStorageRD::reflection_probe_set_max_distance(RID p_probe, float p_distance) {
  4416. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4417. ERR_FAIL_COND(!reflection_probe);
  4418. reflection_probe->max_distance = p_distance;
  4419. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  4420. }
  4421. void RendererStorageRD::reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents) {
  4422. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4423. ERR_FAIL_COND(!reflection_probe);
  4424. if (reflection_probe->extents == p_extents) {
  4425. return;
  4426. }
  4427. reflection_probe->extents = p_extents;
  4428. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  4429. }
  4430. void RendererStorageRD::reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) {
  4431. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4432. ERR_FAIL_COND(!reflection_probe);
  4433. reflection_probe->origin_offset = p_offset;
  4434. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  4435. }
  4436. void RendererStorageRD::reflection_probe_set_as_interior(RID p_probe, bool p_enable) {
  4437. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4438. ERR_FAIL_COND(!reflection_probe);
  4439. reflection_probe->interior = p_enable;
  4440. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  4441. }
  4442. void RendererStorageRD::reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) {
  4443. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4444. ERR_FAIL_COND(!reflection_probe);
  4445. reflection_probe->box_projection = p_enable;
  4446. }
  4447. void RendererStorageRD::reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) {
  4448. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4449. ERR_FAIL_COND(!reflection_probe);
  4450. reflection_probe->enable_shadows = p_enable;
  4451. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  4452. }
  4453. void RendererStorageRD::reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) {
  4454. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4455. ERR_FAIL_COND(!reflection_probe);
  4456. reflection_probe->cull_mask = p_layers;
  4457. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  4458. }
  4459. void RendererStorageRD::reflection_probe_set_resolution(RID p_probe, int p_resolution) {
  4460. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4461. ERR_FAIL_COND(!reflection_probe);
  4462. ERR_FAIL_COND(p_resolution < 32);
  4463. reflection_probe->resolution = p_resolution;
  4464. }
  4465. AABB RendererStorageRD::reflection_probe_get_aabb(RID p_probe) const {
  4466. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4467. ERR_FAIL_COND_V(!reflection_probe, AABB());
  4468. AABB aabb;
  4469. aabb.position = -reflection_probe->extents;
  4470. aabb.size = reflection_probe->extents * 2.0;
  4471. return aabb;
  4472. }
  4473. RS::ReflectionProbeUpdateMode RendererStorageRD::reflection_probe_get_update_mode(RID p_probe) const {
  4474. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4475. ERR_FAIL_COND_V(!reflection_probe, RS::REFLECTION_PROBE_UPDATE_ALWAYS);
  4476. return reflection_probe->update_mode;
  4477. }
  4478. uint32_t RendererStorageRD::reflection_probe_get_cull_mask(RID p_probe) const {
  4479. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4480. ERR_FAIL_COND_V(!reflection_probe, 0);
  4481. return reflection_probe->cull_mask;
  4482. }
  4483. Vector3 RendererStorageRD::reflection_probe_get_extents(RID p_probe) const {
  4484. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4485. ERR_FAIL_COND_V(!reflection_probe, Vector3());
  4486. return reflection_probe->extents;
  4487. }
  4488. Vector3 RendererStorageRD::reflection_probe_get_origin_offset(RID p_probe) const {
  4489. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4490. ERR_FAIL_COND_V(!reflection_probe, Vector3());
  4491. return reflection_probe->origin_offset;
  4492. }
  4493. bool RendererStorageRD::reflection_probe_renders_shadows(RID p_probe) const {
  4494. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4495. ERR_FAIL_COND_V(!reflection_probe, false);
  4496. return reflection_probe->enable_shadows;
  4497. }
  4498. float RendererStorageRD::reflection_probe_get_origin_max_distance(RID p_probe) const {
  4499. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4500. ERR_FAIL_COND_V(!reflection_probe, 0);
  4501. return reflection_probe->max_distance;
  4502. }
  4503. int RendererStorageRD::reflection_probe_get_resolution(RID p_probe) const {
  4504. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4505. ERR_FAIL_COND_V(!reflection_probe, 0);
  4506. return reflection_probe->resolution;
  4507. }
  4508. float RendererStorageRD::reflection_probe_get_intensity(RID p_probe) const {
  4509. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4510. ERR_FAIL_COND_V(!reflection_probe, 0);
  4511. return reflection_probe->intensity;
  4512. }
  4513. bool RendererStorageRD::reflection_probe_is_interior(RID p_probe) const {
  4514. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4515. ERR_FAIL_COND_V(!reflection_probe, false);
  4516. return reflection_probe->interior;
  4517. }
  4518. bool RendererStorageRD::reflection_probe_is_box_projection(RID p_probe) const {
  4519. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4520. ERR_FAIL_COND_V(!reflection_probe, false);
  4521. return reflection_probe->box_projection;
  4522. }
  4523. RS::ReflectionProbeAmbientMode RendererStorageRD::reflection_probe_get_ambient_mode(RID p_probe) const {
  4524. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4525. ERR_FAIL_COND_V(!reflection_probe, RS::REFLECTION_PROBE_AMBIENT_DISABLED);
  4526. return reflection_probe->ambient_mode;
  4527. }
  4528. Color RendererStorageRD::reflection_probe_get_ambient_color(RID p_probe) const {
  4529. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4530. ERR_FAIL_COND_V(!reflection_probe, Color());
  4531. return reflection_probe->ambient_color;
  4532. }
  4533. float RendererStorageRD::reflection_probe_get_ambient_color_energy(RID p_probe) const {
  4534. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4535. ERR_FAIL_COND_V(!reflection_probe, 0);
  4536. return reflection_probe->ambient_color_energy;
  4537. }
  4538. RID RendererStorageRD::decal_create() {
  4539. return decal_owner.make_rid(Decal());
  4540. }
  4541. void RendererStorageRD::decal_set_extents(RID p_decal, const Vector3 &p_extents) {
  4542. Decal *decal = decal_owner.getornull(p_decal);
  4543. ERR_FAIL_COND(!decal);
  4544. decal->extents = p_extents;
  4545. decal->instance_dependency.instance_notify_changed(true, false);
  4546. }
  4547. void RendererStorageRD::decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture) {
  4548. Decal *decal = decal_owner.getornull(p_decal);
  4549. ERR_FAIL_COND(!decal);
  4550. ERR_FAIL_INDEX(p_type, RS::DECAL_TEXTURE_MAX);
  4551. if (decal->textures[p_type] == p_texture) {
  4552. return;
  4553. }
  4554. ERR_FAIL_COND(p_texture.is_valid() && !texture_owner.owns(p_texture));
  4555. if (decal->textures[p_type].is_valid() && texture_owner.owns(decal->textures[p_type])) {
  4556. texture_remove_from_decal_atlas(decal->textures[p_type]);
  4557. }
  4558. decal->textures[p_type] = p_texture;
  4559. if (decal->textures[p_type].is_valid()) {
  4560. texture_add_to_decal_atlas(decal->textures[p_type]);
  4561. }
  4562. decal->instance_dependency.instance_notify_changed(false, true);
  4563. }
  4564. void RendererStorageRD::decal_set_emission_energy(RID p_decal, float p_energy) {
  4565. Decal *decal = decal_owner.getornull(p_decal);
  4566. ERR_FAIL_COND(!decal);
  4567. decal->emission_energy = p_energy;
  4568. }
  4569. void RendererStorageRD::decal_set_albedo_mix(RID p_decal, float p_mix) {
  4570. Decal *decal = decal_owner.getornull(p_decal);
  4571. ERR_FAIL_COND(!decal);
  4572. decal->albedo_mix = p_mix;
  4573. }
  4574. void RendererStorageRD::decal_set_modulate(RID p_decal, const Color &p_modulate) {
  4575. Decal *decal = decal_owner.getornull(p_decal);
  4576. ERR_FAIL_COND(!decal);
  4577. decal->modulate = p_modulate;
  4578. }
  4579. void RendererStorageRD::decal_set_cull_mask(RID p_decal, uint32_t p_layers) {
  4580. Decal *decal = decal_owner.getornull(p_decal);
  4581. ERR_FAIL_COND(!decal);
  4582. decal->cull_mask = p_layers;
  4583. decal->instance_dependency.instance_notify_changed(true, false);
  4584. }
  4585. void RendererStorageRD::decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length) {
  4586. Decal *decal = decal_owner.getornull(p_decal);
  4587. ERR_FAIL_COND(!decal);
  4588. decal->distance_fade = p_enabled;
  4589. decal->distance_fade_begin = p_begin;
  4590. decal->distance_fade_length = p_length;
  4591. }
  4592. void RendererStorageRD::decal_set_fade(RID p_decal, float p_above, float p_below) {
  4593. Decal *decal = decal_owner.getornull(p_decal);
  4594. ERR_FAIL_COND(!decal);
  4595. decal->upper_fade = p_above;
  4596. decal->lower_fade = p_below;
  4597. }
  4598. void RendererStorageRD::decal_set_normal_fade(RID p_decal, float p_fade) {
  4599. Decal *decal = decal_owner.getornull(p_decal);
  4600. ERR_FAIL_COND(!decal);
  4601. decal->normal_fade = p_fade;
  4602. }
  4603. AABB RendererStorageRD::decal_get_aabb(RID p_decal) const {
  4604. Decal *decal = decal_owner.getornull(p_decal);
  4605. ERR_FAIL_COND_V(!decal, AABB());
  4606. return AABB(-decal->extents, decal->extents * 2.0);
  4607. }
  4608. RID RendererStorageRD::gi_probe_create() {
  4609. return gi_probe_owner.make_rid(GIProbe());
  4610. }
  4611. void RendererStorageRD::gi_probe_allocate(RID p_gi_probe, const Transform &p_to_cell_xform, const AABB &p_aabb, const Vector3i &p_octree_size, const Vector<uint8_t> &p_octree_cells, const Vector<uint8_t> &p_data_cells, const Vector<uint8_t> &p_distance_field, const Vector<int> &p_level_counts) {
  4612. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4613. ERR_FAIL_COND(!gi_probe);
  4614. if (gi_probe->octree_buffer.is_valid()) {
  4615. RD::get_singleton()->free(gi_probe->octree_buffer);
  4616. RD::get_singleton()->free(gi_probe->data_buffer);
  4617. if (gi_probe->sdf_texture.is_valid()) {
  4618. RD::get_singleton()->free(gi_probe->sdf_texture);
  4619. }
  4620. gi_probe->sdf_texture = RID();
  4621. gi_probe->octree_buffer = RID();
  4622. gi_probe->data_buffer = RID();
  4623. gi_probe->octree_buffer_size = 0;
  4624. gi_probe->data_buffer_size = 0;
  4625. gi_probe->cell_count = 0;
  4626. }
  4627. gi_probe->to_cell_xform = p_to_cell_xform;
  4628. gi_probe->bounds = p_aabb;
  4629. gi_probe->octree_size = p_octree_size;
  4630. gi_probe->level_counts = p_level_counts;
  4631. if (p_octree_cells.size()) {
  4632. ERR_FAIL_COND(p_octree_cells.size() % 32 != 0); //cells size must be a multiple of 32
  4633. uint32_t cell_count = p_octree_cells.size() / 32;
  4634. ERR_FAIL_COND(p_data_cells.size() != (int)cell_count * 16); //see that data size matches
  4635. gi_probe->cell_count = cell_count;
  4636. gi_probe->octree_buffer = RD::get_singleton()->storage_buffer_create(p_octree_cells.size(), p_octree_cells);
  4637. gi_probe->octree_buffer_size = p_octree_cells.size();
  4638. gi_probe->data_buffer = RD::get_singleton()->storage_buffer_create(p_data_cells.size(), p_data_cells);
  4639. gi_probe->data_buffer_size = p_data_cells.size();
  4640. if (p_distance_field.size()) {
  4641. RD::TextureFormat tf;
  4642. tf.format = RD::DATA_FORMAT_R8_UNORM;
  4643. tf.width = gi_probe->octree_size.x;
  4644. tf.height = gi_probe->octree_size.y;
  4645. tf.depth = gi_probe->octree_size.z;
  4646. tf.type = RD::TEXTURE_TYPE_3D;
  4647. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  4648. Vector<Vector<uint8_t>> s;
  4649. s.push_back(p_distance_field);
  4650. gi_probe->sdf_texture = RD::get_singleton()->texture_create(tf, RD::TextureView(), s);
  4651. }
  4652. #if 0
  4653. {
  4654. RD::TextureFormat tf;
  4655. tf.format = RD::DATA_FORMAT_R8_UNORM;
  4656. tf.width = gi_probe->octree_size.x;
  4657. tf.height = gi_probe->octree_size.y;
  4658. tf.depth = gi_probe->octree_size.z;
  4659. tf.type = RD::TEXTURE_TYPE_3D;
  4660. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  4661. tf.shareable_formats.push_back(RD::DATA_FORMAT_R8_UNORM);
  4662. tf.shareable_formats.push_back(RD::DATA_FORMAT_R8_UINT);
  4663. gi_probe->sdf_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4664. }
  4665. RID shared_tex;
  4666. {
  4667. RD::TextureView tv;
  4668. tv.format_override = RD::DATA_FORMAT_R8_UINT;
  4669. shared_tex = RD::get_singleton()->texture_create_shared(tv, gi_probe->sdf_texture);
  4670. }
  4671. //update SDF texture
  4672. Vector<RD::Uniform> uniforms;
  4673. {
  4674. RD::Uniform u;
  4675. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  4676. u.binding = 1;
  4677. u.ids.push_back(gi_probe->octree_buffer);
  4678. uniforms.push_back(u);
  4679. }
  4680. {
  4681. RD::Uniform u;
  4682. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  4683. u.binding = 2;
  4684. u.ids.push_back(gi_probe->data_buffer);
  4685. uniforms.push_back(u);
  4686. }
  4687. {
  4688. RD::Uniform u;
  4689. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  4690. u.binding = 3;
  4691. u.ids.push_back(shared_tex);
  4692. uniforms.push_back(u);
  4693. }
  4694. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_sdf_shader_version_shader, 0);
  4695. {
  4696. uint32_t push_constant[4] = { 0, 0, 0, 0 };
  4697. for (int i = 0; i < gi_probe->level_counts.size() - 1; i++) {
  4698. push_constant[0] += gi_probe->level_counts[i];
  4699. }
  4700. push_constant[1] = push_constant[0] + gi_probe->level_counts[gi_probe->level_counts.size() - 1];
  4701. print_line("offset: " + itos(push_constant[0]));
  4702. print_line("size: " + itos(push_constant[1]));
  4703. //create SDF
  4704. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  4705. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_sdf_shader_pipeline);
  4706. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set, 0);
  4707. RD::get_singleton()->compute_list_set_push_constant(compute_list, push_constant, sizeof(uint32_t) * 4);
  4708. RD::get_singleton()->compute_list_dispatch(compute_list, gi_probe->octree_size.x / 4, gi_probe->octree_size.y / 4, gi_probe->octree_size.z / 4);
  4709. RD::get_singleton()->compute_list_end();
  4710. }
  4711. RD::get_singleton()->free(uniform_set);
  4712. RD::get_singleton()->free(shared_tex);
  4713. }
  4714. #endif
  4715. }
  4716. gi_probe->version++;
  4717. gi_probe->data_version++;
  4718. gi_probe->instance_dependency.instance_notify_changed(true, false);
  4719. }
  4720. AABB RendererStorageRD::gi_probe_get_bounds(RID p_gi_probe) const {
  4721. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4722. ERR_FAIL_COND_V(!gi_probe, AABB());
  4723. return gi_probe->bounds;
  4724. }
  4725. Vector3i RendererStorageRD::gi_probe_get_octree_size(RID p_gi_probe) const {
  4726. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4727. ERR_FAIL_COND_V(!gi_probe, Vector3i());
  4728. return gi_probe->octree_size;
  4729. }
  4730. Vector<uint8_t> RendererStorageRD::gi_probe_get_octree_cells(RID p_gi_probe) const {
  4731. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4732. ERR_FAIL_COND_V(!gi_probe, Vector<uint8_t>());
  4733. if (gi_probe->octree_buffer.is_valid()) {
  4734. return RD::get_singleton()->buffer_get_data(gi_probe->octree_buffer);
  4735. }
  4736. return Vector<uint8_t>();
  4737. }
  4738. Vector<uint8_t> RendererStorageRD::gi_probe_get_data_cells(RID p_gi_probe) const {
  4739. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4740. ERR_FAIL_COND_V(!gi_probe, Vector<uint8_t>());
  4741. if (gi_probe->data_buffer.is_valid()) {
  4742. return RD::get_singleton()->buffer_get_data(gi_probe->data_buffer);
  4743. }
  4744. return Vector<uint8_t>();
  4745. }
  4746. Vector<uint8_t> RendererStorageRD::gi_probe_get_distance_field(RID p_gi_probe) const {
  4747. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4748. ERR_FAIL_COND_V(!gi_probe, Vector<uint8_t>());
  4749. if (gi_probe->data_buffer.is_valid()) {
  4750. return RD::get_singleton()->texture_get_data(gi_probe->sdf_texture, 0);
  4751. }
  4752. return Vector<uint8_t>();
  4753. }
  4754. Vector<int> RendererStorageRD::gi_probe_get_level_counts(RID p_gi_probe) const {
  4755. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4756. ERR_FAIL_COND_V(!gi_probe, Vector<int>());
  4757. return gi_probe->level_counts;
  4758. }
  4759. Transform RendererStorageRD::gi_probe_get_to_cell_xform(RID p_gi_probe) const {
  4760. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4761. ERR_FAIL_COND_V(!gi_probe, Transform());
  4762. return gi_probe->to_cell_xform;
  4763. }
  4764. void RendererStorageRD::gi_probe_set_dynamic_range(RID p_gi_probe, float p_range) {
  4765. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4766. ERR_FAIL_COND(!gi_probe);
  4767. gi_probe->dynamic_range = p_range;
  4768. gi_probe->version++;
  4769. }
  4770. float RendererStorageRD::gi_probe_get_dynamic_range(RID p_gi_probe) const {
  4771. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4772. ERR_FAIL_COND_V(!gi_probe, 0);
  4773. return gi_probe->dynamic_range;
  4774. }
  4775. void RendererStorageRD::gi_probe_set_propagation(RID p_gi_probe, float p_range) {
  4776. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4777. ERR_FAIL_COND(!gi_probe);
  4778. gi_probe->propagation = p_range;
  4779. gi_probe->version++;
  4780. }
  4781. float RendererStorageRD::gi_probe_get_propagation(RID p_gi_probe) const {
  4782. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4783. ERR_FAIL_COND_V(!gi_probe, 0);
  4784. return gi_probe->propagation;
  4785. }
  4786. void RendererStorageRD::gi_probe_set_energy(RID p_gi_probe, float p_energy) {
  4787. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4788. ERR_FAIL_COND(!gi_probe);
  4789. gi_probe->energy = p_energy;
  4790. }
  4791. float RendererStorageRD::gi_probe_get_energy(RID p_gi_probe) const {
  4792. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4793. ERR_FAIL_COND_V(!gi_probe, 0);
  4794. return gi_probe->energy;
  4795. }
  4796. void RendererStorageRD::gi_probe_set_ao(RID p_gi_probe, float p_ao) {
  4797. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4798. ERR_FAIL_COND(!gi_probe);
  4799. gi_probe->ao = p_ao;
  4800. }
  4801. float RendererStorageRD::gi_probe_get_ao(RID p_gi_probe) const {
  4802. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4803. ERR_FAIL_COND_V(!gi_probe, 0);
  4804. return gi_probe->ao;
  4805. }
  4806. void RendererStorageRD::gi_probe_set_ao_size(RID p_gi_probe, float p_strength) {
  4807. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4808. ERR_FAIL_COND(!gi_probe);
  4809. gi_probe->ao_size = p_strength;
  4810. }
  4811. float RendererStorageRD::gi_probe_get_ao_size(RID p_gi_probe) const {
  4812. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4813. ERR_FAIL_COND_V(!gi_probe, 0);
  4814. return gi_probe->ao_size;
  4815. }
  4816. void RendererStorageRD::gi_probe_set_bias(RID p_gi_probe, float p_bias) {
  4817. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4818. ERR_FAIL_COND(!gi_probe);
  4819. gi_probe->bias = p_bias;
  4820. }
  4821. float RendererStorageRD::gi_probe_get_bias(RID p_gi_probe) const {
  4822. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4823. ERR_FAIL_COND_V(!gi_probe, 0);
  4824. return gi_probe->bias;
  4825. }
  4826. void RendererStorageRD::gi_probe_set_normal_bias(RID p_gi_probe, float p_normal_bias) {
  4827. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4828. ERR_FAIL_COND(!gi_probe);
  4829. gi_probe->normal_bias = p_normal_bias;
  4830. }
  4831. float RendererStorageRD::gi_probe_get_normal_bias(RID p_gi_probe) const {
  4832. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4833. ERR_FAIL_COND_V(!gi_probe, 0);
  4834. return gi_probe->normal_bias;
  4835. }
  4836. void RendererStorageRD::gi_probe_set_anisotropy_strength(RID p_gi_probe, float p_strength) {
  4837. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4838. ERR_FAIL_COND(!gi_probe);
  4839. gi_probe->anisotropy_strength = p_strength;
  4840. }
  4841. float RendererStorageRD::gi_probe_get_anisotropy_strength(RID p_gi_probe) const {
  4842. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4843. ERR_FAIL_COND_V(!gi_probe, 0);
  4844. return gi_probe->anisotropy_strength;
  4845. }
  4846. void RendererStorageRD::gi_probe_set_interior(RID p_gi_probe, bool p_enable) {
  4847. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4848. ERR_FAIL_COND(!gi_probe);
  4849. gi_probe->interior = p_enable;
  4850. }
  4851. void RendererStorageRD::gi_probe_set_use_two_bounces(RID p_gi_probe, bool p_enable) {
  4852. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4853. ERR_FAIL_COND(!gi_probe);
  4854. gi_probe->use_two_bounces = p_enable;
  4855. gi_probe->version++;
  4856. }
  4857. bool RendererStorageRD::gi_probe_is_using_two_bounces(RID p_gi_probe) const {
  4858. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4859. ERR_FAIL_COND_V(!gi_probe, false);
  4860. return gi_probe->use_two_bounces;
  4861. }
  4862. bool RendererStorageRD::gi_probe_is_interior(RID p_gi_probe) const {
  4863. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4864. ERR_FAIL_COND_V(!gi_probe, 0);
  4865. return gi_probe->interior;
  4866. }
  4867. uint32_t RendererStorageRD::gi_probe_get_version(RID p_gi_probe) {
  4868. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4869. ERR_FAIL_COND_V(!gi_probe, 0);
  4870. return gi_probe->version;
  4871. }
  4872. uint32_t RendererStorageRD::gi_probe_get_data_version(RID p_gi_probe) {
  4873. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4874. ERR_FAIL_COND_V(!gi_probe, 0);
  4875. return gi_probe->data_version;
  4876. }
  4877. RID RendererStorageRD::gi_probe_get_octree_buffer(RID p_gi_probe) const {
  4878. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4879. ERR_FAIL_COND_V(!gi_probe, RID());
  4880. return gi_probe->octree_buffer;
  4881. }
  4882. RID RendererStorageRD::gi_probe_get_data_buffer(RID p_gi_probe) const {
  4883. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4884. ERR_FAIL_COND_V(!gi_probe, RID());
  4885. return gi_probe->data_buffer;
  4886. }
  4887. RID RendererStorageRD::gi_probe_get_sdf_texture(RID p_gi_probe) {
  4888. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4889. ERR_FAIL_COND_V(!gi_probe, RID());
  4890. return gi_probe->sdf_texture;
  4891. }
  4892. /* LIGHTMAP API */
  4893. RID RendererStorageRD::lightmap_create() {
  4894. return lightmap_owner.make_rid(Lightmap());
  4895. }
  4896. void RendererStorageRD::lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics) {
  4897. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4898. ERR_FAIL_COND(!lm);
  4899. lightmap_array_version++;
  4900. //erase lightmap users
  4901. if (lm->light_texture.is_valid()) {
  4902. Texture *t = texture_owner.getornull(lm->light_texture);
  4903. if (t) {
  4904. t->lightmap_users.erase(p_lightmap);
  4905. }
  4906. }
  4907. Texture *t = texture_owner.getornull(p_light);
  4908. lm->light_texture = p_light;
  4909. lm->uses_spherical_harmonics = p_uses_spherical_haromics;
  4910. RID default_2d_array = default_rd_textures[DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE];
  4911. if (!t) {
  4912. if (using_lightmap_array) {
  4913. if (lm->array_index >= 0) {
  4914. lightmap_textures.write[lm->array_index] = default_2d_array;
  4915. lm->array_index = -1;
  4916. }
  4917. }
  4918. return;
  4919. }
  4920. t->lightmap_users.insert(p_lightmap);
  4921. if (using_lightmap_array) {
  4922. if (lm->array_index < 0) {
  4923. //not in array, try to put in array
  4924. for (int i = 0; i < lightmap_textures.size(); i++) {
  4925. if (lightmap_textures[i] == default_2d_array) {
  4926. lm->array_index = i;
  4927. break;
  4928. }
  4929. }
  4930. }
  4931. ERR_FAIL_COND_MSG(lm->array_index < 0, "Maximum amount of lightmaps in use (" + itos(lightmap_textures.size()) + ") has been exceeded, lightmap will nod display properly.");
  4932. lightmap_textures.write[lm->array_index] = t->rd_texture;
  4933. }
  4934. }
  4935. void RendererStorageRD::lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds) {
  4936. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4937. ERR_FAIL_COND(!lm);
  4938. lm->bounds = p_bounds;
  4939. }
  4940. void RendererStorageRD::lightmap_set_probe_interior(RID p_lightmap, bool p_interior) {
  4941. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4942. ERR_FAIL_COND(!lm);
  4943. lm->interior = p_interior;
  4944. }
  4945. void RendererStorageRD::lightmap_set_probe_capture_data(RID p_lightmap, const PackedVector3Array &p_points, const PackedColorArray &p_point_sh, const PackedInt32Array &p_tetrahedra, const PackedInt32Array &p_bsp_tree) {
  4946. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4947. ERR_FAIL_COND(!lm);
  4948. if (p_points.size()) {
  4949. ERR_FAIL_COND(p_points.size() * 9 != p_point_sh.size());
  4950. ERR_FAIL_COND((p_tetrahedra.size() % 4) != 0);
  4951. ERR_FAIL_COND((p_bsp_tree.size() % 6) != 0);
  4952. }
  4953. lm->points = p_points;
  4954. lm->bsp_tree = p_bsp_tree;
  4955. lm->point_sh = p_point_sh;
  4956. lm->tetrahedra = p_tetrahedra;
  4957. }
  4958. PackedVector3Array RendererStorageRD::lightmap_get_probe_capture_points(RID p_lightmap) const {
  4959. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4960. ERR_FAIL_COND_V(!lm, PackedVector3Array());
  4961. return lm->points;
  4962. }
  4963. PackedColorArray RendererStorageRD::lightmap_get_probe_capture_sh(RID p_lightmap) const {
  4964. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4965. ERR_FAIL_COND_V(!lm, PackedColorArray());
  4966. return lm->point_sh;
  4967. }
  4968. PackedInt32Array RendererStorageRD::lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const {
  4969. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4970. ERR_FAIL_COND_V(!lm, PackedInt32Array());
  4971. return lm->tetrahedra;
  4972. }
  4973. PackedInt32Array RendererStorageRD::lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const {
  4974. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4975. ERR_FAIL_COND_V(!lm, PackedInt32Array());
  4976. return lm->bsp_tree;
  4977. }
  4978. void RendererStorageRD::lightmap_set_probe_capture_update_speed(float p_speed) {
  4979. lightmap_probe_capture_update_speed = p_speed;
  4980. }
  4981. void RendererStorageRD::lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh) {
  4982. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4983. ERR_FAIL_COND(!lm);
  4984. for (int i = 0; i < 9; i++) {
  4985. r_sh[i] = Color(0, 0, 0, 0);
  4986. }
  4987. if (!lm->points.size() || !lm->bsp_tree.size() || !lm->tetrahedra.size()) {
  4988. return;
  4989. }
  4990. static_assert(sizeof(Lightmap::BSP) == 24);
  4991. const Lightmap::BSP *bsp = (const Lightmap::BSP *)lm->bsp_tree.ptr();
  4992. int32_t node = 0;
  4993. while (node >= 0) {
  4994. if (Plane(bsp[node].plane[0], bsp[node].plane[1], bsp[node].plane[2], bsp[node].plane[3]).is_point_over(p_point)) {
  4995. #ifdef DEBUG_ENABLED
  4996. ERR_FAIL_COND(bsp[node].over >= 0 && bsp[node].over < node);
  4997. #endif
  4998. node = bsp[node].over;
  4999. } else {
  5000. #ifdef DEBUG_ENABLED
  5001. ERR_FAIL_COND(bsp[node].under >= 0 && bsp[node].under < node);
  5002. #endif
  5003. node = bsp[node].under;
  5004. }
  5005. }
  5006. if (node == Lightmap::BSP::EMPTY_LEAF) {
  5007. return; //nothing could be done
  5008. }
  5009. node = ABS(node) - 1;
  5010. uint32_t *tetrahedron = (uint32_t *)&lm->tetrahedra[node * 4];
  5011. Vector3 points[4] = { lm->points[tetrahedron[0]], lm->points[tetrahedron[1]], lm->points[tetrahedron[2]], lm->points[tetrahedron[3]] };
  5012. const Color *sh_colors[4]{ &lm->point_sh[tetrahedron[0] * 9], &lm->point_sh[tetrahedron[1] * 9], &lm->point_sh[tetrahedron[2] * 9], &lm->point_sh[tetrahedron[3] * 9] };
  5013. Color barycentric = Geometry3D::tetrahedron_get_barycentric_coords(points[0], points[1], points[2], points[3], p_point);
  5014. for (int i = 0; i < 4; i++) {
  5015. float c = CLAMP(barycentric[i], 0.0, 1.0);
  5016. for (int j = 0; j < 9; j++) {
  5017. r_sh[j] += sh_colors[i][j] * c;
  5018. }
  5019. }
  5020. }
  5021. bool RendererStorageRD::lightmap_is_interior(RID p_lightmap) const {
  5022. const Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  5023. ERR_FAIL_COND_V(!lm, false);
  5024. return lm->interior;
  5025. }
  5026. AABB RendererStorageRD::lightmap_get_aabb(RID p_lightmap) const {
  5027. const Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  5028. ERR_FAIL_COND_V(!lm, AABB());
  5029. return lm->bounds;
  5030. }
  5031. /* RENDER TARGET API */
  5032. void RendererStorageRD::_clear_render_target(RenderTarget *rt) {
  5033. //free in reverse dependency order
  5034. if (rt->framebuffer.is_valid()) {
  5035. RD::get_singleton()->free(rt->framebuffer);
  5036. rt->framebuffer_uniform_set = RID(); //chain deleted
  5037. }
  5038. if (rt->color.is_valid()) {
  5039. RD::get_singleton()->free(rt->color);
  5040. }
  5041. if (rt->backbuffer.is_valid()) {
  5042. RD::get_singleton()->free(rt->backbuffer);
  5043. rt->backbuffer = RID();
  5044. for (int i = 0; i < rt->backbuffer_mipmaps.size(); i++) {
  5045. //just erase copies, since the rest are erased by dependency
  5046. RD::get_singleton()->free(rt->backbuffer_mipmaps[i].mipmap_copy);
  5047. }
  5048. rt->backbuffer_mipmaps.clear();
  5049. rt->backbuffer_uniform_set = RID(); //chain deleted
  5050. }
  5051. _render_target_clear_sdf(rt);
  5052. rt->framebuffer = RID();
  5053. rt->color = RID();
  5054. }
  5055. void RendererStorageRD::_update_render_target(RenderTarget *rt) {
  5056. if (rt->texture.is_null()) {
  5057. //create a placeholder until updated
  5058. rt->texture = texture_2d_placeholder_create();
  5059. Texture *tex = texture_owner.getornull(rt->texture);
  5060. tex->is_render_target = true;
  5061. }
  5062. _clear_render_target(rt);
  5063. if (rt->size.width == 0 || rt->size.height == 0) {
  5064. return;
  5065. }
  5066. //until we implement support for HDR monitors (and render target is attached to screen), this is enough.
  5067. rt->color_format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  5068. rt->color_format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  5069. rt->image_format = rt->flags[RENDER_TARGET_TRANSPARENT] ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8;
  5070. RD::TextureFormat rd_format;
  5071. RD::TextureView rd_view;
  5072. { //attempt register
  5073. rd_format.format = rt->color_format;
  5074. rd_format.width = rt->size.width;
  5075. rd_format.height = rt->size.height;
  5076. rd_format.depth = 1;
  5077. rd_format.array_layers = 1;
  5078. rd_format.mipmaps = 1;
  5079. rd_format.type = RD::TEXTURE_TYPE_2D;
  5080. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  5081. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  5082. rd_format.shareable_formats.push_back(rt->color_format);
  5083. rd_format.shareable_formats.push_back(rt->color_format_srgb);
  5084. }
  5085. rt->color = RD::get_singleton()->texture_create(rd_format, rd_view);
  5086. ERR_FAIL_COND(rt->color.is_null());
  5087. Vector<RID> fb_textures;
  5088. fb_textures.push_back(rt->color);
  5089. rt->framebuffer = RD::get_singleton()->framebuffer_create(fb_textures);
  5090. if (rt->framebuffer.is_null()) {
  5091. _clear_render_target(rt);
  5092. ERR_FAIL_COND(rt->framebuffer.is_null());
  5093. }
  5094. { //update texture
  5095. Texture *tex = texture_owner.getornull(rt->texture);
  5096. //free existing textures
  5097. if (RD::get_singleton()->texture_is_valid(tex->rd_texture)) {
  5098. RD::get_singleton()->free(tex->rd_texture);
  5099. }
  5100. if (RD::get_singleton()->texture_is_valid(tex->rd_texture_srgb)) {
  5101. RD::get_singleton()->free(tex->rd_texture_srgb);
  5102. }
  5103. tex->rd_texture = RID();
  5104. tex->rd_texture_srgb = RID();
  5105. //create shared textures to the color buffer,
  5106. //so transparent can be supported
  5107. RD::TextureView view;
  5108. view.format_override = rt->color_format;
  5109. if (!rt->flags[RENDER_TARGET_TRANSPARENT]) {
  5110. view.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  5111. }
  5112. tex->rd_texture = RD::get_singleton()->texture_create_shared(view, rt->color);
  5113. if (rt->color_format_srgb != RD::DATA_FORMAT_MAX) {
  5114. view.format_override = rt->color_format_srgb;
  5115. tex->rd_texture_srgb = RD::get_singleton()->texture_create_shared(view, rt->color);
  5116. }
  5117. tex->rd_view = view;
  5118. tex->width = rt->size.width;
  5119. tex->height = rt->size.height;
  5120. tex->width_2d = rt->size.width;
  5121. tex->height_2d = rt->size.height;
  5122. tex->rd_format = rt->color_format;
  5123. tex->rd_format_srgb = rt->color_format_srgb;
  5124. tex->format = rt->image_format;
  5125. Vector<RID> proxies = tex->proxies; //make a copy, since update may change it
  5126. for (int i = 0; i < proxies.size(); i++) {
  5127. texture_proxy_update(proxies[i], rt->texture);
  5128. }
  5129. }
  5130. }
  5131. void RendererStorageRD::_create_render_target_backbuffer(RenderTarget *rt) {
  5132. ERR_FAIL_COND(rt->backbuffer.is_valid());
  5133. uint32_t mipmaps_required = Image::get_image_required_mipmaps(rt->size.width, rt->size.height, Image::FORMAT_RGBA8);
  5134. RD::TextureFormat tf;
  5135. tf.format = rt->color_format;
  5136. tf.width = rt->size.width;
  5137. tf.height = rt->size.height;
  5138. tf.type = RD::TEXTURE_TYPE_2D;
  5139. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  5140. tf.mipmaps = mipmaps_required;
  5141. rt->backbuffer = RD::get_singleton()->texture_create(tf, RD::TextureView());
  5142. rt->backbuffer_mipmap0 = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->backbuffer, 0, 0);
  5143. {
  5144. Vector<RID> fb_tex;
  5145. fb_tex.push_back(rt->backbuffer_mipmap0);
  5146. rt->backbuffer_fb = RD::get_singleton()->framebuffer_create(fb_tex);
  5147. }
  5148. if (rt->framebuffer_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(rt->framebuffer_uniform_set)) {
  5149. //the new one will require the backbuffer.
  5150. RD::get_singleton()->free(rt->framebuffer_uniform_set);
  5151. rt->framebuffer_uniform_set = RID();
  5152. }
  5153. //create mipmaps
  5154. for (uint32_t i = 1; i < mipmaps_required; i++) {
  5155. RenderTarget::BackbufferMipmap mm;
  5156. {
  5157. mm.mipmap = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->backbuffer, 0, i);
  5158. }
  5159. {
  5160. Size2 mm_size = Image::get_image_mipmap_size(tf.width, tf.height, Image::FORMAT_RGBA8, i);
  5161. RD::TextureFormat mmtf = tf;
  5162. mmtf.width = mm_size.width;
  5163. mmtf.height = mm_size.height;
  5164. mmtf.mipmaps = 1;
  5165. mm.mipmap_copy = RD::get_singleton()->texture_create(mmtf, RD::TextureView());
  5166. }
  5167. rt->backbuffer_mipmaps.push_back(mm);
  5168. }
  5169. }
  5170. RID RendererStorageRD::render_target_create() {
  5171. RenderTarget render_target;
  5172. render_target.was_used = false;
  5173. render_target.clear_requested = false;
  5174. for (int i = 0; i < RENDER_TARGET_FLAG_MAX; i++) {
  5175. render_target.flags[i] = false;
  5176. }
  5177. _update_render_target(&render_target);
  5178. return render_target_owner.make_rid(render_target);
  5179. }
  5180. void RendererStorageRD::render_target_set_position(RID p_render_target, int p_x, int p_y) {
  5181. //unused for this render target
  5182. }
  5183. void RendererStorageRD::render_target_set_size(RID p_render_target, int p_width, int p_height) {
  5184. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5185. ERR_FAIL_COND(!rt);
  5186. rt->size.x = p_width;
  5187. rt->size.y = p_height;
  5188. _update_render_target(rt);
  5189. }
  5190. RID RendererStorageRD::render_target_get_texture(RID p_render_target) {
  5191. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5192. ERR_FAIL_COND_V(!rt, RID());
  5193. return rt->texture;
  5194. }
  5195. void RendererStorageRD::render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id) {
  5196. }
  5197. void RendererStorageRD::render_target_set_flag(RID p_render_target, RenderTargetFlags p_flag, bool p_value) {
  5198. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5199. ERR_FAIL_COND(!rt);
  5200. rt->flags[p_flag] = p_value;
  5201. _update_render_target(rt);
  5202. }
  5203. bool RendererStorageRD::render_target_was_used(RID p_render_target) {
  5204. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5205. ERR_FAIL_COND_V(!rt, false);
  5206. return rt->was_used;
  5207. }
  5208. void RendererStorageRD::render_target_set_as_unused(RID p_render_target) {
  5209. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5210. ERR_FAIL_COND(!rt);
  5211. rt->was_used = false;
  5212. }
  5213. Size2 RendererStorageRD::render_target_get_size(RID p_render_target) {
  5214. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5215. ERR_FAIL_COND_V(!rt, Size2());
  5216. return rt->size;
  5217. }
  5218. RID RendererStorageRD::render_target_get_rd_framebuffer(RID p_render_target) {
  5219. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5220. ERR_FAIL_COND_V(!rt, RID());
  5221. return rt->framebuffer;
  5222. }
  5223. RID RendererStorageRD::render_target_get_rd_texture(RID p_render_target) {
  5224. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5225. ERR_FAIL_COND_V(!rt, RID());
  5226. return rt->color;
  5227. }
  5228. RID RendererStorageRD::render_target_get_rd_backbuffer(RID p_render_target) {
  5229. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5230. ERR_FAIL_COND_V(!rt, RID());
  5231. return rt->backbuffer;
  5232. }
  5233. RID RendererStorageRD::render_target_get_rd_backbuffer_framebuffer(RID p_render_target) {
  5234. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5235. ERR_FAIL_COND_V(!rt, RID());
  5236. if (!rt->backbuffer.is_valid()) {
  5237. _create_render_target_backbuffer(rt);
  5238. }
  5239. return rt->backbuffer_fb;
  5240. }
  5241. void RendererStorageRD::render_target_request_clear(RID p_render_target, const Color &p_clear_color) {
  5242. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5243. ERR_FAIL_COND(!rt);
  5244. rt->clear_requested = true;
  5245. rt->clear_color = p_clear_color;
  5246. }
  5247. bool RendererStorageRD::render_target_is_clear_requested(RID p_render_target) {
  5248. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5249. ERR_FAIL_COND_V(!rt, false);
  5250. return rt->clear_requested;
  5251. }
  5252. Color RendererStorageRD::render_target_get_clear_request_color(RID p_render_target) {
  5253. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5254. ERR_FAIL_COND_V(!rt, Color());
  5255. return rt->clear_color;
  5256. }
  5257. void RendererStorageRD::render_target_disable_clear_request(RID p_render_target) {
  5258. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5259. ERR_FAIL_COND(!rt);
  5260. rt->clear_requested = false;
  5261. }
  5262. void RendererStorageRD::render_target_do_clear_request(RID p_render_target) {
  5263. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5264. ERR_FAIL_COND(!rt);
  5265. if (!rt->clear_requested) {
  5266. return;
  5267. }
  5268. Vector<Color> clear_colors;
  5269. clear_colors.push_back(rt->clear_color);
  5270. RD::get_singleton()->draw_list_begin(rt->framebuffer, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD, clear_colors);
  5271. RD::get_singleton()->draw_list_end();
  5272. rt->clear_requested = false;
  5273. }
  5274. void RendererStorageRD::render_target_set_sdf_size_and_scale(RID p_render_target, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale) {
  5275. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5276. ERR_FAIL_COND(!rt);
  5277. if (rt->sdf_oversize == p_size && rt->sdf_scale == p_scale) {
  5278. return;
  5279. }
  5280. rt->sdf_oversize = p_size;
  5281. rt->sdf_scale = p_scale;
  5282. _render_target_clear_sdf(rt);
  5283. }
  5284. Rect2i RendererStorageRD::_render_target_get_sdf_rect(const RenderTarget *rt) const {
  5285. Size2i margin;
  5286. int scale;
  5287. switch (rt->sdf_oversize) {
  5288. case RS::VIEWPORT_SDF_OVERSIZE_100_PERCENT: {
  5289. scale = 100;
  5290. } break;
  5291. case RS::VIEWPORT_SDF_OVERSIZE_120_PERCENT: {
  5292. scale = 120;
  5293. } break;
  5294. case RS::VIEWPORT_SDF_OVERSIZE_150_PERCENT: {
  5295. scale = 150;
  5296. } break;
  5297. case RS::VIEWPORT_SDF_OVERSIZE_200_PERCENT: {
  5298. scale = 200;
  5299. } break;
  5300. default: {
  5301. }
  5302. }
  5303. margin = (rt->size * scale / 100) - rt->size;
  5304. Rect2i r(Vector2i(), rt->size);
  5305. r.position -= margin;
  5306. r.size += margin * 2;
  5307. return r;
  5308. }
  5309. Rect2i RendererStorageRD::render_target_get_sdf_rect(RID p_render_target) const {
  5310. const RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5311. ERR_FAIL_COND_V(!rt, Rect2i());
  5312. return _render_target_get_sdf_rect(rt);
  5313. }
  5314. RID RendererStorageRD::render_target_get_sdf_texture(RID p_render_target) {
  5315. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5316. ERR_FAIL_COND_V(!rt, RID());
  5317. if (rt->sdf_buffer_read.is_null()) {
  5318. // no texture, create a dummy one for the 2D uniform set
  5319. RD::TextureFormat tformat;
  5320. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  5321. tformat.width = 4;
  5322. tformat.height = 4;
  5323. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT;
  5324. tformat.type = RD::TEXTURE_TYPE_2D;
  5325. Vector<uint8_t> pv;
  5326. pv.resize(16 * 4);
  5327. zeromem(pv.ptrw(), 16 * 4);
  5328. Vector<Vector<uint8_t>> vpv;
  5329. rt->sdf_buffer_read = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  5330. }
  5331. return rt->sdf_buffer_read;
  5332. }
  5333. void RendererStorageRD::_render_target_allocate_sdf(RenderTarget *rt) {
  5334. ERR_FAIL_COND(rt->sdf_buffer_write_fb.is_valid());
  5335. if (rt->sdf_buffer_read.is_valid()) {
  5336. RD::get_singleton()->free(rt->sdf_buffer_read);
  5337. rt->sdf_buffer_read = RID();
  5338. }
  5339. Size2i size = _render_target_get_sdf_rect(rt).size;
  5340. RD::TextureFormat tformat;
  5341. tformat.format = RD::DATA_FORMAT_R8_UNORM;
  5342. tformat.width = size.width;
  5343. tformat.height = size.height;
  5344. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  5345. tformat.type = RD::TEXTURE_TYPE_2D;
  5346. rt->sdf_buffer_write = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  5347. {
  5348. Vector<RID> write_fb;
  5349. write_fb.push_back(rt->sdf_buffer_write);
  5350. rt->sdf_buffer_write_fb = RD::get_singleton()->framebuffer_create(write_fb);
  5351. }
  5352. int scale;
  5353. switch (rt->sdf_scale) {
  5354. case RS::VIEWPORT_SDF_SCALE_100_PERCENT: {
  5355. scale = 100;
  5356. } break;
  5357. case RS::VIEWPORT_SDF_SCALE_50_PERCENT: {
  5358. scale = 50;
  5359. } break;
  5360. case RS::VIEWPORT_SDF_SCALE_25_PERCENT: {
  5361. scale = 25;
  5362. } break;
  5363. default: {
  5364. scale = 100;
  5365. } break;
  5366. }
  5367. rt->process_size = size * scale / 100;
  5368. rt->process_size.x = MAX(rt->process_size.x, 1);
  5369. rt->process_size.y = MAX(rt->process_size.y, 1);
  5370. tformat.format = RD::DATA_FORMAT_R16G16_UINT;
  5371. tformat.width = rt->process_size.width;
  5372. tformat.height = rt->process_size.height;
  5373. tformat.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  5374. rt->sdf_buffer_process[0] = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  5375. rt->sdf_buffer_process[1] = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  5376. tformat.format = RD::DATA_FORMAT_R16_UNORM;
  5377. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  5378. rt->sdf_buffer_read = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  5379. {
  5380. Vector<RD::Uniform> uniforms;
  5381. {
  5382. RD::Uniform u;
  5383. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  5384. u.binding = 1;
  5385. u.ids.push_back(rt->sdf_buffer_write);
  5386. uniforms.push_back(u);
  5387. }
  5388. {
  5389. RD::Uniform u;
  5390. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  5391. u.binding = 2;
  5392. u.ids.push_back(rt->sdf_buffer_read);
  5393. uniforms.push_back(u);
  5394. }
  5395. {
  5396. RD::Uniform u;
  5397. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  5398. u.binding = 3;
  5399. u.ids.push_back(rt->sdf_buffer_process[0]);
  5400. uniforms.push_back(u);
  5401. }
  5402. {
  5403. RD::Uniform u;
  5404. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  5405. u.binding = 4;
  5406. u.ids.push_back(rt->sdf_buffer_process[1]);
  5407. uniforms.push_back(u);
  5408. }
  5409. rt->sdf_buffer_process_uniform_sets[0] = RD::get_singleton()->uniform_set_create(uniforms, rt_sdf.shader.version_get_shader(rt_sdf.shader_version, 0), 0);
  5410. SWAP(uniforms.write[2].ids.write[0], uniforms.write[3].ids.write[0]);
  5411. rt->sdf_buffer_process_uniform_sets[1] = RD::get_singleton()->uniform_set_create(uniforms, rt_sdf.shader.version_get_shader(rt_sdf.shader_version, 0), 0);
  5412. }
  5413. }
  5414. void RendererStorageRD::_render_target_clear_sdf(RenderTarget *rt) {
  5415. if (rt->sdf_buffer_read.is_valid()) {
  5416. RD::get_singleton()->free(rt->sdf_buffer_read);
  5417. rt->sdf_buffer_read = RID();
  5418. }
  5419. if (rt->sdf_buffer_write_fb.is_valid()) {
  5420. RD::get_singleton()->free(rt->sdf_buffer_write);
  5421. RD::get_singleton()->free(rt->sdf_buffer_process[0]);
  5422. RD::get_singleton()->free(rt->sdf_buffer_process[1]);
  5423. rt->sdf_buffer_write = RID();
  5424. rt->sdf_buffer_write_fb = RID();
  5425. rt->sdf_buffer_process[0] = RID();
  5426. rt->sdf_buffer_process[1] = RID();
  5427. rt->sdf_buffer_process_uniform_sets[0] = RID();
  5428. rt->sdf_buffer_process_uniform_sets[1] = RID();
  5429. }
  5430. }
  5431. RID RendererStorageRD::render_target_get_sdf_framebuffer(RID p_render_target) {
  5432. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5433. ERR_FAIL_COND_V(!rt, RID());
  5434. if (rt->sdf_buffer_write_fb.is_null()) {
  5435. _render_target_allocate_sdf(rt);
  5436. }
  5437. return rt->sdf_buffer_write_fb;
  5438. }
  5439. void RendererStorageRD::render_target_sdf_process(RID p_render_target) {
  5440. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5441. ERR_FAIL_COND(!rt);
  5442. ERR_FAIL_COND(rt->sdf_buffer_write_fb.is_null());
  5443. RenderTargetSDF::PushConstant push_constant;
  5444. Rect2i r = _render_target_get_sdf_rect(rt);
  5445. push_constant.size[0] = r.size.width;
  5446. push_constant.size[1] = r.size.height;
  5447. push_constant.stride = 0;
  5448. push_constant.shift = 0;
  5449. push_constant.base_size[0] = r.size.width;
  5450. push_constant.base_size[1] = r.size.height;
  5451. bool shrink = false;
  5452. switch (rt->sdf_scale) {
  5453. case RS::VIEWPORT_SDF_SCALE_50_PERCENT: {
  5454. push_constant.size[0] >>= 1;
  5455. push_constant.size[1] >>= 1;
  5456. push_constant.shift = 1;
  5457. shrink = true;
  5458. } break;
  5459. case RS::VIEWPORT_SDF_SCALE_25_PERCENT: {
  5460. push_constant.size[0] >>= 2;
  5461. push_constant.size[1] >>= 2;
  5462. push_constant.shift = 2;
  5463. shrink = true;
  5464. } break;
  5465. default: {
  5466. };
  5467. }
  5468. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  5469. /* Load */
  5470. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, rt_sdf.pipelines[shrink ? RenderTargetSDF::SHADER_LOAD_SHRINK : RenderTargetSDF::SHADER_LOAD]);
  5471. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rt->sdf_buffer_process_uniform_sets[1], 0); //fill [0]
  5472. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(RenderTargetSDF::PushConstant));
  5473. RD::get_singleton()->compute_list_dispatch_threads(compute_list, push_constant.size[0], push_constant.size[1], 1, 8, 8, 1);
  5474. /* Process */
  5475. int stride = nearest_power_of_2_templated(MAX(push_constant.size[0], push_constant.size[1]) / 2);
  5476. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, rt_sdf.pipelines[RenderTargetSDF::SHADER_PROCESS]);
  5477. RD::get_singleton()->compute_list_add_barrier(compute_list);
  5478. bool swap = false;
  5479. //jumpflood
  5480. while (stride > 0) {
  5481. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rt->sdf_buffer_process_uniform_sets[swap ? 1 : 0], 0);
  5482. push_constant.stride = stride;
  5483. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(RenderTargetSDF::PushConstant));
  5484. RD::get_singleton()->compute_list_dispatch_threads(compute_list, push_constant.size[0], push_constant.size[1], 1, 8, 8, 1);
  5485. stride /= 2;
  5486. swap = !swap;
  5487. RD::get_singleton()->compute_list_add_barrier(compute_list);
  5488. }
  5489. /* Store */
  5490. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, rt_sdf.pipelines[shrink ? RenderTargetSDF::SHADER_STORE_SHRINK : RenderTargetSDF::SHADER_STORE]);
  5491. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rt->sdf_buffer_process_uniform_sets[swap ? 1 : 0], 0);
  5492. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(RenderTargetSDF::PushConstant));
  5493. RD::get_singleton()->compute_list_dispatch_threads(compute_list, push_constant.size[0], push_constant.size[1], 1, 8, 8, 1);
  5494. RD::get_singleton()->compute_list_end();
  5495. }
  5496. void RendererStorageRD::render_target_copy_to_back_buffer(RID p_render_target, const Rect2i &p_region, bool p_gen_mipmaps) {
  5497. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5498. ERR_FAIL_COND(!rt);
  5499. if (!rt->backbuffer.is_valid()) {
  5500. _create_render_target_backbuffer(rt);
  5501. }
  5502. Rect2i region;
  5503. if (p_region == Rect2i()) {
  5504. region.size = rt->size;
  5505. } else {
  5506. region = Rect2i(Size2i(), rt->size).clip(p_region);
  5507. if (region.size == Size2i()) {
  5508. return; //nothing to do
  5509. }
  5510. }
  5511. //single texture copy for backbuffer
  5512. //RD::get_singleton()->texture_copy(rt->color, rt->backbuffer_mipmap0, Vector3(region.position.x, region.position.y, 0), Vector3(region.position.x, region.position.y, 0), Vector3(region.size.x, region.size.y, 1), 0, 0, 0, 0, true);
  5513. effects.copy_to_rect(rt->color, rt->backbuffer_mipmap0, region, false, false, false, true, true);
  5514. if (!p_gen_mipmaps) {
  5515. return;
  5516. }
  5517. //then mipmap blur
  5518. RID prev_texture = rt->color; //use color, not backbuffer, as bb has mipmaps.
  5519. for (int i = 0; i < rt->backbuffer_mipmaps.size(); i++) {
  5520. region.position.x >>= 1;
  5521. region.position.y >>= 1;
  5522. region.size.x = MAX(1, region.size.x >> 1);
  5523. region.size.y = MAX(1, region.size.y >> 1);
  5524. const RenderTarget::BackbufferMipmap &mm = rt->backbuffer_mipmaps[i];
  5525. effects.gaussian_blur(prev_texture, mm.mipmap, mm.mipmap_copy, region, true);
  5526. prev_texture = mm.mipmap;
  5527. }
  5528. }
  5529. void RendererStorageRD::render_target_clear_back_buffer(RID p_render_target, const Rect2i &p_region, const Color &p_color) {
  5530. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5531. ERR_FAIL_COND(!rt);
  5532. if (!rt->backbuffer.is_valid()) {
  5533. _create_render_target_backbuffer(rt);
  5534. }
  5535. Rect2i region;
  5536. if (p_region == Rect2i()) {
  5537. region.size = rt->size;
  5538. } else {
  5539. region = Rect2i(Size2i(), rt->size).clip(p_region);
  5540. if (region.size == Size2i()) {
  5541. return; //nothing to do
  5542. }
  5543. }
  5544. //single texture copy for backbuffer
  5545. effects.set_color(rt->backbuffer_mipmap0, p_color, region, true);
  5546. }
  5547. void RendererStorageRD::render_target_gen_back_buffer_mipmaps(RID p_render_target, const Rect2i &p_region) {
  5548. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5549. ERR_FAIL_COND(!rt);
  5550. if (!rt->backbuffer.is_valid()) {
  5551. _create_render_target_backbuffer(rt);
  5552. }
  5553. Rect2i region;
  5554. if (p_region == Rect2i()) {
  5555. region.size = rt->size;
  5556. } else {
  5557. region = Rect2i(Size2i(), rt->size).clip(p_region);
  5558. if (region.size == Size2i()) {
  5559. return; //nothing to do
  5560. }
  5561. }
  5562. //then mipmap blur
  5563. RID prev_texture = rt->backbuffer_mipmap0;
  5564. for (int i = 0; i < rt->backbuffer_mipmaps.size(); i++) {
  5565. region.position.x >>= 1;
  5566. region.position.y >>= 1;
  5567. region.size.x = MAX(1, region.size.x >> 1);
  5568. region.size.y = MAX(1, region.size.y >> 1);
  5569. const RenderTarget::BackbufferMipmap &mm = rt->backbuffer_mipmaps[i];
  5570. effects.gaussian_blur(prev_texture, mm.mipmap, mm.mipmap_copy, region, true);
  5571. prev_texture = mm.mipmap;
  5572. }
  5573. }
  5574. RID RendererStorageRD::render_target_get_framebuffer_uniform_set(RID p_render_target) {
  5575. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5576. ERR_FAIL_COND_V(!rt, RID());
  5577. return rt->framebuffer_uniform_set;
  5578. }
  5579. RID RendererStorageRD::render_target_get_backbuffer_uniform_set(RID p_render_target) {
  5580. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5581. ERR_FAIL_COND_V(!rt, RID());
  5582. return rt->backbuffer_uniform_set;
  5583. }
  5584. void RendererStorageRD::render_target_set_framebuffer_uniform_set(RID p_render_target, RID p_uniform_set) {
  5585. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5586. ERR_FAIL_COND(!rt);
  5587. rt->framebuffer_uniform_set = p_uniform_set;
  5588. }
  5589. void RendererStorageRD::render_target_set_backbuffer_uniform_set(RID p_render_target, RID p_uniform_set) {
  5590. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5591. ERR_FAIL_COND(!rt);
  5592. rt->backbuffer_uniform_set = p_uniform_set;
  5593. }
  5594. void RendererStorageRD::base_update_dependency(RID p_base, InstanceBaseDependency *p_instance) {
  5595. if (mesh_owner.owns(p_base)) {
  5596. Mesh *mesh = mesh_owner.getornull(p_base);
  5597. p_instance->update_dependency(&mesh->instance_dependency);
  5598. } else if (multimesh_owner.owns(p_base)) {
  5599. MultiMesh *multimesh = multimesh_owner.getornull(p_base);
  5600. p_instance->update_dependency(&multimesh->instance_dependency);
  5601. if (multimesh->mesh.is_valid()) {
  5602. base_update_dependency(multimesh->mesh, p_instance);
  5603. }
  5604. } else if (reflection_probe_owner.owns(p_base)) {
  5605. ReflectionProbe *rp = reflection_probe_owner.getornull(p_base);
  5606. p_instance->update_dependency(&rp->instance_dependency);
  5607. } else if (decal_owner.owns(p_base)) {
  5608. Decal *decal = decal_owner.getornull(p_base);
  5609. p_instance->update_dependency(&decal->instance_dependency);
  5610. } else if (gi_probe_owner.owns(p_base)) {
  5611. GIProbe *gip = gi_probe_owner.getornull(p_base);
  5612. p_instance->update_dependency(&gip->instance_dependency);
  5613. } else if (lightmap_owner.owns(p_base)) {
  5614. Lightmap *lm = lightmap_owner.getornull(p_base);
  5615. p_instance->update_dependency(&lm->instance_dependency);
  5616. } else if (light_owner.owns(p_base)) {
  5617. Light *l = light_owner.getornull(p_base);
  5618. p_instance->update_dependency(&l->instance_dependency);
  5619. } else if (particles_owner.owns(p_base)) {
  5620. Particles *p = particles_owner.getornull(p_base);
  5621. p_instance->update_dependency(&p->instance_dependency);
  5622. } else if (particles_collision_owner.owns(p_base)) {
  5623. ParticlesCollision *pc = particles_collision_owner.getornull(p_base);
  5624. p_instance->update_dependency(&pc->instance_dependency);
  5625. }
  5626. }
  5627. void RendererStorageRD::skeleton_update_dependency(RID p_skeleton, InstanceBaseDependency *p_instance) {
  5628. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  5629. ERR_FAIL_COND(!skeleton);
  5630. p_instance->update_dependency(&skeleton->instance_dependency);
  5631. }
  5632. RS::InstanceType RendererStorageRD::get_base_type(RID p_rid) const {
  5633. if (mesh_owner.owns(p_rid)) {
  5634. return RS::INSTANCE_MESH;
  5635. }
  5636. if (multimesh_owner.owns(p_rid)) {
  5637. return RS::INSTANCE_MULTIMESH;
  5638. }
  5639. if (reflection_probe_owner.owns(p_rid)) {
  5640. return RS::INSTANCE_REFLECTION_PROBE;
  5641. }
  5642. if (decal_owner.owns(p_rid)) {
  5643. return RS::INSTANCE_DECAL;
  5644. }
  5645. if (gi_probe_owner.owns(p_rid)) {
  5646. return RS::INSTANCE_GI_PROBE;
  5647. }
  5648. if (light_owner.owns(p_rid)) {
  5649. return RS::INSTANCE_LIGHT;
  5650. }
  5651. if (lightmap_owner.owns(p_rid)) {
  5652. return RS::INSTANCE_LIGHTMAP;
  5653. }
  5654. if (particles_owner.owns(p_rid)) {
  5655. return RS::INSTANCE_PARTICLES;
  5656. }
  5657. if (particles_collision_owner.owns(p_rid)) {
  5658. return RS::INSTANCE_PARTICLES_COLLISION;
  5659. }
  5660. return RS::INSTANCE_NONE;
  5661. }
  5662. void RendererStorageRD::texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp) {
  5663. if (!decal_atlas.textures.has(p_texture)) {
  5664. DecalAtlas::Texture t;
  5665. t.users = 1;
  5666. t.panorama_to_dp_users = p_panorama_to_dp ? 1 : 0;
  5667. decal_atlas.textures[p_texture] = t;
  5668. decal_atlas.dirty = true;
  5669. } else {
  5670. DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);
  5671. t->users++;
  5672. if (p_panorama_to_dp) {
  5673. t->panorama_to_dp_users++;
  5674. }
  5675. }
  5676. }
  5677. void RendererStorageRD::texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp) {
  5678. DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);
  5679. ERR_FAIL_COND(!t);
  5680. t->users--;
  5681. if (p_panorama_to_dp) {
  5682. ERR_FAIL_COND(t->panorama_to_dp_users == 0);
  5683. t->panorama_to_dp_users--;
  5684. }
  5685. if (t->users == 0) {
  5686. decal_atlas.textures.erase(p_texture);
  5687. //do not mark it dirty, there is no need to since it remains working
  5688. }
  5689. }
  5690. RID RendererStorageRD::decal_atlas_get_texture() const {
  5691. return decal_atlas.texture;
  5692. }
  5693. RID RendererStorageRD::decal_atlas_get_texture_srgb() const {
  5694. return decal_atlas.texture_srgb;
  5695. }
  5696. void RendererStorageRD::_update_decal_atlas() {
  5697. if (!decal_atlas.dirty) {
  5698. return; //nothing to do
  5699. }
  5700. decal_atlas.dirty = false;
  5701. if (decal_atlas.texture.is_valid()) {
  5702. RD::get_singleton()->free(decal_atlas.texture);
  5703. decal_atlas.texture = RID();
  5704. decal_atlas.texture_srgb = RID();
  5705. decal_atlas.texture_mipmaps.clear();
  5706. }
  5707. int border = 1 << decal_atlas.mipmaps;
  5708. if (decal_atlas.textures.size()) {
  5709. //generate atlas
  5710. Vector<DecalAtlas::SortItem> itemsv;
  5711. itemsv.resize(decal_atlas.textures.size());
  5712. int base_size = 8;
  5713. const RID *K = nullptr;
  5714. int idx = 0;
  5715. while ((K = decal_atlas.textures.next(K))) {
  5716. DecalAtlas::SortItem &si = itemsv.write[idx];
  5717. Texture *src_tex = texture_owner.getornull(*K);
  5718. si.size.width = (src_tex->width / border) + 1;
  5719. si.size.height = (src_tex->height / border) + 1;
  5720. si.pixel_size = Size2i(src_tex->width, src_tex->height);
  5721. if (base_size < si.size.width) {
  5722. base_size = nearest_power_of_2_templated(si.size.width);
  5723. }
  5724. si.texture = *K;
  5725. idx++;
  5726. }
  5727. //sort items by size
  5728. itemsv.sort();
  5729. //attempt to create atlas
  5730. int item_count = itemsv.size();
  5731. DecalAtlas::SortItem *items = itemsv.ptrw();
  5732. int atlas_height = 0;
  5733. while (true) {
  5734. Vector<int> v_offsetsv;
  5735. v_offsetsv.resize(base_size);
  5736. int *v_offsets = v_offsetsv.ptrw();
  5737. zeromem(v_offsets, sizeof(int) * base_size);
  5738. int max_height = 0;
  5739. for (int i = 0; i < item_count; i++) {
  5740. //best fit
  5741. DecalAtlas::SortItem &si = items[i];
  5742. int best_idx = -1;
  5743. int best_height = 0x7FFFFFFF;
  5744. for (int j = 0; j <= base_size - si.size.width; j++) {
  5745. int height = 0;
  5746. for (int k = 0; k < si.size.width; k++) {
  5747. int h = v_offsets[k + j];
  5748. if (h > height) {
  5749. height = h;
  5750. if (height > best_height) {
  5751. break; //already bad
  5752. }
  5753. }
  5754. }
  5755. if (height < best_height) {
  5756. best_height = height;
  5757. best_idx = j;
  5758. }
  5759. }
  5760. //update
  5761. for (int k = 0; k < si.size.width; k++) {
  5762. v_offsets[k + best_idx] = best_height + si.size.height;
  5763. }
  5764. si.pos.x = best_idx;
  5765. si.pos.y = best_height;
  5766. if (si.pos.y + si.size.height > max_height) {
  5767. max_height = si.pos.y + si.size.height;
  5768. }
  5769. }
  5770. if (max_height <= base_size * 2) {
  5771. atlas_height = max_height;
  5772. break; //good ratio, break;
  5773. }
  5774. base_size *= 2;
  5775. }
  5776. decal_atlas.size.width = base_size * border;
  5777. decal_atlas.size.height = nearest_power_of_2_templated(atlas_height * border);
  5778. for (int i = 0; i < item_count; i++) {
  5779. DecalAtlas::Texture *t = decal_atlas.textures.getptr(items[i].texture);
  5780. t->uv_rect.position = items[i].pos * border + Vector2i(border / 2, border / 2);
  5781. t->uv_rect.size = items[i].pixel_size;
  5782. t->uv_rect.position /= Size2(decal_atlas.size);
  5783. t->uv_rect.size /= Size2(decal_atlas.size);
  5784. }
  5785. } else {
  5786. //use border as size, so it at least has enough mipmaps
  5787. decal_atlas.size.width = border;
  5788. decal_atlas.size.height = border;
  5789. }
  5790. //blit textures
  5791. RD::TextureFormat tformat;
  5792. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  5793. tformat.width = decal_atlas.size.width;
  5794. tformat.height = decal_atlas.size.height;
  5795. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  5796. tformat.type = RD::TEXTURE_TYPE_2D;
  5797. tformat.mipmaps = decal_atlas.mipmaps;
  5798. tformat.shareable_formats.push_back(RD::DATA_FORMAT_R8G8B8A8_UNORM);
  5799. tformat.shareable_formats.push_back(RD::DATA_FORMAT_R8G8B8A8_SRGB);
  5800. decal_atlas.texture = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  5801. {
  5802. //create the framebuffer
  5803. Size2i s = decal_atlas.size;
  5804. for (int i = 0; i < decal_atlas.mipmaps; i++) {
  5805. DecalAtlas::MipMap mm;
  5806. mm.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), decal_atlas.texture, 0, i);
  5807. Vector<RID> fb;
  5808. fb.push_back(mm.texture);
  5809. mm.fb = RD::get_singleton()->framebuffer_create(fb);
  5810. mm.size = s;
  5811. decal_atlas.texture_mipmaps.push_back(mm);
  5812. s.width = MAX(1, s.width >> 1);
  5813. s.height = MAX(1, s.height >> 1);
  5814. }
  5815. {
  5816. //create the SRGB variant
  5817. RD::TextureView rd_view;
  5818. rd_view.format_override = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  5819. decal_atlas.texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, decal_atlas.texture);
  5820. }
  5821. }
  5822. RID prev_texture;
  5823. for (int i = 0; i < decal_atlas.texture_mipmaps.size(); i++) {
  5824. const DecalAtlas::MipMap &mm = decal_atlas.texture_mipmaps[i];
  5825. Color clear_color(0, 0, 0, 0);
  5826. if (decal_atlas.textures.size()) {
  5827. if (i == 0) {
  5828. Vector<Color> cc;
  5829. cc.push_back(clear_color);
  5830. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(mm.fb, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_DROP, RD::FINAL_ACTION_DISCARD, cc);
  5831. const RID *K = nullptr;
  5832. while ((K = decal_atlas.textures.next(K))) {
  5833. DecalAtlas::Texture *t = decal_atlas.textures.getptr(*K);
  5834. Texture *src_tex = texture_owner.getornull(*K);
  5835. effects.copy_to_atlas_fb(src_tex->rd_texture, mm.fb, t->uv_rect, draw_list, false, t->panorama_to_dp_users > 0);
  5836. }
  5837. RD::get_singleton()->draw_list_end();
  5838. prev_texture = mm.texture;
  5839. } else {
  5840. effects.copy_to_fb_rect(prev_texture, mm.fb, Rect2i(Point2i(), mm.size));
  5841. prev_texture = mm.texture;
  5842. }
  5843. } else {
  5844. RD::get_singleton()->texture_clear(mm.texture, clear_color, 0, 1, 0, 1, false);
  5845. }
  5846. }
  5847. }
  5848. int32_t RendererStorageRD::_global_variable_allocate(uint32_t p_elements) {
  5849. int32_t idx = 0;
  5850. while (idx + p_elements <= global_variables.buffer_size) {
  5851. if (global_variables.buffer_usage[idx].elements == 0) {
  5852. bool valid = true;
  5853. for (uint32_t i = 1; i < p_elements; i++) {
  5854. if (global_variables.buffer_usage[idx + i].elements > 0) {
  5855. valid = false;
  5856. idx += i + global_variables.buffer_usage[idx + i].elements;
  5857. break;
  5858. }
  5859. }
  5860. if (!valid) {
  5861. continue; //if not valid, idx is in new position
  5862. }
  5863. return idx;
  5864. } else {
  5865. idx += global_variables.buffer_usage[idx].elements;
  5866. }
  5867. }
  5868. return -1;
  5869. }
  5870. void RendererStorageRD::_global_variable_store_in_buffer(int32_t p_index, RS::GlobalVariableType p_type, const Variant &p_value) {
  5871. switch (p_type) {
  5872. case RS::GLOBAL_VAR_TYPE_BOOL: {
  5873. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  5874. bool b = p_value;
  5875. bv.x = b ? 1.0 : 0.0;
  5876. bv.y = 0.0;
  5877. bv.z = 0.0;
  5878. bv.w = 0.0;
  5879. } break;
  5880. case RS::GLOBAL_VAR_TYPE_BVEC2: {
  5881. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  5882. uint32_t bvec = p_value;
  5883. bv.x = (bvec & 1) ? 1.0 : 0.0;
  5884. bv.y = (bvec & 2) ? 1.0 : 0.0;
  5885. bv.z = 0.0;
  5886. bv.w = 0.0;
  5887. } break;
  5888. case RS::GLOBAL_VAR_TYPE_BVEC3: {
  5889. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  5890. uint32_t bvec = p_value;
  5891. bv.x = (bvec & 1) ? 1.0 : 0.0;
  5892. bv.y = (bvec & 2) ? 1.0 : 0.0;
  5893. bv.z = (bvec & 4) ? 1.0 : 0.0;
  5894. bv.w = 0.0;
  5895. } break;
  5896. case RS::GLOBAL_VAR_TYPE_BVEC4: {
  5897. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  5898. uint32_t bvec = p_value;
  5899. bv.x = (bvec & 1) ? 1.0 : 0.0;
  5900. bv.y = (bvec & 2) ? 1.0 : 0.0;
  5901. bv.z = (bvec & 4) ? 1.0 : 0.0;
  5902. bv.w = (bvec & 8) ? 1.0 : 0.0;
  5903. } break;
  5904. case RS::GLOBAL_VAR_TYPE_INT: {
  5905. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  5906. int32_t v = p_value;
  5907. bv.x = v;
  5908. bv.y = 0;
  5909. bv.z = 0;
  5910. bv.w = 0;
  5911. } break;
  5912. case RS::GLOBAL_VAR_TYPE_IVEC2: {
  5913. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  5914. Vector2i v = p_value;
  5915. bv.x = v.x;
  5916. bv.y = v.y;
  5917. bv.z = 0;
  5918. bv.w = 0;
  5919. } break;
  5920. case RS::GLOBAL_VAR_TYPE_IVEC3: {
  5921. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  5922. Vector3i v = p_value;
  5923. bv.x = v.x;
  5924. bv.y = v.y;
  5925. bv.z = v.z;
  5926. bv.w = 0;
  5927. } break;
  5928. case RS::GLOBAL_VAR_TYPE_IVEC4: {
  5929. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  5930. Vector<int32_t> v = p_value;
  5931. bv.x = v.size() >= 1 ? v[0] : 0;
  5932. bv.y = v.size() >= 2 ? v[1] : 0;
  5933. bv.z = v.size() >= 3 ? v[2] : 0;
  5934. bv.w = v.size() >= 4 ? v[3] : 0;
  5935. } break;
  5936. case RS::GLOBAL_VAR_TYPE_RECT2I: {
  5937. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  5938. Rect2i v = p_value;
  5939. bv.x = v.position.x;
  5940. bv.y = v.position.y;
  5941. bv.z = v.size.x;
  5942. bv.w = v.size.y;
  5943. } break;
  5944. case RS::GLOBAL_VAR_TYPE_UINT: {
  5945. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  5946. uint32_t v = p_value;
  5947. bv.x = v;
  5948. bv.y = 0;
  5949. bv.z = 0;
  5950. bv.w = 0;
  5951. } break;
  5952. case RS::GLOBAL_VAR_TYPE_UVEC2: {
  5953. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  5954. Vector2i v = p_value;
  5955. bv.x = v.x;
  5956. bv.y = v.y;
  5957. bv.z = 0;
  5958. bv.w = 0;
  5959. } break;
  5960. case RS::GLOBAL_VAR_TYPE_UVEC3: {
  5961. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  5962. Vector3i v = p_value;
  5963. bv.x = v.x;
  5964. bv.y = v.y;
  5965. bv.z = v.z;
  5966. bv.w = 0;
  5967. } break;
  5968. case RS::GLOBAL_VAR_TYPE_UVEC4: {
  5969. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  5970. Vector<int32_t> v = p_value;
  5971. bv.x = v.size() >= 1 ? v[0] : 0;
  5972. bv.y = v.size() >= 2 ? v[1] : 0;
  5973. bv.z = v.size() >= 3 ? v[2] : 0;
  5974. bv.w = v.size() >= 4 ? v[3] : 0;
  5975. } break;
  5976. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  5977. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  5978. float v = p_value;
  5979. bv.x = v;
  5980. bv.y = 0;
  5981. bv.z = 0;
  5982. bv.w = 0;
  5983. } break;
  5984. case RS::GLOBAL_VAR_TYPE_VEC2: {
  5985. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  5986. Vector2 v = p_value;
  5987. bv.x = v.x;
  5988. bv.y = v.y;
  5989. bv.z = 0;
  5990. bv.w = 0;
  5991. } break;
  5992. case RS::GLOBAL_VAR_TYPE_VEC3: {
  5993. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  5994. Vector3 v = p_value;
  5995. bv.x = v.x;
  5996. bv.y = v.y;
  5997. bv.z = v.z;
  5998. bv.w = 0;
  5999. } break;
  6000. case RS::GLOBAL_VAR_TYPE_VEC4: {
  6001. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  6002. Plane v = p_value;
  6003. bv.x = v.normal.x;
  6004. bv.y = v.normal.y;
  6005. bv.z = v.normal.z;
  6006. bv.w = v.d;
  6007. } break;
  6008. case RS::GLOBAL_VAR_TYPE_COLOR: {
  6009. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  6010. Color v = p_value;
  6011. bv.x = v.r;
  6012. bv.y = v.g;
  6013. bv.z = v.b;
  6014. bv.w = v.a;
  6015. GlobalVariables::Value &bv_linear = global_variables.buffer_values[p_index + 1];
  6016. v = v.to_linear();
  6017. bv_linear.x = v.r;
  6018. bv_linear.y = v.g;
  6019. bv_linear.z = v.b;
  6020. bv_linear.w = v.a;
  6021. } break;
  6022. case RS::GLOBAL_VAR_TYPE_RECT2: {
  6023. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  6024. Rect2 v = p_value;
  6025. bv.x = v.position.x;
  6026. bv.y = v.position.y;
  6027. bv.z = v.size.x;
  6028. bv.w = v.size.y;
  6029. } break;
  6030. case RS::GLOBAL_VAR_TYPE_MAT2: {
  6031. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  6032. Vector<float> m2 = p_value;
  6033. if (m2.size() < 4) {
  6034. m2.resize(4);
  6035. }
  6036. bv[0].x = m2[0];
  6037. bv[0].y = m2[1];
  6038. bv[0].z = 0;
  6039. bv[0].w = 0;
  6040. bv[1].x = m2[2];
  6041. bv[1].y = m2[3];
  6042. bv[1].z = 0;
  6043. bv[1].w = 0;
  6044. } break;
  6045. case RS::GLOBAL_VAR_TYPE_MAT3: {
  6046. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  6047. Basis v = p_value;
  6048. bv[0].x = v.elements[0][0];
  6049. bv[0].y = v.elements[1][0];
  6050. bv[0].z = v.elements[2][0];
  6051. bv[0].w = 0;
  6052. bv[1].x = v.elements[0][1];
  6053. bv[1].y = v.elements[1][1];
  6054. bv[1].z = v.elements[2][1];
  6055. bv[1].w = 0;
  6056. bv[2].x = v.elements[0][2];
  6057. bv[2].y = v.elements[1][2];
  6058. bv[2].z = v.elements[2][2];
  6059. bv[2].w = 0;
  6060. } break;
  6061. case RS::GLOBAL_VAR_TYPE_MAT4: {
  6062. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  6063. Vector<float> m2 = p_value;
  6064. if (m2.size() < 16) {
  6065. m2.resize(16);
  6066. }
  6067. bv[0].x = m2[0];
  6068. bv[0].y = m2[1];
  6069. bv[0].z = m2[2];
  6070. bv[0].w = m2[3];
  6071. bv[1].x = m2[4];
  6072. bv[1].y = m2[5];
  6073. bv[1].z = m2[6];
  6074. bv[1].w = m2[7];
  6075. bv[2].x = m2[8];
  6076. bv[2].y = m2[9];
  6077. bv[2].z = m2[10];
  6078. bv[2].w = m2[11];
  6079. bv[3].x = m2[12];
  6080. bv[3].y = m2[13];
  6081. bv[3].z = m2[14];
  6082. bv[3].w = m2[15];
  6083. } break;
  6084. case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
  6085. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  6086. Transform2D v = p_value;
  6087. bv[0].x = v.elements[0][0];
  6088. bv[0].y = v.elements[0][1];
  6089. bv[0].z = 0;
  6090. bv[0].w = 0;
  6091. bv[1].x = v.elements[1][0];
  6092. bv[1].y = v.elements[1][1];
  6093. bv[1].z = 0;
  6094. bv[1].w = 0;
  6095. bv[2].x = v.elements[2][0];
  6096. bv[2].y = v.elements[2][1];
  6097. bv[2].z = 1;
  6098. bv[2].w = 0;
  6099. } break;
  6100. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  6101. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  6102. Transform v = p_value;
  6103. bv[0].x = v.basis.elements[0][0];
  6104. bv[0].y = v.basis.elements[1][0];
  6105. bv[0].z = v.basis.elements[2][0];
  6106. bv[0].w = 0;
  6107. bv[1].x = v.basis.elements[0][1];
  6108. bv[1].y = v.basis.elements[1][1];
  6109. bv[1].z = v.basis.elements[2][1];
  6110. bv[1].w = 0;
  6111. bv[2].x = v.basis.elements[0][2];
  6112. bv[2].y = v.basis.elements[1][2];
  6113. bv[2].z = v.basis.elements[2][2];
  6114. bv[2].w = 0;
  6115. bv[3].x = v.origin.x;
  6116. bv[3].y = v.origin.y;
  6117. bv[3].z = v.origin.z;
  6118. bv[3].w = 1;
  6119. } break;
  6120. default: {
  6121. ERR_FAIL();
  6122. }
  6123. }
  6124. }
  6125. void RendererStorageRD::_global_variable_mark_buffer_dirty(int32_t p_index, int32_t p_elements) {
  6126. int32_t prev_chunk = -1;
  6127. for (int32_t i = 0; i < p_elements; i++) {
  6128. int32_t chunk = (p_index + i) / GlobalVariables::BUFFER_DIRTY_REGION_SIZE;
  6129. if (chunk != prev_chunk) {
  6130. if (!global_variables.buffer_dirty_regions[chunk]) {
  6131. global_variables.buffer_dirty_regions[chunk] = true;
  6132. global_variables.buffer_dirty_region_count++;
  6133. }
  6134. }
  6135. prev_chunk = chunk;
  6136. }
  6137. }
  6138. void RendererStorageRD::global_variable_add(const StringName &p_name, RS::GlobalVariableType p_type, const Variant &p_value) {
  6139. ERR_FAIL_COND(global_variables.variables.has(p_name));
  6140. GlobalVariables::Variable gv;
  6141. gv.type = p_type;
  6142. gv.value = p_value;
  6143. gv.buffer_index = -1;
  6144. if (p_type >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
  6145. //is texture
  6146. global_variables.must_update_texture_materials = true; //normally there are none
  6147. } else {
  6148. gv.buffer_elements = 1;
  6149. if (p_type == RS::GLOBAL_VAR_TYPE_COLOR || p_type == RS::GLOBAL_VAR_TYPE_MAT2) {
  6150. //color needs to elements to store srgb and linear
  6151. gv.buffer_elements = 2;
  6152. }
  6153. if (p_type == RS::GLOBAL_VAR_TYPE_MAT3 || p_type == RS::GLOBAL_VAR_TYPE_TRANSFORM_2D) {
  6154. //color needs to elements to store srgb and linear
  6155. gv.buffer_elements = 3;
  6156. }
  6157. if (p_type == RS::GLOBAL_VAR_TYPE_MAT4 || p_type == RS::GLOBAL_VAR_TYPE_TRANSFORM) {
  6158. //color needs to elements to store srgb and linear
  6159. gv.buffer_elements = 4;
  6160. }
  6161. //is vector, allocate in buffer and update index
  6162. gv.buffer_index = _global_variable_allocate(gv.buffer_elements);
  6163. ERR_FAIL_COND_MSG(gv.buffer_index < 0, vformat("Failed allocating global variable '%s' out of buffer memory. Consider increasing it in the Project Settings.", String(p_name)));
  6164. global_variables.buffer_usage[gv.buffer_index].elements = gv.buffer_elements;
  6165. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  6166. _global_variable_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  6167. global_variables.must_update_buffer_materials = true; //normally there are none
  6168. }
  6169. global_variables.variables[p_name] = gv;
  6170. }
  6171. void RendererStorageRD::global_variable_remove(const StringName &p_name) {
  6172. if (!global_variables.variables.has(p_name)) {
  6173. return;
  6174. }
  6175. GlobalVariables::Variable &gv = global_variables.variables[p_name];
  6176. if (gv.buffer_index >= 0) {
  6177. global_variables.buffer_usage[gv.buffer_index].elements = 0;
  6178. global_variables.must_update_buffer_materials = true;
  6179. } else {
  6180. global_variables.must_update_texture_materials = true;
  6181. }
  6182. global_variables.variables.erase(p_name);
  6183. }
  6184. Vector<StringName> RendererStorageRD::global_variable_get_list() const {
  6185. if (!Engine::get_singleton()->is_editor_hint()) {
  6186. ERR_FAIL_V_MSG(Vector<StringName>(), "This function should never be used outside the editor, it can severely damage performance.");
  6187. }
  6188. const StringName *K = nullptr;
  6189. Vector<StringName> names;
  6190. while ((K = global_variables.variables.next(K))) {
  6191. names.push_back(*K);
  6192. }
  6193. names.sort_custom<StringName::AlphCompare>();
  6194. return names;
  6195. }
  6196. void RendererStorageRD::global_variable_set(const StringName &p_name, const Variant &p_value) {
  6197. ERR_FAIL_COND(!global_variables.variables.has(p_name));
  6198. GlobalVariables::Variable &gv = global_variables.variables[p_name];
  6199. gv.value = p_value;
  6200. if (gv.override.get_type() == Variant::NIL) {
  6201. if (gv.buffer_index >= 0) {
  6202. //buffer
  6203. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  6204. _global_variable_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  6205. } else {
  6206. //texture
  6207. for (Set<RID>::Element *E = gv.texture_materials.front(); E; E = E->next()) {
  6208. Material *material = material_owner.getornull(E->get());
  6209. ERR_CONTINUE(!material);
  6210. _material_queue_update(material, false, true);
  6211. }
  6212. }
  6213. }
  6214. }
  6215. void RendererStorageRD::global_variable_set_override(const StringName &p_name, const Variant &p_value) {
  6216. if (!global_variables.variables.has(p_name)) {
  6217. return; //variable may not exist
  6218. }
  6219. GlobalVariables::Variable &gv = global_variables.variables[p_name];
  6220. gv.override = p_value;
  6221. if (gv.buffer_index >= 0) {
  6222. //buffer
  6223. if (gv.override.get_type() == Variant::NIL) {
  6224. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  6225. } else {
  6226. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.override);
  6227. }
  6228. _global_variable_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  6229. } else {
  6230. //texture
  6231. //texture
  6232. for (Set<RID>::Element *E = gv.texture_materials.front(); E; E = E->next()) {
  6233. Material *material = material_owner.getornull(E->get());
  6234. ERR_CONTINUE(!material);
  6235. _material_queue_update(material, false, true);
  6236. }
  6237. }
  6238. }
  6239. Variant RendererStorageRD::global_variable_get(const StringName &p_name) const {
  6240. if (!Engine::get_singleton()->is_editor_hint()) {
  6241. ERR_FAIL_V_MSG(Variant(), "This function should never be used outside the editor, it can severely damage performance.");
  6242. }
  6243. if (!global_variables.variables.has(p_name)) {
  6244. return Variant();
  6245. }
  6246. return global_variables.variables[p_name].value;
  6247. }
  6248. RS::GlobalVariableType RendererStorageRD::global_variable_get_type_internal(const StringName &p_name) const {
  6249. if (!global_variables.variables.has(p_name)) {
  6250. return RS::GLOBAL_VAR_TYPE_MAX;
  6251. }
  6252. return global_variables.variables[p_name].type;
  6253. }
  6254. RS::GlobalVariableType RendererStorageRD::global_variable_get_type(const StringName &p_name) const {
  6255. if (!Engine::get_singleton()->is_editor_hint()) {
  6256. ERR_FAIL_V_MSG(RS::GLOBAL_VAR_TYPE_MAX, "This function should never be used outside the editor, it can severely damage performance.");
  6257. }
  6258. return global_variable_get_type_internal(p_name);
  6259. }
  6260. void RendererStorageRD::global_variables_load_settings(bool p_load_textures) {
  6261. List<PropertyInfo> settings;
  6262. ProjectSettings::get_singleton()->get_property_list(&settings);
  6263. for (List<PropertyInfo>::Element *E = settings.front(); E; E = E->next()) {
  6264. if (E->get().name.begins_with("shader_globals/")) {
  6265. StringName name = E->get().name.get_slice("/", 1);
  6266. Dictionary d = ProjectSettings::get_singleton()->get(E->get().name);
  6267. ERR_CONTINUE(!d.has("type"));
  6268. ERR_CONTINUE(!d.has("value"));
  6269. String type = d["type"];
  6270. static const char *global_var_type_names[RS::GLOBAL_VAR_TYPE_MAX] = {
  6271. "bool",
  6272. "bvec2",
  6273. "bvec3",
  6274. "bvec4",
  6275. "int",
  6276. "ivec2",
  6277. "ivec3",
  6278. "ivec4",
  6279. "rect2i",
  6280. "uint",
  6281. "uvec2",
  6282. "uvec3",
  6283. "uvec4",
  6284. "float",
  6285. "vec2",
  6286. "vec3",
  6287. "vec4",
  6288. "color",
  6289. "rect2",
  6290. "mat2",
  6291. "mat3",
  6292. "mat4",
  6293. "transform_2d",
  6294. "transform",
  6295. "sampler2D",
  6296. "sampler2DArray",
  6297. "sampler3D",
  6298. "samplerCube",
  6299. };
  6300. RS::GlobalVariableType gvtype = RS::GLOBAL_VAR_TYPE_MAX;
  6301. for (int i = 0; i < RS::GLOBAL_VAR_TYPE_MAX; i++) {
  6302. if (global_var_type_names[i] == type) {
  6303. gvtype = RS::GlobalVariableType(i);
  6304. break;
  6305. }
  6306. }
  6307. ERR_CONTINUE(gvtype == RS::GLOBAL_VAR_TYPE_MAX); //type invalid
  6308. Variant value = d["value"];
  6309. if (gvtype >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
  6310. //textire
  6311. if (!p_load_textures) {
  6312. value = RID();
  6313. continue;
  6314. }
  6315. String path = value;
  6316. RES resource = ResourceLoader::load(path);
  6317. ERR_CONTINUE(resource.is_null());
  6318. value = resource;
  6319. }
  6320. if (global_variables.variables.has(name)) {
  6321. //has it, update it
  6322. global_variable_set(name, value);
  6323. } else {
  6324. global_variable_add(name, gvtype, value);
  6325. }
  6326. }
  6327. }
  6328. }
  6329. void RendererStorageRD::global_variables_clear() {
  6330. global_variables.variables.clear(); //not right but for now enough
  6331. }
  6332. RID RendererStorageRD::global_variables_get_storage_buffer() const {
  6333. return global_variables.buffer;
  6334. }
  6335. int32_t RendererStorageRD::global_variables_instance_allocate(RID p_instance) {
  6336. ERR_FAIL_COND_V(global_variables.instance_buffer_pos.has(p_instance), -1);
  6337. int32_t pos = _global_variable_allocate(ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES);
  6338. global_variables.instance_buffer_pos[p_instance] = pos; //save anyway
  6339. ERR_FAIL_COND_V_MSG(pos < 0, -1, "Too many instances using shader instance variables. Increase buffer size in Project Settings.");
  6340. global_variables.buffer_usage[pos].elements = ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES;
  6341. return pos;
  6342. }
  6343. void RendererStorageRD::global_variables_instance_free(RID p_instance) {
  6344. ERR_FAIL_COND(!global_variables.instance_buffer_pos.has(p_instance));
  6345. int32_t pos = global_variables.instance_buffer_pos[p_instance];
  6346. if (pos >= 0) {
  6347. global_variables.buffer_usage[pos].elements = 0;
  6348. }
  6349. global_variables.instance_buffer_pos.erase(p_instance);
  6350. }
  6351. void RendererStorageRD::global_variables_instance_update(RID p_instance, int p_index, const Variant &p_value) {
  6352. if (!global_variables.instance_buffer_pos.has(p_instance)) {
  6353. return; //just not allocated, ignore
  6354. }
  6355. int32_t pos = global_variables.instance_buffer_pos[p_instance];
  6356. if (pos < 0) {
  6357. return; //again, not allocated, ignore
  6358. }
  6359. ERR_FAIL_INDEX(p_index, ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES);
  6360. ERR_FAIL_COND_MSG(p_value.get_type() > Variant::COLOR, "Unsupported variant type for instance parameter: " + Variant::get_type_name(p_value.get_type())); //anything greater not supported
  6361. ShaderLanguage::DataType datatype_from_value[Variant::COLOR + 1] = {
  6362. ShaderLanguage::TYPE_MAX, //nil
  6363. ShaderLanguage::TYPE_BOOL, //bool
  6364. ShaderLanguage::TYPE_INT, //int
  6365. ShaderLanguage::TYPE_FLOAT, //float
  6366. ShaderLanguage::TYPE_MAX, //string
  6367. ShaderLanguage::TYPE_VEC2, //vec2
  6368. ShaderLanguage::TYPE_IVEC2, //vec2i
  6369. ShaderLanguage::TYPE_VEC4, //rect2
  6370. ShaderLanguage::TYPE_IVEC4, //rect2i
  6371. ShaderLanguage::TYPE_VEC3, // vec3
  6372. ShaderLanguage::TYPE_IVEC3, //vec3i
  6373. ShaderLanguage::TYPE_MAX, //xform2d not supported here
  6374. ShaderLanguage::TYPE_VEC4, //plane
  6375. ShaderLanguage::TYPE_VEC4, //quat
  6376. ShaderLanguage::TYPE_MAX, //aabb not supported here
  6377. ShaderLanguage::TYPE_MAX, //basis not supported here
  6378. ShaderLanguage::TYPE_MAX, //xform not supported here
  6379. ShaderLanguage::TYPE_VEC4 //color
  6380. };
  6381. ShaderLanguage::DataType datatype = datatype_from_value[p_value.get_type()];
  6382. ERR_FAIL_COND_MSG(datatype == ShaderLanguage::TYPE_MAX, "Unsupported variant type for instance parameter: " + Variant::get_type_name(p_value.get_type())); //anything greater not supported
  6383. pos += p_index;
  6384. _fill_std140_variant_ubo_value(datatype, p_value, (uint8_t *)&global_variables.buffer_values[pos], true); //instances always use linear color in this renderer
  6385. _global_variable_mark_buffer_dirty(pos, 1);
  6386. }
  6387. void RendererStorageRD::_update_global_variables() {
  6388. if (global_variables.buffer_dirty_region_count > 0) {
  6389. uint32_t total_regions = global_variables.buffer_size / GlobalVariables::BUFFER_DIRTY_REGION_SIZE;
  6390. if (total_regions / global_variables.buffer_dirty_region_count <= 4) {
  6391. // 25% of regions dirty, just update all buffer
  6392. RD::get_singleton()->buffer_update(global_variables.buffer, 0, sizeof(GlobalVariables::Value) * global_variables.buffer_size, global_variables.buffer_values);
  6393. zeromem(global_variables.buffer_dirty_regions, sizeof(bool) * total_regions);
  6394. } else {
  6395. uint32_t region_byte_size = sizeof(GlobalVariables::Value) * GlobalVariables::BUFFER_DIRTY_REGION_SIZE;
  6396. for (uint32_t i = 0; i < total_regions; i++) {
  6397. if (global_variables.buffer_dirty_regions[i]) {
  6398. RD::get_singleton()->buffer_update(global_variables.buffer, i * region_byte_size, region_byte_size, global_variables.buffer_values);
  6399. global_variables.buffer_dirty_regions[i] = false;
  6400. }
  6401. }
  6402. }
  6403. global_variables.buffer_dirty_region_count = 0;
  6404. }
  6405. if (global_variables.must_update_buffer_materials) {
  6406. // only happens in the case of a buffer variable added or removed,
  6407. // so not often.
  6408. for (List<RID>::Element *E = global_variables.materials_using_buffer.front(); E; E = E->next()) {
  6409. Material *material = material_owner.getornull(E->get());
  6410. ERR_CONTINUE(!material); //wtf
  6411. _material_queue_update(material, true, false);
  6412. }
  6413. global_variables.must_update_buffer_materials = false;
  6414. }
  6415. if (global_variables.must_update_texture_materials) {
  6416. // only happens in the case of a buffer variable added or removed,
  6417. // so not often.
  6418. for (List<RID>::Element *E = global_variables.materials_using_texture.front(); E; E = E->next()) {
  6419. Material *material = material_owner.getornull(E->get());
  6420. ERR_CONTINUE(!material); //wtf
  6421. _material_queue_update(material, false, true);
  6422. print_line("update material texture?");
  6423. }
  6424. global_variables.must_update_texture_materials = false;
  6425. }
  6426. }
  6427. void RendererStorageRD::update_dirty_resources() {
  6428. _update_global_variables(); //must do before materials, so it can queue them for update
  6429. _update_queued_materials();
  6430. _update_dirty_multimeshes();
  6431. _update_dirty_skeletons();
  6432. _update_decal_atlas();
  6433. }
  6434. bool RendererStorageRD::has_os_feature(const String &p_feature) const {
  6435. if (p_feature == "rgtc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC5_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  6436. return true;
  6437. }
  6438. if (p_feature == "s3tc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC1_RGB_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  6439. return true;
  6440. }
  6441. if (p_feature == "bptc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC7_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  6442. return true;
  6443. }
  6444. if ((p_feature == "etc" || p_feature == "etc2") && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  6445. return true;
  6446. }
  6447. if (p_feature == "pvrtc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  6448. return true;
  6449. }
  6450. return false;
  6451. }
  6452. bool RendererStorageRD::free(RID p_rid) {
  6453. if (texture_owner.owns(p_rid)) {
  6454. Texture *t = texture_owner.getornull(p_rid);
  6455. ERR_FAIL_COND_V(t->is_render_target, false);
  6456. if (RD::get_singleton()->texture_is_valid(t->rd_texture_srgb)) {
  6457. //erase this first, as it's a dependency of the one below
  6458. RD::get_singleton()->free(t->rd_texture_srgb);
  6459. }
  6460. if (RD::get_singleton()->texture_is_valid(t->rd_texture)) {
  6461. RD::get_singleton()->free(t->rd_texture);
  6462. }
  6463. if (t->is_proxy && t->proxy_to.is_valid()) {
  6464. Texture *proxy_to = texture_owner.getornull(t->proxy_to);
  6465. if (proxy_to) {
  6466. proxy_to->proxies.erase(p_rid);
  6467. }
  6468. }
  6469. if (decal_atlas.textures.has(p_rid)) {
  6470. decal_atlas.textures.erase(p_rid);
  6471. //there is not much a point of making it dirty, just let it be.
  6472. }
  6473. for (int i = 0; i < t->proxies.size(); i++) {
  6474. Texture *p = texture_owner.getornull(t->proxies[i]);
  6475. ERR_CONTINUE(!p);
  6476. p->proxy_to = RID();
  6477. p->rd_texture = RID();
  6478. p->rd_texture_srgb = RID();
  6479. }
  6480. if (t->canvas_texture) {
  6481. memdelete(t->canvas_texture);
  6482. }
  6483. texture_owner.free(p_rid);
  6484. } else if (canvas_texture_owner.owns(p_rid)) {
  6485. CanvasTexture *ct = canvas_texture_owner.getornull(p_rid);
  6486. memdelete(ct);
  6487. canvas_texture_owner.free(p_rid);
  6488. } else if (shader_owner.owns(p_rid)) {
  6489. Shader *shader = shader_owner.getornull(p_rid);
  6490. //make material unreference this
  6491. while (shader->owners.size()) {
  6492. material_set_shader(shader->owners.front()->get()->self, RID());
  6493. }
  6494. //clear data if exists
  6495. if (shader->data) {
  6496. memdelete(shader->data);
  6497. }
  6498. shader_owner.free(p_rid);
  6499. } else if (material_owner.owns(p_rid)) {
  6500. Material *material = material_owner.getornull(p_rid);
  6501. if (material->update_requested) {
  6502. _update_queued_materials();
  6503. }
  6504. material_set_shader(p_rid, RID()); //clean up shader
  6505. material->instance_dependency.instance_notify_deleted(p_rid);
  6506. material_owner.free(p_rid);
  6507. } else if (mesh_owner.owns(p_rid)) {
  6508. mesh_clear(p_rid);
  6509. Mesh *mesh = mesh_owner.getornull(p_rid);
  6510. mesh->instance_dependency.instance_notify_deleted(p_rid);
  6511. mesh_owner.free(p_rid);
  6512. } else if (multimesh_owner.owns(p_rid)) {
  6513. _update_dirty_multimeshes();
  6514. multimesh_allocate(p_rid, 0, RS::MULTIMESH_TRANSFORM_2D);
  6515. MultiMesh *multimesh = multimesh_owner.getornull(p_rid);
  6516. multimesh->instance_dependency.instance_notify_deleted(p_rid);
  6517. multimesh_owner.free(p_rid);
  6518. } else if (skeleton_owner.owns(p_rid)) {
  6519. _update_dirty_skeletons();
  6520. skeleton_allocate(p_rid, 0);
  6521. Skeleton *skeleton = skeleton_owner.getornull(p_rid);
  6522. skeleton->instance_dependency.instance_notify_deleted(p_rid);
  6523. skeleton_owner.free(p_rid);
  6524. } else if (reflection_probe_owner.owns(p_rid)) {
  6525. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_rid);
  6526. reflection_probe->instance_dependency.instance_notify_deleted(p_rid);
  6527. reflection_probe_owner.free(p_rid);
  6528. } else if (decal_owner.owns(p_rid)) {
  6529. Decal *decal = decal_owner.getornull(p_rid);
  6530. for (int i = 0; i < RS::DECAL_TEXTURE_MAX; i++) {
  6531. if (decal->textures[i].is_valid() && texture_owner.owns(decal->textures[i])) {
  6532. texture_remove_from_decal_atlas(decal->textures[i]);
  6533. }
  6534. }
  6535. decal->instance_dependency.instance_notify_deleted(p_rid);
  6536. decal_owner.free(p_rid);
  6537. } else if (gi_probe_owner.owns(p_rid)) {
  6538. gi_probe_allocate(p_rid, Transform(), AABB(), Vector3i(), Vector<uint8_t>(), Vector<uint8_t>(), Vector<uint8_t>(), Vector<int>()); //deallocate
  6539. GIProbe *gi_probe = gi_probe_owner.getornull(p_rid);
  6540. gi_probe->instance_dependency.instance_notify_deleted(p_rid);
  6541. gi_probe_owner.free(p_rid);
  6542. } else if (lightmap_owner.owns(p_rid)) {
  6543. lightmap_set_textures(p_rid, RID(), false);
  6544. Lightmap *lightmap = lightmap_owner.getornull(p_rid);
  6545. lightmap->instance_dependency.instance_notify_deleted(p_rid);
  6546. lightmap_owner.free(p_rid);
  6547. } else if (light_owner.owns(p_rid)) {
  6548. light_set_projector(p_rid, RID()); //clear projector
  6549. // delete the texture
  6550. Light *light = light_owner.getornull(p_rid);
  6551. light->instance_dependency.instance_notify_deleted(p_rid);
  6552. light_owner.free(p_rid);
  6553. } else if (particles_owner.owns(p_rid)) {
  6554. Particles *particles = particles_owner.getornull(p_rid);
  6555. _particles_free_data(particles);
  6556. particles->instance_dependency.instance_notify_deleted(p_rid);
  6557. particles_owner.free(p_rid);
  6558. } else if (particles_collision_owner.owns(p_rid)) {
  6559. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_rid);
  6560. if (particles_collision->heightfield_texture.is_valid()) {
  6561. RD::get_singleton()->free(particles_collision->heightfield_texture);
  6562. }
  6563. particles_collision->instance_dependency.instance_notify_deleted(p_rid);
  6564. particles_collision_owner.free(p_rid);
  6565. } else if (render_target_owner.owns(p_rid)) {
  6566. RenderTarget *rt = render_target_owner.getornull(p_rid);
  6567. _clear_render_target(rt);
  6568. if (rt->texture.is_valid()) {
  6569. Texture *tex = texture_owner.getornull(rt->texture);
  6570. tex->is_render_target = false;
  6571. free(rt->texture);
  6572. }
  6573. render_target_owner.free(p_rid);
  6574. } else {
  6575. return false;
  6576. }
  6577. return true;
  6578. }
  6579. EffectsRD *RendererStorageRD::get_effects() {
  6580. return &effects;
  6581. }
  6582. void RendererStorageRD::capture_timestamps_begin() {
  6583. RD::get_singleton()->capture_timestamp("Frame Begin", false);
  6584. }
  6585. void RendererStorageRD::capture_timestamp(const String &p_name) {
  6586. RD::get_singleton()->capture_timestamp(p_name, true);
  6587. }
  6588. uint32_t RendererStorageRD::get_captured_timestamps_count() const {
  6589. return RD::get_singleton()->get_captured_timestamps_count();
  6590. }
  6591. uint64_t RendererStorageRD::get_captured_timestamps_frame() const {
  6592. return RD::get_singleton()->get_captured_timestamps_frame();
  6593. }
  6594. uint64_t RendererStorageRD::get_captured_timestamp_gpu_time(uint32_t p_index) const {
  6595. return RD::get_singleton()->get_captured_timestamp_gpu_time(p_index);
  6596. }
  6597. uint64_t RendererStorageRD::get_captured_timestamp_cpu_time(uint32_t p_index) const {
  6598. return RD::get_singleton()->get_captured_timestamp_cpu_time(p_index);
  6599. }
  6600. String RendererStorageRD::get_captured_timestamp_name(uint32_t p_index) const {
  6601. return RD::get_singleton()->get_captured_timestamp_name(p_index);
  6602. }
  6603. RendererStorageRD *RendererStorageRD::base_singleton = nullptr;
  6604. RendererStorageRD::RendererStorageRD() {
  6605. base_singleton = this;
  6606. for (int i = 0; i < SHADER_TYPE_MAX; i++) {
  6607. shader_data_request_func[i] = nullptr;
  6608. }
  6609. static_assert(sizeof(GlobalVariables::Value) == 16);
  6610. global_variables.buffer_size = GLOBAL_GET("rendering/high_end/global_shader_variables_buffer_size");
  6611. global_variables.buffer_size = MAX(4096, global_variables.buffer_size);
  6612. global_variables.buffer_values = memnew_arr(GlobalVariables::Value, global_variables.buffer_size);
  6613. zeromem(global_variables.buffer_values, sizeof(GlobalVariables::Value) * global_variables.buffer_size);
  6614. global_variables.buffer_usage = memnew_arr(GlobalVariables::ValueUsage, global_variables.buffer_size);
  6615. global_variables.buffer_dirty_regions = memnew_arr(bool, global_variables.buffer_size / GlobalVariables::BUFFER_DIRTY_REGION_SIZE);
  6616. zeromem(global_variables.buffer_dirty_regions, sizeof(bool) * global_variables.buffer_size / GlobalVariables::BUFFER_DIRTY_REGION_SIZE);
  6617. global_variables.buffer = RD::get_singleton()->storage_buffer_create(sizeof(GlobalVariables::Value) * global_variables.buffer_size);
  6618. material_update_list = nullptr;
  6619. { //create default textures
  6620. RD::TextureFormat tformat;
  6621. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  6622. tformat.width = 4;
  6623. tformat.height = 4;
  6624. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  6625. tformat.type = RD::TEXTURE_TYPE_2D;
  6626. Vector<uint8_t> pv;
  6627. pv.resize(16 * 4);
  6628. for (int i = 0; i < 16; i++) {
  6629. pv.set(i * 4 + 0, 255);
  6630. pv.set(i * 4 + 1, 255);
  6631. pv.set(i * 4 + 2, 255);
  6632. pv.set(i * 4 + 3, 255);
  6633. }
  6634. {
  6635. Vector<Vector<uint8_t>> vpv;
  6636. vpv.push_back(pv);
  6637. default_rd_textures[DEFAULT_RD_TEXTURE_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6638. }
  6639. for (int i = 0; i < 16; i++) {
  6640. pv.set(i * 4 + 0, 0);
  6641. pv.set(i * 4 + 1, 0);
  6642. pv.set(i * 4 + 2, 0);
  6643. pv.set(i * 4 + 3, 255);
  6644. }
  6645. {
  6646. Vector<Vector<uint8_t>> vpv;
  6647. vpv.push_back(pv);
  6648. default_rd_textures[DEFAULT_RD_TEXTURE_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6649. //take the chance and initialize decal atlas to something
  6650. decal_atlas.texture = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6651. decal_atlas.texture_srgb = decal_atlas.texture;
  6652. }
  6653. for (int i = 0; i < 16; i++) {
  6654. pv.set(i * 4 + 0, 128);
  6655. pv.set(i * 4 + 1, 128);
  6656. pv.set(i * 4 + 2, 255);
  6657. pv.set(i * 4 + 3, 255);
  6658. }
  6659. {
  6660. Vector<Vector<uint8_t>> vpv;
  6661. vpv.push_back(pv);
  6662. default_rd_textures[DEFAULT_RD_TEXTURE_NORMAL] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6663. }
  6664. for (int i = 0; i < 16; i++) {
  6665. pv.set(i * 4 + 0, 255);
  6666. pv.set(i * 4 + 1, 128);
  6667. pv.set(i * 4 + 2, 255);
  6668. pv.set(i * 4 + 3, 255);
  6669. }
  6670. {
  6671. Vector<Vector<uint8_t>> vpv;
  6672. vpv.push_back(pv);
  6673. default_rd_textures[DEFAULT_RD_TEXTURE_ANISO] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6674. }
  6675. for (int i = 0; i < 16; i++) {
  6676. pv.set(i * 4 + 0, 0);
  6677. pv.set(i * 4 + 1, 0);
  6678. pv.set(i * 4 + 2, 0);
  6679. pv.set(i * 4 + 3, 0);
  6680. }
  6681. default_rd_textures[DEFAULT_RD_TEXTURE_MULTIMESH_BUFFER] = RD::get_singleton()->texture_buffer_create(16, RD::DATA_FORMAT_R8G8B8A8_UNORM, pv);
  6682. for (int i = 0; i < 16; i++) {
  6683. pv.set(i * 4 + 0, 0);
  6684. pv.set(i * 4 + 1, 0);
  6685. pv.set(i * 4 + 2, 0);
  6686. pv.set(i * 4 + 3, 0);
  6687. }
  6688. {
  6689. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UINT;
  6690. Vector<Vector<uint8_t>> vpv;
  6691. vpv.push_back(pv);
  6692. default_rd_textures[DEFAULT_RD_TEXTURE_2D_UINT] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6693. }
  6694. }
  6695. { //create default cubemap
  6696. RD::TextureFormat tformat;
  6697. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  6698. tformat.width = 4;
  6699. tformat.height = 4;
  6700. tformat.array_layers = 6;
  6701. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  6702. tformat.type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  6703. Vector<uint8_t> pv;
  6704. pv.resize(16 * 4);
  6705. for (int i = 0; i < 16; i++) {
  6706. pv.set(i * 4 + 0, 0);
  6707. pv.set(i * 4 + 1, 0);
  6708. pv.set(i * 4 + 2, 0);
  6709. pv.set(i * 4 + 3, 0);
  6710. }
  6711. {
  6712. Vector<Vector<uint8_t>> vpv;
  6713. for (int i = 0; i < 6; i++) {
  6714. vpv.push_back(pv);
  6715. }
  6716. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6717. }
  6718. }
  6719. { //create default cubemap array
  6720. RD::TextureFormat tformat;
  6721. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  6722. tformat.width = 4;
  6723. tformat.height = 4;
  6724. tformat.array_layers = 6;
  6725. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  6726. tformat.type = RD::TEXTURE_TYPE_CUBE;
  6727. Vector<uint8_t> pv;
  6728. pv.resize(16 * 4);
  6729. for (int i = 0; i < 16; i++) {
  6730. pv.set(i * 4 + 0, 0);
  6731. pv.set(i * 4 + 1, 0);
  6732. pv.set(i * 4 + 2, 0);
  6733. pv.set(i * 4 + 3, 0);
  6734. }
  6735. {
  6736. Vector<Vector<uint8_t>> vpv;
  6737. for (int i = 0; i < 6; i++) {
  6738. vpv.push_back(pv);
  6739. }
  6740. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6741. }
  6742. }
  6743. { //create default cubemap white array
  6744. RD::TextureFormat tformat;
  6745. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  6746. tformat.width = 4;
  6747. tformat.height = 4;
  6748. tformat.array_layers = 6;
  6749. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  6750. tformat.type = RD::TEXTURE_TYPE_CUBE;
  6751. Vector<uint8_t> pv;
  6752. pv.resize(16 * 4);
  6753. for (int i = 0; i < 16; i++) {
  6754. pv.set(i * 4 + 0, 255);
  6755. pv.set(i * 4 + 1, 255);
  6756. pv.set(i * 4 + 2, 255);
  6757. pv.set(i * 4 + 3, 255);
  6758. }
  6759. {
  6760. Vector<Vector<uint8_t>> vpv;
  6761. for (int i = 0; i < 6; i++) {
  6762. vpv.push_back(pv);
  6763. }
  6764. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6765. }
  6766. }
  6767. { //create default 3D
  6768. RD::TextureFormat tformat;
  6769. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  6770. tformat.width = 4;
  6771. tformat.height = 4;
  6772. tformat.depth = 4;
  6773. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  6774. tformat.type = RD::TEXTURE_TYPE_3D;
  6775. Vector<uint8_t> pv;
  6776. pv.resize(64 * 4);
  6777. for (int i = 0; i < 64; i++) {
  6778. pv.set(i * 4 + 0, 0);
  6779. pv.set(i * 4 + 1, 0);
  6780. pv.set(i * 4 + 2, 0);
  6781. pv.set(i * 4 + 3, 0);
  6782. }
  6783. {
  6784. Vector<Vector<uint8_t>> vpv;
  6785. vpv.push_back(pv);
  6786. default_rd_textures[DEFAULT_RD_TEXTURE_3D_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6787. }
  6788. }
  6789. { //create default array
  6790. RD::TextureFormat tformat;
  6791. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  6792. tformat.width = 4;
  6793. tformat.height = 4;
  6794. tformat.array_layers = 1;
  6795. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  6796. tformat.type = RD::TEXTURE_TYPE_2D_ARRAY;
  6797. Vector<uint8_t> pv;
  6798. pv.resize(16 * 4);
  6799. for (int i = 0; i < 16; i++) {
  6800. pv.set(i * 4 + 0, 255);
  6801. pv.set(i * 4 + 1, 255);
  6802. pv.set(i * 4 + 2, 255);
  6803. pv.set(i * 4 + 3, 255);
  6804. }
  6805. {
  6806. Vector<Vector<uint8_t>> vpv;
  6807. vpv.push_back(pv);
  6808. default_rd_textures[DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6809. }
  6810. }
  6811. //default samplers
  6812. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  6813. for (int j = 1; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  6814. RD::SamplerState sampler_state;
  6815. switch (i) {
  6816. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST: {
  6817. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  6818. sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST;
  6819. sampler_state.max_lod = 0;
  6820. } break;
  6821. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR: {
  6822. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  6823. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  6824. sampler_state.max_lod = 0;
  6825. } break;
  6826. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS: {
  6827. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  6828. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  6829. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  6830. } break;
  6831. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS: {
  6832. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  6833. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  6834. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  6835. } break;
  6836. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC: {
  6837. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  6838. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  6839. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  6840. sampler_state.use_anisotropy = true;
  6841. sampler_state.anisotropy_max = 1 << int(GLOBAL_GET("rendering/quality/texture_filters/anisotropic_filtering_level"));
  6842. } break;
  6843. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC: {
  6844. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  6845. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  6846. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  6847. sampler_state.use_anisotropy = true;
  6848. sampler_state.anisotropy_max = 1 << int(GLOBAL_GET("rendering/quality/texture_filters/anisotropic_filtering_level"));
  6849. } break;
  6850. default: {
  6851. }
  6852. }
  6853. switch (j) {
  6854. case RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED: {
  6855. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  6856. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  6857. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  6858. } break;
  6859. case RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED: {
  6860. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_REPEAT;
  6861. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_REPEAT;
  6862. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_REPEAT;
  6863. } break;
  6864. case RS::CANVAS_ITEM_TEXTURE_REPEAT_MIRROR: {
  6865. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  6866. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  6867. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  6868. } break;
  6869. default: {
  6870. }
  6871. }
  6872. default_rd_samplers[i][j] = RD::get_singleton()->sampler_create(sampler_state);
  6873. }
  6874. }
  6875. //default rd buffers
  6876. {
  6877. Vector<uint8_t> buffer;
  6878. {
  6879. buffer.resize(sizeof(float) * 3);
  6880. {
  6881. uint8_t *w = buffer.ptrw();
  6882. float *fptr = (float *)w;
  6883. fptr[0] = 0.0;
  6884. fptr[1] = 0.0;
  6885. fptr[2] = 0.0;
  6886. }
  6887. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_VERTEX] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  6888. }
  6889. { //normal
  6890. buffer.resize(sizeof(float) * 3);
  6891. {
  6892. uint8_t *w = buffer.ptrw();
  6893. float *fptr = (float *)w;
  6894. fptr[0] = 1.0;
  6895. fptr[1] = 0.0;
  6896. fptr[2] = 0.0;
  6897. }
  6898. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_NORMAL] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  6899. }
  6900. { //tangent
  6901. buffer.resize(sizeof(float) * 4);
  6902. {
  6903. uint8_t *w = buffer.ptrw();
  6904. float *fptr = (float *)w;
  6905. fptr[0] = 1.0;
  6906. fptr[1] = 0.0;
  6907. fptr[2] = 0.0;
  6908. fptr[3] = 0.0;
  6909. }
  6910. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_TANGENT] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  6911. }
  6912. { //color
  6913. buffer.resize(sizeof(float) * 4);
  6914. {
  6915. uint8_t *w = buffer.ptrw();
  6916. float *fptr = (float *)w;
  6917. fptr[0] = 1.0;
  6918. fptr[1] = 1.0;
  6919. fptr[2] = 1.0;
  6920. fptr[3] = 1.0;
  6921. }
  6922. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_COLOR] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  6923. }
  6924. { //tex uv 1
  6925. buffer.resize(sizeof(float) * 2);
  6926. {
  6927. uint8_t *w = buffer.ptrw();
  6928. float *fptr = (float *)w;
  6929. fptr[0] = 0.0;
  6930. fptr[1] = 0.0;
  6931. }
  6932. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_TEX_UV] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  6933. }
  6934. { //tex uv 2
  6935. buffer.resize(sizeof(float) * 2);
  6936. {
  6937. uint8_t *w = buffer.ptrw();
  6938. float *fptr = (float *)w;
  6939. fptr[0] = 0.0;
  6940. fptr[1] = 0.0;
  6941. }
  6942. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_TEX_UV2] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  6943. }
  6944. for (int i = 0; i < RS::ARRAY_CUSTOM_COUNT; i++) {
  6945. buffer.resize(sizeof(float) * 4);
  6946. {
  6947. uint8_t *w = buffer.ptrw();
  6948. float *fptr = (float *)w;
  6949. fptr[0] = 0.0;
  6950. fptr[1] = 0.0;
  6951. fptr[2] = 0.0;
  6952. fptr[3] = 0.0;
  6953. }
  6954. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_CUSTOM0 + i] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  6955. }
  6956. { //bones
  6957. buffer.resize(sizeof(uint32_t) * 4);
  6958. {
  6959. uint8_t *w = buffer.ptrw();
  6960. uint32_t *fptr = (uint32_t *)w;
  6961. fptr[0] = 0;
  6962. fptr[1] = 0;
  6963. fptr[2] = 0;
  6964. fptr[3] = 0;
  6965. }
  6966. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_BONES] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  6967. }
  6968. { //weights
  6969. buffer.resize(sizeof(float) * 4);
  6970. {
  6971. uint8_t *w = buffer.ptrw();
  6972. float *fptr = (float *)w;
  6973. fptr[0] = 0.0;
  6974. fptr[1] = 0.0;
  6975. fptr[2] = 0.0;
  6976. fptr[3] = 0.0;
  6977. }
  6978. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_WEIGHTS] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  6979. }
  6980. }
  6981. {
  6982. Vector<String> sdf_versions;
  6983. sdf_versions.push_back(""); //one only
  6984. giprobe_sdf_shader.initialize(sdf_versions);
  6985. giprobe_sdf_shader_version = giprobe_sdf_shader.version_create();
  6986. giprobe_sdf_shader.version_set_compute_code(giprobe_sdf_shader_version, "", "", "", Vector<String>());
  6987. giprobe_sdf_shader_version_shader = giprobe_sdf_shader.version_get_shader(giprobe_sdf_shader_version, 0);
  6988. giprobe_sdf_shader_pipeline = RD::get_singleton()->compute_pipeline_create(giprobe_sdf_shader_version_shader);
  6989. }
  6990. using_lightmap_array = true; // high end
  6991. if (using_lightmap_array) {
  6992. uint32_t textures_per_stage = RD::get_singleton()->limit_get(RD::LIMIT_MAX_TEXTURES_PER_SHADER_STAGE);
  6993. if (textures_per_stage <= 256) {
  6994. lightmap_textures.resize(32);
  6995. } else {
  6996. lightmap_textures.resize(1024);
  6997. }
  6998. for (int i = 0; i < lightmap_textures.size(); i++) {
  6999. lightmap_textures.write[i] = default_rd_textures[DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE];
  7000. }
  7001. }
  7002. lightmap_probe_capture_update_speed = GLOBAL_GET("rendering/lightmapper/probe_capture_update_speed");
  7003. /* Particles */
  7004. {
  7005. // Initialize particles
  7006. Vector<String> particles_modes;
  7007. particles_modes.push_back("");
  7008. particles_shader.shader.initialize(particles_modes, String());
  7009. }
  7010. shader_set_data_request_function(RendererStorageRD::SHADER_TYPE_PARTICLES, _create_particles_shader_funcs);
  7011. material_set_data_request_function(RendererStorageRD::SHADER_TYPE_PARTICLES, _create_particles_material_funcs);
  7012. {
  7013. ShaderCompilerRD::DefaultIdentifierActions actions;
  7014. actions.renames["COLOR"] = "PARTICLE.color";
  7015. actions.renames["VELOCITY"] = "PARTICLE.velocity";
  7016. //actions.renames["MASS"] = "mass"; ?
  7017. actions.renames["ACTIVE"] = "PARTICLE.is_active";
  7018. actions.renames["RESTART"] = "restart";
  7019. actions.renames["CUSTOM"] = "PARTICLE.custom";
  7020. actions.renames["TRANSFORM"] = "PARTICLE.xform";
  7021. actions.renames["TIME"] = "FRAME.time";
  7022. actions.renames["LIFETIME"] = "params.lifetime";
  7023. actions.renames["DELTA"] = "local_delta";
  7024. actions.renames["NUMBER"] = "particle";
  7025. actions.renames["INDEX"] = "index";
  7026. //actions.renames["GRAVITY"] = "current_gravity";
  7027. actions.renames["EMISSION_TRANSFORM"] = "FRAME.emission_transform";
  7028. actions.renames["RANDOM_SEED"] = "FRAME.random_seed";
  7029. actions.renames["FLAG_EMIT_POSITION"] = "EMISSION_FLAG_HAS_POSITION";
  7030. actions.renames["FLAG_EMIT_ROT_SCALE"] = "EMISSION_FLAG_HAS_ROTATION_SCALE";
  7031. actions.renames["FLAG_EMIT_VELOCITY"] = "EMISSION_FLAG_HAS_VELOCITY";
  7032. actions.renames["FLAG_EMIT_COLOR"] = "EMISSION_FLAG_HAS_COLOR";
  7033. actions.renames["FLAG_EMIT_CUSTOM"] = "EMISSION_FLAG_HAS_CUSTOM";
  7034. actions.renames["RESTART_POSITION"] = "restart_position";
  7035. actions.renames["RESTART_ROT_SCALE"] = "restart_rotation_scale";
  7036. actions.renames["RESTART_VELOCITY"] = "restart_velocity";
  7037. actions.renames["RESTART_COLOR"] = "restart_color";
  7038. actions.renames["RESTART_CUSTOM"] = "restart_custom";
  7039. actions.renames["emit_particle"] = "emit_particle";
  7040. actions.renames["COLLIDED"] = "collided";
  7041. actions.renames["COLLISION_NORMAL"] = "collision_normal";
  7042. actions.renames["COLLISION_DEPTH"] = "collision_depth";
  7043. actions.renames["ATTRACTOR_FORCE"] = "attractor_force";
  7044. actions.render_mode_defines["disable_force"] = "#define DISABLE_FORCE\n";
  7045. actions.render_mode_defines["disable_velocity"] = "#define DISABLE_VELOCITY\n";
  7046. actions.render_mode_defines["keep_data"] = "#define ENABLE_KEEP_DATA\n";
  7047. actions.render_mode_defines["collision_use_scale"] = "#define USE_COLLISON_SCALE\n";
  7048. actions.sampler_array_name = "material_samplers";
  7049. actions.base_texture_binding_index = 1;
  7050. actions.texture_layout_set = 3;
  7051. actions.base_uniform_string = "material.";
  7052. actions.base_varying_index = 10;
  7053. actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP;
  7054. actions.default_repeat = ShaderLanguage::REPEAT_ENABLE;
  7055. actions.global_buffer_array_variable = "global_variables.data";
  7056. particles_shader.compiler.initialize(actions);
  7057. }
  7058. {
  7059. // default material and shader for particles shader
  7060. particles_shader.default_shader = shader_create();
  7061. shader_set_code(particles_shader.default_shader, "shader_type particles; void compute() { COLOR = vec4(1.0); } \n");
  7062. particles_shader.default_material = material_create();
  7063. material_set_shader(particles_shader.default_material, particles_shader.default_shader);
  7064. ParticlesMaterialData *md = (ParticlesMaterialData *)material_get_data(particles_shader.default_material, RendererStorageRD::SHADER_TYPE_PARTICLES);
  7065. particles_shader.default_shader_rd = particles_shader.shader.version_get_shader(md->shader_data->version, 0);
  7066. Vector<RD::Uniform> uniforms;
  7067. {
  7068. RD::Uniform u;
  7069. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  7070. u.binding = 1;
  7071. u.ids.resize(12);
  7072. RID *ids_ptr = u.ids.ptrw();
  7073. ids_ptr[0] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  7074. ids_ptr[1] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  7075. ids_ptr[2] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  7076. ids_ptr[3] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  7077. ids_ptr[4] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  7078. ids_ptr[5] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  7079. ids_ptr[6] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  7080. ids_ptr[7] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  7081. ids_ptr[8] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  7082. ids_ptr[9] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  7083. ids_ptr[10] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  7084. ids_ptr[11] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  7085. uniforms.push_back(u);
  7086. }
  7087. {
  7088. RD::Uniform u;
  7089. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  7090. u.binding = 2;
  7091. u.ids.push_back(global_variables_get_storage_buffer());
  7092. uniforms.push_back(u);
  7093. }
  7094. particles_shader.base_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, 0);
  7095. }
  7096. default_rd_storage_buffer = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t) * 4);
  7097. {
  7098. Vector<String> copy_modes;
  7099. copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n");
  7100. copy_modes.push_back("\n#define MODE_FILL_SORT_BUFFER\n#define USE_SORT_BUFFER\n");
  7101. copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n#define USE_SORT_BUFFER\n");
  7102. particles_shader.copy_shader.initialize(copy_modes);
  7103. particles_shader.copy_shader_version = particles_shader.copy_shader.version_create();
  7104. for (int i = 0; i < ParticlesShader::COPY_MODE_MAX; i++) {
  7105. particles_shader.copy_pipelines[i] = RD::get_singleton()->compute_pipeline_create(particles_shader.copy_shader.version_get_shader(particles_shader.copy_shader_version, i));
  7106. }
  7107. }
  7108. {
  7109. Vector<String> sdf_modes;
  7110. sdf_modes.push_back("\n#define MODE_LOAD\n");
  7111. sdf_modes.push_back("\n#define MODE_LOAD_SHRINK\n");
  7112. sdf_modes.push_back("\n#define MODE_PROCESS\n");
  7113. sdf_modes.push_back("\n#define MODE_PROCESS_OPTIMIZED\n");
  7114. sdf_modes.push_back("\n#define MODE_STORE\n");
  7115. sdf_modes.push_back("\n#define MODE_STORE_SHRINK\n");
  7116. rt_sdf.shader.initialize(sdf_modes);
  7117. rt_sdf.shader_version = rt_sdf.shader.version_create();
  7118. for (int i = 0; i < RenderTargetSDF::SHADER_MAX; i++) {
  7119. rt_sdf.pipelines[i] = RD::get_singleton()->compute_pipeline_create(rt_sdf.shader.version_get_shader(rt_sdf.shader_version, i));
  7120. }
  7121. }
  7122. }
  7123. RendererStorageRD::~RendererStorageRD() {
  7124. memdelete_arr(global_variables.buffer_values);
  7125. memdelete_arr(global_variables.buffer_usage);
  7126. memdelete_arr(global_variables.buffer_dirty_regions);
  7127. RD::get_singleton()->free(global_variables.buffer);
  7128. //def textures
  7129. for (int i = 0; i < DEFAULT_RD_TEXTURE_MAX; i++) {
  7130. RD::get_singleton()->free(default_rd_textures[i]);
  7131. }
  7132. //def samplers
  7133. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  7134. for (int j = 1; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  7135. RD::get_singleton()->free(default_rd_samplers[i][j]);
  7136. }
  7137. }
  7138. //def buffers
  7139. for (int i = 0; i < DEFAULT_RD_BUFFER_MAX; i++) {
  7140. RD::get_singleton()->free(mesh_default_rd_buffers[i]);
  7141. }
  7142. giprobe_sdf_shader.version_free(giprobe_sdf_shader_version);
  7143. particles_shader.copy_shader.version_free(particles_shader.copy_shader_version);
  7144. RenderingServer::get_singleton()->free(particles_shader.default_material);
  7145. RenderingServer::get_singleton()->free(particles_shader.default_shader);
  7146. RD::get_singleton()->free(default_rd_storage_buffer);
  7147. if (decal_atlas.textures.size()) {
  7148. ERR_PRINT("Decal Atlas: " + itos(decal_atlas.textures.size()) + " textures were not removed from the atlas.");
  7149. }
  7150. if (decal_atlas.texture.is_valid()) {
  7151. RD::get_singleton()->free(decal_atlas.texture);
  7152. }
  7153. }