rasterizer_storage_rd.cpp 199 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375
  1. /*************************************************************************/
  2. /* rasterizer_storage_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "rasterizer_storage_rd.h"
  31. #include "core/engine.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/project_settings.h"
  34. #include "servers/rendering/shader_language.h"
  35. Ref<Image> RasterizerStorageRD::_validate_texture_format(const Ref<Image> &p_image, TextureToRDFormat &r_format) {
  36. Ref<Image> image = p_image->duplicate();
  37. switch (p_image->get_format()) {
  38. case Image::FORMAT_L8: {
  39. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  40. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  41. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_R;
  42. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  43. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  44. } break; //luminance
  45. case Image::FORMAT_LA8: {
  46. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  47. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  48. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_R;
  49. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  50. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_G;
  51. } break; //luminance-alpha
  52. case Image::FORMAT_R8: {
  53. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  54. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  55. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  56. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  57. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  58. } break;
  59. case Image::FORMAT_RG8: {
  60. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  61. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  62. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  63. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  64. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  65. } break;
  66. case Image::FORMAT_RGB8: {
  67. //this format is not mandatory for specification, check if supported first
  68. 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)) {
  69. r_format.format = RD::DATA_FORMAT_R8G8B8_UNORM;
  70. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8_SRGB;
  71. } else {
  72. //not supported, reconvert
  73. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  74. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  75. image->convert(Image::FORMAT_RGBA8);
  76. }
  77. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  78. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  79. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  80. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  81. } break;
  82. case Image::FORMAT_RGBA8: {
  83. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  84. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  85. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  86. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  87. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  88. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  89. } break;
  90. case Image::FORMAT_RGBA4444: {
  91. r_format.format = RD::DATA_FORMAT_B4G4R4A4_UNORM_PACK16;
  92. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_B; //needs swizzle
  93. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  94. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  95. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  96. } break;
  97. case Image::FORMAT_RGB565: {
  98. r_format.format = RD::DATA_FORMAT_B5G6R5_UNORM_PACK16;
  99. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_B;
  100. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  101. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  102. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  103. } break;
  104. case Image::FORMAT_RF: {
  105. r_format.format = RD::DATA_FORMAT_R32_SFLOAT;
  106. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  107. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  108. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  109. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  110. } break; //float
  111. case Image::FORMAT_RGF: {
  112. r_format.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  113. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  114. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  115. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  116. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  117. } break;
  118. case Image::FORMAT_RGBF: {
  119. //this format is not mandatory for specification, check if supported first
  120. 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)) {
  121. r_format.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  122. } else {
  123. //not supported, reconvert
  124. r_format.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  125. image->convert(Image::FORMAT_RGBAF);
  126. }
  127. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  128. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  129. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  130. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  131. } break;
  132. case Image::FORMAT_RGBAF: {
  133. r_format.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  134. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  135. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  136. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  137. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  138. } break;
  139. case Image::FORMAT_RH: {
  140. r_format.format = RD::DATA_FORMAT_R16_SFLOAT;
  141. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  142. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  143. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  144. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  145. } break; //half float
  146. case Image::FORMAT_RGH: {
  147. r_format.format = RD::DATA_FORMAT_R16G16_SFLOAT;
  148. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  149. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  150. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  151. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  152. } break;
  153. case Image::FORMAT_RGBH: {
  154. //this format is not mandatory for specification, check if supported first
  155. 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)) {
  156. r_format.format = RD::DATA_FORMAT_R16G16B16_SFLOAT;
  157. } else {
  158. //not supported, reconvert
  159. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  160. image->convert(Image::FORMAT_RGBAH);
  161. }
  162. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  163. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  164. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  165. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  166. } break;
  167. case Image::FORMAT_RGBAH: {
  168. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  169. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  170. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  171. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  172. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  173. } break;
  174. case Image::FORMAT_RGBE9995: {
  175. r_format.format = RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32;
  176. #ifndef _MSC_VER
  177. #warning TODO need to make a function in Image to swap bits for this
  178. #endif
  179. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_IDENTITY;
  180. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_IDENTITY;
  181. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_IDENTITY;
  182. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_IDENTITY;
  183. } break;
  184. case Image::FORMAT_DXT1: {
  185. 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)) {
  186. r_format.format = RD::DATA_FORMAT_BC1_RGB_UNORM_BLOCK;
  187. r_format.format_srgb = RD::DATA_FORMAT_BC1_RGB_SRGB_BLOCK;
  188. } else {
  189. //not supported, reconvert
  190. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  191. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  192. image->decompress();
  193. image->convert(Image::FORMAT_RGBA8);
  194. }
  195. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  196. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  197. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  198. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  199. } break; //s3tc bc1
  200. case Image::FORMAT_DXT3: {
  201. 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)) {
  202. r_format.format = RD::DATA_FORMAT_BC2_UNORM_BLOCK;
  203. r_format.format_srgb = RD::DATA_FORMAT_BC2_SRGB_BLOCK;
  204. } else {
  205. //not supported, reconvert
  206. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  207. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  208. image->decompress();
  209. image->convert(Image::FORMAT_RGBA8);
  210. }
  211. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  212. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  213. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  214. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  215. } break; //bc2
  216. case Image::FORMAT_DXT5: {
  217. 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)) {
  218. r_format.format = RD::DATA_FORMAT_BC3_UNORM_BLOCK;
  219. r_format.format_srgb = RD::DATA_FORMAT_BC3_SRGB_BLOCK;
  220. } else {
  221. //not supported, reconvert
  222. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  223. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  224. image->decompress();
  225. image->convert(Image::FORMAT_RGBA8);
  226. }
  227. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  228. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  229. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  230. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  231. } break; //bc3
  232. case Image::FORMAT_RGTC_R: {
  233. 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)) {
  234. r_format.format = RD::DATA_FORMAT_BC4_UNORM_BLOCK;
  235. } else {
  236. //not supported, reconvert
  237. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  238. image->decompress();
  239. image->convert(Image::FORMAT_R8);
  240. }
  241. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  242. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  243. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  244. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  245. } break;
  246. case Image::FORMAT_RGTC_RG: {
  247. 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)) {
  248. r_format.format = RD::DATA_FORMAT_BC5_UNORM_BLOCK;
  249. } else {
  250. //not supported, reconvert
  251. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  252. image->decompress();
  253. image->convert(Image::FORMAT_RG8);
  254. }
  255. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  256. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  257. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  258. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  259. } break;
  260. case Image::FORMAT_BPTC_RGBA: {
  261. 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)) {
  262. r_format.format = RD::DATA_FORMAT_BC7_UNORM_BLOCK;
  263. r_format.format_srgb = RD::DATA_FORMAT_BC7_SRGB_BLOCK;
  264. } else {
  265. //not supported, reconvert
  266. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  267. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  268. image->decompress();
  269. image->convert(Image::FORMAT_RGBA8);
  270. }
  271. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  272. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  273. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  274. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  275. } break; //btpc bc7
  276. case Image::FORMAT_BPTC_RGBF: {
  277. 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)) {
  278. r_format.format = RD::DATA_FORMAT_BC6H_SFLOAT_BLOCK;
  279. } else {
  280. //not supported, reconvert
  281. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  282. image->decompress();
  283. image->convert(Image::FORMAT_RGBAH);
  284. }
  285. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  286. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  287. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  288. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  289. } break; //float bc6h
  290. case Image::FORMAT_BPTC_RGBFU: {
  291. 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)) {
  292. r_format.format = RD::DATA_FORMAT_BC6H_UFLOAT_BLOCK;
  293. } else {
  294. //not supported, reconvert
  295. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  296. image->decompress();
  297. image->convert(Image::FORMAT_RGBAH);
  298. }
  299. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  300. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  301. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  302. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  303. } break; //unsigned float bc6hu
  304. case Image::FORMAT_PVRTC2: {
  305. //this is not properly supported by MoltekVK it seems, so best to use ETC2
  306. 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)) {
  307. r_format.format = RD::DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG;
  308. r_format.format_srgb = RD::DATA_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG;
  309. } else {
  310. //not supported, reconvert
  311. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  312. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  313. image->decompress();
  314. image->convert(Image::FORMAT_RGBA8);
  315. }
  316. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  317. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  318. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  319. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  320. } break; //pvrtc
  321. case Image::FORMAT_PVRTC2A: {
  322. //this is not properly supported by MoltekVK it seems, so best to use ETC2
  323. 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)) {
  324. r_format.format = RD::DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG;
  325. r_format.format_srgb = RD::DATA_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG;
  326. } else {
  327. //not supported, reconvert
  328. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  329. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  330. image->decompress();
  331. image->convert(Image::FORMAT_RGBA8);
  332. }
  333. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  334. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  335. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  336. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  337. } break;
  338. case Image::FORMAT_PVRTC4: {
  339. //this is not properly supported by MoltekVK it seems, so best to use ETC2
  340. 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)) {
  341. r_format.format = RD::DATA_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG;
  342. r_format.format_srgb = RD::DATA_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG;
  343. } else {
  344. //not supported, reconvert
  345. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  346. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  347. image->decompress();
  348. image->convert(Image::FORMAT_RGBA8);
  349. }
  350. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  351. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  352. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  353. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  354. } break;
  355. case Image::FORMAT_PVRTC4A: {
  356. //this is not properly supported by MoltekVK it seems, so best to use ETC2
  357. 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)) {
  358. r_format.format = RD::DATA_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG;
  359. r_format.format_srgb = RD::DATA_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG;
  360. } else {
  361. //not supported, reconvert
  362. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  363. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  364. image->decompress();
  365. image->convert(Image::FORMAT_RGBA8);
  366. }
  367. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  368. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  369. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  370. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  371. } break;
  372. case Image::FORMAT_ETC2_R11: {
  373. 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)) {
  374. r_format.format = RD::DATA_FORMAT_EAC_R11_UNORM_BLOCK;
  375. } else {
  376. //not supported, reconvert
  377. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  378. image->decompress();
  379. image->convert(Image::FORMAT_R8);
  380. }
  381. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  382. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  383. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  384. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  385. } break; //etc2
  386. case Image::FORMAT_ETC2_R11S: {
  387. 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)) {
  388. r_format.format = RD::DATA_FORMAT_EAC_R11_SNORM_BLOCK;
  389. } else {
  390. //not supported, reconvert
  391. r_format.format = RD::DATA_FORMAT_R8_SNORM;
  392. image->decompress();
  393. image->convert(Image::FORMAT_R8);
  394. }
  395. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  396. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  397. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  398. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  399. } break; //signed: {} break; NOT srgb.
  400. case Image::FORMAT_ETC2_RG11: {
  401. 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)) {
  402. r_format.format = RD::DATA_FORMAT_EAC_R11G11_UNORM_BLOCK;
  403. } else {
  404. //not supported, reconvert
  405. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  406. image->decompress();
  407. image->convert(Image::FORMAT_RG8);
  408. }
  409. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  410. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  411. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  412. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  413. } break;
  414. case Image::FORMAT_ETC2_RG11S: {
  415. 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)) {
  416. r_format.format = RD::DATA_FORMAT_EAC_R11G11_SNORM_BLOCK;
  417. } else {
  418. //not supported, reconvert
  419. r_format.format = RD::DATA_FORMAT_R8G8_SNORM;
  420. image->decompress();
  421. image->convert(Image::FORMAT_RG8);
  422. }
  423. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  424. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  425. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  426. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  427. } break;
  428. case Image::FORMAT_ETC:
  429. case Image::FORMAT_ETC2_RGB8: {
  430. //ETC2 is backwards compatible with ETC1, and all modern platforms support it
  431. 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)) {
  432. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
  433. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8_SRGB_BLOCK;
  434. } else {
  435. //not supported, reconvert
  436. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  437. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  438. image->decompress();
  439. image->convert(Image::FORMAT_RGBA8);
  440. }
  441. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  442. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  443. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  444. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  445. } break;
  446. case Image::FORMAT_ETC2_RGBA8: {
  447. 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)) {
  448. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
  449. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK;
  450. } else {
  451. //not supported, reconvert
  452. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  453. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  454. image->decompress();
  455. image->convert(Image::FORMAT_RGBA8);
  456. }
  457. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  458. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  459. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  460. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  461. } break;
  462. case Image::FORMAT_ETC2_RGB8A1: {
  463. 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)) {
  464. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK;
  465. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK;
  466. } else {
  467. //not supported, reconvert
  468. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  469. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  470. image->decompress();
  471. image->convert(Image::FORMAT_RGBA8);
  472. }
  473. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  474. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  475. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  476. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  477. } break;
  478. case Image::FORMAT_ETC2_RA_AS_RG: {
  479. 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)) {
  480. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
  481. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK;
  482. } else {
  483. //not supported, reconvert
  484. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  485. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  486. image->decompress();
  487. image->convert(Image::FORMAT_RGBA8);
  488. }
  489. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  490. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_A;
  491. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  492. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  493. } break;
  494. case Image::FORMAT_DXT5_RA_AS_RG: {
  495. 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)) {
  496. r_format.format = RD::DATA_FORMAT_BC3_UNORM_BLOCK;
  497. r_format.format_srgb = RD::DATA_FORMAT_BC3_SRGB_BLOCK;
  498. } else {
  499. //not supported, reconvert
  500. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  501. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  502. image->decompress();
  503. image->convert(Image::FORMAT_RGBA8);
  504. }
  505. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  506. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_A;
  507. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  508. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  509. } break;
  510. default: {
  511. }
  512. }
  513. return image;
  514. }
  515. RID RasterizerStorageRD::texture_2d_create(const Ref<Image> &p_image) {
  516. ERR_FAIL_COND_V(p_image.is_null(), RID());
  517. ERR_FAIL_COND_V(p_image->empty(), RID());
  518. TextureToRDFormat ret_format;
  519. Ref<Image> image = _validate_texture_format(p_image, ret_format);
  520. Texture texture;
  521. texture.type = Texture::TYPE_2D;
  522. texture.width = p_image->get_width();
  523. texture.height = p_image->get_height();
  524. texture.layers = 1;
  525. texture.mipmaps = p_image->get_mipmap_count() + 1;
  526. texture.depth = 1;
  527. texture.format = p_image->get_format();
  528. texture.validated_format = image->get_format();
  529. texture.rd_type = RD::TEXTURE_TYPE_2D;
  530. texture.rd_format = ret_format.format;
  531. texture.rd_format_srgb = ret_format.format_srgb;
  532. RD::TextureFormat rd_format;
  533. RD::TextureView rd_view;
  534. { //attempt register
  535. rd_format.format = texture.rd_format;
  536. rd_format.width = texture.width;
  537. rd_format.height = texture.height;
  538. rd_format.depth = 1;
  539. rd_format.array_layers = 1;
  540. rd_format.mipmaps = texture.mipmaps;
  541. rd_format.type = texture.rd_type;
  542. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  543. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  544. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  545. rd_format.shareable_formats.push_back(texture.rd_format);
  546. rd_format.shareable_formats.push_back(texture.rd_format_srgb);
  547. }
  548. }
  549. {
  550. rd_view.swizzle_r = ret_format.swizzle_r;
  551. rd_view.swizzle_g = ret_format.swizzle_g;
  552. rd_view.swizzle_b = ret_format.swizzle_b;
  553. rd_view.swizzle_a = ret_format.swizzle_a;
  554. }
  555. Vector<uint8_t> data = image->get_data(); //use image data
  556. Vector<Vector<uint8_t>> data_slices;
  557. data_slices.push_back(data);
  558. texture.rd_texture = RD::get_singleton()->texture_create(rd_format, rd_view, data_slices);
  559. ERR_FAIL_COND_V(texture.rd_texture.is_null(), RID());
  560. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  561. rd_view.format_override = texture.rd_format_srgb;
  562. texture.rd_texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, texture.rd_texture);
  563. if (texture.rd_texture_srgb.is_null()) {
  564. RD::get_singleton()->free(texture.rd_texture);
  565. ERR_FAIL_COND_V(texture.rd_texture_srgb.is_null(), RID());
  566. }
  567. }
  568. //used for 2D, overridable
  569. texture.width_2d = texture.width;
  570. texture.height_2d = texture.height;
  571. texture.is_render_target = false;
  572. texture.rd_view = rd_view;
  573. texture.is_proxy = false;
  574. return texture_owner.make_rid(texture);
  575. }
  576. RID RasterizerStorageRD::texture_2d_layered_create(const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type) {
  577. ERR_FAIL_COND_V(p_layers.size() == 0, RID());
  578. ERR_FAIL_COND_V(p_layered_type == RS::TEXTURE_LAYERED_CUBEMAP && p_layers.size() != 6, RID());
  579. ERR_FAIL_COND_V(p_layered_type == RS::TEXTURE_LAYERED_CUBEMAP_ARRAY && (p_layers.size() < 6 || (p_layers.size() % 6) != 0), RID());
  580. TextureToRDFormat ret_format;
  581. Vector<Ref<Image>> images;
  582. {
  583. int valid_width = 0;
  584. int valid_height = 0;
  585. bool valid_mipmaps = false;
  586. Image::Format valid_format = Image::FORMAT_MAX;
  587. for (int i = 0; i < p_layers.size(); i++) {
  588. ERR_FAIL_COND_V(p_layers[i]->empty(), RID());
  589. if (i == 0) {
  590. valid_width = p_layers[i]->get_width();
  591. valid_height = p_layers[i]->get_height();
  592. valid_format = p_layers[i]->get_format();
  593. valid_mipmaps = p_layers[i]->has_mipmaps();
  594. } else {
  595. ERR_FAIL_COND_V(p_layers[i]->get_width() != valid_width, RID());
  596. ERR_FAIL_COND_V(p_layers[i]->get_height() != valid_height, RID());
  597. ERR_FAIL_COND_V(p_layers[i]->get_format() != valid_format, RID());
  598. ERR_FAIL_COND_V(p_layers[i]->has_mipmaps() != valid_mipmaps, RID());
  599. }
  600. images.push_back(_validate_texture_format(p_layers[i], ret_format));
  601. }
  602. }
  603. Texture texture;
  604. texture.type = Texture::TYPE_LAYERED;
  605. texture.layered_type = p_layered_type;
  606. texture.width = p_layers[0]->get_width();
  607. texture.height = p_layers[0]->get_height();
  608. texture.layers = p_layers.size();
  609. texture.mipmaps = p_layers[0]->get_mipmap_count() + 1;
  610. texture.depth = 1;
  611. texture.format = p_layers[0]->get_format();
  612. texture.validated_format = images[0]->get_format();
  613. switch (p_layered_type) {
  614. case RS::TEXTURE_LAYERED_2D_ARRAY: {
  615. texture.rd_type = RD::TEXTURE_TYPE_2D_ARRAY;
  616. } break;
  617. case RS::TEXTURE_LAYERED_CUBEMAP: {
  618. texture.rd_type = RD::TEXTURE_TYPE_CUBE;
  619. } break;
  620. case RS::TEXTURE_LAYERED_CUBEMAP_ARRAY: {
  621. texture.rd_type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  622. } break;
  623. }
  624. texture.rd_format = ret_format.format;
  625. texture.rd_format_srgb = ret_format.format_srgb;
  626. RD::TextureFormat rd_format;
  627. RD::TextureView rd_view;
  628. { //attempt register
  629. rd_format.format = texture.rd_format;
  630. rd_format.width = texture.width;
  631. rd_format.height = texture.height;
  632. rd_format.depth = 1;
  633. rd_format.array_layers = texture.layers;
  634. rd_format.mipmaps = texture.mipmaps;
  635. rd_format.type = texture.rd_type;
  636. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  637. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  638. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  639. rd_format.shareable_formats.push_back(texture.rd_format);
  640. rd_format.shareable_formats.push_back(texture.rd_format_srgb);
  641. }
  642. }
  643. {
  644. rd_view.swizzle_r = ret_format.swizzle_r;
  645. rd_view.swizzle_g = ret_format.swizzle_g;
  646. rd_view.swizzle_b = ret_format.swizzle_b;
  647. rd_view.swizzle_a = ret_format.swizzle_a;
  648. }
  649. Vector<Vector<uint8_t>> data_slices;
  650. for (int i = 0; i < images.size(); i++) {
  651. Vector<uint8_t> data = images[i]->get_data(); //use image data
  652. data_slices.push_back(data);
  653. }
  654. texture.rd_texture = RD::get_singleton()->texture_create(rd_format, rd_view, data_slices);
  655. ERR_FAIL_COND_V(texture.rd_texture.is_null(), RID());
  656. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  657. rd_view.format_override = texture.rd_format_srgb;
  658. texture.rd_texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, texture.rd_texture);
  659. if (texture.rd_texture_srgb.is_null()) {
  660. RD::get_singleton()->free(texture.rd_texture);
  661. ERR_FAIL_COND_V(texture.rd_texture_srgb.is_null(), RID());
  662. }
  663. }
  664. //used for 2D, overridable
  665. texture.width_2d = texture.width;
  666. texture.height_2d = texture.height;
  667. texture.is_render_target = false;
  668. texture.rd_view = rd_view;
  669. texture.is_proxy = false;
  670. return texture_owner.make_rid(texture);
  671. }
  672. RID RasterizerStorageRD::texture_3d_create(const Vector<Ref<Image>> &p_slices) {
  673. return RID();
  674. }
  675. RID RasterizerStorageRD::texture_proxy_create(RID p_base) {
  676. Texture *tex = texture_owner.getornull(p_base);
  677. ERR_FAIL_COND_V(!tex, RID());
  678. Texture proxy_tex = *tex;
  679. proxy_tex.rd_view.format_override = tex->rd_format;
  680. proxy_tex.rd_texture = RD::get_singleton()->texture_create_shared(proxy_tex.rd_view, tex->rd_texture);
  681. if (proxy_tex.rd_texture_srgb.is_valid()) {
  682. proxy_tex.rd_view.format_override = tex->rd_format_srgb;
  683. proxy_tex.rd_texture_srgb = RD::get_singleton()->texture_create_shared(proxy_tex.rd_view, tex->rd_texture);
  684. }
  685. proxy_tex.proxy_to = p_base;
  686. proxy_tex.is_render_target = false;
  687. proxy_tex.is_proxy = true;
  688. proxy_tex.proxies.clear();
  689. RID rid = texture_owner.make_rid(proxy_tex);
  690. tex->proxies.push_back(rid);
  691. return rid;
  692. }
  693. void RasterizerStorageRD::_texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer, bool p_immediate) {
  694. ERR_FAIL_COND(p_image.is_null() || p_image->empty());
  695. Texture *tex = texture_owner.getornull(p_texture);
  696. ERR_FAIL_COND(!tex);
  697. ERR_FAIL_COND(tex->is_render_target);
  698. ERR_FAIL_COND(p_image->get_width() != tex->width || p_image->get_height() != tex->height);
  699. ERR_FAIL_COND(p_image->get_format() != tex->format);
  700. if (tex->type == Texture::TYPE_LAYERED) {
  701. ERR_FAIL_INDEX(p_layer, tex->layers);
  702. }
  703. #ifdef TOOLS_ENABLED
  704. tex->image_cache_2d.unref();
  705. #endif
  706. TextureToRDFormat f;
  707. Ref<Image> validated = _validate_texture_format(p_image, f);
  708. RD::get_singleton()->texture_update(tex->rd_texture, p_layer, validated->get_data(), !p_immediate);
  709. }
  710. void RasterizerStorageRD::texture_2d_update_immediate(RID p_texture, const Ref<Image> &p_image, int p_layer) {
  711. _texture_2d_update(p_texture, p_image, p_layer, true);
  712. }
  713. void RasterizerStorageRD::texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer) {
  714. _texture_2d_update(p_texture, p_image, p_layer, false);
  715. }
  716. void RasterizerStorageRD::texture_3d_update(RID p_texture, const Ref<Image> &p_image, int p_depth, int p_mipmap) {
  717. }
  718. void RasterizerStorageRD::texture_proxy_update(RID p_texture, RID p_proxy_to) {
  719. Texture *tex = texture_owner.getornull(p_texture);
  720. ERR_FAIL_COND(!tex);
  721. ERR_FAIL_COND(!tex->is_proxy);
  722. Texture *proxy_to = texture_owner.getornull(p_proxy_to);
  723. ERR_FAIL_COND(!proxy_to);
  724. ERR_FAIL_COND(proxy_to->is_proxy);
  725. if (tex->proxy_to.is_valid()) {
  726. //unlink proxy
  727. if (RD::get_singleton()->texture_is_valid(tex->rd_texture)) {
  728. RD::get_singleton()->free(tex->rd_texture);
  729. tex->rd_texture = RID();
  730. }
  731. if (RD::get_singleton()->texture_is_valid(tex->rd_texture_srgb)) {
  732. RD::get_singleton()->free(tex->rd_texture_srgb);
  733. tex->rd_texture_srgb = RID();
  734. }
  735. Texture *prev_tex = texture_owner.getornull(tex->proxy_to);
  736. ERR_FAIL_COND(!prev_tex);
  737. prev_tex->proxies.erase(p_texture);
  738. }
  739. *tex = *proxy_to;
  740. tex->proxy_to = p_proxy_to;
  741. tex->is_render_target = false;
  742. tex->is_proxy = true;
  743. tex->proxies.clear();
  744. proxy_to->proxies.push_back(p_texture);
  745. tex->rd_view.format_override = tex->rd_format;
  746. tex->rd_texture = RD::get_singleton()->texture_create_shared(tex->rd_view, proxy_to->rd_texture);
  747. if (tex->rd_texture_srgb.is_valid()) {
  748. tex->rd_view.format_override = tex->rd_format_srgb;
  749. tex->rd_texture_srgb = RD::get_singleton()->texture_create_shared(tex->rd_view, proxy_to->rd_texture);
  750. }
  751. }
  752. //these two APIs can be used together or in combination with the others.
  753. RID RasterizerStorageRD::texture_2d_placeholder_create() {
  754. //this could be better optimized to reuse an existing image , done this way
  755. //for now to get it working
  756. Ref<Image> image;
  757. image.instance();
  758. image->create(4, 4, false, Image::FORMAT_RGBA8);
  759. for (int i = 0; i < 4; i++) {
  760. for (int j = 0; j < 4; j++) {
  761. image->set_pixel(i, j, Color(1, 0, 1, 1));
  762. }
  763. }
  764. return texture_2d_create(image);
  765. }
  766. RID RasterizerStorageRD::texture_2d_layered_placeholder_create(RS::TextureLayeredType p_layered_type) {
  767. //this could be better optimized to reuse an existing image , done this way
  768. //for now to get it working
  769. Ref<Image> image;
  770. image.instance();
  771. image->create(4, 4, false, Image::FORMAT_RGBA8);
  772. for (int i = 0; i < 4; i++) {
  773. for (int j = 0; j < 4; j++) {
  774. image->set_pixel(i, j, Color(1, 0, 1, 1));
  775. }
  776. }
  777. Vector<Ref<Image>> images;
  778. if (p_layered_type == RS::TEXTURE_LAYERED_2D_ARRAY) {
  779. images.push_back(image);
  780. } else {
  781. //cube
  782. for (int i = 0; i < 6; i++) {
  783. images.push_back(image);
  784. }
  785. }
  786. return texture_2d_layered_create(images, p_layered_type);
  787. }
  788. RID RasterizerStorageRD::texture_3d_placeholder_create() {
  789. return RID();
  790. }
  791. Ref<Image> RasterizerStorageRD::texture_2d_get(RID p_texture) const {
  792. Texture *tex = texture_owner.getornull(p_texture);
  793. ERR_FAIL_COND_V(!tex, Ref<Image>());
  794. #ifdef TOOLS_ENABLED
  795. if (tex->image_cache_2d.is_valid()) {
  796. return tex->image_cache_2d;
  797. }
  798. #endif
  799. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(tex->rd_texture, 0);
  800. ERR_FAIL_COND_V(data.size() == 0, Ref<Image>());
  801. Ref<Image> image;
  802. image.instance();
  803. image->create(tex->width, tex->height, tex->mipmaps > 1, tex->validated_format, data);
  804. ERR_FAIL_COND_V(image->empty(), Ref<Image>());
  805. if (tex->format != tex->validated_format) {
  806. image->convert(tex->format);
  807. }
  808. #ifdef TOOLS_ENABLED
  809. if (Engine::get_singleton()->is_editor_hint()) {
  810. tex->image_cache_2d = image;
  811. }
  812. #endif
  813. return image;
  814. }
  815. Ref<Image> RasterizerStorageRD::texture_2d_layer_get(RID p_texture, int p_layer) const {
  816. return Ref<Image>();
  817. }
  818. Ref<Image> RasterizerStorageRD::texture_3d_slice_get(RID p_texture, int p_depth, int p_mipmap) const {
  819. return Ref<Image>();
  820. }
  821. void RasterizerStorageRD::texture_replace(RID p_texture, RID p_by_texture) {
  822. Texture *tex = texture_owner.getornull(p_texture);
  823. ERR_FAIL_COND(!tex);
  824. ERR_FAIL_COND(tex->proxy_to.is_valid()); //cant replace proxy
  825. Texture *by_tex = texture_owner.getornull(p_by_texture);
  826. ERR_FAIL_COND(!by_tex);
  827. ERR_FAIL_COND(by_tex->proxy_to.is_valid()); //cant replace proxy
  828. if (tex == by_tex) {
  829. return;
  830. }
  831. if (tex->rd_texture_srgb.is_valid()) {
  832. RD::get_singleton()->free(tex->rd_texture_srgb);
  833. }
  834. RD::get_singleton()->free(tex->rd_texture);
  835. Vector<RID> proxies_to_update = tex->proxies;
  836. Vector<RID> proxies_to_redirect = by_tex->proxies;
  837. *tex = *by_tex;
  838. tex->proxies = proxies_to_update; //restore proxies, so they can be updated
  839. for (int i = 0; i < proxies_to_update.size(); i++) {
  840. texture_proxy_update(proxies_to_update[i], p_texture);
  841. }
  842. for (int i = 0; i < proxies_to_redirect.size(); i++) {
  843. texture_proxy_update(proxies_to_redirect[i], p_texture);
  844. }
  845. //delete last, so proxies can be updated
  846. texture_owner.free(p_by_texture);
  847. if (decal_atlas.textures.has(p_texture)) {
  848. //belongs to decal atlas..
  849. decal_atlas.dirty = true; //mark it dirty since it was most likely modified
  850. }
  851. }
  852. void RasterizerStorageRD::texture_set_size_override(RID p_texture, int p_width, int p_height) {
  853. Texture *tex = texture_owner.getornull(p_texture);
  854. ERR_FAIL_COND(!tex);
  855. ERR_FAIL_COND(tex->type != Texture::TYPE_2D);
  856. tex->width_2d = p_width;
  857. tex->height_2d = p_height;
  858. }
  859. void RasterizerStorageRD::texture_set_path(RID p_texture, const String &p_path) {
  860. Texture *tex = texture_owner.getornull(p_texture);
  861. ERR_FAIL_COND(!tex);
  862. tex->path = p_path;
  863. }
  864. String RasterizerStorageRD::texture_get_path(RID p_texture) const {
  865. return String();
  866. }
  867. void RasterizerStorageRD::texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) {
  868. Texture *tex = texture_owner.getornull(p_texture);
  869. ERR_FAIL_COND(!tex);
  870. tex->detect_3d_callback_ud = p_userdata;
  871. tex->detect_3d_callback = p_callback;
  872. }
  873. void RasterizerStorageRD::texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) {
  874. Texture *tex = texture_owner.getornull(p_texture);
  875. ERR_FAIL_COND(!tex);
  876. tex->detect_normal_callback_ud = p_userdata;
  877. tex->detect_normal_callback = p_callback;
  878. }
  879. void RasterizerStorageRD::texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata) {
  880. Texture *tex = texture_owner.getornull(p_texture);
  881. ERR_FAIL_COND(!tex);
  882. tex->detect_roughness_callback_ud = p_userdata;
  883. tex->detect_roughness_callback = p_callback;
  884. }
  885. void RasterizerStorageRD::texture_debug_usage(List<RS::TextureInfo> *r_info) {
  886. }
  887. void RasterizerStorageRD::texture_set_proxy(RID p_proxy, RID p_base) {
  888. }
  889. void RasterizerStorageRD::texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) {
  890. }
  891. Size2 RasterizerStorageRD::texture_size_with_proxy(RID p_proxy) {
  892. return texture_2d_get_size(p_proxy);
  893. }
  894. /* SHADER API */
  895. RID RasterizerStorageRD::shader_create() {
  896. Shader shader;
  897. shader.data = nullptr;
  898. shader.type = SHADER_TYPE_MAX;
  899. return shader_owner.make_rid(shader);
  900. }
  901. void RasterizerStorageRD::shader_set_code(RID p_shader, const String &p_code) {
  902. Shader *shader = shader_owner.getornull(p_shader);
  903. ERR_FAIL_COND(!shader);
  904. shader->code = p_code;
  905. String mode_string = ShaderLanguage::get_shader_type(p_code);
  906. ShaderType new_type;
  907. if (mode_string == "canvas_item")
  908. new_type = SHADER_TYPE_2D;
  909. else if (mode_string == "particles")
  910. new_type = SHADER_TYPE_PARTICLES;
  911. else if (mode_string == "spatial")
  912. new_type = SHADER_TYPE_3D;
  913. else if (mode_string == "sky")
  914. new_type = SHADER_TYPE_SKY;
  915. else
  916. new_type = SHADER_TYPE_MAX;
  917. if (new_type != shader->type) {
  918. if (shader->data) {
  919. memdelete(shader->data);
  920. shader->data = nullptr;
  921. }
  922. for (Set<Material *>::Element *E = shader->owners.front(); E; E = E->next()) {
  923. Material *material = E->get();
  924. material->shader_type = new_type;
  925. if (material->data) {
  926. memdelete(material->data);
  927. material->data = nullptr;
  928. }
  929. }
  930. shader->type = new_type;
  931. if (new_type < SHADER_TYPE_MAX && shader_data_request_func[new_type]) {
  932. shader->data = shader_data_request_func[new_type]();
  933. } else {
  934. shader->type = SHADER_TYPE_MAX; //invalid
  935. }
  936. for (Set<Material *>::Element *E = shader->owners.front(); E; E = E->next()) {
  937. Material *material = E->get();
  938. if (shader->data) {
  939. material->data = material_data_request_func[new_type](shader->data);
  940. material->data->self = material->self;
  941. material->data->set_next_pass(material->next_pass);
  942. material->data->set_render_priority(material->priority);
  943. }
  944. material->shader_type = new_type;
  945. }
  946. }
  947. if (shader->data) {
  948. shader->data->set_code(p_code);
  949. }
  950. for (Set<Material *>::Element *E = shader->owners.front(); E; E = E->next()) {
  951. Material *material = E->get();
  952. material->instance_dependency.instance_notify_changed(false, true);
  953. _material_queue_update(material, true, true);
  954. }
  955. }
  956. String RasterizerStorageRD::shader_get_code(RID p_shader) const {
  957. Shader *shader = shader_owner.getornull(p_shader);
  958. ERR_FAIL_COND_V(!shader, String());
  959. return shader->code;
  960. }
  961. void RasterizerStorageRD::shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const {
  962. Shader *shader = shader_owner.getornull(p_shader);
  963. ERR_FAIL_COND(!shader);
  964. if (shader->data) {
  965. return shader->data->get_param_list(p_param_list);
  966. }
  967. }
  968. void RasterizerStorageRD::shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture) {
  969. Shader *shader = shader_owner.getornull(p_shader);
  970. ERR_FAIL_COND(!shader);
  971. if (p_texture.is_valid() && texture_owner.owns(p_texture)) {
  972. shader->default_texture_parameter[p_name] = p_texture;
  973. } else {
  974. shader->default_texture_parameter.erase(p_name);
  975. }
  976. for (Set<Material *>::Element *E = shader->owners.front(); E; E = E->next()) {
  977. Material *material = E->get();
  978. _material_queue_update(material, false, true);
  979. }
  980. }
  981. RID RasterizerStorageRD::shader_get_default_texture_param(RID p_shader, const StringName &p_name) const {
  982. Shader *shader = shader_owner.getornull(p_shader);
  983. ERR_FAIL_COND_V(!shader, RID());
  984. if (shader->default_texture_parameter.has(p_name)) {
  985. return shader->default_texture_parameter[p_name];
  986. }
  987. return RID();
  988. }
  989. Variant RasterizerStorageRD::shader_get_param_default(RID p_shader, const StringName &p_param) const {
  990. Shader *shader = shader_owner.getornull(p_shader);
  991. ERR_FAIL_COND_V(!shader, Variant());
  992. if (shader->data) {
  993. return shader->data->get_default_parameter(p_param);
  994. }
  995. return Variant();
  996. }
  997. void RasterizerStorageRD::shader_set_data_request_function(ShaderType p_shader_type, ShaderDataRequestFunction p_function) {
  998. ERR_FAIL_INDEX(p_shader_type, SHADER_TYPE_MAX);
  999. shader_data_request_func[p_shader_type] = p_function;
  1000. }
  1001. /* COMMON MATERIAL API */
  1002. RID RasterizerStorageRD::material_create() {
  1003. Material material;
  1004. material.data = nullptr;
  1005. material.shader = nullptr;
  1006. material.shader_type = SHADER_TYPE_MAX;
  1007. material.update_next = nullptr;
  1008. material.update_requested = false;
  1009. material.uniform_dirty = false;
  1010. material.texture_dirty = false;
  1011. material.priority = 0;
  1012. RID id = material_owner.make_rid(material);
  1013. {
  1014. Material *material_ptr = material_owner.getornull(id);
  1015. material_ptr->self = id;
  1016. }
  1017. return id;
  1018. }
  1019. void RasterizerStorageRD::_material_queue_update(Material *material, bool p_uniform, bool p_texture) {
  1020. if (material->update_requested) {
  1021. return;
  1022. }
  1023. material->update_next = material_update_list;
  1024. material_update_list = material;
  1025. material->update_requested = true;
  1026. material->uniform_dirty = material->uniform_dirty || p_uniform;
  1027. material->texture_dirty = material->texture_dirty || p_texture;
  1028. }
  1029. void RasterizerStorageRD::material_set_shader(RID p_material, RID p_shader) {
  1030. Material *material = material_owner.getornull(p_material);
  1031. ERR_FAIL_COND(!material);
  1032. if (material->data) {
  1033. memdelete(material->data);
  1034. material->data = nullptr;
  1035. }
  1036. if (material->shader) {
  1037. material->shader->owners.erase(material);
  1038. material->shader = nullptr;
  1039. material->shader_type = SHADER_TYPE_MAX;
  1040. }
  1041. if (p_shader.is_null()) {
  1042. material->instance_dependency.instance_notify_changed(false, true);
  1043. return;
  1044. }
  1045. Shader *shader = shader_owner.getornull(p_shader);
  1046. ERR_FAIL_COND(!shader);
  1047. material->shader = shader;
  1048. material->shader_type = shader->type;
  1049. shader->owners.insert(material);
  1050. if (shader->type == SHADER_TYPE_MAX) {
  1051. return;
  1052. }
  1053. ERR_FAIL_COND(shader->data == nullptr);
  1054. material->data = material_data_request_func[shader->type](shader->data);
  1055. material->data->self = p_material;
  1056. material->data->set_next_pass(material->next_pass);
  1057. material->data->set_render_priority(material->priority);
  1058. //updating happens later
  1059. material->instance_dependency.instance_notify_changed(false, true);
  1060. _material_queue_update(material, true, true);
  1061. }
  1062. void RasterizerStorageRD::material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) {
  1063. Material *material = material_owner.getornull(p_material);
  1064. ERR_FAIL_COND(!material);
  1065. if (p_value.get_type() == Variant::NIL) {
  1066. material->params.erase(p_param);
  1067. } else {
  1068. material->params[p_param] = p_value;
  1069. }
  1070. if (material->shader && material->shader->data) { //shader is valid
  1071. bool is_texture = material->shader->data->is_param_texture(p_param);
  1072. _material_queue_update(material, !is_texture, is_texture);
  1073. } else {
  1074. _material_queue_update(material, true, true);
  1075. }
  1076. }
  1077. Variant RasterizerStorageRD::material_get_param(RID p_material, const StringName &p_param) const {
  1078. Material *material = material_owner.getornull(p_material);
  1079. ERR_FAIL_COND_V(!material, Variant());
  1080. if (material->params.has(p_param)) {
  1081. return material->params[p_param];
  1082. } else {
  1083. return Variant();
  1084. }
  1085. }
  1086. void RasterizerStorageRD::material_set_next_pass(RID p_material, RID p_next_material) {
  1087. Material *material = material_owner.getornull(p_material);
  1088. ERR_FAIL_COND(!material);
  1089. if (material->next_pass == p_next_material) {
  1090. return;
  1091. }
  1092. material->next_pass = p_next_material;
  1093. if (material->data) {
  1094. material->data->set_next_pass(p_next_material);
  1095. }
  1096. material->instance_dependency.instance_notify_changed(false, true);
  1097. }
  1098. void RasterizerStorageRD::material_set_render_priority(RID p_material, int priority) {
  1099. Material *material = material_owner.getornull(p_material);
  1100. ERR_FAIL_COND(!material);
  1101. material->priority = priority;
  1102. if (material->data) {
  1103. material->data->set_render_priority(priority);
  1104. }
  1105. }
  1106. bool RasterizerStorageRD::material_is_animated(RID p_material) {
  1107. Material *material = material_owner.getornull(p_material);
  1108. ERR_FAIL_COND_V(!material, false);
  1109. if (material->shader && material->shader->data) {
  1110. if (material->shader->data->is_animated()) {
  1111. return true;
  1112. } else if (material->next_pass.is_valid()) {
  1113. return material_is_animated(material->next_pass);
  1114. }
  1115. }
  1116. return false; //by default nothing is animated
  1117. }
  1118. bool RasterizerStorageRD::material_casts_shadows(RID p_material) {
  1119. Material *material = material_owner.getornull(p_material);
  1120. ERR_FAIL_COND_V(!material, true);
  1121. if (material->shader && material->shader->data) {
  1122. if (material->shader->data->casts_shadows()) {
  1123. return true;
  1124. } else if (material->next_pass.is_valid()) {
  1125. return material_casts_shadows(material->next_pass);
  1126. }
  1127. }
  1128. return true; //by default everything casts shadows
  1129. }
  1130. void RasterizerStorageRD::material_get_instance_shader_parameters(RID p_material, List<InstanceShaderParam> *r_parameters) {
  1131. Material *material = material_owner.getornull(p_material);
  1132. ERR_FAIL_COND(!material);
  1133. if (material->shader && material->shader->data) {
  1134. material->shader->data->get_instance_param_list(r_parameters);
  1135. if (material->next_pass.is_valid()) {
  1136. material_get_instance_shader_parameters(material->next_pass, r_parameters);
  1137. }
  1138. }
  1139. }
  1140. void RasterizerStorageRD::material_update_dependency(RID p_material, RasterizerScene::InstanceBase *p_instance) {
  1141. Material *material = material_owner.getornull(p_material);
  1142. ERR_FAIL_COND(!material);
  1143. p_instance->update_dependency(&material->instance_dependency);
  1144. if (material->next_pass.is_valid()) {
  1145. material_update_dependency(material->next_pass, p_instance);
  1146. }
  1147. }
  1148. void RasterizerStorageRD::material_set_data_request_function(ShaderType p_shader_type, MaterialDataRequestFunction p_function) {
  1149. ERR_FAIL_INDEX(p_shader_type, SHADER_TYPE_MAX);
  1150. material_data_request_func[p_shader_type] = p_function;
  1151. }
  1152. _FORCE_INLINE_ static void _fill_std140_variant_ubo_value(ShaderLanguage::DataType type, const Variant &value, uint8_t *data, bool p_linear_color) {
  1153. switch (type) {
  1154. case ShaderLanguage::TYPE_BOOL: {
  1155. bool v = value;
  1156. uint32_t *gui = (uint32_t *)data;
  1157. *gui = v ? 1 : 0;
  1158. } break;
  1159. case ShaderLanguage::TYPE_BVEC2: {
  1160. int v = value;
  1161. uint32_t *gui = (uint32_t *)data;
  1162. gui[0] = v & 1 ? 1 : 0;
  1163. gui[1] = v & 2 ? 1 : 0;
  1164. } break;
  1165. case ShaderLanguage::TYPE_BVEC3: {
  1166. int v = value;
  1167. uint32_t *gui = (uint32_t *)data;
  1168. gui[0] = (v & 1) ? 1 : 0;
  1169. gui[1] = (v & 2) ? 1 : 0;
  1170. gui[2] = (v & 4) ? 1 : 0;
  1171. } break;
  1172. case ShaderLanguage::TYPE_BVEC4: {
  1173. int v = value;
  1174. uint32_t *gui = (uint32_t *)data;
  1175. gui[0] = (v & 1) ? 1 : 0;
  1176. gui[1] = (v & 2) ? 1 : 0;
  1177. gui[2] = (v & 4) ? 1 : 0;
  1178. gui[3] = (v & 8) ? 1 : 0;
  1179. } break;
  1180. case ShaderLanguage::TYPE_INT: {
  1181. int v = value;
  1182. int32_t *gui = (int32_t *)data;
  1183. gui[0] = v;
  1184. } break;
  1185. case ShaderLanguage::TYPE_IVEC2: {
  1186. Vector<int> iv = value;
  1187. int s = iv.size();
  1188. int32_t *gui = (int32_t *)data;
  1189. const int *r = iv.ptr();
  1190. for (int i = 0; i < 2; i++) {
  1191. if (i < s)
  1192. gui[i] = r[i];
  1193. else
  1194. gui[i] = 0;
  1195. }
  1196. } break;
  1197. case ShaderLanguage::TYPE_IVEC3: {
  1198. Vector<int> iv = value;
  1199. int s = iv.size();
  1200. int32_t *gui = (int32_t *)data;
  1201. const int *r = iv.ptr();
  1202. for (int i = 0; i < 3; i++) {
  1203. if (i < s)
  1204. gui[i] = r[i];
  1205. else
  1206. gui[i] = 0;
  1207. }
  1208. } break;
  1209. case ShaderLanguage::TYPE_IVEC4: {
  1210. Vector<int> iv = value;
  1211. int s = iv.size();
  1212. int32_t *gui = (int32_t *)data;
  1213. const int *r = iv.ptr();
  1214. for (int i = 0; i < 4; i++) {
  1215. if (i < s)
  1216. gui[i] = r[i];
  1217. else
  1218. gui[i] = 0;
  1219. }
  1220. } break;
  1221. case ShaderLanguage::TYPE_UINT: {
  1222. int v = value;
  1223. uint32_t *gui = (uint32_t *)data;
  1224. gui[0] = v;
  1225. } break;
  1226. case ShaderLanguage::TYPE_UVEC2: {
  1227. Vector<int> iv = value;
  1228. int s = iv.size();
  1229. uint32_t *gui = (uint32_t *)data;
  1230. const int *r = iv.ptr();
  1231. for (int i = 0; i < 2; i++) {
  1232. if (i < s)
  1233. gui[i] = r[i];
  1234. else
  1235. gui[i] = 0;
  1236. }
  1237. } break;
  1238. case ShaderLanguage::TYPE_UVEC3: {
  1239. Vector<int> iv = value;
  1240. int s = iv.size();
  1241. uint32_t *gui = (uint32_t *)data;
  1242. const int *r = iv.ptr();
  1243. for (int i = 0; i < 3; i++) {
  1244. if (i < s)
  1245. gui[i] = r[i];
  1246. else
  1247. gui[i] = 0;
  1248. }
  1249. } break;
  1250. case ShaderLanguage::TYPE_UVEC4: {
  1251. Vector<int> iv = value;
  1252. int s = iv.size();
  1253. uint32_t *gui = (uint32_t *)data;
  1254. const int *r = iv.ptr();
  1255. for (int i = 0; i < 4; i++) {
  1256. if (i < s)
  1257. gui[i] = r[i];
  1258. else
  1259. gui[i] = 0;
  1260. }
  1261. } break;
  1262. case ShaderLanguage::TYPE_FLOAT: {
  1263. float v = value;
  1264. float *gui = (float *)data;
  1265. gui[0] = v;
  1266. } break;
  1267. case ShaderLanguage::TYPE_VEC2: {
  1268. Vector2 v = value;
  1269. float *gui = (float *)data;
  1270. gui[0] = v.x;
  1271. gui[1] = v.y;
  1272. } break;
  1273. case ShaderLanguage::TYPE_VEC3: {
  1274. Vector3 v = value;
  1275. float *gui = (float *)data;
  1276. gui[0] = v.x;
  1277. gui[1] = v.y;
  1278. gui[2] = v.z;
  1279. } break;
  1280. case ShaderLanguage::TYPE_VEC4: {
  1281. float *gui = (float *)data;
  1282. if (value.get_type() == Variant::COLOR) {
  1283. Color v = value;
  1284. if (p_linear_color) {
  1285. v = v.to_linear();
  1286. }
  1287. gui[0] = v.r;
  1288. gui[1] = v.g;
  1289. gui[2] = v.b;
  1290. gui[3] = v.a;
  1291. } else if (value.get_type() == Variant::RECT2) {
  1292. Rect2 v = value;
  1293. gui[0] = v.position.x;
  1294. gui[1] = v.position.y;
  1295. gui[2] = v.size.x;
  1296. gui[3] = v.size.y;
  1297. } else if (value.get_type() == Variant::QUAT) {
  1298. Quat v = value;
  1299. gui[0] = v.x;
  1300. gui[1] = v.y;
  1301. gui[2] = v.z;
  1302. gui[3] = v.w;
  1303. } else {
  1304. Plane v = value;
  1305. gui[0] = v.normal.x;
  1306. gui[1] = v.normal.y;
  1307. gui[2] = v.normal.z;
  1308. gui[3] = v.d;
  1309. }
  1310. } break;
  1311. case ShaderLanguage::TYPE_MAT2: {
  1312. Transform2D v = value;
  1313. float *gui = (float *)data;
  1314. //in std140 members of mat2 are treated as vec4s
  1315. gui[0] = v.elements[0][0];
  1316. gui[1] = v.elements[0][1];
  1317. gui[2] = 0;
  1318. gui[3] = 0;
  1319. gui[4] = v.elements[1][0];
  1320. gui[5] = v.elements[1][1];
  1321. gui[6] = 0;
  1322. gui[7] = 0;
  1323. } break;
  1324. case ShaderLanguage::TYPE_MAT3: {
  1325. Basis v = value;
  1326. float *gui = (float *)data;
  1327. gui[0] = v.elements[0][0];
  1328. gui[1] = v.elements[1][0];
  1329. gui[2] = v.elements[2][0];
  1330. gui[3] = 0;
  1331. gui[4] = v.elements[0][1];
  1332. gui[5] = v.elements[1][1];
  1333. gui[6] = v.elements[2][1];
  1334. gui[7] = 0;
  1335. gui[8] = v.elements[0][2];
  1336. gui[9] = v.elements[1][2];
  1337. gui[10] = v.elements[2][2];
  1338. gui[11] = 0;
  1339. } break;
  1340. case ShaderLanguage::TYPE_MAT4: {
  1341. Transform v = value;
  1342. float *gui = (float *)data;
  1343. gui[0] = v.basis.elements[0][0];
  1344. gui[1] = v.basis.elements[1][0];
  1345. gui[2] = v.basis.elements[2][0];
  1346. gui[3] = 0;
  1347. gui[4] = v.basis.elements[0][1];
  1348. gui[5] = v.basis.elements[1][1];
  1349. gui[6] = v.basis.elements[2][1];
  1350. gui[7] = 0;
  1351. gui[8] = v.basis.elements[0][2];
  1352. gui[9] = v.basis.elements[1][2];
  1353. gui[10] = v.basis.elements[2][2];
  1354. gui[11] = 0;
  1355. gui[12] = v.origin.x;
  1356. gui[13] = v.origin.y;
  1357. gui[14] = v.origin.z;
  1358. gui[15] = 1;
  1359. } break;
  1360. default: {
  1361. }
  1362. }
  1363. }
  1364. _FORCE_INLINE_ static void _fill_std140_ubo_value(ShaderLanguage::DataType type, const Vector<ShaderLanguage::ConstantNode::Value> &value, uint8_t *data) {
  1365. switch (type) {
  1366. case ShaderLanguage::TYPE_BOOL: {
  1367. uint32_t *gui = (uint32_t *)data;
  1368. *gui = value[0].boolean ? 1 : 0;
  1369. } break;
  1370. case ShaderLanguage::TYPE_BVEC2: {
  1371. uint32_t *gui = (uint32_t *)data;
  1372. gui[0] = value[0].boolean ? 1 : 0;
  1373. gui[1] = value[1].boolean ? 1 : 0;
  1374. } break;
  1375. case ShaderLanguage::TYPE_BVEC3: {
  1376. uint32_t *gui = (uint32_t *)data;
  1377. gui[0] = value[0].boolean ? 1 : 0;
  1378. gui[1] = value[1].boolean ? 1 : 0;
  1379. gui[2] = value[2].boolean ? 1 : 0;
  1380. } break;
  1381. case ShaderLanguage::TYPE_BVEC4: {
  1382. uint32_t *gui = (uint32_t *)data;
  1383. gui[0] = value[0].boolean ? 1 : 0;
  1384. gui[1] = value[1].boolean ? 1 : 0;
  1385. gui[2] = value[2].boolean ? 1 : 0;
  1386. gui[3] = value[3].boolean ? 1 : 0;
  1387. } break;
  1388. case ShaderLanguage::TYPE_INT: {
  1389. int32_t *gui = (int32_t *)data;
  1390. gui[0] = value[0].sint;
  1391. } break;
  1392. case ShaderLanguage::TYPE_IVEC2: {
  1393. int32_t *gui = (int32_t *)data;
  1394. for (int i = 0; i < 2; i++) {
  1395. gui[i] = value[i].sint;
  1396. }
  1397. } break;
  1398. case ShaderLanguage::TYPE_IVEC3: {
  1399. int32_t *gui = (int32_t *)data;
  1400. for (int i = 0; i < 3; i++) {
  1401. gui[i] = value[i].sint;
  1402. }
  1403. } break;
  1404. case ShaderLanguage::TYPE_IVEC4: {
  1405. int32_t *gui = (int32_t *)data;
  1406. for (int i = 0; i < 4; i++) {
  1407. gui[i] = value[i].sint;
  1408. }
  1409. } break;
  1410. case ShaderLanguage::TYPE_UINT: {
  1411. uint32_t *gui = (uint32_t *)data;
  1412. gui[0] = value[0].uint;
  1413. } break;
  1414. case ShaderLanguage::TYPE_UVEC2: {
  1415. int32_t *gui = (int32_t *)data;
  1416. for (int i = 0; i < 2; i++) {
  1417. gui[i] = value[i].uint;
  1418. }
  1419. } break;
  1420. case ShaderLanguage::TYPE_UVEC3: {
  1421. int32_t *gui = (int32_t *)data;
  1422. for (int i = 0; i < 3; i++) {
  1423. gui[i] = value[i].uint;
  1424. }
  1425. } break;
  1426. case ShaderLanguage::TYPE_UVEC4: {
  1427. int32_t *gui = (int32_t *)data;
  1428. for (int i = 0; i < 4; i++) {
  1429. gui[i] = value[i].uint;
  1430. }
  1431. } break;
  1432. case ShaderLanguage::TYPE_FLOAT: {
  1433. float *gui = (float *)data;
  1434. gui[0] = value[0].real;
  1435. } break;
  1436. case ShaderLanguage::TYPE_VEC2: {
  1437. float *gui = (float *)data;
  1438. for (int i = 0; i < 2; i++) {
  1439. gui[i] = value[i].real;
  1440. }
  1441. } break;
  1442. case ShaderLanguage::TYPE_VEC3: {
  1443. float *gui = (float *)data;
  1444. for (int i = 0; i < 3; i++) {
  1445. gui[i] = value[i].real;
  1446. }
  1447. } break;
  1448. case ShaderLanguage::TYPE_VEC4: {
  1449. float *gui = (float *)data;
  1450. for (int i = 0; i < 4; i++) {
  1451. gui[i] = value[i].real;
  1452. }
  1453. } break;
  1454. case ShaderLanguage::TYPE_MAT2: {
  1455. float *gui = (float *)data;
  1456. //in std140 members of mat2 are treated as vec4s
  1457. gui[0] = value[0].real;
  1458. gui[1] = value[1].real;
  1459. gui[2] = 0;
  1460. gui[3] = 0;
  1461. gui[4] = value[2].real;
  1462. gui[5] = value[3].real;
  1463. gui[6] = 0;
  1464. gui[7] = 0;
  1465. } break;
  1466. case ShaderLanguage::TYPE_MAT3: {
  1467. float *gui = (float *)data;
  1468. gui[0] = value[0].real;
  1469. gui[1] = value[1].real;
  1470. gui[2] = value[2].real;
  1471. gui[3] = 0;
  1472. gui[4] = value[3].real;
  1473. gui[5] = value[4].real;
  1474. gui[6] = value[5].real;
  1475. gui[7] = 0;
  1476. gui[8] = value[6].real;
  1477. gui[9] = value[7].real;
  1478. gui[10] = value[8].real;
  1479. gui[11] = 0;
  1480. } break;
  1481. case ShaderLanguage::TYPE_MAT4: {
  1482. float *gui = (float *)data;
  1483. for (int i = 0; i < 16; i++) {
  1484. gui[i] = value[i].real;
  1485. }
  1486. } break;
  1487. default: {
  1488. }
  1489. }
  1490. }
  1491. _FORCE_INLINE_ static void _fill_std140_ubo_empty(ShaderLanguage::DataType type, uint8_t *data) {
  1492. switch (type) {
  1493. case ShaderLanguage::TYPE_BOOL:
  1494. case ShaderLanguage::TYPE_INT:
  1495. case ShaderLanguage::TYPE_UINT:
  1496. case ShaderLanguage::TYPE_FLOAT: {
  1497. zeromem(data, 4);
  1498. } break;
  1499. case ShaderLanguage::TYPE_BVEC2:
  1500. case ShaderLanguage::TYPE_IVEC2:
  1501. case ShaderLanguage::TYPE_UVEC2:
  1502. case ShaderLanguage::TYPE_VEC2: {
  1503. zeromem(data, 8);
  1504. } break;
  1505. case ShaderLanguage::TYPE_BVEC3:
  1506. case ShaderLanguage::TYPE_IVEC3:
  1507. case ShaderLanguage::TYPE_UVEC3:
  1508. case ShaderLanguage::TYPE_VEC3:
  1509. case ShaderLanguage::TYPE_BVEC4:
  1510. case ShaderLanguage::TYPE_IVEC4:
  1511. case ShaderLanguage::TYPE_UVEC4:
  1512. case ShaderLanguage::TYPE_VEC4: {
  1513. zeromem(data, 16);
  1514. } break;
  1515. case ShaderLanguage::TYPE_MAT2: {
  1516. zeromem(data, 32);
  1517. } break;
  1518. case ShaderLanguage::TYPE_MAT3: {
  1519. zeromem(data, 48);
  1520. } break;
  1521. case ShaderLanguage::TYPE_MAT4: {
  1522. zeromem(data, 64);
  1523. } break;
  1524. default: {
  1525. }
  1526. }
  1527. }
  1528. void RasterizerStorageRD::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) {
  1529. bool uses_global_buffer = false;
  1530. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = p_uniforms.front(); E; E = E->next()) {
  1531. if (E->get().order < 0)
  1532. continue; // texture, does not go here
  1533. if (E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  1534. continue; //instance uniforms don't appear in the bufferr
  1535. }
  1536. if (E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  1537. //this is a global variable, get the index to it
  1538. RasterizerStorageRD *rs = base_singleton;
  1539. GlobalVariables::Variable *gv = rs->global_variables.variables.getptr(E->key());
  1540. uint32_t index = 0;
  1541. if (gv) {
  1542. index = gv->buffer_index;
  1543. } else {
  1544. WARN_PRINT("Shader uses global uniform '" + E->key() + "', but it was removed at some point. Material will not display correctly.");
  1545. }
  1546. uint32_t offset = p_uniform_offsets[E->get().order];
  1547. uint32_t *intptr = (uint32_t *)&p_buffer[offset];
  1548. *intptr = index;
  1549. uses_global_buffer = true;
  1550. continue;
  1551. }
  1552. //regular uniform
  1553. uint32_t offset = p_uniform_offsets[E->get().order];
  1554. #ifdef DEBUG_ENABLED
  1555. uint32_t size = ShaderLanguage::get_type_size(E->get().type);
  1556. ERR_CONTINUE(offset + size > p_buffer_size);
  1557. #endif
  1558. uint8_t *data = &p_buffer[offset];
  1559. const Map<StringName, Variant>::Element *V = p_parameters.find(E->key());
  1560. if (V) {
  1561. //user provided
  1562. _fill_std140_variant_ubo_value(E->get().type, V->get(), data, p_use_linear_color);
  1563. } else if (E->get().default_value.size()) {
  1564. //default value
  1565. _fill_std140_ubo_value(E->get().type, E->get().default_value, data);
  1566. //value=E->get().default_value;
  1567. } else {
  1568. //zero because it was not provided
  1569. if (E->get().type == ShaderLanguage::TYPE_VEC4 && E->get().hint == ShaderLanguage::ShaderNode::Uniform::HINT_COLOR) {
  1570. //colors must be set as black, with alpha as 1.0
  1571. _fill_std140_variant_ubo_value(E->get().type, Color(0, 0, 0, 1), data, p_use_linear_color);
  1572. } else {
  1573. //else just zero it out
  1574. _fill_std140_ubo_empty(E->get().type, data);
  1575. }
  1576. }
  1577. }
  1578. if (uses_global_buffer != (global_buffer_E != nullptr)) {
  1579. RasterizerStorageRD *rs = base_singleton;
  1580. if (uses_global_buffer) {
  1581. global_buffer_E = rs->global_variables.materials_using_buffer.push_back(self);
  1582. } else {
  1583. rs->global_variables.materials_using_buffer.erase(global_buffer_E);
  1584. global_buffer_E = nullptr;
  1585. }
  1586. }
  1587. }
  1588. RasterizerStorageRD::MaterialData::~MaterialData() {
  1589. if (global_buffer_E) {
  1590. //unregister global buffers
  1591. RasterizerStorageRD *rs = base_singleton;
  1592. rs->global_variables.materials_using_buffer.erase(global_buffer_E);
  1593. }
  1594. if (global_texture_E) {
  1595. //unregister global textures
  1596. RasterizerStorageRD *rs = base_singleton;
  1597. for (Map<StringName, uint64_t>::Element *E = used_global_textures.front(); E; E = E->next()) {
  1598. GlobalVariables::Variable *v = rs->global_variables.variables.getptr(E->key());
  1599. if (v) {
  1600. v->texture_materials.erase(self);
  1601. }
  1602. }
  1603. //unregister material from those using global textures
  1604. rs->global_variables.materials_using_texture.erase(global_texture_E);
  1605. }
  1606. }
  1607. void RasterizerStorageRD::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) {
  1608. RasterizerStorageRD *singleton = (RasterizerStorageRD *)RasterizerStorage::base_singleton;
  1609. #ifdef TOOLS_ENABLED
  1610. Texture *roughness_detect_texture = nullptr;
  1611. RS::TextureDetectRoughnessChannel roughness_channel = RS::TEXTURE_DETECT_ROUGNHESS_R;
  1612. Texture *normal_detect_texture = nullptr;
  1613. #endif
  1614. bool uses_global_textures = false;
  1615. global_textures_pass++;
  1616. for (int i = 0; i < p_texture_uniforms.size(); i++) {
  1617. const StringName &uniform_name = p_texture_uniforms[i].name;
  1618. RID texture;
  1619. if (p_texture_uniforms[i].global) {
  1620. RasterizerStorageRD *rs = base_singleton;
  1621. uses_global_textures = true;
  1622. GlobalVariables::Variable *v = rs->global_variables.variables.getptr(uniform_name);
  1623. if (v) {
  1624. if (v->buffer_index >= 0) {
  1625. WARN_PRINT("Shader uses global uniform texture '" + String(uniform_name) + "', but it changed type and is no longer a texture!.");
  1626. } else {
  1627. Map<StringName, uint64_t>::Element *E = used_global_textures.find(uniform_name);
  1628. if (!E) {
  1629. E = used_global_textures.insert(uniform_name, global_textures_pass);
  1630. v->texture_materials.insert(self);
  1631. } else {
  1632. E->get() = global_textures_pass;
  1633. }
  1634. texture = v->override.get_type() != Variant::NIL ? v->override : v->value;
  1635. }
  1636. } else {
  1637. WARN_PRINT("Shader uses global uniform texture '" + String(uniform_name) + "', but it was removed at some point. Material will not display correctly.");
  1638. }
  1639. } else {
  1640. if (!texture.is_valid()) {
  1641. const Map<StringName, Variant>::Element *V = p_parameters.find(uniform_name);
  1642. if (V) {
  1643. texture = V->get();
  1644. }
  1645. }
  1646. if (!texture.is_valid()) {
  1647. const Map<StringName, RID>::Element *W = p_default_textures.find(uniform_name);
  1648. if (W) {
  1649. texture = W->get();
  1650. }
  1651. }
  1652. }
  1653. RID rd_texture;
  1654. if (texture.is_null()) {
  1655. //check default usage
  1656. switch (p_texture_uniforms[i].hint) {
  1657. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK:
  1658. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO: {
  1659. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_BLACK);
  1660. } break;
  1661. case ShaderLanguage::ShaderNode::Uniform::HINT_NONE: {
  1662. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_NORMAL);
  1663. } break;
  1664. case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
  1665. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_ANISO);
  1666. } break;
  1667. default: {
  1668. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_WHITE);
  1669. } break;
  1670. }
  1671. } else {
  1672. 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);
  1673. Texture *tex = singleton->texture_owner.getornull(texture);
  1674. if (tex) {
  1675. rd_texture = (srgb && tex->rd_texture_srgb.is_valid()) ? tex->rd_texture_srgb : tex->rd_texture;
  1676. #ifdef TOOLS_ENABLED
  1677. if (tex->detect_3d_callback && p_use_linear_color) {
  1678. tex->detect_3d_callback(tex->detect_3d_callback_ud);
  1679. }
  1680. 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)) {
  1681. if (p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL) {
  1682. normal_detect_texture = tex;
  1683. }
  1684. tex->detect_normal_callback(tex->detect_normal_callback_ud);
  1685. }
  1686. 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)) {
  1687. //find the normal texture
  1688. roughness_detect_texture = tex;
  1689. roughness_channel = RS::TextureDetectRoughnessChannel(p_texture_uniforms[i].hint - ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_R);
  1690. }
  1691. #endif
  1692. }
  1693. if (rd_texture.is_null()) {
  1694. //wtf
  1695. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_WHITE);
  1696. }
  1697. }
  1698. p_textures[i] = rd_texture;
  1699. }
  1700. #ifdef TOOLS_ENABLED
  1701. if (roughness_detect_texture && normal_detect_texture && normal_detect_texture->path != String()) {
  1702. roughness_detect_texture->detect_roughness_callback(roughness_detect_texture->detect_roughness_callback_ud, normal_detect_texture->path, roughness_channel);
  1703. }
  1704. #endif
  1705. {
  1706. //for textures no longer used, unregister them
  1707. List<Map<StringName, uint64_t>::Element *> to_delete;
  1708. RasterizerStorageRD *rs = base_singleton;
  1709. for (Map<StringName, uint64_t>::Element *E = used_global_textures.front(); E; E = E->next()) {
  1710. if (E->get() != global_textures_pass) {
  1711. to_delete.push_back(E);
  1712. GlobalVariables::Variable *v = rs->global_variables.variables.getptr(E->key());
  1713. if (v) {
  1714. v->texture_materials.erase(self);
  1715. }
  1716. }
  1717. }
  1718. while (to_delete.front()) {
  1719. used_global_textures.erase(to_delete.front()->get());
  1720. to_delete.pop_front();
  1721. }
  1722. //handle registering/unregistering global textures
  1723. if (uses_global_textures != (global_texture_E != nullptr)) {
  1724. if (uses_global_textures) {
  1725. global_texture_E = rs->global_variables.materials_using_texture.push_back(self);
  1726. } else {
  1727. rs->global_variables.materials_using_texture.erase(global_texture_E);
  1728. global_texture_E = nullptr;
  1729. }
  1730. }
  1731. }
  1732. }
  1733. void RasterizerStorageRD::material_force_update_textures(RID p_material, ShaderType p_shader_type) {
  1734. Material *material = material_owner.getornull(p_material);
  1735. if (material->shader_type != p_shader_type) {
  1736. return;
  1737. }
  1738. if (material->data) {
  1739. material->data->update_parameters(material->params, false, true);
  1740. }
  1741. }
  1742. void RasterizerStorageRD::_update_queued_materials() {
  1743. Material *material = material_update_list;
  1744. while (material) {
  1745. Material *next = material->update_next;
  1746. if (material->data) {
  1747. material->data->update_parameters(material->params, material->uniform_dirty, material->texture_dirty);
  1748. }
  1749. material->update_requested = false;
  1750. material->texture_dirty = false;
  1751. material->uniform_dirty = false;
  1752. material->update_next = nullptr;
  1753. material = next;
  1754. }
  1755. material_update_list = nullptr;
  1756. }
  1757. /* MESH API */
  1758. RID RasterizerStorageRD::mesh_create() {
  1759. return mesh_owner.make_rid(Mesh());
  1760. }
  1761. /// Returns stride
  1762. void RasterizerStorageRD::mesh_add_surface(RID p_mesh, const RS::SurfaceData &p_surface) {
  1763. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1764. ERR_FAIL_COND(!mesh);
  1765. //ensure blend shape consistency
  1766. ERR_FAIL_COND(mesh->blend_shape_count && p_surface.blend_shapes.size() != (int)mesh->blend_shape_count);
  1767. ERR_FAIL_COND(mesh->blend_shape_count && p_surface.bone_aabbs.size() != mesh->bone_aabbs.size());
  1768. #ifdef DEBUG_ENABLED
  1769. //do a validation, to catch errors first
  1770. {
  1771. uint32_t stride = 0;
  1772. for (int i = 0; i < RS::ARRAY_WEIGHTS; i++) {
  1773. if ((p_surface.format & (1 << i))) {
  1774. switch (i) {
  1775. case RS::ARRAY_VERTEX: {
  1776. if (p_surface.format & RS::ARRAY_FLAG_USE_2D_VERTICES) {
  1777. stride += sizeof(float) * 2;
  1778. } else {
  1779. stride += sizeof(float) * 3;
  1780. }
  1781. } break;
  1782. case RS::ARRAY_NORMAL: {
  1783. if (p_surface.format & RS::ARRAY_COMPRESS_NORMAL) {
  1784. stride += sizeof(int8_t) * 4;
  1785. } else {
  1786. stride += sizeof(float) * 4;
  1787. }
  1788. } break;
  1789. case RS::ARRAY_TANGENT: {
  1790. if (p_surface.format & RS::ARRAY_COMPRESS_TANGENT) {
  1791. stride += sizeof(int8_t) * 4;
  1792. } else {
  1793. stride += sizeof(float) * 4;
  1794. }
  1795. } break;
  1796. case RS::ARRAY_COLOR: {
  1797. if (p_surface.format & RS::ARRAY_COMPRESS_COLOR) {
  1798. stride += sizeof(int8_t) * 4;
  1799. } else {
  1800. stride += sizeof(float) * 4;
  1801. }
  1802. } break;
  1803. case RS::ARRAY_TEX_UV: {
  1804. if (p_surface.format & RS::ARRAY_COMPRESS_TEX_UV) {
  1805. stride += sizeof(int16_t) * 2;
  1806. } else {
  1807. stride += sizeof(float) * 2;
  1808. }
  1809. } break;
  1810. case RS::ARRAY_TEX_UV2: {
  1811. if (p_surface.format & RS::ARRAY_COMPRESS_TEX_UV2) {
  1812. stride += sizeof(int16_t) * 2;
  1813. } else {
  1814. stride += sizeof(float) * 2;
  1815. }
  1816. } break;
  1817. case RS::ARRAY_BONES: {
  1818. //assumed weights too
  1819. //unique format, internally 16 bits, exposed as single array for 32
  1820. stride += sizeof(int32_t) * 4;
  1821. } break;
  1822. }
  1823. }
  1824. }
  1825. int expected_size = stride * p_surface.vertex_count;
  1826. ERR_FAIL_COND_MSG(expected_size != p_surface.vertex_data.size(), "Size of data provided (" + itos(p_surface.vertex_data.size()) + ") does not match expected (" + itos(expected_size) + ")");
  1827. }
  1828. #endif
  1829. Mesh::Surface *s = memnew(Mesh::Surface);
  1830. s->format = p_surface.format;
  1831. s->primitive = p_surface.primitive;
  1832. s->vertex_buffer = RD::get_singleton()->vertex_buffer_create(p_surface.vertex_data.size(), p_surface.vertex_data);
  1833. s->vertex_count = p_surface.vertex_count;
  1834. if (p_surface.index_count) {
  1835. bool is_index_16 = p_surface.vertex_count <= 65536;
  1836. 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);
  1837. s->index_count = p_surface.index_count;
  1838. s->index_array = RD::get_singleton()->index_array_create(s->index_buffer, 0, s->index_count);
  1839. if (p_surface.lods.size()) {
  1840. s->lods = memnew_arr(Mesh::Surface::LOD, p_surface.lods.size());
  1841. s->lod_count = p_surface.lods.size();
  1842. for (int i = 0; i < p_surface.lods.size(); i++) {
  1843. uint32_t indices = p_surface.lods[i].index_data.size() / (is_index_16 ? 2 : 4);
  1844. 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);
  1845. s->lods[i].index_array = RD::get_singleton()->index_array_create(s->lods[i].index_buffer, 0, indices);
  1846. s->lods[i].edge_length = p_surface.lods[i].edge_length;
  1847. }
  1848. }
  1849. }
  1850. s->aabb = p_surface.aabb;
  1851. s->bone_aabbs = p_surface.bone_aabbs; //only really useful for returning them.
  1852. for (int i = 0; i < p_surface.blend_shapes.size(); i++) {
  1853. if (p_surface.blend_shapes[i].size() != p_surface.vertex_data.size()) {
  1854. memdelete(s);
  1855. ERR_FAIL_COND(p_surface.blend_shapes[i].size() != p_surface.vertex_data.size());
  1856. }
  1857. RID vertex_buffer = RD::get_singleton()->vertex_buffer_create(p_surface.blend_shapes[i].size(), p_surface.blend_shapes[i]);
  1858. s->blend_shapes.push_back(vertex_buffer);
  1859. }
  1860. mesh->blend_shape_count = p_surface.blend_shapes.size();
  1861. if (mesh->surface_count == 0) {
  1862. mesh->bone_aabbs = p_surface.bone_aabbs;
  1863. mesh->aabb = p_surface.aabb;
  1864. } else {
  1865. for (int i = 0; i < p_surface.bone_aabbs.size(); i++) {
  1866. mesh->bone_aabbs.write[i].merge_with(p_surface.bone_aabbs[i]);
  1867. }
  1868. mesh->aabb.merge_with(p_surface.aabb);
  1869. }
  1870. s->material = p_surface.material;
  1871. mesh->surfaces = (Mesh::Surface **)memrealloc(mesh->surfaces, sizeof(Mesh::Surface *) * (mesh->surface_count + 1));
  1872. mesh->surfaces[mesh->surface_count] = s;
  1873. mesh->surface_count++;
  1874. mesh->instance_dependency.instance_notify_changed(true, true);
  1875. mesh->material_cache.clear();
  1876. }
  1877. int RasterizerStorageRD::mesh_get_blend_shape_count(RID p_mesh) const {
  1878. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  1879. ERR_FAIL_COND_V(!mesh, -1);
  1880. return mesh->blend_shape_count;
  1881. }
  1882. void RasterizerStorageRD::mesh_set_blend_shape_mode(RID p_mesh, RS::BlendShapeMode p_mode) {
  1883. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1884. ERR_FAIL_COND(!mesh);
  1885. ERR_FAIL_INDEX((int)p_mode, 2);
  1886. mesh->blend_shape_mode = p_mode;
  1887. }
  1888. RS::BlendShapeMode RasterizerStorageRD::mesh_get_blend_shape_mode(RID p_mesh) const {
  1889. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1890. ERR_FAIL_COND_V(!mesh, RS::BLEND_SHAPE_MODE_NORMALIZED);
  1891. return mesh->blend_shape_mode;
  1892. }
  1893. void RasterizerStorageRD::mesh_surface_update_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
  1894. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1895. ERR_FAIL_COND(!mesh);
  1896. ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
  1897. ERR_FAIL_COND(p_data.size() == 0);
  1898. uint64_t data_size = p_data.size();
  1899. const uint8_t *r = p_data.ptr();
  1900. RD::get_singleton()->buffer_update(mesh->surfaces[p_surface]->vertex_buffer, p_offset, data_size, r);
  1901. }
  1902. void RasterizerStorageRD::mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) {
  1903. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1904. ERR_FAIL_COND(!mesh);
  1905. ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
  1906. mesh->surfaces[p_surface]->material = p_material;
  1907. mesh->instance_dependency.instance_notify_changed(false, true);
  1908. mesh->material_cache.clear();
  1909. }
  1910. RID RasterizerStorageRD::mesh_surface_get_material(RID p_mesh, int p_surface) const {
  1911. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1912. ERR_FAIL_COND_V(!mesh, RID());
  1913. ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_surface, mesh->surface_count, RID());
  1914. return mesh->surfaces[p_surface]->material;
  1915. }
  1916. RS::SurfaceData RasterizerStorageRD::mesh_get_surface(RID p_mesh, int p_surface) const {
  1917. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1918. ERR_FAIL_COND_V(!mesh, RS::SurfaceData());
  1919. ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_surface, mesh->surface_count, RS::SurfaceData());
  1920. Mesh::Surface &s = *mesh->surfaces[p_surface];
  1921. RS::SurfaceData sd;
  1922. sd.format = s.format;
  1923. sd.vertex_data = RD::get_singleton()->buffer_get_data(s.vertex_buffer);
  1924. sd.vertex_count = s.vertex_count;
  1925. sd.index_count = s.index_count;
  1926. sd.primitive = s.primitive;
  1927. if (sd.index_count) {
  1928. sd.index_data = RD::get_singleton()->buffer_get_data(s.index_buffer);
  1929. }
  1930. sd.aabb = s.aabb;
  1931. for (uint32_t i = 0; i < s.lod_count; i++) {
  1932. RS::SurfaceData::LOD lod;
  1933. lod.edge_length = s.lods[i].edge_length;
  1934. lod.index_data = RD::get_singleton()->buffer_get_data(s.lods[i].index_buffer);
  1935. sd.lods.push_back(lod);
  1936. }
  1937. sd.bone_aabbs = s.bone_aabbs;
  1938. for (int i = 0; i < s.blend_shapes.size(); i++) {
  1939. Vector<uint8_t> bs = RD::get_singleton()->buffer_get_data(s.blend_shapes[i]);
  1940. sd.blend_shapes.push_back(bs);
  1941. }
  1942. return sd;
  1943. }
  1944. int RasterizerStorageRD::mesh_get_surface_count(RID p_mesh) const {
  1945. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1946. ERR_FAIL_COND_V(!mesh, 0);
  1947. return mesh->surface_count;
  1948. }
  1949. void RasterizerStorageRD::mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) {
  1950. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1951. ERR_FAIL_COND(!mesh);
  1952. mesh->custom_aabb = p_aabb;
  1953. }
  1954. AABB RasterizerStorageRD::mesh_get_custom_aabb(RID p_mesh) const {
  1955. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1956. ERR_FAIL_COND_V(!mesh, AABB());
  1957. return mesh->custom_aabb;
  1958. }
  1959. AABB RasterizerStorageRD::mesh_get_aabb(RID p_mesh, RID p_skeleton) {
  1960. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1961. ERR_FAIL_COND_V(!mesh, AABB());
  1962. if (mesh->custom_aabb != AABB()) {
  1963. return mesh->custom_aabb;
  1964. }
  1965. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  1966. if (!skeleton || skeleton->size == 0) {
  1967. return mesh->aabb;
  1968. }
  1969. AABB aabb;
  1970. for (uint32_t i = 0; i < mesh->surface_count; i++) {
  1971. AABB laabb;
  1972. if ((mesh->surfaces[i]->format & RS::ARRAY_FORMAT_BONES) && mesh->surfaces[i]->bone_aabbs.size()) {
  1973. int bs = mesh->surfaces[i]->bone_aabbs.size();
  1974. const AABB *skbones = mesh->surfaces[i]->bone_aabbs.ptr();
  1975. int sbs = skeleton->size;
  1976. ERR_CONTINUE(bs > sbs);
  1977. const float *baseptr = skeleton->data.ptr();
  1978. bool first = true;
  1979. if (skeleton->use_2d) {
  1980. for (int j = 0; j < bs; j++) {
  1981. if (skbones[0].size == Vector3())
  1982. continue; //bone is unused
  1983. const float *dataptr = baseptr + j * 8;
  1984. Transform mtx;
  1985. mtx.basis.elements[0].x = dataptr[0];
  1986. mtx.basis.elements[1].x = dataptr[1];
  1987. mtx.origin.x = dataptr[3];
  1988. mtx.basis.elements[0].y = dataptr[4];
  1989. mtx.basis.elements[1].y = dataptr[5];
  1990. mtx.origin.y = dataptr[7];
  1991. AABB baabb = mtx.xform(skbones[j]);
  1992. if (first) {
  1993. laabb = baabb;
  1994. first = false;
  1995. } else {
  1996. laabb.merge_with(baabb);
  1997. }
  1998. }
  1999. } else {
  2000. for (int j = 0; j < bs; j++) {
  2001. if (skbones[0].size == Vector3())
  2002. continue; //bone is unused
  2003. const float *dataptr = baseptr + j * 12;
  2004. Transform mtx;
  2005. mtx.basis.elements[0][0] = dataptr[0];
  2006. mtx.basis.elements[0][1] = dataptr[1];
  2007. mtx.basis.elements[0][2] = dataptr[2];
  2008. mtx.origin.x = dataptr[3];
  2009. mtx.basis.elements[1][0] = dataptr[4];
  2010. mtx.basis.elements[1][1] = dataptr[5];
  2011. mtx.basis.elements[1][2] = dataptr[6];
  2012. mtx.origin.y = dataptr[7];
  2013. mtx.basis.elements[2][0] = dataptr[8];
  2014. mtx.basis.elements[2][1] = dataptr[9];
  2015. mtx.basis.elements[2][2] = dataptr[10];
  2016. mtx.origin.z = dataptr[11];
  2017. AABB baabb = mtx.xform(skbones[j]);
  2018. if (first) {
  2019. laabb = baabb;
  2020. first = false;
  2021. } else {
  2022. laabb.merge_with(baabb);
  2023. }
  2024. }
  2025. }
  2026. if (laabb.size == Vector3()) {
  2027. laabb = mesh->surfaces[i]->aabb;
  2028. }
  2029. } else {
  2030. laabb = mesh->surfaces[i]->aabb;
  2031. }
  2032. if (i == 0) {
  2033. aabb = laabb;
  2034. } else {
  2035. aabb.merge_with(laabb);
  2036. }
  2037. }
  2038. return aabb;
  2039. }
  2040. void RasterizerStorageRD::mesh_clear(RID p_mesh) {
  2041. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2042. ERR_FAIL_COND(!mesh);
  2043. for (uint32_t i = 0; i < mesh->surface_count; i++) {
  2044. Mesh::Surface &s = *mesh->surfaces[i];
  2045. RD::get_singleton()->free(s.vertex_buffer); //clears arrays as dependency automatically, including all versions
  2046. if (s.versions) {
  2047. memfree(s.versions); //reallocs, so free with memfree.
  2048. }
  2049. if (s.index_buffer.is_valid()) {
  2050. RD::get_singleton()->free(s.index_buffer);
  2051. }
  2052. if (s.lod_count) {
  2053. for (uint32_t j = 0; j < s.lod_count; j++) {
  2054. RD::get_singleton()->free(s.lods[j].index_buffer);
  2055. }
  2056. memdelete_arr(s.lods);
  2057. }
  2058. for (int32_t j = 0; j < s.blend_shapes.size(); j++) {
  2059. RD::get_singleton()->free(s.blend_shapes[j]);
  2060. }
  2061. if (s.blend_shape_base_buffer.is_valid()) {
  2062. RD::get_singleton()->free(s.blend_shape_base_buffer);
  2063. }
  2064. memdelete(mesh->surfaces[i]);
  2065. }
  2066. if (mesh->surfaces) {
  2067. memfree(mesh->surfaces);
  2068. }
  2069. mesh->surfaces = nullptr;
  2070. mesh->surface_count = 0;
  2071. mesh->material_cache.clear();
  2072. mesh->instance_dependency.instance_notify_changed(true, true);
  2073. }
  2074. void RasterizerStorageRD::_mesh_surface_generate_version_for_input_mask(Mesh::Surface *s, uint32_t p_input_mask) {
  2075. uint32_t version = s->version_count;
  2076. s->version_count++;
  2077. s->versions = (Mesh::Surface::Version *)memrealloc(s->versions, sizeof(Mesh::Surface::Version) * s->version_count);
  2078. Mesh::Surface::Version &v = s->versions[version];
  2079. Vector<RD::VertexAttribute> attributes;
  2080. Vector<RID> buffers;
  2081. uint32_t stride = 0;
  2082. for (int i = 0; i < RS::ARRAY_WEIGHTS; i++) {
  2083. RD::VertexAttribute vd;
  2084. RID buffer;
  2085. vd.location = i;
  2086. if (!(s->format & (1 << i))) {
  2087. // Not supplied by surface, use default value
  2088. buffer = mesh_default_rd_buffers[i];
  2089. switch (i) {
  2090. case RS::ARRAY_VERTEX: {
  2091. vd.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  2092. } break;
  2093. case RS::ARRAY_NORMAL: {
  2094. vd.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  2095. } break;
  2096. case RS::ARRAY_TANGENT: {
  2097. vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  2098. } break;
  2099. case RS::ARRAY_COLOR: {
  2100. vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  2101. } break;
  2102. case RS::ARRAY_TEX_UV: {
  2103. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2104. } break;
  2105. case RS::ARRAY_TEX_UV2: {
  2106. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2107. } break;
  2108. case RS::ARRAY_BONES: {
  2109. //assumed weights too
  2110. vd.format = RD::DATA_FORMAT_R32G32B32A32_UINT;
  2111. } break;
  2112. }
  2113. } else {
  2114. //Supplied, use it
  2115. vd.offset = stride;
  2116. vd.stride = 1; //mark that it needs a stride set
  2117. buffer = s->vertex_buffer;
  2118. switch (i) {
  2119. case RS::ARRAY_VERTEX: {
  2120. if (s->format & RS::ARRAY_FLAG_USE_2D_VERTICES) {
  2121. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2122. stride += sizeof(float) * 2;
  2123. } else {
  2124. vd.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  2125. stride += sizeof(float) * 3;
  2126. }
  2127. } break;
  2128. case RS::ARRAY_NORMAL: {
  2129. if (s->format & RS::ARRAY_COMPRESS_NORMAL) {
  2130. vd.format = RD::DATA_FORMAT_R8G8B8A8_SNORM;
  2131. stride += sizeof(int8_t) * 4;
  2132. } else {
  2133. vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  2134. stride += sizeof(float) * 4;
  2135. }
  2136. } break;
  2137. case RS::ARRAY_TANGENT: {
  2138. if (s->format & RS::ARRAY_COMPRESS_TANGENT) {
  2139. vd.format = RD::DATA_FORMAT_R8G8B8A8_SNORM;
  2140. stride += sizeof(int8_t) * 4;
  2141. } else {
  2142. vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  2143. stride += sizeof(float) * 4;
  2144. }
  2145. } break;
  2146. case RS::ARRAY_COLOR: {
  2147. if (s->format & RS::ARRAY_COMPRESS_COLOR) {
  2148. vd.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  2149. stride += sizeof(int8_t) * 4;
  2150. } else {
  2151. vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  2152. stride += sizeof(float) * 4;
  2153. }
  2154. } break;
  2155. case RS::ARRAY_TEX_UV: {
  2156. if (s->format & RS::ARRAY_COMPRESS_TEX_UV) {
  2157. vd.format = RD::DATA_FORMAT_R16G16_SFLOAT;
  2158. stride += sizeof(int16_t) * 2;
  2159. } else {
  2160. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2161. stride += sizeof(float) * 2;
  2162. }
  2163. } break;
  2164. case RS::ARRAY_TEX_UV2: {
  2165. if (s->format & RS::ARRAY_COMPRESS_TEX_UV2) {
  2166. vd.format = RD::DATA_FORMAT_R16G16_SFLOAT;
  2167. stride += sizeof(int16_t) * 2;
  2168. } else {
  2169. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2170. stride += sizeof(float) * 2;
  2171. }
  2172. } break;
  2173. case RS::ARRAY_BONES: {
  2174. //assumed weights too
  2175. //unique format, internally 16 bits, exposed as single array for 32
  2176. vd.format = RD::DATA_FORMAT_R32G32B32A32_UINT;
  2177. stride += sizeof(int32_t) * 4;
  2178. } break;
  2179. }
  2180. }
  2181. if (!(p_input_mask & (1 << i))) {
  2182. continue; // Shader does not need this, skip it
  2183. }
  2184. attributes.push_back(vd);
  2185. buffers.push_back(buffer);
  2186. }
  2187. //update final stride
  2188. for (int i = 0; i < attributes.size(); i++) {
  2189. if (attributes[i].stride == 1) {
  2190. attributes.write[i].stride = stride;
  2191. }
  2192. }
  2193. v.input_mask = p_input_mask;
  2194. v.vertex_format = RD::get_singleton()->vertex_format_create(attributes);
  2195. v.vertex_array = RD::get_singleton()->vertex_array_create(s->vertex_count, v.vertex_format, buffers);
  2196. }
  2197. ////////////////// MULTIMESH
  2198. RID RasterizerStorageRD::multimesh_create() {
  2199. return multimesh_owner.make_rid(MultiMesh());
  2200. }
  2201. void RasterizerStorageRD::multimesh_allocate(RID p_multimesh, int p_instances, RS::MultimeshTransformFormat p_transform_format, bool p_use_colors, bool p_use_custom_data) {
  2202. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2203. ERR_FAIL_COND(!multimesh);
  2204. 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) {
  2205. return;
  2206. }
  2207. if (multimesh->buffer.is_valid()) {
  2208. RD::get_singleton()->free(multimesh->buffer);
  2209. multimesh->buffer = RID();
  2210. multimesh->uniform_set_3d = RID(); //cleared by dependency
  2211. }
  2212. if (multimesh->data_cache_dirty_regions) {
  2213. memdelete_arr(multimesh->data_cache_dirty_regions);
  2214. multimesh->data_cache_dirty_regions = nullptr;
  2215. multimesh->data_cache_used_dirty_regions = 0;
  2216. }
  2217. multimesh->instances = p_instances;
  2218. multimesh->xform_format = p_transform_format;
  2219. multimesh->uses_colors = p_use_colors;
  2220. multimesh->color_offset_cache = p_transform_format == RS::MULTIMESH_TRANSFORM_2D ? 8 : 12;
  2221. multimesh->uses_custom_data = p_use_custom_data;
  2222. multimesh->custom_data_offset_cache = multimesh->color_offset_cache + (p_use_colors ? 4 : 0);
  2223. multimesh->stride_cache = multimesh->custom_data_offset_cache + (p_use_custom_data ? 4 : 0);
  2224. multimesh->buffer_set = false;
  2225. //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));
  2226. multimesh->data_cache = Vector<float>();
  2227. multimesh->aabb = AABB();
  2228. multimesh->aabb_dirty = false;
  2229. multimesh->visible_instances = MIN(multimesh->visible_instances, multimesh->instances);
  2230. if (multimesh->instances) {
  2231. multimesh->buffer = RD::get_singleton()->storage_buffer_create(multimesh->instances * multimesh->stride_cache * 4);
  2232. }
  2233. }
  2234. int RasterizerStorageRD::multimesh_get_instance_count(RID p_multimesh) const {
  2235. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2236. ERR_FAIL_COND_V(!multimesh, 0);
  2237. return multimesh->instances;
  2238. }
  2239. void RasterizerStorageRD::multimesh_set_mesh(RID p_multimesh, RID p_mesh) {
  2240. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2241. ERR_FAIL_COND(!multimesh);
  2242. if (multimesh->mesh == p_mesh) {
  2243. return;
  2244. }
  2245. multimesh->mesh = p_mesh;
  2246. if (multimesh->instances == 0) {
  2247. return;
  2248. }
  2249. if (multimesh->data_cache.size()) {
  2250. //we have a data cache, just mark it dirt
  2251. _multimesh_mark_all_dirty(multimesh, false, true);
  2252. } else if (multimesh->instances) {
  2253. //need to re-create AABB unfortunately, calling this has a penalty
  2254. if (multimesh->buffer_set) {
  2255. Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(multimesh->buffer);
  2256. const uint8_t *r = buffer.ptr();
  2257. const float *data = (const float *)r;
  2258. _multimesh_re_create_aabb(multimesh, data, multimesh->instances);
  2259. }
  2260. }
  2261. multimesh->instance_dependency.instance_notify_changed(true, true);
  2262. }
  2263. #define MULTIMESH_DIRTY_REGION_SIZE 512
  2264. void RasterizerStorageRD::_multimesh_make_local(MultiMesh *multimesh) const {
  2265. if (multimesh->data_cache.size() > 0) {
  2266. return; //already local
  2267. }
  2268. ERR_FAIL_COND(multimesh->data_cache.size() > 0);
  2269. // this means that the user wants to load/save individual elements,
  2270. // for this, the data must reside on CPU, so just copy it there.
  2271. multimesh->data_cache.resize(multimesh->instances * multimesh->stride_cache);
  2272. {
  2273. float *w = multimesh->data_cache.ptrw();
  2274. if (multimesh->buffer_set) {
  2275. Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(multimesh->buffer);
  2276. {
  2277. const uint8_t *r = buffer.ptr();
  2278. copymem(w, r, buffer.size());
  2279. }
  2280. } else {
  2281. zeromem(w, multimesh->instances * multimesh->stride_cache * sizeof(float));
  2282. }
  2283. }
  2284. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2285. multimesh->data_cache_dirty_regions = memnew_arr(bool, data_cache_dirty_region_count);
  2286. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  2287. multimesh->data_cache_dirty_regions[i] = 0;
  2288. }
  2289. multimesh->data_cache_used_dirty_regions = 0;
  2290. }
  2291. void RasterizerStorageRD::_multimesh_mark_dirty(MultiMesh *multimesh, int p_index, bool p_aabb) {
  2292. uint32_t region_index = p_index / MULTIMESH_DIRTY_REGION_SIZE;
  2293. #ifdef DEBUG_ENABLED
  2294. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2295. ERR_FAIL_UNSIGNED_INDEX(region_index, data_cache_dirty_region_count); //bug
  2296. #endif
  2297. if (!multimesh->data_cache_dirty_regions[region_index]) {
  2298. multimesh->data_cache_dirty_regions[region_index] = true;
  2299. multimesh->data_cache_used_dirty_regions++;
  2300. }
  2301. if (p_aabb) {
  2302. multimesh->aabb_dirty = true;
  2303. }
  2304. if (!multimesh->dirty) {
  2305. multimesh->dirty_list = multimesh_dirty_list;
  2306. multimesh_dirty_list = multimesh;
  2307. multimesh->dirty = true;
  2308. }
  2309. }
  2310. void RasterizerStorageRD::_multimesh_mark_all_dirty(MultiMesh *multimesh, bool p_data, bool p_aabb) {
  2311. if (p_data) {
  2312. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2313. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  2314. if (!multimesh->data_cache_dirty_regions[i]) {
  2315. multimesh->data_cache_dirty_regions[i] = true;
  2316. multimesh->data_cache_used_dirty_regions++;
  2317. }
  2318. }
  2319. }
  2320. if (p_aabb) {
  2321. multimesh->aabb_dirty = true;
  2322. }
  2323. if (!multimesh->dirty) {
  2324. multimesh->dirty_list = multimesh_dirty_list;
  2325. multimesh_dirty_list = multimesh;
  2326. multimesh->dirty = true;
  2327. }
  2328. }
  2329. void RasterizerStorageRD::_multimesh_re_create_aabb(MultiMesh *multimesh, const float *p_data, int p_instances) {
  2330. ERR_FAIL_COND(multimesh->mesh.is_null());
  2331. AABB aabb;
  2332. AABB mesh_aabb = mesh_get_aabb(multimesh->mesh);
  2333. for (int i = 0; i < p_instances; i++) {
  2334. const float *data = p_data + multimesh->stride_cache * i;
  2335. Transform t;
  2336. if (multimesh->xform_format == RS::MULTIMESH_TRANSFORM_3D) {
  2337. t.basis.elements[0][0] = data[0];
  2338. t.basis.elements[0][1] = data[1];
  2339. t.basis.elements[0][2] = data[2];
  2340. t.origin.x = data[3];
  2341. t.basis.elements[1][0] = data[4];
  2342. t.basis.elements[1][1] = data[5];
  2343. t.basis.elements[1][2] = data[6];
  2344. t.origin.y = data[7];
  2345. t.basis.elements[2][0] = data[8];
  2346. t.basis.elements[2][1] = data[9];
  2347. t.basis.elements[2][2] = data[10];
  2348. t.origin.z = data[11];
  2349. } else {
  2350. t.basis.elements[0].x = data[0];
  2351. t.basis.elements[1].x = data[1];
  2352. t.origin.x = data[3];
  2353. t.basis.elements[0].y = data[4];
  2354. t.basis.elements[1].y = data[5];
  2355. t.origin.y = data[7];
  2356. }
  2357. if (i == 0) {
  2358. aabb = t.xform(mesh_aabb);
  2359. } else {
  2360. aabb.merge_with(t.xform(mesh_aabb));
  2361. }
  2362. }
  2363. multimesh->aabb = aabb;
  2364. }
  2365. void RasterizerStorageRD::multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform) {
  2366. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2367. ERR_FAIL_COND(!multimesh);
  2368. ERR_FAIL_INDEX(p_index, multimesh->instances);
  2369. ERR_FAIL_COND(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_3D);
  2370. _multimesh_make_local(multimesh);
  2371. {
  2372. float *w = multimesh->data_cache.ptrw();
  2373. float *dataptr = w + p_index * multimesh->stride_cache;
  2374. dataptr[0] = p_transform.basis.elements[0][0];
  2375. dataptr[1] = p_transform.basis.elements[0][1];
  2376. dataptr[2] = p_transform.basis.elements[0][2];
  2377. dataptr[3] = p_transform.origin.x;
  2378. dataptr[4] = p_transform.basis.elements[1][0];
  2379. dataptr[5] = p_transform.basis.elements[1][1];
  2380. dataptr[6] = p_transform.basis.elements[1][2];
  2381. dataptr[7] = p_transform.origin.y;
  2382. dataptr[8] = p_transform.basis.elements[2][0];
  2383. dataptr[9] = p_transform.basis.elements[2][1];
  2384. dataptr[10] = p_transform.basis.elements[2][2];
  2385. dataptr[11] = p_transform.origin.z;
  2386. }
  2387. _multimesh_mark_dirty(multimesh, p_index, true);
  2388. }
  2389. void RasterizerStorageRD::multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) {
  2390. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2391. ERR_FAIL_COND(!multimesh);
  2392. ERR_FAIL_INDEX(p_index, multimesh->instances);
  2393. ERR_FAIL_COND(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_2D);
  2394. _multimesh_make_local(multimesh);
  2395. {
  2396. float *w = multimesh->data_cache.ptrw();
  2397. float *dataptr = w + p_index * multimesh->stride_cache;
  2398. dataptr[0] = p_transform.elements[0][0];
  2399. dataptr[1] = p_transform.elements[1][0];
  2400. dataptr[2] = 0;
  2401. dataptr[3] = p_transform.elements[2][0];
  2402. dataptr[4] = p_transform.elements[0][1];
  2403. dataptr[5] = p_transform.elements[1][1];
  2404. dataptr[6] = 0;
  2405. dataptr[7] = p_transform.elements[2][1];
  2406. }
  2407. _multimesh_mark_dirty(multimesh, p_index, true);
  2408. }
  2409. void RasterizerStorageRD::multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) {
  2410. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2411. ERR_FAIL_COND(!multimesh);
  2412. ERR_FAIL_INDEX(p_index, multimesh->instances);
  2413. ERR_FAIL_COND(!multimesh->uses_colors);
  2414. _multimesh_make_local(multimesh);
  2415. {
  2416. float *w = multimesh->data_cache.ptrw();
  2417. float *dataptr = w + p_index * multimesh->stride_cache + multimesh->color_offset_cache;
  2418. dataptr[0] = p_color.r;
  2419. dataptr[1] = p_color.g;
  2420. dataptr[2] = p_color.b;
  2421. dataptr[3] = p_color.a;
  2422. }
  2423. _multimesh_mark_dirty(multimesh, p_index, false);
  2424. }
  2425. void RasterizerStorageRD::multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) {
  2426. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2427. ERR_FAIL_COND(!multimesh);
  2428. ERR_FAIL_INDEX(p_index, multimesh->instances);
  2429. ERR_FAIL_COND(!multimesh->uses_custom_data);
  2430. _multimesh_make_local(multimesh);
  2431. {
  2432. float *w = multimesh->data_cache.ptrw();
  2433. float *dataptr = w + p_index * multimesh->stride_cache + multimesh->custom_data_offset_cache;
  2434. dataptr[0] = p_color.r;
  2435. dataptr[1] = p_color.g;
  2436. dataptr[2] = p_color.b;
  2437. dataptr[3] = p_color.a;
  2438. }
  2439. _multimesh_mark_dirty(multimesh, p_index, false);
  2440. }
  2441. RID RasterizerStorageRD::multimesh_get_mesh(RID p_multimesh) const {
  2442. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2443. ERR_FAIL_COND_V(!multimesh, RID());
  2444. return multimesh->mesh;
  2445. }
  2446. Transform RasterizerStorageRD::multimesh_instance_get_transform(RID p_multimesh, int p_index) const {
  2447. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2448. ERR_FAIL_COND_V(!multimesh, Transform());
  2449. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Transform());
  2450. ERR_FAIL_COND_V(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_3D, Transform());
  2451. _multimesh_make_local(multimesh);
  2452. Transform t;
  2453. {
  2454. const float *r = multimesh->data_cache.ptr();
  2455. const float *dataptr = r + p_index * multimesh->stride_cache;
  2456. t.basis.elements[0][0] = dataptr[0];
  2457. t.basis.elements[0][1] = dataptr[1];
  2458. t.basis.elements[0][2] = dataptr[2];
  2459. t.origin.x = dataptr[3];
  2460. t.basis.elements[1][0] = dataptr[4];
  2461. t.basis.elements[1][1] = dataptr[5];
  2462. t.basis.elements[1][2] = dataptr[6];
  2463. t.origin.y = dataptr[7];
  2464. t.basis.elements[2][0] = dataptr[8];
  2465. t.basis.elements[2][1] = dataptr[9];
  2466. t.basis.elements[2][2] = dataptr[10];
  2467. t.origin.z = dataptr[11];
  2468. }
  2469. return t;
  2470. }
  2471. Transform2D RasterizerStorageRD::multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const {
  2472. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2473. ERR_FAIL_COND_V(!multimesh, Transform2D());
  2474. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Transform2D());
  2475. ERR_FAIL_COND_V(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_2D, Transform2D());
  2476. _multimesh_make_local(multimesh);
  2477. Transform2D t;
  2478. {
  2479. const float *r = multimesh->data_cache.ptr();
  2480. const float *dataptr = r + p_index * multimesh->stride_cache;
  2481. t.elements[0][0] = dataptr[0];
  2482. t.elements[1][0] = dataptr[1];
  2483. t.elements[2][0] = dataptr[3];
  2484. t.elements[0][1] = dataptr[4];
  2485. t.elements[1][1] = dataptr[5];
  2486. t.elements[2][1] = dataptr[7];
  2487. }
  2488. return t;
  2489. }
  2490. Color RasterizerStorageRD::multimesh_instance_get_color(RID p_multimesh, int p_index) const {
  2491. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2492. ERR_FAIL_COND_V(!multimesh, Color());
  2493. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Color());
  2494. ERR_FAIL_COND_V(!multimesh->uses_colors, Color());
  2495. _multimesh_make_local(multimesh);
  2496. Color c;
  2497. {
  2498. const float *r = multimesh->data_cache.ptr();
  2499. const float *dataptr = r + p_index * multimesh->stride_cache + multimesh->color_offset_cache;
  2500. c.r = dataptr[0];
  2501. c.g = dataptr[1];
  2502. c.b = dataptr[2];
  2503. c.a = dataptr[3];
  2504. }
  2505. return c;
  2506. }
  2507. Color RasterizerStorageRD::multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const {
  2508. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2509. ERR_FAIL_COND_V(!multimesh, Color());
  2510. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Color());
  2511. ERR_FAIL_COND_V(!multimesh->uses_custom_data, Color());
  2512. _multimesh_make_local(multimesh);
  2513. Color c;
  2514. {
  2515. const float *r = multimesh->data_cache.ptr();
  2516. const float *dataptr = r + p_index * multimesh->stride_cache + multimesh->custom_data_offset_cache;
  2517. c.r = dataptr[0];
  2518. c.g = dataptr[1];
  2519. c.b = dataptr[2];
  2520. c.a = dataptr[3];
  2521. }
  2522. return c;
  2523. }
  2524. void RasterizerStorageRD::multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer) {
  2525. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2526. ERR_FAIL_COND(!multimesh);
  2527. ERR_FAIL_COND(p_buffer.size() != (multimesh->instances * (int)multimesh->stride_cache));
  2528. {
  2529. const float *r = p_buffer.ptr();
  2530. RD::get_singleton()->buffer_update(multimesh->buffer, 0, p_buffer.size() * sizeof(float), r, false);
  2531. multimesh->buffer_set = true;
  2532. }
  2533. if (multimesh->data_cache.size()) {
  2534. //if we have a data cache, just update it
  2535. multimesh->data_cache = p_buffer;
  2536. {
  2537. //clear dirty since nothing will be dirty anymore
  2538. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2539. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  2540. multimesh->data_cache_dirty_regions[i] = false;
  2541. }
  2542. multimesh->data_cache_used_dirty_regions = 0;
  2543. }
  2544. _multimesh_mark_all_dirty(multimesh, false, true); //update AABB
  2545. } else if (multimesh->mesh.is_valid()) {
  2546. //if we have a mesh set, we need to re-generate the AABB from the new data
  2547. const float *data = p_buffer.ptr();
  2548. _multimesh_re_create_aabb(multimesh, data, multimesh->instances);
  2549. multimesh->instance_dependency.instance_notify_changed(true, false);
  2550. }
  2551. }
  2552. Vector<float> RasterizerStorageRD::multimesh_get_buffer(RID p_multimesh) const {
  2553. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2554. ERR_FAIL_COND_V(!multimesh, Vector<float>());
  2555. if (multimesh->buffer.is_null()) {
  2556. return Vector<float>();
  2557. } else if (multimesh->data_cache.size()) {
  2558. return multimesh->data_cache;
  2559. } else {
  2560. //get from memory
  2561. Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(multimesh->buffer);
  2562. Vector<float> ret;
  2563. ret.resize(multimesh->instances);
  2564. {
  2565. float *w = multimesh->data_cache.ptrw();
  2566. const uint8_t *r = buffer.ptr();
  2567. copymem(w, r, buffer.size());
  2568. }
  2569. return ret;
  2570. }
  2571. }
  2572. void RasterizerStorageRD::multimesh_set_visible_instances(RID p_multimesh, int p_visible) {
  2573. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2574. ERR_FAIL_COND(!multimesh);
  2575. ERR_FAIL_COND(p_visible < -1 || p_visible > multimesh->instances);
  2576. if (multimesh->visible_instances == p_visible) {
  2577. return;
  2578. }
  2579. if (multimesh->data_cache.size()) {
  2580. //there is a data cache..
  2581. _multimesh_mark_all_dirty(multimesh, false, true);
  2582. }
  2583. multimesh->visible_instances = p_visible;
  2584. }
  2585. int RasterizerStorageRD::multimesh_get_visible_instances(RID p_multimesh) const {
  2586. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2587. ERR_FAIL_COND_V(!multimesh, 0);
  2588. return multimesh->visible_instances;
  2589. }
  2590. AABB RasterizerStorageRD::multimesh_get_aabb(RID p_multimesh) const {
  2591. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2592. ERR_FAIL_COND_V(!multimesh, AABB());
  2593. if (multimesh->aabb_dirty) {
  2594. const_cast<RasterizerStorageRD *>(this)->_update_dirty_multimeshes();
  2595. }
  2596. return multimesh->aabb;
  2597. }
  2598. void RasterizerStorageRD::_update_dirty_multimeshes() {
  2599. while (multimesh_dirty_list) {
  2600. MultiMesh *multimesh = multimesh_dirty_list;
  2601. if (multimesh->data_cache.size()) { //may have been cleared, so only process if it exists
  2602. const float *data = multimesh->data_cache.ptr();
  2603. uint32_t visible_instances = multimesh->visible_instances >= 0 ? multimesh->visible_instances : multimesh->instances;
  2604. if (multimesh->data_cache_used_dirty_regions) {
  2605. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2606. uint32_t visible_region_count = (visible_instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2607. uint32_t region_size = multimesh->stride_cache * MULTIMESH_DIRTY_REGION_SIZE * sizeof(float);
  2608. if (multimesh->data_cache_used_dirty_regions > 32 || multimesh->data_cache_used_dirty_regions > visible_region_count / 2) {
  2609. //if there too many dirty regions, or represent the majority of regions, just copy all, else transfer cost piles up too much
  2610. RD::get_singleton()->buffer_update(multimesh->buffer, 0, MIN(visible_region_count * region_size, multimesh->instances * multimesh->stride_cache * sizeof(float)), data, false);
  2611. } else {
  2612. //not that many regions? update them all
  2613. for (uint32_t i = 0; i < visible_region_count; i++) {
  2614. if (multimesh->data_cache_dirty_regions[i]) {
  2615. uint64_t offset = i * region_size;
  2616. uint64_t size = multimesh->stride_cache * multimesh->instances * sizeof(float);
  2617. RD::get_singleton()->buffer_update(multimesh->buffer, offset, MIN(region_size, size - offset), &data[i * region_size], false);
  2618. }
  2619. }
  2620. }
  2621. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  2622. multimesh->data_cache_dirty_regions[i] = false;
  2623. }
  2624. multimesh->data_cache_used_dirty_regions = 0;
  2625. }
  2626. if (multimesh->aabb_dirty) {
  2627. //aabb is dirty..
  2628. _multimesh_re_create_aabb(multimesh, data, visible_instances);
  2629. multimesh->aabb_dirty = false;
  2630. multimesh->instance_dependency.instance_notify_changed(true, false);
  2631. }
  2632. }
  2633. multimesh_dirty_list = multimesh->dirty_list;
  2634. multimesh->dirty_list = nullptr;
  2635. multimesh->dirty = false;
  2636. }
  2637. multimesh_dirty_list = nullptr;
  2638. }
  2639. /* SKELETON */
  2640. /* SKELETON API */
  2641. RID RasterizerStorageRD::skeleton_create() {
  2642. return skeleton_owner.make_rid(Skeleton());
  2643. }
  2644. void RasterizerStorageRD::_skeleton_make_dirty(Skeleton *skeleton) {
  2645. if (!skeleton->dirty) {
  2646. skeleton->dirty = true;
  2647. skeleton->dirty_list = skeleton_dirty_list;
  2648. skeleton_dirty_list = skeleton;
  2649. }
  2650. }
  2651. void RasterizerStorageRD::skeleton_allocate(RID p_skeleton, int p_bones, bool p_2d_skeleton) {
  2652. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2653. ERR_FAIL_COND(!skeleton);
  2654. ERR_FAIL_COND(p_bones < 0);
  2655. if (skeleton->size == p_bones && skeleton->use_2d == p_2d_skeleton)
  2656. return;
  2657. skeleton->size = p_bones;
  2658. skeleton->use_2d = p_2d_skeleton;
  2659. skeleton->uniform_set_3d = RID();
  2660. if (skeleton->buffer.is_valid()) {
  2661. RD::get_singleton()->free(skeleton->buffer);
  2662. skeleton->buffer = RID();
  2663. skeleton->data.resize(0);
  2664. }
  2665. if (skeleton->size) {
  2666. skeleton->data.resize(skeleton->size * (skeleton->use_2d ? 8 : 12));
  2667. skeleton->buffer = RD::get_singleton()->storage_buffer_create(skeleton->data.size() * sizeof(float));
  2668. zeromem(skeleton->data.ptrw(), skeleton->data.size() * sizeof(float));
  2669. _skeleton_make_dirty(skeleton);
  2670. }
  2671. }
  2672. int RasterizerStorageRD::skeleton_get_bone_count(RID p_skeleton) const {
  2673. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2674. ERR_FAIL_COND_V(!skeleton, 0);
  2675. return skeleton->size;
  2676. }
  2677. void RasterizerStorageRD::skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform &p_transform) {
  2678. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2679. ERR_FAIL_COND(!skeleton);
  2680. ERR_FAIL_INDEX(p_bone, skeleton->size);
  2681. ERR_FAIL_COND(skeleton->use_2d);
  2682. float *dataptr = skeleton->data.ptrw() + p_bone * 12;
  2683. dataptr[0] = p_transform.basis.elements[0][0];
  2684. dataptr[1] = p_transform.basis.elements[0][1];
  2685. dataptr[2] = p_transform.basis.elements[0][2];
  2686. dataptr[3] = p_transform.origin.x;
  2687. dataptr[4] = p_transform.basis.elements[1][0];
  2688. dataptr[5] = p_transform.basis.elements[1][1];
  2689. dataptr[6] = p_transform.basis.elements[1][2];
  2690. dataptr[7] = p_transform.origin.y;
  2691. dataptr[8] = p_transform.basis.elements[2][0];
  2692. dataptr[9] = p_transform.basis.elements[2][1];
  2693. dataptr[10] = p_transform.basis.elements[2][2];
  2694. dataptr[11] = p_transform.origin.z;
  2695. _skeleton_make_dirty(skeleton);
  2696. }
  2697. Transform RasterizerStorageRD::skeleton_bone_get_transform(RID p_skeleton, int p_bone) const {
  2698. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2699. ERR_FAIL_COND_V(!skeleton, Transform());
  2700. ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform());
  2701. ERR_FAIL_COND_V(skeleton->use_2d, Transform());
  2702. const float *dataptr = skeleton->data.ptr() + p_bone * 12;
  2703. Transform t;
  2704. t.basis.elements[0][0] = dataptr[0];
  2705. t.basis.elements[0][1] = dataptr[1];
  2706. t.basis.elements[0][2] = dataptr[2];
  2707. t.origin.x = dataptr[3];
  2708. t.basis.elements[1][0] = dataptr[4];
  2709. t.basis.elements[1][1] = dataptr[5];
  2710. t.basis.elements[1][2] = dataptr[6];
  2711. t.origin.y = dataptr[7];
  2712. t.basis.elements[2][0] = dataptr[8];
  2713. t.basis.elements[2][1] = dataptr[9];
  2714. t.basis.elements[2][2] = dataptr[10];
  2715. t.origin.z = dataptr[11];
  2716. return t;
  2717. }
  2718. void RasterizerStorageRD::skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) {
  2719. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2720. ERR_FAIL_COND(!skeleton);
  2721. ERR_FAIL_INDEX(p_bone, skeleton->size);
  2722. ERR_FAIL_COND(!skeleton->use_2d);
  2723. float *dataptr = skeleton->data.ptrw() + p_bone * 8;
  2724. dataptr[0] = p_transform.elements[0][0];
  2725. dataptr[1] = p_transform.elements[1][0];
  2726. dataptr[2] = 0;
  2727. dataptr[3] = p_transform.elements[2][0];
  2728. dataptr[4] = p_transform.elements[0][1];
  2729. dataptr[5] = p_transform.elements[1][1];
  2730. dataptr[6] = 0;
  2731. dataptr[7] = p_transform.elements[2][1];
  2732. _skeleton_make_dirty(skeleton);
  2733. }
  2734. Transform2D RasterizerStorageRD::skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const {
  2735. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2736. ERR_FAIL_COND_V(!skeleton, Transform2D());
  2737. ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform2D());
  2738. ERR_FAIL_COND_V(!skeleton->use_2d, Transform2D());
  2739. const float *dataptr = skeleton->data.ptr() + p_bone * 8;
  2740. Transform2D t;
  2741. t.elements[0][0] = dataptr[0];
  2742. t.elements[1][0] = dataptr[1];
  2743. t.elements[2][0] = dataptr[3];
  2744. t.elements[0][1] = dataptr[4];
  2745. t.elements[1][1] = dataptr[5];
  2746. t.elements[2][1] = dataptr[7];
  2747. return t;
  2748. }
  2749. void RasterizerStorageRD::skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) {
  2750. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2751. ERR_FAIL_COND(!skeleton->use_2d);
  2752. skeleton->base_transform_2d = p_base_transform;
  2753. }
  2754. void RasterizerStorageRD::_update_dirty_skeletons() {
  2755. while (skeleton_dirty_list) {
  2756. Skeleton *skeleton = skeleton_dirty_list;
  2757. if (skeleton->size) {
  2758. RD::get_singleton()->buffer_update(skeleton->buffer, 0, skeleton->data.size() * sizeof(float), skeleton->data.ptr(), false);
  2759. }
  2760. skeleton_dirty_list = skeleton->dirty_list;
  2761. skeleton->instance_dependency.instance_notify_changed(true, false);
  2762. skeleton->dirty = false;
  2763. skeleton->dirty_list = nullptr;
  2764. }
  2765. skeleton_dirty_list = nullptr;
  2766. }
  2767. /* LIGHT */
  2768. RID RasterizerStorageRD::light_create(RS::LightType p_type) {
  2769. Light light;
  2770. light.type = p_type;
  2771. light.param[RS::LIGHT_PARAM_ENERGY] = 1.0;
  2772. light.param[RS::LIGHT_PARAM_INDIRECT_ENERGY] = 1.0;
  2773. light.param[RS::LIGHT_PARAM_SPECULAR] = 0.5;
  2774. light.param[RS::LIGHT_PARAM_RANGE] = 1.0;
  2775. light.param[RS::LIGHT_PARAM_SIZE] = 0.0;
  2776. light.param[RS::LIGHT_PARAM_SPOT_ANGLE] = 45;
  2777. light.param[RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE] = 0;
  2778. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET] = 0.1;
  2779. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET] = 0.3;
  2780. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET] = 0.6;
  2781. light.param[RS::LIGHT_PARAM_SHADOW_FADE_START] = 0.8;
  2782. light.param[RS::LIGHT_PARAM_SHADOW_BIAS] = 0.02;
  2783. light.param[RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] = 1.0;
  2784. light.param[RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE] = 20.0;
  2785. light.param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS] = 0.05;
  2786. return light_owner.make_rid(light);
  2787. }
  2788. void RasterizerStorageRD::light_set_color(RID p_light, const Color &p_color) {
  2789. Light *light = light_owner.getornull(p_light);
  2790. ERR_FAIL_COND(!light);
  2791. light->color = p_color;
  2792. }
  2793. void RasterizerStorageRD::light_set_param(RID p_light, RS::LightParam p_param, float p_value) {
  2794. Light *light = light_owner.getornull(p_light);
  2795. ERR_FAIL_COND(!light);
  2796. ERR_FAIL_INDEX(p_param, RS::LIGHT_PARAM_MAX);
  2797. switch (p_param) {
  2798. case RS::LIGHT_PARAM_RANGE:
  2799. case RS::LIGHT_PARAM_SPOT_ANGLE:
  2800. case RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE:
  2801. case RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET:
  2802. case RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET:
  2803. case RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET:
  2804. case RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS:
  2805. case RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE:
  2806. case RS::LIGHT_PARAM_SHADOW_BIAS: {
  2807. light->version++;
  2808. light->instance_dependency.instance_notify_changed(true, false);
  2809. } break;
  2810. default: {
  2811. }
  2812. }
  2813. light->param[p_param] = p_value;
  2814. }
  2815. void RasterizerStorageRD::light_set_shadow(RID p_light, bool p_enabled) {
  2816. Light *light = light_owner.getornull(p_light);
  2817. ERR_FAIL_COND(!light);
  2818. light->shadow = p_enabled;
  2819. light->version++;
  2820. light->instance_dependency.instance_notify_changed(true, false);
  2821. }
  2822. void RasterizerStorageRD::light_set_shadow_color(RID p_light, const Color &p_color) {
  2823. Light *light = light_owner.getornull(p_light);
  2824. ERR_FAIL_COND(!light);
  2825. light->shadow_color = p_color;
  2826. }
  2827. void RasterizerStorageRD::light_set_projector(RID p_light, RID p_texture) {
  2828. Light *light = light_owner.getornull(p_light);
  2829. ERR_FAIL_COND(!light);
  2830. if (light->projector == p_texture) {
  2831. return;
  2832. }
  2833. if (light->type != RS::LIGHT_DIRECTIONAL && light->projector.is_valid()) {
  2834. texture_remove_from_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
  2835. }
  2836. light->projector = p_texture;
  2837. if (light->type != RS::LIGHT_DIRECTIONAL && light->projector.is_valid()) {
  2838. texture_add_to_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
  2839. }
  2840. }
  2841. void RasterizerStorageRD::light_set_negative(RID p_light, bool p_enable) {
  2842. Light *light = light_owner.getornull(p_light);
  2843. ERR_FAIL_COND(!light);
  2844. light->negative = p_enable;
  2845. }
  2846. void RasterizerStorageRD::light_set_cull_mask(RID p_light, uint32_t p_mask) {
  2847. Light *light = light_owner.getornull(p_light);
  2848. ERR_FAIL_COND(!light);
  2849. light->cull_mask = p_mask;
  2850. light->version++;
  2851. light->instance_dependency.instance_notify_changed(true, false);
  2852. }
  2853. void RasterizerStorageRD::light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) {
  2854. Light *light = light_owner.getornull(p_light);
  2855. ERR_FAIL_COND(!light);
  2856. light->reverse_cull = p_enabled;
  2857. light->version++;
  2858. light->instance_dependency.instance_notify_changed(true, false);
  2859. }
  2860. void RasterizerStorageRD::light_set_use_gi(RID p_light, bool p_enabled) {
  2861. Light *light = light_owner.getornull(p_light);
  2862. ERR_FAIL_COND(!light);
  2863. light->use_gi = p_enabled;
  2864. light->version++;
  2865. light->instance_dependency.instance_notify_changed(true, false);
  2866. }
  2867. void RasterizerStorageRD::light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode) {
  2868. Light *light = light_owner.getornull(p_light);
  2869. ERR_FAIL_COND(!light);
  2870. light->omni_shadow_mode = p_mode;
  2871. light->version++;
  2872. light->instance_dependency.instance_notify_changed(true, false);
  2873. }
  2874. RS::LightOmniShadowMode RasterizerStorageRD::light_omni_get_shadow_mode(RID p_light) {
  2875. const Light *light = light_owner.getornull(p_light);
  2876. ERR_FAIL_COND_V(!light, RS::LIGHT_OMNI_SHADOW_CUBE);
  2877. return light->omni_shadow_mode;
  2878. }
  2879. void RasterizerStorageRD::light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode) {
  2880. Light *light = light_owner.getornull(p_light);
  2881. ERR_FAIL_COND(!light);
  2882. light->directional_shadow_mode = p_mode;
  2883. light->version++;
  2884. light->instance_dependency.instance_notify_changed(true, false);
  2885. }
  2886. void RasterizerStorageRD::light_directional_set_blend_splits(RID p_light, bool p_enable) {
  2887. Light *light = light_owner.getornull(p_light);
  2888. ERR_FAIL_COND(!light);
  2889. light->directional_blend_splits = p_enable;
  2890. light->version++;
  2891. light->instance_dependency.instance_notify_changed(true, false);
  2892. }
  2893. bool RasterizerStorageRD::light_directional_get_blend_splits(RID p_light) const {
  2894. const Light *light = light_owner.getornull(p_light);
  2895. ERR_FAIL_COND_V(!light, false);
  2896. return light->directional_blend_splits;
  2897. }
  2898. RS::LightDirectionalShadowMode RasterizerStorageRD::light_directional_get_shadow_mode(RID p_light) {
  2899. const Light *light = light_owner.getornull(p_light);
  2900. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL);
  2901. return light->directional_shadow_mode;
  2902. }
  2903. void RasterizerStorageRD::light_directional_set_shadow_depth_range_mode(RID p_light, RS::LightDirectionalShadowDepthRangeMode p_range_mode) {
  2904. Light *light = light_owner.getornull(p_light);
  2905. ERR_FAIL_COND(!light);
  2906. light->directional_range_mode = p_range_mode;
  2907. }
  2908. RS::LightDirectionalShadowDepthRangeMode RasterizerStorageRD::light_directional_get_shadow_depth_range_mode(RID p_light) const {
  2909. const Light *light = light_owner.getornull(p_light);
  2910. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE);
  2911. return light->directional_range_mode;
  2912. }
  2913. bool RasterizerStorageRD::light_get_use_gi(RID p_light) {
  2914. Light *light = light_owner.getornull(p_light);
  2915. ERR_FAIL_COND_V(!light, false);
  2916. return light->use_gi;
  2917. }
  2918. uint64_t RasterizerStorageRD::light_get_version(RID p_light) const {
  2919. const Light *light = light_owner.getornull(p_light);
  2920. ERR_FAIL_COND_V(!light, 0);
  2921. return light->version;
  2922. }
  2923. AABB RasterizerStorageRD::light_get_aabb(RID p_light) const {
  2924. const Light *light = light_owner.getornull(p_light);
  2925. ERR_FAIL_COND_V(!light, AABB());
  2926. switch (light->type) {
  2927. case RS::LIGHT_SPOT: {
  2928. float len = light->param[RS::LIGHT_PARAM_RANGE];
  2929. float size = Math::tan(Math::deg2rad(light->param[RS::LIGHT_PARAM_SPOT_ANGLE])) * len;
  2930. return AABB(Vector3(-size, -size, -len), Vector3(size * 2, size * 2, len));
  2931. };
  2932. case RS::LIGHT_OMNI: {
  2933. float r = light->param[RS::LIGHT_PARAM_RANGE];
  2934. return AABB(-Vector3(r, r, r), Vector3(r, r, r) * 2);
  2935. };
  2936. case RS::LIGHT_DIRECTIONAL: {
  2937. return AABB();
  2938. };
  2939. }
  2940. ERR_FAIL_V(AABB());
  2941. }
  2942. /* REFLECTION PROBE */
  2943. RID RasterizerStorageRD::reflection_probe_create() {
  2944. return reflection_probe_owner.make_rid(ReflectionProbe());
  2945. }
  2946. void RasterizerStorageRD::reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode) {
  2947. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2948. ERR_FAIL_COND(!reflection_probe);
  2949. reflection_probe->update_mode = p_mode;
  2950. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  2951. }
  2952. void RasterizerStorageRD::reflection_probe_set_intensity(RID p_probe, float p_intensity) {
  2953. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2954. ERR_FAIL_COND(!reflection_probe);
  2955. reflection_probe->intensity = p_intensity;
  2956. }
  2957. void RasterizerStorageRD::reflection_probe_set_interior_ambient(RID p_probe, const Color &p_ambient) {
  2958. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2959. ERR_FAIL_COND(!reflection_probe);
  2960. reflection_probe->interior_ambient = p_ambient;
  2961. }
  2962. void RasterizerStorageRD::reflection_probe_set_interior_ambient_energy(RID p_probe, float p_energy) {
  2963. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2964. ERR_FAIL_COND(!reflection_probe);
  2965. reflection_probe->interior_ambient_energy = p_energy;
  2966. }
  2967. void RasterizerStorageRD::reflection_probe_set_interior_ambient_probe_contribution(RID p_probe, float p_contrib) {
  2968. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2969. ERR_FAIL_COND(!reflection_probe);
  2970. reflection_probe->interior_ambient_probe_contrib = p_contrib;
  2971. }
  2972. void RasterizerStorageRD::reflection_probe_set_max_distance(RID p_probe, float p_distance) {
  2973. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2974. ERR_FAIL_COND(!reflection_probe);
  2975. reflection_probe->max_distance = p_distance;
  2976. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  2977. }
  2978. void RasterizerStorageRD::reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents) {
  2979. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2980. ERR_FAIL_COND(!reflection_probe);
  2981. reflection_probe->extents = p_extents;
  2982. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  2983. }
  2984. void RasterizerStorageRD::reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) {
  2985. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2986. ERR_FAIL_COND(!reflection_probe);
  2987. reflection_probe->origin_offset = p_offset;
  2988. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  2989. }
  2990. void RasterizerStorageRD::reflection_probe_set_as_interior(RID p_probe, bool p_enable) {
  2991. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2992. ERR_FAIL_COND(!reflection_probe);
  2993. reflection_probe->interior = p_enable;
  2994. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  2995. }
  2996. void RasterizerStorageRD::reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) {
  2997. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2998. ERR_FAIL_COND(!reflection_probe);
  2999. reflection_probe->box_projection = p_enable;
  3000. }
  3001. void RasterizerStorageRD::reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) {
  3002. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3003. ERR_FAIL_COND(!reflection_probe);
  3004. reflection_probe->enable_shadows = p_enable;
  3005. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  3006. }
  3007. void RasterizerStorageRD::reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) {
  3008. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3009. ERR_FAIL_COND(!reflection_probe);
  3010. reflection_probe->cull_mask = p_layers;
  3011. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  3012. }
  3013. void RasterizerStorageRD::reflection_probe_set_resolution(RID p_probe, int p_resolution) {
  3014. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3015. ERR_FAIL_COND(!reflection_probe);
  3016. ERR_FAIL_COND(p_resolution < 32);
  3017. reflection_probe->resolution = p_resolution;
  3018. }
  3019. AABB RasterizerStorageRD::reflection_probe_get_aabb(RID p_probe) const {
  3020. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3021. ERR_FAIL_COND_V(!reflection_probe, AABB());
  3022. AABB aabb;
  3023. aabb.position = -reflection_probe->extents;
  3024. aabb.size = reflection_probe->extents * 2.0;
  3025. return aabb;
  3026. }
  3027. RS::ReflectionProbeUpdateMode RasterizerStorageRD::reflection_probe_get_update_mode(RID p_probe) const {
  3028. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3029. ERR_FAIL_COND_V(!reflection_probe, RS::REFLECTION_PROBE_UPDATE_ALWAYS);
  3030. return reflection_probe->update_mode;
  3031. }
  3032. uint32_t RasterizerStorageRD::reflection_probe_get_cull_mask(RID p_probe) const {
  3033. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3034. ERR_FAIL_COND_V(!reflection_probe, 0);
  3035. return reflection_probe->cull_mask;
  3036. }
  3037. Vector3 RasterizerStorageRD::reflection_probe_get_extents(RID p_probe) const {
  3038. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3039. ERR_FAIL_COND_V(!reflection_probe, Vector3());
  3040. return reflection_probe->extents;
  3041. }
  3042. Vector3 RasterizerStorageRD::reflection_probe_get_origin_offset(RID p_probe) const {
  3043. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3044. ERR_FAIL_COND_V(!reflection_probe, Vector3());
  3045. return reflection_probe->origin_offset;
  3046. }
  3047. bool RasterizerStorageRD::reflection_probe_renders_shadows(RID p_probe) const {
  3048. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3049. ERR_FAIL_COND_V(!reflection_probe, false);
  3050. return reflection_probe->enable_shadows;
  3051. }
  3052. float RasterizerStorageRD::reflection_probe_get_origin_max_distance(RID p_probe) const {
  3053. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3054. ERR_FAIL_COND_V(!reflection_probe, 0);
  3055. return reflection_probe->max_distance;
  3056. }
  3057. int RasterizerStorageRD::reflection_probe_get_resolution(RID p_probe) const {
  3058. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3059. ERR_FAIL_COND_V(!reflection_probe, 0);
  3060. return reflection_probe->resolution;
  3061. }
  3062. float RasterizerStorageRD::reflection_probe_get_intensity(RID p_probe) const {
  3063. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3064. ERR_FAIL_COND_V(!reflection_probe, 0);
  3065. return reflection_probe->intensity;
  3066. }
  3067. bool RasterizerStorageRD::reflection_probe_is_interior(RID p_probe) const {
  3068. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3069. ERR_FAIL_COND_V(!reflection_probe, false);
  3070. return reflection_probe->interior;
  3071. }
  3072. bool RasterizerStorageRD::reflection_probe_is_box_projection(RID p_probe) const {
  3073. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3074. ERR_FAIL_COND_V(!reflection_probe, false);
  3075. return reflection_probe->box_projection;
  3076. }
  3077. Color RasterizerStorageRD::reflection_probe_get_interior_ambient(RID p_probe) const {
  3078. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3079. ERR_FAIL_COND_V(!reflection_probe, Color());
  3080. return reflection_probe->interior_ambient;
  3081. }
  3082. float RasterizerStorageRD::reflection_probe_get_interior_ambient_energy(RID p_probe) const {
  3083. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3084. ERR_FAIL_COND_V(!reflection_probe, 0);
  3085. return reflection_probe->interior_ambient_energy;
  3086. }
  3087. float RasterizerStorageRD::reflection_probe_get_interior_ambient_probe_contribution(RID p_probe) const {
  3088. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  3089. ERR_FAIL_COND_V(!reflection_probe, 0);
  3090. return reflection_probe->interior_ambient_probe_contrib;
  3091. }
  3092. RID RasterizerStorageRD::decal_create() {
  3093. return decal_owner.make_rid(Decal());
  3094. }
  3095. void RasterizerStorageRD::decal_set_extents(RID p_decal, const Vector3 &p_extents) {
  3096. Decal *decal = decal_owner.getornull(p_decal);
  3097. ERR_FAIL_COND(!decal);
  3098. decal->extents = p_extents;
  3099. decal->instance_dependency.instance_notify_changed(true, false);
  3100. }
  3101. void RasterizerStorageRD::decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture) {
  3102. Decal *decal = decal_owner.getornull(p_decal);
  3103. ERR_FAIL_COND(!decal);
  3104. ERR_FAIL_INDEX(p_type, RS::DECAL_TEXTURE_MAX);
  3105. if (decal->textures[p_type] == p_texture) {
  3106. return;
  3107. }
  3108. ERR_FAIL_COND(p_texture.is_valid() && !texture_owner.owns(p_texture));
  3109. if (decal->textures[p_type].is_valid() && texture_owner.owns(decal->textures[p_type])) {
  3110. texture_remove_from_decal_atlas(decal->textures[p_type]);
  3111. }
  3112. decal->textures[p_type] = p_texture;
  3113. if (decal->textures[p_type].is_valid()) {
  3114. texture_add_to_decal_atlas(decal->textures[p_type]);
  3115. }
  3116. decal->instance_dependency.instance_notify_changed(false, true);
  3117. }
  3118. void RasterizerStorageRD::decal_set_emission_energy(RID p_decal, float p_energy) {
  3119. Decal *decal = decal_owner.getornull(p_decal);
  3120. ERR_FAIL_COND(!decal);
  3121. decal->emission_energy = p_energy;
  3122. }
  3123. void RasterizerStorageRD::decal_set_albedo_mix(RID p_decal, float p_mix) {
  3124. Decal *decal = decal_owner.getornull(p_decal);
  3125. ERR_FAIL_COND(!decal);
  3126. decal->albedo_mix = p_mix;
  3127. }
  3128. void RasterizerStorageRD::decal_set_modulate(RID p_decal, const Color &p_modulate) {
  3129. Decal *decal = decal_owner.getornull(p_decal);
  3130. ERR_FAIL_COND(!decal);
  3131. decal->modulate = p_modulate;
  3132. }
  3133. void RasterizerStorageRD::decal_set_cull_mask(RID p_decal, uint32_t p_layers) {
  3134. Decal *decal = decal_owner.getornull(p_decal);
  3135. ERR_FAIL_COND(!decal);
  3136. decal->cull_mask = p_layers;
  3137. decal->instance_dependency.instance_notify_changed(true, false);
  3138. }
  3139. void RasterizerStorageRD::decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length) {
  3140. Decal *decal = decal_owner.getornull(p_decal);
  3141. ERR_FAIL_COND(!decal);
  3142. decal->distance_fade = p_enabled;
  3143. decal->distance_fade_begin = p_begin;
  3144. decal->distance_fade_length = p_length;
  3145. }
  3146. void RasterizerStorageRD::decal_set_fade(RID p_decal, float p_above, float p_below) {
  3147. Decal *decal = decal_owner.getornull(p_decal);
  3148. ERR_FAIL_COND(!decal);
  3149. decal->upper_fade = p_above;
  3150. decal->lower_fade = p_below;
  3151. }
  3152. void RasterizerStorageRD::decal_set_normal_fade(RID p_decal, float p_fade) {
  3153. Decal *decal = decal_owner.getornull(p_decal);
  3154. ERR_FAIL_COND(!decal);
  3155. decal->normal_fade = p_fade;
  3156. }
  3157. AABB RasterizerStorageRD::decal_get_aabb(RID p_decal) const {
  3158. Decal *decal = decal_owner.getornull(p_decal);
  3159. ERR_FAIL_COND_V(!decal, AABB());
  3160. return AABB(-decal->extents, decal->extents * 2.0);
  3161. }
  3162. RID RasterizerStorageRD::gi_probe_create() {
  3163. return gi_probe_owner.make_rid(GIProbe());
  3164. }
  3165. void RasterizerStorageRD::gi_probe_allocate(RID p_gi_probe, const Transform &p_to_cell_xform, const AABB &p_aabb, const Vector3i &p_octree_size, const Vector<uint8_t> &p_octree_cells, const Vector<uint8_t> &p_data_cells, const Vector<uint8_t> &p_distance_field, const Vector<int> &p_level_counts) {
  3166. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3167. ERR_FAIL_COND(!gi_probe);
  3168. if (gi_probe->octree_buffer.is_valid()) {
  3169. RD::get_singleton()->free(gi_probe->octree_buffer);
  3170. RD::get_singleton()->free(gi_probe->data_buffer);
  3171. if (gi_probe->sdf_texture.is_valid()) {
  3172. RD::get_singleton()->free(gi_probe->sdf_texture);
  3173. }
  3174. gi_probe->sdf_texture = RID();
  3175. gi_probe->octree_buffer = RID();
  3176. gi_probe->data_buffer = RID();
  3177. gi_probe->octree_buffer_size = 0;
  3178. gi_probe->data_buffer_size = 0;
  3179. gi_probe->cell_count = 0;
  3180. }
  3181. gi_probe->to_cell_xform = p_to_cell_xform;
  3182. gi_probe->bounds = p_aabb;
  3183. gi_probe->octree_size = p_octree_size;
  3184. gi_probe->level_counts = p_level_counts;
  3185. if (p_octree_cells.size()) {
  3186. ERR_FAIL_COND(p_octree_cells.size() % 32 != 0); //cells size must be a multiple of 32
  3187. uint32_t cell_count = p_octree_cells.size() / 32;
  3188. ERR_FAIL_COND(p_data_cells.size() != (int)cell_count * 16); //see that data size matches
  3189. gi_probe->cell_count = cell_count;
  3190. gi_probe->octree_buffer = RD::get_singleton()->storage_buffer_create(p_octree_cells.size(), p_octree_cells);
  3191. gi_probe->octree_buffer_size = p_octree_cells.size();
  3192. gi_probe->data_buffer = RD::get_singleton()->storage_buffer_create(p_data_cells.size(), p_data_cells);
  3193. gi_probe->data_buffer_size = p_data_cells.size();
  3194. if (p_distance_field.size()) {
  3195. RD::TextureFormat tf;
  3196. tf.format = RD::DATA_FORMAT_R8_UNORM;
  3197. tf.width = gi_probe->octree_size.x;
  3198. tf.height = gi_probe->octree_size.y;
  3199. tf.depth = gi_probe->octree_size.z;
  3200. tf.type = RD::TEXTURE_TYPE_3D;
  3201. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  3202. Vector<Vector<uint8_t>> s;
  3203. s.push_back(p_distance_field);
  3204. gi_probe->sdf_texture = RD::get_singleton()->texture_create(tf, RD::TextureView(), s);
  3205. }
  3206. #if 0
  3207. {
  3208. RD::TextureFormat tf;
  3209. tf.format = RD::DATA_FORMAT_R8_UNORM;
  3210. tf.width = gi_probe->octree_size.x;
  3211. tf.height = gi_probe->octree_size.y;
  3212. tf.depth = gi_probe->octree_size.z;
  3213. tf.type = RD::TEXTURE_TYPE_3D;
  3214. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  3215. tf.shareable_formats.push_back(RD::DATA_FORMAT_R8_UNORM);
  3216. tf.shareable_formats.push_back(RD::DATA_FORMAT_R8_UINT);
  3217. gi_probe->sdf_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  3218. }
  3219. RID shared_tex;
  3220. {
  3221. RD::TextureView tv;
  3222. tv.format_override = RD::DATA_FORMAT_R8_UINT;
  3223. shared_tex = RD::get_singleton()->texture_create_shared(tv, gi_probe->sdf_texture);
  3224. }
  3225. //update SDF texture
  3226. Vector<RD::Uniform> uniforms;
  3227. {
  3228. RD::Uniform u;
  3229. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3230. u.binding = 1;
  3231. u.ids.push_back(gi_probe->octree_buffer);
  3232. uniforms.push_back(u);
  3233. }
  3234. {
  3235. RD::Uniform u;
  3236. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3237. u.binding = 2;
  3238. u.ids.push_back(gi_probe->data_buffer);
  3239. uniforms.push_back(u);
  3240. }
  3241. {
  3242. RD::Uniform u;
  3243. u.type = RD::UNIFORM_TYPE_IMAGE;
  3244. u.binding = 3;
  3245. u.ids.push_back(shared_tex);
  3246. uniforms.push_back(u);
  3247. }
  3248. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_sdf_shader_version_shader, 0);
  3249. {
  3250. uint32_t push_constant[4] = { 0, 0, 0, 0 };
  3251. for (int i = 0; i < gi_probe->level_counts.size() - 1; i++) {
  3252. push_constant[0] += gi_probe->level_counts[i];
  3253. }
  3254. push_constant[1] = push_constant[0] + gi_probe->level_counts[gi_probe->level_counts.size() - 1];
  3255. print_line("offset: " + itos(push_constant[0]));
  3256. print_line("size: " + itos(push_constant[1]));
  3257. //create SDF
  3258. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  3259. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_sdf_shader_pipeline);
  3260. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set, 0);
  3261. RD::get_singleton()->compute_list_set_push_constant(compute_list, push_constant, sizeof(uint32_t) * 4);
  3262. RD::get_singleton()->compute_list_dispatch(compute_list, gi_probe->octree_size.x / 4, gi_probe->octree_size.y / 4, gi_probe->octree_size.z / 4);
  3263. RD::get_singleton()->compute_list_end();
  3264. }
  3265. RD::get_singleton()->free(uniform_set);
  3266. RD::get_singleton()->free(shared_tex);
  3267. }
  3268. #endif
  3269. }
  3270. gi_probe->version++;
  3271. gi_probe->data_version++;
  3272. gi_probe->instance_dependency.instance_notify_changed(true, false);
  3273. }
  3274. AABB RasterizerStorageRD::gi_probe_get_bounds(RID p_gi_probe) const {
  3275. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3276. ERR_FAIL_COND_V(!gi_probe, AABB());
  3277. return gi_probe->bounds;
  3278. }
  3279. Vector3i RasterizerStorageRD::gi_probe_get_octree_size(RID p_gi_probe) const {
  3280. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3281. ERR_FAIL_COND_V(!gi_probe, Vector3i());
  3282. return gi_probe->octree_size;
  3283. }
  3284. Vector<uint8_t> RasterizerStorageRD::gi_probe_get_octree_cells(RID p_gi_probe) const {
  3285. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3286. ERR_FAIL_COND_V(!gi_probe, Vector<uint8_t>());
  3287. if (gi_probe->octree_buffer.is_valid()) {
  3288. return RD::get_singleton()->buffer_get_data(gi_probe->octree_buffer);
  3289. }
  3290. return Vector<uint8_t>();
  3291. }
  3292. Vector<uint8_t> RasterizerStorageRD::gi_probe_get_data_cells(RID p_gi_probe) const {
  3293. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3294. ERR_FAIL_COND_V(!gi_probe, Vector<uint8_t>());
  3295. if (gi_probe->data_buffer.is_valid()) {
  3296. return RD::get_singleton()->buffer_get_data(gi_probe->data_buffer);
  3297. }
  3298. return Vector<uint8_t>();
  3299. }
  3300. Vector<uint8_t> RasterizerStorageRD::gi_probe_get_distance_field(RID p_gi_probe) const {
  3301. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3302. ERR_FAIL_COND_V(!gi_probe, Vector<uint8_t>());
  3303. if (gi_probe->data_buffer.is_valid()) {
  3304. return RD::get_singleton()->texture_get_data(gi_probe->sdf_texture, 0);
  3305. }
  3306. return Vector<uint8_t>();
  3307. }
  3308. Vector<int> RasterizerStorageRD::gi_probe_get_level_counts(RID p_gi_probe) const {
  3309. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3310. ERR_FAIL_COND_V(!gi_probe, Vector<int>());
  3311. return gi_probe->level_counts;
  3312. }
  3313. Transform RasterizerStorageRD::gi_probe_get_to_cell_xform(RID p_gi_probe) const {
  3314. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3315. ERR_FAIL_COND_V(!gi_probe, Transform());
  3316. return gi_probe->to_cell_xform;
  3317. }
  3318. void RasterizerStorageRD::gi_probe_set_dynamic_range(RID p_gi_probe, float p_range) {
  3319. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3320. ERR_FAIL_COND(!gi_probe);
  3321. gi_probe->dynamic_range = p_range;
  3322. gi_probe->version++;
  3323. }
  3324. float RasterizerStorageRD::gi_probe_get_dynamic_range(RID p_gi_probe) const {
  3325. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3326. ERR_FAIL_COND_V(!gi_probe, 0);
  3327. return gi_probe->dynamic_range;
  3328. }
  3329. void RasterizerStorageRD::gi_probe_set_propagation(RID p_gi_probe, float p_range) {
  3330. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3331. ERR_FAIL_COND(!gi_probe);
  3332. gi_probe->propagation = p_range;
  3333. gi_probe->version++;
  3334. }
  3335. float RasterizerStorageRD::gi_probe_get_propagation(RID p_gi_probe) const {
  3336. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3337. ERR_FAIL_COND_V(!gi_probe, 0);
  3338. return gi_probe->propagation;
  3339. }
  3340. void RasterizerStorageRD::gi_probe_set_energy(RID p_gi_probe, float p_energy) {
  3341. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3342. ERR_FAIL_COND(!gi_probe);
  3343. gi_probe->energy = p_energy;
  3344. }
  3345. float RasterizerStorageRD::gi_probe_get_energy(RID p_gi_probe) const {
  3346. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3347. ERR_FAIL_COND_V(!gi_probe, 0);
  3348. return gi_probe->energy;
  3349. }
  3350. void RasterizerStorageRD::gi_probe_set_ao(RID p_gi_probe, float p_ao) {
  3351. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3352. ERR_FAIL_COND(!gi_probe);
  3353. gi_probe->ao = p_ao;
  3354. }
  3355. float RasterizerStorageRD::gi_probe_get_ao(RID p_gi_probe) const {
  3356. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3357. ERR_FAIL_COND_V(!gi_probe, 0);
  3358. return gi_probe->ao;
  3359. }
  3360. void RasterizerStorageRD::gi_probe_set_ao_size(RID p_gi_probe, float p_strength) {
  3361. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3362. ERR_FAIL_COND(!gi_probe);
  3363. gi_probe->ao_size = p_strength;
  3364. }
  3365. float RasterizerStorageRD::gi_probe_get_ao_size(RID p_gi_probe) const {
  3366. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3367. ERR_FAIL_COND_V(!gi_probe, 0);
  3368. return gi_probe->ao_size;
  3369. }
  3370. void RasterizerStorageRD::gi_probe_set_bias(RID p_gi_probe, float p_bias) {
  3371. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3372. ERR_FAIL_COND(!gi_probe);
  3373. gi_probe->bias = p_bias;
  3374. }
  3375. float RasterizerStorageRD::gi_probe_get_bias(RID p_gi_probe) const {
  3376. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3377. ERR_FAIL_COND_V(!gi_probe, 0);
  3378. return gi_probe->bias;
  3379. }
  3380. void RasterizerStorageRD::gi_probe_set_normal_bias(RID p_gi_probe, float p_normal_bias) {
  3381. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3382. ERR_FAIL_COND(!gi_probe);
  3383. gi_probe->normal_bias = p_normal_bias;
  3384. }
  3385. float RasterizerStorageRD::gi_probe_get_normal_bias(RID p_gi_probe) const {
  3386. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3387. ERR_FAIL_COND_V(!gi_probe, 0);
  3388. return gi_probe->normal_bias;
  3389. }
  3390. void RasterizerStorageRD::gi_probe_set_anisotropy_strength(RID p_gi_probe, float p_strength) {
  3391. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3392. ERR_FAIL_COND(!gi_probe);
  3393. gi_probe->anisotropy_strength = p_strength;
  3394. }
  3395. float RasterizerStorageRD::gi_probe_get_anisotropy_strength(RID p_gi_probe) const {
  3396. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3397. ERR_FAIL_COND_V(!gi_probe, 0);
  3398. return gi_probe->anisotropy_strength;
  3399. }
  3400. void RasterizerStorageRD::gi_probe_set_interior(RID p_gi_probe, bool p_enable) {
  3401. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3402. ERR_FAIL_COND(!gi_probe);
  3403. gi_probe->interior = p_enable;
  3404. }
  3405. void RasterizerStorageRD::gi_probe_set_use_two_bounces(RID p_gi_probe, bool p_enable) {
  3406. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3407. ERR_FAIL_COND(!gi_probe);
  3408. gi_probe->use_two_bounces = p_enable;
  3409. gi_probe->version++;
  3410. }
  3411. bool RasterizerStorageRD::gi_probe_is_using_two_bounces(RID p_gi_probe) const {
  3412. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3413. ERR_FAIL_COND_V(!gi_probe, false);
  3414. return gi_probe->use_two_bounces;
  3415. }
  3416. bool RasterizerStorageRD::gi_probe_is_interior(RID p_gi_probe) const {
  3417. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3418. ERR_FAIL_COND_V(!gi_probe, 0);
  3419. return gi_probe->interior;
  3420. }
  3421. uint32_t RasterizerStorageRD::gi_probe_get_version(RID p_gi_probe) {
  3422. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3423. ERR_FAIL_COND_V(!gi_probe, 0);
  3424. return gi_probe->version;
  3425. }
  3426. uint32_t RasterizerStorageRD::gi_probe_get_data_version(RID p_gi_probe) {
  3427. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3428. ERR_FAIL_COND_V(!gi_probe, 0);
  3429. return gi_probe->data_version;
  3430. }
  3431. RID RasterizerStorageRD::gi_probe_get_octree_buffer(RID p_gi_probe) const {
  3432. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3433. ERR_FAIL_COND_V(!gi_probe, RID());
  3434. return gi_probe->octree_buffer;
  3435. }
  3436. RID RasterizerStorageRD::gi_probe_get_data_buffer(RID p_gi_probe) const {
  3437. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3438. ERR_FAIL_COND_V(!gi_probe, RID());
  3439. return gi_probe->data_buffer;
  3440. }
  3441. RID RasterizerStorageRD::gi_probe_get_sdf_texture(RID p_gi_probe) {
  3442. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  3443. ERR_FAIL_COND_V(!gi_probe, RID());
  3444. return gi_probe->sdf_texture;
  3445. }
  3446. /* LIGHTMAP API */
  3447. RID RasterizerStorageRD::lightmap_create() {
  3448. return lightmap_owner.make_rid(Lightmap());
  3449. }
  3450. void RasterizerStorageRD::lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics) {
  3451. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  3452. ERR_FAIL_COND(!lm);
  3453. lightmap_array_version++;
  3454. //erase lightmap users
  3455. if (lm->light_texture.is_valid()) {
  3456. Texture *t = texture_owner.getornull(lm->light_texture);
  3457. if (t) {
  3458. t->lightmap_users.erase(p_lightmap);
  3459. }
  3460. }
  3461. Texture *t = texture_owner.getornull(p_light);
  3462. lm->light_texture = p_light;
  3463. lm->uses_spherical_harmonics = p_uses_spherical_haromics;
  3464. RID default_2d_array = default_rd_textures[DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE];
  3465. if (!t) {
  3466. if (using_lightmap_array) {
  3467. if (lm->array_index >= 0) {
  3468. lightmap_textures.write[lm->array_index] = default_2d_array;
  3469. lm->array_index = -1;
  3470. }
  3471. }
  3472. return;
  3473. }
  3474. t->lightmap_users.insert(p_lightmap);
  3475. if (using_lightmap_array) {
  3476. if (lm->array_index < 0) {
  3477. //not in array, try to put in array
  3478. for (int i = 0; i < lightmap_textures.size(); i++) {
  3479. if (lightmap_textures[i] == default_2d_array) {
  3480. lm->array_index = i;
  3481. break;
  3482. }
  3483. }
  3484. }
  3485. 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.");
  3486. lightmap_textures.write[lm->array_index] = t->rd_texture;
  3487. }
  3488. }
  3489. void RasterizerStorageRD::lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds) {
  3490. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  3491. ERR_FAIL_COND(!lm);
  3492. lm->bounds = p_bounds;
  3493. }
  3494. void RasterizerStorageRD::lightmap_set_probe_interior(RID p_lightmap, bool p_interior) {
  3495. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  3496. ERR_FAIL_COND(!lm);
  3497. lm->interior = p_interior;
  3498. }
  3499. void RasterizerStorageRD::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) {
  3500. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  3501. ERR_FAIL_COND(!lm);
  3502. if (p_points.size()) {
  3503. ERR_FAIL_COND(p_points.size() * 9 != p_point_sh.size());
  3504. ERR_FAIL_COND((p_tetrahedra.size() % 4) != 0);
  3505. ERR_FAIL_COND((p_bsp_tree.size() % 6) != 0);
  3506. }
  3507. lm->points = p_points;
  3508. lm->bsp_tree = p_bsp_tree;
  3509. lm->point_sh = p_point_sh;
  3510. lm->tetrahedra = p_tetrahedra;
  3511. }
  3512. PackedVector3Array RasterizerStorageRD::lightmap_get_probe_capture_points(RID p_lightmap) const {
  3513. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  3514. ERR_FAIL_COND_V(!lm, PackedVector3Array());
  3515. return lm->points;
  3516. }
  3517. PackedColorArray RasterizerStorageRD::lightmap_get_probe_capture_sh(RID p_lightmap) const {
  3518. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  3519. ERR_FAIL_COND_V(!lm, PackedColorArray());
  3520. return lm->point_sh;
  3521. }
  3522. PackedInt32Array RasterizerStorageRD::lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const {
  3523. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  3524. ERR_FAIL_COND_V(!lm, PackedInt32Array());
  3525. return lm->tetrahedra;
  3526. }
  3527. PackedInt32Array RasterizerStorageRD::lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const {
  3528. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  3529. ERR_FAIL_COND_V(!lm, PackedInt32Array());
  3530. return lm->bsp_tree;
  3531. }
  3532. void RasterizerStorageRD::lightmap_set_probe_capture_update_speed(float p_speed) {
  3533. lightmap_probe_capture_update_speed = p_speed;
  3534. }
  3535. void RasterizerStorageRD::lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh) {
  3536. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  3537. ERR_FAIL_COND(!lm);
  3538. for (int i = 0; i < 9; i++) {
  3539. r_sh[i] = Color(0, 0, 0, 0);
  3540. }
  3541. if (!lm->points.size() || !lm->bsp_tree.size() || !lm->tetrahedra.size()) {
  3542. return;
  3543. }
  3544. static_assert(sizeof(Lightmap::BSP) == 24);
  3545. const Lightmap::BSP *bsp = (const Lightmap::BSP *)lm->bsp_tree.ptr();
  3546. int32_t node = 0;
  3547. while (node >= 0) {
  3548. if (Plane(bsp[node].plane[0], bsp[node].plane[1], bsp[node].plane[2], bsp[node].plane[3]).is_point_over(p_point)) {
  3549. #ifdef DEBUG_ENABLED
  3550. ERR_FAIL_COND(bsp[node].over >= 0 && bsp[node].over < node);
  3551. #endif
  3552. node = bsp[node].over;
  3553. } else {
  3554. #ifdef DEBUG_ENABLED
  3555. ERR_FAIL_COND(bsp[node].under >= 0 && bsp[node].under < node);
  3556. #endif
  3557. node = bsp[node].under;
  3558. }
  3559. }
  3560. if (node == Lightmap::BSP::EMPTY_LEAF) {
  3561. return; //nothing could be done
  3562. }
  3563. node = ABS(node) - 1;
  3564. uint32_t *tetrahedron = (uint32_t *)&lm->tetrahedra[node * 4];
  3565. Vector3 points[4] = { lm->points[tetrahedron[0]], lm->points[tetrahedron[1]], lm->points[tetrahedron[2]], lm->points[tetrahedron[3]] };
  3566. 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] };
  3567. Color barycentric = Geometry::tetrahedron_get_barycentric_coords(points[0], points[1], points[2], points[3], p_point);
  3568. for (int i = 0; i < 4; i++) {
  3569. float c = CLAMP(barycentric[i], 0.0, 1.0);
  3570. for (int j = 0; j < 9; j++) {
  3571. r_sh[j] += sh_colors[i][j] * c;
  3572. }
  3573. }
  3574. }
  3575. bool RasterizerStorageRD::lightmap_is_interior(RID p_lightmap) const {
  3576. const Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  3577. ERR_FAIL_COND_V(!lm, false);
  3578. return lm->interior;
  3579. }
  3580. AABB RasterizerStorageRD::lightmap_get_aabb(RID p_lightmap) const {
  3581. const Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  3582. ERR_FAIL_COND_V(!lm, AABB());
  3583. return lm->bounds;
  3584. }
  3585. /* RENDER TARGET API */
  3586. void RasterizerStorageRD::_clear_render_target(RenderTarget *rt) {
  3587. //free in reverse dependency order
  3588. if (rt->framebuffer.is_valid()) {
  3589. RD::get_singleton()->free(rt->framebuffer);
  3590. }
  3591. if (rt->color.is_valid()) {
  3592. RD::get_singleton()->free(rt->color);
  3593. }
  3594. if (rt->backbuffer.is_valid()) {
  3595. RD::get_singleton()->free(rt->backbuffer);
  3596. rt->backbuffer = RID();
  3597. for (int i = 0; i < rt->backbuffer_mipmaps.size(); i++) {
  3598. //just erase copies, since the rest are erased by dependency
  3599. RD::get_singleton()->free(rt->backbuffer_mipmaps[i].mipmap_copy);
  3600. }
  3601. rt->backbuffer_mipmaps.clear();
  3602. if (rt->backbuffer_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(rt->backbuffer_uniform_set)) {
  3603. RD::get_singleton()->free(rt->backbuffer_uniform_set);
  3604. }
  3605. rt->backbuffer_uniform_set = RID();
  3606. }
  3607. rt->framebuffer = RID();
  3608. rt->color = RID();
  3609. }
  3610. void RasterizerStorageRD::_update_render_target(RenderTarget *rt) {
  3611. if (rt->texture.is_null()) {
  3612. //create a placeholder until updated
  3613. rt->texture = texture_2d_placeholder_create();
  3614. Texture *tex = texture_owner.getornull(rt->texture);
  3615. tex->is_render_target = true;
  3616. }
  3617. _clear_render_target(rt);
  3618. if (rt->size.width == 0 || rt->size.height == 0) {
  3619. return;
  3620. }
  3621. //until we implement support for HDR monitors (and render target is attached to screen), this is enough.
  3622. rt->color_format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  3623. rt->color_format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  3624. rt->image_format = rt->flags[RENDER_TARGET_TRANSPARENT] ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8;
  3625. RD::TextureFormat rd_format;
  3626. RD::TextureView rd_view;
  3627. { //attempt register
  3628. rd_format.format = rt->color_format;
  3629. rd_format.width = rt->size.width;
  3630. rd_format.height = rt->size.height;
  3631. rd_format.depth = 1;
  3632. rd_format.array_layers = 1;
  3633. rd_format.mipmaps = 1;
  3634. rd_format.type = RD::TEXTURE_TYPE_2D;
  3635. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  3636. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  3637. rd_format.shareable_formats.push_back(rt->color_format);
  3638. rd_format.shareable_formats.push_back(rt->color_format_srgb);
  3639. }
  3640. rt->color = RD::get_singleton()->texture_create(rd_format, rd_view);
  3641. ERR_FAIL_COND(rt->color.is_null());
  3642. Vector<RID> fb_textures;
  3643. fb_textures.push_back(rt->color);
  3644. rt->framebuffer = RD::get_singleton()->framebuffer_create(fb_textures);
  3645. if (rt->framebuffer.is_null()) {
  3646. _clear_render_target(rt);
  3647. ERR_FAIL_COND(rt->framebuffer.is_null());
  3648. }
  3649. { //update texture
  3650. Texture *tex = texture_owner.getornull(rt->texture);
  3651. //free existing textures
  3652. if (RD::get_singleton()->texture_is_valid(tex->rd_texture)) {
  3653. RD::get_singleton()->free(tex->rd_texture);
  3654. }
  3655. if (RD::get_singleton()->texture_is_valid(tex->rd_texture_srgb)) {
  3656. RD::get_singleton()->free(tex->rd_texture_srgb);
  3657. }
  3658. tex->rd_texture = RID();
  3659. tex->rd_texture_srgb = RID();
  3660. //create shared textures to the color buffer,
  3661. //so transparent can be supported
  3662. RD::TextureView view;
  3663. view.format_override = rt->color_format;
  3664. if (!rt->flags[RENDER_TARGET_TRANSPARENT]) {
  3665. view.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  3666. }
  3667. tex->rd_texture = RD::get_singleton()->texture_create_shared(view, rt->color);
  3668. if (rt->color_format_srgb != RD::DATA_FORMAT_MAX) {
  3669. view.format_override = rt->color_format_srgb;
  3670. tex->rd_texture_srgb = RD::get_singleton()->texture_create_shared(view, rt->color);
  3671. }
  3672. tex->rd_view = view;
  3673. tex->width = rt->size.width;
  3674. tex->height = rt->size.height;
  3675. tex->width_2d = rt->size.width;
  3676. tex->height_2d = rt->size.height;
  3677. tex->rd_format = rt->color_format;
  3678. tex->rd_format_srgb = rt->color_format_srgb;
  3679. tex->format = rt->image_format;
  3680. Vector<RID> proxies = tex->proxies; //make a copy, since update may change it
  3681. for (int i = 0; i < proxies.size(); i++) {
  3682. texture_proxy_update(proxies[i], rt->texture);
  3683. }
  3684. }
  3685. }
  3686. void RasterizerStorageRD::_create_render_target_backbuffer(RenderTarget *rt) {
  3687. ERR_FAIL_COND(rt->backbuffer.is_valid());
  3688. uint32_t mipmaps_required = Image::get_image_required_mipmaps(rt->size.width, rt->size.height, Image::FORMAT_RGBA8);
  3689. RD::TextureFormat tf;
  3690. tf.format = rt->color_format;
  3691. tf.width = rt->size.width;
  3692. tf.height = rt->size.height;
  3693. tf.type = RD::TEXTURE_TYPE_2D;
  3694. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  3695. tf.mipmaps = mipmaps_required;
  3696. rt->backbuffer = RD::get_singleton()->texture_create(tf, RD::TextureView());
  3697. rt->backbuffer_mipmap0 = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->backbuffer, 0, 0);
  3698. //create mipmaps
  3699. for (uint32_t i = 1; i < mipmaps_required; i++) {
  3700. RenderTarget::BackbufferMipmap mm;
  3701. {
  3702. mm.mipmap = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->backbuffer, 0, i);
  3703. }
  3704. {
  3705. Size2 mm_size = Image::get_image_mipmap_size(tf.width, tf.height, Image::FORMAT_RGBA8, i);
  3706. RD::TextureFormat mmtf = tf;
  3707. mmtf.width = mm_size.width;
  3708. mmtf.height = mm_size.height;
  3709. mmtf.mipmaps = 1;
  3710. mm.mipmap_copy = RD::get_singleton()->texture_create(mmtf, RD::TextureView());
  3711. }
  3712. rt->backbuffer_mipmaps.push_back(mm);
  3713. }
  3714. }
  3715. RID RasterizerStorageRD::render_target_create() {
  3716. RenderTarget render_target;
  3717. render_target.was_used = false;
  3718. render_target.clear_requested = false;
  3719. for (int i = 0; i < RENDER_TARGET_FLAG_MAX; i++) {
  3720. render_target.flags[i] = false;
  3721. }
  3722. _update_render_target(&render_target);
  3723. return render_target_owner.make_rid(render_target);
  3724. }
  3725. void RasterizerStorageRD::render_target_set_position(RID p_render_target, int p_x, int p_y) {
  3726. //unused for this render target
  3727. }
  3728. void RasterizerStorageRD::render_target_set_size(RID p_render_target, int p_width, int p_height) {
  3729. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3730. ERR_FAIL_COND(!rt);
  3731. rt->size.x = p_width;
  3732. rt->size.y = p_height;
  3733. _update_render_target(rt);
  3734. }
  3735. RID RasterizerStorageRD::render_target_get_texture(RID p_render_target) {
  3736. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3737. ERR_FAIL_COND_V(!rt, RID());
  3738. return rt->texture;
  3739. }
  3740. void RasterizerStorageRD::render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id) {
  3741. }
  3742. void RasterizerStorageRD::render_target_set_flag(RID p_render_target, RenderTargetFlags p_flag, bool p_value) {
  3743. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3744. ERR_FAIL_COND(!rt);
  3745. rt->flags[p_flag] = p_value;
  3746. _update_render_target(rt);
  3747. }
  3748. bool RasterizerStorageRD::render_target_was_used(RID p_render_target) {
  3749. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3750. ERR_FAIL_COND_V(!rt, false);
  3751. return rt->was_used;
  3752. }
  3753. void RasterizerStorageRD::render_target_set_as_unused(RID p_render_target) {
  3754. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3755. ERR_FAIL_COND(!rt);
  3756. rt->was_used = false;
  3757. }
  3758. Size2 RasterizerStorageRD::render_target_get_size(RID p_render_target) {
  3759. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3760. ERR_FAIL_COND_V(!rt, Size2());
  3761. return rt->size;
  3762. }
  3763. RID RasterizerStorageRD::render_target_get_rd_framebuffer(RID p_render_target) {
  3764. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3765. ERR_FAIL_COND_V(!rt, RID());
  3766. return rt->framebuffer;
  3767. }
  3768. RID RasterizerStorageRD::render_target_get_rd_texture(RID p_render_target) {
  3769. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3770. ERR_FAIL_COND_V(!rt, RID());
  3771. return rt->color;
  3772. }
  3773. void RasterizerStorageRD::render_target_request_clear(RID p_render_target, const Color &p_clear_color) {
  3774. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3775. ERR_FAIL_COND(!rt);
  3776. rt->clear_requested = true;
  3777. rt->clear_color = p_clear_color;
  3778. }
  3779. bool RasterizerStorageRD::render_target_is_clear_requested(RID p_render_target) {
  3780. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3781. ERR_FAIL_COND_V(!rt, false);
  3782. return rt->clear_requested;
  3783. }
  3784. Color RasterizerStorageRD::render_target_get_clear_request_color(RID p_render_target) {
  3785. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3786. ERR_FAIL_COND_V(!rt, Color());
  3787. return rt->clear_color;
  3788. }
  3789. void RasterizerStorageRD::render_target_disable_clear_request(RID p_render_target) {
  3790. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3791. ERR_FAIL_COND(!rt);
  3792. rt->clear_requested = false;
  3793. }
  3794. void RasterizerStorageRD::render_target_do_clear_request(RID p_render_target) {
  3795. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3796. ERR_FAIL_COND(!rt);
  3797. if (!rt->clear_requested) {
  3798. return;
  3799. }
  3800. Vector<Color> clear_colors;
  3801. clear_colors.push_back(rt->clear_color);
  3802. 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);
  3803. RD::get_singleton()->draw_list_end();
  3804. rt->clear_requested = false;
  3805. }
  3806. void RasterizerStorageRD::render_target_copy_to_back_buffer(RID p_render_target, const Rect2i &p_region) {
  3807. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3808. ERR_FAIL_COND(!rt);
  3809. if (!rt->backbuffer.is_valid()) {
  3810. _create_render_target_backbuffer(rt);
  3811. }
  3812. Rect2i region = p_region;
  3813. if (region == Rect2i()) {
  3814. region.size = rt->size;
  3815. }
  3816. //single texture copy for backbuffer
  3817. 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);
  3818. //effects.copy(rt->color, rt->backbuffer_fb, blur_region);
  3819. //then mipmap blur
  3820. RID prev_texture = rt->color; //use color, not backbuffer, as bb has mipmaps.
  3821. for (int i = 0; i < rt->backbuffer_mipmaps.size(); i++) {
  3822. region.position.x >>= 1;
  3823. region.position.y >>= 1;
  3824. region.size.x = MAX(1, region.size.x >> 1);
  3825. region.size.y = MAX(1, region.size.y >> 1);
  3826. const RenderTarget::BackbufferMipmap &mm = rt->backbuffer_mipmaps[i];
  3827. effects.gaussian_blur(prev_texture, mm.mipmap, mm.mipmap_copy, region, true);
  3828. prev_texture = mm.mipmap;
  3829. }
  3830. }
  3831. RID RasterizerStorageRD::render_target_get_back_buffer_uniform_set(RID p_render_target, RID p_base_shader) {
  3832. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3833. ERR_FAIL_COND_V(!rt, RID());
  3834. if (!rt->backbuffer.is_valid()) {
  3835. _create_render_target_backbuffer(rt);
  3836. }
  3837. if (rt->backbuffer_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(rt->backbuffer_uniform_set)) {
  3838. return rt->backbuffer_uniform_set; //if still valid, return/reuse it.
  3839. }
  3840. //create otherwise
  3841. Vector<RD::Uniform> uniforms;
  3842. RD::Uniform u;
  3843. u.type = RD::UNIFORM_TYPE_TEXTURE;
  3844. u.binding = 0;
  3845. u.ids.push_back(rt->backbuffer);
  3846. uniforms.push_back(u);
  3847. rt->backbuffer_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, p_base_shader, 3);
  3848. ERR_FAIL_COND_V(!rt->backbuffer_uniform_set.is_valid(), RID());
  3849. return rt->backbuffer_uniform_set;
  3850. }
  3851. void RasterizerStorageRD::base_update_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) {
  3852. if (mesh_owner.owns(p_base)) {
  3853. Mesh *mesh = mesh_owner.getornull(p_base);
  3854. p_instance->update_dependency(&mesh->instance_dependency);
  3855. } else if (multimesh_owner.owns(p_base)) {
  3856. MultiMesh *multimesh = multimesh_owner.getornull(p_base);
  3857. p_instance->update_dependency(&multimesh->instance_dependency);
  3858. if (multimesh->mesh.is_valid()) {
  3859. base_update_dependency(multimesh->mesh, p_instance);
  3860. }
  3861. } else if (reflection_probe_owner.owns(p_base)) {
  3862. ReflectionProbe *rp = reflection_probe_owner.getornull(p_base);
  3863. p_instance->update_dependency(&rp->instance_dependency);
  3864. } else if (decal_owner.owns(p_base)) {
  3865. Decal *decal = decal_owner.getornull(p_base);
  3866. p_instance->update_dependency(&decal->instance_dependency);
  3867. } else if (gi_probe_owner.owns(p_base)) {
  3868. GIProbe *gip = gi_probe_owner.getornull(p_base);
  3869. p_instance->update_dependency(&gip->instance_dependency);
  3870. } else if (lightmap_owner.owns(p_base)) {
  3871. Lightmap *lm = lightmap_owner.getornull(p_base);
  3872. p_instance->update_dependency(&lm->instance_dependency);
  3873. } else if (light_owner.owns(p_base)) {
  3874. Light *l = light_owner.getornull(p_base);
  3875. p_instance->update_dependency(&l->instance_dependency);
  3876. }
  3877. }
  3878. void RasterizerStorageRD::skeleton_update_dependency(RID p_skeleton, RasterizerScene::InstanceBase *p_instance) {
  3879. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  3880. ERR_FAIL_COND(!skeleton);
  3881. p_instance->update_dependency(&skeleton->instance_dependency);
  3882. }
  3883. RS::InstanceType RasterizerStorageRD::get_base_type(RID p_rid) const {
  3884. if (mesh_owner.owns(p_rid)) {
  3885. return RS::INSTANCE_MESH;
  3886. }
  3887. if (multimesh_owner.owns(p_rid)) {
  3888. return RS::INSTANCE_MULTIMESH;
  3889. }
  3890. if (reflection_probe_owner.owns(p_rid)) {
  3891. return RS::INSTANCE_REFLECTION_PROBE;
  3892. }
  3893. if (decal_owner.owns(p_rid)) {
  3894. return RS::INSTANCE_DECAL;
  3895. }
  3896. if (gi_probe_owner.owns(p_rid)) {
  3897. return RS::INSTANCE_GI_PROBE;
  3898. }
  3899. if (light_owner.owns(p_rid)) {
  3900. return RS::INSTANCE_LIGHT;
  3901. }
  3902. if (lightmap_owner.owns(p_rid)) {
  3903. return RS::INSTANCE_LIGHTMAP;
  3904. }
  3905. return RS::INSTANCE_NONE;
  3906. }
  3907. void RasterizerStorageRD::texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp) {
  3908. if (!decal_atlas.textures.has(p_texture)) {
  3909. DecalAtlas::Texture t;
  3910. t.users = 1;
  3911. t.panorama_to_dp_users = p_panorama_to_dp ? 1 : 0;
  3912. decal_atlas.textures[p_texture] = t;
  3913. decal_atlas.dirty = true;
  3914. } else {
  3915. DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);
  3916. t->users++;
  3917. if (p_panorama_to_dp) {
  3918. t->panorama_to_dp_users++;
  3919. }
  3920. }
  3921. }
  3922. void RasterizerStorageRD::texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp) {
  3923. DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);
  3924. ERR_FAIL_COND(!t);
  3925. t->users--;
  3926. if (p_panorama_to_dp) {
  3927. ERR_FAIL_COND(t->panorama_to_dp_users == 0);
  3928. t->panorama_to_dp_users--;
  3929. }
  3930. if (t->users == 0) {
  3931. decal_atlas.textures.erase(p_texture);
  3932. //do not mark it dirty, there is no need to since it remains working
  3933. }
  3934. }
  3935. RID RasterizerStorageRD::decal_atlas_get_texture() const {
  3936. return decal_atlas.texture;
  3937. }
  3938. RID RasterizerStorageRD::decal_atlas_get_texture_srgb() const {
  3939. return decal_atlas.texture;
  3940. }
  3941. void RasterizerStorageRD::_update_decal_atlas() {
  3942. if (!decal_atlas.dirty) {
  3943. return; //nothing to do
  3944. }
  3945. decal_atlas.dirty = false;
  3946. if (decal_atlas.texture.is_valid()) {
  3947. RD::get_singleton()->free(decal_atlas.texture);
  3948. decal_atlas.texture = RID();
  3949. decal_atlas.texture_srgb = RID();
  3950. decal_atlas.texture_mipmaps.clear();
  3951. }
  3952. int border = 1 << decal_atlas.mipmaps;
  3953. if (decal_atlas.textures.size()) {
  3954. //generate atlas
  3955. Vector<DecalAtlas::SortItem> itemsv;
  3956. itemsv.resize(decal_atlas.textures.size());
  3957. int base_size = 8;
  3958. const RID *K = NULL;
  3959. int idx = 0;
  3960. while ((K = decal_atlas.textures.next(K))) {
  3961. DecalAtlas::SortItem &si = itemsv.write[idx];
  3962. Texture *src_tex = texture_owner.getornull(*K);
  3963. si.size.width = (src_tex->width / border) + 1;
  3964. si.size.height = (src_tex->height / border) + 1;
  3965. si.pixel_size = Size2i(src_tex->width, src_tex->height);
  3966. if (base_size < si.size.width) {
  3967. base_size = nearest_power_of_2_templated(si.size.width);
  3968. }
  3969. si.texture = *K;
  3970. idx++;
  3971. }
  3972. //sort items by size
  3973. itemsv.sort();
  3974. //attempt to create atlas
  3975. int item_count = itemsv.size();
  3976. DecalAtlas::SortItem *items = itemsv.ptrw();
  3977. int atlas_height = 0;
  3978. while (true) {
  3979. Vector<int> v_offsetsv;
  3980. v_offsetsv.resize(base_size);
  3981. int *v_offsets = v_offsetsv.ptrw();
  3982. zeromem(v_offsets, sizeof(int) * base_size);
  3983. int max_height = 0;
  3984. for (int i = 0; i < item_count; i++) {
  3985. //best fit
  3986. DecalAtlas::SortItem &si = items[i];
  3987. int best_idx = -1;
  3988. int best_height = 0x7FFFFFFF;
  3989. for (int j = 0; j <= base_size - si.size.width; j++) {
  3990. int height = 0;
  3991. for (int k = 0; k < si.size.width; k++) {
  3992. int h = v_offsets[k + j];
  3993. if (h > height) {
  3994. height = h;
  3995. if (height > best_height) {
  3996. break; //already bad
  3997. }
  3998. }
  3999. }
  4000. if (height < best_height) {
  4001. best_height = height;
  4002. best_idx = j;
  4003. }
  4004. }
  4005. //update
  4006. for (int k = 0; k < si.size.width; k++) {
  4007. v_offsets[k + best_idx] = best_height + si.size.height;
  4008. }
  4009. si.pos.x = best_idx;
  4010. si.pos.y = best_height;
  4011. if (si.pos.y + si.size.height > max_height) {
  4012. max_height = si.pos.y + si.size.height;
  4013. }
  4014. }
  4015. if (max_height <= base_size * 2) {
  4016. atlas_height = max_height;
  4017. break; //good ratio, break;
  4018. }
  4019. base_size *= 2;
  4020. }
  4021. decal_atlas.size.width = base_size * border;
  4022. decal_atlas.size.height = nearest_power_of_2_templated(atlas_height * border);
  4023. for (int i = 0; i < item_count; i++) {
  4024. DecalAtlas::Texture *t = decal_atlas.textures.getptr(items[i].texture);
  4025. t->uv_rect.position = items[i].pos * border + Vector2i(border / 2, border / 2);
  4026. t->uv_rect.size = items[i].pixel_size;
  4027. t->uv_rect.position /= Size2(decal_atlas.size);
  4028. t->uv_rect.size /= Size2(decal_atlas.size);
  4029. }
  4030. } else {
  4031. //use border as size, so it at least has enough mipmaps
  4032. decal_atlas.size.width = border;
  4033. decal_atlas.size.height = border;
  4034. }
  4035. //blit textures
  4036. RD::TextureFormat tformat;
  4037. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  4038. tformat.width = decal_atlas.size.width;
  4039. tformat.height = decal_atlas.size.height;
  4040. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  4041. tformat.type = RD::TEXTURE_TYPE_2D;
  4042. tformat.mipmaps = decal_atlas.mipmaps;
  4043. tformat.shareable_formats.push_back(RD::DATA_FORMAT_R8G8B8A8_UNORM);
  4044. tformat.shareable_formats.push_back(RD::DATA_FORMAT_R8G8B8A8_SRGB);
  4045. decal_atlas.texture = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  4046. {
  4047. //create the framebuffer
  4048. Size2i s = decal_atlas.size;
  4049. for (int i = 0; i < decal_atlas.mipmaps; i++) {
  4050. DecalAtlas::MipMap mm;
  4051. mm.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), decal_atlas.texture, 0, i);
  4052. Vector<RID> fb;
  4053. fb.push_back(mm.texture);
  4054. mm.fb = RD::get_singleton()->framebuffer_create(fb);
  4055. mm.size = s;
  4056. decal_atlas.texture_mipmaps.push_back(mm);
  4057. s.width = MAX(1, s.width >> 1);
  4058. s.height = MAX(1, s.height >> 1);
  4059. }
  4060. {
  4061. //create the SRGB variant
  4062. RD::TextureView rd_view;
  4063. rd_view.format_override = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  4064. decal_atlas.texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, decal_atlas.texture);
  4065. }
  4066. }
  4067. RID prev_texture;
  4068. for (int i = 0; i < decal_atlas.texture_mipmaps.size(); i++) {
  4069. const DecalAtlas::MipMap &mm = decal_atlas.texture_mipmaps[i];
  4070. Color clear_color(0, 0, 0, 0);
  4071. if (decal_atlas.textures.size()) {
  4072. if (i == 0) {
  4073. Vector<Color> cc;
  4074. cc.push_back(clear_color);
  4075. 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);
  4076. const RID *K = NULL;
  4077. while ((K = decal_atlas.textures.next(K))) {
  4078. DecalAtlas::Texture *t = decal_atlas.textures.getptr(*K);
  4079. Texture *src_tex = texture_owner.getornull(*K);
  4080. effects.copy_to_atlas_fb(src_tex->rd_texture, mm.fb, t->uv_rect, draw_list, false, t->panorama_to_dp_users > 0);
  4081. }
  4082. RD::get_singleton()->draw_list_end();
  4083. prev_texture = mm.texture;
  4084. } else {
  4085. effects.copy_to_fb_rect(prev_texture, mm.fb, Rect2i(Point2i(), mm.size));
  4086. prev_texture = mm.texture;
  4087. }
  4088. } else {
  4089. RD::get_singleton()->texture_clear(mm.texture, clear_color, 0, 1, 0, 1, false);
  4090. }
  4091. }
  4092. }
  4093. int32_t RasterizerStorageRD::_global_variable_allocate(uint32_t p_elements) {
  4094. int32_t idx = 0;
  4095. while (idx + p_elements <= global_variables.buffer_size) {
  4096. if (global_variables.buffer_usage[idx].elements == 0) {
  4097. bool valid = true;
  4098. for (uint32_t i = 1; i < p_elements; i++) {
  4099. if (global_variables.buffer_usage[idx + i].elements > 0) {
  4100. valid = false;
  4101. idx += i + global_variables.buffer_usage[idx + i].elements;
  4102. break;
  4103. }
  4104. }
  4105. if (!valid) {
  4106. continue; //if not valid, idx is in new position
  4107. }
  4108. return idx;
  4109. } else {
  4110. idx += global_variables.buffer_usage[idx].elements;
  4111. }
  4112. }
  4113. return -1;
  4114. }
  4115. void RasterizerStorageRD::_global_variable_store_in_buffer(int32_t p_index, RS::GlobalVariableType p_type, const Variant &p_value) {
  4116. switch (p_type) {
  4117. case RS::GLOBAL_VAR_TYPE_BOOL: {
  4118. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  4119. bool b = p_value;
  4120. bv.x = b ? 1.0 : 0.0;
  4121. bv.y = 0.0;
  4122. bv.z = 0.0;
  4123. bv.w = 0.0;
  4124. } break;
  4125. case RS::GLOBAL_VAR_TYPE_BVEC2: {
  4126. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  4127. uint32_t bvec = p_value;
  4128. bv.x = (bvec & 1) ? 1.0 : 0.0;
  4129. bv.y = (bvec & 2) ? 1.0 : 0.0;
  4130. bv.z = 0.0;
  4131. bv.w = 0.0;
  4132. } break;
  4133. case RS::GLOBAL_VAR_TYPE_BVEC3: {
  4134. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  4135. uint32_t bvec = p_value;
  4136. bv.x = (bvec & 1) ? 1.0 : 0.0;
  4137. bv.y = (bvec & 2) ? 1.0 : 0.0;
  4138. bv.z = (bvec & 4) ? 1.0 : 0.0;
  4139. bv.w = 0.0;
  4140. } break;
  4141. case RS::GLOBAL_VAR_TYPE_BVEC4: {
  4142. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  4143. uint32_t bvec = p_value;
  4144. bv.x = (bvec & 1) ? 1.0 : 0.0;
  4145. bv.y = (bvec & 2) ? 1.0 : 0.0;
  4146. bv.z = (bvec & 4) ? 1.0 : 0.0;
  4147. bv.w = (bvec & 8) ? 1.0 : 0.0;
  4148. } break;
  4149. case RS::GLOBAL_VAR_TYPE_INT: {
  4150. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  4151. int32_t v = p_value;
  4152. bv.x = v;
  4153. bv.y = 0;
  4154. bv.z = 0;
  4155. bv.w = 0;
  4156. } break;
  4157. case RS::GLOBAL_VAR_TYPE_IVEC2: {
  4158. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  4159. Vector2i v = p_value;
  4160. bv.x = v.x;
  4161. bv.y = v.y;
  4162. bv.z = 0;
  4163. bv.w = 0;
  4164. } break;
  4165. case RS::GLOBAL_VAR_TYPE_IVEC3: {
  4166. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  4167. Vector3i v = p_value;
  4168. bv.x = v.x;
  4169. bv.y = v.y;
  4170. bv.z = v.z;
  4171. bv.w = 0;
  4172. } break;
  4173. case RS::GLOBAL_VAR_TYPE_IVEC4: {
  4174. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  4175. Vector<int32_t> v = p_value;
  4176. bv.x = v.size() >= 1 ? v[0] : 0;
  4177. bv.y = v.size() >= 2 ? v[1] : 0;
  4178. bv.z = v.size() >= 3 ? v[2] : 0;
  4179. bv.w = v.size() >= 4 ? v[3] : 0;
  4180. } break;
  4181. case RS::GLOBAL_VAR_TYPE_RECT2I: {
  4182. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  4183. Rect2i v = p_value;
  4184. bv.x = v.position.x;
  4185. bv.y = v.position.y;
  4186. bv.z = v.size.x;
  4187. bv.w = v.size.y;
  4188. } break;
  4189. case RS::GLOBAL_VAR_TYPE_UINT: {
  4190. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  4191. uint32_t v = p_value;
  4192. bv.x = v;
  4193. bv.y = 0;
  4194. bv.z = 0;
  4195. bv.w = 0;
  4196. } break;
  4197. case RS::GLOBAL_VAR_TYPE_UVEC2: {
  4198. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  4199. Vector2i v = p_value;
  4200. bv.x = v.x;
  4201. bv.y = v.y;
  4202. bv.z = 0;
  4203. bv.w = 0;
  4204. } break;
  4205. case RS::GLOBAL_VAR_TYPE_UVEC3: {
  4206. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  4207. Vector3i v = p_value;
  4208. bv.x = v.x;
  4209. bv.y = v.y;
  4210. bv.z = v.z;
  4211. bv.w = 0;
  4212. } break;
  4213. case RS::GLOBAL_VAR_TYPE_UVEC4: {
  4214. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  4215. Vector<int32_t> v = p_value;
  4216. bv.x = v.size() >= 1 ? v[0] : 0;
  4217. bv.y = v.size() >= 2 ? v[1] : 0;
  4218. bv.z = v.size() >= 3 ? v[2] : 0;
  4219. bv.w = v.size() >= 4 ? v[3] : 0;
  4220. } break;
  4221. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  4222. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  4223. float v = p_value;
  4224. bv.x = v;
  4225. bv.y = 0;
  4226. bv.z = 0;
  4227. bv.w = 0;
  4228. } break;
  4229. case RS::GLOBAL_VAR_TYPE_VEC2: {
  4230. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  4231. Vector2 v = p_value;
  4232. bv.x = v.x;
  4233. bv.y = v.y;
  4234. bv.z = 0;
  4235. bv.w = 0;
  4236. } break;
  4237. case RS::GLOBAL_VAR_TYPE_VEC3: {
  4238. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  4239. Vector3 v = p_value;
  4240. bv.x = v.x;
  4241. bv.y = v.y;
  4242. bv.z = v.z;
  4243. bv.w = 0;
  4244. } break;
  4245. case RS::GLOBAL_VAR_TYPE_VEC4: {
  4246. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  4247. Plane v = p_value;
  4248. bv.x = v.normal.x;
  4249. bv.y = v.normal.y;
  4250. bv.z = v.normal.z;
  4251. bv.w = v.d;
  4252. } break;
  4253. case RS::GLOBAL_VAR_TYPE_COLOR: {
  4254. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  4255. Color v = p_value;
  4256. bv.x = v.r;
  4257. bv.y = v.g;
  4258. bv.z = v.b;
  4259. bv.w = v.a;
  4260. GlobalVariables::Value &bv_linear = global_variables.buffer_values[p_index + 1];
  4261. v = v.to_linear();
  4262. bv_linear.x = v.r;
  4263. bv_linear.y = v.g;
  4264. bv_linear.z = v.b;
  4265. bv_linear.w = v.a;
  4266. } break;
  4267. case RS::GLOBAL_VAR_TYPE_RECT2: {
  4268. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  4269. Rect2 v = p_value;
  4270. bv.x = v.position.x;
  4271. bv.y = v.position.y;
  4272. bv.z = v.size.x;
  4273. bv.w = v.size.y;
  4274. } break;
  4275. case RS::GLOBAL_VAR_TYPE_MAT2: {
  4276. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  4277. Vector<float> m2 = p_value;
  4278. if (m2.size() < 4) {
  4279. m2.resize(4);
  4280. }
  4281. bv[0].x = m2[0];
  4282. bv[0].y = m2[1];
  4283. bv[0].z = 0;
  4284. bv[0].w = 0;
  4285. bv[1].x = m2[2];
  4286. bv[1].y = m2[3];
  4287. bv[1].z = 0;
  4288. bv[1].w = 0;
  4289. } break;
  4290. case RS::GLOBAL_VAR_TYPE_MAT3: {
  4291. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  4292. Basis v = p_value;
  4293. bv[0].x = v.elements[0][0];
  4294. bv[0].y = v.elements[1][0];
  4295. bv[0].z = v.elements[2][0];
  4296. bv[0].w = 0;
  4297. bv[1].x = v.elements[0][1];
  4298. bv[1].y = v.elements[1][1];
  4299. bv[1].z = v.elements[2][1];
  4300. bv[1].w = 0;
  4301. bv[2].x = v.elements[0][2];
  4302. bv[2].y = v.elements[1][2];
  4303. bv[2].z = v.elements[2][2];
  4304. bv[2].w = 0;
  4305. } break;
  4306. case RS::GLOBAL_VAR_TYPE_MAT4: {
  4307. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  4308. Vector<float> m2 = p_value;
  4309. if (m2.size() < 16) {
  4310. m2.resize(16);
  4311. }
  4312. bv[0].x = m2[0];
  4313. bv[0].y = m2[1];
  4314. bv[0].z = m2[2];
  4315. bv[0].w = m2[3];
  4316. bv[1].x = m2[4];
  4317. bv[1].y = m2[5];
  4318. bv[1].z = m2[6];
  4319. bv[1].w = m2[7];
  4320. bv[2].x = m2[8];
  4321. bv[2].y = m2[9];
  4322. bv[2].z = m2[10];
  4323. bv[2].w = m2[11];
  4324. bv[3].x = m2[12];
  4325. bv[3].y = m2[13];
  4326. bv[3].z = m2[14];
  4327. bv[3].w = m2[15];
  4328. } break;
  4329. case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
  4330. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  4331. Transform2D v = p_value;
  4332. bv[0].x = v.elements[0][0];
  4333. bv[0].y = v.elements[0][1];
  4334. bv[0].z = 0;
  4335. bv[0].w = 0;
  4336. bv[1].x = v.elements[1][0];
  4337. bv[1].y = v.elements[1][1];
  4338. bv[1].z = 0;
  4339. bv[1].w = 0;
  4340. bv[2].x = v.elements[2][0];
  4341. bv[2].y = v.elements[2][1];
  4342. bv[2].z = 1;
  4343. bv[2].w = 0;
  4344. } break;
  4345. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  4346. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  4347. Transform v = p_value;
  4348. bv[0].x = v.basis.elements[0][0];
  4349. bv[0].y = v.basis.elements[1][0];
  4350. bv[0].z = v.basis.elements[2][0];
  4351. bv[0].w = 0;
  4352. bv[1].x = v.basis.elements[0][1];
  4353. bv[1].y = v.basis.elements[1][1];
  4354. bv[1].z = v.basis.elements[2][1];
  4355. bv[1].w = 0;
  4356. bv[2].x = v.basis.elements[0][2];
  4357. bv[2].y = v.basis.elements[1][2];
  4358. bv[2].z = v.basis.elements[2][2];
  4359. bv[2].w = 0;
  4360. bv[3].x = v.origin.x;
  4361. bv[3].y = v.origin.y;
  4362. bv[3].z = v.origin.z;
  4363. bv[3].w = 1;
  4364. } break;
  4365. default: {
  4366. ERR_FAIL();
  4367. }
  4368. }
  4369. }
  4370. void RasterizerStorageRD::_global_variable_mark_buffer_dirty(int32_t p_index, int32_t p_elements) {
  4371. int32_t prev_chunk = -1;
  4372. for (int32_t i = 0; i < p_elements; i++) {
  4373. int32_t chunk = (p_index + i) / GlobalVariables::BUFFER_DIRTY_REGION_SIZE;
  4374. if (chunk != prev_chunk) {
  4375. if (!global_variables.buffer_dirty_regions[chunk]) {
  4376. global_variables.buffer_dirty_regions[chunk] = true;
  4377. global_variables.buffer_dirty_region_count++;
  4378. }
  4379. }
  4380. prev_chunk = chunk;
  4381. }
  4382. }
  4383. void RasterizerStorageRD::global_variable_add(const StringName &p_name, RS::GlobalVariableType p_type, const Variant &p_value) {
  4384. ERR_FAIL_COND(global_variables.variables.has(p_name));
  4385. GlobalVariables::Variable gv;
  4386. gv.type = p_type;
  4387. gv.value = p_value;
  4388. gv.buffer_index = -1;
  4389. if (p_type >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
  4390. //is texture
  4391. global_variables.must_update_texture_materials = true; //normally ther are no
  4392. } else {
  4393. gv.buffer_elements = 1;
  4394. if (p_type == RS::GLOBAL_VAR_TYPE_COLOR || p_type == RS::GLOBAL_VAR_TYPE_MAT2) {
  4395. //color needs to elements to store srgb and linear
  4396. gv.buffer_elements = 2;
  4397. }
  4398. if (p_type == RS::GLOBAL_VAR_TYPE_MAT3 || p_type == RS::GLOBAL_VAR_TYPE_TRANSFORM_2D) {
  4399. //color needs to elements to store srgb and linear
  4400. gv.buffer_elements = 3;
  4401. }
  4402. if (p_type == RS::GLOBAL_VAR_TYPE_MAT4 || p_type == RS::GLOBAL_VAR_TYPE_TRANSFORM) {
  4403. //color needs to elements to store srgb and linear
  4404. gv.buffer_elements = 4;
  4405. }
  4406. //is vector, allocate in buffer and update index
  4407. gv.buffer_index = _global_variable_allocate(gv.buffer_elements);
  4408. 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)));
  4409. global_variables.buffer_usage[gv.buffer_index].elements = gv.buffer_elements;
  4410. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  4411. _global_variable_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  4412. global_variables.must_update_buffer_materials = true; //normally ther are no
  4413. }
  4414. global_variables.variables[p_name] = gv;
  4415. }
  4416. void RasterizerStorageRD::global_variable_remove(const StringName &p_name) {
  4417. if (!global_variables.variables.has(p_name)) {
  4418. return;
  4419. }
  4420. GlobalVariables::Variable &gv = global_variables.variables[p_name];
  4421. if (gv.buffer_index >= 0) {
  4422. global_variables.buffer_usage[gv.buffer_index].elements = 0;
  4423. global_variables.must_update_buffer_materials = true;
  4424. } else {
  4425. global_variables.must_update_texture_materials = true;
  4426. }
  4427. global_variables.variables.erase(p_name);
  4428. }
  4429. Vector<StringName> RasterizerStorageRD::global_variable_get_list() const {
  4430. if (!Engine::get_singleton()->is_editor_hint()) {
  4431. ERR_FAIL_V_MSG(Vector<StringName>(), "This function should never be used outside the editor, it can severely damage performance.");
  4432. }
  4433. const StringName *K = NULL;
  4434. Vector<StringName> names;
  4435. while ((K = global_variables.variables.next(K))) {
  4436. names.push_back(*K);
  4437. }
  4438. names.sort_custom<StringName::AlphCompare>();
  4439. return names;
  4440. }
  4441. void RasterizerStorageRD::global_variable_set(const StringName &p_name, const Variant &p_value) {
  4442. ERR_FAIL_COND(!global_variables.variables.has(p_name));
  4443. GlobalVariables::Variable &gv = global_variables.variables[p_name];
  4444. gv.value = p_value;
  4445. if (gv.override.get_type() == Variant::NIL) {
  4446. if (gv.buffer_index >= 0) {
  4447. //buffer
  4448. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  4449. _global_variable_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  4450. } else {
  4451. //texture
  4452. for (Set<RID>::Element *E = gv.texture_materials.front(); E; E = E->next()) {
  4453. Material *material = material_owner.getornull(E->get());
  4454. ERR_CONTINUE(!material);
  4455. _material_queue_update(material, false, true);
  4456. }
  4457. }
  4458. }
  4459. }
  4460. void RasterizerStorageRD::global_variable_set_override(const StringName &p_name, const Variant &p_value) {
  4461. if (!global_variables.variables.has(p_name)) {
  4462. return; //variable may not exist
  4463. }
  4464. GlobalVariables::Variable &gv = global_variables.variables[p_name];
  4465. gv.override = p_value;
  4466. if (gv.buffer_index >= 0) {
  4467. //buffer
  4468. if (gv.override.get_type() == Variant::NIL) {
  4469. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  4470. } else {
  4471. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.override);
  4472. }
  4473. _global_variable_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  4474. } else {
  4475. //texture
  4476. //texture
  4477. for (Set<RID>::Element *E = gv.texture_materials.front(); E; E = E->next()) {
  4478. Material *material = material_owner.getornull(E->get());
  4479. ERR_CONTINUE(!material);
  4480. _material_queue_update(material, false, true);
  4481. }
  4482. }
  4483. }
  4484. Variant RasterizerStorageRD::global_variable_get(const StringName &p_name) const {
  4485. if (!Engine::get_singleton()->is_editor_hint()) {
  4486. ERR_FAIL_V_MSG(Variant(), "This function should never be used outside the editor, it can severely damage performance.");
  4487. }
  4488. if (!global_variables.variables.has(p_name)) {
  4489. return Variant();
  4490. }
  4491. return global_variables.variables[p_name].value;
  4492. }
  4493. RS::GlobalVariableType RasterizerStorageRD::global_variable_get_type_internal(const StringName &p_name) const {
  4494. if (!global_variables.variables.has(p_name)) {
  4495. return RS::GLOBAL_VAR_TYPE_MAX;
  4496. }
  4497. return global_variables.variables[p_name].type;
  4498. }
  4499. RS::GlobalVariableType RasterizerStorageRD::global_variable_get_type(const StringName &p_name) const {
  4500. if (!Engine::get_singleton()->is_editor_hint()) {
  4501. ERR_FAIL_V_MSG(RS::GLOBAL_VAR_TYPE_MAX, "This function should never be used outside the editor, it can severely damage performance.");
  4502. }
  4503. return global_variable_get_type_internal(p_name);
  4504. }
  4505. void RasterizerStorageRD::global_variables_load_settings(bool p_load_textures) {
  4506. List<PropertyInfo> settings;
  4507. ProjectSettings::get_singleton()->get_property_list(&settings);
  4508. for (List<PropertyInfo>::Element *E = settings.front(); E; E = E->next()) {
  4509. if (E->get().name.begins_with("shader_globals/")) {
  4510. StringName name = E->get().name.get_slice("/", 1);
  4511. Dictionary d = ProjectSettings::get_singleton()->get(E->get().name);
  4512. ERR_CONTINUE(!d.has("type"));
  4513. ERR_CONTINUE(!d.has("value"));
  4514. String type = d["type"];
  4515. static const char *global_var_type_names[RS::GLOBAL_VAR_TYPE_MAX] = {
  4516. "bool",
  4517. "bvec2",
  4518. "bvec3",
  4519. "bvec4",
  4520. "int",
  4521. "ivec2",
  4522. "ivec3",
  4523. "ivec4",
  4524. "rect2i",
  4525. "uint",
  4526. "uvec2",
  4527. "uvec3",
  4528. "uvec4",
  4529. "float",
  4530. "vec2",
  4531. "vec3",
  4532. "vec4",
  4533. "color",
  4534. "rect2",
  4535. "mat2",
  4536. "mat3",
  4537. "mat4",
  4538. "transform_2d",
  4539. "transform",
  4540. "sampler2D",
  4541. "sampler2DArray",
  4542. "sampler3D",
  4543. "samplerCube",
  4544. };
  4545. RS::GlobalVariableType gvtype = RS::GLOBAL_VAR_TYPE_MAX;
  4546. for (int i = 0; i < RS::GLOBAL_VAR_TYPE_MAX; i++) {
  4547. if (global_var_type_names[i] == type) {
  4548. gvtype = RS::GlobalVariableType(i);
  4549. break;
  4550. }
  4551. }
  4552. ERR_CONTINUE(gvtype == RS::GLOBAL_VAR_TYPE_MAX); //type invalid
  4553. Variant value = d["value"];
  4554. if (gvtype >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
  4555. //textire
  4556. if (!p_load_textures) {
  4557. value = RID();
  4558. continue;
  4559. }
  4560. String path = value;
  4561. RES resource = ResourceLoader::load(path);
  4562. ERR_CONTINUE(resource.is_null());
  4563. value = resource;
  4564. }
  4565. if (global_variables.variables.has(name)) {
  4566. //has it, update it
  4567. global_variable_set(name, value);
  4568. } else {
  4569. global_variable_add(name, gvtype, value);
  4570. }
  4571. }
  4572. }
  4573. }
  4574. void RasterizerStorageRD::global_variables_clear() {
  4575. global_variables.variables.clear(); //not right but for now enough
  4576. }
  4577. RID RasterizerStorageRD::global_variables_get_storage_buffer() const {
  4578. return global_variables.buffer;
  4579. }
  4580. int32_t RasterizerStorageRD::global_variables_instance_allocate(RID p_instance) {
  4581. ERR_FAIL_COND_V(global_variables.instance_buffer_pos.has(p_instance), -1);
  4582. int32_t pos = _global_variable_allocate(ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES);
  4583. global_variables.instance_buffer_pos[p_instance] = pos; //save anyway
  4584. ERR_FAIL_COND_V_MSG(pos < 0, -1, "Too many instances using shader instance variables. Increase buffer size in Project Settings.");
  4585. global_variables.buffer_usage[pos].elements = ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES;
  4586. return pos;
  4587. }
  4588. void RasterizerStorageRD::global_variables_instance_free(RID p_instance) {
  4589. ERR_FAIL_COND(!global_variables.instance_buffer_pos.has(p_instance));
  4590. int32_t pos = global_variables.instance_buffer_pos[p_instance];
  4591. if (pos >= 0) {
  4592. global_variables.buffer_usage[pos].elements = 0;
  4593. }
  4594. global_variables.instance_buffer_pos.erase(p_instance);
  4595. }
  4596. void RasterizerStorageRD::global_variables_instance_update(RID p_instance, int p_index, const Variant &p_value) {
  4597. if (!global_variables.instance_buffer_pos.has(p_instance)) {
  4598. return; //just not allocated, ignore
  4599. }
  4600. int32_t pos = global_variables.instance_buffer_pos[p_instance];
  4601. if (pos < 0) {
  4602. return; //again, not allocated, ignore
  4603. }
  4604. ERR_FAIL_INDEX(p_index, ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES);
  4605. 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
  4606. ShaderLanguage::DataType datatype_from_value[Variant::COLOR + 1] = {
  4607. ShaderLanguage::TYPE_MAX, //nil
  4608. ShaderLanguage::TYPE_BOOL, //bool
  4609. ShaderLanguage::TYPE_INT, //int
  4610. ShaderLanguage::TYPE_FLOAT, //float
  4611. ShaderLanguage::TYPE_MAX, //string
  4612. ShaderLanguage::TYPE_VEC2, //vec2
  4613. ShaderLanguage::TYPE_IVEC2, //vec2i
  4614. ShaderLanguage::TYPE_VEC4, //rect2
  4615. ShaderLanguage::TYPE_IVEC4, //rect2i
  4616. ShaderLanguage::TYPE_VEC3, // vec3
  4617. ShaderLanguage::TYPE_IVEC3, //vec3i
  4618. ShaderLanguage::TYPE_MAX, //xform2d not supported here
  4619. ShaderLanguage::TYPE_VEC4, //plane
  4620. ShaderLanguage::TYPE_VEC4, //quat
  4621. ShaderLanguage::TYPE_MAX, //aabb not supported here
  4622. ShaderLanguage::TYPE_MAX, //basis not supported here
  4623. ShaderLanguage::TYPE_MAX, //xform not supported here
  4624. ShaderLanguage::TYPE_VEC4 //color
  4625. };
  4626. ShaderLanguage::DataType datatype = datatype_from_value[p_value.get_type()];
  4627. 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
  4628. pos += p_index;
  4629. _fill_std140_variant_ubo_value(datatype, p_value, (uint8_t *)&global_variables.buffer_values[pos], true); //instances always use linear color in this renderer
  4630. _global_variable_mark_buffer_dirty(pos, 1);
  4631. }
  4632. void RasterizerStorageRD::_update_global_variables() {
  4633. if (global_variables.buffer_dirty_region_count > 0) {
  4634. uint32_t total_regions = global_variables.buffer_size / GlobalVariables::BUFFER_DIRTY_REGION_SIZE;
  4635. if (total_regions / global_variables.buffer_dirty_region_count <= 4) {
  4636. // 25% of regions dirty, just update all buffer
  4637. RD::get_singleton()->buffer_update(global_variables.buffer, 0, sizeof(GlobalVariables::Value) * global_variables.buffer_size, global_variables.buffer_values);
  4638. zeromem(global_variables.buffer_dirty_regions, sizeof(bool) * total_regions);
  4639. } else {
  4640. uint32_t region_byte_size = sizeof(GlobalVariables::Value) * GlobalVariables::BUFFER_DIRTY_REGION_SIZE;
  4641. for (uint32_t i = 0; i < total_regions; i++) {
  4642. if (global_variables.buffer_dirty_regions[i]) {
  4643. RD::get_singleton()->buffer_update(global_variables.buffer, i * region_byte_size, region_byte_size, global_variables.buffer_values);
  4644. global_variables.buffer_dirty_regions[i] = false;
  4645. }
  4646. }
  4647. }
  4648. global_variables.buffer_dirty_region_count = 0;
  4649. }
  4650. if (global_variables.must_update_buffer_materials) {
  4651. // only happens in the case of a buffer variable added or removed,
  4652. // so not often.
  4653. for (List<RID>::Element *E = global_variables.materials_using_buffer.front(); E; E = E->next()) {
  4654. Material *material = material_owner.getornull(E->get());
  4655. ERR_CONTINUE(!material); //wtf
  4656. _material_queue_update(material, true, false);
  4657. }
  4658. global_variables.must_update_buffer_materials = false;
  4659. }
  4660. if (global_variables.must_update_texture_materials) {
  4661. // only happens in the case of a buffer variable added or removed,
  4662. // so not often.
  4663. for (List<RID>::Element *E = global_variables.materials_using_texture.front(); E; E = E->next()) {
  4664. Material *material = material_owner.getornull(E->get());
  4665. ERR_CONTINUE(!material); //wtf
  4666. _material_queue_update(material, false, true);
  4667. print_line("update material texture?");
  4668. }
  4669. global_variables.must_update_texture_materials = false;
  4670. }
  4671. }
  4672. void RasterizerStorageRD::update_dirty_resources() {
  4673. _update_global_variables(); //must do before materials, so it can queue them for update
  4674. _update_queued_materials();
  4675. _update_dirty_multimeshes();
  4676. _update_dirty_skeletons();
  4677. _update_decal_atlas();
  4678. }
  4679. bool RasterizerStorageRD::has_os_feature(const String &p_feature) const {
  4680. if (p_feature == "rgtc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC5_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  4681. return true;
  4682. }
  4683. 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)) {
  4684. return true;
  4685. }
  4686. if (p_feature == "bptc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC7_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  4687. return true;
  4688. }
  4689. 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)) {
  4690. return true;
  4691. }
  4692. 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)) {
  4693. return true;
  4694. }
  4695. return false;
  4696. }
  4697. bool RasterizerStorageRD::free(RID p_rid) {
  4698. if (texture_owner.owns(p_rid)) {
  4699. Texture *t = texture_owner.getornull(p_rid);
  4700. ERR_FAIL_COND_V(t->is_render_target, false);
  4701. if (RD::get_singleton()->texture_is_valid(t->rd_texture_srgb)) {
  4702. //erase this first, as it's a dependency of the one below
  4703. RD::get_singleton()->free(t->rd_texture_srgb);
  4704. }
  4705. if (RD::get_singleton()->texture_is_valid(t->rd_texture)) {
  4706. RD::get_singleton()->free(t->rd_texture);
  4707. }
  4708. if (t->is_proxy && t->proxy_to.is_valid()) {
  4709. Texture *proxy_to = texture_owner.getornull(t->proxy_to);
  4710. if (proxy_to) {
  4711. proxy_to->proxies.erase(p_rid);
  4712. }
  4713. }
  4714. if (decal_atlas.textures.has(p_rid)) {
  4715. decal_atlas.textures.erase(p_rid);
  4716. //there is not much a point of making it dirty, just let it be.
  4717. }
  4718. for (int i = 0; i < t->proxies.size(); i++) {
  4719. Texture *p = texture_owner.getornull(t->proxies[i]);
  4720. ERR_CONTINUE(!p);
  4721. p->proxy_to = RID();
  4722. p->rd_texture = RID();
  4723. p->rd_texture_srgb = RID();
  4724. }
  4725. texture_owner.free(p_rid);
  4726. } else if (shader_owner.owns(p_rid)) {
  4727. Shader *shader = shader_owner.getornull(p_rid);
  4728. //make material unreference this
  4729. while (shader->owners.size()) {
  4730. material_set_shader(shader->owners.front()->get()->self, RID());
  4731. }
  4732. //clear data if exists
  4733. if (shader->data) {
  4734. memdelete(shader->data);
  4735. }
  4736. shader_owner.free(p_rid);
  4737. } else if (material_owner.owns(p_rid)) {
  4738. Material *material = material_owner.getornull(p_rid);
  4739. if (material->update_requested) {
  4740. _update_queued_materials();
  4741. }
  4742. material_set_shader(p_rid, RID()); //clean up shader
  4743. material->instance_dependency.instance_notify_deleted(p_rid);
  4744. material_owner.free(p_rid);
  4745. } else if (mesh_owner.owns(p_rid)) {
  4746. mesh_clear(p_rid);
  4747. Mesh *mesh = mesh_owner.getornull(p_rid);
  4748. mesh->instance_dependency.instance_notify_deleted(p_rid);
  4749. mesh_owner.free(p_rid);
  4750. } else if (multimesh_owner.owns(p_rid)) {
  4751. _update_dirty_multimeshes();
  4752. multimesh_allocate(p_rid, 0, RS::MULTIMESH_TRANSFORM_2D);
  4753. MultiMesh *multimesh = multimesh_owner.getornull(p_rid);
  4754. multimesh->instance_dependency.instance_notify_deleted(p_rid);
  4755. multimesh_owner.free(p_rid);
  4756. } else if (skeleton_owner.owns(p_rid)) {
  4757. _update_dirty_skeletons();
  4758. skeleton_allocate(p_rid, 0);
  4759. Skeleton *skeleton = skeleton_owner.getornull(p_rid);
  4760. skeleton->instance_dependency.instance_notify_deleted(p_rid);
  4761. skeleton_owner.free(p_rid);
  4762. } else if (reflection_probe_owner.owns(p_rid)) {
  4763. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_rid);
  4764. reflection_probe->instance_dependency.instance_notify_deleted(p_rid);
  4765. reflection_probe_owner.free(p_rid);
  4766. } else if (decal_owner.owns(p_rid)) {
  4767. Decal *decal = decal_owner.getornull(p_rid);
  4768. for (int i = 0; i < RS::DECAL_TEXTURE_MAX; i++) {
  4769. if (decal->textures[i].is_valid() && texture_owner.owns(decal->textures[i])) {
  4770. texture_remove_from_decal_atlas(decal->textures[i]);
  4771. }
  4772. }
  4773. decal->instance_dependency.instance_notify_deleted(p_rid);
  4774. decal_owner.free(p_rid);
  4775. } else if (gi_probe_owner.owns(p_rid)) {
  4776. gi_probe_allocate(p_rid, Transform(), AABB(), Vector3i(), Vector<uint8_t>(), Vector<uint8_t>(), Vector<uint8_t>(), Vector<int>()); //deallocate
  4777. GIProbe *gi_probe = gi_probe_owner.getornull(p_rid);
  4778. gi_probe->instance_dependency.instance_notify_deleted(p_rid);
  4779. gi_probe_owner.free(p_rid);
  4780. } else if (lightmap_owner.owns(p_rid)) {
  4781. lightmap_set_textures(p_rid, RID(), false);
  4782. Lightmap *lightmap = lightmap_owner.getornull(p_rid);
  4783. lightmap->instance_dependency.instance_notify_deleted(p_rid);
  4784. lightmap_owner.free(p_rid);
  4785. } else if (light_owner.owns(p_rid)) {
  4786. light_set_projector(p_rid, RID()); //clear projector
  4787. // delete the texture
  4788. Light *light = light_owner.getornull(p_rid);
  4789. light->instance_dependency.instance_notify_deleted(p_rid);
  4790. light_owner.free(p_rid);
  4791. } else if (render_target_owner.owns(p_rid)) {
  4792. RenderTarget *rt = render_target_owner.getornull(p_rid);
  4793. _clear_render_target(rt);
  4794. if (rt->texture.is_valid()) {
  4795. Texture *tex = texture_owner.getornull(rt->texture);
  4796. tex->is_render_target = false;
  4797. free(rt->texture);
  4798. }
  4799. render_target_owner.free(p_rid);
  4800. } else {
  4801. return false;
  4802. }
  4803. return true;
  4804. }
  4805. RasterizerEffectsRD *RasterizerStorageRD::get_effects() {
  4806. return &effects;
  4807. }
  4808. void RasterizerStorageRD::capture_timestamps_begin() {
  4809. RD::get_singleton()->capture_timestamp("Frame Begin", false);
  4810. }
  4811. void RasterizerStorageRD::capture_timestamp(const String &p_name) {
  4812. RD::get_singleton()->capture_timestamp(p_name, true);
  4813. }
  4814. uint32_t RasterizerStorageRD::get_captured_timestamps_count() const {
  4815. return RD::get_singleton()->get_captured_timestamps_count();
  4816. }
  4817. uint64_t RasterizerStorageRD::get_captured_timestamps_frame() const {
  4818. return RD::get_singleton()->get_captured_timestamps_frame();
  4819. }
  4820. uint64_t RasterizerStorageRD::get_captured_timestamp_gpu_time(uint32_t p_index) const {
  4821. return RD::get_singleton()->get_captured_timestamp_gpu_time(p_index);
  4822. }
  4823. uint64_t RasterizerStorageRD::get_captured_timestamp_cpu_time(uint32_t p_index) const {
  4824. return RD::get_singleton()->get_captured_timestamp_cpu_time(p_index);
  4825. }
  4826. String RasterizerStorageRD::get_captured_timestamp_name(uint32_t p_index) const {
  4827. return RD::get_singleton()->get_captured_timestamp_name(p_index);
  4828. }
  4829. RasterizerStorageRD *RasterizerStorageRD::base_singleton = nullptr;
  4830. RasterizerStorageRD::RasterizerStorageRD() {
  4831. base_singleton = this;
  4832. for (int i = 0; i < SHADER_TYPE_MAX; i++) {
  4833. shader_data_request_func[i] = nullptr;
  4834. }
  4835. static_assert(sizeof(GlobalVariables::Value) == 16);
  4836. global_variables.buffer_size = GLOBAL_GET("rendering/high_end/global_shader_variables_buffer_size");
  4837. global_variables.buffer_size = MAX(4096, global_variables.buffer_size);
  4838. global_variables.buffer_values = memnew_arr(GlobalVariables::Value, global_variables.buffer_size);
  4839. zeromem(global_variables.buffer_values, sizeof(GlobalVariables::Value) * global_variables.buffer_size);
  4840. global_variables.buffer_usage = memnew_arr(GlobalVariables::ValueUsage, global_variables.buffer_size);
  4841. global_variables.buffer_dirty_regions = memnew_arr(bool, global_variables.buffer_size / GlobalVariables::BUFFER_DIRTY_REGION_SIZE);
  4842. zeromem(global_variables.buffer_dirty_regions, sizeof(bool) * global_variables.buffer_size / GlobalVariables::BUFFER_DIRTY_REGION_SIZE);
  4843. global_variables.buffer = RD::get_singleton()->storage_buffer_create(sizeof(GlobalVariables::Value) * global_variables.buffer_size);
  4844. material_update_list = nullptr;
  4845. { //create default textures
  4846. RD::TextureFormat tformat;
  4847. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  4848. tformat.width = 4;
  4849. tformat.height = 4;
  4850. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  4851. tformat.type = RD::TEXTURE_TYPE_2D;
  4852. Vector<uint8_t> pv;
  4853. pv.resize(16 * 4);
  4854. for (int i = 0; i < 16; i++) {
  4855. pv.set(i * 4 + 0, 255);
  4856. pv.set(i * 4 + 1, 255);
  4857. pv.set(i * 4 + 2, 255);
  4858. pv.set(i * 4 + 3, 255);
  4859. }
  4860. {
  4861. Vector<Vector<uint8_t>> vpv;
  4862. vpv.push_back(pv);
  4863. default_rd_textures[DEFAULT_RD_TEXTURE_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  4864. }
  4865. for (int i = 0; i < 16; i++) {
  4866. pv.set(i * 4 + 0, 0);
  4867. pv.set(i * 4 + 1, 0);
  4868. pv.set(i * 4 + 2, 0);
  4869. pv.set(i * 4 + 3, 255);
  4870. }
  4871. {
  4872. Vector<Vector<uint8_t>> vpv;
  4873. vpv.push_back(pv);
  4874. default_rd_textures[DEFAULT_RD_TEXTURE_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  4875. //take the chance and initialize decal atlas to something
  4876. decal_atlas.texture = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  4877. decal_atlas.texture_srgb = decal_atlas.texture;
  4878. }
  4879. for (int i = 0; i < 16; i++) {
  4880. pv.set(i * 4 + 0, 128);
  4881. pv.set(i * 4 + 1, 128);
  4882. pv.set(i * 4 + 2, 255);
  4883. pv.set(i * 4 + 3, 255);
  4884. }
  4885. {
  4886. Vector<Vector<uint8_t>> vpv;
  4887. vpv.push_back(pv);
  4888. default_rd_textures[DEFAULT_RD_TEXTURE_NORMAL] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  4889. }
  4890. for (int i = 0; i < 16; i++) {
  4891. pv.set(i * 4 + 0, 255);
  4892. pv.set(i * 4 + 1, 128);
  4893. pv.set(i * 4 + 2, 255);
  4894. pv.set(i * 4 + 3, 255);
  4895. }
  4896. {
  4897. Vector<Vector<uint8_t>> vpv;
  4898. vpv.push_back(pv);
  4899. default_rd_textures[DEFAULT_RD_TEXTURE_ANISO] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  4900. }
  4901. for (int i = 0; i < 16; i++) {
  4902. pv.set(i * 4 + 0, 0);
  4903. pv.set(i * 4 + 1, 0);
  4904. pv.set(i * 4 + 2, 0);
  4905. pv.set(i * 4 + 3, 0);
  4906. }
  4907. default_rd_textures[DEFAULT_RD_TEXTURE_MULTIMESH_BUFFER] = RD::get_singleton()->texture_buffer_create(16, RD::DATA_FORMAT_R8G8B8A8_UNORM, pv);
  4908. }
  4909. { //create default cubemap
  4910. RD::TextureFormat tformat;
  4911. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  4912. tformat.width = 4;
  4913. tformat.height = 4;
  4914. tformat.array_layers = 6;
  4915. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  4916. tformat.type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  4917. Vector<uint8_t> pv;
  4918. pv.resize(16 * 4);
  4919. for (int i = 0; i < 16; i++) {
  4920. pv.set(i * 4 + 0, 0);
  4921. pv.set(i * 4 + 1, 0);
  4922. pv.set(i * 4 + 2, 0);
  4923. pv.set(i * 4 + 3, 0);
  4924. }
  4925. {
  4926. Vector<Vector<uint8_t>> vpv;
  4927. for (int i = 0; i < 6; i++) {
  4928. vpv.push_back(pv);
  4929. }
  4930. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  4931. }
  4932. }
  4933. { //create default cubemap array
  4934. RD::TextureFormat tformat;
  4935. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  4936. tformat.width = 4;
  4937. tformat.height = 4;
  4938. tformat.array_layers = 6;
  4939. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  4940. tformat.type = RD::TEXTURE_TYPE_CUBE;
  4941. Vector<uint8_t> pv;
  4942. pv.resize(16 * 4);
  4943. for (int i = 0; i < 16; i++) {
  4944. pv.set(i * 4 + 0, 0);
  4945. pv.set(i * 4 + 1, 0);
  4946. pv.set(i * 4 + 2, 0);
  4947. pv.set(i * 4 + 3, 0);
  4948. }
  4949. {
  4950. Vector<Vector<uint8_t>> vpv;
  4951. for (int i = 0; i < 6; i++) {
  4952. vpv.push_back(pv);
  4953. }
  4954. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  4955. }
  4956. }
  4957. { //create default 3D
  4958. RD::TextureFormat tformat;
  4959. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  4960. tformat.width = 4;
  4961. tformat.height = 4;
  4962. tformat.depth = 4;
  4963. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  4964. tformat.type = RD::TEXTURE_TYPE_3D;
  4965. Vector<uint8_t> pv;
  4966. pv.resize(64 * 4);
  4967. for (int i = 0; i < 64; i++) {
  4968. pv.set(i * 4 + 0, 0);
  4969. pv.set(i * 4 + 1, 0);
  4970. pv.set(i * 4 + 2, 0);
  4971. pv.set(i * 4 + 3, 0);
  4972. }
  4973. {
  4974. Vector<Vector<uint8_t>> vpv;
  4975. vpv.push_back(pv);
  4976. default_rd_textures[DEFAULT_RD_TEXTURE_3D_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  4977. }
  4978. }
  4979. { //create default array
  4980. RD::TextureFormat tformat;
  4981. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  4982. tformat.width = 4;
  4983. tformat.height = 4;
  4984. tformat.array_layers = 1;
  4985. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  4986. tformat.type = RD::TEXTURE_TYPE_2D_ARRAY;
  4987. Vector<uint8_t> pv;
  4988. pv.resize(16 * 4);
  4989. for (int i = 0; i < 16; i++) {
  4990. pv.set(i * 4 + 0, 255);
  4991. pv.set(i * 4 + 1, 255);
  4992. pv.set(i * 4 + 2, 255);
  4993. pv.set(i * 4 + 3, 255);
  4994. }
  4995. {
  4996. Vector<Vector<uint8_t>> vpv;
  4997. vpv.push_back(pv);
  4998. default_rd_textures[DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  4999. }
  5000. }
  5001. //default samplers
  5002. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  5003. for (int j = 1; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  5004. RD::SamplerState sampler_state;
  5005. switch (i) {
  5006. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST: {
  5007. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  5008. sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST;
  5009. sampler_state.max_lod = 0;
  5010. } break;
  5011. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR: {
  5012. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  5013. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  5014. sampler_state.max_lod = 0;
  5015. } break;
  5016. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS: {
  5017. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  5018. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  5019. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  5020. } break;
  5021. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS: {
  5022. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  5023. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  5024. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  5025. } break;
  5026. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC: {
  5027. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  5028. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  5029. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  5030. sampler_state.use_anisotropy = true;
  5031. sampler_state.anisotropy_max = 1 << int(GLOBAL_GET("rendering/quality/texture_filters/anisotropic_filtering_level"));
  5032. } break;
  5033. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC: {
  5034. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  5035. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  5036. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  5037. sampler_state.use_anisotropy = true;
  5038. sampler_state.anisotropy_max = 1 << int(GLOBAL_GET("rendering/quality/texture_filters/anisotropic_filtering_level"));
  5039. } break;
  5040. default: {
  5041. }
  5042. }
  5043. switch (j) {
  5044. case RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED: {
  5045. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  5046. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  5047. } break;
  5048. case RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED: {
  5049. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_REPEAT;
  5050. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_REPEAT;
  5051. } break;
  5052. case RS::CANVAS_ITEM_TEXTURE_REPEAT_MIRROR: {
  5053. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  5054. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  5055. } break;
  5056. default: {
  5057. }
  5058. }
  5059. default_rd_samplers[i][j] = RD::get_singleton()->sampler_create(sampler_state);
  5060. }
  5061. }
  5062. //default rd buffers
  5063. {
  5064. Vector<uint8_t> buffer;
  5065. {
  5066. buffer.resize(sizeof(float) * 3);
  5067. {
  5068. uint8_t *w = buffer.ptrw();
  5069. float *fptr = (float *)w;
  5070. fptr[0] = 0.0;
  5071. fptr[1] = 0.0;
  5072. fptr[2] = 0.0;
  5073. }
  5074. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_VERTEX] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  5075. }
  5076. { //normal
  5077. buffer.resize(sizeof(float) * 3);
  5078. {
  5079. uint8_t *w = buffer.ptrw();
  5080. float *fptr = (float *)w;
  5081. fptr[0] = 1.0;
  5082. fptr[1] = 0.0;
  5083. fptr[2] = 0.0;
  5084. }
  5085. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_NORMAL] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  5086. }
  5087. { //tangent
  5088. buffer.resize(sizeof(float) * 4);
  5089. {
  5090. uint8_t *w = buffer.ptrw();
  5091. float *fptr = (float *)w;
  5092. fptr[0] = 1.0;
  5093. fptr[1] = 0.0;
  5094. fptr[2] = 0.0;
  5095. fptr[3] = 0.0;
  5096. }
  5097. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_TANGENT] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  5098. }
  5099. { //color
  5100. buffer.resize(sizeof(float) * 4);
  5101. {
  5102. uint8_t *w = buffer.ptrw();
  5103. float *fptr = (float *)w;
  5104. fptr[0] = 1.0;
  5105. fptr[1] = 1.0;
  5106. fptr[2] = 1.0;
  5107. fptr[3] = 1.0;
  5108. }
  5109. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_COLOR] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  5110. }
  5111. { //tex uv 1
  5112. buffer.resize(sizeof(float) * 2);
  5113. {
  5114. uint8_t *w = buffer.ptrw();
  5115. float *fptr = (float *)w;
  5116. fptr[0] = 0.0;
  5117. fptr[1] = 0.0;
  5118. }
  5119. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_TEX_UV] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  5120. }
  5121. { //tex uv 2
  5122. buffer.resize(sizeof(float) * 2);
  5123. {
  5124. uint8_t *w = buffer.ptrw();
  5125. float *fptr = (float *)w;
  5126. fptr[0] = 0.0;
  5127. fptr[1] = 0.0;
  5128. }
  5129. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_TEX_UV2] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  5130. }
  5131. { //bones
  5132. buffer.resize(sizeof(uint32_t) * 4);
  5133. {
  5134. uint8_t *w = buffer.ptrw();
  5135. uint32_t *fptr = (uint32_t *)w;
  5136. fptr[0] = 0;
  5137. fptr[1] = 0;
  5138. fptr[2] = 0;
  5139. fptr[3] = 0;
  5140. }
  5141. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_BONES] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  5142. }
  5143. { //weights
  5144. buffer.resize(sizeof(float) * 4);
  5145. {
  5146. uint8_t *w = buffer.ptrw();
  5147. float *fptr = (float *)w;
  5148. fptr[0] = 0.0;
  5149. fptr[1] = 0.0;
  5150. fptr[2] = 0.0;
  5151. fptr[3] = 0.0;
  5152. }
  5153. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_WEIGHTS] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  5154. }
  5155. }
  5156. {
  5157. Vector<String> sdf_versions;
  5158. sdf_versions.push_back(""); //one only
  5159. giprobe_sdf_shader.initialize(sdf_versions);
  5160. giprobe_sdf_shader_version = giprobe_sdf_shader.version_create();
  5161. giprobe_sdf_shader.version_set_compute_code(giprobe_sdf_shader_version, "", "", "", Vector<String>());
  5162. giprobe_sdf_shader_version_shader = giprobe_sdf_shader.version_get_shader(giprobe_sdf_shader_version, 0);
  5163. giprobe_sdf_shader_pipeline = RD::get_singleton()->compute_pipeline_create(giprobe_sdf_shader_version_shader);
  5164. }
  5165. using_lightmap_array = true; // high end
  5166. if (using_lightmap_array) {
  5167. uint32_t textures_per_stage = RD::get_singleton()->limit_get(RD::LIMIT_MAX_TEXTURES_PER_SHADER_STAGE);
  5168. if (textures_per_stage <= 256) {
  5169. lightmap_textures.resize(32);
  5170. } else {
  5171. lightmap_textures.resize(1024);
  5172. }
  5173. for (int i = 0; i < lightmap_textures.size(); i++) {
  5174. lightmap_textures.write[i] = default_rd_textures[DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE];
  5175. }
  5176. }
  5177. lightmap_probe_capture_update_speed = GLOBAL_GET("rendering/lightmapper/probe_capture_update_speed");
  5178. }
  5179. RasterizerStorageRD::~RasterizerStorageRD() {
  5180. memdelete_arr(global_variables.buffer_values);
  5181. memdelete_arr(global_variables.buffer_usage);
  5182. memdelete_arr(global_variables.buffer_dirty_regions);
  5183. RD::get_singleton()->free(global_variables.buffer);
  5184. //def textures
  5185. for (int i = 0; i < DEFAULT_RD_TEXTURE_MAX; i++) {
  5186. RD::get_singleton()->free(default_rd_textures[i]);
  5187. }
  5188. //def samplers
  5189. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  5190. for (int j = 1; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  5191. RD::get_singleton()->free(default_rd_samplers[i][j]);
  5192. }
  5193. }
  5194. //def buffers
  5195. for (int i = 0; i < DEFAULT_RD_BUFFER_MAX; i++) {
  5196. RD::get_singleton()->free(mesh_default_rd_buffers[i]);
  5197. }
  5198. giprobe_sdf_shader.version_free(giprobe_sdf_shader_version);
  5199. if (decal_atlas.textures.size()) {
  5200. ERR_PRINT("Decal Atlas: " + itos(decal_atlas.textures.size()) + " textures were not removed from the atlas.");
  5201. }
  5202. if (decal_atlas.texture.is_valid()) {
  5203. RD::get_singleton()->free(decal_atlas.texture);
  5204. }
  5205. }