renderer_scene_cull.cpp 161 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189
  1. /**************************************************************************/
  2. /* renderer_scene_cull.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "renderer_scene_cull.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/object/worker_thread_pool.h"
  33. #include "core/os/os.h"
  34. #include "rendering_server_default.h"
  35. #include <new>
  36. /* HALTON SEQUENCE */
  37. #ifndef _3D_DISABLED
  38. static float get_halton_value(int p_index, int p_base) {
  39. float f = 1;
  40. float r = 0;
  41. while (p_index > 0) {
  42. f = f / static_cast<float>(p_base);
  43. r = r + f * (p_index % p_base);
  44. p_index = p_index / p_base;
  45. }
  46. return r * 2.0f - 1.0f;
  47. }
  48. #endif // _3D_DISABLED
  49. /* CAMERA API */
  50. RID RendererSceneCull::camera_allocate() {
  51. return camera_owner.allocate_rid();
  52. }
  53. void RendererSceneCull::camera_initialize(RID p_rid) {
  54. camera_owner.initialize_rid(p_rid);
  55. }
  56. void RendererSceneCull::camera_set_perspective(RID p_camera, float p_fovy_degrees, float p_z_near, float p_z_far) {
  57. Camera *camera = camera_owner.get_or_null(p_camera);
  58. ERR_FAIL_COND(!camera);
  59. camera->type = Camera::PERSPECTIVE;
  60. camera->fov = p_fovy_degrees;
  61. camera->znear = p_z_near;
  62. camera->zfar = p_z_far;
  63. }
  64. void RendererSceneCull::camera_set_orthogonal(RID p_camera, float p_size, float p_z_near, float p_z_far) {
  65. Camera *camera = camera_owner.get_or_null(p_camera);
  66. ERR_FAIL_COND(!camera);
  67. camera->type = Camera::ORTHOGONAL;
  68. camera->size = p_size;
  69. camera->znear = p_z_near;
  70. camera->zfar = p_z_far;
  71. }
  72. void RendererSceneCull::camera_set_frustum(RID p_camera, float p_size, Vector2 p_offset, float p_z_near, float p_z_far) {
  73. Camera *camera = camera_owner.get_or_null(p_camera);
  74. ERR_FAIL_COND(!camera);
  75. camera->type = Camera::FRUSTUM;
  76. camera->size = p_size;
  77. camera->offset = p_offset;
  78. camera->znear = p_z_near;
  79. camera->zfar = p_z_far;
  80. }
  81. void RendererSceneCull::camera_set_transform(RID p_camera, const Transform3D &p_transform) {
  82. Camera *camera = camera_owner.get_or_null(p_camera);
  83. ERR_FAIL_COND(!camera);
  84. camera->transform = p_transform.orthonormalized();
  85. }
  86. void RendererSceneCull::camera_set_cull_mask(RID p_camera, uint32_t p_layers) {
  87. Camera *camera = camera_owner.get_or_null(p_camera);
  88. ERR_FAIL_COND(!camera);
  89. camera->visible_layers = p_layers;
  90. }
  91. void RendererSceneCull::camera_set_environment(RID p_camera, RID p_env) {
  92. Camera *camera = camera_owner.get_or_null(p_camera);
  93. ERR_FAIL_COND(!camera);
  94. camera->env = p_env;
  95. }
  96. void RendererSceneCull::camera_set_camera_attributes(RID p_camera, RID p_attributes) {
  97. Camera *camera = camera_owner.get_or_null(p_camera);
  98. ERR_FAIL_COND(!camera);
  99. camera->attributes = p_attributes;
  100. }
  101. void RendererSceneCull::camera_set_use_vertical_aspect(RID p_camera, bool p_enable) {
  102. Camera *camera = camera_owner.get_or_null(p_camera);
  103. ERR_FAIL_COND(!camera);
  104. camera->vaspect = p_enable;
  105. }
  106. bool RendererSceneCull::is_camera(RID p_camera) const {
  107. return camera_owner.owns(p_camera);
  108. }
  109. /* OCCLUDER API */
  110. RID RendererSceneCull::occluder_allocate() {
  111. return RendererSceneOcclusionCull::get_singleton()->occluder_allocate();
  112. }
  113. void RendererSceneCull::occluder_initialize(RID p_rid) {
  114. RendererSceneOcclusionCull::get_singleton()->occluder_initialize(p_rid);
  115. }
  116. void RendererSceneCull::occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices) {
  117. RendererSceneOcclusionCull::get_singleton()->occluder_set_mesh(p_occluder, p_vertices, p_indices);
  118. }
  119. /* SCENARIO API */
  120. void RendererSceneCull::_instance_pair(Instance *p_A, Instance *p_B) {
  121. RendererSceneCull *self = (RendererSceneCull *)singleton;
  122. Instance *A = p_A;
  123. Instance *B = p_B;
  124. //instance indices are designed so greater always contains lesser
  125. if (A->base_type > B->base_type) {
  126. SWAP(A, B); //lesser always first
  127. }
  128. if (B->base_type == RS::INSTANCE_LIGHT && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  129. InstanceLightData *light = static_cast<InstanceLightData *>(B->base_data);
  130. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  131. geom->lights.insert(B);
  132. light->geometries.insert(A);
  133. if (geom->can_cast_shadows) {
  134. light->shadow_dirty = true;
  135. }
  136. if (A->scenario && A->array_index >= 0) {
  137. InstanceData &idata = A->scenario->instance_data[A->array_index];
  138. idata.flags |= InstanceData::FLAG_GEOM_LIGHTING_DIRTY;
  139. }
  140. if (light->uses_projector) {
  141. geom->projector_count++;
  142. if (geom->projector_count == 1) {
  143. InstanceData &idata = A->scenario->instance_data[A->array_index];
  144. idata.flags |= InstanceData::FLAG_GEOM_PROJECTOR_SOFTSHADOW_DIRTY;
  145. }
  146. }
  147. if (light->uses_softshadow) {
  148. geom->softshadow_count++;
  149. if (geom->softshadow_count == 1) {
  150. InstanceData &idata = A->scenario->instance_data[A->array_index];
  151. idata.flags |= InstanceData::FLAG_GEOM_PROJECTOR_SOFTSHADOW_DIRTY;
  152. }
  153. }
  154. } else if (self->geometry_instance_pair_mask & (1 << RS::INSTANCE_REFLECTION_PROBE) && B->base_type == RS::INSTANCE_REFLECTION_PROBE && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  155. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(B->base_data);
  156. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  157. geom->reflection_probes.insert(B);
  158. reflection_probe->geometries.insert(A);
  159. if (A->scenario && A->array_index >= 0) {
  160. InstanceData &idata = A->scenario->instance_data[A->array_index];
  161. idata.flags |= InstanceData::FLAG_GEOM_REFLECTION_DIRTY;
  162. }
  163. } else if (self->geometry_instance_pair_mask & (1 << RS::INSTANCE_DECAL) && B->base_type == RS::INSTANCE_DECAL && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  164. InstanceDecalData *decal = static_cast<InstanceDecalData *>(B->base_data);
  165. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  166. geom->decals.insert(B);
  167. decal->geometries.insert(A);
  168. if (A->scenario && A->array_index >= 0) {
  169. InstanceData &idata = A->scenario->instance_data[A->array_index];
  170. idata.flags |= InstanceData::FLAG_GEOM_DECAL_DIRTY;
  171. }
  172. } else if (B->base_type == RS::INSTANCE_LIGHTMAP && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  173. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(B->base_data);
  174. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  175. if (A->dynamic_gi) {
  176. geom->lightmap_captures.insert(B);
  177. lightmap_data->geometries.insert(A);
  178. if (A->scenario && A->array_index >= 0) {
  179. InstanceData &idata = A->scenario->instance_data[A->array_index];
  180. idata.flags |= InstanceData::FLAG_LIGHTMAP_CAPTURE;
  181. }
  182. ((RendererSceneCull *)self)->_instance_queue_update(A, false, false); //need to update capture
  183. }
  184. } else if (self->geometry_instance_pair_mask & (1 << RS::INSTANCE_VOXEL_GI) && B->base_type == RS::INSTANCE_VOXEL_GI && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  185. InstanceVoxelGIData *voxel_gi = static_cast<InstanceVoxelGIData *>(B->base_data);
  186. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  187. geom->voxel_gi_instances.insert(B);
  188. if (A->dynamic_gi) {
  189. voxel_gi->dynamic_geometries.insert(A);
  190. } else {
  191. voxel_gi->geometries.insert(A);
  192. }
  193. if (A->scenario && A->array_index >= 0) {
  194. InstanceData &idata = A->scenario->instance_data[A->array_index];
  195. idata.flags |= InstanceData::FLAG_GEOM_VOXEL_GI_DIRTY;
  196. }
  197. } else if (B->base_type == RS::INSTANCE_VOXEL_GI && A->base_type == RS::INSTANCE_LIGHT) {
  198. InstanceVoxelGIData *voxel_gi = static_cast<InstanceVoxelGIData *>(B->base_data);
  199. voxel_gi->lights.insert(A);
  200. } else if (B->base_type == RS::INSTANCE_PARTICLES_COLLISION && A->base_type == RS::INSTANCE_PARTICLES) {
  201. InstanceParticlesCollisionData *collision = static_cast<InstanceParticlesCollisionData *>(B->base_data);
  202. RSG::particles_storage->particles_add_collision(A->base, collision->instance);
  203. }
  204. }
  205. void RendererSceneCull::_instance_unpair(Instance *p_A, Instance *p_B) {
  206. RendererSceneCull *self = (RendererSceneCull *)singleton;
  207. Instance *A = p_A;
  208. Instance *B = p_B;
  209. //instance indices are designed so greater always contains lesser
  210. if (A->base_type > B->base_type) {
  211. SWAP(A, B); //lesser always first
  212. }
  213. if (B->base_type == RS::INSTANCE_LIGHT && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  214. InstanceLightData *light = static_cast<InstanceLightData *>(B->base_data);
  215. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  216. geom->lights.erase(B);
  217. light->geometries.erase(A);
  218. if (geom->can_cast_shadows) {
  219. light->shadow_dirty = true;
  220. }
  221. if (A->scenario && A->array_index >= 0) {
  222. InstanceData &idata = A->scenario->instance_data[A->array_index];
  223. idata.flags |= InstanceData::FLAG_GEOM_LIGHTING_DIRTY;
  224. }
  225. if (light->uses_projector) {
  226. #ifdef DEBUG_ENABLED
  227. if (geom->projector_count == 0) {
  228. ERR_PRINT("geom->projector_count==0 - BUG!");
  229. }
  230. #endif
  231. geom->projector_count--;
  232. if (geom->projector_count == 0) {
  233. InstanceData &idata = A->scenario->instance_data[A->array_index];
  234. idata.flags |= InstanceData::FLAG_GEOM_PROJECTOR_SOFTSHADOW_DIRTY;
  235. }
  236. }
  237. if (light->uses_softshadow) {
  238. #ifdef DEBUG_ENABLED
  239. if (geom->softshadow_count == 0) {
  240. ERR_PRINT("geom->softshadow_count==0 - BUG!");
  241. }
  242. #endif
  243. geom->softshadow_count--;
  244. if (geom->softshadow_count == 0) {
  245. InstanceData &idata = A->scenario->instance_data[A->array_index];
  246. idata.flags |= InstanceData::FLAG_GEOM_PROJECTOR_SOFTSHADOW_DIRTY;
  247. }
  248. }
  249. } else if (self->geometry_instance_pair_mask & (1 << RS::INSTANCE_REFLECTION_PROBE) && B->base_type == RS::INSTANCE_REFLECTION_PROBE && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  250. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(B->base_data);
  251. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  252. geom->reflection_probes.erase(B);
  253. reflection_probe->geometries.erase(A);
  254. if (A->scenario && A->array_index >= 0) {
  255. InstanceData &idata = A->scenario->instance_data[A->array_index];
  256. idata.flags |= InstanceData::FLAG_GEOM_REFLECTION_DIRTY;
  257. }
  258. } else if (self->geometry_instance_pair_mask & (1 << RS::INSTANCE_DECAL) && B->base_type == RS::INSTANCE_DECAL && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  259. InstanceDecalData *decal = static_cast<InstanceDecalData *>(B->base_data);
  260. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  261. geom->decals.erase(B);
  262. decal->geometries.erase(A);
  263. if (A->scenario && A->array_index >= 0) {
  264. InstanceData &idata = A->scenario->instance_data[A->array_index];
  265. idata.flags |= InstanceData::FLAG_GEOM_DECAL_DIRTY;
  266. }
  267. } else if (B->base_type == RS::INSTANCE_LIGHTMAP && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  268. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(B->base_data);
  269. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  270. if (A->dynamic_gi) {
  271. geom->lightmap_captures.erase(B);
  272. if (geom->lightmap_captures.is_empty() && A->scenario && A->array_index >= 0) {
  273. InstanceData &idata = A->scenario->instance_data[A->array_index];
  274. idata.flags &= ~uint32_t(InstanceData::FLAG_LIGHTMAP_CAPTURE);
  275. }
  276. lightmap_data->geometries.erase(A);
  277. ((RendererSceneCull *)self)->_instance_queue_update(A, false, false); //need to update capture
  278. }
  279. } else if (self->geometry_instance_pair_mask & (1 << RS::INSTANCE_VOXEL_GI) && B->base_type == RS::INSTANCE_VOXEL_GI && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  280. InstanceVoxelGIData *voxel_gi = static_cast<InstanceVoxelGIData *>(B->base_data);
  281. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  282. geom->voxel_gi_instances.erase(B);
  283. if (A->dynamic_gi) {
  284. voxel_gi->dynamic_geometries.erase(A);
  285. } else {
  286. voxel_gi->geometries.erase(A);
  287. }
  288. if (A->scenario && A->array_index >= 0) {
  289. InstanceData &idata = A->scenario->instance_data[A->array_index];
  290. idata.flags |= InstanceData::FLAG_GEOM_VOXEL_GI_DIRTY;
  291. }
  292. } else if (B->base_type == RS::INSTANCE_VOXEL_GI && A->base_type == RS::INSTANCE_LIGHT) {
  293. InstanceVoxelGIData *voxel_gi = static_cast<InstanceVoxelGIData *>(B->base_data);
  294. voxel_gi->lights.erase(A);
  295. } else if (B->base_type == RS::INSTANCE_PARTICLES_COLLISION && A->base_type == RS::INSTANCE_PARTICLES) {
  296. InstanceParticlesCollisionData *collision = static_cast<InstanceParticlesCollisionData *>(B->base_data);
  297. RSG::particles_storage->particles_remove_collision(A->base, collision->instance);
  298. }
  299. }
  300. RID RendererSceneCull::scenario_allocate() {
  301. return scenario_owner.allocate_rid();
  302. }
  303. void RendererSceneCull::scenario_initialize(RID p_rid) {
  304. scenario_owner.initialize_rid(p_rid);
  305. Scenario *scenario = scenario_owner.get_or_null(p_rid);
  306. scenario->self = p_rid;
  307. scenario->reflection_probe_shadow_atlas = RSG::light_storage->shadow_atlas_create();
  308. RSG::light_storage->shadow_atlas_set_size(scenario->reflection_probe_shadow_atlas, 1024); //make enough shadows for close distance, don't bother with rest
  309. RSG::light_storage->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas, 0, 4);
  310. RSG::light_storage->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas, 1, 4);
  311. RSG::light_storage->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas, 2, 4);
  312. RSG::light_storage->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas, 3, 8);
  313. scenario->reflection_atlas = RSG::light_storage->reflection_atlas_create();
  314. scenario->instance_aabbs.set_page_pool(&instance_aabb_page_pool);
  315. scenario->instance_data.set_page_pool(&instance_data_page_pool);
  316. scenario->instance_visibility.set_page_pool(&instance_visibility_data_page_pool);
  317. RendererSceneOcclusionCull::get_singleton()->add_scenario(p_rid);
  318. }
  319. void RendererSceneCull::scenario_set_environment(RID p_scenario, RID p_environment) {
  320. Scenario *scenario = scenario_owner.get_or_null(p_scenario);
  321. ERR_FAIL_COND(!scenario);
  322. scenario->environment = p_environment;
  323. }
  324. void RendererSceneCull::scenario_set_camera_attributes(RID p_scenario, RID p_camera_attributes) {
  325. Scenario *scenario = scenario_owner.get_or_null(p_scenario);
  326. ERR_FAIL_COND(!scenario);
  327. scenario->camera_attributes = p_camera_attributes;
  328. }
  329. void RendererSceneCull::scenario_set_fallback_environment(RID p_scenario, RID p_environment) {
  330. Scenario *scenario = scenario_owner.get_or_null(p_scenario);
  331. ERR_FAIL_COND(!scenario);
  332. scenario->fallback_environment = p_environment;
  333. }
  334. void RendererSceneCull::scenario_set_reflection_atlas_size(RID p_scenario, int p_reflection_size, int p_reflection_count) {
  335. Scenario *scenario = scenario_owner.get_or_null(p_scenario);
  336. ERR_FAIL_COND(!scenario);
  337. RSG::light_storage->reflection_atlas_set_size(scenario->reflection_atlas, p_reflection_size, p_reflection_count);
  338. }
  339. bool RendererSceneCull::is_scenario(RID p_scenario) const {
  340. return scenario_owner.owns(p_scenario);
  341. }
  342. RID RendererSceneCull::scenario_get_environment(RID p_scenario) {
  343. Scenario *scenario = scenario_owner.get_or_null(p_scenario);
  344. ERR_FAIL_COND_V(!scenario, RID());
  345. return scenario->environment;
  346. }
  347. void RendererSceneCull::scenario_remove_viewport_visibility_mask(RID p_scenario, RID p_viewport) {
  348. Scenario *scenario = scenario_owner.get_or_null(p_scenario);
  349. ERR_FAIL_COND(!scenario);
  350. if (!scenario->viewport_visibility_masks.has(p_viewport)) {
  351. return;
  352. }
  353. uint64_t mask = scenario->viewport_visibility_masks[p_viewport];
  354. scenario->used_viewport_visibility_bits &= ~mask;
  355. scenario->viewport_visibility_masks.erase(p_viewport);
  356. }
  357. void RendererSceneCull::scenario_add_viewport_visibility_mask(RID p_scenario, RID p_viewport) {
  358. Scenario *scenario = scenario_owner.get_or_null(p_scenario);
  359. ERR_FAIL_COND(!scenario);
  360. ERR_FAIL_COND(scenario->viewport_visibility_masks.has(p_viewport));
  361. uint64_t new_mask = 1;
  362. while (new_mask & scenario->used_viewport_visibility_bits) {
  363. new_mask <<= 1;
  364. }
  365. if (new_mask == 0) {
  366. ERR_PRINT("Only 64 viewports per scenario allowed when using visibility ranges.");
  367. new_mask = ((uint64_t)1) << 63;
  368. }
  369. scenario->viewport_visibility_masks[p_viewport] = new_mask;
  370. scenario->used_viewport_visibility_bits |= new_mask;
  371. }
  372. /* INSTANCING API */
  373. void RendererSceneCull::_instance_queue_update(Instance *p_instance, bool p_update_aabb, bool p_update_dependencies) {
  374. if (p_update_aabb) {
  375. p_instance->update_aabb = true;
  376. }
  377. if (p_update_dependencies) {
  378. p_instance->update_dependencies = true;
  379. }
  380. if (p_instance->update_item.in_list()) {
  381. return;
  382. }
  383. _instance_update_list.add(&p_instance->update_item);
  384. }
  385. RID RendererSceneCull::instance_allocate() {
  386. return instance_owner.allocate_rid();
  387. }
  388. void RendererSceneCull::instance_initialize(RID p_rid) {
  389. instance_owner.initialize_rid(p_rid);
  390. Instance *instance = instance_owner.get_or_null(p_rid);
  391. instance->self = p_rid;
  392. }
  393. void RendererSceneCull::_instance_update_mesh_instance(Instance *p_instance) {
  394. bool needs_instance = RSG::mesh_storage->mesh_needs_instance(p_instance->base, p_instance->skeleton.is_valid());
  395. if (needs_instance != p_instance->mesh_instance.is_valid()) {
  396. if (needs_instance) {
  397. p_instance->mesh_instance = RSG::mesh_storage->mesh_instance_create(p_instance->base);
  398. } else {
  399. RSG::mesh_storage->mesh_instance_free(p_instance->mesh_instance);
  400. p_instance->mesh_instance = RID();
  401. }
  402. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  403. geom->geometry_instance->set_mesh_instance(p_instance->mesh_instance);
  404. if (p_instance->scenario && p_instance->array_index >= 0) {
  405. InstanceData &idata = p_instance->scenario->instance_data[p_instance->array_index];
  406. if (p_instance->mesh_instance.is_valid()) {
  407. idata.flags |= InstanceData::FLAG_USES_MESH_INSTANCE;
  408. } else {
  409. idata.flags &= ~uint32_t(InstanceData::FLAG_USES_MESH_INSTANCE);
  410. }
  411. }
  412. }
  413. if (p_instance->mesh_instance.is_valid()) {
  414. RSG::mesh_storage->mesh_instance_set_skeleton(p_instance->mesh_instance, p_instance->skeleton);
  415. }
  416. }
  417. void RendererSceneCull::instance_set_base(RID p_instance, RID p_base) {
  418. Instance *instance = instance_owner.get_or_null(p_instance);
  419. ERR_FAIL_COND(!instance);
  420. Scenario *scenario = instance->scenario;
  421. if (instance->base_type != RS::INSTANCE_NONE) {
  422. //free anything related to that base
  423. if (scenario && instance->indexer_id.is_valid()) {
  424. _unpair_instance(instance);
  425. }
  426. if (instance->mesh_instance.is_valid()) {
  427. RSG::mesh_storage->mesh_instance_free(instance->mesh_instance);
  428. instance->mesh_instance = RID();
  429. // no need to set instance data flag here, as it was freed above
  430. }
  431. switch (instance->base_type) {
  432. case RS::INSTANCE_MESH:
  433. case RS::INSTANCE_MULTIMESH:
  434. case RS::INSTANCE_PARTICLES: {
  435. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(instance->base_data);
  436. scene_render->geometry_instance_free(geom->geometry_instance);
  437. } break;
  438. case RS::INSTANCE_LIGHT: {
  439. InstanceLightData *light = static_cast<InstanceLightData *>(instance->base_data);
  440. if (scenario && instance->visible && RSG::light_storage->light_get_type(instance->base) != RS::LIGHT_DIRECTIONAL && light->bake_mode == RS::LIGHT_BAKE_DYNAMIC) {
  441. scenario->dynamic_lights.erase(light->instance);
  442. }
  443. #ifdef DEBUG_ENABLED
  444. if (light->geometries.size()) {
  445. ERR_PRINT("BUG, indexing did not unpair geometries from light.");
  446. }
  447. #endif
  448. if (scenario && light->D) {
  449. scenario->directional_lights.erase(light->D);
  450. light->D = nullptr;
  451. }
  452. RSG::light_storage->light_instance_free(light->instance);
  453. } break;
  454. case RS::INSTANCE_PARTICLES_COLLISION: {
  455. InstanceParticlesCollisionData *collision = static_cast<InstanceParticlesCollisionData *>(instance->base_data);
  456. RSG::utilities->free(collision->instance);
  457. } break;
  458. case RS::INSTANCE_FOG_VOLUME: {
  459. InstanceFogVolumeData *volume = static_cast<InstanceFogVolumeData *>(instance->base_data);
  460. scene_render->free(volume->instance);
  461. } break;
  462. case RS::INSTANCE_VISIBLITY_NOTIFIER: {
  463. //none
  464. } break;
  465. case RS::INSTANCE_REFLECTION_PROBE: {
  466. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(instance->base_data);
  467. RSG::light_storage->reflection_probe_instance_free(reflection_probe->instance);
  468. if (reflection_probe->update_list.in_list()) {
  469. reflection_probe_render_list.remove(&reflection_probe->update_list);
  470. }
  471. } break;
  472. case RS::INSTANCE_DECAL: {
  473. InstanceDecalData *decal = static_cast<InstanceDecalData *>(instance->base_data);
  474. RSG::texture_storage->decal_instance_free(decal->instance);
  475. } break;
  476. case RS::INSTANCE_LIGHTMAP: {
  477. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(instance->base_data);
  478. //erase dependencies, since no longer a lightmap
  479. while (lightmap_data->users.begin()) {
  480. instance_geometry_set_lightmap((*lightmap_data->users.begin())->self, RID(), Rect2(), 0);
  481. }
  482. RSG::light_storage->lightmap_instance_free(lightmap_data->instance);
  483. } break;
  484. case RS::INSTANCE_VOXEL_GI: {
  485. InstanceVoxelGIData *voxel_gi = static_cast<InstanceVoxelGIData *>(instance->base_data);
  486. #ifdef DEBUG_ENABLED
  487. if (voxel_gi->geometries.size()) {
  488. ERR_PRINT("BUG, indexing did not unpair geometries from VoxelGI.");
  489. }
  490. #endif
  491. #ifdef DEBUG_ENABLED
  492. if (voxel_gi->lights.size()) {
  493. ERR_PRINT("BUG, indexing did not unpair lights from VoxelGI.");
  494. }
  495. #endif
  496. if (voxel_gi->update_element.in_list()) {
  497. voxel_gi_update_list.remove(&voxel_gi->update_element);
  498. }
  499. scene_render->free(voxel_gi->probe_instance);
  500. } break;
  501. case RS::INSTANCE_OCCLUDER: {
  502. if (scenario && instance->visible) {
  503. RendererSceneOcclusionCull::get_singleton()->scenario_remove_instance(instance->scenario->self, p_instance);
  504. }
  505. } break;
  506. default: {
  507. }
  508. }
  509. if (instance->base_data) {
  510. memdelete(instance->base_data);
  511. instance->base_data = nullptr;
  512. }
  513. instance->materials.clear();
  514. }
  515. instance->base_type = RS::INSTANCE_NONE;
  516. instance->base = RID();
  517. if (p_base.is_valid()) {
  518. instance->base_type = RSG::utilities->get_base_type(p_base);
  519. // fix up a specific malfunctioning case before the switch, so it can be handled
  520. if (instance->base_type == RS::INSTANCE_NONE && RendererSceneOcclusionCull::get_singleton()->is_occluder(p_base)) {
  521. instance->base_type = RS::INSTANCE_OCCLUDER;
  522. }
  523. switch (instance->base_type) {
  524. case RS::INSTANCE_NONE: {
  525. ERR_PRINT_ONCE("unimplemented base type encountered in renderer scene cull");
  526. return;
  527. }
  528. case RS::INSTANCE_LIGHT: {
  529. InstanceLightData *light = memnew(InstanceLightData);
  530. if (scenario && RSG::light_storage->light_get_type(p_base) == RS::LIGHT_DIRECTIONAL) {
  531. light->D = scenario->directional_lights.push_back(instance);
  532. }
  533. light->instance = RSG::light_storage->light_instance_create(p_base);
  534. instance->base_data = light;
  535. } break;
  536. case RS::INSTANCE_MESH:
  537. case RS::INSTANCE_MULTIMESH:
  538. case RS::INSTANCE_PARTICLES: {
  539. InstanceGeometryData *geom = memnew(InstanceGeometryData);
  540. instance->base_data = geom;
  541. geom->geometry_instance = scene_render->geometry_instance_create(p_base);
  542. ERR_FAIL_NULL(geom->geometry_instance);
  543. geom->geometry_instance->set_skeleton(instance->skeleton);
  544. geom->geometry_instance->set_material_override(instance->material_override);
  545. geom->geometry_instance->set_material_overlay(instance->material_overlay);
  546. geom->geometry_instance->set_surface_materials(instance->materials);
  547. geom->geometry_instance->set_transform(instance->transform, instance->aabb, instance->transformed_aabb);
  548. geom->geometry_instance->set_layer_mask(instance->layer_mask);
  549. geom->geometry_instance->set_pivot_data(instance->sorting_offset, instance->use_aabb_center);
  550. geom->geometry_instance->set_lod_bias(instance->lod_bias);
  551. geom->geometry_instance->set_transparency(instance->transparency);
  552. geom->geometry_instance->set_use_baked_light(instance->baked_light);
  553. geom->geometry_instance->set_use_dynamic_gi(instance->dynamic_gi);
  554. geom->geometry_instance->set_use_lightmap(RID(), instance->lightmap_uv_scale, instance->lightmap_slice_index);
  555. geom->geometry_instance->set_instance_shader_uniforms_offset(instance->instance_allocated_shader_uniforms_offset);
  556. geom->geometry_instance->set_cast_double_sided_shadows(instance->cast_shadows == RS::SHADOW_CASTING_SETTING_DOUBLE_SIDED);
  557. if (instance->lightmap_sh.size() == 9) {
  558. geom->geometry_instance->set_lightmap_capture(instance->lightmap_sh.ptr());
  559. }
  560. for (Instance *E : instance->visibility_dependencies) {
  561. Instance *dep_instance = E;
  562. ERR_CONTINUE(dep_instance->array_index == -1);
  563. ERR_CONTINUE(dep_instance->scenario->instance_data[dep_instance->array_index].parent_array_index != -1);
  564. dep_instance->scenario->instance_data[dep_instance->array_index].parent_array_index = instance->array_index;
  565. }
  566. } break;
  567. case RS::INSTANCE_PARTICLES_COLLISION: {
  568. InstanceParticlesCollisionData *collision = memnew(InstanceParticlesCollisionData);
  569. collision->instance = RSG::particles_storage->particles_collision_instance_create(p_base);
  570. RSG::particles_storage->particles_collision_instance_set_active(collision->instance, instance->visible);
  571. instance->base_data = collision;
  572. } break;
  573. case RS::INSTANCE_FOG_VOLUME: {
  574. InstanceFogVolumeData *volume = memnew(InstanceFogVolumeData);
  575. volume->instance = scene_render->fog_volume_instance_create(p_base);
  576. scene_render->fog_volume_instance_set_active(volume->instance, instance->visible);
  577. instance->base_data = volume;
  578. } break;
  579. case RS::INSTANCE_VISIBLITY_NOTIFIER: {
  580. InstanceVisibilityNotifierData *vnd = memnew(InstanceVisibilityNotifierData);
  581. vnd->base = p_base;
  582. instance->base_data = vnd;
  583. } break;
  584. case RS::INSTANCE_REFLECTION_PROBE: {
  585. InstanceReflectionProbeData *reflection_probe = memnew(InstanceReflectionProbeData);
  586. reflection_probe->owner = instance;
  587. instance->base_data = reflection_probe;
  588. reflection_probe->instance = RSG::light_storage->reflection_probe_instance_create(p_base);
  589. } break;
  590. case RS::INSTANCE_DECAL: {
  591. InstanceDecalData *decal = memnew(InstanceDecalData);
  592. decal->owner = instance;
  593. instance->base_data = decal;
  594. decal->instance = RSG::texture_storage->decal_instance_create(p_base);
  595. RSG::texture_storage->decal_instance_set_sorting_offset(decal->instance, instance->sorting_offset);
  596. } break;
  597. case RS::INSTANCE_LIGHTMAP: {
  598. InstanceLightmapData *lightmap_data = memnew(InstanceLightmapData);
  599. instance->base_data = lightmap_data;
  600. lightmap_data->instance = RSG::light_storage->lightmap_instance_create(p_base);
  601. } break;
  602. case RS::INSTANCE_VOXEL_GI: {
  603. InstanceVoxelGIData *voxel_gi = memnew(InstanceVoxelGIData);
  604. instance->base_data = voxel_gi;
  605. voxel_gi->owner = instance;
  606. if (scenario && !voxel_gi->update_element.in_list()) {
  607. voxel_gi_update_list.add(&voxel_gi->update_element);
  608. }
  609. voxel_gi->probe_instance = scene_render->voxel_gi_instance_create(p_base);
  610. } break;
  611. case RS::INSTANCE_OCCLUDER: {
  612. if (scenario) {
  613. RendererSceneOcclusionCull::get_singleton()->scenario_set_instance(scenario->self, p_instance, p_base, instance->transform, instance->visible);
  614. }
  615. } break;
  616. default: {
  617. }
  618. }
  619. instance->base = p_base;
  620. if (instance->base_type == RS::INSTANCE_MESH) {
  621. _instance_update_mesh_instance(instance);
  622. }
  623. //forcefully update the dependency now, so if for some reason it gets removed, we can immediately clear it
  624. RSG::utilities->base_update_dependency(p_base, &instance->dependency_tracker);
  625. }
  626. _instance_queue_update(instance, true, true);
  627. }
  628. void RendererSceneCull::instance_set_scenario(RID p_instance, RID p_scenario) {
  629. Instance *instance = instance_owner.get_or_null(p_instance);
  630. ERR_FAIL_COND(!instance);
  631. if (instance->scenario) {
  632. instance->scenario->instances.remove(&instance->scenario_item);
  633. if (instance->indexer_id.is_valid()) {
  634. _unpair_instance(instance);
  635. }
  636. switch (instance->base_type) {
  637. case RS::INSTANCE_LIGHT: {
  638. InstanceLightData *light = static_cast<InstanceLightData *>(instance->base_data);
  639. if (instance->visible && RSG::light_storage->light_get_type(instance->base) != RS::LIGHT_DIRECTIONAL && light->bake_mode == RS::LIGHT_BAKE_DYNAMIC) {
  640. instance->scenario->dynamic_lights.erase(light->instance);
  641. }
  642. #ifdef DEBUG_ENABLED
  643. if (light->geometries.size()) {
  644. ERR_PRINT("BUG, indexing did not unpair geometries from light.");
  645. }
  646. #endif
  647. if (light->D) {
  648. instance->scenario->directional_lights.erase(light->D);
  649. light->D = nullptr;
  650. }
  651. } break;
  652. case RS::INSTANCE_REFLECTION_PROBE: {
  653. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(instance->base_data);
  654. RSG::light_storage->reflection_probe_release_atlas_index(reflection_probe->instance);
  655. } break;
  656. case RS::INSTANCE_PARTICLES_COLLISION: {
  657. heightfield_particle_colliders_update_list.erase(instance);
  658. } break;
  659. case RS::INSTANCE_VOXEL_GI: {
  660. InstanceVoxelGIData *voxel_gi = static_cast<InstanceVoxelGIData *>(instance->base_data);
  661. #ifdef DEBUG_ENABLED
  662. if (voxel_gi->geometries.size()) {
  663. ERR_PRINT("BUG, indexing did not unpair geometries from VoxelGI.");
  664. }
  665. #endif
  666. #ifdef DEBUG_ENABLED
  667. if (voxel_gi->lights.size()) {
  668. ERR_PRINT("BUG, indexing did not unpair lights from VoxelGI.");
  669. }
  670. #endif
  671. if (voxel_gi->update_element.in_list()) {
  672. voxel_gi_update_list.remove(&voxel_gi->update_element);
  673. }
  674. } break;
  675. case RS::INSTANCE_OCCLUDER: {
  676. if (instance->visible) {
  677. RendererSceneOcclusionCull::get_singleton()->scenario_remove_instance(instance->scenario->self, p_instance);
  678. }
  679. } break;
  680. default: {
  681. }
  682. }
  683. instance->scenario = nullptr;
  684. }
  685. if (p_scenario.is_valid()) {
  686. Scenario *scenario = scenario_owner.get_or_null(p_scenario);
  687. ERR_FAIL_COND(!scenario);
  688. instance->scenario = scenario;
  689. scenario->instances.add(&instance->scenario_item);
  690. switch (instance->base_type) {
  691. case RS::INSTANCE_LIGHT: {
  692. InstanceLightData *light = static_cast<InstanceLightData *>(instance->base_data);
  693. if (RSG::light_storage->light_get_type(instance->base) == RS::LIGHT_DIRECTIONAL) {
  694. light->D = scenario->directional_lights.push_back(instance);
  695. }
  696. } break;
  697. case RS::INSTANCE_VOXEL_GI: {
  698. InstanceVoxelGIData *voxel_gi = static_cast<InstanceVoxelGIData *>(instance->base_data);
  699. if (!voxel_gi->update_element.in_list()) {
  700. voxel_gi_update_list.add(&voxel_gi->update_element);
  701. }
  702. } break;
  703. case RS::INSTANCE_OCCLUDER: {
  704. RendererSceneOcclusionCull::get_singleton()->scenario_set_instance(scenario->self, p_instance, instance->base, instance->transform, instance->visible);
  705. } break;
  706. default: {
  707. }
  708. }
  709. _instance_queue_update(instance, true, true);
  710. }
  711. }
  712. void RendererSceneCull::instance_set_layer_mask(RID p_instance, uint32_t p_mask) {
  713. Instance *instance = instance_owner.get_or_null(p_instance);
  714. ERR_FAIL_COND(!instance);
  715. if (instance->layer_mask == p_mask) {
  716. return;
  717. }
  718. instance->layer_mask = p_mask;
  719. if (instance->scenario && instance->array_index >= 0) {
  720. instance->scenario->instance_data[instance->array_index].layer_mask = p_mask;
  721. }
  722. if ((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK && instance->base_data) {
  723. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(instance->base_data);
  724. ERR_FAIL_NULL(geom->geometry_instance);
  725. geom->geometry_instance->set_layer_mask(p_mask);
  726. if (geom->can_cast_shadows) {
  727. for (HashSet<RendererSceneCull::Instance *>::Iterator I = geom->lights.begin(); I != geom->lights.end(); ++I) {
  728. InstanceLightData *light = static_cast<InstanceLightData *>((*I)->base_data);
  729. light->shadow_dirty = true;
  730. }
  731. }
  732. }
  733. }
  734. void RendererSceneCull::instance_set_pivot_data(RID p_instance, float p_sorting_offset, bool p_use_aabb_center) {
  735. Instance *instance = instance_owner.get_or_null(p_instance);
  736. ERR_FAIL_COND(!instance);
  737. instance->sorting_offset = p_sorting_offset;
  738. instance->use_aabb_center = p_use_aabb_center;
  739. if ((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK && instance->base_data) {
  740. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(instance->base_data);
  741. ERR_FAIL_NULL(geom->geometry_instance);
  742. geom->geometry_instance->set_pivot_data(p_sorting_offset, p_use_aabb_center);
  743. } else if (instance->base_type == RS::INSTANCE_DECAL && instance->base_data) {
  744. InstanceDecalData *decal = static_cast<InstanceDecalData *>(instance->base_data);
  745. RSG::texture_storage->decal_instance_set_sorting_offset(decal->instance, instance->sorting_offset);
  746. }
  747. }
  748. void RendererSceneCull::instance_geometry_set_transparency(RID p_instance, float p_transparency) {
  749. Instance *instance = instance_owner.get_or_null(p_instance);
  750. ERR_FAIL_COND(!instance);
  751. instance->transparency = p_transparency;
  752. if ((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK && instance->base_data) {
  753. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(instance->base_data);
  754. ERR_FAIL_NULL(geom->geometry_instance);
  755. geom->geometry_instance->set_transparency(p_transparency);
  756. }
  757. }
  758. void RendererSceneCull::instance_set_transform(RID p_instance, const Transform3D &p_transform) {
  759. Instance *instance = instance_owner.get_or_null(p_instance);
  760. ERR_FAIL_COND(!instance);
  761. if (instance->transform == p_transform) {
  762. return; //must be checked to avoid worst evil
  763. }
  764. #ifdef DEBUG_ENABLED
  765. for (int i = 0; i < 4; i++) {
  766. const Vector3 &v = i < 3 ? p_transform.basis.rows[i] : p_transform.origin;
  767. ERR_FAIL_COND(!v.is_finite());
  768. }
  769. #endif
  770. instance->transform = p_transform;
  771. _instance_queue_update(instance, true);
  772. }
  773. void RendererSceneCull::instance_attach_object_instance_id(RID p_instance, ObjectID p_id) {
  774. Instance *instance = instance_owner.get_or_null(p_instance);
  775. ERR_FAIL_COND(!instance);
  776. instance->object_id = p_id;
  777. }
  778. void RendererSceneCull::instance_set_blend_shape_weight(RID p_instance, int p_shape, float p_weight) {
  779. Instance *instance = instance_owner.get_or_null(p_instance);
  780. ERR_FAIL_COND(!instance);
  781. if (instance->update_item.in_list()) {
  782. _update_dirty_instance(instance);
  783. }
  784. if (instance->mesh_instance.is_valid()) {
  785. RSG::mesh_storage->mesh_instance_set_blend_shape_weight(instance->mesh_instance, p_shape, p_weight);
  786. }
  787. }
  788. void RendererSceneCull::instance_set_surface_override_material(RID p_instance, int p_surface, RID p_material) {
  789. Instance *instance = instance_owner.get_or_null(p_instance);
  790. ERR_FAIL_COND(!instance);
  791. if (instance->base_type == RS::INSTANCE_MESH) {
  792. //may not have been updated yet, may also have not been set yet. When updated will be correcte, worst case
  793. instance->materials.resize(MAX(p_surface + 1, RSG::mesh_storage->mesh_get_surface_count(instance->base)));
  794. }
  795. ERR_FAIL_INDEX(p_surface, instance->materials.size());
  796. instance->materials.write[p_surface] = p_material;
  797. _instance_queue_update(instance, false, true);
  798. }
  799. void RendererSceneCull::instance_set_visible(RID p_instance, bool p_visible) {
  800. Instance *instance = instance_owner.get_or_null(p_instance);
  801. ERR_FAIL_COND(!instance);
  802. if (instance->visible == p_visible) {
  803. return;
  804. }
  805. instance->visible = p_visible;
  806. if (p_visible) {
  807. if (instance->scenario != nullptr) {
  808. _instance_queue_update(instance, true, false);
  809. }
  810. } else if (instance->indexer_id.is_valid()) {
  811. _unpair_instance(instance);
  812. }
  813. if (instance->base_type == RS::INSTANCE_LIGHT) {
  814. InstanceLightData *light = static_cast<InstanceLightData *>(instance->base_data);
  815. if (instance->scenario && RSG::light_storage->light_get_type(instance->base) != RS::LIGHT_DIRECTIONAL && light->bake_mode == RS::LIGHT_BAKE_DYNAMIC) {
  816. if (p_visible) {
  817. instance->scenario->dynamic_lights.push_back(light->instance);
  818. } else {
  819. instance->scenario->dynamic_lights.erase(light->instance);
  820. }
  821. }
  822. }
  823. if (instance->base_type == RS::INSTANCE_PARTICLES_COLLISION) {
  824. InstanceParticlesCollisionData *collision = static_cast<InstanceParticlesCollisionData *>(instance->base_data);
  825. RSG::particles_storage->particles_collision_instance_set_active(collision->instance, p_visible);
  826. }
  827. if (instance->base_type == RS::INSTANCE_FOG_VOLUME) {
  828. InstanceFogVolumeData *volume = static_cast<InstanceFogVolumeData *>(instance->base_data);
  829. scene_render->fog_volume_instance_set_active(volume->instance, p_visible);
  830. }
  831. if (instance->base_type == RS::INSTANCE_OCCLUDER) {
  832. if (instance->scenario) {
  833. RendererSceneOcclusionCull::get_singleton()->scenario_set_instance(instance->scenario->self, p_instance, instance->base, instance->transform, p_visible);
  834. }
  835. }
  836. }
  837. inline bool is_geometry_instance(RenderingServer::InstanceType p_type) {
  838. return p_type == RS::INSTANCE_MESH || p_type == RS::INSTANCE_MULTIMESH || p_type == RS::INSTANCE_PARTICLES;
  839. }
  840. void RendererSceneCull::instance_set_custom_aabb(RID p_instance, AABB p_aabb) {
  841. Instance *instance = instance_owner.get_or_null(p_instance);
  842. ERR_FAIL_COND(!instance);
  843. ERR_FAIL_COND(!is_geometry_instance(instance->base_type));
  844. if (p_aabb != AABB()) {
  845. // Set custom AABB
  846. if (instance->custom_aabb == nullptr) {
  847. instance->custom_aabb = memnew(AABB);
  848. }
  849. *instance->custom_aabb = p_aabb;
  850. } else {
  851. // Clear custom AABB
  852. if (instance->custom_aabb != nullptr) {
  853. memdelete(instance->custom_aabb);
  854. instance->custom_aabb = nullptr;
  855. }
  856. }
  857. if (instance->scenario) {
  858. _instance_queue_update(instance, true, false);
  859. }
  860. }
  861. void RendererSceneCull::instance_attach_skeleton(RID p_instance, RID p_skeleton) {
  862. Instance *instance = instance_owner.get_or_null(p_instance);
  863. ERR_FAIL_COND(!instance);
  864. if (instance->skeleton == p_skeleton) {
  865. return;
  866. }
  867. instance->skeleton = p_skeleton;
  868. if (p_skeleton.is_valid()) {
  869. //update the dependency now, so if cleared, we remove it
  870. RSG::mesh_storage->skeleton_update_dependency(p_skeleton, &instance->dependency_tracker);
  871. }
  872. _instance_queue_update(instance, true, true);
  873. if ((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK && instance->base_data) {
  874. _instance_update_mesh_instance(instance);
  875. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(instance->base_data);
  876. ERR_FAIL_NULL(geom->geometry_instance);
  877. geom->geometry_instance->set_skeleton(p_skeleton);
  878. }
  879. }
  880. void RendererSceneCull::instance_set_extra_visibility_margin(RID p_instance, real_t p_margin) {
  881. Instance *instance = instance_owner.get_or_null(p_instance);
  882. ERR_FAIL_COND(!instance);
  883. instance->extra_margin = p_margin;
  884. _instance_queue_update(instance, true, false);
  885. }
  886. void RendererSceneCull::instance_set_ignore_culling(RID p_instance, bool p_enabled) {
  887. Instance *instance = instance_owner.get_or_null(p_instance);
  888. ERR_FAIL_COND(!instance);
  889. instance->ignore_all_culling = p_enabled;
  890. if (instance->scenario && instance->array_index >= 0) {
  891. InstanceData &idata = instance->scenario->instance_data[instance->array_index];
  892. if (instance->ignore_all_culling) {
  893. idata.flags |= InstanceData::FLAG_IGNORE_ALL_CULLING;
  894. } else {
  895. idata.flags &= ~uint32_t(InstanceData::FLAG_IGNORE_ALL_CULLING);
  896. }
  897. }
  898. }
  899. Vector<ObjectID> RendererSceneCull::instances_cull_aabb(const AABB &p_aabb, RID p_scenario) const {
  900. Vector<ObjectID> instances;
  901. Scenario *scenario = scenario_owner.get_or_null(p_scenario);
  902. ERR_FAIL_COND_V(!scenario, instances);
  903. const_cast<RendererSceneCull *>(this)->update_dirty_instances(); // check dirty instances before culling
  904. struct CullAABB {
  905. Vector<ObjectID> instances;
  906. _FORCE_INLINE_ bool operator()(void *p_data) {
  907. Instance *p_instance = (Instance *)p_data;
  908. if (!p_instance->object_id.is_null()) {
  909. instances.push_back(p_instance->object_id);
  910. }
  911. return false;
  912. }
  913. };
  914. CullAABB cull_aabb;
  915. scenario->indexers[Scenario::INDEXER_GEOMETRY].aabb_query(p_aabb, cull_aabb);
  916. scenario->indexers[Scenario::INDEXER_VOLUMES].aabb_query(p_aabb, cull_aabb);
  917. return cull_aabb.instances;
  918. }
  919. Vector<ObjectID> RendererSceneCull::instances_cull_ray(const Vector3 &p_from, const Vector3 &p_to, RID p_scenario) const {
  920. Vector<ObjectID> instances;
  921. Scenario *scenario = scenario_owner.get_or_null(p_scenario);
  922. ERR_FAIL_COND_V(!scenario, instances);
  923. const_cast<RendererSceneCull *>(this)->update_dirty_instances(); // check dirty instances before culling
  924. struct CullRay {
  925. Vector<ObjectID> instances;
  926. _FORCE_INLINE_ bool operator()(void *p_data) {
  927. Instance *p_instance = (Instance *)p_data;
  928. if (!p_instance->object_id.is_null()) {
  929. instances.push_back(p_instance->object_id);
  930. }
  931. return false;
  932. }
  933. };
  934. CullRay cull_ray;
  935. scenario->indexers[Scenario::INDEXER_GEOMETRY].ray_query(p_from, p_to, cull_ray);
  936. scenario->indexers[Scenario::INDEXER_VOLUMES].ray_query(p_from, p_to, cull_ray);
  937. return cull_ray.instances;
  938. }
  939. Vector<ObjectID> RendererSceneCull::instances_cull_convex(const Vector<Plane> &p_convex, RID p_scenario) const {
  940. Vector<ObjectID> instances;
  941. Scenario *scenario = scenario_owner.get_or_null(p_scenario);
  942. ERR_FAIL_COND_V(!scenario, instances);
  943. const_cast<RendererSceneCull *>(this)->update_dirty_instances(); // check dirty instances before culling
  944. Vector<Vector3> points = Geometry3D::compute_convex_mesh_points(&p_convex[0], p_convex.size());
  945. struct CullConvex {
  946. Vector<ObjectID> instances;
  947. _FORCE_INLINE_ bool operator()(void *p_data) {
  948. Instance *p_instance = (Instance *)p_data;
  949. if (!p_instance->object_id.is_null()) {
  950. instances.push_back(p_instance->object_id);
  951. }
  952. return false;
  953. }
  954. };
  955. CullConvex cull_convex;
  956. scenario->indexers[Scenario::INDEXER_GEOMETRY].convex_query(p_convex.ptr(), p_convex.size(), points.ptr(), points.size(), cull_convex);
  957. scenario->indexers[Scenario::INDEXER_VOLUMES].convex_query(p_convex.ptr(), p_convex.size(), points.ptr(), points.size(), cull_convex);
  958. return cull_convex.instances;
  959. }
  960. void RendererSceneCull::instance_geometry_set_flag(RID p_instance, RS::InstanceFlags p_flags, bool p_enabled) {
  961. Instance *instance = instance_owner.get_or_null(p_instance);
  962. ERR_FAIL_COND(!instance);
  963. //ERR_FAIL_COND(((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK));
  964. switch (p_flags) {
  965. case RS::INSTANCE_FLAG_USE_BAKED_LIGHT: {
  966. instance->baked_light = p_enabled;
  967. if (instance->scenario && instance->array_index >= 0) {
  968. InstanceData &idata = instance->scenario->instance_data[instance->array_index];
  969. if (instance->baked_light) {
  970. idata.flags |= InstanceData::FLAG_USES_BAKED_LIGHT;
  971. } else {
  972. idata.flags &= ~uint32_t(InstanceData::FLAG_USES_BAKED_LIGHT);
  973. }
  974. }
  975. if ((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK && instance->base_data) {
  976. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(instance->base_data);
  977. ERR_FAIL_NULL(geom->geometry_instance);
  978. geom->geometry_instance->set_use_baked_light(p_enabled);
  979. }
  980. } break;
  981. case RS::INSTANCE_FLAG_USE_DYNAMIC_GI: {
  982. if (p_enabled == instance->dynamic_gi) {
  983. //bye, redundant
  984. return;
  985. }
  986. if (instance->indexer_id.is_valid()) {
  987. _unpair_instance(instance);
  988. _instance_queue_update(instance, true, true);
  989. }
  990. //once out of octree, can be changed
  991. instance->dynamic_gi = p_enabled;
  992. if ((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK && instance->base_data) {
  993. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(instance->base_data);
  994. ERR_FAIL_NULL(geom->geometry_instance);
  995. geom->geometry_instance->set_use_dynamic_gi(p_enabled);
  996. }
  997. } break;
  998. case RS::INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE: {
  999. instance->redraw_if_visible = p_enabled;
  1000. if (instance->scenario && instance->array_index >= 0) {
  1001. InstanceData &idata = instance->scenario->instance_data[instance->array_index];
  1002. if (instance->redraw_if_visible) {
  1003. idata.flags |= InstanceData::FLAG_REDRAW_IF_VISIBLE;
  1004. } else {
  1005. idata.flags &= ~uint32_t(InstanceData::FLAG_REDRAW_IF_VISIBLE);
  1006. }
  1007. }
  1008. } break;
  1009. case RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING: {
  1010. instance->ignore_occlusion_culling = p_enabled;
  1011. if (instance->scenario && instance->array_index >= 0) {
  1012. InstanceData &idata = instance->scenario->instance_data[instance->array_index];
  1013. if (instance->ignore_occlusion_culling) {
  1014. idata.flags |= InstanceData::FLAG_IGNORE_OCCLUSION_CULLING;
  1015. } else {
  1016. idata.flags &= ~uint32_t(InstanceData::FLAG_IGNORE_OCCLUSION_CULLING);
  1017. }
  1018. }
  1019. } break;
  1020. default: {
  1021. }
  1022. }
  1023. }
  1024. void RendererSceneCull::instance_geometry_set_cast_shadows_setting(RID p_instance, RS::ShadowCastingSetting p_shadow_casting_setting) {
  1025. Instance *instance = instance_owner.get_or_null(p_instance);
  1026. ERR_FAIL_COND(!instance);
  1027. instance->cast_shadows = p_shadow_casting_setting;
  1028. if (instance->scenario && instance->array_index >= 0) {
  1029. InstanceData &idata = instance->scenario->instance_data[instance->array_index];
  1030. if (instance->cast_shadows != RS::SHADOW_CASTING_SETTING_OFF) {
  1031. idata.flags |= InstanceData::FLAG_CAST_SHADOWS;
  1032. } else {
  1033. idata.flags &= ~uint32_t(InstanceData::FLAG_CAST_SHADOWS);
  1034. }
  1035. if (instance->cast_shadows == RS::SHADOW_CASTING_SETTING_SHADOWS_ONLY) {
  1036. idata.flags |= InstanceData::FLAG_CAST_SHADOWS_ONLY;
  1037. } else {
  1038. idata.flags &= ~uint32_t(InstanceData::FLAG_CAST_SHADOWS_ONLY);
  1039. }
  1040. }
  1041. if ((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK && instance->base_data) {
  1042. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(instance->base_data);
  1043. ERR_FAIL_NULL(geom->geometry_instance);
  1044. geom->geometry_instance->set_cast_double_sided_shadows(instance->cast_shadows == RS::SHADOW_CASTING_SETTING_DOUBLE_SIDED);
  1045. }
  1046. _instance_queue_update(instance, false, true);
  1047. }
  1048. void RendererSceneCull::instance_geometry_set_material_override(RID p_instance, RID p_material) {
  1049. Instance *instance = instance_owner.get_or_null(p_instance);
  1050. ERR_FAIL_COND(!instance);
  1051. instance->material_override = p_material;
  1052. _instance_queue_update(instance, false, true);
  1053. if ((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK && instance->base_data) {
  1054. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(instance->base_data);
  1055. ERR_FAIL_NULL(geom->geometry_instance);
  1056. geom->geometry_instance->set_material_override(p_material);
  1057. }
  1058. }
  1059. void RendererSceneCull::instance_geometry_set_material_overlay(RID p_instance, RID p_material) {
  1060. Instance *instance = instance_owner.get_or_null(p_instance);
  1061. ERR_FAIL_COND(!instance);
  1062. instance->material_overlay = p_material;
  1063. _instance_queue_update(instance, false, true);
  1064. if ((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK && instance->base_data) {
  1065. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(instance->base_data);
  1066. ERR_FAIL_NULL(geom->geometry_instance);
  1067. geom->geometry_instance->set_material_overlay(p_material);
  1068. }
  1069. }
  1070. void RendererSceneCull::instance_geometry_set_visibility_range(RID p_instance, float p_min, float p_max, float p_min_margin, float p_max_margin, RS::VisibilityRangeFadeMode p_fade_mode) {
  1071. Instance *instance = instance_owner.get_or_null(p_instance);
  1072. ERR_FAIL_COND(!instance);
  1073. instance->visibility_range_begin = p_min;
  1074. instance->visibility_range_end = p_max;
  1075. instance->visibility_range_begin_margin = p_min_margin;
  1076. instance->visibility_range_end_margin = p_max_margin;
  1077. instance->visibility_range_fade_mode = p_fade_mode;
  1078. _update_instance_visibility_dependencies(instance);
  1079. if (instance->scenario && instance->visibility_index != -1) {
  1080. InstanceVisibilityData &vd = instance->scenario->instance_visibility[instance->visibility_index];
  1081. vd.range_begin = instance->visibility_range_begin;
  1082. vd.range_end = instance->visibility_range_end;
  1083. vd.range_begin_margin = instance->visibility_range_begin_margin;
  1084. vd.range_end_margin = instance->visibility_range_end_margin;
  1085. vd.fade_mode = p_fade_mode;
  1086. }
  1087. }
  1088. void RendererSceneCull::instance_set_visibility_parent(RID p_instance, RID p_parent_instance) {
  1089. Instance *instance = instance_owner.get_or_null(p_instance);
  1090. ERR_FAIL_COND(!instance);
  1091. Instance *old_parent = instance->visibility_parent;
  1092. if (old_parent) {
  1093. old_parent->visibility_dependencies.erase(instance);
  1094. instance->visibility_parent = nullptr;
  1095. _update_instance_visibility_depth(old_parent);
  1096. }
  1097. Instance *parent = instance_owner.get_or_null(p_parent_instance);
  1098. ERR_FAIL_COND(p_parent_instance.is_valid() && !parent);
  1099. if (parent) {
  1100. parent->visibility_dependencies.insert(instance);
  1101. instance->visibility_parent = parent;
  1102. bool cycle_detected = _update_instance_visibility_depth(parent);
  1103. if (cycle_detected) {
  1104. ERR_PRINT("Cycle detected in the visibility dependencies tree. The latest change to visibility_parent will have no effect.");
  1105. parent->visibility_dependencies.erase(instance);
  1106. instance->visibility_parent = nullptr;
  1107. }
  1108. }
  1109. _update_instance_visibility_dependencies(instance);
  1110. }
  1111. bool RendererSceneCull::_update_instance_visibility_depth(Instance *p_instance) {
  1112. bool cycle_detected = false;
  1113. HashSet<Instance *> traversed_nodes;
  1114. {
  1115. Instance *instance = p_instance;
  1116. while (instance) {
  1117. if (!instance->visibility_dependencies.is_empty()) {
  1118. uint32_t depth = 0;
  1119. for (const Instance *E : instance->visibility_dependencies) {
  1120. depth = MAX(depth, E->visibility_dependencies_depth);
  1121. }
  1122. instance->visibility_dependencies_depth = depth + 1;
  1123. } else {
  1124. instance->visibility_dependencies_depth = 0;
  1125. }
  1126. if (instance->scenario && instance->visibility_index != -1) {
  1127. instance->scenario->instance_visibility.move(instance->visibility_index, instance->visibility_dependencies_depth);
  1128. }
  1129. traversed_nodes.insert(instance);
  1130. instance = instance->visibility_parent;
  1131. if (traversed_nodes.has(instance)) {
  1132. cycle_detected = true;
  1133. break;
  1134. }
  1135. }
  1136. }
  1137. return cycle_detected;
  1138. }
  1139. void RendererSceneCull::_update_instance_visibility_dependencies(Instance *p_instance) {
  1140. bool is_geometry_instance = ((1 << p_instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) && p_instance->base_data;
  1141. bool has_visibility_range = p_instance->visibility_range_begin > 0.0 || p_instance->visibility_range_end > 0.0;
  1142. bool needs_visibility_cull = has_visibility_range && is_geometry_instance && p_instance->array_index != -1;
  1143. if (!needs_visibility_cull && p_instance->visibility_index != -1) {
  1144. p_instance->scenario->instance_visibility.remove_at(p_instance->visibility_index);
  1145. p_instance->visibility_index = -1;
  1146. } else if (needs_visibility_cull && p_instance->visibility_index == -1) {
  1147. InstanceVisibilityData vd;
  1148. vd.instance = p_instance;
  1149. vd.range_begin = p_instance->visibility_range_begin;
  1150. vd.range_end = p_instance->visibility_range_end;
  1151. vd.range_begin_margin = p_instance->visibility_range_begin_margin;
  1152. vd.range_end_margin = p_instance->visibility_range_end_margin;
  1153. vd.position = p_instance->transformed_aabb.get_center();
  1154. vd.array_index = p_instance->array_index;
  1155. vd.fade_mode = p_instance->visibility_range_fade_mode;
  1156. p_instance->scenario->instance_visibility.insert(vd, p_instance->visibility_dependencies_depth);
  1157. }
  1158. if (p_instance->scenario && p_instance->array_index != -1) {
  1159. InstanceData &idata = p_instance->scenario->instance_data[p_instance->array_index];
  1160. idata.visibility_index = p_instance->visibility_index;
  1161. if (is_geometry_instance) {
  1162. if (has_visibility_range && p_instance->visibility_range_fade_mode == RS::VISIBILITY_RANGE_FADE_SELF) {
  1163. bool begin_enabled = p_instance->visibility_range_begin > 0.0f;
  1164. float begin_min = p_instance->visibility_range_begin - p_instance->visibility_range_begin_margin;
  1165. float begin_max = p_instance->visibility_range_begin + p_instance->visibility_range_begin_margin;
  1166. bool end_enabled = p_instance->visibility_range_end > 0.0f;
  1167. float end_min = p_instance->visibility_range_end - p_instance->visibility_range_end_margin;
  1168. float end_max = p_instance->visibility_range_end + p_instance->visibility_range_end_margin;
  1169. idata.instance_geometry->set_fade_range(begin_enabled, begin_min, begin_max, end_enabled, end_min, end_max);
  1170. } else {
  1171. idata.instance_geometry->set_fade_range(false, 0.0f, 0.0f, false, 0.0f, 0.0f);
  1172. }
  1173. }
  1174. if ((has_visibility_range || p_instance->visibility_parent) && (p_instance->visibility_index == -1 || p_instance->visibility_dependencies_depth == 0)) {
  1175. idata.flags |= InstanceData::FLAG_VISIBILITY_DEPENDENCY_NEEDS_CHECK;
  1176. } else {
  1177. idata.flags &= ~InstanceData::FLAG_VISIBILITY_DEPENDENCY_NEEDS_CHECK;
  1178. }
  1179. if (p_instance->visibility_parent) {
  1180. idata.parent_array_index = p_instance->visibility_parent->array_index;
  1181. } else {
  1182. idata.parent_array_index = -1;
  1183. if (is_geometry_instance) {
  1184. idata.instance_geometry->set_parent_fade_alpha(1.0f);
  1185. }
  1186. }
  1187. }
  1188. }
  1189. void RendererSceneCull::instance_geometry_set_lightmap(RID p_instance, RID p_lightmap, const Rect2 &p_lightmap_uv_scale, int p_slice_index) {
  1190. Instance *instance = instance_owner.get_or_null(p_instance);
  1191. ERR_FAIL_COND(!instance);
  1192. if (instance->lightmap) {
  1193. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(((Instance *)instance->lightmap)->base_data);
  1194. lightmap_data->users.erase(instance);
  1195. instance->lightmap = nullptr;
  1196. }
  1197. Instance *lightmap_instance = instance_owner.get_or_null(p_lightmap);
  1198. instance->lightmap = lightmap_instance;
  1199. instance->lightmap_uv_scale = p_lightmap_uv_scale;
  1200. instance->lightmap_slice_index = p_slice_index;
  1201. RID lightmap_instance_rid;
  1202. if (lightmap_instance) {
  1203. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(lightmap_instance->base_data);
  1204. lightmap_data->users.insert(instance);
  1205. lightmap_instance_rid = lightmap_data->instance;
  1206. }
  1207. if ((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK && instance->base_data) {
  1208. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(instance->base_data);
  1209. ERR_FAIL_NULL(geom->geometry_instance);
  1210. geom->geometry_instance->set_use_lightmap(lightmap_instance_rid, p_lightmap_uv_scale, p_slice_index);
  1211. }
  1212. }
  1213. void RendererSceneCull::instance_geometry_set_lod_bias(RID p_instance, float p_lod_bias) {
  1214. Instance *instance = instance_owner.get_or_null(p_instance);
  1215. ERR_FAIL_COND(!instance);
  1216. instance->lod_bias = p_lod_bias;
  1217. if ((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK && instance->base_data) {
  1218. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(instance->base_data);
  1219. ERR_FAIL_NULL(geom->geometry_instance);
  1220. geom->geometry_instance->set_lod_bias(p_lod_bias);
  1221. }
  1222. }
  1223. void RendererSceneCull::instance_geometry_set_shader_parameter(RID p_instance, const StringName &p_parameter, const Variant &p_value) {
  1224. Instance *instance = instance_owner.get_or_null(p_instance);
  1225. ERR_FAIL_COND(!instance);
  1226. ERR_FAIL_COND(p_value.get_type() == Variant::OBJECT);
  1227. HashMap<StringName, Instance::InstanceShaderParameter>::Iterator E = instance->instance_shader_uniforms.find(p_parameter);
  1228. if (!E) {
  1229. Instance::InstanceShaderParameter isp;
  1230. isp.index = -1;
  1231. isp.info = PropertyInfo();
  1232. isp.value = p_value;
  1233. instance->instance_shader_uniforms[p_parameter] = isp;
  1234. } else {
  1235. E->value.value = p_value;
  1236. if (E->value.index >= 0 && instance->instance_allocated_shader_uniforms) {
  1237. int flags_count = 0;
  1238. if (E->value.info.hint == PROPERTY_HINT_FLAGS) {
  1239. // A small hack to detect boolean flags count and prevent overhead.
  1240. switch (E->value.info.hint_string.length()) {
  1241. case 3: // "x,y"
  1242. flags_count = 1;
  1243. break;
  1244. case 5: // "x,y,z"
  1245. flags_count = 2;
  1246. break;
  1247. case 7: // "x,y,z,w"
  1248. flags_count = 3;
  1249. break;
  1250. }
  1251. }
  1252. //update directly
  1253. RSG::material_storage->global_shader_parameters_instance_update(p_instance, E->value.index, p_value, flags_count);
  1254. }
  1255. }
  1256. }
  1257. Variant RendererSceneCull::instance_geometry_get_shader_parameter(RID p_instance, const StringName &p_parameter) const {
  1258. const Instance *instance = const_cast<RendererSceneCull *>(this)->instance_owner.get_or_null(p_instance);
  1259. ERR_FAIL_COND_V(!instance, Variant());
  1260. if (instance->instance_shader_uniforms.has(p_parameter)) {
  1261. return instance->instance_shader_uniforms[p_parameter].value;
  1262. }
  1263. return Variant();
  1264. }
  1265. Variant RendererSceneCull::instance_geometry_get_shader_parameter_default_value(RID p_instance, const StringName &p_parameter) const {
  1266. const Instance *instance = const_cast<RendererSceneCull *>(this)->instance_owner.get_or_null(p_instance);
  1267. ERR_FAIL_COND_V(!instance, Variant());
  1268. if (instance->instance_shader_uniforms.has(p_parameter)) {
  1269. return instance->instance_shader_uniforms[p_parameter].default_value;
  1270. }
  1271. return Variant();
  1272. }
  1273. void RendererSceneCull::instance_geometry_get_shader_parameter_list(RID p_instance, List<PropertyInfo> *p_parameters) const {
  1274. const Instance *instance = const_cast<RendererSceneCull *>(this)->instance_owner.get_or_null(p_instance);
  1275. ERR_FAIL_COND(!instance);
  1276. const_cast<RendererSceneCull *>(this)->update_dirty_instances();
  1277. Vector<StringName> names;
  1278. for (const KeyValue<StringName, Instance::InstanceShaderParameter> &E : instance->instance_shader_uniforms) {
  1279. names.push_back(E.key);
  1280. }
  1281. names.sort_custom<StringName::AlphCompare>();
  1282. for (int i = 0; i < names.size(); i++) {
  1283. PropertyInfo pinfo = instance->instance_shader_uniforms[names[i]].info;
  1284. p_parameters->push_back(pinfo);
  1285. }
  1286. }
  1287. void RendererSceneCull::_update_instance(Instance *p_instance) {
  1288. p_instance->version++;
  1289. if (p_instance->base_type == RS::INSTANCE_LIGHT) {
  1290. InstanceLightData *light = static_cast<InstanceLightData *>(p_instance->base_data);
  1291. RSG::light_storage->light_instance_set_transform(light->instance, p_instance->transform);
  1292. RSG::light_storage->light_instance_set_aabb(light->instance, p_instance->transform.xform(p_instance->aabb));
  1293. light->shadow_dirty = true;
  1294. RS::LightBakeMode bake_mode = RSG::light_storage->light_get_bake_mode(p_instance->base);
  1295. if (RSG::light_storage->light_get_type(p_instance->base) != RS::LIGHT_DIRECTIONAL && bake_mode != light->bake_mode) {
  1296. if (p_instance->visible && p_instance->scenario && light->bake_mode == RS::LIGHT_BAKE_DYNAMIC) {
  1297. p_instance->scenario->dynamic_lights.erase(light->instance);
  1298. }
  1299. light->bake_mode = bake_mode;
  1300. if (p_instance->visible && p_instance->scenario && light->bake_mode == RS::LIGHT_BAKE_DYNAMIC) {
  1301. p_instance->scenario->dynamic_lights.push_back(light->instance);
  1302. }
  1303. }
  1304. uint32_t max_sdfgi_cascade = RSG::light_storage->light_get_max_sdfgi_cascade(p_instance->base);
  1305. if (light->max_sdfgi_cascade != max_sdfgi_cascade) {
  1306. light->max_sdfgi_cascade = max_sdfgi_cascade; //should most likely make sdfgi dirty in scenario
  1307. }
  1308. } else if (p_instance->base_type == RS::INSTANCE_REFLECTION_PROBE) {
  1309. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(p_instance->base_data);
  1310. RSG::light_storage->reflection_probe_instance_set_transform(reflection_probe->instance, p_instance->transform);
  1311. if (p_instance->scenario && p_instance->array_index >= 0) {
  1312. InstanceData &idata = p_instance->scenario->instance_data[p_instance->array_index];
  1313. idata.flags |= InstanceData::FLAG_REFLECTION_PROBE_DIRTY;
  1314. }
  1315. } else if (p_instance->base_type == RS::INSTANCE_DECAL) {
  1316. InstanceDecalData *decal = static_cast<InstanceDecalData *>(p_instance->base_data);
  1317. RSG::texture_storage->decal_instance_set_transform(decal->instance, p_instance->transform);
  1318. } else if (p_instance->base_type == RS::INSTANCE_LIGHTMAP) {
  1319. InstanceLightmapData *lightmap = static_cast<InstanceLightmapData *>(p_instance->base_data);
  1320. RSG::light_storage->lightmap_instance_set_transform(lightmap->instance, p_instance->transform);
  1321. } else if (p_instance->base_type == RS::INSTANCE_VOXEL_GI) {
  1322. InstanceVoxelGIData *voxel_gi = static_cast<InstanceVoxelGIData *>(p_instance->base_data);
  1323. scene_render->voxel_gi_instance_set_transform_to_data(voxel_gi->probe_instance, p_instance->transform);
  1324. } else if (p_instance->base_type == RS::INSTANCE_PARTICLES) {
  1325. RSG::particles_storage->particles_set_emission_transform(p_instance->base, p_instance->transform);
  1326. } else if (p_instance->base_type == RS::INSTANCE_PARTICLES_COLLISION) {
  1327. InstanceParticlesCollisionData *collision = static_cast<InstanceParticlesCollisionData *>(p_instance->base_data);
  1328. //remove materials no longer used and un-own them
  1329. if (RSG::particles_storage->particles_collision_is_heightfield(p_instance->base)) {
  1330. heightfield_particle_colliders_update_list.insert(p_instance);
  1331. }
  1332. RSG::particles_storage->particles_collision_instance_set_transform(collision->instance, p_instance->transform);
  1333. } else if (p_instance->base_type == RS::INSTANCE_FOG_VOLUME) {
  1334. InstanceFogVolumeData *volume = static_cast<InstanceFogVolumeData *>(p_instance->base_data);
  1335. scene_render->fog_volume_instance_set_transform(volume->instance, p_instance->transform);
  1336. } else if (p_instance->base_type == RS::INSTANCE_OCCLUDER) {
  1337. if (p_instance->scenario) {
  1338. RendererSceneOcclusionCull::get_singleton()->scenario_set_instance(p_instance->scenario->self, p_instance->self, p_instance->base, p_instance->transform, p_instance->visible);
  1339. }
  1340. }
  1341. if (!p_instance->aabb.has_surface()) {
  1342. return;
  1343. }
  1344. if (p_instance->base_type == RS::INSTANCE_LIGHTMAP) {
  1345. //if this moved, update the captured objects
  1346. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(p_instance->base_data);
  1347. //erase dependencies, since no longer a lightmap
  1348. for (Instance *E : lightmap_data->geometries) {
  1349. Instance *geom = E;
  1350. _instance_queue_update(geom, true, false);
  1351. }
  1352. }
  1353. AABB new_aabb;
  1354. new_aabb = p_instance->transform.xform(p_instance->aabb);
  1355. p_instance->transformed_aabb = new_aabb;
  1356. if ((1 << p_instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) {
  1357. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  1358. //make sure lights are updated if it casts shadow
  1359. if (geom->can_cast_shadows) {
  1360. for (const Instance *E : geom->lights) {
  1361. InstanceLightData *light = static_cast<InstanceLightData *>(E->base_data);
  1362. light->shadow_dirty = true;
  1363. }
  1364. }
  1365. if (!p_instance->lightmap && geom->lightmap_captures.size()) {
  1366. //affected by lightmap captures, must update capture info!
  1367. _update_instance_lightmap_captures(p_instance);
  1368. } else {
  1369. if (!p_instance->lightmap_sh.is_empty()) {
  1370. p_instance->lightmap_sh.clear(); //don't need SH
  1371. p_instance->lightmap_target_sh.clear(); //don't need SH
  1372. ERR_FAIL_NULL(geom->geometry_instance);
  1373. geom->geometry_instance->set_lightmap_capture(nullptr);
  1374. }
  1375. }
  1376. ERR_FAIL_NULL(geom->geometry_instance);
  1377. geom->geometry_instance->set_transform(p_instance->transform, p_instance->aabb, p_instance->transformed_aabb);
  1378. }
  1379. // note: we had to remove is equal approx check here, it meant that det == 0.000004 won't work, which is the case for some of our scenes.
  1380. if (p_instance->scenario == nullptr || !p_instance->visible || p_instance->transform.basis.determinant() == 0) {
  1381. p_instance->prev_transformed_aabb = p_instance->transformed_aabb;
  1382. return;
  1383. }
  1384. //quantize to improve moving object performance
  1385. AABB bvh_aabb = p_instance->transformed_aabb;
  1386. if (p_instance->indexer_id.is_valid() && bvh_aabb != p_instance->prev_transformed_aabb) {
  1387. //assume motion, see if bounds need to be quantized
  1388. AABB motion_aabb = bvh_aabb.merge(p_instance->prev_transformed_aabb);
  1389. float motion_longest_axis = motion_aabb.get_longest_axis_size();
  1390. float longest_axis = p_instance->transformed_aabb.get_longest_axis_size();
  1391. if (motion_longest_axis < longest_axis * 2) {
  1392. //moved but not a lot, use motion aabb quantizing
  1393. float quantize_size = Math::pow(2.0, Math::ceil(Math::log(motion_longest_axis) / Math::log(2.0))) * 0.5; //one fifth
  1394. bvh_aabb.quantize(quantize_size);
  1395. }
  1396. }
  1397. if (!p_instance->indexer_id.is_valid()) {
  1398. if ((1 << p_instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) {
  1399. p_instance->indexer_id = p_instance->scenario->indexers[Scenario::INDEXER_GEOMETRY].insert(bvh_aabb, p_instance);
  1400. } else {
  1401. p_instance->indexer_id = p_instance->scenario->indexers[Scenario::INDEXER_VOLUMES].insert(bvh_aabb, p_instance);
  1402. }
  1403. p_instance->array_index = p_instance->scenario->instance_data.size();
  1404. InstanceData idata;
  1405. idata.instance = p_instance;
  1406. idata.layer_mask = p_instance->layer_mask;
  1407. idata.flags = p_instance->base_type; //changing it means de-indexing, so this never needs to be changed later
  1408. idata.base_rid = p_instance->base;
  1409. idata.parent_array_index = p_instance->visibility_parent ? p_instance->visibility_parent->array_index : -1;
  1410. idata.visibility_index = p_instance->visibility_index;
  1411. for (Instance *E : p_instance->visibility_dependencies) {
  1412. Instance *dep_instance = E;
  1413. if (dep_instance->array_index != -1) {
  1414. dep_instance->scenario->instance_data[dep_instance->array_index].parent_array_index = p_instance->array_index;
  1415. }
  1416. }
  1417. switch (p_instance->base_type) {
  1418. case RS::INSTANCE_MESH:
  1419. case RS::INSTANCE_MULTIMESH:
  1420. case RS::INSTANCE_PARTICLES: {
  1421. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  1422. idata.instance_geometry = geom->geometry_instance;
  1423. } break;
  1424. case RS::INSTANCE_LIGHT: {
  1425. InstanceLightData *light_data = static_cast<InstanceLightData *>(p_instance->base_data);
  1426. idata.instance_data_rid = light_data->instance.get_id();
  1427. light_data->uses_projector = RSG::light_storage->light_has_projector(p_instance->base);
  1428. light_data->uses_softshadow = RSG::light_storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_SIZE) > CMP_EPSILON;
  1429. } break;
  1430. case RS::INSTANCE_REFLECTION_PROBE: {
  1431. idata.instance_data_rid = static_cast<InstanceReflectionProbeData *>(p_instance->base_data)->instance.get_id();
  1432. } break;
  1433. case RS::INSTANCE_DECAL: {
  1434. idata.instance_data_rid = static_cast<InstanceDecalData *>(p_instance->base_data)->instance.get_id();
  1435. } break;
  1436. case RS::INSTANCE_LIGHTMAP: {
  1437. idata.instance_data_rid = static_cast<InstanceLightmapData *>(p_instance->base_data)->instance.get_id();
  1438. } break;
  1439. case RS::INSTANCE_VOXEL_GI: {
  1440. idata.instance_data_rid = static_cast<InstanceVoxelGIData *>(p_instance->base_data)->probe_instance.get_id();
  1441. } break;
  1442. case RS::INSTANCE_FOG_VOLUME: {
  1443. idata.instance_data_rid = static_cast<InstanceFogVolumeData *>(p_instance->base_data)->instance.get_id();
  1444. } break;
  1445. case RS::INSTANCE_VISIBLITY_NOTIFIER: {
  1446. idata.visibility_notifier = static_cast<InstanceVisibilityNotifierData *>(p_instance->base_data);
  1447. } break;
  1448. default: {
  1449. }
  1450. }
  1451. if (p_instance->base_type == RS::INSTANCE_REFLECTION_PROBE) {
  1452. //always dirty when added
  1453. idata.flags |= InstanceData::FLAG_REFLECTION_PROBE_DIRTY;
  1454. }
  1455. if (p_instance->cast_shadows != RS::SHADOW_CASTING_SETTING_OFF) {
  1456. idata.flags |= InstanceData::FLAG_CAST_SHADOWS;
  1457. }
  1458. if (p_instance->cast_shadows == RS::SHADOW_CASTING_SETTING_SHADOWS_ONLY) {
  1459. idata.flags |= InstanceData::FLAG_CAST_SHADOWS_ONLY;
  1460. }
  1461. if (p_instance->redraw_if_visible) {
  1462. idata.flags |= InstanceData::FLAG_REDRAW_IF_VISIBLE;
  1463. }
  1464. // dirty flags should not be set here, since no pairing has happened
  1465. if (p_instance->baked_light) {
  1466. idata.flags |= InstanceData::FLAG_USES_BAKED_LIGHT;
  1467. }
  1468. if (p_instance->mesh_instance.is_valid()) {
  1469. idata.flags |= InstanceData::FLAG_USES_MESH_INSTANCE;
  1470. }
  1471. if (p_instance->ignore_occlusion_culling) {
  1472. idata.flags |= InstanceData::FLAG_IGNORE_OCCLUSION_CULLING;
  1473. }
  1474. if (p_instance->ignore_all_culling) {
  1475. idata.flags |= InstanceData::FLAG_IGNORE_ALL_CULLING;
  1476. }
  1477. p_instance->scenario->instance_data.push_back(idata);
  1478. p_instance->scenario->instance_aabbs.push_back(InstanceBounds(p_instance->transformed_aabb));
  1479. _update_instance_visibility_dependencies(p_instance);
  1480. } else {
  1481. if ((1 << p_instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) {
  1482. p_instance->scenario->indexers[Scenario::INDEXER_GEOMETRY].update(p_instance->indexer_id, bvh_aabb);
  1483. } else {
  1484. p_instance->scenario->indexers[Scenario::INDEXER_VOLUMES].update(p_instance->indexer_id, bvh_aabb);
  1485. }
  1486. p_instance->scenario->instance_aabbs[p_instance->array_index] = InstanceBounds(p_instance->transformed_aabb);
  1487. }
  1488. if (p_instance->visibility_index != -1) {
  1489. p_instance->scenario->instance_visibility[p_instance->visibility_index].position = p_instance->transformed_aabb.get_center();
  1490. }
  1491. //move instance and repair
  1492. pair_pass++;
  1493. PairInstances pair;
  1494. pair.instance = p_instance;
  1495. pair.pair_allocator = &pair_allocator;
  1496. pair.pair_pass = pair_pass;
  1497. pair.pair_mask = 0;
  1498. pair.cull_mask = 0xFFFFFFFF;
  1499. if ((1 << p_instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) {
  1500. pair.pair_mask |= 1 << RS::INSTANCE_LIGHT;
  1501. pair.pair_mask |= 1 << RS::INSTANCE_VOXEL_GI;
  1502. pair.pair_mask |= 1 << RS::INSTANCE_LIGHTMAP;
  1503. if (p_instance->base_type == RS::INSTANCE_PARTICLES) {
  1504. pair.pair_mask |= 1 << RS::INSTANCE_PARTICLES_COLLISION;
  1505. }
  1506. pair.pair_mask |= geometry_instance_pair_mask;
  1507. pair.bvh2 = &p_instance->scenario->indexers[Scenario::INDEXER_VOLUMES];
  1508. } else if (p_instance->base_type == RS::INSTANCE_LIGHT) {
  1509. pair.pair_mask |= RS::INSTANCE_GEOMETRY_MASK;
  1510. pair.bvh = &p_instance->scenario->indexers[Scenario::INDEXER_GEOMETRY];
  1511. if (RSG::light_storage->light_get_bake_mode(p_instance->base) == RS::LIGHT_BAKE_DYNAMIC) {
  1512. pair.pair_mask |= (1 << RS::INSTANCE_VOXEL_GI);
  1513. pair.bvh2 = &p_instance->scenario->indexers[Scenario::INDEXER_VOLUMES];
  1514. }
  1515. pair.cull_mask = RSG::light_storage->light_get_cull_mask(p_instance->base);
  1516. } else if (geometry_instance_pair_mask & (1 << RS::INSTANCE_REFLECTION_PROBE) && (p_instance->base_type == RS::INSTANCE_REFLECTION_PROBE)) {
  1517. pair.pair_mask = RS::INSTANCE_GEOMETRY_MASK;
  1518. pair.bvh = &p_instance->scenario->indexers[Scenario::INDEXER_GEOMETRY];
  1519. } else if (geometry_instance_pair_mask & (1 << RS::INSTANCE_DECAL) && (p_instance->base_type == RS::INSTANCE_DECAL)) {
  1520. pair.pair_mask = RS::INSTANCE_GEOMETRY_MASK;
  1521. pair.bvh = &p_instance->scenario->indexers[Scenario::INDEXER_GEOMETRY];
  1522. pair.cull_mask = RSG::texture_storage->decal_get_cull_mask(p_instance->base);
  1523. } else if (p_instance->base_type == RS::INSTANCE_PARTICLES_COLLISION) {
  1524. pair.pair_mask = (1 << RS::INSTANCE_PARTICLES);
  1525. pair.bvh = &p_instance->scenario->indexers[Scenario::INDEXER_GEOMETRY];
  1526. } else if (p_instance->base_type == RS::INSTANCE_VOXEL_GI) {
  1527. //lights and geometries
  1528. pair.pair_mask = RS::INSTANCE_GEOMETRY_MASK | (1 << RS::INSTANCE_LIGHT);
  1529. pair.bvh = &p_instance->scenario->indexers[Scenario::INDEXER_GEOMETRY];
  1530. pair.bvh2 = &p_instance->scenario->indexers[Scenario::INDEXER_VOLUMES];
  1531. }
  1532. pair.pair();
  1533. p_instance->prev_transformed_aabb = p_instance->transformed_aabb;
  1534. }
  1535. void RendererSceneCull::_unpair_instance(Instance *p_instance) {
  1536. if (!p_instance->indexer_id.is_valid()) {
  1537. return; //nothing to do
  1538. }
  1539. while (p_instance->pairs.first()) {
  1540. InstancePair *pair = p_instance->pairs.first()->self();
  1541. Instance *other_instance = p_instance == pair->a ? pair->b : pair->a;
  1542. _instance_unpair(p_instance, other_instance);
  1543. pair_allocator.free(pair);
  1544. }
  1545. if ((1 << p_instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) {
  1546. p_instance->scenario->indexers[Scenario::INDEXER_GEOMETRY].remove(p_instance->indexer_id);
  1547. } else {
  1548. p_instance->scenario->indexers[Scenario::INDEXER_VOLUMES].remove(p_instance->indexer_id);
  1549. }
  1550. p_instance->indexer_id = DynamicBVH::ID();
  1551. //replace this by last
  1552. int32_t swap_with_index = p_instance->scenario->instance_data.size() - 1;
  1553. if (swap_with_index != p_instance->array_index) {
  1554. Instance *swapped_instance = p_instance->scenario->instance_data[swap_with_index].instance;
  1555. swapped_instance->array_index = p_instance->array_index; //swap
  1556. p_instance->scenario->instance_data[p_instance->array_index] = p_instance->scenario->instance_data[swap_with_index];
  1557. p_instance->scenario->instance_aabbs[p_instance->array_index] = p_instance->scenario->instance_aabbs[swap_with_index];
  1558. if (swapped_instance->visibility_index != -1) {
  1559. swapped_instance->scenario->instance_visibility[swapped_instance->visibility_index].array_index = swapped_instance->array_index;
  1560. }
  1561. for (Instance *E : swapped_instance->visibility_dependencies) {
  1562. Instance *dep_instance = E;
  1563. if (dep_instance != p_instance && dep_instance->array_index != -1) {
  1564. dep_instance->scenario->instance_data[dep_instance->array_index].parent_array_index = swapped_instance->array_index;
  1565. }
  1566. }
  1567. }
  1568. // pop last
  1569. p_instance->scenario->instance_data.pop_back();
  1570. p_instance->scenario->instance_aabbs.pop_back();
  1571. //uninitialize
  1572. p_instance->array_index = -1;
  1573. if ((1 << p_instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) {
  1574. // Clear these now because the InstanceData containing the dirty flags is gone
  1575. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  1576. ERR_FAIL_NULL(geom->geometry_instance);
  1577. geom->geometry_instance->pair_light_instances(nullptr, 0);
  1578. geom->geometry_instance->pair_reflection_probe_instances(nullptr, 0);
  1579. geom->geometry_instance->pair_decal_instances(nullptr, 0);
  1580. geom->geometry_instance->pair_voxel_gi_instances(nullptr, 0);
  1581. }
  1582. for (Instance *E : p_instance->visibility_dependencies) {
  1583. Instance *dep_instance = E;
  1584. if (dep_instance->array_index != -1) {
  1585. dep_instance->scenario->instance_data[dep_instance->array_index].parent_array_index = -1;
  1586. if ((1 << dep_instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) {
  1587. dep_instance->scenario->instance_data[dep_instance->array_index].instance_geometry->set_parent_fade_alpha(1.0f);
  1588. }
  1589. }
  1590. }
  1591. _update_instance_visibility_dependencies(p_instance);
  1592. }
  1593. void RendererSceneCull::_update_instance_aabb(Instance *p_instance) {
  1594. AABB new_aabb;
  1595. ERR_FAIL_COND(p_instance->base_type != RS::INSTANCE_NONE && !p_instance->base.is_valid());
  1596. switch (p_instance->base_type) {
  1597. case RenderingServer::INSTANCE_NONE: {
  1598. // do nothing
  1599. } break;
  1600. case RenderingServer::INSTANCE_MESH: {
  1601. if (p_instance->custom_aabb) {
  1602. new_aabb = *p_instance->custom_aabb;
  1603. } else {
  1604. new_aabb = RSG::mesh_storage->mesh_get_aabb(p_instance->base, p_instance->skeleton);
  1605. }
  1606. } break;
  1607. case RenderingServer::INSTANCE_MULTIMESH: {
  1608. if (p_instance->custom_aabb) {
  1609. new_aabb = *p_instance->custom_aabb;
  1610. } else {
  1611. new_aabb = RSG::mesh_storage->multimesh_get_aabb(p_instance->base);
  1612. }
  1613. } break;
  1614. case RenderingServer::INSTANCE_PARTICLES: {
  1615. if (p_instance->custom_aabb) {
  1616. new_aabb = *p_instance->custom_aabb;
  1617. } else {
  1618. new_aabb = RSG::particles_storage->particles_get_aabb(p_instance->base);
  1619. }
  1620. } break;
  1621. case RenderingServer::INSTANCE_PARTICLES_COLLISION: {
  1622. new_aabb = RSG::particles_storage->particles_collision_get_aabb(p_instance->base);
  1623. } break;
  1624. case RenderingServer::INSTANCE_FOG_VOLUME: {
  1625. new_aabb = RSG::fog->fog_volume_get_aabb(p_instance->base);
  1626. } break;
  1627. case RenderingServer::INSTANCE_VISIBLITY_NOTIFIER: {
  1628. new_aabb = RSG::utilities->visibility_notifier_get_aabb(p_instance->base);
  1629. } break;
  1630. case RenderingServer::INSTANCE_LIGHT: {
  1631. new_aabb = RSG::light_storage->light_get_aabb(p_instance->base);
  1632. } break;
  1633. case RenderingServer::INSTANCE_REFLECTION_PROBE: {
  1634. new_aabb = RSG::light_storage->reflection_probe_get_aabb(p_instance->base);
  1635. } break;
  1636. case RenderingServer::INSTANCE_DECAL: {
  1637. new_aabb = RSG::texture_storage->decal_get_aabb(p_instance->base);
  1638. } break;
  1639. case RenderingServer::INSTANCE_VOXEL_GI: {
  1640. new_aabb = RSG::gi->voxel_gi_get_bounds(p_instance->base);
  1641. } break;
  1642. case RenderingServer::INSTANCE_LIGHTMAP: {
  1643. new_aabb = RSG::light_storage->lightmap_get_aabb(p_instance->base);
  1644. } break;
  1645. default: {
  1646. }
  1647. }
  1648. if (p_instance->extra_margin) {
  1649. new_aabb.grow_by(p_instance->extra_margin);
  1650. }
  1651. p_instance->aabb = new_aabb;
  1652. }
  1653. void RendererSceneCull::_update_instance_lightmap_captures(Instance *p_instance) {
  1654. bool first_set = p_instance->lightmap_sh.size() == 0;
  1655. p_instance->lightmap_sh.resize(9); //using SH
  1656. p_instance->lightmap_target_sh.resize(9); //using SH
  1657. Color *instance_sh = p_instance->lightmap_target_sh.ptrw();
  1658. bool inside = false;
  1659. Color accum_sh[9];
  1660. float accum_blend = 0.0;
  1661. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  1662. for (Instance *E : geom->lightmap_captures) {
  1663. Instance *lightmap = E;
  1664. bool interior = RSG::light_storage->lightmap_is_interior(lightmap->base);
  1665. if (inside && !interior) {
  1666. continue; //we are inside, ignore exteriors
  1667. }
  1668. Transform3D to_bounds = lightmap->transform.affine_inverse();
  1669. Vector3 center = p_instance->transform.xform(p_instance->aabb.get_center()); //use aabb center
  1670. Vector3 lm_pos = to_bounds.xform(center);
  1671. AABB bounds = RSG::light_storage->lightmap_get_aabb(lightmap->base);
  1672. if (!bounds.has_point(lm_pos)) {
  1673. continue; //not in this lightmap
  1674. }
  1675. Color sh[9];
  1676. RSG::light_storage->lightmap_tap_sh_light(lightmap->base, lm_pos, sh);
  1677. //rotate it
  1678. Basis rot = lightmap->transform.basis.orthonormalized();
  1679. for (int i = 0; i < 3; i++) {
  1680. real_t csh[9];
  1681. for (int j = 0; j < 9; j++) {
  1682. csh[j] = sh[j][i];
  1683. }
  1684. rot.rotate_sh(csh);
  1685. for (int j = 0; j < 9; j++) {
  1686. sh[j][i] = csh[j];
  1687. }
  1688. }
  1689. Vector3 inner_pos = ((lm_pos - bounds.position) / bounds.size) * 2.0 - Vector3(1.0, 1.0, 1.0);
  1690. real_t blend = MAX(inner_pos.x, MAX(inner_pos.y, inner_pos.z));
  1691. //make blend more rounded
  1692. blend = Math::lerp(inner_pos.length(), blend, blend);
  1693. blend *= blend;
  1694. blend = MAX(0.0, 1.0 - blend);
  1695. if (interior && !inside) {
  1696. //do not blend, just replace
  1697. for (int j = 0; j < 9; j++) {
  1698. accum_sh[j] = sh[j] * blend;
  1699. }
  1700. accum_blend = blend;
  1701. inside = true;
  1702. } else {
  1703. for (int j = 0; j < 9; j++) {
  1704. accum_sh[j] += sh[j] * blend;
  1705. }
  1706. accum_blend += blend;
  1707. }
  1708. }
  1709. if (accum_blend > 0.0) {
  1710. for (int j = 0; j < 9; j++) {
  1711. instance_sh[j] = accum_sh[j] / accum_blend;
  1712. if (first_set) {
  1713. p_instance->lightmap_sh.write[j] = instance_sh[j];
  1714. }
  1715. }
  1716. }
  1717. ERR_FAIL_NULL(geom->geometry_instance);
  1718. geom->geometry_instance->set_lightmap_capture(p_instance->lightmap_sh.ptr());
  1719. }
  1720. void RendererSceneCull::_light_instance_setup_directional_shadow(int p_shadow_index, Instance *p_instance, const Transform3D p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal, bool p_cam_vaspect) {
  1721. InstanceLightData *light = static_cast<InstanceLightData *>(p_instance->base_data);
  1722. Transform3D light_transform = p_instance->transform;
  1723. light_transform.orthonormalize(); //scale does not count on lights
  1724. real_t max_distance = p_cam_projection.get_z_far();
  1725. real_t shadow_max = RSG::light_storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE);
  1726. if (shadow_max > 0 && !p_cam_orthogonal) { //its impractical (and leads to unwanted behaviors) to set max distance in orthogonal camera
  1727. max_distance = MIN(shadow_max, max_distance);
  1728. }
  1729. max_distance = MAX(max_distance, p_cam_projection.get_z_near() + 0.001);
  1730. real_t min_distance = MIN(p_cam_projection.get_z_near(), max_distance);
  1731. real_t pancake_size = RSG::light_storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE);
  1732. real_t range = max_distance - min_distance;
  1733. int splits = 0;
  1734. switch (RSG::light_storage->light_directional_get_shadow_mode(p_instance->base)) {
  1735. case RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL:
  1736. splits = 1;
  1737. break;
  1738. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS:
  1739. splits = 2;
  1740. break;
  1741. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS:
  1742. splits = 4;
  1743. break;
  1744. }
  1745. real_t distances[5];
  1746. distances[0] = min_distance;
  1747. for (int i = 0; i < splits; i++) {
  1748. distances[i + 1] = min_distance + RSG::light_storage->light_get_param(p_instance->base, RS::LightParam(RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET + i)) * range;
  1749. };
  1750. distances[splits] = max_distance;
  1751. real_t texture_size = RSG::light_storage->get_directional_light_shadow_size(light->instance);
  1752. bool overlap = RSG::light_storage->light_directional_get_blend_splits(p_instance->base);
  1753. cull.shadow_count = p_shadow_index + 1;
  1754. cull.shadows[p_shadow_index].cascade_count = splits;
  1755. cull.shadows[p_shadow_index].light_instance = light->instance;
  1756. for (int i = 0; i < splits; i++) {
  1757. RENDER_TIMESTAMP("Cull DirectionalLight3D, Split " + itos(i));
  1758. // setup a camera matrix for that range!
  1759. Projection camera_matrix;
  1760. real_t aspect = p_cam_projection.get_aspect();
  1761. if (p_cam_orthogonal) {
  1762. Vector2 vp_he = p_cam_projection.get_viewport_half_extents();
  1763. camera_matrix.set_orthogonal(vp_he.y * 2.0, aspect, distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], false);
  1764. } else {
  1765. real_t fov = p_cam_projection.get_fov(); //this is actually yfov, because set aspect tries to keep it
  1766. camera_matrix.set_perspective(fov, aspect, distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], true);
  1767. }
  1768. //obtain the frustum endpoints
  1769. Vector3 endpoints[8]; // frustum plane endpoints
  1770. bool res = camera_matrix.get_endpoints(p_cam_transform, endpoints);
  1771. ERR_CONTINUE(!res);
  1772. // obtain the light frustum ranges (given endpoints)
  1773. Transform3D transform = light_transform; //discard scale and stabilize light
  1774. Vector3 x_vec = transform.basis.get_column(Vector3::AXIS_X).normalized();
  1775. Vector3 y_vec = transform.basis.get_column(Vector3::AXIS_Y).normalized();
  1776. Vector3 z_vec = transform.basis.get_column(Vector3::AXIS_Z).normalized();
  1777. //z_vec points against the camera, like in default opengl
  1778. real_t x_min = 0.f, x_max = 0.f;
  1779. real_t y_min = 0.f, y_max = 0.f;
  1780. real_t z_min = 0.f, z_max = 0.f;
  1781. // FIXME: z_max_cam is defined, computed, but not used below when setting up
  1782. // ortho_camera. Commented out for now to fix warnings but should be investigated.
  1783. real_t x_min_cam = 0.f, x_max_cam = 0.f;
  1784. real_t y_min_cam = 0.f, y_max_cam = 0.f;
  1785. real_t z_min_cam = 0.f;
  1786. //real_t z_max_cam = 0.f;
  1787. //real_t bias_scale = 1.0;
  1788. //real_t aspect_bias_scale = 1.0;
  1789. //used for culling
  1790. for (int j = 0; j < 8; j++) {
  1791. real_t d_x = x_vec.dot(endpoints[j]);
  1792. real_t d_y = y_vec.dot(endpoints[j]);
  1793. real_t d_z = z_vec.dot(endpoints[j]);
  1794. if (j == 0 || d_x < x_min) {
  1795. x_min = d_x;
  1796. }
  1797. if (j == 0 || d_x > x_max) {
  1798. x_max = d_x;
  1799. }
  1800. if (j == 0 || d_y < y_min) {
  1801. y_min = d_y;
  1802. }
  1803. if (j == 0 || d_y > y_max) {
  1804. y_max = d_y;
  1805. }
  1806. if (j == 0 || d_z < z_min) {
  1807. z_min = d_z;
  1808. }
  1809. if (j == 0 || d_z > z_max) {
  1810. z_max = d_z;
  1811. }
  1812. }
  1813. real_t radius = 0;
  1814. real_t soft_shadow_expand = 0;
  1815. Vector3 center;
  1816. {
  1817. //camera viewport stuff
  1818. for (int j = 0; j < 8; j++) {
  1819. center += endpoints[j];
  1820. }
  1821. center /= 8.0;
  1822. //center=x_vec*(x_max-x_min)*0.5 + y_vec*(y_max-y_min)*0.5 + z_vec*(z_max-z_min)*0.5;
  1823. for (int j = 0; j < 8; j++) {
  1824. real_t d = center.distance_to(endpoints[j]);
  1825. if (d > radius) {
  1826. radius = d;
  1827. }
  1828. }
  1829. radius *= texture_size / (texture_size - 2.0); //add a texel by each side
  1830. z_min_cam = z_vec.dot(center) - radius;
  1831. {
  1832. float soft_shadow_angle = RSG::light_storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_SIZE);
  1833. if (soft_shadow_angle > 0.0) {
  1834. float z_range = (z_vec.dot(center) + radius + pancake_size) - z_min_cam;
  1835. soft_shadow_expand = Math::tan(Math::deg_to_rad(soft_shadow_angle)) * z_range;
  1836. x_max += soft_shadow_expand;
  1837. y_max += soft_shadow_expand;
  1838. x_min -= soft_shadow_expand;
  1839. y_min -= soft_shadow_expand;
  1840. }
  1841. }
  1842. // This trick here is what stabilizes the shadow (make potential jaggies to not move)
  1843. // at the cost of some wasted resolution. Still, the quality increase is very well worth it.
  1844. const real_t unit = (radius + soft_shadow_expand) * 2.0 / texture_size;
  1845. x_max_cam = Math::snapped(x_vec.dot(center) + radius + soft_shadow_expand, unit);
  1846. x_min_cam = Math::snapped(x_vec.dot(center) - radius - soft_shadow_expand, unit);
  1847. y_max_cam = Math::snapped(y_vec.dot(center) + radius + soft_shadow_expand, unit);
  1848. y_min_cam = Math::snapped(y_vec.dot(center) - radius - soft_shadow_expand, unit);
  1849. }
  1850. //now that we know all ranges, we can proceed to make the light frustum planes, for culling octree
  1851. Vector<Plane> light_frustum_planes;
  1852. light_frustum_planes.resize(6);
  1853. //right/left
  1854. light_frustum_planes.write[0] = Plane(x_vec, x_max);
  1855. light_frustum_planes.write[1] = Plane(-x_vec, -x_min);
  1856. //top/bottom
  1857. light_frustum_planes.write[2] = Plane(y_vec, y_max);
  1858. light_frustum_planes.write[3] = Plane(-y_vec, -y_min);
  1859. //near/far
  1860. light_frustum_planes.write[4] = Plane(z_vec, z_max + 1e6);
  1861. light_frustum_planes.write[5] = Plane(-z_vec, -z_min); // z_min is ok, since casters further than far-light plane are not needed
  1862. // a pre pass will need to be needed to determine the actual z-near to be used
  1863. z_max = z_vec.dot(center) + radius + pancake_size;
  1864. {
  1865. Projection ortho_camera;
  1866. real_t half_x = (x_max_cam - x_min_cam) * 0.5;
  1867. real_t half_y = (y_max_cam - y_min_cam) * 0.5;
  1868. ortho_camera.set_orthogonal(-half_x, half_x, -half_y, half_y, 0, (z_max - z_min_cam));
  1869. Vector2 uv_scale(1.0 / (x_max_cam - x_min_cam), 1.0 / (y_max_cam - y_min_cam));
  1870. Transform3D ortho_transform;
  1871. ortho_transform.basis = transform.basis;
  1872. ortho_transform.origin = x_vec * (x_min_cam + half_x) + y_vec * (y_min_cam + half_y) + z_vec * z_max;
  1873. cull.shadows[p_shadow_index].cascades[i].frustum = Frustum(light_frustum_planes);
  1874. cull.shadows[p_shadow_index].cascades[i].projection = ortho_camera;
  1875. cull.shadows[p_shadow_index].cascades[i].transform = ortho_transform;
  1876. cull.shadows[p_shadow_index].cascades[i].zfar = z_max - z_min_cam;
  1877. cull.shadows[p_shadow_index].cascades[i].split = distances[i + 1];
  1878. cull.shadows[p_shadow_index].cascades[i].shadow_texel_size = radius * 2.0 / texture_size;
  1879. cull.shadows[p_shadow_index].cascades[i].bias_scale = (z_max - z_min_cam);
  1880. cull.shadows[p_shadow_index].cascades[i].range_begin = z_max;
  1881. cull.shadows[p_shadow_index].cascades[i].uv_scale = uv_scale;
  1882. }
  1883. }
  1884. }
  1885. bool RendererSceneCull::_light_instance_update_shadow(Instance *p_instance, const Transform3D p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal, bool p_cam_vaspect, RID p_shadow_atlas, Scenario *p_scenario, float p_screen_mesh_lod_threshold, uint32_t p_visible_layers) {
  1886. InstanceLightData *light = static_cast<InstanceLightData *>(p_instance->base_data);
  1887. Transform3D light_transform = p_instance->transform;
  1888. light_transform.orthonormalize(); //scale does not count on lights
  1889. bool animated_material_found = false;
  1890. switch (RSG::light_storage->light_get_type(p_instance->base)) {
  1891. case RS::LIGHT_DIRECTIONAL: {
  1892. } break;
  1893. case RS::LIGHT_OMNI: {
  1894. RS::LightOmniShadowMode shadow_mode = RSG::light_storage->light_omni_get_shadow_mode(p_instance->base);
  1895. if (shadow_mode == RS::LIGHT_OMNI_SHADOW_DUAL_PARABOLOID || !RSG::light_storage->light_instances_can_render_shadow_cube()) {
  1896. if (max_shadows_used + 2 > MAX_UPDATE_SHADOWS) {
  1897. return true;
  1898. }
  1899. for (int i = 0; i < 2; i++) {
  1900. //using this one ensures that raster deferred will have it
  1901. RENDER_TIMESTAMP("Cull OmniLight3D Shadow Paraboloid, Half " + itos(i));
  1902. real_t radius = RSG::light_storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_RANGE);
  1903. real_t z = i == 0 ? -1 : 1;
  1904. Vector<Plane> planes;
  1905. planes.resize(6);
  1906. planes.write[0] = light_transform.xform(Plane(Vector3(0, 0, z), radius));
  1907. planes.write[1] = light_transform.xform(Plane(Vector3(1, 0, z).normalized(), radius));
  1908. planes.write[2] = light_transform.xform(Plane(Vector3(-1, 0, z).normalized(), radius));
  1909. planes.write[3] = light_transform.xform(Plane(Vector3(0, 1, z).normalized(), radius));
  1910. planes.write[4] = light_transform.xform(Plane(Vector3(0, -1, z).normalized(), radius));
  1911. planes.write[5] = light_transform.xform(Plane(Vector3(0, 0, -z), 0));
  1912. instance_shadow_cull_result.clear();
  1913. Vector<Vector3> points = Geometry3D::compute_convex_mesh_points(&planes[0], planes.size());
  1914. struct CullConvex {
  1915. PagedArray<Instance *> *result;
  1916. _FORCE_INLINE_ bool operator()(void *p_data) {
  1917. Instance *p_instance = (Instance *)p_data;
  1918. result->push_back(p_instance);
  1919. return false;
  1920. }
  1921. };
  1922. CullConvex cull_convex;
  1923. cull_convex.result = &instance_shadow_cull_result;
  1924. p_scenario->indexers[Scenario::INDEXER_GEOMETRY].convex_query(planes.ptr(), planes.size(), points.ptr(), points.size(), cull_convex);
  1925. RendererSceneRender::RenderShadowData &shadow_data = render_shadow_data[max_shadows_used++];
  1926. for (int j = 0; j < (int)instance_shadow_cull_result.size(); j++) {
  1927. Instance *instance = instance_shadow_cull_result[j];
  1928. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows || !(p_visible_layers & instance->layer_mask)) {
  1929. continue;
  1930. } else {
  1931. if (static_cast<InstanceGeometryData *>(instance->base_data)->material_is_animated) {
  1932. animated_material_found = true;
  1933. }
  1934. if (instance->mesh_instance.is_valid()) {
  1935. RSG::mesh_storage->mesh_instance_check_for_update(instance->mesh_instance);
  1936. }
  1937. }
  1938. shadow_data.instances.push_back(static_cast<InstanceGeometryData *>(instance->base_data)->geometry_instance);
  1939. }
  1940. RSG::mesh_storage->update_mesh_instances();
  1941. RSG::light_storage->light_instance_set_shadow_transform(light->instance, Projection(), light_transform, radius, 0, i, 0);
  1942. shadow_data.light = light->instance;
  1943. shadow_data.pass = i;
  1944. }
  1945. } else { //shadow cube
  1946. if (max_shadows_used + 6 > MAX_UPDATE_SHADOWS) {
  1947. return true;
  1948. }
  1949. real_t radius = RSG::light_storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_RANGE);
  1950. Projection cm;
  1951. cm.set_perspective(90, 1, radius * 0.005f, radius);
  1952. for (int i = 0; i < 6; i++) {
  1953. RENDER_TIMESTAMP("Cull OmniLight3D Shadow Cube, Side " + itos(i));
  1954. //using this one ensures that raster deferred will have it
  1955. static const Vector3 view_normals[6] = {
  1956. Vector3(+1, 0, 0),
  1957. Vector3(-1, 0, 0),
  1958. Vector3(0, -1, 0),
  1959. Vector3(0, +1, 0),
  1960. Vector3(0, 0, +1),
  1961. Vector3(0, 0, -1)
  1962. };
  1963. static const Vector3 view_up[6] = {
  1964. Vector3(0, -1, 0),
  1965. Vector3(0, -1, 0),
  1966. Vector3(0, 0, -1),
  1967. Vector3(0, 0, +1),
  1968. Vector3(0, -1, 0),
  1969. Vector3(0, -1, 0)
  1970. };
  1971. Transform3D xform = light_transform * Transform3D().looking_at(view_normals[i], view_up[i]);
  1972. Vector<Plane> planes = cm.get_projection_planes(xform);
  1973. instance_shadow_cull_result.clear();
  1974. Vector<Vector3> points = Geometry3D::compute_convex_mesh_points(&planes[0], planes.size());
  1975. struct CullConvex {
  1976. PagedArray<Instance *> *result;
  1977. _FORCE_INLINE_ bool operator()(void *p_data) {
  1978. Instance *p_instance = (Instance *)p_data;
  1979. result->push_back(p_instance);
  1980. return false;
  1981. }
  1982. };
  1983. CullConvex cull_convex;
  1984. cull_convex.result = &instance_shadow_cull_result;
  1985. p_scenario->indexers[Scenario::INDEXER_GEOMETRY].convex_query(planes.ptr(), planes.size(), points.ptr(), points.size(), cull_convex);
  1986. RendererSceneRender::RenderShadowData &shadow_data = render_shadow_data[max_shadows_used++];
  1987. for (int j = 0; j < (int)instance_shadow_cull_result.size(); j++) {
  1988. Instance *instance = instance_shadow_cull_result[j];
  1989. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows || !(p_visible_layers & instance->layer_mask)) {
  1990. continue;
  1991. } else {
  1992. if (static_cast<InstanceGeometryData *>(instance->base_data)->material_is_animated) {
  1993. animated_material_found = true;
  1994. }
  1995. if (instance->mesh_instance.is_valid()) {
  1996. RSG::mesh_storage->mesh_instance_check_for_update(instance->mesh_instance);
  1997. }
  1998. }
  1999. shadow_data.instances.push_back(static_cast<InstanceGeometryData *>(instance->base_data)->geometry_instance);
  2000. }
  2001. RSG::mesh_storage->update_mesh_instances();
  2002. RSG::light_storage->light_instance_set_shadow_transform(light->instance, cm, xform, radius, 0, i, 0);
  2003. shadow_data.light = light->instance;
  2004. shadow_data.pass = i;
  2005. }
  2006. //restore the regular DP matrix
  2007. //RSG::light_storage->light_instance_set_shadow_transform(light->instance, Projection(), light_transform, radius, 0, 0, 0);
  2008. }
  2009. } break;
  2010. case RS::LIGHT_SPOT: {
  2011. RENDER_TIMESTAMP("Cull SpotLight3D Shadow");
  2012. if (max_shadows_used + 1 > MAX_UPDATE_SHADOWS) {
  2013. return true;
  2014. }
  2015. real_t radius = RSG::light_storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_RANGE);
  2016. real_t angle = RSG::light_storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_SPOT_ANGLE);
  2017. Projection cm;
  2018. cm.set_perspective(angle * 2.0, 1.0, 0.005f * radius, radius);
  2019. Vector<Plane> planes = cm.get_projection_planes(light_transform);
  2020. instance_shadow_cull_result.clear();
  2021. Vector<Vector3> points = Geometry3D::compute_convex_mesh_points(&planes[0], planes.size());
  2022. struct CullConvex {
  2023. PagedArray<Instance *> *result;
  2024. _FORCE_INLINE_ bool operator()(void *p_data) {
  2025. Instance *p_instance = (Instance *)p_data;
  2026. result->push_back(p_instance);
  2027. return false;
  2028. }
  2029. };
  2030. CullConvex cull_convex;
  2031. cull_convex.result = &instance_shadow_cull_result;
  2032. p_scenario->indexers[Scenario::INDEXER_GEOMETRY].convex_query(planes.ptr(), planes.size(), points.ptr(), points.size(), cull_convex);
  2033. RendererSceneRender::RenderShadowData &shadow_data = render_shadow_data[max_shadows_used++];
  2034. for (int j = 0; j < (int)instance_shadow_cull_result.size(); j++) {
  2035. Instance *instance = instance_shadow_cull_result[j];
  2036. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows || !(p_visible_layers & instance->layer_mask)) {
  2037. continue;
  2038. } else {
  2039. if (static_cast<InstanceGeometryData *>(instance->base_data)->material_is_animated) {
  2040. animated_material_found = true;
  2041. }
  2042. if (instance->mesh_instance.is_valid()) {
  2043. RSG::mesh_storage->mesh_instance_check_for_update(instance->mesh_instance);
  2044. }
  2045. }
  2046. shadow_data.instances.push_back(static_cast<InstanceGeometryData *>(instance->base_data)->geometry_instance);
  2047. }
  2048. RSG::mesh_storage->update_mesh_instances();
  2049. RSG::light_storage->light_instance_set_shadow_transform(light->instance, cm, light_transform, radius, 0, 0, 0);
  2050. shadow_data.light = light->instance;
  2051. shadow_data.pass = 0;
  2052. } break;
  2053. }
  2054. return animated_material_found;
  2055. }
  2056. void RendererSceneCull::render_camera(const Ref<RenderSceneBuffers> &p_render_buffers, RID p_camera, RID p_scenario, RID p_viewport, Size2 p_viewport_size, uint32_t p_jitter_phase_count, float p_screen_mesh_lod_threshold, RID p_shadow_atlas, Ref<XRInterface> &p_xr_interface, RenderInfo *r_render_info) {
  2057. #ifndef _3D_DISABLED
  2058. Camera *camera = camera_owner.get_or_null(p_camera);
  2059. ERR_FAIL_COND(!camera);
  2060. Vector2 jitter;
  2061. if (p_jitter_phase_count > 0) {
  2062. uint32_t current_jitter_count = camera_jitter_array.size();
  2063. if (p_jitter_phase_count != current_jitter_count) {
  2064. // Resize the jitter array and fill it with the pre-computed Halton sequence.
  2065. camera_jitter_array.resize(p_jitter_phase_count);
  2066. for (uint32_t i = current_jitter_count; i < p_jitter_phase_count; i++) {
  2067. camera_jitter_array[i].x = get_halton_value(i, 2);
  2068. camera_jitter_array[i].y = get_halton_value(i, 3);
  2069. }
  2070. }
  2071. jitter = camera_jitter_array[RSG::rasterizer->get_frame_number() % p_jitter_phase_count] / p_viewport_size;
  2072. }
  2073. RendererSceneRender::CameraData camera_data;
  2074. // Setup Camera(s)
  2075. if (p_xr_interface.is_null()) {
  2076. // Normal camera
  2077. Transform3D transform = camera->transform;
  2078. Projection projection;
  2079. bool vaspect = camera->vaspect;
  2080. bool is_orthogonal = false;
  2081. switch (camera->type) {
  2082. case Camera::ORTHOGONAL: {
  2083. projection.set_orthogonal(
  2084. camera->size,
  2085. p_viewport_size.width / (float)p_viewport_size.height,
  2086. camera->znear,
  2087. camera->zfar,
  2088. camera->vaspect);
  2089. is_orthogonal = true;
  2090. } break;
  2091. case Camera::PERSPECTIVE: {
  2092. projection.set_perspective(
  2093. camera->fov,
  2094. p_viewport_size.width / (float)p_viewport_size.height,
  2095. camera->znear,
  2096. camera->zfar,
  2097. camera->vaspect);
  2098. } break;
  2099. case Camera::FRUSTUM: {
  2100. projection.set_frustum(
  2101. camera->size,
  2102. p_viewport_size.width / (float)p_viewport_size.height,
  2103. camera->offset,
  2104. camera->znear,
  2105. camera->zfar,
  2106. camera->vaspect);
  2107. } break;
  2108. }
  2109. camera_data.set_camera(transform, projection, is_orthogonal, vaspect, jitter, camera->visible_layers);
  2110. } else {
  2111. // Setup our camera for our XR interface.
  2112. // We can support multiple views here each with their own camera
  2113. Transform3D transforms[RendererSceneRender::MAX_RENDER_VIEWS];
  2114. Projection projections[RendererSceneRender::MAX_RENDER_VIEWS];
  2115. uint32_t view_count = p_xr_interface->get_view_count();
  2116. ERR_FAIL_COND_MSG(view_count == 0 || view_count > RendererSceneRender::MAX_RENDER_VIEWS, "Requested view count is not supported");
  2117. float aspect = p_viewport_size.width / (float)p_viewport_size.height;
  2118. Transform3D world_origin = XRServer::get_singleton()->get_world_origin();
  2119. // We ignore our camera position, it will have been positioned with a slightly old tracking position.
  2120. // Instead we take our origin point and have our XR interface add fresh tracking data! Whoohoo!
  2121. for (uint32_t v = 0; v < view_count; v++) {
  2122. transforms[v] = p_xr_interface->get_transform_for_view(v, world_origin);
  2123. projections[v] = p_xr_interface->get_projection_for_view(v, aspect, camera->znear, camera->zfar);
  2124. }
  2125. if (view_count == 1) {
  2126. camera_data.set_camera(transforms[0], projections[0], false, camera->vaspect, jitter, camera->visible_layers);
  2127. } else if (view_count == 2) {
  2128. camera_data.set_multiview_camera(view_count, transforms, projections, false, camera->vaspect);
  2129. } else {
  2130. // this won't be called (see fail check above) but keeping this comment to indicate we may support more then 2 views in the future...
  2131. }
  2132. }
  2133. RID environment = _render_get_environment(p_camera, p_scenario);
  2134. RENDER_TIMESTAMP("Update Occlusion Buffer")
  2135. // For now just cull on the first camera
  2136. RendererSceneOcclusionCull::get_singleton()->buffer_update(p_viewport, camera_data.main_transform, camera_data.main_projection, camera_data.is_orthogonal);
  2137. _render_scene(&camera_data, p_render_buffers, environment, camera->attributes, camera->visible_layers, p_scenario, p_viewport, p_shadow_atlas, RID(), -1, p_screen_mesh_lod_threshold, true, r_render_info);
  2138. #endif
  2139. }
  2140. void RendererSceneCull::_visibility_cull_threaded(uint32_t p_thread, VisibilityCullData *cull_data) {
  2141. uint32_t total_threads = WorkerThreadPool::get_singleton()->get_thread_count();
  2142. uint32_t bin_from = p_thread * cull_data->cull_count / total_threads;
  2143. uint32_t bin_to = (p_thread + 1 == total_threads) ? cull_data->cull_count : ((p_thread + 1) * cull_data->cull_count / total_threads);
  2144. _visibility_cull(*cull_data, cull_data->cull_offset + bin_from, cull_data->cull_offset + bin_to);
  2145. }
  2146. void RendererSceneCull::_visibility_cull(const VisibilityCullData &cull_data, uint64_t p_from, uint64_t p_to) {
  2147. Scenario *scenario = cull_data.scenario;
  2148. for (unsigned int i = p_from; i < p_to; i++) {
  2149. InstanceVisibilityData &vd = scenario->instance_visibility[i];
  2150. InstanceData &idata = scenario->instance_data[vd.array_index];
  2151. if (idata.parent_array_index >= 0) {
  2152. uint32_t parent_flags = scenario->instance_data[idata.parent_array_index].flags;
  2153. if ((parent_flags & InstanceData::FLAG_VISIBILITY_DEPENDENCY_HIDDEN) || !(parent_flags & (InstanceData::FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE | InstanceData::FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN))) {
  2154. idata.flags |= InstanceData::FLAG_VISIBILITY_DEPENDENCY_HIDDEN;
  2155. idata.flags &= ~InstanceData::FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE;
  2156. idata.flags &= ~InstanceData::FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN;
  2157. continue;
  2158. }
  2159. }
  2160. int range_check = _visibility_range_check<true>(vd, cull_data.camera_position, cull_data.viewport_mask);
  2161. if (range_check == -1) {
  2162. idata.flags |= InstanceData::FLAG_VISIBILITY_DEPENDENCY_HIDDEN;
  2163. idata.flags &= ~InstanceData::FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE;
  2164. idata.flags &= ~InstanceData::FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN;
  2165. } else if (range_check == 1) {
  2166. idata.flags &= ~InstanceData::FLAG_VISIBILITY_DEPENDENCY_HIDDEN;
  2167. idata.flags |= InstanceData::FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE;
  2168. idata.flags &= ~InstanceData::FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN;
  2169. } else {
  2170. idata.flags &= ~InstanceData::FLAG_VISIBILITY_DEPENDENCY_HIDDEN;
  2171. idata.flags &= ~InstanceData::FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE;
  2172. if (range_check == 2) {
  2173. idata.flags |= InstanceData::FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN;
  2174. } else {
  2175. idata.flags &= ~InstanceData::FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN;
  2176. }
  2177. }
  2178. }
  2179. }
  2180. template <bool p_fade_check>
  2181. int RendererSceneCull::_visibility_range_check(InstanceVisibilityData &r_vis_data, const Vector3 &p_camera_pos, uint64_t p_viewport_mask) {
  2182. float dist = p_camera_pos.distance_to(r_vis_data.position);
  2183. const RS::VisibilityRangeFadeMode &fade_mode = r_vis_data.fade_mode;
  2184. float begin_offset = -r_vis_data.range_begin_margin;
  2185. float end_offset = r_vis_data.range_end_margin;
  2186. if (fade_mode == RS::VISIBILITY_RANGE_FADE_DISABLED && !(p_viewport_mask & r_vis_data.viewport_state)) {
  2187. begin_offset = -begin_offset;
  2188. end_offset = -end_offset;
  2189. }
  2190. if (r_vis_data.range_end > 0.0f && dist > r_vis_data.range_end + end_offset) {
  2191. r_vis_data.viewport_state &= ~p_viewport_mask;
  2192. return -1;
  2193. } else if (r_vis_data.range_begin > 0.0f && dist < r_vis_data.range_begin + begin_offset) {
  2194. r_vis_data.viewport_state &= ~p_viewport_mask;
  2195. return 1;
  2196. } else {
  2197. r_vis_data.viewport_state |= p_viewport_mask;
  2198. if (p_fade_check) {
  2199. if (fade_mode != RS::VISIBILITY_RANGE_FADE_DISABLED) {
  2200. r_vis_data.children_fade_alpha = 1.0f;
  2201. if (r_vis_data.range_end > 0.0f && dist > r_vis_data.range_end - end_offset) {
  2202. if (fade_mode == RS::VISIBILITY_RANGE_FADE_DEPENDENCIES) {
  2203. r_vis_data.children_fade_alpha = MIN(1.0f, (dist - (r_vis_data.range_end - end_offset)) / (2.0f * r_vis_data.range_end_margin));
  2204. }
  2205. return 2;
  2206. } else if (r_vis_data.range_begin > 0.0f && dist < r_vis_data.range_begin - begin_offset) {
  2207. if (fade_mode == RS::VISIBILITY_RANGE_FADE_DEPENDENCIES) {
  2208. r_vis_data.children_fade_alpha = MIN(1.0f, 1.0 - (dist - (r_vis_data.range_begin + begin_offset)) / (2.0f * r_vis_data.range_begin_margin));
  2209. }
  2210. return 2;
  2211. }
  2212. }
  2213. }
  2214. return 0;
  2215. }
  2216. }
  2217. bool RendererSceneCull::_visibility_parent_check(const CullData &p_cull_data, const InstanceData &p_instance_data) {
  2218. if (p_instance_data.parent_array_index == -1) {
  2219. return true;
  2220. }
  2221. const uint32_t &parent_flags = p_cull_data.scenario->instance_data[p_instance_data.parent_array_index].flags;
  2222. return ((parent_flags & InstanceData::FLAG_VISIBILITY_DEPENDENCY_NEEDS_CHECK) == InstanceData::FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE) || (parent_flags & InstanceData::FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN);
  2223. }
  2224. void RendererSceneCull::_scene_cull_threaded(uint32_t p_thread, CullData *cull_data) {
  2225. uint32_t cull_total = cull_data->scenario->instance_data.size();
  2226. uint32_t total_threads = WorkerThreadPool::get_singleton()->get_thread_count();
  2227. uint32_t cull_from = p_thread * cull_total / total_threads;
  2228. uint32_t cull_to = (p_thread + 1 == total_threads) ? cull_total : ((p_thread + 1) * cull_total / total_threads);
  2229. _scene_cull(*cull_data, scene_cull_result_threads[p_thread], cull_from, cull_to);
  2230. }
  2231. void RendererSceneCull::_scene_cull(CullData &cull_data, InstanceCullResult &cull_result, uint64_t p_from, uint64_t p_to) {
  2232. uint64_t frame_number = RSG::rasterizer->get_frame_number();
  2233. float lightmap_probe_update_speed = RSG::light_storage->lightmap_get_probe_capture_update_speed() * RSG::rasterizer->get_frame_delta_time();
  2234. uint32_t sdfgi_last_light_index = 0xFFFFFFFF;
  2235. uint32_t sdfgi_last_light_cascade = 0xFFFFFFFF;
  2236. RID instance_pair_buffer[MAX_INSTANCE_PAIRS];
  2237. Transform3D inv_cam_transform = cull_data.cam_transform.inverse();
  2238. float z_near = cull_data.camera_matrix->get_z_near();
  2239. for (uint64_t i = p_from; i < p_to; i++) {
  2240. bool mesh_visible = false;
  2241. InstanceData &idata = cull_data.scenario->instance_data[i];
  2242. uint32_t visibility_flags = idata.flags & (InstanceData::FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE | InstanceData::FLAG_VISIBILITY_DEPENDENCY_HIDDEN | InstanceData::FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN);
  2243. int32_t visibility_check = -1;
  2244. #define HIDDEN_BY_VISIBILITY_CHECKS (visibility_flags == InstanceData::FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE || visibility_flags == InstanceData::FLAG_VISIBILITY_DEPENDENCY_HIDDEN)
  2245. #define LAYER_CHECK (cull_data.visible_layers & idata.layer_mask)
  2246. #define IN_FRUSTUM(f) (cull_data.scenario->instance_aabbs[i].in_frustum(f))
  2247. #define VIS_RANGE_CHECK ((idata.visibility_index == -1) || _visibility_range_check<false>(cull_data.scenario->instance_visibility[idata.visibility_index], cull_data.cam_transform.origin, cull_data.visibility_viewport_mask) == 0)
  2248. #define VIS_PARENT_CHECK (_visibility_parent_check(cull_data, idata))
  2249. #define VIS_CHECK (visibility_check < 0 ? (visibility_check = (visibility_flags != InstanceData::FLAG_VISIBILITY_DEPENDENCY_NEEDS_CHECK || (VIS_RANGE_CHECK && VIS_PARENT_CHECK))) : visibility_check)
  2250. #define OCCLUSION_CULLED (cull_data.occlusion_buffer != nullptr && (cull_data.scenario->instance_data[i].flags & InstanceData::FLAG_IGNORE_OCCLUSION_CULLING) == 0 && cull_data.occlusion_buffer->is_occluded(cull_data.scenario->instance_aabbs[i].bounds, cull_data.cam_transform.origin, inv_cam_transform, *cull_data.camera_matrix, z_near))
  2251. if (!HIDDEN_BY_VISIBILITY_CHECKS) {
  2252. if ((LAYER_CHECK && IN_FRUSTUM(cull_data.cull->frustum) && VIS_CHECK && !OCCLUSION_CULLED) || (cull_data.scenario->instance_data[i].flags & InstanceData::FLAG_IGNORE_ALL_CULLING)) {
  2253. uint32_t base_type = idata.flags & InstanceData::FLAG_BASE_TYPE_MASK;
  2254. if (base_type == RS::INSTANCE_LIGHT) {
  2255. cull_result.lights.push_back(idata.instance);
  2256. cull_result.light_instances.push_back(RID::from_uint64(idata.instance_data_rid));
  2257. if (cull_data.shadow_atlas.is_valid() && RSG::light_storage->light_has_shadow(idata.base_rid)) {
  2258. RSG::light_storage->light_instance_mark_visible(RID::from_uint64(idata.instance_data_rid)); //mark it visible for shadow allocation later
  2259. }
  2260. } else if (base_type == RS::INSTANCE_REFLECTION_PROBE) {
  2261. if (cull_data.render_reflection_probe != idata.instance) {
  2262. //avoid entering The Matrix
  2263. if ((idata.flags & InstanceData::FLAG_REFLECTION_PROBE_DIRTY) || RSG::light_storage->reflection_probe_instance_needs_redraw(RID::from_uint64(idata.instance_data_rid))) {
  2264. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(idata.instance->base_data);
  2265. cull_data.cull->lock.lock();
  2266. if (!reflection_probe->update_list.in_list()) {
  2267. reflection_probe->render_step = 0;
  2268. reflection_probe_render_list.add_last(&reflection_probe->update_list);
  2269. }
  2270. cull_data.cull->lock.unlock();
  2271. idata.flags &= ~uint32_t(InstanceData::FLAG_REFLECTION_PROBE_DIRTY);
  2272. }
  2273. if (RSG::light_storage->reflection_probe_instance_has_reflection(RID::from_uint64(idata.instance_data_rid))) {
  2274. cull_result.reflections.push_back(RID::from_uint64(idata.instance_data_rid));
  2275. }
  2276. }
  2277. } else if (base_type == RS::INSTANCE_DECAL) {
  2278. cull_result.decals.push_back(RID::from_uint64(idata.instance_data_rid));
  2279. } else if (base_type == RS::INSTANCE_VOXEL_GI) {
  2280. InstanceVoxelGIData *voxel_gi = static_cast<InstanceVoxelGIData *>(idata.instance->base_data);
  2281. cull_data.cull->lock.lock();
  2282. if (!voxel_gi->update_element.in_list()) {
  2283. voxel_gi_update_list.add(&voxel_gi->update_element);
  2284. }
  2285. cull_data.cull->lock.unlock();
  2286. cull_result.voxel_gi_instances.push_back(RID::from_uint64(idata.instance_data_rid));
  2287. } else if (base_type == RS::INSTANCE_LIGHTMAP) {
  2288. cull_result.lightmaps.push_back(RID::from_uint64(idata.instance_data_rid));
  2289. } else if (base_type == RS::INSTANCE_FOG_VOLUME) {
  2290. cull_result.fog_volumes.push_back(RID::from_uint64(idata.instance_data_rid));
  2291. } else if (base_type == RS::INSTANCE_VISIBLITY_NOTIFIER) {
  2292. InstanceVisibilityNotifierData *vnd = idata.visibility_notifier;
  2293. if (!vnd->list_element.in_list()) {
  2294. visible_notifier_list_lock.lock();
  2295. visible_notifier_list.add(&vnd->list_element);
  2296. visible_notifier_list_lock.unlock();
  2297. vnd->just_visible = true;
  2298. }
  2299. vnd->visible_in_frame = RSG::rasterizer->get_frame_number();
  2300. } else if (((1 << base_type) & RS::INSTANCE_GEOMETRY_MASK) && !(idata.flags & InstanceData::FLAG_CAST_SHADOWS_ONLY)) {
  2301. bool keep = true;
  2302. if (idata.flags & InstanceData::FLAG_REDRAW_IF_VISIBLE) {
  2303. RenderingServerDefault::redraw_request();
  2304. }
  2305. if (base_type == RS::INSTANCE_MESH) {
  2306. mesh_visible = true;
  2307. } else if (base_type == RS::INSTANCE_PARTICLES) {
  2308. //particles visible? process them
  2309. if (RSG::particles_storage->particles_is_inactive(idata.base_rid)) {
  2310. //but if nothing is going on, don't do it.
  2311. keep = false;
  2312. } else {
  2313. cull_data.cull->lock.lock();
  2314. RSG::particles_storage->particles_request_process(idata.base_rid);
  2315. cull_data.cull->lock.unlock();
  2316. RSG::particles_storage->particles_set_view_axis(idata.base_rid, -cull_data.cam_transform.basis.get_column(2).normalized(), cull_data.cam_transform.basis.get_column(1).normalized());
  2317. //particles visible? request redraw
  2318. RenderingServerDefault::redraw_request();
  2319. }
  2320. }
  2321. if (idata.parent_array_index != -1) {
  2322. float fade = 1.0f;
  2323. const uint32_t &parent_flags = cull_data.scenario->instance_data[idata.parent_array_index].flags;
  2324. if (parent_flags & InstanceData::FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN) {
  2325. const int32_t &parent_idx = cull_data.scenario->instance_data[idata.parent_array_index].visibility_index;
  2326. fade = cull_data.scenario->instance_visibility[parent_idx].children_fade_alpha;
  2327. }
  2328. idata.instance_geometry->set_parent_fade_alpha(fade);
  2329. }
  2330. if (geometry_instance_pair_mask & (1 << RS::INSTANCE_LIGHT) && (idata.flags & InstanceData::FLAG_GEOM_LIGHTING_DIRTY)) {
  2331. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(idata.instance->base_data);
  2332. uint32_t idx = 0;
  2333. for (const Instance *E : geom->lights) {
  2334. InstanceLightData *light = static_cast<InstanceLightData *>(E->base_data);
  2335. instance_pair_buffer[idx++] = light->instance;
  2336. if (idx == MAX_INSTANCE_PAIRS) {
  2337. break;
  2338. }
  2339. }
  2340. ERR_FAIL_NULL(geom->geometry_instance);
  2341. geom->geometry_instance->pair_light_instances(instance_pair_buffer, idx);
  2342. idata.flags &= ~uint32_t(InstanceData::FLAG_GEOM_LIGHTING_DIRTY);
  2343. }
  2344. if (idata.flags & InstanceData::FLAG_GEOM_PROJECTOR_SOFTSHADOW_DIRTY) {
  2345. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(idata.instance->base_data);
  2346. ERR_FAIL_NULL(geom->geometry_instance);
  2347. cull_data.cull->lock.lock();
  2348. geom->geometry_instance->set_softshadow_projector_pairing(geom->softshadow_count > 0, geom->projector_count > 0);
  2349. cull_data.cull->lock.unlock();
  2350. idata.flags &= ~uint32_t(InstanceData::FLAG_GEOM_PROJECTOR_SOFTSHADOW_DIRTY);
  2351. }
  2352. if (geometry_instance_pair_mask & (1 << RS::INSTANCE_REFLECTION_PROBE) && (idata.flags & InstanceData::FLAG_GEOM_REFLECTION_DIRTY)) {
  2353. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(idata.instance->base_data);
  2354. uint32_t idx = 0;
  2355. for (const Instance *E : geom->reflection_probes) {
  2356. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(E->base_data);
  2357. instance_pair_buffer[idx++] = reflection_probe->instance;
  2358. if (idx == MAX_INSTANCE_PAIRS) {
  2359. break;
  2360. }
  2361. }
  2362. ERR_FAIL_NULL(geom->geometry_instance);
  2363. geom->geometry_instance->pair_reflection_probe_instances(instance_pair_buffer, idx);
  2364. idata.flags &= ~uint32_t(InstanceData::FLAG_GEOM_REFLECTION_DIRTY);
  2365. }
  2366. if (geometry_instance_pair_mask & (1 << RS::INSTANCE_DECAL) && (idata.flags & InstanceData::FLAG_GEOM_DECAL_DIRTY)) {
  2367. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(idata.instance->base_data);
  2368. uint32_t idx = 0;
  2369. for (const Instance *E : geom->decals) {
  2370. InstanceDecalData *decal = static_cast<InstanceDecalData *>(E->base_data);
  2371. instance_pair_buffer[idx++] = decal->instance;
  2372. if (idx == MAX_INSTANCE_PAIRS) {
  2373. break;
  2374. }
  2375. }
  2376. ERR_FAIL_NULL(geom->geometry_instance);
  2377. geom->geometry_instance->pair_decal_instances(instance_pair_buffer, idx);
  2378. idata.flags &= ~uint32_t(InstanceData::FLAG_GEOM_DECAL_DIRTY);
  2379. }
  2380. if (idata.flags & InstanceData::FLAG_GEOM_VOXEL_GI_DIRTY) {
  2381. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(idata.instance->base_data);
  2382. uint32_t idx = 0;
  2383. for (const Instance *E : geom->voxel_gi_instances) {
  2384. InstanceVoxelGIData *voxel_gi = static_cast<InstanceVoxelGIData *>(E->base_data);
  2385. instance_pair_buffer[idx++] = voxel_gi->probe_instance;
  2386. if (idx == MAX_INSTANCE_PAIRS) {
  2387. break;
  2388. }
  2389. }
  2390. ERR_FAIL_NULL(geom->geometry_instance);
  2391. geom->geometry_instance->pair_voxel_gi_instances(instance_pair_buffer, idx);
  2392. idata.flags &= ~uint32_t(InstanceData::FLAG_GEOM_VOXEL_GI_DIRTY);
  2393. }
  2394. if ((idata.flags & InstanceData::FLAG_LIGHTMAP_CAPTURE) && idata.instance->last_frame_pass != frame_number && !idata.instance->lightmap_target_sh.is_empty() && !idata.instance->lightmap_sh.is_empty()) {
  2395. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(idata.instance->base_data);
  2396. Color *sh = idata.instance->lightmap_sh.ptrw();
  2397. const Color *target_sh = idata.instance->lightmap_target_sh.ptr();
  2398. for (uint32_t j = 0; j < 9; j++) {
  2399. sh[j] = sh[j].lerp(target_sh[j], MIN(1.0, lightmap_probe_update_speed));
  2400. }
  2401. ERR_FAIL_NULL(geom->geometry_instance);
  2402. cull_data.cull->lock.lock();
  2403. geom->geometry_instance->set_lightmap_capture(sh);
  2404. cull_data.cull->lock.unlock();
  2405. idata.instance->last_frame_pass = frame_number;
  2406. }
  2407. if (keep) {
  2408. cull_result.geometry_instances.push_back(idata.instance_geometry);
  2409. }
  2410. }
  2411. }
  2412. for (uint32_t j = 0; j < cull_data.cull->shadow_count; j++) {
  2413. for (uint32_t k = 0; k < cull_data.cull->shadows[j].cascade_count; k++) {
  2414. if (IN_FRUSTUM(cull_data.cull->shadows[j].cascades[k].frustum) && VIS_CHECK) {
  2415. uint32_t base_type = idata.flags & InstanceData::FLAG_BASE_TYPE_MASK;
  2416. if (((1 << base_type) & RS::INSTANCE_GEOMETRY_MASK) && idata.flags & InstanceData::FLAG_CAST_SHADOWS && LAYER_CHECK) {
  2417. cull_result.directional_shadows[j].cascade_geometry_instances[k].push_back(idata.instance_geometry);
  2418. mesh_visible = true;
  2419. }
  2420. }
  2421. }
  2422. }
  2423. }
  2424. #undef HIDDEN_BY_VISIBILITY_CHECKS
  2425. #undef LAYER_CHECK
  2426. #undef IN_FRUSTUM
  2427. #undef VIS_RANGE_CHECK
  2428. #undef VIS_PARENT_CHECK
  2429. #undef VIS_CHECK
  2430. #undef OCCLUSION_CULLED
  2431. for (uint32_t j = 0; j < cull_data.cull->sdfgi.region_count; j++) {
  2432. if (cull_data.scenario->instance_aabbs[i].in_aabb(cull_data.cull->sdfgi.region_aabb[j])) {
  2433. uint32_t base_type = idata.flags & InstanceData::FLAG_BASE_TYPE_MASK;
  2434. if (base_type == RS::INSTANCE_LIGHT) {
  2435. InstanceLightData *instance_light = (InstanceLightData *)idata.instance->base_data;
  2436. if (instance_light->bake_mode == RS::LIGHT_BAKE_STATIC && cull_data.cull->sdfgi.region_cascade[j] <= instance_light->max_sdfgi_cascade) {
  2437. if (sdfgi_last_light_index != i || sdfgi_last_light_cascade != cull_data.cull->sdfgi.region_cascade[j]) {
  2438. sdfgi_last_light_index = i;
  2439. sdfgi_last_light_cascade = cull_data.cull->sdfgi.region_cascade[j];
  2440. cull_result.sdfgi_cascade_lights[sdfgi_last_light_cascade].push_back(instance_light->instance);
  2441. }
  2442. }
  2443. } else if ((1 << base_type) & RS::INSTANCE_GEOMETRY_MASK) {
  2444. if (idata.flags & InstanceData::FLAG_USES_BAKED_LIGHT) {
  2445. cull_result.sdfgi_region_geometry_instances[j].push_back(idata.instance_geometry);
  2446. mesh_visible = true;
  2447. }
  2448. }
  2449. }
  2450. }
  2451. if (mesh_visible && cull_data.scenario->instance_data[i].flags & InstanceData::FLAG_USES_MESH_INSTANCE) {
  2452. cull_result.mesh_instances.push_back(cull_data.scenario->instance_data[i].instance->mesh_instance);
  2453. }
  2454. }
  2455. }
  2456. void RendererSceneCull::_render_scene(const RendererSceneRender::CameraData *p_camera_data, const Ref<RenderSceneBuffers> &p_render_buffers, RID p_environment, RID p_force_camera_attributes, uint32_t p_visible_layers, RID p_scenario, RID p_viewport, RID p_shadow_atlas, RID p_reflection_probe, int p_reflection_probe_pass, float p_screen_mesh_lod_threshold, bool p_using_shadows, RenderingMethod::RenderInfo *r_render_info) {
  2457. Instance *render_reflection_probe = instance_owner.get_or_null(p_reflection_probe); //if null, not rendering to it
  2458. Scenario *scenario = scenario_owner.get_or_null(p_scenario);
  2459. ERR_FAIL_COND(p_render_buffers.is_null());
  2460. render_pass++;
  2461. scene_render->set_scene_pass(render_pass);
  2462. if (p_reflection_probe.is_null()) {
  2463. //no rendering code here, this is only to set up what needs to be done, request regions, etc.
  2464. scene_render->sdfgi_update(p_render_buffers, p_environment, p_camera_data->main_transform.origin); //update conditions for SDFGI (whether its used or not)
  2465. }
  2466. RENDER_TIMESTAMP("Update Visibility Dependencies");
  2467. if (scenario->instance_visibility.get_bin_count() > 0) {
  2468. if (!scenario->viewport_visibility_masks.has(p_viewport)) {
  2469. scenario_add_viewport_visibility_mask(scenario->self, p_viewport);
  2470. }
  2471. VisibilityCullData visibility_cull_data;
  2472. visibility_cull_data.scenario = scenario;
  2473. visibility_cull_data.viewport_mask = scenario->viewport_visibility_masks[p_viewport];
  2474. visibility_cull_data.camera_position = p_camera_data->main_transform.origin;
  2475. for (int i = scenario->instance_visibility.get_bin_count() - 1; i > 0; i--) { // We skip bin 0
  2476. visibility_cull_data.cull_offset = scenario->instance_visibility.get_bin_start(i);
  2477. visibility_cull_data.cull_count = scenario->instance_visibility.get_bin_size(i);
  2478. if (visibility_cull_data.cull_count == 0) {
  2479. continue;
  2480. }
  2481. if (visibility_cull_data.cull_count > thread_cull_threshold) {
  2482. WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &RendererSceneCull::_visibility_cull_threaded, &visibility_cull_data, WorkerThreadPool::get_singleton()->get_thread_count(), -1, true, SNAME("VisibilityCullInstances"));
  2483. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  2484. } else {
  2485. _visibility_cull(visibility_cull_data, visibility_cull_data.cull_offset, visibility_cull_data.cull_offset + visibility_cull_data.cull_count);
  2486. }
  2487. }
  2488. }
  2489. RENDER_TIMESTAMP("Cull 3D Scene");
  2490. //rasterizer->set_camera(p_camera_data->main_transform, p_camera_data.main_projection, p_camera_data.is_orthogonal);
  2491. /* STEP 2 - CULL */
  2492. Vector<Plane> planes = p_camera_data->main_projection.get_projection_planes(p_camera_data->main_transform);
  2493. cull.frustum = Frustum(planes);
  2494. Vector<RID> directional_lights;
  2495. // directional lights
  2496. {
  2497. cull.shadow_count = 0;
  2498. Vector<Instance *> lights_with_shadow;
  2499. for (Instance *E : scenario->directional_lights) {
  2500. if (!E->visible) {
  2501. continue;
  2502. }
  2503. if (directional_lights.size() > RendererSceneRender::MAX_DIRECTIONAL_LIGHTS) {
  2504. break;
  2505. }
  2506. InstanceLightData *light = static_cast<InstanceLightData *>(E->base_data);
  2507. //check shadow..
  2508. if (light) {
  2509. if (p_using_shadows && p_shadow_atlas.is_valid() && RSG::light_storage->light_has_shadow(E->base) && !(RSG::light_storage->light_get_type(E->base) == RS::LIGHT_DIRECTIONAL && RSG::light_storage->light_directional_get_sky_mode(E->base) == RS::LIGHT_DIRECTIONAL_SKY_MODE_SKY_ONLY)) {
  2510. lights_with_shadow.push_back(E);
  2511. }
  2512. //add to list
  2513. directional_lights.push_back(light->instance);
  2514. }
  2515. }
  2516. RSG::light_storage->set_directional_shadow_count(lights_with_shadow.size());
  2517. for (int i = 0; i < lights_with_shadow.size(); i++) {
  2518. _light_instance_setup_directional_shadow(i, lights_with_shadow[i], p_camera_data->main_transform, p_camera_data->main_projection, p_camera_data->is_orthogonal, p_camera_data->vaspect);
  2519. }
  2520. }
  2521. { //sdfgi
  2522. cull.sdfgi.region_count = 0;
  2523. if (p_reflection_probe.is_null()) {
  2524. cull.sdfgi.cascade_light_count = 0;
  2525. uint32_t prev_cascade = 0xFFFFFFFF;
  2526. uint32_t pending_region_count = scene_render->sdfgi_get_pending_region_count(p_render_buffers);
  2527. for (uint32_t i = 0; i < pending_region_count; i++) {
  2528. cull.sdfgi.region_aabb[i] = scene_render->sdfgi_get_pending_region_bounds(p_render_buffers, i);
  2529. uint32_t region_cascade = scene_render->sdfgi_get_pending_region_cascade(p_render_buffers, i);
  2530. cull.sdfgi.region_cascade[i] = region_cascade;
  2531. if (region_cascade != prev_cascade) {
  2532. cull.sdfgi.cascade_light_index[cull.sdfgi.cascade_light_count] = region_cascade;
  2533. cull.sdfgi.cascade_light_count++;
  2534. prev_cascade = region_cascade;
  2535. }
  2536. }
  2537. cull.sdfgi.region_count = pending_region_count;
  2538. }
  2539. }
  2540. scene_cull_result.clear();
  2541. {
  2542. uint64_t cull_from = 0;
  2543. uint64_t cull_to = scenario->instance_data.size();
  2544. CullData cull_data;
  2545. //prepare for eventual thread usage
  2546. cull_data.cull = &cull;
  2547. cull_data.scenario = scenario;
  2548. cull_data.shadow_atlas = p_shadow_atlas;
  2549. cull_data.cam_transform = p_camera_data->main_transform;
  2550. cull_data.visible_layers = p_visible_layers;
  2551. cull_data.render_reflection_probe = render_reflection_probe;
  2552. cull_data.occlusion_buffer = RendererSceneOcclusionCull::get_singleton()->buffer_get_ptr(p_viewport);
  2553. cull_data.camera_matrix = &p_camera_data->main_projection;
  2554. cull_data.visibility_viewport_mask = scenario->viewport_visibility_masks.has(p_viewport) ? scenario->viewport_visibility_masks[p_viewport] : 0;
  2555. //#define DEBUG_CULL_TIME
  2556. #ifdef DEBUG_CULL_TIME
  2557. uint64_t time_from = OS::get_singleton()->get_ticks_usec();
  2558. #endif
  2559. if (cull_to > thread_cull_threshold) {
  2560. //multiple threads
  2561. for (InstanceCullResult &thread : scene_cull_result_threads) {
  2562. thread.clear();
  2563. }
  2564. WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &RendererSceneCull::_scene_cull_threaded, &cull_data, scene_cull_result_threads.size(), -1, true, SNAME("RenderCullInstances"));
  2565. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  2566. for (InstanceCullResult &thread : scene_cull_result_threads) {
  2567. scene_cull_result.append_from(thread);
  2568. }
  2569. } else {
  2570. //single threaded
  2571. _scene_cull(cull_data, scene_cull_result, cull_from, cull_to);
  2572. }
  2573. #ifdef DEBUG_CULL_TIME
  2574. static float time_avg = 0;
  2575. static uint32_t time_count = 0;
  2576. time_avg += double(OS::get_singleton()->get_ticks_usec() - time_from) / 1000.0;
  2577. time_count++;
  2578. print_line("time taken: " + rtos(time_avg / time_count));
  2579. #endif
  2580. if (scene_cull_result.mesh_instances.size()) {
  2581. for (uint64_t i = 0; i < scene_cull_result.mesh_instances.size(); i++) {
  2582. RSG::mesh_storage->mesh_instance_check_for_update(scene_cull_result.mesh_instances[i]);
  2583. }
  2584. RSG::mesh_storage->update_mesh_instances();
  2585. }
  2586. }
  2587. //render shadows
  2588. max_shadows_used = 0;
  2589. if (p_using_shadows) { //setup shadow maps
  2590. // Directional Shadows
  2591. for (uint32_t i = 0; i < cull.shadow_count; i++) {
  2592. for (uint32_t j = 0; j < cull.shadows[i].cascade_count; j++) {
  2593. const Cull::Shadow::Cascade &c = cull.shadows[i].cascades[j];
  2594. // print_line("shadow " + itos(i) + " cascade " + itos(j) + " elements: " + itos(c.cull_result.size()));
  2595. RSG::light_storage->light_instance_set_shadow_transform(cull.shadows[i].light_instance, c.projection, c.transform, c.zfar, c.split, j, c.shadow_texel_size, c.bias_scale, c.range_begin, c.uv_scale);
  2596. if (max_shadows_used == MAX_UPDATE_SHADOWS) {
  2597. continue;
  2598. }
  2599. render_shadow_data[max_shadows_used].light = cull.shadows[i].light_instance;
  2600. render_shadow_data[max_shadows_used].pass = j;
  2601. render_shadow_data[max_shadows_used].instances.merge_unordered(scene_cull_result.directional_shadows[i].cascade_geometry_instances[j]);
  2602. max_shadows_used++;
  2603. }
  2604. }
  2605. // Positional Shadowss
  2606. for (uint32_t i = 0; i < (uint32_t)scene_cull_result.lights.size(); i++) {
  2607. Instance *ins = scene_cull_result.lights[i];
  2608. if (!p_shadow_atlas.is_valid() || !RSG::light_storage->light_has_shadow(ins->base)) {
  2609. continue;
  2610. }
  2611. InstanceLightData *light = static_cast<InstanceLightData *>(ins->base_data);
  2612. float coverage = 0.f;
  2613. { //compute coverage
  2614. Transform3D cam_xf = p_camera_data->main_transform;
  2615. float zn = p_camera_data->main_projection.get_z_near();
  2616. Plane p(-cam_xf.basis.get_column(2), cam_xf.origin + cam_xf.basis.get_column(2) * -zn); //camera near plane
  2617. // near plane half width and height
  2618. Vector2 vp_half_extents = p_camera_data->main_projection.get_viewport_half_extents();
  2619. switch (RSG::light_storage->light_get_type(ins->base)) {
  2620. case RS::LIGHT_OMNI: {
  2621. float radius = RSG::light_storage->light_get_param(ins->base, RS::LIGHT_PARAM_RANGE);
  2622. //get two points parallel to near plane
  2623. Vector3 points[2] = {
  2624. ins->transform.origin,
  2625. ins->transform.origin + cam_xf.basis.get_column(0) * radius
  2626. };
  2627. if (!p_camera_data->is_orthogonal) {
  2628. //if using perspetive, map them to near plane
  2629. for (int j = 0; j < 2; j++) {
  2630. if (p.distance_to(points[j]) < 0) {
  2631. points[j].z = -zn; //small hack to keep size constant when hitting the screen
  2632. }
  2633. p.intersects_segment(cam_xf.origin, points[j], &points[j]); //map to plane
  2634. }
  2635. }
  2636. float screen_diameter = points[0].distance_to(points[1]) * 2;
  2637. coverage = screen_diameter / (vp_half_extents.x + vp_half_extents.y);
  2638. } break;
  2639. case RS::LIGHT_SPOT: {
  2640. float radius = RSG::light_storage->light_get_param(ins->base, RS::LIGHT_PARAM_RANGE);
  2641. float angle = RSG::light_storage->light_get_param(ins->base, RS::LIGHT_PARAM_SPOT_ANGLE);
  2642. float w = radius * Math::sin(Math::deg_to_rad(angle));
  2643. float d = radius * Math::cos(Math::deg_to_rad(angle));
  2644. Vector3 base = ins->transform.origin - ins->transform.basis.get_column(2).normalized() * d;
  2645. Vector3 points[2] = {
  2646. base,
  2647. base + cam_xf.basis.get_column(0) * w
  2648. };
  2649. if (!p_camera_data->is_orthogonal) {
  2650. //if using perspetive, map them to near plane
  2651. for (int j = 0; j < 2; j++) {
  2652. if (p.distance_to(points[j]) < 0) {
  2653. points[j].z = -zn; //small hack to keep size constant when hitting the screen
  2654. }
  2655. p.intersects_segment(cam_xf.origin, points[j], &points[j]); //map to plane
  2656. }
  2657. }
  2658. float screen_diameter = points[0].distance_to(points[1]) * 2;
  2659. coverage = screen_diameter / (vp_half_extents.x + vp_half_extents.y);
  2660. } break;
  2661. default: {
  2662. ERR_PRINT("Invalid Light Type");
  2663. }
  2664. }
  2665. }
  2666. if (light->shadow_dirty) {
  2667. light->last_version++;
  2668. light->shadow_dirty = false;
  2669. }
  2670. bool redraw = RSG::light_storage->shadow_atlas_update_light(p_shadow_atlas, light->instance, coverage, light->last_version);
  2671. if (redraw && max_shadows_used < MAX_UPDATE_SHADOWS) {
  2672. //must redraw!
  2673. RENDER_TIMESTAMP("> Render Light3D " + itos(i));
  2674. light->shadow_dirty = _light_instance_update_shadow(ins, p_camera_data->main_transform, p_camera_data->main_projection, p_camera_data->is_orthogonal, p_camera_data->vaspect, p_shadow_atlas, scenario, p_screen_mesh_lod_threshold, p_visible_layers);
  2675. RENDER_TIMESTAMP("< Render Light3D " + itos(i));
  2676. } else {
  2677. light->shadow_dirty = redraw;
  2678. }
  2679. }
  2680. }
  2681. //render SDFGI
  2682. {
  2683. // Q: Should this whole block be skipped if we're rendering our reflection probe?
  2684. sdfgi_update_data.update_static = false;
  2685. if (cull.sdfgi.region_count > 0) {
  2686. //update regions
  2687. for (uint32_t i = 0; i < cull.sdfgi.region_count; i++) {
  2688. render_sdfgi_data[i].instances.merge_unordered(scene_cull_result.sdfgi_region_geometry_instances[i]);
  2689. render_sdfgi_data[i].region = i;
  2690. }
  2691. //check if static lights were culled
  2692. bool static_lights_culled = false;
  2693. for (uint32_t i = 0; i < cull.sdfgi.cascade_light_count; i++) {
  2694. if (scene_cull_result.sdfgi_cascade_lights[i].size()) {
  2695. static_lights_culled = true;
  2696. break;
  2697. }
  2698. }
  2699. if (static_lights_culled) {
  2700. sdfgi_update_data.static_cascade_count = cull.sdfgi.cascade_light_count;
  2701. sdfgi_update_data.static_cascade_indices = cull.sdfgi.cascade_light_index;
  2702. sdfgi_update_data.static_positional_lights = scene_cull_result.sdfgi_cascade_lights;
  2703. sdfgi_update_data.update_static = true;
  2704. }
  2705. }
  2706. if (p_reflection_probe.is_null()) {
  2707. sdfgi_update_data.directional_lights = &directional_lights;
  2708. sdfgi_update_data.positional_light_instances = scenario->dynamic_lights.ptr();
  2709. sdfgi_update_data.positional_light_count = scenario->dynamic_lights.size();
  2710. }
  2711. }
  2712. //append the directional lights to the lights culled
  2713. for (int i = 0; i < directional_lights.size(); i++) {
  2714. scene_cull_result.light_instances.push_back(directional_lights[i]);
  2715. }
  2716. RID camera_attributes;
  2717. if (p_force_camera_attributes.is_valid()) {
  2718. camera_attributes = p_force_camera_attributes;
  2719. } else {
  2720. camera_attributes = scenario->camera_attributes;
  2721. }
  2722. /* PROCESS GEOMETRY AND DRAW SCENE */
  2723. RID occluders_tex;
  2724. const RendererSceneRender::CameraData *prev_camera_data = p_camera_data;
  2725. if (p_viewport.is_valid()) {
  2726. occluders_tex = RSG::viewport->viewport_get_occluder_debug_texture(p_viewport);
  2727. prev_camera_data = RSG::viewport->viewport_get_prev_camera_data(p_viewport);
  2728. }
  2729. RENDER_TIMESTAMP("Render 3D Scene");
  2730. scene_render->render_scene(p_render_buffers, p_camera_data, prev_camera_data, scene_cull_result.geometry_instances, scene_cull_result.light_instances, scene_cull_result.reflections, scene_cull_result.voxel_gi_instances, scene_cull_result.decals, scene_cull_result.lightmaps, scene_cull_result.fog_volumes, p_environment, camera_attributes, p_shadow_atlas, occluders_tex, p_reflection_probe.is_valid() ? RID() : scenario->reflection_atlas, p_reflection_probe, p_reflection_probe_pass, p_screen_mesh_lod_threshold, render_shadow_data, max_shadows_used, render_sdfgi_data, cull.sdfgi.region_count, &sdfgi_update_data, r_render_info);
  2731. if (p_viewport.is_valid()) {
  2732. RSG::viewport->viewport_set_prev_camera_data(p_viewport, p_camera_data);
  2733. }
  2734. for (uint32_t i = 0; i < max_shadows_used; i++) {
  2735. render_shadow_data[i].instances.clear();
  2736. }
  2737. max_shadows_used = 0;
  2738. for (uint32_t i = 0; i < cull.sdfgi.region_count; i++) {
  2739. render_sdfgi_data[i].instances.clear();
  2740. }
  2741. }
  2742. RID RendererSceneCull::_render_get_environment(RID p_camera, RID p_scenario) {
  2743. Camera *camera = camera_owner.get_or_null(p_camera);
  2744. if (camera && scene_render->is_environment(camera->env)) {
  2745. return camera->env;
  2746. }
  2747. Scenario *scenario = scenario_owner.get_or_null(p_scenario);
  2748. if (!scenario) {
  2749. return RID();
  2750. }
  2751. if (scene_render->is_environment(scenario->environment)) {
  2752. return scenario->environment;
  2753. }
  2754. if (scene_render->is_environment(scenario->fallback_environment)) {
  2755. return scenario->fallback_environment;
  2756. }
  2757. return RID();
  2758. }
  2759. void RendererSceneCull::render_empty_scene(const Ref<RenderSceneBuffers> &p_render_buffers, RID p_scenario, RID p_shadow_atlas) {
  2760. #ifndef _3D_DISABLED
  2761. Scenario *scenario = scenario_owner.get_or_null(p_scenario);
  2762. RID environment;
  2763. if (scenario->environment.is_valid()) {
  2764. environment = scenario->environment;
  2765. } else {
  2766. environment = scenario->fallback_environment;
  2767. }
  2768. RENDER_TIMESTAMP("Render Empty 3D Scene");
  2769. RendererSceneRender::CameraData camera_data;
  2770. camera_data.set_camera(Transform3D(), Projection(), true, false);
  2771. scene_render->render_scene(p_render_buffers, &camera_data, &camera_data, PagedArray<RenderGeometryInstance *>(), PagedArray<RID>(), PagedArray<RID>(), PagedArray<RID>(), PagedArray<RID>(), PagedArray<RID>(), PagedArray<RID>(), environment, RID(), p_shadow_atlas, RID(), scenario->reflection_atlas, RID(), 0, 0, nullptr, 0, nullptr, 0, nullptr);
  2772. #endif
  2773. }
  2774. bool RendererSceneCull::_render_reflection_probe_step(Instance *p_instance, int p_step) {
  2775. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(p_instance->base_data);
  2776. Scenario *scenario = p_instance->scenario;
  2777. ERR_FAIL_COND_V(!scenario, true);
  2778. RenderingServerDefault::redraw_request(); //update, so it updates in editor
  2779. if (p_step == 0) {
  2780. if (!RSG::light_storage->reflection_probe_instance_begin_render(reflection_probe->instance, scenario->reflection_atlas)) {
  2781. return true; //all full
  2782. }
  2783. }
  2784. if (p_step >= 0 && p_step < 6) {
  2785. static const Vector3 view_normals[6] = {
  2786. Vector3(+1, 0, 0),
  2787. Vector3(-1, 0, 0),
  2788. Vector3(0, +1, 0),
  2789. Vector3(0, -1, 0),
  2790. Vector3(0, 0, +1),
  2791. Vector3(0, 0, -1)
  2792. };
  2793. static const Vector3 view_up[6] = {
  2794. Vector3(0, -1, 0),
  2795. Vector3(0, -1, 0),
  2796. Vector3(0, 0, +1),
  2797. Vector3(0, 0, -1),
  2798. Vector3(0, -1, 0),
  2799. Vector3(0, -1, 0)
  2800. };
  2801. Vector3 probe_size = RSG::light_storage->reflection_probe_get_size(p_instance->base);
  2802. Vector3 origin_offset = RSG::light_storage->reflection_probe_get_origin_offset(p_instance->base);
  2803. float max_distance = RSG::light_storage->reflection_probe_get_origin_max_distance(p_instance->base);
  2804. float atlas_size = RSG::light_storage->reflection_atlas_get_size(scenario->reflection_atlas);
  2805. float mesh_lod_threshold = RSG::light_storage->reflection_probe_get_mesh_lod_threshold(p_instance->base) / atlas_size;
  2806. Vector3 edge = view_normals[p_step] * probe_size / 2;
  2807. float distance = ABS(view_normals[p_step].dot(edge) - view_normals[p_step].dot(origin_offset)); //distance from origin offset to actual view distance limit
  2808. max_distance = MAX(max_distance, distance);
  2809. //render cubemap side
  2810. Projection cm;
  2811. cm.set_perspective(90, 1, 0.01, max_distance);
  2812. Transform3D local_view;
  2813. local_view.set_look_at(origin_offset, origin_offset + view_normals[p_step], view_up[p_step]);
  2814. Transform3D xform = p_instance->transform * local_view;
  2815. RID shadow_atlas;
  2816. bool use_shadows = RSG::light_storage->reflection_probe_renders_shadows(p_instance->base);
  2817. if (use_shadows) {
  2818. shadow_atlas = scenario->reflection_probe_shadow_atlas;
  2819. }
  2820. RID environment;
  2821. if (scenario->environment.is_valid()) {
  2822. environment = scenario->environment;
  2823. } else {
  2824. environment = scenario->fallback_environment;
  2825. }
  2826. RENDER_TIMESTAMP("Render ReflectionProbe, Step " + itos(p_step));
  2827. RendererSceneRender::CameraData camera_data;
  2828. camera_data.set_camera(xform, cm, false, false);
  2829. Ref<RenderSceneBuffers> render_buffers = RSG::light_storage->reflection_probe_atlas_get_render_buffers(scenario->reflection_atlas);
  2830. _render_scene(&camera_data, render_buffers, environment, RID(), RSG::light_storage->reflection_probe_get_cull_mask(p_instance->base), p_instance->scenario->self, RID(), shadow_atlas, reflection_probe->instance, p_step, mesh_lod_threshold, use_shadows);
  2831. } else {
  2832. //do roughness postprocess step until it believes it's done
  2833. RENDER_TIMESTAMP("Post-Process ReflectionProbe, Step " + itos(p_step));
  2834. return RSG::light_storage->reflection_probe_instance_postprocess_step(reflection_probe->instance);
  2835. }
  2836. return false;
  2837. }
  2838. void RendererSceneCull::render_probes() {
  2839. /* REFLECTION PROBES */
  2840. SelfList<InstanceReflectionProbeData> *ref_probe = reflection_probe_render_list.first();
  2841. bool busy = false;
  2842. while (ref_probe) {
  2843. SelfList<InstanceReflectionProbeData> *next = ref_probe->next();
  2844. RID base = ref_probe->self()->owner->base;
  2845. switch (RSG::light_storage->reflection_probe_get_update_mode(base)) {
  2846. case RS::REFLECTION_PROBE_UPDATE_ONCE: {
  2847. if (busy) { //already rendering something
  2848. break;
  2849. }
  2850. bool done = _render_reflection_probe_step(ref_probe->self()->owner, ref_probe->self()->render_step);
  2851. if (done) {
  2852. reflection_probe_render_list.remove(ref_probe);
  2853. } else {
  2854. ref_probe->self()->render_step++;
  2855. }
  2856. busy = true; //do not render another one of this kind
  2857. } break;
  2858. case RS::REFLECTION_PROBE_UPDATE_ALWAYS: {
  2859. int step = 0;
  2860. bool done = false;
  2861. while (!done) {
  2862. done = _render_reflection_probe_step(ref_probe->self()->owner, step);
  2863. step++;
  2864. }
  2865. reflection_probe_render_list.remove(ref_probe);
  2866. } break;
  2867. }
  2868. ref_probe = next;
  2869. }
  2870. /* VOXEL GIS */
  2871. SelfList<InstanceVoxelGIData> *voxel_gi = voxel_gi_update_list.first();
  2872. if (voxel_gi) {
  2873. RENDER_TIMESTAMP("Render VoxelGI");
  2874. }
  2875. while (voxel_gi) {
  2876. SelfList<InstanceVoxelGIData> *next = voxel_gi->next();
  2877. InstanceVoxelGIData *probe = voxel_gi->self();
  2878. //Instance *instance_probe = probe->owner;
  2879. //check if probe must be setup, but don't do if on the lighting thread
  2880. bool cache_dirty = false;
  2881. int cache_count = 0;
  2882. {
  2883. int light_cache_size = probe->light_cache.size();
  2884. const InstanceVoxelGIData::LightCache *caches = probe->light_cache.ptr();
  2885. const RID *instance_caches = probe->light_instances.ptr();
  2886. int idx = 0; //must count visible lights
  2887. for (Instance *E : probe->lights) {
  2888. Instance *instance = E;
  2889. InstanceLightData *instance_light = (InstanceLightData *)instance->base_data;
  2890. if (!instance->visible) {
  2891. continue;
  2892. }
  2893. if (cache_dirty) {
  2894. //do nothing, since idx must count all visible lights anyway
  2895. } else if (idx >= light_cache_size) {
  2896. cache_dirty = true;
  2897. } else {
  2898. const InstanceVoxelGIData::LightCache *cache = &caches[idx];
  2899. if (
  2900. instance_caches[idx] != instance_light->instance ||
  2901. cache->has_shadow != RSG::light_storage->light_has_shadow(instance->base) ||
  2902. cache->type != RSG::light_storage->light_get_type(instance->base) ||
  2903. cache->transform != instance->transform ||
  2904. cache->color != RSG::light_storage->light_get_color(instance->base) ||
  2905. cache->energy != RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_ENERGY) ||
  2906. cache->intensity != RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_INTENSITY) ||
  2907. cache->bake_energy != RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_INDIRECT_ENERGY) ||
  2908. cache->radius != RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_RANGE) ||
  2909. cache->attenuation != RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_ATTENUATION) ||
  2910. cache->spot_angle != RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ANGLE) ||
  2911. cache->spot_attenuation != RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ATTENUATION)) {
  2912. cache_dirty = true;
  2913. }
  2914. }
  2915. idx++;
  2916. }
  2917. for (const Instance *instance : probe->owner->scenario->directional_lights) {
  2918. InstanceLightData *instance_light = (InstanceLightData *)instance->base_data;
  2919. if (!instance->visible) {
  2920. continue;
  2921. }
  2922. if (cache_dirty) {
  2923. //do nothing, since idx must count all visible lights anyway
  2924. } else if (idx >= light_cache_size) {
  2925. cache_dirty = true;
  2926. } else {
  2927. const InstanceVoxelGIData::LightCache *cache = &caches[idx];
  2928. if (
  2929. instance_caches[idx] != instance_light->instance ||
  2930. cache->has_shadow != RSG::light_storage->light_has_shadow(instance->base) ||
  2931. cache->type != RSG::light_storage->light_get_type(instance->base) ||
  2932. cache->transform != instance->transform ||
  2933. cache->color != RSG::light_storage->light_get_color(instance->base) ||
  2934. cache->energy != RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_ENERGY) ||
  2935. cache->intensity != RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_INTENSITY) ||
  2936. cache->bake_energy != RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_INDIRECT_ENERGY) ||
  2937. cache->radius != RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_RANGE) ||
  2938. cache->attenuation != RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_ATTENUATION) ||
  2939. cache->spot_angle != RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ANGLE) ||
  2940. cache->spot_attenuation != RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ATTENUATION) ||
  2941. cache->sky_mode != RSG::light_storage->light_directional_get_sky_mode(instance->base)) {
  2942. cache_dirty = true;
  2943. }
  2944. }
  2945. idx++;
  2946. }
  2947. if (idx != light_cache_size) {
  2948. cache_dirty = true;
  2949. }
  2950. cache_count = idx;
  2951. }
  2952. bool update_lights = scene_render->voxel_gi_needs_update(probe->probe_instance);
  2953. if (cache_dirty) {
  2954. probe->light_cache.resize(cache_count);
  2955. probe->light_instances.resize(cache_count);
  2956. if (cache_count) {
  2957. InstanceVoxelGIData::LightCache *caches = probe->light_cache.ptrw();
  2958. RID *instance_caches = probe->light_instances.ptrw();
  2959. int idx = 0; //must count visible lights
  2960. for (Instance *E : probe->lights) {
  2961. Instance *instance = E;
  2962. InstanceLightData *instance_light = (InstanceLightData *)instance->base_data;
  2963. if (!instance->visible) {
  2964. continue;
  2965. }
  2966. InstanceVoxelGIData::LightCache *cache = &caches[idx];
  2967. instance_caches[idx] = instance_light->instance;
  2968. cache->has_shadow = RSG::light_storage->light_has_shadow(instance->base);
  2969. cache->type = RSG::light_storage->light_get_type(instance->base);
  2970. cache->transform = instance->transform;
  2971. cache->color = RSG::light_storage->light_get_color(instance->base);
  2972. cache->energy = RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_ENERGY);
  2973. cache->intensity = RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_INTENSITY);
  2974. cache->bake_energy = RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_INDIRECT_ENERGY);
  2975. cache->radius = RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_RANGE);
  2976. cache->attenuation = RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_ATTENUATION);
  2977. cache->spot_angle = RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ANGLE);
  2978. cache->spot_attenuation = RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  2979. idx++;
  2980. }
  2981. for (const Instance *instance : probe->owner->scenario->directional_lights) {
  2982. InstanceLightData *instance_light = (InstanceLightData *)instance->base_data;
  2983. if (!instance->visible) {
  2984. continue;
  2985. }
  2986. InstanceVoxelGIData::LightCache *cache = &caches[idx];
  2987. instance_caches[idx] = instance_light->instance;
  2988. cache->has_shadow = RSG::light_storage->light_has_shadow(instance->base);
  2989. cache->type = RSG::light_storage->light_get_type(instance->base);
  2990. cache->transform = instance->transform;
  2991. cache->color = RSG::light_storage->light_get_color(instance->base);
  2992. cache->energy = RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_ENERGY);
  2993. cache->intensity = RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_INTENSITY);
  2994. cache->bake_energy = RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_INDIRECT_ENERGY);
  2995. cache->radius = RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_RANGE);
  2996. cache->attenuation = RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_ATTENUATION);
  2997. cache->spot_angle = RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ANGLE);
  2998. cache->spot_attenuation = RSG::light_storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  2999. cache->sky_mode = RSG::light_storage->light_directional_get_sky_mode(instance->base);
  3000. idx++;
  3001. }
  3002. }
  3003. update_lights = true;
  3004. }
  3005. scene_cull_result.geometry_instances.clear();
  3006. RID instance_pair_buffer[MAX_INSTANCE_PAIRS];
  3007. for (Instance *E : probe->dynamic_geometries) {
  3008. Instance *ins = E;
  3009. if (!ins->visible) {
  3010. continue;
  3011. }
  3012. InstanceGeometryData *geom = (InstanceGeometryData *)ins->base_data;
  3013. if (ins->scenario && ins->array_index >= 0 && (ins->scenario->instance_data[ins->array_index].flags & InstanceData::FLAG_GEOM_VOXEL_GI_DIRTY)) {
  3014. uint32_t idx = 0;
  3015. for (const Instance *F : geom->voxel_gi_instances) {
  3016. InstanceVoxelGIData *voxel_gi2 = static_cast<InstanceVoxelGIData *>(F->base_data);
  3017. instance_pair_buffer[idx++] = voxel_gi2->probe_instance;
  3018. if (idx == MAX_INSTANCE_PAIRS) {
  3019. break;
  3020. }
  3021. }
  3022. ERR_FAIL_NULL(geom->geometry_instance);
  3023. geom->geometry_instance->pair_voxel_gi_instances(instance_pair_buffer, idx);
  3024. ins->scenario->instance_data[ins->array_index].flags &= ~uint32_t(InstanceData::FLAG_GEOM_VOXEL_GI_DIRTY);
  3025. }
  3026. ERR_FAIL_NULL(geom->geometry_instance);
  3027. scene_cull_result.geometry_instances.push_back(geom->geometry_instance);
  3028. }
  3029. scene_render->voxel_gi_update(probe->probe_instance, update_lights, probe->light_instances, scene_cull_result.geometry_instances);
  3030. voxel_gi_update_list.remove(voxel_gi);
  3031. voxel_gi = next;
  3032. }
  3033. }
  3034. void RendererSceneCull::render_particle_colliders() {
  3035. while (heightfield_particle_colliders_update_list.begin()) {
  3036. Instance *hfpc = *heightfield_particle_colliders_update_list.begin();
  3037. if (hfpc->scenario && hfpc->base_type == RS::INSTANCE_PARTICLES_COLLISION && RSG::particles_storage->particles_collision_is_heightfield(hfpc->base)) {
  3038. //update heightfield
  3039. instance_cull_result.clear();
  3040. scene_cull_result.geometry_instances.clear();
  3041. struct CullAABB {
  3042. PagedArray<Instance *> *result;
  3043. _FORCE_INLINE_ bool operator()(void *p_data) {
  3044. Instance *p_instance = (Instance *)p_data;
  3045. result->push_back(p_instance);
  3046. return false;
  3047. }
  3048. };
  3049. CullAABB cull_aabb;
  3050. cull_aabb.result = &instance_cull_result;
  3051. hfpc->scenario->indexers[Scenario::INDEXER_GEOMETRY].aabb_query(hfpc->transformed_aabb, cull_aabb);
  3052. hfpc->scenario->indexers[Scenario::INDEXER_VOLUMES].aabb_query(hfpc->transformed_aabb, cull_aabb);
  3053. for (int i = 0; i < (int)instance_cull_result.size(); i++) {
  3054. Instance *instance = instance_cull_result[i];
  3055. if (!instance || !((1 << instance->base_type) & (RS::INSTANCE_GEOMETRY_MASK & (~(1 << RS::INSTANCE_PARTICLES))))) { //all but particles to avoid self collision
  3056. continue;
  3057. }
  3058. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(instance->base_data);
  3059. ERR_FAIL_NULL(geom->geometry_instance);
  3060. scene_cull_result.geometry_instances.push_back(geom->geometry_instance);
  3061. }
  3062. scene_render->render_particle_collider_heightfield(hfpc->base, hfpc->transform, scene_cull_result.geometry_instances);
  3063. }
  3064. heightfield_particle_colliders_update_list.remove(heightfield_particle_colliders_update_list.begin());
  3065. }
  3066. }
  3067. void RendererSceneCull::_update_instance_shader_uniforms_from_material(HashMap<StringName, Instance::InstanceShaderParameter> &isparams, const HashMap<StringName, Instance::InstanceShaderParameter> &existing_isparams, RID p_material) {
  3068. List<RendererMaterialStorage::InstanceShaderParam> plist;
  3069. RSG::material_storage->material_get_instance_shader_parameters(p_material, &plist);
  3070. for (const RendererMaterialStorage::InstanceShaderParam &E : plist) {
  3071. StringName name = E.info.name;
  3072. if (isparams.has(name)) {
  3073. if (isparams[name].info.type != E.info.type) {
  3074. WARN_PRINT("More than one material in instance export the same instance shader uniform '" + E.info.name + "', but they do it with different data types. Only the first one (in order) will display correctly.");
  3075. }
  3076. if (isparams[name].index != E.index) {
  3077. WARN_PRINT("More than one material in instance export the same instance shader uniform '" + E.info.name + "', but they do it with different indices. Only the first one (in order) will display correctly.");
  3078. }
  3079. continue; //first one found always has priority
  3080. }
  3081. Instance::InstanceShaderParameter isp;
  3082. isp.index = E.index;
  3083. isp.info = E.info;
  3084. isp.default_value = E.default_value;
  3085. if (existing_isparams.has(name)) {
  3086. isp.value = existing_isparams[name].value;
  3087. } else {
  3088. isp.value = E.default_value;
  3089. }
  3090. isparams[name] = isp;
  3091. }
  3092. }
  3093. void RendererSceneCull::_update_dirty_instance(Instance *p_instance) {
  3094. if (p_instance->update_aabb) {
  3095. _update_instance_aabb(p_instance);
  3096. }
  3097. if (p_instance->update_dependencies) {
  3098. p_instance->dependency_tracker.update_begin();
  3099. if (p_instance->base.is_valid()) {
  3100. RSG::utilities->base_update_dependency(p_instance->base, &p_instance->dependency_tracker);
  3101. }
  3102. if (p_instance->material_override.is_valid()) {
  3103. RSG::material_storage->material_update_dependency(p_instance->material_override, &p_instance->dependency_tracker);
  3104. }
  3105. if (p_instance->material_overlay.is_valid()) {
  3106. RSG::material_storage->material_update_dependency(p_instance->material_overlay, &p_instance->dependency_tracker);
  3107. }
  3108. if (p_instance->base_type == RS::INSTANCE_MESH) {
  3109. //remove materials no longer used and un-own them
  3110. int new_mat_count = RSG::mesh_storage->mesh_get_surface_count(p_instance->base);
  3111. p_instance->materials.resize(new_mat_count);
  3112. _instance_update_mesh_instance(p_instance);
  3113. }
  3114. if (p_instance->base_type == RS::INSTANCE_PARTICLES) {
  3115. // update the process material dependency
  3116. RID particle_material = RSG::particles_storage->particles_get_process_material(p_instance->base);
  3117. if (particle_material.is_valid()) {
  3118. RSG::material_storage->material_update_dependency(particle_material, &p_instance->dependency_tracker);
  3119. }
  3120. }
  3121. if ((1 << p_instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) {
  3122. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  3123. bool can_cast_shadows = true;
  3124. bool is_animated = false;
  3125. HashMap<StringName, Instance::InstanceShaderParameter> isparams;
  3126. if (p_instance->cast_shadows == RS::SHADOW_CASTING_SETTING_OFF) {
  3127. can_cast_shadows = false;
  3128. }
  3129. if (p_instance->material_override.is_valid()) {
  3130. if (!RSG::material_storage->material_casts_shadows(p_instance->material_override)) {
  3131. can_cast_shadows = false;
  3132. }
  3133. is_animated = RSG::material_storage->material_is_animated(p_instance->material_override);
  3134. _update_instance_shader_uniforms_from_material(isparams, p_instance->instance_shader_uniforms, p_instance->material_override);
  3135. } else {
  3136. if (p_instance->base_type == RS::INSTANCE_MESH) {
  3137. RID mesh = p_instance->base;
  3138. if (mesh.is_valid()) {
  3139. bool cast_shadows = false;
  3140. for (int i = 0; i < p_instance->materials.size(); i++) {
  3141. RID mat = p_instance->materials[i].is_valid() ? p_instance->materials[i] : RSG::mesh_storage->mesh_surface_get_material(mesh, i);
  3142. if (!mat.is_valid()) {
  3143. cast_shadows = true;
  3144. } else {
  3145. if (RSG::material_storage->material_casts_shadows(mat)) {
  3146. cast_shadows = true;
  3147. }
  3148. if (RSG::material_storage->material_is_animated(mat)) {
  3149. is_animated = true;
  3150. }
  3151. _update_instance_shader_uniforms_from_material(isparams, p_instance->instance_shader_uniforms, mat);
  3152. RSG::material_storage->material_update_dependency(mat, &p_instance->dependency_tracker);
  3153. }
  3154. }
  3155. if (!cast_shadows) {
  3156. can_cast_shadows = false;
  3157. }
  3158. }
  3159. } else if (p_instance->base_type == RS::INSTANCE_MULTIMESH) {
  3160. RID mesh = RSG::mesh_storage->multimesh_get_mesh(p_instance->base);
  3161. if (mesh.is_valid()) {
  3162. bool cast_shadows = false;
  3163. int sc = RSG::mesh_storage->mesh_get_surface_count(mesh);
  3164. for (int i = 0; i < sc; i++) {
  3165. RID mat = RSG::mesh_storage->mesh_surface_get_material(mesh, i);
  3166. if (!mat.is_valid()) {
  3167. cast_shadows = true;
  3168. } else {
  3169. if (RSG::material_storage->material_casts_shadows(mat)) {
  3170. cast_shadows = true;
  3171. }
  3172. if (RSG::material_storage->material_is_animated(mat)) {
  3173. is_animated = true;
  3174. }
  3175. _update_instance_shader_uniforms_from_material(isparams, p_instance->instance_shader_uniforms, mat);
  3176. RSG::material_storage->material_update_dependency(mat, &p_instance->dependency_tracker);
  3177. }
  3178. }
  3179. if (!cast_shadows) {
  3180. can_cast_shadows = false;
  3181. }
  3182. RSG::utilities->base_update_dependency(mesh, &p_instance->dependency_tracker);
  3183. }
  3184. } else if (p_instance->base_type == RS::INSTANCE_PARTICLES) {
  3185. bool cast_shadows = false;
  3186. int dp = RSG::particles_storage->particles_get_draw_passes(p_instance->base);
  3187. for (int i = 0; i < dp; i++) {
  3188. RID mesh = RSG::particles_storage->particles_get_draw_pass_mesh(p_instance->base, i);
  3189. if (!mesh.is_valid()) {
  3190. continue;
  3191. }
  3192. int sc = RSG::mesh_storage->mesh_get_surface_count(mesh);
  3193. for (int j = 0; j < sc; j++) {
  3194. RID mat = RSG::mesh_storage->mesh_surface_get_material(mesh, j);
  3195. if (!mat.is_valid()) {
  3196. cast_shadows = true;
  3197. } else {
  3198. if (RSG::material_storage->material_casts_shadows(mat)) {
  3199. cast_shadows = true;
  3200. }
  3201. if (RSG::material_storage->material_is_animated(mat)) {
  3202. is_animated = true;
  3203. }
  3204. _update_instance_shader_uniforms_from_material(isparams, p_instance->instance_shader_uniforms, mat);
  3205. RSG::material_storage->material_update_dependency(mat, &p_instance->dependency_tracker);
  3206. }
  3207. }
  3208. }
  3209. if (!cast_shadows) {
  3210. can_cast_shadows = false;
  3211. }
  3212. }
  3213. }
  3214. if (p_instance->material_overlay.is_valid()) {
  3215. can_cast_shadows = can_cast_shadows && RSG::material_storage->material_casts_shadows(p_instance->material_overlay);
  3216. is_animated = is_animated || RSG::material_storage->material_is_animated(p_instance->material_overlay);
  3217. _update_instance_shader_uniforms_from_material(isparams, p_instance->instance_shader_uniforms, p_instance->material_overlay);
  3218. }
  3219. if (can_cast_shadows != geom->can_cast_shadows) {
  3220. //ability to cast shadows change, let lights now
  3221. for (const Instance *E : geom->lights) {
  3222. InstanceLightData *light = static_cast<InstanceLightData *>(E->base_data);
  3223. light->shadow_dirty = true;
  3224. }
  3225. geom->can_cast_shadows = can_cast_shadows;
  3226. }
  3227. geom->material_is_animated = is_animated;
  3228. p_instance->instance_shader_uniforms = isparams;
  3229. if (p_instance->instance_allocated_shader_uniforms != (p_instance->instance_shader_uniforms.size() > 0)) {
  3230. p_instance->instance_allocated_shader_uniforms = (p_instance->instance_shader_uniforms.size() > 0);
  3231. if (p_instance->instance_allocated_shader_uniforms) {
  3232. p_instance->instance_allocated_shader_uniforms_offset = RSG::material_storage->global_shader_parameters_instance_allocate(p_instance->self);
  3233. ERR_FAIL_NULL(geom->geometry_instance);
  3234. geom->geometry_instance->set_instance_shader_uniforms_offset(p_instance->instance_allocated_shader_uniforms_offset);
  3235. for (const KeyValue<StringName, Instance::InstanceShaderParameter> &E : p_instance->instance_shader_uniforms) {
  3236. if (E.value.value.get_type() != Variant::NIL) {
  3237. int flags_count = 0;
  3238. if (E.value.info.hint == PROPERTY_HINT_FLAGS) {
  3239. // A small hack to detect boolean flags count and prevent overhead.
  3240. switch (E.value.info.hint_string.length()) {
  3241. case 3: // "x,y"
  3242. flags_count = 1;
  3243. break;
  3244. case 5: // "x,y,z"
  3245. flags_count = 2;
  3246. break;
  3247. case 7: // "x,y,z,w"
  3248. flags_count = 3;
  3249. break;
  3250. }
  3251. }
  3252. RSG::material_storage->global_shader_parameters_instance_update(p_instance->self, E.value.index, E.value.value, flags_count);
  3253. }
  3254. }
  3255. } else {
  3256. RSG::material_storage->global_shader_parameters_instance_free(p_instance->self);
  3257. p_instance->instance_allocated_shader_uniforms_offset = -1;
  3258. ERR_FAIL_NULL(geom->geometry_instance);
  3259. geom->geometry_instance->set_instance_shader_uniforms_offset(-1);
  3260. }
  3261. }
  3262. }
  3263. if (p_instance->skeleton.is_valid()) {
  3264. RSG::mesh_storage->skeleton_update_dependency(p_instance->skeleton, &p_instance->dependency_tracker);
  3265. }
  3266. p_instance->dependency_tracker.update_end();
  3267. if ((1 << p_instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) {
  3268. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  3269. ERR_FAIL_NULL(geom->geometry_instance);
  3270. geom->geometry_instance->set_surface_materials(p_instance->materials);
  3271. }
  3272. }
  3273. _instance_update_list.remove(&p_instance->update_item);
  3274. _update_instance(p_instance);
  3275. p_instance->update_aabb = false;
  3276. p_instance->update_dependencies = false;
  3277. }
  3278. void RendererSceneCull::update_dirty_instances() {
  3279. while (_instance_update_list.first()) {
  3280. _update_dirty_instance(_instance_update_list.first()->self());
  3281. }
  3282. // Update dirty resources after dirty instances as instance updates may affect resources.
  3283. RSG::utilities->update_dirty_resources();
  3284. }
  3285. void RendererSceneCull::update() {
  3286. //optimize bvhs
  3287. uint32_t rid_count = scenario_owner.get_rid_count();
  3288. RID *rids = (RID *)alloca(sizeof(RID) * rid_count);
  3289. scenario_owner.fill_owned_buffer(rids);
  3290. for (uint32_t i = 0; i < rid_count; i++) {
  3291. Scenario *s = scenario_owner.get_or_null(rids[i]);
  3292. s->indexers[Scenario::INDEXER_GEOMETRY].optimize_incremental(indexer_update_iterations);
  3293. s->indexers[Scenario::INDEXER_VOLUMES].optimize_incremental(indexer_update_iterations);
  3294. }
  3295. scene_render->update();
  3296. update_dirty_instances();
  3297. render_particle_colliders();
  3298. }
  3299. bool RendererSceneCull::free(RID p_rid) {
  3300. if (p_rid.is_null()) {
  3301. return true;
  3302. }
  3303. if (scene_render->free(p_rid)) {
  3304. return true;
  3305. }
  3306. if (camera_owner.owns(p_rid)) {
  3307. camera_owner.free(p_rid);
  3308. } else if (scenario_owner.owns(p_rid)) {
  3309. Scenario *scenario = scenario_owner.get_or_null(p_rid);
  3310. while (scenario->instances.first()) {
  3311. instance_set_scenario(scenario->instances.first()->self()->self, RID());
  3312. }
  3313. scenario->instance_aabbs.reset();
  3314. scenario->instance_data.reset();
  3315. scenario->instance_visibility.reset();
  3316. RSG::light_storage->shadow_atlas_free(scenario->reflection_probe_shadow_atlas);
  3317. RSG::light_storage->reflection_atlas_free(scenario->reflection_atlas);
  3318. scenario_owner.free(p_rid);
  3319. RendererSceneOcclusionCull::get_singleton()->remove_scenario(p_rid);
  3320. } else if (RendererSceneOcclusionCull::get_singleton()->is_occluder(p_rid)) {
  3321. RendererSceneOcclusionCull::get_singleton()->free_occluder(p_rid);
  3322. } else if (instance_owner.owns(p_rid)) {
  3323. // delete the instance
  3324. update_dirty_instances();
  3325. Instance *instance = instance_owner.get_or_null(p_rid);
  3326. instance_geometry_set_lightmap(p_rid, RID(), Rect2(), 0);
  3327. instance_set_scenario(p_rid, RID());
  3328. instance_set_base(p_rid, RID());
  3329. instance_geometry_set_material_override(p_rid, RID());
  3330. instance_geometry_set_material_overlay(p_rid, RID());
  3331. instance_attach_skeleton(p_rid, RID());
  3332. if (instance->instance_allocated_shader_uniforms) {
  3333. //free the used shader parameters
  3334. RSG::material_storage->global_shader_parameters_instance_free(instance->self);
  3335. }
  3336. update_dirty_instances(); //in case something changed this
  3337. instance_owner.free(p_rid);
  3338. } else {
  3339. return false;
  3340. }
  3341. return true;
  3342. }
  3343. TypedArray<Image> RendererSceneCull::bake_render_uv2(RID p_base, const TypedArray<RID> &p_material_overrides, const Size2i &p_image_size) {
  3344. return scene_render->bake_render_uv2(p_base, p_material_overrides, p_image_size);
  3345. }
  3346. void RendererSceneCull::update_visibility_notifiers() {
  3347. SelfList<InstanceVisibilityNotifierData> *E = visible_notifier_list.first();
  3348. while (E) {
  3349. SelfList<InstanceVisibilityNotifierData> *N = E->next();
  3350. InstanceVisibilityNotifierData *visibility_notifier = E->self();
  3351. if (visibility_notifier->just_visible) {
  3352. visibility_notifier->just_visible = false;
  3353. RSG::utilities->visibility_notifier_call(visibility_notifier->base, true, RSG::threaded);
  3354. } else {
  3355. if (visibility_notifier->visible_in_frame != RSG::rasterizer->get_frame_number()) {
  3356. visible_notifier_list.remove(E);
  3357. RSG::utilities->visibility_notifier_call(visibility_notifier->base, false, RSG::threaded);
  3358. }
  3359. }
  3360. E = N;
  3361. }
  3362. }
  3363. /*******************************/
  3364. /* Passthrough to Scene Render */
  3365. /*******************************/
  3366. /* ENVIRONMENT API */
  3367. RendererSceneCull *RendererSceneCull::singleton = nullptr;
  3368. void RendererSceneCull::set_scene_render(RendererSceneRender *p_scene_render) {
  3369. scene_render = p_scene_render;
  3370. geometry_instance_pair_mask = scene_render->geometry_instance_get_pair_mask();
  3371. }
  3372. RendererSceneCull::RendererSceneCull() {
  3373. render_pass = 1;
  3374. singleton = this;
  3375. instance_cull_result.set_page_pool(&instance_cull_page_pool);
  3376. instance_shadow_cull_result.set_page_pool(&instance_cull_page_pool);
  3377. for (uint32_t i = 0; i < MAX_UPDATE_SHADOWS; i++) {
  3378. render_shadow_data[i].instances.set_page_pool(&geometry_instance_cull_page_pool);
  3379. }
  3380. for (uint32_t i = 0; i < SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE; i++) {
  3381. render_sdfgi_data[i].instances.set_page_pool(&geometry_instance_cull_page_pool);
  3382. }
  3383. scene_cull_result.init(&rid_cull_page_pool, &geometry_instance_cull_page_pool, &instance_cull_page_pool);
  3384. scene_cull_result_threads.resize(WorkerThreadPool::get_singleton()->get_thread_count());
  3385. for (InstanceCullResult &thread : scene_cull_result_threads) {
  3386. thread.init(&rid_cull_page_pool, &geometry_instance_cull_page_pool, &instance_cull_page_pool);
  3387. }
  3388. indexer_update_iterations = GLOBAL_GET("rendering/limits/spatial_indexer/update_iterations_per_frame");
  3389. thread_cull_threshold = GLOBAL_GET("rendering/limits/spatial_indexer/threaded_cull_minimum_instances");
  3390. thread_cull_threshold = MAX(thread_cull_threshold, (uint32_t)WorkerThreadPool::get_singleton()->get_thread_count()); //make sure there is at least one thread per CPU
  3391. dummy_occlusion_culling = memnew(RendererSceneOcclusionCull);
  3392. }
  3393. RendererSceneCull::~RendererSceneCull() {
  3394. instance_cull_result.reset();
  3395. instance_shadow_cull_result.reset();
  3396. for (uint32_t i = 0; i < MAX_UPDATE_SHADOWS; i++) {
  3397. render_shadow_data[i].instances.reset();
  3398. }
  3399. for (uint32_t i = 0; i < SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE; i++) {
  3400. render_sdfgi_data[i].instances.reset();
  3401. }
  3402. scene_cull_result.reset();
  3403. for (InstanceCullResult &thread : scene_cull_result_threads) {
  3404. thread.reset();
  3405. }
  3406. scene_cull_result_threads.clear();
  3407. if (dummy_occlusion_culling) {
  3408. memdelete(dummy_occlusion_culling);
  3409. }
  3410. }