renderer_scene_render_rd.cpp 330 KB

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