renderer_scene_render_rd.cpp 331 KB

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