renderer_scene_render_rd.cpp 327 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523
  1. /*************************************************************************/
  2. /* renderer_scene_render_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "renderer_scene_render_rd.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/os/os.h"
  33. #include "renderer_compositor_rd.h"
  34. #include "servers/rendering/rendering_server_default.h"
  35. uint64_t RendererSceneRenderRD::auto_exposure_counter = 2;
  36. void get_vogel_disk(float *r_kernel, int p_sample_count) {
  37. const float golden_angle = 2.4;
  38. for (int i = 0; i < p_sample_count; i++) {
  39. float r = Math::sqrt(float(i) + 0.5) / Math::sqrt(float(p_sample_count));
  40. float theta = float(i) * golden_angle;
  41. r_kernel[i * 4] = Math::cos(theta) * r;
  42. r_kernel[i * 4 + 1] = Math::sin(theta) * r;
  43. }
  44. }
  45. void RendererSceneRenderRD::_clear_reflection_data(ReflectionData &rd) {
  46. rd.layers.clear();
  47. rd.radiance_base_cubemap = RID();
  48. if (rd.downsampled_radiance_cubemap.is_valid()) {
  49. RD::get_singleton()->free(rd.downsampled_radiance_cubemap);
  50. }
  51. rd.downsampled_radiance_cubemap = RID();
  52. rd.downsampled_layer.mipmaps.clear();
  53. rd.coefficient_buffer = RID();
  54. }
  55. void RendererSceneRenderRD::_update_reflection_data(ReflectionData &rd, int p_size, int p_mipmaps, bool p_use_array, RID p_base_cube, int p_base_layer, bool p_low_quality) {
  56. //recreate radiance and all data
  57. int mipmaps = p_mipmaps;
  58. uint32_t w = p_size, h = p_size;
  59. if (p_use_array) {
  60. int layers = p_low_quality ? 8 : roughness_layers;
  61. for (int i = 0; i < layers; i++) {
  62. ReflectionData::Layer layer;
  63. uint32_t mmw = w;
  64. uint32_t mmh = h;
  65. layer.mipmaps.resize(mipmaps);
  66. layer.views.resize(mipmaps);
  67. for (int j = 0; j < mipmaps; j++) {
  68. ReflectionData::Layer::Mipmap &mm = layer.mipmaps.write[j];
  69. mm.size.width = mmw;
  70. mm.size.height = mmh;
  71. for (int k = 0; k < 6; k++) {
  72. mm.views[k] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer + i * 6 + k, j);
  73. Vector<RID> fbtex;
  74. fbtex.push_back(mm.views[k]);
  75. mm.framebuffers[k] = RD::get_singleton()->framebuffer_create(fbtex);
  76. }
  77. layer.views.write[j] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer + i * 6, j, RD::TEXTURE_SLICE_CUBEMAP);
  78. mmw = MAX(1, mmw >> 1);
  79. mmh = MAX(1, mmh >> 1);
  80. }
  81. rd.layers.push_back(layer);
  82. }
  83. } else {
  84. mipmaps = p_low_quality ? 8 : mipmaps;
  85. //regular cubemap, lower quality (aliasing, less memory)
  86. ReflectionData::Layer layer;
  87. uint32_t mmw = w;
  88. uint32_t mmh = h;
  89. layer.mipmaps.resize(mipmaps);
  90. layer.views.resize(mipmaps);
  91. for (int j = 0; j < mipmaps; j++) {
  92. ReflectionData::Layer::Mipmap &mm = layer.mipmaps.write[j];
  93. mm.size.width = mmw;
  94. mm.size.height = mmh;
  95. for (int k = 0; k < 6; k++) {
  96. mm.views[k] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer + k, j);
  97. Vector<RID> fbtex;
  98. fbtex.push_back(mm.views[k]);
  99. mm.framebuffers[k] = RD::get_singleton()->framebuffer_create(fbtex);
  100. }
  101. layer.views.write[j] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer, j, RD::TEXTURE_SLICE_CUBEMAP);
  102. mmw = MAX(1, mmw >> 1);
  103. mmh = MAX(1, mmh >> 1);
  104. }
  105. rd.layers.push_back(layer);
  106. }
  107. rd.radiance_base_cubemap = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), p_base_cube, p_base_layer, 0, RD::TEXTURE_SLICE_CUBEMAP);
  108. RD::TextureFormat tf;
  109. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  110. tf.width = 64; // Always 64x64
  111. tf.height = 64;
  112. tf.texture_type = RD::TEXTURE_TYPE_CUBE;
  113. tf.array_layers = 6;
  114. tf.mipmaps = 7;
  115. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  116. rd.downsampled_radiance_cubemap = RD::get_singleton()->texture_create(tf, RD::TextureView());
  117. {
  118. uint32_t mmw = 64;
  119. uint32_t mmh = 64;
  120. rd.downsampled_layer.mipmaps.resize(7);
  121. for (int j = 0; j < rd.downsampled_layer.mipmaps.size(); j++) {
  122. ReflectionData::DownsampleLayer::Mipmap &mm = rd.downsampled_layer.mipmaps.write[j];
  123. mm.size.width = mmw;
  124. mm.size.height = mmh;
  125. mm.view = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rd.downsampled_radiance_cubemap, 0, j, RD::TEXTURE_SLICE_CUBEMAP);
  126. mmw = MAX(1, mmw >> 1);
  127. mmh = MAX(1, mmh >> 1);
  128. }
  129. }
  130. }
  131. void RendererSceneRenderRD::_create_reflection_fast_filter(ReflectionData &rd, bool p_use_arrays) {
  132. storage->get_effects()->cubemap_downsample(rd.radiance_base_cubemap, rd.downsampled_layer.mipmaps[0].view, rd.downsampled_layer.mipmaps[0].size);
  133. for (int i = 1; i < rd.downsampled_layer.mipmaps.size(); i++) {
  134. storage->get_effects()->cubemap_downsample(rd.downsampled_layer.mipmaps[i - 1].view, rd.downsampled_layer.mipmaps[i].view, rd.downsampled_layer.mipmaps[i].size);
  135. }
  136. Vector<RID> views;
  137. if (p_use_arrays) {
  138. for (int i = 1; i < rd.layers.size(); i++) {
  139. views.push_back(rd.layers[i].views[0]);
  140. }
  141. } else {
  142. for (int i = 1; i < rd.layers[0].views.size(); i++) {
  143. views.push_back(rd.layers[0].views[i]);
  144. }
  145. }
  146. storage->get_effects()->cubemap_filter(rd.downsampled_radiance_cubemap, views, p_use_arrays);
  147. }
  148. void RendererSceneRenderRD::_create_reflection_importance_sample(ReflectionData &rd, bool p_use_arrays, int p_cube_side, int p_base_layer) {
  149. if (p_use_arrays) {
  150. //render directly to the layers
  151. storage->get_effects()->cubemap_roughness(rd.radiance_base_cubemap, rd.layers[p_base_layer].views[0], p_cube_side, sky_ggx_samples_quality, float(p_base_layer) / (rd.layers.size() - 1.0), rd.layers[p_base_layer].mipmaps[0].size.x);
  152. } else {
  153. storage->get_effects()->cubemap_roughness(rd.layers[0].views[p_base_layer - 1], rd.layers[0].views[p_base_layer], p_cube_side, sky_ggx_samples_quality, float(p_base_layer) / (rd.layers[0].mipmaps.size() - 1.0), rd.layers[0].mipmaps[p_base_layer].size.x);
  154. }
  155. }
  156. void RendererSceneRenderRD::_update_reflection_mipmaps(ReflectionData &rd, int p_start, int p_end) {
  157. for (int i = p_start; i < p_end; i++) {
  158. for (int j = 0; j < rd.layers[i].mipmaps.size() - 1; j++) {
  159. for (int k = 0; k < 6; k++) {
  160. RID view = rd.layers[i].mipmaps[j].views[k];
  161. RID texture = rd.layers[i].mipmaps[j + 1].views[k];
  162. Size2i size = rd.layers[i].mipmaps[j + 1].size;
  163. storage->get_effects()->make_mipmap(view, texture, size);
  164. }
  165. }
  166. }
  167. }
  168. void RendererSceneRenderRD::_sdfgi_erase(RenderBuffers *rb) {
  169. for (uint32_t i = 0; i < rb->sdfgi->cascades.size(); i++) {
  170. const SDFGI::Cascade &c = rb->sdfgi->cascades[i];
  171. RD::get_singleton()->free(c.light_data);
  172. RD::get_singleton()->free(c.light_aniso_0_tex);
  173. RD::get_singleton()->free(c.light_aniso_1_tex);
  174. RD::get_singleton()->free(c.sdf_tex);
  175. RD::get_singleton()->free(c.solid_cell_dispatch_buffer);
  176. RD::get_singleton()->free(c.solid_cell_buffer);
  177. RD::get_singleton()->free(c.lightprobe_history_tex);
  178. RD::get_singleton()->free(c.lightprobe_average_tex);
  179. RD::get_singleton()->free(c.lights_buffer);
  180. }
  181. RD::get_singleton()->free(rb->sdfgi->render_albedo);
  182. RD::get_singleton()->free(rb->sdfgi->render_emission);
  183. RD::get_singleton()->free(rb->sdfgi->render_emission_aniso);
  184. RD::get_singleton()->free(rb->sdfgi->render_sdf[0]);
  185. RD::get_singleton()->free(rb->sdfgi->render_sdf[1]);
  186. RD::get_singleton()->free(rb->sdfgi->render_sdf_half[0]);
  187. RD::get_singleton()->free(rb->sdfgi->render_sdf_half[1]);
  188. for (int i = 0; i < 8; i++) {
  189. RD::get_singleton()->free(rb->sdfgi->render_occlusion[i]);
  190. }
  191. RD::get_singleton()->free(rb->sdfgi->render_geom_facing);
  192. RD::get_singleton()->free(rb->sdfgi->lightprobe_data);
  193. RD::get_singleton()->free(rb->sdfgi->lightprobe_history_scroll);
  194. RD::get_singleton()->free(rb->sdfgi->occlusion_data);
  195. RD::get_singleton()->free(rb->sdfgi->ambient_texture);
  196. RD::get_singleton()->free(rb->sdfgi->cascades_ubo);
  197. memdelete(rb->sdfgi);
  198. rb->sdfgi = nullptr;
  199. }
  200. const Vector3i RendererSceneRenderRD::SDFGI::Cascade::DIRTY_ALL = Vector3i(0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF);
  201. void RendererSceneRenderRD::sdfgi_update(RID p_render_buffers, RID p_environment, const Vector3 &p_world_position) {
  202. Environment *env = environment_owner.getornull(p_environment);
  203. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  204. bool needs_sdfgi = env && env->sdfgi_enabled;
  205. if (!needs_sdfgi) {
  206. if (rb->sdfgi != nullptr) {
  207. //erase it
  208. _sdfgi_erase(rb);
  209. _render_buffers_uniform_set_changed(p_render_buffers);
  210. }
  211. return;
  212. }
  213. static const uint32_t history_frames_to_converge[RS::ENV_SDFGI_CONVERGE_MAX] = { 5, 10, 15, 20, 25, 30 };
  214. uint32_t requested_history_size = history_frames_to_converge[sdfgi_frames_to_converge];
  215. if (rb->sdfgi && (rb->sdfgi->cascade_mode != env->sdfgi_cascades || rb->sdfgi->min_cell_size != env->sdfgi_min_cell_size || requested_history_size != rb->sdfgi->history_size || rb->sdfgi->uses_occlusion != env->sdfgi_use_occlusion || rb->sdfgi->y_scale_mode != env->sdfgi_y_scale)) {
  216. //configuration changed, erase
  217. _sdfgi_erase(rb);
  218. }
  219. SDFGI *sdfgi = rb->sdfgi;
  220. if (sdfgi == nullptr) {
  221. //re-create
  222. rb->sdfgi = memnew(SDFGI);
  223. sdfgi = rb->sdfgi;
  224. sdfgi->cascade_mode = env->sdfgi_cascades;
  225. sdfgi->min_cell_size = env->sdfgi_min_cell_size;
  226. sdfgi->uses_occlusion = env->sdfgi_use_occlusion;
  227. sdfgi->y_scale_mode = env->sdfgi_y_scale;
  228. static const float y_scale[3] = { 1.0, 1.5, 2.0 };
  229. sdfgi->y_mult = y_scale[sdfgi->y_scale_mode];
  230. static const int cascasde_size[3] = { 4, 6, 8 };
  231. sdfgi->cascades.resize(cascasde_size[sdfgi->cascade_mode]);
  232. sdfgi->probe_axis_count = SDFGI::PROBE_DIVISOR + 1;
  233. sdfgi->solid_cell_ratio = sdfgi_solid_cell_ratio;
  234. sdfgi->solid_cell_count = uint32_t(float(sdfgi->cascade_size * sdfgi->cascade_size * sdfgi->cascade_size) * sdfgi->solid_cell_ratio);
  235. float base_cell_size = sdfgi->min_cell_size;
  236. RD::TextureFormat tf_sdf;
  237. tf_sdf.format = RD::DATA_FORMAT_R8_UNORM;
  238. tf_sdf.width = sdfgi->cascade_size; // Always 64x64
  239. tf_sdf.height = sdfgi->cascade_size;
  240. tf_sdf.depth = sdfgi->cascade_size;
  241. tf_sdf.texture_type = RD::TEXTURE_TYPE_3D;
  242. tf_sdf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  243. {
  244. RD::TextureFormat tf_render = tf_sdf;
  245. tf_render.format = RD::DATA_FORMAT_R16_UINT;
  246. sdfgi->render_albedo = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  247. tf_render.format = RD::DATA_FORMAT_R32_UINT;
  248. sdfgi->render_emission = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  249. sdfgi->render_emission_aniso = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  250. tf_render.format = RD::DATA_FORMAT_R8_UNORM; //at least its easy to visualize
  251. for (int i = 0; i < 8; i++) {
  252. sdfgi->render_occlusion[i] = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  253. }
  254. tf_render.format = RD::DATA_FORMAT_R32_UINT;
  255. sdfgi->render_geom_facing = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  256. tf_render.format = RD::DATA_FORMAT_R8G8B8A8_UINT;
  257. sdfgi->render_sdf[0] = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  258. sdfgi->render_sdf[1] = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  259. tf_render.width /= 2;
  260. tf_render.height /= 2;
  261. tf_render.depth /= 2;
  262. sdfgi->render_sdf_half[0] = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  263. sdfgi->render_sdf_half[1] = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  264. }
  265. RD::TextureFormat tf_occlusion = tf_sdf;
  266. tf_occlusion.format = RD::DATA_FORMAT_R16_UINT;
  267. tf_occlusion.shareable_formats.push_back(RD::DATA_FORMAT_R16_UINT);
  268. tf_occlusion.shareable_formats.push_back(RD::DATA_FORMAT_R4G4B4A4_UNORM_PACK16);
  269. tf_occlusion.depth *= sdfgi->cascades.size(); //use depth for occlusion slices
  270. tf_occlusion.width *= 2; //use width for the other half
  271. RD::TextureFormat tf_light = tf_sdf;
  272. tf_light.format = RD::DATA_FORMAT_R32_UINT;
  273. tf_light.shareable_formats.push_back(RD::DATA_FORMAT_R32_UINT);
  274. tf_light.shareable_formats.push_back(RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32);
  275. RD::TextureFormat tf_aniso0 = tf_sdf;
  276. tf_aniso0.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  277. RD::TextureFormat tf_aniso1 = tf_sdf;
  278. tf_aniso1.format = RD::DATA_FORMAT_R8G8_UNORM;
  279. int passes = nearest_shift(sdfgi->cascade_size) - 1;
  280. //store lightprobe SH
  281. RD::TextureFormat tf_probes;
  282. tf_probes.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  283. tf_probes.width = sdfgi->probe_axis_count * sdfgi->probe_axis_count;
  284. tf_probes.height = sdfgi->probe_axis_count * SDFGI::SH_SIZE;
  285. tf_probes.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  286. tf_probes.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  287. sdfgi->history_size = requested_history_size;
  288. RD::TextureFormat tf_probe_history = tf_probes;
  289. tf_probe_history.format = RD::DATA_FORMAT_R16G16B16A16_SINT; //signed integer because SH are signed
  290. tf_probe_history.array_layers = sdfgi->history_size;
  291. RD::TextureFormat tf_probe_average = tf_probes;
  292. tf_probe_average.format = RD::DATA_FORMAT_R32G32B32A32_SINT; //signed integer because SH are signed
  293. tf_probe_average.texture_type = RD::TEXTURE_TYPE_2D;
  294. sdfgi->lightprobe_history_scroll = RD::get_singleton()->texture_create(tf_probe_history, RD::TextureView());
  295. sdfgi->lightprobe_average_scroll = RD::get_singleton()->texture_create(tf_probe_average, RD::TextureView());
  296. {
  297. //octahedral lightprobes
  298. RD::TextureFormat tf_octprobes = tf_probes;
  299. tf_octprobes.array_layers = sdfgi->cascades.size() * 2;
  300. tf_octprobes.format = RD::DATA_FORMAT_R32_UINT; //pack well with RGBE
  301. tf_octprobes.width = sdfgi->probe_axis_count * sdfgi->probe_axis_count * (SDFGI::LIGHTPROBE_OCT_SIZE + 2);
  302. tf_octprobes.height = sdfgi->probe_axis_count * (SDFGI::LIGHTPROBE_OCT_SIZE + 2);
  303. tf_octprobes.shareable_formats.push_back(RD::DATA_FORMAT_R32_UINT);
  304. tf_octprobes.shareable_formats.push_back(RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32);
  305. //lightprobe texture is an octahedral texture
  306. sdfgi->lightprobe_data = RD::get_singleton()->texture_create(tf_octprobes, RD::TextureView());
  307. RD::TextureView tv;
  308. tv.format_override = RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32;
  309. sdfgi->lightprobe_texture = RD::get_singleton()->texture_create_shared(tv, sdfgi->lightprobe_data);
  310. //texture handling ambient data, to integrate with volumetric foc
  311. RD::TextureFormat tf_ambient = tf_probes;
  312. tf_ambient.array_layers = sdfgi->cascades.size();
  313. tf_ambient.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT; //pack well with RGBE
  314. tf_ambient.width = sdfgi->probe_axis_count * sdfgi->probe_axis_count;
  315. tf_ambient.height = sdfgi->probe_axis_count;
  316. tf_ambient.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  317. //lightprobe texture is an octahedral texture
  318. sdfgi->ambient_texture = RD::get_singleton()->texture_create(tf_ambient, RD::TextureView());
  319. }
  320. sdfgi->cascades_ubo = RD::get_singleton()->uniform_buffer_create(sizeof(SDFGI::Cascade::UBO) * SDFGI::MAX_CASCADES);
  321. sdfgi->occlusion_data = RD::get_singleton()->texture_create(tf_occlusion, RD::TextureView());
  322. {
  323. RD::TextureView tv;
  324. tv.format_override = RD::DATA_FORMAT_R4G4B4A4_UNORM_PACK16;
  325. sdfgi->occlusion_texture = RD::get_singleton()->texture_create_shared(tv, sdfgi->occlusion_data);
  326. }
  327. for (uint32_t i = 0; i < sdfgi->cascades.size(); i++) {
  328. SDFGI::Cascade &cascade = sdfgi->cascades[i];
  329. /* 3D Textures */
  330. cascade.sdf_tex = RD::get_singleton()->texture_create(tf_sdf, RD::TextureView());
  331. cascade.light_data = RD::get_singleton()->texture_create(tf_light, RD::TextureView());
  332. cascade.light_aniso_0_tex = RD::get_singleton()->texture_create(tf_aniso0, RD::TextureView());
  333. cascade.light_aniso_1_tex = RD::get_singleton()->texture_create(tf_aniso1, RD::TextureView());
  334. {
  335. RD::TextureView tv;
  336. tv.format_override = RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32;
  337. cascade.light_tex = RD::get_singleton()->texture_create_shared(tv, cascade.light_data);
  338. RD::get_singleton()->texture_clear(cascade.light_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
  339. RD::get_singleton()->texture_clear(cascade.light_aniso_0_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
  340. RD::get_singleton()->texture_clear(cascade.light_aniso_1_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
  341. }
  342. cascade.cell_size = base_cell_size;
  343. Vector3 world_position = p_world_position;
  344. world_position.y *= sdfgi->y_mult;
  345. int32_t probe_cells = sdfgi->cascade_size / SDFGI::PROBE_DIVISOR;
  346. Vector3 probe_size = Vector3(1, 1, 1) * cascade.cell_size * probe_cells;
  347. Vector3i probe_pos = Vector3i((world_position / probe_size + Vector3(0.5, 0.5, 0.5)).floor());
  348. cascade.position = probe_pos * probe_cells;
  349. cascade.dirty_regions = SDFGI::Cascade::DIRTY_ALL;
  350. base_cell_size *= 2.0;
  351. /* Probe History */
  352. cascade.lightprobe_history_tex = RD::get_singleton()->texture_create(tf_probe_history, RD::TextureView());
  353. RD::get_singleton()->texture_clear(cascade.lightprobe_history_tex, Color(0, 0, 0, 0), 0, 1, 0, tf_probe_history.array_layers); //needs to be cleared for average to work
  354. cascade.lightprobe_average_tex = RD::get_singleton()->texture_create(tf_probe_average, RD::TextureView());
  355. RD::get_singleton()->texture_clear(cascade.lightprobe_average_tex, Color(0, 0, 0, 0), 0, 1, 0, 1); //needs to be cleared for average to work
  356. /* Buffers */
  357. cascade.solid_cell_buffer = RD::get_singleton()->storage_buffer_create(sizeof(SDFGI::Cascade::SolidCell) * sdfgi->solid_cell_count);
  358. cascade.solid_cell_dispatch_buffer = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t) * 4, Vector<uint8_t>(), RD::STORAGE_BUFFER_USAGE_DISPATCH_INDIRECT);
  359. cascade.lights_buffer = RD::get_singleton()->storage_buffer_create(sizeof(SDGIShader::Light) * MAX(SDFGI::MAX_STATIC_LIGHTS, SDFGI::MAX_DYNAMIC_LIGHTS));
  360. {
  361. Vector<RD::Uniform> uniforms;
  362. {
  363. RD::Uniform u;
  364. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  365. u.binding = 1;
  366. u.ids.push_back(sdfgi->render_sdf[(passes & 1) ? 1 : 0]); //if passes are even, we read from buffer 0, else we read from buffer 1
  367. uniforms.push_back(u);
  368. }
  369. {
  370. RD::Uniform u;
  371. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  372. u.binding = 2;
  373. u.ids.push_back(sdfgi->render_albedo);
  374. uniforms.push_back(u);
  375. }
  376. {
  377. RD::Uniform u;
  378. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  379. u.binding = 3;
  380. for (int j = 0; j < 8; j++) {
  381. u.ids.push_back(sdfgi->render_occlusion[j]);
  382. }
  383. uniforms.push_back(u);
  384. }
  385. {
  386. RD::Uniform u;
  387. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  388. u.binding = 4;
  389. u.ids.push_back(sdfgi->render_emission);
  390. uniforms.push_back(u);
  391. }
  392. {
  393. RD::Uniform u;
  394. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  395. u.binding = 5;
  396. u.ids.push_back(sdfgi->render_emission_aniso);
  397. uniforms.push_back(u);
  398. }
  399. {
  400. RD::Uniform u;
  401. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  402. u.binding = 6;
  403. u.ids.push_back(sdfgi->render_geom_facing);
  404. uniforms.push_back(u);
  405. }
  406. {
  407. RD::Uniform u;
  408. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  409. u.binding = 7;
  410. u.ids.push_back(cascade.sdf_tex);
  411. uniforms.push_back(u);
  412. }
  413. {
  414. RD::Uniform u;
  415. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  416. u.binding = 8;
  417. u.ids.push_back(sdfgi->occlusion_data);
  418. uniforms.push_back(u);
  419. }
  420. {
  421. RD::Uniform u;
  422. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  423. u.binding = 10;
  424. u.ids.push_back(cascade.solid_cell_dispatch_buffer);
  425. uniforms.push_back(u);
  426. }
  427. {
  428. RD::Uniform u;
  429. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  430. u.binding = 11;
  431. u.ids.push_back(cascade.solid_cell_buffer);
  432. uniforms.push_back(u);
  433. }
  434. cascade.sdf_store_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.preprocess.version_get_shader(sdfgi_shader.preprocess_shader, SDGIShader::PRE_PROCESS_STORE), 0);
  435. }
  436. {
  437. Vector<RD::Uniform> uniforms;
  438. {
  439. RD::Uniform u;
  440. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  441. u.binding = 1;
  442. u.ids.push_back(sdfgi->render_albedo);
  443. uniforms.push_back(u);
  444. }
  445. {
  446. RD::Uniform u;
  447. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  448. u.binding = 2;
  449. u.ids.push_back(sdfgi->render_geom_facing);
  450. uniforms.push_back(u);
  451. }
  452. {
  453. RD::Uniform u;
  454. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  455. u.binding = 3;
  456. u.ids.push_back(sdfgi->render_emission);
  457. uniforms.push_back(u);
  458. }
  459. {
  460. RD::Uniform u;
  461. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  462. u.binding = 4;
  463. u.ids.push_back(sdfgi->render_emission_aniso);
  464. uniforms.push_back(u);
  465. }
  466. {
  467. RD::Uniform u;
  468. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  469. u.binding = 5;
  470. u.ids.push_back(cascade.solid_cell_dispatch_buffer);
  471. uniforms.push_back(u);
  472. }
  473. {
  474. RD::Uniform u;
  475. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  476. u.binding = 6;
  477. u.ids.push_back(cascade.solid_cell_buffer);
  478. uniforms.push_back(u);
  479. }
  480. cascade.scroll_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.preprocess.version_get_shader(sdfgi_shader.preprocess_shader, SDGIShader::PRE_PROCESS_SCROLL), 0);
  481. }
  482. {
  483. Vector<RD::Uniform> uniforms;
  484. {
  485. RD::Uniform u;
  486. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  487. u.binding = 1;
  488. for (int j = 0; j < 8; j++) {
  489. u.ids.push_back(sdfgi->render_occlusion[j]);
  490. }
  491. uniforms.push_back(u);
  492. }
  493. {
  494. RD::Uniform u;
  495. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  496. u.binding = 2;
  497. u.ids.push_back(sdfgi->occlusion_data);
  498. uniforms.push_back(u);
  499. }
  500. cascade.scroll_occlusion_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.preprocess.version_get_shader(sdfgi_shader.preprocess_shader, SDGIShader::PRE_PROCESS_SCROLL_OCCLUSION), 0);
  501. }
  502. }
  503. //direct light
  504. for (uint32_t i = 0; i < sdfgi->cascades.size(); i++) {
  505. SDFGI::Cascade &cascade = sdfgi->cascades[i];
  506. Vector<RD::Uniform> uniforms;
  507. {
  508. RD::Uniform u;
  509. u.binding = 1;
  510. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  511. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  512. if (j < rb->sdfgi->cascades.size()) {
  513. u.ids.push_back(rb->sdfgi->cascades[j].sdf_tex);
  514. } else {
  515. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  516. }
  517. }
  518. uniforms.push_back(u);
  519. }
  520. {
  521. RD::Uniform u;
  522. u.binding = 2;
  523. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  524. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  525. uniforms.push_back(u);
  526. }
  527. {
  528. RD::Uniform u;
  529. u.binding = 3;
  530. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  531. u.ids.push_back(cascade.solid_cell_dispatch_buffer);
  532. uniforms.push_back(u);
  533. }
  534. {
  535. RD::Uniform u;
  536. u.binding = 4;
  537. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  538. u.ids.push_back(cascade.solid_cell_buffer);
  539. uniforms.push_back(u);
  540. }
  541. {
  542. RD::Uniform u;
  543. u.binding = 5;
  544. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  545. u.ids.push_back(cascade.light_data);
  546. uniforms.push_back(u);
  547. }
  548. {
  549. RD::Uniform u;
  550. u.binding = 6;
  551. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  552. u.ids.push_back(cascade.light_aniso_0_tex);
  553. uniforms.push_back(u);
  554. }
  555. {
  556. RD::Uniform u;
  557. u.binding = 7;
  558. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  559. u.ids.push_back(cascade.light_aniso_1_tex);
  560. uniforms.push_back(u);
  561. }
  562. {
  563. RD::Uniform u;
  564. u.binding = 8;
  565. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  566. u.ids.push_back(rb->sdfgi->cascades_ubo);
  567. uniforms.push_back(u);
  568. }
  569. {
  570. RD::Uniform u;
  571. u.binding = 9;
  572. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  573. u.ids.push_back(cascade.lights_buffer);
  574. uniforms.push_back(u);
  575. }
  576. {
  577. RD::Uniform u;
  578. u.binding = 10;
  579. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  580. u.ids.push_back(rb->sdfgi->lightprobe_texture);
  581. uniforms.push_back(u);
  582. }
  583. cascade.sdf_direct_light_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.direct_light.version_get_shader(sdfgi_shader.direct_light_shader, 0), 0);
  584. }
  585. //preprocess initialize uniform set
  586. {
  587. Vector<RD::Uniform> uniforms;
  588. {
  589. RD::Uniform u;
  590. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  591. u.binding = 1;
  592. u.ids.push_back(sdfgi->render_albedo);
  593. uniforms.push_back(u);
  594. }
  595. {
  596. RD::Uniform u;
  597. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  598. u.binding = 2;
  599. u.ids.push_back(sdfgi->render_sdf[0]);
  600. uniforms.push_back(u);
  601. }
  602. sdfgi->sdf_initialize_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.preprocess.version_get_shader(sdfgi_shader.preprocess_shader, SDGIShader::PRE_PROCESS_JUMP_FLOOD_INITIALIZE), 0);
  603. }
  604. {
  605. Vector<RD::Uniform> uniforms;
  606. {
  607. RD::Uniform u;
  608. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  609. u.binding = 1;
  610. u.ids.push_back(sdfgi->render_albedo);
  611. uniforms.push_back(u);
  612. }
  613. {
  614. RD::Uniform u;
  615. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  616. u.binding = 2;
  617. u.ids.push_back(sdfgi->render_sdf_half[0]);
  618. uniforms.push_back(u);
  619. }
  620. sdfgi->sdf_initialize_half_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.preprocess.version_get_shader(sdfgi_shader.preprocess_shader, SDGIShader::PRE_PROCESS_JUMP_FLOOD_INITIALIZE_HALF), 0);
  621. }
  622. //jump flood uniform set
  623. {
  624. Vector<RD::Uniform> uniforms;
  625. {
  626. RD::Uniform u;
  627. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  628. u.binding = 1;
  629. u.ids.push_back(sdfgi->render_sdf[0]);
  630. uniforms.push_back(u);
  631. }
  632. {
  633. RD::Uniform u;
  634. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  635. u.binding = 2;
  636. u.ids.push_back(sdfgi->render_sdf[1]);
  637. uniforms.push_back(u);
  638. }
  639. sdfgi->jump_flood_uniform_set[0] = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.preprocess.version_get_shader(sdfgi_shader.preprocess_shader, SDGIShader::PRE_PROCESS_JUMP_FLOOD), 0);
  640. SWAP(uniforms.write[0].ids.write[0], uniforms.write[1].ids.write[0]);
  641. sdfgi->jump_flood_uniform_set[1] = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.preprocess.version_get_shader(sdfgi_shader.preprocess_shader, SDGIShader::PRE_PROCESS_JUMP_FLOOD), 0);
  642. }
  643. //jump flood half uniform set
  644. {
  645. Vector<RD::Uniform> uniforms;
  646. {
  647. RD::Uniform u;
  648. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  649. u.binding = 1;
  650. u.ids.push_back(sdfgi->render_sdf_half[0]);
  651. uniforms.push_back(u);
  652. }
  653. {
  654. RD::Uniform u;
  655. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  656. u.binding = 2;
  657. u.ids.push_back(sdfgi->render_sdf_half[1]);
  658. uniforms.push_back(u);
  659. }
  660. sdfgi->jump_flood_half_uniform_set[0] = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.preprocess.version_get_shader(sdfgi_shader.preprocess_shader, SDGIShader::PRE_PROCESS_JUMP_FLOOD), 0);
  661. SWAP(uniforms.write[0].ids.write[0], uniforms.write[1].ids.write[0]);
  662. sdfgi->jump_flood_half_uniform_set[1] = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.preprocess.version_get_shader(sdfgi_shader.preprocess_shader, SDGIShader::PRE_PROCESS_JUMP_FLOOD), 0);
  663. }
  664. //upscale half size sdf
  665. {
  666. Vector<RD::Uniform> uniforms;
  667. {
  668. RD::Uniform u;
  669. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  670. u.binding = 1;
  671. u.ids.push_back(sdfgi->render_albedo);
  672. uniforms.push_back(u);
  673. }
  674. {
  675. RD::Uniform u;
  676. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  677. u.binding = 2;
  678. u.ids.push_back(sdfgi->render_sdf_half[(passes & 1) ? 0 : 1]); //reverse pass order because half size
  679. uniforms.push_back(u);
  680. }
  681. {
  682. RD::Uniform u;
  683. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  684. u.binding = 3;
  685. u.ids.push_back(sdfgi->render_sdf[(passes & 1) ? 0 : 1]); //reverse pass order because it needs an extra JFA pass
  686. uniforms.push_back(u);
  687. }
  688. sdfgi->upscale_jfa_uniform_set_index = (passes & 1) ? 0 : 1;
  689. sdfgi->sdf_upscale_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.preprocess.version_get_shader(sdfgi_shader.preprocess_shader, SDGIShader::PRE_PROCESS_JUMP_FLOOD_UPSCALE), 0);
  690. }
  691. //occlusion uniform set
  692. {
  693. Vector<RD::Uniform> uniforms;
  694. {
  695. RD::Uniform u;
  696. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  697. u.binding = 1;
  698. u.ids.push_back(sdfgi->render_albedo);
  699. uniforms.push_back(u);
  700. }
  701. {
  702. RD::Uniform u;
  703. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  704. u.binding = 2;
  705. for (int i = 0; i < 8; i++) {
  706. u.ids.push_back(sdfgi->render_occlusion[i]);
  707. }
  708. uniforms.push_back(u);
  709. }
  710. {
  711. RD::Uniform u;
  712. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  713. u.binding = 3;
  714. u.ids.push_back(sdfgi->render_geom_facing);
  715. uniforms.push_back(u);
  716. }
  717. sdfgi->occlusion_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.preprocess.version_get_shader(sdfgi_shader.preprocess_shader, SDGIShader::PRE_PROCESS_OCCLUSION), 0);
  718. }
  719. for (uint32_t i = 0; i < sdfgi->cascades.size(); i++) {
  720. //integrate uniform
  721. Vector<RD::Uniform> uniforms;
  722. {
  723. RD::Uniform u;
  724. u.binding = 1;
  725. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  726. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  727. if (j < sdfgi->cascades.size()) {
  728. u.ids.push_back(sdfgi->cascades[j].sdf_tex);
  729. } else {
  730. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  731. }
  732. }
  733. uniforms.push_back(u);
  734. }
  735. {
  736. RD::Uniform u;
  737. u.binding = 2;
  738. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  739. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  740. if (j < sdfgi->cascades.size()) {
  741. u.ids.push_back(sdfgi->cascades[j].light_tex);
  742. } else {
  743. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  744. }
  745. }
  746. uniforms.push_back(u);
  747. }
  748. {
  749. RD::Uniform u;
  750. u.binding = 3;
  751. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  752. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  753. if (j < sdfgi->cascades.size()) {
  754. u.ids.push_back(sdfgi->cascades[j].light_aniso_0_tex);
  755. } else {
  756. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  757. }
  758. }
  759. uniforms.push_back(u);
  760. }
  761. {
  762. RD::Uniform u;
  763. u.binding = 4;
  764. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  765. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  766. if (j < sdfgi->cascades.size()) {
  767. u.ids.push_back(sdfgi->cascades[j].light_aniso_1_tex);
  768. } else {
  769. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  770. }
  771. }
  772. uniforms.push_back(u);
  773. }
  774. {
  775. RD::Uniform u;
  776. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  777. u.binding = 6;
  778. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  779. uniforms.push_back(u);
  780. }
  781. {
  782. RD::Uniform u;
  783. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  784. u.binding = 7;
  785. u.ids.push_back(sdfgi->cascades_ubo);
  786. uniforms.push_back(u);
  787. }
  788. {
  789. RD::Uniform u;
  790. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  791. u.binding = 8;
  792. u.ids.push_back(sdfgi->lightprobe_data);
  793. uniforms.push_back(u);
  794. }
  795. {
  796. RD::Uniform u;
  797. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  798. u.binding = 9;
  799. u.ids.push_back(sdfgi->cascades[i].lightprobe_history_tex);
  800. uniforms.push_back(u);
  801. }
  802. {
  803. RD::Uniform u;
  804. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  805. u.binding = 10;
  806. u.ids.push_back(sdfgi->cascades[i].lightprobe_average_tex);
  807. uniforms.push_back(u);
  808. }
  809. {
  810. RD::Uniform u;
  811. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  812. u.binding = 11;
  813. u.ids.push_back(sdfgi->lightprobe_history_scroll);
  814. uniforms.push_back(u);
  815. }
  816. {
  817. RD::Uniform u;
  818. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  819. u.binding = 12;
  820. u.ids.push_back(sdfgi->lightprobe_average_scroll);
  821. uniforms.push_back(u);
  822. }
  823. {
  824. RD::Uniform u;
  825. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  826. u.binding = 13;
  827. RID parent_average;
  828. if (i < sdfgi->cascades.size() - 1) {
  829. parent_average = sdfgi->cascades[i + 1].lightprobe_average_tex;
  830. } else {
  831. parent_average = sdfgi->cascades[i - 1].lightprobe_average_tex; //to use something, but it won't be used
  832. }
  833. u.ids.push_back(parent_average);
  834. uniforms.push_back(u);
  835. }
  836. {
  837. RD::Uniform u;
  838. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  839. u.binding = 14;
  840. u.ids.push_back(sdfgi->ambient_texture);
  841. uniforms.push_back(u);
  842. }
  843. sdfgi->cascades[i].integrate_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.integrate.version_get_shader(sdfgi_shader.integrate_shader, 0), 0);
  844. }
  845. sdfgi->uses_multibounce = env->sdfgi_use_multibounce;
  846. sdfgi->energy = env->sdfgi_energy;
  847. sdfgi->normal_bias = env->sdfgi_normal_bias;
  848. sdfgi->probe_bias = env->sdfgi_probe_bias;
  849. sdfgi->reads_sky = env->sdfgi_read_sky_light;
  850. _render_buffers_uniform_set_changed(p_render_buffers);
  851. return; //done. all levels will need to be rendered which its going to take a bit
  852. }
  853. //check for updates
  854. sdfgi->uses_multibounce = env->sdfgi_use_multibounce;
  855. sdfgi->energy = env->sdfgi_energy;
  856. sdfgi->normal_bias = env->sdfgi_normal_bias;
  857. sdfgi->probe_bias = env->sdfgi_probe_bias;
  858. sdfgi->reads_sky = env->sdfgi_read_sky_light;
  859. int32_t drag_margin = (sdfgi->cascade_size / SDFGI::PROBE_DIVISOR) / 2;
  860. for (uint32_t i = 0; i < sdfgi->cascades.size(); i++) {
  861. SDFGI::Cascade &cascade = sdfgi->cascades[i];
  862. cascade.dirty_regions = Vector3i();
  863. Vector3 probe_half_size = Vector3(1, 1, 1) * cascade.cell_size * float(sdfgi->cascade_size / SDFGI::PROBE_DIVISOR) * 0.5;
  864. probe_half_size = Vector3(0, 0, 0);
  865. Vector3 world_position = p_world_position;
  866. world_position.y *= sdfgi->y_mult;
  867. Vector3i pos_in_cascade = Vector3i((world_position + probe_half_size) / cascade.cell_size);
  868. for (int j = 0; j < 3; j++) {
  869. if (pos_in_cascade[j] < cascade.position[j]) {
  870. while (pos_in_cascade[j] < (cascade.position[j] - drag_margin)) {
  871. cascade.position[j] -= drag_margin * 2;
  872. cascade.dirty_regions[j] += drag_margin * 2;
  873. }
  874. } else if (pos_in_cascade[j] > cascade.position[j]) {
  875. while (pos_in_cascade[j] > (cascade.position[j] + drag_margin)) {
  876. cascade.position[j] += drag_margin * 2;
  877. cascade.dirty_regions[j] -= drag_margin * 2;
  878. }
  879. }
  880. if (cascade.dirty_regions[j] == 0) {
  881. continue; // not dirty
  882. } else if (uint32_t(ABS(cascade.dirty_regions[j])) >= sdfgi->cascade_size) {
  883. //moved too much, just redraw everything (make all dirty)
  884. cascade.dirty_regions = SDFGI::Cascade::DIRTY_ALL;
  885. break;
  886. }
  887. }
  888. if (cascade.dirty_regions != Vector3i() && cascade.dirty_regions != SDFGI::Cascade::DIRTY_ALL) {
  889. //see how much the total dirty volume represents from the total volume
  890. uint32_t total_volume = sdfgi->cascade_size * sdfgi->cascade_size * sdfgi->cascade_size;
  891. uint32_t safe_volume = 1;
  892. for (int j = 0; j < 3; j++) {
  893. safe_volume *= sdfgi->cascade_size - ABS(cascade.dirty_regions[j]);
  894. }
  895. uint32_t dirty_volume = total_volume - safe_volume;
  896. if (dirty_volume > (safe_volume / 2)) {
  897. //more than half the volume is dirty, make all dirty so its only rendered once
  898. cascade.dirty_regions = SDFGI::Cascade::DIRTY_ALL;
  899. }
  900. }
  901. }
  902. }
  903. int RendererSceneRenderRD::sdfgi_get_pending_region_count(RID p_render_buffers) const {
  904. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  905. ERR_FAIL_COND_V(rb == nullptr, 0);
  906. if (rb->sdfgi == nullptr) {
  907. return 0;
  908. }
  909. int dirty_count = 0;
  910. for (uint32_t i = 0; i < rb->sdfgi->cascades.size(); i++) {
  911. const SDFGI::Cascade &c = rb->sdfgi->cascades[i];
  912. if (c.dirty_regions == SDFGI::Cascade::DIRTY_ALL) {
  913. dirty_count++;
  914. } else {
  915. for (int j = 0; j < 3; j++) {
  916. if (c.dirty_regions[j] != 0) {
  917. dirty_count++;
  918. }
  919. }
  920. }
  921. }
  922. return dirty_count;
  923. }
  924. int RendererSceneRenderRD::_sdfgi_get_pending_region_data(RID p_render_buffers, int p_region, Vector3i &r_local_offset, Vector3i &r_local_size, AABB &r_bounds) const {
  925. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  926. ERR_FAIL_COND_V(rb == nullptr, -1);
  927. ERR_FAIL_COND_V(rb->sdfgi == nullptr, -1);
  928. int dirty_count = 0;
  929. for (uint32_t i = 0; i < rb->sdfgi->cascades.size(); i++) {
  930. const SDFGI::Cascade &c = rb->sdfgi->cascades[i];
  931. if (c.dirty_regions == SDFGI::Cascade::DIRTY_ALL) {
  932. if (dirty_count == p_region) {
  933. r_local_offset = Vector3i();
  934. r_local_size = Vector3i(1, 1, 1) * rb->sdfgi->cascade_size;
  935. r_bounds.position = Vector3((Vector3i(1, 1, 1) * -int32_t(rb->sdfgi->cascade_size >> 1) + c.position)) * c.cell_size * Vector3(1, 1.0 / rb->sdfgi->y_mult, 1);
  936. r_bounds.size = Vector3(r_local_size) * c.cell_size * Vector3(1, 1.0 / rb->sdfgi->y_mult, 1);
  937. return i;
  938. }
  939. dirty_count++;
  940. } else {
  941. for (int j = 0; j < 3; j++) {
  942. if (c.dirty_regions[j] != 0) {
  943. if (dirty_count == p_region) {
  944. Vector3i from = Vector3i(0, 0, 0);
  945. Vector3i to = Vector3i(1, 1, 1) * rb->sdfgi->cascade_size;
  946. if (c.dirty_regions[j] > 0) {
  947. //fill from the beginning
  948. to[j] = c.dirty_regions[j];
  949. } else {
  950. //fill from the end
  951. from[j] = to[j] + c.dirty_regions[j];
  952. }
  953. for (int k = 0; k < j; k++) {
  954. // "chip" away previous regions to avoid re-voxelizing the same thing
  955. if (c.dirty_regions[k] > 0) {
  956. from[k] += c.dirty_regions[k];
  957. } else if (c.dirty_regions[k] < 0) {
  958. to[k] += c.dirty_regions[k];
  959. }
  960. }
  961. r_local_offset = from;
  962. r_local_size = to - from;
  963. r_bounds.position = Vector3(from + Vector3i(1, 1, 1) * -int32_t(rb->sdfgi->cascade_size >> 1) + c.position) * c.cell_size * Vector3(1, 1.0 / rb->sdfgi->y_mult, 1);
  964. r_bounds.size = Vector3(r_local_size) * c.cell_size * Vector3(1, 1.0 / rb->sdfgi->y_mult, 1);
  965. return i;
  966. }
  967. dirty_count++;
  968. }
  969. }
  970. }
  971. }
  972. return -1;
  973. }
  974. AABB RendererSceneRenderRD::sdfgi_get_pending_region_bounds(RID p_render_buffers, int p_region) const {
  975. AABB bounds;
  976. Vector3i from;
  977. Vector3i size;
  978. int c = _sdfgi_get_pending_region_data(p_render_buffers, p_region, from, size, bounds);
  979. ERR_FAIL_COND_V(c == -1, AABB());
  980. return bounds;
  981. }
  982. uint32_t RendererSceneRenderRD::sdfgi_get_pending_region_cascade(RID p_render_buffers, int p_region) const {
  983. AABB bounds;
  984. Vector3i from;
  985. Vector3i size;
  986. return _sdfgi_get_pending_region_data(p_render_buffers, p_region, from, size, bounds);
  987. }
  988. void RendererSceneRenderRD::_sdfgi_update_cascades(RID p_render_buffers) {
  989. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  990. ERR_FAIL_COND(rb == nullptr);
  991. if (rb->sdfgi == nullptr) {
  992. return;
  993. }
  994. //update cascades
  995. SDFGI::Cascade::UBO cascade_data[SDFGI::MAX_CASCADES];
  996. int32_t probe_divisor = rb->sdfgi->cascade_size / SDFGI::PROBE_DIVISOR;
  997. for (uint32_t i = 0; i < rb->sdfgi->cascades.size(); i++) {
  998. Vector3 pos = Vector3((Vector3i(1, 1, 1) * -int32_t(rb->sdfgi->cascade_size >> 1) + rb->sdfgi->cascades[i].position)) * rb->sdfgi->cascades[i].cell_size;
  999. cascade_data[i].offset[0] = pos.x;
  1000. cascade_data[i].offset[1] = pos.y;
  1001. cascade_data[i].offset[2] = pos.z;
  1002. cascade_data[i].to_cell = 1.0 / rb->sdfgi->cascades[i].cell_size;
  1003. cascade_data[i].probe_offset[0] = rb->sdfgi->cascades[i].position.x / probe_divisor;
  1004. cascade_data[i].probe_offset[1] = rb->sdfgi->cascades[i].position.y / probe_divisor;
  1005. cascade_data[i].probe_offset[2] = rb->sdfgi->cascades[i].position.z / probe_divisor;
  1006. cascade_data[i].pad = 0;
  1007. }
  1008. RD::get_singleton()->buffer_update(rb->sdfgi->cascades_ubo, 0, sizeof(SDFGI::Cascade::UBO) * SDFGI::MAX_CASCADES, cascade_data, true);
  1009. }
  1010. void RendererSceneRenderRD::sdfgi_update_probes(RID p_render_buffers, RID p_environment, const RID *p_directional_light_instances, uint32_t p_directional_light_count, const RID *p_positional_light_instances, uint32_t p_positional_light_count) {
  1011. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  1012. ERR_FAIL_COND(rb == nullptr);
  1013. if (rb->sdfgi == nullptr) {
  1014. return;
  1015. }
  1016. Environment *env = environment_owner.getornull(p_environment);
  1017. RENDER_TIMESTAMP(">SDFGI Update Probes");
  1018. /* Update Cascades UBO */
  1019. _sdfgi_update_cascades(p_render_buffers);
  1020. /* Update Dynamic Lights Buffer */
  1021. RENDER_TIMESTAMP("Update Lights");
  1022. /* Update dynamic lights */
  1023. {
  1024. int32_t cascade_light_count[SDFGI::MAX_CASCADES];
  1025. for (uint32_t i = 0; i < rb->sdfgi->cascades.size(); i++) {
  1026. SDFGI::Cascade &cascade = rb->sdfgi->cascades[i];
  1027. SDGIShader::Light lights[SDFGI::MAX_DYNAMIC_LIGHTS];
  1028. uint32_t idx = 0;
  1029. for (uint32_t j = 0; j < p_directional_light_count; j++) {
  1030. if (idx == SDFGI::MAX_DYNAMIC_LIGHTS) {
  1031. break;
  1032. }
  1033. LightInstance *li = light_instance_owner.getornull(p_directional_light_instances[j]);
  1034. ERR_CONTINUE(!li);
  1035. if (storage->light_directional_is_sky_only(li->light)) {
  1036. continue;
  1037. }
  1038. Vector3 dir = -li->transform.basis.get_axis(Vector3::AXIS_Z);
  1039. dir.y *= rb->sdfgi->y_mult;
  1040. dir.normalize();
  1041. lights[idx].direction[0] = dir.x;
  1042. lights[idx].direction[1] = dir.y;
  1043. lights[idx].direction[2] = dir.z;
  1044. Color color = storage->light_get_color(li->light);
  1045. color = color.to_linear();
  1046. lights[idx].color[0] = color.r;
  1047. lights[idx].color[1] = color.g;
  1048. lights[idx].color[2] = color.b;
  1049. lights[idx].type = RS::LIGHT_DIRECTIONAL;
  1050. lights[idx].energy = storage->light_get_param(li->light, RS::LIGHT_PARAM_ENERGY);
  1051. lights[idx].has_shadow = storage->light_has_shadow(li->light);
  1052. idx++;
  1053. }
  1054. AABB cascade_aabb;
  1055. cascade_aabb.position = Vector3((Vector3i(1, 1, 1) * -int32_t(rb->sdfgi->cascade_size >> 1) + cascade.position)) * cascade.cell_size;
  1056. cascade_aabb.size = Vector3(1, 1, 1) * rb->sdfgi->cascade_size * cascade.cell_size;
  1057. for (uint32_t j = 0; j < p_positional_light_count; j++) {
  1058. if (idx == SDFGI::MAX_DYNAMIC_LIGHTS) {
  1059. break;
  1060. }
  1061. LightInstance *li = light_instance_owner.getornull(p_positional_light_instances[j]);
  1062. ERR_CONTINUE(!li);
  1063. uint32_t max_sdfgi_cascade = storage->light_get_max_sdfgi_cascade(li->light);
  1064. if (i > max_sdfgi_cascade) {
  1065. continue;
  1066. }
  1067. if (!cascade_aabb.intersects(li->aabb)) {
  1068. continue;
  1069. }
  1070. Vector3 dir = -li->transform.basis.get_axis(Vector3::AXIS_Z);
  1071. //faster to not do this here
  1072. //dir.y *= rb->sdfgi->y_mult;
  1073. //dir.normalize();
  1074. lights[idx].direction[0] = dir.x;
  1075. lights[idx].direction[1] = dir.y;
  1076. lights[idx].direction[2] = dir.z;
  1077. Vector3 pos = li->transform.origin;
  1078. pos.y *= rb->sdfgi->y_mult;
  1079. lights[idx].position[0] = pos.x;
  1080. lights[idx].position[1] = pos.y;
  1081. lights[idx].position[2] = pos.z;
  1082. Color color = storage->light_get_color(li->light);
  1083. color = color.to_linear();
  1084. lights[idx].color[0] = color.r;
  1085. lights[idx].color[1] = color.g;
  1086. lights[idx].color[2] = color.b;
  1087. lights[idx].type = storage->light_get_type(li->light);
  1088. lights[idx].energy = storage->light_get_param(li->light, RS::LIGHT_PARAM_ENERGY);
  1089. lights[idx].has_shadow = storage->light_has_shadow(li->light);
  1090. lights[idx].attenuation = storage->light_get_param(li->light, RS::LIGHT_PARAM_ATTENUATION);
  1091. lights[idx].radius = storage->light_get_param(li->light, RS::LIGHT_PARAM_RANGE);
  1092. lights[idx].spot_angle = Math::deg2rad(storage->light_get_param(li->light, RS::LIGHT_PARAM_SPOT_ANGLE));
  1093. lights[idx].spot_attenuation = storage->light_get_param(li->light, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  1094. idx++;
  1095. }
  1096. if (idx > 0) {
  1097. RD::get_singleton()->buffer_update(cascade.lights_buffer, 0, idx * sizeof(SDGIShader::Light), lights, true);
  1098. }
  1099. cascade_light_count[i] = idx;
  1100. }
  1101. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1102. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.direct_light_pipeline[SDGIShader::DIRECT_LIGHT_MODE_DYNAMIC]);
  1103. SDGIShader::DirectLightPushConstant push_constant;
  1104. push_constant.grid_size[0] = rb->sdfgi->cascade_size;
  1105. push_constant.grid_size[1] = rb->sdfgi->cascade_size;
  1106. push_constant.grid_size[2] = rb->sdfgi->cascade_size;
  1107. push_constant.max_cascades = rb->sdfgi->cascades.size();
  1108. push_constant.probe_axis_size = rb->sdfgi->probe_axis_count;
  1109. push_constant.multibounce = rb->sdfgi->uses_multibounce;
  1110. push_constant.y_mult = rb->sdfgi->y_mult;
  1111. push_constant.process_offset = 0;
  1112. push_constant.process_increment = 1;
  1113. for (uint32_t i = 0; i < rb->sdfgi->cascades.size(); i++) {
  1114. SDFGI::Cascade &cascade = rb->sdfgi->cascades[i];
  1115. push_constant.light_count = cascade_light_count[i];
  1116. push_constant.cascade = i;
  1117. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascade.sdf_direct_light_uniform_set, 0);
  1118. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::DirectLightPushConstant));
  1119. RD::get_singleton()->compute_list_dispatch_indirect(compute_list, cascade.solid_cell_dispatch_buffer, 0);
  1120. }
  1121. RD::get_singleton()->compute_list_end();
  1122. }
  1123. RENDER_TIMESTAMP("Raytrace");
  1124. SDGIShader::IntegratePushConstant push_constant;
  1125. push_constant.grid_size[1] = rb->sdfgi->cascade_size;
  1126. push_constant.grid_size[2] = rb->sdfgi->cascade_size;
  1127. push_constant.grid_size[0] = rb->sdfgi->cascade_size;
  1128. push_constant.max_cascades = rb->sdfgi->cascades.size();
  1129. push_constant.probe_axis_size = rb->sdfgi->probe_axis_count;
  1130. push_constant.history_index = rb->sdfgi->render_pass % rb->sdfgi->history_size;
  1131. push_constant.history_size = rb->sdfgi->history_size;
  1132. static const uint32_t ray_count[RS::ENV_SDFGI_RAY_COUNT_MAX] = { 8, 16, 32, 64, 96, 128 };
  1133. push_constant.ray_count = ray_count[sdfgi_ray_count];
  1134. push_constant.ray_bias = rb->sdfgi->probe_bias;
  1135. push_constant.image_size[0] = rb->sdfgi->probe_axis_count * rb->sdfgi->probe_axis_count;
  1136. push_constant.image_size[1] = rb->sdfgi->probe_axis_count;
  1137. push_constant.store_ambient_texture = env->volumetric_fog_enabled;
  1138. RID sky_uniform_set = sdfgi_shader.integrate_default_sky_uniform_set;
  1139. push_constant.sky_mode = SDGIShader::IntegratePushConstant::SKY_MODE_DISABLED;
  1140. push_constant.y_mult = rb->sdfgi->y_mult;
  1141. if (rb->sdfgi->reads_sky && env) {
  1142. push_constant.sky_energy = env->bg_energy;
  1143. if (env->background == RS::ENV_BG_CLEAR_COLOR) {
  1144. push_constant.sky_mode = SDGIShader::IntegratePushConstant::SKY_MODE_COLOR;
  1145. Color c = storage->get_default_clear_color().to_linear();
  1146. push_constant.sky_color[0] = c.r;
  1147. push_constant.sky_color[1] = c.g;
  1148. push_constant.sky_color[2] = c.b;
  1149. } else if (env->background == RS::ENV_BG_COLOR) {
  1150. push_constant.sky_mode = SDGIShader::IntegratePushConstant::SKY_MODE_COLOR;
  1151. Color c = env->bg_color;
  1152. push_constant.sky_color[0] = c.r;
  1153. push_constant.sky_color[1] = c.g;
  1154. push_constant.sky_color[2] = c.b;
  1155. } else if (env->background == RS::ENV_BG_SKY) {
  1156. Sky *sky = sky_owner.getornull(env->sky);
  1157. if (sky && sky->radiance.is_valid()) {
  1158. if (sky->sdfgi_integrate_sky_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(sky->sdfgi_integrate_sky_uniform_set)) {
  1159. Vector<RD::Uniform> uniforms;
  1160. {
  1161. RD::Uniform u;
  1162. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1163. u.binding = 0;
  1164. u.ids.push_back(sky->radiance);
  1165. uniforms.push_back(u);
  1166. }
  1167. {
  1168. RD::Uniform u;
  1169. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  1170. u.binding = 1;
  1171. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1172. uniforms.push_back(u);
  1173. }
  1174. sky->sdfgi_integrate_sky_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.integrate.version_get_shader(sdfgi_shader.integrate_shader, 0), 1);
  1175. }
  1176. sky_uniform_set = sky->sdfgi_integrate_sky_uniform_set;
  1177. push_constant.sky_mode = SDGIShader::IntegratePushConstant::SKY_MODE_SKY;
  1178. }
  1179. }
  1180. }
  1181. rb->sdfgi->render_pass++;
  1182. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1183. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.integrate_pipeline[SDGIShader::INTEGRATE_MODE_PROCESS]);
  1184. int32_t probe_divisor = rb->sdfgi->cascade_size / SDFGI::PROBE_DIVISOR;
  1185. for (uint32_t i = 0; i < rb->sdfgi->cascades.size(); i++) {
  1186. push_constant.cascade = i;
  1187. push_constant.world_offset[0] = rb->sdfgi->cascades[i].position.x / probe_divisor;
  1188. push_constant.world_offset[1] = rb->sdfgi->cascades[i].position.y / probe_divisor;
  1189. push_constant.world_offset[2] = rb->sdfgi->cascades[i].position.z / probe_divisor;
  1190. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->cascades[i].integrate_uniform_set, 0);
  1191. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sky_uniform_set, 1);
  1192. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::IntegratePushConstant));
  1193. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->sdfgi->probe_axis_count * rb->sdfgi->probe_axis_count, rb->sdfgi->probe_axis_count, 1, 8, 8, 1);
  1194. }
  1195. RD::get_singleton()->compute_list_add_barrier(compute_list); //wait until done
  1196. // Then store values into the lightprobe texture. Separating these steps has a small performance hit, but it allows for multiple bounces
  1197. RENDER_TIMESTAMP("Average Probes");
  1198. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.integrate_pipeline[SDGIShader::INTEGRATE_MODE_STORE]);
  1199. //convert to octahedral to store
  1200. push_constant.image_size[0] *= SDFGI::LIGHTPROBE_OCT_SIZE;
  1201. push_constant.image_size[1] *= SDFGI::LIGHTPROBE_OCT_SIZE;
  1202. for (uint32_t i = 0; i < rb->sdfgi->cascades.size(); i++) {
  1203. push_constant.cascade = i;
  1204. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->cascades[i].integrate_uniform_set, 0);
  1205. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::IntegratePushConstant));
  1206. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->sdfgi->probe_axis_count * rb->sdfgi->probe_axis_count * SDFGI::LIGHTPROBE_OCT_SIZE, rb->sdfgi->probe_axis_count * SDFGI::LIGHTPROBE_OCT_SIZE, 1, 8, 8, 1);
  1207. }
  1208. RD::get_singleton()->compute_list_end();
  1209. RENDER_TIMESTAMP("<SDFGI Update Probes");
  1210. }
  1211. void RendererSceneRenderRD::_setup_giprobes(RID p_render_buffers, const Transform &p_transform, RID *p_gi_probe_cull_result, int p_gi_probe_cull_count, uint32_t &r_gi_probes_used) {
  1212. r_gi_probes_used = 0;
  1213. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  1214. ERR_FAIL_COND(rb == nullptr);
  1215. RID gi_probe_buffer = render_buffers_get_gi_probe_buffer(p_render_buffers);
  1216. GI::GIProbeData gi_probe_data[RenderBuffers::MAX_GIPROBES];
  1217. bool giprobes_changed = false;
  1218. Transform to_camera;
  1219. to_camera.origin = p_transform.origin; //only translation, make local
  1220. for (int i = 0; i < RenderBuffers::MAX_GIPROBES; i++) {
  1221. RID texture;
  1222. if (i < p_gi_probe_cull_count) {
  1223. GIProbeInstance *gipi = gi_probe_instance_owner.getornull(p_gi_probe_cull_result[i]);
  1224. if (gipi) {
  1225. texture = gipi->texture;
  1226. GI::GIProbeData &gipd = gi_probe_data[i];
  1227. RID base_probe = gipi->probe;
  1228. Transform to_cell = storage->gi_probe_get_to_cell_xform(gipi->probe) * gipi->transform.affine_inverse() * to_camera;
  1229. gipd.xform[0] = to_cell.basis.elements[0][0];
  1230. gipd.xform[1] = to_cell.basis.elements[1][0];
  1231. gipd.xform[2] = to_cell.basis.elements[2][0];
  1232. gipd.xform[3] = 0;
  1233. gipd.xform[4] = to_cell.basis.elements[0][1];
  1234. gipd.xform[5] = to_cell.basis.elements[1][1];
  1235. gipd.xform[6] = to_cell.basis.elements[2][1];
  1236. gipd.xform[7] = 0;
  1237. gipd.xform[8] = to_cell.basis.elements[0][2];
  1238. gipd.xform[9] = to_cell.basis.elements[1][2];
  1239. gipd.xform[10] = to_cell.basis.elements[2][2];
  1240. gipd.xform[11] = 0;
  1241. gipd.xform[12] = to_cell.origin.x;
  1242. gipd.xform[13] = to_cell.origin.y;
  1243. gipd.xform[14] = to_cell.origin.z;
  1244. gipd.xform[15] = 1;
  1245. Vector3 bounds = storage->gi_probe_get_octree_size(base_probe);
  1246. gipd.bounds[0] = bounds.x;
  1247. gipd.bounds[1] = bounds.y;
  1248. gipd.bounds[2] = bounds.z;
  1249. gipd.dynamic_range = storage->gi_probe_get_dynamic_range(base_probe) * storage->gi_probe_get_energy(base_probe);
  1250. gipd.bias = storage->gi_probe_get_bias(base_probe);
  1251. gipd.normal_bias = storage->gi_probe_get_normal_bias(base_probe);
  1252. gipd.blend_ambient = !storage->gi_probe_is_interior(base_probe);
  1253. gipd.anisotropy_strength = 0;
  1254. gipd.ao = storage->gi_probe_get_ao(base_probe);
  1255. gipd.ao_size = Math::pow(storage->gi_probe_get_ao_size(base_probe), 4.0f);
  1256. gipd.mipmaps = gipi->mipmaps.size();
  1257. }
  1258. r_gi_probes_used++;
  1259. }
  1260. if (texture == RID()) {
  1261. texture = storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE);
  1262. }
  1263. if (texture != rb->giprobe_textures[i]) {
  1264. giprobes_changed = true;
  1265. rb->giprobe_textures[i] = texture;
  1266. }
  1267. }
  1268. if (giprobes_changed) {
  1269. if (RD::get_singleton()->uniform_set_is_valid(rb->gi_uniform_set)) {
  1270. RD::get_singleton()->free(rb->gi_uniform_set);
  1271. }
  1272. rb->gi_uniform_set = RID();
  1273. if (rb->volumetric_fog) {
  1274. if (RD::get_singleton()->uniform_set_is_valid(rb->volumetric_fog->uniform_set)) {
  1275. RD::get_singleton()->free(rb->volumetric_fog->uniform_set);
  1276. RD::get_singleton()->free(rb->volumetric_fog->uniform_set2);
  1277. }
  1278. rb->volumetric_fog->uniform_set = RID();
  1279. rb->volumetric_fog->uniform_set2 = RID();
  1280. }
  1281. }
  1282. if (p_gi_probe_cull_count > 0) {
  1283. RD::get_singleton()->buffer_update(gi_probe_buffer, 0, sizeof(GI::GIProbeData) * MIN(RenderBuffers::MAX_GIPROBES, p_gi_probe_cull_count), gi_probe_data, true);
  1284. }
  1285. }
  1286. void RendererSceneRenderRD::_process_gi(RID p_render_buffers, RID p_normal_roughness_buffer, RID p_ambient_buffer, RID p_reflection_buffer, RID p_gi_probe_buffer, RID p_environment, const CameraMatrix &p_projection, const Transform &p_transform, RID *p_gi_probe_cull_result, int p_gi_probe_cull_count) {
  1287. RENDER_TIMESTAMP("Render GI");
  1288. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  1289. ERR_FAIL_COND(rb == nullptr);
  1290. Environment *env = environment_owner.getornull(p_environment);
  1291. GI::PushConstant push_constant;
  1292. push_constant.screen_size[0] = rb->width;
  1293. push_constant.screen_size[1] = rb->height;
  1294. push_constant.z_near = p_projection.get_z_near();
  1295. push_constant.z_far = p_projection.get_z_far();
  1296. push_constant.orthogonal = p_projection.is_orthogonal();
  1297. push_constant.proj_info[0] = -2.0f / (rb->width * p_projection.matrix[0][0]);
  1298. push_constant.proj_info[1] = -2.0f / (rb->height * p_projection.matrix[1][1]);
  1299. push_constant.proj_info[2] = (1.0f - p_projection.matrix[0][2]) / p_projection.matrix[0][0];
  1300. push_constant.proj_info[3] = (1.0f + p_projection.matrix[1][2]) / p_projection.matrix[1][1];
  1301. push_constant.max_giprobes = MIN(RenderBuffers::MAX_GIPROBES, p_gi_probe_cull_count);
  1302. push_constant.high_quality_vct = gi_probe_quality == RS::GI_PROBE_QUALITY_HIGH;
  1303. push_constant.use_sdfgi = rb->sdfgi != nullptr;
  1304. if (env) {
  1305. push_constant.ao_color[0] = env->ao_color.r;
  1306. push_constant.ao_color[1] = env->ao_color.g;
  1307. push_constant.ao_color[2] = env->ao_color.b;
  1308. } else {
  1309. push_constant.ao_color[0] = 0;
  1310. push_constant.ao_color[1] = 0;
  1311. push_constant.ao_color[2] = 0;
  1312. }
  1313. push_constant.cam_rotation[0] = p_transform.basis[0][0];
  1314. push_constant.cam_rotation[1] = p_transform.basis[1][0];
  1315. push_constant.cam_rotation[2] = p_transform.basis[2][0];
  1316. push_constant.cam_rotation[3] = 0;
  1317. push_constant.cam_rotation[4] = p_transform.basis[0][1];
  1318. push_constant.cam_rotation[5] = p_transform.basis[1][1];
  1319. push_constant.cam_rotation[6] = p_transform.basis[2][1];
  1320. push_constant.cam_rotation[7] = 0;
  1321. push_constant.cam_rotation[8] = p_transform.basis[0][2];
  1322. push_constant.cam_rotation[9] = p_transform.basis[1][2];
  1323. push_constant.cam_rotation[10] = p_transform.basis[2][2];
  1324. push_constant.cam_rotation[11] = 0;
  1325. if (rb->sdfgi) {
  1326. GI::SDFGIData sdfgi_data;
  1327. sdfgi_data.grid_size[0] = rb->sdfgi->cascade_size;
  1328. sdfgi_data.grid_size[1] = rb->sdfgi->cascade_size;
  1329. sdfgi_data.grid_size[2] = rb->sdfgi->cascade_size;
  1330. sdfgi_data.max_cascades = rb->sdfgi->cascades.size();
  1331. sdfgi_data.probe_axis_size = rb->sdfgi->probe_axis_count;
  1332. sdfgi_data.cascade_probe_size[0] = sdfgi_data.probe_axis_size - 1; //float version for performance
  1333. sdfgi_data.cascade_probe_size[1] = sdfgi_data.probe_axis_size - 1;
  1334. sdfgi_data.cascade_probe_size[2] = sdfgi_data.probe_axis_size - 1;
  1335. float csize = rb->sdfgi->cascade_size;
  1336. sdfgi_data.probe_to_uvw = 1.0 / float(sdfgi_data.cascade_probe_size[0]);
  1337. sdfgi_data.use_occlusion = rb->sdfgi->uses_occlusion;
  1338. //sdfgi_data.energy = rb->sdfgi->energy;
  1339. sdfgi_data.y_mult = rb->sdfgi->y_mult;
  1340. float cascade_voxel_size = (csize / sdfgi_data.cascade_probe_size[0]);
  1341. float occlusion_clamp = (cascade_voxel_size - 0.5) / cascade_voxel_size;
  1342. sdfgi_data.occlusion_clamp[0] = occlusion_clamp;
  1343. sdfgi_data.occlusion_clamp[1] = occlusion_clamp;
  1344. sdfgi_data.occlusion_clamp[2] = occlusion_clamp;
  1345. sdfgi_data.normal_bias = (rb->sdfgi->normal_bias / csize) * sdfgi_data.cascade_probe_size[0];
  1346. //vec2 tex_pixel_size = 1.0 / vec2(ivec2( (OCT_SIZE+2) * params.probe_axis_size * params.probe_axis_size, (OCT_SIZE+2) * params.probe_axis_size ) );
  1347. //vec3 probe_uv_offset = (ivec3(OCT_SIZE+2,OCT_SIZE+2,(OCT_SIZE+2) * params.probe_axis_size)) * tex_pixel_size.xyx;
  1348. uint32_t oct_size = SDFGI::LIGHTPROBE_OCT_SIZE;
  1349. sdfgi_data.lightprobe_tex_pixel_size[0] = 1.0 / ((oct_size + 2) * sdfgi_data.probe_axis_size * sdfgi_data.probe_axis_size);
  1350. sdfgi_data.lightprobe_tex_pixel_size[1] = 1.0 / ((oct_size + 2) * sdfgi_data.probe_axis_size);
  1351. sdfgi_data.lightprobe_tex_pixel_size[2] = 1.0;
  1352. sdfgi_data.energy = rb->sdfgi->energy;
  1353. sdfgi_data.lightprobe_uv_offset[0] = float(oct_size + 2) * sdfgi_data.lightprobe_tex_pixel_size[0];
  1354. sdfgi_data.lightprobe_uv_offset[1] = float(oct_size + 2) * sdfgi_data.lightprobe_tex_pixel_size[1];
  1355. sdfgi_data.lightprobe_uv_offset[2] = float((oct_size + 2) * sdfgi_data.probe_axis_size) * sdfgi_data.lightprobe_tex_pixel_size[0];
  1356. sdfgi_data.occlusion_renormalize[0] = 0.5;
  1357. sdfgi_data.occlusion_renormalize[1] = 1.0;
  1358. sdfgi_data.occlusion_renormalize[2] = 1.0 / float(sdfgi_data.max_cascades);
  1359. int32_t probe_divisor = rb->sdfgi->cascade_size / SDFGI::PROBE_DIVISOR;
  1360. for (uint32_t i = 0; i < sdfgi_data.max_cascades; i++) {
  1361. GI::SDFGIData::ProbeCascadeData &c = sdfgi_data.cascades[i];
  1362. Vector3 pos = Vector3((Vector3i(1, 1, 1) * -int32_t(rb->sdfgi->cascade_size >> 1) + rb->sdfgi->cascades[i].position)) * rb->sdfgi->cascades[i].cell_size;
  1363. Vector3 cam_origin = p_transform.origin;
  1364. cam_origin.y *= rb->sdfgi->y_mult;
  1365. pos -= cam_origin; //make pos local to camera, to reduce numerical error
  1366. c.position[0] = pos.x;
  1367. c.position[1] = pos.y;
  1368. c.position[2] = pos.z;
  1369. c.to_probe = 1.0 / (float(rb->sdfgi->cascade_size) * rb->sdfgi->cascades[i].cell_size / float(rb->sdfgi->probe_axis_count - 1));
  1370. Vector3i probe_ofs = rb->sdfgi->cascades[i].position / probe_divisor;
  1371. c.probe_world_offset[0] = probe_ofs.x;
  1372. c.probe_world_offset[1] = probe_ofs.y;
  1373. c.probe_world_offset[2] = probe_ofs.z;
  1374. c.to_cell = 1.0 / rb->sdfgi->cascades[i].cell_size;
  1375. }
  1376. RD::get_singleton()->buffer_update(gi.sdfgi_ubo, 0, sizeof(GI::SDFGIData), &sdfgi_data, true);
  1377. }
  1378. if (rb->gi_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(rb->gi_uniform_set)) {
  1379. Vector<RD::Uniform> uniforms;
  1380. {
  1381. RD::Uniform u;
  1382. u.binding = 1;
  1383. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1384. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  1385. if (rb->sdfgi && j < rb->sdfgi->cascades.size()) {
  1386. u.ids.push_back(rb->sdfgi->cascades[j].sdf_tex);
  1387. } else {
  1388. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  1389. }
  1390. }
  1391. uniforms.push_back(u);
  1392. }
  1393. {
  1394. RD::Uniform u;
  1395. u.binding = 2;
  1396. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1397. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  1398. if (rb->sdfgi && j < rb->sdfgi->cascades.size()) {
  1399. u.ids.push_back(rb->sdfgi->cascades[j].light_tex);
  1400. } else {
  1401. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  1402. }
  1403. }
  1404. uniforms.push_back(u);
  1405. }
  1406. {
  1407. RD::Uniform u;
  1408. u.binding = 3;
  1409. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1410. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  1411. if (rb->sdfgi && j < rb->sdfgi->cascades.size()) {
  1412. u.ids.push_back(rb->sdfgi->cascades[j].light_aniso_0_tex);
  1413. } else {
  1414. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  1415. }
  1416. }
  1417. uniforms.push_back(u);
  1418. }
  1419. {
  1420. RD::Uniform u;
  1421. u.binding = 4;
  1422. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1423. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  1424. if (rb->sdfgi && j < rb->sdfgi->cascades.size()) {
  1425. u.ids.push_back(rb->sdfgi->cascades[j].light_aniso_1_tex);
  1426. } else {
  1427. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  1428. }
  1429. }
  1430. uniforms.push_back(u);
  1431. }
  1432. {
  1433. RD::Uniform u;
  1434. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1435. u.binding = 5;
  1436. if (rb->sdfgi) {
  1437. u.ids.push_back(rb->sdfgi->occlusion_texture);
  1438. } else {
  1439. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  1440. }
  1441. uniforms.push_back(u);
  1442. }
  1443. {
  1444. RD::Uniform u;
  1445. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  1446. u.binding = 6;
  1447. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1448. uniforms.push_back(u);
  1449. }
  1450. {
  1451. RD::Uniform u;
  1452. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  1453. u.binding = 7;
  1454. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1455. uniforms.push_back(u);
  1456. }
  1457. {
  1458. RD::Uniform u;
  1459. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1460. u.binding = 9;
  1461. u.ids.push_back(p_ambient_buffer);
  1462. uniforms.push_back(u);
  1463. }
  1464. {
  1465. RD::Uniform u;
  1466. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1467. u.binding = 10;
  1468. u.ids.push_back(p_reflection_buffer);
  1469. uniforms.push_back(u);
  1470. }
  1471. {
  1472. RD::Uniform u;
  1473. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1474. u.binding = 11;
  1475. if (rb->sdfgi) {
  1476. u.ids.push_back(rb->sdfgi->lightprobe_texture);
  1477. } else {
  1478. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE));
  1479. }
  1480. uniforms.push_back(u);
  1481. }
  1482. {
  1483. RD::Uniform u;
  1484. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1485. u.binding = 12;
  1486. u.ids.push_back(rb->depth_texture);
  1487. uniforms.push_back(u);
  1488. }
  1489. {
  1490. RD::Uniform u;
  1491. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1492. u.binding = 13;
  1493. u.ids.push_back(p_normal_roughness_buffer);
  1494. uniforms.push_back(u);
  1495. }
  1496. {
  1497. RD::Uniform u;
  1498. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1499. u.binding = 14;
  1500. RID buffer = p_gi_probe_buffer.is_valid() ? p_gi_probe_buffer : storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_BLACK);
  1501. u.ids.push_back(buffer);
  1502. uniforms.push_back(u);
  1503. }
  1504. {
  1505. RD::Uniform u;
  1506. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1507. u.binding = 15;
  1508. u.ids.push_back(gi.sdfgi_ubo);
  1509. uniforms.push_back(u);
  1510. }
  1511. {
  1512. RD::Uniform u;
  1513. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1514. u.binding = 16;
  1515. u.ids.push_back(rb->giprobe_buffer);
  1516. uniforms.push_back(u);
  1517. }
  1518. {
  1519. RD::Uniform u;
  1520. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1521. u.binding = 17;
  1522. for (int i = 0; i < RenderBuffers::MAX_GIPROBES; i++) {
  1523. u.ids.push_back(rb->giprobe_textures[i]);
  1524. }
  1525. uniforms.push_back(u);
  1526. }
  1527. rb->gi_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi.shader.version_get_shader(gi.shader_version, 0), 0);
  1528. }
  1529. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1530. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi.pipelines[0]);
  1531. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->gi_uniform_set, 0);
  1532. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GI::PushConstant));
  1533. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->width, rb->height, 1, 8, 8, 1);
  1534. RD::get_singleton()->compute_list_end();
  1535. }
  1536. RID RendererSceneRenderRD::sky_create() {
  1537. return sky_owner.make_rid(Sky());
  1538. }
  1539. void RendererSceneRenderRD::_sky_invalidate(Sky *p_sky) {
  1540. if (!p_sky->dirty) {
  1541. p_sky->dirty = true;
  1542. p_sky->dirty_list = dirty_sky_list;
  1543. dirty_sky_list = p_sky;
  1544. }
  1545. }
  1546. void RendererSceneRenderRD::sky_set_radiance_size(RID p_sky, int p_radiance_size) {
  1547. Sky *sky = sky_owner.getornull(p_sky);
  1548. ERR_FAIL_COND(!sky);
  1549. ERR_FAIL_COND(p_radiance_size < 32 || p_radiance_size > 2048);
  1550. if (sky->radiance_size == p_radiance_size) {
  1551. return;
  1552. }
  1553. sky->radiance_size = p_radiance_size;
  1554. if (sky->mode == RS::SKY_MODE_REALTIME && sky->radiance_size != 256) {
  1555. WARN_PRINT("Realtime Skies can only use a radiance size of 256. Radiance size will be set to 256 internally.");
  1556. sky->radiance_size = 256;
  1557. }
  1558. _sky_invalidate(sky);
  1559. if (sky->radiance.is_valid()) {
  1560. RD::get_singleton()->free(sky->radiance);
  1561. sky->radiance = RID();
  1562. }
  1563. _clear_reflection_data(sky->reflection);
  1564. }
  1565. void RendererSceneRenderRD::sky_set_mode(RID p_sky, RS::SkyMode p_mode) {
  1566. Sky *sky = sky_owner.getornull(p_sky);
  1567. ERR_FAIL_COND(!sky);
  1568. if (sky->mode == p_mode) {
  1569. return;
  1570. }
  1571. sky->mode = p_mode;
  1572. if (sky->mode == RS::SKY_MODE_REALTIME && sky->radiance_size != 256) {
  1573. WARN_PRINT("Realtime Skies can only use a radiance size of 256. Radiance size will be set to 256 internally.");
  1574. sky_set_radiance_size(p_sky, 256);
  1575. }
  1576. _sky_invalidate(sky);
  1577. if (sky->radiance.is_valid()) {
  1578. RD::get_singleton()->free(sky->radiance);
  1579. sky->radiance = RID();
  1580. }
  1581. _clear_reflection_data(sky->reflection);
  1582. }
  1583. void RendererSceneRenderRD::sky_set_material(RID p_sky, RID p_material) {
  1584. Sky *sky = sky_owner.getornull(p_sky);
  1585. ERR_FAIL_COND(!sky);
  1586. sky->material = p_material;
  1587. _sky_invalidate(sky);
  1588. }
  1589. Ref<Image> RendererSceneRenderRD::sky_bake_panorama(RID p_sky, float p_energy, bool p_bake_irradiance, const Size2i &p_size) {
  1590. Sky *sky = sky_owner.getornull(p_sky);
  1591. ERR_FAIL_COND_V(!sky, Ref<Image>());
  1592. _update_dirty_skys();
  1593. if (sky->radiance.is_valid()) {
  1594. RD::TextureFormat tf;
  1595. tf.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  1596. tf.width = p_size.width;
  1597. tf.height = p_size.height;
  1598. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  1599. RID rad_tex = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1600. storage->get_effects()->copy_cubemap_to_panorama(sky->radiance, rad_tex, p_size, p_bake_irradiance ? roughness_layers : 0, sky->reflection.layers.size() > 1);
  1601. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(rad_tex, 0);
  1602. RD::get_singleton()->free(rad_tex);
  1603. Ref<Image> img;
  1604. img.instance();
  1605. img->create(p_size.width, p_size.height, false, Image::FORMAT_RGBAF, data);
  1606. for (int i = 0; i < p_size.width; i++) {
  1607. for (int j = 0; j < p_size.height; j++) {
  1608. Color c = img->get_pixel(i, j);
  1609. c.r *= p_energy;
  1610. c.g *= p_energy;
  1611. c.b *= p_energy;
  1612. img->set_pixel(i, j, c);
  1613. }
  1614. }
  1615. return img;
  1616. }
  1617. return Ref<Image>();
  1618. }
  1619. void RendererSceneRenderRD::_update_dirty_skys() {
  1620. Sky *sky = dirty_sky_list;
  1621. while (sky) {
  1622. bool texture_set_dirty = false;
  1623. //update sky configuration if texture is missing
  1624. if (sky->radiance.is_null()) {
  1625. int mipmaps = Image::get_image_required_mipmaps(sky->radiance_size, sky->radiance_size, Image::FORMAT_RGBAH) + 1;
  1626. uint32_t w = sky->radiance_size, h = sky->radiance_size;
  1627. int layers = roughness_layers;
  1628. if (sky->mode == RS::SKY_MODE_REALTIME) {
  1629. layers = 8;
  1630. if (roughness_layers != 8) {
  1631. WARN_PRINT("When using REALTIME skies, roughness_layers should be set to 8 in the project settings for best quality reflections");
  1632. }
  1633. }
  1634. if (sky_use_cubemap_array) {
  1635. //array (higher quality, 6 times more memory)
  1636. RD::TextureFormat tf;
  1637. tf.array_layers = layers * 6;
  1638. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1639. tf.texture_type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  1640. tf.mipmaps = mipmaps;
  1641. tf.width = w;
  1642. tf.height = h;
  1643. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  1644. sky->radiance = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1645. _update_reflection_data(sky->reflection, sky->radiance_size, mipmaps, true, sky->radiance, 0, sky->mode == RS::SKY_MODE_REALTIME);
  1646. } else {
  1647. //regular cubemap, lower quality (aliasing, less memory)
  1648. RD::TextureFormat tf;
  1649. tf.array_layers = 6;
  1650. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1651. tf.texture_type = RD::TEXTURE_TYPE_CUBE;
  1652. tf.mipmaps = MIN(mipmaps, layers);
  1653. tf.width = w;
  1654. tf.height = h;
  1655. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  1656. sky->radiance = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1657. _update_reflection_data(sky->reflection, sky->radiance_size, MIN(mipmaps, layers), false, sky->radiance, 0, sky->mode == RS::SKY_MODE_REALTIME);
  1658. }
  1659. texture_set_dirty = true;
  1660. }
  1661. // Create subpass buffers if they haven't been created already
  1662. if (sky->half_res_pass.is_null() && !RD::get_singleton()->texture_is_valid(sky->half_res_pass) && sky->screen_size.x >= 4 && sky->screen_size.y >= 4) {
  1663. RD::TextureFormat tformat;
  1664. tformat.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1665. tformat.width = sky->screen_size.x / 2;
  1666. tformat.height = sky->screen_size.y / 2;
  1667. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1668. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  1669. sky->half_res_pass = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  1670. Vector<RID> texs;
  1671. texs.push_back(sky->half_res_pass);
  1672. sky->half_res_framebuffer = RD::get_singleton()->framebuffer_create(texs);
  1673. texture_set_dirty = true;
  1674. }
  1675. if (sky->quarter_res_pass.is_null() && !RD::get_singleton()->texture_is_valid(sky->quarter_res_pass) && sky->screen_size.x >= 4 && sky->screen_size.y >= 4) {
  1676. RD::TextureFormat tformat;
  1677. tformat.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1678. tformat.width = sky->screen_size.x / 4;
  1679. tformat.height = sky->screen_size.y / 4;
  1680. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1681. tformat.texture_type = RD::TEXTURE_TYPE_2D;
  1682. sky->quarter_res_pass = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  1683. Vector<RID> texs;
  1684. texs.push_back(sky->quarter_res_pass);
  1685. sky->quarter_res_framebuffer = RD::get_singleton()->framebuffer_create(texs);
  1686. texture_set_dirty = true;
  1687. }
  1688. if (texture_set_dirty) {
  1689. for (int i = 0; i < SKY_TEXTURE_SET_MAX; i++) {
  1690. if (sky->texture_uniform_sets[i].is_valid() && RD::get_singleton()->uniform_set_is_valid(sky->texture_uniform_sets[i])) {
  1691. RD::get_singleton()->free(sky->texture_uniform_sets[i]);
  1692. sky->texture_uniform_sets[i] = RID();
  1693. }
  1694. }
  1695. }
  1696. sky->reflection.dirty = true;
  1697. sky->processing_layer = 0;
  1698. Sky *next = sky->dirty_list;
  1699. sky->dirty_list = nullptr;
  1700. sky->dirty = false;
  1701. sky = next;
  1702. }
  1703. dirty_sky_list = nullptr;
  1704. }
  1705. RID RendererSceneRenderRD::sky_get_radiance_texture_rd(RID p_sky) const {
  1706. Sky *sky = sky_owner.getornull(p_sky);
  1707. ERR_FAIL_COND_V(!sky, RID());
  1708. return sky->radiance;
  1709. }
  1710. RID RendererSceneRenderRD::sky_get_radiance_uniform_set_rd(RID p_sky, RID p_shader, int p_set) const {
  1711. Sky *sky = sky_owner.getornull(p_sky);
  1712. ERR_FAIL_COND_V(!sky, RID());
  1713. if (sky->uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(sky->uniform_set)) {
  1714. sky->uniform_set = RID();
  1715. if (sky->radiance.is_valid()) {
  1716. Vector<RD::Uniform> uniforms;
  1717. {
  1718. RD::Uniform u;
  1719. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1720. u.binding = 0;
  1721. u.ids.push_back(sky->radiance);
  1722. uniforms.push_back(u);
  1723. }
  1724. sky->uniform_set = RD::get_singleton()->uniform_set_create(uniforms, p_shader, p_set);
  1725. }
  1726. }
  1727. return sky->uniform_set;
  1728. }
  1729. RID RendererSceneRenderRD::_get_sky_textures(Sky *p_sky, SkyTextureSetVersion p_version) {
  1730. if (p_sky->texture_uniform_sets[p_version].is_valid() && RD::get_singleton()->uniform_set_is_valid(p_sky->texture_uniform_sets[p_version])) {
  1731. return p_sky->texture_uniform_sets[p_version];
  1732. }
  1733. Vector<RD::Uniform> uniforms;
  1734. {
  1735. RD::Uniform u;
  1736. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1737. u.binding = 0;
  1738. if (p_sky->radiance.is_valid() && p_version <= SKY_TEXTURE_SET_QUARTER_RES) {
  1739. u.ids.push_back(p_sky->radiance);
  1740. } else {
  1741. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  1742. }
  1743. uniforms.push_back(u);
  1744. }
  1745. {
  1746. RD::Uniform u;
  1747. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1748. u.binding = 1; // half res
  1749. if (p_sky->half_res_pass.is_valid() && p_version != SKY_TEXTURE_SET_HALF_RES && p_version != SKY_TEXTURE_SET_CUBEMAP_HALF_RES) {
  1750. if (p_version >= SKY_TEXTURE_SET_CUBEMAP) {
  1751. u.ids.push_back(p_sky->reflection.layers[0].views[1]);
  1752. } else {
  1753. u.ids.push_back(p_sky->half_res_pass);
  1754. }
  1755. } else {
  1756. if (p_version < SKY_TEXTURE_SET_CUBEMAP) {
  1757. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_WHITE));
  1758. } else {
  1759. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  1760. }
  1761. }
  1762. uniforms.push_back(u);
  1763. }
  1764. {
  1765. RD::Uniform u;
  1766. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1767. u.binding = 2; // quarter res
  1768. if (p_sky->quarter_res_pass.is_valid() && p_version != SKY_TEXTURE_SET_QUARTER_RES && p_version != SKY_TEXTURE_SET_CUBEMAP_QUARTER_RES) {
  1769. if (p_version >= SKY_TEXTURE_SET_CUBEMAP) {
  1770. u.ids.push_back(p_sky->reflection.layers[0].views[2]);
  1771. } else {
  1772. u.ids.push_back(p_sky->quarter_res_pass);
  1773. }
  1774. } else {
  1775. if (p_version < SKY_TEXTURE_SET_CUBEMAP) {
  1776. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_WHITE));
  1777. } else {
  1778. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  1779. }
  1780. }
  1781. uniforms.push_back(u);
  1782. }
  1783. p_sky->texture_uniform_sets[p_version] = RD::get_singleton()->uniform_set_create(uniforms, sky_shader.default_shader_rd, SKY_SET_TEXTURES);
  1784. return p_sky->texture_uniform_sets[p_version];
  1785. }
  1786. RID RendererSceneRenderRD::sky_get_material(RID p_sky) const {
  1787. Sky *sky = sky_owner.getornull(p_sky);
  1788. ERR_FAIL_COND_V(!sky, RID());
  1789. return sky->material;
  1790. }
  1791. void RendererSceneRenderRD::_draw_sky(bool p_can_continue_color, bool p_can_continue_depth, RID p_fb, RID p_environment, const CameraMatrix &p_projection, const Transform &p_transform) {
  1792. ERR_FAIL_COND(!is_environment(p_environment));
  1793. SkyMaterialData *material = nullptr;
  1794. Sky *sky = sky_owner.getornull(environment_get_sky(p_environment));
  1795. RID sky_material;
  1796. RS::EnvironmentBG background = environment_get_background(p_environment);
  1797. if (!(background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) || sky) {
  1798. ERR_FAIL_COND(!sky);
  1799. sky_material = sky_get_material(environment_get_sky(p_environment));
  1800. if (sky_material.is_valid()) {
  1801. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  1802. if (!material || !material->shader_data->valid) {
  1803. material = nullptr;
  1804. }
  1805. }
  1806. if (!material) {
  1807. sky_material = sky_shader.default_material;
  1808. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  1809. }
  1810. }
  1811. if (background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) {
  1812. sky_material = sky_scene_state.fog_material;
  1813. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  1814. }
  1815. ERR_FAIL_COND(!material);
  1816. SkyShaderData *shader_data = material->shader_data;
  1817. ERR_FAIL_COND(!shader_data);
  1818. Basis sky_transform = environment_get_sky_orientation(p_environment);
  1819. sky_transform.invert();
  1820. float multiplier = environment_get_bg_energy(p_environment);
  1821. float custom_fov = environment_get_sky_custom_fov(p_environment);
  1822. // Camera
  1823. CameraMatrix camera;
  1824. if (custom_fov) {
  1825. float near_plane = p_projection.get_z_near();
  1826. float far_plane = p_projection.get_z_far();
  1827. float aspect = p_projection.get_aspect();
  1828. camera.set_perspective(custom_fov, aspect, near_plane, far_plane);
  1829. } else {
  1830. camera = p_projection;
  1831. }
  1832. sky_transform = p_transform.basis * sky_transform;
  1833. if (shader_data->uses_quarter_res) {
  1834. PipelineCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_QUARTER_RES];
  1835. RID texture_uniform_set = _get_sky_textures(sky, SKY_TEXTURE_SET_QUARTER_RES);
  1836. Vector<Color> clear_colors;
  1837. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1838. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(sky->quarter_res_framebuffer, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_DISCARD, clear_colors);
  1839. storage->get_effects()->render_sky(draw_list, time, sky->quarter_res_framebuffer, sky_scene_state.uniform_set, sky_scene_state.fog_uniform_set, pipeline, material->uniform_set, texture_uniform_set, camera, sky_transform, multiplier, p_transform.origin);
  1840. RD::get_singleton()->draw_list_end();
  1841. }
  1842. if (shader_data->uses_half_res) {
  1843. PipelineCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_HALF_RES];
  1844. RID texture_uniform_set = _get_sky_textures(sky, SKY_TEXTURE_SET_HALF_RES);
  1845. Vector<Color> clear_colors;
  1846. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1847. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(sky->half_res_framebuffer, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_DISCARD, clear_colors);
  1848. storage->get_effects()->render_sky(draw_list, time, sky->half_res_framebuffer, sky_scene_state.uniform_set, sky_scene_state.fog_uniform_set, pipeline, material->uniform_set, texture_uniform_set, camera, sky_transform, multiplier, p_transform.origin);
  1849. RD::get_singleton()->draw_list_end();
  1850. }
  1851. PipelineCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_BACKGROUND];
  1852. RID texture_uniform_set;
  1853. if (sky) {
  1854. texture_uniform_set = _get_sky_textures(sky, SKY_TEXTURE_SET_BACKGROUND);
  1855. } else {
  1856. texture_uniform_set = sky_scene_state.fog_only_texture_uniform_set;
  1857. }
  1858. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_fb, RD::INITIAL_ACTION_CONTINUE, p_can_continue_color ? RD::FINAL_ACTION_CONTINUE : RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CONTINUE, p_can_continue_depth ? RD::FINAL_ACTION_CONTINUE : RD::FINAL_ACTION_READ);
  1859. storage->get_effects()->render_sky(draw_list, time, p_fb, sky_scene_state.uniform_set, sky_scene_state.fog_uniform_set, pipeline, material->uniform_set, texture_uniform_set, camera, sky_transform, multiplier, p_transform.origin);
  1860. RD::get_singleton()->draw_list_end();
  1861. }
  1862. void RendererSceneRenderRD::_setup_sky(RID p_environment, RID p_render_buffers, const CameraMatrix &p_projection, const Transform &p_transform, const Size2i p_screen_size) {
  1863. ERR_FAIL_COND(!is_environment(p_environment));
  1864. SkyMaterialData *material = nullptr;
  1865. Sky *sky = sky_owner.getornull(environment_get_sky(p_environment));
  1866. RID sky_material;
  1867. SkyShaderData *shader_data = nullptr;
  1868. RS::EnvironmentBG background = environment_get_background(p_environment);
  1869. if (!(background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) || sky) {
  1870. ERR_FAIL_COND(!sky);
  1871. sky_material = sky_get_material(environment_get_sky(p_environment));
  1872. if (sky_material.is_valid()) {
  1873. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  1874. if (!material || !material->shader_data->valid) {
  1875. material = nullptr;
  1876. }
  1877. }
  1878. if (!material) {
  1879. sky_material = sky_shader.default_material;
  1880. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  1881. }
  1882. ERR_FAIL_COND(!material);
  1883. shader_data = material->shader_data;
  1884. ERR_FAIL_COND(!shader_data);
  1885. }
  1886. if (sky) {
  1887. // Invalidate supbass buffers if screen size changes
  1888. if (sky->screen_size != p_screen_size) {
  1889. sky->screen_size = p_screen_size;
  1890. sky->screen_size.x = sky->screen_size.x < 4 ? 4 : sky->screen_size.x;
  1891. sky->screen_size.y = sky->screen_size.y < 4 ? 4 : sky->screen_size.y;
  1892. if (shader_data->uses_half_res) {
  1893. if (sky->half_res_pass.is_valid()) {
  1894. RD::get_singleton()->free(sky->half_res_pass);
  1895. sky->half_res_pass = RID();
  1896. }
  1897. _sky_invalidate(sky);
  1898. }
  1899. if (shader_data->uses_quarter_res) {
  1900. if (sky->quarter_res_pass.is_valid()) {
  1901. RD::get_singleton()->free(sky->quarter_res_pass);
  1902. sky->quarter_res_pass = RID();
  1903. }
  1904. _sky_invalidate(sky);
  1905. }
  1906. }
  1907. // Create new subpass buffers if necessary
  1908. if ((shader_data->uses_half_res && sky->half_res_pass.is_null()) ||
  1909. (shader_data->uses_quarter_res && sky->quarter_res_pass.is_null()) ||
  1910. sky->radiance.is_null()) {
  1911. _sky_invalidate(sky);
  1912. _update_dirty_skys();
  1913. }
  1914. if (shader_data->uses_time && time - sky->prev_time > 0.00001) {
  1915. sky->prev_time = time;
  1916. sky->reflection.dirty = true;
  1917. RenderingServerDefault::redraw_request();
  1918. }
  1919. if (material != sky->prev_material) {
  1920. sky->prev_material = material;
  1921. sky->reflection.dirty = true;
  1922. }
  1923. if (material->uniform_set_updated) {
  1924. material->uniform_set_updated = false;
  1925. sky->reflection.dirty = true;
  1926. }
  1927. if (!p_transform.origin.is_equal_approx(sky->prev_position) && shader_data->uses_position) {
  1928. sky->prev_position = p_transform.origin;
  1929. sky->reflection.dirty = true;
  1930. }
  1931. if (shader_data->uses_light) {
  1932. // Check whether the directional_light_buffer changes
  1933. bool light_data_dirty = false;
  1934. if (sky_scene_state.ubo.directional_light_count != sky_scene_state.last_frame_directional_light_count) {
  1935. light_data_dirty = true;
  1936. for (uint32_t i = sky_scene_state.ubo.directional_light_count; i < sky_scene_state.max_directional_lights; i++) {
  1937. sky_scene_state.directional_lights[i].enabled = false;
  1938. }
  1939. }
  1940. if (!light_data_dirty) {
  1941. for (uint32_t i = 0; i < sky_scene_state.ubo.directional_light_count; i++) {
  1942. if (sky_scene_state.directional_lights[i].direction[0] != sky_scene_state.last_frame_directional_lights[i].direction[0] ||
  1943. sky_scene_state.directional_lights[i].direction[1] != sky_scene_state.last_frame_directional_lights[i].direction[1] ||
  1944. sky_scene_state.directional_lights[i].direction[2] != sky_scene_state.last_frame_directional_lights[i].direction[2] ||
  1945. sky_scene_state.directional_lights[i].energy != sky_scene_state.last_frame_directional_lights[i].energy ||
  1946. sky_scene_state.directional_lights[i].color[0] != sky_scene_state.last_frame_directional_lights[i].color[0] ||
  1947. sky_scene_state.directional_lights[i].color[1] != sky_scene_state.last_frame_directional_lights[i].color[1] ||
  1948. sky_scene_state.directional_lights[i].color[2] != sky_scene_state.last_frame_directional_lights[i].color[2] ||
  1949. sky_scene_state.directional_lights[i].enabled != sky_scene_state.last_frame_directional_lights[i].enabled ||
  1950. sky_scene_state.directional_lights[i].size != sky_scene_state.last_frame_directional_lights[i].size) {
  1951. light_data_dirty = true;
  1952. break;
  1953. }
  1954. }
  1955. }
  1956. if (light_data_dirty) {
  1957. RD::get_singleton()->buffer_update(sky_scene_state.directional_light_buffer, 0, sizeof(SkyDirectionalLightData) * sky_scene_state.max_directional_lights, sky_scene_state.directional_lights, true);
  1958. RendererSceneRenderRD::SkyDirectionalLightData *temp = sky_scene_state.last_frame_directional_lights;
  1959. sky_scene_state.last_frame_directional_lights = sky_scene_state.directional_lights;
  1960. sky_scene_state.directional_lights = temp;
  1961. sky_scene_state.last_frame_directional_light_count = sky_scene_state.ubo.directional_light_count;
  1962. sky->reflection.dirty = true;
  1963. }
  1964. }
  1965. }
  1966. //setup fog variables
  1967. sky_scene_state.ubo.volumetric_fog_enabled = false;
  1968. if (p_render_buffers.is_valid()) {
  1969. if (render_buffers_has_volumetric_fog(p_render_buffers)) {
  1970. sky_scene_state.ubo.volumetric_fog_enabled = true;
  1971. float fog_end = render_buffers_get_volumetric_fog_end(p_render_buffers);
  1972. if (fog_end > 0.0) {
  1973. sky_scene_state.ubo.volumetric_fog_inv_length = 1.0 / fog_end;
  1974. } else {
  1975. sky_scene_state.ubo.volumetric_fog_inv_length = 1.0;
  1976. }
  1977. float fog_detail_spread = render_buffers_get_volumetric_fog_detail_spread(p_render_buffers); //reverse lookup
  1978. if (fog_detail_spread > 0.0) {
  1979. sky_scene_state.ubo.volumetric_fog_detail_spread = 1.0 / fog_detail_spread;
  1980. } else {
  1981. sky_scene_state.ubo.volumetric_fog_detail_spread = 1.0;
  1982. }
  1983. }
  1984. RID fog_uniform_set = render_buffers_get_volumetric_fog_sky_uniform_set(p_render_buffers);
  1985. if (fog_uniform_set != RID()) {
  1986. sky_scene_state.fog_uniform_set = fog_uniform_set;
  1987. } else {
  1988. sky_scene_state.fog_uniform_set = sky_scene_state.default_fog_uniform_set;
  1989. }
  1990. }
  1991. sky_scene_state.ubo.z_far = p_projection.get_z_far();
  1992. sky_scene_state.ubo.fog_enabled = environment_is_fog_enabled(p_environment);
  1993. sky_scene_state.ubo.fog_density = environment_get_fog_density(p_environment);
  1994. sky_scene_state.ubo.fog_aerial_perspective = environment_get_fog_aerial_perspective(p_environment);
  1995. Color fog_color = environment_get_fog_light_color(p_environment).to_linear();
  1996. float fog_energy = environment_get_fog_light_energy(p_environment);
  1997. sky_scene_state.ubo.fog_light_color[0] = fog_color.r * fog_energy;
  1998. sky_scene_state.ubo.fog_light_color[1] = fog_color.g * fog_energy;
  1999. sky_scene_state.ubo.fog_light_color[2] = fog_color.b * fog_energy;
  2000. sky_scene_state.ubo.fog_sun_scatter = environment_get_fog_sun_scatter(p_environment);
  2001. RD::get_singleton()->buffer_update(sky_scene_state.uniform_buffer, 0, sizeof(SkySceneState::UBO), &sky_scene_state.ubo, true);
  2002. }
  2003. void RendererSceneRenderRD::_update_sky(RID p_environment, const CameraMatrix &p_projection, const Transform &p_transform) {
  2004. ERR_FAIL_COND(!is_environment(p_environment));
  2005. Sky *sky = sky_owner.getornull(environment_get_sky(p_environment));
  2006. ERR_FAIL_COND(!sky);
  2007. RID sky_material = sky_get_material(environment_get_sky(p_environment));
  2008. SkyMaterialData *material = nullptr;
  2009. if (sky_material.is_valid()) {
  2010. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  2011. if (!material || !material->shader_data->valid) {
  2012. material = nullptr;
  2013. }
  2014. }
  2015. if (!material) {
  2016. sky_material = sky_shader.default_material;
  2017. material = (SkyMaterialData *)storage->material_get_data(sky_material, RendererStorageRD::SHADER_TYPE_SKY);
  2018. }
  2019. ERR_FAIL_COND(!material);
  2020. SkyShaderData *shader_data = material->shader_data;
  2021. ERR_FAIL_COND(!shader_data);
  2022. float multiplier = environment_get_bg_energy(p_environment);
  2023. bool update_single_frame = sky->mode == RS::SKY_MODE_REALTIME || sky->mode == RS::SKY_MODE_QUALITY;
  2024. RS::SkyMode sky_mode = sky->mode;
  2025. if (sky_mode == RS::SKY_MODE_AUTOMATIC) {
  2026. if (shader_data->uses_time || shader_data->uses_position) {
  2027. update_single_frame = true;
  2028. sky_mode = RS::SKY_MODE_REALTIME;
  2029. } else if (shader_data->uses_light || shader_data->ubo_size > 0) {
  2030. update_single_frame = false;
  2031. sky_mode = RS::SKY_MODE_INCREMENTAL;
  2032. } else {
  2033. update_single_frame = true;
  2034. sky_mode = RS::SKY_MODE_QUALITY;
  2035. }
  2036. }
  2037. if (sky->processing_layer == 0 && sky_mode == RS::SKY_MODE_INCREMENTAL) {
  2038. // On the first frame after creating sky, rebuild in single frame
  2039. update_single_frame = true;
  2040. sky_mode = RS::SKY_MODE_QUALITY;
  2041. }
  2042. int max_processing_layer = sky_use_cubemap_array ? sky->reflection.layers.size() : sky->reflection.layers[0].mipmaps.size();
  2043. // Update radiance cubemap
  2044. if (sky->reflection.dirty && (sky->processing_layer >= max_processing_layer || update_single_frame)) {
  2045. static const Vector3 view_normals[6] = {
  2046. Vector3(+1, 0, 0),
  2047. Vector3(-1, 0, 0),
  2048. Vector3(0, +1, 0),
  2049. Vector3(0, -1, 0),
  2050. Vector3(0, 0, +1),
  2051. Vector3(0, 0, -1)
  2052. };
  2053. static const Vector3 view_up[6] = {
  2054. Vector3(0, -1, 0),
  2055. Vector3(0, -1, 0),
  2056. Vector3(0, 0, +1),
  2057. Vector3(0, 0, -1),
  2058. Vector3(0, -1, 0),
  2059. Vector3(0, -1, 0)
  2060. };
  2061. CameraMatrix cm;
  2062. cm.set_perspective(90, 1, 0.01, 10.0);
  2063. CameraMatrix correction;
  2064. correction.set_depth_correction(true);
  2065. cm = correction * cm;
  2066. if (shader_data->uses_quarter_res) {
  2067. PipelineCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_CUBEMAP_QUARTER_RES];
  2068. Vector<Color> clear_colors;
  2069. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  2070. RD::DrawListID cubemap_draw_list;
  2071. for (int i = 0; i < 6; i++) {
  2072. Transform local_view;
  2073. local_view.set_look_at(Vector3(0, 0, 0), view_normals[i], view_up[i]);
  2074. RID texture_uniform_set = _get_sky_textures(sky, SKY_TEXTURE_SET_CUBEMAP_QUARTER_RES);
  2075. cubemap_draw_list = RD::get_singleton()->draw_list_begin(sky->reflection.layers[0].mipmaps[2].framebuffers[i], RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  2076. storage->get_effects()->render_sky(cubemap_draw_list, time, sky->reflection.layers[0].mipmaps[2].framebuffers[i], sky_scene_state.uniform_set, sky_scene_state.fog_uniform_set, pipeline, material->uniform_set, texture_uniform_set, cm, local_view.basis, multiplier, p_transform.origin);
  2077. RD::get_singleton()->draw_list_end();
  2078. }
  2079. }
  2080. if (shader_data->uses_half_res) {
  2081. PipelineCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_CUBEMAP_HALF_RES];
  2082. Vector<Color> clear_colors;
  2083. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  2084. RD::DrawListID cubemap_draw_list;
  2085. for (int i = 0; i < 6; i++) {
  2086. Transform local_view;
  2087. local_view.set_look_at(Vector3(0, 0, 0), view_normals[i], view_up[i]);
  2088. RID texture_uniform_set = _get_sky_textures(sky, SKY_TEXTURE_SET_CUBEMAP_HALF_RES);
  2089. cubemap_draw_list = RD::get_singleton()->draw_list_begin(sky->reflection.layers[0].mipmaps[1].framebuffers[i], RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  2090. storage->get_effects()->render_sky(cubemap_draw_list, time, sky->reflection.layers[0].mipmaps[1].framebuffers[i], sky_scene_state.uniform_set, sky_scene_state.fog_uniform_set, pipeline, material->uniform_set, texture_uniform_set, cm, local_view.basis, multiplier, p_transform.origin);
  2091. RD::get_singleton()->draw_list_end();
  2092. }
  2093. }
  2094. RD::DrawListID cubemap_draw_list;
  2095. PipelineCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_CUBEMAP];
  2096. for (int i = 0; i < 6; i++) {
  2097. Transform local_view;
  2098. local_view.set_look_at(Vector3(0, 0, 0), view_normals[i], view_up[i]);
  2099. RID texture_uniform_set = _get_sky_textures(sky, SKY_TEXTURE_SET_CUBEMAP);
  2100. cubemap_draw_list = RD::get_singleton()->draw_list_begin(sky->reflection.layers[0].mipmaps[0].framebuffers[i], RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD);
  2101. storage->get_effects()->render_sky(cubemap_draw_list, time, sky->reflection.layers[0].mipmaps[0].framebuffers[i], sky_scene_state.uniform_set, sky_scene_state.fog_uniform_set, pipeline, material->uniform_set, texture_uniform_set, cm, local_view.basis, multiplier, p_transform.origin);
  2102. RD::get_singleton()->draw_list_end();
  2103. }
  2104. if (sky_mode == RS::SKY_MODE_REALTIME) {
  2105. _create_reflection_fast_filter(sky->reflection, sky_use_cubemap_array);
  2106. if (sky_use_cubemap_array) {
  2107. _update_reflection_mipmaps(sky->reflection, 0, sky->reflection.layers.size());
  2108. }
  2109. } else {
  2110. if (update_single_frame) {
  2111. for (int i = 1; i < max_processing_layer; i++) {
  2112. _create_reflection_importance_sample(sky->reflection, sky_use_cubemap_array, 10, i);
  2113. }
  2114. if (sky_use_cubemap_array) {
  2115. _update_reflection_mipmaps(sky->reflection, 0, sky->reflection.layers.size());
  2116. }
  2117. } else {
  2118. if (sky_use_cubemap_array) {
  2119. // Multi-Frame so just update the first array level
  2120. _update_reflection_mipmaps(sky->reflection, 0, 1);
  2121. }
  2122. }
  2123. sky->processing_layer = 1;
  2124. }
  2125. sky->reflection.dirty = false;
  2126. } else {
  2127. if (sky_mode == RS::SKY_MODE_INCREMENTAL && sky->processing_layer < max_processing_layer) {
  2128. _create_reflection_importance_sample(sky->reflection, sky_use_cubemap_array, 10, sky->processing_layer);
  2129. if (sky_use_cubemap_array) {
  2130. _update_reflection_mipmaps(sky->reflection, sky->processing_layer, sky->processing_layer + 1);
  2131. }
  2132. sky->processing_layer++;
  2133. }
  2134. }
  2135. }
  2136. /* SKY SHADER */
  2137. void RendererSceneRenderRD::SkyShaderData::set_code(const String &p_code) {
  2138. //compile
  2139. code = p_code;
  2140. valid = false;
  2141. ubo_size = 0;
  2142. uniforms.clear();
  2143. if (code == String()) {
  2144. return; //just invalid, but no error
  2145. }
  2146. ShaderCompilerRD::GeneratedCode gen_code;
  2147. ShaderCompilerRD::IdentifierActions actions;
  2148. uses_time = false;
  2149. uses_half_res = false;
  2150. uses_quarter_res = false;
  2151. uses_position = false;
  2152. uses_light = false;
  2153. actions.render_mode_flags["use_half_res_pass"] = &uses_half_res;
  2154. actions.render_mode_flags["use_quarter_res_pass"] = &uses_quarter_res;
  2155. actions.usage_flag_pointers["TIME"] = &uses_time;
  2156. actions.usage_flag_pointers["POSITION"] = &uses_position;
  2157. actions.usage_flag_pointers["LIGHT0_ENABLED"] = &uses_light;
  2158. actions.usage_flag_pointers["LIGHT0_ENERGY"] = &uses_light;
  2159. actions.usage_flag_pointers["LIGHT0_DIRECTION"] = &uses_light;
  2160. actions.usage_flag_pointers["LIGHT0_COLOR"] = &uses_light;
  2161. actions.usage_flag_pointers["LIGHT0_SIZE"] = &uses_light;
  2162. actions.usage_flag_pointers["LIGHT1_ENABLED"] = &uses_light;
  2163. actions.usage_flag_pointers["LIGHT1_ENERGY"] = &uses_light;
  2164. actions.usage_flag_pointers["LIGHT1_DIRECTION"] = &uses_light;
  2165. actions.usage_flag_pointers["LIGHT1_COLOR"] = &uses_light;
  2166. actions.usage_flag_pointers["LIGHT1_SIZE"] = &uses_light;
  2167. actions.usage_flag_pointers["LIGHT2_ENABLED"] = &uses_light;
  2168. actions.usage_flag_pointers["LIGHT2_ENERGY"] = &uses_light;
  2169. actions.usage_flag_pointers["LIGHT2_DIRECTION"] = &uses_light;
  2170. actions.usage_flag_pointers["LIGHT2_COLOR"] = &uses_light;
  2171. actions.usage_flag_pointers["LIGHT2_SIZE"] = &uses_light;
  2172. actions.usage_flag_pointers["LIGHT3_ENABLED"] = &uses_light;
  2173. actions.usage_flag_pointers["LIGHT3_ENERGY"] = &uses_light;
  2174. actions.usage_flag_pointers["LIGHT3_DIRECTION"] = &uses_light;
  2175. actions.usage_flag_pointers["LIGHT3_COLOR"] = &uses_light;
  2176. actions.usage_flag_pointers["LIGHT3_SIZE"] = &uses_light;
  2177. actions.uniforms = &uniforms;
  2178. RendererSceneRenderRD *scene_singleton = (RendererSceneRenderRD *)RendererSceneRenderRD::singleton;
  2179. Error err = scene_singleton->sky_shader.compiler.compile(RS::SHADER_SKY, code, &actions, path, gen_code);
  2180. ERR_FAIL_COND(err != OK);
  2181. if (version.is_null()) {
  2182. version = scene_singleton->sky_shader.shader.version_create();
  2183. }
  2184. #if 0
  2185. print_line("**compiling shader:");
  2186. print_line("**defines:\n");
  2187. for (int i = 0; i < gen_code.defines.size(); i++) {
  2188. print_line(gen_code.defines[i]);
  2189. }
  2190. print_line("\n**uniforms:\n" + gen_code.uniforms);
  2191. // print_line("\n**vertex_globals:\n" + gen_code.vertex_global);
  2192. // print_line("\n**vertex_code:\n" + gen_code.vertex);
  2193. print_line("\n**fragment_globals:\n" + gen_code.fragment_global);
  2194. print_line("\n**fragment_code:\n" + gen_code.fragment);
  2195. print_line("\n**light_code:\n" + gen_code.light);
  2196. #endif
  2197. scene_singleton->sky_shader.shader.version_set_code(version, gen_code.uniforms, gen_code.vertex_global, gen_code.vertex, gen_code.fragment_global, gen_code.light, gen_code.fragment, gen_code.defines);
  2198. ERR_FAIL_COND(!scene_singleton->sky_shader.shader.version_is_valid(version));
  2199. ubo_size = gen_code.uniform_total_size;
  2200. ubo_offsets = gen_code.uniform_offsets;
  2201. texture_uniforms = gen_code.texture_uniforms;
  2202. //update pipelines
  2203. for (int i = 0; i < SKY_VERSION_MAX; i++) {
  2204. RD::PipelineDepthStencilState depth_stencil_state;
  2205. depth_stencil_state.enable_depth_test = true;
  2206. depth_stencil_state.depth_compare_operator = RD::COMPARE_OP_LESS_OR_EQUAL;
  2207. RID shader_variant = scene_singleton->sky_shader.shader.version_get_shader(version, i);
  2208. pipelines[i].setup(shader_variant, RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), depth_stencil_state, RD::PipelineColorBlendState::create_disabled(), 0);
  2209. }
  2210. valid = true;
  2211. }
  2212. void RendererSceneRenderRD::SkyShaderData::set_default_texture_param(const StringName &p_name, RID p_texture) {
  2213. if (!p_texture.is_valid()) {
  2214. default_texture_params.erase(p_name);
  2215. } else {
  2216. default_texture_params[p_name] = p_texture;
  2217. }
  2218. }
  2219. void RendererSceneRenderRD::SkyShaderData::get_param_list(List<PropertyInfo> *p_param_list) const {
  2220. Map<int, StringName> order;
  2221. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = uniforms.front(); E; E = E->next()) {
  2222. if (E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL || E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  2223. continue;
  2224. }
  2225. if (E->get().texture_order >= 0) {
  2226. order[E->get().texture_order + 100000] = E->key();
  2227. } else {
  2228. order[E->get().order] = E->key();
  2229. }
  2230. }
  2231. for (Map<int, StringName>::Element *E = order.front(); E; E = E->next()) {
  2232. PropertyInfo pi = ShaderLanguage::uniform_to_property_info(uniforms[E->get()]);
  2233. pi.name = E->get();
  2234. p_param_list->push_back(pi);
  2235. }
  2236. }
  2237. void RendererSceneRenderRD::SkyShaderData::get_instance_param_list(List<RendererStorage::InstanceShaderParam> *p_param_list) const {
  2238. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = uniforms.front(); E; E = E->next()) {
  2239. if (E->get().scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  2240. continue;
  2241. }
  2242. RendererStorage::InstanceShaderParam p;
  2243. p.info = ShaderLanguage::uniform_to_property_info(E->get());
  2244. p.info.name = E->key(); //supply name
  2245. p.index = E->get().instance_index;
  2246. p.default_value = ShaderLanguage::constant_value_to_variant(E->get().default_value, E->get().type, E->get().hint);
  2247. p_param_list->push_back(p);
  2248. }
  2249. }
  2250. bool RendererSceneRenderRD::SkyShaderData::is_param_texture(const StringName &p_param) const {
  2251. if (!uniforms.has(p_param)) {
  2252. return false;
  2253. }
  2254. return uniforms[p_param].texture_order >= 0;
  2255. }
  2256. bool RendererSceneRenderRD::SkyShaderData::is_animated() const {
  2257. return false;
  2258. }
  2259. bool RendererSceneRenderRD::SkyShaderData::casts_shadows() const {
  2260. return false;
  2261. }
  2262. Variant RendererSceneRenderRD::SkyShaderData::get_default_parameter(const StringName &p_parameter) const {
  2263. if (uniforms.has(p_parameter)) {
  2264. ShaderLanguage::ShaderNode::Uniform uniform = uniforms[p_parameter];
  2265. Vector<ShaderLanguage::ConstantNode::Value> default_value = uniform.default_value;
  2266. return ShaderLanguage::constant_value_to_variant(default_value, uniform.type, uniform.hint);
  2267. }
  2268. return Variant();
  2269. }
  2270. RendererSceneRenderRD::SkyShaderData::SkyShaderData() {
  2271. valid = false;
  2272. }
  2273. RendererSceneRenderRD::SkyShaderData::~SkyShaderData() {
  2274. RendererSceneRenderRD *scene_singleton = (RendererSceneRenderRD *)RendererSceneRenderRD::singleton;
  2275. ERR_FAIL_COND(!scene_singleton);
  2276. //pipeline variants will clear themselves if shader is gone
  2277. if (version.is_valid()) {
  2278. scene_singleton->sky_shader.shader.version_free(version);
  2279. }
  2280. }
  2281. RendererStorageRD::ShaderData *RendererSceneRenderRD::_create_sky_shader_func() {
  2282. SkyShaderData *shader_data = memnew(SkyShaderData);
  2283. return shader_data;
  2284. }
  2285. void RendererSceneRenderRD::SkyMaterialData::update_parameters(const Map<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) {
  2286. RendererSceneRenderRD *scene_singleton = (RendererSceneRenderRD *)RendererSceneRenderRD::singleton;
  2287. uniform_set_updated = true;
  2288. if ((uint32_t)ubo_data.size() != shader_data->ubo_size) {
  2289. p_uniform_dirty = true;
  2290. if (uniform_buffer.is_valid()) {
  2291. RD::get_singleton()->free(uniform_buffer);
  2292. uniform_buffer = RID();
  2293. }
  2294. ubo_data.resize(shader_data->ubo_size);
  2295. if (ubo_data.size()) {
  2296. uniform_buffer = RD::get_singleton()->uniform_buffer_create(ubo_data.size());
  2297. memset(ubo_data.ptrw(), 0, ubo_data.size()); //clear
  2298. }
  2299. //clear previous uniform set
  2300. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  2301. RD::get_singleton()->free(uniform_set);
  2302. uniform_set = RID();
  2303. }
  2304. }
  2305. //check whether buffer changed
  2306. if (p_uniform_dirty && ubo_data.size()) {
  2307. update_uniform_buffer(shader_data->uniforms, shader_data->ubo_offsets.ptr(), p_parameters, ubo_data.ptrw(), ubo_data.size(), false);
  2308. RD::get_singleton()->buffer_update(uniform_buffer, 0, ubo_data.size(), ubo_data.ptrw());
  2309. }
  2310. uint32_t tex_uniform_count = shader_data->texture_uniforms.size();
  2311. if ((uint32_t)texture_cache.size() != tex_uniform_count) {
  2312. texture_cache.resize(tex_uniform_count);
  2313. p_textures_dirty = true;
  2314. //clear previous uniform set
  2315. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  2316. RD::get_singleton()->free(uniform_set);
  2317. uniform_set = RID();
  2318. }
  2319. }
  2320. if (p_textures_dirty && tex_uniform_count) {
  2321. update_textures(p_parameters, shader_data->default_texture_params, shader_data->texture_uniforms, texture_cache.ptrw(), true);
  2322. }
  2323. if (shader_data->ubo_size == 0 && shader_data->texture_uniforms.size() == 0) {
  2324. // This material does not require an uniform set, so don't create it.
  2325. return;
  2326. }
  2327. if (!p_textures_dirty && uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  2328. //no reason to update uniform set, only UBO (or nothing) was needed to update
  2329. return;
  2330. }
  2331. Vector<RD::Uniform> uniforms;
  2332. {
  2333. if (shader_data->ubo_size) {
  2334. RD::Uniform u;
  2335. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  2336. u.binding = 0;
  2337. u.ids.push_back(uniform_buffer);
  2338. uniforms.push_back(u);
  2339. }
  2340. const RID *textures = texture_cache.ptrw();
  2341. for (uint32_t i = 0; i < tex_uniform_count; i++) {
  2342. RD::Uniform u;
  2343. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  2344. u.binding = 1 + i;
  2345. u.ids.push_back(textures[i]);
  2346. uniforms.push_back(u);
  2347. }
  2348. }
  2349. uniform_set = RD::get_singleton()->uniform_set_create(uniforms, scene_singleton->sky_shader.shader.version_get_shader(shader_data->version, 0), SKY_SET_MATERIAL);
  2350. }
  2351. RendererSceneRenderRD::SkyMaterialData::~SkyMaterialData() {
  2352. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  2353. RD::get_singleton()->free(uniform_set);
  2354. }
  2355. if (uniform_buffer.is_valid()) {
  2356. RD::get_singleton()->free(uniform_buffer);
  2357. }
  2358. }
  2359. RendererStorageRD::MaterialData *RendererSceneRenderRD::_create_sky_material_func(SkyShaderData *p_shader) {
  2360. SkyMaterialData *material_data = memnew(SkyMaterialData);
  2361. material_data->shader_data = p_shader;
  2362. material_data->last_frame = false;
  2363. //update will happen later anyway so do nothing.
  2364. return material_data;
  2365. }
  2366. RID RendererSceneRenderRD::environment_create() {
  2367. return environment_owner.make_rid(Environment());
  2368. }
  2369. void RendererSceneRenderRD::environment_set_background(RID p_env, RS::EnvironmentBG p_bg) {
  2370. Environment *env = environment_owner.getornull(p_env);
  2371. ERR_FAIL_COND(!env);
  2372. env->background = p_bg;
  2373. }
  2374. void RendererSceneRenderRD::environment_set_sky(RID p_env, RID p_sky) {
  2375. Environment *env = environment_owner.getornull(p_env);
  2376. ERR_FAIL_COND(!env);
  2377. env->sky = p_sky;
  2378. }
  2379. void RendererSceneRenderRD::environment_set_sky_custom_fov(RID p_env, float p_scale) {
  2380. Environment *env = environment_owner.getornull(p_env);
  2381. ERR_FAIL_COND(!env);
  2382. env->sky_custom_fov = p_scale;
  2383. }
  2384. void RendererSceneRenderRD::environment_set_sky_orientation(RID p_env, const Basis &p_orientation) {
  2385. Environment *env = environment_owner.getornull(p_env);
  2386. ERR_FAIL_COND(!env);
  2387. env->sky_orientation = p_orientation;
  2388. }
  2389. void RendererSceneRenderRD::environment_set_bg_color(RID p_env, const Color &p_color) {
  2390. Environment *env = environment_owner.getornull(p_env);
  2391. ERR_FAIL_COND(!env);
  2392. env->bg_color = p_color;
  2393. }
  2394. void RendererSceneRenderRD::environment_set_bg_energy(RID p_env, float p_energy) {
  2395. Environment *env = environment_owner.getornull(p_env);
  2396. ERR_FAIL_COND(!env);
  2397. env->bg_energy = p_energy;
  2398. }
  2399. void RendererSceneRenderRD::environment_set_canvas_max_layer(RID p_env, int p_max_layer) {
  2400. Environment *env = environment_owner.getornull(p_env);
  2401. ERR_FAIL_COND(!env);
  2402. env->canvas_max_layer = p_max_layer;
  2403. }
  2404. void RendererSceneRenderRD::environment_set_ambient_light(RID p_env, const Color &p_color, RS::EnvironmentAmbientSource p_ambient, float p_energy, float p_sky_contribution, RS::EnvironmentReflectionSource p_reflection_source, const Color &p_ao_color) {
  2405. Environment *env = environment_owner.getornull(p_env);
  2406. ERR_FAIL_COND(!env);
  2407. env->ambient_light = p_color;
  2408. env->ambient_source = p_ambient;
  2409. env->ambient_light_energy = p_energy;
  2410. env->ambient_sky_contribution = p_sky_contribution;
  2411. env->reflection_source = p_reflection_source;
  2412. env->ao_color = p_ao_color;
  2413. }
  2414. RS::EnvironmentBG RendererSceneRenderRD::environment_get_background(RID p_env) const {
  2415. Environment *env = environment_owner.getornull(p_env);
  2416. ERR_FAIL_COND_V(!env, RS::ENV_BG_MAX);
  2417. return env->background;
  2418. }
  2419. RID RendererSceneRenderRD::environment_get_sky(RID p_env) const {
  2420. Environment *env = environment_owner.getornull(p_env);
  2421. ERR_FAIL_COND_V(!env, RID());
  2422. return env->sky;
  2423. }
  2424. float RendererSceneRenderRD::environment_get_sky_custom_fov(RID p_env) const {
  2425. Environment *env = environment_owner.getornull(p_env);
  2426. ERR_FAIL_COND_V(!env, 0);
  2427. return env->sky_custom_fov;
  2428. }
  2429. Basis RendererSceneRenderRD::environment_get_sky_orientation(RID p_env) const {
  2430. Environment *env = environment_owner.getornull(p_env);
  2431. ERR_FAIL_COND_V(!env, Basis());
  2432. return env->sky_orientation;
  2433. }
  2434. Color RendererSceneRenderRD::environment_get_bg_color(RID p_env) const {
  2435. Environment *env = environment_owner.getornull(p_env);
  2436. ERR_FAIL_COND_V(!env, Color());
  2437. return env->bg_color;
  2438. }
  2439. float RendererSceneRenderRD::environment_get_bg_energy(RID p_env) const {
  2440. Environment *env = environment_owner.getornull(p_env);
  2441. ERR_FAIL_COND_V(!env, 0);
  2442. return env->bg_energy;
  2443. }
  2444. int RendererSceneRenderRD::environment_get_canvas_max_layer(RID p_env) const {
  2445. Environment *env = environment_owner.getornull(p_env);
  2446. ERR_FAIL_COND_V(!env, 0);
  2447. return env->canvas_max_layer;
  2448. }
  2449. Color RendererSceneRenderRD::environment_get_ambient_light_color(RID p_env) const {
  2450. Environment *env = environment_owner.getornull(p_env);
  2451. ERR_FAIL_COND_V(!env, Color());
  2452. return env->ambient_light;
  2453. }
  2454. RS::EnvironmentAmbientSource RendererSceneRenderRD::environment_get_ambient_source(RID p_env) const {
  2455. Environment *env = environment_owner.getornull(p_env);
  2456. ERR_FAIL_COND_V(!env, RS::ENV_AMBIENT_SOURCE_BG);
  2457. return env->ambient_source;
  2458. }
  2459. float RendererSceneRenderRD::environment_get_ambient_light_energy(RID p_env) const {
  2460. Environment *env = environment_owner.getornull(p_env);
  2461. ERR_FAIL_COND_V(!env, 0);
  2462. return env->ambient_light_energy;
  2463. }
  2464. float RendererSceneRenderRD::environment_get_ambient_sky_contribution(RID p_env) const {
  2465. Environment *env = environment_owner.getornull(p_env);
  2466. ERR_FAIL_COND_V(!env, 0);
  2467. return env->ambient_sky_contribution;
  2468. }
  2469. RS::EnvironmentReflectionSource RendererSceneRenderRD::environment_get_reflection_source(RID p_env) const {
  2470. Environment *env = environment_owner.getornull(p_env);
  2471. ERR_FAIL_COND_V(!env, RS::ENV_REFLECTION_SOURCE_DISABLED);
  2472. return env->reflection_source;
  2473. }
  2474. Color RendererSceneRenderRD::environment_get_ao_color(RID p_env) const {
  2475. Environment *env = environment_owner.getornull(p_env);
  2476. ERR_FAIL_COND_V(!env, Color());
  2477. return env->ao_color;
  2478. }
  2479. void RendererSceneRenderRD::environment_set_tonemap(RID p_env, RS::EnvironmentToneMapper p_tone_mapper, float p_exposure, float p_white, bool p_auto_exposure, float p_min_luminance, float p_max_luminance, float p_auto_exp_speed, float p_auto_exp_scale) {
  2480. Environment *env = environment_owner.getornull(p_env);
  2481. ERR_FAIL_COND(!env);
  2482. env->exposure = p_exposure;
  2483. env->tone_mapper = p_tone_mapper;
  2484. if (!env->auto_exposure && p_auto_exposure) {
  2485. env->auto_exposure_version = ++auto_exposure_counter;
  2486. }
  2487. env->auto_exposure = p_auto_exposure;
  2488. env->white = p_white;
  2489. env->min_luminance = p_min_luminance;
  2490. env->max_luminance = p_max_luminance;
  2491. env->auto_exp_speed = p_auto_exp_speed;
  2492. env->auto_exp_scale = p_auto_exp_scale;
  2493. }
  2494. void RendererSceneRenderRD::environment_set_glow(RID p_env, bool p_enable, Vector<float> p_levels, float p_intensity, float p_strength, float p_mix, float p_bloom_threshold, RS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, float p_hdr_luminance_cap) {
  2495. Environment *env = environment_owner.getornull(p_env);
  2496. ERR_FAIL_COND(!env);
  2497. ERR_FAIL_COND_MSG(p_levels.size() != 7, "Size of array of glow levels must be 7");
  2498. env->glow_enabled = p_enable;
  2499. env->glow_levels = p_levels;
  2500. env->glow_intensity = p_intensity;
  2501. env->glow_strength = p_strength;
  2502. env->glow_mix = p_mix;
  2503. env->glow_bloom = p_bloom_threshold;
  2504. env->glow_blend_mode = p_blend_mode;
  2505. env->glow_hdr_bleed_threshold = p_hdr_bleed_threshold;
  2506. env->glow_hdr_bleed_scale = p_hdr_bleed_scale;
  2507. env->glow_hdr_luminance_cap = p_hdr_luminance_cap;
  2508. }
  2509. void RendererSceneRenderRD::environment_glow_set_use_bicubic_upscale(bool p_enable) {
  2510. glow_bicubic_upscale = p_enable;
  2511. }
  2512. void RendererSceneRenderRD::environment_glow_set_use_high_quality(bool p_enable) {
  2513. glow_high_quality = p_enable;
  2514. }
  2515. void RendererSceneRenderRD::environment_set_sdfgi(RID p_env, bool p_enable, RS::EnvironmentSDFGICascades p_cascades, float p_min_cell_size, RS::EnvironmentSDFGIYScale p_y_scale, bool p_use_occlusion, bool p_use_multibounce, bool p_read_sky, float p_energy, float p_normal_bias, float p_probe_bias) {
  2516. Environment *env = environment_owner.getornull(p_env);
  2517. ERR_FAIL_COND(!env);
  2518. if (low_end) {
  2519. return;
  2520. }
  2521. env->sdfgi_enabled = p_enable;
  2522. env->sdfgi_cascades = p_cascades;
  2523. env->sdfgi_min_cell_size = p_min_cell_size;
  2524. env->sdfgi_use_occlusion = p_use_occlusion;
  2525. env->sdfgi_use_multibounce = p_use_multibounce;
  2526. env->sdfgi_read_sky_light = p_read_sky;
  2527. env->sdfgi_energy = p_energy;
  2528. env->sdfgi_normal_bias = p_normal_bias;
  2529. env->sdfgi_probe_bias = p_probe_bias;
  2530. env->sdfgi_y_scale = p_y_scale;
  2531. }
  2532. void RendererSceneRenderRD::environment_set_fog(RID p_env, bool p_enable, const Color &p_light_color, float p_light_energy, float p_sun_scatter, float p_density, float p_height, float p_height_density, float p_fog_aerial_perspective) {
  2533. Environment *env = environment_owner.getornull(p_env);
  2534. ERR_FAIL_COND(!env);
  2535. env->fog_enabled = p_enable;
  2536. env->fog_light_color = p_light_color;
  2537. env->fog_light_energy = p_light_energy;
  2538. env->fog_sun_scatter = p_sun_scatter;
  2539. env->fog_density = p_density;
  2540. env->fog_height = p_height;
  2541. env->fog_height_density = p_height_density;
  2542. env->fog_aerial_perspective = p_fog_aerial_perspective;
  2543. }
  2544. bool RendererSceneRenderRD::environment_is_fog_enabled(RID p_env) const {
  2545. const Environment *env = environment_owner.getornull(p_env);
  2546. ERR_FAIL_COND_V(!env, false);
  2547. return env->fog_enabled;
  2548. }
  2549. Color RendererSceneRenderRD::environment_get_fog_light_color(RID p_env) const {
  2550. const Environment *env = environment_owner.getornull(p_env);
  2551. ERR_FAIL_COND_V(!env, Color());
  2552. return env->fog_light_color;
  2553. }
  2554. float RendererSceneRenderRD::environment_get_fog_light_energy(RID p_env) const {
  2555. const Environment *env = environment_owner.getornull(p_env);
  2556. ERR_FAIL_COND_V(!env, 0);
  2557. return env->fog_light_energy;
  2558. }
  2559. float RendererSceneRenderRD::environment_get_fog_sun_scatter(RID p_env) const {
  2560. const Environment *env = environment_owner.getornull(p_env);
  2561. ERR_FAIL_COND_V(!env, 0);
  2562. return env->fog_sun_scatter;
  2563. }
  2564. float RendererSceneRenderRD::environment_get_fog_density(RID p_env) const {
  2565. const Environment *env = environment_owner.getornull(p_env);
  2566. ERR_FAIL_COND_V(!env, 0);
  2567. return env->fog_density;
  2568. }
  2569. float RendererSceneRenderRD::environment_get_fog_height(RID p_env) const {
  2570. const Environment *env = environment_owner.getornull(p_env);
  2571. ERR_FAIL_COND_V(!env, 0);
  2572. return env->fog_height;
  2573. }
  2574. float RendererSceneRenderRD::environment_get_fog_height_density(RID p_env) const {
  2575. const Environment *env = environment_owner.getornull(p_env);
  2576. ERR_FAIL_COND_V(!env, 0);
  2577. return env->fog_height_density;
  2578. }
  2579. float RendererSceneRenderRD::environment_get_fog_aerial_perspective(RID p_env) const {
  2580. const Environment *env = environment_owner.getornull(p_env);
  2581. ERR_FAIL_COND_V(!env, 0);
  2582. return env->fog_aerial_perspective;
  2583. }
  2584. void RendererSceneRenderRD::environment_set_volumetric_fog(RID p_env, bool p_enable, float p_density, const Color &p_light, float p_light_energy, float p_length, float p_detail_spread, float p_gi_inject, RenderingServer::EnvVolumetricFogShadowFilter p_shadow_filter) {
  2585. Environment *env = environment_owner.getornull(p_env);
  2586. ERR_FAIL_COND(!env);
  2587. if (low_end) {
  2588. return;
  2589. }
  2590. env->volumetric_fog_enabled = p_enable;
  2591. env->volumetric_fog_density = p_density;
  2592. env->volumetric_fog_light = p_light;
  2593. env->volumetric_fog_light_energy = p_light_energy;
  2594. env->volumetric_fog_length = p_length;
  2595. env->volumetric_fog_detail_spread = p_detail_spread;
  2596. env->volumetric_fog_shadow_filter = p_shadow_filter;
  2597. env->volumetric_fog_gi_inject = p_gi_inject;
  2598. }
  2599. void RendererSceneRenderRD::environment_set_volumetric_fog_volume_size(int p_size, int p_depth) {
  2600. volumetric_fog_size = p_size;
  2601. volumetric_fog_depth = p_depth;
  2602. }
  2603. void RendererSceneRenderRD::environment_set_volumetric_fog_filter_active(bool p_enable) {
  2604. volumetric_fog_filter_active = p_enable;
  2605. }
  2606. void RendererSceneRenderRD::environment_set_volumetric_fog_directional_shadow_shrink_size(int p_shrink_size) {
  2607. p_shrink_size = nearest_power_of_2_templated(p_shrink_size);
  2608. if (volumetric_fog_directional_shadow_shrink == (uint32_t)p_shrink_size) {
  2609. return;
  2610. }
  2611. _clear_shadow_shrink_stages(directional_shadow.shrink_stages);
  2612. }
  2613. void RendererSceneRenderRD::environment_set_volumetric_fog_positional_shadow_shrink_size(int p_shrink_size) {
  2614. p_shrink_size = nearest_power_of_2_templated(p_shrink_size);
  2615. if (volumetric_fog_positional_shadow_shrink == (uint32_t)p_shrink_size) {
  2616. return;
  2617. }
  2618. for (uint32_t i = 0; i < shadow_atlas_owner.get_rid_count(); i++) {
  2619. ShadowAtlas *sa = shadow_atlas_owner.get_ptr_by_index(i);
  2620. _clear_shadow_shrink_stages(sa->shrink_stages);
  2621. }
  2622. }
  2623. void RendererSceneRenderRD::environment_set_sdfgi_ray_count(RS::EnvironmentSDFGIRayCount p_ray_count) {
  2624. sdfgi_ray_count = p_ray_count;
  2625. }
  2626. void RendererSceneRenderRD::environment_set_sdfgi_frames_to_converge(RS::EnvironmentSDFGIFramesToConverge p_frames) {
  2627. sdfgi_frames_to_converge = p_frames;
  2628. }
  2629. void RendererSceneRenderRD::environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_fade_int, float p_fade_out, float p_depth_tolerance) {
  2630. Environment *env = environment_owner.getornull(p_env);
  2631. ERR_FAIL_COND(!env);
  2632. if (low_end) {
  2633. return;
  2634. }
  2635. env->ssr_enabled = p_enable;
  2636. env->ssr_max_steps = p_max_steps;
  2637. env->ssr_fade_in = p_fade_int;
  2638. env->ssr_fade_out = p_fade_out;
  2639. env->ssr_depth_tolerance = p_depth_tolerance;
  2640. }
  2641. void RendererSceneRenderRD::environment_set_ssr_roughness_quality(RS::EnvironmentSSRRoughnessQuality p_quality) {
  2642. ssr_roughness_quality = p_quality;
  2643. }
  2644. RS::EnvironmentSSRRoughnessQuality RendererSceneRenderRD::environment_get_ssr_roughness_quality() const {
  2645. return ssr_roughness_quality;
  2646. }
  2647. void RendererSceneRenderRD::environment_set_ssao(RID p_env, bool p_enable, float p_radius, float p_intensity, float p_bias, float p_light_affect, float p_ao_channel_affect, RS::EnvironmentSSAOBlur p_blur, float p_bilateral_sharpness) {
  2648. Environment *env = environment_owner.getornull(p_env);
  2649. ERR_FAIL_COND(!env);
  2650. if (low_end) {
  2651. return;
  2652. }
  2653. env->ssao_enabled = p_enable;
  2654. env->ssao_radius = p_radius;
  2655. env->ssao_intensity = p_intensity;
  2656. env->ssao_bias = p_bias;
  2657. env->ssao_direct_light_affect = p_light_affect;
  2658. env->ssao_ao_channel_affect = p_ao_channel_affect;
  2659. env->ssao_blur = p_blur;
  2660. }
  2661. void RendererSceneRenderRD::environment_set_ssao_quality(RS::EnvironmentSSAOQuality p_quality, bool p_half_size) {
  2662. ssao_quality = p_quality;
  2663. ssao_half_size = p_half_size;
  2664. }
  2665. bool RendererSceneRenderRD::environment_is_ssao_enabled(RID p_env) const {
  2666. Environment *env = environment_owner.getornull(p_env);
  2667. ERR_FAIL_COND_V(!env, false);
  2668. return env->ssao_enabled;
  2669. }
  2670. float RendererSceneRenderRD::environment_get_ssao_ao_affect(RID p_env) const {
  2671. Environment *env = environment_owner.getornull(p_env);
  2672. ERR_FAIL_COND_V(!env, 0.0);
  2673. return env->ssao_ao_channel_affect;
  2674. }
  2675. float RendererSceneRenderRD::environment_get_ssao_light_affect(RID p_env) const {
  2676. Environment *env = environment_owner.getornull(p_env);
  2677. ERR_FAIL_COND_V(!env, 0.0);
  2678. return env->ssao_direct_light_affect;
  2679. }
  2680. bool RendererSceneRenderRD::environment_is_ssr_enabled(RID p_env) const {
  2681. Environment *env = environment_owner.getornull(p_env);
  2682. ERR_FAIL_COND_V(!env, false);
  2683. return env->ssr_enabled;
  2684. }
  2685. bool RendererSceneRenderRD::environment_is_sdfgi_enabled(RID p_env) const {
  2686. Environment *env = environment_owner.getornull(p_env);
  2687. ERR_FAIL_COND_V(!env, false);
  2688. return env->sdfgi_enabled;
  2689. }
  2690. bool RendererSceneRenderRD::is_environment(RID p_env) const {
  2691. return environment_owner.owns(p_env);
  2692. }
  2693. Ref<Image> RendererSceneRenderRD::environment_bake_panorama(RID p_env, bool p_bake_irradiance, const Size2i &p_size) {
  2694. Environment *env = environment_owner.getornull(p_env);
  2695. ERR_FAIL_COND_V(!env, Ref<Image>());
  2696. if (env->background == RS::ENV_BG_CAMERA_FEED || env->background == RS::ENV_BG_CANVAS || env->background == RS::ENV_BG_KEEP) {
  2697. return Ref<Image>(); //nothing to bake
  2698. }
  2699. if (env->background == RS::ENV_BG_CLEAR_COLOR || env->background == RS::ENV_BG_COLOR) {
  2700. Color color;
  2701. if (env->background == RS::ENV_BG_CLEAR_COLOR) {
  2702. color = storage->get_default_clear_color();
  2703. } else {
  2704. color = env->bg_color;
  2705. }
  2706. color.r *= env->bg_energy;
  2707. color.g *= env->bg_energy;
  2708. color.b *= env->bg_energy;
  2709. Ref<Image> ret;
  2710. ret.instance();
  2711. ret->create(p_size.width, p_size.height, false, Image::FORMAT_RGBAF);
  2712. for (int i = 0; i < p_size.width; i++) {
  2713. for (int j = 0; j < p_size.height; j++) {
  2714. ret->set_pixel(i, j, color);
  2715. }
  2716. }
  2717. return ret;
  2718. }
  2719. if (env->background == RS::ENV_BG_SKY && env->sky.is_valid()) {
  2720. return sky_bake_panorama(env->sky, env->bg_energy, p_bake_irradiance, p_size);
  2721. }
  2722. return Ref<Image>();
  2723. }
  2724. ////////////////////////////////////////////////////////////
  2725. RID RendererSceneRenderRD::reflection_atlas_create() {
  2726. ReflectionAtlas ra;
  2727. ra.count = GLOBAL_GET("rendering/quality/reflection_atlas/reflection_count");
  2728. ra.size = GLOBAL_GET("rendering/quality/reflection_atlas/reflection_size");
  2729. return reflection_atlas_owner.make_rid(ra);
  2730. }
  2731. void RendererSceneRenderRD::reflection_atlas_set_size(RID p_ref_atlas, int p_reflection_size, int p_reflection_count) {
  2732. ReflectionAtlas *ra = reflection_atlas_owner.getornull(p_ref_atlas);
  2733. ERR_FAIL_COND(!ra);
  2734. if (ra->size == p_reflection_size && ra->count == p_reflection_count) {
  2735. return; //no changes
  2736. }
  2737. ra->size = p_reflection_size;
  2738. ra->count = p_reflection_count;
  2739. if (ra->reflection.is_valid()) {
  2740. //clear and invalidate everything
  2741. RD::get_singleton()->free(ra->reflection);
  2742. ra->reflection = RID();
  2743. RD::get_singleton()->free(ra->depth_buffer);
  2744. ra->depth_buffer = RID();
  2745. for (int i = 0; i < ra->reflections.size(); i++) {
  2746. _clear_reflection_data(ra->reflections.write[i].data);
  2747. if (ra->reflections[i].owner.is_null()) {
  2748. continue;
  2749. }
  2750. reflection_probe_release_atlas_index(ra->reflections[i].owner);
  2751. //rp->atlasindex clear
  2752. }
  2753. ra->reflections.clear();
  2754. }
  2755. }
  2756. int RendererSceneRenderRD::reflection_atlas_get_size(RID p_ref_atlas) const {
  2757. ReflectionAtlas *ra = reflection_atlas_owner.getornull(p_ref_atlas);
  2758. ERR_FAIL_COND_V(!ra, 0);
  2759. return ra->size;
  2760. }
  2761. ////////////////////////
  2762. RID RendererSceneRenderRD::reflection_probe_instance_create(RID p_probe) {
  2763. ReflectionProbeInstance rpi;
  2764. rpi.probe = p_probe;
  2765. return reflection_probe_instance_owner.make_rid(rpi);
  2766. }
  2767. void RendererSceneRenderRD::reflection_probe_instance_set_transform(RID p_instance, const Transform &p_transform) {
  2768. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2769. ERR_FAIL_COND(!rpi);
  2770. rpi->transform = p_transform;
  2771. rpi->dirty = true;
  2772. }
  2773. void RendererSceneRenderRD::reflection_probe_release_atlas_index(RID p_instance) {
  2774. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2775. ERR_FAIL_COND(!rpi);
  2776. if (rpi->atlas.is_null()) {
  2777. return; //nothing to release
  2778. }
  2779. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  2780. ERR_FAIL_COND(!atlas);
  2781. ERR_FAIL_INDEX(rpi->atlas_index, atlas->reflections.size());
  2782. atlas->reflections.write[rpi->atlas_index].owner = RID();
  2783. rpi->atlas_index = -1;
  2784. rpi->atlas = RID();
  2785. }
  2786. bool RendererSceneRenderRD::reflection_probe_instance_needs_redraw(RID p_instance) {
  2787. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2788. ERR_FAIL_COND_V(!rpi, false);
  2789. if (rpi->rendering) {
  2790. return false;
  2791. }
  2792. if (rpi->dirty) {
  2793. return true;
  2794. }
  2795. if (storage->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS) {
  2796. return true;
  2797. }
  2798. return rpi->atlas_index == -1;
  2799. }
  2800. bool RendererSceneRenderRD::reflection_probe_instance_has_reflection(RID p_instance) {
  2801. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2802. ERR_FAIL_COND_V(!rpi, false);
  2803. return rpi->atlas.is_valid();
  2804. }
  2805. bool RendererSceneRenderRD::reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas) {
  2806. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(p_reflection_atlas);
  2807. ERR_FAIL_COND_V(!atlas, false);
  2808. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2809. ERR_FAIL_COND_V(!rpi, false);
  2810. if (storage->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS && atlas->reflection.is_valid() && atlas->size != 256) {
  2811. WARN_PRINT("ReflectionProbes set to UPDATE_ALWAYS must have an atlas size of 256. Please update the atlas size in the ProjectSettings.");
  2812. reflection_atlas_set_size(p_reflection_atlas, 256, atlas->count);
  2813. }
  2814. if (storage->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS && atlas->reflection.is_valid() && atlas->reflections[0].data.layers[0].mipmaps.size() != 8) {
  2815. // Invalidate reflection atlas, need to regenerate
  2816. RD::get_singleton()->free(atlas->reflection);
  2817. atlas->reflection = RID();
  2818. for (int i = 0; i < atlas->reflections.size(); i++) {
  2819. if (atlas->reflections[i].owner.is_null()) {
  2820. continue;
  2821. }
  2822. reflection_probe_release_atlas_index(atlas->reflections[i].owner);
  2823. }
  2824. atlas->reflections.clear();
  2825. }
  2826. if (atlas->reflection.is_null()) {
  2827. int mipmaps = MIN(roughness_layers, Image::get_image_required_mipmaps(atlas->size, atlas->size, Image::FORMAT_RGBAH) + 1);
  2828. mipmaps = storage->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS ? 8 : mipmaps; // always use 8 mipmaps with real time filtering
  2829. {
  2830. //reflection atlas was unused, create:
  2831. RD::TextureFormat tf;
  2832. tf.array_layers = 6 * atlas->count;
  2833. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  2834. tf.texture_type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  2835. tf.mipmaps = mipmaps;
  2836. tf.width = atlas->size;
  2837. tf.height = atlas->size;
  2838. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  2839. atlas->reflection = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2840. }
  2841. {
  2842. RD::TextureFormat tf;
  2843. tf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D32_SFLOAT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D32_SFLOAT : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
  2844. tf.width = atlas->size;
  2845. tf.height = atlas->size;
  2846. tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  2847. atlas->depth_buffer = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2848. }
  2849. atlas->reflections.resize(atlas->count);
  2850. for (int i = 0; i < atlas->count; i++) {
  2851. _update_reflection_data(atlas->reflections.write[i].data, atlas->size, mipmaps, false, atlas->reflection, i * 6, storage->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS);
  2852. for (int j = 0; j < 6; j++) {
  2853. Vector<RID> fb;
  2854. fb.push_back(atlas->reflections.write[i].data.layers[0].mipmaps[0].views[j]);
  2855. fb.push_back(atlas->depth_buffer);
  2856. atlas->reflections.write[i].fbs[j] = RD::get_singleton()->framebuffer_create(fb);
  2857. }
  2858. }
  2859. Vector<RID> fb;
  2860. fb.push_back(atlas->depth_buffer);
  2861. atlas->depth_fb = RD::get_singleton()->framebuffer_create(fb);
  2862. }
  2863. if (rpi->atlas_index == -1) {
  2864. for (int i = 0; i < atlas->reflections.size(); i++) {
  2865. if (atlas->reflections[i].owner.is_null()) {
  2866. rpi->atlas_index = i;
  2867. break;
  2868. }
  2869. }
  2870. //find the one used last
  2871. if (rpi->atlas_index == -1) {
  2872. //everything is in use, find the one least used via LRU
  2873. uint64_t pass_min = 0;
  2874. for (int i = 0; i < atlas->reflections.size(); i++) {
  2875. ReflectionProbeInstance *rpi2 = reflection_probe_instance_owner.getornull(atlas->reflections[i].owner);
  2876. if (rpi2->last_pass < pass_min) {
  2877. pass_min = rpi2->last_pass;
  2878. rpi->atlas_index = i;
  2879. }
  2880. }
  2881. }
  2882. }
  2883. rpi->atlas = p_reflection_atlas;
  2884. rpi->rendering = true;
  2885. rpi->dirty = false;
  2886. rpi->processing_layer = 1;
  2887. rpi->processing_side = 0;
  2888. return true;
  2889. }
  2890. bool RendererSceneRenderRD::reflection_probe_instance_postprocess_step(RID p_instance) {
  2891. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2892. ERR_FAIL_COND_V(!rpi, false);
  2893. ERR_FAIL_COND_V(!rpi->rendering, false);
  2894. ERR_FAIL_COND_V(rpi->atlas.is_null(), false);
  2895. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  2896. if (!atlas || rpi->atlas_index == -1) {
  2897. //does not belong to an atlas anymore, cancel (was removed from atlas or atlas changed while rendering)
  2898. rpi->rendering = false;
  2899. return false;
  2900. }
  2901. if (storage->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS) {
  2902. // Using real time reflections, all roughness is done in one step
  2903. _create_reflection_fast_filter(atlas->reflections.write[rpi->atlas_index].data, false);
  2904. rpi->rendering = false;
  2905. rpi->processing_side = 0;
  2906. rpi->processing_layer = 1;
  2907. return true;
  2908. }
  2909. if (rpi->processing_layer > 1) {
  2910. _create_reflection_importance_sample(atlas->reflections.write[rpi->atlas_index].data, false, 10, rpi->processing_layer);
  2911. rpi->processing_layer++;
  2912. if (rpi->processing_layer == atlas->reflections[rpi->atlas_index].data.layers[0].mipmaps.size()) {
  2913. rpi->rendering = false;
  2914. rpi->processing_side = 0;
  2915. rpi->processing_layer = 1;
  2916. return true;
  2917. }
  2918. return false;
  2919. } else {
  2920. _create_reflection_importance_sample(atlas->reflections.write[rpi->atlas_index].data, false, rpi->processing_side, rpi->processing_layer);
  2921. }
  2922. rpi->processing_side++;
  2923. if (rpi->processing_side == 6) {
  2924. rpi->processing_side = 0;
  2925. rpi->processing_layer++;
  2926. }
  2927. return false;
  2928. }
  2929. uint32_t RendererSceneRenderRD::reflection_probe_instance_get_resolution(RID p_instance) {
  2930. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2931. ERR_FAIL_COND_V(!rpi, 0);
  2932. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  2933. ERR_FAIL_COND_V(!atlas, 0);
  2934. return atlas->size;
  2935. }
  2936. RID RendererSceneRenderRD::reflection_probe_instance_get_framebuffer(RID p_instance, int p_index) {
  2937. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2938. ERR_FAIL_COND_V(!rpi, RID());
  2939. ERR_FAIL_INDEX_V(p_index, 6, RID());
  2940. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  2941. ERR_FAIL_COND_V(!atlas, RID());
  2942. return atlas->reflections[rpi->atlas_index].fbs[p_index];
  2943. }
  2944. RID RendererSceneRenderRD::reflection_probe_instance_get_depth_framebuffer(RID p_instance, int p_index) {
  2945. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2946. ERR_FAIL_COND_V(!rpi, RID());
  2947. ERR_FAIL_INDEX_V(p_index, 6, RID());
  2948. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  2949. ERR_FAIL_COND_V(!atlas, RID());
  2950. return atlas->depth_fb;
  2951. }
  2952. ///////////////////////////////////////////////////////////
  2953. RID RendererSceneRenderRD::shadow_atlas_create() {
  2954. return shadow_atlas_owner.make_rid(ShadowAtlas());
  2955. }
  2956. void RendererSceneRenderRD::shadow_atlas_set_size(RID p_atlas, int p_size) {
  2957. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  2958. ERR_FAIL_COND(!shadow_atlas);
  2959. ERR_FAIL_COND(p_size < 0);
  2960. p_size = next_power_of_2(p_size);
  2961. if (p_size == shadow_atlas->size) {
  2962. return;
  2963. }
  2964. // erasing atlas
  2965. if (shadow_atlas->depth.is_valid()) {
  2966. RD::get_singleton()->free(shadow_atlas->depth);
  2967. shadow_atlas->depth = RID();
  2968. _clear_shadow_shrink_stages(shadow_atlas->shrink_stages);
  2969. }
  2970. for (int i = 0; i < 4; i++) {
  2971. //clear subdivisions
  2972. shadow_atlas->quadrants[i].shadows.resize(0);
  2973. shadow_atlas->quadrants[i].shadows.resize(1 << shadow_atlas->quadrants[i].subdivision);
  2974. }
  2975. //erase shadow atlas reference from lights
  2976. for (Map<RID, uint32_t>::Element *E = shadow_atlas->shadow_owners.front(); E; E = E->next()) {
  2977. LightInstance *li = light_instance_owner.getornull(E->key());
  2978. ERR_CONTINUE(!li);
  2979. li->shadow_atlases.erase(p_atlas);
  2980. }
  2981. //clear owners
  2982. shadow_atlas->shadow_owners.clear();
  2983. shadow_atlas->size = p_size;
  2984. if (shadow_atlas->size) {
  2985. RD::TextureFormat tf;
  2986. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  2987. tf.width = shadow_atlas->size;
  2988. tf.height = shadow_atlas->size;
  2989. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  2990. shadow_atlas->depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2991. }
  2992. }
  2993. void RendererSceneRenderRD::shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision) {
  2994. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  2995. ERR_FAIL_COND(!shadow_atlas);
  2996. ERR_FAIL_INDEX(p_quadrant, 4);
  2997. ERR_FAIL_INDEX(p_subdivision, 16384);
  2998. uint32_t subdiv = next_power_of_2(p_subdivision);
  2999. if (subdiv & 0xaaaaaaaa) { //sqrt(subdiv) must be integer
  3000. subdiv <<= 1;
  3001. }
  3002. subdiv = int(Math::sqrt((float)subdiv));
  3003. //obtain the number that will be x*x
  3004. if (shadow_atlas->quadrants[p_quadrant].subdivision == subdiv) {
  3005. return;
  3006. }
  3007. //erase all data from quadrant
  3008. for (int i = 0; i < shadow_atlas->quadrants[p_quadrant].shadows.size(); i++) {
  3009. if (shadow_atlas->quadrants[p_quadrant].shadows[i].owner.is_valid()) {
  3010. shadow_atlas->shadow_owners.erase(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  3011. LightInstance *li = light_instance_owner.getornull(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  3012. ERR_CONTINUE(!li);
  3013. li->shadow_atlases.erase(p_atlas);
  3014. }
  3015. }
  3016. shadow_atlas->quadrants[p_quadrant].shadows.resize(0);
  3017. shadow_atlas->quadrants[p_quadrant].shadows.resize(subdiv * subdiv);
  3018. shadow_atlas->quadrants[p_quadrant].subdivision = subdiv;
  3019. //cache the smallest subdiv (for faster allocation in light update)
  3020. shadow_atlas->smallest_subdiv = 1 << 30;
  3021. for (int i = 0; i < 4; i++) {
  3022. if (shadow_atlas->quadrants[i].subdivision) {
  3023. shadow_atlas->smallest_subdiv = MIN(shadow_atlas->smallest_subdiv, shadow_atlas->quadrants[i].subdivision);
  3024. }
  3025. }
  3026. if (shadow_atlas->smallest_subdiv == 1 << 30) {
  3027. shadow_atlas->smallest_subdiv = 0;
  3028. }
  3029. //resort the size orders, simple bublesort for 4 elements..
  3030. int swaps = 0;
  3031. do {
  3032. swaps = 0;
  3033. for (int i = 0; i < 3; i++) {
  3034. if (shadow_atlas->quadrants[shadow_atlas->size_order[i]].subdivision < shadow_atlas->quadrants[shadow_atlas->size_order[i + 1]].subdivision) {
  3035. SWAP(shadow_atlas->size_order[i], shadow_atlas->size_order[i + 1]);
  3036. swaps++;
  3037. }
  3038. }
  3039. } while (swaps > 0);
  3040. }
  3041. bool RendererSceneRenderRD::_shadow_atlas_find_shadow(ShadowAtlas *shadow_atlas, int *p_in_quadrants, int p_quadrant_count, int p_current_subdiv, uint64_t p_tick, int &r_quadrant, int &r_shadow) {
  3042. for (int i = p_quadrant_count - 1; i >= 0; i--) {
  3043. int qidx = p_in_quadrants[i];
  3044. if (shadow_atlas->quadrants[qidx].subdivision == (uint32_t)p_current_subdiv) {
  3045. return false;
  3046. }
  3047. //look for an empty space
  3048. int sc = shadow_atlas->quadrants[qidx].shadows.size();
  3049. ShadowAtlas::Quadrant::Shadow *sarr = shadow_atlas->quadrants[qidx].shadows.ptrw();
  3050. int found_free_idx = -1; //found a free one
  3051. int found_used_idx = -1; //found existing one, must steal it
  3052. uint64_t min_pass = 0; // pass of the existing one, try to use the least recently used one (LRU fashion)
  3053. for (int j = 0; j < sc; j++) {
  3054. if (!sarr[j].owner.is_valid()) {
  3055. found_free_idx = j;
  3056. break;
  3057. }
  3058. LightInstance *sli = light_instance_owner.getornull(sarr[j].owner);
  3059. ERR_CONTINUE(!sli);
  3060. if (sli->last_scene_pass != scene_pass) {
  3061. //was just allocated, don't kill it so soon, wait a bit..
  3062. if (p_tick - sarr[j].alloc_tick < shadow_atlas_realloc_tolerance_msec) {
  3063. continue;
  3064. }
  3065. if (found_used_idx == -1 || sli->last_scene_pass < min_pass) {
  3066. found_used_idx = j;
  3067. min_pass = sli->last_scene_pass;
  3068. }
  3069. }
  3070. }
  3071. if (found_free_idx == -1 && found_used_idx == -1) {
  3072. continue; //nothing found
  3073. }
  3074. if (found_free_idx == -1 && found_used_idx != -1) {
  3075. found_free_idx = found_used_idx;
  3076. }
  3077. r_quadrant = qidx;
  3078. r_shadow = found_free_idx;
  3079. return true;
  3080. }
  3081. return false;
  3082. }
  3083. bool RendererSceneRenderRD::shadow_atlas_update_light(RID p_atlas, RID p_light_intance, float p_coverage, uint64_t p_light_version) {
  3084. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  3085. ERR_FAIL_COND_V(!shadow_atlas, false);
  3086. LightInstance *li = light_instance_owner.getornull(p_light_intance);
  3087. ERR_FAIL_COND_V(!li, false);
  3088. if (shadow_atlas->size == 0 || shadow_atlas->smallest_subdiv == 0) {
  3089. return false;
  3090. }
  3091. uint32_t quad_size = shadow_atlas->size >> 1;
  3092. int desired_fit = MIN(quad_size / shadow_atlas->smallest_subdiv, next_power_of_2(quad_size * p_coverage));
  3093. int valid_quadrants[4];
  3094. int valid_quadrant_count = 0;
  3095. int best_size = -1; //best size found
  3096. int best_subdiv = -1; //subdiv for the best size
  3097. //find the quadrants this fits into, and the best possible size it can fit into
  3098. for (int i = 0; i < 4; i++) {
  3099. int q = shadow_atlas->size_order[i];
  3100. int sd = shadow_atlas->quadrants[q].subdivision;
  3101. if (sd == 0) {
  3102. continue; //unused
  3103. }
  3104. int max_fit = quad_size / sd;
  3105. if (best_size != -1 && max_fit > best_size) {
  3106. break; //too large
  3107. }
  3108. valid_quadrants[valid_quadrant_count++] = q;
  3109. best_subdiv = sd;
  3110. if (max_fit >= desired_fit) {
  3111. best_size = max_fit;
  3112. }
  3113. }
  3114. ERR_FAIL_COND_V(valid_quadrant_count == 0, false);
  3115. uint64_t tick = OS::get_singleton()->get_ticks_msec();
  3116. //see if it already exists
  3117. if (shadow_atlas->shadow_owners.has(p_light_intance)) {
  3118. //it does!
  3119. uint32_t key = shadow_atlas->shadow_owners[p_light_intance];
  3120. uint32_t q = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  3121. uint32_t s = key & ShadowAtlas::SHADOW_INDEX_MASK;
  3122. bool should_realloc = shadow_atlas->quadrants[q].subdivision != (uint32_t)best_subdiv && (shadow_atlas->quadrants[q].shadows[s].alloc_tick - tick > shadow_atlas_realloc_tolerance_msec);
  3123. bool should_redraw = shadow_atlas->quadrants[q].shadows[s].version != p_light_version;
  3124. if (!should_realloc) {
  3125. shadow_atlas->quadrants[q].shadows.write[s].version = p_light_version;
  3126. //already existing, see if it should redraw or it's just OK
  3127. return should_redraw;
  3128. }
  3129. int new_quadrant, new_shadow;
  3130. //find a better place
  3131. if (_shadow_atlas_find_shadow(shadow_atlas, valid_quadrants, valid_quadrant_count, shadow_atlas->quadrants[q].subdivision, tick, new_quadrant, new_shadow)) {
  3132. //found a better place!
  3133. ShadowAtlas::Quadrant::Shadow *sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_shadow];
  3134. if (sh->owner.is_valid()) {
  3135. //is taken, but is invalid, erasing it
  3136. shadow_atlas->shadow_owners.erase(sh->owner);
  3137. LightInstance *sli = light_instance_owner.getornull(sh->owner);
  3138. sli->shadow_atlases.erase(p_atlas);
  3139. }
  3140. //erase previous
  3141. shadow_atlas->quadrants[q].shadows.write[s].version = 0;
  3142. shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
  3143. sh->owner = p_light_intance;
  3144. sh->alloc_tick = tick;
  3145. sh->version = p_light_version;
  3146. li->shadow_atlases.insert(p_atlas);
  3147. //make new key
  3148. key = new_quadrant << ShadowAtlas::QUADRANT_SHIFT;
  3149. key |= new_shadow;
  3150. //update it in map
  3151. shadow_atlas->shadow_owners[p_light_intance] = key;
  3152. //make it dirty, as it should redraw anyway
  3153. return true;
  3154. }
  3155. //no better place for this shadow found, keep current
  3156. //already existing, see if it should redraw or it's just OK
  3157. shadow_atlas->quadrants[q].shadows.write[s].version = p_light_version;
  3158. return should_redraw;
  3159. }
  3160. int new_quadrant, new_shadow;
  3161. //find a better place
  3162. if (_shadow_atlas_find_shadow(shadow_atlas, valid_quadrants, valid_quadrant_count, -1, tick, new_quadrant, new_shadow)) {
  3163. //found a better place!
  3164. ShadowAtlas::Quadrant::Shadow *sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_shadow];
  3165. if (sh->owner.is_valid()) {
  3166. //is taken, but is invalid, erasing it
  3167. shadow_atlas->shadow_owners.erase(sh->owner);
  3168. LightInstance *sli = light_instance_owner.getornull(sh->owner);
  3169. sli->shadow_atlases.erase(p_atlas);
  3170. }
  3171. sh->owner = p_light_intance;
  3172. sh->alloc_tick = tick;
  3173. sh->version = p_light_version;
  3174. li->shadow_atlases.insert(p_atlas);
  3175. //make new key
  3176. uint32_t key = new_quadrant << ShadowAtlas::QUADRANT_SHIFT;
  3177. key |= new_shadow;
  3178. //update it in map
  3179. shadow_atlas->shadow_owners[p_light_intance] = key;
  3180. //make it dirty, as it should redraw anyway
  3181. return true;
  3182. }
  3183. //no place to allocate this light, apologies
  3184. return false;
  3185. }
  3186. void RendererSceneRenderRD::directional_shadow_atlas_set_size(int p_size) {
  3187. p_size = nearest_power_of_2_templated(p_size);
  3188. if (directional_shadow.size == p_size) {
  3189. return;
  3190. }
  3191. directional_shadow.size = p_size;
  3192. if (directional_shadow.depth.is_valid()) {
  3193. RD::get_singleton()->free(directional_shadow.depth);
  3194. _clear_shadow_shrink_stages(directional_shadow.shrink_stages);
  3195. directional_shadow.depth = RID();
  3196. }
  3197. if (p_size > 0) {
  3198. RD::TextureFormat tf;
  3199. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  3200. tf.width = p_size;
  3201. tf.height = p_size;
  3202. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  3203. directional_shadow.depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
  3204. }
  3205. _base_uniforms_changed();
  3206. }
  3207. void RendererSceneRenderRD::set_directional_shadow_count(int p_count) {
  3208. directional_shadow.light_count = p_count;
  3209. directional_shadow.current_light = 0;
  3210. }
  3211. static Rect2i _get_directional_shadow_rect(int p_size, int p_shadow_count, int p_shadow_index) {
  3212. int split_h = 1;
  3213. int split_v = 1;
  3214. while (split_h * split_v < p_shadow_count) {
  3215. if (split_h == split_v) {
  3216. split_h <<= 1;
  3217. } else {
  3218. split_v <<= 1;
  3219. }
  3220. }
  3221. Rect2i rect(0, 0, p_size, p_size);
  3222. rect.size.width /= split_h;
  3223. rect.size.height /= split_v;
  3224. rect.position.x = rect.size.width * (p_shadow_index % split_h);
  3225. rect.position.y = rect.size.height * (p_shadow_index / split_h);
  3226. return rect;
  3227. }
  3228. int RendererSceneRenderRD::get_directional_light_shadow_size(RID p_light_intance) {
  3229. ERR_FAIL_COND_V(directional_shadow.light_count == 0, 0);
  3230. Rect2i r = _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, 0);
  3231. LightInstance *light_instance = light_instance_owner.getornull(p_light_intance);
  3232. ERR_FAIL_COND_V(!light_instance, 0);
  3233. switch (storage->light_directional_get_shadow_mode(light_instance->light)) {
  3234. case RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL:
  3235. break; //none
  3236. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS:
  3237. r.size.height /= 2;
  3238. break;
  3239. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS:
  3240. r.size /= 2;
  3241. break;
  3242. }
  3243. return MAX(r.size.width, r.size.height);
  3244. }
  3245. //////////////////////////////////////////////////
  3246. RID RendererSceneRenderRD::camera_effects_create() {
  3247. return camera_effects_owner.make_rid(CameraEffects());
  3248. }
  3249. void RendererSceneRenderRD::camera_effects_set_dof_blur_quality(RS::DOFBlurQuality p_quality, bool p_use_jitter) {
  3250. dof_blur_quality = p_quality;
  3251. dof_blur_use_jitter = p_use_jitter;
  3252. }
  3253. void RendererSceneRenderRD::camera_effects_set_dof_blur_bokeh_shape(RS::DOFBokehShape p_shape) {
  3254. dof_blur_bokeh_shape = p_shape;
  3255. }
  3256. void RendererSceneRenderRD::camera_effects_set_dof_blur(RID p_camera_effects, bool p_far_enable, float p_far_distance, float p_far_transition, bool p_near_enable, float p_near_distance, float p_near_transition, float p_amount) {
  3257. CameraEffects *camfx = camera_effects_owner.getornull(p_camera_effects);
  3258. ERR_FAIL_COND(!camfx);
  3259. camfx->dof_blur_far_enabled = p_far_enable;
  3260. camfx->dof_blur_far_distance = p_far_distance;
  3261. camfx->dof_blur_far_transition = p_far_transition;
  3262. camfx->dof_blur_near_enabled = p_near_enable;
  3263. camfx->dof_blur_near_distance = p_near_distance;
  3264. camfx->dof_blur_near_transition = p_near_transition;
  3265. camfx->dof_blur_amount = p_amount;
  3266. }
  3267. void RendererSceneRenderRD::camera_effects_set_custom_exposure(RID p_camera_effects, bool p_enable, float p_exposure) {
  3268. CameraEffects *camfx = camera_effects_owner.getornull(p_camera_effects);
  3269. ERR_FAIL_COND(!camfx);
  3270. camfx->override_exposure_enabled = p_enable;
  3271. camfx->override_exposure = p_exposure;
  3272. }
  3273. RID RendererSceneRenderRD::light_instance_create(RID p_light) {
  3274. RID li = light_instance_owner.make_rid(LightInstance());
  3275. LightInstance *light_instance = light_instance_owner.getornull(li);
  3276. light_instance->self = li;
  3277. light_instance->light = p_light;
  3278. light_instance->light_type = storage->light_get_type(p_light);
  3279. return li;
  3280. }
  3281. void RendererSceneRenderRD::light_instance_set_transform(RID p_light_instance, const Transform &p_transform) {
  3282. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  3283. ERR_FAIL_COND(!light_instance);
  3284. light_instance->transform = p_transform;
  3285. }
  3286. void RendererSceneRenderRD::light_instance_set_aabb(RID p_light_instance, const AABB &p_aabb) {
  3287. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  3288. ERR_FAIL_COND(!light_instance);
  3289. light_instance->aabb = p_aabb;
  3290. }
  3291. void RendererSceneRenderRD::light_instance_set_shadow_transform(RID p_light_instance, const CameraMatrix &p_projection, const Transform &p_transform, float p_far, float p_split, int p_pass, float p_shadow_texel_size, float p_bias_scale, float p_range_begin, const Vector2 &p_uv_scale) {
  3292. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  3293. ERR_FAIL_COND(!light_instance);
  3294. if (storage->light_get_type(light_instance->light) != RS::LIGHT_DIRECTIONAL) {
  3295. p_pass = 0;
  3296. }
  3297. ERR_FAIL_INDEX(p_pass, 4);
  3298. light_instance->shadow_transform[p_pass].camera = p_projection;
  3299. light_instance->shadow_transform[p_pass].transform = p_transform;
  3300. light_instance->shadow_transform[p_pass].farplane = p_far;
  3301. light_instance->shadow_transform[p_pass].split = p_split;
  3302. light_instance->shadow_transform[p_pass].bias_scale = p_bias_scale;
  3303. light_instance->shadow_transform[p_pass].range_begin = p_range_begin;
  3304. light_instance->shadow_transform[p_pass].shadow_texel_size = p_shadow_texel_size;
  3305. light_instance->shadow_transform[p_pass].uv_scale = p_uv_scale;
  3306. }
  3307. void RendererSceneRenderRD::light_instance_mark_visible(RID p_light_instance) {
  3308. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  3309. ERR_FAIL_COND(!light_instance);
  3310. light_instance->last_scene_pass = scene_pass;
  3311. }
  3312. RendererSceneRenderRD::ShadowCubemap *RendererSceneRenderRD::_get_shadow_cubemap(int p_size) {
  3313. if (!shadow_cubemaps.has(p_size)) {
  3314. ShadowCubemap sc;
  3315. {
  3316. RD::TextureFormat tf;
  3317. tf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D32_SFLOAT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D32_SFLOAT : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
  3318. tf.width = p_size;
  3319. tf.height = p_size;
  3320. tf.texture_type = RD::TEXTURE_TYPE_CUBE;
  3321. tf.array_layers = 6;
  3322. tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  3323. sc.cubemap = RD::get_singleton()->texture_create(tf, RD::TextureView());
  3324. }
  3325. for (int i = 0; i < 6; i++) {
  3326. RID side_texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), sc.cubemap, i, 0);
  3327. Vector<RID> fbtex;
  3328. fbtex.push_back(side_texture);
  3329. sc.side_fb[i] = RD::get_singleton()->framebuffer_create(fbtex);
  3330. }
  3331. shadow_cubemaps[p_size] = sc;
  3332. }
  3333. return &shadow_cubemaps[p_size];
  3334. }
  3335. RendererSceneRenderRD::ShadowMap *RendererSceneRenderRD::_get_shadow_map(const Size2i &p_size) {
  3336. if (!shadow_maps.has(p_size)) {
  3337. ShadowMap sm;
  3338. {
  3339. RD::TextureFormat tf;
  3340. tf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D32_SFLOAT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D32_SFLOAT : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
  3341. tf.width = p_size.width;
  3342. tf.height = p_size.height;
  3343. tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  3344. sm.depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
  3345. }
  3346. Vector<RID> fbtex;
  3347. fbtex.push_back(sm.depth);
  3348. sm.fb = RD::get_singleton()->framebuffer_create(fbtex);
  3349. shadow_maps[p_size] = sm;
  3350. }
  3351. return &shadow_maps[p_size];
  3352. }
  3353. //////////////////////////
  3354. RID RendererSceneRenderRD::decal_instance_create(RID p_decal) {
  3355. DecalInstance di;
  3356. di.decal = p_decal;
  3357. return decal_instance_owner.make_rid(di);
  3358. }
  3359. void RendererSceneRenderRD::decal_instance_set_transform(RID p_decal, const Transform &p_transform) {
  3360. DecalInstance *di = decal_instance_owner.getornull(p_decal);
  3361. ERR_FAIL_COND(!di);
  3362. di->transform = p_transform;
  3363. }
  3364. /////////////////////////////////
  3365. RID RendererSceneRenderRD::gi_probe_instance_create(RID p_base) {
  3366. GIProbeInstance gi_probe;
  3367. gi_probe.probe = p_base;
  3368. RID rid = gi_probe_instance_owner.make_rid(gi_probe);
  3369. return rid;
  3370. }
  3371. void RendererSceneRenderRD::gi_probe_instance_set_transform_to_data(RID p_probe, const Transform &p_xform) {
  3372. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_probe);
  3373. ERR_FAIL_COND(!gi_probe);
  3374. gi_probe->transform = p_xform;
  3375. }
  3376. bool RendererSceneRenderRD::gi_probe_needs_update(RID p_probe) const {
  3377. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_probe);
  3378. ERR_FAIL_COND_V(!gi_probe, false);
  3379. if (low_end) {
  3380. return false;
  3381. }
  3382. //return true;
  3383. return gi_probe->last_probe_version != storage->gi_probe_get_version(gi_probe->probe);
  3384. }
  3385. void RendererSceneRenderRD::gi_probe_update(RID p_probe, bool p_update_light_instances, const Vector<RID> &p_light_instances, int p_dynamic_object_count, InstanceBase **p_dynamic_objects) {
  3386. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_probe);
  3387. ERR_FAIL_COND(!gi_probe);
  3388. if (low_end) {
  3389. return;
  3390. }
  3391. uint32_t data_version = storage->gi_probe_get_data_version(gi_probe->probe);
  3392. // (RE)CREATE IF NEEDED
  3393. if (gi_probe->last_probe_data_version != data_version) {
  3394. //need to re-create everything
  3395. if (gi_probe->texture.is_valid()) {
  3396. RD::get_singleton()->free(gi_probe->texture);
  3397. RD::get_singleton()->free(gi_probe->write_buffer);
  3398. gi_probe->mipmaps.clear();
  3399. }
  3400. for (int i = 0; i < gi_probe->dynamic_maps.size(); i++) {
  3401. RD::get_singleton()->free(gi_probe->dynamic_maps[i].texture);
  3402. RD::get_singleton()->free(gi_probe->dynamic_maps[i].depth);
  3403. }
  3404. gi_probe->dynamic_maps.clear();
  3405. Vector3i octree_size = storage->gi_probe_get_octree_size(gi_probe->probe);
  3406. if (octree_size != Vector3i()) {
  3407. //can create a 3D texture
  3408. Vector<int> levels = storage->gi_probe_get_level_counts(gi_probe->probe);
  3409. RD::TextureFormat tf;
  3410. tf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  3411. tf.width = octree_size.x;
  3412. tf.height = octree_size.y;
  3413. tf.depth = octree_size.z;
  3414. tf.texture_type = RD::TEXTURE_TYPE_3D;
  3415. tf.mipmaps = levels.size();
  3416. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  3417. gi_probe->texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  3418. RD::get_singleton()->texture_clear(gi_probe->texture, Color(0, 0, 0, 0), 0, levels.size(), 0, 1, false);
  3419. {
  3420. int total_elements = 0;
  3421. for (int i = 0; i < levels.size(); i++) {
  3422. total_elements += levels[i];
  3423. }
  3424. gi_probe->write_buffer = RD::get_singleton()->storage_buffer_create(total_elements * 16);
  3425. }
  3426. for (int i = 0; i < levels.size(); i++) {
  3427. GIProbeInstance::Mipmap mipmap;
  3428. mipmap.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), gi_probe->texture, 0, i, RD::TEXTURE_SLICE_3D);
  3429. mipmap.level = levels.size() - i - 1;
  3430. mipmap.cell_offset = 0;
  3431. for (uint32_t j = 0; j < mipmap.level; j++) {
  3432. mipmap.cell_offset += levels[j];
  3433. }
  3434. mipmap.cell_count = levels[mipmap.level];
  3435. Vector<RD::Uniform> uniforms;
  3436. {
  3437. RD::Uniform u;
  3438. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3439. u.binding = 1;
  3440. u.ids.push_back(storage->gi_probe_get_octree_buffer(gi_probe->probe));
  3441. uniforms.push_back(u);
  3442. }
  3443. {
  3444. RD::Uniform u;
  3445. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3446. u.binding = 2;
  3447. u.ids.push_back(storage->gi_probe_get_data_buffer(gi_probe->probe));
  3448. uniforms.push_back(u);
  3449. }
  3450. {
  3451. RD::Uniform u;
  3452. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3453. u.binding = 4;
  3454. u.ids.push_back(gi_probe->write_buffer);
  3455. uniforms.push_back(u);
  3456. }
  3457. {
  3458. RD::Uniform u;
  3459. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  3460. u.binding = 9;
  3461. u.ids.push_back(storage->gi_probe_get_sdf_texture(gi_probe->probe));
  3462. uniforms.push_back(u);
  3463. }
  3464. {
  3465. RD::Uniform u;
  3466. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  3467. u.binding = 10;
  3468. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  3469. uniforms.push_back(u);
  3470. }
  3471. {
  3472. Vector<RD::Uniform> copy_uniforms = uniforms;
  3473. if (i == 0) {
  3474. {
  3475. RD::Uniform u;
  3476. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  3477. u.binding = 3;
  3478. u.ids.push_back(gi_probe_lights_uniform);
  3479. copy_uniforms.push_back(u);
  3480. }
  3481. mipmap.uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_COMPUTE_LIGHT], 0);
  3482. copy_uniforms = uniforms; //restore
  3483. {
  3484. RD::Uniform u;
  3485. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  3486. u.binding = 5;
  3487. u.ids.push_back(gi_probe->texture);
  3488. copy_uniforms.push_back(u);
  3489. }
  3490. mipmap.second_bounce_uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_COMPUTE_SECOND_BOUNCE], 0);
  3491. } else {
  3492. mipmap.uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_COMPUTE_MIPMAP], 0);
  3493. }
  3494. }
  3495. {
  3496. RD::Uniform u;
  3497. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  3498. u.binding = 5;
  3499. u.ids.push_back(mipmap.texture);
  3500. uniforms.push_back(u);
  3501. }
  3502. mipmap.write_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_WRITE_TEXTURE], 0);
  3503. gi_probe->mipmaps.push_back(mipmap);
  3504. }
  3505. {
  3506. uint32_t dynamic_map_size = MAX(MAX(octree_size.x, octree_size.y), octree_size.z);
  3507. uint32_t oversample = nearest_power_of_2_templated(4);
  3508. int mipmap_index = 0;
  3509. while (mipmap_index < gi_probe->mipmaps.size()) {
  3510. GIProbeInstance::DynamicMap dmap;
  3511. if (oversample > 0) {
  3512. dmap.size = dynamic_map_size * (1 << oversample);
  3513. dmap.mipmap = -1;
  3514. oversample--;
  3515. } else {
  3516. dmap.size = dynamic_map_size >> mipmap_index;
  3517. dmap.mipmap = mipmap_index;
  3518. mipmap_index++;
  3519. }
  3520. RD::TextureFormat dtf;
  3521. dtf.width = dmap.size;
  3522. dtf.height = dmap.size;
  3523. dtf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  3524. dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  3525. if (gi_probe->dynamic_maps.size() == 0) {
  3526. dtf.usage_bits |= RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  3527. }
  3528. dmap.texture = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  3529. if (gi_probe->dynamic_maps.size() == 0) {
  3530. //render depth for first one
  3531. dtf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D32_SFLOAT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D32_SFLOAT : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
  3532. dtf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  3533. dmap.fb_depth = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  3534. }
  3535. //just use depth as-is
  3536. dtf.format = RD::DATA_FORMAT_R32_SFLOAT;
  3537. dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  3538. dmap.depth = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  3539. if (gi_probe->dynamic_maps.size() == 0) {
  3540. dtf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  3541. dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  3542. dmap.albedo = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  3543. dmap.normal = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  3544. dmap.orm = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  3545. Vector<RID> fb;
  3546. fb.push_back(dmap.albedo);
  3547. fb.push_back(dmap.normal);
  3548. fb.push_back(dmap.orm);
  3549. fb.push_back(dmap.texture); //emission
  3550. fb.push_back(dmap.depth);
  3551. fb.push_back(dmap.fb_depth);
  3552. dmap.fb = RD::get_singleton()->framebuffer_create(fb);
  3553. {
  3554. Vector<RD::Uniform> uniforms;
  3555. {
  3556. RD::Uniform u;
  3557. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  3558. u.binding = 3;
  3559. u.ids.push_back(gi_probe_lights_uniform);
  3560. uniforms.push_back(u);
  3561. }
  3562. {
  3563. RD::Uniform u;
  3564. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  3565. u.binding = 5;
  3566. u.ids.push_back(dmap.albedo);
  3567. uniforms.push_back(u);
  3568. }
  3569. {
  3570. RD::Uniform u;
  3571. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  3572. u.binding = 6;
  3573. u.ids.push_back(dmap.normal);
  3574. uniforms.push_back(u);
  3575. }
  3576. {
  3577. RD::Uniform u;
  3578. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  3579. u.binding = 7;
  3580. u.ids.push_back(dmap.orm);
  3581. uniforms.push_back(u);
  3582. }
  3583. {
  3584. RD::Uniform u;
  3585. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  3586. u.binding = 8;
  3587. u.ids.push_back(dmap.fb_depth);
  3588. uniforms.push_back(u);
  3589. }
  3590. {
  3591. RD::Uniform u;
  3592. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  3593. u.binding = 9;
  3594. u.ids.push_back(storage->gi_probe_get_sdf_texture(gi_probe->probe));
  3595. uniforms.push_back(u);
  3596. }
  3597. {
  3598. RD::Uniform u;
  3599. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  3600. u.binding = 10;
  3601. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  3602. uniforms.push_back(u);
  3603. }
  3604. {
  3605. RD::Uniform u;
  3606. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  3607. u.binding = 11;
  3608. u.ids.push_back(dmap.texture);
  3609. uniforms.push_back(u);
  3610. }
  3611. {
  3612. RD::Uniform u;
  3613. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  3614. u.binding = 12;
  3615. u.ids.push_back(dmap.depth);
  3616. uniforms.push_back(u);
  3617. }
  3618. dmap.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_DYNAMIC_OBJECT_LIGHTING], 0);
  3619. }
  3620. } else {
  3621. bool plot = dmap.mipmap >= 0;
  3622. bool write = dmap.mipmap < (gi_probe->mipmaps.size() - 1);
  3623. Vector<RD::Uniform> uniforms;
  3624. {
  3625. RD::Uniform u;
  3626. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  3627. u.binding = 5;
  3628. u.ids.push_back(gi_probe->dynamic_maps[gi_probe->dynamic_maps.size() - 1].texture);
  3629. uniforms.push_back(u);
  3630. }
  3631. {
  3632. RD::Uniform u;
  3633. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  3634. u.binding = 6;
  3635. u.ids.push_back(gi_probe->dynamic_maps[gi_probe->dynamic_maps.size() - 1].depth);
  3636. uniforms.push_back(u);
  3637. }
  3638. if (write) {
  3639. {
  3640. RD::Uniform u;
  3641. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  3642. u.binding = 7;
  3643. u.ids.push_back(dmap.texture);
  3644. uniforms.push_back(u);
  3645. }
  3646. {
  3647. RD::Uniform u;
  3648. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  3649. u.binding = 8;
  3650. u.ids.push_back(dmap.depth);
  3651. uniforms.push_back(u);
  3652. }
  3653. }
  3654. {
  3655. RD::Uniform u;
  3656. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  3657. u.binding = 9;
  3658. u.ids.push_back(storage->gi_probe_get_sdf_texture(gi_probe->probe));
  3659. uniforms.push_back(u);
  3660. }
  3661. {
  3662. RD::Uniform u;
  3663. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  3664. u.binding = 10;
  3665. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  3666. uniforms.push_back(u);
  3667. }
  3668. if (plot) {
  3669. {
  3670. RD::Uniform u;
  3671. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  3672. u.binding = 11;
  3673. u.ids.push_back(gi_probe->mipmaps[dmap.mipmap].texture);
  3674. uniforms.push_back(u);
  3675. }
  3676. }
  3677. dmap.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_lighting_shader_version_shaders[(write && plot) ? GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_WRITE_PLOT : write ? GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_WRITE : GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_PLOT], 0);
  3678. }
  3679. gi_probe->dynamic_maps.push_back(dmap);
  3680. }
  3681. }
  3682. }
  3683. gi_probe->last_probe_data_version = data_version;
  3684. p_update_light_instances = true; //just in case
  3685. _base_uniforms_changed();
  3686. }
  3687. // UDPDATE TIME
  3688. if (gi_probe->has_dynamic_object_data) {
  3689. //if it has dynamic object data, it needs to be cleared
  3690. RD::get_singleton()->texture_clear(gi_probe->texture, Color(0, 0, 0, 0), 0, gi_probe->mipmaps.size(), 0, 1, true);
  3691. }
  3692. uint32_t light_count = 0;
  3693. if (p_update_light_instances || p_dynamic_object_count > 0) {
  3694. light_count = MIN(gi_probe_max_lights, (uint32_t)p_light_instances.size());
  3695. {
  3696. Transform to_cell = storage->gi_probe_get_to_cell_xform(gi_probe->probe);
  3697. Transform to_probe_xform = (gi_probe->transform * to_cell.affine_inverse()).affine_inverse();
  3698. //update lights
  3699. for (uint32_t i = 0; i < light_count; i++) {
  3700. GIProbeLight &l = gi_probe_lights[i];
  3701. RID light_instance = p_light_instances[i];
  3702. RID light = light_instance_get_base_light(light_instance);
  3703. l.type = storage->light_get_type(light);
  3704. if (l.type == RS::LIGHT_DIRECTIONAL && storage->light_directional_is_sky_only(light)) {
  3705. light_count--;
  3706. continue;
  3707. }
  3708. l.attenuation = storage->light_get_param(light, RS::LIGHT_PARAM_ATTENUATION);
  3709. l.energy = storage->light_get_param(light, RS::LIGHT_PARAM_ENERGY) * storage->light_get_param(light, RS::LIGHT_PARAM_INDIRECT_ENERGY);
  3710. l.radius = to_cell.basis.xform(Vector3(storage->light_get_param(light, RS::LIGHT_PARAM_RANGE), 0, 0)).length();
  3711. Color color = storage->light_get_color(light).to_linear();
  3712. l.color[0] = color.r;
  3713. l.color[1] = color.g;
  3714. l.color[2] = color.b;
  3715. l.spot_angle_radians = Math::deg2rad(storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ANGLE));
  3716. l.spot_attenuation = storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  3717. Transform xform = light_instance_get_base_transform(light_instance);
  3718. Vector3 pos = to_probe_xform.xform(xform.origin);
  3719. Vector3 dir = to_probe_xform.basis.xform(-xform.basis.get_axis(2)).normalized();
  3720. l.position[0] = pos.x;
  3721. l.position[1] = pos.y;
  3722. l.position[2] = pos.z;
  3723. l.direction[0] = dir.x;
  3724. l.direction[1] = dir.y;
  3725. l.direction[2] = dir.z;
  3726. l.has_shadow = storage->light_has_shadow(light);
  3727. }
  3728. RD::get_singleton()->buffer_update(gi_probe_lights_uniform, 0, sizeof(GIProbeLight) * light_count, gi_probe_lights, true);
  3729. }
  3730. }
  3731. if (gi_probe->has_dynamic_object_data || p_update_light_instances || p_dynamic_object_count) {
  3732. // PROCESS MIPMAPS
  3733. if (gi_probe->mipmaps.size()) {
  3734. //can update mipmaps
  3735. Vector3i probe_size = storage->gi_probe_get_octree_size(gi_probe->probe);
  3736. GIProbePushConstant push_constant;
  3737. push_constant.limits[0] = probe_size.x;
  3738. push_constant.limits[1] = probe_size.y;
  3739. push_constant.limits[2] = probe_size.z;
  3740. push_constant.stack_size = gi_probe->mipmaps.size();
  3741. push_constant.emission_scale = 1.0;
  3742. push_constant.propagation = storage->gi_probe_get_propagation(gi_probe->probe);
  3743. push_constant.dynamic_range = storage->gi_probe_get_dynamic_range(gi_probe->probe);
  3744. push_constant.light_count = light_count;
  3745. push_constant.aniso_strength = 0;
  3746. /* print_line("probe update to version " + itos(gi_probe->last_probe_version));
  3747. print_line("propagation " + rtos(push_constant.propagation));
  3748. print_line("dynrange " + rtos(push_constant.dynamic_range));
  3749. */
  3750. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  3751. int passes;
  3752. if (p_update_light_instances) {
  3753. passes = storage->gi_probe_is_using_two_bounces(gi_probe->probe) ? 2 : 1;
  3754. } else {
  3755. passes = 1; //only re-blitting is necessary
  3756. }
  3757. int wg_size = 64;
  3758. int wg_limit_x = RD::get_singleton()->limit_get(RD::LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X);
  3759. for (int pass = 0; pass < passes; pass++) {
  3760. if (p_update_light_instances) {
  3761. for (int i = 0; i < gi_probe->mipmaps.size(); i++) {
  3762. if (i == 0) {
  3763. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[pass == 0 ? GI_PROBE_SHADER_VERSION_COMPUTE_LIGHT : GI_PROBE_SHADER_VERSION_COMPUTE_SECOND_BOUNCE]);
  3764. } else if (i == 1) {
  3765. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_COMPUTE_MIPMAP]);
  3766. }
  3767. if (pass == 1 || i > 0) {
  3768. RD::get_singleton()->compute_list_add_barrier(compute_list); //wait til previous step is done
  3769. }
  3770. if (pass == 0 || i > 0) {
  3771. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->mipmaps[i].uniform_set, 0);
  3772. } else {
  3773. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->mipmaps[i].second_bounce_uniform_set, 0);
  3774. }
  3775. push_constant.cell_offset = gi_probe->mipmaps[i].cell_offset;
  3776. push_constant.cell_count = gi_probe->mipmaps[i].cell_count;
  3777. int wg_todo = (gi_probe->mipmaps[i].cell_count - 1) / wg_size + 1;
  3778. while (wg_todo) {
  3779. int wg_count = MIN(wg_todo, wg_limit_x);
  3780. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GIProbePushConstant));
  3781. RD::get_singleton()->compute_list_dispatch(compute_list, wg_count, 1, 1);
  3782. wg_todo -= wg_count;
  3783. push_constant.cell_offset += wg_count * wg_size;
  3784. }
  3785. }
  3786. RD::get_singleton()->compute_list_add_barrier(compute_list); //wait til previous step is done
  3787. }
  3788. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_WRITE_TEXTURE]);
  3789. for (int i = 0; i < gi_probe->mipmaps.size(); i++) {
  3790. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->mipmaps[i].write_uniform_set, 0);
  3791. push_constant.cell_offset = gi_probe->mipmaps[i].cell_offset;
  3792. push_constant.cell_count = gi_probe->mipmaps[i].cell_count;
  3793. int wg_todo = (gi_probe->mipmaps[i].cell_count - 1) / wg_size + 1;
  3794. while (wg_todo) {
  3795. int wg_count = MIN(wg_todo, wg_limit_x);
  3796. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GIProbePushConstant));
  3797. RD::get_singleton()->compute_list_dispatch(compute_list, wg_count, 1, 1);
  3798. wg_todo -= wg_count;
  3799. push_constant.cell_offset += wg_count * wg_size;
  3800. }
  3801. }
  3802. }
  3803. RD::get_singleton()->compute_list_end();
  3804. }
  3805. }
  3806. gi_probe->has_dynamic_object_data = false; //clear until dynamic object data is used again
  3807. if (p_dynamic_object_count && gi_probe->dynamic_maps.size()) {
  3808. Vector3i octree_size = storage->gi_probe_get_octree_size(gi_probe->probe);
  3809. int multiplier = gi_probe->dynamic_maps[0].size / MAX(MAX(octree_size.x, octree_size.y), octree_size.z);
  3810. Transform oversample_scale;
  3811. oversample_scale.basis.scale(Vector3(multiplier, multiplier, multiplier));
  3812. Transform to_cell = oversample_scale * storage->gi_probe_get_to_cell_xform(gi_probe->probe);
  3813. Transform to_world_xform = gi_probe->transform * to_cell.affine_inverse();
  3814. Transform to_probe_xform = to_world_xform.affine_inverse();
  3815. AABB probe_aabb(Vector3(), octree_size);
  3816. //this could probably be better parallelized in compute..
  3817. for (int i = 0; i < p_dynamic_object_count; i++) {
  3818. InstanceBase *instance = p_dynamic_objects[i];
  3819. //not used, so clear
  3820. instance->depth_layer = 0;
  3821. instance->depth = 0;
  3822. //transform aabb to giprobe
  3823. AABB aabb = (to_probe_xform * instance->transform).xform(instance->aabb);
  3824. //this needs to wrap to grid resolution to avoid jitter
  3825. //also extend margin a bit just in case
  3826. Vector3i begin = aabb.position - Vector3i(1, 1, 1);
  3827. Vector3i end = aabb.position + aabb.size + Vector3i(1, 1, 1);
  3828. for (int j = 0; j < 3; j++) {
  3829. if ((end[j] - begin[j]) & 1) {
  3830. end[j]++; //for half extents split, it needs to be even
  3831. }
  3832. begin[j] = MAX(begin[j], 0);
  3833. end[j] = MIN(end[j], octree_size[j] * multiplier);
  3834. }
  3835. //aabb = aabb.intersection(probe_aabb); //intersect
  3836. aabb.position = begin;
  3837. aabb.size = end - begin;
  3838. //print_line("aabb: " + aabb);
  3839. for (int j = 0; j < 6; j++) {
  3840. //if (j != 0 && j != 3) {
  3841. // continue;
  3842. //}
  3843. static const Vector3 render_z[6] = {
  3844. Vector3(1, 0, 0),
  3845. Vector3(0, 1, 0),
  3846. Vector3(0, 0, 1),
  3847. Vector3(-1, 0, 0),
  3848. Vector3(0, -1, 0),
  3849. Vector3(0, 0, -1),
  3850. };
  3851. static const Vector3 render_up[6] = {
  3852. Vector3(0, 1, 0),
  3853. Vector3(0, 0, 1),
  3854. Vector3(0, 1, 0),
  3855. Vector3(0, 1, 0),
  3856. Vector3(0, 0, 1),
  3857. Vector3(0, 1, 0),
  3858. };
  3859. Vector3 render_dir = render_z[j];
  3860. Vector3 up_dir = render_up[j];
  3861. Vector3 center = aabb.position + aabb.size * 0.5;
  3862. Transform xform;
  3863. xform.set_look_at(center - aabb.size * 0.5 * render_dir, center, up_dir);
  3864. Vector3 x_dir = xform.basis.get_axis(0).abs();
  3865. int x_axis = int(Vector3(0, 1, 2).dot(x_dir));
  3866. Vector3 y_dir = xform.basis.get_axis(1).abs();
  3867. int y_axis = int(Vector3(0, 1, 2).dot(y_dir));
  3868. Vector3 z_dir = -xform.basis.get_axis(2);
  3869. int z_axis = int(Vector3(0, 1, 2).dot(z_dir.abs()));
  3870. Rect2i rect(aabb.position[x_axis], aabb.position[y_axis], aabb.size[x_axis], aabb.size[y_axis]);
  3871. bool x_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_axis(0)) < 0);
  3872. bool y_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_axis(1)) < 0);
  3873. bool z_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_axis(2)) > 0);
  3874. CameraMatrix cm;
  3875. cm.set_orthogonal(-rect.size.width / 2, rect.size.width / 2, -rect.size.height / 2, rect.size.height / 2, 0.0001, aabb.size[z_axis]);
  3876. _render_material(to_world_xform * xform, cm, true, &instance, 1, gi_probe->dynamic_maps[0].fb, Rect2i(Vector2i(), rect.size));
  3877. GIProbeDynamicPushConstant push_constant;
  3878. zeromem(&push_constant, sizeof(GIProbeDynamicPushConstant));
  3879. push_constant.limits[0] = octree_size.x;
  3880. push_constant.limits[1] = octree_size.y;
  3881. push_constant.limits[2] = octree_size.z;
  3882. push_constant.light_count = p_light_instances.size();
  3883. push_constant.x_dir[0] = x_dir[0];
  3884. push_constant.x_dir[1] = x_dir[1];
  3885. push_constant.x_dir[2] = x_dir[2];
  3886. push_constant.y_dir[0] = y_dir[0];
  3887. push_constant.y_dir[1] = y_dir[1];
  3888. push_constant.y_dir[2] = y_dir[2];
  3889. push_constant.z_dir[0] = z_dir[0];
  3890. push_constant.z_dir[1] = z_dir[1];
  3891. push_constant.z_dir[2] = z_dir[2];
  3892. push_constant.z_base = xform.origin[z_axis];
  3893. push_constant.z_sign = (z_flip ? -1.0 : 1.0);
  3894. push_constant.pos_multiplier = float(1.0) / multiplier;
  3895. push_constant.dynamic_range = storage->gi_probe_get_dynamic_range(gi_probe->probe);
  3896. push_constant.flip_x = x_flip;
  3897. push_constant.flip_y = y_flip;
  3898. push_constant.rect_pos[0] = rect.position[0];
  3899. push_constant.rect_pos[1] = rect.position[1];
  3900. push_constant.rect_size[0] = rect.size[0];
  3901. push_constant.rect_size[1] = rect.size[1];
  3902. push_constant.prev_rect_ofs[0] = 0;
  3903. push_constant.prev_rect_ofs[1] = 0;
  3904. push_constant.prev_rect_size[0] = 0;
  3905. push_constant.prev_rect_size[1] = 0;
  3906. push_constant.on_mipmap = false;
  3907. push_constant.propagation = storage->gi_probe_get_propagation(gi_probe->probe);
  3908. push_constant.pad[0] = 0;
  3909. push_constant.pad[1] = 0;
  3910. push_constant.pad[2] = 0;
  3911. //process lighting
  3912. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  3913. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_DYNAMIC_OBJECT_LIGHTING]);
  3914. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->dynamic_maps[0].uniform_set, 0);
  3915. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GIProbeDynamicPushConstant));
  3916. RD::get_singleton()->compute_list_dispatch(compute_list, (rect.size.x - 1) / 8 + 1, (rect.size.y - 1) / 8 + 1, 1);
  3917. //print_line("rect: " + itos(i) + ": " + rect);
  3918. for (int k = 1; k < gi_probe->dynamic_maps.size(); k++) {
  3919. // enlarge the rect if needed so all pixels fit when downscaled,
  3920. // this ensures downsampling is smooth and optimal because no pixels are left behind
  3921. //x
  3922. if (rect.position.x & 1) {
  3923. rect.size.x++;
  3924. push_constant.prev_rect_ofs[0] = 1; //this is used to ensure reading is also optimal
  3925. } else {
  3926. push_constant.prev_rect_ofs[0] = 0;
  3927. }
  3928. if (rect.size.x & 1) {
  3929. rect.size.x++;
  3930. }
  3931. rect.position.x >>= 1;
  3932. rect.size.x = MAX(1, rect.size.x >> 1);
  3933. //y
  3934. if (rect.position.y & 1) {
  3935. rect.size.y++;
  3936. push_constant.prev_rect_ofs[1] = 1;
  3937. } else {
  3938. push_constant.prev_rect_ofs[1] = 0;
  3939. }
  3940. if (rect.size.y & 1) {
  3941. rect.size.y++;
  3942. }
  3943. rect.position.y >>= 1;
  3944. rect.size.y = MAX(1, rect.size.y >> 1);
  3945. //shrink limits to ensure plot does not go outside map
  3946. if (gi_probe->dynamic_maps[k].mipmap > 0) {
  3947. for (int l = 0; l < 3; l++) {
  3948. push_constant.limits[l] = MAX(1, push_constant.limits[l] >> 1);
  3949. }
  3950. }
  3951. //print_line("rect: " + itos(i) + ": " + rect);
  3952. push_constant.rect_pos[0] = rect.position[0];
  3953. push_constant.rect_pos[1] = rect.position[1];
  3954. push_constant.prev_rect_size[0] = push_constant.rect_size[0];
  3955. push_constant.prev_rect_size[1] = push_constant.rect_size[1];
  3956. push_constant.rect_size[0] = rect.size[0];
  3957. push_constant.rect_size[1] = rect.size[1];
  3958. push_constant.on_mipmap = gi_probe->dynamic_maps[k].mipmap > 0;
  3959. RD::get_singleton()->compute_list_add_barrier(compute_list);
  3960. if (gi_probe->dynamic_maps[k].mipmap < 0) {
  3961. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_WRITE]);
  3962. } else if (k < gi_probe->dynamic_maps.size() - 1) {
  3963. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_WRITE_PLOT]);
  3964. } else {
  3965. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_PLOT]);
  3966. }
  3967. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->dynamic_maps[k].uniform_set, 0);
  3968. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GIProbeDynamicPushConstant));
  3969. RD::get_singleton()->compute_list_dispatch(compute_list, (rect.size.x - 1) / 8 + 1, (rect.size.y - 1) / 8 + 1, 1);
  3970. }
  3971. RD::get_singleton()->compute_list_end();
  3972. }
  3973. }
  3974. gi_probe->has_dynamic_object_data = true; //clear until dynamic object data is used again
  3975. }
  3976. gi_probe->last_probe_version = storage->gi_probe_get_version(gi_probe->probe);
  3977. }
  3978. void RendererSceneRenderRD::_debug_giprobe(RID p_gi_probe, RD::DrawListID p_draw_list, RID p_framebuffer, const CameraMatrix &p_camera_with_transform, bool p_lighting, bool p_emission, float p_alpha) {
  3979. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_gi_probe);
  3980. ERR_FAIL_COND(!gi_probe);
  3981. if (gi_probe->mipmaps.size() == 0) {
  3982. return;
  3983. }
  3984. CameraMatrix transform = (p_camera_with_transform * CameraMatrix(gi_probe->transform)) * CameraMatrix(storage->gi_probe_get_to_cell_xform(gi_probe->probe).affine_inverse());
  3985. int level = 0;
  3986. Vector3i octree_size = storage->gi_probe_get_octree_size(gi_probe->probe);
  3987. GIProbeDebugPushConstant push_constant;
  3988. push_constant.alpha = p_alpha;
  3989. push_constant.dynamic_range = storage->gi_probe_get_dynamic_range(gi_probe->probe);
  3990. push_constant.cell_offset = gi_probe->mipmaps[level].cell_offset;
  3991. push_constant.level = level;
  3992. push_constant.bounds[0] = octree_size.x >> level;
  3993. push_constant.bounds[1] = octree_size.y >> level;
  3994. push_constant.bounds[2] = octree_size.z >> level;
  3995. push_constant.pad = 0;
  3996. for (int i = 0; i < 4; i++) {
  3997. for (int j = 0; j < 4; j++) {
  3998. push_constant.projection[i * 4 + j] = transform.matrix[i][j];
  3999. }
  4000. }
  4001. if (giprobe_debug_uniform_set.is_valid()) {
  4002. RD::get_singleton()->free(giprobe_debug_uniform_set);
  4003. }
  4004. Vector<RD::Uniform> uniforms;
  4005. {
  4006. RD::Uniform u;
  4007. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  4008. u.binding = 1;
  4009. u.ids.push_back(storage->gi_probe_get_data_buffer(gi_probe->probe));
  4010. uniforms.push_back(u);
  4011. }
  4012. {
  4013. RD::Uniform u;
  4014. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  4015. u.binding = 2;
  4016. u.ids.push_back(gi_probe->texture);
  4017. uniforms.push_back(u);
  4018. }
  4019. {
  4020. RD::Uniform u;
  4021. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  4022. u.binding = 3;
  4023. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  4024. uniforms.push_back(u);
  4025. }
  4026. int cell_count;
  4027. if (!p_emission && p_lighting && gi_probe->has_dynamic_object_data) {
  4028. cell_count = push_constant.bounds[0] * push_constant.bounds[1] * push_constant.bounds[2];
  4029. } else {
  4030. cell_count = gi_probe->mipmaps[level].cell_count;
  4031. }
  4032. giprobe_debug_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_debug_shader_version_shaders[0], 0);
  4033. RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, giprobe_debug_shader_version_pipelines[p_emission ? GI_PROBE_DEBUG_EMISSION : p_lighting ? (gi_probe->has_dynamic_object_data ? GI_PROBE_DEBUG_LIGHT_FULL : GI_PROBE_DEBUG_LIGHT) : GI_PROBE_DEBUG_COLOR].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_framebuffer)));
  4034. RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, giprobe_debug_uniform_set, 0);
  4035. RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(GIProbeDebugPushConstant));
  4036. RD::get_singleton()->draw_list_draw(p_draw_list, false, cell_count, 36);
  4037. }
  4038. void RendererSceneRenderRD::_debug_sdfgi_probes(RID p_render_buffers, RD::DrawListID p_draw_list, RID p_framebuffer, const CameraMatrix &p_camera_with_transform) {
  4039. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4040. ERR_FAIL_COND(!rb);
  4041. if (!rb->sdfgi) {
  4042. return; //nothing to debug
  4043. }
  4044. SDGIShader::DebugProbesPushConstant push_constant;
  4045. for (int i = 0; i < 4; i++) {
  4046. for (int j = 0; j < 4; j++) {
  4047. push_constant.projection[i * 4 + j] = p_camera_with_transform.matrix[i][j];
  4048. }
  4049. }
  4050. //gen spheres from strips
  4051. uint32_t band_points = 16;
  4052. push_constant.band_power = 4;
  4053. push_constant.sections_in_band = ((band_points / 2) - 1);
  4054. push_constant.band_mask = band_points - 2;
  4055. push_constant.section_arc = (Math_PI * 2.0) / float(push_constant.sections_in_band);
  4056. push_constant.y_mult = rb->sdfgi->y_mult;
  4057. uint32_t total_points = push_constant.sections_in_band * band_points;
  4058. uint32_t total_probes = rb->sdfgi->probe_axis_count * rb->sdfgi->probe_axis_count * rb->sdfgi->probe_axis_count;
  4059. push_constant.grid_size[0] = rb->sdfgi->cascade_size;
  4060. push_constant.grid_size[1] = rb->sdfgi->cascade_size;
  4061. push_constant.grid_size[2] = rb->sdfgi->cascade_size;
  4062. push_constant.cascade = 0;
  4063. push_constant.probe_axis_size = rb->sdfgi->probe_axis_count;
  4064. if (!rb->sdfgi->debug_probes_uniform_set.is_valid() || !RD::get_singleton()->uniform_set_is_valid(rb->sdfgi->debug_probes_uniform_set)) {
  4065. Vector<RD::Uniform> uniforms;
  4066. {
  4067. RD::Uniform u;
  4068. u.binding = 1;
  4069. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  4070. u.ids.push_back(rb->sdfgi->cascades_ubo);
  4071. uniforms.push_back(u);
  4072. }
  4073. {
  4074. RD::Uniform u;
  4075. u.binding = 2;
  4076. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  4077. u.ids.push_back(rb->sdfgi->lightprobe_texture);
  4078. uniforms.push_back(u);
  4079. }
  4080. {
  4081. RD::Uniform u;
  4082. u.binding = 3;
  4083. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  4084. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  4085. uniforms.push_back(u);
  4086. }
  4087. {
  4088. RD::Uniform u;
  4089. u.binding = 4;
  4090. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  4091. u.ids.push_back(rb->sdfgi->occlusion_texture);
  4092. uniforms.push_back(u);
  4093. }
  4094. rb->sdfgi->debug_probes_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.debug_probes.version_get_shader(sdfgi_shader.debug_probes_shader, 0), 0);
  4095. }
  4096. RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, sdfgi_shader.debug_probes_pipeline[SDGIShader::PROBE_DEBUG_PROBES].get_render_pipeline(RD::INVALID_FORMAT_ID, RD::get_singleton()->framebuffer_get_format(p_framebuffer)));
  4097. RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, rb->sdfgi->debug_probes_uniform_set, 0);
  4098. RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(SDGIShader::DebugProbesPushConstant));
  4099. RD::get_singleton()->draw_list_draw(p_draw_list, false, total_probes, total_points);
  4100. if (sdfgi_debug_probe_dir != Vector3()) {
  4101. print_line("CLICK DEBUG ME?");
  4102. uint32_t cascade = 0;
  4103. Vector3 offset = Vector3((Vector3i(1, 1, 1) * -int32_t(rb->sdfgi->cascade_size >> 1) + rb->sdfgi->cascades[cascade].position)) * rb->sdfgi->cascades[cascade].cell_size * Vector3(1.0, 1.0 / rb->sdfgi->y_mult, 1.0);
  4104. Vector3 probe_size = rb->sdfgi->cascades[cascade].cell_size * (rb->sdfgi->cascade_size / SDFGI::PROBE_DIVISOR) * Vector3(1.0, 1.0 / rb->sdfgi->y_mult, 1.0);
  4105. Vector3 ray_from = sdfgi_debug_probe_pos;
  4106. Vector3 ray_to = sdfgi_debug_probe_pos + sdfgi_debug_probe_dir * rb->sdfgi->cascades[cascade].cell_size * Math::sqrt(3.0) * rb->sdfgi->cascade_size;
  4107. float sphere_radius = 0.2;
  4108. float closest_dist = 1e20;
  4109. sdfgi_debug_probe_enabled = false;
  4110. Vector3i probe_from = rb->sdfgi->cascades[cascade].position / (rb->sdfgi->cascade_size / SDFGI::PROBE_DIVISOR);
  4111. for (int i = 0; i < (SDFGI::PROBE_DIVISOR + 1); i++) {
  4112. for (int j = 0; j < (SDFGI::PROBE_DIVISOR + 1); j++) {
  4113. for (int k = 0; k < (SDFGI::PROBE_DIVISOR + 1); k++) {
  4114. Vector3 pos = offset + probe_size * Vector3(i, j, k);
  4115. Vector3 res;
  4116. if (Geometry3D::segment_intersects_sphere(ray_from, ray_to, pos, sphere_radius, &res)) {
  4117. float d = ray_from.distance_to(res);
  4118. if (d < closest_dist) {
  4119. closest_dist = d;
  4120. sdfgi_debug_probe_enabled = true;
  4121. sdfgi_debug_probe_index = probe_from + Vector3i(i, j, k);
  4122. }
  4123. }
  4124. }
  4125. }
  4126. }
  4127. if (sdfgi_debug_probe_enabled) {
  4128. print_line("found: " + sdfgi_debug_probe_index);
  4129. } else {
  4130. print_line("no found");
  4131. }
  4132. sdfgi_debug_probe_dir = Vector3();
  4133. }
  4134. if (sdfgi_debug_probe_enabled) {
  4135. uint32_t cascade = 0;
  4136. uint32_t probe_cells = (rb->sdfgi->cascade_size / SDFGI::PROBE_DIVISOR);
  4137. Vector3i probe_from = rb->sdfgi->cascades[cascade].position / probe_cells;
  4138. Vector3i ofs = sdfgi_debug_probe_index - probe_from;
  4139. if (ofs.x < 0 || ofs.y < 0 || ofs.z < 0) {
  4140. return;
  4141. }
  4142. if (ofs.x > SDFGI::PROBE_DIVISOR || ofs.y > SDFGI::PROBE_DIVISOR || ofs.z > SDFGI::PROBE_DIVISOR) {
  4143. return;
  4144. }
  4145. uint32_t mult = (SDFGI::PROBE_DIVISOR + 1);
  4146. uint32_t index = ofs.z * mult * mult + ofs.y * mult + ofs.x;
  4147. push_constant.probe_debug_index = index;
  4148. uint32_t cell_count = probe_cells * 2 * probe_cells * 2 * probe_cells * 2;
  4149. RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, sdfgi_shader.debug_probes_pipeline[SDGIShader::PROBE_DEBUG_VISIBILITY].get_render_pipeline(RD::INVALID_FORMAT_ID, RD::get_singleton()->framebuffer_get_format(p_framebuffer)));
  4150. RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, rb->sdfgi->debug_probes_uniform_set, 0);
  4151. RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(SDGIShader::DebugProbesPushConstant));
  4152. RD::get_singleton()->draw_list_draw(p_draw_list, false, cell_count, total_points);
  4153. }
  4154. }
  4155. ////////////////////////////////
  4156. RID RendererSceneRenderRD::render_buffers_create() {
  4157. RenderBuffers rb;
  4158. rb.data = _create_render_buffer_data();
  4159. return render_buffers_owner.make_rid(rb);
  4160. }
  4161. void RendererSceneRenderRD::_allocate_blur_textures(RenderBuffers *rb) {
  4162. ERR_FAIL_COND(!rb->blur[0].texture.is_null());
  4163. uint32_t mipmaps_required = Image::get_image_required_mipmaps(rb->width, rb->height, Image::FORMAT_RGBAH);
  4164. RD::TextureFormat tf;
  4165. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  4166. tf.width = rb->width;
  4167. tf.height = rb->height;
  4168. tf.texture_type = RD::TEXTURE_TYPE_2D;
  4169. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  4170. tf.mipmaps = mipmaps_required;
  4171. rb->blur[0].texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4172. //the second one is smaller (only used for separatable part of blur)
  4173. tf.width >>= 1;
  4174. tf.height >>= 1;
  4175. tf.mipmaps--;
  4176. rb->blur[1].texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4177. int base_width = rb->width;
  4178. int base_height = rb->height;
  4179. for (uint32_t i = 0; i < mipmaps_required; i++) {
  4180. RenderBuffers::Blur::Mipmap mm;
  4181. mm.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rb->blur[0].texture, 0, i);
  4182. mm.width = base_width;
  4183. mm.height = base_height;
  4184. rb->blur[0].mipmaps.push_back(mm);
  4185. if (i > 0) {
  4186. mm.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rb->blur[1].texture, 0, i - 1);
  4187. rb->blur[1].mipmaps.push_back(mm);
  4188. }
  4189. base_width = MAX(1, base_width >> 1);
  4190. base_height = MAX(1, base_height >> 1);
  4191. }
  4192. }
  4193. void RendererSceneRenderRD::_allocate_luminance_textures(RenderBuffers *rb) {
  4194. ERR_FAIL_COND(!rb->luminance.current.is_null());
  4195. int w = rb->width;
  4196. int h = rb->height;
  4197. while (true) {
  4198. w = MAX(w / 8, 1);
  4199. h = MAX(h / 8, 1);
  4200. RD::TextureFormat tf;
  4201. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  4202. tf.width = w;
  4203. tf.height = h;
  4204. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  4205. bool final = w == 1 && h == 1;
  4206. if (final) {
  4207. tf.usage_bits |= RD::TEXTURE_USAGE_SAMPLING_BIT;
  4208. }
  4209. RID texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4210. rb->luminance.reduce.push_back(texture);
  4211. if (final) {
  4212. rb->luminance.current = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4213. break;
  4214. }
  4215. }
  4216. }
  4217. void RendererSceneRenderRD::_free_render_buffer_data(RenderBuffers *rb) {
  4218. if (rb->texture.is_valid()) {
  4219. RD::get_singleton()->free(rb->texture);
  4220. rb->texture = RID();
  4221. }
  4222. if (rb->depth_texture.is_valid()) {
  4223. RD::get_singleton()->free(rb->depth_texture);
  4224. rb->depth_texture = RID();
  4225. }
  4226. for (int i = 0; i < 2; i++) {
  4227. if (rb->blur[i].texture.is_valid()) {
  4228. RD::get_singleton()->free(rb->blur[i].texture);
  4229. rb->blur[i].texture = RID();
  4230. rb->blur[i].mipmaps.clear();
  4231. }
  4232. }
  4233. for (int i = 0; i < rb->luminance.reduce.size(); i++) {
  4234. RD::get_singleton()->free(rb->luminance.reduce[i]);
  4235. }
  4236. for (int i = 0; i < rb->luminance.reduce.size(); i++) {
  4237. RD::get_singleton()->free(rb->luminance.reduce[i]);
  4238. }
  4239. rb->luminance.reduce.clear();
  4240. if (rb->luminance.current.is_valid()) {
  4241. RD::get_singleton()->free(rb->luminance.current);
  4242. rb->luminance.current = RID();
  4243. }
  4244. if (rb->ssao.ao[0].is_valid()) {
  4245. RD::get_singleton()->free(rb->ssao.depth);
  4246. RD::get_singleton()->free(rb->ssao.ao[0]);
  4247. if (rb->ssao.ao[1].is_valid()) {
  4248. RD::get_singleton()->free(rb->ssao.ao[1]);
  4249. }
  4250. if (rb->ssao.ao_full.is_valid()) {
  4251. RD::get_singleton()->free(rb->ssao.ao_full);
  4252. }
  4253. rb->ssao.depth = RID();
  4254. rb->ssao.ao[0] = RID();
  4255. rb->ssao.ao[1] = RID();
  4256. rb->ssao.ao_full = RID();
  4257. rb->ssao.depth_slices.clear();
  4258. }
  4259. if (rb->ssr.blur_radius[0].is_valid()) {
  4260. RD::get_singleton()->free(rb->ssr.blur_radius[0]);
  4261. RD::get_singleton()->free(rb->ssr.blur_radius[1]);
  4262. rb->ssr.blur_radius[0] = RID();
  4263. rb->ssr.blur_radius[1] = RID();
  4264. }
  4265. if (rb->ssr.depth_scaled.is_valid()) {
  4266. RD::get_singleton()->free(rb->ssr.depth_scaled);
  4267. rb->ssr.depth_scaled = RID();
  4268. RD::get_singleton()->free(rb->ssr.normal_scaled);
  4269. rb->ssr.normal_scaled = RID();
  4270. }
  4271. }
  4272. void RendererSceneRenderRD::_process_sss(RID p_render_buffers, const CameraMatrix &p_camera) {
  4273. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4274. ERR_FAIL_COND(!rb);
  4275. bool can_use_effects = rb->width >= 8 && rb->height >= 8;
  4276. if (!can_use_effects) {
  4277. //just copy
  4278. return;
  4279. }
  4280. if (rb->blur[0].texture.is_null()) {
  4281. _allocate_blur_textures(rb);
  4282. _render_buffers_uniform_set_changed(p_render_buffers);
  4283. }
  4284. storage->get_effects()->sub_surface_scattering(rb->texture, rb->blur[0].mipmaps[0].texture, rb->depth_texture, p_camera, Size2i(rb->width, rb->height), sss_scale, sss_depth_scale, sss_quality);
  4285. }
  4286. void RendererSceneRenderRD::_process_ssr(RID p_render_buffers, RID p_dest_framebuffer, RID p_normal_buffer, RID p_specular_buffer, RID p_metallic, const Color &p_metallic_mask, RID p_environment, const CameraMatrix &p_projection, bool p_use_additive) {
  4287. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4288. ERR_FAIL_COND(!rb);
  4289. bool can_use_effects = rb->width >= 8 && rb->height >= 8;
  4290. if (!can_use_effects) {
  4291. //just copy
  4292. storage->get_effects()->merge_specular(p_dest_framebuffer, p_specular_buffer, p_use_additive ? RID() : rb->texture, RID());
  4293. return;
  4294. }
  4295. Environment *env = environment_owner.getornull(p_environment);
  4296. ERR_FAIL_COND(!env);
  4297. ERR_FAIL_COND(!env->ssr_enabled);
  4298. if (rb->ssr.depth_scaled.is_null()) {
  4299. RD::TextureFormat tf;
  4300. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  4301. tf.width = rb->width / 2;
  4302. tf.height = rb->height / 2;
  4303. tf.texture_type = RD::TEXTURE_TYPE_2D;
  4304. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  4305. rb->ssr.depth_scaled = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4306. tf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  4307. rb->ssr.normal_scaled = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4308. }
  4309. if (ssr_roughness_quality != RS::ENV_SSR_ROUGNESS_QUALITY_DISABLED && !rb->ssr.blur_radius[0].is_valid()) {
  4310. RD::TextureFormat tf;
  4311. tf.format = RD::DATA_FORMAT_R8_UNORM;
  4312. tf.width = rb->width / 2;
  4313. tf.height = rb->height / 2;
  4314. tf.texture_type = RD::TEXTURE_TYPE_2D;
  4315. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  4316. rb->ssr.blur_radius[0] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4317. rb->ssr.blur_radius[1] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4318. }
  4319. if (rb->blur[0].texture.is_null()) {
  4320. _allocate_blur_textures(rb);
  4321. _render_buffers_uniform_set_changed(p_render_buffers);
  4322. }
  4323. storage->get_effects()->screen_space_reflection(rb->texture, p_normal_buffer, ssr_roughness_quality, rb->ssr.blur_radius[0], rb->ssr.blur_radius[1], p_metallic, p_metallic_mask, rb->depth_texture, rb->ssr.depth_scaled, rb->ssr.normal_scaled, rb->blur[0].mipmaps[1].texture, rb->blur[1].mipmaps[0].texture, Size2i(rb->width / 2, rb->height / 2), env->ssr_max_steps, env->ssr_fade_in, env->ssr_fade_out, env->ssr_depth_tolerance, p_projection);
  4324. storage->get_effects()->merge_specular(p_dest_framebuffer, p_specular_buffer, p_use_additive ? RID() : rb->texture, rb->blur[0].mipmaps[1].texture);
  4325. }
  4326. void RendererSceneRenderRD::_process_ssao(RID p_render_buffers, RID p_environment, RID p_normal_buffer, const CameraMatrix &p_projection) {
  4327. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4328. ERR_FAIL_COND(!rb);
  4329. Environment *env = environment_owner.getornull(p_environment);
  4330. ERR_FAIL_COND(!env);
  4331. RENDER_TIMESTAMP("Process SSAO");
  4332. if (rb->ssao.ao[0].is_valid() && rb->ssao.ao_full.is_valid() != ssao_half_size) {
  4333. RD::get_singleton()->free(rb->ssao.depth);
  4334. RD::get_singleton()->free(rb->ssao.ao[0]);
  4335. if (rb->ssao.ao[1].is_valid()) {
  4336. RD::get_singleton()->free(rb->ssao.ao[1]);
  4337. }
  4338. if (rb->ssao.ao_full.is_valid()) {
  4339. RD::get_singleton()->free(rb->ssao.ao_full);
  4340. }
  4341. rb->ssao.depth = RID();
  4342. rb->ssao.ao[0] = RID();
  4343. rb->ssao.ao[1] = RID();
  4344. rb->ssao.ao_full = RID();
  4345. rb->ssao.depth_slices.clear();
  4346. }
  4347. if (!rb->ssao.ao[0].is_valid()) {
  4348. //allocate depth slices
  4349. {
  4350. RD::TextureFormat tf;
  4351. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  4352. tf.width = rb->width / 2;
  4353. tf.height = rb->height / 2;
  4354. tf.mipmaps = Image::get_image_required_mipmaps(tf.width, tf.height, Image::FORMAT_RF) + 1;
  4355. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  4356. rb->ssao.depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4357. for (uint32_t i = 0; i < tf.mipmaps; i++) {
  4358. RID slice = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rb->ssao.depth, 0, i);
  4359. rb->ssao.depth_slices.push_back(slice);
  4360. }
  4361. }
  4362. {
  4363. RD::TextureFormat tf;
  4364. tf.format = RD::DATA_FORMAT_R8_UNORM;
  4365. tf.width = ssao_half_size ? rb->width / 2 : rb->width;
  4366. tf.height = ssao_half_size ? rb->height / 2 : rb->height;
  4367. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  4368. rb->ssao.ao[0] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4369. rb->ssao.ao[1] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4370. }
  4371. if (ssao_half_size) {
  4372. //upsample texture
  4373. RD::TextureFormat tf;
  4374. tf.format = RD::DATA_FORMAT_R8_UNORM;
  4375. tf.width = rb->width;
  4376. tf.height = rb->height;
  4377. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  4378. rb->ssao.ao_full = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4379. }
  4380. _render_buffers_uniform_set_changed(p_render_buffers);
  4381. }
  4382. storage->get_effects()->generate_ssao(rb->depth_texture, p_normal_buffer, Size2i(rb->width, rb->height), rb->ssao.depth, rb->ssao.depth_slices, rb->ssao.ao[0], rb->ssao.ao_full.is_valid(), rb->ssao.ao[1], rb->ssao.ao_full, env->ssao_intensity, env->ssao_radius, env->ssao_bias, p_projection, ssao_quality, env->ssao_blur, env->ssao_blur_edge_sharpness);
  4383. }
  4384. void RendererSceneRenderRD::_render_buffers_post_process_and_tonemap(RID p_render_buffers, RID p_environment, RID p_camera_effects, const CameraMatrix &p_projection) {
  4385. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4386. ERR_FAIL_COND(!rb);
  4387. Environment *env = environment_owner.getornull(p_environment);
  4388. //glow (if enabled)
  4389. CameraEffects *camfx = camera_effects_owner.getornull(p_camera_effects);
  4390. bool can_use_effects = rb->width >= 8 && rb->height >= 8;
  4391. if (can_use_effects && camfx && (camfx->dof_blur_near_enabled || camfx->dof_blur_far_enabled) && camfx->dof_blur_amount > 0.0) {
  4392. if (rb->blur[0].texture.is_null()) {
  4393. _allocate_blur_textures(rb);
  4394. _render_buffers_uniform_set_changed(p_render_buffers);
  4395. }
  4396. float bokeh_size = camfx->dof_blur_amount * 64.0;
  4397. storage->get_effects()->bokeh_dof(rb->texture, rb->depth_texture, Size2i(rb->width, rb->height), rb->blur[0].mipmaps[0].texture, rb->blur[1].mipmaps[0].texture, rb->blur[0].mipmaps[1].texture, camfx->dof_blur_far_enabled, camfx->dof_blur_far_distance, camfx->dof_blur_far_transition, camfx->dof_blur_near_enabled, camfx->dof_blur_near_distance, camfx->dof_blur_near_transition, bokeh_size, dof_blur_bokeh_shape, dof_blur_quality, dof_blur_use_jitter, p_projection.get_z_near(), p_projection.get_z_far(), p_projection.is_orthogonal());
  4398. }
  4399. if (can_use_effects && env && env->auto_exposure) {
  4400. if (rb->luminance.current.is_null()) {
  4401. _allocate_luminance_textures(rb);
  4402. _render_buffers_uniform_set_changed(p_render_buffers);
  4403. }
  4404. bool set_immediate = env->auto_exposure_version != rb->auto_exposure_version;
  4405. rb->auto_exposure_version = env->auto_exposure_version;
  4406. double step = env->auto_exp_speed * time_step;
  4407. storage->get_effects()->luminance_reduction(rb->texture, Size2i(rb->width, rb->height), rb->luminance.reduce, rb->luminance.current, env->min_luminance, env->max_luminance, step, set_immediate);
  4408. //swap final reduce with prev luminance
  4409. SWAP(rb->luminance.current, rb->luminance.reduce.write[rb->luminance.reduce.size() - 1]);
  4410. RenderingServerDefault::redraw_request(); //redraw all the time if auto exposure rendering is on
  4411. }
  4412. int max_glow_level = -1;
  4413. if (can_use_effects && env && env->glow_enabled) {
  4414. /* see that blur textures are allocated */
  4415. if (rb->blur[1].texture.is_null()) {
  4416. _allocate_blur_textures(rb);
  4417. _render_buffers_uniform_set_changed(p_render_buffers);
  4418. }
  4419. for (int i = 0; i < RS::MAX_GLOW_LEVELS; i++) {
  4420. if (env->glow_levels[i] > 0.0) {
  4421. if (i >= rb->blur[1].mipmaps.size()) {
  4422. max_glow_level = rb->blur[1].mipmaps.size() - 1;
  4423. } else {
  4424. max_glow_level = i;
  4425. }
  4426. }
  4427. }
  4428. for (int i = 0; i < (max_glow_level + 1); i++) {
  4429. int vp_w = rb->blur[1].mipmaps[i].width;
  4430. int vp_h = rb->blur[1].mipmaps[i].height;
  4431. if (i == 0) {
  4432. RID luminance_texture;
  4433. if (env->auto_exposure && rb->luminance.current.is_valid()) {
  4434. luminance_texture = rb->luminance.current;
  4435. }
  4436. storage->get_effects()->gaussian_glow(rb->texture, rb->blur[1].mipmaps[i].texture, Size2i(vp_w, vp_h), env->glow_strength, glow_high_quality, true, env->glow_hdr_luminance_cap, env->exposure, env->glow_bloom, env->glow_hdr_bleed_threshold, env->glow_hdr_bleed_scale, luminance_texture, env->auto_exp_scale);
  4437. } else {
  4438. storage->get_effects()->gaussian_glow(rb->blur[1].mipmaps[i - 1].texture, rb->blur[1].mipmaps[i].texture, Size2i(vp_w, vp_h), env->glow_strength, glow_high_quality);
  4439. }
  4440. }
  4441. }
  4442. {
  4443. //tonemap
  4444. EffectsRD::TonemapSettings tonemap;
  4445. if (can_use_effects && env && env->auto_exposure && rb->luminance.current.is_valid()) {
  4446. tonemap.use_auto_exposure = true;
  4447. tonemap.exposure_texture = rb->luminance.current;
  4448. tonemap.auto_exposure_grey = env->auto_exp_scale;
  4449. } else {
  4450. tonemap.exposure_texture = storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_WHITE);
  4451. }
  4452. if (can_use_effects && env && env->glow_enabled) {
  4453. tonemap.use_glow = true;
  4454. tonemap.glow_mode = EffectsRD::TonemapSettings::GlowMode(env->glow_blend_mode);
  4455. tonemap.glow_intensity = env->glow_blend_mode == RS::ENV_GLOW_BLEND_MODE_MIX ? env->glow_mix : env->glow_intensity;
  4456. for (int i = 0; i < RS::MAX_GLOW_LEVELS; i++) {
  4457. tonemap.glow_levels[i] = env->glow_levels[i];
  4458. }
  4459. tonemap.glow_texture_size.x = rb->blur[1].mipmaps[0].width;
  4460. tonemap.glow_texture_size.y = rb->blur[1].mipmaps[0].height;
  4461. tonemap.glow_use_bicubic_upscale = glow_bicubic_upscale;
  4462. tonemap.glow_texture = rb->blur[1].texture;
  4463. } else {
  4464. tonemap.glow_texture = storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_BLACK);
  4465. }
  4466. if (rb->screen_space_aa == RS::VIEWPORT_SCREEN_SPACE_AA_FXAA) {
  4467. tonemap.use_fxaa = true;
  4468. }
  4469. tonemap.use_debanding = rb->use_debanding;
  4470. tonemap.texture_size = Vector2i(rb->width, rb->height);
  4471. if (env) {
  4472. tonemap.tonemap_mode = env->tone_mapper;
  4473. tonemap.white = env->white;
  4474. tonemap.exposure = env->exposure;
  4475. }
  4476. tonemap.use_color_correction = false;
  4477. tonemap.use_1d_color_correction = false;
  4478. tonemap.color_correction_texture = storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE);
  4479. if (can_use_effects && env) {
  4480. tonemap.use_bcs = env->adjustments_enabled;
  4481. tonemap.brightness = env->adjustments_brightness;
  4482. tonemap.contrast = env->adjustments_contrast;
  4483. tonemap.saturation = env->adjustments_saturation;
  4484. if (env->adjustments_enabled && env->color_correction.is_valid()) {
  4485. tonemap.use_color_correction = true;
  4486. tonemap.use_1d_color_correction = env->use_1d_color_correction;
  4487. tonemap.color_correction_texture = storage->texture_get_rd_texture(env->color_correction);
  4488. }
  4489. }
  4490. storage->get_effects()->tonemapper(rb->texture, storage->render_target_get_rd_framebuffer(rb->render_target), tonemap);
  4491. }
  4492. storage->render_target_disable_clear_request(rb->render_target);
  4493. }
  4494. void RendererSceneRenderRD::_render_buffers_debug_draw(RID p_render_buffers, RID p_shadow_atlas) {
  4495. EffectsRD *effects = storage->get_effects();
  4496. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4497. ERR_FAIL_COND(!rb);
  4498. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_SHADOW_ATLAS) {
  4499. if (p_shadow_atlas.is_valid()) {
  4500. RID shadow_atlas_texture = shadow_atlas_get_texture(p_shadow_atlas);
  4501. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  4502. effects->copy_to_fb_rect(shadow_atlas_texture, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2i(Vector2(), rtsize / 2), false, true);
  4503. }
  4504. }
  4505. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS) {
  4506. if (directional_shadow_get_texture().is_valid()) {
  4507. RID shadow_atlas_texture = directional_shadow_get_texture();
  4508. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  4509. effects->copy_to_fb_rect(shadow_atlas_texture, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2i(Vector2(), rtsize / 2), false, true);
  4510. }
  4511. }
  4512. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_DECAL_ATLAS) {
  4513. RID decal_atlas = storage->decal_atlas_get_texture();
  4514. if (decal_atlas.is_valid()) {
  4515. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  4516. effects->copy_to_fb_rect(decal_atlas, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2i(Vector2(), rtsize / 2), false, false, true);
  4517. }
  4518. }
  4519. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_SCENE_LUMINANCE) {
  4520. if (rb->luminance.current.is_valid()) {
  4521. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  4522. effects->copy_to_fb_rect(rb->luminance.current, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize / 8), false, true);
  4523. }
  4524. }
  4525. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_SSAO && rb->ssao.ao[0].is_valid()) {
  4526. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  4527. RID ao_buf = rb->ssao.ao_full.is_valid() ? rb->ssao.ao_full : rb->ssao.ao[0];
  4528. effects->copy_to_fb_rect(ao_buf, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize), false, true);
  4529. }
  4530. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_NORMAL_BUFFER && _render_buffers_get_normal_texture(p_render_buffers).is_valid()) {
  4531. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  4532. effects->copy_to_fb_rect(_render_buffers_get_normal_texture(p_render_buffers), storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize), false, false);
  4533. }
  4534. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_GI_BUFFER && _render_buffers_get_ambient_texture(p_render_buffers).is_valid()) {
  4535. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  4536. RID ambient_texture = _render_buffers_get_ambient_texture(p_render_buffers);
  4537. RID reflection_texture = _render_buffers_get_reflection_texture(p_render_buffers);
  4538. effects->copy_to_fb_rect(ambient_texture, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize), false, false, false, true, reflection_texture);
  4539. }
  4540. }
  4541. void RendererSceneRenderRD::environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, bool p_use_1d_color_correction, RID p_color_correction) {
  4542. Environment *env = environment_owner.getornull(p_env);
  4543. ERR_FAIL_COND(!env);
  4544. env->adjustments_enabled = p_enable;
  4545. env->adjustments_brightness = p_brightness;
  4546. env->adjustments_contrast = p_contrast;
  4547. env->adjustments_saturation = p_saturation;
  4548. env->use_1d_color_correction = p_use_1d_color_correction;
  4549. env->color_correction = p_color_correction;
  4550. }
  4551. void RendererSceneRenderRD::_sdfgi_debug_draw(RID p_render_buffers, const CameraMatrix &p_projection, const Transform &p_transform) {
  4552. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4553. ERR_FAIL_COND(!rb);
  4554. if (!rb->sdfgi) {
  4555. return; //eh
  4556. }
  4557. if (!rb->sdfgi->debug_uniform_set.is_valid() || !RD::get_singleton()->uniform_set_is_valid(rb->sdfgi->debug_uniform_set)) {
  4558. Vector<RD::Uniform> uniforms;
  4559. {
  4560. RD::Uniform u;
  4561. u.binding = 1;
  4562. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  4563. for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
  4564. if (i < rb->sdfgi->cascades.size()) {
  4565. u.ids.push_back(rb->sdfgi->cascades[i].sdf_tex);
  4566. } else {
  4567. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  4568. }
  4569. }
  4570. uniforms.push_back(u);
  4571. }
  4572. {
  4573. RD::Uniform u;
  4574. u.binding = 2;
  4575. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  4576. for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
  4577. if (i < rb->sdfgi->cascades.size()) {
  4578. u.ids.push_back(rb->sdfgi->cascades[i].light_tex);
  4579. } else {
  4580. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  4581. }
  4582. }
  4583. uniforms.push_back(u);
  4584. }
  4585. {
  4586. RD::Uniform u;
  4587. u.binding = 3;
  4588. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  4589. for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
  4590. if (i < rb->sdfgi->cascades.size()) {
  4591. u.ids.push_back(rb->sdfgi->cascades[i].light_aniso_0_tex);
  4592. } else {
  4593. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  4594. }
  4595. }
  4596. uniforms.push_back(u);
  4597. }
  4598. {
  4599. RD::Uniform u;
  4600. u.binding = 4;
  4601. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  4602. for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
  4603. if (i < rb->sdfgi->cascades.size()) {
  4604. u.ids.push_back(rb->sdfgi->cascades[i].light_aniso_1_tex);
  4605. } else {
  4606. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  4607. }
  4608. }
  4609. uniforms.push_back(u);
  4610. }
  4611. {
  4612. RD::Uniform u;
  4613. u.binding = 5;
  4614. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  4615. u.ids.push_back(rb->sdfgi->occlusion_texture);
  4616. uniforms.push_back(u);
  4617. }
  4618. {
  4619. RD::Uniform u;
  4620. u.binding = 8;
  4621. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  4622. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  4623. uniforms.push_back(u);
  4624. }
  4625. {
  4626. RD::Uniform u;
  4627. u.binding = 9;
  4628. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  4629. u.ids.push_back(rb->sdfgi->cascades_ubo);
  4630. uniforms.push_back(u);
  4631. }
  4632. {
  4633. RD::Uniform u;
  4634. u.binding = 10;
  4635. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  4636. u.ids.push_back(rb->texture);
  4637. uniforms.push_back(u);
  4638. }
  4639. {
  4640. RD::Uniform u;
  4641. u.binding = 11;
  4642. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  4643. u.ids.push_back(rb->sdfgi->lightprobe_texture);
  4644. uniforms.push_back(u);
  4645. }
  4646. rb->sdfgi->debug_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.debug_shader_version, 0);
  4647. }
  4648. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  4649. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.debug_pipeline);
  4650. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->debug_uniform_set, 0);
  4651. SDGIShader::DebugPushConstant push_constant;
  4652. push_constant.grid_size[0] = rb->sdfgi->cascade_size;
  4653. push_constant.grid_size[1] = rb->sdfgi->cascade_size;
  4654. push_constant.grid_size[2] = rb->sdfgi->cascade_size;
  4655. push_constant.max_cascades = rb->sdfgi->cascades.size();
  4656. push_constant.screen_size[0] = rb->width;
  4657. push_constant.screen_size[1] = rb->height;
  4658. push_constant.probe_axis_size = rb->sdfgi->probe_axis_count;
  4659. push_constant.use_occlusion = rb->sdfgi->uses_occlusion;
  4660. push_constant.y_mult = rb->sdfgi->y_mult;
  4661. Vector2 vp_half = p_projection.get_viewport_half_extents();
  4662. push_constant.cam_extent[0] = vp_half.x;
  4663. push_constant.cam_extent[1] = vp_half.y;
  4664. push_constant.cam_extent[2] = -p_projection.get_z_near();
  4665. push_constant.cam_transform[0] = p_transform.basis.elements[0][0];
  4666. push_constant.cam_transform[1] = p_transform.basis.elements[1][0];
  4667. push_constant.cam_transform[2] = p_transform.basis.elements[2][0];
  4668. push_constant.cam_transform[3] = 0;
  4669. push_constant.cam_transform[4] = p_transform.basis.elements[0][1];
  4670. push_constant.cam_transform[5] = p_transform.basis.elements[1][1];
  4671. push_constant.cam_transform[6] = p_transform.basis.elements[2][1];
  4672. push_constant.cam_transform[7] = 0;
  4673. push_constant.cam_transform[8] = p_transform.basis.elements[0][2];
  4674. push_constant.cam_transform[9] = p_transform.basis.elements[1][2];
  4675. push_constant.cam_transform[10] = p_transform.basis.elements[2][2];
  4676. push_constant.cam_transform[11] = 0;
  4677. push_constant.cam_transform[12] = p_transform.origin.x;
  4678. push_constant.cam_transform[13] = p_transform.origin.y;
  4679. push_constant.cam_transform[14] = p_transform.origin.z;
  4680. push_constant.cam_transform[15] = 1;
  4681. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::DebugPushConstant));
  4682. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->width, rb->height, 1, 8, 8, 1);
  4683. RD::get_singleton()->compute_list_end();
  4684. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  4685. storage->get_effects()->copy_to_fb_rect(rb->texture, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize), true);
  4686. }
  4687. RID RendererSceneRenderRD::render_buffers_get_back_buffer_texture(RID p_render_buffers) {
  4688. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4689. ERR_FAIL_COND_V(!rb, RID());
  4690. if (!rb->blur[0].texture.is_valid()) {
  4691. return RID(); //not valid at the moment
  4692. }
  4693. return rb->blur[0].texture;
  4694. }
  4695. RID RendererSceneRenderRD::render_buffers_get_ao_texture(RID p_render_buffers) {
  4696. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4697. ERR_FAIL_COND_V(!rb, RID());
  4698. return rb->ssao.ao_full.is_valid() ? rb->ssao.ao_full : rb->ssao.ao[0];
  4699. }
  4700. RID RendererSceneRenderRD::render_buffers_get_gi_probe_buffer(RID p_render_buffers) {
  4701. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4702. ERR_FAIL_COND_V(!rb, RID());
  4703. if (rb->giprobe_buffer.is_null()) {
  4704. rb->giprobe_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(GI::GIProbeData) * RenderBuffers::MAX_GIPROBES);
  4705. }
  4706. return rb->giprobe_buffer;
  4707. }
  4708. RID RendererSceneRenderRD::render_buffers_get_default_gi_probe_buffer() {
  4709. return default_giprobe_buffer;
  4710. }
  4711. uint32_t RendererSceneRenderRD::render_buffers_get_sdfgi_cascade_count(RID p_render_buffers) const {
  4712. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4713. ERR_FAIL_COND_V(!rb, 0);
  4714. ERR_FAIL_COND_V(!rb->sdfgi, 0);
  4715. return rb->sdfgi->cascades.size();
  4716. }
  4717. bool RendererSceneRenderRD::render_buffers_is_sdfgi_enabled(RID p_render_buffers) const {
  4718. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4719. ERR_FAIL_COND_V(!rb, false);
  4720. return rb->sdfgi != nullptr;
  4721. }
  4722. RID RendererSceneRenderRD::render_buffers_get_sdfgi_irradiance_probes(RID p_render_buffers) const {
  4723. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4724. ERR_FAIL_COND_V(!rb, RID());
  4725. ERR_FAIL_COND_V(!rb->sdfgi, RID());
  4726. return rb->sdfgi->lightprobe_texture;
  4727. }
  4728. Vector3 RendererSceneRenderRD::render_buffers_get_sdfgi_cascade_offset(RID p_render_buffers, uint32_t p_cascade) const {
  4729. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4730. ERR_FAIL_COND_V(!rb, Vector3());
  4731. ERR_FAIL_COND_V(!rb->sdfgi, Vector3());
  4732. ERR_FAIL_UNSIGNED_INDEX_V(p_cascade, rb->sdfgi->cascades.size(), Vector3());
  4733. return Vector3((Vector3i(1, 1, 1) * -int32_t(rb->sdfgi->cascade_size >> 1) + rb->sdfgi->cascades[p_cascade].position)) * rb->sdfgi->cascades[p_cascade].cell_size;
  4734. }
  4735. Vector3i RendererSceneRenderRD::render_buffers_get_sdfgi_cascade_probe_offset(RID p_render_buffers, uint32_t p_cascade) const {
  4736. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4737. ERR_FAIL_COND_V(!rb, Vector3i());
  4738. ERR_FAIL_COND_V(!rb->sdfgi, Vector3i());
  4739. ERR_FAIL_UNSIGNED_INDEX_V(p_cascade, rb->sdfgi->cascades.size(), Vector3i());
  4740. int32_t probe_divisor = rb->sdfgi->cascade_size / SDFGI::PROBE_DIVISOR;
  4741. return rb->sdfgi->cascades[p_cascade].position / probe_divisor;
  4742. }
  4743. float RendererSceneRenderRD::render_buffers_get_sdfgi_normal_bias(RID p_render_buffers) const {
  4744. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4745. ERR_FAIL_COND_V(!rb, 0);
  4746. ERR_FAIL_COND_V(!rb->sdfgi, 0);
  4747. return rb->sdfgi->normal_bias;
  4748. }
  4749. float RendererSceneRenderRD::render_buffers_get_sdfgi_cascade_probe_size(RID p_render_buffers, uint32_t p_cascade) const {
  4750. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4751. ERR_FAIL_COND_V(!rb, 0);
  4752. ERR_FAIL_COND_V(!rb->sdfgi, 0);
  4753. ERR_FAIL_UNSIGNED_INDEX_V(p_cascade, rb->sdfgi->cascades.size(), 0);
  4754. return float(rb->sdfgi->cascade_size) * rb->sdfgi->cascades[p_cascade].cell_size / float(rb->sdfgi->probe_axis_count - 1);
  4755. }
  4756. uint32_t RendererSceneRenderRD::render_buffers_get_sdfgi_cascade_probe_count(RID p_render_buffers) const {
  4757. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4758. ERR_FAIL_COND_V(!rb, 0);
  4759. ERR_FAIL_COND_V(!rb->sdfgi, 0);
  4760. return rb->sdfgi->probe_axis_count;
  4761. }
  4762. uint32_t RendererSceneRenderRD::render_buffers_get_sdfgi_cascade_size(RID p_render_buffers) const {
  4763. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4764. ERR_FAIL_COND_V(!rb, 0);
  4765. ERR_FAIL_COND_V(!rb->sdfgi, 0);
  4766. return rb->sdfgi->cascade_size;
  4767. }
  4768. bool RendererSceneRenderRD::render_buffers_is_sdfgi_using_occlusion(RID p_render_buffers) const {
  4769. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4770. ERR_FAIL_COND_V(!rb, false);
  4771. ERR_FAIL_COND_V(!rb->sdfgi, false);
  4772. return rb->sdfgi->uses_occlusion;
  4773. }
  4774. float RendererSceneRenderRD::render_buffers_get_sdfgi_energy(RID p_render_buffers) const {
  4775. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4776. ERR_FAIL_COND_V(!rb, 0.0);
  4777. ERR_FAIL_COND_V(!rb->sdfgi, 0.0);
  4778. return rb->sdfgi->energy;
  4779. }
  4780. RID RendererSceneRenderRD::render_buffers_get_sdfgi_occlusion_texture(RID p_render_buffers) const {
  4781. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4782. ERR_FAIL_COND_V(!rb, RID());
  4783. ERR_FAIL_COND_V(!rb->sdfgi, RID());
  4784. return rb->sdfgi->occlusion_texture;
  4785. }
  4786. bool RendererSceneRenderRD::render_buffers_has_volumetric_fog(RID p_render_buffers) const {
  4787. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4788. ERR_FAIL_COND_V(!rb, false);
  4789. return rb->volumetric_fog != nullptr;
  4790. }
  4791. RID RendererSceneRenderRD::render_buffers_get_volumetric_fog_texture(RID p_render_buffers) {
  4792. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4793. ERR_FAIL_COND_V(!rb || !rb->volumetric_fog, RID());
  4794. return rb->volumetric_fog->fog_map;
  4795. }
  4796. RID RendererSceneRenderRD::render_buffers_get_volumetric_fog_sky_uniform_set(RID p_render_buffers) {
  4797. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4798. ERR_FAIL_COND_V(!rb, RID());
  4799. if (!rb->volumetric_fog) {
  4800. return RID();
  4801. }
  4802. return rb->volumetric_fog->sky_uniform_set;
  4803. }
  4804. float RendererSceneRenderRD::render_buffers_get_volumetric_fog_end(RID p_render_buffers) {
  4805. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4806. ERR_FAIL_COND_V(!rb || !rb->volumetric_fog, 0);
  4807. return rb->volumetric_fog->length;
  4808. }
  4809. float RendererSceneRenderRD::render_buffers_get_volumetric_fog_detail_spread(RID p_render_buffers) {
  4810. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4811. ERR_FAIL_COND_V(!rb || !rb->volumetric_fog, 0);
  4812. return rb->volumetric_fog->spread;
  4813. }
  4814. void RendererSceneRenderRD::render_buffers_configure(RID p_render_buffers, RID p_render_target, int p_width, int p_height, RS::ViewportMSAA p_msaa, RenderingServer::ViewportScreenSpaceAA p_screen_space_aa, bool p_use_debanding) {
  4815. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4816. rb->width = p_width;
  4817. rb->height = p_height;
  4818. rb->render_target = p_render_target;
  4819. rb->msaa = p_msaa;
  4820. rb->screen_space_aa = p_screen_space_aa;
  4821. rb->use_debanding = p_use_debanding;
  4822. _free_render_buffer_data(rb);
  4823. {
  4824. RD::TextureFormat tf;
  4825. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  4826. tf.width = rb->width;
  4827. tf.height = rb->height;
  4828. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  4829. if (rb->msaa != RS::VIEWPORT_MSAA_DISABLED) {
  4830. tf.usage_bits |= RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  4831. } else {
  4832. tf.usage_bits |= RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  4833. }
  4834. rb->texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4835. }
  4836. {
  4837. RD::TextureFormat tf;
  4838. if (rb->msaa == RS::VIEWPORT_MSAA_DISABLED) {
  4839. tf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D24_UNORM_S8_UINT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D24_UNORM_S8_UINT : RD::DATA_FORMAT_D32_SFLOAT_S8_UINT;
  4840. } else {
  4841. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  4842. }
  4843. tf.width = p_width;
  4844. tf.height = p_height;
  4845. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT;
  4846. if (rb->msaa != RS::VIEWPORT_MSAA_DISABLED) {
  4847. tf.usage_bits |= RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  4848. } else {
  4849. tf.usage_bits |= RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  4850. }
  4851. rb->depth_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4852. }
  4853. rb->data->configure(rb->texture, rb->depth_texture, p_width, p_height, p_msaa);
  4854. _render_buffers_uniform_set_changed(p_render_buffers);
  4855. }
  4856. void RendererSceneRenderRD::sub_surface_scattering_set_quality(RS::SubSurfaceScatteringQuality p_quality) {
  4857. sss_quality = p_quality;
  4858. }
  4859. RS::SubSurfaceScatteringQuality RendererSceneRenderRD::sub_surface_scattering_get_quality() const {
  4860. return sss_quality;
  4861. }
  4862. void RendererSceneRenderRD::sub_surface_scattering_set_scale(float p_scale, float p_depth_scale) {
  4863. sss_scale = p_scale;
  4864. sss_depth_scale = p_depth_scale;
  4865. }
  4866. void RendererSceneRenderRD::shadows_quality_set(RS::ShadowQuality p_quality) {
  4867. ERR_FAIL_INDEX_MSG(p_quality, RS::SHADOW_QUALITY_MAX, "Shadow quality too high, please see RenderingServer's ShadowQuality enum");
  4868. if (shadows_quality != p_quality) {
  4869. shadows_quality = p_quality;
  4870. switch (shadows_quality) {
  4871. case RS::SHADOW_QUALITY_HARD: {
  4872. penumbra_shadow_samples = 4;
  4873. soft_shadow_samples = 1;
  4874. shadows_quality_radius = 1.0;
  4875. } break;
  4876. case RS::SHADOW_QUALITY_SOFT_LOW: {
  4877. penumbra_shadow_samples = 8;
  4878. soft_shadow_samples = 4;
  4879. shadows_quality_radius = 2.0;
  4880. } break;
  4881. case RS::SHADOW_QUALITY_SOFT_MEDIUM: {
  4882. penumbra_shadow_samples = 12;
  4883. soft_shadow_samples = 8;
  4884. shadows_quality_radius = 2.0;
  4885. } break;
  4886. case RS::SHADOW_QUALITY_SOFT_HIGH: {
  4887. penumbra_shadow_samples = 24;
  4888. soft_shadow_samples = 16;
  4889. shadows_quality_radius = 3.0;
  4890. } break;
  4891. case RS::SHADOW_QUALITY_SOFT_ULTRA: {
  4892. penumbra_shadow_samples = 32;
  4893. soft_shadow_samples = 32;
  4894. shadows_quality_radius = 4.0;
  4895. } break;
  4896. case RS::SHADOW_QUALITY_MAX:
  4897. break;
  4898. }
  4899. get_vogel_disk(penumbra_shadow_kernel, penumbra_shadow_samples);
  4900. get_vogel_disk(soft_shadow_kernel, soft_shadow_samples);
  4901. }
  4902. }
  4903. void RendererSceneRenderRD::directional_shadow_quality_set(RS::ShadowQuality p_quality) {
  4904. ERR_FAIL_INDEX_MSG(p_quality, RS::SHADOW_QUALITY_MAX, "Shadow quality too high, please see RenderingServer's ShadowQuality enum");
  4905. if (directional_shadow_quality != p_quality) {
  4906. directional_shadow_quality = p_quality;
  4907. switch (directional_shadow_quality) {
  4908. case RS::SHADOW_QUALITY_HARD: {
  4909. directional_penumbra_shadow_samples = 4;
  4910. directional_soft_shadow_samples = 1;
  4911. directional_shadow_quality_radius = 1.0;
  4912. } break;
  4913. case RS::SHADOW_QUALITY_SOFT_LOW: {
  4914. directional_penumbra_shadow_samples = 8;
  4915. directional_soft_shadow_samples = 4;
  4916. directional_shadow_quality_radius = 2.0;
  4917. } break;
  4918. case RS::SHADOW_QUALITY_SOFT_MEDIUM: {
  4919. directional_penumbra_shadow_samples = 12;
  4920. directional_soft_shadow_samples = 8;
  4921. directional_shadow_quality_radius = 2.0;
  4922. } break;
  4923. case RS::SHADOW_QUALITY_SOFT_HIGH: {
  4924. directional_penumbra_shadow_samples = 24;
  4925. directional_soft_shadow_samples = 16;
  4926. directional_shadow_quality_radius = 3.0;
  4927. } break;
  4928. case RS::SHADOW_QUALITY_SOFT_ULTRA: {
  4929. directional_penumbra_shadow_samples = 32;
  4930. directional_soft_shadow_samples = 32;
  4931. directional_shadow_quality_radius = 4.0;
  4932. } break;
  4933. case RS::SHADOW_QUALITY_MAX:
  4934. break;
  4935. }
  4936. get_vogel_disk(directional_penumbra_shadow_kernel, directional_penumbra_shadow_samples);
  4937. get_vogel_disk(directional_soft_shadow_kernel, directional_soft_shadow_samples);
  4938. }
  4939. }
  4940. int RendererSceneRenderRD::get_roughness_layers() const {
  4941. return roughness_layers;
  4942. }
  4943. bool RendererSceneRenderRD::is_using_radiance_cubemap_array() const {
  4944. return sky_use_cubemap_array;
  4945. }
  4946. RendererSceneRenderRD::RenderBufferData *RendererSceneRenderRD::render_buffers_get_data(RID p_render_buffers) {
  4947. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4948. ERR_FAIL_COND_V(!rb, nullptr);
  4949. return rb->data;
  4950. }
  4951. void RendererSceneRenderRD::_setup_reflections(RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, const Transform &p_camera_inverse_transform, RID p_environment) {
  4952. for (int i = 0; i < p_reflection_probe_cull_count; i++) {
  4953. RID rpi = p_reflection_probe_cull_result[i];
  4954. if (i >= (int)cluster.max_reflections) {
  4955. reflection_probe_instance_set_render_index(rpi, 0); //invalid, but something needs to be set
  4956. continue;
  4957. }
  4958. reflection_probe_instance_set_render_index(rpi, i);
  4959. RID base_probe = reflection_probe_instance_get_probe(rpi);
  4960. Cluster::ReflectionData &reflection_ubo = cluster.reflections[i];
  4961. Vector3 extents = storage->reflection_probe_get_extents(base_probe);
  4962. reflection_ubo.box_extents[0] = extents.x;
  4963. reflection_ubo.box_extents[1] = extents.y;
  4964. reflection_ubo.box_extents[2] = extents.z;
  4965. reflection_ubo.index = reflection_probe_instance_get_atlas_index(rpi);
  4966. Vector3 origin_offset = storage->reflection_probe_get_origin_offset(base_probe);
  4967. reflection_ubo.box_offset[0] = origin_offset.x;
  4968. reflection_ubo.box_offset[1] = origin_offset.y;
  4969. reflection_ubo.box_offset[2] = origin_offset.z;
  4970. reflection_ubo.mask = storage->reflection_probe_get_cull_mask(base_probe);
  4971. float intensity = storage->reflection_probe_get_intensity(base_probe);
  4972. bool interior = storage->reflection_probe_is_interior(base_probe);
  4973. bool box_projection = storage->reflection_probe_is_box_projection(base_probe);
  4974. reflection_ubo.params[0] = intensity;
  4975. reflection_ubo.params[1] = 0;
  4976. reflection_ubo.params[2] = interior ? 1.0 : 0.0;
  4977. reflection_ubo.params[3] = box_projection ? 1.0 : 0.0;
  4978. Color ambient_linear = storage->reflection_probe_get_ambient_color(base_probe).to_linear();
  4979. float interior_ambient_energy = storage->reflection_probe_get_ambient_color_energy(base_probe);
  4980. uint32_t ambient_mode = storage->reflection_probe_get_ambient_mode(base_probe);
  4981. reflection_ubo.ambient[0] = ambient_linear.r * interior_ambient_energy;
  4982. reflection_ubo.ambient[1] = ambient_linear.g * interior_ambient_energy;
  4983. reflection_ubo.ambient[2] = ambient_linear.b * interior_ambient_energy;
  4984. reflection_ubo.ambient_mode = ambient_mode;
  4985. Transform transform = reflection_probe_instance_get_transform(rpi);
  4986. Transform proj = (p_camera_inverse_transform * transform).inverse();
  4987. RendererStorageRD::store_transform(proj, reflection_ubo.local_matrix);
  4988. cluster.builder.add_reflection_probe(transform, extents);
  4989. reflection_probe_instance_set_render_pass(rpi, RSG::rasterizer->get_frame_number());
  4990. }
  4991. if (p_reflection_probe_cull_count) {
  4992. RD::get_singleton()->buffer_update(cluster.reflection_buffer, 0, MIN(cluster.max_reflections, (unsigned int)p_reflection_probe_cull_count) * sizeof(ReflectionData), cluster.reflections, true);
  4993. }
  4994. }
  4995. void RendererSceneRenderRD::_setup_lights(RID *p_light_cull_result, int p_light_cull_count, const Transform &p_camera_inverse_transform, RID p_shadow_atlas, bool p_using_shadows, uint32_t &r_directional_light_count, uint32_t &r_positional_light_count) {
  4996. uint32_t light_count = 0;
  4997. r_directional_light_count = 0;
  4998. r_positional_light_count = 0;
  4999. sky_scene_state.ubo.directional_light_count = 0;
  5000. for (int i = 0; i < p_light_cull_count; i++) {
  5001. RID li = p_light_cull_result[i];
  5002. RID base = light_instance_get_base_light(li);
  5003. ERR_CONTINUE(base.is_null());
  5004. RS::LightType type = storage->light_get_type(base);
  5005. switch (type) {
  5006. case RS::LIGHT_DIRECTIONAL: {
  5007. // Copy to SkyDirectionalLightData
  5008. if (r_directional_light_count < sky_scene_state.max_directional_lights) {
  5009. SkyDirectionalLightData &sky_light_data = sky_scene_state.directional_lights[r_directional_light_count];
  5010. Transform light_transform = light_instance_get_base_transform(li);
  5011. Vector3 world_direction = light_transform.basis.xform(Vector3(0, 0, 1)).normalized();
  5012. sky_light_data.direction[0] = world_direction.x;
  5013. sky_light_data.direction[1] = world_direction.y;
  5014. sky_light_data.direction[2] = -world_direction.z;
  5015. float sign = storage->light_is_negative(base) ? -1 : 1;
  5016. sky_light_data.energy = sign * storage->light_get_param(base, RS::LIGHT_PARAM_ENERGY);
  5017. Color linear_col = storage->light_get_color(base).to_linear();
  5018. sky_light_data.color[0] = linear_col.r;
  5019. sky_light_data.color[1] = linear_col.g;
  5020. sky_light_data.color[2] = linear_col.b;
  5021. sky_light_data.enabled = true;
  5022. float angular_diameter = storage->light_get_param(base, RS::LIGHT_PARAM_SIZE);
  5023. if (angular_diameter > 0.0) {
  5024. // I know tan(0) is 0, but let's not risk it with numerical precision.
  5025. // technically this will keep expanding until reaching the sun, but all we care
  5026. // is expand until we reach the radius of the near plane (there can't be more occluders than that)
  5027. angular_diameter = Math::tan(Math::deg2rad(angular_diameter));
  5028. } else {
  5029. angular_diameter = 0.0;
  5030. }
  5031. sky_light_data.size = angular_diameter;
  5032. sky_scene_state.ubo.directional_light_count++;
  5033. }
  5034. if (r_directional_light_count >= cluster.max_directional_lights || storage->light_directional_is_sky_only(base)) {
  5035. continue;
  5036. }
  5037. Cluster::DirectionalLightData &light_data = cluster.directional_lights[r_directional_light_count];
  5038. Transform light_transform = light_instance_get_base_transform(li);
  5039. Vector3 direction = p_camera_inverse_transform.basis.xform(light_transform.basis.xform(Vector3(0, 0, 1))).normalized();
  5040. light_data.direction[0] = direction.x;
  5041. light_data.direction[1] = direction.y;
  5042. light_data.direction[2] = direction.z;
  5043. float sign = storage->light_is_negative(base) ? -1 : 1;
  5044. light_data.energy = sign * storage->light_get_param(base, RS::LIGHT_PARAM_ENERGY) * Math_PI;
  5045. Color linear_col = storage->light_get_color(base).to_linear();
  5046. light_data.color[0] = linear_col.r;
  5047. light_data.color[1] = linear_col.g;
  5048. light_data.color[2] = linear_col.b;
  5049. light_data.specular = storage->light_get_param(base, RS::LIGHT_PARAM_SPECULAR);
  5050. light_data.mask = storage->light_get_cull_mask(base);
  5051. float size = storage->light_get_param(base, RS::LIGHT_PARAM_SIZE);
  5052. light_data.size = 1.0 - Math::cos(Math::deg2rad(size)); //angle to cosine offset
  5053. Color shadow_col = storage->light_get_shadow_color(base).to_linear();
  5054. if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_PSSM_SPLITS) {
  5055. light_data.shadow_color1[0] = 1.0;
  5056. light_data.shadow_color1[1] = 0.0;
  5057. light_data.shadow_color1[2] = 0.0;
  5058. light_data.shadow_color1[3] = 1.0;
  5059. light_data.shadow_color2[0] = 0.0;
  5060. light_data.shadow_color2[1] = 1.0;
  5061. light_data.shadow_color2[2] = 0.0;
  5062. light_data.shadow_color2[3] = 1.0;
  5063. light_data.shadow_color3[0] = 0.0;
  5064. light_data.shadow_color3[1] = 0.0;
  5065. light_data.shadow_color3[2] = 1.0;
  5066. light_data.shadow_color3[3] = 1.0;
  5067. light_data.shadow_color4[0] = 1.0;
  5068. light_data.shadow_color4[1] = 1.0;
  5069. light_data.shadow_color4[2] = 0.0;
  5070. light_data.shadow_color4[3] = 1.0;
  5071. } else {
  5072. light_data.shadow_color1[0] = shadow_col.r;
  5073. light_data.shadow_color1[1] = shadow_col.g;
  5074. light_data.shadow_color1[2] = shadow_col.b;
  5075. light_data.shadow_color1[3] = 1.0;
  5076. light_data.shadow_color2[0] = shadow_col.r;
  5077. light_data.shadow_color2[1] = shadow_col.g;
  5078. light_data.shadow_color2[2] = shadow_col.b;
  5079. light_data.shadow_color2[3] = 1.0;
  5080. light_data.shadow_color3[0] = shadow_col.r;
  5081. light_data.shadow_color3[1] = shadow_col.g;
  5082. light_data.shadow_color3[2] = shadow_col.b;
  5083. light_data.shadow_color3[3] = 1.0;
  5084. light_data.shadow_color4[0] = shadow_col.r;
  5085. light_data.shadow_color4[1] = shadow_col.g;
  5086. light_data.shadow_color4[2] = shadow_col.b;
  5087. light_data.shadow_color4[3] = 1.0;
  5088. }
  5089. light_data.shadow_enabled = p_using_shadows && storage->light_has_shadow(base);
  5090. float angular_diameter = storage->light_get_param(base, RS::LIGHT_PARAM_SIZE);
  5091. if (angular_diameter > 0.0) {
  5092. // I know tan(0) is 0, but let's not risk it with numerical precision.
  5093. // technically this will keep expanding until reaching the sun, but all we care
  5094. // is expand until we reach the radius of the near plane (there can't be more occluders than that)
  5095. angular_diameter = Math::tan(Math::deg2rad(angular_diameter));
  5096. } else {
  5097. angular_diameter = 0.0;
  5098. }
  5099. if (light_data.shadow_enabled) {
  5100. RS::LightDirectionalShadowMode smode = storage->light_directional_get_shadow_mode(base);
  5101. int limit = smode == RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL ? 0 : (smode == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS ? 1 : 3);
  5102. light_data.blend_splits = storage->light_directional_get_blend_splits(base);
  5103. for (int j = 0; j < 4; j++) {
  5104. Rect2 atlas_rect = light_instance_get_directional_shadow_atlas_rect(li, j);
  5105. CameraMatrix matrix = light_instance_get_shadow_camera(li, j);
  5106. float split = light_instance_get_directional_shadow_split(li, MIN(limit, j));
  5107. CameraMatrix bias;
  5108. bias.set_light_bias();
  5109. CameraMatrix rectm;
  5110. rectm.set_light_atlas_rect(atlas_rect);
  5111. Transform modelview = (p_camera_inverse_transform * light_instance_get_shadow_transform(li, j)).inverse();
  5112. CameraMatrix shadow_mtx = rectm * bias * matrix * modelview;
  5113. light_data.shadow_split_offsets[j] = split;
  5114. float bias_scale = light_instance_get_shadow_bias_scale(li, j);
  5115. light_data.shadow_bias[j] = storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_BIAS) * bias_scale;
  5116. light_data.shadow_normal_bias[j] = storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS) * light_instance_get_directional_shadow_texel_size(li, j);
  5117. light_data.shadow_transmittance_bias[j] = storage->light_get_transmittance_bias(base) * bias_scale;
  5118. light_data.shadow_z_range[j] = light_instance_get_shadow_range(li, j);
  5119. light_data.shadow_range_begin[j] = light_instance_get_shadow_range_begin(li, j);
  5120. RendererStorageRD::store_camera(shadow_mtx, light_data.shadow_matrices[j]);
  5121. Vector2 uv_scale = light_instance_get_shadow_uv_scale(li, j);
  5122. uv_scale *= atlas_rect.size; //adapt to atlas size
  5123. switch (j) {
  5124. case 0: {
  5125. light_data.uv_scale1[0] = uv_scale.x;
  5126. light_data.uv_scale1[1] = uv_scale.y;
  5127. } break;
  5128. case 1: {
  5129. light_data.uv_scale2[0] = uv_scale.x;
  5130. light_data.uv_scale2[1] = uv_scale.y;
  5131. } break;
  5132. case 2: {
  5133. light_data.uv_scale3[0] = uv_scale.x;
  5134. light_data.uv_scale3[1] = uv_scale.y;
  5135. } break;
  5136. case 3: {
  5137. light_data.uv_scale4[0] = uv_scale.x;
  5138. light_data.uv_scale4[1] = uv_scale.y;
  5139. } break;
  5140. }
  5141. }
  5142. float fade_start = storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_FADE_START);
  5143. light_data.fade_from = -light_data.shadow_split_offsets[3] * MIN(fade_start, 0.999); //using 1.0 would break smoothstep
  5144. light_data.fade_to = -light_data.shadow_split_offsets[3];
  5145. light_data.shadow_volumetric_fog_fade = 1.0 / storage->light_get_shadow_volumetric_fog_fade(base);
  5146. light_data.soft_shadow_scale = storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_BLUR);
  5147. light_data.softshadow_angle = angular_diameter;
  5148. if (angular_diameter <= 0.0) {
  5149. light_data.soft_shadow_scale *= directional_shadow_quality_radius_get(); // Only use quality radius for PCF
  5150. }
  5151. }
  5152. r_directional_light_count++;
  5153. } break;
  5154. case RS::LIGHT_SPOT:
  5155. case RS::LIGHT_OMNI: {
  5156. if (light_count >= cluster.max_lights) {
  5157. continue;
  5158. }
  5159. Transform light_transform = light_instance_get_base_transform(li);
  5160. Cluster::LightData &light_data = cluster.lights[light_count];
  5161. cluster.lights_instances[light_count] = li;
  5162. float sign = storage->light_is_negative(base) ? -1 : 1;
  5163. Color linear_col = storage->light_get_color(base).to_linear();
  5164. light_data.attenuation_energy[0] = Math::make_half_float(storage->light_get_param(base, RS::LIGHT_PARAM_ATTENUATION));
  5165. light_data.attenuation_energy[1] = Math::make_half_float(sign * storage->light_get_param(base, RS::LIGHT_PARAM_ENERGY) * Math_PI);
  5166. light_data.color_specular[0] = MIN(uint32_t(linear_col.r * 255), 255);
  5167. light_data.color_specular[1] = MIN(uint32_t(linear_col.g * 255), 255);
  5168. light_data.color_specular[2] = MIN(uint32_t(linear_col.b * 255), 255);
  5169. light_data.color_specular[3] = MIN(uint32_t(storage->light_get_param(base, RS::LIGHT_PARAM_SPECULAR) * 255), 255);
  5170. float radius = MAX(0.001, storage->light_get_param(base, RS::LIGHT_PARAM_RANGE));
  5171. light_data.inv_radius = 1.0 / radius;
  5172. Vector3 pos = p_camera_inverse_transform.xform(light_transform.origin);
  5173. light_data.position[0] = pos.x;
  5174. light_data.position[1] = pos.y;
  5175. light_data.position[2] = pos.z;
  5176. Vector3 direction = p_camera_inverse_transform.basis.xform(light_transform.basis.xform(Vector3(0, 0, -1))).normalized();
  5177. light_data.direction[0] = direction.x;
  5178. light_data.direction[1] = direction.y;
  5179. light_data.direction[2] = direction.z;
  5180. float size = storage->light_get_param(base, RS::LIGHT_PARAM_SIZE);
  5181. light_data.size = size;
  5182. light_data.cone_attenuation_angle[0] = Math::make_half_float(storage->light_get_param(base, RS::LIGHT_PARAM_SPOT_ATTENUATION));
  5183. float spot_angle = storage->light_get_param(base, RS::LIGHT_PARAM_SPOT_ANGLE);
  5184. light_data.cone_attenuation_angle[1] = Math::make_half_float(Math::cos(Math::deg2rad(spot_angle)));
  5185. light_data.mask = storage->light_get_cull_mask(base);
  5186. light_data.atlas_rect[0] = 0;
  5187. light_data.atlas_rect[1] = 0;
  5188. light_data.atlas_rect[2] = 0;
  5189. light_data.atlas_rect[3] = 0;
  5190. RID projector = storage->light_get_projector(base);
  5191. if (projector.is_valid()) {
  5192. Rect2 rect = storage->decal_atlas_get_texture_rect(projector);
  5193. if (type == RS::LIGHT_SPOT) {
  5194. light_data.projector_rect[0] = rect.position.x;
  5195. light_data.projector_rect[1] = rect.position.y + rect.size.height; //flip because shadow is flipped
  5196. light_data.projector_rect[2] = rect.size.width;
  5197. light_data.projector_rect[3] = -rect.size.height;
  5198. } else {
  5199. light_data.projector_rect[0] = rect.position.x;
  5200. light_data.projector_rect[1] = rect.position.y;
  5201. light_data.projector_rect[2] = rect.size.width;
  5202. light_data.projector_rect[3] = rect.size.height * 0.5; //used by dp, so needs to be half
  5203. }
  5204. } else {
  5205. light_data.projector_rect[0] = 0;
  5206. light_data.projector_rect[1] = 0;
  5207. light_data.projector_rect[2] = 0;
  5208. light_data.projector_rect[3] = 0;
  5209. }
  5210. if (p_using_shadows && p_shadow_atlas.is_valid() && shadow_atlas_owns_light_instance(p_shadow_atlas, li)) {
  5211. // fill in the shadow information
  5212. Color shadow_color = storage->light_get_shadow_color(base);
  5213. light_data.shadow_color_enabled[0] = MIN(uint32_t(shadow_color.r * 255), 255);
  5214. light_data.shadow_color_enabled[1] = MIN(uint32_t(shadow_color.g * 255), 255);
  5215. light_data.shadow_color_enabled[2] = MIN(uint32_t(shadow_color.b * 255), 255);
  5216. light_data.shadow_color_enabled[3] = 255;
  5217. if (type == RS::LIGHT_SPOT) {
  5218. light_data.shadow_bias = (storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_BIAS) * radius / 10.0);
  5219. float shadow_texel_size = Math::tan(Math::deg2rad(spot_angle)) * radius * 2.0;
  5220. shadow_texel_size *= light_instance_get_shadow_texel_size(li, p_shadow_atlas);
  5221. light_data.shadow_normal_bias = storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS) * shadow_texel_size;
  5222. } else { //omni
  5223. light_data.shadow_bias = storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_BIAS) * radius / 10.0;
  5224. float shadow_texel_size = light_instance_get_shadow_texel_size(li, p_shadow_atlas);
  5225. light_data.shadow_normal_bias = storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS) * shadow_texel_size * 2.0; // applied in -1 .. 1 space
  5226. }
  5227. light_data.transmittance_bias = storage->light_get_transmittance_bias(base);
  5228. Rect2 rect = light_instance_get_shadow_atlas_rect(li, p_shadow_atlas);
  5229. light_data.atlas_rect[0] = rect.position.x;
  5230. light_data.atlas_rect[1] = rect.position.y;
  5231. light_data.atlas_rect[2] = rect.size.width;
  5232. light_data.atlas_rect[3] = rect.size.height;
  5233. light_data.soft_shadow_scale = storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_BLUR);
  5234. light_data.shadow_volumetric_fog_fade = 1.0 / storage->light_get_shadow_volumetric_fog_fade(base);
  5235. if (type == RS::LIGHT_OMNI) {
  5236. light_data.atlas_rect[3] *= 0.5; //one paraboloid on top of another
  5237. Transform proj = (p_camera_inverse_transform * light_transform).inverse();
  5238. RendererStorageRD::store_transform(proj, light_data.shadow_matrix);
  5239. if (size > 0.0) {
  5240. light_data.soft_shadow_size = size;
  5241. } else {
  5242. light_data.soft_shadow_size = 0.0;
  5243. light_data.soft_shadow_scale *= shadows_quality_radius_get(); // Only use quality radius for PCF
  5244. }
  5245. } else if (type == RS::LIGHT_SPOT) {
  5246. Transform modelview = (p_camera_inverse_transform * light_transform).inverse();
  5247. CameraMatrix bias;
  5248. bias.set_light_bias();
  5249. CameraMatrix shadow_mtx = bias * light_instance_get_shadow_camera(li, 0) * modelview;
  5250. RendererStorageRD::store_camera(shadow_mtx, light_data.shadow_matrix);
  5251. if (size > 0.0) {
  5252. CameraMatrix cm = light_instance_get_shadow_camera(li, 0);
  5253. float half_np = cm.get_z_near() * Math::tan(Math::deg2rad(spot_angle));
  5254. light_data.soft_shadow_size = (size * 0.5 / radius) / (half_np / cm.get_z_near()) * rect.size.width;
  5255. } else {
  5256. light_data.soft_shadow_size = 0.0;
  5257. light_data.soft_shadow_scale *= shadows_quality_radius_get(); // Only use quality radius for PCF
  5258. }
  5259. }
  5260. } else {
  5261. light_data.shadow_color_enabled[3] = 0;
  5262. }
  5263. light_instance_set_index(li, light_count);
  5264. cluster.builder.add_light(type == RS::LIGHT_SPOT ? LightClusterBuilder::LIGHT_TYPE_SPOT : LightClusterBuilder::LIGHT_TYPE_OMNI, light_transform, radius, spot_angle);
  5265. light_count++;
  5266. r_positional_light_count++;
  5267. } break;
  5268. }
  5269. light_instance_set_render_pass(li, RSG::rasterizer->get_frame_number());
  5270. //update UBO for forward rendering, blit to texture for clustered
  5271. }
  5272. if (light_count) {
  5273. RD::get_singleton()->buffer_update(cluster.light_buffer, 0, sizeof(Cluster::LightData) * light_count, cluster.lights, true);
  5274. }
  5275. if (r_directional_light_count) {
  5276. RD::get_singleton()->buffer_update(cluster.directional_light_buffer, 0, sizeof(Cluster::DirectionalLightData) * r_directional_light_count, cluster.directional_lights, true);
  5277. }
  5278. }
  5279. void RendererSceneRenderRD::_setup_decals(const RID *p_decal_instances, int p_decal_count, const Transform &p_camera_inverse_xform) {
  5280. Transform uv_xform;
  5281. uv_xform.basis.scale(Vector3(2.0, 1.0, 2.0));
  5282. uv_xform.origin = Vector3(-1.0, 0.0, -1.0);
  5283. p_decal_count = MIN((uint32_t)p_decal_count, cluster.max_decals);
  5284. int idx = 0;
  5285. for (int i = 0; i < p_decal_count; i++) {
  5286. RID di = p_decal_instances[i];
  5287. RID decal = decal_instance_get_base(di);
  5288. Transform xform = decal_instance_get_transform(di);
  5289. float fade = 1.0;
  5290. if (storage->decal_is_distance_fade_enabled(decal)) {
  5291. real_t distance = -p_camera_inverse_xform.xform(xform.origin).z;
  5292. float fade_begin = storage->decal_get_distance_fade_begin(decal);
  5293. float fade_length = storage->decal_get_distance_fade_length(decal);
  5294. if (distance > fade_begin) {
  5295. if (distance > fade_begin + fade_length) {
  5296. continue; // do not use this decal, its invisible
  5297. }
  5298. fade = 1.0 - (distance - fade_begin) / fade_length;
  5299. }
  5300. }
  5301. Cluster::DecalData &dd = cluster.decals[idx];
  5302. Vector3 decal_extents = storage->decal_get_extents(decal);
  5303. Transform scale_xform;
  5304. scale_xform.basis.scale(Vector3(decal_extents.x, decal_extents.y, decal_extents.z));
  5305. Transform to_decal_xform = (p_camera_inverse_xform * decal_instance_get_transform(di) * scale_xform * uv_xform).affine_inverse();
  5306. RendererStorageRD::store_transform(to_decal_xform, dd.xform);
  5307. Vector3 normal = xform.basis.get_axis(Vector3::AXIS_Y).normalized();
  5308. normal = p_camera_inverse_xform.basis.xform(normal); //camera is normalized, so fine
  5309. dd.normal[0] = normal.x;
  5310. dd.normal[1] = normal.y;
  5311. dd.normal[2] = normal.z;
  5312. dd.normal_fade = storage->decal_get_normal_fade(decal);
  5313. RID albedo_tex = storage->decal_get_texture(decal, RS::DECAL_TEXTURE_ALBEDO);
  5314. RID emission_tex = storage->decal_get_texture(decal, RS::DECAL_TEXTURE_EMISSION);
  5315. if (albedo_tex.is_valid()) {
  5316. Rect2 rect = storage->decal_atlas_get_texture_rect(albedo_tex);
  5317. dd.albedo_rect[0] = rect.position.x;
  5318. dd.albedo_rect[1] = rect.position.y;
  5319. dd.albedo_rect[2] = rect.size.x;
  5320. dd.albedo_rect[3] = rect.size.y;
  5321. } else {
  5322. if (!emission_tex.is_valid()) {
  5323. continue; //no albedo, no emission, no decal.
  5324. }
  5325. dd.albedo_rect[0] = 0;
  5326. dd.albedo_rect[1] = 0;
  5327. dd.albedo_rect[2] = 0;
  5328. dd.albedo_rect[3] = 0;
  5329. }
  5330. RID normal_tex = storage->decal_get_texture(decal, RS::DECAL_TEXTURE_NORMAL);
  5331. if (normal_tex.is_valid()) {
  5332. Rect2 rect = storage->decal_atlas_get_texture_rect(normal_tex);
  5333. dd.normal_rect[0] = rect.position.x;
  5334. dd.normal_rect[1] = rect.position.y;
  5335. dd.normal_rect[2] = rect.size.x;
  5336. dd.normal_rect[3] = rect.size.y;
  5337. Basis normal_xform = p_camera_inverse_xform.basis * xform.basis.orthonormalized();
  5338. RendererStorageRD::store_basis_3x4(normal_xform, dd.normal_xform);
  5339. } else {
  5340. dd.normal_rect[0] = 0;
  5341. dd.normal_rect[1] = 0;
  5342. dd.normal_rect[2] = 0;
  5343. dd.normal_rect[3] = 0;
  5344. }
  5345. RID orm_tex = storage->decal_get_texture(decal, RS::DECAL_TEXTURE_ORM);
  5346. if (orm_tex.is_valid()) {
  5347. Rect2 rect = storage->decal_atlas_get_texture_rect(orm_tex);
  5348. dd.orm_rect[0] = rect.position.x;
  5349. dd.orm_rect[1] = rect.position.y;
  5350. dd.orm_rect[2] = rect.size.x;
  5351. dd.orm_rect[3] = rect.size.y;
  5352. } else {
  5353. dd.orm_rect[0] = 0;
  5354. dd.orm_rect[1] = 0;
  5355. dd.orm_rect[2] = 0;
  5356. dd.orm_rect[3] = 0;
  5357. }
  5358. if (emission_tex.is_valid()) {
  5359. Rect2 rect = storage->decal_atlas_get_texture_rect(emission_tex);
  5360. dd.emission_rect[0] = rect.position.x;
  5361. dd.emission_rect[1] = rect.position.y;
  5362. dd.emission_rect[2] = rect.size.x;
  5363. dd.emission_rect[3] = rect.size.y;
  5364. } else {
  5365. dd.emission_rect[0] = 0;
  5366. dd.emission_rect[1] = 0;
  5367. dd.emission_rect[2] = 0;
  5368. dd.emission_rect[3] = 0;
  5369. }
  5370. Color modulate = storage->decal_get_modulate(decal);
  5371. dd.modulate[0] = modulate.r;
  5372. dd.modulate[1] = modulate.g;
  5373. dd.modulate[2] = modulate.b;
  5374. dd.modulate[3] = modulate.a * fade;
  5375. dd.emission_energy = storage->decal_get_emission_energy(decal) * fade;
  5376. dd.albedo_mix = storage->decal_get_albedo_mix(decal);
  5377. dd.mask = storage->decal_get_cull_mask(decal);
  5378. dd.upper_fade = storage->decal_get_upper_fade(decal);
  5379. dd.lower_fade = storage->decal_get_lower_fade(decal);
  5380. cluster.builder.add_decal(xform, decal_extents);
  5381. idx++;
  5382. }
  5383. if (idx > 0) {
  5384. RD::get_singleton()->buffer_update(cluster.decal_buffer, 0, sizeof(Cluster::DecalData) * idx, cluster.decals, true);
  5385. }
  5386. }
  5387. void RendererSceneRenderRD::_volumetric_fog_erase(RenderBuffers *rb) {
  5388. ERR_FAIL_COND(!rb->volumetric_fog);
  5389. RD::get_singleton()->free(rb->volumetric_fog->light_density_map);
  5390. RD::get_singleton()->free(rb->volumetric_fog->fog_map);
  5391. if (rb->volumetric_fog->uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(rb->volumetric_fog->uniform_set)) {
  5392. RD::get_singleton()->free(rb->volumetric_fog->uniform_set);
  5393. }
  5394. if (rb->volumetric_fog->uniform_set2.is_valid() && RD::get_singleton()->uniform_set_is_valid(rb->volumetric_fog->uniform_set2)) {
  5395. RD::get_singleton()->free(rb->volumetric_fog->uniform_set2);
  5396. }
  5397. if (rb->volumetric_fog->sdfgi_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(rb->volumetric_fog->sdfgi_uniform_set)) {
  5398. RD::get_singleton()->free(rb->volumetric_fog->sdfgi_uniform_set);
  5399. }
  5400. if (rb->volumetric_fog->sky_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(rb->volumetric_fog->sky_uniform_set)) {
  5401. RD::get_singleton()->free(rb->volumetric_fog->sky_uniform_set);
  5402. }
  5403. memdelete(rb->volumetric_fog);
  5404. rb->volumetric_fog = nullptr;
  5405. }
  5406. void RendererSceneRenderRD::_allocate_shadow_shrink_stages(RID p_base, int p_base_size, Vector<ShadowShrinkStage> &shrink_stages, uint32_t p_target_size) {
  5407. //create fog mipmaps
  5408. uint32_t fog_texture_size = p_target_size;
  5409. uint32_t base_texture_size = p_base_size;
  5410. ShadowShrinkStage first;
  5411. first.size = base_texture_size;
  5412. first.texture = p_base;
  5413. shrink_stages.push_back(first); //put depth first in case we dont find smaller ones
  5414. while (fog_texture_size < base_texture_size) {
  5415. base_texture_size = MAX(base_texture_size / 8, fog_texture_size);
  5416. ShadowShrinkStage s;
  5417. s.size = base_texture_size;
  5418. RD::TextureFormat tf;
  5419. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  5420. tf.width = base_texture_size;
  5421. tf.height = base_texture_size;
  5422. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  5423. if (base_texture_size == fog_texture_size) {
  5424. s.filter_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  5425. tf.usage_bits |= RD::TEXTURE_USAGE_SAMPLING_BIT;
  5426. }
  5427. s.texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  5428. shrink_stages.push_back(s);
  5429. }
  5430. }
  5431. void RendererSceneRenderRD::_clear_shadow_shrink_stages(Vector<ShadowShrinkStage> &shrink_stages) {
  5432. for (int i = 1; i < shrink_stages.size(); i++) {
  5433. RD::get_singleton()->free(shrink_stages[i].texture);
  5434. if (shrink_stages[i].filter_texture.is_valid()) {
  5435. RD::get_singleton()->free(shrink_stages[i].filter_texture);
  5436. }
  5437. }
  5438. shrink_stages.clear();
  5439. }
  5440. void RendererSceneRenderRD::_update_volumetric_fog(RID p_render_buffers, RID p_environment, const CameraMatrix &p_cam_projection, const Transform &p_cam_transform, RID p_shadow_atlas, int p_directional_light_count, bool p_use_directional_shadows, int p_positional_light_count, int p_gi_probe_count) {
  5441. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  5442. ERR_FAIL_COND(!rb);
  5443. Environment *env = environment_owner.getornull(p_environment);
  5444. float ratio = float(rb->width) / float((rb->width + rb->height) / 2);
  5445. uint32_t target_width = uint32_t(float(volumetric_fog_size) * ratio);
  5446. uint32_t target_height = uint32_t(float(volumetric_fog_size) / ratio);
  5447. if (rb->volumetric_fog) {
  5448. //validate
  5449. if (!env || !env->volumetric_fog_enabled || rb->volumetric_fog->width != target_width || rb->volumetric_fog->height != target_height || rb->volumetric_fog->depth != volumetric_fog_depth) {
  5450. _volumetric_fog_erase(rb);
  5451. _render_buffers_uniform_set_changed(p_render_buffers);
  5452. }
  5453. }
  5454. if (!env || !env->volumetric_fog_enabled) {
  5455. //no reason to enable or update, bye
  5456. return;
  5457. }
  5458. if (env && env->volumetric_fog_enabled && !rb->volumetric_fog) {
  5459. //required volumetric fog but not existing, create
  5460. rb->volumetric_fog = memnew(VolumetricFog);
  5461. rb->volumetric_fog->width = target_width;
  5462. rb->volumetric_fog->height = target_height;
  5463. rb->volumetric_fog->depth = volumetric_fog_depth;
  5464. RD::TextureFormat tf;
  5465. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  5466. tf.width = target_width;
  5467. tf.height = target_height;
  5468. tf.depth = volumetric_fog_depth;
  5469. tf.texture_type = RD::TEXTURE_TYPE_3D;
  5470. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  5471. rb->volumetric_fog->light_density_map = RD::get_singleton()->texture_create(tf, RD::TextureView());
  5472. tf.usage_bits |= RD::TEXTURE_USAGE_SAMPLING_BIT;
  5473. rb->volumetric_fog->fog_map = RD::get_singleton()->texture_create(tf, RD::TextureView());
  5474. _render_buffers_uniform_set_changed(p_render_buffers);
  5475. Vector<RD::Uniform> uniforms;
  5476. {
  5477. RD::Uniform u;
  5478. u.binding = 0;
  5479. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  5480. u.ids.push_back(rb->volumetric_fog->fog_map);
  5481. uniforms.push_back(u);
  5482. }
  5483. rb->volumetric_fog->sky_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sky_shader.default_shader_rd, SKY_SET_FOG);
  5484. }
  5485. //update directional shadow
  5486. if (p_use_directional_shadows) {
  5487. if (directional_shadow.shrink_stages.empty()) {
  5488. if (rb->volumetric_fog->uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(rb->volumetric_fog->uniform_set)) {
  5489. //invalidate uniform set, we will need a new one
  5490. RD::get_singleton()->free(rb->volumetric_fog->uniform_set);
  5491. rb->volumetric_fog->uniform_set = RID();
  5492. }
  5493. _allocate_shadow_shrink_stages(directional_shadow.depth, directional_shadow.size, directional_shadow.shrink_stages, volumetric_fog_directional_shadow_shrink);
  5494. }
  5495. if (directional_shadow.shrink_stages.size() > 1) {
  5496. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  5497. for (int i = 1; i < directional_shadow.shrink_stages.size(); i++) {
  5498. int32_t src_size = directional_shadow.shrink_stages[i - 1].size;
  5499. int32_t dst_size = directional_shadow.shrink_stages[i].size;
  5500. Rect2i r(0, 0, src_size, src_size);
  5501. int32_t shrink_limit = 8 / (src_size / dst_size);
  5502. storage->get_effects()->reduce_shadow(directional_shadow.shrink_stages[i - 1].texture, directional_shadow.shrink_stages[i].texture, Size2i(src_size, src_size), r, shrink_limit, compute_list);
  5503. RD::get_singleton()->compute_list_add_barrier(compute_list);
  5504. if (env->volumetric_fog_shadow_filter != RS::ENV_VOLUMETRIC_FOG_SHADOW_FILTER_DISABLED && directional_shadow.shrink_stages[i].filter_texture.is_valid()) {
  5505. Rect2i rf(0, 0, dst_size, dst_size);
  5506. storage->get_effects()->filter_shadow(directional_shadow.shrink_stages[i].texture, directional_shadow.shrink_stages[i].filter_texture, Size2i(dst_size, dst_size), rf, env->volumetric_fog_shadow_filter, compute_list);
  5507. }
  5508. }
  5509. RD::get_singleton()->compute_list_end();
  5510. }
  5511. }
  5512. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  5513. if (shadow_atlas) {
  5514. //shrink shadows that need to be shrunk
  5515. bool force_shrink_shadows = false;
  5516. if (shadow_atlas->shrink_stages.empty()) {
  5517. if (rb->volumetric_fog->uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(rb->volumetric_fog->uniform_set)) {
  5518. //invalidate uniform set, we will need a new one
  5519. RD::get_singleton()->free(rb->volumetric_fog->uniform_set);
  5520. rb->volumetric_fog->uniform_set = RID();
  5521. }
  5522. _allocate_shadow_shrink_stages(shadow_atlas->depth, shadow_atlas->size, shadow_atlas->shrink_stages, volumetric_fog_positional_shadow_shrink);
  5523. force_shrink_shadows = true;
  5524. }
  5525. if (rb->volumetric_fog->last_shadow_filter != env->volumetric_fog_shadow_filter) {
  5526. //if shadow filter changed, invalidate caches
  5527. rb->volumetric_fog->last_shadow_filter = env->volumetric_fog_shadow_filter;
  5528. force_shrink_shadows = true;
  5529. }
  5530. cluster.lights_shadow_rect_cache_count = 0;
  5531. for (int i = 0; i < p_positional_light_count; i++) {
  5532. if (cluster.lights[i].shadow_color_enabled[3] > 127) {
  5533. RID li = cluster.lights_instances[i];
  5534. ERR_CONTINUE(!shadow_atlas->shadow_owners.has(li));
  5535. uint32_t key = shadow_atlas->shadow_owners[li];
  5536. uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  5537. uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK;
  5538. ERR_CONTINUE((int)shadow >= shadow_atlas->quadrants[quadrant].shadows.size());
  5539. ShadowAtlas::Quadrant::Shadow &s = shadow_atlas->quadrants[quadrant].shadows.write[shadow];
  5540. if (!force_shrink_shadows && s.fog_version == s.version) {
  5541. continue; //do not update, no need
  5542. }
  5543. s.fog_version = s.version;
  5544. uint32_t quadrant_size = shadow_atlas->size >> 1;
  5545. Rect2i atlas_rect;
  5546. atlas_rect.position.x = (quadrant & 1) * quadrant_size;
  5547. atlas_rect.position.y = (quadrant >> 1) * quadrant_size;
  5548. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  5549. atlas_rect.position.x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  5550. atlas_rect.position.y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  5551. atlas_rect.size.x = shadow_size;
  5552. atlas_rect.size.y = shadow_size;
  5553. cluster.lights_shadow_rect_cache[cluster.lights_shadow_rect_cache_count] = atlas_rect;
  5554. cluster.lights_shadow_rect_cache_count++;
  5555. if (cluster.lights_shadow_rect_cache_count == cluster.max_lights) {
  5556. break; //light limit reached
  5557. }
  5558. }
  5559. }
  5560. if (cluster.lights_shadow_rect_cache_count > 0) {
  5561. //there are shadows to be shrunk, try to do them in parallel
  5562. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  5563. for (int i = 1; i < shadow_atlas->shrink_stages.size(); i++) {
  5564. int32_t base_size = shadow_atlas->shrink_stages[0].size;
  5565. int32_t src_size = shadow_atlas->shrink_stages[i - 1].size;
  5566. int32_t dst_size = shadow_atlas->shrink_stages[i].size;
  5567. uint32_t rect_divisor = base_size / src_size;
  5568. int32_t shrink_limit = 8 / (src_size / dst_size);
  5569. //shrink in parallel for more performance
  5570. for (uint32_t j = 0; j < cluster.lights_shadow_rect_cache_count; j++) {
  5571. Rect2i src_rect = cluster.lights_shadow_rect_cache[j];
  5572. src_rect.position /= rect_divisor;
  5573. src_rect.size /= rect_divisor;
  5574. storage->get_effects()->reduce_shadow(shadow_atlas->shrink_stages[i - 1].texture, shadow_atlas->shrink_stages[i].texture, Size2i(src_size, src_size), src_rect, shrink_limit, compute_list);
  5575. }
  5576. RD::get_singleton()->compute_list_add_barrier(compute_list);
  5577. if (env->volumetric_fog_shadow_filter != RS::ENV_VOLUMETRIC_FOG_SHADOW_FILTER_DISABLED && shadow_atlas->shrink_stages[i].filter_texture.is_valid()) {
  5578. uint32_t filter_divisor = base_size / dst_size;
  5579. //filter in parallel for more performance
  5580. for (uint32_t j = 0; j < cluster.lights_shadow_rect_cache_count; j++) {
  5581. Rect2i dst_rect = cluster.lights_shadow_rect_cache[j];
  5582. dst_rect.position /= filter_divisor;
  5583. dst_rect.size /= filter_divisor;
  5584. storage->get_effects()->filter_shadow(shadow_atlas->shrink_stages[i].texture, shadow_atlas->shrink_stages[i].filter_texture, Size2i(dst_size, dst_size), dst_rect, env->volumetric_fog_shadow_filter, compute_list, true, false);
  5585. }
  5586. RD::get_singleton()->compute_list_add_barrier(compute_list);
  5587. for (uint32_t j = 0; j < cluster.lights_shadow_rect_cache_count; j++) {
  5588. Rect2i dst_rect = cluster.lights_shadow_rect_cache[j];
  5589. dst_rect.position /= filter_divisor;
  5590. dst_rect.size /= filter_divisor;
  5591. storage->get_effects()->filter_shadow(shadow_atlas->shrink_stages[i].texture, shadow_atlas->shrink_stages[i].filter_texture, Size2i(dst_size, dst_size), dst_rect, env->volumetric_fog_shadow_filter, compute_list, false, true);
  5592. }
  5593. }
  5594. }
  5595. RD::get_singleton()->compute_list_end();
  5596. }
  5597. }
  5598. //update volumetric fog
  5599. if (rb->volumetric_fog->uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(rb->volumetric_fog->uniform_set)) {
  5600. //re create uniform set if needed
  5601. Vector<RD::Uniform> uniforms;
  5602. {
  5603. RD::Uniform u;
  5604. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  5605. u.binding = 1;
  5606. if (shadow_atlas == nullptr || shadow_atlas->shrink_stages.size() == 0) {
  5607. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_BLACK));
  5608. } else {
  5609. u.ids.push_back(shadow_atlas->shrink_stages[shadow_atlas->shrink_stages.size() - 1].texture);
  5610. }
  5611. uniforms.push_back(u);
  5612. }
  5613. {
  5614. RD::Uniform u;
  5615. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  5616. u.binding = 2;
  5617. if (directional_shadow.shrink_stages.size() == 0) {
  5618. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_BLACK));
  5619. } else {
  5620. u.ids.push_back(directional_shadow.shrink_stages[directional_shadow.shrink_stages.size() - 1].texture);
  5621. }
  5622. uniforms.push_back(u);
  5623. }
  5624. {
  5625. RD::Uniform u;
  5626. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  5627. u.binding = 3;
  5628. u.ids.push_back(get_positional_light_buffer());
  5629. uniforms.push_back(u);
  5630. }
  5631. {
  5632. RD::Uniform u;
  5633. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  5634. u.binding = 4;
  5635. u.ids.push_back(get_directional_light_buffer());
  5636. uniforms.push_back(u);
  5637. }
  5638. {
  5639. RD::Uniform u;
  5640. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  5641. u.binding = 5;
  5642. u.ids.push_back(get_cluster_builder_texture());
  5643. uniforms.push_back(u);
  5644. }
  5645. {
  5646. RD::Uniform u;
  5647. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  5648. u.binding = 6;
  5649. u.ids.push_back(get_cluster_builder_indices_buffer());
  5650. uniforms.push_back(u);
  5651. }
  5652. {
  5653. RD::Uniform u;
  5654. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  5655. u.binding = 7;
  5656. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  5657. uniforms.push_back(u);
  5658. }
  5659. {
  5660. RD::Uniform u;
  5661. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  5662. u.binding = 8;
  5663. u.ids.push_back(rb->volumetric_fog->light_density_map);
  5664. uniforms.push_back(u);
  5665. }
  5666. {
  5667. RD::Uniform u;
  5668. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  5669. u.binding = 9;
  5670. u.ids.push_back(rb->volumetric_fog->fog_map);
  5671. uniforms.push_back(u);
  5672. }
  5673. {
  5674. RD::Uniform u;
  5675. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  5676. u.binding = 10;
  5677. u.ids.push_back(shadow_sampler);
  5678. uniforms.push_back(u);
  5679. }
  5680. {
  5681. RD::Uniform u;
  5682. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  5683. u.binding = 11;
  5684. u.ids.push_back(render_buffers_get_gi_probe_buffer(p_render_buffers));
  5685. uniforms.push_back(u);
  5686. }
  5687. {
  5688. RD::Uniform u;
  5689. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  5690. u.binding = 12;
  5691. for (int i = 0; i < RenderBuffers::MAX_GIPROBES; i++) {
  5692. u.ids.push_back(rb->giprobe_textures[i]);
  5693. }
  5694. uniforms.push_back(u);
  5695. }
  5696. {
  5697. RD::Uniform u;
  5698. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  5699. u.binding = 13;
  5700. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  5701. uniforms.push_back(u);
  5702. }
  5703. rb->volumetric_fog->uniform_set = RD::get_singleton()->uniform_set_create(uniforms, volumetric_fog.shader.version_get_shader(volumetric_fog.shader_version, 0), 0);
  5704. SWAP(uniforms.write[7].ids.write[0], uniforms.write[8].ids.write[0]);
  5705. rb->volumetric_fog->uniform_set2 = RD::get_singleton()->uniform_set_create(uniforms, volumetric_fog.shader.version_get_shader(volumetric_fog.shader_version, 0), 0);
  5706. }
  5707. bool using_sdfgi = env->volumetric_fog_gi_inject > 0.0001 && env->sdfgi_enabled && (rb->sdfgi != nullptr);
  5708. if (using_sdfgi) {
  5709. if (rb->volumetric_fog->sdfgi_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(rb->volumetric_fog->sdfgi_uniform_set)) {
  5710. Vector<RD::Uniform> uniforms;
  5711. {
  5712. RD::Uniform u;
  5713. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  5714. u.binding = 0;
  5715. u.ids.push_back(gi.sdfgi_ubo);
  5716. uniforms.push_back(u);
  5717. }
  5718. {
  5719. RD::Uniform u;
  5720. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  5721. u.binding = 1;
  5722. u.ids.push_back(rb->sdfgi->ambient_texture);
  5723. uniforms.push_back(u);
  5724. }
  5725. {
  5726. RD::Uniform u;
  5727. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  5728. u.binding = 2;
  5729. u.ids.push_back(rb->sdfgi->occlusion_texture);
  5730. uniforms.push_back(u);
  5731. }
  5732. rb->volumetric_fog->sdfgi_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, volumetric_fog.shader.version_get_shader(volumetric_fog.shader_version, VOLUMETRIC_FOG_SHADER_DENSITY_WITH_SDFGI), 1);
  5733. }
  5734. }
  5735. rb->volumetric_fog->length = env->volumetric_fog_length;
  5736. rb->volumetric_fog->spread = env->volumetric_fog_detail_spread;
  5737. VolumetricFogShader::PushConstant push_constant;
  5738. Vector2 frustum_near_size = p_cam_projection.get_viewport_half_extents();
  5739. Vector2 frustum_far_size = p_cam_projection.get_far_plane_half_extents();
  5740. float z_near = p_cam_projection.get_z_near();
  5741. float z_far = p_cam_projection.get_z_far();
  5742. float fog_end = env->volumetric_fog_length;
  5743. Vector2 fog_far_size = frustum_near_size.lerp(frustum_far_size, (fog_end - z_near) / (z_far - z_near));
  5744. Vector2 fog_near_size;
  5745. if (p_cam_projection.is_orthogonal()) {
  5746. fog_near_size = fog_far_size;
  5747. } else {
  5748. fog_near_size = Vector2();
  5749. }
  5750. push_constant.fog_frustum_size_begin[0] = fog_near_size.x;
  5751. push_constant.fog_frustum_size_begin[1] = fog_near_size.y;
  5752. push_constant.fog_frustum_size_end[0] = fog_far_size.x;
  5753. push_constant.fog_frustum_size_end[1] = fog_far_size.y;
  5754. push_constant.z_near = z_near;
  5755. push_constant.z_far = z_far;
  5756. push_constant.fog_frustum_end = fog_end;
  5757. push_constant.fog_volume_size[0] = rb->volumetric_fog->width;
  5758. push_constant.fog_volume_size[1] = rb->volumetric_fog->height;
  5759. push_constant.fog_volume_size[2] = rb->volumetric_fog->depth;
  5760. push_constant.directional_light_count = p_directional_light_count;
  5761. Color light = env->volumetric_fog_light.to_linear();
  5762. push_constant.light_energy[0] = light.r * env->volumetric_fog_light_energy;
  5763. push_constant.light_energy[1] = light.g * env->volumetric_fog_light_energy;
  5764. push_constant.light_energy[2] = light.b * env->volumetric_fog_light_energy;
  5765. push_constant.base_density = env->volumetric_fog_density;
  5766. push_constant.detail_spread = env->volumetric_fog_detail_spread;
  5767. push_constant.gi_inject = env->volumetric_fog_gi_inject;
  5768. push_constant.cam_rotation[0] = p_cam_transform.basis[0][0];
  5769. push_constant.cam_rotation[1] = p_cam_transform.basis[1][0];
  5770. push_constant.cam_rotation[2] = p_cam_transform.basis[2][0];
  5771. push_constant.cam_rotation[3] = 0;
  5772. push_constant.cam_rotation[4] = p_cam_transform.basis[0][1];
  5773. push_constant.cam_rotation[5] = p_cam_transform.basis[1][1];
  5774. push_constant.cam_rotation[6] = p_cam_transform.basis[2][1];
  5775. push_constant.cam_rotation[7] = 0;
  5776. push_constant.cam_rotation[8] = p_cam_transform.basis[0][2];
  5777. push_constant.cam_rotation[9] = p_cam_transform.basis[1][2];
  5778. push_constant.cam_rotation[10] = p_cam_transform.basis[2][2];
  5779. push_constant.cam_rotation[11] = 0;
  5780. push_constant.filter_axis = 0;
  5781. push_constant.max_gi_probes = env->volumetric_fog_gi_inject > 0.001 ? p_gi_probe_count : 0;
  5782. /* Vector2 dssize = directional_shadow_get_size();
  5783. push_constant.directional_shadow_pixel_size[0] = 1.0 / dssize.x;
  5784. push_constant.directional_shadow_pixel_size[1] = 1.0 / dssize.y;
  5785. */
  5786. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  5787. bool use_filter = volumetric_fog_filter_active;
  5788. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, volumetric_fog.pipelines[using_sdfgi ? VOLUMETRIC_FOG_SHADER_DENSITY_WITH_SDFGI : VOLUMETRIC_FOG_SHADER_DENSITY]);
  5789. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->volumetric_fog->uniform_set, 0);
  5790. if (using_sdfgi) {
  5791. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->volumetric_fog->sdfgi_uniform_set, 1);
  5792. }
  5793. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VolumetricFogShader::PushConstant));
  5794. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->volumetric_fog->width, rb->volumetric_fog->height, rb->volumetric_fog->depth, 4, 4, 4);
  5795. RD::get_singleton()->compute_list_add_barrier(compute_list);
  5796. if (use_filter) {
  5797. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, volumetric_fog.pipelines[VOLUMETRIC_FOG_SHADER_FILTER]);
  5798. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->volumetric_fog->uniform_set, 0);
  5799. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VolumetricFogShader::PushConstant));
  5800. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->volumetric_fog->width, rb->volumetric_fog->height, rb->volumetric_fog->depth, 8, 8, 1);
  5801. RD::get_singleton()->compute_list_add_barrier(compute_list);
  5802. push_constant.filter_axis = 1;
  5803. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->volumetric_fog->uniform_set2, 0);
  5804. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VolumetricFogShader::PushConstant));
  5805. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->volumetric_fog->width, rb->volumetric_fog->height, rb->volumetric_fog->depth, 8, 8, 1);
  5806. RD::get_singleton()->compute_list_add_barrier(compute_list);
  5807. }
  5808. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, volumetric_fog.pipelines[VOLUMETRIC_FOG_SHADER_FOG]);
  5809. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->volumetric_fog->uniform_set, 0);
  5810. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(VolumetricFogShader::PushConstant));
  5811. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->volumetric_fog->width, rb->volumetric_fog->height, 1, 8, 8, 1);
  5812. RD::get_singleton()->compute_list_end();
  5813. }
  5814. void RendererSceneRenderRD::render_scene(RID p_render_buffers, const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID *p_light_cull_result, int p_light_cull_count, RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, RID *p_gi_probe_cull_result, int p_gi_probe_cull_count, RID *p_decal_cull_result, int p_decal_cull_count, InstanceBase **p_lightmap_cull_result, int p_lightmap_cull_count, RID p_environment, RID p_camera_effects, RID p_shadow_atlas, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass, float p_screen_lod_threshold) {
  5815. Color clear_color;
  5816. if (p_render_buffers.is_valid()) {
  5817. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  5818. ERR_FAIL_COND(!rb);
  5819. clear_color = storage->render_target_get_clear_request_color(rb->render_target);
  5820. } else {
  5821. clear_color = storage->get_default_clear_color();
  5822. }
  5823. //assign render indices to giprobes
  5824. for (int i = 0; i < p_gi_probe_cull_count; i++) {
  5825. GIProbeInstance *giprobe_inst = gi_probe_instance_owner.getornull(p_gi_probe_cull_result[i]);
  5826. if (giprobe_inst) {
  5827. giprobe_inst->render_index = i;
  5828. }
  5829. }
  5830. if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_UNSHADED) {
  5831. p_light_cull_count = 0;
  5832. p_reflection_probe_cull_count = 0;
  5833. p_gi_probe_cull_count = 0;
  5834. }
  5835. cluster.builder.begin(p_cam_transform.affine_inverse(), p_cam_projection); //prepare cluster
  5836. bool using_shadows = true;
  5837. if (p_reflection_probe.is_valid()) {
  5838. if (!storage->reflection_probe_renders_shadows(reflection_probe_instance_get_probe(p_reflection_probe))) {
  5839. using_shadows = false;
  5840. }
  5841. } else {
  5842. //do not render reflections when rendering a reflection probe
  5843. _setup_reflections(p_reflection_probe_cull_result, p_reflection_probe_cull_count, p_cam_transform.affine_inverse(), p_environment);
  5844. }
  5845. uint32_t directional_light_count = 0;
  5846. uint32_t positional_light_count = 0;
  5847. _setup_lights(p_light_cull_result, p_light_cull_count, p_cam_transform.affine_inverse(), p_shadow_atlas, using_shadows, directional_light_count, positional_light_count);
  5848. _setup_decals(p_decal_cull_result, p_decal_cull_count, p_cam_transform.affine_inverse());
  5849. cluster.builder.bake_cluster(); //bake to cluster
  5850. uint32_t gi_probe_count = 0;
  5851. _setup_giprobes(p_render_buffers, p_cam_transform, p_gi_probe_cull_result, p_gi_probe_cull_count, gi_probe_count);
  5852. if (p_render_buffers.is_valid()) {
  5853. bool directional_shadows = false;
  5854. for (uint32_t i = 0; i < directional_light_count; i++) {
  5855. if (cluster.directional_lights[i].shadow_enabled) {
  5856. directional_shadows = true;
  5857. break;
  5858. }
  5859. }
  5860. _update_volumetric_fog(p_render_buffers, p_environment, p_cam_projection, p_cam_transform, p_shadow_atlas, directional_light_count, directional_shadows, positional_light_count, gi_probe_count);
  5861. }
  5862. _render_scene(p_render_buffers, p_cam_transform, p_cam_projection, p_cam_ortogonal, p_cull_result, p_cull_count, directional_light_count, p_gi_probe_cull_result, p_gi_probe_cull_count, p_lightmap_cull_result, p_lightmap_cull_count, p_environment, p_camera_effects, p_shadow_atlas, p_reflection_atlas, p_reflection_probe, p_reflection_probe_pass, clear_color, p_screen_lod_threshold);
  5863. if (p_render_buffers.is_valid()) {
  5864. RENDER_TIMESTAMP("Tonemap");
  5865. _render_buffers_post_process_and_tonemap(p_render_buffers, p_environment, p_camera_effects, p_cam_projection);
  5866. _render_buffers_debug_draw(p_render_buffers, p_shadow_atlas);
  5867. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_SDFGI) {
  5868. _sdfgi_debug_draw(p_render_buffers, p_cam_projection, p_cam_transform);
  5869. }
  5870. }
  5871. }
  5872. void RendererSceneRenderRD::render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count, const Plane &p_camera_plane, float p_lod_distance_multiplier, float p_screen_lod_threshold) {
  5873. LightInstance *light_instance = light_instance_owner.getornull(p_light);
  5874. ERR_FAIL_COND(!light_instance);
  5875. Rect2i atlas_rect;
  5876. RID atlas_texture;
  5877. bool using_dual_paraboloid = false;
  5878. bool using_dual_paraboloid_flip = false;
  5879. float znear = 0;
  5880. float zfar = 0;
  5881. RID render_fb;
  5882. RID render_texture;
  5883. float bias = 0;
  5884. float normal_bias = 0;
  5885. bool use_pancake = false;
  5886. bool use_linear_depth = false;
  5887. bool render_cubemap = false;
  5888. bool finalize_cubemap = false;
  5889. CameraMatrix light_projection;
  5890. Transform light_transform;
  5891. if (storage->light_get_type(light_instance->light) == RS::LIGHT_DIRECTIONAL) {
  5892. //set pssm stuff
  5893. if (light_instance->last_scene_shadow_pass != scene_pass) {
  5894. light_instance->directional_rect = _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, directional_shadow.current_light);
  5895. directional_shadow.current_light++;
  5896. light_instance->last_scene_shadow_pass = scene_pass;
  5897. }
  5898. use_pancake = storage->light_get_param(light_instance->light, RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE) > 0;
  5899. light_projection = light_instance->shadow_transform[p_pass].camera;
  5900. light_transform = light_instance->shadow_transform[p_pass].transform;
  5901. atlas_rect.position.x = light_instance->directional_rect.position.x;
  5902. atlas_rect.position.y = light_instance->directional_rect.position.y;
  5903. atlas_rect.size.width = light_instance->directional_rect.size.x;
  5904. atlas_rect.size.height = light_instance->directional_rect.size.y;
  5905. if (storage->light_directional_get_shadow_mode(light_instance->light) == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) {
  5906. atlas_rect.size.width /= 2;
  5907. atlas_rect.size.height /= 2;
  5908. if (p_pass == 1) {
  5909. atlas_rect.position.x += atlas_rect.size.width;
  5910. } else if (p_pass == 2) {
  5911. atlas_rect.position.y += atlas_rect.size.height;
  5912. } else if (p_pass == 3) {
  5913. atlas_rect.position.x += atlas_rect.size.width;
  5914. atlas_rect.position.y += atlas_rect.size.height;
  5915. }
  5916. } else if (storage->light_directional_get_shadow_mode(light_instance->light) == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) {
  5917. atlas_rect.size.height /= 2;
  5918. if (p_pass == 0) {
  5919. } else {
  5920. atlas_rect.position.y += atlas_rect.size.height;
  5921. }
  5922. }
  5923. light_instance->shadow_transform[p_pass].atlas_rect = atlas_rect;
  5924. light_instance->shadow_transform[p_pass].atlas_rect.position /= directional_shadow.size;
  5925. light_instance->shadow_transform[p_pass].atlas_rect.size /= directional_shadow.size;
  5926. float bias_mult = light_instance->shadow_transform[p_pass].bias_scale;
  5927. zfar = storage->light_get_param(light_instance->light, RS::LIGHT_PARAM_RANGE);
  5928. bias = storage->light_get_param(light_instance->light, RS::LIGHT_PARAM_SHADOW_BIAS) * bias_mult;
  5929. normal_bias = storage->light_get_param(light_instance->light, RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS) * bias_mult;
  5930. ShadowMap *shadow_map = _get_shadow_map(atlas_rect.size);
  5931. render_fb = shadow_map->fb;
  5932. render_texture = shadow_map->depth;
  5933. atlas_texture = directional_shadow.depth;
  5934. } else {
  5935. //set from shadow atlas
  5936. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  5937. ERR_FAIL_COND(!shadow_atlas);
  5938. ERR_FAIL_COND(!shadow_atlas->shadow_owners.has(p_light));
  5939. uint32_t key = shadow_atlas->shadow_owners[p_light];
  5940. uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  5941. uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK;
  5942. ERR_FAIL_INDEX((int)shadow, shadow_atlas->quadrants[quadrant].shadows.size());
  5943. uint32_t quadrant_size = shadow_atlas->size >> 1;
  5944. atlas_rect.position.x = (quadrant & 1) * quadrant_size;
  5945. atlas_rect.position.y = (quadrant >> 1) * quadrant_size;
  5946. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  5947. atlas_rect.position.x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  5948. atlas_rect.position.y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  5949. atlas_rect.size.width = shadow_size;
  5950. atlas_rect.size.height = shadow_size;
  5951. atlas_texture = shadow_atlas->depth;
  5952. zfar = storage->light_get_param(light_instance->light, RS::LIGHT_PARAM_RANGE);
  5953. bias = storage->light_get_param(light_instance->light, RS::LIGHT_PARAM_SHADOW_BIAS);
  5954. normal_bias = storage->light_get_param(light_instance->light, RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS);
  5955. if (storage->light_get_type(light_instance->light) == RS::LIGHT_OMNI) {
  5956. if (storage->light_omni_get_shadow_mode(light_instance->light) == RS::LIGHT_OMNI_SHADOW_CUBE) {
  5957. ShadowCubemap *cubemap = _get_shadow_cubemap(shadow_size / 2);
  5958. render_fb = cubemap->side_fb[p_pass];
  5959. render_texture = cubemap->cubemap;
  5960. light_projection = light_instance->shadow_transform[0].camera;
  5961. light_transform = light_instance->shadow_transform[0].transform;
  5962. render_cubemap = true;
  5963. finalize_cubemap = p_pass == 5;
  5964. } else {
  5965. light_projection = light_instance->shadow_transform[0].camera;
  5966. light_transform = light_instance->shadow_transform[0].transform;
  5967. atlas_rect.size.height /= 2;
  5968. atlas_rect.position.y += p_pass * atlas_rect.size.height;
  5969. using_dual_paraboloid = true;
  5970. using_dual_paraboloid_flip = p_pass == 1;
  5971. ShadowMap *shadow_map = _get_shadow_map(atlas_rect.size);
  5972. render_fb = shadow_map->fb;
  5973. render_texture = shadow_map->depth;
  5974. }
  5975. } else if (storage->light_get_type(light_instance->light) == RS::LIGHT_SPOT) {
  5976. light_projection = light_instance->shadow_transform[0].camera;
  5977. light_transform = light_instance->shadow_transform[0].transform;
  5978. ShadowMap *shadow_map = _get_shadow_map(atlas_rect.size);
  5979. render_fb = shadow_map->fb;
  5980. render_texture = shadow_map->depth;
  5981. znear = light_instance->shadow_transform[0].camera.get_z_near();
  5982. use_linear_depth = true;
  5983. }
  5984. }
  5985. if (render_cubemap) {
  5986. //rendering to cubemap
  5987. _render_shadow(render_fb, p_cull_result, p_cull_count, light_projection, light_transform, zfar, 0, 0, false, false, use_pancake, p_camera_plane, p_lod_distance_multiplier, p_screen_lod_threshold);
  5988. if (finalize_cubemap) {
  5989. //reblit
  5990. atlas_rect.size.height /= 2;
  5991. storage->get_effects()->copy_cubemap_to_dp(render_texture, atlas_texture, atlas_rect, light_projection.get_z_near(), light_projection.get_z_far(), 0.0, false);
  5992. atlas_rect.position.y += atlas_rect.size.height;
  5993. storage->get_effects()->copy_cubemap_to_dp(render_texture, atlas_texture, atlas_rect, light_projection.get_z_near(), light_projection.get_z_far(), 0.0, true);
  5994. }
  5995. } else {
  5996. //render shadow
  5997. _render_shadow(render_fb, p_cull_result, p_cull_count, light_projection, light_transform, zfar, bias, normal_bias, using_dual_paraboloid, using_dual_paraboloid_flip, use_pancake, p_camera_plane, p_lod_distance_multiplier, p_screen_lod_threshold);
  5998. //copy to atlas
  5999. if (use_linear_depth) {
  6000. storage->get_effects()->copy_depth_to_rect_and_linearize(render_texture, atlas_texture, atlas_rect, true, znear, zfar);
  6001. } else {
  6002. storage->get_effects()->copy_depth_to_rect(render_texture, atlas_texture, atlas_rect, true);
  6003. }
  6004. //does not work from depth to color
  6005. //RD::get_singleton()->texture_copy(render_texture, atlas_texture, Vector3(0, 0, 0), Vector3(atlas_rect.position.x, atlas_rect.position.y, 0), Vector3(atlas_rect.size.x, atlas_rect.size.y, 1), 0, 0, 0, 0, true);
  6006. }
  6007. }
  6008. void RendererSceneRenderRD::render_material(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID p_framebuffer, const Rect2i &p_region) {
  6009. _render_material(p_cam_transform, p_cam_projection, p_cam_ortogonal, p_cull_result, p_cull_count, p_framebuffer, p_region);
  6010. }
  6011. void RendererSceneRenderRD::render_sdfgi(RID p_render_buffers, int p_region, InstanceBase **p_cull_result, int p_cull_count) {
  6012. //print_line("rendering region " + itos(p_region));
  6013. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  6014. ERR_FAIL_COND(!rb);
  6015. ERR_FAIL_COND(!rb->sdfgi);
  6016. AABB bounds;
  6017. Vector3i from;
  6018. Vector3i size;
  6019. int cascade_prev = _sdfgi_get_pending_region_data(p_render_buffers, p_region - 1, from, size, bounds);
  6020. int cascade_next = _sdfgi_get_pending_region_data(p_render_buffers, p_region + 1, from, size, bounds);
  6021. int cascade = _sdfgi_get_pending_region_data(p_render_buffers, p_region, from, size, bounds);
  6022. ERR_FAIL_COND(cascade < 0);
  6023. if (cascade_prev != cascade) {
  6024. //initialize render
  6025. RD::get_singleton()->texture_clear(rb->sdfgi->render_albedo, Color(0, 0, 0, 0), 0, 1, 0, 1, true);
  6026. RD::get_singleton()->texture_clear(rb->sdfgi->render_emission, Color(0, 0, 0, 0), 0, 1, 0, 1, true);
  6027. RD::get_singleton()->texture_clear(rb->sdfgi->render_emission_aniso, Color(0, 0, 0, 0), 0, 1, 0, 1, true);
  6028. RD::get_singleton()->texture_clear(rb->sdfgi->render_geom_facing, Color(0, 0, 0, 0), 0, 1, 0, 1, true);
  6029. }
  6030. //print_line("rendering cascade " + itos(p_region) + " objects: " + itos(p_cull_count) + " bounds: " + bounds + " from: " + from + " size: " + size + " cell size: " + rtos(rb->sdfgi->cascades[cascade].cell_size));
  6031. _render_sdfgi(p_render_buffers, from, size, bounds, p_cull_result, p_cull_count, rb->sdfgi->render_albedo, rb->sdfgi->render_emission, rb->sdfgi->render_emission_aniso, rb->sdfgi->render_geom_facing);
  6032. if (cascade_next != cascade) {
  6033. RENDER_TIMESTAMP(">SDFGI Update SDF");
  6034. //done rendering! must update SDF
  6035. //clear dispatch indirect data
  6036. SDGIShader::PreprocessPushConstant push_constant;
  6037. zeromem(&push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  6038. RENDER_TIMESTAMP("Scroll SDF");
  6039. //scroll
  6040. if (rb->sdfgi->cascades[cascade].dirty_regions != SDFGI::Cascade::DIRTY_ALL) {
  6041. //for scroll
  6042. Vector3i dirty = rb->sdfgi->cascades[cascade].dirty_regions;
  6043. push_constant.scroll[0] = dirty.x;
  6044. push_constant.scroll[1] = dirty.y;
  6045. push_constant.scroll[2] = dirty.z;
  6046. } else {
  6047. //for no scroll
  6048. push_constant.scroll[0] = 0;
  6049. push_constant.scroll[1] = 0;
  6050. push_constant.scroll[2] = 0;
  6051. }
  6052. push_constant.grid_size = rb->sdfgi->cascade_size;
  6053. push_constant.cascade = cascade;
  6054. if (rb->sdfgi->cascades[cascade].dirty_regions != SDFGI::Cascade::DIRTY_ALL) {
  6055. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  6056. //must pre scroll existing data because not all is dirty
  6057. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_SCROLL]);
  6058. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->cascades[cascade].scroll_uniform_set, 0);
  6059. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  6060. RD::get_singleton()->compute_list_dispatch_indirect(compute_list, rb->sdfgi->cascades[cascade].solid_cell_dispatch_buffer, 0);
  6061. // no barrier do all together
  6062. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_SCROLL_OCCLUSION]);
  6063. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->cascades[cascade].scroll_occlusion_uniform_set, 0);
  6064. Vector3i dirty = rb->sdfgi->cascades[cascade].dirty_regions;
  6065. Vector3i groups;
  6066. groups.x = rb->sdfgi->cascade_size - ABS(dirty.x);
  6067. groups.y = rb->sdfgi->cascade_size - ABS(dirty.y);
  6068. groups.z = rb->sdfgi->cascade_size - ABS(dirty.z);
  6069. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  6070. RD::get_singleton()->compute_list_dispatch_threads(compute_list, groups.x, groups.y, groups.z, 4, 4, 4);
  6071. //no barrier, continue together
  6072. {
  6073. //scroll probes and their history also
  6074. SDGIShader::IntegratePushConstant ipush_constant;
  6075. ipush_constant.grid_size[1] = rb->sdfgi->cascade_size;
  6076. ipush_constant.grid_size[2] = rb->sdfgi->cascade_size;
  6077. ipush_constant.grid_size[0] = rb->sdfgi->cascade_size;
  6078. ipush_constant.max_cascades = rb->sdfgi->cascades.size();
  6079. ipush_constant.probe_axis_size = rb->sdfgi->probe_axis_count;
  6080. ipush_constant.history_index = 0;
  6081. ipush_constant.history_size = rb->sdfgi->history_size;
  6082. ipush_constant.ray_count = 0;
  6083. ipush_constant.ray_bias = 0;
  6084. ipush_constant.sky_mode = 0;
  6085. ipush_constant.sky_energy = 0;
  6086. ipush_constant.sky_color[0] = 0;
  6087. ipush_constant.sky_color[1] = 0;
  6088. ipush_constant.sky_color[2] = 0;
  6089. ipush_constant.y_mult = rb->sdfgi->y_mult;
  6090. ipush_constant.store_ambient_texture = false;
  6091. ipush_constant.image_size[0] = rb->sdfgi->probe_axis_count * rb->sdfgi->probe_axis_count;
  6092. ipush_constant.image_size[1] = rb->sdfgi->probe_axis_count;
  6093. int32_t probe_divisor = rb->sdfgi->cascade_size / SDFGI::PROBE_DIVISOR;
  6094. ipush_constant.cascade = cascade;
  6095. ipush_constant.world_offset[0] = rb->sdfgi->cascades[cascade].position.x / probe_divisor;
  6096. ipush_constant.world_offset[1] = rb->sdfgi->cascades[cascade].position.y / probe_divisor;
  6097. ipush_constant.world_offset[2] = rb->sdfgi->cascades[cascade].position.z / probe_divisor;
  6098. ipush_constant.scroll[0] = dirty.x / probe_divisor;
  6099. ipush_constant.scroll[1] = dirty.y / probe_divisor;
  6100. ipush_constant.scroll[2] = dirty.z / probe_divisor;
  6101. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.integrate_pipeline[SDGIShader::INTEGRATE_MODE_SCROLL]);
  6102. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->cascades[cascade].integrate_uniform_set, 0);
  6103. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sdfgi_shader.integrate_default_sky_uniform_set, 1);
  6104. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ipush_constant, sizeof(SDGIShader::IntegratePushConstant));
  6105. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->sdfgi->probe_axis_count * rb->sdfgi->probe_axis_count, rb->sdfgi->probe_axis_count, 1, 8, 8, 1);
  6106. RD::get_singleton()->compute_list_add_barrier(compute_list);
  6107. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.integrate_pipeline[SDGIShader::INTEGRATE_MODE_SCROLL_STORE]);
  6108. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->cascades[cascade].integrate_uniform_set, 0);
  6109. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sdfgi_shader.integrate_default_sky_uniform_set, 1);
  6110. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ipush_constant, sizeof(SDGIShader::IntegratePushConstant));
  6111. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->sdfgi->probe_axis_count * rb->sdfgi->probe_axis_count, rb->sdfgi->probe_axis_count, 1, 8, 8, 1);
  6112. }
  6113. //ok finally barrier
  6114. RD::get_singleton()->compute_list_end();
  6115. }
  6116. //clear dispatch indirect data
  6117. uint32_t dispatch_indirct_data[4] = { 0, 0, 0, 0 };
  6118. RD::get_singleton()->buffer_update(rb->sdfgi->cascades[cascade].solid_cell_dispatch_buffer, 0, sizeof(uint32_t) * 4, dispatch_indirct_data, true);
  6119. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  6120. bool half_size = true; //much faster, very little difference
  6121. static const int optimized_jf_group_size = 8;
  6122. if (half_size) {
  6123. push_constant.grid_size >>= 1;
  6124. uint32_t cascade_half_size = rb->sdfgi->cascade_size >> 1;
  6125. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_JUMP_FLOOD_INITIALIZE_HALF]);
  6126. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->sdf_initialize_half_uniform_set, 0);
  6127. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  6128. RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_half_size, cascade_half_size, cascade_half_size, 4, 4, 4);
  6129. RD::get_singleton()->compute_list_add_barrier(compute_list);
  6130. //must start with regular jumpflood
  6131. push_constant.half_size = true;
  6132. {
  6133. RENDER_TIMESTAMP("SDFGI Jump Flood (Half Size)");
  6134. uint32_t s = cascade_half_size;
  6135. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_JUMP_FLOOD]);
  6136. int jf_us = 0;
  6137. //start with regular jump flood for very coarse reads, as this is impossible to optimize
  6138. while (s > 1) {
  6139. s /= 2;
  6140. push_constant.step_size = s;
  6141. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->jump_flood_half_uniform_set[jf_us], 0);
  6142. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  6143. RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_half_size, cascade_half_size, cascade_half_size, 4, 4, 4);
  6144. RD::get_singleton()->compute_list_add_barrier(compute_list);
  6145. jf_us = jf_us == 0 ? 1 : 0;
  6146. if (cascade_half_size / (s / 2) >= optimized_jf_group_size) {
  6147. break;
  6148. }
  6149. }
  6150. RENDER_TIMESTAMP("SDFGI Jump Flood Optimized (Half Size)");
  6151. //continue with optimized jump flood for smaller reads
  6152. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_JUMP_FLOOD_OPTIMIZED]);
  6153. while (s > 1) {
  6154. s /= 2;
  6155. push_constant.step_size = s;
  6156. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->jump_flood_half_uniform_set[jf_us], 0);
  6157. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  6158. RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_half_size, cascade_half_size, cascade_half_size, optimized_jf_group_size, optimized_jf_group_size, optimized_jf_group_size);
  6159. RD::get_singleton()->compute_list_add_barrier(compute_list);
  6160. jf_us = jf_us == 0 ? 1 : 0;
  6161. }
  6162. }
  6163. // restore grid size for last passes
  6164. push_constant.grid_size = rb->sdfgi->cascade_size;
  6165. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_JUMP_FLOOD_UPSCALE]);
  6166. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->sdf_upscale_uniform_set, 0);
  6167. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  6168. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, 4, 4, 4);
  6169. RD::get_singleton()->compute_list_add_barrier(compute_list);
  6170. //run one pass of fullsize jumpflood to fix up half size arctifacts
  6171. push_constant.half_size = false;
  6172. push_constant.step_size = 1;
  6173. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_JUMP_FLOOD_OPTIMIZED]);
  6174. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->jump_flood_uniform_set[rb->sdfgi->upscale_jfa_uniform_set_index], 0);
  6175. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  6176. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, optimized_jf_group_size, optimized_jf_group_size, optimized_jf_group_size);
  6177. RD::get_singleton()->compute_list_add_barrier(compute_list);
  6178. } else {
  6179. //full size jumpflood
  6180. RENDER_TIMESTAMP("SDFGI Jump Flood");
  6181. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_JUMP_FLOOD_INITIALIZE]);
  6182. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->sdf_initialize_uniform_set, 0);
  6183. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  6184. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, 4, 4, 4);
  6185. RD::get_singleton()->compute_list_add_barrier(compute_list);
  6186. push_constant.half_size = false;
  6187. {
  6188. uint32_t s = rb->sdfgi->cascade_size;
  6189. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_JUMP_FLOOD]);
  6190. int jf_us = 0;
  6191. //start with regular jump flood for very coarse reads, as this is impossible to optimize
  6192. while (s > 1) {
  6193. s /= 2;
  6194. push_constant.step_size = s;
  6195. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->jump_flood_uniform_set[jf_us], 0);
  6196. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  6197. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, 4, 4, 4);
  6198. RD::get_singleton()->compute_list_add_barrier(compute_list);
  6199. jf_us = jf_us == 0 ? 1 : 0;
  6200. if (rb->sdfgi->cascade_size / (s / 2) >= optimized_jf_group_size) {
  6201. break;
  6202. }
  6203. }
  6204. RENDER_TIMESTAMP("SDFGI Jump Flood Optimized");
  6205. //continue with optimized jump flood for smaller reads
  6206. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_JUMP_FLOOD_OPTIMIZED]);
  6207. while (s > 1) {
  6208. s /= 2;
  6209. push_constant.step_size = s;
  6210. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->jump_flood_uniform_set[jf_us], 0);
  6211. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  6212. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, optimized_jf_group_size, optimized_jf_group_size, optimized_jf_group_size);
  6213. RD::get_singleton()->compute_list_add_barrier(compute_list);
  6214. jf_us = jf_us == 0 ? 1 : 0;
  6215. }
  6216. }
  6217. }
  6218. RENDER_TIMESTAMP("SDFGI Occlusion");
  6219. // occlusion
  6220. {
  6221. uint32_t probe_size = rb->sdfgi->cascade_size / SDFGI::PROBE_DIVISOR;
  6222. Vector3i probe_global_pos = rb->sdfgi->cascades[cascade].position / probe_size;
  6223. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_OCCLUSION]);
  6224. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->occlusion_uniform_set, 0);
  6225. for (int i = 0; i < 8; i++) {
  6226. //dispatch all at once for performance
  6227. Vector3i offset(i & 1, (i >> 1) & 1, (i >> 2) & 1);
  6228. if ((probe_global_pos.x & 1) != 0) {
  6229. offset.x = (offset.x + 1) & 1;
  6230. }
  6231. if ((probe_global_pos.y & 1) != 0) {
  6232. offset.y = (offset.y + 1) & 1;
  6233. }
  6234. if ((probe_global_pos.z & 1) != 0) {
  6235. offset.z = (offset.z + 1) & 1;
  6236. }
  6237. push_constant.probe_offset[0] = offset.x;
  6238. push_constant.probe_offset[1] = offset.y;
  6239. push_constant.probe_offset[2] = offset.z;
  6240. push_constant.occlusion_index = i;
  6241. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  6242. Vector3i groups = Vector3i(probe_size + 1, probe_size + 1, probe_size + 1) - offset; //if offset, it's one less probe per axis to compute
  6243. RD::get_singleton()->compute_list_dispatch(compute_list, groups.x, groups.y, groups.z);
  6244. }
  6245. RD::get_singleton()->compute_list_add_barrier(compute_list);
  6246. }
  6247. RENDER_TIMESTAMP("SDFGI Store");
  6248. // store
  6249. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_STORE]);
  6250. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->cascades[cascade].sdf_store_uniform_set, 0);
  6251. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  6252. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, 4, 4, 4);
  6253. RD::get_singleton()->compute_list_end();
  6254. //clear these textures, as they will have previous garbage on next draw
  6255. RD::get_singleton()->texture_clear(rb->sdfgi->cascades[cascade].light_tex, Color(0, 0, 0, 0), 0, 1, 0, 1, true);
  6256. RD::get_singleton()->texture_clear(rb->sdfgi->cascades[cascade].light_aniso_0_tex, Color(0, 0, 0, 0), 0, 1, 0, 1, true);
  6257. RD::get_singleton()->texture_clear(rb->sdfgi->cascades[cascade].light_aniso_1_tex, Color(0, 0, 0, 0), 0, 1, 0, 1, true);
  6258. #if 0
  6259. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(rb->sdfgi->cascades[cascade].sdf, 0);
  6260. Ref<Image> img;
  6261. img.instance();
  6262. for (uint32_t i = 0; i < rb->sdfgi->cascade_size; i++) {
  6263. Vector<uint8_t> subarr = data.subarray(128 * 128 * i, 128 * 128 * (i + 1) - 1);
  6264. img->create(rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, false, Image::FORMAT_L8, subarr);
  6265. img->save_png("res://cascade_sdf_" + itos(cascade) + "_" + itos(i) + ".png");
  6266. }
  6267. //finalize render and update sdf
  6268. #endif
  6269. #if 0
  6270. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(rb->sdfgi->render_albedo, 0);
  6271. Ref<Image> img;
  6272. img.instance();
  6273. for (uint32_t i = 0; i < rb->sdfgi->cascade_size; i++) {
  6274. Vector<uint8_t> subarr = data.subarray(128 * 128 * i * 2, 128 * 128 * (i + 1) * 2 - 1);
  6275. img->create(rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, false, Image::FORMAT_RGB565, subarr);
  6276. img->convert(Image::FORMAT_RGBA8);
  6277. img->save_png("res://cascade_" + itos(cascade) + "_" + itos(i) + ".png");
  6278. }
  6279. //finalize render and update sdf
  6280. #endif
  6281. RENDER_TIMESTAMP("<SDFGI Update SDF");
  6282. }
  6283. }
  6284. void RendererSceneRenderRD::render_particle_collider_heightfield(RID p_collider, const Transform &p_transform, InstanceBase **p_cull_result, int p_cull_count) {
  6285. ERR_FAIL_COND(!storage->particles_collision_is_heightfield(p_collider));
  6286. Vector3 extents = storage->particles_collision_get_extents(p_collider) * p_transform.basis.get_scale();
  6287. CameraMatrix cm;
  6288. cm.set_orthogonal(-extents.x, extents.x, -extents.z, extents.z, 0, extents.y * 2.0);
  6289. Vector3 cam_pos = p_transform.origin;
  6290. cam_pos.y += extents.y;
  6291. Transform cam_xform;
  6292. cam_xform.set_look_at(cam_pos, cam_pos - p_transform.basis.get_axis(Vector3::AXIS_Y), -p_transform.basis.get_axis(Vector3::AXIS_Z).normalized());
  6293. RID fb = storage->particles_collision_get_heightfield_framebuffer(p_collider);
  6294. _render_particle_collider_heightfield(fb, cam_xform, cm, p_cull_result, p_cull_count);
  6295. }
  6296. void RendererSceneRenderRD::render_sdfgi_static_lights(RID p_render_buffers, uint32_t p_cascade_count, const uint32_t *p_cascade_indices, const RID **p_positional_light_cull_result, const uint32_t *p_positional_light_cull_count) {
  6297. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  6298. ERR_FAIL_COND(!rb);
  6299. ERR_FAIL_COND(!rb->sdfgi);
  6300. ERR_FAIL_COND(p_positional_light_cull_count == 0);
  6301. _sdfgi_update_cascades(p_render_buffers); //need cascades updated for this
  6302. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  6303. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.direct_light_pipeline[SDGIShader::DIRECT_LIGHT_MODE_STATIC]);
  6304. SDGIShader::DirectLightPushConstant dl_push_constant;
  6305. dl_push_constant.grid_size[0] = rb->sdfgi->cascade_size;
  6306. dl_push_constant.grid_size[1] = rb->sdfgi->cascade_size;
  6307. dl_push_constant.grid_size[2] = rb->sdfgi->cascade_size;
  6308. dl_push_constant.max_cascades = rb->sdfgi->cascades.size();
  6309. dl_push_constant.probe_axis_size = rb->sdfgi->probe_axis_count;
  6310. dl_push_constant.multibounce = false; // this is static light, do not multibounce yet
  6311. dl_push_constant.y_mult = rb->sdfgi->y_mult;
  6312. //all must be processed
  6313. dl_push_constant.process_offset = 0;
  6314. dl_push_constant.process_increment = 1;
  6315. SDGIShader::Light lights[SDFGI::MAX_STATIC_LIGHTS];
  6316. for (uint32_t i = 0; i < p_cascade_count; i++) {
  6317. ERR_CONTINUE(p_cascade_indices[i] >= rb->sdfgi->cascades.size());
  6318. SDFGI::Cascade &cc = rb->sdfgi->cascades[p_cascade_indices[i]];
  6319. { //fill light buffer
  6320. AABB cascade_aabb;
  6321. cascade_aabb.position = Vector3((Vector3i(1, 1, 1) * -int32_t(rb->sdfgi->cascade_size >> 1) + cc.position)) * cc.cell_size;
  6322. cascade_aabb.size = Vector3(1, 1, 1) * rb->sdfgi->cascade_size * cc.cell_size;
  6323. int idx = 0;
  6324. for (uint32_t j = 0; j < p_positional_light_cull_count[i]; j++) {
  6325. if (idx == SDFGI::MAX_STATIC_LIGHTS) {
  6326. break;
  6327. }
  6328. LightInstance *li = light_instance_owner.getornull(p_positional_light_cull_result[i][j]);
  6329. ERR_CONTINUE(!li);
  6330. uint32_t max_sdfgi_cascade = storage->light_get_max_sdfgi_cascade(li->light);
  6331. if (p_cascade_indices[i] > max_sdfgi_cascade) {
  6332. continue;
  6333. }
  6334. if (!cascade_aabb.intersects(li->aabb)) {
  6335. continue;
  6336. }
  6337. lights[idx].type = storage->light_get_type(li->light);
  6338. Vector3 dir = -li->transform.basis.get_axis(Vector3::AXIS_Z);
  6339. if (lights[idx].type == RS::LIGHT_DIRECTIONAL) {
  6340. dir.y *= rb->sdfgi->y_mult; //only makes sense for directional
  6341. dir.normalize();
  6342. }
  6343. lights[idx].direction[0] = dir.x;
  6344. lights[idx].direction[1] = dir.y;
  6345. lights[idx].direction[2] = dir.z;
  6346. Vector3 pos = li->transform.origin;
  6347. pos.y *= rb->sdfgi->y_mult;
  6348. lights[idx].position[0] = pos.x;
  6349. lights[idx].position[1] = pos.y;
  6350. lights[idx].position[2] = pos.z;
  6351. Color color = storage->light_get_color(li->light);
  6352. color = color.to_linear();
  6353. lights[idx].color[0] = color.r;
  6354. lights[idx].color[1] = color.g;
  6355. lights[idx].color[2] = color.b;
  6356. lights[idx].energy = storage->light_get_param(li->light, RS::LIGHT_PARAM_ENERGY);
  6357. lights[idx].has_shadow = storage->light_has_shadow(li->light);
  6358. lights[idx].attenuation = storage->light_get_param(li->light, RS::LIGHT_PARAM_ATTENUATION);
  6359. lights[idx].radius = storage->light_get_param(li->light, RS::LIGHT_PARAM_RANGE);
  6360. lights[idx].spot_angle = Math::deg2rad(storage->light_get_param(li->light, RS::LIGHT_PARAM_SPOT_ANGLE));
  6361. lights[idx].spot_attenuation = storage->light_get_param(li->light, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  6362. idx++;
  6363. }
  6364. if (idx > 0) {
  6365. RD::get_singleton()->buffer_update(cc.lights_buffer, 0, idx * sizeof(SDGIShader::Light), lights, true);
  6366. }
  6367. dl_push_constant.light_count = idx;
  6368. }
  6369. dl_push_constant.cascade = p_cascade_indices[i];
  6370. if (dl_push_constant.light_count > 0) {
  6371. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cc.sdf_direct_light_uniform_set, 0);
  6372. RD::get_singleton()->compute_list_set_push_constant(compute_list, &dl_push_constant, sizeof(SDGIShader::DirectLightPushConstant));
  6373. RD::get_singleton()->compute_list_dispatch_indirect(compute_list, cc.solid_cell_dispatch_buffer, 0);
  6374. }
  6375. }
  6376. RD::get_singleton()->compute_list_end();
  6377. }
  6378. bool RendererSceneRenderRD::free(RID p_rid) {
  6379. if (render_buffers_owner.owns(p_rid)) {
  6380. RenderBuffers *rb = render_buffers_owner.getornull(p_rid);
  6381. _free_render_buffer_data(rb);
  6382. memdelete(rb->data);
  6383. if (rb->sdfgi) {
  6384. _sdfgi_erase(rb);
  6385. }
  6386. if (rb->volumetric_fog) {
  6387. _volumetric_fog_erase(rb);
  6388. }
  6389. render_buffers_owner.free(p_rid);
  6390. } else if (environment_owner.owns(p_rid)) {
  6391. //not much to delete, just free it
  6392. environment_owner.free(p_rid);
  6393. } else if (camera_effects_owner.owns(p_rid)) {
  6394. //not much to delete, just free it
  6395. camera_effects_owner.free(p_rid);
  6396. } else if (reflection_atlas_owner.owns(p_rid)) {
  6397. reflection_atlas_set_size(p_rid, 0, 0);
  6398. reflection_atlas_owner.free(p_rid);
  6399. } else if (reflection_probe_instance_owner.owns(p_rid)) {
  6400. //not much to delete, just free it
  6401. //ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_rid);
  6402. reflection_probe_release_atlas_index(p_rid);
  6403. reflection_probe_instance_owner.free(p_rid);
  6404. } else if (decal_instance_owner.owns(p_rid)) {
  6405. decal_instance_owner.free(p_rid);
  6406. } else if (gi_probe_instance_owner.owns(p_rid)) {
  6407. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_rid);
  6408. if (gi_probe->texture.is_valid()) {
  6409. RD::get_singleton()->free(gi_probe->texture);
  6410. RD::get_singleton()->free(gi_probe->write_buffer);
  6411. }
  6412. for (int i = 0; i < gi_probe->dynamic_maps.size(); i++) {
  6413. RD::get_singleton()->free(gi_probe->dynamic_maps[i].texture);
  6414. RD::get_singleton()->free(gi_probe->dynamic_maps[i].depth);
  6415. }
  6416. gi_probe_instance_owner.free(p_rid);
  6417. } else if (sky_owner.owns(p_rid)) {
  6418. _update_dirty_skys();
  6419. Sky *sky = sky_owner.getornull(p_rid);
  6420. if (sky->radiance.is_valid()) {
  6421. RD::get_singleton()->free(sky->radiance);
  6422. sky->radiance = RID();
  6423. }
  6424. _clear_reflection_data(sky->reflection);
  6425. if (sky->uniform_buffer.is_valid()) {
  6426. RD::get_singleton()->free(sky->uniform_buffer);
  6427. sky->uniform_buffer = RID();
  6428. }
  6429. if (sky->half_res_pass.is_valid()) {
  6430. RD::get_singleton()->free(sky->half_res_pass);
  6431. sky->half_res_pass = RID();
  6432. }
  6433. if (sky->quarter_res_pass.is_valid()) {
  6434. RD::get_singleton()->free(sky->quarter_res_pass);
  6435. sky->quarter_res_pass = RID();
  6436. }
  6437. if (sky->material.is_valid()) {
  6438. storage->free(sky->material);
  6439. }
  6440. sky_owner.free(p_rid);
  6441. } else if (light_instance_owner.owns(p_rid)) {
  6442. LightInstance *light_instance = light_instance_owner.getornull(p_rid);
  6443. //remove from shadow atlases..
  6444. for (Set<RID>::Element *E = light_instance->shadow_atlases.front(); E; E = E->next()) {
  6445. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(E->get());
  6446. ERR_CONTINUE(!shadow_atlas->shadow_owners.has(p_rid));
  6447. uint32_t key = shadow_atlas->shadow_owners[p_rid];
  6448. uint32_t q = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  6449. uint32_t s = key & ShadowAtlas::SHADOW_INDEX_MASK;
  6450. shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
  6451. shadow_atlas->shadow_owners.erase(p_rid);
  6452. }
  6453. light_instance_owner.free(p_rid);
  6454. } else if (shadow_atlas_owner.owns(p_rid)) {
  6455. shadow_atlas_set_size(p_rid, 0);
  6456. shadow_atlas_owner.free(p_rid);
  6457. } else {
  6458. return false;
  6459. }
  6460. return true;
  6461. }
  6462. void RendererSceneRenderRD::set_debug_draw_mode(RS::ViewportDebugDraw p_debug_draw) {
  6463. debug_draw = p_debug_draw;
  6464. }
  6465. void RendererSceneRenderRD::update() {
  6466. _update_dirty_skys();
  6467. }
  6468. void RendererSceneRenderRD::set_time(double p_time, double p_step) {
  6469. time = p_time;
  6470. time_step = p_step;
  6471. }
  6472. void RendererSceneRenderRD::screen_space_roughness_limiter_set_active(bool p_enable, float p_amount, float p_limit) {
  6473. screen_space_roughness_limiter = p_enable;
  6474. screen_space_roughness_limiter_amount = p_amount;
  6475. screen_space_roughness_limiter_limit = p_limit;
  6476. }
  6477. bool RendererSceneRenderRD::screen_space_roughness_limiter_is_active() const {
  6478. return screen_space_roughness_limiter;
  6479. }
  6480. float RendererSceneRenderRD::screen_space_roughness_limiter_get_amount() const {
  6481. return screen_space_roughness_limiter_amount;
  6482. }
  6483. float RendererSceneRenderRD::screen_space_roughness_limiter_get_limit() const {
  6484. return screen_space_roughness_limiter_limit;
  6485. }
  6486. TypedArray<Image> RendererSceneRenderRD::bake_render_uv2(RID p_base, const Vector<RID> &p_material_overrides, const Size2i &p_image_size) {
  6487. RD::TextureFormat tf;
  6488. tf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  6489. tf.width = p_image_size.width; // Always 64x64
  6490. tf.height = p_image_size.height;
  6491. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  6492. RID albedo_alpha_tex = RD::get_singleton()->texture_create(tf, RD::TextureView());
  6493. RID normal_tex = RD::get_singleton()->texture_create(tf, RD::TextureView());
  6494. RID orm_tex = RD::get_singleton()->texture_create(tf, RD::TextureView());
  6495. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  6496. RID emission_tex = RD::get_singleton()->texture_create(tf, RD::TextureView());
  6497. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  6498. RID depth_write_tex = RD::get_singleton()->texture_create(tf, RD::TextureView());
  6499. tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  6500. tf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D32_SFLOAT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D32_SFLOAT : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
  6501. RID depth_tex = RD::get_singleton()->texture_create(tf, RD::TextureView());
  6502. Vector<RID> fb_tex;
  6503. fb_tex.push_back(albedo_alpha_tex);
  6504. fb_tex.push_back(normal_tex);
  6505. fb_tex.push_back(orm_tex);
  6506. fb_tex.push_back(emission_tex);
  6507. fb_tex.push_back(depth_write_tex);
  6508. fb_tex.push_back(depth_tex);
  6509. RID fb = RD::get_singleton()->framebuffer_create(fb_tex);
  6510. //RID sampled_light;
  6511. InstanceBase ins;
  6512. ins.base_type = RSG::storage->get_base_type(p_base);
  6513. ins.base = p_base;
  6514. ins.materials.resize(RSG::storage->mesh_get_surface_count(p_base));
  6515. for (int i = 0; i < ins.materials.size(); i++) {
  6516. if (i < p_material_overrides.size()) {
  6517. ins.materials.write[i] = p_material_overrides[i];
  6518. }
  6519. }
  6520. InstanceBase *cull = &ins;
  6521. _render_uv2(&cull, 1, fb, Rect2i(0, 0, p_image_size.width, p_image_size.height));
  6522. TypedArray<Image> ret;
  6523. {
  6524. PackedByteArray data = RD::get_singleton()->texture_get_data(albedo_alpha_tex, 0);
  6525. Ref<Image> img;
  6526. img.instance();
  6527. img->create(p_image_size.width, p_image_size.height, false, Image::FORMAT_RGBA8, data);
  6528. RD::get_singleton()->free(albedo_alpha_tex);
  6529. ret.push_back(img);
  6530. }
  6531. {
  6532. PackedByteArray data = RD::get_singleton()->texture_get_data(normal_tex, 0);
  6533. Ref<Image> img;
  6534. img.instance();
  6535. img->create(p_image_size.width, p_image_size.height, false, Image::FORMAT_RGBA8, data);
  6536. RD::get_singleton()->free(normal_tex);
  6537. ret.push_back(img);
  6538. }
  6539. {
  6540. PackedByteArray data = RD::get_singleton()->texture_get_data(orm_tex, 0);
  6541. Ref<Image> img;
  6542. img.instance();
  6543. img->create(p_image_size.width, p_image_size.height, false, Image::FORMAT_RGBA8, data);
  6544. RD::get_singleton()->free(orm_tex);
  6545. ret.push_back(img);
  6546. }
  6547. {
  6548. PackedByteArray data = RD::get_singleton()->texture_get_data(emission_tex, 0);
  6549. Ref<Image> img;
  6550. img.instance();
  6551. img->create(p_image_size.width, p_image_size.height, false, Image::FORMAT_RGBAH, data);
  6552. RD::get_singleton()->free(emission_tex);
  6553. ret.push_back(img);
  6554. }
  6555. RD::get_singleton()->free(depth_write_tex);
  6556. RD::get_singleton()->free(depth_tex);
  6557. return ret;
  6558. }
  6559. void RendererSceneRenderRD::sdfgi_set_debug_probe_select(const Vector3 &p_position, const Vector3 &p_dir) {
  6560. sdfgi_debug_probe_pos = p_position;
  6561. sdfgi_debug_probe_dir = p_dir;
  6562. }
  6563. RendererSceneRenderRD *RendererSceneRenderRD::singleton = nullptr;
  6564. RID RendererSceneRenderRD::get_cluster_builder_texture() {
  6565. return cluster.builder.get_cluster_texture();
  6566. }
  6567. RID RendererSceneRenderRD::get_cluster_builder_indices_buffer() {
  6568. return cluster.builder.get_cluster_indices_buffer();
  6569. }
  6570. RID RendererSceneRenderRD::get_reflection_probe_buffer() {
  6571. return cluster.reflection_buffer;
  6572. }
  6573. RID RendererSceneRenderRD::get_positional_light_buffer() {
  6574. return cluster.light_buffer;
  6575. }
  6576. RID RendererSceneRenderRD::get_directional_light_buffer() {
  6577. return cluster.directional_light_buffer;
  6578. }
  6579. RID RendererSceneRenderRD::get_decal_buffer() {
  6580. return cluster.decal_buffer;
  6581. }
  6582. int RendererSceneRenderRD::get_max_directional_lights() const {
  6583. return cluster.max_directional_lights;
  6584. }
  6585. bool RendererSceneRenderRD::is_low_end() const {
  6586. return low_end;
  6587. }
  6588. RendererSceneRenderRD::RendererSceneRenderRD(RendererStorageRD *p_storage) {
  6589. storage = p_storage;
  6590. singleton = this;
  6591. roughness_layers = GLOBAL_GET("rendering/quality/reflections/roughness_layers");
  6592. sky_ggx_samples_quality = GLOBAL_GET("rendering/quality/reflections/ggx_samples");
  6593. sky_use_cubemap_array = GLOBAL_GET("rendering/quality/reflections/texture_array_reflections");
  6594. // sky_use_cubemap_array = false;
  6595. uint32_t textures_per_stage = RD::get_singleton()->limit_get(RD::LIMIT_MAX_TEXTURES_PER_SHADER_STAGE);
  6596. low_end = GLOBAL_GET("rendering/quality/rd_renderer/use_low_end_renderer");
  6597. if (textures_per_stage < 48) {
  6598. low_end = true;
  6599. }
  6600. if (!low_end) {
  6601. //kinda complicated to compute the amount of slots, we try to use as many as we can
  6602. gi_probe_max_lights = 32;
  6603. gi_probe_lights = memnew_arr(GIProbeLight, gi_probe_max_lights);
  6604. gi_probe_lights_uniform = RD::get_singleton()->uniform_buffer_create(gi_probe_max_lights * sizeof(GIProbeLight));
  6605. gi_probe_quality = RS::GIProbeQuality(CLAMP(int(GLOBAL_GET("rendering/quality/gi_probes/quality")), 0, 1));
  6606. String defines = "\n#define MAX_LIGHTS " + itos(gi_probe_max_lights) + "\n";
  6607. Vector<String> versions;
  6608. versions.push_back("\n#define MODE_COMPUTE_LIGHT\n");
  6609. versions.push_back("\n#define MODE_SECOND_BOUNCE\n");
  6610. versions.push_back("\n#define MODE_UPDATE_MIPMAPS\n");
  6611. versions.push_back("\n#define MODE_WRITE_TEXTURE\n");
  6612. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_LIGHTING\n");
  6613. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_WRITE\n");
  6614. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_PLOT\n");
  6615. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_PLOT\n#define MODE_DYNAMIC_SHRINK_WRITE\n");
  6616. giprobe_shader.initialize(versions, defines);
  6617. giprobe_lighting_shader_version = giprobe_shader.version_create();
  6618. for (int i = 0; i < GI_PROBE_SHADER_VERSION_MAX; i++) {
  6619. giprobe_lighting_shader_version_shaders[i] = giprobe_shader.version_get_shader(giprobe_lighting_shader_version, i);
  6620. giprobe_lighting_shader_version_pipelines[i] = RD::get_singleton()->compute_pipeline_create(giprobe_lighting_shader_version_shaders[i]);
  6621. }
  6622. }
  6623. if (!low_end) {
  6624. String defines;
  6625. Vector<String> versions;
  6626. versions.push_back("\n#define MODE_DEBUG_COLOR\n");
  6627. versions.push_back("\n#define MODE_DEBUG_LIGHT\n");
  6628. versions.push_back("\n#define MODE_DEBUG_EMISSION\n");
  6629. versions.push_back("\n#define MODE_DEBUG_LIGHT\n#define MODE_DEBUG_LIGHT_FULL\n");
  6630. giprobe_debug_shader.initialize(versions, defines);
  6631. giprobe_debug_shader_version = giprobe_debug_shader.version_create();
  6632. for (int i = 0; i < GI_PROBE_DEBUG_MAX; i++) {
  6633. giprobe_debug_shader_version_shaders[i] = giprobe_debug_shader.version_get_shader(giprobe_debug_shader_version, i);
  6634. RD::PipelineRasterizationState rs;
  6635. rs.cull_mode = RD::POLYGON_CULL_FRONT;
  6636. RD::PipelineDepthStencilState ds;
  6637. ds.enable_depth_test = true;
  6638. ds.enable_depth_write = true;
  6639. ds.depth_compare_operator = RD::COMPARE_OP_LESS_OR_EQUAL;
  6640. giprobe_debug_shader_version_pipelines[i].setup(giprobe_debug_shader_version_shaders[i], RD::RENDER_PRIMITIVE_TRIANGLES, rs, RD::PipelineMultisampleState(), ds, RD::PipelineColorBlendState::create_disabled(), 0);
  6641. }
  6642. }
  6643. /* SKY SHADER */
  6644. {
  6645. // Start with the directional lights for the sky
  6646. sky_scene_state.max_directional_lights = 4;
  6647. uint32_t directional_light_buffer_size = sky_scene_state.max_directional_lights * sizeof(SkyDirectionalLightData);
  6648. sky_scene_state.directional_lights = memnew_arr(SkyDirectionalLightData, sky_scene_state.max_directional_lights);
  6649. sky_scene_state.last_frame_directional_lights = memnew_arr(SkyDirectionalLightData, sky_scene_state.max_directional_lights);
  6650. sky_scene_state.last_frame_directional_light_count = sky_scene_state.max_directional_lights + 1;
  6651. sky_scene_state.directional_light_buffer = RD::get_singleton()->uniform_buffer_create(directional_light_buffer_size);
  6652. String defines = "\n#define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS " + itos(sky_scene_state.max_directional_lights) + "\n";
  6653. // Initialize sky
  6654. Vector<String> sky_modes;
  6655. sky_modes.push_back(""); // Full size
  6656. sky_modes.push_back("\n#define USE_HALF_RES_PASS\n"); // Half Res
  6657. sky_modes.push_back("\n#define USE_QUARTER_RES_PASS\n"); // Quarter res
  6658. sky_modes.push_back("\n#define USE_CUBEMAP_PASS\n"); // Cubemap
  6659. sky_modes.push_back("\n#define USE_CUBEMAP_PASS\n#define USE_HALF_RES_PASS\n"); // Half Res Cubemap
  6660. sky_modes.push_back("\n#define USE_CUBEMAP_PASS\n#define USE_QUARTER_RES_PASS\n"); // Quarter res Cubemap
  6661. sky_shader.shader.initialize(sky_modes, defines);
  6662. }
  6663. // register our shader funds
  6664. storage->shader_set_data_request_function(RendererStorageRD::SHADER_TYPE_SKY, _create_sky_shader_funcs);
  6665. storage->material_set_data_request_function(RendererStorageRD::SHADER_TYPE_SKY, _create_sky_material_funcs);
  6666. {
  6667. ShaderCompilerRD::DefaultIdentifierActions actions;
  6668. actions.renames["COLOR"] = "color";
  6669. actions.renames["ALPHA"] = "alpha";
  6670. actions.renames["EYEDIR"] = "cube_normal";
  6671. actions.renames["POSITION"] = "params.position_multiplier.xyz";
  6672. actions.renames["SKY_COORDS"] = "panorama_coords";
  6673. actions.renames["SCREEN_UV"] = "uv";
  6674. actions.renames["TIME"] = "params.time";
  6675. actions.renames["HALF_RES_COLOR"] = "half_res_color";
  6676. actions.renames["QUARTER_RES_COLOR"] = "quarter_res_color";
  6677. actions.renames["RADIANCE"] = "radiance";
  6678. actions.renames["FOG"] = "custom_fog";
  6679. actions.renames["LIGHT0_ENABLED"] = "directional_lights.data[0].enabled";
  6680. actions.renames["LIGHT0_DIRECTION"] = "directional_lights.data[0].direction_energy.xyz";
  6681. actions.renames["LIGHT0_ENERGY"] = "directional_lights.data[0].direction_energy.w";
  6682. actions.renames["LIGHT0_COLOR"] = "directional_lights.data[0].color_size.xyz";
  6683. actions.renames["LIGHT0_SIZE"] = "directional_lights.data[0].color_size.w";
  6684. actions.renames["LIGHT1_ENABLED"] = "directional_lights.data[1].enabled";
  6685. actions.renames["LIGHT1_DIRECTION"] = "directional_lights.data[1].direction_energy.xyz";
  6686. actions.renames["LIGHT1_ENERGY"] = "directional_lights.data[1].direction_energy.w";
  6687. actions.renames["LIGHT1_COLOR"] = "directional_lights.data[1].color_size.xyz";
  6688. actions.renames["LIGHT1_SIZE"] = "directional_lights.data[1].color_size.w";
  6689. actions.renames["LIGHT2_ENABLED"] = "directional_lights.data[2].enabled";
  6690. actions.renames["LIGHT2_DIRECTION"] = "directional_lights.data[2].direction_energy.xyz";
  6691. actions.renames["LIGHT2_ENERGY"] = "directional_lights.data[2].direction_energy.w";
  6692. actions.renames["LIGHT2_COLOR"] = "directional_lights.data[2].color_size.xyz";
  6693. actions.renames["LIGHT2_SIZE"] = "directional_lights.data[2].color_size.w";
  6694. actions.renames["LIGHT3_ENABLED"] = "directional_lights.data[3].enabled";
  6695. actions.renames["LIGHT3_DIRECTION"] = "directional_lights.data[3].direction_energy.xyz";
  6696. actions.renames["LIGHT3_ENERGY"] = "directional_lights.data[3].direction_energy.w";
  6697. actions.renames["LIGHT3_COLOR"] = "directional_lights.data[3].color_size.xyz";
  6698. actions.renames["LIGHT3_SIZE"] = "directional_lights.data[3].color_size.w";
  6699. actions.renames["AT_CUBEMAP_PASS"] = "AT_CUBEMAP_PASS";
  6700. actions.renames["AT_HALF_RES_PASS"] = "AT_HALF_RES_PASS";
  6701. actions.renames["AT_QUARTER_RES_PASS"] = "AT_QUARTER_RES_PASS";
  6702. actions.custom_samplers["RADIANCE"] = "material_samplers[3]";
  6703. actions.usage_defines["HALF_RES_COLOR"] = "\n#define USES_HALF_RES_COLOR\n";
  6704. actions.usage_defines["QUARTER_RES_COLOR"] = "\n#define USES_QUARTER_RES_COLOR\n";
  6705. actions.render_mode_defines["disable_fog"] = "#define DISABLE_FOG\n";
  6706. actions.sampler_array_name = "material_samplers";
  6707. actions.base_texture_binding_index = 1;
  6708. actions.texture_layout_set = 1;
  6709. actions.base_uniform_string = "material.";
  6710. actions.base_varying_index = 10;
  6711. actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP;
  6712. actions.default_repeat = ShaderLanguage::REPEAT_ENABLE;
  6713. actions.global_buffer_array_variable = "global_variables.data";
  6714. sky_shader.compiler.initialize(actions);
  6715. }
  6716. {
  6717. // default material and shader for sky shader
  6718. sky_shader.default_shader = storage->shader_create();
  6719. storage->shader_set_code(sky_shader.default_shader, "shader_type sky; void fragment() { COLOR = vec3(0.0); } \n");
  6720. sky_shader.default_material = storage->material_create();
  6721. storage->material_set_shader(sky_shader.default_material, sky_shader.default_shader);
  6722. SkyMaterialData *md = (SkyMaterialData *)storage->material_get_data(sky_shader.default_material, RendererStorageRD::SHADER_TYPE_SKY);
  6723. sky_shader.default_shader_rd = sky_shader.shader.version_get_shader(md->shader_data->version, SKY_VERSION_BACKGROUND);
  6724. sky_scene_state.uniform_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(SkySceneState::UBO));
  6725. Vector<RD::Uniform> uniforms;
  6726. {
  6727. RD::Uniform u;
  6728. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  6729. u.binding = 0;
  6730. u.ids.resize(12);
  6731. RID *ids_ptr = u.ids.ptrw();
  6732. ids_ptr[0] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  6733. ids_ptr[1] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  6734. ids_ptr[2] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  6735. ids_ptr[3] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  6736. ids_ptr[4] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  6737. ids_ptr[5] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  6738. ids_ptr[6] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  6739. ids_ptr[7] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  6740. ids_ptr[8] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  6741. ids_ptr[9] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  6742. ids_ptr[10] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  6743. ids_ptr[11] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  6744. uniforms.push_back(u);
  6745. }
  6746. {
  6747. RD::Uniform u;
  6748. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  6749. u.binding = 1;
  6750. u.ids.push_back(storage->global_variables_get_storage_buffer());
  6751. uniforms.push_back(u);
  6752. }
  6753. {
  6754. RD::Uniform u;
  6755. u.binding = 2;
  6756. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  6757. u.ids.push_back(sky_scene_state.uniform_buffer);
  6758. uniforms.push_back(u);
  6759. }
  6760. {
  6761. RD::Uniform u;
  6762. u.binding = 3;
  6763. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  6764. u.ids.push_back(sky_scene_state.directional_light_buffer);
  6765. uniforms.push_back(u);
  6766. }
  6767. sky_scene_state.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sky_shader.default_shader_rd, SKY_SET_UNIFORMS);
  6768. }
  6769. {
  6770. Vector<RD::Uniform> uniforms;
  6771. {
  6772. RD::Uniform u;
  6773. u.binding = 0;
  6774. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  6775. RID vfog = storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE);
  6776. u.ids.push_back(vfog);
  6777. uniforms.push_back(u);
  6778. }
  6779. sky_scene_state.default_fog_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sky_shader.default_shader_rd, SKY_SET_FOG);
  6780. }
  6781. {
  6782. // Need defaults for using fog with clear color
  6783. sky_scene_state.fog_shader = storage->shader_create();
  6784. storage->shader_set_code(sky_scene_state.fog_shader, "shader_type sky; uniform vec4 clear_color; void fragment() { COLOR = clear_color.rgb; } \n");
  6785. sky_scene_state.fog_material = storage->material_create();
  6786. storage->material_set_shader(sky_scene_state.fog_material, sky_scene_state.fog_shader);
  6787. Vector<RD::Uniform> uniforms;
  6788. {
  6789. RD::Uniform u;
  6790. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  6791. u.binding = 0;
  6792. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  6793. uniforms.push_back(u);
  6794. }
  6795. {
  6796. RD::Uniform u;
  6797. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  6798. u.binding = 1;
  6799. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_WHITE));
  6800. uniforms.push_back(u);
  6801. }
  6802. {
  6803. RD::Uniform u;
  6804. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  6805. u.binding = 2;
  6806. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_WHITE));
  6807. uniforms.push_back(u);
  6808. }
  6809. sky_scene_state.fog_only_texture_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sky_shader.default_shader_rd, SKY_SET_TEXTURES);
  6810. }
  6811. if (!low_end) {
  6812. //SDFGI
  6813. {
  6814. Vector<String> preprocess_modes;
  6815. preprocess_modes.push_back("\n#define MODE_SCROLL\n");
  6816. preprocess_modes.push_back("\n#define MODE_SCROLL_OCCLUSION\n");
  6817. preprocess_modes.push_back("\n#define MODE_INITIALIZE_JUMP_FLOOD\n");
  6818. preprocess_modes.push_back("\n#define MODE_INITIALIZE_JUMP_FLOOD_HALF\n");
  6819. preprocess_modes.push_back("\n#define MODE_JUMPFLOOD\n");
  6820. preprocess_modes.push_back("\n#define MODE_JUMPFLOOD_OPTIMIZED\n");
  6821. preprocess_modes.push_back("\n#define MODE_UPSCALE_JUMP_FLOOD\n");
  6822. preprocess_modes.push_back("\n#define MODE_OCCLUSION\n");
  6823. preprocess_modes.push_back("\n#define MODE_STORE\n");
  6824. String defines = "\n#define OCCLUSION_SIZE " + itos(SDFGI::CASCADE_SIZE / SDFGI::PROBE_DIVISOR) + "\n";
  6825. sdfgi_shader.preprocess.initialize(preprocess_modes, defines);
  6826. sdfgi_shader.preprocess_shader = sdfgi_shader.preprocess.version_create();
  6827. for (int i = 0; i < SDGIShader::PRE_PROCESS_MAX; i++) {
  6828. sdfgi_shader.preprocess_pipeline[i] = RD::get_singleton()->compute_pipeline_create(sdfgi_shader.preprocess.version_get_shader(sdfgi_shader.preprocess_shader, i));
  6829. }
  6830. }
  6831. {
  6832. //calculate tables
  6833. String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
  6834. Vector<String> direct_light_modes;
  6835. direct_light_modes.push_back("\n#define MODE_PROCESS_STATIC\n");
  6836. direct_light_modes.push_back("\n#define MODE_PROCESS_DYNAMIC\n");
  6837. sdfgi_shader.direct_light.initialize(direct_light_modes, defines);
  6838. sdfgi_shader.direct_light_shader = sdfgi_shader.direct_light.version_create();
  6839. for (int i = 0; i < SDGIShader::DIRECT_LIGHT_MODE_MAX; i++) {
  6840. sdfgi_shader.direct_light_pipeline[i] = RD::get_singleton()->compute_pipeline_create(sdfgi_shader.direct_light.version_get_shader(sdfgi_shader.direct_light_shader, i));
  6841. }
  6842. }
  6843. {
  6844. //calculate tables
  6845. String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
  6846. defines += "\n#define SH_SIZE " + itos(SDFGI::SH_SIZE) + "\n";
  6847. Vector<String> integrate_modes;
  6848. integrate_modes.push_back("\n#define MODE_PROCESS\n");
  6849. integrate_modes.push_back("\n#define MODE_STORE\n");
  6850. integrate_modes.push_back("\n#define MODE_SCROLL\n");
  6851. integrate_modes.push_back("\n#define MODE_SCROLL_STORE\n");
  6852. sdfgi_shader.integrate.initialize(integrate_modes, defines);
  6853. sdfgi_shader.integrate_shader = sdfgi_shader.integrate.version_create();
  6854. for (int i = 0; i < SDGIShader::INTEGRATE_MODE_MAX; i++) {
  6855. sdfgi_shader.integrate_pipeline[i] = RD::get_singleton()->compute_pipeline_create(sdfgi_shader.integrate.version_get_shader(sdfgi_shader.integrate_shader, i));
  6856. }
  6857. {
  6858. Vector<RD::Uniform> uniforms;
  6859. {
  6860. RD::Uniform u;
  6861. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  6862. u.binding = 0;
  6863. u.ids.push_back(storage->texture_rd_get_default(RendererStorageRD::DEFAULT_RD_TEXTURE_CUBEMAP_WHITE));
  6864. uniforms.push_back(u);
  6865. }
  6866. {
  6867. RD::Uniform u;
  6868. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  6869. u.binding = 1;
  6870. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  6871. uniforms.push_back(u);
  6872. }
  6873. sdfgi_shader.integrate_default_sky_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.integrate.version_get_shader(sdfgi_shader.integrate_shader, 0), 1);
  6874. }
  6875. }
  6876. {
  6877. //calculate tables
  6878. String defines = "\n#define SDFGI_OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
  6879. Vector<String> gi_modes;
  6880. gi_modes.push_back("");
  6881. gi.shader.initialize(gi_modes, defines);
  6882. gi.shader_version = gi.shader.version_create();
  6883. for (int i = 0; i < GI::MODE_MAX; i++) {
  6884. gi.pipelines[i] = RD::get_singleton()->compute_pipeline_create(gi.shader.version_get_shader(gi.shader_version, i));
  6885. }
  6886. gi.sdfgi_ubo = RD::get_singleton()->uniform_buffer_create(sizeof(GI::SDFGIData));
  6887. }
  6888. {
  6889. String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
  6890. Vector<String> debug_modes;
  6891. debug_modes.push_back("");
  6892. sdfgi_shader.debug.initialize(debug_modes, defines);
  6893. sdfgi_shader.debug_shader = sdfgi_shader.debug.version_create();
  6894. sdfgi_shader.debug_shader_version = sdfgi_shader.debug.version_get_shader(sdfgi_shader.debug_shader, 0);
  6895. sdfgi_shader.debug_pipeline = RD::get_singleton()->compute_pipeline_create(sdfgi_shader.debug_shader_version);
  6896. }
  6897. {
  6898. String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
  6899. Vector<String> versions;
  6900. versions.push_back("\n#define MODE_PROBES\n");
  6901. versions.push_back("\n#define MODE_VISIBILITY\n");
  6902. sdfgi_shader.debug_probes.initialize(versions, defines);
  6903. sdfgi_shader.debug_probes_shader = sdfgi_shader.debug_probes.version_create();
  6904. {
  6905. RD::PipelineRasterizationState rs;
  6906. rs.cull_mode = RD::POLYGON_CULL_DISABLED;
  6907. RD::PipelineDepthStencilState ds;
  6908. ds.enable_depth_test = true;
  6909. ds.enable_depth_write = true;
  6910. ds.depth_compare_operator = RD::COMPARE_OP_LESS_OR_EQUAL;
  6911. for (int i = 0; i < SDGIShader::PROBE_DEBUG_MAX; i++) {
  6912. RID debug_probes_shader_version = sdfgi_shader.debug_probes.version_get_shader(sdfgi_shader.debug_probes_shader, i);
  6913. sdfgi_shader.debug_probes_pipeline[i].setup(debug_probes_shader_version, RD::RENDER_PRIMITIVE_TRIANGLE_STRIPS, rs, RD::PipelineMultisampleState(), ds, RD::PipelineColorBlendState::create_disabled(), 0);
  6914. }
  6915. }
  6916. }
  6917. default_giprobe_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(GI::GIProbeData) * RenderBuffers::MAX_GIPROBES);
  6918. }
  6919. //cluster setup
  6920. uint32_t uniform_max_size = RD::get_singleton()->limit_get(RD::LIMIT_MAX_UNIFORM_BUFFER_SIZE);
  6921. { //reflections
  6922. uint32_t reflection_buffer_size;
  6923. if (uniform_max_size < 65536) {
  6924. //Yes, you guessed right, ARM again
  6925. reflection_buffer_size = uniform_max_size;
  6926. } else {
  6927. reflection_buffer_size = 65536;
  6928. }
  6929. cluster.max_reflections = reflection_buffer_size / sizeof(Cluster::ReflectionData);
  6930. cluster.reflections = memnew_arr(Cluster::ReflectionData, cluster.max_reflections);
  6931. cluster.reflection_buffer = RD::get_singleton()->storage_buffer_create(reflection_buffer_size);
  6932. }
  6933. { //lights
  6934. cluster.max_lights = MIN(1024 * 1024, uniform_max_size) / sizeof(Cluster::LightData); //1mb of lights
  6935. uint32_t light_buffer_size = cluster.max_lights * sizeof(Cluster::LightData);
  6936. cluster.lights = memnew_arr(Cluster::LightData, cluster.max_lights);
  6937. cluster.light_buffer = RD::get_singleton()->storage_buffer_create(light_buffer_size);
  6938. //defines += "\n#define MAX_LIGHT_DATA_STRUCTS " + itos(cluster.max_lights) + "\n";
  6939. cluster.lights_instances = memnew_arr(RID, cluster.max_lights);
  6940. cluster.lights_shadow_rect_cache = memnew_arr(Rect2i, cluster.max_lights);
  6941. cluster.max_directional_lights = 8;
  6942. uint32_t directional_light_buffer_size = cluster.max_directional_lights * sizeof(Cluster::DirectionalLightData);
  6943. cluster.directional_lights = memnew_arr(Cluster::DirectionalLightData, cluster.max_directional_lights);
  6944. cluster.directional_light_buffer = RD::get_singleton()->uniform_buffer_create(directional_light_buffer_size);
  6945. }
  6946. { //decals
  6947. cluster.max_decals = MIN(1024 * 1024, uniform_max_size) / sizeof(Cluster::DecalData); //1mb of decals
  6948. uint32_t decal_buffer_size = cluster.max_decals * sizeof(Cluster::DecalData);
  6949. cluster.decals = memnew_arr(Cluster::DecalData, cluster.max_decals);
  6950. cluster.decal_buffer = RD::get_singleton()->storage_buffer_create(decal_buffer_size);
  6951. }
  6952. cluster.builder.setup(16, 8, 24);
  6953. if (!low_end) {
  6954. String defines = "\n#define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS " + itos(cluster.max_directional_lights) + "\n";
  6955. Vector<String> volumetric_fog_modes;
  6956. volumetric_fog_modes.push_back("\n#define MODE_DENSITY\n");
  6957. volumetric_fog_modes.push_back("\n#define MODE_DENSITY\n#define ENABLE_SDFGI\n");
  6958. volumetric_fog_modes.push_back("\n#define MODE_FILTER\n");
  6959. volumetric_fog_modes.push_back("\n#define MODE_FOG\n");
  6960. volumetric_fog.shader.initialize(volumetric_fog_modes, defines);
  6961. volumetric_fog.shader_version = volumetric_fog.shader.version_create();
  6962. for (int i = 0; i < VOLUMETRIC_FOG_SHADER_MAX; i++) {
  6963. volumetric_fog.pipelines[i] = RD::get_singleton()->compute_pipeline_create(volumetric_fog.shader.version_get_shader(volumetric_fog.shader_version, i));
  6964. }
  6965. }
  6966. {
  6967. RD::SamplerState sampler;
  6968. sampler.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  6969. sampler.min_filter = RD::SAMPLER_FILTER_NEAREST;
  6970. sampler.enable_compare = true;
  6971. sampler.compare_op = RD::COMPARE_OP_LESS;
  6972. shadow_sampler = RD::get_singleton()->sampler_create(sampler);
  6973. }
  6974. camera_effects_set_dof_blur_bokeh_shape(RS::DOFBokehShape(int(GLOBAL_GET("rendering/quality/depth_of_field/depth_of_field_bokeh_shape"))));
  6975. camera_effects_set_dof_blur_quality(RS::DOFBlurQuality(int(GLOBAL_GET("rendering/quality/depth_of_field/depth_of_field_bokeh_quality"))), GLOBAL_GET("rendering/quality/depth_of_field/depth_of_field_use_jitter"));
  6976. environment_set_ssao_quality(RS::EnvironmentSSAOQuality(int(GLOBAL_GET("rendering/quality/ssao/quality"))), GLOBAL_GET("rendering/quality/ssao/half_size"));
  6977. screen_space_roughness_limiter = GLOBAL_GET("rendering/quality/screen_filters/screen_space_roughness_limiter_enabled");
  6978. screen_space_roughness_limiter_amount = GLOBAL_GET("rendering/quality/screen_filters/screen_space_roughness_limiter_amount");
  6979. screen_space_roughness_limiter_limit = GLOBAL_GET("rendering/quality/screen_filters/screen_space_roughness_limiter_limit");
  6980. glow_bicubic_upscale = int(GLOBAL_GET("rendering/quality/glow/upscale_mode")) > 0;
  6981. glow_high_quality = GLOBAL_GET("rendering/quality/glow/use_high_quality");
  6982. ssr_roughness_quality = RS::EnvironmentSSRRoughnessQuality(int(GLOBAL_GET("rendering/quality/screen_space_reflection/roughness_quality")));
  6983. sss_quality = RS::SubSurfaceScatteringQuality(int(GLOBAL_GET("rendering/quality/subsurface_scattering/subsurface_scattering_quality")));
  6984. sss_scale = GLOBAL_GET("rendering/quality/subsurface_scattering/subsurface_scattering_scale");
  6985. sss_depth_scale = GLOBAL_GET("rendering/quality/subsurface_scattering/subsurface_scattering_depth_scale");
  6986. directional_penumbra_shadow_kernel = memnew_arr(float, 128);
  6987. directional_soft_shadow_kernel = memnew_arr(float, 128);
  6988. penumbra_shadow_kernel = memnew_arr(float, 128);
  6989. soft_shadow_kernel = memnew_arr(float, 128);
  6990. shadows_quality_set(RS::ShadowQuality(int(GLOBAL_GET("rendering/quality/shadows/soft_shadow_quality"))));
  6991. directional_shadow_quality_set(RS::ShadowQuality(int(GLOBAL_GET("rendering/quality/directional_shadow/soft_shadow_quality"))));
  6992. environment_set_volumetric_fog_volume_size(GLOBAL_GET("rendering/volumetric_fog/volume_size"), GLOBAL_GET("rendering/volumetric_fog/volume_depth"));
  6993. environment_set_volumetric_fog_filter_active(GLOBAL_GET("rendering/volumetric_fog/use_filter"));
  6994. environment_set_volumetric_fog_directional_shadow_shrink_size(GLOBAL_GET("rendering/volumetric_fog/directional_shadow_shrink"));
  6995. environment_set_volumetric_fog_positional_shadow_shrink_size(GLOBAL_GET("rendering/volumetric_fog/positional_shadow_shrink"));
  6996. }
  6997. RendererSceneRenderRD::~RendererSceneRenderRD() {
  6998. for (Map<Vector2i, ShadowMap>::Element *E = shadow_maps.front(); E; E = E->next()) {
  6999. RD::get_singleton()->free(E->get().depth);
  7000. }
  7001. for (Map<int, ShadowCubemap>::Element *E = shadow_cubemaps.front(); E; E = E->next()) {
  7002. RD::get_singleton()->free(E->get().cubemap);
  7003. }
  7004. if (sky_scene_state.uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(sky_scene_state.uniform_set)) {
  7005. RD::get_singleton()->free(sky_scene_state.uniform_set);
  7006. }
  7007. if (!low_end) {
  7008. RD::get_singleton()->free(default_giprobe_buffer);
  7009. RD::get_singleton()->free(gi_probe_lights_uniform);
  7010. RD::get_singleton()->free(gi.sdfgi_ubo);
  7011. giprobe_debug_shader.version_free(giprobe_debug_shader_version);
  7012. giprobe_shader.version_free(giprobe_lighting_shader_version);
  7013. gi.shader.version_free(gi.shader_version);
  7014. sdfgi_shader.debug_probes.version_free(sdfgi_shader.debug_probes_shader);
  7015. sdfgi_shader.debug.version_free(sdfgi_shader.debug_shader);
  7016. sdfgi_shader.direct_light.version_free(sdfgi_shader.direct_light_shader);
  7017. sdfgi_shader.integrate.version_free(sdfgi_shader.integrate_shader);
  7018. sdfgi_shader.preprocess.version_free(sdfgi_shader.preprocess_shader);
  7019. volumetric_fog.shader.version_free(volumetric_fog.shader_version);
  7020. memdelete_arr(gi_probe_lights);
  7021. }
  7022. SkyMaterialData *md = (SkyMaterialData *)storage->material_get_data(sky_shader.default_material, RendererStorageRD::SHADER_TYPE_SKY);
  7023. sky_shader.shader.version_free(md->shader_data->version);
  7024. RD::get_singleton()->free(sky_scene_state.directional_light_buffer);
  7025. RD::get_singleton()->free(sky_scene_state.uniform_buffer);
  7026. memdelete_arr(sky_scene_state.directional_lights);
  7027. memdelete_arr(sky_scene_state.last_frame_directional_lights);
  7028. storage->free(sky_shader.default_shader);
  7029. storage->free(sky_shader.default_material);
  7030. storage->free(sky_scene_state.fog_shader);
  7031. storage->free(sky_scene_state.fog_material);
  7032. memdelete_arr(directional_penumbra_shadow_kernel);
  7033. memdelete_arr(directional_soft_shadow_kernel);
  7034. memdelete_arr(penumbra_shadow_kernel);
  7035. memdelete_arr(soft_shadow_kernel);
  7036. {
  7037. RD::get_singleton()->free(cluster.directional_light_buffer);
  7038. RD::get_singleton()->free(cluster.light_buffer);
  7039. RD::get_singleton()->free(cluster.reflection_buffer);
  7040. RD::get_singleton()->free(cluster.decal_buffer);
  7041. memdelete_arr(cluster.directional_lights);
  7042. memdelete_arr(cluster.lights);
  7043. memdelete_arr(cluster.lights_shadow_rect_cache);
  7044. memdelete_arr(cluster.lights_instances);
  7045. memdelete_arr(cluster.reflections);
  7046. memdelete_arr(cluster.decals);
  7047. }
  7048. RD::get_singleton()->free(shadow_sampler);
  7049. directional_shadow_atlas_set_size(0);
  7050. }