renderer_storage_rd.cpp 294 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_PVRTC1_2: {
  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_PVRTC1_2A: {
  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_PVRTC1_4: {
  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_PVRTC1_4A: {
  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->is_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.texture_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]->is_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.texture_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.texture_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->is_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->is_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->is_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->is_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->normal_map = 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->normal_map);
  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->dependency.changed_notify(DEPENDENCY_CHANGED_MATERIAL);
  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. RS::ShaderNativeSourceCode RendererStorageRD::shader_get_native_source_code(RID p_shader) const {
  1330. Shader *shader = shader_owner.getornull(p_shader);
  1331. ERR_FAIL_COND_V(!shader, RS::ShaderNativeSourceCode());
  1332. if (shader->data) {
  1333. return shader->data->get_native_source_code();
  1334. }
  1335. return RS::ShaderNativeSourceCode();
  1336. }
  1337. /* COMMON MATERIAL API */
  1338. RID RendererStorageRD::material_create() {
  1339. Material material;
  1340. material.data = nullptr;
  1341. material.shader = nullptr;
  1342. material.shader_type = SHADER_TYPE_MAX;
  1343. material.update_next = nullptr;
  1344. material.update_requested = false;
  1345. material.uniform_dirty = false;
  1346. material.texture_dirty = false;
  1347. material.priority = 0;
  1348. RID id = material_owner.make_rid(material);
  1349. {
  1350. Material *material_ptr = material_owner.getornull(id);
  1351. material_ptr->self = id;
  1352. }
  1353. return id;
  1354. }
  1355. void RendererStorageRD::_material_queue_update(Material *material, bool p_uniform, bool p_texture) {
  1356. if (material->update_requested) {
  1357. return;
  1358. }
  1359. material->update_next = material_update_list;
  1360. material_update_list = material;
  1361. material->update_requested = true;
  1362. material->uniform_dirty = material->uniform_dirty || p_uniform;
  1363. material->texture_dirty = material->texture_dirty || p_texture;
  1364. }
  1365. void RendererStorageRD::material_set_shader(RID p_material, RID p_shader) {
  1366. Material *material = material_owner.getornull(p_material);
  1367. ERR_FAIL_COND(!material);
  1368. if (material->data) {
  1369. memdelete(material->data);
  1370. material->data = nullptr;
  1371. }
  1372. if (material->shader) {
  1373. material->shader->owners.erase(material);
  1374. material->shader = nullptr;
  1375. material->shader_type = SHADER_TYPE_MAX;
  1376. }
  1377. if (p_shader.is_null()) {
  1378. material->dependency.changed_notify(DEPENDENCY_CHANGED_MATERIAL);
  1379. material->shader_id = 0;
  1380. return;
  1381. }
  1382. Shader *shader = shader_owner.getornull(p_shader);
  1383. ERR_FAIL_COND(!shader);
  1384. material->shader = shader;
  1385. material->shader_type = shader->type;
  1386. material->shader_id = p_shader.get_local_index();
  1387. shader->owners.insert(material);
  1388. if (shader->type == SHADER_TYPE_MAX) {
  1389. return;
  1390. }
  1391. ERR_FAIL_COND(shader->data == nullptr);
  1392. material->data = material_data_request_func[shader->type](shader->data);
  1393. material->data->self = p_material;
  1394. material->data->set_next_pass(material->next_pass);
  1395. material->data->set_render_priority(material->priority);
  1396. //updating happens later
  1397. material->dependency.changed_notify(DEPENDENCY_CHANGED_MATERIAL);
  1398. _material_queue_update(material, true, true);
  1399. }
  1400. void RendererStorageRD::material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) {
  1401. Material *material = material_owner.getornull(p_material);
  1402. ERR_FAIL_COND(!material);
  1403. if (p_value.get_type() == Variant::NIL) {
  1404. material->params.erase(p_param);
  1405. } else {
  1406. material->params[p_param] = p_value;
  1407. }
  1408. if (material->shader && material->shader->data) { //shader is valid
  1409. bool is_texture = material->shader->data->is_param_texture(p_param);
  1410. _material_queue_update(material, !is_texture, is_texture);
  1411. } else {
  1412. _material_queue_update(material, true, true);
  1413. }
  1414. }
  1415. Variant RendererStorageRD::material_get_param(RID p_material, const StringName &p_param) const {
  1416. Material *material = material_owner.getornull(p_material);
  1417. ERR_FAIL_COND_V(!material, Variant());
  1418. if (material->params.has(p_param)) {
  1419. return material->params[p_param];
  1420. } else {
  1421. return Variant();
  1422. }
  1423. }
  1424. void RendererStorageRD::material_set_next_pass(RID p_material, RID p_next_material) {
  1425. Material *material = material_owner.getornull(p_material);
  1426. ERR_FAIL_COND(!material);
  1427. if (material->next_pass == p_next_material) {
  1428. return;
  1429. }
  1430. material->next_pass = p_next_material;
  1431. if (material->data) {
  1432. material->data->set_next_pass(p_next_material);
  1433. }
  1434. material->dependency.changed_notify(DEPENDENCY_CHANGED_MATERIAL);
  1435. }
  1436. void RendererStorageRD::material_set_render_priority(RID p_material, int priority) {
  1437. Material *material = material_owner.getornull(p_material);
  1438. ERR_FAIL_COND(!material);
  1439. material->priority = priority;
  1440. if (material->data) {
  1441. material->data->set_render_priority(priority);
  1442. }
  1443. }
  1444. bool RendererStorageRD::material_is_animated(RID p_material) {
  1445. Material *material = material_owner.getornull(p_material);
  1446. ERR_FAIL_COND_V(!material, false);
  1447. if (material->shader && material->shader->data) {
  1448. if (material->shader->data->is_animated()) {
  1449. return true;
  1450. } else if (material->next_pass.is_valid()) {
  1451. return material_is_animated(material->next_pass);
  1452. }
  1453. }
  1454. return false; //by default nothing is animated
  1455. }
  1456. bool RendererStorageRD::material_casts_shadows(RID p_material) {
  1457. Material *material = material_owner.getornull(p_material);
  1458. ERR_FAIL_COND_V(!material, true);
  1459. if (material->shader && material->shader->data) {
  1460. if (material->shader->data->casts_shadows()) {
  1461. return true;
  1462. } else if (material->next_pass.is_valid()) {
  1463. return material_casts_shadows(material->next_pass);
  1464. }
  1465. }
  1466. return true; //by default everything casts shadows
  1467. }
  1468. void RendererStorageRD::material_get_instance_shader_parameters(RID p_material, List<InstanceShaderParam> *r_parameters) {
  1469. Material *material = material_owner.getornull(p_material);
  1470. ERR_FAIL_COND(!material);
  1471. if (material->shader && material->shader->data) {
  1472. material->shader->data->get_instance_param_list(r_parameters);
  1473. if (material->next_pass.is_valid()) {
  1474. material_get_instance_shader_parameters(material->next_pass, r_parameters);
  1475. }
  1476. }
  1477. }
  1478. void RendererStorageRD::material_update_dependency(RID p_material, DependencyTracker *p_instance) {
  1479. Material *material = material_owner.getornull(p_material);
  1480. ERR_FAIL_COND(!material);
  1481. p_instance->update_dependency(&material->dependency);
  1482. if (material->next_pass.is_valid()) {
  1483. material_update_dependency(material->next_pass, p_instance);
  1484. }
  1485. }
  1486. void RendererStorageRD::material_set_data_request_function(ShaderType p_shader_type, MaterialDataRequestFunction p_function) {
  1487. ERR_FAIL_INDEX(p_shader_type, SHADER_TYPE_MAX);
  1488. material_data_request_func[p_shader_type] = p_function;
  1489. }
  1490. _FORCE_INLINE_ static void _fill_std140_variant_ubo_value(ShaderLanguage::DataType type, const Variant &value, uint8_t *data, bool p_linear_color) {
  1491. switch (type) {
  1492. case ShaderLanguage::TYPE_BOOL: {
  1493. bool v = value;
  1494. uint32_t *gui = (uint32_t *)data;
  1495. *gui = v ? 1 : 0;
  1496. } break;
  1497. case ShaderLanguage::TYPE_BVEC2: {
  1498. int v = value;
  1499. uint32_t *gui = (uint32_t *)data;
  1500. gui[0] = v & 1 ? 1 : 0;
  1501. gui[1] = v & 2 ? 1 : 0;
  1502. } break;
  1503. case ShaderLanguage::TYPE_BVEC3: {
  1504. int v = value;
  1505. uint32_t *gui = (uint32_t *)data;
  1506. gui[0] = (v & 1) ? 1 : 0;
  1507. gui[1] = (v & 2) ? 1 : 0;
  1508. gui[2] = (v & 4) ? 1 : 0;
  1509. } break;
  1510. case ShaderLanguage::TYPE_BVEC4: {
  1511. int v = value;
  1512. uint32_t *gui = (uint32_t *)data;
  1513. gui[0] = (v & 1) ? 1 : 0;
  1514. gui[1] = (v & 2) ? 1 : 0;
  1515. gui[2] = (v & 4) ? 1 : 0;
  1516. gui[3] = (v & 8) ? 1 : 0;
  1517. } break;
  1518. case ShaderLanguage::TYPE_INT: {
  1519. int v = value;
  1520. int32_t *gui = (int32_t *)data;
  1521. gui[0] = v;
  1522. } break;
  1523. case ShaderLanguage::TYPE_IVEC2: {
  1524. Vector<int> iv = value;
  1525. int s = iv.size();
  1526. int32_t *gui = (int32_t *)data;
  1527. const int *r = iv.ptr();
  1528. for (int i = 0; i < 2; i++) {
  1529. if (i < s) {
  1530. gui[i] = r[i];
  1531. } else {
  1532. gui[i] = 0;
  1533. }
  1534. }
  1535. } break;
  1536. case ShaderLanguage::TYPE_IVEC3: {
  1537. Vector<int> iv = value;
  1538. int s = iv.size();
  1539. int32_t *gui = (int32_t *)data;
  1540. const int *r = iv.ptr();
  1541. for (int i = 0; i < 3; i++) {
  1542. if (i < s) {
  1543. gui[i] = r[i];
  1544. } else {
  1545. gui[i] = 0;
  1546. }
  1547. }
  1548. } break;
  1549. case ShaderLanguage::TYPE_IVEC4: {
  1550. Vector<int> iv = value;
  1551. int s = iv.size();
  1552. int32_t *gui = (int32_t *)data;
  1553. const int *r = iv.ptr();
  1554. for (int i = 0; i < 4; i++) {
  1555. if (i < s) {
  1556. gui[i] = r[i];
  1557. } else {
  1558. gui[i] = 0;
  1559. }
  1560. }
  1561. } break;
  1562. case ShaderLanguage::TYPE_UINT: {
  1563. int v = value;
  1564. uint32_t *gui = (uint32_t *)data;
  1565. gui[0] = v;
  1566. } break;
  1567. case ShaderLanguage::TYPE_UVEC2: {
  1568. Vector<int> iv = value;
  1569. int s = iv.size();
  1570. uint32_t *gui = (uint32_t *)data;
  1571. const int *r = iv.ptr();
  1572. for (int i = 0; i < 2; i++) {
  1573. if (i < s) {
  1574. gui[i] = r[i];
  1575. } else {
  1576. gui[i] = 0;
  1577. }
  1578. }
  1579. } break;
  1580. case ShaderLanguage::TYPE_UVEC3: {
  1581. Vector<int> iv = value;
  1582. int s = iv.size();
  1583. uint32_t *gui = (uint32_t *)data;
  1584. const int *r = iv.ptr();
  1585. for (int i = 0; i < 3; i++) {
  1586. if (i < s) {
  1587. gui[i] = r[i];
  1588. } else {
  1589. gui[i] = 0;
  1590. }
  1591. }
  1592. } break;
  1593. case ShaderLanguage::TYPE_UVEC4: {
  1594. Vector<int> iv = value;
  1595. int s = iv.size();
  1596. uint32_t *gui = (uint32_t *)data;
  1597. const int *r = iv.ptr();
  1598. for (int i = 0; i < 4; i++) {
  1599. if (i < s) {
  1600. gui[i] = r[i];
  1601. } else {
  1602. gui[i] = 0;
  1603. }
  1604. }
  1605. } break;
  1606. case ShaderLanguage::TYPE_FLOAT: {
  1607. float v = value;
  1608. float *gui = (float *)data;
  1609. gui[0] = v;
  1610. } break;
  1611. case ShaderLanguage::TYPE_VEC2: {
  1612. Vector2 v = value;
  1613. float *gui = (float *)data;
  1614. gui[0] = v.x;
  1615. gui[1] = v.y;
  1616. } break;
  1617. case ShaderLanguage::TYPE_VEC3: {
  1618. Vector3 v = value;
  1619. float *gui = (float *)data;
  1620. gui[0] = v.x;
  1621. gui[1] = v.y;
  1622. gui[2] = v.z;
  1623. } break;
  1624. case ShaderLanguage::TYPE_VEC4: {
  1625. float *gui = (float *)data;
  1626. if (value.get_type() == Variant::COLOR) {
  1627. Color v = value;
  1628. if (p_linear_color) {
  1629. v = v.to_linear();
  1630. }
  1631. gui[0] = v.r;
  1632. gui[1] = v.g;
  1633. gui[2] = v.b;
  1634. gui[3] = v.a;
  1635. } else if (value.get_type() == Variant::RECT2) {
  1636. Rect2 v = value;
  1637. gui[0] = v.position.x;
  1638. gui[1] = v.position.y;
  1639. gui[2] = v.size.x;
  1640. gui[3] = v.size.y;
  1641. } else if (value.get_type() == Variant::QUAT) {
  1642. Quat v = value;
  1643. gui[0] = v.x;
  1644. gui[1] = v.y;
  1645. gui[2] = v.z;
  1646. gui[3] = v.w;
  1647. } else {
  1648. Plane v = value;
  1649. gui[0] = v.normal.x;
  1650. gui[1] = v.normal.y;
  1651. gui[2] = v.normal.z;
  1652. gui[3] = v.d;
  1653. }
  1654. } break;
  1655. case ShaderLanguage::TYPE_MAT2: {
  1656. Transform2D v = value;
  1657. float *gui = (float *)data;
  1658. //in std140 members of mat2 are treated as vec4s
  1659. gui[0] = v.elements[0][0];
  1660. gui[1] = v.elements[0][1];
  1661. gui[2] = 0;
  1662. gui[3] = 0;
  1663. gui[4] = v.elements[1][0];
  1664. gui[5] = v.elements[1][1];
  1665. gui[6] = 0;
  1666. gui[7] = 0;
  1667. } break;
  1668. case ShaderLanguage::TYPE_MAT3: {
  1669. Basis v = value;
  1670. float *gui = (float *)data;
  1671. gui[0] = v.elements[0][0];
  1672. gui[1] = v.elements[1][0];
  1673. gui[2] = v.elements[2][0];
  1674. gui[3] = 0;
  1675. gui[4] = v.elements[0][1];
  1676. gui[5] = v.elements[1][1];
  1677. gui[6] = v.elements[2][1];
  1678. gui[7] = 0;
  1679. gui[8] = v.elements[0][2];
  1680. gui[9] = v.elements[1][2];
  1681. gui[10] = v.elements[2][2];
  1682. gui[11] = 0;
  1683. } break;
  1684. case ShaderLanguage::TYPE_MAT4: {
  1685. Transform v = value;
  1686. float *gui = (float *)data;
  1687. gui[0] = v.basis.elements[0][0];
  1688. gui[1] = v.basis.elements[1][0];
  1689. gui[2] = v.basis.elements[2][0];
  1690. gui[3] = 0;
  1691. gui[4] = v.basis.elements[0][1];
  1692. gui[5] = v.basis.elements[1][1];
  1693. gui[6] = v.basis.elements[2][1];
  1694. gui[7] = 0;
  1695. gui[8] = v.basis.elements[0][2];
  1696. gui[9] = v.basis.elements[1][2];
  1697. gui[10] = v.basis.elements[2][2];
  1698. gui[11] = 0;
  1699. gui[12] = v.origin.x;
  1700. gui[13] = v.origin.y;
  1701. gui[14] = v.origin.z;
  1702. gui[15] = 1;
  1703. } break;
  1704. default: {
  1705. }
  1706. }
  1707. }
  1708. _FORCE_INLINE_ static void _fill_std140_ubo_value(ShaderLanguage::DataType type, const Vector<ShaderLanguage::ConstantNode::Value> &value, uint8_t *data) {
  1709. switch (type) {
  1710. case ShaderLanguage::TYPE_BOOL: {
  1711. uint32_t *gui = (uint32_t *)data;
  1712. *gui = value[0].boolean ? 1 : 0;
  1713. } break;
  1714. case ShaderLanguage::TYPE_BVEC2: {
  1715. uint32_t *gui = (uint32_t *)data;
  1716. gui[0] = value[0].boolean ? 1 : 0;
  1717. gui[1] = value[1].boolean ? 1 : 0;
  1718. } break;
  1719. case ShaderLanguage::TYPE_BVEC3: {
  1720. uint32_t *gui = (uint32_t *)data;
  1721. gui[0] = value[0].boolean ? 1 : 0;
  1722. gui[1] = value[1].boolean ? 1 : 0;
  1723. gui[2] = value[2].boolean ? 1 : 0;
  1724. } break;
  1725. case ShaderLanguage::TYPE_BVEC4: {
  1726. uint32_t *gui = (uint32_t *)data;
  1727. gui[0] = value[0].boolean ? 1 : 0;
  1728. gui[1] = value[1].boolean ? 1 : 0;
  1729. gui[2] = value[2].boolean ? 1 : 0;
  1730. gui[3] = value[3].boolean ? 1 : 0;
  1731. } break;
  1732. case ShaderLanguage::TYPE_INT: {
  1733. int32_t *gui = (int32_t *)data;
  1734. gui[0] = value[0].sint;
  1735. } break;
  1736. case ShaderLanguage::TYPE_IVEC2: {
  1737. int32_t *gui = (int32_t *)data;
  1738. for (int i = 0; i < 2; i++) {
  1739. gui[i] = value[i].sint;
  1740. }
  1741. } break;
  1742. case ShaderLanguage::TYPE_IVEC3: {
  1743. int32_t *gui = (int32_t *)data;
  1744. for (int i = 0; i < 3; i++) {
  1745. gui[i] = value[i].sint;
  1746. }
  1747. } break;
  1748. case ShaderLanguage::TYPE_IVEC4: {
  1749. int32_t *gui = (int32_t *)data;
  1750. for (int i = 0; i < 4; i++) {
  1751. gui[i] = value[i].sint;
  1752. }
  1753. } break;
  1754. case ShaderLanguage::TYPE_UINT: {
  1755. uint32_t *gui = (uint32_t *)data;
  1756. gui[0] = value[0].uint;
  1757. } break;
  1758. case ShaderLanguage::TYPE_UVEC2: {
  1759. int32_t *gui = (int32_t *)data;
  1760. for (int i = 0; i < 2; i++) {
  1761. gui[i] = value[i].uint;
  1762. }
  1763. } break;
  1764. case ShaderLanguage::TYPE_UVEC3: {
  1765. int32_t *gui = (int32_t *)data;
  1766. for (int i = 0; i < 3; i++) {
  1767. gui[i] = value[i].uint;
  1768. }
  1769. } break;
  1770. case ShaderLanguage::TYPE_UVEC4: {
  1771. int32_t *gui = (int32_t *)data;
  1772. for (int i = 0; i < 4; i++) {
  1773. gui[i] = value[i].uint;
  1774. }
  1775. } break;
  1776. case ShaderLanguage::TYPE_FLOAT: {
  1777. float *gui = (float *)data;
  1778. gui[0] = value[0].real;
  1779. } break;
  1780. case ShaderLanguage::TYPE_VEC2: {
  1781. float *gui = (float *)data;
  1782. for (int i = 0; i < 2; i++) {
  1783. gui[i] = value[i].real;
  1784. }
  1785. } break;
  1786. case ShaderLanguage::TYPE_VEC3: {
  1787. float *gui = (float *)data;
  1788. for (int i = 0; i < 3; i++) {
  1789. gui[i] = value[i].real;
  1790. }
  1791. } break;
  1792. case ShaderLanguage::TYPE_VEC4: {
  1793. float *gui = (float *)data;
  1794. for (int i = 0; i < 4; i++) {
  1795. gui[i] = value[i].real;
  1796. }
  1797. } break;
  1798. case ShaderLanguage::TYPE_MAT2: {
  1799. float *gui = (float *)data;
  1800. //in std140 members of mat2 are treated as vec4s
  1801. gui[0] = value[0].real;
  1802. gui[1] = value[1].real;
  1803. gui[2] = 0;
  1804. gui[3] = 0;
  1805. gui[4] = value[2].real;
  1806. gui[5] = value[3].real;
  1807. gui[6] = 0;
  1808. gui[7] = 0;
  1809. } break;
  1810. case ShaderLanguage::TYPE_MAT3: {
  1811. float *gui = (float *)data;
  1812. gui[0] = value[0].real;
  1813. gui[1] = value[1].real;
  1814. gui[2] = value[2].real;
  1815. gui[3] = 0;
  1816. gui[4] = value[3].real;
  1817. gui[5] = value[4].real;
  1818. gui[6] = value[5].real;
  1819. gui[7] = 0;
  1820. gui[8] = value[6].real;
  1821. gui[9] = value[7].real;
  1822. gui[10] = value[8].real;
  1823. gui[11] = 0;
  1824. } break;
  1825. case ShaderLanguage::TYPE_MAT4: {
  1826. float *gui = (float *)data;
  1827. for (int i = 0; i < 16; i++) {
  1828. gui[i] = value[i].real;
  1829. }
  1830. } break;
  1831. default: {
  1832. }
  1833. }
  1834. }
  1835. _FORCE_INLINE_ static void _fill_std140_ubo_empty(ShaderLanguage::DataType type, uint8_t *data) {
  1836. switch (type) {
  1837. case ShaderLanguage::TYPE_BOOL:
  1838. case ShaderLanguage::TYPE_INT:
  1839. case ShaderLanguage::TYPE_UINT:
  1840. case ShaderLanguage::TYPE_FLOAT: {
  1841. zeromem(data, 4);
  1842. } break;
  1843. case ShaderLanguage::TYPE_BVEC2:
  1844. case ShaderLanguage::TYPE_IVEC2:
  1845. case ShaderLanguage::TYPE_UVEC2:
  1846. case ShaderLanguage::TYPE_VEC2: {
  1847. zeromem(data, 8);
  1848. } break;
  1849. case ShaderLanguage::TYPE_BVEC3:
  1850. case ShaderLanguage::TYPE_IVEC3:
  1851. case ShaderLanguage::TYPE_UVEC3:
  1852. case ShaderLanguage::TYPE_VEC3:
  1853. case ShaderLanguage::TYPE_BVEC4:
  1854. case ShaderLanguage::TYPE_IVEC4:
  1855. case ShaderLanguage::TYPE_UVEC4:
  1856. case ShaderLanguage::TYPE_VEC4: {
  1857. zeromem(data, 16);
  1858. } break;
  1859. case ShaderLanguage::TYPE_MAT2: {
  1860. zeromem(data, 32);
  1861. } break;
  1862. case ShaderLanguage::TYPE_MAT3: {
  1863. zeromem(data, 48);
  1864. } break;
  1865. case ShaderLanguage::TYPE_MAT4: {
  1866. zeromem(data, 64);
  1867. } break;
  1868. default: {
  1869. }
  1870. }
  1871. }
  1872. 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) {
  1873. bool uses_global_buffer = false;
  1874. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = p_uniforms.front(); E; E = E->next()) {
  1875. if (E->get().order < 0) {
  1876. continue; // texture, does not go here
  1877. }
  1878. if (E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  1879. continue; //instance uniforms don't appear in the bufferr
  1880. }
  1881. if (E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  1882. //this is a global variable, get the index to it
  1883. RendererStorageRD *rs = base_singleton;
  1884. GlobalVariables::Variable *gv = rs->global_variables.variables.getptr(E->key());
  1885. uint32_t index = 0;
  1886. if (gv) {
  1887. index = gv->buffer_index;
  1888. } else {
  1889. WARN_PRINT("Shader uses global uniform '" + E->key() + "', but it was removed at some point. Material will not display correctly.");
  1890. }
  1891. uint32_t offset = p_uniform_offsets[E->get().order];
  1892. uint32_t *intptr = (uint32_t *)&p_buffer[offset];
  1893. *intptr = index;
  1894. uses_global_buffer = true;
  1895. continue;
  1896. }
  1897. //regular uniform
  1898. uint32_t offset = p_uniform_offsets[E->get().order];
  1899. #ifdef DEBUG_ENABLED
  1900. uint32_t size = ShaderLanguage::get_type_size(E->get().type);
  1901. ERR_CONTINUE(offset + size > p_buffer_size);
  1902. #endif
  1903. uint8_t *data = &p_buffer[offset];
  1904. const Map<StringName, Variant>::Element *V = p_parameters.find(E->key());
  1905. if (V) {
  1906. //user provided
  1907. _fill_std140_variant_ubo_value(E->get().type, V->get(), data, p_use_linear_color);
  1908. } else if (E->get().default_value.size()) {
  1909. //default value
  1910. _fill_std140_ubo_value(E->get().type, E->get().default_value, data);
  1911. //value=E->get().default_value;
  1912. } else {
  1913. //zero because it was not provided
  1914. if (E->get().type == ShaderLanguage::TYPE_VEC4 && E->get().hint == ShaderLanguage::ShaderNode::Uniform::HINT_COLOR) {
  1915. //colors must be set as black, with alpha as 1.0
  1916. _fill_std140_variant_ubo_value(E->get().type, Color(0, 0, 0, 1), data, p_use_linear_color);
  1917. } else {
  1918. //else just zero it out
  1919. _fill_std140_ubo_empty(E->get().type, data);
  1920. }
  1921. }
  1922. }
  1923. if (uses_global_buffer != (global_buffer_E != nullptr)) {
  1924. RendererStorageRD *rs = base_singleton;
  1925. if (uses_global_buffer) {
  1926. global_buffer_E = rs->global_variables.materials_using_buffer.push_back(self);
  1927. } else {
  1928. rs->global_variables.materials_using_buffer.erase(global_buffer_E);
  1929. global_buffer_E = nullptr;
  1930. }
  1931. }
  1932. }
  1933. RendererStorageRD::MaterialData::~MaterialData() {
  1934. if (global_buffer_E) {
  1935. //unregister global buffers
  1936. RendererStorageRD *rs = base_singleton;
  1937. rs->global_variables.materials_using_buffer.erase(global_buffer_E);
  1938. }
  1939. if (global_texture_E) {
  1940. //unregister global textures
  1941. RendererStorageRD *rs = base_singleton;
  1942. for (Map<StringName, uint64_t>::Element *E = used_global_textures.front(); E; E = E->next()) {
  1943. GlobalVariables::Variable *v = rs->global_variables.variables.getptr(E->key());
  1944. if (v) {
  1945. v->texture_materials.erase(self);
  1946. }
  1947. }
  1948. //unregister material from those using global textures
  1949. rs->global_variables.materials_using_texture.erase(global_texture_E);
  1950. }
  1951. }
  1952. 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) {
  1953. RendererStorageRD *singleton = (RendererStorageRD *)RendererStorage::base_singleton;
  1954. #ifdef TOOLS_ENABLED
  1955. Texture *roughness_detect_texture = nullptr;
  1956. RS::TextureDetectRoughnessChannel roughness_channel = RS::TEXTURE_DETECT_ROUGHNESS_R;
  1957. Texture *normal_detect_texture = nullptr;
  1958. #endif
  1959. bool uses_global_textures = false;
  1960. global_textures_pass++;
  1961. for (int i = 0; i < p_texture_uniforms.size(); i++) {
  1962. const StringName &uniform_name = p_texture_uniforms[i].name;
  1963. RID texture;
  1964. if (p_texture_uniforms[i].global) {
  1965. RendererStorageRD *rs = base_singleton;
  1966. uses_global_textures = true;
  1967. GlobalVariables::Variable *v = rs->global_variables.variables.getptr(uniform_name);
  1968. if (v) {
  1969. if (v->buffer_index >= 0) {
  1970. WARN_PRINT("Shader uses global uniform texture '" + String(uniform_name) + "', but it changed type and is no longer a texture!.");
  1971. } else {
  1972. Map<StringName, uint64_t>::Element *E = used_global_textures.find(uniform_name);
  1973. if (!E) {
  1974. E = used_global_textures.insert(uniform_name, global_textures_pass);
  1975. v->texture_materials.insert(self);
  1976. } else {
  1977. E->get() = global_textures_pass;
  1978. }
  1979. texture = v->override.get_type() != Variant::NIL ? v->override : v->value;
  1980. }
  1981. } else {
  1982. WARN_PRINT("Shader uses global uniform texture '" + String(uniform_name) + "', but it was removed at some point. Material will not display correctly.");
  1983. }
  1984. } else {
  1985. if (!texture.is_valid()) {
  1986. const Map<StringName, Variant>::Element *V = p_parameters.find(uniform_name);
  1987. if (V) {
  1988. texture = V->get();
  1989. }
  1990. }
  1991. if (!texture.is_valid()) {
  1992. const Map<StringName, RID>::Element *W = p_default_textures.find(uniform_name);
  1993. if (W) {
  1994. texture = W->get();
  1995. }
  1996. }
  1997. }
  1998. RID rd_texture;
  1999. if (texture.is_null()) {
  2000. //check default usage
  2001. switch (p_texture_uniforms[i].hint) {
  2002. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK:
  2003. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO: {
  2004. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_BLACK);
  2005. } break;
  2006. case ShaderLanguage::ShaderNode::Uniform::HINT_NONE: {
  2007. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_NORMAL);
  2008. } break;
  2009. case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
  2010. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_ANISO);
  2011. } break;
  2012. default: {
  2013. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_WHITE);
  2014. } break;
  2015. }
  2016. } else {
  2017. 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);
  2018. Texture *tex = singleton->texture_owner.getornull(texture);
  2019. if (tex) {
  2020. rd_texture = (srgb && tex->rd_texture_srgb.is_valid()) ? tex->rd_texture_srgb : tex->rd_texture;
  2021. #ifdef TOOLS_ENABLED
  2022. if (tex->detect_3d_callback && p_use_linear_color) {
  2023. tex->detect_3d_callback(tex->detect_3d_callback_ud);
  2024. }
  2025. 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)) {
  2026. if (p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL) {
  2027. normal_detect_texture = tex;
  2028. }
  2029. tex->detect_normal_callback(tex->detect_normal_callback_ud);
  2030. }
  2031. 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)) {
  2032. //find the normal texture
  2033. roughness_detect_texture = tex;
  2034. roughness_channel = RS::TextureDetectRoughnessChannel(p_texture_uniforms[i].hint - ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_R);
  2035. }
  2036. #endif
  2037. }
  2038. if (rd_texture.is_null()) {
  2039. //wtf
  2040. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_WHITE);
  2041. }
  2042. }
  2043. p_textures[i] = rd_texture;
  2044. }
  2045. #ifdef TOOLS_ENABLED
  2046. if (roughness_detect_texture && normal_detect_texture && normal_detect_texture->path != String()) {
  2047. roughness_detect_texture->detect_roughness_callback(roughness_detect_texture->detect_roughness_callback_ud, normal_detect_texture->path, roughness_channel);
  2048. }
  2049. #endif
  2050. {
  2051. //for textures no longer used, unregister them
  2052. List<Map<StringName, uint64_t>::Element *> to_delete;
  2053. RendererStorageRD *rs = base_singleton;
  2054. for (Map<StringName, uint64_t>::Element *E = used_global_textures.front(); E; E = E->next()) {
  2055. if (E->get() != global_textures_pass) {
  2056. to_delete.push_back(E);
  2057. GlobalVariables::Variable *v = rs->global_variables.variables.getptr(E->key());
  2058. if (v) {
  2059. v->texture_materials.erase(self);
  2060. }
  2061. }
  2062. }
  2063. while (to_delete.front()) {
  2064. used_global_textures.erase(to_delete.front()->get());
  2065. to_delete.pop_front();
  2066. }
  2067. //handle registering/unregistering global textures
  2068. if (uses_global_textures != (global_texture_E != nullptr)) {
  2069. if (uses_global_textures) {
  2070. global_texture_E = rs->global_variables.materials_using_texture.push_back(self);
  2071. } else {
  2072. rs->global_variables.materials_using_texture.erase(global_texture_E);
  2073. global_texture_E = nullptr;
  2074. }
  2075. }
  2076. }
  2077. }
  2078. void RendererStorageRD::material_force_update_textures(RID p_material, ShaderType p_shader_type) {
  2079. Material *material = material_owner.getornull(p_material);
  2080. if (material->shader_type != p_shader_type) {
  2081. return;
  2082. }
  2083. if (material->data) {
  2084. material->data->update_parameters(material->params, false, true);
  2085. }
  2086. }
  2087. void RendererStorageRD::_update_queued_materials() {
  2088. Material *material = material_update_list;
  2089. while (material) {
  2090. Material *next = material->update_next;
  2091. if (material->data) {
  2092. material->data->update_parameters(material->params, material->uniform_dirty, material->texture_dirty);
  2093. }
  2094. material->update_requested = false;
  2095. material->texture_dirty = false;
  2096. material->uniform_dirty = false;
  2097. material->update_next = nullptr;
  2098. material = next;
  2099. }
  2100. material_update_list = nullptr;
  2101. }
  2102. /* MESH API */
  2103. RID RendererStorageRD::mesh_create() {
  2104. return mesh_owner.make_rid(Mesh());
  2105. }
  2106. void RendererStorageRD::mesh_set_blend_shape_count(RID p_mesh, int p_blend_shape_count) {
  2107. ERR_FAIL_COND(p_blend_shape_count < 0);
  2108. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2109. ERR_FAIL_COND(!mesh);
  2110. ERR_FAIL_COND(mesh->surface_count > 0); //surfaces already exist
  2111. mesh->blend_shape_count = p_blend_shape_count;
  2112. }
  2113. /// Returns stride
  2114. void RendererStorageRD::mesh_add_surface(RID p_mesh, const RS::SurfaceData &p_surface) {
  2115. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2116. ERR_FAIL_COND(!mesh);
  2117. #ifdef DEBUG_ENABLED
  2118. //do a validation, to catch errors first
  2119. {
  2120. uint32_t stride = 0;
  2121. uint32_t attrib_stride = 0;
  2122. uint32_t skin_stride = 0;
  2123. for (int i = 0; i < RS::ARRAY_WEIGHTS; i++) {
  2124. if ((p_surface.format & (1 << i))) {
  2125. switch (i) {
  2126. case RS::ARRAY_VERTEX: {
  2127. if (p_surface.format & RS::ARRAY_FLAG_USE_2D_VERTICES) {
  2128. stride += sizeof(float) * 2;
  2129. } else {
  2130. stride += sizeof(float) * 3;
  2131. }
  2132. } break;
  2133. case RS::ARRAY_NORMAL: {
  2134. stride += sizeof(int32_t);
  2135. } break;
  2136. case RS::ARRAY_TANGENT: {
  2137. stride += sizeof(int32_t);
  2138. } break;
  2139. case RS::ARRAY_COLOR: {
  2140. attrib_stride += sizeof(int16_t) * 4;
  2141. } break;
  2142. case RS::ARRAY_TEX_UV: {
  2143. attrib_stride += sizeof(float) * 2;
  2144. } break;
  2145. case RS::ARRAY_TEX_UV2: {
  2146. attrib_stride += sizeof(float) * 2;
  2147. } break;
  2148. case RS::ARRAY_CUSTOM0:
  2149. case RS::ARRAY_CUSTOM1:
  2150. case RS::ARRAY_CUSTOM2:
  2151. case RS::ARRAY_CUSTOM3: {
  2152. int idx = i - RS::ARRAY_CUSTOM0;
  2153. 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 };
  2154. uint32_t fmt = (p_surface.format >> fmt_shift[idx]) & RS::ARRAY_FORMAT_CUSTOM_MASK;
  2155. uint32_t fmtsize[RS::ARRAY_CUSTOM_MAX] = { 4, 4, 4, 8, 4, 8, 12, 16 };
  2156. attrib_stride += fmtsize[fmt];
  2157. } break;
  2158. case RS::ARRAY_WEIGHTS:
  2159. case RS::ARRAY_BONES: {
  2160. //uses a separate array
  2161. bool use_8 = p_surface.format & RS::ARRAY_FLAG_USE_8_BONE_WEIGHTS;
  2162. skin_stride += sizeof(int16_t) * (use_8 ? 16 : 8);
  2163. } break;
  2164. }
  2165. }
  2166. }
  2167. int expected_size = stride * p_surface.vertex_count;
  2168. 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) + ")");
  2169. int bs_expected_size = expected_size * mesh->blend_shape_count;
  2170. ERR_FAIL_COND_MSG(bs_expected_size != p_surface.blend_shape_data.size(), "Size of blend shape data provided (" + itos(p_surface.blend_shape_data.size()) + ") does not match expected (" + itos(bs_expected_size) + ")");
  2171. int expected_attrib_size = attrib_stride * p_surface.vertex_count;
  2172. 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) + ")");
  2173. if ((p_surface.format & RS::ARRAY_FORMAT_WEIGHTS) && (p_surface.format & RS::ARRAY_FORMAT_BONES)) {
  2174. expected_size = skin_stride * p_surface.vertex_count;
  2175. 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) + ")");
  2176. }
  2177. }
  2178. #endif
  2179. Mesh::Surface *s = memnew(Mesh::Surface);
  2180. s->format = p_surface.format;
  2181. s->primitive = p_surface.primitive;
  2182. bool use_as_storage = (p_surface.skin_data.size() || mesh->blend_shape_count > 0);
  2183. s->vertex_buffer = RD::get_singleton()->vertex_buffer_create(p_surface.vertex_data.size(), p_surface.vertex_data, use_as_storage);
  2184. s->vertex_buffer_size = p_surface.vertex_data.size();
  2185. if (p_surface.attribute_data.size()) {
  2186. s->attribute_buffer = RD::get_singleton()->vertex_buffer_create(p_surface.attribute_data.size(), p_surface.attribute_data);
  2187. }
  2188. if (p_surface.skin_data.size()) {
  2189. s->skin_buffer = RD::get_singleton()->vertex_buffer_create(p_surface.skin_data.size(), p_surface.skin_data, use_as_storage);
  2190. s->skin_buffer_size = p_surface.skin_data.size();
  2191. }
  2192. s->vertex_count = p_surface.vertex_count;
  2193. if (p_surface.format & RS::ARRAY_FORMAT_BONES) {
  2194. mesh->has_bone_weights = true;
  2195. }
  2196. if (p_surface.index_count) {
  2197. bool is_index_16 = p_surface.vertex_count <= 65536;
  2198. 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);
  2199. s->index_count = p_surface.index_count;
  2200. s->index_array = RD::get_singleton()->index_array_create(s->index_buffer, 0, s->index_count);
  2201. if (p_surface.lods.size()) {
  2202. s->lods = memnew_arr(Mesh::Surface::LOD, p_surface.lods.size());
  2203. s->lod_count = p_surface.lods.size();
  2204. for (int i = 0; i < p_surface.lods.size(); i++) {
  2205. uint32_t indices = p_surface.lods[i].index_data.size() / (is_index_16 ? 2 : 4);
  2206. 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);
  2207. s->lods[i].index_array = RD::get_singleton()->index_array_create(s->lods[i].index_buffer, 0, indices);
  2208. s->lods[i].edge_length = p_surface.lods[i].edge_length;
  2209. }
  2210. }
  2211. }
  2212. s->aabb = p_surface.aabb;
  2213. s->bone_aabbs = p_surface.bone_aabbs; //only really useful for returning them.
  2214. if (mesh->blend_shape_count > 0) {
  2215. s->blend_shape_buffer = RD::get_singleton()->storage_buffer_create(p_surface.blend_shape_data.size(), p_surface.blend_shape_data);
  2216. }
  2217. if (use_as_storage) {
  2218. Vector<RD::Uniform> uniforms;
  2219. {
  2220. RD::Uniform u;
  2221. u.binding = 0;
  2222. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  2223. u.ids.push_back(s->vertex_buffer);
  2224. uniforms.push_back(u);
  2225. }
  2226. {
  2227. RD::Uniform u;
  2228. u.binding = 1;
  2229. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  2230. if (s->skin_buffer.is_valid()) {
  2231. u.ids.push_back(s->skin_buffer);
  2232. } else {
  2233. u.ids.push_back(default_rd_storage_buffer);
  2234. }
  2235. uniforms.push_back(u);
  2236. }
  2237. {
  2238. RD::Uniform u;
  2239. u.binding = 2;
  2240. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  2241. if (s->blend_shape_buffer.is_valid()) {
  2242. u.ids.push_back(s->blend_shape_buffer);
  2243. } else {
  2244. u.ids.push_back(default_rd_storage_buffer);
  2245. }
  2246. uniforms.push_back(u);
  2247. }
  2248. s->uniform_set = RD::get_singleton()->uniform_set_create(uniforms, skeleton_shader.version_shader[0], SkeletonShader::UNIFORM_SET_SURFACE);
  2249. }
  2250. if (mesh->surface_count == 0) {
  2251. mesh->bone_aabbs = p_surface.bone_aabbs;
  2252. mesh->aabb = p_surface.aabb;
  2253. } else {
  2254. if (mesh->bone_aabbs.size() < p_surface.bone_aabbs.size()) {
  2255. // ArrayMesh::_surface_set_data only allocates bone_aabbs up to max_bone
  2256. // Each surface may affect different numbers of bones.
  2257. mesh->bone_aabbs.resize(p_surface.bone_aabbs.size());
  2258. }
  2259. for (int i = 0; i < p_surface.bone_aabbs.size(); i++) {
  2260. mesh->bone_aabbs.write[i].merge_with(p_surface.bone_aabbs[i]);
  2261. }
  2262. mesh->aabb.merge_with(p_surface.aabb);
  2263. }
  2264. s->material = p_surface.material;
  2265. mesh->surfaces = (Mesh::Surface **)memrealloc(mesh->surfaces, sizeof(Mesh::Surface *) * (mesh->surface_count + 1));
  2266. mesh->surfaces[mesh->surface_count] = s;
  2267. mesh->surface_count++;
  2268. for (List<MeshInstance *>::Element *E = mesh->instances.front(); E; E = E->next()) {
  2269. //update instances
  2270. MeshInstance *mi = E->get();
  2271. _mesh_instance_add_surface(mi, mesh, mesh->surface_count - 1);
  2272. }
  2273. mesh->dependency.changed_notify(DEPENDENCY_CHANGED_MESH);
  2274. mesh->material_cache.clear();
  2275. }
  2276. int RendererStorageRD::mesh_get_blend_shape_count(RID p_mesh) const {
  2277. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2278. ERR_FAIL_COND_V(!mesh, -1);
  2279. return mesh->blend_shape_count;
  2280. }
  2281. void RendererStorageRD::mesh_set_blend_shape_mode(RID p_mesh, RS::BlendShapeMode p_mode) {
  2282. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2283. ERR_FAIL_COND(!mesh);
  2284. ERR_FAIL_INDEX((int)p_mode, 2);
  2285. mesh->blend_shape_mode = p_mode;
  2286. }
  2287. RS::BlendShapeMode RendererStorageRD::mesh_get_blend_shape_mode(RID p_mesh) const {
  2288. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2289. ERR_FAIL_COND_V(!mesh, RS::BLEND_SHAPE_MODE_NORMALIZED);
  2290. return mesh->blend_shape_mode;
  2291. }
  2292. void RendererStorageRD::mesh_surface_update_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
  2293. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2294. ERR_FAIL_COND(!mesh);
  2295. ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
  2296. ERR_FAIL_COND(p_data.size() == 0);
  2297. uint64_t data_size = p_data.size();
  2298. const uint8_t *r = p_data.ptr();
  2299. RD::get_singleton()->buffer_update(mesh->surfaces[p_surface]->vertex_buffer, p_offset, data_size, r);
  2300. }
  2301. void RendererStorageRD::mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) {
  2302. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2303. ERR_FAIL_COND(!mesh);
  2304. ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
  2305. mesh->surfaces[p_surface]->material = p_material;
  2306. mesh->dependency.changed_notify(DEPENDENCY_CHANGED_MATERIAL);
  2307. mesh->material_cache.clear();
  2308. }
  2309. RID RendererStorageRD::mesh_surface_get_material(RID p_mesh, int p_surface) const {
  2310. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2311. ERR_FAIL_COND_V(!mesh, RID());
  2312. ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_surface, mesh->surface_count, RID());
  2313. return mesh->surfaces[p_surface]->material;
  2314. }
  2315. RS::SurfaceData RendererStorageRD::mesh_get_surface(RID p_mesh, int p_surface) const {
  2316. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2317. ERR_FAIL_COND_V(!mesh, RS::SurfaceData());
  2318. ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_surface, mesh->surface_count, RS::SurfaceData());
  2319. Mesh::Surface &s = *mesh->surfaces[p_surface];
  2320. RS::SurfaceData sd;
  2321. sd.format = s.format;
  2322. sd.vertex_data = RD::get_singleton()->buffer_get_data(s.vertex_buffer);
  2323. if (s.attribute_buffer.is_valid()) {
  2324. sd.attribute_data = RD::get_singleton()->buffer_get_data(s.attribute_buffer);
  2325. }
  2326. if (s.skin_buffer.is_valid()) {
  2327. sd.skin_data = RD::get_singleton()->buffer_get_data(s.skin_buffer);
  2328. }
  2329. sd.vertex_count = s.vertex_count;
  2330. sd.index_count = s.index_count;
  2331. sd.primitive = s.primitive;
  2332. if (sd.index_count) {
  2333. sd.index_data = RD::get_singleton()->buffer_get_data(s.index_buffer);
  2334. }
  2335. sd.aabb = s.aabb;
  2336. for (uint32_t i = 0; i < s.lod_count; i++) {
  2337. RS::SurfaceData::LOD lod;
  2338. lod.edge_length = s.lods[i].edge_length;
  2339. lod.index_data = RD::get_singleton()->buffer_get_data(s.lods[i].index_buffer);
  2340. sd.lods.push_back(lod);
  2341. }
  2342. sd.bone_aabbs = s.bone_aabbs;
  2343. if (s.blend_shape_buffer.is_valid()) {
  2344. sd.blend_shape_data = RD::get_singleton()->buffer_get_data(s.blend_shape_buffer);
  2345. }
  2346. return sd;
  2347. }
  2348. int RendererStorageRD::mesh_get_surface_count(RID p_mesh) const {
  2349. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2350. ERR_FAIL_COND_V(!mesh, 0);
  2351. return mesh->surface_count;
  2352. }
  2353. void RendererStorageRD::mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) {
  2354. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2355. ERR_FAIL_COND(!mesh);
  2356. mesh->custom_aabb = p_aabb;
  2357. }
  2358. AABB RendererStorageRD::mesh_get_custom_aabb(RID p_mesh) const {
  2359. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2360. ERR_FAIL_COND_V(!mesh, AABB());
  2361. return mesh->custom_aabb;
  2362. }
  2363. AABB RendererStorageRD::mesh_get_aabb(RID p_mesh, RID p_skeleton) {
  2364. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2365. ERR_FAIL_COND_V(!mesh, AABB());
  2366. if (mesh->custom_aabb != AABB()) {
  2367. return mesh->custom_aabb;
  2368. }
  2369. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2370. if (!skeleton || skeleton->size == 0) {
  2371. return mesh->aabb;
  2372. }
  2373. AABB aabb;
  2374. for (uint32_t i = 0; i < mesh->surface_count; i++) {
  2375. AABB laabb;
  2376. if ((mesh->surfaces[i]->format & RS::ARRAY_FORMAT_BONES) && mesh->surfaces[i]->bone_aabbs.size()) {
  2377. int bs = mesh->surfaces[i]->bone_aabbs.size();
  2378. const AABB *skbones = mesh->surfaces[i]->bone_aabbs.ptr();
  2379. int sbs = skeleton->size;
  2380. ERR_CONTINUE(bs > sbs);
  2381. const float *baseptr = skeleton->data.ptr();
  2382. bool first = true;
  2383. if (skeleton->use_2d) {
  2384. for (int j = 0; j < bs; j++) {
  2385. if (skbones[0].size == Vector3()) {
  2386. continue; //bone is unused
  2387. }
  2388. const float *dataptr = baseptr + j * 8;
  2389. Transform mtx;
  2390. mtx.basis.elements[0].x = dataptr[0];
  2391. mtx.basis.elements[1].x = dataptr[1];
  2392. mtx.origin.x = dataptr[3];
  2393. mtx.basis.elements[0].y = dataptr[4];
  2394. mtx.basis.elements[1].y = dataptr[5];
  2395. mtx.origin.y = dataptr[7];
  2396. AABB baabb = mtx.xform(skbones[j]);
  2397. if (first) {
  2398. laabb = baabb;
  2399. first = false;
  2400. } else {
  2401. laabb.merge_with(baabb);
  2402. }
  2403. }
  2404. } else {
  2405. for (int j = 0; j < bs; j++) {
  2406. if (skbones[0].size == Vector3()) {
  2407. continue; //bone is unused
  2408. }
  2409. const float *dataptr = baseptr + j * 12;
  2410. Transform mtx;
  2411. mtx.basis.elements[0][0] = dataptr[0];
  2412. mtx.basis.elements[0][1] = dataptr[1];
  2413. mtx.basis.elements[0][2] = dataptr[2];
  2414. mtx.origin.x = dataptr[3];
  2415. mtx.basis.elements[1][0] = dataptr[4];
  2416. mtx.basis.elements[1][1] = dataptr[5];
  2417. mtx.basis.elements[1][2] = dataptr[6];
  2418. mtx.origin.y = dataptr[7];
  2419. mtx.basis.elements[2][0] = dataptr[8];
  2420. mtx.basis.elements[2][1] = dataptr[9];
  2421. mtx.basis.elements[2][2] = dataptr[10];
  2422. mtx.origin.z = dataptr[11];
  2423. AABB baabb = mtx.xform(skbones[j]);
  2424. if (first) {
  2425. laabb = baabb;
  2426. first = false;
  2427. } else {
  2428. laabb.merge_with(baabb);
  2429. }
  2430. }
  2431. }
  2432. if (laabb.size == Vector3()) {
  2433. laabb = mesh->surfaces[i]->aabb;
  2434. }
  2435. } else {
  2436. laabb = mesh->surfaces[i]->aabb;
  2437. }
  2438. if (i == 0) {
  2439. aabb = laabb;
  2440. } else {
  2441. aabb.merge_with(laabb);
  2442. }
  2443. }
  2444. return aabb;
  2445. }
  2446. void RendererStorageRD::mesh_clear(RID p_mesh) {
  2447. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2448. ERR_FAIL_COND(!mesh);
  2449. for (uint32_t i = 0; i < mesh->surface_count; i++) {
  2450. Mesh::Surface &s = *mesh->surfaces[i];
  2451. RD::get_singleton()->free(s.vertex_buffer); //clears arrays as dependency automatically, including all versions
  2452. if (s.attribute_buffer.is_valid()) {
  2453. RD::get_singleton()->free(s.attribute_buffer);
  2454. }
  2455. if (s.skin_buffer.is_valid()) {
  2456. RD::get_singleton()->free(s.skin_buffer);
  2457. }
  2458. if (s.versions) {
  2459. memfree(s.versions); //reallocs, so free with memfree.
  2460. }
  2461. if (s.index_buffer.is_valid()) {
  2462. RD::get_singleton()->free(s.index_buffer);
  2463. }
  2464. if (s.lod_count) {
  2465. for (uint32_t j = 0; j < s.lod_count; j++) {
  2466. RD::get_singleton()->free(s.lods[j].index_buffer);
  2467. }
  2468. memdelete_arr(s.lods);
  2469. }
  2470. if (s.blend_shape_buffer.is_valid()) {
  2471. RD::get_singleton()->free(s.blend_shape_buffer);
  2472. }
  2473. memdelete(mesh->surfaces[i]);
  2474. }
  2475. if (mesh->surfaces) {
  2476. memfree(mesh->surfaces);
  2477. }
  2478. mesh->surfaces = nullptr;
  2479. mesh->surface_count = 0;
  2480. mesh->material_cache.clear();
  2481. //clear instance data
  2482. for (List<MeshInstance *>::Element *E = mesh->instances.front(); E; E = E->next()) {
  2483. MeshInstance *mi = E->get();
  2484. _mesh_instance_clear(mi);
  2485. }
  2486. mesh->has_bone_weights = false;
  2487. mesh->dependency.changed_notify(DEPENDENCY_CHANGED_MESH);
  2488. }
  2489. bool RendererStorageRD::mesh_needs_instance(RID p_mesh, bool p_has_skeleton) {
  2490. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2491. ERR_FAIL_COND_V(!mesh, false);
  2492. return mesh->blend_shape_count > 0 || (mesh->has_bone_weights && p_has_skeleton);
  2493. }
  2494. /* MESH INSTANCE */
  2495. RID RendererStorageRD::mesh_instance_create(RID p_base) {
  2496. Mesh *mesh = mesh_owner.getornull(p_base);
  2497. ERR_FAIL_COND_V(!mesh, RID());
  2498. MeshInstance *mi = memnew(MeshInstance);
  2499. mi->mesh = mesh;
  2500. for (uint32_t i = 0; i < mesh->surface_count; i++) {
  2501. _mesh_instance_add_surface(mi, mesh, i);
  2502. }
  2503. mi->I = mesh->instances.push_back(mi);
  2504. mi->dirty = true;
  2505. return mesh_instance_owner.make_rid(mi);
  2506. }
  2507. void RendererStorageRD::mesh_instance_set_skeleton(RID p_mesh_instance, RID p_skeleton) {
  2508. MeshInstance *mi = mesh_instance_owner.getornull(p_mesh_instance);
  2509. if (mi->skeleton == p_skeleton) {
  2510. return;
  2511. }
  2512. mi->skeleton = p_skeleton;
  2513. mi->skeleton_version = 0;
  2514. mi->dirty = true;
  2515. }
  2516. void RendererStorageRD::mesh_instance_set_blend_shape_weight(RID p_mesh_instance, int p_shape, float p_weight) {
  2517. MeshInstance *mi = mesh_instance_owner.getornull(p_mesh_instance);
  2518. ERR_FAIL_COND(!mi);
  2519. ERR_FAIL_INDEX(p_shape, (int)mi->blend_weights.size());
  2520. mi->blend_weights[p_shape] = p_weight;
  2521. mi->weights_dirty = true;
  2522. //will be eventually updated
  2523. }
  2524. void RendererStorageRD::_mesh_instance_clear(MeshInstance *mi) {
  2525. for (uint32_t i = 0; i < mi->surfaces.size(); i++) {
  2526. if (mi->surfaces[i].vertex_buffer.is_valid()) {
  2527. RD::get_singleton()->free(mi->surfaces[i].vertex_buffer);
  2528. }
  2529. if (mi->surfaces[i].versions) {
  2530. for (uint32_t j = 0; j < mi->surfaces[i].version_count; j++) {
  2531. RD::get_singleton()->free(mi->surfaces[i].versions[j].vertex_array);
  2532. }
  2533. memfree(mi->surfaces[i].versions);
  2534. }
  2535. }
  2536. mi->surfaces.clear();
  2537. if (mi->blend_weights_buffer.is_valid()) {
  2538. RD::get_singleton()->free(mi->blend_weights_buffer);
  2539. }
  2540. mi->blend_weights.clear();
  2541. mi->weights_dirty = false;
  2542. mi->skeleton_version = 0;
  2543. }
  2544. void RendererStorageRD::_mesh_instance_add_surface(MeshInstance *mi, Mesh *mesh, uint32_t p_surface) {
  2545. if (mesh->blend_shape_count > 0 && mi->blend_weights_buffer.is_null()) {
  2546. mi->blend_weights.resize(mesh->blend_shape_count);
  2547. for (uint32_t i = 0; i < mi->blend_weights.size(); i++) {
  2548. mi->blend_weights[i] = 0;
  2549. }
  2550. mi->blend_weights_buffer = RD::get_singleton()->storage_buffer_create(sizeof(float) * mi->blend_weights.size(), mi->blend_weights.to_byte_array());
  2551. mi->weights_dirty = true;
  2552. }
  2553. MeshInstance::Surface s;
  2554. if (mesh->blend_shape_count > 0 || (mesh->surfaces[p_surface]->format & RS::ARRAY_FORMAT_BONES)) {
  2555. //surface warrants transform
  2556. s.vertex_buffer = RD::get_singleton()->vertex_buffer_create(mesh->surfaces[p_surface]->vertex_buffer_size, Vector<uint8_t>(), true);
  2557. Vector<RD::Uniform> uniforms;
  2558. {
  2559. RD::Uniform u;
  2560. u.binding = 1;
  2561. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  2562. u.ids.push_back(s.vertex_buffer);
  2563. uniforms.push_back(u);
  2564. }
  2565. {
  2566. RD::Uniform u;
  2567. u.binding = 2;
  2568. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  2569. if (mi->blend_weights_buffer.is_valid()) {
  2570. u.ids.push_back(mi->blend_weights_buffer);
  2571. } else {
  2572. u.ids.push_back(default_rd_storage_buffer);
  2573. }
  2574. uniforms.push_back(u);
  2575. }
  2576. s.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, skeleton_shader.version_shader[0], SkeletonShader::UNIFORM_SET_INSTANCE);
  2577. }
  2578. mi->surfaces.push_back(s);
  2579. mi->dirty = true;
  2580. }
  2581. void RendererStorageRD::mesh_instance_check_for_update(RID p_mesh_instance) {
  2582. MeshInstance *mi = mesh_instance_owner.getornull(p_mesh_instance);
  2583. bool needs_update = mi->dirty;
  2584. if (mi->weights_dirty && !mi->weight_update_list.in_list()) {
  2585. dirty_mesh_instance_weights.add(&mi->weight_update_list);
  2586. needs_update = true;
  2587. }
  2588. if (mi->array_update_list.in_list()) {
  2589. return;
  2590. }
  2591. if (!needs_update && mi->skeleton.is_valid()) {
  2592. Skeleton *sk = skeleton_owner.getornull(mi->skeleton);
  2593. if (sk && sk->version != mi->skeleton_version) {
  2594. needs_update = true;
  2595. }
  2596. }
  2597. if (needs_update) {
  2598. dirty_mesh_instance_arrays.add(&mi->array_update_list);
  2599. }
  2600. }
  2601. void RendererStorageRD::update_mesh_instances() {
  2602. while (dirty_mesh_instance_weights.first()) {
  2603. MeshInstance *mi = dirty_mesh_instance_weights.first()->self();
  2604. if (mi->blend_weights_buffer.is_valid()) {
  2605. RD::get_singleton()->buffer_update(mi->blend_weights_buffer, 0, mi->blend_weights.size() * sizeof(float), mi->blend_weights.ptr(), true);
  2606. }
  2607. dirty_mesh_instance_weights.remove(&mi->weight_update_list);
  2608. mi->weights_dirty = false;
  2609. }
  2610. if (dirty_mesh_instance_arrays.first() == nullptr) {
  2611. return; //nothing to do
  2612. }
  2613. //process skeletons and blend shapes
  2614. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  2615. while (dirty_mesh_instance_arrays.first()) {
  2616. MeshInstance *mi = dirty_mesh_instance_arrays.first()->self();
  2617. Skeleton *sk = skeleton_owner.getornull(mi->skeleton);
  2618. for (uint32_t i = 0; i < mi->surfaces.size(); i++) {
  2619. if (mi->surfaces[i].uniform_set == RID() || mi->mesh->surfaces[i]->uniform_set == RID()) {
  2620. continue;
  2621. }
  2622. bool array_is_2d = mi->mesh->surfaces[i]->format & RS::ARRAY_FLAG_USE_2D_VERTICES;
  2623. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, skeleton_shader.pipeline[array_is_2d ? SkeletonShader::SHADER_MODE_2D : SkeletonShader::SHADER_MODE_3D]);
  2624. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, mi->surfaces[i].uniform_set, SkeletonShader::UNIFORM_SET_INSTANCE);
  2625. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, mi->mesh->surfaces[i]->uniform_set, SkeletonShader::UNIFORM_SET_SURFACE);
  2626. if (sk && sk->uniform_set_mi.is_valid()) {
  2627. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sk->uniform_set_mi, SkeletonShader::UNIFORM_SET_SKELETON);
  2628. } else {
  2629. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, skeleton_shader.default_skeleton_uniform_set, SkeletonShader::UNIFORM_SET_SKELETON);
  2630. }
  2631. SkeletonShader::PushConstant push_constant;
  2632. push_constant.has_normal = mi->mesh->surfaces[i]->format & RS::ARRAY_FORMAT_NORMAL;
  2633. push_constant.has_tangent = mi->mesh->surfaces[i]->format & RS::ARRAY_FORMAT_TANGENT;
  2634. push_constant.has_skeleton = sk != nullptr && sk->use_2d == array_is_2d && (mi->mesh->surfaces[i]->format & RS::ARRAY_FORMAT_BONES);
  2635. push_constant.has_blend_shape = mi->mesh->blend_shape_count > 0;
  2636. push_constant.vertex_count = mi->mesh->surfaces[i]->vertex_count;
  2637. push_constant.vertex_stride = (mi->mesh->surfaces[i]->vertex_buffer_size / mi->mesh->surfaces[i]->vertex_count) / 4;
  2638. push_constant.skin_stride = (mi->mesh->surfaces[i]->skin_buffer_size / mi->mesh->surfaces[i]->vertex_count) / 4;
  2639. push_constant.skin_weight_offset = (mi->mesh->surfaces[i]->format & RS::ARRAY_FLAG_USE_8_BONE_WEIGHTS) ? 4 : 2;
  2640. push_constant.blend_shape_count = mi->mesh->blend_shape_count;
  2641. push_constant.normalized_blend_shapes = mi->mesh->blend_shape_mode == RS::BLEND_SHAPE_MODE_NORMALIZED;
  2642. push_constant.pad0 = 0;
  2643. push_constant.pad1 = 0;
  2644. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SkeletonShader::PushConstant));
  2645. //dispatch without barrier, so all is done at the same time
  2646. RD::get_singleton()->compute_list_dispatch_threads(compute_list, push_constant.vertex_count, 1, 1, 64, 1, 1);
  2647. }
  2648. mi->dirty = false;
  2649. if (sk) {
  2650. mi->skeleton_version = sk->version;
  2651. }
  2652. dirty_mesh_instance_arrays.remove(&mi->array_update_list);
  2653. }
  2654. RD::get_singleton()->compute_list_end();
  2655. }
  2656. void RendererStorageRD::_mesh_surface_generate_version_for_input_mask(Mesh::Surface::Version &v, Mesh::Surface *s, uint32_t p_input_mask, MeshInstance::Surface *mis) {
  2657. Vector<RD::VertexAttribute> attributes;
  2658. Vector<RID> buffers;
  2659. uint32_t stride = 0;
  2660. uint32_t attribute_stride = 0;
  2661. uint32_t skin_stride = 0;
  2662. for (int i = 0; i < RS::ARRAY_INDEX; i++) {
  2663. RD::VertexAttribute vd;
  2664. RID buffer;
  2665. vd.location = i;
  2666. if (!(s->format & (1 << i))) {
  2667. // Not supplied by surface, use default value
  2668. buffer = mesh_default_rd_buffers[i];
  2669. vd.stride = 0;
  2670. switch (i) {
  2671. case RS::ARRAY_VERTEX: {
  2672. vd.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  2673. } break;
  2674. case RS::ARRAY_NORMAL: {
  2675. vd.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  2676. } break;
  2677. case RS::ARRAY_TANGENT: {
  2678. vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  2679. } break;
  2680. case RS::ARRAY_COLOR: {
  2681. vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  2682. } break;
  2683. case RS::ARRAY_TEX_UV: {
  2684. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2685. } break;
  2686. case RS::ARRAY_TEX_UV2: {
  2687. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2688. } break;
  2689. case RS::ARRAY_CUSTOM0:
  2690. case RS::ARRAY_CUSTOM1:
  2691. case RS::ARRAY_CUSTOM2:
  2692. case RS::ARRAY_CUSTOM3: {
  2693. //assumed weights too
  2694. vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  2695. } break;
  2696. case RS::ARRAY_BONES: {
  2697. //assumed weights too
  2698. vd.format = RD::DATA_FORMAT_R32G32B32A32_UINT;
  2699. } break;
  2700. case RS::ARRAY_WEIGHTS: {
  2701. //assumed weights too
  2702. vd.format = RD::DATA_FORMAT_R32G32B32A32_UINT;
  2703. } break;
  2704. }
  2705. } else {
  2706. //Supplied, use it
  2707. vd.stride = 1; //mark that it needs a stride set (default uses 0)
  2708. switch (i) {
  2709. case RS::ARRAY_VERTEX: {
  2710. vd.offset = stride;
  2711. if (s->format & RS::ARRAY_FLAG_USE_2D_VERTICES) {
  2712. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2713. stride += sizeof(float) * 2;
  2714. } else {
  2715. vd.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  2716. stride += sizeof(float) * 3;
  2717. }
  2718. if (mis) {
  2719. buffer = mis->vertex_buffer;
  2720. } else {
  2721. buffer = s->vertex_buffer;
  2722. }
  2723. } break;
  2724. case RS::ARRAY_NORMAL: {
  2725. vd.offset = stride;
  2726. vd.format = RD::DATA_FORMAT_A2B10G10R10_UNORM_PACK32;
  2727. stride += sizeof(uint32_t);
  2728. if (mis) {
  2729. buffer = mis->vertex_buffer;
  2730. } else {
  2731. buffer = s->vertex_buffer;
  2732. }
  2733. } break;
  2734. case RS::ARRAY_TANGENT: {
  2735. vd.offset = stride;
  2736. vd.format = RD::DATA_FORMAT_A2B10G10R10_UNORM_PACK32;
  2737. stride += sizeof(uint32_t);
  2738. if (mis) {
  2739. buffer = mis->vertex_buffer;
  2740. } else {
  2741. buffer = s->vertex_buffer;
  2742. }
  2743. } break;
  2744. case RS::ARRAY_COLOR: {
  2745. vd.offset = attribute_stride;
  2746. vd.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  2747. attribute_stride += sizeof(int16_t) * 4;
  2748. buffer = s->attribute_buffer;
  2749. } break;
  2750. case RS::ARRAY_TEX_UV: {
  2751. vd.offset = attribute_stride;
  2752. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2753. attribute_stride += sizeof(float) * 2;
  2754. buffer = s->attribute_buffer;
  2755. } break;
  2756. case RS::ARRAY_TEX_UV2: {
  2757. vd.offset = attribute_stride;
  2758. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2759. attribute_stride += sizeof(float) * 2;
  2760. buffer = s->attribute_buffer;
  2761. } break;
  2762. case RS::ARRAY_CUSTOM0:
  2763. case RS::ARRAY_CUSTOM1:
  2764. case RS::ARRAY_CUSTOM2:
  2765. case RS::ARRAY_CUSTOM3: {
  2766. vd.offset = attribute_stride;
  2767. int idx = i - RS::ARRAY_CUSTOM0;
  2768. 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 };
  2769. uint32_t fmt = (s->format >> fmt_shift[idx]) & RS::ARRAY_FORMAT_CUSTOM_MASK;
  2770. uint32_t fmtsize[RS::ARRAY_CUSTOM_MAX] = { 4, 4, 4, 8, 4, 8, 12, 16 };
  2771. 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 };
  2772. vd.format = fmtrd[fmt];
  2773. attribute_stride += fmtsize[fmt];
  2774. buffer = s->attribute_buffer;
  2775. } break;
  2776. case RS::ARRAY_BONES: {
  2777. vd.offset = skin_stride;
  2778. vd.format = RD::DATA_FORMAT_R16G16B16A16_UINT;
  2779. skin_stride += sizeof(int16_t) * 4;
  2780. buffer = s->skin_buffer;
  2781. } break;
  2782. case RS::ARRAY_WEIGHTS: {
  2783. vd.offset = skin_stride;
  2784. vd.format = RD::DATA_FORMAT_R16G16B16A16_UNORM;
  2785. skin_stride += sizeof(int16_t) * 4;
  2786. buffer = s->skin_buffer;
  2787. } break;
  2788. }
  2789. }
  2790. if (!(p_input_mask & (1 << i))) {
  2791. continue; // Shader does not need this, skip it (but computing stride was important anyway)
  2792. }
  2793. attributes.push_back(vd);
  2794. buffers.push_back(buffer);
  2795. }
  2796. //update final stride
  2797. for (int i = 0; i < attributes.size(); i++) {
  2798. if (attributes[i].stride == 0) {
  2799. continue; //default location
  2800. }
  2801. int loc = attributes[i].location;
  2802. if (loc < RS::ARRAY_COLOR) {
  2803. attributes.write[i].stride = stride;
  2804. } else if (loc < RS::ARRAY_BONES) {
  2805. attributes.write[i].stride = attribute_stride;
  2806. } else {
  2807. attributes.write[i].stride = skin_stride;
  2808. }
  2809. }
  2810. v.input_mask = p_input_mask;
  2811. v.vertex_format = RD::get_singleton()->vertex_format_create(attributes);
  2812. v.vertex_array = RD::get_singleton()->vertex_array_create(s->vertex_count, v.vertex_format, buffers);
  2813. }
  2814. ////////////////// MULTIMESH
  2815. RID RendererStorageRD::multimesh_create() {
  2816. return multimesh_owner.make_rid(MultiMesh());
  2817. }
  2818. void RendererStorageRD::multimesh_allocate(RID p_multimesh, int p_instances, RS::MultimeshTransformFormat p_transform_format, bool p_use_colors, bool p_use_custom_data) {
  2819. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2820. ERR_FAIL_COND(!multimesh);
  2821. 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) {
  2822. return;
  2823. }
  2824. if (multimesh->buffer.is_valid()) {
  2825. RD::get_singleton()->free(multimesh->buffer);
  2826. multimesh->buffer = RID();
  2827. multimesh->uniform_set_3d = RID(); //cleared by dependency
  2828. }
  2829. if (multimesh->data_cache_dirty_regions) {
  2830. memdelete_arr(multimesh->data_cache_dirty_regions);
  2831. multimesh->data_cache_dirty_regions = nullptr;
  2832. multimesh->data_cache_used_dirty_regions = 0;
  2833. }
  2834. multimesh->instances = p_instances;
  2835. multimesh->xform_format = p_transform_format;
  2836. multimesh->uses_colors = p_use_colors;
  2837. multimesh->color_offset_cache = p_transform_format == RS::MULTIMESH_TRANSFORM_2D ? 8 : 12;
  2838. multimesh->uses_custom_data = p_use_custom_data;
  2839. multimesh->custom_data_offset_cache = multimesh->color_offset_cache + (p_use_colors ? 4 : 0);
  2840. multimesh->stride_cache = multimesh->custom_data_offset_cache + (p_use_custom_data ? 4 : 0);
  2841. multimesh->buffer_set = false;
  2842. //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));
  2843. multimesh->data_cache = Vector<float>();
  2844. multimesh->aabb = AABB();
  2845. multimesh->aabb_dirty = false;
  2846. multimesh->visible_instances = MIN(multimesh->visible_instances, multimesh->instances);
  2847. if (multimesh->instances) {
  2848. multimesh->buffer = RD::get_singleton()->storage_buffer_create(multimesh->instances * multimesh->stride_cache * 4);
  2849. }
  2850. multimesh->dependency.changed_notify(DEPENDENCY_CHANGED_MULTIMESH);
  2851. }
  2852. int RendererStorageRD::multimesh_get_instance_count(RID p_multimesh) const {
  2853. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2854. ERR_FAIL_COND_V(!multimesh, 0);
  2855. return multimesh->instances;
  2856. }
  2857. void RendererStorageRD::multimesh_set_mesh(RID p_multimesh, RID p_mesh) {
  2858. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2859. ERR_FAIL_COND(!multimesh);
  2860. if (multimesh->mesh == p_mesh) {
  2861. return;
  2862. }
  2863. multimesh->mesh = p_mesh;
  2864. if (multimesh->instances == 0) {
  2865. return;
  2866. }
  2867. if (multimesh->data_cache.size()) {
  2868. //we have a data cache, just mark it dirt
  2869. _multimesh_mark_all_dirty(multimesh, false, true);
  2870. } else if (multimesh->instances) {
  2871. //need to re-create AABB unfortunately, calling this has a penalty
  2872. if (multimesh->buffer_set) {
  2873. Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(multimesh->buffer);
  2874. const uint8_t *r = buffer.ptr();
  2875. const float *data = (const float *)r;
  2876. _multimesh_re_create_aabb(multimesh, data, multimesh->instances);
  2877. }
  2878. }
  2879. multimesh->dependency.changed_notify(DEPENDENCY_CHANGED_MESH);
  2880. }
  2881. #define MULTIMESH_DIRTY_REGION_SIZE 512
  2882. void RendererStorageRD::_multimesh_make_local(MultiMesh *multimesh) const {
  2883. if (multimesh->data_cache.size() > 0) {
  2884. return; //already local
  2885. }
  2886. ERR_FAIL_COND(multimesh->data_cache.size() > 0);
  2887. // this means that the user wants to load/save individual elements,
  2888. // for this, the data must reside on CPU, so just copy it there.
  2889. multimesh->data_cache.resize(multimesh->instances * multimesh->stride_cache);
  2890. {
  2891. float *w = multimesh->data_cache.ptrw();
  2892. if (multimesh->buffer_set) {
  2893. Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(multimesh->buffer);
  2894. {
  2895. const uint8_t *r = buffer.ptr();
  2896. copymem(w, r, buffer.size());
  2897. }
  2898. } else {
  2899. zeromem(w, multimesh->instances * multimesh->stride_cache * sizeof(float));
  2900. }
  2901. }
  2902. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2903. multimesh->data_cache_dirty_regions = memnew_arr(bool, data_cache_dirty_region_count);
  2904. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  2905. multimesh->data_cache_dirty_regions[i] = false;
  2906. }
  2907. multimesh->data_cache_used_dirty_regions = 0;
  2908. }
  2909. void RendererStorageRD::_multimesh_mark_dirty(MultiMesh *multimesh, int p_index, bool p_aabb) {
  2910. uint32_t region_index = p_index / MULTIMESH_DIRTY_REGION_SIZE;
  2911. #ifdef DEBUG_ENABLED
  2912. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2913. ERR_FAIL_UNSIGNED_INDEX(region_index, data_cache_dirty_region_count); //bug
  2914. #endif
  2915. if (!multimesh->data_cache_dirty_regions[region_index]) {
  2916. multimesh->data_cache_dirty_regions[region_index] = true;
  2917. multimesh->data_cache_used_dirty_regions++;
  2918. }
  2919. if (p_aabb) {
  2920. multimesh->aabb_dirty = true;
  2921. }
  2922. if (!multimesh->dirty) {
  2923. multimesh->dirty_list = multimesh_dirty_list;
  2924. multimesh_dirty_list = multimesh;
  2925. multimesh->dirty = true;
  2926. }
  2927. }
  2928. void RendererStorageRD::_multimesh_mark_all_dirty(MultiMesh *multimesh, bool p_data, bool p_aabb) {
  2929. if (p_data) {
  2930. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2931. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  2932. if (!multimesh->data_cache_dirty_regions[i]) {
  2933. multimesh->data_cache_dirty_regions[i] = true;
  2934. multimesh->data_cache_used_dirty_regions++;
  2935. }
  2936. }
  2937. }
  2938. if (p_aabb) {
  2939. multimesh->aabb_dirty = true;
  2940. }
  2941. if (!multimesh->dirty) {
  2942. multimesh->dirty_list = multimesh_dirty_list;
  2943. multimesh_dirty_list = multimesh;
  2944. multimesh->dirty = true;
  2945. }
  2946. }
  2947. void RendererStorageRD::_multimesh_re_create_aabb(MultiMesh *multimesh, const float *p_data, int p_instances) {
  2948. ERR_FAIL_COND(multimesh->mesh.is_null());
  2949. AABB aabb;
  2950. AABB mesh_aabb = mesh_get_aabb(multimesh->mesh);
  2951. for (int i = 0; i < p_instances; i++) {
  2952. const float *data = p_data + multimesh->stride_cache * i;
  2953. Transform t;
  2954. if (multimesh->xform_format == RS::MULTIMESH_TRANSFORM_3D) {
  2955. t.basis.elements[0][0] = data[0];
  2956. t.basis.elements[0][1] = data[1];
  2957. t.basis.elements[0][2] = data[2];
  2958. t.origin.x = data[3];
  2959. t.basis.elements[1][0] = data[4];
  2960. t.basis.elements[1][1] = data[5];
  2961. t.basis.elements[1][2] = data[6];
  2962. t.origin.y = data[7];
  2963. t.basis.elements[2][0] = data[8];
  2964. t.basis.elements[2][1] = data[9];
  2965. t.basis.elements[2][2] = data[10];
  2966. t.origin.z = data[11];
  2967. } else {
  2968. t.basis.elements[0].x = data[0];
  2969. t.basis.elements[1].x = data[1];
  2970. t.origin.x = data[3];
  2971. t.basis.elements[0].y = data[4];
  2972. t.basis.elements[1].y = data[5];
  2973. t.origin.y = data[7];
  2974. }
  2975. if (i == 0) {
  2976. aabb = t.xform(mesh_aabb);
  2977. } else {
  2978. aabb.merge_with(t.xform(mesh_aabb));
  2979. }
  2980. }
  2981. multimesh->aabb = aabb;
  2982. }
  2983. void RendererStorageRD::multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform) {
  2984. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2985. ERR_FAIL_COND(!multimesh);
  2986. ERR_FAIL_INDEX(p_index, multimesh->instances);
  2987. ERR_FAIL_COND(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_3D);
  2988. _multimesh_make_local(multimesh);
  2989. {
  2990. float *w = multimesh->data_cache.ptrw();
  2991. float *dataptr = w + p_index * multimesh->stride_cache;
  2992. dataptr[0] = p_transform.basis.elements[0][0];
  2993. dataptr[1] = p_transform.basis.elements[0][1];
  2994. dataptr[2] = p_transform.basis.elements[0][2];
  2995. dataptr[3] = p_transform.origin.x;
  2996. dataptr[4] = p_transform.basis.elements[1][0];
  2997. dataptr[5] = p_transform.basis.elements[1][1];
  2998. dataptr[6] = p_transform.basis.elements[1][2];
  2999. dataptr[7] = p_transform.origin.y;
  3000. dataptr[8] = p_transform.basis.elements[2][0];
  3001. dataptr[9] = p_transform.basis.elements[2][1];
  3002. dataptr[10] = p_transform.basis.elements[2][2];
  3003. dataptr[11] = p_transform.origin.z;
  3004. }
  3005. _multimesh_mark_dirty(multimesh, p_index, true);
  3006. }
  3007. void RendererStorageRD::multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) {
  3008. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3009. ERR_FAIL_COND(!multimesh);
  3010. ERR_FAIL_INDEX(p_index, multimesh->instances);
  3011. ERR_FAIL_COND(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_2D);
  3012. _multimesh_make_local(multimesh);
  3013. {
  3014. float *w = multimesh->data_cache.ptrw();
  3015. float *dataptr = w + p_index * multimesh->stride_cache;
  3016. dataptr[0] = p_transform.elements[0][0];
  3017. dataptr[1] = p_transform.elements[1][0];
  3018. dataptr[2] = 0;
  3019. dataptr[3] = p_transform.elements[2][0];
  3020. dataptr[4] = p_transform.elements[0][1];
  3021. dataptr[5] = p_transform.elements[1][1];
  3022. dataptr[6] = 0;
  3023. dataptr[7] = p_transform.elements[2][1];
  3024. }
  3025. _multimesh_mark_dirty(multimesh, p_index, true);
  3026. }
  3027. void RendererStorageRD::multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) {
  3028. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3029. ERR_FAIL_COND(!multimesh);
  3030. ERR_FAIL_INDEX(p_index, multimesh->instances);
  3031. ERR_FAIL_COND(!multimesh->uses_colors);
  3032. _multimesh_make_local(multimesh);
  3033. {
  3034. float *w = multimesh->data_cache.ptrw();
  3035. float *dataptr = w + p_index * multimesh->stride_cache + multimesh->color_offset_cache;
  3036. dataptr[0] = p_color.r;
  3037. dataptr[1] = p_color.g;
  3038. dataptr[2] = p_color.b;
  3039. dataptr[3] = p_color.a;
  3040. }
  3041. _multimesh_mark_dirty(multimesh, p_index, false);
  3042. }
  3043. void RendererStorageRD::multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) {
  3044. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3045. ERR_FAIL_COND(!multimesh);
  3046. ERR_FAIL_INDEX(p_index, multimesh->instances);
  3047. ERR_FAIL_COND(!multimesh->uses_custom_data);
  3048. _multimesh_make_local(multimesh);
  3049. {
  3050. float *w = multimesh->data_cache.ptrw();
  3051. float *dataptr = w + p_index * multimesh->stride_cache + multimesh->custom_data_offset_cache;
  3052. dataptr[0] = p_color.r;
  3053. dataptr[1] = p_color.g;
  3054. dataptr[2] = p_color.b;
  3055. dataptr[3] = p_color.a;
  3056. }
  3057. _multimesh_mark_dirty(multimesh, p_index, false);
  3058. }
  3059. RID RendererStorageRD::multimesh_get_mesh(RID p_multimesh) const {
  3060. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3061. ERR_FAIL_COND_V(!multimesh, RID());
  3062. return multimesh->mesh;
  3063. }
  3064. Transform RendererStorageRD::multimesh_instance_get_transform(RID p_multimesh, int p_index) const {
  3065. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3066. ERR_FAIL_COND_V(!multimesh, Transform());
  3067. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Transform());
  3068. ERR_FAIL_COND_V(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_3D, Transform());
  3069. _multimesh_make_local(multimesh);
  3070. Transform t;
  3071. {
  3072. const float *r = multimesh->data_cache.ptr();
  3073. const float *dataptr = r + p_index * multimesh->stride_cache;
  3074. t.basis.elements[0][0] = dataptr[0];
  3075. t.basis.elements[0][1] = dataptr[1];
  3076. t.basis.elements[0][2] = dataptr[2];
  3077. t.origin.x = dataptr[3];
  3078. t.basis.elements[1][0] = dataptr[4];
  3079. t.basis.elements[1][1] = dataptr[5];
  3080. t.basis.elements[1][2] = dataptr[6];
  3081. t.origin.y = dataptr[7];
  3082. t.basis.elements[2][0] = dataptr[8];
  3083. t.basis.elements[2][1] = dataptr[9];
  3084. t.basis.elements[2][2] = dataptr[10];
  3085. t.origin.z = dataptr[11];
  3086. }
  3087. return t;
  3088. }
  3089. Transform2D RendererStorageRD::multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const {
  3090. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3091. ERR_FAIL_COND_V(!multimesh, Transform2D());
  3092. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Transform2D());
  3093. ERR_FAIL_COND_V(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_2D, Transform2D());
  3094. _multimesh_make_local(multimesh);
  3095. Transform2D t;
  3096. {
  3097. const float *r = multimesh->data_cache.ptr();
  3098. const float *dataptr = r + p_index * multimesh->stride_cache;
  3099. t.elements[0][0] = dataptr[0];
  3100. t.elements[1][0] = dataptr[1];
  3101. t.elements[2][0] = dataptr[3];
  3102. t.elements[0][1] = dataptr[4];
  3103. t.elements[1][1] = dataptr[5];
  3104. t.elements[2][1] = dataptr[7];
  3105. }
  3106. return t;
  3107. }
  3108. Color RendererStorageRD::multimesh_instance_get_color(RID p_multimesh, int p_index) const {
  3109. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3110. ERR_FAIL_COND_V(!multimesh, Color());
  3111. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Color());
  3112. ERR_FAIL_COND_V(!multimesh->uses_colors, Color());
  3113. _multimesh_make_local(multimesh);
  3114. Color c;
  3115. {
  3116. const float *r = multimesh->data_cache.ptr();
  3117. const float *dataptr = r + p_index * multimesh->stride_cache + multimesh->color_offset_cache;
  3118. c.r = dataptr[0];
  3119. c.g = dataptr[1];
  3120. c.b = dataptr[2];
  3121. c.a = dataptr[3];
  3122. }
  3123. return c;
  3124. }
  3125. Color RendererStorageRD::multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const {
  3126. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3127. ERR_FAIL_COND_V(!multimesh, Color());
  3128. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Color());
  3129. ERR_FAIL_COND_V(!multimesh->uses_custom_data, Color());
  3130. _multimesh_make_local(multimesh);
  3131. Color c;
  3132. {
  3133. const float *r = multimesh->data_cache.ptr();
  3134. const float *dataptr = r + p_index * multimesh->stride_cache + multimesh->custom_data_offset_cache;
  3135. c.r = dataptr[0];
  3136. c.g = dataptr[1];
  3137. c.b = dataptr[2];
  3138. c.a = dataptr[3];
  3139. }
  3140. return c;
  3141. }
  3142. void RendererStorageRD::multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer) {
  3143. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3144. ERR_FAIL_COND(!multimesh);
  3145. ERR_FAIL_COND(p_buffer.size() != (multimesh->instances * (int)multimesh->stride_cache));
  3146. {
  3147. const float *r = p_buffer.ptr();
  3148. RD::get_singleton()->buffer_update(multimesh->buffer, 0, p_buffer.size() * sizeof(float), r, false);
  3149. multimesh->buffer_set = true;
  3150. }
  3151. if (multimesh->data_cache.size()) {
  3152. //if we have a data cache, just update it
  3153. multimesh->data_cache = p_buffer;
  3154. {
  3155. //clear dirty since nothing will be dirty anymore
  3156. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  3157. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  3158. multimesh->data_cache_dirty_regions[i] = false;
  3159. }
  3160. multimesh->data_cache_used_dirty_regions = 0;
  3161. }
  3162. _multimesh_mark_all_dirty(multimesh, false, true); //update AABB
  3163. } else if (multimesh->mesh.is_valid()) {
  3164. //if we have a mesh set, we need to re-generate the AABB from the new data
  3165. const float *data = p_buffer.ptr();
  3166. _multimesh_re_create_aabb(multimesh, data, multimesh->instances);
  3167. multimesh->dependency.changed_notify(DEPENDENCY_CHANGED_AABB);
  3168. }
  3169. }
  3170. Vector<float> RendererStorageRD::multimesh_get_buffer(RID p_multimesh) const {
  3171. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3172. ERR_FAIL_COND_V(!multimesh, Vector<float>());
  3173. if (multimesh->buffer.is_null()) {
  3174. return Vector<float>();
  3175. } else if (multimesh->data_cache.size()) {
  3176. return multimesh->data_cache;
  3177. } else {
  3178. //get from memory
  3179. Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(multimesh->buffer);
  3180. Vector<float> ret;
  3181. ret.resize(multimesh->instances * multimesh->stride_cache);
  3182. {
  3183. float *w = ret.ptrw();
  3184. const uint8_t *r = buffer.ptr();
  3185. copymem(w, r, buffer.size());
  3186. }
  3187. return ret;
  3188. }
  3189. }
  3190. void RendererStorageRD::multimesh_set_visible_instances(RID p_multimesh, int p_visible) {
  3191. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3192. ERR_FAIL_COND(!multimesh);
  3193. ERR_FAIL_COND(p_visible < -1 || p_visible > multimesh->instances);
  3194. if (multimesh->visible_instances == p_visible) {
  3195. return;
  3196. }
  3197. if (multimesh->data_cache.size()) {
  3198. //there is a data cache..
  3199. _multimesh_mark_all_dirty(multimesh, false, true);
  3200. }
  3201. multimesh->visible_instances = p_visible;
  3202. multimesh->dependency.changed_notify(DEPENDENCY_CHANGED_MULTIMESH_VISIBLE_INSTANCES);
  3203. }
  3204. int RendererStorageRD::multimesh_get_visible_instances(RID p_multimesh) const {
  3205. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3206. ERR_FAIL_COND_V(!multimesh, 0);
  3207. return multimesh->visible_instances;
  3208. }
  3209. AABB RendererStorageRD::multimesh_get_aabb(RID p_multimesh) const {
  3210. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  3211. ERR_FAIL_COND_V(!multimesh, AABB());
  3212. if (multimesh->aabb_dirty) {
  3213. const_cast<RendererStorageRD *>(this)->_update_dirty_multimeshes();
  3214. }
  3215. return multimesh->aabb;
  3216. }
  3217. void RendererStorageRD::_update_dirty_multimeshes() {
  3218. while (multimesh_dirty_list) {
  3219. MultiMesh *multimesh = multimesh_dirty_list;
  3220. if (multimesh->data_cache.size()) { //may have been cleared, so only process if it exists
  3221. const float *data = multimesh->data_cache.ptr();
  3222. uint32_t visible_instances = multimesh->visible_instances >= 0 ? multimesh->visible_instances : multimesh->instances;
  3223. if (multimesh->data_cache_used_dirty_regions) {
  3224. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  3225. uint32_t visible_region_count = (visible_instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  3226. uint32_t region_size = multimesh->stride_cache * MULTIMESH_DIRTY_REGION_SIZE * sizeof(float);
  3227. if (multimesh->data_cache_used_dirty_regions > 32 || multimesh->data_cache_used_dirty_regions > visible_region_count / 2) {
  3228. //if there too many dirty regions, or represent the majority of regions, just copy all, else transfer cost piles up too much
  3229. RD::get_singleton()->buffer_update(multimesh->buffer, 0, MIN(visible_region_count * region_size, multimesh->instances * multimesh->stride_cache * sizeof(float)), data, false);
  3230. } else {
  3231. //not that many regions? update them all
  3232. for (uint32_t i = 0; i < visible_region_count; i++) {
  3233. if (multimesh->data_cache_dirty_regions[i]) {
  3234. uint64_t offset = i * region_size;
  3235. uint64_t size = multimesh->stride_cache * multimesh->instances * sizeof(float);
  3236. RD::get_singleton()->buffer_update(multimesh->buffer, offset, MIN(region_size, size - offset), &data[i * region_size], false);
  3237. }
  3238. }
  3239. }
  3240. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  3241. multimesh->data_cache_dirty_regions[i] = false;
  3242. }
  3243. multimesh->data_cache_used_dirty_regions = 0;
  3244. }
  3245. if (multimesh->aabb_dirty) {
  3246. //aabb is dirty..
  3247. _multimesh_re_create_aabb(multimesh, data, visible_instances);
  3248. multimesh->aabb_dirty = false;
  3249. multimesh->dependency.changed_notify(DEPENDENCY_CHANGED_AABB);
  3250. }
  3251. }
  3252. multimesh_dirty_list = multimesh->dirty_list;
  3253. multimesh->dirty_list = nullptr;
  3254. multimesh->dirty = false;
  3255. }
  3256. multimesh_dirty_list = nullptr;
  3257. }
  3258. /* PARTICLES */
  3259. RID RendererStorageRD::particles_create() {
  3260. return particles_owner.make_rid(Particles());
  3261. }
  3262. void RendererStorageRD::particles_set_emitting(RID p_particles, bool p_emitting) {
  3263. Particles *particles = particles_owner.getornull(p_particles);
  3264. ERR_FAIL_COND(!particles);
  3265. particles->emitting = p_emitting;
  3266. }
  3267. bool RendererStorageRD::particles_get_emitting(RID p_particles) {
  3268. Particles *particles = particles_owner.getornull(p_particles);
  3269. ERR_FAIL_COND_V(!particles, false);
  3270. return particles->emitting;
  3271. }
  3272. void RendererStorageRD::_particles_free_data(Particles *particles) {
  3273. if (!particles->particle_buffer.is_valid()) {
  3274. return;
  3275. }
  3276. RD::get_singleton()->free(particles->particle_buffer);
  3277. RD::get_singleton()->free(particles->frame_params_buffer);
  3278. RD::get_singleton()->free(particles->particle_instance_buffer);
  3279. particles->particles_transforms_buffer_uniform_set = RID();
  3280. particles->particle_buffer = RID();
  3281. if (RD::get_singleton()->uniform_set_is_valid(particles->collision_textures_uniform_set)) {
  3282. RD::get_singleton()->free(particles->collision_textures_uniform_set);
  3283. }
  3284. if (particles->particles_sort_buffer.is_valid()) {
  3285. RD::get_singleton()->free(particles->particles_sort_buffer);
  3286. particles->particles_sort_buffer = RID();
  3287. }
  3288. if (particles->emission_buffer != nullptr) {
  3289. particles->emission_buffer = nullptr;
  3290. particles->emission_buffer_data.clear();
  3291. RD::get_singleton()->free(particles->emission_storage_buffer);
  3292. particles->emission_storage_buffer = RID();
  3293. }
  3294. }
  3295. void RendererStorageRD::particles_set_amount(RID p_particles, int p_amount) {
  3296. Particles *particles = particles_owner.getornull(p_particles);
  3297. ERR_FAIL_COND(!particles);
  3298. if (particles->amount == p_amount) {
  3299. return;
  3300. }
  3301. _particles_free_data(particles);
  3302. particles->amount = p_amount;
  3303. if (particles->amount > 0) {
  3304. particles->particle_buffer = RD::get_singleton()->storage_buffer_create(sizeof(ParticleData) * p_amount);
  3305. particles->frame_params_buffer = RD::get_singleton()->storage_buffer_create(sizeof(ParticlesFrameParams) * 1);
  3306. particles->particle_instance_buffer = RD::get_singleton()->storage_buffer_create(sizeof(float) * 4 * (3 + 1 + 1) * p_amount);
  3307. //needs to clear it
  3308. {
  3309. Vector<RD::Uniform> uniforms;
  3310. {
  3311. RD::Uniform u;
  3312. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3313. u.binding = 1;
  3314. u.ids.push_back(particles->particle_buffer);
  3315. uniforms.push_back(u);
  3316. }
  3317. {
  3318. RD::Uniform u;
  3319. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3320. u.binding = 2;
  3321. u.ids.push_back(particles->particle_instance_buffer);
  3322. uniforms.push_back(u);
  3323. }
  3324. 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);
  3325. }
  3326. }
  3327. particles->prev_ticks = 0;
  3328. particles->phase = 0;
  3329. particles->prev_phase = 0;
  3330. particles->clear = true;
  3331. }
  3332. void RendererStorageRD::particles_set_lifetime(RID p_particles, float p_lifetime) {
  3333. Particles *particles = particles_owner.getornull(p_particles);
  3334. ERR_FAIL_COND(!particles);
  3335. particles->lifetime = p_lifetime;
  3336. }
  3337. void RendererStorageRD::particles_set_one_shot(RID p_particles, bool p_one_shot) {
  3338. Particles *particles = particles_owner.getornull(p_particles);
  3339. ERR_FAIL_COND(!particles);
  3340. particles->one_shot = p_one_shot;
  3341. }
  3342. void RendererStorageRD::particles_set_pre_process_time(RID p_particles, float p_time) {
  3343. Particles *particles = particles_owner.getornull(p_particles);
  3344. ERR_FAIL_COND(!particles);
  3345. particles->pre_process_time = p_time;
  3346. }
  3347. void RendererStorageRD::particles_set_explosiveness_ratio(RID p_particles, float p_ratio) {
  3348. Particles *particles = particles_owner.getornull(p_particles);
  3349. ERR_FAIL_COND(!particles);
  3350. particles->explosiveness = p_ratio;
  3351. }
  3352. void RendererStorageRD::particles_set_randomness_ratio(RID p_particles, float p_ratio) {
  3353. Particles *particles = particles_owner.getornull(p_particles);
  3354. ERR_FAIL_COND(!particles);
  3355. particles->randomness = p_ratio;
  3356. }
  3357. void RendererStorageRD::particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) {
  3358. Particles *particles = particles_owner.getornull(p_particles);
  3359. ERR_FAIL_COND(!particles);
  3360. particles->custom_aabb = p_aabb;
  3361. particles->dependency.changed_notify(DEPENDENCY_CHANGED_AABB);
  3362. }
  3363. void RendererStorageRD::particles_set_speed_scale(RID p_particles, float p_scale) {
  3364. Particles *particles = particles_owner.getornull(p_particles);
  3365. ERR_FAIL_COND(!particles);
  3366. particles->speed_scale = p_scale;
  3367. }
  3368. void RendererStorageRD::particles_set_use_local_coordinates(RID p_particles, bool p_enable) {
  3369. Particles *particles = particles_owner.getornull(p_particles);
  3370. ERR_FAIL_COND(!particles);
  3371. particles->use_local_coords = p_enable;
  3372. }
  3373. void RendererStorageRD::particles_set_fixed_fps(RID p_particles, int p_fps) {
  3374. Particles *particles = particles_owner.getornull(p_particles);
  3375. ERR_FAIL_COND(!particles);
  3376. particles->fixed_fps = p_fps;
  3377. }
  3378. void RendererStorageRD::particles_set_fractional_delta(RID p_particles, bool p_enable) {
  3379. Particles *particles = particles_owner.getornull(p_particles);
  3380. ERR_FAIL_COND(!particles);
  3381. particles->fractional_delta = p_enable;
  3382. }
  3383. void RendererStorageRD::particles_set_collision_base_size(RID p_particles, float p_size) {
  3384. Particles *particles = particles_owner.getornull(p_particles);
  3385. ERR_FAIL_COND(!particles);
  3386. particles->collision_base_size = p_size;
  3387. }
  3388. void RendererStorageRD::particles_set_process_material(RID p_particles, RID p_material) {
  3389. Particles *particles = particles_owner.getornull(p_particles);
  3390. ERR_FAIL_COND(!particles);
  3391. particles->process_material = p_material;
  3392. }
  3393. void RendererStorageRD::particles_set_draw_order(RID p_particles, RS::ParticlesDrawOrder p_order) {
  3394. Particles *particles = particles_owner.getornull(p_particles);
  3395. ERR_FAIL_COND(!particles);
  3396. particles->draw_order = p_order;
  3397. }
  3398. void RendererStorageRD::particles_set_draw_passes(RID p_particles, int p_passes) {
  3399. Particles *particles = particles_owner.getornull(p_particles);
  3400. ERR_FAIL_COND(!particles);
  3401. particles->draw_passes.resize(p_passes);
  3402. }
  3403. void RendererStorageRD::particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) {
  3404. Particles *particles = particles_owner.getornull(p_particles);
  3405. ERR_FAIL_COND(!particles);
  3406. ERR_FAIL_INDEX(p_pass, particles->draw_passes.size());
  3407. particles->draw_passes.write[p_pass] = p_mesh;
  3408. }
  3409. void RendererStorageRD::particles_restart(RID p_particles) {
  3410. Particles *particles = particles_owner.getornull(p_particles);
  3411. ERR_FAIL_COND(!particles);
  3412. particles->restart_request = true;
  3413. }
  3414. void RendererStorageRD::_particles_allocate_emission_buffer(Particles *particles) {
  3415. ERR_FAIL_COND(particles->emission_buffer != nullptr);
  3416. particles->emission_buffer_data.resize(sizeof(ParticleEmissionBuffer::Data) * particles->amount + sizeof(uint32_t) * 4);
  3417. zeromem(particles->emission_buffer_data.ptrw(), particles->emission_buffer_data.size());
  3418. particles->emission_buffer = (ParticleEmissionBuffer *)particles->emission_buffer_data.ptrw();
  3419. particles->emission_buffer->particle_max = particles->amount;
  3420. particles->emission_storage_buffer = RD::get_singleton()->storage_buffer_create(particles->emission_buffer_data.size(), particles->emission_buffer_data);
  3421. if (RD::get_singleton()->uniform_set_is_valid(particles->particles_material_uniform_set)) {
  3422. //will need to be re-created
  3423. RD::get_singleton()->free(particles->particles_material_uniform_set);
  3424. particles->particles_material_uniform_set = RID();
  3425. }
  3426. }
  3427. void RendererStorageRD::particles_set_subemitter(RID p_particles, RID p_subemitter_particles) {
  3428. Particles *particles = particles_owner.getornull(p_particles);
  3429. ERR_FAIL_COND(!particles);
  3430. ERR_FAIL_COND(p_particles == p_subemitter_particles);
  3431. particles->sub_emitter = p_subemitter_particles;
  3432. if (RD::get_singleton()->uniform_set_is_valid(particles->particles_material_uniform_set)) {
  3433. RD::get_singleton()->free(particles->particles_material_uniform_set);
  3434. particles->particles_material_uniform_set = RID(); //clear and force to re create sub emitting
  3435. }
  3436. }
  3437. 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) {
  3438. Particles *particles = particles_owner.getornull(p_particles);
  3439. ERR_FAIL_COND(!particles);
  3440. ERR_FAIL_COND(particles->amount == 0);
  3441. if (particles->emitting) {
  3442. particles->clear = true;
  3443. particles->emitting = false;
  3444. }
  3445. if (particles->emission_buffer == nullptr) {
  3446. _particles_allocate_emission_buffer(particles);
  3447. }
  3448. if (particles->inactive) {
  3449. //in case it was inactive, make active again
  3450. particles->inactive = false;
  3451. particles->inactive_time = 0;
  3452. }
  3453. int32_t idx = particles->emission_buffer->particle_count;
  3454. if (idx < particles->emission_buffer->particle_max) {
  3455. store_transform(p_transform, particles->emission_buffer->data[idx].xform);
  3456. particles->emission_buffer->data[idx].velocity[0] = p_velocity.x;
  3457. particles->emission_buffer->data[idx].velocity[1] = p_velocity.y;
  3458. particles->emission_buffer->data[idx].velocity[2] = p_velocity.z;
  3459. particles->emission_buffer->data[idx].custom[0] = p_custom.r;
  3460. particles->emission_buffer->data[idx].custom[1] = p_custom.g;
  3461. particles->emission_buffer->data[idx].custom[2] = p_custom.b;
  3462. particles->emission_buffer->data[idx].custom[3] = p_custom.a;
  3463. particles->emission_buffer->data[idx].color[0] = p_color.r;
  3464. particles->emission_buffer->data[idx].color[1] = p_color.g;
  3465. particles->emission_buffer->data[idx].color[2] = p_color.b;
  3466. particles->emission_buffer->data[idx].color[3] = p_color.a;
  3467. particles->emission_buffer->data[idx].flags = p_emit_flags;
  3468. particles->emission_buffer->particle_count++;
  3469. }
  3470. }
  3471. void RendererStorageRD::particles_request_process(RID p_particles) {
  3472. Particles *particles = particles_owner.getornull(p_particles);
  3473. ERR_FAIL_COND(!particles);
  3474. if (!particles->dirty) {
  3475. particles->dirty = true;
  3476. particles->update_list = particle_update_list;
  3477. particle_update_list = particles;
  3478. }
  3479. }
  3480. AABB RendererStorageRD::particles_get_current_aabb(RID p_particles) {
  3481. const Particles *particles = particles_owner.getornull(p_particles);
  3482. ERR_FAIL_COND_V(!particles, AABB());
  3483. Vector<ParticleData> data;
  3484. data.resize(particles->amount);
  3485. Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(particles->particle_buffer);
  3486. Transform inv = particles->emission_transform.affine_inverse();
  3487. AABB aabb;
  3488. if (buffer.size()) {
  3489. bool first = true;
  3490. const ParticleData *particle_data = (const ParticleData *)data.ptr();
  3491. for (int i = 0; i < particles->amount; i++) {
  3492. if (particle_data[i].active) {
  3493. Vector3 pos = Vector3(particle_data[i].xform[12], particle_data[i].xform[13], particle_data[i].xform[14]);
  3494. if (!particles->use_local_coords) {
  3495. pos = inv.xform(pos);
  3496. }
  3497. if (first) {
  3498. aabb.position = pos;
  3499. first = false;
  3500. } else {
  3501. aabb.expand_to(pos);
  3502. }
  3503. }
  3504. }
  3505. }
  3506. float longest_axis_size = 0;
  3507. for (int i = 0; i < particles->draw_passes.size(); i++) {
  3508. if (particles->draw_passes[i].is_valid()) {
  3509. AABB maabb = mesh_get_aabb(particles->draw_passes[i], RID());
  3510. longest_axis_size = MAX(maabb.get_longest_axis_size(), longest_axis_size);
  3511. }
  3512. }
  3513. aabb.grow_by(longest_axis_size);
  3514. return aabb;
  3515. }
  3516. AABB RendererStorageRD::particles_get_aabb(RID p_particles) const {
  3517. const Particles *particles = particles_owner.getornull(p_particles);
  3518. ERR_FAIL_COND_V(!particles, AABB());
  3519. return particles->custom_aabb;
  3520. }
  3521. void RendererStorageRD::particles_set_emission_transform(RID p_particles, const Transform &p_transform) {
  3522. Particles *particles = particles_owner.getornull(p_particles);
  3523. ERR_FAIL_COND(!particles);
  3524. particles->emission_transform = p_transform;
  3525. }
  3526. int RendererStorageRD::particles_get_draw_passes(RID p_particles) const {
  3527. const Particles *particles = particles_owner.getornull(p_particles);
  3528. ERR_FAIL_COND_V(!particles, 0);
  3529. return particles->draw_passes.size();
  3530. }
  3531. RID RendererStorageRD::particles_get_draw_pass_mesh(RID p_particles, int p_pass) const {
  3532. const Particles *particles = particles_owner.getornull(p_particles);
  3533. ERR_FAIL_COND_V(!particles, RID());
  3534. ERR_FAIL_INDEX_V(p_pass, particles->draw_passes.size(), RID());
  3535. return particles->draw_passes[p_pass];
  3536. }
  3537. void RendererStorageRD::particles_add_collision(RID p_particles, RID p_particles_collision_instance) {
  3538. Particles *particles = particles_owner.getornull(p_particles);
  3539. ERR_FAIL_COND(!particles);
  3540. particles->collisions.insert(p_particles_collision_instance);
  3541. }
  3542. void RendererStorageRD::particles_remove_collision(RID p_particles, RID p_particles_collision_instance) {
  3543. Particles *particles = particles_owner.getornull(p_particles);
  3544. ERR_FAIL_COND(!particles);
  3545. particles->collisions.erase(p_particles_collision_instance);
  3546. }
  3547. void RendererStorageRD::_particles_process(Particles *p_particles, float p_delta) {
  3548. if (p_particles->particles_material_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(p_particles->particles_material_uniform_set)) {
  3549. Vector<RD::Uniform> uniforms;
  3550. {
  3551. RD::Uniform u;
  3552. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3553. u.binding = 0;
  3554. u.ids.push_back(p_particles->frame_params_buffer);
  3555. uniforms.push_back(u);
  3556. }
  3557. {
  3558. RD::Uniform u;
  3559. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3560. u.binding = 1;
  3561. u.ids.push_back(p_particles->particle_buffer);
  3562. uniforms.push_back(u);
  3563. }
  3564. {
  3565. RD::Uniform u;
  3566. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3567. u.binding = 2;
  3568. if (p_particles->emission_storage_buffer.is_valid()) {
  3569. u.ids.push_back(p_particles->emission_storage_buffer);
  3570. } else {
  3571. u.ids.push_back(default_rd_storage_buffer);
  3572. }
  3573. uniforms.push_back(u);
  3574. }
  3575. {
  3576. RD::Uniform u;
  3577. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3578. u.binding = 3;
  3579. Particles *sub_emitter = particles_owner.getornull(p_particles->sub_emitter);
  3580. if (sub_emitter) {
  3581. if (sub_emitter->emission_buffer == nullptr) { //no emission buffer, allocate emission buffer
  3582. _particles_allocate_emission_buffer(sub_emitter);
  3583. }
  3584. u.ids.push_back(sub_emitter->emission_storage_buffer);
  3585. } else {
  3586. u.ids.push_back(default_rd_storage_buffer);
  3587. }
  3588. uniforms.push_back(u);
  3589. }
  3590. p_particles->particles_material_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, 1);
  3591. }
  3592. float new_phase = Math::fmod((float)p_particles->phase + (p_delta / p_particles->lifetime) * p_particles->speed_scale, (float)1.0);
  3593. ParticlesFrameParams &frame_params = p_particles->frame_params;
  3594. if (p_particles->clear) {
  3595. p_particles->cycle_number = 0;
  3596. p_particles->random_seed = Math::rand();
  3597. } else if (new_phase < p_particles->phase) {
  3598. if (p_particles->one_shot) {
  3599. p_particles->emitting = false;
  3600. }
  3601. p_particles->cycle_number++;
  3602. }
  3603. frame_params.emitting = p_particles->emitting;
  3604. frame_params.system_phase = new_phase;
  3605. frame_params.prev_system_phase = p_particles->phase;
  3606. p_particles->phase = new_phase;
  3607. frame_params.time = RendererCompositorRD::singleton->get_total_time();
  3608. frame_params.delta = p_delta * p_particles->speed_scale;
  3609. frame_params.random_seed = p_particles->random_seed;
  3610. frame_params.explosiveness = p_particles->explosiveness;
  3611. frame_params.randomness = p_particles->randomness;
  3612. if (p_particles->use_local_coords) {
  3613. store_transform(Transform(), frame_params.emission_transform);
  3614. } else {
  3615. store_transform(p_particles->emission_transform, frame_params.emission_transform);
  3616. }
  3617. frame_params.cycle = p_particles->cycle_number;
  3618. { //collision and attractors
  3619. frame_params.collider_count = 0;
  3620. frame_params.attractor_count = 0;
  3621. frame_params.particle_size = p_particles->collision_base_size;
  3622. RID collision_3d_textures[ParticlesFrameParams::MAX_3D_TEXTURES];
  3623. RID collision_heightmap_texture;
  3624. Transform to_particles;
  3625. if (p_particles->use_local_coords) {
  3626. to_particles = p_particles->emission_transform.affine_inverse();
  3627. }
  3628. uint32_t collision_3d_textures_used = 0;
  3629. for (const Set<RID>::Element *E = p_particles->collisions.front(); E; E = E->next()) {
  3630. ParticlesCollisionInstance *pci = particles_collision_instance_owner.getornull(E->get());
  3631. if (!pci || !pci->active) {
  3632. continue;
  3633. }
  3634. ParticlesCollision *pc = particles_collision_owner.getornull(pci->collision);
  3635. ERR_CONTINUE(!pc);
  3636. Transform to_collider = pci->transform;
  3637. if (p_particles->use_local_coords) {
  3638. to_collider = to_particles * to_collider;
  3639. }
  3640. Vector3 scale = to_collider.basis.get_scale();
  3641. to_collider.basis.orthonormalize();
  3642. if (pc->type <= RS::PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT) {
  3643. //attractor
  3644. if (frame_params.attractor_count >= ParticlesFrameParams::MAX_ATTRACTORS) {
  3645. continue;
  3646. }
  3647. ParticlesFrameParams::Attractor &attr = frame_params.attractors[frame_params.attractor_count];
  3648. store_transform(to_collider, attr.transform);
  3649. attr.strength = pc->attractor_strength;
  3650. attr.attenuation = pc->attractor_attenuation;
  3651. attr.directionality = pc->attractor_directionality;
  3652. switch (pc->type) {
  3653. case RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT: {
  3654. attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_SPHERE;
  3655. float radius = pc->radius;
  3656. radius *= (scale.x + scale.y + scale.z) / 3.0;
  3657. attr.extents[0] = radius;
  3658. attr.extents[1] = radius;
  3659. attr.extents[2] = radius;
  3660. } break;
  3661. case RS::PARTICLES_COLLISION_TYPE_BOX_ATTRACT: {
  3662. attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_BOX;
  3663. Vector3 extents = pc->extents * scale;
  3664. attr.extents[0] = extents.x;
  3665. attr.extents[1] = extents.y;
  3666. attr.extents[2] = extents.z;
  3667. } break;
  3668. case RS::PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT: {
  3669. if (collision_3d_textures_used >= ParticlesFrameParams::MAX_3D_TEXTURES) {
  3670. continue;
  3671. }
  3672. attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_VECTOR_FIELD;
  3673. Vector3 extents = pc->extents * scale;
  3674. attr.extents[0] = extents.x;
  3675. attr.extents[1] = extents.y;
  3676. attr.extents[2] = extents.z;
  3677. attr.texture_index = collision_3d_textures_used;
  3678. collision_3d_textures[collision_3d_textures_used] = pc->field_texture;
  3679. collision_3d_textures_used++;
  3680. } break;
  3681. default: {
  3682. }
  3683. }
  3684. frame_params.attractor_count++;
  3685. } else {
  3686. //collider
  3687. if (frame_params.collider_count >= ParticlesFrameParams::MAX_COLLIDERS) {
  3688. continue;
  3689. }
  3690. ParticlesFrameParams::Collider &col = frame_params.colliders[frame_params.collider_count];
  3691. store_transform(to_collider, col.transform);
  3692. switch (pc->type) {
  3693. case RS::PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE: {
  3694. col.type = ParticlesFrameParams::COLLISION_TYPE_SPHERE;
  3695. float radius = pc->radius;
  3696. radius *= (scale.x + scale.y + scale.z) / 3.0;
  3697. col.extents[0] = radius;
  3698. col.extents[1] = radius;
  3699. col.extents[2] = radius;
  3700. } break;
  3701. case RS::PARTICLES_COLLISION_TYPE_BOX_COLLIDE: {
  3702. col.type = ParticlesFrameParams::COLLISION_TYPE_BOX;
  3703. Vector3 extents = pc->extents * scale;
  3704. col.extents[0] = extents.x;
  3705. col.extents[1] = extents.y;
  3706. col.extents[2] = extents.z;
  3707. } break;
  3708. case RS::PARTICLES_COLLISION_TYPE_SDF_COLLIDE: {
  3709. if (collision_3d_textures_used >= ParticlesFrameParams::MAX_3D_TEXTURES) {
  3710. continue;
  3711. }
  3712. col.type = ParticlesFrameParams::COLLISION_TYPE_SDF;
  3713. Vector3 extents = pc->extents * scale;
  3714. col.extents[0] = extents.x;
  3715. col.extents[1] = extents.y;
  3716. col.extents[2] = extents.z;
  3717. col.texture_index = collision_3d_textures_used;
  3718. col.scale = (scale.x + scale.y + scale.z) * 0.333333333333; //non uniform scale non supported
  3719. collision_3d_textures[collision_3d_textures_used] = pc->field_texture;
  3720. collision_3d_textures_used++;
  3721. } break;
  3722. case RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE: {
  3723. if (collision_heightmap_texture != RID()) { //already taken
  3724. continue;
  3725. }
  3726. col.type = ParticlesFrameParams::COLLISION_TYPE_HEIGHT_FIELD;
  3727. Vector3 extents = pc->extents * scale;
  3728. col.extents[0] = extents.x;
  3729. col.extents[1] = extents.y;
  3730. col.extents[2] = extents.z;
  3731. collision_heightmap_texture = pc->heightfield_texture;
  3732. } break;
  3733. default: {
  3734. }
  3735. }
  3736. frame_params.collider_count++;
  3737. }
  3738. }
  3739. bool different = false;
  3740. if (collision_3d_textures_used == p_particles->collision_3d_textures_used) {
  3741. for (int i = 0; i < ParticlesFrameParams::MAX_3D_TEXTURES; i++) {
  3742. if (p_particles->collision_3d_textures[i] != collision_3d_textures[i]) {
  3743. different = true;
  3744. break;
  3745. }
  3746. }
  3747. }
  3748. if (collision_heightmap_texture != p_particles->collision_heightmap_texture) {
  3749. different = true;
  3750. }
  3751. bool uniform_set_valid = RD::get_singleton()->uniform_set_is_valid(p_particles->collision_textures_uniform_set);
  3752. if (different || !uniform_set_valid) {
  3753. if (uniform_set_valid) {
  3754. RD::get_singleton()->free(p_particles->collision_textures_uniform_set);
  3755. }
  3756. Vector<RD::Uniform> uniforms;
  3757. {
  3758. RD::Uniform u;
  3759. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  3760. u.binding = 0;
  3761. for (uint32_t i = 0; i < ParticlesFrameParams::MAX_3D_TEXTURES; i++) {
  3762. RID rd_tex;
  3763. if (i < collision_3d_textures_used) {
  3764. Texture *t = texture_owner.getornull(collision_3d_textures[i]);
  3765. if (t && t->type == Texture::TYPE_3D) {
  3766. rd_tex = t->rd_texture;
  3767. }
  3768. }
  3769. if (rd_tex == RID()) {
  3770. rd_tex = default_rd_textures[DEFAULT_RD_TEXTURE_3D_WHITE];
  3771. }
  3772. u.ids.push_back(rd_tex);
  3773. }
  3774. uniforms.push_back(u);
  3775. }
  3776. {
  3777. RD::Uniform u;
  3778. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  3779. u.binding = 1;
  3780. if (collision_heightmap_texture.is_valid()) {
  3781. u.ids.push_back(collision_heightmap_texture);
  3782. } else {
  3783. u.ids.push_back(default_rd_textures[DEFAULT_RD_TEXTURE_BLACK]);
  3784. }
  3785. uniforms.push_back(u);
  3786. }
  3787. p_particles->collision_textures_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, 2);
  3788. }
  3789. }
  3790. ParticlesShader::PushConstant push_constant;
  3791. push_constant.clear = p_particles->clear;
  3792. push_constant.total_particles = p_particles->amount;
  3793. push_constant.lifetime = p_particles->lifetime;
  3794. push_constant.trail_size = 1;
  3795. push_constant.use_fractional_delta = p_particles->fractional_delta;
  3796. push_constant.sub_emitter_mode = !p_particles->emitting && p_particles->emission_buffer && (p_particles->emission_buffer->particle_count > 0 || p_particles->force_sub_emit);
  3797. p_particles->force_sub_emit = false; //reset
  3798. Particles *sub_emitter = particles_owner.getornull(p_particles->sub_emitter);
  3799. if (sub_emitter && sub_emitter->emission_storage_buffer.is_valid()) {
  3800. // print_line("updating subemitter buffer");
  3801. int32_t zero[4] = { 0, sub_emitter->amount, 0, 0 };
  3802. RD::get_singleton()->buffer_update(sub_emitter->emission_storage_buffer, 0, sizeof(uint32_t) * 4, zero, true);
  3803. push_constant.can_emit = true;
  3804. if (sub_emitter->emitting) {
  3805. sub_emitter->emitting = false;
  3806. sub_emitter->clear = true; //will need to clear if it was emitting, sorry
  3807. }
  3808. //make sure the sub emitter processes particles too
  3809. sub_emitter->inactive = false;
  3810. sub_emitter->inactive_time = 0;
  3811. sub_emitter->force_sub_emit = true;
  3812. } else {
  3813. push_constant.can_emit = false;
  3814. }
  3815. if (p_particles->emission_buffer && p_particles->emission_buffer->particle_count) {
  3816. 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);
  3817. p_particles->emission_buffer->particle_count = 0;
  3818. }
  3819. p_particles->clear = false;
  3820. RD::get_singleton()->buffer_update(p_particles->frame_params_buffer, 0, sizeof(ParticlesFrameParams), &frame_params, true);
  3821. ParticlesMaterialData *m = (ParticlesMaterialData *)material_get_data(p_particles->process_material, SHADER_TYPE_PARTICLES);
  3822. if (!m) {
  3823. m = (ParticlesMaterialData *)material_get_data(particles_shader.default_material, SHADER_TYPE_PARTICLES);
  3824. }
  3825. ERR_FAIL_COND(!m);
  3826. //todo should maybe compute all particle systems together?
  3827. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  3828. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, m->shader_data->pipeline);
  3829. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles_shader.base_uniform_set, 0);
  3830. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_particles->particles_material_uniform_set, 1);
  3831. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_particles->collision_textures_uniform_set, 2);
  3832. if (m->uniform_set.is_valid()) {
  3833. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, m->uniform_set, 3);
  3834. }
  3835. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ParticlesShader::PushConstant));
  3836. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_particles->amount, 1, 1, 64, 1, 1);
  3837. RD::get_singleton()->compute_list_end();
  3838. }
  3839. void RendererStorageRD::particles_set_view_axis(RID p_particles, const Vector3 &p_axis) {
  3840. Particles *particles = particles_owner.getornull(p_particles);
  3841. ERR_FAIL_COND(!particles);
  3842. if (particles->draw_order != RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH) {
  3843. return; //uninteresting for other modes
  3844. }
  3845. //copy to sort buffer
  3846. if (particles->particles_sort_buffer == RID()) {
  3847. uint32_t size = particles->amount;
  3848. if (size & 1) {
  3849. size++; //make multiple of 16
  3850. }
  3851. size *= sizeof(float) * 2;
  3852. particles->particles_sort_buffer = RD::get_singleton()->storage_buffer_create(size);
  3853. {
  3854. Vector<RD::Uniform> uniforms;
  3855. {
  3856. RD::Uniform u;
  3857. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3858. u.binding = 0;
  3859. u.ids.push_back(particles->particles_sort_buffer);
  3860. uniforms.push_back(u);
  3861. }
  3862. 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);
  3863. }
  3864. }
  3865. Vector3 axis = -p_axis; // cameras look to z negative
  3866. if (particles->use_local_coords) {
  3867. axis = particles->emission_transform.basis.xform_inv(axis).normalized();
  3868. }
  3869. ParticlesShader::CopyPushConstant copy_push_constant;
  3870. copy_push_constant.total_particles = particles->amount;
  3871. copy_push_constant.sort_direction[0] = axis.x;
  3872. copy_push_constant.sort_direction[1] = axis.y;
  3873. copy_push_constant.sort_direction[2] = axis.z;
  3874. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  3875. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[ParticlesShader::COPY_MODE_FILL_SORT_BUFFER]);
  3876. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0);
  3877. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_sort_uniform_set, 1);
  3878. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy_push_constant, sizeof(ParticlesShader::CopyPushConstant));
  3879. RD::get_singleton()->compute_list_dispatch_threads(compute_list, particles->amount, 1, 1, 64, 1, 1);
  3880. RD::get_singleton()->compute_list_end();
  3881. effects.sort_buffer(particles->particles_sort_uniform_set, particles->amount);
  3882. compute_list = RD::get_singleton()->compute_list_begin();
  3883. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[ParticlesShader::COPY_MODE_FILL_INSTANCES_WITH_SORT_BUFFER]);
  3884. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0);
  3885. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_sort_uniform_set, 1);
  3886. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy_push_constant, sizeof(ParticlesShader::CopyPushConstant));
  3887. RD::get_singleton()->compute_list_dispatch_threads(compute_list, particles->amount, 1, 1, 64, 1, 1);
  3888. RD::get_singleton()->compute_list_end();
  3889. }
  3890. void RendererStorageRD::update_particles() {
  3891. while (particle_update_list) {
  3892. //use transform feedback to process particles
  3893. Particles *particles = particle_update_list;
  3894. //take and remove
  3895. particle_update_list = particles->update_list;
  3896. particles->update_list = nullptr;
  3897. particles->dirty = false;
  3898. if (particles->restart_request) {
  3899. particles->prev_ticks = 0;
  3900. particles->phase = 0;
  3901. particles->prev_phase = 0;
  3902. particles->clear = true;
  3903. particles->restart_request = false;
  3904. }
  3905. if (particles->inactive && !particles->emitting) {
  3906. //go next
  3907. continue;
  3908. }
  3909. if (particles->emitting) {
  3910. if (particles->inactive) {
  3911. //restart system from scratch
  3912. particles->prev_ticks = 0;
  3913. particles->phase = 0;
  3914. particles->prev_phase = 0;
  3915. particles->clear = true;
  3916. }
  3917. particles->inactive = false;
  3918. particles->inactive_time = 0;
  3919. } else {
  3920. particles->inactive_time += particles->speed_scale * RendererCompositorRD::singleton->get_frame_delta_time();
  3921. if (particles->inactive_time > particles->lifetime * 1.2) {
  3922. particles->inactive = true;
  3923. continue;
  3924. }
  3925. }
  3926. bool zero_time_scale = Engine::get_singleton()->get_time_scale() <= 0.0;
  3927. if (particles->clear && particles->pre_process_time > 0.0) {
  3928. float frame_time;
  3929. if (particles->fixed_fps > 0)
  3930. frame_time = 1.0 / particles->fixed_fps;
  3931. else
  3932. frame_time = 1.0 / 30.0;
  3933. float todo = particles->pre_process_time;
  3934. while (todo >= 0) {
  3935. _particles_process(particles, frame_time);
  3936. todo -= frame_time;
  3937. }
  3938. }
  3939. if (particles->fixed_fps > 0) {
  3940. float frame_time;
  3941. float decr;
  3942. if (zero_time_scale) {
  3943. frame_time = 0.0;
  3944. decr = 1.0 / particles->fixed_fps;
  3945. } else {
  3946. frame_time = 1.0 / particles->fixed_fps;
  3947. decr = frame_time;
  3948. }
  3949. float delta = RendererCompositorRD::singleton->get_frame_delta_time();
  3950. if (delta > 0.1) { //avoid recursive stalls if fps goes below 10
  3951. delta = 0.1;
  3952. } else if (delta <= 0.0) { //unlikely but..
  3953. delta = 0.001;
  3954. }
  3955. float todo = particles->frame_remainder + delta;
  3956. while (todo >= frame_time) {
  3957. _particles_process(particles, frame_time);
  3958. todo -= decr;
  3959. }
  3960. particles->frame_remainder = todo;
  3961. } else {
  3962. if (zero_time_scale)
  3963. _particles_process(particles, 0.0);
  3964. else
  3965. _particles_process(particles, RendererCompositorRD::singleton->get_frame_delta_time());
  3966. }
  3967. //copy particles to instance buffer
  3968. if (particles->draw_order != RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH) {
  3969. ParticlesShader::CopyPushConstant copy_push_constant;
  3970. copy_push_constant.total_particles = particles->amount;
  3971. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  3972. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[ParticlesShader::COPY_MODE_FILL_INSTANCES]);
  3973. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0);
  3974. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy_push_constant, sizeof(ParticlesShader::CopyPushConstant));
  3975. RD::get_singleton()->compute_list_dispatch_threads(compute_list, particles->amount, 1, 1, 64, 1, 1);
  3976. RD::get_singleton()->compute_list_end();
  3977. }
  3978. particles->dependency.changed_notify(DEPENDENCY_CHANGED_AABB);
  3979. }
  3980. }
  3981. bool RendererStorageRD::particles_is_inactive(RID p_particles) const {
  3982. const Particles *particles = particles_owner.getornull(p_particles);
  3983. ERR_FAIL_COND_V(!particles, false);
  3984. return !particles->emitting && particles->inactive;
  3985. }
  3986. /* SKY SHADER */
  3987. void RendererStorageRD::ParticlesShaderData::set_code(const String &p_code) {
  3988. //compile
  3989. code = p_code;
  3990. valid = false;
  3991. ubo_size = 0;
  3992. uniforms.clear();
  3993. if (code == String()) {
  3994. return; //just invalid, but no error
  3995. }
  3996. ShaderCompilerRD::GeneratedCode gen_code;
  3997. ShaderCompilerRD::IdentifierActions actions;
  3998. /*
  3999. uses_time = false;
  4000. actions.render_mode_flags["use_half_res_pass"] = &uses_half_res;
  4001. actions.render_mode_flags["use_quarter_res_pass"] = &uses_quarter_res;
  4002. actions.usage_flag_pointers["TIME"] = &uses_time;
  4003. */
  4004. actions.uniforms = &uniforms;
  4005. Error err = base_singleton->particles_shader.compiler.compile(RS::SHADER_PARTICLES, code, &actions, path, gen_code);
  4006. ERR_FAIL_COND(err != OK);
  4007. if (version.is_null()) {
  4008. version = base_singleton->particles_shader.shader.version_create();
  4009. }
  4010. base_singleton->particles_shader.shader.version_set_compute_code(version, gen_code.uniforms, gen_code.compute_global, gen_code.compute, gen_code.defines);
  4011. ERR_FAIL_COND(!base_singleton->particles_shader.shader.version_is_valid(version));
  4012. ubo_size = gen_code.uniform_total_size;
  4013. ubo_offsets = gen_code.uniform_offsets;
  4014. texture_uniforms = gen_code.texture_uniforms;
  4015. //update pipelines
  4016. pipeline = RD::get_singleton()->compute_pipeline_create(base_singleton->particles_shader.shader.version_get_shader(version, 0));
  4017. valid = true;
  4018. }
  4019. void RendererStorageRD::ParticlesShaderData::set_default_texture_param(const StringName &p_name, RID p_texture) {
  4020. if (!p_texture.is_valid()) {
  4021. default_texture_params.erase(p_name);
  4022. } else {
  4023. default_texture_params[p_name] = p_texture;
  4024. }
  4025. }
  4026. void RendererStorageRD::ParticlesShaderData::get_param_list(List<PropertyInfo> *p_param_list) const {
  4027. Map<int, StringName> order;
  4028. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = uniforms.front(); E; E = E->next()) {
  4029. if (E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL || E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  4030. continue;
  4031. }
  4032. if (E->get().texture_order >= 0) {
  4033. order[E->get().texture_order + 100000] = E->key();
  4034. } else {
  4035. order[E->get().order] = E->key();
  4036. }
  4037. }
  4038. for (Map<int, StringName>::Element *E = order.front(); E; E = E->next()) {
  4039. PropertyInfo pi = ShaderLanguage::uniform_to_property_info(uniforms[E->get()]);
  4040. pi.name = E->get();
  4041. p_param_list->push_back(pi);
  4042. }
  4043. }
  4044. void RendererStorageRD::ParticlesShaderData::get_instance_param_list(List<RendererStorage::InstanceShaderParam> *p_param_list) const {
  4045. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = uniforms.front(); E; E = E->next()) {
  4046. if (E->get().scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  4047. continue;
  4048. }
  4049. RendererStorage::InstanceShaderParam p;
  4050. p.info = ShaderLanguage::uniform_to_property_info(E->get());
  4051. p.info.name = E->key(); //supply name
  4052. p.index = E->get().instance_index;
  4053. p.default_value = ShaderLanguage::constant_value_to_variant(E->get().default_value, E->get().type, E->get().hint);
  4054. p_param_list->push_back(p);
  4055. }
  4056. }
  4057. bool RendererStorageRD::ParticlesShaderData::is_param_texture(const StringName &p_param) const {
  4058. if (!uniforms.has(p_param)) {
  4059. return false;
  4060. }
  4061. return uniforms[p_param].texture_order >= 0;
  4062. }
  4063. bool RendererStorageRD::ParticlesShaderData::is_animated() const {
  4064. return false;
  4065. }
  4066. bool RendererStorageRD::ParticlesShaderData::casts_shadows() const {
  4067. return false;
  4068. }
  4069. Variant RendererStorageRD::ParticlesShaderData::get_default_parameter(const StringName &p_parameter) const {
  4070. if (uniforms.has(p_parameter)) {
  4071. ShaderLanguage::ShaderNode::Uniform uniform = uniforms[p_parameter];
  4072. Vector<ShaderLanguage::ConstantNode::Value> default_value = uniform.default_value;
  4073. return ShaderLanguage::constant_value_to_variant(default_value, uniform.type, uniform.hint);
  4074. }
  4075. return Variant();
  4076. }
  4077. RS::ShaderNativeSourceCode RendererStorageRD::ParticlesShaderData::get_native_source_code() const {
  4078. return base_singleton->particles_shader.shader.version_get_native_source_code(version);
  4079. }
  4080. RendererStorageRD::ParticlesShaderData::ParticlesShaderData() {
  4081. valid = false;
  4082. }
  4083. RendererStorageRD::ParticlesShaderData::~ParticlesShaderData() {
  4084. //pipeline variants will clear themselves if shader is gone
  4085. if (version.is_valid()) {
  4086. base_singleton->particles_shader.shader.version_free(version);
  4087. }
  4088. }
  4089. RendererStorageRD::ShaderData *RendererStorageRD::_create_particles_shader_func() {
  4090. ParticlesShaderData *shader_data = memnew(ParticlesShaderData);
  4091. return shader_data;
  4092. }
  4093. void RendererStorageRD::ParticlesMaterialData::update_parameters(const Map<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) {
  4094. uniform_set_updated = true;
  4095. if ((uint32_t)ubo_data.size() != shader_data->ubo_size) {
  4096. p_uniform_dirty = true;
  4097. if (uniform_buffer.is_valid()) {
  4098. RD::get_singleton()->free(uniform_buffer);
  4099. uniform_buffer = RID();
  4100. }
  4101. ubo_data.resize(shader_data->ubo_size);
  4102. if (ubo_data.size()) {
  4103. uniform_buffer = RD::get_singleton()->uniform_buffer_create(ubo_data.size());
  4104. memset(ubo_data.ptrw(), 0, ubo_data.size()); //clear
  4105. }
  4106. //clear previous uniform set
  4107. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  4108. RD::get_singleton()->free(uniform_set);
  4109. uniform_set = RID();
  4110. }
  4111. }
  4112. //check whether buffer changed
  4113. if (p_uniform_dirty && ubo_data.size()) {
  4114. update_uniform_buffer(shader_data->uniforms, shader_data->ubo_offsets.ptr(), p_parameters, ubo_data.ptrw(), ubo_data.size(), false);
  4115. RD::get_singleton()->buffer_update(uniform_buffer, 0, ubo_data.size(), ubo_data.ptrw());
  4116. }
  4117. uint32_t tex_uniform_count = shader_data->texture_uniforms.size();
  4118. if ((uint32_t)texture_cache.size() != tex_uniform_count) {
  4119. texture_cache.resize(tex_uniform_count);
  4120. p_textures_dirty = true;
  4121. //clear previous uniform set
  4122. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  4123. RD::get_singleton()->free(uniform_set);
  4124. uniform_set = RID();
  4125. }
  4126. }
  4127. if (p_textures_dirty && tex_uniform_count) {
  4128. update_textures(p_parameters, shader_data->default_texture_params, shader_data->texture_uniforms, texture_cache.ptrw(), true);
  4129. }
  4130. if (shader_data->ubo_size == 0 && shader_data->texture_uniforms.size() == 0) {
  4131. // This material does not require an uniform set, so don't create it.
  4132. return;
  4133. }
  4134. if (!p_textures_dirty && uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  4135. //no reason to update uniform set, only UBO (or nothing) was needed to update
  4136. return;
  4137. }
  4138. Vector<RD::Uniform> uniforms;
  4139. {
  4140. if (shader_data->ubo_size) {
  4141. RD::Uniform u;
  4142. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  4143. u.binding = 0;
  4144. u.ids.push_back(uniform_buffer);
  4145. uniforms.push_back(u);
  4146. }
  4147. const RID *textures = texture_cache.ptrw();
  4148. for (uint32_t i = 0; i < tex_uniform_count; i++) {
  4149. RD::Uniform u;
  4150. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  4151. u.binding = 1 + i;
  4152. u.ids.push_back(textures[i]);
  4153. uniforms.push_back(u);
  4154. }
  4155. }
  4156. uniform_set = RD::get_singleton()->uniform_set_create(uniforms, base_singleton->particles_shader.shader.version_get_shader(shader_data->version, 0), 3);
  4157. }
  4158. RendererStorageRD::ParticlesMaterialData::~ParticlesMaterialData() {
  4159. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  4160. RD::get_singleton()->free(uniform_set);
  4161. }
  4162. if (uniform_buffer.is_valid()) {
  4163. RD::get_singleton()->free(uniform_buffer);
  4164. }
  4165. }
  4166. RendererStorageRD::MaterialData *RendererStorageRD::_create_particles_material_func(ParticlesShaderData *p_shader) {
  4167. ParticlesMaterialData *material_data = memnew(ParticlesMaterialData);
  4168. material_data->shader_data = p_shader;
  4169. material_data->last_frame = false;
  4170. //update will happen later anyway so do nothing.
  4171. return material_data;
  4172. }
  4173. ////////
  4174. /* PARTICLES COLLISION API */
  4175. RID RendererStorageRD::particles_collision_create() {
  4176. return particles_collision_owner.make_rid(ParticlesCollision());
  4177. }
  4178. RID RendererStorageRD::particles_collision_get_heightfield_framebuffer(RID p_particles_collision) const {
  4179. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4180. ERR_FAIL_COND_V(!particles_collision, RID());
  4181. ERR_FAIL_COND_V(particles_collision->type != RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE, RID());
  4182. if (particles_collision->heightfield_texture == RID()) {
  4183. //create
  4184. int resolutions[RS::PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_MAX] = { 256, 512, 1024, 2048, 4096, 8192 };
  4185. Size2i size;
  4186. if (particles_collision->extents.x > particles_collision->extents.z) {
  4187. size.x = resolutions[particles_collision->heightfield_resolution];
  4188. size.y = int32_t(particles_collision->extents.z / particles_collision->extents.x * size.x);
  4189. } else {
  4190. size.y = resolutions[particles_collision->heightfield_resolution];
  4191. size.x = int32_t(particles_collision->extents.x / particles_collision->extents.z * size.y);
  4192. }
  4193. RD::TextureFormat tf;
  4194. tf.format = RD::DATA_FORMAT_D32_SFLOAT;
  4195. tf.width = size.x;
  4196. tf.height = size.y;
  4197. tf.texture_type = RD::TEXTURE_TYPE_2D;
  4198. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  4199. particles_collision->heightfield_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4200. Vector<RID> fb_tex;
  4201. fb_tex.push_back(particles_collision->heightfield_texture);
  4202. particles_collision->heightfield_fb = RD::get_singleton()->framebuffer_create(fb_tex);
  4203. particles_collision->heightfield_fb_size = size;
  4204. }
  4205. return particles_collision->heightfield_fb;
  4206. }
  4207. void RendererStorageRD::particles_collision_set_collision_type(RID p_particles_collision, RS::ParticlesCollisionType p_type) {
  4208. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4209. ERR_FAIL_COND(!particles_collision);
  4210. if (p_type == particles_collision->type) {
  4211. return;
  4212. }
  4213. if (particles_collision->heightfield_texture.is_valid()) {
  4214. RD::get_singleton()->free(particles_collision->heightfield_texture);
  4215. particles_collision->heightfield_texture = RID();
  4216. }
  4217. particles_collision->type = p_type;
  4218. particles_collision->dependency.changed_notify(DEPENDENCY_CHANGED_AABB);
  4219. }
  4220. void RendererStorageRD::particles_collision_set_cull_mask(RID p_particles_collision, uint32_t p_cull_mask) {
  4221. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4222. ERR_FAIL_COND(!particles_collision);
  4223. particles_collision->cull_mask = p_cull_mask;
  4224. }
  4225. void RendererStorageRD::particles_collision_set_sphere_radius(RID p_particles_collision, float p_radius) {
  4226. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4227. ERR_FAIL_COND(!particles_collision);
  4228. particles_collision->radius = p_radius;
  4229. particles_collision->dependency.changed_notify(DEPENDENCY_CHANGED_AABB);
  4230. }
  4231. void RendererStorageRD::particles_collision_set_box_extents(RID p_particles_collision, const Vector3 &p_extents) {
  4232. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4233. ERR_FAIL_COND(!particles_collision);
  4234. particles_collision->extents = p_extents;
  4235. particles_collision->dependency.changed_notify(DEPENDENCY_CHANGED_AABB);
  4236. }
  4237. void RendererStorageRD::particles_collision_set_attractor_strength(RID p_particles_collision, float p_strength) {
  4238. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4239. ERR_FAIL_COND(!particles_collision);
  4240. particles_collision->attractor_strength = p_strength;
  4241. }
  4242. void RendererStorageRD::particles_collision_set_attractor_directionality(RID p_particles_collision, float p_directionality) {
  4243. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4244. ERR_FAIL_COND(!particles_collision);
  4245. particles_collision->attractor_directionality = p_directionality;
  4246. }
  4247. void RendererStorageRD::particles_collision_set_attractor_attenuation(RID p_particles_collision, float p_curve) {
  4248. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4249. ERR_FAIL_COND(!particles_collision);
  4250. particles_collision->attractor_attenuation = p_curve;
  4251. }
  4252. void RendererStorageRD::particles_collision_set_field_texture(RID p_particles_collision, RID p_texture) {
  4253. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4254. ERR_FAIL_COND(!particles_collision);
  4255. particles_collision->field_texture = p_texture;
  4256. }
  4257. void RendererStorageRD::particles_collision_height_field_update(RID p_particles_collision) {
  4258. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4259. ERR_FAIL_COND(!particles_collision);
  4260. particles_collision->dependency.changed_notify(DEPENDENCY_CHANGED_AABB);
  4261. }
  4262. void RendererStorageRD::particles_collision_set_height_field_resolution(RID p_particles_collision, RS::ParticlesCollisionHeightfieldResolution p_resolution) {
  4263. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4264. ERR_FAIL_COND(!particles_collision);
  4265. if (particles_collision->heightfield_resolution == p_resolution) {
  4266. return;
  4267. }
  4268. particles_collision->heightfield_resolution = p_resolution;
  4269. if (particles_collision->heightfield_texture.is_valid()) {
  4270. RD::get_singleton()->free(particles_collision->heightfield_texture);
  4271. particles_collision->heightfield_texture = RID();
  4272. }
  4273. }
  4274. AABB RendererStorageRD::particles_collision_get_aabb(RID p_particles_collision) const {
  4275. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4276. ERR_FAIL_COND_V(!particles_collision, AABB());
  4277. switch (particles_collision->type) {
  4278. case RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT:
  4279. case RS::PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE: {
  4280. AABB aabb;
  4281. aabb.position = -Vector3(1, 1, 1) * particles_collision->radius;
  4282. aabb.size = Vector3(2, 2, 2) * particles_collision->radius;
  4283. return aabb;
  4284. }
  4285. default: {
  4286. AABB aabb;
  4287. aabb.position = -particles_collision->extents;
  4288. aabb.size = particles_collision->extents * 2;
  4289. return aabb;
  4290. }
  4291. }
  4292. return AABB();
  4293. }
  4294. Vector3 RendererStorageRD::particles_collision_get_extents(RID p_particles_collision) const {
  4295. const ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4296. ERR_FAIL_COND_V(!particles_collision, Vector3());
  4297. return particles_collision->extents;
  4298. }
  4299. bool RendererStorageRD::particles_collision_is_heightfield(RID p_particles_collision) const {
  4300. const ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  4301. ERR_FAIL_COND_V(!particles_collision, false);
  4302. return particles_collision->type == RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE;
  4303. }
  4304. RID RendererStorageRD::particles_collision_instance_create(RID p_collision) {
  4305. ParticlesCollisionInstance pci;
  4306. pci.collision = p_collision;
  4307. return particles_collision_instance_owner.make_rid(pci);
  4308. }
  4309. void RendererStorageRD::particles_collision_instance_set_transform(RID p_collision_instance, const Transform &p_transform) {
  4310. ParticlesCollisionInstance *pci = particles_collision_instance_owner.getornull(p_collision_instance);
  4311. ERR_FAIL_COND(!pci);
  4312. pci->transform = p_transform;
  4313. }
  4314. void RendererStorageRD::particles_collision_instance_set_active(RID p_collision_instance, bool p_active) {
  4315. ParticlesCollisionInstance *pci = particles_collision_instance_owner.getornull(p_collision_instance);
  4316. ERR_FAIL_COND(!pci);
  4317. pci->active = p_active;
  4318. }
  4319. /* SKELETON API */
  4320. RID RendererStorageRD::skeleton_create() {
  4321. return skeleton_owner.make_rid(Skeleton());
  4322. }
  4323. void RendererStorageRD::_skeleton_make_dirty(Skeleton *skeleton) {
  4324. if (!skeleton->dirty) {
  4325. skeleton->dirty = true;
  4326. skeleton->dirty_list = skeleton_dirty_list;
  4327. skeleton_dirty_list = skeleton;
  4328. }
  4329. }
  4330. void RendererStorageRD::skeleton_allocate(RID p_skeleton, int p_bones, bool p_2d_skeleton) {
  4331. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4332. ERR_FAIL_COND(!skeleton);
  4333. ERR_FAIL_COND(p_bones < 0);
  4334. if (skeleton->size == p_bones && skeleton->use_2d == p_2d_skeleton) {
  4335. return;
  4336. }
  4337. skeleton->size = p_bones;
  4338. skeleton->use_2d = p_2d_skeleton;
  4339. skeleton->uniform_set_3d = RID();
  4340. if (skeleton->buffer.is_valid()) {
  4341. RD::get_singleton()->free(skeleton->buffer);
  4342. skeleton->buffer = RID();
  4343. skeleton->data.resize(0);
  4344. skeleton->uniform_set_mi = RID();
  4345. }
  4346. if (skeleton->size) {
  4347. skeleton->data.resize(skeleton->size * (skeleton->use_2d ? 8 : 12));
  4348. skeleton->buffer = RD::get_singleton()->storage_buffer_create(skeleton->data.size() * sizeof(float));
  4349. zeromem(skeleton->data.ptrw(), skeleton->data.size() * sizeof(float));
  4350. _skeleton_make_dirty(skeleton);
  4351. {
  4352. Vector<RD::Uniform> uniforms;
  4353. {
  4354. RD::Uniform u;
  4355. u.binding = 0;
  4356. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  4357. u.ids.push_back(skeleton->buffer);
  4358. uniforms.push_back(u);
  4359. }
  4360. skeleton->uniform_set_mi = RD::get_singleton()->uniform_set_create(uniforms, skeleton_shader.version_shader[0], SkeletonShader::UNIFORM_SET_SKELETON);
  4361. }
  4362. }
  4363. skeleton->dependency.changed_notify(DEPENDENCY_CHANGED_SKELETON_DATA);
  4364. }
  4365. int RendererStorageRD::skeleton_get_bone_count(RID p_skeleton) const {
  4366. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4367. ERR_FAIL_COND_V(!skeleton, 0);
  4368. return skeleton->size;
  4369. }
  4370. void RendererStorageRD::skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform &p_transform) {
  4371. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4372. ERR_FAIL_COND(!skeleton);
  4373. ERR_FAIL_INDEX(p_bone, skeleton->size);
  4374. ERR_FAIL_COND(skeleton->use_2d);
  4375. float *dataptr = skeleton->data.ptrw() + p_bone * 12;
  4376. dataptr[0] = p_transform.basis.elements[0][0];
  4377. dataptr[1] = p_transform.basis.elements[0][1];
  4378. dataptr[2] = p_transform.basis.elements[0][2];
  4379. dataptr[3] = p_transform.origin.x;
  4380. dataptr[4] = p_transform.basis.elements[1][0];
  4381. dataptr[5] = p_transform.basis.elements[1][1];
  4382. dataptr[6] = p_transform.basis.elements[1][2];
  4383. dataptr[7] = p_transform.origin.y;
  4384. dataptr[8] = p_transform.basis.elements[2][0];
  4385. dataptr[9] = p_transform.basis.elements[2][1];
  4386. dataptr[10] = p_transform.basis.elements[2][2];
  4387. dataptr[11] = p_transform.origin.z;
  4388. _skeleton_make_dirty(skeleton);
  4389. }
  4390. Transform RendererStorageRD::skeleton_bone_get_transform(RID p_skeleton, int p_bone) const {
  4391. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4392. ERR_FAIL_COND_V(!skeleton, Transform());
  4393. ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform());
  4394. ERR_FAIL_COND_V(skeleton->use_2d, Transform());
  4395. const float *dataptr = skeleton->data.ptr() + p_bone * 12;
  4396. Transform t;
  4397. t.basis.elements[0][0] = dataptr[0];
  4398. t.basis.elements[0][1] = dataptr[1];
  4399. t.basis.elements[0][2] = dataptr[2];
  4400. t.origin.x = dataptr[3];
  4401. t.basis.elements[1][0] = dataptr[4];
  4402. t.basis.elements[1][1] = dataptr[5];
  4403. t.basis.elements[1][2] = dataptr[6];
  4404. t.origin.y = dataptr[7];
  4405. t.basis.elements[2][0] = dataptr[8];
  4406. t.basis.elements[2][1] = dataptr[9];
  4407. t.basis.elements[2][2] = dataptr[10];
  4408. t.origin.z = dataptr[11];
  4409. return t;
  4410. }
  4411. void RendererStorageRD::skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) {
  4412. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4413. ERR_FAIL_COND(!skeleton);
  4414. ERR_FAIL_INDEX(p_bone, skeleton->size);
  4415. ERR_FAIL_COND(!skeleton->use_2d);
  4416. float *dataptr = skeleton->data.ptrw() + p_bone * 8;
  4417. dataptr[0] = p_transform.elements[0][0];
  4418. dataptr[1] = p_transform.elements[1][0];
  4419. dataptr[2] = 0;
  4420. dataptr[3] = p_transform.elements[2][0];
  4421. dataptr[4] = p_transform.elements[0][1];
  4422. dataptr[5] = p_transform.elements[1][1];
  4423. dataptr[6] = 0;
  4424. dataptr[7] = p_transform.elements[2][1];
  4425. _skeleton_make_dirty(skeleton);
  4426. }
  4427. Transform2D RendererStorageRD::skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const {
  4428. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4429. ERR_FAIL_COND_V(!skeleton, Transform2D());
  4430. ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform2D());
  4431. ERR_FAIL_COND_V(!skeleton->use_2d, Transform2D());
  4432. const float *dataptr = skeleton->data.ptr() + p_bone * 8;
  4433. Transform2D t;
  4434. t.elements[0][0] = dataptr[0];
  4435. t.elements[1][0] = dataptr[1];
  4436. t.elements[2][0] = dataptr[3];
  4437. t.elements[0][1] = dataptr[4];
  4438. t.elements[1][1] = dataptr[5];
  4439. t.elements[2][1] = dataptr[7];
  4440. return t;
  4441. }
  4442. void RendererStorageRD::skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) {
  4443. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  4444. ERR_FAIL_COND(!skeleton->use_2d);
  4445. skeleton->base_transform_2d = p_base_transform;
  4446. }
  4447. void RendererStorageRD::_update_dirty_skeletons() {
  4448. while (skeleton_dirty_list) {
  4449. Skeleton *skeleton = skeleton_dirty_list;
  4450. if (skeleton->size) {
  4451. RD::get_singleton()->buffer_update(skeleton->buffer, 0, skeleton->data.size() * sizeof(float), skeleton->data.ptr(), false);
  4452. }
  4453. skeleton_dirty_list = skeleton->dirty_list;
  4454. skeleton->dependency.changed_notify(DEPENDENCY_CHANGED_SKELETON_BONES);
  4455. skeleton->version++;
  4456. skeleton->dirty = false;
  4457. skeleton->dirty_list = nullptr;
  4458. }
  4459. skeleton_dirty_list = nullptr;
  4460. }
  4461. /* LIGHT */
  4462. RID RendererStorageRD::light_create(RS::LightType p_type) {
  4463. Light light;
  4464. light.type = p_type;
  4465. light.param[RS::LIGHT_PARAM_ENERGY] = 1.0;
  4466. light.param[RS::LIGHT_PARAM_INDIRECT_ENERGY] = 1.0;
  4467. light.param[RS::LIGHT_PARAM_SPECULAR] = 0.5;
  4468. light.param[RS::LIGHT_PARAM_RANGE] = 1.0;
  4469. light.param[RS::LIGHT_PARAM_SIZE] = 0.0;
  4470. light.param[RS::LIGHT_PARAM_ATTENUATION] = 1.0;
  4471. light.param[RS::LIGHT_PARAM_SPOT_ANGLE] = 45;
  4472. light.param[RS::LIGHT_PARAM_SPOT_ATTENUATION] = 1.0;
  4473. light.param[RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE] = 0;
  4474. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET] = 0.1;
  4475. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET] = 0.3;
  4476. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET] = 0.6;
  4477. light.param[RS::LIGHT_PARAM_SHADOW_FADE_START] = 0.8;
  4478. light.param[RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] = 1.0;
  4479. light.param[RS::LIGHT_PARAM_SHADOW_BIAS] = 0.02;
  4480. light.param[RS::LIGHT_PARAM_SHADOW_BLUR] = 0;
  4481. light.param[RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE] = 20.0;
  4482. light.param[RS::LIGHT_PARAM_SHADOW_VOLUMETRIC_FOG_FADE] = 1.0;
  4483. light.param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS] = 0.05;
  4484. return light_owner.make_rid(light);
  4485. }
  4486. void RendererStorageRD::light_set_color(RID p_light, const Color &p_color) {
  4487. Light *light = light_owner.getornull(p_light);
  4488. ERR_FAIL_COND(!light);
  4489. light->color = p_color;
  4490. }
  4491. void RendererStorageRD::light_set_param(RID p_light, RS::LightParam p_param, float p_value) {
  4492. Light *light = light_owner.getornull(p_light);
  4493. ERR_FAIL_COND(!light);
  4494. ERR_FAIL_INDEX(p_param, RS::LIGHT_PARAM_MAX);
  4495. switch (p_param) {
  4496. case RS::LIGHT_PARAM_RANGE:
  4497. case RS::LIGHT_PARAM_SPOT_ANGLE:
  4498. case RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE:
  4499. case RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET:
  4500. case RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET:
  4501. case RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET:
  4502. case RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS:
  4503. case RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE:
  4504. case RS::LIGHT_PARAM_SHADOW_BIAS: {
  4505. light->version++;
  4506. light->dependency.changed_notify(DEPENDENCY_CHANGED_LIGHT);
  4507. } break;
  4508. default: {
  4509. }
  4510. }
  4511. light->param[p_param] = p_value;
  4512. }
  4513. void RendererStorageRD::light_set_shadow(RID p_light, bool p_enabled) {
  4514. Light *light = light_owner.getornull(p_light);
  4515. ERR_FAIL_COND(!light);
  4516. light->shadow = p_enabled;
  4517. light->version++;
  4518. light->dependency.changed_notify(DEPENDENCY_CHANGED_LIGHT);
  4519. }
  4520. void RendererStorageRD::light_set_shadow_color(RID p_light, const Color &p_color) {
  4521. Light *light = light_owner.getornull(p_light);
  4522. ERR_FAIL_COND(!light);
  4523. light->shadow_color = p_color;
  4524. }
  4525. void RendererStorageRD::light_set_projector(RID p_light, RID p_texture) {
  4526. Light *light = light_owner.getornull(p_light);
  4527. ERR_FAIL_COND(!light);
  4528. if (light->projector == p_texture) {
  4529. return;
  4530. }
  4531. if (light->type != RS::LIGHT_DIRECTIONAL && light->projector.is_valid()) {
  4532. texture_remove_from_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
  4533. }
  4534. light->projector = p_texture;
  4535. if (light->type != RS::LIGHT_DIRECTIONAL && light->projector.is_valid()) {
  4536. texture_add_to_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
  4537. }
  4538. }
  4539. void RendererStorageRD::light_set_negative(RID p_light, bool p_enable) {
  4540. Light *light = light_owner.getornull(p_light);
  4541. ERR_FAIL_COND(!light);
  4542. light->negative = p_enable;
  4543. }
  4544. void RendererStorageRD::light_set_cull_mask(RID p_light, uint32_t p_mask) {
  4545. Light *light = light_owner.getornull(p_light);
  4546. ERR_FAIL_COND(!light);
  4547. light->cull_mask = p_mask;
  4548. light->version++;
  4549. light->dependency.changed_notify(DEPENDENCY_CHANGED_LIGHT);
  4550. }
  4551. void RendererStorageRD::light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) {
  4552. Light *light = light_owner.getornull(p_light);
  4553. ERR_FAIL_COND(!light);
  4554. light->reverse_cull = p_enabled;
  4555. light->version++;
  4556. light->dependency.changed_notify(DEPENDENCY_CHANGED_LIGHT);
  4557. }
  4558. void RendererStorageRD::light_set_bake_mode(RID p_light, RS::LightBakeMode p_bake_mode) {
  4559. Light *light = light_owner.getornull(p_light);
  4560. ERR_FAIL_COND(!light);
  4561. light->bake_mode = p_bake_mode;
  4562. light->version++;
  4563. light->dependency.changed_notify(DEPENDENCY_CHANGED_LIGHT);
  4564. }
  4565. void RendererStorageRD::light_set_max_sdfgi_cascade(RID p_light, uint32_t p_cascade) {
  4566. Light *light = light_owner.getornull(p_light);
  4567. ERR_FAIL_COND(!light);
  4568. light->max_sdfgi_cascade = p_cascade;
  4569. light->version++;
  4570. light->dependency.changed_notify(DEPENDENCY_CHANGED_LIGHT);
  4571. }
  4572. void RendererStorageRD::light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode) {
  4573. Light *light = light_owner.getornull(p_light);
  4574. ERR_FAIL_COND(!light);
  4575. light->omni_shadow_mode = p_mode;
  4576. light->version++;
  4577. light->dependency.changed_notify(DEPENDENCY_CHANGED_LIGHT);
  4578. }
  4579. RS::LightOmniShadowMode RendererStorageRD::light_omni_get_shadow_mode(RID p_light) {
  4580. const Light *light = light_owner.getornull(p_light);
  4581. ERR_FAIL_COND_V(!light, RS::LIGHT_OMNI_SHADOW_CUBE);
  4582. return light->omni_shadow_mode;
  4583. }
  4584. void RendererStorageRD::light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode) {
  4585. Light *light = light_owner.getornull(p_light);
  4586. ERR_FAIL_COND(!light);
  4587. light->directional_shadow_mode = p_mode;
  4588. light->version++;
  4589. light->dependency.changed_notify(DEPENDENCY_CHANGED_LIGHT);
  4590. }
  4591. void RendererStorageRD::light_directional_set_blend_splits(RID p_light, bool p_enable) {
  4592. Light *light = light_owner.getornull(p_light);
  4593. ERR_FAIL_COND(!light);
  4594. light->directional_blend_splits = p_enable;
  4595. light->version++;
  4596. light->dependency.changed_notify(DEPENDENCY_CHANGED_LIGHT);
  4597. }
  4598. bool RendererStorageRD::light_directional_get_blend_splits(RID p_light) const {
  4599. const Light *light = light_owner.getornull(p_light);
  4600. ERR_FAIL_COND_V(!light, false);
  4601. return light->directional_blend_splits;
  4602. }
  4603. void RendererStorageRD::light_directional_set_sky_only(RID p_light, bool p_sky_only) {
  4604. Light *light = light_owner.getornull(p_light);
  4605. ERR_FAIL_COND(!light);
  4606. light->directional_sky_only = p_sky_only;
  4607. }
  4608. bool RendererStorageRD::light_directional_is_sky_only(RID p_light) const {
  4609. const Light *light = light_owner.getornull(p_light);
  4610. ERR_FAIL_COND_V(!light, false);
  4611. return light->directional_sky_only;
  4612. }
  4613. RS::LightDirectionalShadowMode RendererStorageRD::light_directional_get_shadow_mode(RID p_light) {
  4614. const Light *light = light_owner.getornull(p_light);
  4615. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL);
  4616. return light->directional_shadow_mode;
  4617. }
  4618. void RendererStorageRD::light_directional_set_shadow_depth_range_mode(RID p_light, RS::LightDirectionalShadowDepthRangeMode p_range_mode) {
  4619. Light *light = light_owner.getornull(p_light);
  4620. ERR_FAIL_COND(!light);
  4621. light->directional_range_mode = p_range_mode;
  4622. }
  4623. RS::LightDirectionalShadowDepthRangeMode RendererStorageRD::light_directional_get_shadow_depth_range_mode(RID p_light) const {
  4624. const Light *light = light_owner.getornull(p_light);
  4625. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE);
  4626. return light->directional_range_mode;
  4627. }
  4628. uint32_t RendererStorageRD::light_get_max_sdfgi_cascade(RID p_light) {
  4629. const Light *light = light_owner.getornull(p_light);
  4630. ERR_FAIL_COND_V(!light, 0);
  4631. return light->max_sdfgi_cascade;
  4632. }
  4633. RS::LightBakeMode RendererStorageRD::light_get_bake_mode(RID p_light) {
  4634. const Light *light = light_owner.getornull(p_light);
  4635. ERR_FAIL_COND_V(!light, RS::LIGHT_BAKE_DISABLED);
  4636. return light->bake_mode;
  4637. }
  4638. uint64_t RendererStorageRD::light_get_version(RID p_light) const {
  4639. const Light *light = light_owner.getornull(p_light);
  4640. ERR_FAIL_COND_V(!light, 0);
  4641. return light->version;
  4642. }
  4643. AABB RendererStorageRD::light_get_aabb(RID p_light) const {
  4644. const Light *light = light_owner.getornull(p_light);
  4645. ERR_FAIL_COND_V(!light, AABB());
  4646. switch (light->type) {
  4647. case RS::LIGHT_SPOT: {
  4648. float len = light->param[RS::LIGHT_PARAM_RANGE];
  4649. float size = Math::tan(Math::deg2rad(light->param[RS::LIGHT_PARAM_SPOT_ANGLE])) * len;
  4650. return AABB(Vector3(-size, -size, -len), Vector3(size * 2, size * 2, len));
  4651. };
  4652. case RS::LIGHT_OMNI: {
  4653. float r = light->param[RS::LIGHT_PARAM_RANGE];
  4654. return AABB(-Vector3(r, r, r), Vector3(r, r, r) * 2);
  4655. };
  4656. case RS::LIGHT_DIRECTIONAL: {
  4657. return AABB();
  4658. };
  4659. }
  4660. ERR_FAIL_V(AABB());
  4661. }
  4662. /* REFLECTION PROBE */
  4663. RID RendererStorageRD::reflection_probe_create() {
  4664. return reflection_probe_owner.make_rid(ReflectionProbe());
  4665. }
  4666. void RendererStorageRD::reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode) {
  4667. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4668. ERR_FAIL_COND(!reflection_probe);
  4669. reflection_probe->update_mode = p_mode;
  4670. reflection_probe->dependency.changed_notify(DEPENDENCY_CHANGED_REFLECTION_PROBE);
  4671. }
  4672. void RendererStorageRD::reflection_probe_set_intensity(RID p_probe, float p_intensity) {
  4673. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4674. ERR_FAIL_COND(!reflection_probe);
  4675. reflection_probe->intensity = p_intensity;
  4676. }
  4677. void RendererStorageRD::reflection_probe_set_ambient_mode(RID p_probe, RS::ReflectionProbeAmbientMode p_mode) {
  4678. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4679. ERR_FAIL_COND(!reflection_probe);
  4680. reflection_probe->ambient_mode = p_mode;
  4681. }
  4682. void RendererStorageRD::reflection_probe_set_ambient_color(RID p_probe, const Color &p_color) {
  4683. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4684. ERR_FAIL_COND(!reflection_probe);
  4685. reflection_probe->ambient_color = p_color;
  4686. }
  4687. void RendererStorageRD::reflection_probe_set_ambient_energy(RID p_probe, float p_energy) {
  4688. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4689. ERR_FAIL_COND(!reflection_probe);
  4690. reflection_probe->ambient_color_energy = p_energy;
  4691. }
  4692. void RendererStorageRD::reflection_probe_set_max_distance(RID p_probe, float p_distance) {
  4693. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4694. ERR_FAIL_COND(!reflection_probe);
  4695. reflection_probe->max_distance = p_distance;
  4696. reflection_probe->dependency.changed_notify(DEPENDENCY_CHANGED_REFLECTION_PROBE);
  4697. }
  4698. void RendererStorageRD::reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents) {
  4699. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4700. ERR_FAIL_COND(!reflection_probe);
  4701. if (reflection_probe->extents == p_extents) {
  4702. return;
  4703. }
  4704. reflection_probe->extents = p_extents;
  4705. reflection_probe->dependency.changed_notify(DEPENDENCY_CHANGED_REFLECTION_PROBE);
  4706. }
  4707. void RendererStorageRD::reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) {
  4708. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4709. ERR_FAIL_COND(!reflection_probe);
  4710. reflection_probe->origin_offset = p_offset;
  4711. reflection_probe->dependency.changed_notify(DEPENDENCY_CHANGED_REFLECTION_PROBE);
  4712. }
  4713. void RendererStorageRD::reflection_probe_set_as_interior(RID p_probe, bool p_enable) {
  4714. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4715. ERR_FAIL_COND(!reflection_probe);
  4716. reflection_probe->interior = p_enable;
  4717. reflection_probe->dependency.changed_notify(DEPENDENCY_CHANGED_REFLECTION_PROBE);
  4718. }
  4719. void RendererStorageRD::reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) {
  4720. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4721. ERR_FAIL_COND(!reflection_probe);
  4722. reflection_probe->box_projection = p_enable;
  4723. }
  4724. void RendererStorageRD::reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) {
  4725. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4726. ERR_FAIL_COND(!reflection_probe);
  4727. reflection_probe->enable_shadows = p_enable;
  4728. reflection_probe->dependency.changed_notify(DEPENDENCY_CHANGED_REFLECTION_PROBE);
  4729. }
  4730. void RendererStorageRD::reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) {
  4731. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4732. ERR_FAIL_COND(!reflection_probe);
  4733. reflection_probe->cull_mask = p_layers;
  4734. reflection_probe->dependency.changed_notify(DEPENDENCY_CHANGED_REFLECTION_PROBE);
  4735. }
  4736. void RendererStorageRD::reflection_probe_set_resolution(RID p_probe, int p_resolution) {
  4737. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4738. ERR_FAIL_COND(!reflection_probe);
  4739. ERR_FAIL_COND(p_resolution < 32);
  4740. reflection_probe->resolution = p_resolution;
  4741. }
  4742. void RendererStorageRD::reflection_probe_set_lod_threshold(RID p_probe, float p_ratio) {
  4743. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4744. ERR_FAIL_COND(!reflection_probe);
  4745. reflection_probe->lod_threshold = p_ratio;
  4746. reflection_probe->dependency.changed_notify(DEPENDENCY_CHANGED_REFLECTION_PROBE);
  4747. }
  4748. AABB RendererStorageRD::reflection_probe_get_aabb(RID p_probe) const {
  4749. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4750. ERR_FAIL_COND_V(!reflection_probe, AABB());
  4751. AABB aabb;
  4752. aabb.position = -reflection_probe->extents;
  4753. aabb.size = reflection_probe->extents * 2.0;
  4754. return aabb;
  4755. }
  4756. RS::ReflectionProbeUpdateMode RendererStorageRD::reflection_probe_get_update_mode(RID p_probe) const {
  4757. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4758. ERR_FAIL_COND_V(!reflection_probe, RS::REFLECTION_PROBE_UPDATE_ALWAYS);
  4759. return reflection_probe->update_mode;
  4760. }
  4761. uint32_t RendererStorageRD::reflection_probe_get_cull_mask(RID p_probe) const {
  4762. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4763. ERR_FAIL_COND_V(!reflection_probe, 0);
  4764. return reflection_probe->cull_mask;
  4765. }
  4766. Vector3 RendererStorageRD::reflection_probe_get_extents(RID p_probe) const {
  4767. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4768. ERR_FAIL_COND_V(!reflection_probe, Vector3());
  4769. return reflection_probe->extents;
  4770. }
  4771. Vector3 RendererStorageRD::reflection_probe_get_origin_offset(RID p_probe) const {
  4772. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4773. ERR_FAIL_COND_V(!reflection_probe, Vector3());
  4774. return reflection_probe->origin_offset;
  4775. }
  4776. bool RendererStorageRD::reflection_probe_renders_shadows(RID p_probe) const {
  4777. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4778. ERR_FAIL_COND_V(!reflection_probe, false);
  4779. return reflection_probe->enable_shadows;
  4780. }
  4781. float RendererStorageRD::reflection_probe_get_origin_max_distance(RID p_probe) const {
  4782. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4783. ERR_FAIL_COND_V(!reflection_probe, 0);
  4784. return reflection_probe->max_distance;
  4785. }
  4786. float RendererStorageRD::reflection_probe_get_lod_threshold(RID p_probe) const {
  4787. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4788. ERR_FAIL_COND_V(!reflection_probe, 0);
  4789. return reflection_probe->lod_threshold;
  4790. }
  4791. int RendererStorageRD::reflection_probe_get_resolution(RID p_probe) const {
  4792. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4793. ERR_FAIL_COND_V(!reflection_probe, 0);
  4794. return reflection_probe->resolution;
  4795. }
  4796. float RendererStorageRD::reflection_probe_get_intensity(RID p_probe) const {
  4797. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4798. ERR_FAIL_COND_V(!reflection_probe, 0);
  4799. return reflection_probe->intensity;
  4800. }
  4801. bool RendererStorageRD::reflection_probe_is_interior(RID p_probe) const {
  4802. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4803. ERR_FAIL_COND_V(!reflection_probe, false);
  4804. return reflection_probe->interior;
  4805. }
  4806. bool RendererStorageRD::reflection_probe_is_box_projection(RID p_probe) const {
  4807. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4808. ERR_FAIL_COND_V(!reflection_probe, false);
  4809. return reflection_probe->box_projection;
  4810. }
  4811. RS::ReflectionProbeAmbientMode RendererStorageRD::reflection_probe_get_ambient_mode(RID p_probe) const {
  4812. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4813. ERR_FAIL_COND_V(!reflection_probe, RS::REFLECTION_PROBE_AMBIENT_DISABLED);
  4814. return reflection_probe->ambient_mode;
  4815. }
  4816. Color RendererStorageRD::reflection_probe_get_ambient_color(RID p_probe) const {
  4817. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4818. ERR_FAIL_COND_V(!reflection_probe, Color());
  4819. return reflection_probe->ambient_color;
  4820. }
  4821. float RendererStorageRD::reflection_probe_get_ambient_color_energy(RID p_probe) const {
  4822. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4823. ERR_FAIL_COND_V(!reflection_probe, 0);
  4824. return reflection_probe->ambient_color_energy;
  4825. }
  4826. RID RendererStorageRD::decal_create() {
  4827. return decal_owner.make_rid(Decal());
  4828. }
  4829. void RendererStorageRD::decal_set_extents(RID p_decal, const Vector3 &p_extents) {
  4830. Decal *decal = decal_owner.getornull(p_decal);
  4831. ERR_FAIL_COND(!decal);
  4832. decal->extents = p_extents;
  4833. decal->dependency.changed_notify(DEPENDENCY_CHANGED_AABB);
  4834. }
  4835. void RendererStorageRD::decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture) {
  4836. Decal *decal = decal_owner.getornull(p_decal);
  4837. ERR_FAIL_COND(!decal);
  4838. ERR_FAIL_INDEX(p_type, RS::DECAL_TEXTURE_MAX);
  4839. if (decal->textures[p_type] == p_texture) {
  4840. return;
  4841. }
  4842. ERR_FAIL_COND(p_texture.is_valid() && !texture_owner.owns(p_texture));
  4843. if (decal->textures[p_type].is_valid() && texture_owner.owns(decal->textures[p_type])) {
  4844. texture_remove_from_decal_atlas(decal->textures[p_type]);
  4845. }
  4846. decal->textures[p_type] = p_texture;
  4847. if (decal->textures[p_type].is_valid()) {
  4848. texture_add_to_decal_atlas(decal->textures[p_type]);
  4849. }
  4850. decal->dependency.changed_notify(DEPENDENCY_CHANGED_DECAL);
  4851. }
  4852. void RendererStorageRD::decal_set_emission_energy(RID p_decal, float p_energy) {
  4853. Decal *decal = decal_owner.getornull(p_decal);
  4854. ERR_FAIL_COND(!decal);
  4855. decal->emission_energy = p_energy;
  4856. }
  4857. void RendererStorageRD::decal_set_albedo_mix(RID p_decal, float p_mix) {
  4858. Decal *decal = decal_owner.getornull(p_decal);
  4859. ERR_FAIL_COND(!decal);
  4860. decal->albedo_mix = p_mix;
  4861. }
  4862. void RendererStorageRD::decal_set_modulate(RID p_decal, const Color &p_modulate) {
  4863. Decal *decal = decal_owner.getornull(p_decal);
  4864. ERR_FAIL_COND(!decal);
  4865. decal->modulate = p_modulate;
  4866. }
  4867. void RendererStorageRD::decal_set_cull_mask(RID p_decal, uint32_t p_layers) {
  4868. Decal *decal = decal_owner.getornull(p_decal);
  4869. ERR_FAIL_COND(!decal);
  4870. decal->cull_mask = p_layers;
  4871. decal->dependency.changed_notify(DEPENDENCY_CHANGED_AABB);
  4872. }
  4873. void RendererStorageRD::decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length) {
  4874. Decal *decal = decal_owner.getornull(p_decal);
  4875. ERR_FAIL_COND(!decal);
  4876. decal->distance_fade = p_enabled;
  4877. decal->distance_fade_begin = p_begin;
  4878. decal->distance_fade_length = p_length;
  4879. }
  4880. void RendererStorageRD::decal_set_fade(RID p_decal, float p_above, float p_below) {
  4881. Decal *decal = decal_owner.getornull(p_decal);
  4882. ERR_FAIL_COND(!decal);
  4883. decal->upper_fade = p_above;
  4884. decal->lower_fade = p_below;
  4885. }
  4886. void RendererStorageRD::decal_set_normal_fade(RID p_decal, float p_fade) {
  4887. Decal *decal = decal_owner.getornull(p_decal);
  4888. ERR_FAIL_COND(!decal);
  4889. decal->normal_fade = p_fade;
  4890. }
  4891. AABB RendererStorageRD::decal_get_aabb(RID p_decal) const {
  4892. Decal *decal = decal_owner.getornull(p_decal);
  4893. ERR_FAIL_COND_V(!decal, AABB());
  4894. return AABB(-decal->extents, decal->extents * 2.0);
  4895. }
  4896. RID RendererStorageRD::gi_probe_create() {
  4897. return gi_probe_owner.make_rid(GIProbe());
  4898. }
  4899. 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) {
  4900. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4901. ERR_FAIL_COND(!gi_probe);
  4902. if (gi_probe->octree_buffer.is_valid()) {
  4903. RD::get_singleton()->free(gi_probe->octree_buffer);
  4904. RD::get_singleton()->free(gi_probe->data_buffer);
  4905. if (gi_probe->sdf_texture.is_valid()) {
  4906. RD::get_singleton()->free(gi_probe->sdf_texture);
  4907. }
  4908. gi_probe->sdf_texture = RID();
  4909. gi_probe->octree_buffer = RID();
  4910. gi_probe->data_buffer = RID();
  4911. gi_probe->octree_buffer_size = 0;
  4912. gi_probe->data_buffer_size = 0;
  4913. gi_probe->cell_count = 0;
  4914. }
  4915. gi_probe->to_cell_xform = p_to_cell_xform;
  4916. gi_probe->bounds = p_aabb;
  4917. gi_probe->octree_size = p_octree_size;
  4918. gi_probe->level_counts = p_level_counts;
  4919. if (p_octree_cells.size()) {
  4920. ERR_FAIL_COND(p_octree_cells.size() % 32 != 0); //cells size must be a multiple of 32
  4921. uint32_t cell_count = p_octree_cells.size() / 32;
  4922. ERR_FAIL_COND(p_data_cells.size() != (int)cell_count * 16); //see that data size matches
  4923. gi_probe->cell_count = cell_count;
  4924. gi_probe->octree_buffer = RD::get_singleton()->storage_buffer_create(p_octree_cells.size(), p_octree_cells);
  4925. gi_probe->octree_buffer_size = p_octree_cells.size();
  4926. gi_probe->data_buffer = RD::get_singleton()->storage_buffer_create(p_data_cells.size(), p_data_cells);
  4927. gi_probe->data_buffer_size = p_data_cells.size();
  4928. if (p_distance_field.size()) {
  4929. RD::TextureFormat tf;
  4930. tf.format = RD::DATA_FORMAT_R8_UNORM;
  4931. tf.width = gi_probe->octree_size.x;
  4932. tf.height = gi_probe->octree_size.y;
  4933. tf.depth = gi_probe->octree_size.z;
  4934. tf.texture_type = RD::TEXTURE_TYPE_3D;
  4935. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  4936. Vector<Vector<uint8_t>> s;
  4937. s.push_back(p_distance_field);
  4938. gi_probe->sdf_texture = RD::get_singleton()->texture_create(tf, RD::TextureView(), s);
  4939. }
  4940. #if 0
  4941. {
  4942. RD::TextureFormat tf;
  4943. tf.format = RD::DATA_FORMAT_R8_UNORM;
  4944. tf.width = gi_probe->octree_size.x;
  4945. tf.height = gi_probe->octree_size.y;
  4946. tf.depth = gi_probe->octree_size.z;
  4947. tf.type = RD::TEXTURE_TYPE_3D;
  4948. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  4949. tf.shareable_formats.push_back(RD::DATA_FORMAT_R8_UNORM);
  4950. tf.shareable_formats.push_back(RD::DATA_FORMAT_R8_UINT);
  4951. gi_probe->sdf_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4952. }
  4953. RID shared_tex;
  4954. {
  4955. RD::TextureView tv;
  4956. tv.format_override = RD::DATA_FORMAT_R8_UINT;
  4957. shared_tex = RD::get_singleton()->texture_create_shared(tv, gi_probe->sdf_texture);
  4958. }
  4959. //update SDF texture
  4960. Vector<RD::Uniform> uniforms;
  4961. {
  4962. RD::Uniform u;
  4963. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  4964. u.binding = 1;
  4965. u.ids.push_back(gi_probe->octree_buffer);
  4966. uniforms.push_back(u);
  4967. }
  4968. {
  4969. RD::Uniform u;
  4970. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  4971. u.binding = 2;
  4972. u.ids.push_back(gi_probe->data_buffer);
  4973. uniforms.push_back(u);
  4974. }
  4975. {
  4976. RD::Uniform u;
  4977. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  4978. u.binding = 3;
  4979. u.ids.push_back(shared_tex);
  4980. uniforms.push_back(u);
  4981. }
  4982. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_sdf_shader_version_shader, 0);
  4983. {
  4984. uint32_t push_constant[4] = { 0, 0, 0, 0 };
  4985. for (int i = 0; i < gi_probe->level_counts.size() - 1; i++) {
  4986. push_constant[0] += gi_probe->level_counts[i];
  4987. }
  4988. push_constant[1] = push_constant[0] + gi_probe->level_counts[gi_probe->level_counts.size() - 1];
  4989. print_line("offset: " + itos(push_constant[0]));
  4990. print_line("size: " + itos(push_constant[1]));
  4991. //create SDF
  4992. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  4993. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_sdf_shader_pipeline);
  4994. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set, 0);
  4995. RD::get_singleton()->compute_list_set_push_constant(compute_list, push_constant, sizeof(uint32_t) * 4);
  4996. 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);
  4997. RD::get_singleton()->compute_list_end();
  4998. }
  4999. RD::get_singleton()->free(uniform_set);
  5000. RD::get_singleton()->free(shared_tex);
  5001. }
  5002. #endif
  5003. }
  5004. gi_probe->version++;
  5005. gi_probe->data_version++;
  5006. gi_probe->dependency.changed_notify(DEPENDENCY_CHANGED_AABB);
  5007. }
  5008. AABB RendererStorageRD::gi_probe_get_bounds(RID p_gi_probe) const {
  5009. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5010. ERR_FAIL_COND_V(!gi_probe, AABB());
  5011. return gi_probe->bounds;
  5012. }
  5013. Vector3i RendererStorageRD::gi_probe_get_octree_size(RID p_gi_probe) const {
  5014. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5015. ERR_FAIL_COND_V(!gi_probe, Vector3i());
  5016. return gi_probe->octree_size;
  5017. }
  5018. Vector<uint8_t> RendererStorageRD::gi_probe_get_octree_cells(RID p_gi_probe) const {
  5019. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5020. ERR_FAIL_COND_V(!gi_probe, Vector<uint8_t>());
  5021. if (gi_probe->octree_buffer.is_valid()) {
  5022. return RD::get_singleton()->buffer_get_data(gi_probe->octree_buffer);
  5023. }
  5024. return Vector<uint8_t>();
  5025. }
  5026. Vector<uint8_t> RendererStorageRD::gi_probe_get_data_cells(RID p_gi_probe) const {
  5027. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5028. ERR_FAIL_COND_V(!gi_probe, Vector<uint8_t>());
  5029. if (gi_probe->data_buffer.is_valid()) {
  5030. return RD::get_singleton()->buffer_get_data(gi_probe->data_buffer);
  5031. }
  5032. return Vector<uint8_t>();
  5033. }
  5034. Vector<uint8_t> RendererStorageRD::gi_probe_get_distance_field(RID p_gi_probe) const {
  5035. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5036. ERR_FAIL_COND_V(!gi_probe, Vector<uint8_t>());
  5037. if (gi_probe->data_buffer.is_valid()) {
  5038. return RD::get_singleton()->texture_get_data(gi_probe->sdf_texture, 0);
  5039. }
  5040. return Vector<uint8_t>();
  5041. }
  5042. Vector<int> RendererStorageRD::gi_probe_get_level_counts(RID p_gi_probe) const {
  5043. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5044. ERR_FAIL_COND_V(!gi_probe, Vector<int>());
  5045. return gi_probe->level_counts;
  5046. }
  5047. Transform RendererStorageRD::gi_probe_get_to_cell_xform(RID p_gi_probe) const {
  5048. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5049. ERR_FAIL_COND_V(!gi_probe, Transform());
  5050. return gi_probe->to_cell_xform;
  5051. }
  5052. void RendererStorageRD::gi_probe_set_dynamic_range(RID p_gi_probe, float p_range) {
  5053. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5054. ERR_FAIL_COND(!gi_probe);
  5055. gi_probe->dynamic_range = p_range;
  5056. gi_probe->version++;
  5057. }
  5058. float RendererStorageRD::gi_probe_get_dynamic_range(RID p_gi_probe) const {
  5059. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5060. ERR_FAIL_COND_V(!gi_probe, 0);
  5061. return gi_probe->dynamic_range;
  5062. }
  5063. void RendererStorageRD::gi_probe_set_propagation(RID p_gi_probe, float p_range) {
  5064. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5065. ERR_FAIL_COND(!gi_probe);
  5066. gi_probe->propagation = p_range;
  5067. gi_probe->version++;
  5068. }
  5069. float RendererStorageRD::gi_probe_get_propagation(RID p_gi_probe) const {
  5070. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5071. ERR_FAIL_COND_V(!gi_probe, 0);
  5072. return gi_probe->propagation;
  5073. }
  5074. void RendererStorageRD::gi_probe_set_energy(RID p_gi_probe, float p_energy) {
  5075. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5076. ERR_FAIL_COND(!gi_probe);
  5077. gi_probe->energy = p_energy;
  5078. }
  5079. float RendererStorageRD::gi_probe_get_energy(RID p_gi_probe) const {
  5080. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5081. ERR_FAIL_COND_V(!gi_probe, 0);
  5082. return gi_probe->energy;
  5083. }
  5084. void RendererStorageRD::gi_probe_set_ao(RID p_gi_probe, float p_ao) {
  5085. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5086. ERR_FAIL_COND(!gi_probe);
  5087. gi_probe->ao = p_ao;
  5088. }
  5089. float RendererStorageRD::gi_probe_get_ao(RID p_gi_probe) const {
  5090. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5091. ERR_FAIL_COND_V(!gi_probe, 0);
  5092. return gi_probe->ao;
  5093. }
  5094. void RendererStorageRD::gi_probe_set_ao_size(RID p_gi_probe, float p_strength) {
  5095. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5096. ERR_FAIL_COND(!gi_probe);
  5097. gi_probe->ao_size = p_strength;
  5098. }
  5099. float RendererStorageRD::gi_probe_get_ao_size(RID p_gi_probe) const {
  5100. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5101. ERR_FAIL_COND_V(!gi_probe, 0);
  5102. return gi_probe->ao_size;
  5103. }
  5104. void RendererStorageRD::gi_probe_set_bias(RID p_gi_probe, float p_bias) {
  5105. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5106. ERR_FAIL_COND(!gi_probe);
  5107. gi_probe->bias = p_bias;
  5108. }
  5109. float RendererStorageRD::gi_probe_get_bias(RID p_gi_probe) const {
  5110. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5111. ERR_FAIL_COND_V(!gi_probe, 0);
  5112. return gi_probe->bias;
  5113. }
  5114. void RendererStorageRD::gi_probe_set_normal_bias(RID p_gi_probe, float p_normal_bias) {
  5115. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5116. ERR_FAIL_COND(!gi_probe);
  5117. gi_probe->normal_bias = p_normal_bias;
  5118. }
  5119. float RendererStorageRD::gi_probe_get_normal_bias(RID p_gi_probe) const {
  5120. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5121. ERR_FAIL_COND_V(!gi_probe, 0);
  5122. return gi_probe->normal_bias;
  5123. }
  5124. void RendererStorageRD::gi_probe_set_anisotropy_strength(RID p_gi_probe, float p_strength) {
  5125. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5126. ERR_FAIL_COND(!gi_probe);
  5127. gi_probe->anisotropy_strength = p_strength;
  5128. }
  5129. float RendererStorageRD::gi_probe_get_anisotropy_strength(RID p_gi_probe) const {
  5130. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5131. ERR_FAIL_COND_V(!gi_probe, 0);
  5132. return gi_probe->anisotropy_strength;
  5133. }
  5134. void RendererStorageRD::gi_probe_set_interior(RID p_gi_probe, bool p_enable) {
  5135. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5136. ERR_FAIL_COND(!gi_probe);
  5137. gi_probe->interior = p_enable;
  5138. }
  5139. void RendererStorageRD::gi_probe_set_use_two_bounces(RID p_gi_probe, bool p_enable) {
  5140. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5141. ERR_FAIL_COND(!gi_probe);
  5142. gi_probe->use_two_bounces = p_enable;
  5143. gi_probe->version++;
  5144. }
  5145. bool RendererStorageRD::gi_probe_is_using_two_bounces(RID p_gi_probe) const {
  5146. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5147. ERR_FAIL_COND_V(!gi_probe, false);
  5148. return gi_probe->use_two_bounces;
  5149. }
  5150. bool RendererStorageRD::gi_probe_is_interior(RID p_gi_probe) const {
  5151. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5152. ERR_FAIL_COND_V(!gi_probe, 0);
  5153. return gi_probe->interior;
  5154. }
  5155. uint32_t RendererStorageRD::gi_probe_get_version(RID p_gi_probe) {
  5156. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5157. ERR_FAIL_COND_V(!gi_probe, 0);
  5158. return gi_probe->version;
  5159. }
  5160. uint32_t RendererStorageRD::gi_probe_get_data_version(RID p_gi_probe) {
  5161. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5162. ERR_FAIL_COND_V(!gi_probe, 0);
  5163. return gi_probe->data_version;
  5164. }
  5165. RID RendererStorageRD::gi_probe_get_octree_buffer(RID p_gi_probe) const {
  5166. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5167. ERR_FAIL_COND_V(!gi_probe, RID());
  5168. return gi_probe->octree_buffer;
  5169. }
  5170. RID RendererStorageRD::gi_probe_get_data_buffer(RID p_gi_probe) const {
  5171. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5172. ERR_FAIL_COND_V(!gi_probe, RID());
  5173. return gi_probe->data_buffer;
  5174. }
  5175. RID RendererStorageRD::gi_probe_get_sdf_texture(RID p_gi_probe) {
  5176. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  5177. ERR_FAIL_COND_V(!gi_probe, RID());
  5178. return gi_probe->sdf_texture;
  5179. }
  5180. /* LIGHTMAP API */
  5181. RID RendererStorageRD::lightmap_create() {
  5182. return lightmap_owner.make_rid(Lightmap());
  5183. }
  5184. void RendererStorageRD::lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics) {
  5185. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  5186. ERR_FAIL_COND(!lm);
  5187. lightmap_array_version++;
  5188. //erase lightmap users
  5189. if (lm->light_texture.is_valid()) {
  5190. Texture *t = texture_owner.getornull(lm->light_texture);
  5191. if (t) {
  5192. t->lightmap_users.erase(p_lightmap);
  5193. }
  5194. }
  5195. Texture *t = texture_owner.getornull(p_light);
  5196. lm->light_texture = p_light;
  5197. lm->uses_spherical_harmonics = p_uses_spherical_haromics;
  5198. RID default_2d_array = default_rd_textures[DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE];
  5199. if (!t) {
  5200. if (using_lightmap_array) {
  5201. if (lm->array_index >= 0) {
  5202. lightmap_textures.write[lm->array_index] = default_2d_array;
  5203. lm->array_index = -1;
  5204. }
  5205. }
  5206. return;
  5207. }
  5208. t->lightmap_users.insert(p_lightmap);
  5209. if (using_lightmap_array) {
  5210. if (lm->array_index < 0) {
  5211. //not in array, try to put in array
  5212. for (int i = 0; i < lightmap_textures.size(); i++) {
  5213. if (lightmap_textures[i] == default_2d_array) {
  5214. lm->array_index = i;
  5215. break;
  5216. }
  5217. }
  5218. }
  5219. 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.");
  5220. lightmap_textures.write[lm->array_index] = t->rd_texture;
  5221. }
  5222. }
  5223. void RendererStorageRD::lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds) {
  5224. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  5225. ERR_FAIL_COND(!lm);
  5226. lm->bounds = p_bounds;
  5227. }
  5228. void RendererStorageRD::lightmap_set_probe_interior(RID p_lightmap, bool p_interior) {
  5229. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  5230. ERR_FAIL_COND(!lm);
  5231. lm->interior = p_interior;
  5232. }
  5233. 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) {
  5234. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  5235. ERR_FAIL_COND(!lm);
  5236. if (p_points.size()) {
  5237. ERR_FAIL_COND(p_points.size() * 9 != p_point_sh.size());
  5238. ERR_FAIL_COND((p_tetrahedra.size() % 4) != 0);
  5239. ERR_FAIL_COND((p_bsp_tree.size() % 6) != 0);
  5240. }
  5241. lm->points = p_points;
  5242. lm->bsp_tree = p_bsp_tree;
  5243. lm->point_sh = p_point_sh;
  5244. lm->tetrahedra = p_tetrahedra;
  5245. }
  5246. PackedVector3Array RendererStorageRD::lightmap_get_probe_capture_points(RID p_lightmap) const {
  5247. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  5248. ERR_FAIL_COND_V(!lm, PackedVector3Array());
  5249. return lm->points;
  5250. }
  5251. PackedColorArray RendererStorageRD::lightmap_get_probe_capture_sh(RID p_lightmap) const {
  5252. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  5253. ERR_FAIL_COND_V(!lm, PackedColorArray());
  5254. return lm->point_sh;
  5255. }
  5256. PackedInt32Array RendererStorageRD::lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const {
  5257. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  5258. ERR_FAIL_COND_V(!lm, PackedInt32Array());
  5259. return lm->tetrahedra;
  5260. }
  5261. PackedInt32Array RendererStorageRD::lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const {
  5262. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  5263. ERR_FAIL_COND_V(!lm, PackedInt32Array());
  5264. return lm->bsp_tree;
  5265. }
  5266. void RendererStorageRD::lightmap_set_probe_capture_update_speed(float p_speed) {
  5267. lightmap_probe_capture_update_speed = p_speed;
  5268. }
  5269. void RendererStorageRD::lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh) {
  5270. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  5271. ERR_FAIL_COND(!lm);
  5272. for (int i = 0; i < 9; i++) {
  5273. r_sh[i] = Color(0, 0, 0, 0);
  5274. }
  5275. if (!lm->points.size() || !lm->bsp_tree.size() || !lm->tetrahedra.size()) {
  5276. return;
  5277. }
  5278. static_assert(sizeof(Lightmap::BSP) == 24);
  5279. const Lightmap::BSP *bsp = (const Lightmap::BSP *)lm->bsp_tree.ptr();
  5280. int32_t node = 0;
  5281. while (node >= 0) {
  5282. if (Plane(bsp[node].plane[0], bsp[node].plane[1], bsp[node].plane[2], bsp[node].plane[3]).is_point_over(p_point)) {
  5283. #ifdef DEBUG_ENABLED
  5284. ERR_FAIL_COND(bsp[node].over >= 0 && bsp[node].over < node);
  5285. #endif
  5286. node = bsp[node].over;
  5287. } else {
  5288. #ifdef DEBUG_ENABLED
  5289. ERR_FAIL_COND(bsp[node].under >= 0 && bsp[node].under < node);
  5290. #endif
  5291. node = bsp[node].under;
  5292. }
  5293. }
  5294. if (node == Lightmap::BSP::EMPTY_LEAF) {
  5295. return; //nothing could be done
  5296. }
  5297. node = ABS(node) - 1;
  5298. uint32_t *tetrahedron = (uint32_t *)&lm->tetrahedra[node * 4];
  5299. Vector3 points[4] = { lm->points[tetrahedron[0]], lm->points[tetrahedron[1]], lm->points[tetrahedron[2]], lm->points[tetrahedron[3]] };
  5300. 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] };
  5301. Color barycentric = Geometry3D::tetrahedron_get_barycentric_coords(points[0], points[1], points[2], points[3], p_point);
  5302. for (int i = 0; i < 4; i++) {
  5303. float c = CLAMP(barycentric[i], 0.0, 1.0);
  5304. for (int j = 0; j < 9; j++) {
  5305. r_sh[j] += sh_colors[i][j] * c;
  5306. }
  5307. }
  5308. }
  5309. bool RendererStorageRD::lightmap_is_interior(RID p_lightmap) const {
  5310. const Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  5311. ERR_FAIL_COND_V(!lm, false);
  5312. return lm->interior;
  5313. }
  5314. AABB RendererStorageRD::lightmap_get_aabb(RID p_lightmap) const {
  5315. const Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  5316. ERR_FAIL_COND_V(!lm, AABB());
  5317. return lm->bounds;
  5318. }
  5319. /* RENDER TARGET API */
  5320. void RendererStorageRD::_clear_render_target(RenderTarget *rt) {
  5321. //free in reverse dependency order
  5322. if (rt->framebuffer.is_valid()) {
  5323. RD::get_singleton()->free(rt->framebuffer);
  5324. rt->framebuffer_uniform_set = RID(); //chain deleted
  5325. }
  5326. if (rt->color.is_valid()) {
  5327. RD::get_singleton()->free(rt->color);
  5328. }
  5329. if (rt->backbuffer.is_valid()) {
  5330. RD::get_singleton()->free(rt->backbuffer);
  5331. rt->backbuffer = RID();
  5332. for (int i = 0; i < rt->backbuffer_mipmaps.size(); i++) {
  5333. //just erase copies, since the rest are erased by dependency
  5334. RD::get_singleton()->free(rt->backbuffer_mipmaps[i].mipmap_copy);
  5335. }
  5336. rt->backbuffer_mipmaps.clear();
  5337. rt->backbuffer_uniform_set = RID(); //chain deleted
  5338. }
  5339. _render_target_clear_sdf(rt);
  5340. rt->framebuffer = RID();
  5341. rt->color = RID();
  5342. }
  5343. void RendererStorageRD::_update_render_target(RenderTarget *rt) {
  5344. if (rt->texture.is_null()) {
  5345. //create a placeholder until updated
  5346. rt->texture = texture_2d_placeholder_create();
  5347. Texture *tex = texture_owner.getornull(rt->texture);
  5348. tex->is_render_target = true;
  5349. }
  5350. _clear_render_target(rt);
  5351. if (rt->size.width == 0 || rt->size.height == 0) {
  5352. return;
  5353. }
  5354. //until we implement support for HDR monitors (and render target is attached to screen), this is enough.
  5355. rt->color_format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  5356. rt->color_format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  5357. rt->image_format = rt->flags[RENDER_TARGET_TRANSPARENT] ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8;
  5358. RD::TextureFormat rd_format;
  5359. RD::TextureView rd_view;
  5360. { //attempt register
  5361. rd_format.format = rt->color_format;
  5362. rd_format.width = rt->size.width;
  5363. rd_format.height = rt->size.height;
  5364. rd_format.depth = 1;
  5365. rd_format.array_layers = 1;
  5366. rd_format.mipmaps = 1;
  5367. rd_format.texture_type = RD::TEXTURE_TYPE_2D;
  5368. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  5369. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  5370. rd_format.shareable_formats.push_back(rt->color_format);
  5371. rd_format.shareable_formats.push_back(rt->color_format_srgb);
  5372. }
  5373. rt->color = RD::get_singleton()->texture_create(rd_format, rd_view);
  5374. ERR_FAIL_COND(rt->color.is_null());
  5375. Vector<RID> fb_textures;
  5376. fb_textures.push_back(rt->color);
  5377. rt->framebuffer = RD::get_singleton()->framebuffer_create(fb_textures);
  5378. if (rt->framebuffer.is_null()) {
  5379. _clear_render_target(rt);
  5380. ERR_FAIL_COND(rt->framebuffer.is_null());
  5381. }
  5382. { //update texture
  5383. Texture *tex = texture_owner.getornull(rt->texture);
  5384. //free existing textures
  5385. if (RD::get_singleton()->texture_is_valid(tex->rd_texture)) {
  5386. RD::get_singleton()->free(tex->rd_texture);
  5387. }
  5388. if (RD::get_singleton()->texture_is_valid(tex->rd_texture_srgb)) {
  5389. RD::get_singleton()->free(tex->rd_texture_srgb);
  5390. }
  5391. tex->rd_texture = RID();
  5392. tex->rd_texture_srgb = RID();
  5393. //create shared textures to the color buffer,
  5394. //so transparent can be supported
  5395. RD::TextureView view;
  5396. view.format_override = rt->color_format;
  5397. if (!rt->flags[RENDER_TARGET_TRANSPARENT]) {
  5398. view.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  5399. }
  5400. tex->rd_texture = RD::get_singleton()->texture_create_shared(view, rt->color);
  5401. if (rt->color_format_srgb != RD::DATA_FORMAT_MAX) {
  5402. view.format_override = rt->color_format_srgb;
  5403. tex->rd_texture_srgb = RD::get_singleton()->texture_create_shared(view, rt->color);
  5404. }
  5405. tex->rd_view = view;
  5406. tex->width = rt->size.width;
  5407. tex->height = rt->size.height;
  5408. tex->width_2d = rt->size.width;
  5409. tex->height_2d = rt->size.height;
  5410. tex->rd_format = rt->color_format;
  5411. tex->rd_format_srgb = rt->color_format_srgb;
  5412. tex->format = rt->image_format;
  5413. Vector<RID> proxies = tex->proxies; //make a copy, since update may change it
  5414. for (int i = 0; i < proxies.size(); i++) {
  5415. texture_proxy_update(proxies[i], rt->texture);
  5416. }
  5417. }
  5418. }
  5419. void RendererStorageRD::_create_render_target_backbuffer(RenderTarget *rt) {
  5420. ERR_FAIL_COND(rt->backbuffer.is_valid());
  5421. uint32_t mipmaps_required = Image::get_image_required_mipmaps(rt->size.width, rt->size.height, Image::FORMAT_RGBA8);
  5422. RD::TextureFormat tf;
  5423. tf.format = rt->color_format;
  5424. tf.width = rt->size.width;
  5425. tf.height = rt->size.height;
  5426. tf.texture_type = RD::TEXTURE_TYPE_2D;
  5427. 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;
  5428. tf.mipmaps = mipmaps_required;
  5429. rt->backbuffer = RD::get_singleton()->texture_create(tf, RD::TextureView());
  5430. rt->backbuffer_mipmap0 = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->backbuffer, 0, 0);
  5431. {
  5432. Vector<RID> fb_tex;
  5433. fb_tex.push_back(rt->backbuffer_mipmap0);
  5434. rt->backbuffer_fb = RD::get_singleton()->framebuffer_create(fb_tex);
  5435. }
  5436. if (rt->framebuffer_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(rt->framebuffer_uniform_set)) {
  5437. //the new one will require the backbuffer.
  5438. RD::get_singleton()->free(rt->framebuffer_uniform_set);
  5439. rt->framebuffer_uniform_set = RID();
  5440. }
  5441. //create mipmaps
  5442. for (uint32_t i = 1; i < mipmaps_required; i++) {
  5443. RenderTarget::BackbufferMipmap mm;
  5444. {
  5445. mm.mipmap = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->backbuffer, 0, i);
  5446. }
  5447. {
  5448. Size2 mm_size = Image::get_image_mipmap_size(tf.width, tf.height, Image::FORMAT_RGBA8, i);
  5449. RD::TextureFormat mmtf = tf;
  5450. mmtf.width = mm_size.width;
  5451. mmtf.height = mm_size.height;
  5452. mmtf.mipmaps = 1;
  5453. mm.mipmap_copy = RD::get_singleton()->texture_create(mmtf, RD::TextureView());
  5454. }
  5455. rt->backbuffer_mipmaps.push_back(mm);
  5456. }
  5457. }
  5458. RID RendererStorageRD::render_target_create() {
  5459. RenderTarget render_target;
  5460. render_target.was_used = false;
  5461. render_target.clear_requested = false;
  5462. for (int i = 0; i < RENDER_TARGET_FLAG_MAX; i++) {
  5463. render_target.flags[i] = false;
  5464. }
  5465. _update_render_target(&render_target);
  5466. return render_target_owner.make_rid(render_target);
  5467. }
  5468. void RendererStorageRD::render_target_set_position(RID p_render_target, int p_x, int p_y) {
  5469. //unused for this render target
  5470. }
  5471. void RendererStorageRD::render_target_set_size(RID p_render_target, int p_width, int p_height) {
  5472. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5473. ERR_FAIL_COND(!rt);
  5474. rt->size.x = p_width;
  5475. rt->size.y = p_height;
  5476. _update_render_target(rt);
  5477. }
  5478. RID RendererStorageRD::render_target_get_texture(RID p_render_target) {
  5479. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5480. ERR_FAIL_COND_V(!rt, RID());
  5481. return rt->texture;
  5482. }
  5483. void RendererStorageRD::render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id) {
  5484. }
  5485. void RendererStorageRD::render_target_set_flag(RID p_render_target, RenderTargetFlags p_flag, bool p_value) {
  5486. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5487. ERR_FAIL_COND(!rt);
  5488. rt->flags[p_flag] = p_value;
  5489. _update_render_target(rt);
  5490. }
  5491. bool RendererStorageRD::render_target_was_used(RID p_render_target) {
  5492. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5493. ERR_FAIL_COND_V(!rt, false);
  5494. return rt->was_used;
  5495. }
  5496. void RendererStorageRD::render_target_set_as_unused(RID p_render_target) {
  5497. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5498. ERR_FAIL_COND(!rt);
  5499. rt->was_used = false;
  5500. }
  5501. Size2 RendererStorageRD::render_target_get_size(RID p_render_target) {
  5502. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5503. ERR_FAIL_COND_V(!rt, Size2());
  5504. return rt->size;
  5505. }
  5506. RID RendererStorageRD::render_target_get_rd_framebuffer(RID p_render_target) {
  5507. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5508. ERR_FAIL_COND_V(!rt, RID());
  5509. return rt->framebuffer;
  5510. }
  5511. RID RendererStorageRD::render_target_get_rd_texture(RID p_render_target) {
  5512. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5513. ERR_FAIL_COND_V(!rt, RID());
  5514. return rt->color;
  5515. }
  5516. RID RendererStorageRD::render_target_get_rd_backbuffer(RID p_render_target) {
  5517. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5518. ERR_FAIL_COND_V(!rt, RID());
  5519. return rt->backbuffer;
  5520. }
  5521. RID RendererStorageRD::render_target_get_rd_backbuffer_framebuffer(RID p_render_target) {
  5522. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5523. ERR_FAIL_COND_V(!rt, RID());
  5524. if (!rt->backbuffer.is_valid()) {
  5525. _create_render_target_backbuffer(rt);
  5526. }
  5527. return rt->backbuffer_fb;
  5528. }
  5529. void RendererStorageRD::render_target_request_clear(RID p_render_target, const Color &p_clear_color) {
  5530. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5531. ERR_FAIL_COND(!rt);
  5532. rt->clear_requested = true;
  5533. rt->clear_color = p_clear_color;
  5534. }
  5535. bool RendererStorageRD::render_target_is_clear_requested(RID p_render_target) {
  5536. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5537. ERR_FAIL_COND_V(!rt, false);
  5538. return rt->clear_requested;
  5539. }
  5540. Color RendererStorageRD::render_target_get_clear_request_color(RID p_render_target) {
  5541. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5542. ERR_FAIL_COND_V(!rt, Color());
  5543. return rt->clear_color;
  5544. }
  5545. void RendererStorageRD::render_target_disable_clear_request(RID p_render_target) {
  5546. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5547. ERR_FAIL_COND(!rt);
  5548. rt->clear_requested = false;
  5549. }
  5550. void RendererStorageRD::render_target_do_clear_request(RID p_render_target) {
  5551. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5552. ERR_FAIL_COND(!rt);
  5553. if (!rt->clear_requested) {
  5554. return;
  5555. }
  5556. Vector<Color> clear_colors;
  5557. clear_colors.push_back(rt->clear_color);
  5558. 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);
  5559. RD::get_singleton()->draw_list_end();
  5560. rt->clear_requested = false;
  5561. }
  5562. void RendererStorageRD::render_target_set_sdf_size_and_scale(RID p_render_target, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale) {
  5563. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5564. ERR_FAIL_COND(!rt);
  5565. if (rt->sdf_oversize == p_size && rt->sdf_scale == p_scale) {
  5566. return;
  5567. }
  5568. rt->sdf_oversize = p_size;
  5569. rt->sdf_scale = p_scale;
  5570. _render_target_clear_sdf(rt);
  5571. }
  5572. Rect2i RendererStorageRD::_render_target_get_sdf_rect(const RenderTarget *rt) const {
  5573. Size2i margin;
  5574. int scale;
  5575. switch (rt->sdf_oversize) {
  5576. case RS::VIEWPORT_SDF_OVERSIZE_100_PERCENT: {
  5577. scale = 100;
  5578. } break;
  5579. case RS::VIEWPORT_SDF_OVERSIZE_120_PERCENT: {
  5580. scale = 120;
  5581. } break;
  5582. case RS::VIEWPORT_SDF_OVERSIZE_150_PERCENT: {
  5583. scale = 150;
  5584. } break;
  5585. case RS::VIEWPORT_SDF_OVERSIZE_200_PERCENT: {
  5586. scale = 200;
  5587. } break;
  5588. default: {
  5589. }
  5590. }
  5591. margin = (rt->size * scale / 100) - rt->size;
  5592. Rect2i r(Vector2i(), rt->size);
  5593. r.position -= margin;
  5594. r.size += margin * 2;
  5595. return r;
  5596. }
  5597. Rect2i RendererStorageRD::render_target_get_sdf_rect(RID p_render_target) const {
  5598. const RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5599. ERR_FAIL_COND_V(!rt, Rect2i());
  5600. return _render_target_get_sdf_rect(rt);
  5601. }
  5602. RID RendererStorageRD::render_target_get_sdf_texture(RID p_render_target) {
  5603. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5604. ERR_FAIL_COND_V(!rt, RID());
  5605. if (rt->sdf_buffer_read.is_null()) {
  5606. // no texture, create a dummy one for the 2D uniform set
  5607. RD::TextureFormat tformat;
  5608. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  5609. tformat.width = 4;
  5610. tformat.height = 4;
  5611. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT;
  5612. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  5613. Vector<uint8_t> pv;
  5614. pv.resize(16 * 4);
  5615. zeromem(pv.ptrw(), 16 * 4);
  5616. Vector<Vector<uint8_t>> vpv;
  5617. rt->sdf_buffer_read = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  5618. }
  5619. return rt->sdf_buffer_read;
  5620. }
  5621. void RendererStorageRD::_render_target_allocate_sdf(RenderTarget *rt) {
  5622. ERR_FAIL_COND(rt->sdf_buffer_write_fb.is_valid());
  5623. if (rt->sdf_buffer_read.is_valid()) {
  5624. RD::get_singleton()->free(rt->sdf_buffer_read);
  5625. rt->sdf_buffer_read = RID();
  5626. }
  5627. Size2i size = _render_target_get_sdf_rect(rt).size;
  5628. RD::TextureFormat tformat;
  5629. tformat.format = RD::DATA_FORMAT_R8_UNORM;
  5630. tformat.width = size.width;
  5631. tformat.height = size.height;
  5632. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  5633. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  5634. rt->sdf_buffer_write = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  5635. {
  5636. Vector<RID> write_fb;
  5637. write_fb.push_back(rt->sdf_buffer_write);
  5638. rt->sdf_buffer_write_fb = RD::get_singleton()->framebuffer_create(write_fb);
  5639. }
  5640. int scale;
  5641. switch (rt->sdf_scale) {
  5642. case RS::VIEWPORT_SDF_SCALE_100_PERCENT: {
  5643. scale = 100;
  5644. } break;
  5645. case RS::VIEWPORT_SDF_SCALE_50_PERCENT: {
  5646. scale = 50;
  5647. } break;
  5648. case RS::VIEWPORT_SDF_SCALE_25_PERCENT: {
  5649. scale = 25;
  5650. } break;
  5651. default: {
  5652. scale = 100;
  5653. } break;
  5654. }
  5655. rt->process_size = size * scale / 100;
  5656. rt->process_size.x = MAX(rt->process_size.x, 1);
  5657. rt->process_size.y = MAX(rt->process_size.y, 1);
  5658. tformat.format = RD::DATA_FORMAT_R16G16_UINT;
  5659. tformat.width = rt->process_size.width;
  5660. tformat.height = rt->process_size.height;
  5661. tformat.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  5662. rt->sdf_buffer_process[0] = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  5663. rt->sdf_buffer_process[1] = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  5664. tformat.format = RD::DATA_FORMAT_R16_UNORM;
  5665. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  5666. rt->sdf_buffer_read = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  5667. {
  5668. Vector<RD::Uniform> uniforms;
  5669. {
  5670. RD::Uniform u;
  5671. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  5672. u.binding = 1;
  5673. u.ids.push_back(rt->sdf_buffer_write);
  5674. uniforms.push_back(u);
  5675. }
  5676. {
  5677. RD::Uniform u;
  5678. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  5679. u.binding = 2;
  5680. u.ids.push_back(rt->sdf_buffer_read);
  5681. uniforms.push_back(u);
  5682. }
  5683. {
  5684. RD::Uniform u;
  5685. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  5686. u.binding = 3;
  5687. u.ids.push_back(rt->sdf_buffer_process[0]);
  5688. uniforms.push_back(u);
  5689. }
  5690. {
  5691. RD::Uniform u;
  5692. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  5693. u.binding = 4;
  5694. u.ids.push_back(rt->sdf_buffer_process[1]);
  5695. uniforms.push_back(u);
  5696. }
  5697. 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);
  5698. SWAP(uniforms.write[2].ids.write[0], uniforms.write[3].ids.write[0]);
  5699. 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);
  5700. }
  5701. }
  5702. void RendererStorageRD::_render_target_clear_sdf(RenderTarget *rt) {
  5703. if (rt->sdf_buffer_read.is_valid()) {
  5704. RD::get_singleton()->free(rt->sdf_buffer_read);
  5705. rt->sdf_buffer_read = RID();
  5706. }
  5707. if (rt->sdf_buffer_write_fb.is_valid()) {
  5708. RD::get_singleton()->free(rt->sdf_buffer_write);
  5709. RD::get_singleton()->free(rt->sdf_buffer_process[0]);
  5710. RD::get_singleton()->free(rt->sdf_buffer_process[1]);
  5711. rt->sdf_buffer_write = RID();
  5712. rt->sdf_buffer_write_fb = RID();
  5713. rt->sdf_buffer_process[0] = RID();
  5714. rt->sdf_buffer_process[1] = RID();
  5715. rt->sdf_buffer_process_uniform_sets[0] = RID();
  5716. rt->sdf_buffer_process_uniform_sets[1] = RID();
  5717. }
  5718. }
  5719. RID RendererStorageRD::render_target_get_sdf_framebuffer(RID p_render_target) {
  5720. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5721. ERR_FAIL_COND_V(!rt, RID());
  5722. if (rt->sdf_buffer_write_fb.is_null()) {
  5723. _render_target_allocate_sdf(rt);
  5724. }
  5725. return rt->sdf_buffer_write_fb;
  5726. }
  5727. void RendererStorageRD::render_target_sdf_process(RID p_render_target) {
  5728. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5729. ERR_FAIL_COND(!rt);
  5730. ERR_FAIL_COND(rt->sdf_buffer_write_fb.is_null());
  5731. RenderTargetSDF::PushConstant push_constant;
  5732. Rect2i r = _render_target_get_sdf_rect(rt);
  5733. push_constant.size[0] = r.size.width;
  5734. push_constant.size[1] = r.size.height;
  5735. push_constant.stride = 0;
  5736. push_constant.shift = 0;
  5737. push_constant.base_size[0] = r.size.width;
  5738. push_constant.base_size[1] = r.size.height;
  5739. bool shrink = false;
  5740. switch (rt->sdf_scale) {
  5741. case RS::VIEWPORT_SDF_SCALE_50_PERCENT: {
  5742. push_constant.size[0] >>= 1;
  5743. push_constant.size[1] >>= 1;
  5744. push_constant.shift = 1;
  5745. shrink = true;
  5746. } break;
  5747. case RS::VIEWPORT_SDF_SCALE_25_PERCENT: {
  5748. push_constant.size[0] >>= 2;
  5749. push_constant.size[1] >>= 2;
  5750. push_constant.shift = 2;
  5751. shrink = true;
  5752. } break;
  5753. default: {
  5754. };
  5755. }
  5756. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  5757. /* Load */
  5758. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, rt_sdf.pipelines[shrink ? RenderTargetSDF::SHADER_LOAD_SHRINK : RenderTargetSDF::SHADER_LOAD]);
  5759. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rt->sdf_buffer_process_uniform_sets[1], 0); //fill [0]
  5760. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(RenderTargetSDF::PushConstant));
  5761. RD::get_singleton()->compute_list_dispatch_threads(compute_list, push_constant.size[0], push_constant.size[1], 1, 8, 8, 1);
  5762. /* Process */
  5763. int stride = nearest_power_of_2_templated(MAX(push_constant.size[0], push_constant.size[1]) / 2);
  5764. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, rt_sdf.pipelines[RenderTargetSDF::SHADER_PROCESS]);
  5765. RD::get_singleton()->compute_list_add_barrier(compute_list);
  5766. bool swap = false;
  5767. //jumpflood
  5768. while (stride > 0) {
  5769. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rt->sdf_buffer_process_uniform_sets[swap ? 1 : 0], 0);
  5770. push_constant.stride = stride;
  5771. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(RenderTargetSDF::PushConstant));
  5772. RD::get_singleton()->compute_list_dispatch_threads(compute_list, push_constant.size[0], push_constant.size[1], 1, 8, 8, 1);
  5773. stride /= 2;
  5774. swap = !swap;
  5775. RD::get_singleton()->compute_list_add_barrier(compute_list);
  5776. }
  5777. /* Store */
  5778. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, rt_sdf.pipelines[shrink ? RenderTargetSDF::SHADER_STORE_SHRINK : RenderTargetSDF::SHADER_STORE]);
  5779. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rt->sdf_buffer_process_uniform_sets[swap ? 1 : 0], 0);
  5780. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(RenderTargetSDF::PushConstant));
  5781. RD::get_singleton()->compute_list_dispatch_threads(compute_list, push_constant.size[0], push_constant.size[1], 1, 8, 8, 1);
  5782. RD::get_singleton()->compute_list_end();
  5783. }
  5784. void RendererStorageRD::render_target_copy_to_back_buffer(RID p_render_target, const Rect2i &p_region, bool p_gen_mipmaps) {
  5785. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5786. ERR_FAIL_COND(!rt);
  5787. if (!rt->backbuffer.is_valid()) {
  5788. _create_render_target_backbuffer(rt);
  5789. }
  5790. Rect2i region;
  5791. if (p_region == Rect2i()) {
  5792. region.size = rt->size;
  5793. } else {
  5794. region = Rect2i(Size2i(), rt->size).intersection(p_region);
  5795. if (region.size == Size2i()) {
  5796. return; //nothing to do
  5797. }
  5798. }
  5799. //single texture copy for backbuffer
  5800. //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);
  5801. effects.copy_to_rect(rt->color, rt->backbuffer_mipmap0, region, false, false, false, true, true);
  5802. if (!p_gen_mipmaps) {
  5803. return;
  5804. }
  5805. //then mipmap blur
  5806. RID prev_texture = rt->color; //use color, not backbuffer, as bb has mipmaps.
  5807. for (int i = 0; i < rt->backbuffer_mipmaps.size(); i++) {
  5808. region.position.x >>= 1;
  5809. region.position.y >>= 1;
  5810. region.size.x = MAX(1, region.size.x >> 1);
  5811. region.size.y = MAX(1, region.size.y >> 1);
  5812. const RenderTarget::BackbufferMipmap &mm = rt->backbuffer_mipmaps[i];
  5813. effects.gaussian_blur(prev_texture, mm.mipmap, mm.mipmap_copy, region, true);
  5814. prev_texture = mm.mipmap;
  5815. }
  5816. }
  5817. void RendererStorageRD::render_target_clear_back_buffer(RID p_render_target, const Rect2i &p_region, const Color &p_color) {
  5818. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5819. ERR_FAIL_COND(!rt);
  5820. if (!rt->backbuffer.is_valid()) {
  5821. _create_render_target_backbuffer(rt);
  5822. }
  5823. Rect2i region;
  5824. if (p_region == Rect2i()) {
  5825. region.size = rt->size;
  5826. } else {
  5827. region = Rect2i(Size2i(), rt->size).intersection(p_region);
  5828. if (region.size == Size2i()) {
  5829. return; //nothing to do
  5830. }
  5831. }
  5832. //single texture copy for backbuffer
  5833. effects.set_color(rt->backbuffer_mipmap0, p_color, region, true);
  5834. }
  5835. void RendererStorageRD::render_target_gen_back_buffer_mipmaps(RID p_render_target, const Rect2i &p_region) {
  5836. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5837. ERR_FAIL_COND(!rt);
  5838. if (!rt->backbuffer.is_valid()) {
  5839. _create_render_target_backbuffer(rt);
  5840. }
  5841. Rect2i region;
  5842. if (p_region == Rect2i()) {
  5843. region.size = rt->size;
  5844. } else {
  5845. region = Rect2i(Size2i(), rt->size).intersection(p_region);
  5846. if (region.size == Size2i()) {
  5847. return; //nothing to do
  5848. }
  5849. }
  5850. //then mipmap blur
  5851. RID prev_texture = rt->backbuffer_mipmap0;
  5852. for (int i = 0; i < rt->backbuffer_mipmaps.size(); i++) {
  5853. region.position.x >>= 1;
  5854. region.position.y >>= 1;
  5855. region.size.x = MAX(1, region.size.x >> 1);
  5856. region.size.y = MAX(1, region.size.y >> 1);
  5857. const RenderTarget::BackbufferMipmap &mm = rt->backbuffer_mipmaps[i];
  5858. effects.gaussian_blur(prev_texture, mm.mipmap, mm.mipmap_copy, region, true);
  5859. prev_texture = mm.mipmap;
  5860. }
  5861. }
  5862. RID RendererStorageRD::render_target_get_framebuffer_uniform_set(RID p_render_target) {
  5863. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5864. ERR_FAIL_COND_V(!rt, RID());
  5865. return rt->framebuffer_uniform_set;
  5866. }
  5867. RID RendererStorageRD::render_target_get_backbuffer_uniform_set(RID p_render_target) {
  5868. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5869. ERR_FAIL_COND_V(!rt, RID());
  5870. return rt->backbuffer_uniform_set;
  5871. }
  5872. void RendererStorageRD::render_target_set_framebuffer_uniform_set(RID p_render_target, RID p_uniform_set) {
  5873. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5874. ERR_FAIL_COND(!rt);
  5875. rt->framebuffer_uniform_set = p_uniform_set;
  5876. }
  5877. void RendererStorageRD::render_target_set_backbuffer_uniform_set(RID p_render_target, RID p_uniform_set) {
  5878. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5879. ERR_FAIL_COND(!rt);
  5880. rt->backbuffer_uniform_set = p_uniform_set;
  5881. }
  5882. void RendererStorageRD::base_update_dependency(RID p_base, DependencyTracker *p_instance) {
  5883. if (mesh_owner.owns(p_base)) {
  5884. Mesh *mesh = mesh_owner.getornull(p_base);
  5885. p_instance->update_dependency(&mesh->dependency);
  5886. } else if (multimesh_owner.owns(p_base)) {
  5887. MultiMesh *multimesh = multimesh_owner.getornull(p_base);
  5888. p_instance->update_dependency(&multimesh->dependency);
  5889. if (multimesh->mesh.is_valid()) {
  5890. base_update_dependency(multimesh->mesh, p_instance);
  5891. }
  5892. } else if (reflection_probe_owner.owns(p_base)) {
  5893. ReflectionProbe *rp = reflection_probe_owner.getornull(p_base);
  5894. p_instance->update_dependency(&rp->dependency);
  5895. } else if (decal_owner.owns(p_base)) {
  5896. Decal *decal = decal_owner.getornull(p_base);
  5897. p_instance->update_dependency(&decal->dependency);
  5898. } else if (gi_probe_owner.owns(p_base)) {
  5899. GIProbe *gip = gi_probe_owner.getornull(p_base);
  5900. p_instance->update_dependency(&gip->dependency);
  5901. } else if (lightmap_owner.owns(p_base)) {
  5902. Lightmap *lm = lightmap_owner.getornull(p_base);
  5903. p_instance->update_dependency(&lm->dependency);
  5904. } else if (light_owner.owns(p_base)) {
  5905. Light *l = light_owner.getornull(p_base);
  5906. p_instance->update_dependency(&l->dependency);
  5907. } else if (particles_owner.owns(p_base)) {
  5908. Particles *p = particles_owner.getornull(p_base);
  5909. p_instance->update_dependency(&p->dependency);
  5910. } else if (particles_collision_owner.owns(p_base)) {
  5911. ParticlesCollision *pc = particles_collision_owner.getornull(p_base);
  5912. p_instance->update_dependency(&pc->dependency);
  5913. }
  5914. }
  5915. void RendererStorageRD::skeleton_update_dependency(RID p_skeleton, DependencyTracker *p_instance) {
  5916. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  5917. ERR_FAIL_COND(!skeleton);
  5918. p_instance->update_dependency(&skeleton->dependency);
  5919. }
  5920. RS::InstanceType RendererStorageRD::get_base_type(RID p_rid) const {
  5921. if (mesh_owner.owns(p_rid)) {
  5922. return RS::INSTANCE_MESH;
  5923. }
  5924. if (multimesh_owner.owns(p_rid)) {
  5925. return RS::INSTANCE_MULTIMESH;
  5926. }
  5927. if (reflection_probe_owner.owns(p_rid)) {
  5928. return RS::INSTANCE_REFLECTION_PROBE;
  5929. }
  5930. if (decal_owner.owns(p_rid)) {
  5931. return RS::INSTANCE_DECAL;
  5932. }
  5933. if (gi_probe_owner.owns(p_rid)) {
  5934. return RS::INSTANCE_GI_PROBE;
  5935. }
  5936. if (light_owner.owns(p_rid)) {
  5937. return RS::INSTANCE_LIGHT;
  5938. }
  5939. if (lightmap_owner.owns(p_rid)) {
  5940. return RS::INSTANCE_LIGHTMAP;
  5941. }
  5942. if (particles_owner.owns(p_rid)) {
  5943. return RS::INSTANCE_PARTICLES;
  5944. }
  5945. if (particles_collision_owner.owns(p_rid)) {
  5946. return RS::INSTANCE_PARTICLES_COLLISION;
  5947. }
  5948. return RS::INSTANCE_NONE;
  5949. }
  5950. void RendererStorageRD::texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp) {
  5951. if (!decal_atlas.textures.has(p_texture)) {
  5952. DecalAtlas::Texture t;
  5953. t.users = 1;
  5954. t.panorama_to_dp_users = p_panorama_to_dp ? 1 : 0;
  5955. decal_atlas.textures[p_texture] = t;
  5956. decal_atlas.dirty = true;
  5957. } else {
  5958. DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);
  5959. t->users++;
  5960. if (p_panorama_to_dp) {
  5961. t->panorama_to_dp_users++;
  5962. }
  5963. }
  5964. }
  5965. void RendererStorageRD::texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp) {
  5966. DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);
  5967. ERR_FAIL_COND(!t);
  5968. t->users--;
  5969. if (p_panorama_to_dp) {
  5970. ERR_FAIL_COND(t->panorama_to_dp_users == 0);
  5971. t->panorama_to_dp_users--;
  5972. }
  5973. if (t->users == 0) {
  5974. decal_atlas.textures.erase(p_texture);
  5975. //do not mark it dirty, there is no need to since it remains working
  5976. }
  5977. }
  5978. RID RendererStorageRD::decal_atlas_get_texture() const {
  5979. return decal_atlas.texture;
  5980. }
  5981. RID RendererStorageRD::decal_atlas_get_texture_srgb() const {
  5982. return decal_atlas.texture_srgb;
  5983. }
  5984. void RendererStorageRD::_update_decal_atlas() {
  5985. if (!decal_atlas.dirty) {
  5986. return; //nothing to do
  5987. }
  5988. decal_atlas.dirty = false;
  5989. if (decal_atlas.texture.is_valid()) {
  5990. RD::get_singleton()->free(decal_atlas.texture);
  5991. decal_atlas.texture = RID();
  5992. decal_atlas.texture_srgb = RID();
  5993. decal_atlas.texture_mipmaps.clear();
  5994. }
  5995. int border = 1 << decal_atlas.mipmaps;
  5996. if (decal_atlas.textures.size()) {
  5997. //generate atlas
  5998. Vector<DecalAtlas::SortItem> itemsv;
  5999. itemsv.resize(decal_atlas.textures.size());
  6000. int base_size = 8;
  6001. const RID *K = nullptr;
  6002. int idx = 0;
  6003. while ((K = decal_atlas.textures.next(K))) {
  6004. DecalAtlas::SortItem &si = itemsv.write[idx];
  6005. Texture *src_tex = texture_owner.getornull(*K);
  6006. si.size.width = (src_tex->width / border) + 1;
  6007. si.size.height = (src_tex->height / border) + 1;
  6008. si.pixel_size = Size2i(src_tex->width, src_tex->height);
  6009. if (base_size < si.size.width) {
  6010. base_size = nearest_power_of_2_templated(si.size.width);
  6011. }
  6012. si.texture = *K;
  6013. idx++;
  6014. }
  6015. //sort items by size
  6016. itemsv.sort();
  6017. //attempt to create atlas
  6018. int item_count = itemsv.size();
  6019. DecalAtlas::SortItem *items = itemsv.ptrw();
  6020. int atlas_height = 0;
  6021. while (true) {
  6022. Vector<int> v_offsetsv;
  6023. v_offsetsv.resize(base_size);
  6024. int *v_offsets = v_offsetsv.ptrw();
  6025. zeromem(v_offsets, sizeof(int) * base_size);
  6026. int max_height = 0;
  6027. for (int i = 0; i < item_count; i++) {
  6028. //best fit
  6029. DecalAtlas::SortItem &si = items[i];
  6030. int best_idx = -1;
  6031. int best_height = 0x7FFFFFFF;
  6032. for (int j = 0; j <= base_size - si.size.width; j++) {
  6033. int height = 0;
  6034. for (int k = 0; k < si.size.width; k++) {
  6035. int h = v_offsets[k + j];
  6036. if (h > height) {
  6037. height = h;
  6038. if (height > best_height) {
  6039. break; //already bad
  6040. }
  6041. }
  6042. }
  6043. if (height < best_height) {
  6044. best_height = height;
  6045. best_idx = j;
  6046. }
  6047. }
  6048. //update
  6049. for (int k = 0; k < si.size.width; k++) {
  6050. v_offsets[k + best_idx] = best_height + si.size.height;
  6051. }
  6052. si.pos.x = best_idx;
  6053. si.pos.y = best_height;
  6054. if (si.pos.y + si.size.height > max_height) {
  6055. max_height = si.pos.y + si.size.height;
  6056. }
  6057. }
  6058. if (max_height <= base_size * 2) {
  6059. atlas_height = max_height;
  6060. break; //good ratio, break;
  6061. }
  6062. base_size *= 2;
  6063. }
  6064. decal_atlas.size.width = base_size * border;
  6065. decal_atlas.size.height = nearest_power_of_2_templated(atlas_height * border);
  6066. for (int i = 0; i < item_count; i++) {
  6067. DecalAtlas::Texture *t = decal_atlas.textures.getptr(items[i].texture);
  6068. t->uv_rect.position = items[i].pos * border + Vector2i(border / 2, border / 2);
  6069. t->uv_rect.size = items[i].pixel_size;
  6070. t->uv_rect.position /= Size2(decal_atlas.size);
  6071. t->uv_rect.size /= Size2(decal_atlas.size);
  6072. }
  6073. } else {
  6074. //use border as size, so it at least has enough mipmaps
  6075. decal_atlas.size.width = border;
  6076. decal_atlas.size.height = border;
  6077. }
  6078. //blit textures
  6079. RD::TextureFormat tformat;
  6080. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  6081. tformat.width = decal_atlas.size.width;
  6082. tformat.height = decal_atlas.size.height;
  6083. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  6084. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  6085. tformat.mipmaps = decal_atlas.mipmaps;
  6086. tformat.shareable_formats.push_back(RD::DATA_FORMAT_R8G8B8A8_UNORM);
  6087. tformat.shareable_formats.push_back(RD::DATA_FORMAT_R8G8B8A8_SRGB);
  6088. decal_atlas.texture = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  6089. RD::get_singleton()->texture_clear(decal_atlas.texture, Color(0, 0, 0, 0), 0, decal_atlas.mipmaps, 0, 1, true);
  6090. {
  6091. //create the framebuffer
  6092. Size2i s = decal_atlas.size;
  6093. for (int i = 0; i < decal_atlas.mipmaps; i++) {
  6094. DecalAtlas::MipMap mm;
  6095. mm.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), decal_atlas.texture, 0, i);
  6096. Vector<RID> fb;
  6097. fb.push_back(mm.texture);
  6098. mm.fb = RD::get_singleton()->framebuffer_create(fb);
  6099. mm.size = s;
  6100. decal_atlas.texture_mipmaps.push_back(mm);
  6101. s.width = MAX(1, s.width >> 1);
  6102. s.height = MAX(1, s.height >> 1);
  6103. }
  6104. {
  6105. //create the SRGB variant
  6106. RD::TextureView rd_view;
  6107. rd_view.format_override = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  6108. decal_atlas.texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, decal_atlas.texture);
  6109. }
  6110. }
  6111. RID prev_texture;
  6112. for (int i = 0; i < decal_atlas.texture_mipmaps.size(); i++) {
  6113. const DecalAtlas::MipMap &mm = decal_atlas.texture_mipmaps[i];
  6114. Color clear_color(0, 0, 0, 0);
  6115. if (decal_atlas.textures.size()) {
  6116. if (i == 0) {
  6117. Vector<Color> cc;
  6118. cc.push_back(clear_color);
  6119. 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);
  6120. const RID *K = nullptr;
  6121. while ((K = decal_atlas.textures.next(K))) {
  6122. DecalAtlas::Texture *t = decal_atlas.textures.getptr(*K);
  6123. Texture *src_tex = texture_owner.getornull(*K);
  6124. effects.copy_to_atlas_fb(src_tex->rd_texture, mm.fb, t->uv_rect, draw_list, false, t->panorama_to_dp_users > 0);
  6125. }
  6126. RD::get_singleton()->draw_list_end();
  6127. prev_texture = mm.texture;
  6128. } else {
  6129. effects.copy_to_fb_rect(prev_texture, mm.fb, Rect2i(Point2i(), mm.size));
  6130. prev_texture = mm.texture;
  6131. }
  6132. } else {
  6133. RD::get_singleton()->texture_clear(mm.texture, clear_color, 0, 1, 0, 1, false);
  6134. }
  6135. }
  6136. }
  6137. int32_t RendererStorageRD::_global_variable_allocate(uint32_t p_elements) {
  6138. int32_t idx = 0;
  6139. while (idx + p_elements <= global_variables.buffer_size) {
  6140. if (global_variables.buffer_usage[idx].elements == 0) {
  6141. bool valid = true;
  6142. for (uint32_t i = 1; i < p_elements; i++) {
  6143. if (global_variables.buffer_usage[idx + i].elements > 0) {
  6144. valid = false;
  6145. idx += i + global_variables.buffer_usage[idx + i].elements;
  6146. break;
  6147. }
  6148. }
  6149. if (!valid) {
  6150. continue; //if not valid, idx is in new position
  6151. }
  6152. return idx;
  6153. } else {
  6154. idx += global_variables.buffer_usage[idx].elements;
  6155. }
  6156. }
  6157. return -1;
  6158. }
  6159. void RendererStorageRD::_global_variable_store_in_buffer(int32_t p_index, RS::GlobalVariableType p_type, const Variant &p_value) {
  6160. switch (p_type) {
  6161. case RS::GLOBAL_VAR_TYPE_BOOL: {
  6162. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  6163. bool b = p_value;
  6164. bv.x = b ? 1.0 : 0.0;
  6165. bv.y = 0.0;
  6166. bv.z = 0.0;
  6167. bv.w = 0.0;
  6168. } break;
  6169. case RS::GLOBAL_VAR_TYPE_BVEC2: {
  6170. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  6171. uint32_t bvec = p_value;
  6172. bv.x = (bvec & 1) ? 1.0 : 0.0;
  6173. bv.y = (bvec & 2) ? 1.0 : 0.0;
  6174. bv.z = 0.0;
  6175. bv.w = 0.0;
  6176. } break;
  6177. case RS::GLOBAL_VAR_TYPE_BVEC3: {
  6178. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  6179. uint32_t bvec = p_value;
  6180. bv.x = (bvec & 1) ? 1.0 : 0.0;
  6181. bv.y = (bvec & 2) ? 1.0 : 0.0;
  6182. bv.z = (bvec & 4) ? 1.0 : 0.0;
  6183. bv.w = 0.0;
  6184. } break;
  6185. case RS::GLOBAL_VAR_TYPE_BVEC4: {
  6186. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  6187. uint32_t bvec = p_value;
  6188. bv.x = (bvec & 1) ? 1.0 : 0.0;
  6189. bv.y = (bvec & 2) ? 1.0 : 0.0;
  6190. bv.z = (bvec & 4) ? 1.0 : 0.0;
  6191. bv.w = (bvec & 8) ? 1.0 : 0.0;
  6192. } break;
  6193. case RS::GLOBAL_VAR_TYPE_INT: {
  6194. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  6195. int32_t v = p_value;
  6196. bv.x = v;
  6197. bv.y = 0;
  6198. bv.z = 0;
  6199. bv.w = 0;
  6200. } break;
  6201. case RS::GLOBAL_VAR_TYPE_IVEC2: {
  6202. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  6203. Vector2i v = p_value;
  6204. bv.x = v.x;
  6205. bv.y = v.y;
  6206. bv.z = 0;
  6207. bv.w = 0;
  6208. } break;
  6209. case RS::GLOBAL_VAR_TYPE_IVEC3: {
  6210. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  6211. Vector3i v = p_value;
  6212. bv.x = v.x;
  6213. bv.y = v.y;
  6214. bv.z = v.z;
  6215. bv.w = 0;
  6216. } break;
  6217. case RS::GLOBAL_VAR_TYPE_IVEC4: {
  6218. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  6219. Vector<int32_t> v = p_value;
  6220. bv.x = v.size() >= 1 ? v[0] : 0;
  6221. bv.y = v.size() >= 2 ? v[1] : 0;
  6222. bv.z = v.size() >= 3 ? v[2] : 0;
  6223. bv.w = v.size() >= 4 ? v[3] : 0;
  6224. } break;
  6225. case RS::GLOBAL_VAR_TYPE_RECT2I: {
  6226. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  6227. Rect2i v = p_value;
  6228. bv.x = v.position.x;
  6229. bv.y = v.position.y;
  6230. bv.z = v.size.x;
  6231. bv.w = v.size.y;
  6232. } break;
  6233. case RS::GLOBAL_VAR_TYPE_UINT: {
  6234. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  6235. uint32_t v = p_value;
  6236. bv.x = v;
  6237. bv.y = 0;
  6238. bv.z = 0;
  6239. bv.w = 0;
  6240. } break;
  6241. case RS::GLOBAL_VAR_TYPE_UVEC2: {
  6242. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  6243. Vector2i v = p_value;
  6244. bv.x = v.x;
  6245. bv.y = v.y;
  6246. bv.z = 0;
  6247. bv.w = 0;
  6248. } break;
  6249. case RS::GLOBAL_VAR_TYPE_UVEC3: {
  6250. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  6251. Vector3i v = p_value;
  6252. bv.x = v.x;
  6253. bv.y = v.y;
  6254. bv.z = v.z;
  6255. bv.w = 0;
  6256. } break;
  6257. case RS::GLOBAL_VAR_TYPE_UVEC4: {
  6258. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  6259. Vector<int32_t> v = p_value;
  6260. bv.x = v.size() >= 1 ? v[0] : 0;
  6261. bv.y = v.size() >= 2 ? v[1] : 0;
  6262. bv.z = v.size() >= 3 ? v[2] : 0;
  6263. bv.w = v.size() >= 4 ? v[3] : 0;
  6264. } break;
  6265. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  6266. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  6267. float v = p_value;
  6268. bv.x = v;
  6269. bv.y = 0;
  6270. bv.z = 0;
  6271. bv.w = 0;
  6272. } break;
  6273. case RS::GLOBAL_VAR_TYPE_VEC2: {
  6274. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  6275. Vector2 v = p_value;
  6276. bv.x = v.x;
  6277. bv.y = v.y;
  6278. bv.z = 0;
  6279. bv.w = 0;
  6280. } break;
  6281. case RS::GLOBAL_VAR_TYPE_VEC3: {
  6282. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  6283. Vector3 v = p_value;
  6284. bv.x = v.x;
  6285. bv.y = v.y;
  6286. bv.z = v.z;
  6287. bv.w = 0;
  6288. } break;
  6289. case RS::GLOBAL_VAR_TYPE_VEC4: {
  6290. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  6291. Plane v = p_value;
  6292. bv.x = v.normal.x;
  6293. bv.y = v.normal.y;
  6294. bv.z = v.normal.z;
  6295. bv.w = v.d;
  6296. } break;
  6297. case RS::GLOBAL_VAR_TYPE_COLOR: {
  6298. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  6299. Color v = p_value;
  6300. bv.x = v.r;
  6301. bv.y = v.g;
  6302. bv.z = v.b;
  6303. bv.w = v.a;
  6304. GlobalVariables::Value &bv_linear = global_variables.buffer_values[p_index + 1];
  6305. v = v.to_linear();
  6306. bv_linear.x = v.r;
  6307. bv_linear.y = v.g;
  6308. bv_linear.z = v.b;
  6309. bv_linear.w = v.a;
  6310. } break;
  6311. case RS::GLOBAL_VAR_TYPE_RECT2: {
  6312. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  6313. Rect2 v = p_value;
  6314. bv.x = v.position.x;
  6315. bv.y = v.position.y;
  6316. bv.z = v.size.x;
  6317. bv.w = v.size.y;
  6318. } break;
  6319. case RS::GLOBAL_VAR_TYPE_MAT2: {
  6320. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  6321. Vector<float> m2 = p_value;
  6322. if (m2.size() < 4) {
  6323. m2.resize(4);
  6324. }
  6325. bv[0].x = m2[0];
  6326. bv[0].y = m2[1];
  6327. bv[0].z = 0;
  6328. bv[0].w = 0;
  6329. bv[1].x = m2[2];
  6330. bv[1].y = m2[3];
  6331. bv[1].z = 0;
  6332. bv[1].w = 0;
  6333. } break;
  6334. case RS::GLOBAL_VAR_TYPE_MAT3: {
  6335. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  6336. Basis v = p_value;
  6337. bv[0].x = v.elements[0][0];
  6338. bv[0].y = v.elements[1][0];
  6339. bv[0].z = v.elements[2][0];
  6340. bv[0].w = 0;
  6341. bv[1].x = v.elements[0][1];
  6342. bv[1].y = v.elements[1][1];
  6343. bv[1].z = v.elements[2][1];
  6344. bv[1].w = 0;
  6345. bv[2].x = v.elements[0][2];
  6346. bv[2].y = v.elements[1][2];
  6347. bv[2].z = v.elements[2][2];
  6348. bv[2].w = 0;
  6349. } break;
  6350. case RS::GLOBAL_VAR_TYPE_MAT4: {
  6351. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  6352. Vector<float> m2 = p_value;
  6353. if (m2.size() < 16) {
  6354. m2.resize(16);
  6355. }
  6356. bv[0].x = m2[0];
  6357. bv[0].y = m2[1];
  6358. bv[0].z = m2[2];
  6359. bv[0].w = m2[3];
  6360. bv[1].x = m2[4];
  6361. bv[1].y = m2[5];
  6362. bv[1].z = m2[6];
  6363. bv[1].w = m2[7];
  6364. bv[2].x = m2[8];
  6365. bv[2].y = m2[9];
  6366. bv[2].z = m2[10];
  6367. bv[2].w = m2[11];
  6368. bv[3].x = m2[12];
  6369. bv[3].y = m2[13];
  6370. bv[3].z = m2[14];
  6371. bv[3].w = m2[15];
  6372. } break;
  6373. case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
  6374. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  6375. Transform2D v = p_value;
  6376. bv[0].x = v.elements[0][0];
  6377. bv[0].y = v.elements[0][1];
  6378. bv[0].z = 0;
  6379. bv[0].w = 0;
  6380. bv[1].x = v.elements[1][0];
  6381. bv[1].y = v.elements[1][1];
  6382. bv[1].z = 0;
  6383. bv[1].w = 0;
  6384. bv[2].x = v.elements[2][0];
  6385. bv[2].y = v.elements[2][1];
  6386. bv[2].z = 1;
  6387. bv[2].w = 0;
  6388. } break;
  6389. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  6390. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  6391. Transform v = p_value;
  6392. bv[0].x = v.basis.elements[0][0];
  6393. bv[0].y = v.basis.elements[1][0];
  6394. bv[0].z = v.basis.elements[2][0];
  6395. bv[0].w = 0;
  6396. bv[1].x = v.basis.elements[0][1];
  6397. bv[1].y = v.basis.elements[1][1];
  6398. bv[1].z = v.basis.elements[2][1];
  6399. bv[1].w = 0;
  6400. bv[2].x = v.basis.elements[0][2];
  6401. bv[2].y = v.basis.elements[1][2];
  6402. bv[2].z = v.basis.elements[2][2];
  6403. bv[2].w = 0;
  6404. bv[3].x = v.origin.x;
  6405. bv[3].y = v.origin.y;
  6406. bv[3].z = v.origin.z;
  6407. bv[3].w = 1;
  6408. } break;
  6409. default: {
  6410. ERR_FAIL();
  6411. }
  6412. }
  6413. }
  6414. void RendererStorageRD::_global_variable_mark_buffer_dirty(int32_t p_index, int32_t p_elements) {
  6415. int32_t prev_chunk = -1;
  6416. for (int32_t i = 0; i < p_elements; i++) {
  6417. int32_t chunk = (p_index + i) / GlobalVariables::BUFFER_DIRTY_REGION_SIZE;
  6418. if (chunk != prev_chunk) {
  6419. if (!global_variables.buffer_dirty_regions[chunk]) {
  6420. global_variables.buffer_dirty_regions[chunk] = true;
  6421. global_variables.buffer_dirty_region_count++;
  6422. }
  6423. }
  6424. prev_chunk = chunk;
  6425. }
  6426. }
  6427. void RendererStorageRD::global_variable_add(const StringName &p_name, RS::GlobalVariableType p_type, const Variant &p_value) {
  6428. ERR_FAIL_COND(global_variables.variables.has(p_name));
  6429. GlobalVariables::Variable gv;
  6430. gv.type = p_type;
  6431. gv.value = p_value;
  6432. gv.buffer_index = -1;
  6433. if (p_type >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
  6434. //is texture
  6435. global_variables.must_update_texture_materials = true; //normally there are none
  6436. } else {
  6437. gv.buffer_elements = 1;
  6438. if (p_type == RS::GLOBAL_VAR_TYPE_COLOR || p_type == RS::GLOBAL_VAR_TYPE_MAT2) {
  6439. //color needs to elements to store srgb and linear
  6440. gv.buffer_elements = 2;
  6441. }
  6442. if (p_type == RS::GLOBAL_VAR_TYPE_MAT3 || p_type == RS::GLOBAL_VAR_TYPE_TRANSFORM_2D) {
  6443. //color needs to elements to store srgb and linear
  6444. gv.buffer_elements = 3;
  6445. }
  6446. if (p_type == RS::GLOBAL_VAR_TYPE_MAT4 || p_type == RS::GLOBAL_VAR_TYPE_TRANSFORM) {
  6447. //color needs to elements to store srgb and linear
  6448. gv.buffer_elements = 4;
  6449. }
  6450. //is vector, allocate in buffer and update index
  6451. gv.buffer_index = _global_variable_allocate(gv.buffer_elements);
  6452. 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)));
  6453. global_variables.buffer_usage[gv.buffer_index].elements = gv.buffer_elements;
  6454. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  6455. _global_variable_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  6456. global_variables.must_update_buffer_materials = true; //normally there are none
  6457. }
  6458. global_variables.variables[p_name] = gv;
  6459. }
  6460. void RendererStorageRD::global_variable_remove(const StringName &p_name) {
  6461. if (!global_variables.variables.has(p_name)) {
  6462. return;
  6463. }
  6464. GlobalVariables::Variable &gv = global_variables.variables[p_name];
  6465. if (gv.buffer_index >= 0) {
  6466. global_variables.buffer_usage[gv.buffer_index].elements = 0;
  6467. global_variables.must_update_buffer_materials = true;
  6468. } else {
  6469. global_variables.must_update_texture_materials = true;
  6470. }
  6471. global_variables.variables.erase(p_name);
  6472. }
  6473. Vector<StringName> RendererStorageRD::global_variable_get_list() const {
  6474. if (!Engine::get_singleton()->is_editor_hint()) {
  6475. ERR_FAIL_V_MSG(Vector<StringName>(), "This function should never be used outside the editor, it can severely damage performance.");
  6476. }
  6477. const StringName *K = nullptr;
  6478. Vector<StringName> names;
  6479. while ((K = global_variables.variables.next(K))) {
  6480. names.push_back(*K);
  6481. }
  6482. names.sort_custom<StringName::AlphCompare>();
  6483. return names;
  6484. }
  6485. void RendererStorageRD::global_variable_set(const StringName &p_name, const Variant &p_value) {
  6486. ERR_FAIL_COND(!global_variables.variables.has(p_name));
  6487. GlobalVariables::Variable &gv = global_variables.variables[p_name];
  6488. gv.value = p_value;
  6489. if (gv.override.get_type() == Variant::NIL) {
  6490. if (gv.buffer_index >= 0) {
  6491. //buffer
  6492. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  6493. _global_variable_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  6494. } else {
  6495. //texture
  6496. for (Set<RID>::Element *E = gv.texture_materials.front(); E; E = E->next()) {
  6497. Material *material = material_owner.getornull(E->get());
  6498. ERR_CONTINUE(!material);
  6499. _material_queue_update(material, false, true);
  6500. }
  6501. }
  6502. }
  6503. }
  6504. void RendererStorageRD::global_variable_set_override(const StringName &p_name, const Variant &p_value) {
  6505. if (!global_variables.variables.has(p_name)) {
  6506. return; //variable may not exist
  6507. }
  6508. GlobalVariables::Variable &gv = global_variables.variables[p_name];
  6509. gv.override = p_value;
  6510. if (gv.buffer_index >= 0) {
  6511. //buffer
  6512. if (gv.override.get_type() == Variant::NIL) {
  6513. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  6514. } else {
  6515. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.override);
  6516. }
  6517. _global_variable_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  6518. } else {
  6519. //texture
  6520. //texture
  6521. for (Set<RID>::Element *E = gv.texture_materials.front(); E; E = E->next()) {
  6522. Material *material = material_owner.getornull(E->get());
  6523. ERR_CONTINUE(!material);
  6524. _material_queue_update(material, false, true);
  6525. }
  6526. }
  6527. }
  6528. Variant RendererStorageRD::global_variable_get(const StringName &p_name) const {
  6529. if (!Engine::get_singleton()->is_editor_hint()) {
  6530. ERR_FAIL_V_MSG(Variant(), "This function should never be used outside the editor, it can severely damage performance.");
  6531. }
  6532. if (!global_variables.variables.has(p_name)) {
  6533. return Variant();
  6534. }
  6535. return global_variables.variables[p_name].value;
  6536. }
  6537. RS::GlobalVariableType RendererStorageRD::global_variable_get_type_internal(const StringName &p_name) const {
  6538. if (!global_variables.variables.has(p_name)) {
  6539. return RS::GLOBAL_VAR_TYPE_MAX;
  6540. }
  6541. return global_variables.variables[p_name].type;
  6542. }
  6543. RS::GlobalVariableType RendererStorageRD::global_variable_get_type(const StringName &p_name) const {
  6544. if (!Engine::get_singleton()->is_editor_hint()) {
  6545. ERR_FAIL_V_MSG(RS::GLOBAL_VAR_TYPE_MAX, "This function should never be used outside the editor, it can severely damage performance.");
  6546. }
  6547. return global_variable_get_type_internal(p_name);
  6548. }
  6549. void RendererStorageRD::global_variables_load_settings(bool p_load_textures) {
  6550. List<PropertyInfo> settings;
  6551. ProjectSettings::get_singleton()->get_property_list(&settings);
  6552. for (List<PropertyInfo>::Element *E = settings.front(); E; E = E->next()) {
  6553. if (E->get().name.begins_with("shader_globals/")) {
  6554. StringName name = E->get().name.get_slice("/", 1);
  6555. Dictionary d = ProjectSettings::get_singleton()->get(E->get().name);
  6556. ERR_CONTINUE(!d.has("type"));
  6557. ERR_CONTINUE(!d.has("value"));
  6558. String type = d["type"];
  6559. static const char *global_var_type_names[RS::GLOBAL_VAR_TYPE_MAX] = {
  6560. "bool",
  6561. "bvec2",
  6562. "bvec3",
  6563. "bvec4",
  6564. "int",
  6565. "ivec2",
  6566. "ivec3",
  6567. "ivec4",
  6568. "rect2i",
  6569. "uint",
  6570. "uvec2",
  6571. "uvec3",
  6572. "uvec4",
  6573. "float",
  6574. "vec2",
  6575. "vec3",
  6576. "vec4",
  6577. "color",
  6578. "rect2",
  6579. "mat2",
  6580. "mat3",
  6581. "mat4",
  6582. "transform_2d",
  6583. "transform",
  6584. "sampler2D",
  6585. "sampler2DArray",
  6586. "sampler3D",
  6587. "samplerCube",
  6588. };
  6589. RS::GlobalVariableType gvtype = RS::GLOBAL_VAR_TYPE_MAX;
  6590. for (int i = 0; i < RS::GLOBAL_VAR_TYPE_MAX; i++) {
  6591. if (global_var_type_names[i] == type) {
  6592. gvtype = RS::GlobalVariableType(i);
  6593. break;
  6594. }
  6595. }
  6596. ERR_CONTINUE(gvtype == RS::GLOBAL_VAR_TYPE_MAX); //type invalid
  6597. Variant value = d["value"];
  6598. if (gvtype >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
  6599. //textire
  6600. if (!p_load_textures) {
  6601. value = RID();
  6602. continue;
  6603. }
  6604. String path = value;
  6605. RES resource = ResourceLoader::load(path);
  6606. ERR_CONTINUE(resource.is_null());
  6607. value = resource;
  6608. }
  6609. if (global_variables.variables.has(name)) {
  6610. //has it, update it
  6611. global_variable_set(name, value);
  6612. } else {
  6613. global_variable_add(name, gvtype, value);
  6614. }
  6615. }
  6616. }
  6617. }
  6618. void RendererStorageRD::global_variables_clear() {
  6619. global_variables.variables.clear(); //not right but for now enough
  6620. }
  6621. RID RendererStorageRD::global_variables_get_storage_buffer() const {
  6622. return global_variables.buffer;
  6623. }
  6624. int32_t RendererStorageRD::global_variables_instance_allocate(RID p_instance) {
  6625. ERR_FAIL_COND_V(global_variables.instance_buffer_pos.has(p_instance), -1);
  6626. int32_t pos = _global_variable_allocate(ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES);
  6627. global_variables.instance_buffer_pos[p_instance] = pos; //save anyway
  6628. ERR_FAIL_COND_V_MSG(pos < 0, -1, "Too many instances using shader instance variables. Increase buffer size in Project Settings.");
  6629. global_variables.buffer_usage[pos].elements = ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES;
  6630. return pos;
  6631. }
  6632. void RendererStorageRD::global_variables_instance_free(RID p_instance) {
  6633. ERR_FAIL_COND(!global_variables.instance_buffer_pos.has(p_instance));
  6634. int32_t pos = global_variables.instance_buffer_pos[p_instance];
  6635. if (pos >= 0) {
  6636. global_variables.buffer_usage[pos].elements = 0;
  6637. }
  6638. global_variables.instance_buffer_pos.erase(p_instance);
  6639. }
  6640. void RendererStorageRD::global_variables_instance_update(RID p_instance, int p_index, const Variant &p_value) {
  6641. if (!global_variables.instance_buffer_pos.has(p_instance)) {
  6642. return; //just not allocated, ignore
  6643. }
  6644. int32_t pos = global_variables.instance_buffer_pos[p_instance];
  6645. if (pos < 0) {
  6646. return; //again, not allocated, ignore
  6647. }
  6648. ERR_FAIL_INDEX(p_index, ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES);
  6649. 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
  6650. ShaderLanguage::DataType datatype_from_value[Variant::COLOR + 1] = {
  6651. ShaderLanguage::TYPE_MAX, //nil
  6652. ShaderLanguage::TYPE_BOOL, //bool
  6653. ShaderLanguage::TYPE_INT, //int
  6654. ShaderLanguage::TYPE_FLOAT, //float
  6655. ShaderLanguage::TYPE_MAX, //string
  6656. ShaderLanguage::TYPE_VEC2, //vec2
  6657. ShaderLanguage::TYPE_IVEC2, //vec2i
  6658. ShaderLanguage::TYPE_VEC4, //rect2
  6659. ShaderLanguage::TYPE_IVEC4, //rect2i
  6660. ShaderLanguage::TYPE_VEC3, // vec3
  6661. ShaderLanguage::TYPE_IVEC3, //vec3i
  6662. ShaderLanguage::TYPE_MAX, //xform2d not supported here
  6663. ShaderLanguage::TYPE_VEC4, //plane
  6664. ShaderLanguage::TYPE_VEC4, //quat
  6665. ShaderLanguage::TYPE_MAX, //aabb not supported here
  6666. ShaderLanguage::TYPE_MAX, //basis not supported here
  6667. ShaderLanguage::TYPE_MAX, //xform not supported here
  6668. ShaderLanguage::TYPE_VEC4 //color
  6669. };
  6670. ShaderLanguage::DataType datatype = datatype_from_value[p_value.get_type()];
  6671. 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
  6672. pos += p_index;
  6673. _fill_std140_variant_ubo_value(datatype, p_value, (uint8_t *)&global_variables.buffer_values[pos], true); //instances always use linear color in this renderer
  6674. _global_variable_mark_buffer_dirty(pos, 1);
  6675. }
  6676. void RendererStorageRD::_update_global_variables() {
  6677. if (global_variables.buffer_dirty_region_count > 0) {
  6678. uint32_t total_regions = global_variables.buffer_size / GlobalVariables::BUFFER_DIRTY_REGION_SIZE;
  6679. if (total_regions / global_variables.buffer_dirty_region_count <= 4) {
  6680. // 25% of regions dirty, just update all buffer
  6681. RD::get_singleton()->buffer_update(global_variables.buffer, 0, sizeof(GlobalVariables::Value) * global_variables.buffer_size, global_variables.buffer_values);
  6682. zeromem(global_variables.buffer_dirty_regions, sizeof(bool) * total_regions);
  6683. } else {
  6684. uint32_t region_byte_size = sizeof(GlobalVariables::Value) * GlobalVariables::BUFFER_DIRTY_REGION_SIZE;
  6685. for (uint32_t i = 0; i < total_regions; i++) {
  6686. if (global_variables.buffer_dirty_regions[i]) {
  6687. RD::get_singleton()->buffer_update(global_variables.buffer, i * region_byte_size, region_byte_size, global_variables.buffer_values);
  6688. global_variables.buffer_dirty_regions[i] = false;
  6689. }
  6690. }
  6691. }
  6692. global_variables.buffer_dirty_region_count = 0;
  6693. }
  6694. if (global_variables.must_update_buffer_materials) {
  6695. // only happens in the case of a buffer variable added or removed,
  6696. // so not often.
  6697. for (List<RID>::Element *E = global_variables.materials_using_buffer.front(); E; E = E->next()) {
  6698. Material *material = material_owner.getornull(E->get());
  6699. ERR_CONTINUE(!material); //wtf
  6700. _material_queue_update(material, true, false);
  6701. }
  6702. global_variables.must_update_buffer_materials = false;
  6703. }
  6704. if (global_variables.must_update_texture_materials) {
  6705. // only happens in the case of a buffer variable added or removed,
  6706. // so not often.
  6707. for (List<RID>::Element *E = global_variables.materials_using_texture.front(); E; E = E->next()) {
  6708. Material *material = material_owner.getornull(E->get());
  6709. ERR_CONTINUE(!material); //wtf
  6710. _material_queue_update(material, false, true);
  6711. print_line("update material texture?");
  6712. }
  6713. global_variables.must_update_texture_materials = false;
  6714. }
  6715. }
  6716. void RendererStorageRD::update_dirty_resources() {
  6717. _update_global_variables(); //must do before materials, so it can queue them for update
  6718. _update_queued_materials();
  6719. _update_dirty_multimeshes();
  6720. _update_dirty_skeletons();
  6721. _update_decal_atlas();
  6722. }
  6723. bool RendererStorageRD::has_os_feature(const String &p_feature) const {
  6724. if (p_feature == "rgtc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC5_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  6725. return true;
  6726. }
  6727. 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)) {
  6728. return true;
  6729. }
  6730. if (p_feature == "bptc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC7_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  6731. return true;
  6732. }
  6733. 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)) {
  6734. return true;
  6735. }
  6736. 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)) {
  6737. return true;
  6738. }
  6739. return false;
  6740. }
  6741. bool RendererStorageRD::free(RID p_rid) {
  6742. if (texture_owner.owns(p_rid)) {
  6743. Texture *t = texture_owner.getornull(p_rid);
  6744. ERR_FAIL_COND_V(t->is_render_target, false);
  6745. if (RD::get_singleton()->texture_is_valid(t->rd_texture_srgb)) {
  6746. //erase this first, as it's a dependency of the one below
  6747. RD::get_singleton()->free(t->rd_texture_srgb);
  6748. }
  6749. if (RD::get_singleton()->texture_is_valid(t->rd_texture)) {
  6750. RD::get_singleton()->free(t->rd_texture);
  6751. }
  6752. if (t->is_proxy && t->proxy_to.is_valid()) {
  6753. Texture *proxy_to = texture_owner.getornull(t->proxy_to);
  6754. if (proxy_to) {
  6755. proxy_to->proxies.erase(p_rid);
  6756. }
  6757. }
  6758. if (decal_atlas.textures.has(p_rid)) {
  6759. decal_atlas.textures.erase(p_rid);
  6760. //there is not much a point of making it dirty, just let it be.
  6761. }
  6762. for (int i = 0; i < t->proxies.size(); i++) {
  6763. Texture *p = texture_owner.getornull(t->proxies[i]);
  6764. ERR_CONTINUE(!p);
  6765. p->proxy_to = RID();
  6766. p->rd_texture = RID();
  6767. p->rd_texture_srgb = RID();
  6768. }
  6769. if (t->canvas_texture) {
  6770. memdelete(t->canvas_texture);
  6771. }
  6772. texture_owner.free(p_rid);
  6773. } else if (canvas_texture_owner.owns(p_rid)) {
  6774. CanvasTexture *ct = canvas_texture_owner.getornull(p_rid);
  6775. memdelete(ct);
  6776. canvas_texture_owner.free(p_rid);
  6777. } else if (shader_owner.owns(p_rid)) {
  6778. Shader *shader = shader_owner.getornull(p_rid);
  6779. //make material unreference this
  6780. while (shader->owners.size()) {
  6781. material_set_shader(shader->owners.front()->get()->self, RID());
  6782. }
  6783. //clear data if exists
  6784. if (shader->data) {
  6785. memdelete(shader->data);
  6786. }
  6787. shader_owner.free(p_rid);
  6788. } else if (material_owner.owns(p_rid)) {
  6789. Material *material = material_owner.getornull(p_rid);
  6790. if (material->update_requested) {
  6791. _update_queued_materials();
  6792. }
  6793. material_set_shader(p_rid, RID()); //clean up shader
  6794. material->dependency.deleted_notify(p_rid);
  6795. material_owner.free(p_rid);
  6796. } else if (mesh_owner.owns(p_rid)) {
  6797. mesh_clear(p_rid);
  6798. Mesh *mesh = mesh_owner.getornull(p_rid);
  6799. mesh->dependency.deleted_notify(p_rid);
  6800. if (mesh->instances.size()) {
  6801. ERR_PRINT("deleting mesh with active instances");
  6802. }
  6803. mesh_owner.free(p_rid);
  6804. } else if (mesh_instance_owner.owns(p_rid)) {
  6805. MeshInstance *mi = mesh_instance_owner.getornull(p_rid);
  6806. _mesh_instance_clear(mi);
  6807. mi->mesh->instances.erase(mi->I);
  6808. mi->I = nullptr;
  6809. mesh_instance_owner.free(p_rid);
  6810. memdelete(mi);
  6811. } else if (multimesh_owner.owns(p_rid)) {
  6812. _update_dirty_multimeshes();
  6813. multimesh_allocate(p_rid, 0, RS::MULTIMESH_TRANSFORM_2D);
  6814. MultiMesh *multimesh = multimesh_owner.getornull(p_rid);
  6815. multimesh->dependency.deleted_notify(p_rid);
  6816. multimesh_owner.free(p_rid);
  6817. } else if (skeleton_owner.owns(p_rid)) {
  6818. _update_dirty_skeletons();
  6819. skeleton_allocate(p_rid, 0);
  6820. Skeleton *skeleton = skeleton_owner.getornull(p_rid);
  6821. skeleton->dependency.deleted_notify(p_rid);
  6822. skeleton_owner.free(p_rid);
  6823. } else if (reflection_probe_owner.owns(p_rid)) {
  6824. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_rid);
  6825. reflection_probe->dependency.deleted_notify(p_rid);
  6826. reflection_probe_owner.free(p_rid);
  6827. } else if (decal_owner.owns(p_rid)) {
  6828. Decal *decal = decal_owner.getornull(p_rid);
  6829. for (int i = 0; i < RS::DECAL_TEXTURE_MAX; i++) {
  6830. if (decal->textures[i].is_valid() && texture_owner.owns(decal->textures[i])) {
  6831. texture_remove_from_decal_atlas(decal->textures[i]);
  6832. }
  6833. }
  6834. decal->dependency.deleted_notify(p_rid);
  6835. decal_owner.free(p_rid);
  6836. } else if (gi_probe_owner.owns(p_rid)) {
  6837. gi_probe_allocate(p_rid, Transform(), AABB(), Vector3i(), Vector<uint8_t>(), Vector<uint8_t>(), Vector<uint8_t>(), Vector<int>()); //deallocate
  6838. GIProbe *gi_probe = gi_probe_owner.getornull(p_rid);
  6839. gi_probe->dependency.deleted_notify(p_rid);
  6840. gi_probe_owner.free(p_rid);
  6841. } else if (lightmap_owner.owns(p_rid)) {
  6842. lightmap_set_textures(p_rid, RID(), false);
  6843. Lightmap *lightmap = lightmap_owner.getornull(p_rid);
  6844. lightmap->dependency.deleted_notify(p_rid);
  6845. lightmap_owner.free(p_rid);
  6846. } else if (light_owner.owns(p_rid)) {
  6847. light_set_projector(p_rid, RID()); //clear projector
  6848. // delete the texture
  6849. Light *light = light_owner.getornull(p_rid);
  6850. light->dependency.deleted_notify(p_rid);
  6851. light_owner.free(p_rid);
  6852. } else if (particles_owner.owns(p_rid)) {
  6853. Particles *particles = particles_owner.getornull(p_rid);
  6854. _particles_free_data(particles);
  6855. particles->dependency.deleted_notify(p_rid);
  6856. particles_owner.free(p_rid);
  6857. } else if (particles_collision_owner.owns(p_rid)) {
  6858. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_rid);
  6859. if (particles_collision->heightfield_texture.is_valid()) {
  6860. RD::get_singleton()->free(particles_collision->heightfield_texture);
  6861. }
  6862. particles_collision->dependency.deleted_notify(p_rid);
  6863. particles_collision_owner.free(p_rid);
  6864. } else if (particles_collision_instance_owner.owns(p_rid)) {
  6865. particles_collision_instance_owner.free(p_rid);
  6866. } else if (render_target_owner.owns(p_rid)) {
  6867. RenderTarget *rt = render_target_owner.getornull(p_rid);
  6868. _clear_render_target(rt);
  6869. if (rt->texture.is_valid()) {
  6870. Texture *tex = texture_owner.getornull(rt->texture);
  6871. tex->is_render_target = false;
  6872. free(rt->texture);
  6873. }
  6874. render_target_owner.free(p_rid);
  6875. } else {
  6876. return false;
  6877. }
  6878. return true;
  6879. }
  6880. EffectsRD *RendererStorageRD::get_effects() {
  6881. return &effects;
  6882. }
  6883. void RendererStorageRD::capture_timestamps_begin() {
  6884. RD::get_singleton()->capture_timestamp("Frame Begin", false);
  6885. }
  6886. void RendererStorageRD::capture_timestamp(const String &p_name) {
  6887. RD::get_singleton()->capture_timestamp(p_name, true);
  6888. }
  6889. uint32_t RendererStorageRD::get_captured_timestamps_count() const {
  6890. return RD::get_singleton()->get_captured_timestamps_count();
  6891. }
  6892. uint64_t RendererStorageRD::get_captured_timestamps_frame() const {
  6893. return RD::get_singleton()->get_captured_timestamps_frame();
  6894. }
  6895. uint64_t RendererStorageRD::get_captured_timestamp_gpu_time(uint32_t p_index) const {
  6896. return RD::get_singleton()->get_captured_timestamp_gpu_time(p_index);
  6897. }
  6898. uint64_t RendererStorageRD::get_captured_timestamp_cpu_time(uint32_t p_index) const {
  6899. return RD::get_singleton()->get_captured_timestamp_cpu_time(p_index);
  6900. }
  6901. String RendererStorageRD::get_captured_timestamp_name(uint32_t p_index) const {
  6902. return RD::get_singleton()->get_captured_timestamp_name(p_index);
  6903. }
  6904. RendererStorageRD *RendererStorageRD::base_singleton = nullptr;
  6905. RendererStorageRD::RendererStorageRD() {
  6906. base_singleton = this;
  6907. for (int i = 0; i < SHADER_TYPE_MAX; i++) {
  6908. shader_data_request_func[i] = nullptr;
  6909. }
  6910. static_assert(sizeof(GlobalVariables::Value) == 16);
  6911. global_variables.buffer_size = GLOBAL_GET("rendering/high_end/global_shader_variables_buffer_size");
  6912. global_variables.buffer_size = MAX(4096, global_variables.buffer_size);
  6913. global_variables.buffer_values = memnew_arr(GlobalVariables::Value, global_variables.buffer_size);
  6914. zeromem(global_variables.buffer_values, sizeof(GlobalVariables::Value) * global_variables.buffer_size);
  6915. global_variables.buffer_usage = memnew_arr(GlobalVariables::ValueUsage, global_variables.buffer_size);
  6916. global_variables.buffer_dirty_regions = memnew_arr(bool, global_variables.buffer_size / GlobalVariables::BUFFER_DIRTY_REGION_SIZE);
  6917. zeromem(global_variables.buffer_dirty_regions, sizeof(bool) * global_variables.buffer_size / GlobalVariables::BUFFER_DIRTY_REGION_SIZE);
  6918. global_variables.buffer = RD::get_singleton()->storage_buffer_create(sizeof(GlobalVariables::Value) * global_variables.buffer_size);
  6919. material_update_list = nullptr;
  6920. { //create default textures
  6921. RD::TextureFormat tformat;
  6922. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  6923. tformat.width = 4;
  6924. tformat.height = 4;
  6925. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  6926. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  6927. Vector<uint8_t> pv;
  6928. pv.resize(16 * 4);
  6929. for (int i = 0; i < 16; i++) {
  6930. pv.set(i * 4 + 0, 255);
  6931. pv.set(i * 4 + 1, 255);
  6932. pv.set(i * 4 + 2, 255);
  6933. pv.set(i * 4 + 3, 255);
  6934. }
  6935. {
  6936. Vector<Vector<uint8_t>> vpv;
  6937. vpv.push_back(pv);
  6938. default_rd_textures[DEFAULT_RD_TEXTURE_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6939. }
  6940. for (int i = 0; i < 16; i++) {
  6941. pv.set(i * 4 + 0, 0);
  6942. pv.set(i * 4 + 1, 0);
  6943. pv.set(i * 4 + 2, 0);
  6944. pv.set(i * 4 + 3, 255);
  6945. }
  6946. {
  6947. Vector<Vector<uint8_t>> vpv;
  6948. vpv.push_back(pv);
  6949. default_rd_textures[DEFAULT_RD_TEXTURE_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6950. //take the chance and initialize decal atlas to something
  6951. decal_atlas.texture = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6952. decal_atlas.texture_srgb = decal_atlas.texture;
  6953. }
  6954. for (int i = 0; i < 16; i++) {
  6955. pv.set(i * 4 + 0, 128);
  6956. pv.set(i * 4 + 1, 128);
  6957. pv.set(i * 4 + 2, 255);
  6958. pv.set(i * 4 + 3, 255);
  6959. }
  6960. {
  6961. Vector<Vector<uint8_t>> vpv;
  6962. vpv.push_back(pv);
  6963. default_rd_textures[DEFAULT_RD_TEXTURE_NORMAL] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6964. }
  6965. for (int i = 0; i < 16; i++) {
  6966. pv.set(i * 4 + 0, 255);
  6967. pv.set(i * 4 + 1, 128);
  6968. pv.set(i * 4 + 2, 255);
  6969. pv.set(i * 4 + 3, 255);
  6970. }
  6971. {
  6972. Vector<Vector<uint8_t>> vpv;
  6973. vpv.push_back(pv);
  6974. default_rd_textures[DEFAULT_RD_TEXTURE_ANISO] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6975. }
  6976. for (int i = 0; i < 16; i++) {
  6977. pv.set(i * 4 + 0, 0);
  6978. pv.set(i * 4 + 1, 0);
  6979. pv.set(i * 4 + 2, 0);
  6980. pv.set(i * 4 + 3, 0);
  6981. }
  6982. default_rd_textures[DEFAULT_RD_TEXTURE_MULTIMESH_BUFFER] = RD::get_singleton()->texture_buffer_create(16, RD::DATA_FORMAT_R8G8B8A8_UNORM, pv);
  6983. for (int i = 0; i < 16; i++) {
  6984. pv.set(i * 4 + 0, 0);
  6985. pv.set(i * 4 + 1, 0);
  6986. pv.set(i * 4 + 2, 0);
  6987. pv.set(i * 4 + 3, 0);
  6988. }
  6989. {
  6990. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UINT;
  6991. Vector<Vector<uint8_t>> vpv;
  6992. vpv.push_back(pv);
  6993. default_rd_textures[DEFAULT_RD_TEXTURE_2D_UINT] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6994. }
  6995. }
  6996. { //create default cubemap
  6997. RD::TextureFormat tformat;
  6998. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  6999. tformat.width = 4;
  7000. tformat.height = 4;
  7001. tformat.array_layers = 6;
  7002. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  7003. tformat.texture_type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  7004. Vector<uint8_t> pv;
  7005. pv.resize(16 * 4);
  7006. for (int i = 0; i < 16; i++) {
  7007. pv.set(i * 4 + 0, 0);
  7008. pv.set(i * 4 + 1, 0);
  7009. pv.set(i * 4 + 2, 0);
  7010. pv.set(i * 4 + 3, 0);
  7011. }
  7012. {
  7013. Vector<Vector<uint8_t>> vpv;
  7014. for (int i = 0; i < 6; i++) {
  7015. vpv.push_back(pv);
  7016. }
  7017. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  7018. }
  7019. }
  7020. { //create default cubemap array
  7021. RD::TextureFormat tformat;
  7022. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  7023. tformat.width = 4;
  7024. tformat.height = 4;
  7025. tformat.array_layers = 6;
  7026. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  7027. tformat.texture_type = RD::TEXTURE_TYPE_CUBE;
  7028. Vector<uint8_t> pv;
  7029. pv.resize(16 * 4);
  7030. for (int i = 0; i < 16; i++) {
  7031. pv.set(i * 4 + 0, 0);
  7032. pv.set(i * 4 + 1, 0);
  7033. pv.set(i * 4 + 2, 0);
  7034. pv.set(i * 4 + 3, 0);
  7035. }
  7036. {
  7037. Vector<Vector<uint8_t>> vpv;
  7038. for (int i = 0; i < 6; i++) {
  7039. vpv.push_back(pv);
  7040. }
  7041. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  7042. }
  7043. }
  7044. { //create default cubemap white array
  7045. RD::TextureFormat tformat;
  7046. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  7047. tformat.width = 4;
  7048. tformat.height = 4;
  7049. tformat.array_layers = 6;
  7050. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  7051. tformat.texture_type = RD::TEXTURE_TYPE_CUBE;
  7052. Vector<uint8_t> pv;
  7053. pv.resize(16 * 4);
  7054. for (int i = 0; i < 16; i++) {
  7055. pv.set(i * 4 + 0, 255);
  7056. pv.set(i * 4 + 1, 255);
  7057. pv.set(i * 4 + 2, 255);
  7058. pv.set(i * 4 + 3, 255);
  7059. }
  7060. {
  7061. Vector<Vector<uint8_t>> vpv;
  7062. for (int i = 0; i < 6; i++) {
  7063. vpv.push_back(pv);
  7064. }
  7065. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  7066. }
  7067. }
  7068. { //create default 3D
  7069. RD::TextureFormat tformat;
  7070. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  7071. tformat.width = 4;
  7072. tformat.height = 4;
  7073. tformat.depth = 4;
  7074. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  7075. tformat.texture_type = RD::TEXTURE_TYPE_3D;
  7076. Vector<uint8_t> pv;
  7077. pv.resize(64 * 4);
  7078. for (int i = 0; i < 64; i++) {
  7079. pv.set(i * 4 + 0, 0);
  7080. pv.set(i * 4 + 1, 0);
  7081. pv.set(i * 4 + 2, 0);
  7082. pv.set(i * 4 + 3, 0);
  7083. }
  7084. {
  7085. Vector<Vector<uint8_t>> vpv;
  7086. vpv.push_back(pv);
  7087. default_rd_textures[DEFAULT_RD_TEXTURE_3D_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  7088. }
  7089. }
  7090. { //create default array
  7091. RD::TextureFormat tformat;
  7092. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  7093. tformat.width = 4;
  7094. tformat.height = 4;
  7095. tformat.array_layers = 1;
  7096. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  7097. tformat.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  7098. Vector<uint8_t> pv;
  7099. pv.resize(16 * 4);
  7100. for (int i = 0; i < 16; i++) {
  7101. pv.set(i * 4 + 0, 255);
  7102. pv.set(i * 4 + 1, 255);
  7103. pv.set(i * 4 + 2, 255);
  7104. pv.set(i * 4 + 3, 255);
  7105. }
  7106. {
  7107. Vector<Vector<uint8_t>> vpv;
  7108. vpv.push_back(pv);
  7109. default_rd_textures[DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  7110. }
  7111. }
  7112. //default samplers
  7113. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  7114. for (int j = 1; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  7115. RD::SamplerState sampler_state;
  7116. switch (i) {
  7117. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST: {
  7118. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  7119. sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST;
  7120. sampler_state.max_lod = 0;
  7121. } break;
  7122. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR: {
  7123. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  7124. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  7125. sampler_state.max_lod = 0;
  7126. } break;
  7127. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS: {
  7128. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  7129. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  7130. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  7131. } break;
  7132. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS: {
  7133. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  7134. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  7135. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  7136. } break;
  7137. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC: {
  7138. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  7139. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  7140. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  7141. sampler_state.use_anisotropy = true;
  7142. sampler_state.anisotropy_max = 1 << int(GLOBAL_GET("rendering/quality/texture_filters/anisotropic_filtering_level"));
  7143. } break;
  7144. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC: {
  7145. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  7146. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  7147. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  7148. sampler_state.use_anisotropy = true;
  7149. sampler_state.anisotropy_max = 1 << int(GLOBAL_GET("rendering/quality/texture_filters/anisotropic_filtering_level"));
  7150. } break;
  7151. default: {
  7152. }
  7153. }
  7154. switch (j) {
  7155. case RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED: {
  7156. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  7157. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  7158. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  7159. } break;
  7160. case RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED: {
  7161. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_REPEAT;
  7162. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_REPEAT;
  7163. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_REPEAT;
  7164. } break;
  7165. case RS::CANVAS_ITEM_TEXTURE_REPEAT_MIRROR: {
  7166. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  7167. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  7168. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  7169. } break;
  7170. default: {
  7171. }
  7172. }
  7173. default_rd_samplers[i][j] = RD::get_singleton()->sampler_create(sampler_state);
  7174. }
  7175. }
  7176. //default rd buffers
  7177. {
  7178. Vector<uint8_t> buffer;
  7179. {
  7180. buffer.resize(sizeof(float) * 3);
  7181. {
  7182. uint8_t *w = buffer.ptrw();
  7183. float *fptr = (float *)w;
  7184. fptr[0] = 0.0;
  7185. fptr[1] = 0.0;
  7186. fptr[2] = 0.0;
  7187. }
  7188. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_VERTEX] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  7189. }
  7190. { //normal
  7191. buffer.resize(sizeof(float) * 3);
  7192. {
  7193. uint8_t *w = buffer.ptrw();
  7194. float *fptr = (float *)w;
  7195. fptr[0] = 1.0;
  7196. fptr[1] = 0.0;
  7197. fptr[2] = 0.0;
  7198. }
  7199. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_NORMAL] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  7200. }
  7201. { //tangent
  7202. buffer.resize(sizeof(float) * 4);
  7203. {
  7204. uint8_t *w = buffer.ptrw();
  7205. float *fptr = (float *)w;
  7206. fptr[0] = 1.0;
  7207. fptr[1] = 0.0;
  7208. fptr[2] = 0.0;
  7209. fptr[3] = 0.0;
  7210. }
  7211. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_TANGENT] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  7212. }
  7213. { //color
  7214. buffer.resize(sizeof(float) * 4);
  7215. {
  7216. uint8_t *w = buffer.ptrw();
  7217. float *fptr = (float *)w;
  7218. fptr[0] = 1.0;
  7219. fptr[1] = 1.0;
  7220. fptr[2] = 1.0;
  7221. fptr[3] = 1.0;
  7222. }
  7223. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_COLOR] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  7224. }
  7225. { //tex uv 1
  7226. buffer.resize(sizeof(float) * 2);
  7227. {
  7228. uint8_t *w = buffer.ptrw();
  7229. float *fptr = (float *)w;
  7230. fptr[0] = 0.0;
  7231. fptr[1] = 0.0;
  7232. }
  7233. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_TEX_UV] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  7234. }
  7235. { //tex uv 2
  7236. buffer.resize(sizeof(float) * 2);
  7237. {
  7238. uint8_t *w = buffer.ptrw();
  7239. float *fptr = (float *)w;
  7240. fptr[0] = 0.0;
  7241. fptr[1] = 0.0;
  7242. }
  7243. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_TEX_UV2] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  7244. }
  7245. for (int i = 0; i < RS::ARRAY_CUSTOM_COUNT; i++) {
  7246. buffer.resize(sizeof(float) * 4);
  7247. {
  7248. uint8_t *w = buffer.ptrw();
  7249. float *fptr = (float *)w;
  7250. fptr[0] = 0.0;
  7251. fptr[1] = 0.0;
  7252. fptr[2] = 0.0;
  7253. fptr[3] = 0.0;
  7254. }
  7255. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_CUSTOM0 + i] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  7256. }
  7257. { //bones
  7258. buffer.resize(sizeof(uint32_t) * 4);
  7259. {
  7260. uint8_t *w = buffer.ptrw();
  7261. uint32_t *fptr = (uint32_t *)w;
  7262. fptr[0] = 0;
  7263. fptr[1] = 0;
  7264. fptr[2] = 0;
  7265. fptr[3] = 0;
  7266. }
  7267. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_BONES] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  7268. }
  7269. { //weights
  7270. buffer.resize(sizeof(float) * 4);
  7271. {
  7272. uint8_t *w = buffer.ptrw();
  7273. float *fptr = (float *)w;
  7274. fptr[0] = 0.0;
  7275. fptr[1] = 0.0;
  7276. fptr[2] = 0.0;
  7277. fptr[3] = 0.0;
  7278. }
  7279. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_WEIGHTS] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  7280. }
  7281. }
  7282. {
  7283. Vector<String> sdf_versions;
  7284. sdf_versions.push_back(""); //one only
  7285. giprobe_sdf_shader.initialize(sdf_versions);
  7286. giprobe_sdf_shader_version = giprobe_sdf_shader.version_create();
  7287. giprobe_sdf_shader.version_set_compute_code(giprobe_sdf_shader_version, "", "", "", Vector<String>());
  7288. giprobe_sdf_shader_version_shader = giprobe_sdf_shader.version_get_shader(giprobe_sdf_shader_version, 0);
  7289. giprobe_sdf_shader_pipeline = RD::get_singleton()->compute_pipeline_create(giprobe_sdf_shader_version_shader);
  7290. }
  7291. using_lightmap_array = true; // high end
  7292. if (using_lightmap_array) {
  7293. uint32_t textures_per_stage = RD::get_singleton()->limit_get(RD::LIMIT_MAX_TEXTURES_PER_SHADER_STAGE);
  7294. if (textures_per_stage <= 256) {
  7295. lightmap_textures.resize(32);
  7296. } else {
  7297. lightmap_textures.resize(1024);
  7298. }
  7299. for (int i = 0; i < lightmap_textures.size(); i++) {
  7300. lightmap_textures.write[i] = default_rd_textures[DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE];
  7301. }
  7302. }
  7303. lightmap_probe_capture_update_speed = GLOBAL_GET("rendering/lightmapper/probe_capture_update_speed");
  7304. /* Particles */
  7305. {
  7306. // Initialize particles
  7307. Vector<String> particles_modes;
  7308. particles_modes.push_back("");
  7309. particles_shader.shader.initialize(particles_modes, String());
  7310. }
  7311. shader_set_data_request_function(RendererStorageRD::SHADER_TYPE_PARTICLES, _create_particles_shader_funcs);
  7312. material_set_data_request_function(RendererStorageRD::SHADER_TYPE_PARTICLES, _create_particles_material_funcs);
  7313. {
  7314. ShaderCompilerRD::DefaultIdentifierActions actions;
  7315. actions.renames["COLOR"] = "PARTICLE.color";
  7316. actions.renames["VELOCITY"] = "PARTICLE.velocity";
  7317. //actions.renames["MASS"] = "mass"; ?
  7318. actions.renames["ACTIVE"] = "PARTICLE.is_active";
  7319. actions.renames["RESTART"] = "restart";
  7320. actions.renames["CUSTOM"] = "PARTICLE.custom";
  7321. actions.renames["TRANSFORM"] = "PARTICLE.xform";
  7322. actions.renames["TIME"] = "FRAME.time";
  7323. actions.renames["LIFETIME"] = "params.lifetime";
  7324. actions.renames["DELTA"] = "local_delta";
  7325. actions.renames["NUMBER"] = "particle";
  7326. actions.renames["INDEX"] = "index";
  7327. //actions.renames["GRAVITY"] = "current_gravity";
  7328. actions.renames["EMISSION_TRANSFORM"] = "FRAME.emission_transform";
  7329. actions.renames["RANDOM_SEED"] = "FRAME.random_seed";
  7330. actions.renames["FLAG_EMIT_POSITION"] = "EMISSION_FLAG_HAS_POSITION";
  7331. actions.renames["FLAG_EMIT_ROT_SCALE"] = "EMISSION_FLAG_HAS_ROTATION_SCALE";
  7332. actions.renames["FLAG_EMIT_VELOCITY"] = "EMISSION_FLAG_HAS_VELOCITY";
  7333. actions.renames["FLAG_EMIT_COLOR"] = "EMISSION_FLAG_HAS_COLOR";
  7334. actions.renames["FLAG_EMIT_CUSTOM"] = "EMISSION_FLAG_HAS_CUSTOM";
  7335. actions.renames["RESTART_POSITION"] = "restart_position";
  7336. actions.renames["RESTART_ROT_SCALE"] = "restart_rotation_scale";
  7337. actions.renames["RESTART_VELOCITY"] = "restart_velocity";
  7338. actions.renames["RESTART_COLOR"] = "restart_color";
  7339. actions.renames["RESTART_CUSTOM"] = "restart_custom";
  7340. actions.renames["emit_subparticle"] = "emit_subparticle";
  7341. actions.renames["COLLIDED"] = "collided";
  7342. actions.renames["COLLISION_NORMAL"] = "collision_normal";
  7343. actions.renames["COLLISION_DEPTH"] = "collision_depth";
  7344. actions.renames["ATTRACTOR_FORCE"] = "attractor_force";
  7345. actions.render_mode_defines["disable_force"] = "#define DISABLE_FORCE\n";
  7346. actions.render_mode_defines["disable_velocity"] = "#define DISABLE_VELOCITY\n";
  7347. actions.render_mode_defines["keep_data"] = "#define ENABLE_KEEP_DATA\n";
  7348. actions.render_mode_defines["collision_use_scale"] = "#define USE_COLLISON_SCALE\n";
  7349. actions.sampler_array_name = "material_samplers";
  7350. actions.base_texture_binding_index = 1;
  7351. actions.texture_layout_set = 3;
  7352. actions.base_uniform_string = "material.";
  7353. actions.base_varying_index = 10;
  7354. actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP;
  7355. actions.default_repeat = ShaderLanguage::REPEAT_ENABLE;
  7356. actions.global_buffer_array_variable = "global_variables.data";
  7357. particles_shader.compiler.initialize(actions);
  7358. }
  7359. {
  7360. // default material and shader for particles shader
  7361. particles_shader.default_shader = shader_create();
  7362. shader_set_code(particles_shader.default_shader, "shader_type particles; void compute() { COLOR = vec4(1.0); } \n");
  7363. particles_shader.default_material = material_create();
  7364. material_set_shader(particles_shader.default_material, particles_shader.default_shader);
  7365. ParticlesMaterialData *md = (ParticlesMaterialData *)material_get_data(particles_shader.default_material, RendererStorageRD::SHADER_TYPE_PARTICLES);
  7366. particles_shader.default_shader_rd = particles_shader.shader.version_get_shader(md->shader_data->version, 0);
  7367. Vector<RD::Uniform> uniforms;
  7368. {
  7369. RD::Uniform u;
  7370. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  7371. u.binding = 1;
  7372. u.ids.resize(12);
  7373. RID *ids_ptr = u.ids.ptrw();
  7374. ids_ptr[0] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  7375. ids_ptr[1] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  7376. ids_ptr[2] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  7377. ids_ptr[3] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  7378. ids_ptr[4] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  7379. ids_ptr[5] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  7380. ids_ptr[6] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  7381. ids_ptr[7] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  7382. ids_ptr[8] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  7383. ids_ptr[9] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  7384. ids_ptr[10] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  7385. ids_ptr[11] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  7386. uniforms.push_back(u);
  7387. }
  7388. {
  7389. RD::Uniform u;
  7390. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  7391. u.binding = 2;
  7392. u.ids.push_back(global_variables_get_storage_buffer());
  7393. uniforms.push_back(u);
  7394. }
  7395. particles_shader.base_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, 0);
  7396. }
  7397. default_rd_storage_buffer = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t) * 4);
  7398. {
  7399. Vector<String> copy_modes;
  7400. copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n");
  7401. copy_modes.push_back("\n#define MODE_FILL_SORT_BUFFER\n#define USE_SORT_BUFFER\n");
  7402. copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n#define USE_SORT_BUFFER\n");
  7403. particles_shader.copy_shader.initialize(copy_modes);
  7404. particles_shader.copy_shader_version = particles_shader.copy_shader.version_create();
  7405. for (int i = 0; i < ParticlesShader::COPY_MODE_MAX; i++) {
  7406. particles_shader.copy_pipelines[i] = RD::get_singleton()->compute_pipeline_create(particles_shader.copy_shader.version_get_shader(particles_shader.copy_shader_version, i));
  7407. }
  7408. }
  7409. {
  7410. Vector<String> sdf_modes;
  7411. sdf_modes.push_back("\n#define MODE_LOAD\n");
  7412. sdf_modes.push_back("\n#define MODE_LOAD_SHRINK\n");
  7413. sdf_modes.push_back("\n#define MODE_PROCESS\n");
  7414. sdf_modes.push_back("\n#define MODE_PROCESS_OPTIMIZED\n");
  7415. sdf_modes.push_back("\n#define MODE_STORE\n");
  7416. sdf_modes.push_back("\n#define MODE_STORE_SHRINK\n");
  7417. rt_sdf.shader.initialize(sdf_modes);
  7418. rt_sdf.shader_version = rt_sdf.shader.version_create();
  7419. for (int i = 0; i < RenderTargetSDF::SHADER_MAX; i++) {
  7420. rt_sdf.pipelines[i] = RD::get_singleton()->compute_pipeline_create(rt_sdf.shader.version_get_shader(rt_sdf.shader_version, i));
  7421. }
  7422. }
  7423. {
  7424. Vector<String> skeleton_modes;
  7425. skeleton_modes.push_back("\n#define MODE_2D\n");
  7426. skeleton_modes.push_back("");
  7427. skeleton_shader.shader.initialize(skeleton_modes);
  7428. skeleton_shader.version = skeleton_shader.shader.version_create();
  7429. for (int i = 0; i < SkeletonShader::SHADER_MODE_MAX; i++) {
  7430. skeleton_shader.version_shader[i] = skeleton_shader.shader.version_get_shader(skeleton_shader.version, i);
  7431. skeleton_shader.pipeline[i] = RD::get_singleton()->compute_pipeline_create(skeleton_shader.version_shader[i]);
  7432. }
  7433. {
  7434. Vector<RD::Uniform> uniforms;
  7435. {
  7436. RD::Uniform u;
  7437. u.binding = 0;
  7438. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  7439. u.ids.push_back(default_rd_storage_buffer);
  7440. uniforms.push_back(u);
  7441. }
  7442. skeleton_shader.default_skeleton_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, skeleton_shader.version_shader[0], SkeletonShader::UNIFORM_SET_SKELETON);
  7443. }
  7444. }
  7445. }
  7446. RendererStorageRD::~RendererStorageRD() {
  7447. memdelete_arr(global_variables.buffer_values);
  7448. memdelete_arr(global_variables.buffer_usage);
  7449. memdelete_arr(global_variables.buffer_dirty_regions);
  7450. RD::get_singleton()->free(global_variables.buffer);
  7451. //def textures
  7452. for (int i = 0; i < DEFAULT_RD_TEXTURE_MAX; i++) {
  7453. RD::get_singleton()->free(default_rd_textures[i]);
  7454. }
  7455. //def samplers
  7456. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  7457. for (int j = 1; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  7458. RD::get_singleton()->free(default_rd_samplers[i][j]);
  7459. }
  7460. }
  7461. //def buffers
  7462. for (int i = 0; i < DEFAULT_RD_BUFFER_MAX; i++) {
  7463. RD::get_singleton()->free(mesh_default_rd_buffers[i]);
  7464. }
  7465. giprobe_sdf_shader.version_free(giprobe_sdf_shader_version);
  7466. particles_shader.copy_shader.version_free(particles_shader.copy_shader_version);
  7467. rt_sdf.shader.version_free(rt_sdf.shader_version);
  7468. skeleton_shader.shader.version_free(skeleton_shader.version);
  7469. RenderingServer::get_singleton()->free(particles_shader.default_material);
  7470. RenderingServer::get_singleton()->free(particles_shader.default_shader);
  7471. RD::get_singleton()->free(default_rd_storage_buffer);
  7472. if (decal_atlas.textures.size()) {
  7473. ERR_PRINT("Decal Atlas: " + itos(decal_atlas.textures.size()) + " textures were not removed from the atlas.");
  7474. }
  7475. if (decal_atlas.texture.is_valid()) {
  7476. RD::get_singleton()->free(decal_atlas.texture);
  7477. }
  7478. }