renderer_scene_render_rd.cpp 327 KB

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