rasterizer_scene_rd.cpp 260 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829
  1. /*************************************************************************/
  2. /* rasterizer_scene_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "rasterizer_scene_rd.h"
  31. #include "core/os/os.h"
  32. #include "core/project_settings.h"
  33. #include "rasterizer_rd.h"
  34. #include "servers/rendering/rendering_server_raster.h"
  35. uint64_t RasterizerSceneRD::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 RasterizerSceneRD::_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 RasterizerSceneRD::_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.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 RasterizerSceneRD::_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 RasterizerSceneRD::_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 RasterizerSceneRD::_update_reflection_mipmaps(ReflectionData &rd) {
  157. if (sky_use_cubemap_array) {
  158. for (int i = 0; i < rd.layers.size(); i++) {
  159. for (int j = 0; j < rd.layers[i].mipmaps.size() - 1; j++) {
  160. for (int k = 0; k < 6; k++) {
  161. RID view = rd.layers[i].mipmaps[j].views[k];
  162. RID texture = rd.layers[i].mipmaps[j + 1].views[k];
  163. Size2i size = rd.layers[i].mipmaps[j + 1].size;
  164. storage->get_effects()->make_mipmap(view, texture, size);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. void RasterizerSceneRD::_sdfgi_erase(RenderBuffers *rb) {
  171. for (uint32_t i = 0; i < rb->sdfgi->cascades.size(); i++) {
  172. const SDFGI::Cascade &c = rb->sdfgi->cascades[i];
  173. RD::get_singleton()->free(c.light_data);
  174. RD::get_singleton()->free(c.light_aniso_0_tex);
  175. RD::get_singleton()->free(c.light_aniso_1_tex);
  176. RD::get_singleton()->free(c.sdf_tex);
  177. RD::get_singleton()->free(c.solid_cell_dispatch_buffer);
  178. RD::get_singleton()->free(c.solid_cell_buffer);
  179. RD::get_singleton()->free(c.lightprobe_history_tex);
  180. RD::get_singleton()->free(c.lightprobe_average_tex);
  181. RD::get_singleton()->free(c.lights_buffer);
  182. }
  183. RD::get_singleton()->free(rb->sdfgi->render_albedo);
  184. RD::get_singleton()->free(rb->sdfgi->render_emission);
  185. RD::get_singleton()->free(rb->sdfgi->render_emission_aniso);
  186. RD::get_singleton()->free(rb->sdfgi->render_sdf[0]);
  187. RD::get_singleton()->free(rb->sdfgi->render_sdf[1]);
  188. RD::get_singleton()->free(rb->sdfgi->render_sdf_half[0]);
  189. RD::get_singleton()->free(rb->sdfgi->render_sdf_half[1]);
  190. for (int i = 0; i < 8; i++) {
  191. RD::get_singleton()->free(rb->sdfgi->render_occlusion[i]);
  192. }
  193. RD::get_singleton()->free(rb->sdfgi->render_geom_facing);
  194. RD::get_singleton()->free(rb->sdfgi->lightprobe_data);
  195. RD::get_singleton()->free(rb->sdfgi->lightprobe_history_scroll);
  196. RD::get_singleton()->free(rb->sdfgi->occlusion_data);
  197. RD::get_singleton()->free(rb->sdfgi->cascades_ubo);
  198. memdelete(rb->sdfgi);
  199. rb->sdfgi = nullptr;
  200. }
  201. const Vector3i RasterizerSceneRD::SDFGI::Cascade::DIRTY_ALL = Vector3i(0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF);
  202. void RasterizerSceneRD::sdfgi_update(RID p_render_buffers, RID p_environment, const Vector3 &p_world_position) {
  203. Environent *env = environment_owner.getornull(p_environment);
  204. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  205. bool needs_sdfgi = env && env->sdfgi_enabled;
  206. if (!needs_sdfgi) {
  207. if (rb->sdfgi != nullptr) {
  208. //erase it
  209. _sdfgi_erase(rb);
  210. _render_buffers_uniform_set_changed(p_render_buffers);
  211. }
  212. return;
  213. }
  214. static const uint32_t history_frames_to_converge[RS::ENV_SDFGI_CONVERGE_MAX] = { 5, 10, 15, 20, 25, 30 };
  215. uint32_t requested_history_size = history_frames_to_converge[sdfgi_frames_to_converge];
  216. 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)) {
  217. //configuration changed, erase
  218. _sdfgi_erase(rb);
  219. }
  220. SDFGI *sdfgi = rb->sdfgi;
  221. if (sdfgi == nullptr) {
  222. //re-create
  223. rb->sdfgi = memnew(SDFGI);
  224. sdfgi = rb->sdfgi;
  225. sdfgi->cascade_mode = env->sdfgi_cascades;
  226. sdfgi->min_cell_size = env->sdfgi_min_cell_size;
  227. sdfgi->uses_occlusion = env->sdfgi_use_occlusion;
  228. sdfgi->y_scale_mode = env->sdfgi_y_scale;
  229. static const float y_scale[3] = { 1.0, 1.5, 2.0 };
  230. sdfgi->y_mult = y_scale[sdfgi->y_scale_mode];
  231. static const int cascasde_size[3] = { 4, 6, 8 };
  232. sdfgi->cascades.resize(cascasde_size[sdfgi->cascade_mode]);
  233. sdfgi->probe_axis_count = SDFGI::PROBE_DIVISOR + 1;
  234. sdfgi->solid_cell_ratio = sdfgi_solid_cell_ratio;
  235. sdfgi->solid_cell_count = uint32_t(float(sdfgi->cascade_size * sdfgi->cascade_size * sdfgi->cascade_size) * sdfgi->solid_cell_ratio);
  236. float base_cell_size = sdfgi->min_cell_size;
  237. RD::TextureFormat tf_sdf;
  238. tf_sdf.format = RD::DATA_FORMAT_R8_UNORM;
  239. tf_sdf.width = sdfgi->cascade_size; // Always 64x64
  240. tf_sdf.height = sdfgi->cascade_size;
  241. tf_sdf.depth = sdfgi->cascade_size;
  242. tf_sdf.type = RD::TEXTURE_TYPE_3D;
  243. 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;
  244. {
  245. RD::TextureFormat tf_render = tf_sdf;
  246. tf_render.format = RD::DATA_FORMAT_R16_UINT;
  247. sdfgi->render_albedo = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  248. tf_render.format = RD::DATA_FORMAT_R32_UINT;
  249. sdfgi->render_emission = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  250. sdfgi->render_emission_aniso = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  251. tf_render.format = RD::DATA_FORMAT_R8_UNORM; //at least its easy to visualize
  252. for (int i = 0; i < 8; i++) {
  253. sdfgi->render_occlusion[i] = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  254. }
  255. tf_render.format = RD::DATA_FORMAT_R32_UINT;
  256. sdfgi->render_geom_facing = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  257. tf_render.format = RD::DATA_FORMAT_R8G8B8A8_UINT;
  258. sdfgi->render_sdf[0] = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  259. sdfgi->render_sdf[1] = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  260. tf_render.width /= 2;
  261. tf_render.height /= 2;
  262. tf_render.depth /= 2;
  263. sdfgi->render_sdf_half[0] = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  264. sdfgi->render_sdf_half[1] = RD::get_singleton()->texture_create(tf_render, RD::TextureView());
  265. }
  266. RD::TextureFormat tf_occlusion = tf_sdf;
  267. tf_occlusion.format = RD::DATA_FORMAT_R16_UINT;
  268. tf_occlusion.shareable_formats.push_back(RD::DATA_FORMAT_R16_UINT);
  269. tf_occlusion.shareable_formats.push_back(RD::DATA_FORMAT_R4G4B4A4_UNORM_PACK16);
  270. tf_occlusion.depth *= sdfgi->cascades.size(); //use depth for occlusion slices
  271. tf_occlusion.width *= 2; //use width for the other half
  272. RD::TextureFormat tf_light = tf_sdf;
  273. tf_light.format = RD::DATA_FORMAT_R32_UINT;
  274. tf_light.shareable_formats.push_back(RD::DATA_FORMAT_R32_UINT);
  275. tf_light.shareable_formats.push_back(RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32);
  276. RD::TextureFormat tf_aniso0 = tf_sdf;
  277. tf_aniso0.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  278. RD::TextureFormat tf_aniso1 = tf_sdf;
  279. tf_aniso1.format = RD::DATA_FORMAT_R8G8_UNORM;
  280. int passes = nearest_shift(sdfgi->cascade_size) - 1;
  281. //store lightprobe SH
  282. RD::TextureFormat tf_probes;
  283. tf_probes.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  284. tf_probes.width = sdfgi->probe_axis_count * sdfgi->probe_axis_count;
  285. tf_probes.height = sdfgi->probe_axis_count * SDFGI::SH_SIZE;
  286. 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;
  287. tf_probes.type = RD::TEXTURE_TYPE_2D_ARRAY;
  288. sdfgi->history_size = requested_history_size;
  289. RD::TextureFormat tf_probe_history = tf_probes;
  290. tf_probe_history.format = RD::DATA_FORMAT_R16G16B16A16_SINT; //signed integer because SH are signed
  291. tf_probe_history.array_layers = sdfgi->history_size;
  292. RD::TextureFormat tf_probe_average = tf_probes;
  293. tf_probe_average.format = RD::DATA_FORMAT_R32G32B32A32_SINT; //signed integer because SH are signed
  294. tf_probe_average.type = RD::TEXTURE_TYPE_2D_ARRAY;
  295. tf_probe_average.array_layers = 1;
  296. sdfgi->lightprobe_history_scroll = RD::get_singleton()->texture_create(tf_probe_history, RD::TextureView());
  297. sdfgi->lightprobe_average_scroll = RD::get_singleton()->texture_create(tf_probe_average, RD::TextureView());
  298. {
  299. //octahedral lightprobes
  300. RD::TextureFormat tf_octprobes = tf_probes;
  301. tf_octprobes.array_layers = sdfgi->cascades.size() * 2;
  302. tf_octprobes.format = RD::DATA_FORMAT_R32_UINT; //pack well with RGBE
  303. tf_octprobes.width = sdfgi->probe_axis_count * sdfgi->probe_axis_count * (SDFGI::LIGHTPROBE_OCT_SIZE + 2);
  304. tf_octprobes.height = sdfgi->probe_axis_count * (SDFGI::LIGHTPROBE_OCT_SIZE + 2);
  305. tf_octprobes.shareable_formats.push_back(RD::DATA_FORMAT_R32_UINT);
  306. tf_octprobes.shareable_formats.push_back(RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32);
  307. //lightprobe texture is an octahedral texture
  308. sdfgi->lightprobe_data = RD::get_singleton()->texture_create(tf_octprobes, RD::TextureView());
  309. RD::TextureView tv;
  310. tv.format_override = RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32;
  311. sdfgi->lightprobe_texture = RD::get_singleton()->texture_create_shared(tv, sdfgi->lightprobe_data);
  312. }
  313. sdfgi->cascades_ubo = RD::get_singleton()->uniform_buffer_create(sizeof(SDFGI::Cascade::UBO) * SDFGI::MAX_CASCADES);
  314. sdfgi->occlusion_data = RD::get_singleton()->texture_create(tf_occlusion, RD::TextureView());
  315. {
  316. RD::TextureView tv;
  317. tv.format_override = RD::DATA_FORMAT_R4G4B4A4_UNORM_PACK16;
  318. sdfgi->occlusion_texture = RD::get_singleton()->texture_create_shared(tv, sdfgi->occlusion_data);
  319. }
  320. for (uint32_t i = 0; i < sdfgi->cascades.size(); i++) {
  321. SDFGI::Cascade &cascade = sdfgi->cascades[i];
  322. /* 3D Textures */
  323. cascade.sdf_tex = RD::get_singleton()->texture_create(tf_sdf, RD::TextureView());
  324. cascade.light_data = RD::get_singleton()->texture_create(tf_light, RD::TextureView());
  325. cascade.light_aniso_0_tex = RD::get_singleton()->texture_create(tf_aniso0, RD::TextureView());
  326. cascade.light_aniso_1_tex = RD::get_singleton()->texture_create(tf_aniso1, RD::TextureView());
  327. {
  328. RD::TextureView tv;
  329. tv.format_override = RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32;
  330. cascade.light_tex = RD::get_singleton()->texture_create_shared(tv, cascade.light_data);
  331. RD::get_singleton()->texture_clear(cascade.light_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
  332. RD::get_singleton()->texture_clear(cascade.light_aniso_0_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
  333. RD::get_singleton()->texture_clear(cascade.light_aniso_1_tex, Color(0, 0, 0, 0), 0, 1, 0, 1);
  334. }
  335. cascade.cell_size = base_cell_size;
  336. Vector3 world_position = p_world_position;
  337. world_position.y *= sdfgi->y_mult;
  338. int32_t probe_cells = sdfgi->cascade_size / SDFGI::PROBE_DIVISOR;
  339. Vector3 probe_size = Vector3(1, 1, 1) * cascade.cell_size * probe_cells;
  340. Vector3i probe_pos = Vector3i((world_position / probe_size + Vector3(0.5, 0.5, 0.5)).floor());
  341. cascade.position = probe_pos * probe_cells;
  342. cascade.dirty_regions = SDFGI::Cascade::DIRTY_ALL;
  343. base_cell_size *= 2.0;
  344. /* Probe History */
  345. cascade.lightprobe_history_tex = RD::get_singleton()->texture_create(tf_probe_history, RD::TextureView());
  346. 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
  347. cascade.lightprobe_average_tex = RD::get_singleton()->texture_create(tf_probe_average, RD::TextureView());
  348. 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
  349. /* Buffers */
  350. cascade.solid_cell_buffer = RD::get_singleton()->storage_buffer_create(sizeof(SDFGI::Cascade::SolidCell) * sdfgi->solid_cell_count);
  351. cascade.solid_cell_dispatch_buffer = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t) * 4, Vector<uint8_t>(), RD::STORAGE_BUFFER_USAGE_DISPATCH_INDIRECT);
  352. cascade.lights_buffer = RD::get_singleton()->storage_buffer_create(sizeof(SDGIShader::Light) * MAX(SDFGI::MAX_STATIC_LIGHTS, SDFGI::MAX_DYNAMIC_LIGHTS));
  353. {
  354. Vector<RD::Uniform> uniforms;
  355. {
  356. RD::Uniform u;
  357. u.type = RD::UNIFORM_TYPE_IMAGE;
  358. u.binding = 1;
  359. 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
  360. uniforms.push_back(u);
  361. }
  362. {
  363. RD::Uniform u;
  364. u.type = RD::UNIFORM_TYPE_IMAGE;
  365. u.binding = 2;
  366. u.ids.push_back(sdfgi->render_albedo);
  367. uniforms.push_back(u);
  368. }
  369. {
  370. RD::Uniform u;
  371. u.type = RD::UNIFORM_TYPE_IMAGE;
  372. u.binding = 3;
  373. for (int j = 0; j < 8; j++) {
  374. u.ids.push_back(sdfgi->render_occlusion[j]);
  375. }
  376. uniforms.push_back(u);
  377. }
  378. {
  379. RD::Uniform u;
  380. u.type = RD::UNIFORM_TYPE_IMAGE;
  381. u.binding = 4;
  382. u.ids.push_back(sdfgi->render_emission);
  383. uniforms.push_back(u);
  384. }
  385. {
  386. RD::Uniform u;
  387. u.type = RD::UNIFORM_TYPE_IMAGE;
  388. u.binding = 5;
  389. u.ids.push_back(sdfgi->render_emission_aniso);
  390. uniforms.push_back(u);
  391. }
  392. {
  393. RD::Uniform u;
  394. u.type = RD::UNIFORM_TYPE_IMAGE;
  395. u.binding = 6;
  396. u.ids.push_back(sdfgi->render_geom_facing);
  397. uniforms.push_back(u);
  398. }
  399. {
  400. RD::Uniform u;
  401. u.type = RD::UNIFORM_TYPE_IMAGE;
  402. u.binding = 7;
  403. u.ids.push_back(cascade.sdf_tex);
  404. uniforms.push_back(u);
  405. }
  406. {
  407. RD::Uniform u;
  408. u.type = RD::UNIFORM_TYPE_IMAGE;
  409. u.binding = 8;
  410. u.ids.push_back(sdfgi->occlusion_data);
  411. uniforms.push_back(u);
  412. }
  413. {
  414. RD::Uniform u;
  415. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  416. u.binding = 10;
  417. u.ids.push_back(cascade.solid_cell_dispatch_buffer);
  418. uniforms.push_back(u);
  419. }
  420. {
  421. RD::Uniform u;
  422. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  423. u.binding = 11;
  424. u.ids.push_back(cascade.solid_cell_buffer);
  425. uniforms.push_back(u);
  426. }
  427. 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);
  428. }
  429. {
  430. Vector<RD::Uniform> uniforms;
  431. {
  432. RD::Uniform u;
  433. u.type = RD::UNIFORM_TYPE_IMAGE;
  434. u.binding = 1;
  435. u.ids.push_back(sdfgi->render_albedo);
  436. uniforms.push_back(u);
  437. }
  438. {
  439. RD::Uniform u;
  440. u.type = RD::UNIFORM_TYPE_IMAGE;
  441. u.binding = 2;
  442. u.ids.push_back(sdfgi->render_geom_facing);
  443. uniforms.push_back(u);
  444. }
  445. {
  446. RD::Uniform u;
  447. u.type = RD::UNIFORM_TYPE_IMAGE;
  448. u.binding = 3;
  449. u.ids.push_back(sdfgi->render_emission);
  450. uniforms.push_back(u);
  451. }
  452. {
  453. RD::Uniform u;
  454. u.type = RD::UNIFORM_TYPE_IMAGE;
  455. u.binding = 4;
  456. u.ids.push_back(sdfgi->render_emission_aniso);
  457. uniforms.push_back(u);
  458. }
  459. {
  460. RD::Uniform u;
  461. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  462. u.binding = 5;
  463. u.ids.push_back(cascade.solid_cell_dispatch_buffer);
  464. uniforms.push_back(u);
  465. }
  466. {
  467. RD::Uniform u;
  468. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  469. u.binding = 6;
  470. u.ids.push_back(cascade.solid_cell_buffer);
  471. uniforms.push_back(u);
  472. }
  473. 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);
  474. }
  475. {
  476. Vector<RD::Uniform> uniforms;
  477. {
  478. RD::Uniform u;
  479. u.type = RD::UNIFORM_TYPE_IMAGE;
  480. u.binding = 1;
  481. for (int j = 0; j < 8; j++) {
  482. u.ids.push_back(sdfgi->render_occlusion[j]);
  483. }
  484. uniforms.push_back(u);
  485. }
  486. {
  487. RD::Uniform u;
  488. u.type = RD::UNIFORM_TYPE_IMAGE;
  489. u.binding = 2;
  490. u.ids.push_back(sdfgi->occlusion_data);
  491. uniforms.push_back(u);
  492. }
  493. 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);
  494. }
  495. }
  496. //direct light
  497. for (uint32_t i = 0; i < sdfgi->cascades.size(); i++) {
  498. SDFGI::Cascade &cascade = sdfgi->cascades[i];
  499. Vector<RD::Uniform> uniforms;
  500. {
  501. RD::Uniform u;
  502. u.binding = 1;
  503. u.type = RD::UNIFORM_TYPE_TEXTURE;
  504. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  505. if (j < rb->sdfgi->cascades.size()) {
  506. u.ids.push_back(rb->sdfgi->cascades[j].sdf_tex);
  507. } else {
  508. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  509. }
  510. }
  511. uniforms.push_back(u);
  512. }
  513. {
  514. RD::Uniform u;
  515. u.binding = 2;
  516. u.type = RD::UNIFORM_TYPE_SAMPLER;
  517. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  518. uniforms.push_back(u);
  519. }
  520. {
  521. RD::Uniform u;
  522. u.binding = 3;
  523. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  524. u.ids.push_back(cascade.solid_cell_dispatch_buffer);
  525. uniforms.push_back(u);
  526. }
  527. {
  528. RD::Uniform u;
  529. u.binding = 4;
  530. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  531. u.ids.push_back(cascade.solid_cell_buffer);
  532. uniforms.push_back(u);
  533. }
  534. {
  535. RD::Uniform u;
  536. u.binding = 5;
  537. u.type = RD::UNIFORM_TYPE_IMAGE;
  538. u.ids.push_back(cascade.light_data);
  539. uniforms.push_back(u);
  540. }
  541. {
  542. RD::Uniform u;
  543. u.binding = 6;
  544. u.type = RD::UNIFORM_TYPE_IMAGE;
  545. u.ids.push_back(cascade.light_aniso_0_tex);
  546. uniforms.push_back(u);
  547. }
  548. {
  549. RD::Uniform u;
  550. u.binding = 7;
  551. u.type = RD::UNIFORM_TYPE_IMAGE;
  552. u.ids.push_back(cascade.light_aniso_1_tex);
  553. uniforms.push_back(u);
  554. }
  555. {
  556. RD::Uniform u;
  557. u.binding = 8;
  558. u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  559. u.ids.push_back(rb->sdfgi->cascades_ubo);
  560. uniforms.push_back(u);
  561. }
  562. {
  563. RD::Uniform u;
  564. u.binding = 9;
  565. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  566. u.ids.push_back(cascade.lights_buffer);
  567. uniforms.push_back(u);
  568. }
  569. {
  570. RD::Uniform u;
  571. u.binding = 10;
  572. u.type = RD::UNIFORM_TYPE_TEXTURE;
  573. u.ids.push_back(rb->sdfgi->lightprobe_texture);
  574. uniforms.push_back(u);
  575. }
  576. 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);
  577. }
  578. //preprocess initialize uniform set
  579. {
  580. Vector<RD::Uniform> uniforms;
  581. {
  582. RD::Uniform u;
  583. u.type = RD::UNIFORM_TYPE_IMAGE;
  584. u.binding = 1;
  585. u.ids.push_back(sdfgi->render_albedo);
  586. uniforms.push_back(u);
  587. }
  588. {
  589. RD::Uniform u;
  590. u.type = RD::UNIFORM_TYPE_IMAGE;
  591. u.binding = 2;
  592. u.ids.push_back(sdfgi->render_sdf[0]);
  593. uniforms.push_back(u);
  594. }
  595. 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);
  596. }
  597. {
  598. Vector<RD::Uniform> uniforms;
  599. {
  600. RD::Uniform u;
  601. u.type = RD::UNIFORM_TYPE_IMAGE;
  602. u.binding = 1;
  603. u.ids.push_back(sdfgi->render_albedo);
  604. uniforms.push_back(u);
  605. }
  606. {
  607. RD::Uniform u;
  608. u.type = RD::UNIFORM_TYPE_IMAGE;
  609. u.binding = 2;
  610. u.ids.push_back(sdfgi->render_sdf_half[0]);
  611. uniforms.push_back(u);
  612. }
  613. 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);
  614. }
  615. //jump flood uniform set
  616. {
  617. Vector<RD::Uniform> uniforms;
  618. {
  619. RD::Uniform u;
  620. u.type = RD::UNIFORM_TYPE_IMAGE;
  621. u.binding = 1;
  622. u.ids.push_back(sdfgi->render_sdf[0]);
  623. uniforms.push_back(u);
  624. }
  625. {
  626. RD::Uniform u;
  627. u.type = RD::UNIFORM_TYPE_IMAGE;
  628. u.binding = 2;
  629. u.ids.push_back(sdfgi->render_sdf[1]);
  630. uniforms.push_back(u);
  631. }
  632. 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);
  633. SWAP(uniforms.write[0].ids.write[0], uniforms.write[1].ids.write[0]);
  634. 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);
  635. }
  636. //jump flood half uniform set
  637. {
  638. Vector<RD::Uniform> uniforms;
  639. {
  640. RD::Uniform u;
  641. u.type = RD::UNIFORM_TYPE_IMAGE;
  642. u.binding = 1;
  643. u.ids.push_back(sdfgi->render_sdf_half[0]);
  644. uniforms.push_back(u);
  645. }
  646. {
  647. RD::Uniform u;
  648. u.type = RD::UNIFORM_TYPE_IMAGE;
  649. u.binding = 2;
  650. u.ids.push_back(sdfgi->render_sdf_half[1]);
  651. uniforms.push_back(u);
  652. }
  653. 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);
  654. SWAP(uniforms.write[0].ids.write[0], uniforms.write[1].ids.write[0]);
  655. 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);
  656. }
  657. //upscale half size sdf
  658. {
  659. Vector<RD::Uniform> uniforms;
  660. {
  661. RD::Uniform u;
  662. u.type = RD::UNIFORM_TYPE_IMAGE;
  663. u.binding = 1;
  664. u.ids.push_back(sdfgi->render_albedo);
  665. uniforms.push_back(u);
  666. }
  667. {
  668. RD::Uniform u;
  669. u.type = RD::UNIFORM_TYPE_IMAGE;
  670. u.binding = 2;
  671. u.ids.push_back(sdfgi->render_sdf_half[(passes & 1) ? 0 : 1]); //reverse pass order because half size
  672. uniforms.push_back(u);
  673. }
  674. {
  675. RD::Uniform u;
  676. u.type = RD::UNIFORM_TYPE_IMAGE;
  677. u.binding = 3;
  678. u.ids.push_back(sdfgi->render_sdf[(passes & 1) ? 0 : 1]); //reverse pass order because it needs an extra JFA pass
  679. uniforms.push_back(u);
  680. }
  681. sdfgi->upscale_jfa_uniform_set_index = (passes & 1) ? 0 : 1;
  682. 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);
  683. }
  684. //occlusion uniform set
  685. {
  686. Vector<RD::Uniform> uniforms;
  687. {
  688. RD::Uniform u;
  689. u.type = RD::UNIFORM_TYPE_IMAGE;
  690. u.binding = 1;
  691. u.ids.push_back(sdfgi->render_albedo);
  692. uniforms.push_back(u);
  693. }
  694. {
  695. RD::Uniform u;
  696. u.type = RD::UNIFORM_TYPE_IMAGE;
  697. u.binding = 2;
  698. for (int i = 0; i < 8; i++) {
  699. u.ids.push_back(sdfgi->render_occlusion[i]);
  700. }
  701. uniforms.push_back(u);
  702. }
  703. {
  704. RD::Uniform u;
  705. u.type = RD::UNIFORM_TYPE_IMAGE;
  706. u.binding = 3;
  707. u.ids.push_back(sdfgi->render_geom_facing);
  708. uniforms.push_back(u);
  709. }
  710. 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);
  711. }
  712. for (uint32_t i = 0; i < sdfgi->cascades.size(); i++) {
  713. //integrate uniform
  714. Vector<RD::Uniform> uniforms;
  715. {
  716. RD::Uniform u;
  717. u.binding = 1;
  718. u.type = RD::UNIFORM_TYPE_TEXTURE;
  719. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  720. if (j < sdfgi->cascades.size()) {
  721. u.ids.push_back(sdfgi->cascades[j].sdf_tex);
  722. } else {
  723. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  724. }
  725. }
  726. uniforms.push_back(u);
  727. }
  728. {
  729. RD::Uniform u;
  730. u.binding = 2;
  731. u.type = RD::UNIFORM_TYPE_TEXTURE;
  732. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  733. if (j < sdfgi->cascades.size()) {
  734. u.ids.push_back(sdfgi->cascades[j].light_tex);
  735. } else {
  736. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  737. }
  738. }
  739. uniforms.push_back(u);
  740. }
  741. {
  742. RD::Uniform u;
  743. u.binding = 3;
  744. u.type = RD::UNIFORM_TYPE_TEXTURE;
  745. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  746. if (j < sdfgi->cascades.size()) {
  747. u.ids.push_back(sdfgi->cascades[j].light_aniso_0_tex);
  748. } else {
  749. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  750. }
  751. }
  752. uniforms.push_back(u);
  753. }
  754. {
  755. RD::Uniform u;
  756. u.binding = 4;
  757. u.type = RD::UNIFORM_TYPE_TEXTURE;
  758. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  759. if (j < sdfgi->cascades.size()) {
  760. u.ids.push_back(sdfgi->cascades[j].light_aniso_1_tex);
  761. } else {
  762. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  763. }
  764. }
  765. uniforms.push_back(u);
  766. }
  767. {
  768. RD::Uniform u;
  769. u.type = RD::UNIFORM_TYPE_SAMPLER;
  770. u.binding = 6;
  771. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  772. uniforms.push_back(u);
  773. }
  774. {
  775. RD::Uniform u;
  776. u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  777. u.binding = 7;
  778. u.ids.push_back(sdfgi->cascades_ubo);
  779. uniforms.push_back(u);
  780. }
  781. {
  782. RD::Uniform u;
  783. u.type = RD::UNIFORM_TYPE_IMAGE;
  784. u.binding = 8;
  785. u.ids.push_back(sdfgi->lightprobe_data);
  786. uniforms.push_back(u);
  787. }
  788. {
  789. RD::Uniform u;
  790. u.type = RD::UNIFORM_TYPE_IMAGE;
  791. u.binding = 9;
  792. u.ids.push_back(sdfgi->cascades[i].lightprobe_history_tex);
  793. uniforms.push_back(u);
  794. }
  795. {
  796. RD::Uniform u;
  797. u.type = RD::UNIFORM_TYPE_IMAGE;
  798. u.binding = 10;
  799. u.ids.push_back(sdfgi->cascades[i].lightprobe_average_tex);
  800. uniforms.push_back(u);
  801. }
  802. {
  803. RD::Uniform u;
  804. u.type = RD::UNIFORM_TYPE_IMAGE;
  805. u.binding = 11;
  806. u.ids.push_back(sdfgi->lightprobe_history_scroll);
  807. uniforms.push_back(u);
  808. }
  809. {
  810. RD::Uniform u;
  811. u.type = RD::UNIFORM_TYPE_IMAGE;
  812. u.binding = 12;
  813. u.ids.push_back(sdfgi->lightprobe_average_scroll);
  814. uniforms.push_back(u);
  815. }
  816. {
  817. RD::Uniform u;
  818. u.type = RD::UNIFORM_TYPE_IMAGE;
  819. u.binding = 13;
  820. RID parent_average;
  821. if (i < sdfgi->cascades.size() - 1) {
  822. parent_average = sdfgi->cascades[i + 1].lightprobe_average_tex;
  823. } else {
  824. parent_average = sdfgi->cascades[i - 1].lightprobe_average_tex; //to use something, but it wont be used
  825. }
  826. u.ids.push_back(parent_average);
  827. uniforms.push_back(u);
  828. }
  829. 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);
  830. }
  831. sdfgi->uses_multibounce = env->sdfgi_use_multibounce;
  832. sdfgi->energy = env->sdfgi_energy;
  833. sdfgi->normal_bias = env->sdfgi_normal_bias;
  834. sdfgi->probe_bias = env->sdfgi_probe_bias;
  835. sdfgi->reads_sky = env->sdfgi_read_sky_light;
  836. _render_buffers_uniform_set_changed(p_render_buffers);
  837. return; //done. all levels will need to be rendered which its going to take a bit
  838. }
  839. //check for updates
  840. sdfgi->uses_multibounce = env->sdfgi_use_multibounce;
  841. sdfgi->energy = env->sdfgi_energy;
  842. sdfgi->normal_bias = env->sdfgi_normal_bias;
  843. sdfgi->probe_bias = env->sdfgi_probe_bias;
  844. sdfgi->reads_sky = env->sdfgi_read_sky_light;
  845. int32_t drag_margin = (sdfgi->cascade_size / SDFGI::PROBE_DIVISOR) / 2;
  846. for (uint32_t i = 0; i < sdfgi->cascades.size(); i++) {
  847. SDFGI::Cascade &cascade = sdfgi->cascades[i];
  848. cascade.dirty_regions = Vector3i();
  849. Vector3 probe_half_size = Vector3(1, 1, 1) * cascade.cell_size * float(sdfgi->cascade_size / SDFGI::PROBE_DIVISOR) * 0.5;
  850. probe_half_size = Vector3(0, 0, 0);
  851. Vector3 world_position = p_world_position;
  852. world_position.y *= sdfgi->y_mult;
  853. Vector3i pos_in_cascade = Vector3i((world_position + probe_half_size) / cascade.cell_size);
  854. for (int j = 0; j < 3; j++) {
  855. if (pos_in_cascade[j] < cascade.position[j]) {
  856. while (pos_in_cascade[j] < (cascade.position[j] - drag_margin)) {
  857. cascade.position[j] -= drag_margin * 2;
  858. cascade.dirty_regions[j] += drag_margin * 2;
  859. }
  860. } else if (pos_in_cascade[j] > cascade.position[j]) {
  861. while (pos_in_cascade[j] > (cascade.position[j] + drag_margin)) {
  862. cascade.position[j] += drag_margin * 2;
  863. cascade.dirty_regions[j] -= drag_margin * 2;
  864. }
  865. }
  866. if (cascade.dirty_regions[j] == 0) {
  867. continue; // not dirty
  868. } else if (uint32_t(ABS(cascade.dirty_regions[j])) >= sdfgi->cascade_size) {
  869. //moved too much, just redraw everything (make all dirty)
  870. cascade.dirty_regions = SDFGI::Cascade::DIRTY_ALL;
  871. break;
  872. }
  873. }
  874. if (cascade.dirty_regions != Vector3i() && cascade.dirty_regions != SDFGI::Cascade::DIRTY_ALL) {
  875. //see how much the total dirty volume represents from the total volume
  876. uint32_t total_volume = sdfgi->cascade_size * sdfgi->cascade_size * sdfgi->cascade_size;
  877. uint32_t safe_volume = 1;
  878. for (int j = 0; j < 3; j++) {
  879. safe_volume *= sdfgi->cascade_size - ABS(cascade.dirty_regions[j]);
  880. }
  881. uint32_t dirty_volume = total_volume - safe_volume;
  882. if (dirty_volume > (safe_volume / 2)) {
  883. //more than half the volume is dirty, make all dirty so its only rendered once
  884. cascade.dirty_regions = SDFGI::Cascade::DIRTY_ALL;
  885. }
  886. }
  887. }
  888. }
  889. int RasterizerSceneRD::sdfgi_get_pending_region_count(RID p_render_buffers) const {
  890. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  891. ERR_FAIL_COND_V(rb == nullptr, 0);
  892. if (rb->sdfgi == nullptr) {
  893. return 0;
  894. }
  895. int dirty_count = 0;
  896. for (uint32_t i = 0; i < rb->sdfgi->cascades.size(); i++) {
  897. const SDFGI::Cascade &c = rb->sdfgi->cascades[i];
  898. if (c.dirty_regions == SDFGI::Cascade::DIRTY_ALL) {
  899. dirty_count++;
  900. } else {
  901. for (int j = 0; j < 3; j++) {
  902. if (c.dirty_regions[j] != 0) {
  903. dirty_count++;
  904. }
  905. }
  906. }
  907. }
  908. return dirty_count;
  909. }
  910. int RasterizerSceneRD::_sdfgi_get_pending_region_data(RID p_render_buffers, int p_region, Vector3i &r_local_offset, Vector3i &r_local_size, AABB &r_bounds) const {
  911. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  912. ERR_FAIL_COND_V(rb == nullptr, -1);
  913. ERR_FAIL_COND_V(rb->sdfgi == nullptr, -1);
  914. int dirty_count = 0;
  915. for (uint32_t i = 0; i < rb->sdfgi->cascades.size(); i++) {
  916. const SDFGI::Cascade &c = rb->sdfgi->cascades[i];
  917. if (c.dirty_regions == SDFGI::Cascade::DIRTY_ALL) {
  918. if (dirty_count == p_region) {
  919. r_local_offset = Vector3i();
  920. r_local_size = Vector3i(1, 1, 1) * rb->sdfgi->cascade_size;
  921. 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);
  922. r_bounds.size = Vector3(r_local_size) * c.cell_size * Vector3(1, 1.0 / rb->sdfgi->y_mult, 1);
  923. return i;
  924. }
  925. dirty_count++;
  926. } else {
  927. for (int j = 0; j < 3; j++) {
  928. if (c.dirty_regions[j] != 0) {
  929. if (dirty_count == p_region) {
  930. Vector3i from = Vector3i(0, 0, 0);
  931. Vector3i to = Vector3i(1, 1, 1) * rb->sdfgi->cascade_size;
  932. if (c.dirty_regions[j] > 0) {
  933. //fill from the beginning
  934. to[j] = c.dirty_regions[j];
  935. } else {
  936. //fill from the end
  937. from[j] = to[j] + c.dirty_regions[j];
  938. }
  939. for (int k = 0; k < j; k++) {
  940. // "chip" away previous regions to avoid re-voxelizing the same thing
  941. if (c.dirty_regions[k] > 0) {
  942. from[k] += c.dirty_regions[k];
  943. } else if (c.dirty_regions[k] < 0) {
  944. to[k] += c.dirty_regions[k];
  945. }
  946. }
  947. r_local_offset = from;
  948. r_local_size = to - from;
  949. 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);
  950. r_bounds.size = Vector3(r_local_size) * c.cell_size * Vector3(1, 1.0 / rb->sdfgi->y_mult, 1);
  951. return i;
  952. }
  953. dirty_count++;
  954. }
  955. }
  956. }
  957. }
  958. return -1;
  959. }
  960. AABB RasterizerSceneRD::sdfgi_get_pending_region_bounds(RID p_render_buffers, int p_region) const {
  961. AABB bounds;
  962. Vector3i from;
  963. Vector3i size;
  964. int c = _sdfgi_get_pending_region_data(p_render_buffers, p_region, from, size, bounds);
  965. ERR_FAIL_COND_V(c == -1, AABB());
  966. return bounds;
  967. }
  968. uint32_t RasterizerSceneRD::sdfgi_get_pending_region_cascade(RID p_render_buffers, int p_region) const {
  969. AABB bounds;
  970. Vector3i from;
  971. Vector3i size;
  972. return _sdfgi_get_pending_region_data(p_render_buffers, p_region, from, size, bounds);
  973. }
  974. void RasterizerSceneRD::_sdfgi_update_cascades(RID p_render_buffers) {
  975. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  976. ERR_FAIL_COND(rb == nullptr);
  977. if (rb->sdfgi == nullptr) {
  978. return;
  979. }
  980. //update cascades
  981. SDFGI::Cascade::UBO cascade_data[SDFGI::MAX_CASCADES];
  982. int32_t probe_divisor = rb->sdfgi->cascade_size / SDFGI::PROBE_DIVISOR;
  983. for (uint32_t i = 0; i < rb->sdfgi->cascades.size(); i++) {
  984. 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;
  985. cascade_data[i].offset[0] = pos.x;
  986. cascade_data[i].offset[1] = pos.y;
  987. cascade_data[i].offset[2] = pos.z;
  988. cascade_data[i].to_cell = 1.0 / rb->sdfgi->cascades[i].cell_size;
  989. cascade_data[i].probe_offset[0] = rb->sdfgi->cascades[i].position.x / probe_divisor;
  990. cascade_data[i].probe_offset[1] = rb->sdfgi->cascades[i].position.y / probe_divisor;
  991. cascade_data[i].probe_offset[2] = rb->sdfgi->cascades[i].position.z / probe_divisor;
  992. cascade_data[i].pad = 0;
  993. }
  994. RD::get_singleton()->buffer_update(rb->sdfgi->cascades_ubo, 0, sizeof(SDFGI::Cascade::UBO) * SDFGI::MAX_CASCADES, cascade_data, true);
  995. }
  996. void RasterizerSceneRD::sdfgi_update_probes(RID p_render_buffers, RID p_environment, const RID *p_directional_light_instances, uint32_t p_directional_light_count, const RID *p_positional_light_instances, uint32_t p_positional_light_count) {
  997. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  998. ERR_FAIL_COND(rb == nullptr);
  999. if (rb->sdfgi == nullptr) {
  1000. return;
  1001. }
  1002. Environent *env = environment_owner.getornull(p_environment);
  1003. RENDER_TIMESTAMP(">SDFGI Update Probes");
  1004. /* Update Cascades UBO */
  1005. _sdfgi_update_cascades(p_render_buffers);
  1006. /* Update Dynamic Lights Buffer */
  1007. RENDER_TIMESTAMP("Update Lights");
  1008. /* Update dynamic lights */
  1009. {
  1010. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1011. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.direct_light_pipeline[SDGIShader::DIRECT_LIGHT_MODE_DYNAMIC]);
  1012. SDGIShader::DirectLightPushConstant push_constant;
  1013. push_constant.grid_size[0] = rb->sdfgi->cascade_size;
  1014. push_constant.grid_size[1] = rb->sdfgi->cascade_size;
  1015. push_constant.grid_size[2] = rb->sdfgi->cascade_size;
  1016. push_constant.max_cascades = rb->sdfgi->cascades.size();
  1017. push_constant.probe_axis_size = rb->sdfgi->probe_axis_count;
  1018. push_constant.multibounce = rb->sdfgi->uses_multibounce;
  1019. push_constant.y_mult = rb->sdfgi->y_mult;
  1020. push_constant.process_offset = 0;
  1021. push_constant.process_increment = 1;
  1022. for (uint32_t i = 0; i < rb->sdfgi->cascades.size(); i++) {
  1023. SDFGI::Cascade &cascade = rb->sdfgi->cascades[i];
  1024. { //fill light buffer
  1025. SDGIShader::Light lights[SDFGI::MAX_DYNAMIC_LIGHTS];
  1026. uint32_t idx = 0;
  1027. for (uint32_t j = 0; j < p_directional_light_count; j++) {
  1028. if (idx == SDFGI::MAX_DYNAMIC_LIGHTS) {
  1029. break;
  1030. }
  1031. LightInstance *li = light_instance_owner.getornull(p_directional_light_instances[j]);
  1032. ERR_CONTINUE(!li);
  1033. Vector3 dir = -li->transform.basis.get_axis(Vector3::AXIS_Z);
  1034. dir.y *= rb->sdfgi->y_mult;
  1035. dir.normalize();
  1036. lights[idx].direction[0] = dir.x;
  1037. lights[idx].direction[1] = dir.y;
  1038. lights[idx].direction[2] = dir.z;
  1039. Color color = storage->light_get_color(li->light);
  1040. color = color.to_linear();
  1041. lights[idx].color[0] = color.r;
  1042. lights[idx].color[1] = color.g;
  1043. lights[idx].color[2] = color.b;
  1044. lights[idx].type = RS::LIGHT_DIRECTIONAL;
  1045. lights[idx].energy = storage->light_get_param(li->light, RS::LIGHT_PARAM_ENERGY);
  1046. lights[idx].has_shadow = storage->light_has_shadow(li->light);
  1047. idx++;
  1048. }
  1049. AABB cascade_aabb;
  1050. cascade_aabb.position = Vector3((Vector3i(1, 1, 1) * -int32_t(rb->sdfgi->cascade_size >> 1) + cascade.position)) * cascade.cell_size;
  1051. cascade_aabb.size = Vector3(1, 1, 1) * rb->sdfgi->cascade_size * cascade.cell_size;
  1052. for (uint32_t j = 0; j < p_positional_light_count; j++) {
  1053. if (idx == SDFGI::MAX_DYNAMIC_LIGHTS) {
  1054. break;
  1055. }
  1056. LightInstance *li = light_instance_owner.getornull(p_positional_light_instances[j]);
  1057. ERR_CONTINUE(!li);
  1058. uint32_t max_sdfgi_cascade = storage->light_get_max_sdfgi_cascade(li->light);
  1059. if (i > max_sdfgi_cascade) {
  1060. continue;
  1061. }
  1062. if (!cascade_aabb.intersects(li->aabb)) {
  1063. continue;
  1064. }
  1065. Vector3 dir = -li->transform.basis.get_axis(Vector3::AXIS_Z);
  1066. //faster to not do this here
  1067. //dir.y *= rb->sdfgi->y_mult;
  1068. //dir.normalize();
  1069. lights[idx].direction[0] = dir.x;
  1070. lights[idx].direction[1] = dir.y;
  1071. lights[idx].direction[2] = dir.z;
  1072. Vector3 pos = li->transform.origin;
  1073. pos.y *= rb->sdfgi->y_mult;
  1074. lights[idx].position[0] = pos.x;
  1075. lights[idx].position[1] = pos.y;
  1076. lights[idx].position[2] = pos.z;
  1077. Color color = storage->light_get_color(li->light);
  1078. color = color.to_linear();
  1079. lights[idx].color[0] = color.r;
  1080. lights[idx].color[1] = color.g;
  1081. lights[idx].color[2] = color.b;
  1082. lights[idx].type = storage->light_get_type(li->light);
  1083. lights[idx].energy = storage->light_get_param(li->light, RS::LIGHT_PARAM_ENERGY);
  1084. lights[idx].has_shadow = storage->light_has_shadow(li->light);
  1085. lights[idx].attenuation = storage->light_get_param(li->light, RS::LIGHT_PARAM_ATTENUATION);
  1086. lights[idx].radius = storage->light_get_param(li->light, RS::LIGHT_PARAM_RANGE);
  1087. lights[idx].spot_angle = Math::deg2rad(storage->light_get_param(li->light, RS::LIGHT_PARAM_SPOT_ANGLE));
  1088. lights[idx].spot_attenuation = storage->light_get_param(li->light, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  1089. idx++;
  1090. }
  1091. if (idx > 0) {
  1092. RD::get_singleton()->buffer_update(cascade.lights_buffer, 0, idx * sizeof(SDGIShader::Light), lights, true);
  1093. }
  1094. push_constant.light_count = idx;
  1095. }
  1096. push_constant.cascade = i;
  1097. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cascade.sdf_direct_light_uniform_set, 0);
  1098. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::DirectLightPushConstant));
  1099. RD::get_singleton()->compute_list_dispatch_indirect(compute_list, cascade.solid_cell_dispatch_buffer, 0);
  1100. }
  1101. RD::get_singleton()->compute_list_end();
  1102. }
  1103. RENDER_TIMESTAMP("Raytrace");
  1104. SDGIShader::IntegratePushConstant push_constant;
  1105. push_constant.grid_size[1] = rb->sdfgi->cascade_size;
  1106. push_constant.grid_size[2] = rb->sdfgi->cascade_size;
  1107. push_constant.grid_size[0] = rb->sdfgi->cascade_size;
  1108. push_constant.max_cascades = rb->sdfgi->cascades.size();
  1109. push_constant.probe_axis_size = rb->sdfgi->probe_axis_count;
  1110. push_constant.history_index = rb->sdfgi->render_pass % rb->sdfgi->history_size;
  1111. push_constant.history_size = rb->sdfgi->history_size;
  1112. static const uint32_t ray_count[RS::ENV_SDFGI_RAY_COUNT_MAX] = { 8, 16, 32, 64, 96, 128 };
  1113. push_constant.ray_count = ray_count[sdfgi_ray_count];
  1114. push_constant.ray_bias = rb->sdfgi->probe_bias;
  1115. push_constant.image_size[0] = rb->sdfgi->probe_axis_count * rb->sdfgi->probe_axis_count;
  1116. push_constant.image_size[1] = rb->sdfgi->probe_axis_count;
  1117. RID sky_uniform_set = sdfgi_shader.integrate_default_sky_uniform_set;
  1118. push_constant.sky_mode = SDGIShader::IntegratePushConstant::SKY_MODE_DISABLED;
  1119. push_constant.y_mult = rb->sdfgi->y_mult;
  1120. if (rb->sdfgi->reads_sky && env) {
  1121. push_constant.sky_energy = env->bg_energy;
  1122. if (env->background == RS::ENV_BG_CLEAR_COLOR) {
  1123. push_constant.sky_mode = SDGIShader::IntegratePushConstant::SKY_MODE_COLOR;
  1124. Color c = storage->get_default_clear_color().to_linear();
  1125. push_constant.sky_color[0] = c.r;
  1126. push_constant.sky_color[1] = c.g;
  1127. push_constant.sky_color[2] = c.b;
  1128. } else if (env->background == RS::ENV_BG_COLOR) {
  1129. push_constant.sky_mode = SDGIShader::IntegratePushConstant::SKY_MODE_COLOR;
  1130. Color c = env->bg_color;
  1131. push_constant.sky_color[0] = c.r;
  1132. push_constant.sky_color[1] = c.g;
  1133. push_constant.sky_color[2] = c.b;
  1134. } else if (env->background == RS::ENV_BG_SKY) {
  1135. Sky *sky = sky_owner.getornull(env->sky);
  1136. if (sky && sky->radiance.is_valid()) {
  1137. if (sky->sdfgi_integrate_sky_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(sky->sdfgi_integrate_sky_uniform_set)) {
  1138. Vector<RD::Uniform> uniforms;
  1139. {
  1140. RD::Uniform u;
  1141. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1142. u.binding = 0;
  1143. u.ids.push_back(sky->radiance);
  1144. uniforms.push_back(u);
  1145. }
  1146. {
  1147. RD::Uniform u;
  1148. u.type = RD::UNIFORM_TYPE_SAMPLER;
  1149. u.binding = 1;
  1150. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1151. uniforms.push_back(u);
  1152. }
  1153. 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);
  1154. }
  1155. sky_uniform_set = sky->sdfgi_integrate_sky_uniform_set;
  1156. push_constant.sky_mode = SDGIShader::IntegratePushConstant::SKY_MODE_SKY;
  1157. }
  1158. }
  1159. }
  1160. rb->sdfgi->render_pass++;
  1161. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1162. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.integrate_pipeline[SDGIShader::INTEGRATE_MODE_PROCESS]);
  1163. int32_t probe_divisor = rb->sdfgi->cascade_size / SDFGI::PROBE_DIVISOR;
  1164. for (uint32_t i = 0; i < rb->sdfgi->cascades.size(); i++) {
  1165. push_constant.cascade = i;
  1166. push_constant.world_offset[0] = rb->sdfgi->cascades[i].position.x / probe_divisor;
  1167. push_constant.world_offset[1] = rb->sdfgi->cascades[i].position.y / probe_divisor;
  1168. push_constant.world_offset[2] = rb->sdfgi->cascades[i].position.z / probe_divisor;
  1169. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->cascades[i].integrate_uniform_set, 0);
  1170. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sky_uniform_set, 1);
  1171. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::IntegratePushConstant));
  1172. 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);
  1173. }
  1174. RD::get_singleton()->compute_list_add_barrier(compute_list); //wait until done
  1175. // Then store values into the lightprobe texture. Separating these steps has a small performance hit, but it allows for multiple bounces
  1176. RENDER_TIMESTAMP("Average Probes");
  1177. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.integrate_pipeline[SDGIShader::INTEGRATE_MODE_STORE]);
  1178. //convert to octahedral to store
  1179. push_constant.image_size[0] *= SDFGI::LIGHTPROBE_OCT_SIZE;
  1180. push_constant.image_size[1] *= SDFGI::LIGHTPROBE_OCT_SIZE;
  1181. for (uint32_t i = 0; i < rb->sdfgi->cascades.size(); i++) {
  1182. push_constant.cascade = i;
  1183. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->cascades[i].integrate_uniform_set, 0);
  1184. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::IntegratePushConstant));
  1185. 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);
  1186. }
  1187. RD::get_singleton()->compute_list_end();
  1188. RENDER_TIMESTAMP("<SDFGI Update Probes");
  1189. }
  1190. void RasterizerSceneRD::_process_gi(RID p_render_buffers, RID p_normal_roughness_buffer, RID p_ambient_buffer, RID p_reflection_buffer, RID p_gi_probe_buffer, RID p_environment, const CameraMatrix &p_projection, const Transform &p_transform, RID *p_gi_probe_cull_result, int p_gi_probe_cull_count) {
  1191. RENDER_TIMESTAMP("Render GI");
  1192. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  1193. ERR_FAIL_COND(rb == nullptr);
  1194. Environent *env = environment_owner.getornull(p_environment);
  1195. GI::PushConstant push_constant;
  1196. push_constant.screen_size[0] = rb->width;
  1197. push_constant.screen_size[1] = rb->height;
  1198. push_constant.z_near = p_projection.get_z_near();
  1199. push_constant.z_far = p_projection.get_z_far();
  1200. push_constant.orthogonal = p_projection.is_orthogonal();
  1201. push_constant.proj_info[0] = -2.0f / (rb->width * p_projection.matrix[0][0]);
  1202. push_constant.proj_info[1] = -2.0f / (rb->height * p_projection.matrix[1][1]);
  1203. push_constant.proj_info[2] = (1.0f - p_projection.matrix[0][2]) / p_projection.matrix[0][0];
  1204. push_constant.proj_info[3] = (1.0f + p_projection.matrix[1][2]) / p_projection.matrix[1][1];
  1205. push_constant.max_giprobes = MIN(RenderBuffers::MAX_GIPROBES, p_gi_probe_cull_count);
  1206. push_constant.high_quality_vct = gi_probe_quality == RS::GI_PROBE_QUALITY_HIGH;
  1207. push_constant.use_sdfgi = rb->sdfgi != nullptr;
  1208. if (env) {
  1209. push_constant.ao_color[0] = env->ao_color.r;
  1210. push_constant.ao_color[1] = env->ao_color.g;
  1211. push_constant.ao_color[2] = env->ao_color.b;
  1212. } else {
  1213. push_constant.ao_color[0] = 0;
  1214. push_constant.ao_color[1] = 0;
  1215. push_constant.ao_color[2] = 0;
  1216. }
  1217. push_constant.cam_rotation[0] = p_transform.basis[0][0];
  1218. push_constant.cam_rotation[1] = p_transform.basis[1][0];
  1219. push_constant.cam_rotation[2] = p_transform.basis[2][0];
  1220. push_constant.cam_rotation[3] = 0;
  1221. push_constant.cam_rotation[4] = p_transform.basis[0][1];
  1222. push_constant.cam_rotation[5] = p_transform.basis[1][1];
  1223. push_constant.cam_rotation[6] = p_transform.basis[2][1];
  1224. push_constant.cam_rotation[7] = 0;
  1225. push_constant.cam_rotation[8] = p_transform.basis[0][2];
  1226. push_constant.cam_rotation[9] = p_transform.basis[1][2];
  1227. push_constant.cam_rotation[10] = p_transform.basis[2][2];
  1228. push_constant.cam_rotation[11] = 0;
  1229. if (rb->sdfgi) {
  1230. GI::SDFGIData sdfgi_data;
  1231. sdfgi_data.grid_size[0] = rb->sdfgi->cascade_size;
  1232. sdfgi_data.grid_size[1] = rb->sdfgi->cascade_size;
  1233. sdfgi_data.grid_size[2] = rb->sdfgi->cascade_size;
  1234. sdfgi_data.max_cascades = rb->sdfgi->cascades.size();
  1235. sdfgi_data.probe_axis_size = rb->sdfgi->probe_axis_count;
  1236. sdfgi_data.cascade_probe_size[0] = sdfgi_data.probe_axis_size - 1; //float version for performance
  1237. sdfgi_data.cascade_probe_size[1] = sdfgi_data.probe_axis_size - 1;
  1238. sdfgi_data.cascade_probe_size[2] = sdfgi_data.probe_axis_size - 1;
  1239. float csize = rb->sdfgi->cascade_size;
  1240. sdfgi_data.probe_to_uvw = 1.0 / float(sdfgi_data.cascade_probe_size[0]);
  1241. sdfgi_data.use_occlusion = rb->sdfgi->uses_occlusion;
  1242. //sdfgi_data.energy = rb->sdfgi->energy;
  1243. sdfgi_data.y_mult = rb->sdfgi->y_mult;
  1244. float cascade_voxel_size = (csize / sdfgi_data.cascade_probe_size[0]);
  1245. float occlusion_clamp = (cascade_voxel_size - 0.5) / cascade_voxel_size;
  1246. sdfgi_data.occlusion_clamp[0] = occlusion_clamp;
  1247. sdfgi_data.occlusion_clamp[1] = occlusion_clamp;
  1248. sdfgi_data.occlusion_clamp[2] = occlusion_clamp;
  1249. sdfgi_data.normal_bias = (rb->sdfgi->normal_bias / csize) * sdfgi_data.cascade_probe_size[0];
  1250. //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 ) );
  1251. //vec3 probe_uv_offset = (ivec3(OCT_SIZE+2,OCT_SIZE+2,(OCT_SIZE+2) * params.probe_axis_size)) * tex_pixel_size.xyx;
  1252. uint32_t oct_size = SDFGI::LIGHTPROBE_OCT_SIZE;
  1253. sdfgi_data.lightprobe_tex_pixel_size[0] = 1.0 / ((oct_size + 2) * sdfgi_data.probe_axis_size * sdfgi_data.probe_axis_size);
  1254. sdfgi_data.lightprobe_tex_pixel_size[1] = 1.0 / ((oct_size + 2) * sdfgi_data.probe_axis_size);
  1255. sdfgi_data.lightprobe_tex_pixel_size[2] = 1.0;
  1256. sdfgi_data.energy = rb->sdfgi->energy;
  1257. sdfgi_data.lightprobe_uv_offset[0] = float(oct_size + 2) * sdfgi_data.lightprobe_tex_pixel_size[0];
  1258. sdfgi_data.lightprobe_uv_offset[1] = float(oct_size + 2) * sdfgi_data.lightprobe_tex_pixel_size[1];
  1259. sdfgi_data.lightprobe_uv_offset[2] = float((oct_size + 2) * sdfgi_data.probe_axis_size) * sdfgi_data.lightprobe_tex_pixel_size[0];
  1260. sdfgi_data.occlusion_renormalize[0] = 0.5;
  1261. sdfgi_data.occlusion_renormalize[1] = 1.0;
  1262. sdfgi_data.occlusion_renormalize[2] = 1.0 / float(sdfgi_data.max_cascades);
  1263. int32_t probe_divisor = rb->sdfgi->cascade_size / SDFGI::PROBE_DIVISOR;
  1264. for (uint32_t i = 0; i < sdfgi_data.max_cascades; i++) {
  1265. GI::SDFGIData::ProbeCascadeData &c = sdfgi_data.cascades[i];
  1266. 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;
  1267. Vector3 cam_origin = p_transform.origin;
  1268. cam_origin.y *= rb->sdfgi->y_mult;
  1269. pos -= cam_origin; //make pos local to camera, to reduce numerical error
  1270. c.position[0] = pos.x;
  1271. c.position[1] = pos.y;
  1272. c.position[2] = pos.z;
  1273. c.to_probe = 1.0 / (float(rb->sdfgi->cascade_size) * rb->sdfgi->cascades[i].cell_size / float(rb->sdfgi->probe_axis_count - 1));
  1274. Vector3i probe_ofs = rb->sdfgi->cascades[i].position / probe_divisor;
  1275. c.probe_world_offset[0] = probe_ofs.x;
  1276. c.probe_world_offset[1] = probe_ofs.y;
  1277. c.probe_world_offset[2] = probe_ofs.z;
  1278. c.to_cell = 1.0 / rb->sdfgi->cascades[i].cell_size;
  1279. }
  1280. RD::get_singleton()->buffer_update(gi.sdfgi_ubo, 0, sizeof(GI::SDFGIData), &sdfgi_data, true);
  1281. }
  1282. {
  1283. RID gi_probe_buffer = render_buffers_get_gi_probe_buffer(p_render_buffers);
  1284. GI::GIProbeData gi_probe_data[RenderBuffers::MAX_GIPROBES];
  1285. bool giprobes_changed = false;
  1286. Transform to_camera;
  1287. to_camera.origin = p_transform.origin; //only translation, make local
  1288. for (int i = 0; i < RenderBuffers::MAX_GIPROBES; i++) {
  1289. RID texture;
  1290. if (i < p_gi_probe_cull_count) {
  1291. GIProbeInstance *gipi = gi_probe_instance_owner.getornull(p_gi_probe_cull_result[i]);
  1292. if (gipi) {
  1293. texture = gipi->texture;
  1294. GI::GIProbeData &gipd = gi_probe_data[i];
  1295. RID base_probe = gipi->probe;
  1296. Transform to_cell = storage->gi_probe_get_to_cell_xform(gipi->probe) * gipi->transform.affine_inverse() * to_camera;
  1297. gipd.xform[0] = to_cell.basis.elements[0][0];
  1298. gipd.xform[1] = to_cell.basis.elements[1][0];
  1299. gipd.xform[2] = to_cell.basis.elements[2][0];
  1300. gipd.xform[3] = 0;
  1301. gipd.xform[4] = to_cell.basis.elements[0][1];
  1302. gipd.xform[5] = to_cell.basis.elements[1][1];
  1303. gipd.xform[6] = to_cell.basis.elements[2][1];
  1304. gipd.xform[7] = 0;
  1305. gipd.xform[8] = to_cell.basis.elements[0][2];
  1306. gipd.xform[9] = to_cell.basis.elements[1][2];
  1307. gipd.xform[10] = to_cell.basis.elements[2][2];
  1308. gipd.xform[11] = 0;
  1309. gipd.xform[12] = to_cell.origin.x;
  1310. gipd.xform[13] = to_cell.origin.y;
  1311. gipd.xform[14] = to_cell.origin.z;
  1312. gipd.xform[15] = 1;
  1313. Vector3 bounds = storage->gi_probe_get_octree_size(base_probe);
  1314. gipd.bounds[0] = bounds.x;
  1315. gipd.bounds[1] = bounds.y;
  1316. gipd.bounds[2] = bounds.z;
  1317. gipd.dynamic_range = storage->gi_probe_get_dynamic_range(base_probe) * storage->gi_probe_get_energy(base_probe);
  1318. gipd.bias = storage->gi_probe_get_bias(base_probe);
  1319. gipd.normal_bias = storage->gi_probe_get_normal_bias(base_probe);
  1320. gipd.blend_ambient = !storage->gi_probe_is_interior(base_probe);
  1321. gipd.anisotropy_strength = 0;
  1322. gipd.ao = storage->gi_probe_get_ao(base_probe);
  1323. gipd.ao_size = Math::pow(storage->gi_probe_get_ao_size(base_probe), 4.0f);
  1324. }
  1325. }
  1326. if (texture == RID()) {
  1327. texture = storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE);
  1328. }
  1329. if (texture != rb->giprobe_textures[i]) {
  1330. giprobes_changed = true;
  1331. rb->giprobe_textures[i] = texture;
  1332. }
  1333. }
  1334. if (giprobes_changed) {
  1335. RD::get_singleton()->free(rb->gi_uniform_set);
  1336. rb->gi_uniform_set = RID();
  1337. }
  1338. if (p_gi_probe_cull_count > 0) {
  1339. RD::get_singleton()->buffer_update(gi_probe_buffer, 0, sizeof(GI::GIProbeData) * MIN(RenderBuffers::MAX_GIPROBES, p_gi_probe_cull_count), gi_probe_data, true);
  1340. }
  1341. }
  1342. if (rb->gi_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(rb->gi_uniform_set)) {
  1343. Vector<RD::Uniform> uniforms;
  1344. {
  1345. RD::Uniform u;
  1346. u.binding = 1;
  1347. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1348. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  1349. if (rb->sdfgi && j < rb->sdfgi->cascades.size()) {
  1350. u.ids.push_back(rb->sdfgi->cascades[j].sdf_tex);
  1351. } else {
  1352. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  1353. }
  1354. }
  1355. uniforms.push_back(u);
  1356. }
  1357. {
  1358. RD::Uniform u;
  1359. u.binding = 2;
  1360. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1361. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  1362. if (rb->sdfgi && j < rb->sdfgi->cascades.size()) {
  1363. u.ids.push_back(rb->sdfgi->cascades[j].light_tex);
  1364. } else {
  1365. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  1366. }
  1367. }
  1368. uniforms.push_back(u);
  1369. }
  1370. {
  1371. RD::Uniform u;
  1372. u.binding = 3;
  1373. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1374. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  1375. if (rb->sdfgi && j < rb->sdfgi->cascades.size()) {
  1376. u.ids.push_back(rb->sdfgi->cascades[j].light_aniso_0_tex);
  1377. } else {
  1378. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  1379. }
  1380. }
  1381. uniforms.push_back(u);
  1382. }
  1383. {
  1384. RD::Uniform u;
  1385. u.binding = 4;
  1386. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1387. for (uint32_t j = 0; j < SDFGI::MAX_CASCADES; j++) {
  1388. if (rb->sdfgi && j < rb->sdfgi->cascades.size()) {
  1389. u.ids.push_back(rb->sdfgi->cascades[j].light_aniso_1_tex);
  1390. } else {
  1391. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  1392. }
  1393. }
  1394. uniforms.push_back(u);
  1395. }
  1396. {
  1397. RD::Uniform u;
  1398. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1399. u.binding = 5;
  1400. if (rb->sdfgi) {
  1401. u.ids.push_back(rb->sdfgi->occlusion_texture);
  1402. } else {
  1403. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  1404. }
  1405. uniforms.push_back(u);
  1406. }
  1407. {
  1408. RD::Uniform u;
  1409. u.type = RD::UNIFORM_TYPE_SAMPLER;
  1410. u.binding = 6;
  1411. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1412. uniforms.push_back(u);
  1413. }
  1414. {
  1415. RD::Uniform u;
  1416. u.type = RD::UNIFORM_TYPE_SAMPLER;
  1417. u.binding = 7;
  1418. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  1419. uniforms.push_back(u);
  1420. }
  1421. {
  1422. RD::Uniform u;
  1423. u.type = RD::UNIFORM_TYPE_IMAGE;
  1424. u.binding = 9;
  1425. u.ids.push_back(p_ambient_buffer);
  1426. uniforms.push_back(u);
  1427. }
  1428. {
  1429. RD::Uniform u;
  1430. u.type = RD::UNIFORM_TYPE_IMAGE;
  1431. u.binding = 10;
  1432. u.ids.push_back(p_reflection_buffer);
  1433. uniforms.push_back(u);
  1434. }
  1435. {
  1436. RD::Uniform u;
  1437. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1438. u.binding = 11;
  1439. if (rb->sdfgi) {
  1440. u.ids.push_back(rb->sdfgi->lightprobe_texture);
  1441. } else {
  1442. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE));
  1443. }
  1444. uniforms.push_back(u);
  1445. }
  1446. {
  1447. RD::Uniform u;
  1448. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1449. u.binding = 12;
  1450. u.ids.push_back(rb->depth_texture);
  1451. uniforms.push_back(u);
  1452. }
  1453. {
  1454. RD::Uniform u;
  1455. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1456. u.binding = 13;
  1457. u.ids.push_back(p_normal_roughness_buffer);
  1458. uniforms.push_back(u);
  1459. }
  1460. {
  1461. RD::Uniform u;
  1462. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1463. u.binding = 14;
  1464. RID buffer = p_gi_probe_buffer.is_valid() ? p_gi_probe_buffer : storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_BLACK);
  1465. u.ids.push_back(buffer);
  1466. uniforms.push_back(u);
  1467. }
  1468. {
  1469. RD::Uniform u;
  1470. u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1471. u.binding = 15;
  1472. u.ids.push_back(gi.sdfgi_ubo);
  1473. uniforms.push_back(u);
  1474. }
  1475. {
  1476. RD::Uniform u;
  1477. u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1478. u.binding = 16;
  1479. u.ids.push_back(rb->giprobe_buffer);
  1480. uniforms.push_back(u);
  1481. }
  1482. {
  1483. RD::Uniform u;
  1484. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1485. u.binding = 17;
  1486. for (int i = 0; i < RenderBuffers::MAX_GIPROBES; i++) {
  1487. u.ids.push_back(rb->giprobe_textures[i]);
  1488. }
  1489. uniforms.push_back(u);
  1490. }
  1491. rb->gi_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, gi.shader.version_get_shader(gi.shader_version, 0), 0);
  1492. }
  1493. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  1494. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, gi.pipelines[0]);
  1495. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->gi_uniform_set, 0);
  1496. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GI::PushConstant));
  1497. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->width, rb->height, 1, 8, 8, 1);
  1498. RD::get_singleton()->compute_list_end();
  1499. }
  1500. RID RasterizerSceneRD::sky_create() {
  1501. return sky_owner.make_rid(Sky());
  1502. }
  1503. void RasterizerSceneRD::_sky_invalidate(Sky *p_sky) {
  1504. if (!p_sky->dirty) {
  1505. p_sky->dirty = true;
  1506. p_sky->dirty_list = dirty_sky_list;
  1507. dirty_sky_list = p_sky;
  1508. }
  1509. }
  1510. void RasterizerSceneRD::sky_set_radiance_size(RID p_sky, int p_radiance_size) {
  1511. Sky *sky = sky_owner.getornull(p_sky);
  1512. ERR_FAIL_COND(!sky);
  1513. ERR_FAIL_COND(p_radiance_size < 32 || p_radiance_size > 2048);
  1514. if (sky->radiance_size == p_radiance_size) {
  1515. return;
  1516. }
  1517. sky->radiance_size = p_radiance_size;
  1518. if (sky->mode == RS::SKY_MODE_REALTIME && sky->radiance_size != 256) {
  1519. WARN_PRINT("Realtime Skies can only use a radiance size of 256. Radiance size will be set to 256 internally.");
  1520. sky->radiance_size = 256;
  1521. }
  1522. _sky_invalidate(sky);
  1523. if (sky->radiance.is_valid()) {
  1524. RD::get_singleton()->free(sky->radiance);
  1525. sky->radiance = RID();
  1526. }
  1527. _clear_reflection_data(sky->reflection);
  1528. }
  1529. void RasterizerSceneRD::sky_set_mode(RID p_sky, RS::SkyMode p_mode) {
  1530. Sky *sky = sky_owner.getornull(p_sky);
  1531. ERR_FAIL_COND(!sky);
  1532. if (sky->mode == p_mode) {
  1533. return;
  1534. }
  1535. sky->mode = p_mode;
  1536. if (sky->mode == RS::SKY_MODE_REALTIME && sky->radiance_size != 256) {
  1537. WARN_PRINT("Realtime Skies can only use a radiance size of 256. Radiance size will be set to 256 internally.");
  1538. sky_set_radiance_size(p_sky, 256);
  1539. }
  1540. _sky_invalidate(sky);
  1541. if (sky->radiance.is_valid()) {
  1542. RD::get_singleton()->free(sky->radiance);
  1543. sky->radiance = RID();
  1544. }
  1545. _clear_reflection_data(sky->reflection);
  1546. }
  1547. void RasterizerSceneRD::sky_set_material(RID p_sky, RID p_material) {
  1548. Sky *sky = sky_owner.getornull(p_sky);
  1549. ERR_FAIL_COND(!sky);
  1550. sky->material = p_material;
  1551. _sky_invalidate(sky);
  1552. }
  1553. Ref<Image> RasterizerSceneRD::sky_bake_panorama(RID p_sky, float p_energy, bool p_bake_irradiance, const Size2i &p_size) {
  1554. Sky *sky = sky_owner.getornull(p_sky);
  1555. ERR_FAIL_COND_V(!sky, Ref<Image>());
  1556. _update_dirty_skys();
  1557. if (sky->radiance.is_valid()) {
  1558. RD::TextureFormat tf;
  1559. tf.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  1560. tf.width = p_size.width;
  1561. tf.height = p_size.height;
  1562. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  1563. RID rad_tex = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1564. storage->get_effects()->copy_cubemap_to_panorama(sky->radiance, rad_tex, p_size, p_bake_irradiance ? roughness_layers : 0, sky->reflection.layers.size() > 1);
  1565. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(rad_tex, 0);
  1566. RD::get_singleton()->free(rad_tex);
  1567. Ref<Image> img;
  1568. img.instance();
  1569. img->create(p_size.width, p_size.height, false, Image::FORMAT_RGBAF, data);
  1570. for (int i = 0; i < p_size.width; i++) {
  1571. for (int j = 0; j < p_size.height; j++) {
  1572. Color c = img->get_pixel(i, j);
  1573. c.r *= p_energy;
  1574. c.g *= p_energy;
  1575. c.b *= p_energy;
  1576. img->set_pixel(i, j, c);
  1577. }
  1578. }
  1579. return img;
  1580. }
  1581. return Ref<Image>();
  1582. }
  1583. void RasterizerSceneRD::_update_dirty_skys() {
  1584. Sky *sky = dirty_sky_list;
  1585. while (sky) {
  1586. bool texture_set_dirty = false;
  1587. //update sky configuration if texture is missing
  1588. if (sky->radiance.is_null()) {
  1589. int mipmaps = Image::get_image_required_mipmaps(sky->radiance_size, sky->radiance_size, Image::FORMAT_RGBAH) + 1;
  1590. uint32_t w = sky->radiance_size, h = sky->radiance_size;
  1591. int layers = roughness_layers;
  1592. if (sky->mode == RS::SKY_MODE_REALTIME) {
  1593. layers = 8;
  1594. if (roughness_layers != 8) {
  1595. WARN_PRINT("When using REALTIME skies, roughness_layers should be set to 8 in the project settings for best quality reflections");
  1596. }
  1597. }
  1598. if (sky_use_cubemap_array) {
  1599. //array (higher quality, 6 times more memory)
  1600. RD::TextureFormat tf;
  1601. tf.array_layers = layers * 6;
  1602. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1603. tf.type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  1604. tf.mipmaps = mipmaps;
  1605. tf.width = w;
  1606. tf.height = h;
  1607. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  1608. sky->radiance = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1609. _update_reflection_data(sky->reflection, sky->radiance_size, mipmaps, true, sky->radiance, 0, sky->mode == RS::SKY_MODE_REALTIME);
  1610. } else {
  1611. //regular cubemap, lower quality (aliasing, less memory)
  1612. RD::TextureFormat tf;
  1613. tf.array_layers = 6;
  1614. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1615. tf.type = RD::TEXTURE_TYPE_CUBE;
  1616. tf.mipmaps = MIN(mipmaps, layers);
  1617. tf.width = w;
  1618. tf.height = h;
  1619. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  1620. sky->radiance = RD::get_singleton()->texture_create(tf, RD::TextureView());
  1621. _update_reflection_data(sky->reflection, sky->radiance_size, MIN(mipmaps, layers), false, sky->radiance, 0, sky->mode == RS::SKY_MODE_REALTIME);
  1622. }
  1623. texture_set_dirty = true;
  1624. }
  1625. // Create subpass buffers if they havent been created already
  1626. 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) {
  1627. RD::TextureFormat tformat;
  1628. tformat.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1629. tformat.width = sky->screen_size.x / 2;
  1630. tformat.height = sky->screen_size.y / 2;
  1631. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1632. tformat.type = RD::TEXTURE_TYPE_2D;
  1633. sky->half_res_pass = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  1634. Vector<RID> texs;
  1635. texs.push_back(sky->half_res_pass);
  1636. sky->half_res_framebuffer = RD::get_singleton()->framebuffer_create(texs);
  1637. texture_set_dirty = true;
  1638. }
  1639. 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) {
  1640. RD::TextureFormat tformat;
  1641. tformat.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  1642. tformat.width = sky->screen_size.x / 4;
  1643. tformat.height = sky->screen_size.y / 4;
  1644. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  1645. tformat.type = RD::TEXTURE_TYPE_2D;
  1646. sky->quarter_res_pass = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  1647. Vector<RID> texs;
  1648. texs.push_back(sky->quarter_res_pass);
  1649. sky->quarter_res_framebuffer = RD::get_singleton()->framebuffer_create(texs);
  1650. texture_set_dirty = true;
  1651. }
  1652. if (texture_set_dirty) {
  1653. for (int i = 0; i < SKY_TEXTURE_SET_MAX; i++) {
  1654. if (sky->texture_uniform_sets[i].is_valid() && RD::get_singleton()->uniform_set_is_valid(sky->texture_uniform_sets[i])) {
  1655. RD::get_singleton()->free(sky->texture_uniform_sets[i]);
  1656. sky->texture_uniform_sets[i] = RID();
  1657. }
  1658. }
  1659. }
  1660. sky->reflection.dirty = true;
  1661. Sky *next = sky->dirty_list;
  1662. sky->dirty_list = nullptr;
  1663. sky->dirty = false;
  1664. sky = next;
  1665. }
  1666. dirty_sky_list = nullptr;
  1667. }
  1668. RID RasterizerSceneRD::sky_get_radiance_texture_rd(RID p_sky) const {
  1669. Sky *sky = sky_owner.getornull(p_sky);
  1670. ERR_FAIL_COND_V(!sky, RID());
  1671. return sky->radiance;
  1672. }
  1673. RID RasterizerSceneRD::sky_get_radiance_uniform_set_rd(RID p_sky, RID p_shader, int p_set) const {
  1674. Sky *sky = sky_owner.getornull(p_sky);
  1675. ERR_FAIL_COND_V(!sky, RID());
  1676. if (sky->uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(sky->uniform_set)) {
  1677. sky->uniform_set = RID();
  1678. if (sky->radiance.is_valid()) {
  1679. Vector<RD::Uniform> uniforms;
  1680. {
  1681. RD::Uniform u;
  1682. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1683. u.binding = 0;
  1684. u.ids.push_back(sky->radiance);
  1685. uniforms.push_back(u);
  1686. }
  1687. sky->uniform_set = RD::get_singleton()->uniform_set_create(uniforms, p_shader, p_set);
  1688. }
  1689. }
  1690. return sky->uniform_set;
  1691. }
  1692. RID RasterizerSceneRD::_get_sky_textures(Sky *p_sky, SkyTextureSetVersion p_version) {
  1693. if (p_sky->texture_uniform_sets[p_version].is_valid() && RD::get_singleton()->uniform_set_is_valid(p_sky->texture_uniform_sets[p_version])) {
  1694. return p_sky->texture_uniform_sets[p_version];
  1695. }
  1696. Vector<RD::Uniform> uniforms;
  1697. {
  1698. RD::Uniform u;
  1699. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1700. u.binding = 0;
  1701. if (p_sky->radiance.is_valid() && p_version <= SKY_TEXTURE_SET_QUARTER_RES) {
  1702. u.ids.push_back(p_sky->radiance);
  1703. } else {
  1704. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  1705. }
  1706. uniforms.push_back(u);
  1707. }
  1708. {
  1709. RD::Uniform u;
  1710. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1711. u.binding = 1; // half res
  1712. if (p_sky->half_res_pass.is_valid() && p_version != SKY_TEXTURE_SET_HALF_RES && p_version != SKY_TEXTURE_SET_CUBEMAP_HALF_RES) {
  1713. if (p_version >= SKY_TEXTURE_SET_CUBEMAP) {
  1714. u.ids.push_back(p_sky->reflection.layers[0].views[1]);
  1715. } else {
  1716. u.ids.push_back(p_sky->half_res_pass);
  1717. }
  1718. } else {
  1719. if (p_version < SKY_TEXTURE_SET_CUBEMAP) {
  1720. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_WHITE));
  1721. } else {
  1722. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  1723. }
  1724. }
  1725. uniforms.push_back(u);
  1726. }
  1727. {
  1728. RD::Uniform u;
  1729. u.type = RD::UNIFORM_TYPE_TEXTURE;
  1730. u.binding = 2; // quarter res
  1731. if (p_sky->quarter_res_pass.is_valid() && p_version != SKY_TEXTURE_SET_QUARTER_RES && p_version != SKY_TEXTURE_SET_CUBEMAP_QUARTER_RES) {
  1732. if (p_version >= SKY_TEXTURE_SET_CUBEMAP) {
  1733. u.ids.push_back(p_sky->reflection.layers[0].views[2]);
  1734. } else {
  1735. u.ids.push_back(p_sky->quarter_res_pass);
  1736. }
  1737. } else {
  1738. if (p_version < SKY_TEXTURE_SET_CUBEMAP) {
  1739. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_WHITE));
  1740. } else {
  1741. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK));
  1742. }
  1743. }
  1744. uniforms.push_back(u);
  1745. }
  1746. p_sky->texture_uniform_sets[p_version] = RD::get_singleton()->uniform_set_create(uniforms, sky_shader.default_shader_rd, SKY_SET_TEXTURES);
  1747. return p_sky->texture_uniform_sets[p_version];
  1748. }
  1749. RID RasterizerSceneRD::sky_get_material(RID p_sky) const {
  1750. Sky *sky = sky_owner.getornull(p_sky);
  1751. ERR_FAIL_COND_V(!sky, RID());
  1752. return sky->material;
  1753. }
  1754. void RasterizerSceneRD::_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) {
  1755. ERR_FAIL_COND(!is_environment(p_environment));
  1756. Sky *sky = sky_owner.getornull(environment_get_sky(p_environment));
  1757. ERR_FAIL_COND(!sky);
  1758. RID sky_material = sky_get_material(environment_get_sky(p_environment));
  1759. SkyMaterialData *material = nullptr;
  1760. if (sky_material.is_valid()) {
  1761. material = (SkyMaterialData *)storage->material_get_data(sky_material, RasterizerStorageRD::SHADER_TYPE_SKY);
  1762. if (!material || !material->shader_data->valid) {
  1763. material = nullptr;
  1764. }
  1765. }
  1766. if (!material) {
  1767. sky_material = sky_shader.default_material;
  1768. material = (SkyMaterialData *)storage->material_get_data(sky_material, RasterizerStorageRD::SHADER_TYPE_SKY);
  1769. }
  1770. ERR_FAIL_COND(!material);
  1771. SkyShaderData *shader_data = material->shader_data;
  1772. ERR_FAIL_COND(!shader_data);
  1773. Basis sky_transform = environment_get_sky_orientation(p_environment);
  1774. sky_transform.invert();
  1775. float multiplier = environment_get_bg_energy(p_environment);
  1776. float custom_fov = environment_get_sky_custom_fov(p_environment);
  1777. // Camera
  1778. CameraMatrix camera;
  1779. if (custom_fov) {
  1780. float near_plane = p_projection.get_z_near();
  1781. float far_plane = p_projection.get_z_far();
  1782. float aspect = p_projection.get_aspect();
  1783. camera.set_perspective(custom_fov, aspect, near_plane, far_plane);
  1784. } else {
  1785. camera = p_projection;
  1786. }
  1787. sky_transform = p_transform.basis * sky_transform;
  1788. if (shader_data->uses_quarter_res) {
  1789. RenderPipelineVertexFormatCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_QUARTER_RES];
  1790. RID texture_uniform_set = _get_sky_textures(sky, SKY_TEXTURE_SET_QUARTER_RES);
  1791. Vector<Color> clear_colors;
  1792. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1793. 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);
  1794. storage->get_effects()->render_sky(draw_list, time, sky->quarter_res_framebuffer, sky_scene_state.sampler_uniform_set, sky_scene_state.light_uniform_set, pipeline, material->uniform_set, texture_uniform_set, camera, sky_transform, multiplier, p_transform.origin);
  1795. RD::get_singleton()->draw_list_end();
  1796. }
  1797. if (shader_data->uses_half_res) {
  1798. RenderPipelineVertexFormatCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_HALF_RES];
  1799. RID texture_uniform_set = _get_sky_textures(sky, SKY_TEXTURE_SET_HALF_RES);
  1800. Vector<Color> clear_colors;
  1801. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1802. 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);
  1803. storage->get_effects()->render_sky(draw_list, time, sky->half_res_framebuffer, sky_scene_state.sampler_uniform_set, sky_scene_state.light_uniform_set, pipeline, material->uniform_set, texture_uniform_set, camera, sky_transform, multiplier, p_transform.origin);
  1804. RD::get_singleton()->draw_list_end();
  1805. }
  1806. RenderPipelineVertexFormatCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_BACKGROUND];
  1807. RID texture_uniform_set = _get_sky_textures(sky, SKY_TEXTURE_SET_BACKGROUND);
  1808. 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);
  1809. storage->get_effects()->render_sky(draw_list, time, p_fb, sky_scene_state.sampler_uniform_set, sky_scene_state.light_uniform_set, pipeline, material->uniform_set, texture_uniform_set, camera, sky_transform, multiplier, p_transform.origin);
  1810. RD::get_singleton()->draw_list_end();
  1811. }
  1812. void RasterizerSceneRD::_setup_sky(RID p_environment, const Vector3 &p_position, const Size2i p_screen_size) {
  1813. ERR_FAIL_COND(!is_environment(p_environment));
  1814. Sky *sky = sky_owner.getornull(environment_get_sky(p_environment));
  1815. ERR_FAIL_COND(!sky);
  1816. RID sky_material = sky_get_material(environment_get_sky(p_environment));
  1817. SkyMaterialData *material = nullptr;
  1818. if (sky_material.is_valid()) {
  1819. material = (SkyMaterialData *)storage->material_get_data(sky_material, RasterizerStorageRD::SHADER_TYPE_SKY);
  1820. if (!material || !material->shader_data->valid) {
  1821. material = nullptr;
  1822. }
  1823. }
  1824. if (!material) {
  1825. sky_material = sky_shader.default_material;
  1826. material = (SkyMaterialData *)storage->material_get_data(sky_material, RasterizerStorageRD::SHADER_TYPE_SKY);
  1827. }
  1828. ERR_FAIL_COND(!material);
  1829. SkyShaderData *shader_data = material->shader_data;
  1830. ERR_FAIL_COND(!shader_data);
  1831. // Invalidate supbass buffers if screen size changes
  1832. if (sky->screen_size != p_screen_size) {
  1833. sky->screen_size = p_screen_size;
  1834. sky->screen_size.x = sky->screen_size.x < 4 ? 4 : sky->screen_size.x;
  1835. sky->screen_size.y = sky->screen_size.y < 4 ? 4 : sky->screen_size.y;
  1836. if (shader_data->uses_half_res) {
  1837. if (sky->half_res_pass.is_valid()) {
  1838. RD::get_singleton()->free(sky->half_res_pass);
  1839. sky->half_res_pass = RID();
  1840. }
  1841. _sky_invalidate(sky);
  1842. }
  1843. if (shader_data->uses_quarter_res) {
  1844. if (sky->quarter_res_pass.is_valid()) {
  1845. RD::get_singleton()->free(sky->quarter_res_pass);
  1846. sky->quarter_res_pass = RID();
  1847. }
  1848. _sky_invalidate(sky);
  1849. }
  1850. }
  1851. // Create new subpass buffers if necessary
  1852. if ((shader_data->uses_half_res && sky->half_res_pass.is_null()) ||
  1853. (shader_data->uses_quarter_res && sky->quarter_res_pass.is_null()) ||
  1854. sky->radiance.is_null()) {
  1855. _sky_invalidate(sky);
  1856. _update_dirty_skys();
  1857. }
  1858. if (shader_data->uses_time && time - sky->prev_time > 0.00001) {
  1859. sky->prev_time = time;
  1860. sky->reflection.dirty = true;
  1861. RenderingServerRaster::redraw_request();
  1862. }
  1863. if (material != sky->prev_material) {
  1864. sky->prev_material = material;
  1865. sky->reflection.dirty = true;
  1866. }
  1867. if (material->uniform_set_updated) {
  1868. material->uniform_set_updated = false;
  1869. sky->reflection.dirty = true;
  1870. }
  1871. if (!p_position.is_equal_approx(sky->prev_position) && shader_data->uses_position) {
  1872. sky->prev_position = p_position;
  1873. sky->reflection.dirty = true;
  1874. }
  1875. if (shader_data->uses_light || sky_scene_state.light_uniform_set.is_null()) {
  1876. // Check whether the directional_light_buffer changes
  1877. bool light_data_dirty = false;
  1878. if (sky_scene_state.directional_light_count != sky_scene_state.last_frame_directional_light_count) {
  1879. light_data_dirty = true;
  1880. for (uint32_t i = sky_scene_state.directional_light_count; i < sky_scene_state.max_directional_lights; i++) {
  1881. sky_scene_state.directional_lights[i].enabled = false;
  1882. }
  1883. }
  1884. if (!light_data_dirty) {
  1885. for (uint32_t i = 0; i < sky_scene_state.directional_light_count; i++) {
  1886. if (sky_scene_state.directional_lights[i].direction[0] != sky_scene_state.last_frame_directional_lights[i].direction[0] ||
  1887. sky_scene_state.directional_lights[i].direction[1] != sky_scene_state.last_frame_directional_lights[i].direction[1] ||
  1888. sky_scene_state.directional_lights[i].direction[2] != sky_scene_state.last_frame_directional_lights[i].direction[2] ||
  1889. sky_scene_state.directional_lights[i].energy != sky_scene_state.last_frame_directional_lights[i].energy ||
  1890. sky_scene_state.directional_lights[i].color[0] != sky_scene_state.last_frame_directional_lights[i].color[0] ||
  1891. sky_scene_state.directional_lights[i].color[1] != sky_scene_state.last_frame_directional_lights[i].color[1] ||
  1892. sky_scene_state.directional_lights[i].color[2] != sky_scene_state.last_frame_directional_lights[i].color[2] ||
  1893. sky_scene_state.directional_lights[i].enabled != sky_scene_state.last_frame_directional_lights[i].enabled ||
  1894. sky_scene_state.directional_lights[i].size != sky_scene_state.last_frame_directional_lights[i].size) {
  1895. light_data_dirty = true;
  1896. break;
  1897. }
  1898. }
  1899. }
  1900. if (light_data_dirty || sky_scene_state.light_uniform_set.is_null()) {
  1901. 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);
  1902. if (sky_scene_state.light_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(sky_scene_state.light_uniform_set)) {
  1903. RD::get_singleton()->free(sky_scene_state.light_uniform_set);
  1904. }
  1905. Vector<RD::Uniform> uniforms;
  1906. {
  1907. RD::Uniform u;
  1908. u.binding = 0;
  1909. u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1910. u.ids.push_back(sky_scene_state.directional_light_buffer);
  1911. uniforms.push_back(u);
  1912. }
  1913. sky_scene_state.light_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sky_shader.default_shader_rd, SKY_SET_LIGHTS);
  1914. RasterizerSceneRD::SkyDirectionalLightData *temp = sky_scene_state.last_frame_directional_lights;
  1915. sky_scene_state.last_frame_directional_lights = sky_scene_state.directional_lights;
  1916. sky_scene_state.directional_lights = temp;
  1917. sky_scene_state.last_frame_directional_light_count = sky_scene_state.directional_light_count;
  1918. sky->reflection.dirty = true;
  1919. }
  1920. }
  1921. }
  1922. void RasterizerSceneRD::_update_sky(RID p_environment, const CameraMatrix &p_projection, const Transform &p_transform) {
  1923. ERR_FAIL_COND(!is_environment(p_environment));
  1924. Sky *sky = sky_owner.getornull(environment_get_sky(p_environment));
  1925. ERR_FAIL_COND(!sky);
  1926. RID sky_material = sky_get_material(environment_get_sky(p_environment));
  1927. SkyMaterialData *material = nullptr;
  1928. if (sky_material.is_valid()) {
  1929. material = (SkyMaterialData *)storage->material_get_data(sky_material, RasterizerStorageRD::SHADER_TYPE_SKY);
  1930. if (!material || !material->shader_data->valid) {
  1931. material = nullptr;
  1932. }
  1933. }
  1934. if (!material) {
  1935. sky_material = sky_shader.default_material;
  1936. material = (SkyMaterialData *)storage->material_get_data(sky_material, RasterizerStorageRD::SHADER_TYPE_SKY);
  1937. }
  1938. ERR_FAIL_COND(!material);
  1939. SkyShaderData *shader_data = material->shader_data;
  1940. ERR_FAIL_COND(!shader_data);
  1941. float multiplier = environment_get_bg_energy(p_environment);
  1942. // Update radiance cubemap
  1943. if (sky->reflection.dirty) {
  1944. static const Vector3 view_normals[6] = {
  1945. Vector3(+1, 0, 0),
  1946. Vector3(-1, 0, 0),
  1947. Vector3(0, +1, 0),
  1948. Vector3(0, -1, 0),
  1949. Vector3(0, 0, +1),
  1950. Vector3(0, 0, -1)
  1951. };
  1952. static const Vector3 view_up[6] = {
  1953. Vector3(0, -1, 0),
  1954. Vector3(0, -1, 0),
  1955. Vector3(0, 0, +1),
  1956. Vector3(0, 0, -1),
  1957. Vector3(0, -1, 0),
  1958. Vector3(0, -1, 0)
  1959. };
  1960. CameraMatrix cm;
  1961. cm.set_perspective(90, 1, 0.01, 10.0);
  1962. CameraMatrix correction;
  1963. correction.set_depth_correction(true);
  1964. cm = correction * cm;
  1965. if (shader_data->uses_quarter_res) {
  1966. RenderPipelineVertexFormatCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_CUBEMAP_QUARTER_RES];
  1967. Vector<Color> clear_colors;
  1968. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1969. RD::DrawListID cubemap_draw_list;
  1970. for (int i = 0; i < 6; i++) {
  1971. Transform local_view;
  1972. local_view.set_look_at(Vector3(0, 0, 0), view_normals[i], view_up[i]);
  1973. RID texture_uniform_set = _get_sky_textures(sky, SKY_TEXTURE_SET_CUBEMAP_QUARTER_RES);
  1974. 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);
  1975. storage->get_effects()->render_sky(cubemap_draw_list, time, sky->reflection.layers[0].mipmaps[2].framebuffers[i], sky_scene_state.sampler_uniform_set, sky_scene_state.light_uniform_set, pipeline, material->uniform_set, texture_uniform_set, cm, local_view.basis, multiplier, p_transform.origin);
  1976. RD::get_singleton()->draw_list_end();
  1977. }
  1978. }
  1979. if (shader_data->uses_half_res) {
  1980. RenderPipelineVertexFormatCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_CUBEMAP_HALF_RES];
  1981. Vector<Color> clear_colors;
  1982. clear_colors.push_back(Color(0.0, 0.0, 0.0));
  1983. RD::DrawListID cubemap_draw_list;
  1984. for (int i = 0; i < 6; i++) {
  1985. Transform local_view;
  1986. local_view.set_look_at(Vector3(0, 0, 0), view_normals[i], view_up[i]);
  1987. RID texture_uniform_set = _get_sky_textures(sky, SKY_TEXTURE_SET_CUBEMAP_HALF_RES);
  1988. 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);
  1989. storage->get_effects()->render_sky(cubemap_draw_list, time, sky->reflection.layers[0].mipmaps[1].framebuffers[i], sky_scene_state.sampler_uniform_set, sky_scene_state.light_uniform_set, pipeline, material->uniform_set, texture_uniform_set, cm, local_view.basis, multiplier, p_transform.origin);
  1990. RD::get_singleton()->draw_list_end();
  1991. }
  1992. }
  1993. RD::DrawListID cubemap_draw_list;
  1994. RenderPipelineVertexFormatCacheRD *pipeline = &shader_data->pipelines[SKY_VERSION_CUBEMAP];
  1995. for (int i = 0; i < 6; i++) {
  1996. Transform local_view;
  1997. local_view.set_look_at(Vector3(0, 0, 0), view_normals[i], view_up[i]);
  1998. RID texture_uniform_set = _get_sky_textures(sky, SKY_TEXTURE_SET_CUBEMAP);
  1999. 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);
  2000. storage->get_effects()->render_sky(cubemap_draw_list, time, sky->reflection.layers[0].mipmaps[0].framebuffers[i], sky_scene_state.sampler_uniform_set, sky_scene_state.light_uniform_set, pipeline, material->uniform_set, texture_uniform_set, cm, local_view.basis, multiplier, p_transform.origin);
  2001. RD::get_singleton()->draw_list_end();
  2002. }
  2003. if (sky_use_cubemap_array) {
  2004. if (sky->mode == RS::SKY_MODE_QUALITY) {
  2005. for (int i = 1; i < sky->reflection.layers.size(); i++) {
  2006. _create_reflection_importance_sample(sky->reflection, sky_use_cubemap_array, 10, i);
  2007. }
  2008. } else {
  2009. _create_reflection_fast_filter(sky->reflection, sky_use_cubemap_array);
  2010. }
  2011. _update_reflection_mipmaps(sky->reflection);
  2012. } else {
  2013. if (sky->mode == RS::SKY_MODE_QUALITY) {
  2014. for (int i = 1; i < sky->reflection.layers[0].mipmaps.size(); i++) {
  2015. _create_reflection_importance_sample(sky->reflection, sky_use_cubemap_array, 10, i);
  2016. }
  2017. } else {
  2018. _create_reflection_fast_filter(sky->reflection, sky_use_cubemap_array);
  2019. }
  2020. }
  2021. sky->reflection.dirty = false;
  2022. }
  2023. }
  2024. /* SKY SHADER */
  2025. void RasterizerSceneRD::SkyShaderData::set_code(const String &p_code) {
  2026. //compile
  2027. code = p_code;
  2028. valid = false;
  2029. ubo_size = 0;
  2030. uniforms.clear();
  2031. if (code == String()) {
  2032. return; //just invalid, but no error
  2033. }
  2034. ShaderCompilerRD::GeneratedCode gen_code;
  2035. ShaderCompilerRD::IdentifierActions actions;
  2036. uses_time = false;
  2037. uses_half_res = false;
  2038. uses_quarter_res = false;
  2039. uses_position = false;
  2040. uses_light = false;
  2041. actions.render_mode_flags["use_half_res_pass"] = &uses_half_res;
  2042. actions.render_mode_flags["use_quarter_res_pass"] = &uses_quarter_res;
  2043. actions.usage_flag_pointers["TIME"] = &uses_time;
  2044. actions.usage_flag_pointers["POSITION"] = &uses_position;
  2045. actions.usage_flag_pointers["LIGHT0_ENABLED"] = &uses_light;
  2046. actions.usage_flag_pointers["LIGHT0_ENERGY"] = &uses_light;
  2047. actions.usage_flag_pointers["LIGHT0_DIRECTION"] = &uses_light;
  2048. actions.usage_flag_pointers["LIGHT0_COLOR"] = &uses_light;
  2049. actions.usage_flag_pointers["LIGHT0_SIZE"] = &uses_light;
  2050. actions.usage_flag_pointers["LIGHT1_ENABLED"] = &uses_light;
  2051. actions.usage_flag_pointers["LIGHT1_ENERGY"] = &uses_light;
  2052. actions.usage_flag_pointers["LIGHT1_DIRECTION"] = &uses_light;
  2053. actions.usage_flag_pointers["LIGHT1_COLOR"] = &uses_light;
  2054. actions.usage_flag_pointers["LIGHT1_SIZE"] = &uses_light;
  2055. actions.usage_flag_pointers["LIGHT2_ENABLED"] = &uses_light;
  2056. actions.usage_flag_pointers["LIGHT2_ENERGY"] = &uses_light;
  2057. actions.usage_flag_pointers["LIGHT2_DIRECTION"] = &uses_light;
  2058. actions.usage_flag_pointers["LIGHT2_COLOR"] = &uses_light;
  2059. actions.usage_flag_pointers["LIGHT2_SIZE"] = &uses_light;
  2060. actions.usage_flag_pointers["LIGHT3_ENABLED"] = &uses_light;
  2061. actions.usage_flag_pointers["LIGHT3_ENERGY"] = &uses_light;
  2062. actions.usage_flag_pointers["LIGHT3_DIRECTION"] = &uses_light;
  2063. actions.usage_flag_pointers["LIGHT3_COLOR"] = &uses_light;
  2064. actions.usage_flag_pointers["LIGHT3_SIZE"] = &uses_light;
  2065. actions.uniforms = &uniforms;
  2066. RasterizerSceneRD *scene_singleton = (RasterizerSceneRD *)RasterizerSceneRD::singleton;
  2067. Error err = scene_singleton->sky_shader.compiler.compile(RS::SHADER_SKY, code, &actions, path, gen_code);
  2068. ERR_FAIL_COND(err != OK);
  2069. if (version.is_null()) {
  2070. version = scene_singleton->sky_shader.shader.version_create();
  2071. }
  2072. #if 0
  2073. print_line("**compiling shader:");
  2074. print_line("**defines:\n");
  2075. for (int i = 0; i < gen_code.defines.size(); i++) {
  2076. print_line(gen_code.defines[i]);
  2077. }
  2078. print_line("\n**uniforms:\n" + gen_code.uniforms);
  2079. // print_line("\n**vertex_globals:\n" + gen_code.vertex_global);
  2080. // print_line("\n**vertex_code:\n" + gen_code.vertex);
  2081. print_line("\n**fragment_globals:\n" + gen_code.fragment_global);
  2082. print_line("\n**fragment_code:\n" + gen_code.fragment);
  2083. print_line("\n**light_code:\n" + gen_code.light);
  2084. #endif
  2085. 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);
  2086. ERR_FAIL_COND(!scene_singleton->sky_shader.shader.version_is_valid(version));
  2087. ubo_size = gen_code.uniform_total_size;
  2088. ubo_offsets = gen_code.uniform_offsets;
  2089. texture_uniforms = gen_code.texture_uniforms;
  2090. //update pipelines
  2091. for (int i = 0; i < SKY_VERSION_MAX; i++) {
  2092. RD::PipelineDepthStencilState depth_stencil_state;
  2093. depth_stencil_state.enable_depth_test = true;
  2094. depth_stencil_state.depth_compare_operator = RD::COMPARE_OP_LESS_OR_EQUAL;
  2095. RID shader_variant = scene_singleton->sky_shader.shader.version_get_shader(version, i);
  2096. pipelines[i].setup(shader_variant, RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), depth_stencil_state, RD::PipelineColorBlendState::create_disabled(), 0);
  2097. }
  2098. valid = true;
  2099. }
  2100. void RasterizerSceneRD::SkyShaderData::set_default_texture_param(const StringName &p_name, RID p_texture) {
  2101. if (!p_texture.is_valid()) {
  2102. default_texture_params.erase(p_name);
  2103. } else {
  2104. default_texture_params[p_name] = p_texture;
  2105. }
  2106. }
  2107. void RasterizerSceneRD::SkyShaderData::get_param_list(List<PropertyInfo> *p_param_list) const {
  2108. Map<int, StringName> order;
  2109. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = uniforms.front(); E; E = E->next()) {
  2110. if (E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL || E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  2111. continue;
  2112. }
  2113. if (E->get().texture_order >= 0) {
  2114. order[E->get().texture_order + 100000] = E->key();
  2115. } else {
  2116. order[E->get().order] = E->key();
  2117. }
  2118. }
  2119. for (Map<int, StringName>::Element *E = order.front(); E; E = E->next()) {
  2120. PropertyInfo pi = ShaderLanguage::uniform_to_property_info(uniforms[E->get()]);
  2121. pi.name = E->get();
  2122. p_param_list->push_back(pi);
  2123. }
  2124. }
  2125. void RasterizerSceneRD::SkyShaderData::get_instance_param_list(List<RasterizerStorage::InstanceShaderParam> *p_param_list) const {
  2126. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = uniforms.front(); E; E = E->next()) {
  2127. if (E->get().scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  2128. continue;
  2129. }
  2130. RasterizerStorage::InstanceShaderParam p;
  2131. p.info = ShaderLanguage::uniform_to_property_info(E->get());
  2132. p.info.name = E->key(); //supply name
  2133. p.index = E->get().instance_index;
  2134. p.default_value = ShaderLanguage::constant_value_to_variant(E->get().default_value, E->get().type, E->get().hint);
  2135. p_param_list->push_back(p);
  2136. }
  2137. }
  2138. bool RasterizerSceneRD::SkyShaderData::is_param_texture(const StringName &p_param) const {
  2139. if (!uniforms.has(p_param)) {
  2140. return false;
  2141. }
  2142. return uniforms[p_param].texture_order >= 0;
  2143. }
  2144. bool RasterizerSceneRD::SkyShaderData::is_animated() const {
  2145. return false;
  2146. }
  2147. bool RasterizerSceneRD::SkyShaderData::casts_shadows() const {
  2148. return false;
  2149. }
  2150. Variant RasterizerSceneRD::SkyShaderData::get_default_parameter(const StringName &p_parameter) const {
  2151. if (uniforms.has(p_parameter)) {
  2152. ShaderLanguage::ShaderNode::Uniform uniform = uniforms[p_parameter];
  2153. Vector<ShaderLanguage::ConstantNode::Value> default_value = uniform.default_value;
  2154. return ShaderLanguage::constant_value_to_variant(default_value, uniform.type, uniform.hint);
  2155. }
  2156. return Variant();
  2157. }
  2158. RasterizerSceneRD::SkyShaderData::SkyShaderData() {
  2159. valid = false;
  2160. }
  2161. RasterizerSceneRD::SkyShaderData::~SkyShaderData() {
  2162. RasterizerSceneRD *scene_singleton = (RasterizerSceneRD *)RasterizerSceneRD::singleton;
  2163. ERR_FAIL_COND(!scene_singleton);
  2164. //pipeline variants will clear themselves if shader is gone
  2165. if (version.is_valid()) {
  2166. scene_singleton->sky_shader.shader.version_free(version);
  2167. }
  2168. }
  2169. RasterizerStorageRD::ShaderData *RasterizerSceneRD::_create_sky_shader_func() {
  2170. SkyShaderData *shader_data = memnew(SkyShaderData);
  2171. return shader_data;
  2172. }
  2173. void RasterizerSceneRD::SkyMaterialData::update_parameters(const Map<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) {
  2174. RasterizerSceneRD *scene_singleton = (RasterizerSceneRD *)RasterizerSceneRD::singleton;
  2175. uniform_set_updated = true;
  2176. if ((uint32_t)ubo_data.size() != shader_data->ubo_size) {
  2177. p_uniform_dirty = true;
  2178. if (uniform_buffer.is_valid()) {
  2179. RD::get_singleton()->free(uniform_buffer);
  2180. uniform_buffer = RID();
  2181. }
  2182. ubo_data.resize(shader_data->ubo_size);
  2183. if (ubo_data.size()) {
  2184. uniform_buffer = RD::get_singleton()->uniform_buffer_create(ubo_data.size());
  2185. memset(ubo_data.ptrw(), 0, ubo_data.size()); //clear
  2186. }
  2187. //clear previous uniform set
  2188. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  2189. RD::get_singleton()->free(uniform_set);
  2190. uniform_set = RID();
  2191. }
  2192. }
  2193. //check whether buffer changed
  2194. if (p_uniform_dirty && ubo_data.size()) {
  2195. update_uniform_buffer(shader_data->uniforms, shader_data->ubo_offsets.ptr(), p_parameters, ubo_data.ptrw(), ubo_data.size(), false);
  2196. RD::get_singleton()->buffer_update(uniform_buffer, 0, ubo_data.size(), ubo_data.ptrw());
  2197. }
  2198. uint32_t tex_uniform_count = shader_data->texture_uniforms.size();
  2199. if ((uint32_t)texture_cache.size() != tex_uniform_count) {
  2200. texture_cache.resize(tex_uniform_count);
  2201. p_textures_dirty = true;
  2202. //clear previous uniform set
  2203. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  2204. RD::get_singleton()->free(uniform_set);
  2205. uniform_set = RID();
  2206. }
  2207. }
  2208. if (p_textures_dirty && tex_uniform_count) {
  2209. update_textures(p_parameters, shader_data->default_texture_params, shader_data->texture_uniforms, texture_cache.ptrw(), true);
  2210. }
  2211. if (shader_data->ubo_size == 0 && shader_data->texture_uniforms.size() == 0) {
  2212. // This material does not require an uniform set, so don't create it.
  2213. return;
  2214. }
  2215. if (!p_textures_dirty && uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  2216. //no reason to update uniform set, only UBO (or nothing) was needed to update
  2217. return;
  2218. }
  2219. Vector<RD::Uniform> uniforms;
  2220. {
  2221. if (shader_data->ubo_size) {
  2222. RD::Uniform u;
  2223. u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  2224. u.binding = 0;
  2225. u.ids.push_back(uniform_buffer);
  2226. uniforms.push_back(u);
  2227. }
  2228. const RID *textures = texture_cache.ptrw();
  2229. for (uint32_t i = 0; i < tex_uniform_count; i++) {
  2230. RD::Uniform u;
  2231. u.type = RD::UNIFORM_TYPE_TEXTURE;
  2232. u.binding = 1 + i;
  2233. u.ids.push_back(textures[i]);
  2234. uniforms.push_back(u);
  2235. }
  2236. }
  2237. uniform_set = RD::get_singleton()->uniform_set_create(uniforms, scene_singleton->sky_shader.shader.version_get_shader(shader_data->version, 0), SKY_SET_MATERIAL);
  2238. }
  2239. RasterizerSceneRD::SkyMaterialData::~SkyMaterialData() {
  2240. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  2241. RD::get_singleton()->free(uniform_set);
  2242. }
  2243. if (uniform_buffer.is_valid()) {
  2244. RD::get_singleton()->free(uniform_buffer);
  2245. }
  2246. }
  2247. RasterizerStorageRD::MaterialData *RasterizerSceneRD::_create_sky_material_func(SkyShaderData *p_shader) {
  2248. SkyMaterialData *material_data = memnew(SkyMaterialData);
  2249. material_data->shader_data = p_shader;
  2250. material_data->last_frame = false;
  2251. //update will happen later anyway so do nothing.
  2252. return material_data;
  2253. }
  2254. RID RasterizerSceneRD::environment_create() {
  2255. return environment_owner.make_rid(Environent());
  2256. }
  2257. void RasterizerSceneRD::environment_set_background(RID p_env, RS::EnvironmentBG p_bg) {
  2258. Environent *env = environment_owner.getornull(p_env);
  2259. ERR_FAIL_COND(!env);
  2260. env->background = p_bg;
  2261. }
  2262. void RasterizerSceneRD::environment_set_sky(RID p_env, RID p_sky) {
  2263. Environent *env = environment_owner.getornull(p_env);
  2264. ERR_FAIL_COND(!env);
  2265. env->sky = p_sky;
  2266. }
  2267. void RasterizerSceneRD::environment_set_sky_custom_fov(RID p_env, float p_scale) {
  2268. Environent *env = environment_owner.getornull(p_env);
  2269. ERR_FAIL_COND(!env);
  2270. env->sky_custom_fov = p_scale;
  2271. }
  2272. void RasterizerSceneRD::environment_set_sky_orientation(RID p_env, const Basis &p_orientation) {
  2273. Environent *env = environment_owner.getornull(p_env);
  2274. ERR_FAIL_COND(!env);
  2275. env->sky_orientation = p_orientation;
  2276. }
  2277. void RasterizerSceneRD::environment_set_bg_color(RID p_env, const Color &p_color) {
  2278. Environent *env = environment_owner.getornull(p_env);
  2279. ERR_FAIL_COND(!env);
  2280. env->bg_color = p_color;
  2281. }
  2282. void RasterizerSceneRD::environment_set_bg_energy(RID p_env, float p_energy) {
  2283. Environent *env = environment_owner.getornull(p_env);
  2284. ERR_FAIL_COND(!env);
  2285. env->bg_energy = p_energy;
  2286. }
  2287. void RasterizerSceneRD::environment_set_canvas_max_layer(RID p_env, int p_max_layer) {
  2288. Environent *env = environment_owner.getornull(p_env);
  2289. ERR_FAIL_COND(!env);
  2290. env->canvas_max_layer = p_max_layer;
  2291. }
  2292. void RasterizerSceneRD::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) {
  2293. Environent *env = environment_owner.getornull(p_env);
  2294. ERR_FAIL_COND(!env);
  2295. env->ambient_light = p_color;
  2296. env->ambient_source = p_ambient;
  2297. env->ambient_light_energy = p_energy;
  2298. env->ambient_sky_contribution = p_sky_contribution;
  2299. env->reflection_source = p_reflection_source;
  2300. env->ao_color = p_ao_color;
  2301. }
  2302. RS::EnvironmentBG RasterizerSceneRD::environment_get_background(RID p_env) const {
  2303. Environent *env = environment_owner.getornull(p_env);
  2304. ERR_FAIL_COND_V(!env, RS::ENV_BG_MAX);
  2305. return env->background;
  2306. }
  2307. RID RasterizerSceneRD::environment_get_sky(RID p_env) const {
  2308. Environent *env = environment_owner.getornull(p_env);
  2309. ERR_FAIL_COND_V(!env, RID());
  2310. return env->sky;
  2311. }
  2312. float RasterizerSceneRD::environment_get_sky_custom_fov(RID p_env) const {
  2313. Environent *env = environment_owner.getornull(p_env);
  2314. ERR_FAIL_COND_V(!env, 0);
  2315. return env->sky_custom_fov;
  2316. }
  2317. Basis RasterizerSceneRD::environment_get_sky_orientation(RID p_env) const {
  2318. Environent *env = environment_owner.getornull(p_env);
  2319. ERR_FAIL_COND_V(!env, Basis());
  2320. return env->sky_orientation;
  2321. }
  2322. Color RasterizerSceneRD::environment_get_bg_color(RID p_env) const {
  2323. Environent *env = environment_owner.getornull(p_env);
  2324. ERR_FAIL_COND_V(!env, Color());
  2325. return env->bg_color;
  2326. }
  2327. float RasterizerSceneRD::environment_get_bg_energy(RID p_env) const {
  2328. Environent *env = environment_owner.getornull(p_env);
  2329. ERR_FAIL_COND_V(!env, 0);
  2330. return env->bg_energy;
  2331. }
  2332. int RasterizerSceneRD::environment_get_canvas_max_layer(RID p_env) const {
  2333. Environent *env = environment_owner.getornull(p_env);
  2334. ERR_FAIL_COND_V(!env, 0);
  2335. return env->canvas_max_layer;
  2336. }
  2337. Color RasterizerSceneRD::environment_get_ambient_light_color(RID p_env) const {
  2338. Environent *env = environment_owner.getornull(p_env);
  2339. ERR_FAIL_COND_V(!env, Color());
  2340. return env->ambient_light;
  2341. }
  2342. RS::EnvironmentAmbientSource RasterizerSceneRD::environment_get_ambient_source(RID p_env) const {
  2343. Environent *env = environment_owner.getornull(p_env);
  2344. ERR_FAIL_COND_V(!env, RS::ENV_AMBIENT_SOURCE_BG);
  2345. return env->ambient_source;
  2346. }
  2347. float RasterizerSceneRD::environment_get_ambient_light_energy(RID p_env) const {
  2348. Environent *env = environment_owner.getornull(p_env);
  2349. ERR_FAIL_COND_V(!env, 0);
  2350. return env->ambient_light_energy;
  2351. }
  2352. float RasterizerSceneRD::environment_get_ambient_sky_contribution(RID p_env) const {
  2353. Environent *env = environment_owner.getornull(p_env);
  2354. ERR_FAIL_COND_V(!env, 0);
  2355. return env->ambient_sky_contribution;
  2356. }
  2357. RS::EnvironmentReflectionSource RasterizerSceneRD::environment_get_reflection_source(RID p_env) const {
  2358. Environent *env = environment_owner.getornull(p_env);
  2359. ERR_FAIL_COND_V(!env, RS::ENV_REFLECTION_SOURCE_DISABLED);
  2360. return env->reflection_source;
  2361. }
  2362. Color RasterizerSceneRD::environment_get_ao_color(RID p_env) const {
  2363. Environent *env = environment_owner.getornull(p_env);
  2364. ERR_FAIL_COND_V(!env, Color());
  2365. return env->ao_color;
  2366. }
  2367. void RasterizerSceneRD::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) {
  2368. Environent *env = environment_owner.getornull(p_env);
  2369. ERR_FAIL_COND(!env);
  2370. env->exposure = p_exposure;
  2371. env->tone_mapper = p_tone_mapper;
  2372. if (!env->auto_exposure && p_auto_exposure) {
  2373. env->auto_exposure_version = ++auto_exposure_counter;
  2374. }
  2375. env->auto_exposure = p_auto_exposure;
  2376. env->white = p_white;
  2377. env->min_luminance = p_min_luminance;
  2378. env->max_luminance = p_max_luminance;
  2379. env->auto_exp_speed = p_auto_exp_speed;
  2380. env->auto_exp_scale = p_auto_exp_scale;
  2381. }
  2382. void RasterizerSceneRD::environment_set_glow(RID p_env, bool p_enable, int p_level_flags, 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) {
  2383. Environent *env = environment_owner.getornull(p_env);
  2384. ERR_FAIL_COND(!env);
  2385. env->glow_enabled = p_enable;
  2386. env->glow_levels = p_level_flags;
  2387. env->glow_intensity = p_intensity;
  2388. env->glow_strength = p_strength;
  2389. env->glow_mix = p_mix;
  2390. env->glow_bloom = p_bloom_threshold;
  2391. env->glow_blend_mode = p_blend_mode;
  2392. env->glow_hdr_bleed_threshold = p_hdr_bleed_threshold;
  2393. env->glow_hdr_bleed_scale = p_hdr_bleed_scale;
  2394. env->glow_hdr_luminance_cap = p_hdr_luminance_cap;
  2395. }
  2396. void RasterizerSceneRD::environment_glow_set_use_bicubic_upscale(bool p_enable) {
  2397. glow_bicubic_upscale = p_enable;
  2398. }
  2399. void RasterizerSceneRD::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, bool p_enhance_ssr, float p_energy, float p_normal_bias, float p_probe_bias) {
  2400. Environent *env = environment_owner.getornull(p_env);
  2401. ERR_FAIL_COND(!env);
  2402. env->sdfgi_enabled = p_enable;
  2403. env->sdfgi_cascades = p_cascades;
  2404. env->sdfgi_min_cell_size = p_min_cell_size;
  2405. env->sdfgi_use_occlusion = p_use_occlusion;
  2406. env->sdfgi_use_multibounce = p_use_multibounce;
  2407. env->sdfgi_read_sky_light = p_read_sky;
  2408. env->sdfgi_enhance_ssr = p_enhance_ssr;
  2409. env->sdfgi_energy = p_energy;
  2410. env->sdfgi_normal_bias = p_normal_bias;
  2411. env->sdfgi_probe_bias = p_probe_bias;
  2412. env->sdfgi_y_scale = p_y_scale;
  2413. }
  2414. void RasterizerSceneRD::environment_set_sdfgi_ray_count(RS::EnvironmentSDFGIRayCount p_ray_count) {
  2415. sdfgi_ray_count = p_ray_count;
  2416. }
  2417. void RasterizerSceneRD::environment_set_sdfgi_frames_to_converge(RS::EnvironmentSDFGIFramesToConverge p_frames) {
  2418. sdfgi_frames_to_converge = p_frames;
  2419. }
  2420. void RasterizerSceneRD::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) {
  2421. Environent *env = environment_owner.getornull(p_env);
  2422. ERR_FAIL_COND(!env);
  2423. env->ssr_enabled = p_enable;
  2424. env->ssr_max_steps = p_max_steps;
  2425. env->ssr_fade_in = p_fade_int;
  2426. env->ssr_fade_out = p_fade_out;
  2427. env->ssr_depth_tolerance = p_depth_tolerance;
  2428. }
  2429. void RasterizerSceneRD::environment_set_ssr_roughness_quality(RS::EnvironmentSSRRoughnessQuality p_quality) {
  2430. ssr_roughness_quality = p_quality;
  2431. }
  2432. RS::EnvironmentSSRRoughnessQuality RasterizerSceneRD::environment_get_ssr_roughness_quality() const {
  2433. return ssr_roughness_quality;
  2434. }
  2435. void RasterizerSceneRD::environment_set_ssao(RID p_env, bool p_enable, float p_radius, float p_intensity, float p_bias, float p_light_affect, float p_ao_channel_affect, RS::EnvironmentSSAOBlur p_blur, float p_bilateral_sharpness) {
  2436. Environent *env = environment_owner.getornull(p_env);
  2437. ERR_FAIL_COND(!env);
  2438. env->ssao_enabled = p_enable;
  2439. env->ssao_radius = p_radius;
  2440. env->ssao_intensity = p_intensity;
  2441. env->ssao_bias = p_bias;
  2442. env->ssao_direct_light_affect = p_light_affect;
  2443. env->ssao_ao_channel_affect = p_ao_channel_affect;
  2444. env->ssao_blur = p_blur;
  2445. }
  2446. void RasterizerSceneRD::environment_set_ssao_quality(RS::EnvironmentSSAOQuality p_quality, bool p_half_size) {
  2447. ssao_quality = p_quality;
  2448. ssao_half_size = p_half_size;
  2449. }
  2450. bool RasterizerSceneRD::environment_is_ssao_enabled(RID p_env) const {
  2451. Environent *env = environment_owner.getornull(p_env);
  2452. ERR_FAIL_COND_V(!env, false);
  2453. return env->ssao_enabled;
  2454. }
  2455. float RasterizerSceneRD::environment_get_ssao_ao_affect(RID p_env) const {
  2456. Environent *env = environment_owner.getornull(p_env);
  2457. ERR_FAIL_COND_V(!env, false);
  2458. return env->ssao_ao_channel_affect;
  2459. }
  2460. float RasterizerSceneRD::environment_get_ssao_light_affect(RID p_env) const {
  2461. Environent *env = environment_owner.getornull(p_env);
  2462. ERR_FAIL_COND_V(!env, false);
  2463. return env->ssao_direct_light_affect;
  2464. }
  2465. bool RasterizerSceneRD::environment_is_ssr_enabled(RID p_env) const {
  2466. Environent *env = environment_owner.getornull(p_env);
  2467. ERR_FAIL_COND_V(!env, false);
  2468. return env->ssr_enabled;
  2469. }
  2470. bool RasterizerSceneRD::environment_is_sdfgi_enabled(RID p_env) const {
  2471. Environent *env = environment_owner.getornull(p_env);
  2472. ERR_FAIL_COND_V(!env, false);
  2473. return env->sdfgi_enabled;
  2474. }
  2475. bool RasterizerSceneRD::is_environment(RID p_env) const {
  2476. return environment_owner.owns(p_env);
  2477. }
  2478. Ref<Image> RasterizerSceneRD::environment_bake_panorama(RID p_env, bool p_bake_irradiance, const Size2i &p_size) {
  2479. Environent *env = environment_owner.getornull(p_env);
  2480. ERR_FAIL_COND_V(!env, Ref<Image>());
  2481. if (env->background == RS::ENV_BG_CAMERA_FEED || env->background == RS::ENV_BG_CANVAS || env->background == RS::ENV_BG_KEEP) {
  2482. return Ref<Image>(); //nothing to bake
  2483. }
  2484. if (env->background == RS::ENV_BG_CLEAR_COLOR || env->background == RS::ENV_BG_COLOR) {
  2485. Color color;
  2486. if (env->background == RS::ENV_BG_CLEAR_COLOR) {
  2487. color = storage->get_default_clear_color();
  2488. } else {
  2489. color = env->bg_color;
  2490. }
  2491. color.r *= env->bg_energy;
  2492. color.g *= env->bg_energy;
  2493. color.b *= env->bg_energy;
  2494. Ref<Image> ret;
  2495. ret.instance();
  2496. ret->create(p_size.width, p_size.height, false, Image::FORMAT_RGBAF);
  2497. for (int i = 0; i < p_size.width; i++) {
  2498. for (int j = 0; j < p_size.height; j++) {
  2499. ret->set_pixel(i, j, color);
  2500. }
  2501. }
  2502. return ret;
  2503. }
  2504. if (env->background == RS::ENV_BG_SKY && env->sky.is_valid()) {
  2505. return sky_bake_panorama(env->sky, env->bg_energy, p_bake_irradiance, p_size);
  2506. }
  2507. return Ref<Image>();
  2508. }
  2509. ////////////////////////////////////////////////////////////
  2510. RID RasterizerSceneRD::reflection_atlas_create() {
  2511. ReflectionAtlas ra;
  2512. ra.count = GLOBAL_GET("rendering/quality/reflection_atlas/reflection_count");
  2513. ra.size = GLOBAL_GET("rendering/quality/reflection_atlas/reflection_size");
  2514. return reflection_atlas_owner.make_rid(ra);
  2515. }
  2516. void RasterizerSceneRD::reflection_atlas_set_size(RID p_ref_atlas, int p_reflection_size, int p_reflection_count) {
  2517. ReflectionAtlas *ra = reflection_atlas_owner.getornull(p_ref_atlas);
  2518. ERR_FAIL_COND(!ra);
  2519. if (ra->size == p_reflection_size && ra->count == p_reflection_count) {
  2520. return; //no changes
  2521. }
  2522. ra->size = p_reflection_size;
  2523. ra->count = p_reflection_count;
  2524. if (ra->reflection.is_valid()) {
  2525. //clear and invalidate everything
  2526. RD::get_singleton()->free(ra->reflection);
  2527. ra->reflection = RID();
  2528. RD::get_singleton()->free(ra->depth_buffer);
  2529. ra->depth_buffer = RID();
  2530. for (int i = 0; i < ra->reflections.size(); i++) {
  2531. _clear_reflection_data(ra->reflections.write[i].data);
  2532. if (ra->reflections[i].owner.is_null()) {
  2533. continue;
  2534. }
  2535. reflection_probe_release_atlas_index(ra->reflections[i].owner);
  2536. //rp->atlasindex clear
  2537. }
  2538. ra->reflections.clear();
  2539. }
  2540. }
  2541. ////////////////////////
  2542. RID RasterizerSceneRD::reflection_probe_instance_create(RID p_probe) {
  2543. ReflectionProbeInstance rpi;
  2544. rpi.probe = p_probe;
  2545. return reflection_probe_instance_owner.make_rid(rpi);
  2546. }
  2547. void RasterizerSceneRD::reflection_probe_instance_set_transform(RID p_instance, const Transform &p_transform) {
  2548. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2549. ERR_FAIL_COND(!rpi);
  2550. rpi->transform = p_transform;
  2551. rpi->dirty = true;
  2552. }
  2553. void RasterizerSceneRD::reflection_probe_release_atlas_index(RID p_instance) {
  2554. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2555. ERR_FAIL_COND(!rpi);
  2556. if (rpi->atlas.is_null()) {
  2557. return; //nothing to release
  2558. }
  2559. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  2560. ERR_FAIL_COND(!atlas);
  2561. ERR_FAIL_INDEX(rpi->atlas_index, atlas->reflections.size());
  2562. atlas->reflections.write[rpi->atlas_index].owner = RID();
  2563. rpi->atlas_index = -1;
  2564. rpi->atlas = RID();
  2565. }
  2566. bool RasterizerSceneRD::reflection_probe_instance_needs_redraw(RID p_instance) {
  2567. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2568. ERR_FAIL_COND_V(!rpi, false);
  2569. if (rpi->rendering) {
  2570. return false;
  2571. }
  2572. if (rpi->dirty) {
  2573. return true;
  2574. }
  2575. if (storage->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS) {
  2576. return true;
  2577. }
  2578. return rpi->atlas_index == -1;
  2579. }
  2580. bool RasterizerSceneRD::reflection_probe_instance_has_reflection(RID p_instance) {
  2581. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2582. ERR_FAIL_COND_V(!rpi, false);
  2583. return rpi->atlas.is_valid();
  2584. }
  2585. bool RasterizerSceneRD::reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas) {
  2586. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(p_reflection_atlas);
  2587. ERR_FAIL_COND_V(!atlas, false);
  2588. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2589. ERR_FAIL_COND_V(!rpi, false);
  2590. if (storage->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS && atlas->reflection.is_valid() && atlas->size != 256) {
  2591. WARN_PRINT("ReflectionProbes set to UPDATE_ALWAYS must have an atlas size of 256. Please update the atlas size in the ProjectSettings.");
  2592. reflection_atlas_set_size(p_reflection_atlas, 256, atlas->count);
  2593. }
  2594. 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) {
  2595. // Invalidate reflection atlas, need to regenerate
  2596. RD::get_singleton()->free(atlas->reflection);
  2597. atlas->reflection = RID();
  2598. for (int i = 0; i < atlas->reflections.size(); i++) {
  2599. if (atlas->reflections[i].owner.is_null()) {
  2600. continue;
  2601. }
  2602. reflection_probe_release_atlas_index(atlas->reflections[i].owner);
  2603. }
  2604. atlas->reflections.clear();
  2605. }
  2606. if (atlas->reflection.is_null()) {
  2607. int mipmaps = MIN(roughness_layers, Image::get_image_required_mipmaps(atlas->size, atlas->size, Image::FORMAT_RGBAH) + 1);
  2608. mipmaps = storage->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS ? 8 : mipmaps; // always use 8 mipmaps with real time filtering
  2609. {
  2610. //reflection atlas was unused, create:
  2611. RD::TextureFormat tf;
  2612. tf.array_layers = 6 * atlas->count;
  2613. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  2614. tf.type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  2615. tf.mipmaps = mipmaps;
  2616. tf.width = atlas->size;
  2617. tf.height = atlas->size;
  2618. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  2619. atlas->reflection = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2620. }
  2621. {
  2622. RD::TextureFormat tf;
  2623. 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;
  2624. tf.width = atlas->size;
  2625. tf.height = atlas->size;
  2626. tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  2627. atlas->depth_buffer = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2628. }
  2629. atlas->reflections.resize(atlas->count);
  2630. for (int i = 0; i < atlas->count; i++) {
  2631. _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);
  2632. for (int j = 0; j < 6; j++) {
  2633. Vector<RID> fb;
  2634. fb.push_back(atlas->reflections.write[i].data.layers[0].mipmaps[0].views[j]);
  2635. fb.push_back(atlas->depth_buffer);
  2636. atlas->reflections.write[i].fbs[j] = RD::get_singleton()->framebuffer_create(fb);
  2637. }
  2638. }
  2639. Vector<RID> fb;
  2640. fb.push_back(atlas->depth_buffer);
  2641. atlas->depth_fb = RD::get_singleton()->framebuffer_create(fb);
  2642. }
  2643. if (rpi->atlas_index == -1) {
  2644. for (int i = 0; i < atlas->reflections.size(); i++) {
  2645. if (atlas->reflections[i].owner.is_null()) {
  2646. rpi->atlas_index = i;
  2647. break;
  2648. }
  2649. }
  2650. //find the one used last
  2651. if (rpi->atlas_index == -1) {
  2652. //everything is in use, find the one least used via LRU
  2653. uint64_t pass_min = 0;
  2654. for (int i = 0; i < atlas->reflections.size(); i++) {
  2655. ReflectionProbeInstance *rpi2 = reflection_probe_instance_owner.getornull(atlas->reflections[i].owner);
  2656. if (rpi2->last_pass < pass_min) {
  2657. pass_min = rpi2->last_pass;
  2658. rpi->atlas_index = i;
  2659. }
  2660. }
  2661. }
  2662. }
  2663. rpi->atlas = p_reflection_atlas;
  2664. rpi->rendering = true;
  2665. rpi->dirty = false;
  2666. rpi->processing_layer = 1;
  2667. rpi->processing_side = 0;
  2668. return true;
  2669. }
  2670. bool RasterizerSceneRD::reflection_probe_instance_postprocess_step(RID p_instance) {
  2671. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2672. ERR_FAIL_COND_V(!rpi, false);
  2673. ERR_FAIL_COND_V(!rpi->rendering, false);
  2674. ERR_FAIL_COND_V(rpi->atlas.is_null(), false);
  2675. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  2676. if (!atlas || rpi->atlas_index == -1) {
  2677. //does not belong to an atlas anymore, cancel (was removed from atlas or atlas changed while rendering)
  2678. rpi->rendering = false;
  2679. return false;
  2680. }
  2681. if (storage->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS) {
  2682. // Using real time reflections, all roughness is done in one step
  2683. _create_reflection_fast_filter(atlas->reflections.write[rpi->atlas_index].data, false);
  2684. rpi->rendering = false;
  2685. rpi->processing_side = 0;
  2686. rpi->processing_layer = 1;
  2687. return true;
  2688. }
  2689. if (rpi->processing_layer > 1) {
  2690. _create_reflection_importance_sample(atlas->reflections.write[rpi->atlas_index].data, false, 10, rpi->processing_layer);
  2691. rpi->processing_layer++;
  2692. if (rpi->processing_layer == atlas->reflections[rpi->atlas_index].data.layers[0].mipmaps.size()) {
  2693. rpi->rendering = false;
  2694. rpi->processing_side = 0;
  2695. rpi->processing_layer = 1;
  2696. return true;
  2697. }
  2698. return false;
  2699. } else {
  2700. _create_reflection_importance_sample(atlas->reflections.write[rpi->atlas_index].data, false, rpi->processing_side, rpi->processing_layer);
  2701. }
  2702. rpi->processing_side++;
  2703. if (rpi->processing_side == 6) {
  2704. rpi->processing_side = 0;
  2705. rpi->processing_layer++;
  2706. }
  2707. return false;
  2708. }
  2709. uint32_t RasterizerSceneRD::reflection_probe_instance_get_resolution(RID p_instance) {
  2710. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2711. ERR_FAIL_COND_V(!rpi, 0);
  2712. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  2713. ERR_FAIL_COND_V(!atlas, 0);
  2714. return atlas->size;
  2715. }
  2716. RID RasterizerSceneRD::reflection_probe_instance_get_framebuffer(RID p_instance, int p_index) {
  2717. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2718. ERR_FAIL_COND_V(!rpi, RID());
  2719. ERR_FAIL_INDEX_V(p_index, 6, RID());
  2720. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  2721. ERR_FAIL_COND_V(!atlas, RID());
  2722. return atlas->reflections[rpi->atlas_index].fbs[p_index];
  2723. }
  2724. RID RasterizerSceneRD::reflection_probe_instance_get_depth_framebuffer(RID p_instance, int p_index) {
  2725. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  2726. ERR_FAIL_COND_V(!rpi, RID());
  2727. ERR_FAIL_INDEX_V(p_index, 6, RID());
  2728. ReflectionAtlas *atlas = reflection_atlas_owner.getornull(rpi->atlas);
  2729. ERR_FAIL_COND_V(!atlas, RID());
  2730. return atlas->depth_fb;
  2731. }
  2732. ///////////////////////////////////////////////////////////
  2733. RID RasterizerSceneRD::shadow_atlas_create() {
  2734. return shadow_atlas_owner.make_rid(ShadowAtlas());
  2735. }
  2736. void RasterizerSceneRD::shadow_atlas_set_size(RID p_atlas, int p_size) {
  2737. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  2738. ERR_FAIL_COND(!shadow_atlas);
  2739. ERR_FAIL_COND(p_size < 0);
  2740. p_size = next_power_of_2(p_size);
  2741. if (p_size == shadow_atlas->size) {
  2742. return;
  2743. }
  2744. // erasing atlas
  2745. if (shadow_atlas->depth.is_valid()) {
  2746. RD::get_singleton()->free(shadow_atlas->depth);
  2747. shadow_atlas->depth = RID();
  2748. }
  2749. for (int i = 0; i < 4; i++) {
  2750. //clear subdivisions
  2751. shadow_atlas->quadrants[i].shadows.resize(0);
  2752. shadow_atlas->quadrants[i].shadows.resize(1 << shadow_atlas->quadrants[i].subdivision);
  2753. }
  2754. //erase shadow atlas reference from lights
  2755. for (Map<RID, uint32_t>::Element *E = shadow_atlas->shadow_owners.front(); E; E = E->next()) {
  2756. LightInstance *li = light_instance_owner.getornull(E->key());
  2757. ERR_CONTINUE(!li);
  2758. li->shadow_atlases.erase(p_atlas);
  2759. }
  2760. //clear owners
  2761. shadow_atlas->shadow_owners.clear();
  2762. shadow_atlas->size = p_size;
  2763. if (shadow_atlas->size) {
  2764. RD::TextureFormat tf;
  2765. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  2766. tf.width = shadow_atlas->size;
  2767. tf.height = shadow_atlas->size;
  2768. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  2769. shadow_atlas->depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2770. }
  2771. }
  2772. void RasterizerSceneRD::shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision) {
  2773. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  2774. ERR_FAIL_COND(!shadow_atlas);
  2775. ERR_FAIL_INDEX(p_quadrant, 4);
  2776. ERR_FAIL_INDEX(p_subdivision, 16384);
  2777. uint32_t subdiv = next_power_of_2(p_subdivision);
  2778. if (subdiv & 0xaaaaaaaa) { //sqrt(subdiv) must be integer
  2779. subdiv <<= 1;
  2780. }
  2781. subdiv = int(Math::sqrt((float)subdiv));
  2782. //obtain the number that will be x*x
  2783. if (shadow_atlas->quadrants[p_quadrant].subdivision == subdiv) {
  2784. return;
  2785. }
  2786. //erase all data from quadrant
  2787. for (int i = 0; i < shadow_atlas->quadrants[p_quadrant].shadows.size(); i++) {
  2788. if (shadow_atlas->quadrants[p_quadrant].shadows[i].owner.is_valid()) {
  2789. shadow_atlas->shadow_owners.erase(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  2790. LightInstance *li = light_instance_owner.getornull(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  2791. ERR_CONTINUE(!li);
  2792. li->shadow_atlases.erase(p_atlas);
  2793. }
  2794. }
  2795. shadow_atlas->quadrants[p_quadrant].shadows.resize(0);
  2796. shadow_atlas->quadrants[p_quadrant].shadows.resize(subdiv * subdiv);
  2797. shadow_atlas->quadrants[p_quadrant].subdivision = subdiv;
  2798. //cache the smallest subdiv (for faster allocation in light update)
  2799. shadow_atlas->smallest_subdiv = 1 << 30;
  2800. for (int i = 0; i < 4; i++) {
  2801. if (shadow_atlas->quadrants[i].subdivision) {
  2802. shadow_atlas->smallest_subdiv = MIN(shadow_atlas->smallest_subdiv, shadow_atlas->quadrants[i].subdivision);
  2803. }
  2804. }
  2805. if (shadow_atlas->smallest_subdiv == 1 << 30) {
  2806. shadow_atlas->smallest_subdiv = 0;
  2807. }
  2808. //resort the size orders, simple bublesort for 4 elements..
  2809. int swaps = 0;
  2810. do {
  2811. swaps = 0;
  2812. for (int i = 0; i < 3; i++) {
  2813. if (shadow_atlas->quadrants[shadow_atlas->size_order[i]].subdivision < shadow_atlas->quadrants[shadow_atlas->size_order[i + 1]].subdivision) {
  2814. SWAP(shadow_atlas->size_order[i], shadow_atlas->size_order[i + 1]);
  2815. swaps++;
  2816. }
  2817. }
  2818. } while (swaps > 0);
  2819. }
  2820. bool RasterizerSceneRD::_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) {
  2821. for (int i = p_quadrant_count - 1; i >= 0; i--) {
  2822. int qidx = p_in_quadrants[i];
  2823. if (shadow_atlas->quadrants[qidx].subdivision == (uint32_t)p_current_subdiv) {
  2824. return false;
  2825. }
  2826. //look for an empty space
  2827. int sc = shadow_atlas->quadrants[qidx].shadows.size();
  2828. ShadowAtlas::Quadrant::Shadow *sarr = shadow_atlas->quadrants[qidx].shadows.ptrw();
  2829. int found_free_idx = -1; //found a free one
  2830. int found_used_idx = -1; //found existing one, must steal it
  2831. uint64_t min_pass = 0; // pass of the existing one, try to use the least recently used one (LRU fashion)
  2832. for (int j = 0; j < sc; j++) {
  2833. if (!sarr[j].owner.is_valid()) {
  2834. found_free_idx = j;
  2835. break;
  2836. }
  2837. LightInstance *sli = light_instance_owner.getornull(sarr[j].owner);
  2838. ERR_CONTINUE(!sli);
  2839. if (sli->last_scene_pass != scene_pass) {
  2840. //was just allocated, don't kill it so soon, wait a bit..
  2841. if (p_tick - sarr[j].alloc_tick < shadow_atlas_realloc_tolerance_msec) {
  2842. continue;
  2843. }
  2844. if (found_used_idx == -1 || sli->last_scene_pass < min_pass) {
  2845. found_used_idx = j;
  2846. min_pass = sli->last_scene_pass;
  2847. }
  2848. }
  2849. }
  2850. if (found_free_idx == -1 && found_used_idx == -1) {
  2851. continue; //nothing found
  2852. }
  2853. if (found_free_idx == -1 && found_used_idx != -1) {
  2854. found_free_idx = found_used_idx;
  2855. }
  2856. r_quadrant = qidx;
  2857. r_shadow = found_free_idx;
  2858. return true;
  2859. }
  2860. return false;
  2861. }
  2862. bool RasterizerSceneRD::shadow_atlas_update_light(RID p_atlas, RID p_light_intance, float p_coverage, uint64_t p_light_version) {
  2863. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  2864. ERR_FAIL_COND_V(!shadow_atlas, false);
  2865. LightInstance *li = light_instance_owner.getornull(p_light_intance);
  2866. ERR_FAIL_COND_V(!li, false);
  2867. if (shadow_atlas->size == 0 || shadow_atlas->smallest_subdiv == 0) {
  2868. return false;
  2869. }
  2870. uint32_t quad_size = shadow_atlas->size >> 1;
  2871. int desired_fit = MIN(quad_size / shadow_atlas->smallest_subdiv, next_power_of_2(quad_size * p_coverage));
  2872. int valid_quadrants[4];
  2873. int valid_quadrant_count = 0;
  2874. int best_size = -1; //best size found
  2875. int best_subdiv = -1; //subdiv for the best size
  2876. //find the quadrants this fits into, and the best possible size it can fit into
  2877. for (int i = 0; i < 4; i++) {
  2878. int q = shadow_atlas->size_order[i];
  2879. int sd = shadow_atlas->quadrants[q].subdivision;
  2880. if (sd == 0) {
  2881. continue; //unused
  2882. }
  2883. int max_fit = quad_size / sd;
  2884. if (best_size != -1 && max_fit > best_size) {
  2885. break; //too large
  2886. }
  2887. valid_quadrants[valid_quadrant_count++] = q;
  2888. best_subdiv = sd;
  2889. if (max_fit >= desired_fit) {
  2890. best_size = max_fit;
  2891. }
  2892. }
  2893. ERR_FAIL_COND_V(valid_quadrant_count == 0, false);
  2894. uint64_t tick = OS::get_singleton()->get_ticks_msec();
  2895. //see if it already exists
  2896. if (shadow_atlas->shadow_owners.has(p_light_intance)) {
  2897. //it does!
  2898. uint32_t key = shadow_atlas->shadow_owners[p_light_intance];
  2899. uint32_t q = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  2900. uint32_t s = key & ShadowAtlas::SHADOW_INDEX_MASK;
  2901. 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);
  2902. bool should_redraw = shadow_atlas->quadrants[q].shadows[s].version != p_light_version;
  2903. if (!should_realloc) {
  2904. shadow_atlas->quadrants[q].shadows.write[s].version = p_light_version;
  2905. //already existing, see if it should redraw or it's just OK
  2906. return should_redraw;
  2907. }
  2908. int new_quadrant, new_shadow;
  2909. //find a better place
  2910. if (_shadow_atlas_find_shadow(shadow_atlas, valid_quadrants, valid_quadrant_count, shadow_atlas->quadrants[q].subdivision, tick, new_quadrant, new_shadow)) {
  2911. //found a better place!
  2912. ShadowAtlas::Quadrant::Shadow *sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_shadow];
  2913. if (sh->owner.is_valid()) {
  2914. //is taken, but is invalid, erasing it
  2915. shadow_atlas->shadow_owners.erase(sh->owner);
  2916. LightInstance *sli = light_instance_owner.getornull(sh->owner);
  2917. sli->shadow_atlases.erase(p_atlas);
  2918. }
  2919. //erase previous
  2920. shadow_atlas->quadrants[q].shadows.write[s].version = 0;
  2921. shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
  2922. sh->owner = p_light_intance;
  2923. sh->alloc_tick = tick;
  2924. sh->version = p_light_version;
  2925. li->shadow_atlases.insert(p_atlas);
  2926. //make new key
  2927. key = new_quadrant << ShadowAtlas::QUADRANT_SHIFT;
  2928. key |= new_shadow;
  2929. //update it in map
  2930. shadow_atlas->shadow_owners[p_light_intance] = key;
  2931. //make it dirty, as it should redraw anyway
  2932. return true;
  2933. }
  2934. //no better place for this shadow found, keep current
  2935. //already existing, see if it should redraw or it's just OK
  2936. shadow_atlas->quadrants[q].shadows.write[s].version = p_light_version;
  2937. return should_redraw;
  2938. }
  2939. int new_quadrant, new_shadow;
  2940. //find a better place
  2941. if (_shadow_atlas_find_shadow(shadow_atlas, valid_quadrants, valid_quadrant_count, -1, tick, new_quadrant, new_shadow)) {
  2942. //found a better place!
  2943. ShadowAtlas::Quadrant::Shadow *sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_shadow];
  2944. if (sh->owner.is_valid()) {
  2945. //is taken, but is invalid, erasing it
  2946. shadow_atlas->shadow_owners.erase(sh->owner);
  2947. LightInstance *sli = light_instance_owner.getornull(sh->owner);
  2948. sli->shadow_atlases.erase(p_atlas);
  2949. }
  2950. sh->owner = p_light_intance;
  2951. sh->alloc_tick = tick;
  2952. sh->version = p_light_version;
  2953. li->shadow_atlases.insert(p_atlas);
  2954. //make new key
  2955. uint32_t key = new_quadrant << ShadowAtlas::QUADRANT_SHIFT;
  2956. key |= new_shadow;
  2957. //update it in map
  2958. shadow_atlas->shadow_owners[p_light_intance] = key;
  2959. //make it dirty, as it should redraw anyway
  2960. return true;
  2961. }
  2962. //no place to allocate this light, apologies
  2963. return false;
  2964. }
  2965. void RasterizerSceneRD::directional_shadow_atlas_set_size(int p_size) {
  2966. p_size = nearest_power_of_2_templated(p_size);
  2967. if (directional_shadow.size == p_size) {
  2968. return;
  2969. }
  2970. directional_shadow.size = p_size;
  2971. if (directional_shadow.depth.is_valid()) {
  2972. RD::get_singleton()->free(directional_shadow.depth);
  2973. directional_shadow.depth = RID();
  2974. }
  2975. if (p_size > 0) {
  2976. RD::TextureFormat tf;
  2977. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  2978. tf.width = p_size;
  2979. tf.height = p_size;
  2980. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  2981. directional_shadow.depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
  2982. }
  2983. _base_uniforms_changed();
  2984. }
  2985. void RasterizerSceneRD::set_directional_shadow_count(int p_count) {
  2986. directional_shadow.light_count = p_count;
  2987. directional_shadow.current_light = 0;
  2988. }
  2989. static Rect2i _get_directional_shadow_rect(int p_size, int p_shadow_count, int p_shadow_index) {
  2990. int split_h = 1;
  2991. int split_v = 1;
  2992. while (split_h * split_v < p_shadow_count) {
  2993. if (split_h == split_v) {
  2994. split_h <<= 1;
  2995. } else {
  2996. split_v <<= 1;
  2997. }
  2998. }
  2999. Rect2i rect(0, 0, p_size, p_size);
  3000. rect.size.width /= split_h;
  3001. rect.size.height /= split_v;
  3002. rect.position.x = rect.size.width * (p_shadow_index % split_h);
  3003. rect.position.y = rect.size.height * (p_shadow_index / split_h);
  3004. return rect;
  3005. }
  3006. int RasterizerSceneRD::get_directional_light_shadow_size(RID p_light_intance) {
  3007. ERR_FAIL_COND_V(directional_shadow.light_count == 0, 0);
  3008. Rect2i r = _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, 0);
  3009. LightInstance *light_instance = light_instance_owner.getornull(p_light_intance);
  3010. ERR_FAIL_COND_V(!light_instance, 0);
  3011. switch (storage->light_directional_get_shadow_mode(light_instance->light)) {
  3012. case RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL:
  3013. break; //none
  3014. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS:
  3015. r.size.height /= 2;
  3016. break;
  3017. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS:
  3018. r.size /= 2;
  3019. break;
  3020. }
  3021. return MAX(r.size.width, r.size.height);
  3022. }
  3023. //////////////////////////////////////////////////
  3024. RID RasterizerSceneRD::camera_effects_create() {
  3025. return camera_effects_owner.make_rid(CameraEffects());
  3026. }
  3027. void RasterizerSceneRD::camera_effects_set_dof_blur_quality(RS::DOFBlurQuality p_quality, bool p_use_jitter) {
  3028. dof_blur_quality = p_quality;
  3029. dof_blur_use_jitter = p_use_jitter;
  3030. }
  3031. void RasterizerSceneRD::camera_effects_set_dof_blur_bokeh_shape(RS::DOFBokehShape p_shape) {
  3032. dof_blur_bokeh_shape = p_shape;
  3033. }
  3034. void RasterizerSceneRD::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) {
  3035. CameraEffects *camfx = camera_effects_owner.getornull(p_camera_effects);
  3036. ERR_FAIL_COND(!camfx);
  3037. camfx->dof_blur_far_enabled = p_far_enable;
  3038. camfx->dof_blur_far_distance = p_far_distance;
  3039. camfx->dof_blur_far_transition = p_far_transition;
  3040. camfx->dof_blur_near_enabled = p_near_enable;
  3041. camfx->dof_blur_near_distance = p_near_distance;
  3042. camfx->dof_blur_near_transition = p_near_transition;
  3043. camfx->dof_blur_amount = p_amount;
  3044. }
  3045. void RasterizerSceneRD::camera_effects_set_custom_exposure(RID p_camera_effects, bool p_enable, float p_exposure) {
  3046. CameraEffects *camfx = camera_effects_owner.getornull(p_camera_effects);
  3047. ERR_FAIL_COND(!camfx);
  3048. camfx->override_exposure_enabled = p_enable;
  3049. camfx->override_exposure = p_exposure;
  3050. }
  3051. RID RasterizerSceneRD::light_instance_create(RID p_light) {
  3052. RID li = light_instance_owner.make_rid(LightInstance());
  3053. LightInstance *light_instance = light_instance_owner.getornull(li);
  3054. light_instance->self = li;
  3055. light_instance->light = p_light;
  3056. light_instance->light_type = storage->light_get_type(p_light);
  3057. return li;
  3058. }
  3059. void RasterizerSceneRD::light_instance_set_transform(RID p_light_instance, const Transform &p_transform) {
  3060. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  3061. ERR_FAIL_COND(!light_instance);
  3062. light_instance->transform = p_transform;
  3063. }
  3064. void RasterizerSceneRD::light_instance_set_aabb(RID p_light_instance, const AABB &p_aabb) {
  3065. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  3066. ERR_FAIL_COND(!light_instance);
  3067. light_instance->aabb = p_aabb;
  3068. }
  3069. void RasterizerSceneRD::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) {
  3070. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  3071. ERR_FAIL_COND(!light_instance);
  3072. if (storage->light_get_type(light_instance->light) != RS::LIGHT_DIRECTIONAL) {
  3073. p_pass = 0;
  3074. }
  3075. ERR_FAIL_INDEX(p_pass, 4);
  3076. light_instance->shadow_transform[p_pass].camera = p_projection;
  3077. light_instance->shadow_transform[p_pass].transform = p_transform;
  3078. light_instance->shadow_transform[p_pass].farplane = p_far;
  3079. light_instance->shadow_transform[p_pass].split = p_split;
  3080. light_instance->shadow_transform[p_pass].bias_scale = p_bias_scale;
  3081. light_instance->shadow_transform[p_pass].range_begin = p_range_begin;
  3082. light_instance->shadow_transform[p_pass].shadow_texel_size = p_shadow_texel_size;
  3083. light_instance->shadow_transform[p_pass].uv_scale = p_uv_scale;
  3084. }
  3085. void RasterizerSceneRD::light_instance_mark_visible(RID p_light_instance) {
  3086. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  3087. ERR_FAIL_COND(!light_instance);
  3088. light_instance->last_scene_pass = scene_pass;
  3089. }
  3090. RasterizerSceneRD::ShadowCubemap *RasterizerSceneRD::_get_shadow_cubemap(int p_size) {
  3091. if (!shadow_cubemaps.has(p_size)) {
  3092. ShadowCubemap sc;
  3093. {
  3094. RD::TextureFormat tf;
  3095. 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;
  3096. tf.width = p_size;
  3097. tf.height = p_size;
  3098. tf.type = RD::TEXTURE_TYPE_CUBE;
  3099. tf.array_layers = 6;
  3100. tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  3101. sc.cubemap = RD::get_singleton()->texture_create(tf, RD::TextureView());
  3102. }
  3103. for (int i = 0; i < 6; i++) {
  3104. RID side_texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), sc.cubemap, i, 0);
  3105. Vector<RID> fbtex;
  3106. fbtex.push_back(side_texture);
  3107. sc.side_fb[i] = RD::get_singleton()->framebuffer_create(fbtex);
  3108. }
  3109. shadow_cubemaps[p_size] = sc;
  3110. }
  3111. return &shadow_cubemaps[p_size];
  3112. }
  3113. RasterizerSceneRD::ShadowMap *RasterizerSceneRD::_get_shadow_map(const Size2i &p_size) {
  3114. if (!shadow_maps.has(p_size)) {
  3115. ShadowMap sm;
  3116. {
  3117. RD::TextureFormat tf;
  3118. 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;
  3119. tf.width = p_size.width;
  3120. tf.height = p_size.height;
  3121. tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  3122. sm.depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
  3123. }
  3124. Vector<RID> fbtex;
  3125. fbtex.push_back(sm.depth);
  3126. sm.fb = RD::get_singleton()->framebuffer_create(fbtex);
  3127. shadow_maps[p_size] = sm;
  3128. }
  3129. return &shadow_maps[p_size];
  3130. }
  3131. //////////////////////////
  3132. RID RasterizerSceneRD::decal_instance_create(RID p_decal) {
  3133. DecalInstance di;
  3134. di.decal = p_decal;
  3135. return decal_instance_owner.make_rid(di);
  3136. }
  3137. void RasterizerSceneRD::decal_instance_set_transform(RID p_decal, const Transform &p_transform) {
  3138. DecalInstance *di = decal_instance_owner.getornull(p_decal);
  3139. ERR_FAIL_COND(!di);
  3140. di->transform = p_transform;
  3141. }
  3142. /////////////////////////////////
  3143. RID RasterizerSceneRD::gi_probe_instance_create(RID p_base) {
  3144. GIProbeInstance gi_probe;
  3145. gi_probe.probe = p_base;
  3146. RID rid = gi_probe_instance_owner.make_rid(gi_probe);
  3147. return rid;
  3148. }
  3149. void RasterizerSceneRD::gi_probe_instance_set_transform_to_data(RID p_probe, const Transform &p_xform) {
  3150. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_probe);
  3151. ERR_FAIL_COND(!gi_probe);
  3152. gi_probe->transform = p_xform;
  3153. }
  3154. bool RasterizerSceneRD::gi_probe_needs_update(RID p_probe) const {
  3155. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_probe);
  3156. ERR_FAIL_COND_V(!gi_probe, false);
  3157. //return true;
  3158. return gi_probe->last_probe_version != storage->gi_probe_get_version(gi_probe->probe);
  3159. }
  3160. void RasterizerSceneRD::gi_probe_update(RID p_probe, bool p_update_light_instances, const Vector<RID> &p_light_instances, int p_dynamic_object_count, InstanceBase **p_dynamic_objects) {
  3161. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_probe);
  3162. ERR_FAIL_COND(!gi_probe);
  3163. uint32_t data_version = storage->gi_probe_get_data_version(gi_probe->probe);
  3164. // (RE)CREATE IF NEEDED
  3165. if (gi_probe->last_probe_data_version != data_version) {
  3166. //need to re-create everything
  3167. if (gi_probe->texture.is_valid()) {
  3168. RD::get_singleton()->free(gi_probe->texture);
  3169. RD::get_singleton()->free(gi_probe->write_buffer);
  3170. gi_probe->mipmaps.clear();
  3171. }
  3172. for (int i = 0; i < gi_probe->dynamic_maps.size(); i++) {
  3173. RD::get_singleton()->free(gi_probe->dynamic_maps[i].texture);
  3174. RD::get_singleton()->free(gi_probe->dynamic_maps[i].depth);
  3175. }
  3176. gi_probe->dynamic_maps.clear();
  3177. Vector3i octree_size = storage->gi_probe_get_octree_size(gi_probe->probe);
  3178. if (octree_size != Vector3i()) {
  3179. //can create a 3D texture
  3180. Vector<int> levels = storage->gi_probe_get_level_counts(gi_probe->probe);
  3181. RD::TextureFormat tf;
  3182. tf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  3183. tf.width = octree_size.x;
  3184. tf.height = octree_size.y;
  3185. tf.depth = octree_size.z;
  3186. tf.type = RD::TEXTURE_TYPE_3D;
  3187. tf.mipmaps = levels.size();
  3188. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  3189. gi_probe->texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  3190. RD::get_singleton()->texture_clear(gi_probe->texture, Color(0, 0, 0, 0), 0, levels.size(), 0, 1, false);
  3191. {
  3192. int total_elements = 0;
  3193. for (int i = 0; i < levels.size(); i++) {
  3194. total_elements += levels[i];
  3195. }
  3196. gi_probe->write_buffer = RD::get_singleton()->storage_buffer_create(total_elements * 16);
  3197. }
  3198. for (int i = 0; i < levels.size(); i++) {
  3199. GIProbeInstance::Mipmap mipmap;
  3200. mipmap.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), gi_probe->texture, 0, i, RD::TEXTURE_SLICE_3D);
  3201. mipmap.level = levels.size() - i - 1;
  3202. mipmap.cell_offset = 0;
  3203. for (uint32_t j = 0; j < mipmap.level; j++) {
  3204. mipmap.cell_offset += levels[j];
  3205. }
  3206. mipmap.cell_count = levels[mipmap.level];
  3207. Vector<RD::Uniform> uniforms;
  3208. {
  3209. RD::Uniform u;
  3210. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3211. u.binding = 1;
  3212. u.ids.push_back(storage->gi_probe_get_octree_buffer(gi_probe->probe));
  3213. uniforms.push_back(u);
  3214. }
  3215. {
  3216. RD::Uniform u;
  3217. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3218. u.binding = 2;
  3219. u.ids.push_back(storage->gi_probe_get_data_buffer(gi_probe->probe));
  3220. uniforms.push_back(u);
  3221. }
  3222. {
  3223. RD::Uniform u;
  3224. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3225. u.binding = 4;
  3226. u.ids.push_back(gi_probe->write_buffer);
  3227. uniforms.push_back(u);
  3228. }
  3229. {
  3230. RD::Uniform u;
  3231. u.type = RD::UNIFORM_TYPE_TEXTURE;
  3232. u.binding = 9;
  3233. u.ids.push_back(storage->gi_probe_get_sdf_texture(gi_probe->probe));
  3234. uniforms.push_back(u);
  3235. }
  3236. {
  3237. RD::Uniform u;
  3238. u.type = RD::UNIFORM_TYPE_SAMPLER;
  3239. u.binding = 10;
  3240. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  3241. uniforms.push_back(u);
  3242. }
  3243. {
  3244. Vector<RD::Uniform> copy_uniforms = uniforms;
  3245. if (i == 0) {
  3246. {
  3247. RD::Uniform u;
  3248. u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  3249. u.binding = 3;
  3250. u.ids.push_back(gi_probe_lights_uniform);
  3251. copy_uniforms.push_back(u);
  3252. }
  3253. mipmap.uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_COMPUTE_LIGHT], 0);
  3254. copy_uniforms = uniforms; //restore
  3255. {
  3256. RD::Uniform u;
  3257. u.type = RD::UNIFORM_TYPE_TEXTURE;
  3258. u.binding = 5;
  3259. u.ids.push_back(gi_probe->texture);
  3260. copy_uniforms.push_back(u);
  3261. }
  3262. 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);
  3263. } else {
  3264. mipmap.uniform_set = RD::get_singleton()->uniform_set_create(copy_uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_COMPUTE_MIPMAP], 0);
  3265. }
  3266. }
  3267. {
  3268. RD::Uniform u;
  3269. u.type = RD::UNIFORM_TYPE_IMAGE;
  3270. u.binding = 5;
  3271. u.ids.push_back(mipmap.texture);
  3272. uniforms.push_back(u);
  3273. }
  3274. mipmap.write_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_WRITE_TEXTURE], 0);
  3275. gi_probe->mipmaps.push_back(mipmap);
  3276. }
  3277. {
  3278. uint32_t dynamic_map_size = MAX(MAX(octree_size.x, octree_size.y), octree_size.z);
  3279. uint32_t oversample = nearest_power_of_2_templated(4);
  3280. int mipmap_index = 0;
  3281. while (mipmap_index < gi_probe->mipmaps.size()) {
  3282. GIProbeInstance::DynamicMap dmap;
  3283. if (oversample > 0) {
  3284. dmap.size = dynamic_map_size * (1 << oversample);
  3285. dmap.mipmap = -1;
  3286. oversample--;
  3287. } else {
  3288. dmap.size = dynamic_map_size >> mipmap_index;
  3289. dmap.mipmap = mipmap_index;
  3290. mipmap_index++;
  3291. }
  3292. RD::TextureFormat dtf;
  3293. dtf.width = dmap.size;
  3294. dtf.height = dmap.size;
  3295. dtf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  3296. dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  3297. if (gi_probe->dynamic_maps.size() == 0) {
  3298. dtf.usage_bits |= RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  3299. }
  3300. dmap.texture = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  3301. if (gi_probe->dynamic_maps.size() == 0) {
  3302. //render depth for first one
  3303. 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;
  3304. dtf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  3305. dmap.fb_depth = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  3306. }
  3307. //just use depth as-is
  3308. dtf.format = RD::DATA_FORMAT_R32_SFLOAT;
  3309. dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  3310. dmap.depth = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  3311. if (gi_probe->dynamic_maps.size() == 0) {
  3312. dtf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  3313. dtf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  3314. dmap.albedo = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  3315. dmap.normal = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  3316. dmap.orm = RD::get_singleton()->texture_create(dtf, RD::TextureView());
  3317. Vector<RID> fb;
  3318. fb.push_back(dmap.albedo);
  3319. fb.push_back(dmap.normal);
  3320. fb.push_back(dmap.orm);
  3321. fb.push_back(dmap.texture); //emission
  3322. fb.push_back(dmap.depth);
  3323. fb.push_back(dmap.fb_depth);
  3324. dmap.fb = RD::get_singleton()->framebuffer_create(fb);
  3325. {
  3326. Vector<RD::Uniform> uniforms;
  3327. {
  3328. RD::Uniform u;
  3329. u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  3330. u.binding = 3;
  3331. u.ids.push_back(gi_probe_lights_uniform);
  3332. uniforms.push_back(u);
  3333. }
  3334. {
  3335. RD::Uniform u;
  3336. u.type = RD::UNIFORM_TYPE_IMAGE;
  3337. u.binding = 5;
  3338. u.ids.push_back(dmap.albedo);
  3339. uniforms.push_back(u);
  3340. }
  3341. {
  3342. RD::Uniform u;
  3343. u.type = RD::UNIFORM_TYPE_IMAGE;
  3344. u.binding = 6;
  3345. u.ids.push_back(dmap.normal);
  3346. uniforms.push_back(u);
  3347. }
  3348. {
  3349. RD::Uniform u;
  3350. u.type = RD::UNIFORM_TYPE_IMAGE;
  3351. u.binding = 7;
  3352. u.ids.push_back(dmap.orm);
  3353. uniforms.push_back(u);
  3354. }
  3355. {
  3356. RD::Uniform u;
  3357. u.type = RD::UNIFORM_TYPE_TEXTURE;
  3358. u.binding = 8;
  3359. u.ids.push_back(dmap.fb_depth);
  3360. uniforms.push_back(u);
  3361. }
  3362. {
  3363. RD::Uniform u;
  3364. u.type = RD::UNIFORM_TYPE_TEXTURE;
  3365. u.binding = 9;
  3366. u.ids.push_back(storage->gi_probe_get_sdf_texture(gi_probe->probe));
  3367. uniforms.push_back(u);
  3368. }
  3369. {
  3370. RD::Uniform u;
  3371. u.type = RD::UNIFORM_TYPE_SAMPLER;
  3372. u.binding = 10;
  3373. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  3374. uniforms.push_back(u);
  3375. }
  3376. {
  3377. RD::Uniform u;
  3378. u.type = RD::UNIFORM_TYPE_IMAGE;
  3379. u.binding = 11;
  3380. u.ids.push_back(dmap.texture);
  3381. uniforms.push_back(u);
  3382. }
  3383. {
  3384. RD::Uniform u;
  3385. u.type = RD::UNIFORM_TYPE_IMAGE;
  3386. u.binding = 12;
  3387. u.ids.push_back(dmap.depth);
  3388. uniforms.push_back(u);
  3389. }
  3390. dmap.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_lighting_shader_version_shaders[GI_PROBE_SHADER_VERSION_DYNAMIC_OBJECT_LIGHTING], 0);
  3391. }
  3392. } else {
  3393. bool plot = dmap.mipmap >= 0;
  3394. bool write = dmap.mipmap < (gi_probe->mipmaps.size() - 1);
  3395. Vector<RD::Uniform> uniforms;
  3396. {
  3397. RD::Uniform u;
  3398. u.type = RD::UNIFORM_TYPE_IMAGE;
  3399. u.binding = 5;
  3400. u.ids.push_back(gi_probe->dynamic_maps[gi_probe->dynamic_maps.size() - 1].texture);
  3401. uniforms.push_back(u);
  3402. }
  3403. {
  3404. RD::Uniform u;
  3405. u.type = RD::UNIFORM_TYPE_IMAGE;
  3406. u.binding = 6;
  3407. u.ids.push_back(gi_probe->dynamic_maps[gi_probe->dynamic_maps.size() - 1].depth);
  3408. uniforms.push_back(u);
  3409. }
  3410. if (write) {
  3411. {
  3412. RD::Uniform u;
  3413. u.type = RD::UNIFORM_TYPE_IMAGE;
  3414. u.binding = 7;
  3415. u.ids.push_back(dmap.texture);
  3416. uniforms.push_back(u);
  3417. }
  3418. {
  3419. RD::Uniform u;
  3420. u.type = RD::UNIFORM_TYPE_IMAGE;
  3421. u.binding = 8;
  3422. u.ids.push_back(dmap.depth);
  3423. uniforms.push_back(u);
  3424. }
  3425. }
  3426. {
  3427. RD::Uniform u;
  3428. u.type = RD::UNIFORM_TYPE_TEXTURE;
  3429. u.binding = 9;
  3430. u.ids.push_back(storage->gi_probe_get_sdf_texture(gi_probe->probe));
  3431. uniforms.push_back(u);
  3432. }
  3433. {
  3434. RD::Uniform u;
  3435. u.type = RD::UNIFORM_TYPE_SAMPLER;
  3436. u.binding = 10;
  3437. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  3438. uniforms.push_back(u);
  3439. }
  3440. if (plot) {
  3441. {
  3442. RD::Uniform u;
  3443. u.type = RD::UNIFORM_TYPE_IMAGE;
  3444. u.binding = 11;
  3445. u.ids.push_back(gi_probe->mipmaps[dmap.mipmap].texture);
  3446. uniforms.push_back(u);
  3447. }
  3448. }
  3449. 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);
  3450. }
  3451. gi_probe->dynamic_maps.push_back(dmap);
  3452. }
  3453. }
  3454. }
  3455. gi_probe->last_probe_data_version = data_version;
  3456. p_update_light_instances = true; //just in case
  3457. _base_uniforms_changed();
  3458. }
  3459. // UDPDATE TIME
  3460. if (gi_probe->has_dynamic_object_data) {
  3461. //if it has dynamic object data, it needs to be cleared
  3462. RD::get_singleton()->texture_clear(gi_probe->texture, Color(0, 0, 0, 0), 0, gi_probe->mipmaps.size(), 0, 1, true);
  3463. }
  3464. uint32_t light_count = 0;
  3465. if (p_update_light_instances || p_dynamic_object_count > 0) {
  3466. light_count = MIN(gi_probe_max_lights, (uint32_t)p_light_instances.size());
  3467. {
  3468. Transform to_cell = storage->gi_probe_get_to_cell_xform(gi_probe->probe);
  3469. Transform to_probe_xform = (gi_probe->transform * to_cell.affine_inverse()).affine_inverse();
  3470. //update lights
  3471. for (uint32_t i = 0; i < light_count; i++) {
  3472. GIProbeLight &l = gi_probe_lights[i];
  3473. RID light_instance = p_light_instances[i];
  3474. RID light = light_instance_get_base_light(light_instance);
  3475. l.type = storage->light_get_type(light);
  3476. l.attenuation = storage->light_get_param(light, RS::LIGHT_PARAM_ATTENUATION);
  3477. l.energy = storage->light_get_param(light, RS::LIGHT_PARAM_ENERGY) * storage->light_get_param(light, RS::LIGHT_PARAM_INDIRECT_ENERGY);
  3478. l.radius = to_cell.basis.xform(Vector3(storage->light_get_param(light, RS::LIGHT_PARAM_RANGE), 0, 0)).length();
  3479. Color color = storage->light_get_color(light).to_linear();
  3480. l.color[0] = color.r;
  3481. l.color[1] = color.g;
  3482. l.color[2] = color.b;
  3483. l.spot_angle_radians = Math::deg2rad(storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ANGLE));
  3484. l.spot_attenuation = storage->light_get_param(light, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  3485. Transform xform = light_instance_get_base_transform(light_instance);
  3486. Vector3 pos = to_probe_xform.xform(xform.origin);
  3487. Vector3 dir = to_probe_xform.basis.xform(-xform.basis.get_axis(2)).normalized();
  3488. l.position[0] = pos.x;
  3489. l.position[1] = pos.y;
  3490. l.position[2] = pos.z;
  3491. l.direction[0] = dir.x;
  3492. l.direction[1] = dir.y;
  3493. l.direction[2] = dir.z;
  3494. l.has_shadow = storage->light_has_shadow(light);
  3495. }
  3496. RD::get_singleton()->buffer_update(gi_probe_lights_uniform, 0, sizeof(GIProbeLight) * light_count, gi_probe_lights, true);
  3497. }
  3498. }
  3499. if (gi_probe->has_dynamic_object_data || p_update_light_instances || p_dynamic_object_count) {
  3500. // PROCESS MIPMAPS
  3501. if (gi_probe->mipmaps.size()) {
  3502. //can update mipmaps
  3503. Vector3i probe_size = storage->gi_probe_get_octree_size(gi_probe->probe);
  3504. GIProbePushConstant push_constant;
  3505. push_constant.limits[0] = probe_size.x;
  3506. push_constant.limits[1] = probe_size.y;
  3507. push_constant.limits[2] = probe_size.z;
  3508. push_constant.stack_size = gi_probe->mipmaps.size();
  3509. push_constant.emission_scale = 1.0;
  3510. push_constant.propagation = storage->gi_probe_get_propagation(gi_probe->probe);
  3511. push_constant.dynamic_range = storage->gi_probe_get_dynamic_range(gi_probe->probe);
  3512. push_constant.light_count = light_count;
  3513. push_constant.aniso_strength = 0;
  3514. /* print_line("probe update to version " + itos(gi_probe->last_probe_version));
  3515. print_line("propagation " + rtos(push_constant.propagation));
  3516. print_line("dynrange " + rtos(push_constant.dynamic_range));
  3517. */
  3518. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  3519. int passes;
  3520. if (p_update_light_instances) {
  3521. passes = storage->gi_probe_is_using_two_bounces(gi_probe->probe) ? 2 : 1;
  3522. } else {
  3523. passes = 1; //only re-blitting is necessary
  3524. }
  3525. int wg_size = 64;
  3526. int wg_limit_x = RD::get_singleton()->limit_get(RD::LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X);
  3527. for (int pass = 0; pass < passes; pass++) {
  3528. if (p_update_light_instances) {
  3529. for (int i = 0; i < gi_probe->mipmaps.size(); i++) {
  3530. if (i == 0) {
  3531. 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]);
  3532. } else if (i == 1) {
  3533. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_COMPUTE_MIPMAP]);
  3534. }
  3535. if (pass == 1 || i > 0) {
  3536. RD::get_singleton()->compute_list_add_barrier(compute_list); //wait til previous step is done
  3537. }
  3538. if (pass == 0 || i > 0) {
  3539. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->mipmaps[i].uniform_set, 0);
  3540. } else {
  3541. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->mipmaps[i].second_bounce_uniform_set, 0);
  3542. }
  3543. push_constant.cell_offset = gi_probe->mipmaps[i].cell_offset;
  3544. push_constant.cell_count = gi_probe->mipmaps[i].cell_count;
  3545. int wg_todo = (gi_probe->mipmaps[i].cell_count - 1) / wg_size + 1;
  3546. while (wg_todo) {
  3547. int wg_count = MIN(wg_todo, wg_limit_x);
  3548. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GIProbePushConstant));
  3549. RD::get_singleton()->compute_list_dispatch(compute_list, wg_count, 1, 1);
  3550. wg_todo -= wg_count;
  3551. push_constant.cell_offset += wg_count * wg_size;
  3552. }
  3553. }
  3554. RD::get_singleton()->compute_list_add_barrier(compute_list); //wait til previous step is done
  3555. }
  3556. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_WRITE_TEXTURE]);
  3557. for (int i = 0; i < gi_probe->mipmaps.size(); i++) {
  3558. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->mipmaps[i].write_uniform_set, 0);
  3559. push_constant.cell_offset = gi_probe->mipmaps[i].cell_offset;
  3560. push_constant.cell_count = gi_probe->mipmaps[i].cell_count;
  3561. int wg_todo = (gi_probe->mipmaps[i].cell_count - 1) / wg_size + 1;
  3562. while (wg_todo) {
  3563. int wg_count = MIN(wg_todo, wg_limit_x);
  3564. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GIProbePushConstant));
  3565. RD::get_singleton()->compute_list_dispatch(compute_list, wg_count, 1, 1);
  3566. wg_todo -= wg_count;
  3567. push_constant.cell_offset += wg_count * wg_size;
  3568. }
  3569. }
  3570. }
  3571. RD::get_singleton()->compute_list_end();
  3572. }
  3573. }
  3574. gi_probe->has_dynamic_object_data = false; //clear until dynamic object data is used again
  3575. if (p_dynamic_object_count && gi_probe->dynamic_maps.size()) {
  3576. Vector3i octree_size = storage->gi_probe_get_octree_size(gi_probe->probe);
  3577. int multiplier = gi_probe->dynamic_maps[0].size / MAX(MAX(octree_size.x, octree_size.y), octree_size.z);
  3578. Transform oversample_scale;
  3579. oversample_scale.basis.scale(Vector3(multiplier, multiplier, multiplier));
  3580. Transform to_cell = oversample_scale * storage->gi_probe_get_to_cell_xform(gi_probe->probe);
  3581. Transform to_world_xform = gi_probe->transform * to_cell.affine_inverse();
  3582. Transform to_probe_xform = to_world_xform.affine_inverse();
  3583. AABB probe_aabb(Vector3(), octree_size);
  3584. //this could probably be better parallelized in compute..
  3585. for (int i = 0; i < p_dynamic_object_count; i++) {
  3586. InstanceBase *instance = p_dynamic_objects[i];
  3587. //not used, so clear
  3588. instance->depth_layer = 0;
  3589. instance->depth = 0;
  3590. //transform aabb to giprobe
  3591. AABB aabb = (to_probe_xform * instance->transform).xform(instance->aabb);
  3592. //this needs to wrap to grid resolution to avoid jitter
  3593. //also extend margin a bit just in case
  3594. Vector3i begin = aabb.position - Vector3i(1, 1, 1);
  3595. Vector3i end = aabb.position + aabb.size + Vector3i(1, 1, 1);
  3596. for (int j = 0; j < 3; j++) {
  3597. if ((end[j] - begin[j]) & 1) {
  3598. end[j]++; //for half extents split, it needs to be even
  3599. }
  3600. begin[j] = MAX(begin[j], 0);
  3601. end[j] = MIN(end[j], octree_size[j] * multiplier);
  3602. }
  3603. //aabb = aabb.intersection(probe_aabb); //intersect
  3604. aabb.position = begin;
  3605. aabb.size = end - begin;
  3606. //print_line("aabb: " + aabb);
  3607. for (int j = 0; j < 6; j++) {
  3608. //if (j != 0 && j != 3) {
  3609. // continue;
  3610. //}
  3611. static const Vector3 render_z[6] = {
  3612. Vector3(1, 0, 0),
  3613. Vector3(0, 1, 0),
  3614. Vector3(0, 0, 1),
  3615. Vector3(-1, 0, 0),
  3616. Vector3(0, -1, 0),
  3617. Vector3(0, 0, -1),
  3618. };
  3619. static const Vector3 render_up[6] = {
  3620. Vector3(0, 1, 0),
  3621. Vector3(0, 0, 1),
  3622. Vector3(0, 1, 0),
  3623. Vector3(0, 1, 0),
  3624. Vector3(0, 0, 1),
  3625. Vector3(0, 1, 0),
  3626. };
  3627. Vector3 render_dir = render_z[j];
  3628. Vector3 up_dir = render_up[j];
  3629. Vector3 center = aabb.position + aabb.size * 0.5;
  3630. Transform xform;
  3631. xform.set_look_at(center - aabb.size * 0.5 * render_dir, center, up_dir);
  3632. Vector3 x_dir = xform.basis.get_axis(0).abs();
  3633. int x_axis = int(Vector3(0, 1, 2).dot(x_dir));
  3634. Vector3 y_dir = xform.basis.get_axis(1).abs();
  3635. int y_axis = int(Vector3(0, 1, 2).dot(y_dir));
  3636. Vector3 z_dir = -xform.basis.get_axis(2);
  3637. int z_axis = int(Vector3(0, 1, 2).dot(z_dir.abs()));
  3638. Rect2i rect(aabb.position[x_axis], aabb.position[y_axis], aabb.size[x_axis], aabb.size[y_axis]);
  3639. bool x_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_axis(0)) < 0);
  3640. bool y_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_axis(1)) < 0);
  3641. bool z_flip = bool(Vector3(1, 1, 1).dot(xform.basis.get_axis(2)) > 0);
  3642. CameraMatrix cm;
  3643. 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]);
  3644. _render_material(to_world_xform * xform, cm, true, &instance, 1, gi_probe->dynamic_maps[0].fb, Rect2i(Vector2i(), rect.size));
  3645. GIProbeDynamicPushConstant push_constant;
  3646. zeromem(&push_constant, sizeof(GIProbeDynamicPushConstant));
  3647. push_constant.limits[0] = octree_size.x;
  3648. push_constant.limits[1] = octree_size.y;
  3649. push_constant.limits[2] = octree_size.z;
  3650. push_constant.light_count = p_light_instances.size();
  3651. push_constant.x_dir[0] = x_dir[0];
  3652. push_constant.x_dir[1] = x_dir[1];
  3653. push_constant.x_dir[2] = x_dir[2];
  3654. push_constant.y_dir[0] = y_dir[0];
  3655. push_constant.y_dir[1] = y_dir[1];
  3656. push_constant.y_dir[2] = y_dir[2];
  3657. push_constant.z_dir[0] = z_dir[0];
  3658. push_constant.z_dir[1] = z_dir[1];
  3659. push_constant.z_dir[2] = z_dir[2];
  3660. push_constant.z_base = xform.origin[z_axis];
  3661. push_constant.z_sign = (z_flip ? -1.0 : 1.0);
  3662. push_constant.pos_multiplier = float(1.0) / multiplier;
  3663. push_constant.dynamic_range = storage->gi_probe_get_dynamic_range(gi_probe->probe);
  3664. push_constant.flip_x = x_flip;
  3665. push_constant.flip_y = y_flip;
  3666. push_constant.rect_pos[0] = rect.position[0];
  3667. push_constant.rect_pos[1] = rect.position[1];
  3668. push_constant.rect_size[0] = rect.size[0];
  3669. push_constant.rect_size[1] = rect.size[1];
  3670. push_constant.prev_rect_ofs[0] = 0;
  3671. push_constant.prev_rect_ofs[1] = 0;
  3672. push_constant.prev_rect_size[0] = 0;
  3673. push_constant.prev_rect_size[1] = 0;
  3674. push_constant.on_mipmap = false;
  3675. push_constant.propagation = storage->gi_probe_get_propagation(gi_probe->probe);
  3676. push_constant.pad[0] = 0;
  3677. push_constant.pad[1] = 0;
  3678. push_constant.pad[2] = 0;
  3679. //process lighting
  3680. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  3681. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_DYNAMIC_OBJECT_LIGHTING]);
  3682. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->dynamic_maps[0].uniform_set, 0);
  3683. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GIProbeDynamicPushConstant));
  3684. RD::get_singleton()->compute_list_dispatch(compute_list, (rect.size.x - 1) / 8 + 1, (rect.size.y - 1) / 8 + 1, 1);
  3685. //print_line("rect: " + itos(i) + ": " + rect);
  3686. for (int k = 1; k < gi_probe->dynamic_maps.size(); k++) {
  3687. // enlarge the rect if needed so all pixels fit when downscaled,
  3688. // this ensures downsampling is smooth and optimal because no pixels are left behind
  3689. //x
  3690. if (rect.position.x & 1) {
  3691. rect.size.x++;
  3692. push_constant.prev_rect_ofs[0] = 1; //this is used to ensure reading is also optimal
  3693. } else {
  3694. push_constant.prev_rect_ofs[0] = 0;
  3695. }
  3696. if (rect.size.x & 1) {
  3697. rect.size.x++;
  3698. }
  3699. rect.position.x >>= 1;
  3700. rect.size.x = MAX(1, rect.size.x >> 1);
  3701. //y
  3702. if (rect.position.y & 1) {
  3703. rect.size.y++;
  3704. push_constant.prev_rect_ofs[1] = 1;
  3705. } else {
  3706. push_constant.prev_rect_ofs[1] = 0;
  3707. }
  3708. if (rect.size.y & 1) {
  3709. rect.size.y++;
  3710. }
  3711. rect.position.y >>= 1;
  3712. rect.size.y = MAX(1, rect.size.y >> 1);
  3713. //shrink limits to ensure plot does not go outside map
  3714. if (gi_probe->dynamic_maps[k].mipmap > 0) {
  3715. for (int l = 0; l < 3; l++) {
  3716. push_constant.limits[l] = MAX(1, push_constant.limits[l] >> 1);
  3717. }
  3718. }
  3719. //print_line("rect: " + itos(i) + ": " + rect);
  3720. push_constant.rect_pos[0] = rect.position[0];
  3721. push_constant.rect_pos[1] = rect.position[1];
  3722. push_constant.prev_rect_size[0] = push_constant.rect_size[0];
  3723. push_constant.prev_rect_size[1] = push_constant.rect_size[1];
  3724. push_constant.rect_size[0] = rect.size[0];
  3725. push_constant.rect_size[1] = rect.size[1];
  3726. push_constant.on_mipmap = gi_probe->dynamic_maps[k].mipmap > 0;
  3727. RD::get_singleton()->compute_list_add_barrier(compute_list);
  3728. if (gi_probe->dynamic_maps[k].mipmap < 0) {
  3729. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_WRITE]);
  3730. } else if (k < gi_probe->dynamic_maps.size() - 1) {
  3731. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_WRITE_PLOT]);
  3732. } else {
  3733. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_lighting_shader_version_pipelines[GI_PROBE_SHADER_VERSION_DYNAMIC_SHRINK_PLOT]);
  3734. }
  3735. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, gi_probe->dynamic_maps[k].uniform_set, 0);
  3736. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(GIProbeDynamicPushConstant));
  3737. RD::get_singleton()->compute_list_dispatch(compute_list, (rect.size.x - 1) / 8 + 1, (rect.size.y - 1) / 8 + 1, 1);
  3738. }
  3739. RD::get_singleton()->compute_list_end();
  3740. }
  3741. }
  3742. gi_probe->has_dynamic_object_data = true; //clear until dynamic object data is used again
  3743. }
  3744. gi_probe->last_probe_version = storage->gi_probe_get_version(gi_probe->probe);
  3745. }
  3746. void RasterizerSceneRD::_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) {
  3747. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_gi_probe);
  3748. ERR_FAIL_COND(!gi_probe);
  3749. if (gi_probe->mipmaps.size() == 0) {
  3750. return;
  3751. }
  3752. CameraMatrix transform = (p_camera_with_transform * CameraMatrix(gi_probe->transform)) * CameraMatrix(storage->gi_probe_get_to_cell_xform(gi_probe->probe).affine_inverse());
  3753. int level = 0;
  3754. Vector3i octree_size = storage->gi_probe_get_octree_size(gi_probe->probe);
  3755. GIProbeDebugPushConstant push_constant;
  3756. push_constant.alpha = p_alpha;
  3757. push_constant.dynamic_range = storage->gi_probe_get_dynamic_range(gi_probe->probe);
  3758. push_constant.cell_offset = gi_probe->mipmaps[level].cell_offset;
  3759. push_constant.level = level;
  3760. push_constant.bounds[0] = octree_size.x >> level;
  3761. push_constant.bounds[1] = octree_size.y >> level;
  3762. push_constant.bounds[2] = octree_size.z >> level;
  3763. push_constant.pad = 0;
  3764. for (int i = 0; i < 4; i++) {
  3765. for (int j = 0; j < 4; j++) {
  3766. push_constant.projection[i * 4 + j] = transform.matrix[i][j];
  3767. }
  3768. }
  3769. if (giprobe_debug_uniform_set.is_valid()) {
  3770. RD::get_singleton()->free(giprobe_debug_uniform_set);
  3771. }
  3772. Vector<RD::Uniform> uniforms;
  3773. {
  3774. RD::Uniform u;
  3775. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3776. u.binding = 1;
  3777. u.ids.push_back(storage->gi_probe_get_data_buffer(gi_probe->probe));
  3778. uniforms.push_back(u);
  3779. }
  3780. {
  3781. RD::Uniform u;
  3782. u.type = RD::UNIFORM_TYPE_TEXTURE;
  3783. u.binding = 2;
  3784. u.ids.push_back(gi_probe->texture);
  3785. uniforms.push_back(u);
  3786. }
  3787. {
  3788. RD::Uniform u;
  3789. u.type = RD::UNIFORM_TYPE_SAMPLER;
  3790. u.binding = 3;
  3791. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  3792. uniforms.push_back(u);
  3793. }
  3794. int cell_count;
  3795. if (!p_emission && p_lighting && gi_probe->has_dynamic_object_data) {
  3796. cell_count = push_constant.bounds[0] * push_constant.bounds[1] * push_constant.bounds[2];
  3797. } else {
  3798. cell_count = gi_probe->mipmaps[level].cell_count;
  3799. }
  3800. giprobe_debug_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_debug_shader_version_shaders[0], 0);
  3801. 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)));
  3802. RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, giprobe_debug_uniform_set, 0);
  3803. RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(GIProbeDebugPushConstant));
  3804. RD::get_singleton()->draw_list_draw(p_draw_list, false, cell_count, 36);
  3805. }
  3806. void RasterizerSceneRD::_debug_sdfgi_probes(RID p_render_buffers, RD::DrawListID p_draw_list, RID p_framebuffer, const CameraMatrix &p_camera_with_transform) {
  3807. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  3808. ERR_FAIL_COND(!rb);
  3809. if (!rb->sdfgi) {
  3810. return; //nothing to debug
  3811. }
  3812. SDGIShader::DebugProbesPushConstant push_constant;
  3813. for (int i = 0; i < 4; i++) {
  3814. for (int j = 0; j < 4; j++) {
  3815. push_constant.projection[i * 4 + j] = p_camera_with_transform.matrix[i][j];
  3816. }
  3817. }
  3818. //gen spheres from strips
  3819. uint32_t band_points = 16;
  3820. push_constant.band_power = 4;
  3821. push_constant.sections_in_band = ((band_points / 2) - 1);
  3822. push_constant.band_mask = band_points - 2;
  3823. push_constant.section_arc = (Math_PI * 2.0) / float(push_constant.sections_in_band);
  3824. push_constant.y_mult = rb->sdfgi->y_mult;
  3825. uint32_t total_points = push_constant.sections_in_band * band_points;
  3826. uint32_t total_probes = rb->sdfgi->probe_axis_count * rb->sdfgi->probe_axis_count * rb->sdfgi->probe_axis_count;
  3827. push_constant.grid_size[0] = rb->sdfgi->cascade_size;
  3828. push_constant.grid_size[1] = rb->sdfgi->cascade_size;
  3829. push_constant.grid_size[2] = rb->sdfgi->cascade_size;
  3830. push_constant.cascade = 0;
  3831. push_constant.probe_axis_size = rb->sdfgi->probe_axis_count;
  3832. if (!rb->sdfgi->debug_probes_uniform_set.is_valid() || !RD::get_singleton()->uniform_set_is_valid(rb->sdfgi->debug_probes_uniform_set)) {
  3833. Vector<RD::Uniform> uniforms;
  3834. {
  3835. RD::Uniform u;
  3836. u.binding = 1;
  3837. u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  3838. u.ids.push_back(rb->sdfgi->cascades_ubo);
  3839. uniforms.push_back(u);
  3840. }
  3841. {
  3842. RD::Uniform u;
  3843. u.binding = 2;
  3844. u.type = RD::UNIFORM_TYPE_TEXTURE;
  3845. u.ids.push_back(rb->sdfgi->lightprobe_texture);
  3846. uniforms.push_back(u);
  3847. }
  3848. {
  3849. RD::Uniform u;
  3850. u.binding = 3;
  3851. u.type = RD::UNIFORM_TYPE_SAMPLER;
  3852. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  3853. uniforms.push_back(u);
  3854. }
  3855. {
  3856. RD::Uniform u;
  3857. u.binding = 4;
  3858. u.type = RD::UNIFORM_TYPE_TEXTURE;
  3859. u.ids.push_back(rb->sdfgi->occlusion_texture);
  3860. uniforms.push_back(u);
  3861. }
  3862. 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);
  3863. }
  3864. 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)));
  3865. RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, rb->sdfgi->debug_probes_uniform_set, 0);
  3866. RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(SDGIShader::DebugProbesPushConstant));
  3867. RD::get_singleton()->draw_list_draw(p_draw_list, false, total_probes, total_points);
  3868. if (sdfgi_debug_probe_dir != Vector3()) {
  3869. print_line("CLICK DEBUG ME?");
  3870. uint32_t cascade = 0;
  3871. 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);
  3872. 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);
  3873. Vector3 ray_from = sdfgi_debug_probe_pos;
  3874. 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;
  3875. float sphere_radius = 0.2;
  3876. float closest_dist = 1e20;
  3877. sdfgi_debug_probe_enabled = false;
  3878. Vector3i probe_from = rb->sdfgi->cascades[cascade].position / (rb->sdfgi->cascade_size / SDFGI::PROBE_DIVISOR);
  3879. for (int i = 0; i < (SDFGI::PROBE_DIVISOR + 1); i++) {
  3880. for (int j = 0; j < (SDFGI::PROBE_DIVISOR + 1); j++) {
  3881. for (int k = 0; k < (SDFGI::PROBE_DIVISOR + 1); k++) {
  3882. Vector3 pos = offset + probe_size * Vector3(i, j, k);
  3883. Vector3 res;
  3884. if (Geometry3D::segment_intersects_sphere(ray_from, ray_to, pos, sphere_radius, &res)) {
  3885. float d = ray_from.distance_to(res);
  3886. if (d < closest_dist) {
  3887. closest_dist = d;
  3888. sdfgi_debug_probe_enabled = true;
  3889. sdfgi_debug_probe_index = probe_from + Vector3i(i, j, k);
  3890. }
  3891. }
  3892. }
  3893. }
  3894. }
  3895. if (sdfgi_debug_probe_enabled) {
  3896. print_line("found: " + sdfgi_debug_probe_index);
  3897. } else {
  3898. print_line("no found");
  3899. }
  3900. sdfgi_debug_probe_dir = Vector3();
  3901. }
  3902. if (sdfgi_debug_probe_enabled) {
  3903. uint32_t cascade = 0;
  3904. uint32_t probe_cells = (rb->sdfgi->cascade_size / SDFGI::PROBE_DIVISOR);
  3905. Vector3i probe_from = rb->sdfgi->cascades[cascade].position / probe_cells;
  3906. Vector3i ofs = sdfgi_debug_probe_index - probe_from;
  3907. if (ofs.x < 0 || ofs.y < 0 || ofs.z < 0) {
  3908. return;
  3909. }
  3910. if (ofs.x > SDFGI::PROBE_DIVISOR || ofs.y > SDFGI::PROBE_DIVISOR || ofs.z > SDFGI::PROBE_DIVISOR) {
  3911. return;
  3912. }
  3913. uint32_t mult = (SDFGI::PROBE_DIVISOR + 1);
  3914. uint32_t index = ofs.z * mult * mult + ofs.y * mult + ofs.x;
  3915. push_constant.probe_debug_index = index;
  3916. uint32_t cell_count = probe_cells * 2 * probe_cells * 2 * probe_cells * 2;
  3917. 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)));
  3918. RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, rb->sdfgi->debug_probes_uniform_set, 0);
  3919. RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(SDGIShader::DebugProbesPushConstant));
  3920. RD::get_singleton()->draw_list_draw(p_draw_list, false, cell_count, total_points);
  3921. }
  3922. }
  3923. ////////////////////////////////
  3924. RID RasterizerSceneRD::render_buffers_create() {
  3925. RenderBuffers rb;
  3926. rb.data = _create_render_buffer_data();
  3927. return render_buffers_owner.make_rid(rb);
  3928. }
  3929. void RasterizerSceneRD::_allocate_blur_textures(RenderBuffers *rb) {
  3930. ERR_FAIL_COND(!rb->blur[0].texture.is_null());
  3931. uint32_t mipmaps_required = Image::get_image_required_mipmaps(rb->width, rb->height, Image::FORMAT_RGBAH);
  3932. RD::TextureFormat tf;
  3933. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  3934. tf.width = rb->width;
  3935. tf.height = rb->height;
  3936. tf.type = RD::TEXTURE_TYPE_2D;
  3937. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  3938. tf.mipmaps = mipmaps_required;
  3939. rb->blur[0].texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  3940. //the second one is smaller (only used for separatable part of blur)
  3941. tf.width >>= 1;
  3942. tf.height >>= 1;
  3943. tf.mipmaps--;
  3944. rb->blur[1].texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  3945. int base_width = rb->width;
  3946. int base_height = rb->height;
  3947. for (uint32_t i = 0; i < mipmaps_required; i++) {
  3948. RenderBuffers::Blur::Mipmap mm;
  3949. mm.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rb->blur[0].texture, 0, i);
  3950. mm.width = base_width;
  3951. mm.height = base_height;
  3952. rb->blur[0].mipmaps.push_back(mm);
  3953. if (i > 0) {
  3954. mm.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rb->blur[1].texture, 0, i - 1);
  3955. rb->blur[1].mipmaps.push_back(mm);
  3956. }
  3957. base_width = MAX(1, base_width >> 1);
  3958. base_height = MAX(1, base_height >> 1);
  3959. }
  3960. }
  3961. void RasterizerSceneRD::_allocate_luminance_textures(RenderBuffers *rb) {
  3962. ERR_FAIL_COND(!rb->luminance.current.is_null());
  3963. int w = rb->width;
  3964. int h = rb->height;
  3965. while (true) {
  3966. w = MAX(w / 8, 1);
  3967. h = MAX(h / 8, 1);
  3968. RD::TextureFormat tf;
  3969. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  3970. tf.width = w;
  3971. tf.height = h;
  3972. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  3973. bool final = w == 1 && h == 1;
  3974. if (final) {
  3975. tf.usage_bits |= RD::TEXTURE_USAGE_SAMPLING_BIT;
  3976. }
  3977. RID texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  3978. rb->luminance.reduce.push_back(texture);
  3979. if (final) {
  3980. rb->luminance.current = RD::get_singleton()->texture_create(tf, RD::TextureView());
  3981. break;
  3982. }
  3983. }
  3984. }
  3985. void RasterizerSceneRD::_free_render_buffer_data(RenderBuffers *rb) {
  3986. if (rb->texture.is_valid()) {
  3987. RD::get_singleton()->free(rb->texture);
  3988. rb->texture = RID();
  3989. }
  3990. if (rb->depth_texture.is_valid()) {
  3991. RD::get_singleton()->free(rb->depth_texture);
  3992. rb->depth_texture = RID();
  3993. }
  3994. for (int i = 0; i < 2; i++) {
  3995. if (rb->blur[i].texture.is_valid()) {
  3996. RD::get_singleton()->free(rb->blur[i].texture);
  3997. rb->blur[i].texture = RID();
  3998. rb->blur[i].mipmaps.clear();
  3999. }
  4000. }
  4001. for (int i = 0; i < rb->luminance.reduce.size(); i++) {
  4002. RD::get_singleton()->free(rb->luminance.reduce[i]);
  4003. }
  4004. for (int i = 0; i < rb->luminance.reduce.size(); i++) {
  4005. RD::get_singleton()->free(rb->luminance.reduce[i]);
  4006. }
  4007. rb->luminance.reduce.clear();
  4008. if (rb->luminance.current.is_valid()) {
  4009. RD::get_singleton()->free(rb->luminance.current);
  4010. rb->luminance.current = RID();
  4011. }
  4012. if (rb->ssao.ao[0].is_valid()) {
  4013. RD::get_singleton()->free(rb->ssao.depth);
  4014. RD::get_singleton()->free(rb->ssao.ao[0]);
  4015. if (rb->ssao.ao[1].is_valid()) {
  4016. RD::get_singleton()->free(rb->ssao.ao[1]);
  4017. }
  4018. if (rb->ssao.ao_full.is_valid()) {
  4019. RD::get_singleton()->free(rb->ssao.ao_full);
  4020. }
  4021. rb->ssao.depth = RID();
  4022. rb->ssao.ao[0] = RID();
  4023. rb->ssao.ao[1] = RID();
  4024. rb->ssao.ao_full = RID();
  4025. rb->ssao.depth_slices.clear();
  4026. }
  4027. if (rb->ssr.blur_radius[0].is_valid()) {
  4028. RD::get_singleton()->free(rb->ssr.blur_radius[0]);
  4029. RD::get_singleton()->free(rb->ssr.blur_radius[1]);
  4030. rb->ssr.blur_radius[0] = RID();
  4031. rb->ssr.blur_radius[1] = RID();
  4032. }
  4033. if (rb->ssr.depth_scaled.is_valid()) {
  4034. RD::get_singleton()->free(rb->ssr.depth_scaled);
  4035. rb->ssr.depth_scaled = RID();
  4036. RD::get_singleton()->free(rb->ssr.normal_scaled);
  4037. rb->ssr.normal_scaled = RID();
  4038. }
  4039. }
  4040. void RasterizerSceneRD::_process_sss(RID p_render_buffers, const CameraMatrix &p_camera) {
  4041. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4042. ERR_FAIL_COND(!rb);
  4043. bool can_use_effects = rb->width >= 8 && rb->height >= 8;
  4044. if (!can_use_effects) {
  4045. //just copy
  4046. return;
  4047. }
  4048. if (rb->blur[0].texture.is_null()) {
  4049. _allocate_blur_textures(rb);
  4050. _render_buffers_uniform_set_changed(p_render_buffers);
  4051. }
  4052. 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);
  4053. }
  4054. void RasterizerSceneRD::_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) {
  4055. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4056. ERR_FAIL_COND(!rb);
  4057. bool can_use_effects = rb->width >= 8 && rb->height >= 8;
  4058. if (!can_use_effects) {
  4059. //just copy
  4060. storage->get_effects()->merge_specular(p_dest_framebuffer, p_specular_buffer, p_use_additive ? RID() : rb->texture, RID());
  4061. return;
  4062. }
  4063. Environent *env = environment_owner.getornull(p_environment);
  4064. ERR_FAIL_COND(!env);
  4065. ERR_FAIL_COND(!env->ssr_enabled);
  4066. if (rb->ssr.depth_scaled.is_null()) {
  4067. RD::TextureFormat tf;
  4068. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  4069. tf.width = rb->width / 2;
  4070. tf.height = rb->height / 2;
  4071. tf.type = RD::TEXTURE_TYPE_2D;
  4072. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT;
  4073. rb->ssr.depth_scaled = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4074. tf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  4075. rb->ssr.normal_scaled = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4076. }
  4077. if (ssr_roughness_quality != RS::ENV_SSR_ROUGNESS_QUALITY_DISABLED && !rb->ssr.blur_radius[0].is_valid()) {
  4078. RD::TextureFormat tf;
  4079. tf.format = RD::DATA_FORMAT_R8_UNORM;
  4080. tf.width = rb->width / 2;
  4081. tf.height = rb->height / 2;
  4082. tf.type = RD::TEXTURE_TYPE_2D;
  4083. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
  4084. rb->ssr.blur_radius[0] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4085. rb->ssr.blur_radius[1] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4086. }
  4087. if (rb->blur[0].texture.is_null()) {
  4088. _allocate_blur_textures(rb);
  4089. _render_buffers_uniform_set_changed(p_render_buffers);
  4090. }
  4091. 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);
  4092. storage->get_effects()->merge_specular(p_dest_framebuffer, p_specular_buffer, p_use_additive ? RID() : rb->texture, rb->blur[0].mipmaps[1].texture);
  4093. }
  4094. void RasterizerSceneRD::_process_ssao(RID p_render_buffers, RID p_environment, RID p_normal_buffer, const CameraMatrix &p_projection) {
  4095. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4096. ERR_FAIL_COND(!rb);
  4097. Environent *env = environment_owner.getornull(p_environment);
  4098. ERR_FAIL_COND(!env);
  4099. if (rb->ssao.ao[0].is_valid() && rb->ssao.ao_full.is_valid() != ssao_half_size) {
  4100. RD::get_singleton()->free(rb->ssao.depth);
  4101. RD::get_singleton()->free(rb->ssao.ao[0]);
  4102. if (rb->ssao.ao[1].is_valid()) {
  4103. RD::get_singleton()->free(rb->ssao.ao[1]);
  4104. }
  4105. if (rb->ssao.ao_full.is_valid()) {
  4106. RD::get_singleton()->free(rb->ssao.ao_full);
  4107. }
  4108. rb->ssao.depth = RID();
  4109. rb->ssao.ao[0] = RID();
  4110. rb->ssao.ao[1] = RID();
  4111. rb->ssao.ao_full = RID();
  4112. rb->ssao.depth_slices.clear();
  4113. }
  4114. if (!rb->ssao.ao[0].is_valid()) {
  4115. //allocate depth slices
  4116. {
  4117. RD::TextureFormat tf;
  4118. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  4119. tf.width = rb->width / 2;
  4120. tf.height = rb->height / 2;
  4121. tf.mipmaps = Image::get_image_required_mipmaps(tf.width, tf.height, Image::FORMAT_RF) + 1;
  4122. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  4123. rb->ssao.depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4124. for (uint32_t i = 0; i < tf.mipmaps; i++) {
  4125. RID slice = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rb->ssao.depth, 0, i);
  4126. rb->ssao.depth_slices.push_back(slice);
  4127. }
  4128. }
  4129. {
  4130. RD::TextureFormat tf;
  4131. tf.format = RD::DATA_FORMAT_R8_UNORM;
  4132. tf.width = ssao_half_size ? rb->width / 2 : rb->width;
  4133. tf.height = ssao_half_size ? rb->height / 2 : rb->height;
  4134. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  4135. rb->ssao.ao[0] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4136. rb->ssao.ao[1] = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4137. }
  4138. if (ssao_half_size) {
  4139. //upsample texture
  4140. RD::TextureFormat tf;
  4141. tf.format = RD::DATA_FORMAT_R8_UNORM;
  4142. tf.width = rb->width;
  4143. tf.height = rb->height;
  4144. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  4145. rb->ssao.ao_full = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4146. }
  4147. _render_buffers_uniform_set_changed(p_render_buffers);
  4148. }
  4149. storage->get_effects()->generate_ssao(rb->depth_texture, p_normal_buffer, Size2i(rb->width, rb->height), rb->ssao.depth, rb->ssao.depth_slices, rb->ssao.ao[0], rb->ssao.ao_full.is_valid(), rb->ssao.ao[1], rb->ssao.ao_full, env->ssao_intensity, env->ssao_radius, env->ssao_bias, p_projection, ssao_quality, env->ssao_blur, env->ssao_blur_edge_sharpness);
  4150. }
  4151. void RasterizerSceneRD::_render_buffers_post_process_and_tonemap(RID p_render_buffers, RID p_environment, RID p_camera_effects, const CameraMatrix &p_projection) {
  4152. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4153. ERR_FAIL_COND(!rb);
  4154. Environent *env = environment_owner.getornull(p_environment);
  4155. //glow (if enabled)
  4156. CameraEffects *camfx = camera_effects_owner.getornull(p_camera_effects);
  4157. bool can_use_effects = rb->width >= 8 && rb->height >= 8;
  4158. if (can_use_effects && camfx && (camfx->dof_blur_near_enabled || camfx->dof_blur_far_enabled) && camfx->dof_blur_amount > 0.0) {
  4159. if (rb->blur[0].texture.is_null()) {
  4160. _allocate_blur_textures(rb);
  4161. _render_buffers_uniform_set_changed(p_render_buffers);
  4162. }
  4163. float bokeh_size = camfx->dof_blur_amount * 64.0;
  4164. 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());
  4165. }
  4166. if (can_use_effects && env && env->auto_exposure) {
  4167. if (rb->luminance.current.is_null()) {
  4168. _allocate_luminance_textures(rb);
  4169. _render_buffers_uniform_set_changed(p_render_buffers);
  4170. }
  4171. bool set_immediate = env->auto_exposure_version != rb->auto_exposure_version;
  4172. rb->auto_exposure_version = env->auto_exposure_version;
  4173. double step = env->auto_exp_speed * time_step;
  4174. 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);
  4175. //swap final reduce with prev luminance
  4176. SWAP(rb->luminance.current, rb->luminance.reduce.write[rb->luminance.reduce.size() - 1]);
  4177. RenderingServerRaster::redraw_request(); //redraw all the time if auto exposure rendering is on
  4178. }
  4179. int max_glow_level = -1;
  4180. int glow_mask = 0;
  4181. if (can_use_effects && env && env->glow_enabled) {
  4182. /* see that blur textures are allocated */
  4183. if (rb->blur[0].texture.is_null()) {
  4184. _allocate_blur_textures(rb);
  4185. _render_buffers_uniform_set_changed(p_render_buffers);
  4186. }
  4187. for (int i = 0; i < RS::MAX_GLOW_LEVELS; i++) {
  4188. if (env->glow_levels & (1 << i)) {
  4189. if (i >= rb->blur[1].mipmaps.size()) {
  4190. max_glow_level = rb->blur[1].mipmaps.size() - 1;
  4191. glow_mask |= 1 << max_glow_level;
  4192. } else {
  4193. max_glow_level = i;
  4194. glow_mask |= (1 << i);
  4195. }
  4196. }
  4197. }
  4198. for (int i = 0; i < (max_glow_level + 1); i++) {
  4199. int vp_w = rb->blur[1].mipmaps[i].width;
  4200. int vp_h = rb->blur[1].mipmaps[i].height;
  4201. if (i == 0) {
  4202. RID luminance_texture;
  4203. if (env->auto_exposure && rb->luminance.current.is_valid()) {
  4204. luminance_texture = rb->luminance.current;
  4205. }
  4206. storage->get_effects()->gaussian_glow(rb->texture, rb->blur[0].mipmaps[i + 1].texture, rb->blur[1].mipmaps[i].texture, Size2i(vp_w, vp_h), env->glow_strength, 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);
  4207. } else {
  4208. storage->get_effects()->gaussian_glow(rb->blur[1].mipmaps[i - 1].texture, rb->blur[0].mipmaps[i + 1].texture, rb->blur[1].mipmaps[i].texture, Size2i(vp_w, vp_h), env->glow_strength);
  4209. }
  4210. }
  4211. }
  4212. {
  4213. //tonemap
  4214. RasterizerEffectsRD::TonemapSettings tonemap;
  4215. tonemap.color_correction_texture = storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE);
  4216. if (can_use_effects && env && env->auto_exposure && rb->luminance.current.is_valid()) {
  4217. tonemap.use_auto_exposure = true;
  4218. tonemap.exposure_texture = rb->luminance.current;
  4219. tonemap.auto_exposure_grey = env->auto_exp_scale;
  4220. } else {
  4221. tonemap.exposure_texture = storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_WHITE);
  4222. }
  4223. if (can_use_effects && env && env->glow_enabled) {
  4224. tonemap.use_glow = true;
  4225. tonemap.glow_mode = RasterizerEffectsRD::TonemapSettings::GlowMode(env->glow_blend_mode);
  4226. tonemap.glow_intensity = env->glow_blend_mode == RS::ENV_GLOW_BLEND_MODE_MIX ? env->glow_mix : env->glow_intensity;
  4227. tonemap.glow_level_flags = glow_mask;
  4228. tonemap.glow_texture_size.x = rb->blur[1].mipmaps[0].width;
  4229. tonemap.glow_texture_size.y = rb->blur[1].mipmaps[0].height;
  4230. tonemap.glow_use_bicubic_upscale = glow_bicubic_upscale;
  4231. tonemap.glow_texture = rb->blur[1].texture;
  4232. } else {
  4233. tonemap.glow_texture = storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_BLACK);
  4234. }
  4235. if (rb->screen_space_aa == RS::VIEWPORT_SCREEN_SPACE_AA_FXAA) {
  4236. tonemap.use_fxaa = true;
  4237. }
  4238. tonemap.texture_size = Vector2i(rb->width, rb->height);
  4239. if (env) {
  4240. tonemap.tonemap_mode = env->tone_mapper;
  4241. tonemap.white = env->white;
  4242. tonemap.exposure = env->exposure;
  4243. }
  4244. storage->get_effects()->tonemapper(rb->texture, storage->render_target_get_rd_framebuffer(rb->render_target), tonemap);
  4245. }
  4246. storage->render_target_disable_clear_request(rb->render_target);
  4247. }
  4248. void RasterizerSceneRD::_render_buffers_debug_draw(RID p_render_buffers, RID p_shadow_atlas) {
  4249. RasterizerEffectsRD *effects = storage->get_effects();
  4250. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4251. ERR_FAIL_COND(!rb);
  4252. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_SHADOW_ATLAS) {
  4253. if (p_shadow_atlas.is_valid()) {
  4254. RID shadow_atlas_texture = shadow_atlas_get_texture(p_shadow_atlas);
  4255. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  4256. effects->copy_to_fb_rect(shadow_atlas_texture, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2i(Vector2(), rtsize / 2), false, true);
  4257. }
  4258. }
  4259. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS) {
  4260. if (directional_shadow_get_texture().is_valid()) {
  4261. RID shadow_atlas_texture = directional_shadow_get_texture();
  4262. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  4263. effects->copy_to_fb_rect(shadow_atlas_texture, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2i(Vector2(), rtsize / 2), false, true);
  4264. }
  4265. }
  4266. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_DECAL_ATLAS) {
  4267. RID decal_atlas = storage->decal_atlas_get_texture();
  4268. if (decal_atlas.is_valid()) {
  4269. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  4270. effects->copy_to_fb_rect(decal_atlas, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2i(Vector2(), rtsize / 2), false, false, true);
  4271. }
  4272. }
  4273. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_SCENE_LUMINANCE) {
  4274. if (rb->luminance.current.is_valid()) {
  4275. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  4276. effects->copy_to_fb_rect(rb->luminance.current, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize / 8), false, true);
  4277. }
  4278. }
  4279. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_SSAO && rb->ssao.ao[0].is_valid()) {
  4280. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  4281. RID ao_buf = rb->ssao.ao_full.is_valid() ? rb->ssao.ao_full : rb->ssao.ao[0];
  4282. effects->copy_to_fb_rect(ao_buf, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize), false, true);
  4283. }
  4284. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_NORMAL_BUFFER && _render_buffers_get_normal_texture(p_render_buffers).is_valid()) {
  4285. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  4286. 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);
  4287. }
  4288. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_GI_BUFFER && _render_buffers_get_ambient_texture(p_render_buffers).is_valid()) {
  4289. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  4290. RID ambient_texture = _render_buffers_get_ambient_texture(p_render_buffers);
  4291. RID reflection_texture = _render_buffers_get_reflection_texture(p_render_buffers);
  4292. 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);
  4293. }
  4294. }
  4295. void RasterizerSceneRD::_sdfgi_debug_draw(RID p_render_buffers, const CameraMatrix &p_projection, const Transform &p_transform) {
  4296. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4297. ERR_FAIL_COND(!rb);
  4298. if (!rb->sdfgi) {
  4299. return; //eh
  4300. }
  4301. if (!rb->sdfgi->debug_uniform_set.is_valid() || !RD::get_singleton()->uniform_set_is_valid(rb->sdfgi->debug_uniform_set)) {
  4302. Vector<RD::Uniform> uniforms;
  4303. {
  4304. RD::Uniform u;
  4305. u.binding = 1;
  4306. u.type = RD::UNIFORM_TYPE_TEXTURE;
  4307. for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
  4308. if (i < rb->sdfgi->cascades.size()) {
  4309. u.ids.push_back(rb->sdfgi->cascades[i].sdf_tex);
  4310. } else {
  4311. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  4312. }
  4313. }
  4314. uniforms.push_back(u);
  4315. }
  4316. {
  4317. RD::Uniform u;
  4318. u.binding = 2;
  4319. u.type = RD::UNIFORM_TYPE_TEXTURE;
  4320. for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
  4321. if (i < rb->sdfgi->cascades.size()) {
  4322. u.ids.push_back(rb->sdfgi->cascades[i].light_tex);
  4323. } else {
  4324. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  4325. }
  4326. }
  4327. uniforms.push_back(u);
  4328. }
  4329. {
  4330. RD::Uniform u;
  4331. u.binding = 3;
  4332. u.type = RD::UNIFORM_TYPE_TEXTURE;
  4333. for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
  4334. if (i < rb->sdfgi->cascades.size()) {
  4335. u.ids.push_back(rb->sdfgi->cascades[i].light_aniso_0_tex);
  4336. } else {
  4337. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  4338. }
  4339. }
  4340. uniforms.push_back(u);
  4341. }
  4342. {
  4343. RD::Uniform u;
  4344. u.binding = 4;
  4345. u.type = RD::UNIFORM_TYPE_TEXTURE;
  4346. for (uint32_t i = 0; i < SDFGI::MAX_CASCADES; i++) {
  4347. if (i < rb->sdfgi->cascades.size()) {
  4348. u.ids.push_back(rb->sdfgi->cascades[i].light_aniso_1_tex);
  4349. } else {
  4350. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  4351. }
  4352. }
  4353. uniforms.push_back(u);
  4354. }
  4355. {
  4356. RD::Uniform u;
  4357. u.binding = 5;
  4358. u.type = RD::UNIFORM_TYPE_TEXTURE;
  4359. u.ids.push_back(rb->sdfgi->occlusion_texture);
  4360. uniforms.push_back(u);
  4361. }
  4362. {
  4363. RD::Uniform u;
  4364. u.binding = 8;
  4365. u.type = RD::UNIFORM_TYPE_SAMPLER;
  4366. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  4367. uniforms.push_back(u);
  4368. }
  4369. {
  4370. RD::Uniform u;
  4371. u.binding = 9;
  4372. u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  4373. u.ids.push_back(rb->sdfgi->cascades_ubo);
  4374. uniforms.push_back(u);
  4375. }
  4376. {
  4377. RD::Uniform u;
  4378. u.binding = 10;
  4379. u.type = RD::UNIFORM_TYPE_IMAGE;
  4380. u.ids.push_back(rb->texture);
  4381. uniforms.push_back(u);
  4382. }
  4383. {
  4384. RD::Uniform u;
  4385. u.binding = 11;
  4386. u.type = RD::UNIFORM_TYPE_TEXTURE;
  4387. u.ids.push_back(rb->sdfgi->lightprobe_texture);
  4388. uniforms.push_back(u);
  4389. }
  4390. rb->sdfgi->debug_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sdfgi_shader.debug_shader_version, 0);
  4391. }
  4392. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  4393. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.debug_pipeline);
  4394. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->debug_uniform_set, 0);
  4395. SDGIShader::DebugPushConstant push_constant;
  4396. push_constant.grid_size[0] = rb->sdfgi->cascade_size;
  4397. push_constant.grid_size[1] = rb->sdfgi->cascade_size;
  4398. push_constant.grid_size[2] = rb->sdfgi->cascade_size;
  4399. push_constant.max_cascades = rb->sdfgi->cascades.size();
  4400. push_constant.screen_size[0] = rb->width;
  4401. push_constant.screen_size[1] = rb->height;
  4402. push_constant.probe_axis_size = rb->sdfgi->probe_axis_count;
  4403. push_constant.use_occlusion = rb->sdfgi->uses_occlusion;
  4404. push_constant.y_mult = rb->sdfgi->y_mult;
  4405. Vector2 vp_half = p_projection.get_viewport_half_extents();
  4406. push_constant.cam_extent[0] = vp_half.x;
  4407. push_constant.cam_extent[1] = vp_half.y;
  4408. push_constant.cam_extent[2] = -p_projection.get_z_near();
  4409. push_constant.cam_transform[0] = p_transform.basis.elements[0][0];
  4410. push_constant.cam_transform[1] = p_transform.basis.elements[1][0];
  4411. push_constant.cam_transform[2] = p_transform.basis.elements[2][0];
  4412. push_constant.cam_transform[3] = 0;
  4413. push_constant.cam_transform[4] = p_transform.basis.elements[0][1];
  4414. push_constant.cam_transform[5] = p_transform.basis.elements[1][1];
  4415. push_constant.cam_transform[6] = p_transform.basis.elements[2][1];
  4416. push_constant.cam_transform[7] = 0;
  4417. push_constant.cam_transform[8] = p_transform.basis.elements[0][2];
  4418. push_constant.cam_transform[9] = p_transform.basis.elements[1][2];
  4419. push_constant.cam_transform[10] = p_transform.basis.elements[2][2];
  4420. push_constant.cam_transform[11] = 0;
  4421. push_constant.cam_transform[12] = p_transform.origin.x;
  4422. push_constant.cam_transform[13] = p_transform.origin.y;
  4423. push_constant.cam_transform[14] = p_transform.origin.z;
  4424. push_constant.cam_transform[15] = 1;
  4425. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::DebugPushConstant));
  4426. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->width, rb->height, 1, 8, 8, 1);
  4427. RD::get_singleton()->compute_list_end();
  4428. Size2 rtsize = storage->render_target_get_size(rb->render_target);
  4429. storage->get_effects()->copy_to_fb_rect(rb->texture, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize), true);
  4430. }
  4431. RID RasterizerSceneRD::render_buffers_get_back_buffer_texture(RID p_render_buffers) {
  4432. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4433. ERR_FAIL_COND_V(!rb, RID());
  4434. if (!rb->blur[0].texture.is_valid()) {
  4435. return RID(); //not valid at the moment
  4436. }
  4437. return rb->blur[0].texture;
  4438. }
  4439. RID RasterizerSceneRD::render_buffers_get_ao_texture(RID p_render_buffers) {
  4440. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4441. ERR_FAIL_COND_V(!rb, RID());
  4442. return rb->ssao.ao_full.is_valid() ? rb->ssao.ao_full : rb->ssao.ao[0];
  4443. }
  4444. RID RasterizerSceneRD::render_buffers_get_gi_probe_buffer(RID p_render_buffers) {
  4445. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4446. ERR_FAIL_COND_V(!rb, RID());
  4447. if (rb->giprobe_buffer.is_null()) {
  4448. rb->giprobe_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(GI::GIProbeData) * RenderBuffers::MAX_GIPROBES);
  4449. }
  4450. return rb->giprobe_buffer;
  4451. }
  4452. RID RasterizerSceneRD::render_buffers_get_default_gi_probe_buffer() {
  4453. return default_giprobe_buffer;
  4454. }
  4455. uint32_t RasterizerSceneRD::render_buffers_get_sdfgi_cascade_count(RID p_render_buffers) const {
  4456. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4457. ERR_FAIL_COND_V(!rb, 0);
  4458. ERR_FAIL_COND_V(!rb->sdfgi, 0);
  4459. return rb->sdfgi->cascades.size();
  4460. }
  4461. bool RasterizerSceneRD::render_buffers_is_sdfgi_enabled(RID p_render_buffers) const {
  4462. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4463. ERR_FAIL_COND_V(!rb, false);
  4464. return rb->sdfgi != nullptr;
  4465. }
  4466. RID RasterizerSceneRD::render_buffers_get_sdfgi_irradiance_probes(RID p_render_buffers) const {
  4467. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4468. ERR_FAIL_COND_V(!rb, RID());
  4469. ERR_FAIL_COND_V(!rb->sdfgi, RID());
  4470. return rb->sdfgi->lightprobe_texture;
  4471. }
  4472. Vector3 RasterizerSceneRD::render_buffers_get_sdfgi_cascade_offset(RID p_render_buffers, uint32_t p_cascade) const {
  4473. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4474. ERR_FAIL_COND_V(!rb, Vector3());
  4475. ERR_FAIL_COND_V(!rb->sdfgi, Vector3());
  4476. ERR_FAIL_UNSIGNED_INDEX_V(p_cascade, rb->sdfgi->cascades.size(), Vector3());
  4477. 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;
  4478. }
  4479. Vector3i RasterizerSceneRD::render_buffers_get_sdfgi_cascade_probe_offset(RID p_render_buffers, uint32_t p_cascade) const {
  4480. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4481. ERR_FAIL_COND_V(!rb, Vector3i());
  4482. ERR_FAIL_COND_V(!rb->sdfgi, Vector3i());
  4483. ERR_FAIL_UNSIGNED_INDEX_V(p_cascade, rb->sdfgi->cascades.size(), Vector3i());
  4484. int32_t probe_divisor = rb->sdfgi->cascade_size / SDFGI::PROBE_DIVISOR;
  4485. return rb->sdfgi->cascades[p_cascade].position / probe_divisor;
  4486. }
  4487. float RasterizerSceneRD::render_buffers_get_sdfgi_normal_bias(RID p_render_buffers) const {
  4488. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4489. ERR_FAIL_COND_V(!rb, 0);
  4490. ERR_FAIL_COND_V(!rb->sdfgi, 0);
  4491. return rb->sdfgi->normal_bias;
  4492. }
  4493. float RasterizerSceneRD::render_buffers_get_sdfgi_cascade_probe_size(RID p_render_buffers, uint32_t p_cascade) const {
  4494. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4495. ERR_FAIL_COND_V(!rb, 0);
  4496. ERR_FAIL_COND_V(!rb->sdfgi, 0);
  4497. ERR_FAIL_UNSIGNED_INDEX_V(p_cascade, rb->sdfgi->cascades.size(), 0);
  4498. return float(rb->sdfgi->cascade_size) * rb->sdfgi->cascades[p_cascade].cell_size / float(rb->sdfgi->probe_axis_count - 1);
  4499. }
  4500. uint32_t RasterizerSceneRD::render_buffers_get_sdfgi_cascade_probe_count(RID p_render_buffers) const {
  4501. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4502. ERR_FAIL_COND_V(!rb, 0);
  4503. ERR_FAIL_COND_V(!rb->sdfgi, 0);
  4504. return rb->sdfgi->probe_axis_count;
  4505. }
  4506. uint32_t RasterizerSceneRD::render_buffers_get_sdfgi_cascade_size(RID p_render_buffers) const {
  4507. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4508. ERR_FAIL_COND_V(!rb, 0);
  4509. ERR_FAIL_COND_V(!rb->sdfgi, 0);
  4510. return rb->sdfgi->cascade_size;
  4511. }
  4512. bool RasterizerSceneRD::render_buffers_is_sdfgi_using_occlusion(RID p_render_buffers) const {
  4513. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4514. ERR_FAIL_COND_V(!rb, false);
  4515. ERR_FAIL_COND_V(!rb->sdfgi, false);
  4516. return rb->sdfgi->uses_occlusion;
  4517. }
  4518. float RasterizerSceneRD::render_buffers_get_sdfgi_energy(RID p_render_buffers) const {
  4519. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4520. ERR_FAIL_COND_V(!rb, 0);
  4521. ERR_FAIL_COND_V(!rb->sdfgi, false);
  4522. return rb->sdfgi->energy;
  4523. }
  4524. RID RasterizerSceneRD::render_buffers_get_sdfgi_occlusion_texture(RID p_render_buffers) const {
  4525. const RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4526. ERR_FAIL_COND_V(!rb, RID());
  4527. ERR_FAIL_COND_V(!rb->sdfgi, RID());
  4528. return rb->sdfgi->occlusion_texture;
  4529. }
  4530. void RasterizerSceneRD::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) {
  4531. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4532. rb->width = p_width;
  4533. rb->height = p_height;
  4534. rb->render_target = p_render_target;
  4535. rb->msaa = p_msaa;
  4536. rb->screen_space_aa = p_screen_space_aa;
  4537. _free_render_buffer_data(rb);
  4538. {
  4539. RD::TextureFormat tf;
  4540. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  4541. tf.width = rb->width;
  4542. tf.height = rb->height;
  4543. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  4544. if (rb->msaa != RS::VIEWPORT_MSAA_DISABLED) {
  4545. tf.usage_bits |= RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  4546. } else {
  4547. tf.usage_bits |= RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  4548. }
  4549. rb->texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4550. }
  4551. {
  4552. RD::TextureFormat tf;
  4553. if (rb->msaa == RS::VIEWPORT_MSAA_DISABLED) {
  4554. 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;
  4555. } else {
  4556. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  4557. }
  4558. tf.width = p_width;
  4559. tf.height = p_height;
  4560. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT;
  4561. if (rb->msaa != RS::VIEWPORT_MSAA_DISABLED) {
  4562. tf.usage_bits |= RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  4563. } else {
  4564. tf.usage_bits |= RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  4565. }
  4566. rb->depth_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4567. }
  4568. rb->data->configure(rb->texture, rb->depth_texture, p_width, p_height, p_msaa);
  4569. _render_buffers_uniform_set_changed(p_render_buffers);
  4570. }
  4571. void RasterizerSceneRD::sub_surface_scattering_set_quality(RS::SubSurfaceScatteringQuality p_quality) {
  4572. sss_quality = p_quality;
  4573. }
  4574. RS::SubSurfaceScatteringQuality RasterizerSceneRD::sub_surface_scattering_get_quality() const {
  4575. return sss_quality;
  4576. }
  4577. void RasterizerSceneRD::sub_surface_scattering_set_scale(float p_scale, float p_depth_scale) {
  4578. sss_scale = p_scale;
  4579. sss_depth_scale = p_depth_scale;
  4580. }
  4581. void RasterizerSceneRD::shadows_quality_set(RS::ShadowQuality p_quality) {
  4582. ERR_FAIL_INDEX_MSG(p_quality, RS::SHADOW_QUALITY_MAX, "Shadow quality too high, please see RenderingServer's ShadowQuality enum");
  4583. if (shadows_quality != p_quality) {
  4584. shadows_quality = p_quality;
  4585. switch (shadows_quality) {
  4586. case RS::SHADOW_QUALITY_HARD: {
  4587. penumbra_shadow_samples = 4;
  4588. soft_shadow_samples = 1;
  4589. shadows_quality_radius = 1.0;
  4590. } break;
  4591. case RS::SHADOW_QUALITY_SOFT_LOW: {
  4592. penumbra_shadow_samples = 8;
  4593. soft_shadow_samples = 4;
  4594. shadows_quality_radius = 2.0;
  4595. } break;
  4596. case RS::SHADOW_QUALITY_SOFT_MEDIUM: {
  4597. penumbra_shadow_samples = 12;
  4598. soft_shadow_samples = 8;
  4599. shadows_quality_radius = 2.0;
  4600. } break;
  4601. case RS::SHADOW_QUALITY_SOFT_HIGH: {
  4602. penumbra_shadow_samples = 24;
  4603. soft_shadow_samples = 16;
  4604. shadows_quality_radius = 3.0;
  4605. } break;
  4606. case RS::SHADOW_QUALITY_SOFT_ULTRA: {
  4607. penumbra_shadow_samples = 32;
  4608. soft_shadow_samples = 32;
  4609. shadows_quality_radius = 4.0;
  4610. } break;
  4611. case RS::SHADOW_QUALITY_MAX:
  4612. break;
  4613. }
  4614. get_vogel_disk(penumbra_shadow_kernel, penumbra_shadow_samples);
  4615. get_vogel_disk(soft_shadow_kernel, soft_shadow_samples);
  4616. }
  4617. }
  4618. void RasterizerSceneRD::directional_shadow_quality_set(RS::ShadowQuality p_quality) {
  4619. ERR_FAIL_INDEX_MSG(p_quality, RS::SHADOW_QUALITY_MAX, "Shadow quality too high, please see RenderingServer's ShadowQuality enum");
  4620. if (directional_shadow_quality != p_quality) {
  4621. directional_shadow_quality = p_quality;
  4622. switch (directional_shadow_quality) {
  4623. case RS::SHADOW_QUALITY_HARD: {
  4624. directional_penumbra_shadow_samples = 4;
  4625. directional_soft_shadow_samples = 1;
  4626. directional_shadow_quality_radius = 1.0;
  4627. } break;
  4628. case RS::SHADOW_QUALITY_SOFT_LOW: {
  4629. directional_penumbra_shadow_samples = 8;
  4630. directional_soft_shadow_samples = 4;
  4631. directional_shadow_quality_radius = 2.0;
  4632. } break;
  4633. case RS::SHADOW_QUALITY_SOFT_MEDIUM: {
  4634. directional_penumbra_shadow_samples = 12;
  4635. directional_soft_shadow_samples = 8;
  4636. directional_shadow_quality_radius = 2.0;
  4637. } break;
  4638. case RS::SHADOW_QUALITY_SOFT_HIGH: {
  4639. directional_penumbra_shadow_samples = 24;
  4640. directional_soft_shadow_samples = 16;
  4641. directional_shadow_quality_radius = 3.0;
  4642. } break;
  4643. case RS::SHADOW_QUALITY_SOFT_ULTRA: {
  4644. directional_penumbra_shadow_samples = 32;
  4645. directional_soft_shadow_samples = 32;
  4646. directional_shadow_quality_radius = 4.0;
  4647. } break;
  4648. case RS::SHADOW_QUALITY_MAX:
  4649. break;
  4650. }
  4651. get_vogel_disk(directional_penumbra_shadow_kernel, directional_penumbra_shadow_samples);
  4652. get_vogel_disk(directional_soft_shadow_kernel, directional_soft_shadow_samples);
  4653. }
  4654. }
  4655. int RasterizerSceneRD::get_roughness_layers() const {
  4656. return roughness_layers;
  4657. }
  4658. bool RasterizerSceneRD::is_using_radiance_cubemap_array() const {
  4659. return sky_use_cubemap_array;
  4660. }
  4661. RasterizerSceneRD::RenderBufferData *RasterizerSceneRD::render_buffers_get_data(RID p_render_buffers) {
  4662. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4663. ERR_FAIL_COND_V(!rb, nullptr);
  4664. return rb->data;
  4665. }
  4666. void RasterizerSceneRD::render_scene(RID p_render_buffers, const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID *p_light_cull_result, int p_light_cull_count, RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, RID *p_gi_probe_cull_result, int p_gi_probe_cull_count, RID *p_decal_cull_result, int p_decal_cull_count, InstanceBase **p_lightmap_cull_result, int p_lightmap_cull_count, RID p_environment, RID p_camera_effects, RID p_shadow_atlas, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass) {
  4667. Color clear_color;
  4668. if (p_render_buffers.is_valid()) {
  4669. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4670. ERR_FAIL_COND(!rb);
  4671. clear_color = storage->render_target_get_clear_request_color(rb->render_target);
  4672. } else {
  4673. clear_color = storage->get_default_clear_color();
  4674. }
  4675. //assign render indices to giprobes
  4676. for (int i = 0; i < p_gi_probe_cull_count; i++) {
  4677. GIProbeInstance *giprobe_inst = gi_probe_instance_owner.getornull(p_gi_probe_cull_result[i]);
  4678. if (giprobe_inst) {
  4679. giprobe_inst->render_index = i;
  4680. }
  4681. }
  4682. _render_scene(p_render_buffers, p_cam_transform, p_cam_projection, p_cam_ortogonal, p_cull_result, p_cull_count, p_light_cull_result, p_light_cull_count, p_reflection_probe_cull_result, p_reflection_probe_cull_count, p_gi_probe_cull_result, p_gi_probe_cull_count, p_decal_cull_result, p_decal_cull_count, p_lightmap_cull_result, p_lightmap_cull_count, p_environment, p_camera_effects, p_shadow_atlas, p_reflection_atlas, p_reflection_probe, p_reflection_probe_pass, clear_color);
  4683. if (p_render_buffers.is_valid()) {
  4684. RENDER_TIMESTAMP("Tonemap");
  4685. _render_buffers_post_process_and_tonemap(p_render_buffers, p_environment, p_camera_effects, p_cam_projection);
  4686. _render_buffers_debug_draw(p_render_buffers, p_shadow_atlas);
  4687. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_SDFGI) {
  4688. _sdfgi_debug_draw(p_render_buffers, p_cam_projection, p_cam_transform);
  4689. }
  4690. }
  4691. }
  4692. void RasterizerSceneRD::render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count) {
  4693. LightInstance *light_instance = light_instance_owner.getornull(p_light);
  4694. ERR_FAIL_COND(!light_instance);
  4695. Rect2i atlas_rect;
  4696. RID atlas_texture;
  4697. bool using_dual_paraboloid = false;
  4698. bool using_dual_paraboloid_flip = false;
  4699. float znear = 0;
  4700. float zfar = 0;
  4701. RID render_fb;
  4702. RID render_texture;
  4703. float bias = 0;
  4704. float normal_bias = 0;
  4705. bool use_pancake = false;
  4706. bool use_linear_depth = false;
  4707. bool render_cubemap = false;
  4708. bool finalize_cubemap = false;
  4709. CameraMatrix light_projection;
  4710. Transform light_transform;
  4711. if (storage->light_get_type(light_instance->light) == RS::LIGHT_DIRECTIONAL) {
  4712. //set pssm stuff
  4713. if (light_instance->last_scene_shadow_pass != scene_pass) {
  4714. light_instance->directional_rect = _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, directional_shadow.current_light);
  4715. directional_shadow.current_light++;
  4716. light_instance->last_scene_shadow_pass = scene_pass;
  4717. }
  4718. use_pancake = storage->light_get_param(light_instance->light, RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE) > 0;
  4719. light_projection = light_instance->shadow_transform[p_pass].camera;
  4720. light_transform = light_instance->shadow_transform[p_pass].transform;
  4721. atlas_rect.position.x = light_instance->directional_rect.position.x;
  4722. atlas_rect.position.y = light_instance->directional_rect.position.y;
  4723. atlas_rect.size.width = light_instance->directional_rect.size.x;
  4724. atlas_rect.size.height = light_instance->directional_rect.size.y;
  4725. if (storage->light_directional_get_shadow_mode(light_instance->light) == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) {
  4726. atlas_rect.size.width /= 2;
  4727. atlas_rect.size.height /= 2;
  4728. if (p_pass == 1) {
  4729. atlas_rect.position.x += atlas_rect.size.width;
  4730. } else if (p_pass == 2) {
  4731. atlas_rect.position.y += atlas_rect.size.height;
  4732. } else if (p_pass == 3) {
  4733. atlas_rect.position.x += atlas_rect.size.width;
  4734. atlas_rect.position.y += atlas_rect.size.height;
  4735. }
  4736. } else if (storage->light_directional_get_shadow_mode(light_instance->light) == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) {
  4737. atlas_rect.size.height /= 2;
  4738. if (p_pass == 0) {
  4739. } else {
  4740. atlas_rect.position.y += atlas_rect.size.height;
  4741. }
  4742. }
  4743. light_instance->shadow_transform[p_pass].atlas_rect = atlas_rect;
  4744. light_instance->shadow_transform[p_pass].atlas_rect.position /= directional_shadow.size;
  4745. light_instance->shadow_transform[p_pass].atlas_rect.size /= directional_shadow.size;
  4746. float bias_mult = light_instance->shadow_transform[p_pass].bias_scale;
  4747. zfar = storage->light_get_param(light_instance->light, RS::LIGHT_PARAM_RANGE);
  4748. bias = storage->light_get_param(light_instance->light, RS::LIGHT_PARAM_SHADOW_BIAS) * bias_mult;
  4749. normal_bias = storage->light_get_param(light_instance->light, RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS) * bias_mult;
  4750. ShadowMap *shadow_map = _get_shadow_map(atlas_rect.size);
  4751. render_fb = shadow_map->fb;
  4752. render_texture = shadow_map->depth;
  4753. atlas_texture = directional_shadow.depth;
  4754. } else {
  4755. //set from shadow atlas
  4756. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  4757. ERR_FAIL_COND(!shadow_atlas);
  4758. ERR_FAIL_COND(!shadow_atlas->shadow_owners.has(p_light));
  4759. uint32_t key = shadow_atlas->shadow_owners[p_light];
  4760. uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  4761. uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK;
  4762. ERR_FAIL_INDEX((int)shadow, shadow_atlas->quadrants[quadrant].shadows.size());
  4763. uint32_t quadrant_size = shadow_atlas->size >> 1;
  4764. atlas_rect.position.x = (quadrant & 1) * quadrant_size;
  4765. atlas_rect.position.y = (quadrant >> 1) * quadrant_size;
  4766. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  4767. atlas_rect.position.x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  4768. atlas_rect.position.y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  4769. atlas_rect.size.width = shadow_size;
  4770. atlas_rect.size.height = shadow_size;
  4771. atlas_texture = shadow_atlas->depth;
  4772. zfar = storage->light_get_param(light_instance->light, RS::LIGHT_PARAM_RANGE);
  4773. bias = storage->light_get_param(light_instance->light, RS::LIGHT_PARAM_SHADOW_BIAS);
  4774. normal_bias = storage->light_get_param(light_instance->light, RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS);
  4775. if (storage->light_get_type(light_instance->light) == RS::LIGHT_OMNI) {
  4776. if (storage->light_omni_get_shadow_mode(light_instance->light) == RS::LIGHT_OMNI_SHADOW_CUBE) {
  4777. ShadowCubemap *cubemap = _get_shadow_cubemap(shadow_size / 2);
  4778. render_fb = cubemap->side_fb[p_pass];
  4779. render_texture = cubemap->cubemap;
  4780. light_projection = light_instance->shadow_transform[0].camera;
  4781. light_transform = light_instance->shadow_transform[0].transform;
  4782. render_cubemap = true;
  4783. finalize_cubemap = p_pass == 5;
  4784. } else {
  4785. light_projection = light_instance->shadow_transform[0].camera;
  4786. light_transform = light_instance->shadow_transform[0].transform;
  4787. atlas_rect.size.height /= 2;
  4788. atlas_rect.position.y += p_pass * atlas_rect.size.height;
  4789. using_dual_paraboloid = true;
  4790. using_dual_paraboloid_flip = p_pass == 1;
  4791. ShadowMap *shadow_map = _get_shadow_map(atlas_rect.size);
  4792. render_fb = shadow_map->fb;
  4793. render_texture = shadow_map->depth;
  4794. }
  4795. } else if (storage->light_get_type(light_instance->light) == RS::LIGHT_SPOT) {
  4796. light_projection = light_instance->shadow_transform[0].camera;
  4797. light_transform = light_instance->shadow_transform[0].transform;
  4798. ShadowMap *shadow_map = _get_shadow_map(atlas_rect.size);
  4799. render_fb = shadow_map->fb;
  4800. render_texture = shadow_map->depth;
  4801. znear = light_instance->shadow_transform[0].camera.get_z_near();
  4802. use_linear_depth = true;
  4803. }
  4804. }
  4805. if (render_cubemap) {
  4806. //rendering to cubemap
  4807. _render_shadow(render_fb, p_cull_result, p_cull_count, light_projection, light_transform, zfar, 0, 0, false, false, use_pancake);
  4808. if (finalize_cubemap) {
  4809. //reblit
  4810. atlas_rect.size.height /= 2;
  4811. 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);
  4812. atlas_rect.position.y += atlas_rect.size.height;
  4813. 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);
  4814. }
  4815. } else {
  4816. //render shadow
  4817. _render_shadow(render_fb, p_cull_result, p_cull_count, light_projection, light_transform, zfar, bias, normal_bias, using_dual_paraboloid, using_dual_paraboloid_flip, use_pancake);
  4818. //copy to atlas
  4819. if (use_linear_depth) {
  4820. storage->get_effects()->copy_depth_to_rect_and_linearize(render_texture, atlas_texture, atlas_rect, true, znear, zfar);
  4821. } else {
  4822. storage->get_effects()->copy_depth_to_rect(render_texture, atlas_texture, atlas_rect, true);
  4823. }
  4824. //does not work from depth to color
  4825. //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);
  4826. }
  4827. }
  4828. void RasterizerSceneRD::render_material(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID p_framebuffer, const Rect2i &p_region) {
  4829. _render_material(p_cam_transform, p_cam_projection, p_cam_ortogonal, p_cull_result, p_cull_count, p_framebuffer, p_region);
  4830. }
  4831. void RasterizerSceneRD::render_sdfgi(RID p_render_buffers, int p_region, InstanceBase **p_cull_result, int p_cull_count) {
  4832. //print_line("rendering region " + itos(p_region));
  4833. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  4834. ERR_FAIL_COND(!rb);
  4835. ERR_FAIL_COND(!rb->sdfgi);
  4836. AABB bounds;
  4837. Vector3i from;
  4838. Vector3i size;
  4839. int cascade_prev = _sdfgi_get_pending_region_data(p_render_buffers, p_region - 1, from, size, bounds);
  4840. int cascade_next = _sdfgi_get_pending_region_data(p_render_buffers, p_region + 1, from, size, bounds);
  4841. int cascade = _sdfgi_get_pending_region_data(p_render_buffers, p_region, from, size, bounds);
  4842. ERR_FAIL_COND(cascade < 0);
  4843. if (cascade_prev != cascade) {
  4844. //initialize render
  4845. RD::get_singleton()->texture_clear(rb->sdfgi->render_albedo, Color(0, 0, 0, 0), 0, 1, 0, 1, true);
  4846. RD::get_singleton()->texture_clear(rb->sdfgi->render_emission, Color(0, 0, 0, 0), 0, 1, 0, 1, true);
  4847. RD::get_singleton()->texture_clear(rb->sdfgi->render_emission_aniso, Color(0, 0, 0, 0), 0, 1, 0, 1, true);
  4848. RD::get_singleton()->texture_clear(rb->sdfgi->render_geom_facing, Color(0, 0, 0, 0), 0, 1, 0, 1, true);
  4849. }
  4850. //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));
  4851. _render_sdfgi(p_render_buffers, from, size, bounds, p_cull_result, p_cull_count, rb->sdfgi->render_albedo, rb->sdfgi->render_emission, rb->sdfgi->render_emission_aniso, rb->sdfgi->render_geom_facing);
  4852. if (cascade_next != cascade) {
  4853. RENDER_TIMESTAMP(">SDFGI Update SDF");
  4854. //done rendering! must update SDF
  4855. //clear dispatch indirect data
  4856. SDGIShader::PreprocessPushConstant push_constant;
  4857. zeromem(&push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  4858. RENDER_TIMESTAMP("Scroll SDF");
  4859. //scroll
  4860. if (rb->sdfgi->cascades[cascade].dirty_regions != SDFGI::Cascade::DIRTY_ALL) {
  4861. //for scroll
  4862. Vector3i dirty = rb->sdfgi->cascades[cascade].dirty_regions;
  4863. push_constant.scroll[0] = dirty.x;
  4864. push_constant.scroll[1] = dirty.y;
  4865. push_constant.scroll[2] = dirty.z;
  4866. } else {
  4867. //for no scroll
  4868. push_constant.scroll[0] = 0;
  4869. push_constant.scroll[1] = 0;
  4870. push_constant.scroll[2] = 0;
  4871. }
  4872. push_constant.grid_size = rb->sdfgi->cascade_size;
  4873. push_constant.cascade = cascade;
  4874. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  4875. if (rb->sdfgi->cascades[cascade].dirty_regions != SDFGI::Cascade::DIRTY_ALL) {
  4876. //must pre scroll existing data because not all is dirty
  4877. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_SCROLL]);
  4878. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->cascades[cascade].scroll_uniform_set, 0);
  4879. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  4880. RD::get_singleton()->compute_list_dispatch_indirect(compute_list, rb->sdfgi->cascades[cascade].solid_cell_dispatch_buffer, 0);
  4881. // no barrier do all together
  4882. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_SCROLL_OCCLUSION]);
  4883. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->cascades[cascade].scroll_occlusion_uniform_set, 0);
  4884. Vector3i dirty = rb->sdfgi->cascades[cascade].dirty_regions;
  4885. Vector3i groups;
  4886. groups.x = rb->sdfgi->cascade_size - ABS(dirty.x);
  4887. groups.y = rb->sdfgi->cascade_size - ABS(dirty.y);
  4888. groups.z = rb->sdfgi->cascade_size - ABS(dirty.z);
  4889. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  4890. RD::get_singleton()->compute_list_dispatch_threads(compute_list, groups.x, groups.y, groups.z, 4, 4, 4);
  4891. //no barrier, continue together
  4892. {
  4893. //scroll probes and their history also
  4894. SDGIShader::IntegratePushConstant ipush_constant;
  4895. ipush_constant.grid_size[1] = rb->sdfgi->cascade_size;
  4896. ipush_constant.grid_size[2] = rb->sdfgi->cascade_size;
  4897. ipush_constant.grid_size[0] = rb->sdfgi->cascade_size;
  4898. ipush_constant.max_cascades = rb->sdfgi->cascades.size();
  4899. ipush_constant.probe_axis_size = rb->sdfgi->probe_axis_count;
  4900. ipush_constant.history_index = 0;
  4901. ipush_constant.history_size = rb->sdfgi->history_size;
  4902. ipush_constant.ray_count = 0;
  4903. ipush_constant.ray_bias = 0;
  4904. ipush_constant.sky_mode = 0;
  4905. ipush_constant.sky_energy = 0;
  4906. ipush_constant.sky_color[0] = 0;
  4907. ipush_constant.sky_color[1] = 0;
  4908. ipush_constant.sky_color[2] = 0;
  4909. ipush_constant.y_mult = rb->sdfgi->y_mult;
  4910. ipush_constant.image_size[0] = rb->sdfgi->probe_axis_count * rb->sdfgi->probe_axis_count;
  4911. ipush_constant.image_size[1] = rb->sdfgi->probe_axis_count;
  4912. ipush_constant.image_size[1] = rb->sdfgi->probe_axis_count;
  4913. int32_t probe_divisor = rb->sdfgi->cascade_size / SDFGI::PROBE_DIVISOR;
  4914. ipush_constant.cascade = cascade;
  4915. ipush_constant.world_offset[0] = rb->sdfgi->cascades[cascade].position.x / probe_divisor;
  4916. ipush_constant.world_offset[1] = rb->sdfgi->cascades[cascade].position.y / probe_divisor;
  4917. ipush_constant.world_offset[2] = rb->sdfgi->cascades[cascade].position.z / probe_divisor;
  4918. ipush_constant.scroll[0] = dirty.x / probe_divisor;
  4919. ipush_constant.scroll[1] = dirty.y / probe_divisor;
  4920. ipush_constant.scroll[2] = dirty.z / probe_divisor;
  4921. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.integrate_pipeline[SDGIShader::INTEGRATE_MODE_SCROLL]);
  4922. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->cascades[cascade].integrate_uniform_set, 0);
  4923. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sdfgi_shader.integrate_default_sky_uniform_set, 1);
  4924. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ipush_constant, sizeof(SDGIShader::IntegratePushConstant));
  4925. 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);
  4926. RD::get_singleton()->compute_list_add_barrier(compute_list);
  4927. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.integrate_pipeline[SDGIShader::INTEGRATE_MODE_SCROLL_STORE]);
  4928. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->cascades[cascade].integrate_uniform_set, 0);
  4929. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sdfgi_shader.integrate_default_sky_uniform_set, 1);
  4930. RD::get_singleton()->compute_list_set_push_constant(compute_list, &ipush_constant, sizeof(SDGIShader::IntegratePushConstant));
  4931. 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);
  4932. }
  4933. //ok finally barrier
  4934. RD::get_singleton()->compute_list_add_barrier(compute_list);
  4935. }
  4936. //clear dispatch indirect data
  4937. uint32_t dispatch_indirct_data[4] = { 0, 0, 0, 0 };
  4938. RD::get_singleton()->buffer_update(rb->sdfgi->cascades[cascade].solid_cell_dispatch_buffer, 0, sizeof(uint32_t) * 4, dispatch_indirct_data, true);
  4939. bool half_size = true; //much faster, very little differnce
  4940. static const int optimized_jf_group_size = 8;
  4941. if (half_size) {
  4942. push_constant.grid_size >>= 1;
  4943. uint32_t cascade_half_size = rb->sdfgi->cascade_size >> 1;
  4944. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_JUMP_FLOOD_INITIALIZE_HALF]);
  4945. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->sdf_initialize_half_uniform_set, 0);
  4946. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  4947. RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_half_size, cascade_half_size, cascade_half_size, 4, 4, 4);
  4948. RD::get_singleton()->compute_list_add_barrier(compute_list);
  4949. //must start with regular jumpflood
  4950. push_constant.half_size = true;
  4951. {
  4952. RENDER_TIMESTAMP("SDFGI Jump Flood (Half Size)");
  4953. uint32_t s = cascade_half_size;
  4954. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_JUMP_FLOOD]);
  4955. int jf_us = 0;
  4956. //start with regular jump flood for very coarse reads, as this is impossible to optimize
  4957. while (s > 1) {
  4958. s /= 2;
  4959. push_constant.step_size = s;
  4960. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->jump_flood_half_uniform_set[jf_us], 0);
  4961. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  4962. RD::get_singleton()->compute_list_dispatch_threads(compute_list, cascade_half_size, cascade_half_size, cascade_half_size, 4, 4, 4);
  4963. RD::get_singleton()->compute_list_add_barrier(compute_list);
  4964. jf_us = jf_us == 0 ? 1 : 0;
  4965. if (cascade_half_size / (s / 2) >= optimized_jf_group_size) {
  4966. break;
  4967. }
  4968. }
  4969. RENDER_TIMESTAMP("SDFGI Jump Flood Optimized (Half Size)");
  4970. //continue with optimized jump flood for smaller reads
  4971. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_JUMP_FLOOD_OPTIMIZED]);
  4972. while (s > 1) {
  4973. s /= 2;
  4974. push_constant.step_size = s;
  4975. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->jump_flood_half_uniform_set[jf_us], 0);
  4976. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  4977. 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);
  4978. RD::get_singleton()->compute_list_add_barrier(compute_list);
  4979. jf_us = jf_us == 0 ? 1 : 0;
  4980. }
  4981. }
  4982. // restore grid size for last passes
  4983. push_constant.grid_size = rb->sdfgi->cascade_size;
  4984. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_JUMP_FLOOD_UPSCALE]);
  4985. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->sdf_upscale_uniform_set, 0);
  4986. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  4987. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, 4, 4, 4);
  4988. RD::get_singleton()->compute_list_add_barrier(compute_list);
  4989. //run one pass of fullsize jumpflood to fix up half size arctifacts
  4990. push_constant.half_size = false;
  4991. push_constant.step_size = 1;
  4992. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_JUMP_FLOOD_OPTIMIZED]);
  4993. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->jump_flood_uniform_set[rb->sdfgi->upscale_jfa_uniform_set_index], 0);
  4994. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  4995. 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);
  4996. RD::get_singleton()->compute_list_add_barrier(compute_list);
  4997. } else {
  4998. //full size jumpflood
  4999. RENDER_TIMESTAMP("SDFGI Jump Flood");
  5000. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_JUMP_FLOOD_INITIALIZE]);
  5001. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->sdf_initialize_uniform_set, 0);
  5002. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  5003. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, 4, 4, 4);
  5004. RD::get_singleton()->compute_list_add_barrier(compute_list);
  5005. push_constant.half_size = false;
  5006. {
  5007. uint32_t s = rb->sdfgi->cascade_size;
  5008. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_JUMP_FLOOD]);
  5009. int jf_us = 0;
  5010. //start with regular jump flood for very coarse reads, as this is impossible to optimize
  5011. while (s > 1) {
  5012. s /= 2;
  5013. push_constant.step_size = s;
  5014. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->jump_flood_uniform_set[jf_us], 0);
  5015. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  5016. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, 4, 4, 4);
  5017. RD::get_singleton()->compute_list_add_barrier(compute_list);
  5018. jf_us = jf_us == 0 ? 1 : 0;
  5019. if (rb->sdfgi->cascade_size / (s / 2) >= optimized_jf_group_size) {
  5020. break;
  5021. }
  5022. }
  5023. RENDER_TIMESTAMP("SDFGI Jump Flood Optimized");
  5024. //continue with optimized jump flood for smaller reads
  5025. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_JUMP_FLOOD_OPTIMIZED]);
  5026. while (s > 1) {
  5027. s /= 2;
  5028. push_constant.step_size = s;
  5029. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->jump_flood_uniform_set[jf_us], 0);
  5030. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  5031. 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);
  5032. RD::get_singleton()->compute_list_add_barrier(compute_list);
  5033. jf_us = jf_us == 0 ? 1 : 0;
  5034. }
  5035. }
  5036. }
  5037. RENDER_TIMESTAMP("SDFGI Occlusion");
  5038. // occlusion
  5039. {
  5040. uint32_t probe_size = rb->sdfgi->cascade_size / SDFGI::PROBE_DIVISOR;
  5041. Vector3i probe_global_pos = rb->sdfgi->cascades[cascade].position / probe_size;
  5042. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_OCCLUSION]);
  5043. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->occlusion_uniform_set, 0);
  5044. for (int i = 0; i < 8; i++) {
  5045. //dispatch all at once for performance
  5046. Vector3i offset(i & 1, (i >> 1) & 1, (i >> 2) & 1);
  5047. if ((probe_global_pos.x & 1) != 0) {
  5048. offset.x = (offset.x + 1) & 1;
  5049. }
  5050. if ((probe_global_pos.y & 1) != 0) {
  5051. offset.y = (offset.y + 1) & 1;
  5052. }
  5053. if ((probe_global_pos.z & 1) != 0) {
  5054. offset.z = (offset.z + 1) & 1;
  5055. }
  5056. push_constant.probe_offset[0] = offset.x;
  5057. push_constant.probe_offset[1] = offset.y;
  5058. push_constant.probe_offset[2] = offset.z;
  5059. push_constant.occlusion_index = i;
  5060. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  5061. Vector3i groups = Vector3i(probe_size + 1, probe_size + 1, probe_size + 1) - offset; //if offseted, its one less probe per axis to compute
  5062. RD::get_singleton()->compute_list_dispatch(compute_list, groups.x, groups.y, groups.z);
  5063. }
  5064. RD::get_singleton()->compute_list_add_barrier(compute_list);
  5065. }
  5066. RENDER_TIMESTAMP("SDFGI Store");
  5067. // store
  5068. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.preprocess_pipeline[SDGIShader::PRE_PROCESS_STORE]);
  5069. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, rb->sdfgi->cascades[cascade].sdf_store_uniform_set, 0);
  5070. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SDGIShader::PreprocessPushConstant));
  5071. RD::get_singleton()->compute_list_dispatch_threads(compute_list, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, 4, 4, 4);
  5072. RD::get_singleton()->compute_list_end();
  5073. //clear these textures, as they will have previous garbage on next draw
  5074. RD::get_singleton()->texture_clear(rb->sdfgi->cascades[cascade].light_tex, Color(0, 0, 0, 0), 0, 1, 0, 1, true);
  5075. RD::get_singleton()->texture_clear(rb->sdfgi->cascades[cascade].light_aniso_0_tex, Color(0, 0, 0, 0), 0, 1, 0, 1, true);
  5076. RD::get_singleton()->texture_clear(rb->sdfgi->cascades[cascade].light_aniso_1_tex, Color(0, 0, 0, 0), 0, 1, 0, 1, true);
  5077. #if 0
  5078. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(rb->sdfgi->cascades[cascade].sdf, 0);
  5079. Ref<Image> img;
  5080. img.instance();
  5081. for (uint32_t i = 0; i < rb->sdfgi->cascade_size; i++) {
  5082. Vector<uint8_t> subarr = data.subarray(128 * 128 * i, 128 * 128 * (i + 1) - 1);
  5083. img->create(rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, false, Image::FORMAT_L8, subarr);
  5084. img->save_png("res://cascade_sdf_" + itos(cascade) + "_" + itos(i) + ".png");
  5085. }
  5086. //finalize render and update sdf
  5087. #endif
  5088. #if 0
  5089. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(rb->sdfgi->render_albedo, 0);
  5090. Ref<Image> img;
  5091. img.instance();
  5092. for (uint32_t i = 0; i < rb->sdfgi->cascade_size; i++) {
  5093. Vector<uint8_t> subarr = data.subarray(128 * 128 * i * 2, 128 * 128 * (i + 1) * 2 - 1);
  5094. img->create(rb->sdfgi->cascade_size, rb->sdfgi->cascade_size, false, Image::FORMAT_RGB565, subarr);
  5095. img->convert(Image::FORMAT_RGBA8);
  5096. img->save_png("res://cascade_" + itos(cascade) + "_" + itos(i) + ".png");
  5097. }
  5098. //finalize render and update sdf
  5099. #endif
  5100. RENDER_TIMESTAMP("<SDFGI Update SDF");
  5101. }
  5102. }
  5103. void RasterizerSceneRD::render_sdfgi_static_lights(RID p_render_buffers, uint32_t p_cascade_count, const uint32_t *p_cascade_indices, const RID **p_positional_light_cull_result, const uint32_t *p_positional_light_cull_count) {
  5104. RenderBuffers *rb = render_buffers_owner.getornull(p_render_buffers);
  5105. ERR_FAIL_COND(!rb);
  5106. ERR_FAIL_COND(!rb->sdfgi);
  5107. ERR_FAIL_COND(p_positional_light_cull_count == 0);
  5108. _sdfgi_update_cascades(p_render_buffers); //need cascades updated for this
  5109. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  5110. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, sdfgi_shader.direct_light_pipeline[SDGIShader::DIRECT_LIGHT_MODE_STATIC]);
  5111. SDGIShader::DirectLightPushConstant dl_push_constant;
  5112. dl_push_constant.grid_size[0] = rb->sdfgi->cascade_size;
  5113. dl_push_constant.grid_size[1] = rb->sdfgi->cascade_size;
  5114. dl_push_constant.grid_size[2] = rb->sdfgi->cascade_size;
  5115. dl_push_constant.max_cascades = rb->sdfgi->cascades.size();
  5116. dl_push_constant.probe_axis_size = rb->sdfgi->probe_axis_count;
  5117. dl_push_constant.multibounce = false; // this is static light, do not multibounce yet
  5118. dl_push_constant.y_mult = rb->sdfgi->y_mult;
  5119. //all must be processed
  5120. dl_push_constant.process_offset = 0;
  5121. dl_push_constant.process_increment = 1;
  5122. SDGIShader::Light lights[SDFGI::MAX_STATIC_LIGHTS];
  5123. for (uint32_t i = 0; i < p_cascade_count; i++) {
  5124. ERR_CONTINUE(p_cascade_indices[i] >= rb->sdfgi->cascades.size());
  5125. SDFGI::Cascade &cc = rb->sdfgi->cascades[p_cascade_indices[i]];
  5126. { //fill light buffer
  5127. AABB cascade_aabb;
  5128. cascade_aabb.position = Vector3((Vector3i(1, 1, 1) * -int32_t(rb->sdfgi->cascade_size >> 1) + cc.position)) * cc.cell_size;
  5129. cascade_aabb.size = Vector3(1, 1, 1) * rb->sdfgi->cascade_size * cc.cell_size;
  5130. int idx = 0;
  5131. for (uint32_t j = 0; j < p_positional_light_cull_count[i]; j++) {
  5132. if (idx == SDFGI::MAX_STATIC_LIGHTS) {
  5133. break;
  5134. }
  5135. LightInstance *li = light_instance_owner.getornull(p_positional_light_cull_result[i][j]);
  5136. ERR_CONTINUE(!li);
  5137. uint32_t max_sdfgi_cascade = storage->light_get_max_sdfgi_cascade(li->light);
  5138. if (p_cascade_indices[i] > max_sdfgi_cascade) {
  5139. continue;
  5140. }
  5141. if (!cascade_aabb.intersects(li->aabb)) {
  5142. continue;
  5143. }
  5144. lights[idx].type = storage->light_get_type(li->light);
  5145. Vector3 dir = -li->transform.basis.get_axis(Vector3::AXIS_Z);
  5146. if (lights[idx].type == RS::LIGHT_DIRECTIONAL) {
  5147. dir.y *= rb->sdfgi->y_mult; //only makes sense for directional
  5148. dir.normalize();
  5149. }
  5150. lights[idx].direction[0] = dir.x;
  5151. lights[idx].direction[1] = dir.y;
  5152. lights[idx].direction[2] = dir.z;
  5153. Vector3 pos = li->transform.origin;
  5154. pos.y *= rb->sdfgi->y_mult;
  5155. lights[idx].position[0] = pos.x;
  5156. lights[idx].position[1] = pos.y;
  5157. lights[idx].position[2] = pos.z;
  5158. Color color = storage->light_get_color(li->light);
  5159. color = color.to_linear();
  5160. lights[idx].color[0] = color.r;
  5161. lights[idx].color[1] = color.g;
  5162. lights[idx].color[2] = color.b;
  5163. lights[idx].energy = storage->light_get_param(li->light, RS::LIGHT_PARAM_ENERGY);
  5164. lights[idx].has_shadow = storage->light_has_shadow(li->light);
  5165. lights[idx].attenuation = storage->light_get_param(li->light, RS::LIGHT_PARAM_ATTENUATION);
  5166. lights[idx].radius = storage->light_get_param(li->light, RS::LIGHT_PARAM_RANGE);
  5167. lights[idx].spot_angle = Math::deg2rad(storage->light_get_param(li->light, RS::LIGHT_PARAM_SPOT_ANGLE));
  5168. lights[idx].spot_attenuation = storage->light_get_param(li->light, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  5169. idx++;
  5170. }
  5171. if (idx > 0) {
  5172. RD::get_singleton()->buffer_update(cc.lights_buffer, 0, idx * sizeof(SDGIShader::Light), lights, true);
  5173. }
  5174. dl_push_constant.light_count = idx;
  5175. }
  5176. dl_push_constant.cascade = p_cascade_indices[i];
  5177. if (dl_push_constant.light_count > 0) {
  5178. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, cc.sdf_direct_light_uniform_set, 0);
  5179. RD::get_singleton()->compute_list_set_push_constant(compute_list, &dl_push_constant, sizeof(SDGIShader::DirectLightPushConstant));
  5180. RD::get_singleton()->compute_list_dispatch_indirect(compute_list, cc.solid_cell_dispatch_buffer, 0);
  5181. }
  5182. }
  5183. RD::get_singleton()->compute_list_end();
  5184. }
  5185. bool RasterizerSceneRD::free(RID p_rid) {
  5186. if (render_buffers_owner.owns(p_rid)) {
  5187. RenderBuffers *rb = render_buffers_owner.getornull(p_rid);
  5188. _free_render_buffer_data(rb);
  5189. memdelete(rb->data);
  5190. if (rb->sdfgi) {
  5191. _sdfgi_erase(rb);
  5192. }
  5193. render_buffers_owner.free(p_rid);
  5194. } else if (environment_owner.owns(p_rid)) {
  5195. //not much to delete, just free it
  5196. environment_owner.free(p_rid);
  5197. } else if (camera_effects_owner.owns(p_rid)) {
  5198. //not much to delete, just free it
  5199. camera_effects_owner.free(p_rid);
  5200. } else if (reflection_atlas_owner.owns(p_rid)) {
  5201. reflection_atlas_set_size(p_rid, 0, 0);
  5202. reflection_atlas_owner.free(p_rid);
  5203. } else if (reflection_probe_instance_owner.owns(p_rid)) {
  5204. //not much to delete, just free it
  5205. //ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_rid);
  5206. reflection_probe_release_atlas_index(p_rid);
  5207. reflection_probe_instance_owner.free(p_rid);
  5208. } else if (decal_instance_owner.owns(p_rid)) {
  5209. decal_instance_owner.free(p_rid);
  5210. } else if (gi_probe_instance_owner.owns(p_rid)) {
  5211. GIProbeInstance *gi_probe = gi_probe_instance_owner.getornull(p_rid);
  5212. if (gi_probe->texture.is_valid()) {
  5213. RD::get_singleton()->free(gi_probe->texture);
  5214. RD::get_singleton()->free(gi_probe->write_buffer);
  5215. }
  5216. for (int i = 0; i < gi_probe->dynamic_maps.size(); i++) {
  5217. RD::get_singleton()->free(gi_probe->dynamic_maps[i].texture);
  5218. RD::get_singleton()->free(gi_probe->dynamic_maps[i].depth);
  5219. }
  5220. gi_probe_instance_owner.free(p_rid);
  5221. } else if (sky_owner.owns(p_rid)) {
  5222. _update_dirty_skys();
  5223. Sky *sky = sky_owner.getornull(p_rid);
  5224. if (sky->radiance.is_valid()) {
  5225. RD::get_singleton()->free(sky->radiance);
  5226. sky->radiance = RID();
  5227. }
  5228. _clear_reflection_data(sky->reflection);
  5229. if (sky->uniform_buffer.is_valid()) {
  5230. RD::get_singleton()->free(sky->uniform_buffer);
  5231. sky->uniform_buffer = RID();
  5232. }
  5233. if (sky->half_res_pass.is_valid()) {
  5234. RD::get_singleton()->free(sky->half_res_pass);
  5235. sky->half_res_pass = RID();
  5236. }
  5237. if (sky->quarter_res_pass.is_valid()) {
  5238. RD::get_singleton()->free(sky->quarter_res_pass);
  5239. sky->quarter_res_pass = RID();
  5240. }
  5241. if (sky->material.is_valid()) {
  5242. storage->free(sky->material);
  5243. }
  5244. sky_owner.free(p_rid);
  5245. } else if (light_instance_owner.owns(p_rid)) {
  5246. LightInstance *light_instance = light_instance_owner.getornull(p_rid);
  5247. //remove from shadow atlases..
  5248. for (Set<RID>::Element *E = light_instance->shadow_atlases.front(); E; E = E->next()) {
  5249. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(E->get());
  5250. ERR_CONTINUE(!shadow_atlas->shadow_owners.has(p_rid));
  5251. uint32_t key = shadow_atlas->shadow_owners[p_rid];
  5252. uint32_t q = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  5253. uint32_t s = key & ShadowAtlas::SHADOW_INDEX_MASK;
  5254. shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
  5255. shadow_atlas->shadow_owners.erase(p_rid);
  5256. }
  5257. light_instance_owner.free(p_rid);
  5258. } else if (shadow_atlas_owner.owns(p_rid)) {
  5259. shadow_atlas_set_size(p_rid, 0);
  5260. shadow_atlas_owner.free(p_rid);
  5261. } else {
  5262. return false;
  5263. }
  5264. return true;
  5265. }
  5266. void RasterizerSceneRD::set_debug_draw_mode(RS::ViewportDebugDraw p_debug_draw) {
  5267. debug_draw = p_debug_draw;
  5268. }
  5269. void RasterizerSceneRD::update() {
  5270. _update_dirty_skys();
  5271. }
  5272. void RasterizerSceneRD::set_time(double p_time, double p_step) {
  5273. time = p_time;
  5274. time_step = p_step;
  5275. }
  5276. void RasterizerSceneRD::screen_space_roughness_limiter_set_active(bool p_enable, float p_amount, float p_limit) {
  5277. screen_space_roughness_limiter = p_enable;
  5278. screen_space_roughness_limiter_amount = p_amount;
  5279. screen_space_roughness_limiter_limit = p_limit;
  5280. }
  5281. bool RasterizerSceneRD::screen_space_roughness_limiter_is_active() const {
  5282. return screen_space_roughness_limiter;
  5283. }
  5284. float RasterizerSceneRD::screen_space_roughness_limiter_get_amount() const {
  5285. return screen_space_roughness_limiter_amount;
  5286. }
  5287. float RasterizerSceneRD::screen_space_roughness_limiter_get_limit() const {
  5288. return screen_space_roughness_limiter_limit;
  5289. }
  5290. TypedArray<Image> RasterizerSceneRD::bake_render_uv2(RID p_base, const Vector<RID> &p_material_overrides, const Size2i &p_image_size) {
  5291. RD::TextureFormat tf;
  5292. tf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  5293. tf.width = p_image_size.width; // Always 64x64
  5294. tf.height = p_image_size.height;
  5295. tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  5296. RID albedo_alpha_tex = RD::get_singleton()->texture_create(tf, RD::TextureView());
  5297. RID normal_tex = RD::get_singleton()->texture_create(tf, RD::TextureView());
  5298. RID orm_tex = RD::get_singleton()->texture_create(tf, RD::TextureView());
  5299. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  5300. RID emission_tex = RD::get_singleton()->texture_create(tf, RD::TextureView());
  5301. tf.format = RD::DATA_FORMAT_R32_SFLOAT;
  5302. RID depth_write_tex = RD::get_singleton()->texture_create(tf, RD::TextureView());
  5303. tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  5304. 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;
  5305. RID depth_tex = RD::get_singleton()->texture_create(tf, RD::TextureView());
  5306. Vector<RID> fb_tex;
  5307. fb_tex.push_back(albedo_alpha_tex);
  5308. fb_tex.push_back(normal_tex);
  5309. fb_tex.push_back(orm_tex);
  5310. fb_tex.push_back(emission_tex);
  5311. fb_tex.push_back(depth_write_tex);
  5312. fb_tex.push_back(depth_tex);
  5313. RID fb = RD::get_singleton()->framebuffer_create(fb_tex);
  5314. //RID sampled_light;
  5315. InstanceBase ins;
  5316. ins.base_type = RSG::storage->get_base_type(p_base);
  5317. ins.base = p_base;
  5318. ins.materials.resize(RSG::storage->mesh_get_surface_count(p_base));
  5319. for (int i = 0; i < ins.materials.size(); i++) {
  5320. if (i < p_material_overrides.size()) {
  5321. ins.materials.write[i] = p_material_overrides[i];
  5322. }
  5323. }
  5324. InstanceBase *cull = &ins;
  5325. _render_uv2(&cull, 1, fb, Rect2i(0, 0, p_image_size.width, p_image_size.height));
  5326. TypedArray<Image> ret;
  5327. {
  5328. PackedByteArray data = RD::get_singleton()->texture_get_data(albedo_alpha_tex, 0);
  5329. Ref<Image> img;
  5330. img.instance();
  5331. img->create(p_image_size.width, p_image_size.height, false, Image::FORMAT_RGBA8, data);
  5332. RD::get_singleton()->free(albedo_alpha_tex);
  5333. ret.push_back(img);
  5334. }
  5335. {
  5336. PackedByteArray data = RD::get_singleton()->texture_get_data(normal_tex, 0);
  5337. Ref<Image> img;
  5338. img.instance();
  5339. img->create(p_image_size.width, p_image_size.height, false, Image::FORMAT_RGBA8, data);
  5340. RD::get_singleton()->free(normal_tex);
  5341. ret.push_back(img);
  5342. }
  5343. {
  5344. PackedByteArray data = RD::get_singleton()->texture_get_data(orm_tex, 0);
  5345. Ref<Image> img;
  5346. img.instance();
  5347. img->create(p_image_size.width, p_image_size.height, false, Image::FORMAT_RGBA8, data);
  5348. RD::get_singleton()->free(orm_tex);
  5349. ret.push_back(img);
  5350. }
  5351. {
  5352. PackedByteArray data = RD::get_singleton()->texture_get_data(emission_tex, 0);
  5353. Ref<Image> img;
  5354. img.instance();
  5355. img->create(p_image_size.width, p_image_size.height, false, Image::FORMAT_RGBAH, data);
  5356. RD::get_singleton()->free(emission_tex);
  5357. ret.push_back(img);
  5358. }
  5359. RD::get_singleton()->free(depth_write_tex);
  5360. RD::get_singleton()->free(depth_tex);
  5361. return ret;
  5362. }
  5363. void RasterizerSceneRD::sdfgi_set_debug_probe_select(const Vector3 &p_position, const Vector3 &p_dir) {
  5364. sdfgi_debug_probe_pos = p_position;
  5365. sdfgi_debug_probe_dir = p_dir;
  5366. }
  5367. RasterizerSceneRD *RasterizerSceneRD::singleton = nullptr;
  5368. RasterizerSceneRD::RasterizerSceneRD(RasterizerStorageRD *p_storage) {
  5369. storage = p_storage;
  5370. singleton = this;
  5371. roughness_layers = GLOBAL_GET("rendering/quality/reflections/roughness_layers");
  5372. sky_ggx_samples_quality = GLOBAL_GET("rendering/quality/reflections/ggx_samples");
  5373. sky_use_cubemap_array = GLOBAL_GET("rendering/quality/reflections/texture_array_reflections");
  5374. // sky_use_cubemap_array = false;
  5375. //uint32_t textures_per_stage = RD::get_singleton()->limit_get(RD::LIMIT_MAX_TEXTURES_PER_SHADER_STAGE);
  5376. {
  5377. //kinda complicated to compute the amount of slots, we try to use as many as we can
  5378. gi_probe_max_lights = 32;
  5379. gi_probe_lights = memnew_arr(GIProbeLight, gi_probe_max_lights);
  5380. gi_probe_lights_uniform = RD::get_singleton()->uniform_buffer_create(gi_probe_max_lights * sizeof(GIProbeLight));
  5381. gi_probe_quality = RS::GIProbeQuality(CLAMP(int(GLOBAL_GET("rendering/quality/gi_probes/quality")), 0, 1));
  5382. String defines = "\n#define MAX_LIGHTS " + itos(gi_probe_max_lights) + "\n";
  5383. Vector<String> versions;
  5384. versions.push_back("\n#define MODE_COMPUTE_LIGHT\n");
  5385. versions.push_back("\n#define MODE_SECOND_BOUNCE\n");
  5386. versions.push_back("\n#define MODE_UPDATE_MIPMAPS\n");
  5387. versions.push_back("\n#define MODE_WRITE_TEXTURE\n");
  5388. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_LIGHTING\n");
  5389. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_WRITE\n");
  5390. versions.push_back("\n#define MODE_DYNAMIC\n#define MODE_DYNAMIC_SHRINK\n#define MODE_DYNAMIC_SHRINK_PLOT\n");
  5391. 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");
  5392. giprobe_shader.initialize(versions, defines);
  5393. giprobe_lighting_shader_version = giprobe_shader.version_create();
  5394. for (int i = 0; i < GI_PROBE_SHADER_VERSION_MAX; i++) {
  5395. giprobe_lighting_shader_version_shaders[i] = giprobe_shader.version_get_shader(giprobe_lighting_shader_version, i);
  5396. giprobe_lighting_shader_version_pipelines[i] = RD::get_singleton()->compute_pipeline_create(giprobe_lighting_shader_version_shaders[i]);
  5397. }
  5398. }
  5399. {
  5400. String defines;
  5401. Vector<String> versions;
  5402. versions.push_back("\n#define MODE_DEBUG_COLOR\n");
  5403. versions.push_back("\n#define MODE_DEBUG_LIGHT\n");
  5404. versions.push_back("\n#define MODE_DEBUG_EMISSION\n");
  5405. versions.push_back("\n#define MODE_DEBUG_LIGHT\n#define MODE_DEBUG_LIGHT_FULL\n");
  5406. giprobe_debug_shader.initialize(versions, defines);
  5407. giprobe_debug_shader_version = giprobe_debug_shader.version_create();
  5408. for (int i = 0; i < GI_PROBE_DEBUG_MAX; i++) {
  5409. giprobe_debug_shader_version_shaders[i] = giprobe_debug_shader.version_get_shader(giprobe_debug_shader_version, i);
  5410. RD::PipelineRasterizationState rs;
  5411. rs.cull_mode = RD::POLYGON_CULL_FRONT;
  5412. RD::PipelineDepthStencilState ds;
  5413. ds.enable_depth_test = true;
  5414. ds.enable_depth_write = true;
  5415. ds.depth_compare_operator = RD::COMPARE_OP_LESS_OR_EQUAL;
  5416. 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);
  5417. }
  5418. }
  5419. /* SKY SHADER */
  5420. {
  5421. // Start with the directional lights for the sky
  5422. sky_scene_state.max_directional_lights = 4;
  5423. uint32_t directional_light_buffer_size = sky_scene_state.max_directional_lights * sizeof(SkyDirectionalLightData);
  5424. sky_scene_state.directional_lights = memnew_arr(SkyDirectionalLightData, sky_scene_state.max_directional_lights);
  5425. sky_scene_state.last_frame_directional_lights = memnew_arr(SkyDirectionalLightData, sky_scene_state.max_directional_lights);
  5426. sky_scene_state.last_frame_directional_light_count = sky_scene_state.max_directional_lights + 1;
  5427. sky_scene_state.directional_light_buffer = RD::get_singleton()->uniform_buffer_create(directional_light_buffer_size);
  5428. String defines = "\n#define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS " + itos(sky_scene_state.max_directional_lights) + "\n";
  5429. // Initialize sky
  5430. Vector<String> sky_modes;
  5431. sky_modes.push_back(""); // Full size
  5432. sky_modes.push_back("\n#define USE_HALF_RES_PASS\n"); // Half Res
  5433. sky_modes.push_back("\n#define USE_QUARTER_RES_PASS\n"); // Quarter res
  5434. sky_modes.push_back("\n#define USE_CUBEMAP_PASS\n"); // Cubemap
  5435. sky_modes.push_back("\n#define USE_CUBEMAP_PASS\n#define USE_HALF_RES_PASS\n"); // Half Res Cubemap
  5436. sky_modes.push_back("\n#define USE_CUBEMAP_PASS\n#define USE_QUARTER_RES_PASS\n"); // Quarter res Cubemap
  5437. sky_shader.shader.initialize(sky_modes, defines);
  5438. }
  5439. // register our shader funds
  5440. storage->shader_set_data_request_function(RasterizerStorageRD::SHADER_TYPE_SKY, _create_sky_shader_funcs);
  5441. storage->material_set_data_request_function(RasterizerStorageRD::SHADER_TYPE_SKY, _create_sky_material_funcs);
  5442. {
  5443. ShaderCompilerRD::DefaultIdentifierActions actions;
  5444. actions.renames["COLOR"] = "color";
  5445. actions.renames["ALPHA"] = "alpha";
  5446. actions.renames["EYEDIR"] = "cube_normal";
  5447. actions.renames["POSITION"] = "params.position_multiplier.xyz";
  5448. actions.renames["SKY_COORDS"] = "panorama_coords";
  5449. actions.renames["SCREEN_UV"] = "uv";
  5450. actions.renames["TIME"] = "params.time";
  5451. actions.renames["HALF_RES_COLOR"] = "half_res_color";
  5452. actions.renames["QUARTER_RES_COLOR"] = "quarter_res_color";
  5453. actions.renames["RADIANCE"] = "radiance";
  5454. actions.renames["LIGHT0_ENABLED"] = "directional_lights.data[0].enabled";
  5455. actions.renames["LIGHT0_DIRECTION"] = "directional_lights.data[0].direction_energy.xyz";
  5456. actions.renames["LIGHT0_ENERGY"] = "directional_lights.data[0].direction_energy.w";
  5457. actions.renames["LIGHT0_COLOR"] = "directional_lights.data[0].color_size.xyz";
  5458. actions.renames["LIGHT0_SIZE"] = "directional_lights.data[0].color_size.w";
  5459. actions.renames["LIGHT1_ENABLED"] = "directional_lights.data[1].enabled";
  5460. actions.renames["LIGHT1_DIRECTION"] = "directional_lights.data[1].direction_energy.xyz";
  5461. actions.renames["LIGHT1_ENERGY"] = "directional_lights.data[1].direction_energy.w";
  5462. actions.renames["LIGHT1_COLOR"] = "directional_lights.data[1].color_size.xyz";
  5463. actions.renames["LIGHT1_SIZE"] = "directional_lights.data[1].color_size.w";
  5464. actions.renames["LIGHT2_ENABLED"] = "directional_lights.data[2].enabled";
  5465. actions.renames["LIGHT2_DIRECTION"] = "directional_lights.data[2].direction_energy.xyz";
  5466. actions.renames["LIGHT2_ENERGY"] = "directional_lights.data[2].direction_energy.w";
  5467. actions.renames["LIGHT2_COLOR"] = "directional_lights.data[2].color_size.xyz";
  5468. actions.renames["LIGHT2_SIZE"] = "directional_lights.data[2].color_size.w";
  5469. actions.renames["LIGHT3_ENABLED"] = "directional_lights.data[3].enabled";
  5470. actions.renames["LIGHT3_DIRECTION"] = "directional_lights.data[3].direction_energy.xyz";
  5471. actions.renames["LIGHT3_ENERGY"] = "directional_lights.data[3].direction_energy.w";
  5472. actions.renames["LIGHT3_COLOR"] = "directional_lights.data[3].color_size.xyz";
  5473. actions.renames["LIGHT3_SIZE"] = "directional_lights.data[3].color_size.w";
  5474. actions.renames["AT_CUBEMAP_PASS"] = "AT_CUBEMAP_PASS";
  5475. actions.renames["AT_HALF_RES_PASS"] = "AT_HALF_RES_PASS";
  5476. actions.renames["AT_QUARTER_RES_PASS"] = "AT_QUARTER_RES_PASS";
  5477. actions.custom_samplers["RADIANCE"] = "material_samplers[3]";
  5478. actions.usage_defines["HALF_RES_COLOR"] = "\n#define USES_HALF_RES_COLOR\n";
  5479. actions.usage_defines["QUARTER_RES_COLOR"] = "\n#define USES_QUARTER_RES_COLOR\n";
  5480. actions.sampler_array_name = "material_samplers";
  5481. actions.base_texture_binding_index = 1;
  5482. actions.texture_layout_set = 1;
  5483. actions.base_uniform_string = "material.";
  5484. actions.base_varying_index = 10;
  5485. actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP;
  5486. actions.default_repeat = ShaderLanguage::REPEAT_ENABLE;
  5487. actions.global_buffer_array_variable = "global_variables.data";
  5488. sky_shader.compiler.initialize(actions);
  5489. }
  5490. {
  5491. // default material and shader for sky shader
  5492. sky_shader.default_shader = storage->shader_create();
  5493. storage->shader_set_code(sky_shader.default_shader, "shader_type sky; void fragment() { COLOR = vec3(0.0); } \n");
  5494. sky_shader.default_material = storage->material_create();
  5495. storage->material_set_shader(sky_shader.default_material, sky_shader.default_shader);
  5496. SkyMaterialData *md = (SkyMaterialData *)storage->material_get_data(sky_shader.default_material, RasterizerStorageRD::SHADER_TYPE_SKY);
  5497. sky_shader.default_shader_rd = sky_shader.shader.version_get_shader(md->shader_data->version, SKY_VERSION_BACKGROUND);
  5498. Vector<RD::Uniform> uniforms;
  5499. {
  5500. RD::Uniform u;
  5501. u.type = RD::UNIFORM_TYPE_SAMPLER;
  5502. u.binding = 0;
  5503. u.ids.resize(12);
  5504. RID *ids_ptr = u.ids.ptrw();
  5505. ids_ptr[0] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  5506. ids_ptr[1] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  5507. ids_ptr[2] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  5508. ids_ptr[3] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  5509. ids_ptr[4] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  5510. ids_ptr[5] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  5511. ids_ptr[6] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  5512. ids_ptr[7] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  5513. ids_ptr[8] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  5514. ids_ptr[9] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  5515. ids_ptr[10] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  5516. ids_ptr[11] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  5517. uniforms.push_back(u);
  5518. }
  5519. {
  5520. RD::Uniform u;
  5521. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  5522. u.binding = 1;
  5523. u.ids.push_back(storage->global_variables_get_storage_buffer());
  5524. uniforms.push_back(u);
  5525. }
  5526. sky_scene_state.sampler_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, sky_shader.default_shader_rd, SKY_SET_SAMPLERS);
  5527. }
  5528. {
  5529. Vector<String> preprocess_modes;
  5530. preprocess_modes.push_back("\n#define MODE_SCROLL\n");
  5531. preprocess_modes.push_back("\n#define MODE_SCROLL_OCCLUSION\n");
  5532. preprocess_modes.push_back("\n#define MODE_INITIALIZE_JUMP_FLOOD\n");
  5533. preprocess_modes.push_back("\n#define MODE_INITIALIZE_JUMP_FLOOD_HALF\n");
  5534. preprocess_modes.push_back("\n#define MODE_JUMPFLOOD\n");
  5535. preprocess_modes.push_back("\n#define MODE_JUMPFLOOD_OPTIMIZED\n");
  5536. preprocess_modes.push_back("\n#define MODE_UPSCALE_JUMP_FLOOD\n");
  5537. preprocess_modes.push_back("\n#define MODE_OCCLUSION\n");
  5538. preprocess_modes.push_back("\n#define MODE_STORE\n");
  5539. String defines = "\n#define OCCLUSION_SIZE " + itos(SDFGI::CASCADE_SIZE / SDFGI::PROBE_DIVISOR) + "\n";
  5540. sdfgi_shader.preprocess.initialize(preprocess_modes, defines);
  5541. sdfgi_shader.preprocess_shader = sdfgi_shader.preprocess.version_create();
  5542. for (int i = 0; i < SDGIShader::PRE_PROCESS_MAX; i++) {
  5543. sdfgi_shader.preprocess_pipeline[i] = RD::get_singleton()->compute_pipeline_create(sdfgi_shader.preprocess.version_get_shader(sdfgi_shader.preprocess_shader, i));
  5544. }
  5545. }
  5546. {
  5547. //calculate tables
  5548. String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
  5549. Vector<String> direct_light_modes;
  5550. direct_light_modes.push_back("\n#define MODE_PROCESS_STATIC\n");
  5551. direct_light_modes.push_back("\n#define MODE_PROCESS_DYNAMIC\n");
  5552. sdfgi_shader.direct_light.initialize(direct_light_modes, defines);
  5553. sdfgi_shader.direct_light_shader = sdfgi_shader.direct_light.version_create();
  5554. for (int i = 0; i < SDGIShader::DIRECT_LIGHT_MODE_MAX; i++) {
  5555. 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));
  5556. }
  5557. }
  5558. {
  5559. //calculate tables
  5560. String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
  5561. defines += "\n#define SH_SIZE " + itos(SDFGI::SH_SIZE) + "\n";
  5562. Vector<String> integrate_modes;
  5563. integrate_modes.push_back("\n#define MODE_PROCESS\n");
  5564. integrate_modes.push_back("\n#define MODE_STORE\n");
  5565. integrate_modes.push_back("\n#define MODE_SCROLL\n");
  5566. integrate_modes.push_back("\n#define MODE_SCROLL_STORE\n");
  5567. sdfgi_shader.integrate.initialize(integrate_modes, defines);
  5568. sdfgi_shader.integrate_shader = sdfgi_shader.integrate.version_create();
  5569. for (int i = 0; i < SDGIShader::INTEGRATE_MODE_MAX; i++) {
  5570. sdfgi_shader.integrate_pipeline[i] = RD::get_singleton()->compute_pipeline_create(sdfgi_shader.integrate.version_get_shader(sdfgi_shader.integrate_shader, i));
  5571. }
  5572. {
  5573. Vector<RD::Uniform> uniforms;
  5574. {
  5575. RD::Uniform u;
  5576. u.type = RD::UNIFORM_TYPE_TEXTURE;
  5577. u.binding = 0;
  5578. u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE));
  5579. uniforms.push_back(u);
  5580. }
  5581. {
  5582. RD::Uniform u;
  5583. u.type = RD::UNIFORM_TYPE_SAMPLER;
  5584. u.binding = 1;
  5585. u.ids.push_back(storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED));
  5586. uniforms.push_back(u);
  5587. }
  5588. 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);
  5589. }
  5590. }
  5591. {
  5592. //calculate tables
  5593. String defines = "\n#define SDFGI_OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
  5594. Vector<String> gi_modes;
  5595. gi_modes.push_back("");
  5596. gi.shader.initialize(gi_modes, defines);
  5597. gi.shader_version = gi.shader.version_create();
  5598. for (int i = 0; i < GI::MODE_MAX; i++) {
  5599. gi.pipelines[i] = RD::get_singleton()->compute_pipeline_create(gi.shader.version_get_shader(gi.shader_version, i));
  5600. }
  5601. gi.sdfgi_ubo = RD::get_singleton()->uniform_buffer_create(sizeof(GI::SDFGIData));
  5602. }
  5603. {
  5604. String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
  5605. Vector<String> debug_modes;
  5606. debug_modes.push_back("");
  5607. sdfgi_shader.debug.initialize(debug_modes, defines);
  5608. sdfgi_shader.debug_shader = sdfgi_shader.debug.version_create();
  5609. sdfgi_shader.debug_shader_version = sdfgi_shader.debug.version_get_shader(sdfgi_shader.debug_shader, 0);
  5610. sdfgi_shader.debug_pipeline = RD::get_singleton()->compute_pipeline_create(sdfgi_shader.debug_shader_version);
  5611. }
  5612. {
  5613. String defines = "\n#define OCT_SIZE " + itos(SDFGI::LIGHTPROBE_OCT_SIZE) + "\n";
  5614. Vector<String> versions;
  5615. versions.push_back("\n#define MODE_PROBES\n");
  5616. versions.push_back("\n#define MODE_VISIBILITY\n");
  5617. sdfgi_shader.debug_probes.initialize(versions, defines);
  5618. sdfgi_shader.debug_probes_shader = sdfgi_shader.debug_probes.version_create();
  5619. {
  5620. RD::PipelineRasterizationState rs;
  5621. rs.cull_mode = RD::POLYGON_CULL_DISABLED;
  5622. RD::PipelineDepthStencilState ds;
  5623. ds.enable_depth_test = true;
  5624. ds.enable_depth_write = true;
  5625. ds.depth_compare_operator = RD::COMPARE_OP_LESS_OR_EQUAL;
  5626. for (int i = 0; i < SDGIShader::PROBE_DEBUG_MAX; i++) {
  5627. RID debug_probes_shader_version = sdfgi_shader.debug_probes.version_get_shader(sdfgi_shader.debug_probes_shader, i);
  5628. 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);
  5629. }
  5630. }
  5631. }
  5632. default_giprobe_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(GI::GIProbeData) * RenderBuffers::MAX_GIPROBES);
  5633. camera_effects_set_dof_blur_bokeh_shape(RS::DOFBokehShape(int(GLOBAL_GET("rendering/quality/depth_of_field/depth_of_field_bokeh_shape"))));
  5634. 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"));
  5635. environment_set_ssao_quality(RS::EnvironmentSSAOQuality(int(GLOBAL_GET("rendering/quality/ssao/quality"))), GLOBAL_GET("rendering/quality/ssao/half_size"));
  5636. screen_space_roughness_limiter = GLOBAL_GET("rendering/quality/screen_filters/screen_space_roughness_limiter_enabled");
  5637. screen_space_roughness_limiter_amount = GLOBAL_GET("rendering/quality/screen_filters/screen_space_roughness_limiter_amount");
  5638. screen_space_roughness_limiter_limit = GLOBAL_GET("rendering/quality/screen_filters/screen_space_roughness_limiter_limit");
  5639. glow_bicubic_upscale = int(GLOBAL_GET("rendering/quality/glow/upscale_mode")) > 0;
  5640. ssr_roughness_quality = RS::EnvironmentSSRRoughnessQuality(int(GLOBAL_GET("rendering/quality/screen_space_reflection/roughness_quality")));
  5641. sss_quality = RS::SubSurfaceScatteringQuality(int(GLOBAL_GET("rendering/quality/subsurface_scattering/subsurface_scattering_quality")));
  5642. sss_scale = GLOBAL_GET("rendering/quality/subsurface_scattering/subsurface_scattering_scale");
  5643. sss_depth_scale = GLOBAL_GET("rendering/quality/subsurface_scattering/subsurface_scattering_depth_scale");
  5644. directional_penumbra_shadow_kernel = memnew_arr(float, 128);
  5645. directional_soft_shadow_kernel = memnew_arr(float, 128);
  5646. penumbra_shadow_kernel = memnew_arr(float, 128);
  5647. soft_shadow_kernel = memnew_arr(float, 128);
  5648. shadows_quality_set(RS::ShadowQuality(int(GLOBAL_GET("rendering/quality/shadows/soft_shadow_quality"))));
  5649. directional_shadow_quality_set(RS::ShadowQuality(int(GLOBAL_GET("rendering/quality/directional_shadow/soft_shadow_quality"))));
  5650. }
  5651. RasterizerSceneRD::~RasterizerSceneRD() {
  5652. for (Map<Vector2i, ShadowMap>::Element *E = shadow_maps.front(); E; E = E->next()) {
  5653. RD::get_singleton()->free(E->get().depth);
  5654. }
  5655. for (Map<int, ShadowCubemap>::Element *E = shadow_cubemaps.front(); E; E = E->next()) {
  5656. RD::get_singleton()->free(E->get().cubemap);
  5657. }
  5658. if (sky_scene_state.sampler_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(sky_scene_state.sampler_uniform_set)) {
  5659. RD::get_singleton()->free(sky_scene_state.sampler_uniform_set);
  5660. }
  5661. if (sky_scene_state.light_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(sky_scene_state.light_uniform_set)) {
  5662. RD::get_singleton()->free(sky_scene_state.light_uniform_set);
  5663. }
  5664. RD::get_singleton()->free(default_giprobe_buffer);
  5665. RD::get_singleton()->free(gi_probe_lights_uniform);
  5666. RD::get_singleton()->free(gi.sdfgi_ubo);
  5667. giprobe_debug_shader.version_free(giprobe_debug_shader_version);
  5668. giprobe_shader.version_free(giprobe_lighting_shader_version);
  5669. gi.shader.version_free(gi.shader_version);
  5670. sdfgi_shader.debug_probes.version_free(sdfgi_shader.debug_probes_shader);
  5671. sdfgi_shader.debug.version_free(sdfgi_shader.debug_shader);
  5672. sdfgi_shader.direct_light.version_free(sdfgi_shader.direct_light_shader);
  5673. sdfgi_shader.integrate.version_free(sdfgi_shader.integrate_shader);
  5674. sdfgi_shader.preprocess.version_free(sdfgi_shader.preprocess_shader);
  5675. memdelete_arr(gi_probe_lights);
  5676. SkyMaterialData *md = (SkyMaterialData *)storage->material_get_data(sky_shader.default_material, RasterizerStorageRD::SHADER_TYPE_SKY);
  5677. sky_shader.shader.version_free(md->shader_data->version);
  5678. RD::get_singleton()->free(sky_scene_state.directional_light_buffer);
  5679. memdelete_arr(sky_scene_state.directional_lights);
  5680. memdelete_arr(sky_scene_state.last_frame_directional_lights);
  5681. storage->free(sky_shader.default_shader);
  5682. storage->free(sky_shader.default_material);
  5683. memdelete_arr(directional_penumbra_shadow_kernel);
  5684. memdelete_arr(directional_soft_shadow_kernel);
  5685. memdelete_arr(penumbra_shadow_kernel);
  5686. memdelete_arr(soft_shadow_kernel);
  5687. }