renderer_scene_render_rd.cpp 343 KB

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