renderer_storage_rd.cpp 316 KB

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