renderer_scene_render_rd.cpp 345 KB

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