renderer_scene_cull.cpp 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125
  1. /*************************************************************************/
  2. /* renderer_scene_cull.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "renderer_scene_cull.h"
  31. #include "core/os/os.h"
  32. #include "rendering_server_default.h"
  33. #include "rendering_server_globals.h"
  34. #include <new>
  35. /* CAMERA API */
  36. RID RendererSceneCull::camera_create() {
  37. Camera *camera = memnew(Camera);
  38. return camera_owner.make_rid(camera);
  39. }
  40. void RendererSceneCull::camera_set_perspective(RID p_camera, float p_fovy_degrees, float p_z_near, float p_z_far) {
  41. Camera *camera = camera_owner.getornull(p_camera);
  42. ERR_FAIL_COND(!camera);
  43. camera->type = Camera::PERSPECTIVE;
  44. camera->fov = p_fovy_degrees;
  45. camera->znear = p_z_near;
  46. camera->zfar = p_z_far;
  47. }
  48. void RendererSceneCull::camera_set_orthogonal(RID p_camera, float p_size, float p_z_near, float p_z_far) {
  49. Camera *camera = camera_owner.getornull(p_camera);
  50. ERR_FAIL_COND(!camera);
  51. camera->type = Camera::ORTHOGONAL;
  52. camera->size = p_size;
  53. camera->znear = p_z_near;
  54. camera->zfar = p_z_far;
  55. }
  56. void RendererSceneCull::camera_set_frustum(RID p_camera, float p_size, Vector2 p_offset, float p_z_near, float p_z_far) {
  57. Camera *camera = camera_owner.getornull(p_camera);
  58. ERR_FAIL_COND(!camera);
  59. camera->type = Camera::FRUSTUM;
  60. camera->size = p_size;
  61. camera->offset = p_offset;
  62. camera->znear = p_z_near;
  63. camera->zfar = p_z_far;
  64. }
  65. void RendererSceneCull::camera_set_transform(RID p_camera, const Transform &p_transform) {
  66. Camera *camera = camera_owner.getornull(p_camera);
  67. ERR_FAIL_COND(!camera);
  68. camera->transform = p_transform.orthonormalized();
  69. }
  70. void RendererSceneCull::camera_set_cull_mask(RID p_camera, uint32_t p_layers) {
  71. Camera *camera = camera_owner.getornull(p_camera);
  72. ERR_FAIL_COND(!camera);
  73. camera->visible_layers = p_layers;
  74. }
  75. void RendererSceneCull::camera_set_environment(RID p_camera, RID p_env) {
  76. Camera *camera = camera_owner.getornull(p_camera);
  77. ERR_FAIL_COND(!camera);
  78. camera->env = p_env;
  79. }
  80. void RendererSceneCull::camera_set_camera_effects(RID p_camera, RID p_fx) {
  81. Camera *camera = camera_owner.getornull(p_camera);
  82. ERR_FAIL_COND(!camera);
  83. camera->effects = p_fx;
  84. }
  85. void RendererSceneCull::camera_set_use_vertical_aspect(RID p_camera, bool p_enable) {
  86. Camera *camera = camera_owner.getornull(p_camera);
  87. ERR_FAIL_COND(!camera);
  88. camera->vaspect = p_enable;
  89. }
  90. bool RendererSceneCull::is_camera(RID p_camera) const {
  91. return camera_owner.owns(p_camera);
  92. }
  93. /* SCENARIO API */
  94. void *RendererSceneCull::_instance_pair(void *p_self, OctreeElementID, Instance *p_A, int, OctreeElementID, Instance *p_B, int) {
  95. //RendererSceneCull *self = (RendererSceneCull*)p_self;
  96. Instance *A = p_A;
  97. Instance *B = p_B;
  98. //instance indices are designed so greater always contains lesser
  99. if (A->base_type > B->base_type) {
  100. SWAP(A, B); //lesser always first
  101. }
  102. if (B->base_type == RS::INSTANCE_LIGHT && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  103. InstanceLightData *light = static_cast<InstanceLightData *>(B->base_data);
  104. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  105. InstanceLightData::PairInfo pinfo;
  106. pinfo.geometry = A;
  107. pinfo.L = geom->lighting.push_back(B);
  108. List<InstanceLightData::PairInfo>::Element *E = light->geometries.push_back(pinfo);
  109. if (geom->can_cast_shadows) {
  110. light->shadow_dirty = true;
  111. }
  112. geom->lighting_dirty = true;
  113. return E; //this element should make freeing faster
  114. } else if (B->base_type == RS::INSTANCE_REFLECTION_PROBE && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  115. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(B->base_data);
  116. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  117. InstanceReflectionProbeData::PairInfo pinfo;
  118. pinfo.geometry = A;
  119. pinfo.L = geom->reflection_probes.push_back(B);
  120. List<InstanceReflectionProbeData::PairInfo>::Element *E = reflection_probe->geometries.push_back(pinfo);
  121. geom->reflection_dirty = true;
  122. return E; //this element should make freeing faster
  123. } else if (B->base_type == RS::INSTANCE_DECAL && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  124. InstanceDecalData *decal = static_cast<InstanceDecalData *>(B->base_data);
  125. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  126. InstanceDecalData::PairInfo pinfo;
  127. pinfo.geometry = A;
  128. pinfo.L = geom->decals.push_back(B);
  129. List<InstanceDecalData::PairInfo>::Element *E = decal->geometries.push_back(pinfo);
  130. geom->decal_dirty = true;
  131. return E; //this element should make freeing faster
  132. } else if (B->base_type == RS::INSTANCE_LIGHTMAP && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  133. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(B->base_data);
  134. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  135. if (A->dynamic_gi) {
  136. InstanceLightmapData::PairInfo pinfo;
  137. pinfo.geometry = A;
  138. pinfo.L = geom->lightmap_captures.push_back(B);
  139. List<InstanceLightmapData::PairInfo>::Element *E = lightmap_data->geometries.push_back(pinfo);
  140. ((RendererSceneCull *)p_self)->_instance_queue_update(A, false, false); //need to update capture
  141. return E; //this element should make freeing faster
  142. } else {
  143. return nullptr;
  144. }
  145. } else if (B->base_type == RS::INSTANCE_GI_PROBE && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  146. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(B->base_data);
  147. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  148. InstanceGIProbeData::PairInfo pinfo;
  149. pinfo.geometry = A;
  150. pinfo.L = geom->gi_probes.push_back(B);
  151. List<InstanceGIProbeData::PairInfo>::Element *E;
  152. if (A->dynamic_gi) {
  153. E = gi_probe->dynamic_geometries.push_back(pinfo);
  154. } else {
  155. E = gi_probe->geometries.push_back(pinfo);
  156. }
  157. geom->gi_probes_dirty = true;
  158. return E; //this element should make freeing faster
  159. } else if (B->base_type == RS::INSTANCE_GI_PROBE && A->base_type == RS::INSTANCE_LIGHT) {
  160. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(B->base_data);
  161. return gi_probe->lights.insert(A);
  162. } else if (B->base_type == RS::INSTANCE_PARTICLES_COLLISION && A->base_type == RS::INSTANCE_PARTICLES) {
  163. RSG::storage->particles_add_collision(A->base, B);
  164. }
  165. return nullptr;
  166. }
  167. void RendererSceneCull::_instance_unpair(void *p_self, OctreeElementID, Instance *p_A, int, OctreeElementID, Instance *p_B, int, void *udata) {
  168. //RendererSceneCull *self = (RendererSceneCull*)p_self;
  169. Instance *A = p_A;
  170. Instance *B = p_B;
  171. //instance indices are designed so greater always contains lesser
  172. if (A->base_type > B->base_type) {
  173. SWAP(A, B); //lesser always first
  174. }
  175. if (B->base_type == RS::INSTANCE_LIGHT && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  176. InstanceLightData *light = static_cast<InstanceLightData *>(B->base_data);
  177. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  178. List<InstanceLightData::PairInfo>::Element *E = reinterpret_cast<List<InstanceLightData::PairInfo>::Element *>(udata);
  179. geom->lighting.erase(E->get().L);
  180. light->geometries.erase(E);
  181. if (geom->can_cast_shadows) {
  182. light->shadow_dirty = true;
  183. }
  184. geom->lighting_dirty = true;
  185. } else if (B->base_type == RS::INSTANCE_REFLECTION_PROBE && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  186. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(B->base_data);
  187. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  188. List<InstanceReflectionProbeData::PairInfo>::Element *E = reinterpret_cast<List<InstanceReflectionProbeData::PairInfo>::Element *>(udata);
  189. geom->reflection_probes.erase(E->get().L);
  190. reflection_probe->geometries.erase(E);
  191. geom->reflection_dirty = true;
  192. } else if (B->base_type == RS::INSTANCE_DECAL && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  193. InstanceDecalData *decal = static_cast<InstanceDecalData *>(B->base_data);
  194. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  195. List<InstanceDecalData::PairInfo>::Element *E = reinterpret_cast<List<InstanceDecalData::PairInfo>::Element *>(udata);
  196. geom->decals.erase(E->get().L);
  197. decal->geometries.erase(E);
  198. geom->decal_dirty = true;
  199. } else if (B->base_type == RS::INSTANCE_LIGHTMAP && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  200. if (udata) { //only for dynamic geometries
  201. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(B->base_data);
  202. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  203. List<InstanceLightmapData::PairInfo>::Element *E = reinterpret_cast<List<InstanceLightmapData::PairInfo>::Element *>(udata);
  204. geom->lightmap_captures.erase(E->get().L);
  205. lightmap_data->geometries.erase(E);
  206. ((RendererSceneCull *)p_self)->_instance_queue_update(A, false, false); //need to update capture
  207. }
  208. } else if (B->base_type == RS::INSTANCE_GI_PROBE && ((1 << A->base_type) & RS::INSTANCE_GEOMETRY_MASK)) {
  209. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(B->base_data);
  210. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(A->base_data);
  211. List<InstanceGIProbeData::PairInfo>::Element *E = reinterpret_cast<List<InstanceGIProbeData::PairInfo>::Element *>(udata);
  212. geom->gi_probes.erase(E->get().L);
  213. if (A->dynamic_gi) {
  214. gi_probe->dynamic_geometries.erase(E);
  215. } else {
  216. gi_probe->geometries.erase(E);
  217. }
  218. geom->gi_probes_dirty = true;
  219. } else if (B->base_type == RS::INSTANCE_GI_PROBE && A->base_type == RS::INSTANCE_LIGHT) {
  220. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(B->base_data);
  221. Set<Instance *>::Element *E = reinterpret_cast<Set<Instance *>::Element *>(udata);
  222. gi_probe->lights.erase(E);
  223. } else if (B->base_type == RS::INSTANCE_PARTICLES_COLLISION && A->base_type == RS::INSTANCE_PARTICLES) {
  224. RSG::storage->particles_remove_collision(A->base, B);
  225. }
  226. }
  227. RID RendererSceneCull::scenario_create() {
  228. Scenario *scenario = memnew(Scenario);
  229. ERR_FAIL_COND_V(!scenario, RID());
  230. RID scenario_rid = scenario_owner.make_rid(scenario);
  231. scenario->self = scenario_rid;
  232. scenario->octree.set_pair_callback(_instance_pair, this);
  233. scenario->octree.set_unpair_callback(_instance_unpair, this);
  234. scenario->reflection_probe_shadow_atlas = scene_render->shadow_atlas_create();
  235. scene_render->shadow_atlas_set_size(scenario->reflection_probe_shadow_atlas, 1024); //make enough shadows for close distance, don't bother with rest
  236. scene_render->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas, 0, 4);
  237. scene_render->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas, 1, 4);
  238. scene_render->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas, 2, 4);
  239. scene_render->shadow_atlas_set_quadrant_subdivision(scenario->reflection_probe_shadow_atlas, 3, 8);
  240. scenario->reflection_atlas = scene_render->reflection_atlas_create();
  241. return scenario_rid;
  242. }
  243. void RendererSceneCull::scenario_set_debug(RID p_scenario, RS::ScenarioDebugMode p_debug_mode) {
  244. Scenario *scenario = scenario_owner.getornull(p_scenario);
  245. ERR_FAIL_COND(!scenario);
  246. scenario->debug = p_debug_mode;
  247. }
  248. void RendererSceneCull::scenario_set_environment(RID p_scenario, RID p_environment) {
  249. Scenario *scenario = scenario_owner.getornull(p_scenario);
  250. ERR_FAIL_COND(!scenario);
  251. scenario->environment = p_environment;
  252. }
  253. void RendererSceneCull::scenario_set_camera_effects(RID p_scenario, RID p_camera_effects) {
  254. Scenario *scenario = scenario_owner.getornull(p_scenario);
  255. ERR_FAIL_COND(!scenario);
  256. scenario->camera_effects = p_camera_effects;
  257. }
  258. void RendererSceneCull::scenario_set_fallback_environment(RID p_scenario, RID p_environment) {
  259. Scenario *scenario = scenario_owner.getornull(p_scenario);
  260. ERR_FAIL_COND(!scenario);
  261. scenario->fallback_environment = p_environment;
  262. }
  263. void RendererSceneCull::scenario_set_reflection_atlas_size(RID p_scenario, int p_reflection_size, int p_reflection_count) {
  264. Scenario *scenario = scenario_owner.getornull(p_scenario);
  265. ERR_FAIL_COND(!scenario);
  266. scene_render->reflection_atlas_set_size(scenario->reflection_atlas, p_reflection_size, p_reflection_count);
  267. }
  268. bool RendererSceneCull::is_scenario(RID p_scenario) const {
  269. return scenario_owner.owns(p_scenario);
  270. }
  271. RID RendererSceneCull::scenario_get_environment(RID p_scenario) {
  272. Scenario *scenario = scenario_owner.getornull(p_scenario);
  273. ERR_FAIL_COND_V(!scenario, RID());
  274. return scenario->environment;
  275. }
  276. /* INSTANCING API */
  277. void RendererSceneCull::_instance_queue_update(Instance *p_instance, bool p_update_aabb, bool p_update_dependencies) {
  278. if (p_update_aabb) {
  279. p_instance->update_aabb = true;
  280. }
  281. if (p_update_dependencies) {
  282. p_instance->update_dependencies = true;
  283. }
  284. if (p_instance->update_item.in_list()) {
  285. return;
  286. }
  287. _instance_update_list.add(&p_instance->update_item);
  288. }
  289. RID RendererSceneCull::instance_create() {
  290. Instance *instance = memnew(Instance);
  291. ERR_FAIL_COND_V(!instance, RID());
  292. RID instance_rid = instance_owner.make_rid(instance);
  293. instance->self = instance_rid;
  294. return instance_rid;
  295. }
  296. void RendererSceneCull::_instance_update_mesh_instance(Instance *p_instance) {
  297. bool needs_instance = RSG::storage->mesh_needs_instance(p_instance->base, p_instance->skeleton.is_valid());
  298. if (needs_instance != p_instance->mesh_instance.is_valid()) {
  299. if (needs_instance) {
  300. p_instance->mesh_instance = RSG::storage->mesh_instance_create(p_instance->base);
  301. } else {
  302. RSG::storage->free(p_instance->mesh_instance);
  303. p_instance->mesh_instance = RID();
  304. }
  305. }
  306. if (p_instance->mesh_instance.is_valid()) {
  307. RSG::storage->mesh_instance_set_skeleton(p_instance->mesh_instance, p_instance->skeleton);
  308. }
  309. }
  310. void RendererSceneCull::instance_set_base(RID p_instance, RID p_base) {
  311. Instance *instance = instance_owner.getornull(p_instance);
  312. ERR_FAIL_COND(!instance);
  313. Scenario *scenario = instance->scenario;
  314. if (instance->base_type != RS::INSTANCE_NONE) {
  315. //free anything related to that base
  316. if (scenario && instance->octree_id) {
  317. scenario->octree.erase(instance->octree_id); //make dependencies generated by the octree go away
  318. instance->octree_id = 0;
  319. }
  320. if (instance->mesh_instance.is_valid()) {
  321. RSG::storage->free(instance->mesh_instance);
  322. instance->mesh_instance = RID();
  323. }
  324. switch (instance->base_type) {
  325. case RS::INSTANCE_LIGHT: {
  326. InstanceLightData *light = static_cast<InstanceLightData *>(instance->base_data);
  327. if (scenario && RSG::storage->light_get_type(instance->base) != RS::LIGHT_DIRECTIONAL && light->bake_mode == RS::LIGHT_BAKE_DYNAMIC) {
  328. scenario->dynamic_lights.erase(light->instance);
  329. }
  330. #ifdef DEBUG_ENABLED
  331. if (light->geometries.size()) {
  332. ERR_PRINT("BUG, indexing did not unpair geometries from light.");
  333. }
  334. #endif
  335. if (scenario && light->D) {
  336. scenario->directional_lights.erase(light->D);
  337. light->D = nullptr;
  338. }
  339. scene_render->free(light->instance);
  340. } break;
  341. case RS::INSTANCE_REFLECTION_PROBE: {
  342. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(instance->base_data);
  343. scene_render->free(reflection_probe->instance);
  344. if (reflection_probe->update_list.in_list()) {
  345. reflection_probe_render_list.remove(&reflection_probe->update_list);
  346. }
  347. } break;
  348. case RS::INSTANCE_DECAL: {
  349. InstanceDecalData *decal = static_cast<InstanceDecalData *>(instance->base_data);
  350. scene_render->free(decal->instance);
  351. } break;
  352. case RS::INSTANCE_LIGHTMAP: {
  353. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(instance->base_data);
  354. //erase dependencies, since no longer a lightmap
  355. while (lightmap_data->users.front()) {
  356. instance_geometry_set_lightmap(lightmap_data->users.front()->get()->self, RID(), Rect2(), 0);
  357. }
  358. } break;
  359. case RS::INSTANCE_GI_PROBE: {
  360. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(instance->base_data);
  361. #ifdef DEBUG_ENABLED
  362. if (gi_probe->geometries.size()) {
  363. ERR_PRINT("BUG, indexing did not unpair geometries from GIProbe.");
  364. }
  365. #endif
  366. #ifdef DEBUG_ENABLED
  367. if (gi_probe->lights.size()) {
  368. ERR_PRINT("BUG, indexing did not unpair lights from GIProbe.");
  369. }
  370. #endif
  371. if (gi_probe->update_element.in_list()) {
  372. gi_probe_update_list.remove(&gi_probe->update_element);
  373. }
  374. scene_render->free(gi_probe->probe_instance);
  375. } break;
  376. default: {
  377. }
  378. }
  379. if (instance->base_data) {
  380. memdelete(instance->base_data);
  381. instance->base_data = nullptr;
  382. }
  383. instance->materials.clear();
  384. }
  385. instance->base_type = RS::INSTANCE_NONE;
  386. instance->base = RID();
  387. if (p_base.is_valid()) {
  388. instance->base_type = RSG::storage->get_base_type(p_base);
  389. ERR_FAIL_COND(instance->base_type == RS::INSTANCE_NONE);
  390. switch (instance->base_type) {
  391. case RS::INSTANCE_LIGHT: {
  392. InstanceLightData *light = memnew(InstanceLightData);
  393. if (scenario && RSG::storage->light_get_type(p_base) == RS::LIGHT_DIRECTIONAL) {
  394. light->D = scenario->directional_lights.push_back(instance);
  395. }
  396. light->instance = scene_render->light_instance_create(p_base);
  397. instance->base_data = light;
  398. } break;
  399. case RS::INSTANCE_MESH:
  400. case RS::INSTANCE_MULTIMESH:
  401. case RS::INSTANCE_IMMEDIATE:
  402. case RS::INSTANCE_PARTICLES: {
  403. InstanceGeometryData *geom = memnew(InstanceGeometryData);
  404. instance->base_data = geom;
  405. } break;
  406. case RS::INSTANCE_REFLECTION_PROBE: {
  407. InstanceReflectionProbeData *reflection_probe = memnew(InstanceReflectionProbeData);
  408. reflection_probe->owner = instance;
  409. instance->base_data = reflection_probe;
  410. reflection_probe->instance = scene_render->reflection_probe_instance_create(p_base);
  411. } break;
  412. case RS::INSTANCE_DECAL: {
  413. InstanceDecalData *decal = memnew(InstanceDecalData);
  414. decal->owner = instance;
  415. instance->base_data = decal;
  416. decal->instance = scene_render->decal_instance_create(p_base);
  417. } break;
  418. case RS::INSTANCE_LIGHTMAP: {
  419. InstanceLightmapData *lightmap_data = memnew(InstanceLightmapData);
  420. instance->base_data = lightmap_data;
  421. //lightmap_data->instance = scene_render->lightmap_data_instance_create(p_base);
  422. } break;
  423. case RS::INSTANCE_GI_PROBE: {
  424. InstanceGIProbeData *gi_probe = memnew(InstanceGIProbeData);
  425. instance->base_data = gi_probe;
  426. gi_probe->owner = instance;
  427. if (scenario && !gi_probe->update_element.in_list()) {
  428. gi_probe_update_list.add(&gi_probe->update_element);
  429. }
  430. gi_probe->probe_instance = scene_render->gi_probe_instance_create(p_base);
  431. } break;
  432. default: {
  433. }
  434. }
  435. instance->base = p_base;
  436. if (instance->base_type == RS::INSTANCE_MESH) {
  437. _instance_update_mesh_instance(instance);
  438. }
  439. //forcefully update the dependency now, so if for some reason it gets removed, we can immediately clear it
  440. RSG::storage->base_update_dependency(p_base, instance);
  441. }
  442. _instance_queue_update(instance, true, true);
  443. }
  444. void RendererSceneCull::instance_set_scenario(RID p_instance, RID p_scenario) {
  445. Instance *instance = instance_owner.getornull(p_instance);
  446. ERR_FAIL_COND(!instance);
  447. if (instance->scenario) {
  448. instance->scenario->instances.remove(&instance->scenario_item);
  449. if (instance->octree_id) {
  450. instance->scenario->octree.erase(instance->octree_id); //make dependencies generated by the octree go away
  451. instance->octree_id = 0;
  452. }
  453. switch (instance->base_type) {
  454. case RS::INSTANCE_LIGHT: {
  455. InstanceLightData *light = static_cast<InstanceLightData *>(instance->base_data);
  456. #ifdef DEBUG_ENABLED
  457. if (light->geometries.size()) {
  458. ERR_PRINT("BUG, indexing did not unpair geometries from light.");
  459. }
  460. #endif
  461. if (light->D) {
  462. instance->scenario->directional_lights.erase(light->D);
  463. light->D = nullptr;
  464. }
  465. } break;
  466. case RS::INSTANCE_REFLECTION_PROBE: {
  467. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(instance->base_data);
  468. scene_render->reflection_probe_release_atlas_index(reflection_probe->instance);
  469. } break;
  470. case RS::INSTANCE_PARTICLES_COLLISION: {
  471. heightfield_particle_colliders_update_list.erase(instance);
  472. } break;
  473. case RS::INSTANCE_GI_PROBE: {
  474. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(instance->base_data);
  475. #ifdef DEBUG_ENABLED
  476. if (gi_probe->geometries.size()) {
  477. ERR_PRINT("BUG, indexing did not unpair geometries from GIProbe.");
  478. }
  479. #endif
  480. #ifdef DEBUG_ENABLED
  481. if (gi_probe->lights.size()) {
  482. ERR_PRINT("BUG, indexing did not unpair lights from GIProbe.");
  483. }
  484. #endif
  485. if (gi_probe->update_element.in_list()) {
  486. gi_probe_update_list.remove(&gi_probe->update_element);
  487. }
  488. } break;
  489. default: {
  490. }
  491. }
  492. instance->scenario = nullptr;
  493. }
  494. if (p_scenario.is_valid()) {
  495. Scenario *scenario = scenario_owner.getornull(p_scenario);
  496. ERR_FAIL_COND(!scenario);
  497. instance->scenario = scenario;
  498. scenario->instances.add(&instance->scenario_item);
  499. switch (instance->base_type) {
  500. case RS::INSTANCE_LIGHT: {
  501. InstanceLightData *light = static_cast<InstanceLightData *>(instance->base_data);
  502. if (RSG::storage->light_get_type(instance->base) == RS::LIGHT_DIRECTIONAL) {
  503. light->D = scenario->directional_lights.push_back(instance);
  504. }
  505. } break;
  506. case RS::INSTANCE_GI_PROBE: {
  507. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(instance->base_data);
  508. if (!gi_probe->update_element.in_list()) {
  509. gi_probe_update_list.add(&gi_probe->update_element);
  510. }
  511. } break;
  512. default: {
  513. }
  514. }
  515. _instance_queue_update(instance, true, true);
  516. }
  517. }
  518. void RendererSceneCull::instance_set_layer_mask(RID p_instance, uint32_t p_mask) {
  519. Instance *instance = instance_owner.getornull(p_instance);
  520. ERR_FAIL_COND(!instance);
  521. instance->layer_mask = p_mask;
  522. }
  523. void RendererSceneCull::instance_set_transform(RID p_instance, const Transform &p_transform) {
  524. Instance *instance = instance_owner.getornull(p_instance);
  525. ERR_FAIL_COND(!instance);
  526. if (instance->transform == p_transform) {
  527. return; //must be checked to avoid worst evil
  528. }
  529. #ifdef DEBUG_ENABLED
  530. for (int i = 0; i < 4; i++) {
  531. const Vector3 &v = i < 3 ? p_transform.basis.elements[i] : p_transform.origin;
  532. ERR_FAIL_COND(Math::is_inf(v.x));
  533. ERR_FAIL_COND(Math::is_nan(v.x));
  534. ERR_FAIL_COND(Math::is_inf(v.y));
  535. ERR_FAIL_COND(Math::is_nan(v.y));
  536. ERR_FAIL_COND(Math::is_inf(v.z));
  537. ERR_FAIL_COND(Math::is_nan(v.z));
  538. }
  539. #endif
  540. instance->transform = p_transform;
  541. _instance_queue_update(instance, true);
  542. }
  543. void RendererSceneCull::instance_attach_object_instance_id(RID p_instance, ObjectID p_id) {
  544. Instance *instance = instance_owner.getornull(p_instance);
  545. ERR_FAIL_COND(!instance);
  546. instance->object_id = p_id;
  547. }
  548. void RendererSceneCull::instance_set_blend_shape_weight(RID p_instance, int p_shape, float p_weight) {
  549. Instance *instance = instance_owner.getornull(p_instance);
  550. ERR_FAIL_COND(!instance);
  551. if (instance->update_item.in_list()) {
  552. _update_dirty_instance(instance);
  553. }
  554. if (instance->mesh_instance.is_valid()) {
  555. RSG::storage->mesh_instance_set_blend_shape_weight(instance->mesh_instance, p_shape, p_weight);
  556. }
  557. }
  558. void RendererSceneCull::instance_set_surface_material(RID p_instance, int p_surface, RID p_material) {
  559. Instance *instance = instance_owner.getornull(p_instance);
  560. ERR_FAIL_COND(!instance);
  561. if (instance->base_type == RS::INSTANCE_MESH) {
  562. //may not have been updated yet, may also have not been set yet. When updated will be correcte, worst case
  563. instance->materials.resize(MAX(p_surface + 1, RSG::storage->mesh_get_surface_count(instance->base)));
  564. }
  565. ERR_FAIL_INDEX(p_surface, instance->materials.size());
  566. instance->materials.write[p_surface] = p_material;
  567. _instance_queue_update(instance, false, true);
  568. }
  569. void RendererSceneCull::instance_set_visible(RID p_instance, bool p_visible) {
  570. Instance *instance = instance_owner.getornull(p_instance);
  571. ERR_FAIL_COND(!instance);
  572. if (instance->visible == p_visible) {
  573. return;
  574. }
  575. instance->visible = p_visible;
  576. switch (instance->base_type) {
  577. case RS::INSTANCE_LIGHT: {
  578. if (RSG::storage->light_get_type(instance->base) != RS::LIGHT_DIRECTIONAL && instance->octree_id && instance->scenario) {
  579. instance->scenario->octree.set_pairable(instance->octree_id, p_visible, 1 << RS::INSTANCE_LIGHT, p_visible ? RS::INSTANCE_GEOMETRY_MASK : 0);
  580. }
  581. } break;
  582. case RS::INSTANCE_REFLECTION_PROBE: {
  583. if (instance->octree_id && instance->scenario) {
  584. instance->scenario->octree.set_pairable(instance->octree_id, p_visible, 1 << RS::INSTANCE_REFLECTION_PROBE, p_visible ? RS::INSTANCE_GEOMETRY_MASK : 0);
  585. }
  586. } break;
  587. case RS::INSTANCE_DECAL: {
  588. if (instance->octree_id && instance->scenario) {
  589. instance->scenario->octree.set_pairable(instance->octree_id, p_visible, 1 << RS::INSTANCE_DECAL, p_visible ? RS::INSTANCE_GEOMETRY_MASK : 0);
  590. }
  591. } break;
  592. case RS::INSTANCE_LIGHTMAP: {
  593. if (instance->octree_id && instance->scenario) {
  594. instance->scenario->octree.set_pairable(instance->octree_id, p_visible, 1 << RS::INSTANCE_LIGHTMAP, p_visible ? RS::INSTANCE_GEOMETRY_MASK : 0);
  595. }
  596. } break;
  597. case RS::INSTANCE_GI_PROBE: {
  598. if (instance->octree_id && instance->scenario) {
  599. instance->scenario->octree.set_pairable(instance->octree_id, p_visible, 1 << RS::INSTANCE_GI_PROBE, p_visible ? (RS::INSTANCE_GEOMETRY_MASK | (1 << RS::INSTANCE_LIGHT)) : 0);
  600. }
  601. } break;
  602. case RS::INSTANCE_PARTICLES_COLLISION: {
  603. if (instance->octree_id && instance->scenario) {
  604. instance->scenario->octree.set_pairable(instance->octree_id, p_visible, 1 << RS::INSTANCE_PARTICLES_COLLISION, p_visible ? (1 << RS::INSTANCE_PARTICLES) : 0);
  605. }
  606. } break;
  607. default: {
  608. }
  609. }
  610. }
  611. inline bool is_geometry_instance(RenderingServer::InstanceType p_type) {
  612. return p_type == RS::INSTANCE_MESH || p_type == RS::INSTANCE_MULTIMESH || p_type == RS::INSTANCE_PARTICLES || p_type == RS::INSTANCE_IMMEDIATE;
  613. }
  614. void RendererSceneCull::instance_set_custom_aabb(RID p_instance, AABB p_aabb) {
  615. Instance *instance = instance_owner.getornull(p_instance);
  616. ERR_FAIL_COND(!instance);
  617. ERR_FAIL_COND(!is_geometry_instance(instance->base_type));
  618. if (p_aabb != AABB()) {
  619. // Set custom AABB
  620. if (instance->custom_aabb == nullptr) {
  621. instance->custom_aabb = memnew(AABB);
  622. }
  623. *instance->custom_aabb = p_aabb;
  624. } else {
  625. // Clear custom AABB
  626. if (instance->custom_aabb != nullptr) {
  627. memdelete(instance->custom_aabb);
  628. instance->custom_aabb = nullptr;
  629. }
  630. }
  631. if (instance->scenario) {
  632. _instance_queue_update(instance, true, false);
  633. }
  634. }
  635. void RendererSceneCull::instance_attach_skeleton(RID p_instance, RID p_skeleton) {
  636. Instance *instance = instance_owner.getornull(p_instance);
  637. ERR_FAIL_COND(!instance);
  638. if (instance->skeleton == p_skeleton) {
  639. return;
  640. }
  641. instance->skeleton = p_skeleton;
  642. if (p_skeleton.is_valid()) {
  643. //update the dependency now, so if cleared, we remove it
  644. RSG::storage->skeleton_update_dependency(p_skeleton, instance);
  645. }
  646. _instance_update_mesh_instance(instance);
  647. _instance_queue_update(instance, true, true);
  648. }
  649. void RendererSceneCull::instance_set_exterior(RID p_instance, bool p_enabled) {
  650. }
  651. void RendererSceneCull::instance_set_extra_visibility_margin(RID p_instance, real_t p_margin) {
  652. Instance *instance = instance_owner.getornull(p_instance);
  653. ERR_FAIL_COND(!instance);
  654. instance->extra_margin = p_margin;
  655. _instance_queue_update(instance, true, false);
  656. }
  657. Vector<ObjectID> RendererSceneCull::instances_cull_aabb(const AABB &p_aabb, RID p_scenario) const {
  658. Vector<ObjectID> instances;
  659. Scenario *scenario = scenario_owner.getornull(p_scenario);
  660. ERR_FAIL_COND_V(!scenario, instances);
  661. const_cast<RendererSceneCull *>(this)->update_dirty_instances(); // check dirty instances before culling
  662. int culled = 0;
  663. Instance *cull[1024];
  664. culled = scenario->octree.cull_aabb(p_aabb, cull, 1024);
  665. for (int i = 0; i < culled; i++) {
  666. Instance *instance = cull[i];
  667. ERR_CONTINUE(!instance);
  668. if (instance->object_id.is_null()) {
  669. continue;
  670. }
  671. instances.push_back(instance->object_id);
  672. }
  673. return instances;
  674. }
  675. Vector<ObjectID> RendererSceneCull::instances_cull_ray(const Vector3 &p_from, const Vector3 &p_to, RID p_scenario) const {
  676. Vector<ObjectID> instances;
  677. Scenario *scenario = scenario_owner.getornull(p_scenario);
  678. ERR_FAIL_COND_V(!scenario, instances);
  679. const_cast<RendererSceneCull *>(this)->update_dirty_instances(); // check dirty instances before culling
  680. int culled = 0;
  681. Instance *cull[1024];
  682. culled = scenario->octree.cull_segment(p_from, p_from + p_to * 10000, cull, 1024);
  683. for (int i = 0; i < culled; i++) {
  684. Instance *instance = cull[i];
  685. ERR_CONTINUE(!instance);
  686. if (instance->object_id.is_null()) {
  687. continue;
  688. }
  689. instances.push_back(instance->object_id);
  690. }
  691. return instances;
  692. }
  693. Vector<ObjectID> RendererSceneCull::instances_cull_convex(const Vector<Plane> &p_convex, RID p_scenario) const {
  694. Vector<ObjectID> instances;
  695. Scenario *scenario = scenario_owner.getornull(p_scenario);
  696. ERR_FAIL_COND_V(!scenario, instances);
  697. const_cast<RendererSceneCull *>(this)->update_dirty_instances(); // check dirty instances before culling
  698. int culled = 0;
  699. Instance *cull[1024];
  700. culled = scenario->octree.cull_convex(p_convex, cull, 1024);
  701. for (int i = 0; i < culled; i++) {
  702. Instance *instance = cull[i];
  703. ERR_CONTINUE(!instance);
  704. if (instance->object_id.is_null()) {
  705. continue;
  706. }
  707. instances.push_back(instance->object_id);
  708. }
  709. return instances;
  710. }
  711. void RendererSceneCull::instance_geometry_set_flag(RID p_instance, RS::InstanceFlags p_flags, bool p_enabled) {
  712. Instance *instance = instance_owner.getornull(p_instance);
  713. ERR_FAIL_COND(!instance);
  714. //ERR_FAIL_COND(((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK));
  715. switch (p_flags) {
  716. case RS::INSTANCE_FLAG_USE_BAKED_LIGHT: {
  717. instance->baked_light = p_enabled;
  718. } break;
  719. case RS::INSTANCE_FLAG_USE_DYNAMIC_GI: {
  720. if (p_enabled == instance->dynamic_gi) {
  721. //bye, redundant
  722. return;
  723. }
  724. if (instance->octree_id != 0) {
  725. //remove from octree, it needs to be re-paired
  726. instance->scenario->octree.erase(instance->octree_id);
  727. instance->octree_id = 0;
  728. _instance_queue_update(instance, true, true);
  729. }
  730. //once out of octree, can be changed
  731. instance->dynamic_gi = p_enabled;
  732. } break;
  733. case RS::INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE: {
  734. instance->redraw_if_visible = p_enabled;
  735. } break;
  736. default: {
  737. }
  738. }
  739. }
  740. void RendererSceneCull::instance_geometry_set_cast_shadows_setting(RID p_instance, RS::ShadowCastingSetting p_shadow_casting_setting) {
  741. Instance *instance = instance_owner.getornull(p_instance);
  742. ERR_FAIL_COND(!instance);
  743. instance->cast_shadows = p_shadow_casting_setting;
  744. _instance_queue_update(instance, false, true);
  745. }
  746. void RendererSceneCull::instance_geometry_set_material_override(RID p_instance, RID p_material) {
  747. Instance *instance = instance_owner.getornull(p_instance);
  748. ERR_FAIL_COND(!instance);
  749. instance->material_override = p_material;
  750. _instance_queue_update(instance, false, true);
  751. }
  752. void RendererSceneCull::instance_geometry_set_draw_range(RID p_instance, float p_min, float p_max, float p_min_margin, float p_max_margin) {
  753. }
  754. void RendererSceneCull::instance_geometry_set_as_instance_lod(RID p_instance, RID p_as_lod_of_instance) {
  755. }
  756. void RendererSceneCull::instance_geometry_set_lightmap(RID p_instance, RID p_lightmap, const Rect2 &p_lightmap_uv_scale, int p_slice_index) {
  757. Instance *instance = instance_owner.getornull(p_instance);
  758. ERR_FAIL_COND(!instance);
  759. if (instance->lightmap) {
  760. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(((Instance *)instance->lightmap)->base_data);
  761. lightmap_data->users.erase(instance);
  762. instance->lightmap = nullptr;
  763. }
  764. Instance *lightmap_instance = instance_owner.getornull(p_lightmap);
  765. instance->lightmap = lightmap_instance;
  766. instance->lightmap_uv_scale = p_lightmap_uv_scale;
  767. instance->lightmap_slice_index = p_slice_index;
  768. if (lightmap_instance) {
  769. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(lightmap_instance->base_data);
  770. lightmap_data->users.insert(instance);
  771. }
  772. }
  773. void RendererSceneCull::instance_geometry_set_shader_parameter(RID p_instance, const StringName &p_parameter, const Variant &p_value) {
  774. Instance *instance = instance_owner.getornull(p_instance);
  775. ERR_FAIL_COND(!instance);
  776. Map<StringName, RendererSceneRender::InstanceBase::InstanceShaderParameter>::Element *E = instance->instance_shader_parameters.find(p_parameter);
  777. if (!E) {
  778. RendererSceneRender::InstanceBase::InstanceShaderParameter isp;
  779. isp.index = -1;
  780. isp.info = PropertyInfo();
  781. isp.value = p_value;
  782. instance->instance_shader_parameters[p_parameter] = isp;
  783. } else {
  784. E->get().value = p_value;
  785. if (E->get().index >= 0 && instance->instance_allocated_shader_parameters) {
  786. //update directly
  787. RSG::storage->global_variables_instance_update(p_instance, E->get().index, p_value);
  788. }
  789. }
  790. }
  791. Variant RendererSceneCull::instance_geometry_get_shader_parameter(RID p_instance, const StringName &p_parameter) const {
  792. const Instance *instance = const_cast<RendererSceneCull *>(this)->instance_owner.getornull(p_instance);
  793. ERR_FAIL_COND_V(!instance, Variant());
  794. if (instance->instance_shader_parameters.has(p_parameter)) {
  795. return instance->instance_shader_parameters[p_parameter].value;
  796. }
  797. return Variant();
  798. }
  799. Variant RendererSceneCull::instance_geometry_get_shader_parameter_default_value(RID p_instance, const StringName &p_parameter) const {
  800. const Instance *instance = const_cast<RendererSceneCull *>(this)->instance_owner.getornull(p_instance);
  801. ERR_FAIL_COND_V(!instance, Variant());
  802. if (instance->instance_shader_parameters.has(p_parameter)) {
  803. return instance->instance_shader_parameters[p_parameter].default_value;
  804. }
  805. return Variant();
  806. }
  807. void RendererSceneCull::instance_geometry_get_shader_parameter_list(RID p_instance, List<PropertyInfo> *p_parameters) const {
  808. const Instance *instance = const_cast<RendererSceneCull *>(this)->instance_owner.getornull(p_instance);
  809. ERR_FAIL_COND(!instance);
  810. const_cast<RendererSceneCull *>(this)->update_dirty_instances();
  811. Vector<StringName> names;
  812. for (Map<StringName, RendererSceneRender::InstanceBase::InstanceShaderParameter>::Element *E = instance->instance_shader_parameters.front(); E; E = E->next()) {
  813. names.push_back(E->key());
  814. }
  815. names.sort_custom<StringName::AlphCompare>();
  816. for (int i = 0; i < names.size(); i++) {
  817. PropertyInfo pinfo = instance->instance_shader_parameters[names[i]].info;
  818. p_parameters->push_back(pinfo);
  819. }
  820. }
  821. void RendererSceneCull::_update_instance(Instance *p_instance) {
  822. p_instance->version++;
  823. if (p_instance->base_type == RS::INSTANCE_LIGHT) {
  824. InstanceLightData *light = static_cast<InstanceLightData *>(p_instance->base_data);
  825. scene_render->light_instance_set_transform(light->instance, p_instance->transform);
  826. scene_render->light_instance_set_aabb(light->instance, p_instance->transform.xform(p_instance->aabb));
  827. light->shadow_dirty = true;
  828. RS::LightBakeMode bake_mode = RSG::storage->light_get_bake_mode(p_instance->base);
  829. if (RSG::storage->light_get_type(p_instance->base) != RS::LIGHT_DIRECTIONAL && bake_mode != light->bake_mode) {
  830. if (p_instance->scenario && light->bake_mode == RS::LIGHT_BAKE_DYNAMIC) {
  831. p_instance->scenario->dynamic_lights.erase(light->instance);
  832. }
  833. light->bake_mode = bake_mode;
  834. if (p_instance->scenario && light->bake_mode == RS::LIGHT_BAKE_DYNAMIC) {
  835. p_instance->scenario->dynamic_lights.push_back(light->instance);
  836. }
  837. }
  838. uint32_t max_sdfgi_cascade = RSG::storage->light_get_max_sdfgi_cascade(p_instance->base);
  839. if (light->max_sdfgi_cascade != max_sdfgi_cascade) {
  840. light->max_sdfgi_cascade = max_sdfgi_cascade; //should most likely make sdfgi dirty in scenario
  841. }
  842. }
  843. if (p_instance->base_type == RS::INSTANCE_REFLECTION_PROBE) {
  844. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(p_instance->base_data);
  845. scene_render->reflection_probe_instance_set_transform(reflection_probe->instance, p_instance->transform);
  846. reflection_probe->reflection_dirty = true;
  847. }
  848. if (p_instance->base_type == RS::INSTANCE_DECAL) {
  849. InstanceDecalData *decal = static_cast<InstanceDecalData *>(p_instance->base_data);
  850. scene_render->decal_instance_set_transform(decal->instance, p_instance->transform);
  851. }
  852. if (p_instance->base_type == RS::INSTANCE_GI_PROBE) {
  853. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(p_instance->base_data);
  854. scene_render->gi_probe_instance_set_transform_to_data(gi_probe->probe_instance, p_instance->transform);
  855. }
  856. if (p_instance->base_type == RS::INSTANCE_PARTICLES) {
  857. RSG::storage->particles_set_emission_transform(p_instance->base, p_instance->transform);
  858. }
  859. if (p_instance->base_type == RS::INSTANCE_PARTICLES_COLLISION) {
  860. //remove materials no longer used and un-own them
  861. if (RSG::storage->particles_collision_is_heightfield(p_instance->base)) {
  862. heightfield_particle_colliders_update_list.insert(p_instance);
  863. }
  864. }
  865. if (p_instance->aabb.has_no_surface()) {
  866. return;
  867. }
  868. if ((1 << p_instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) {
  869. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  870. //make sure lights are updated if it casts shadow
  871. if (geom->can_cast_shadows) {
  872. for (List<Instance *>::Element *E = geom->lighting.front(); E; E = E->next()) {
  873. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  874. light->shadow_dirty = true;
  875. }
  876. }
  877. if (!p_instance->lightmap && geom->lightmap_captures.size()) {
  878. //affected by lightmap captures, must update capture info!
  879. _update_instance_lightmap_captures(p_instance);
  880. } else {
  881. if (!p_instance->lightmap_sh.empty()) {
  882. p_instance->lightmap_sh.clear(); //don't need SH
  883. p_instance->lightmap_target_sh.clear(); //don't need SH
  884. }
  885. }
  886. }
  887. if (p_instance->base_type == RS::INSTANCE_LIGHTMAP) {
  888. //if this moved, update the captured objects
  889. InstanceLightmapData *lightmap_data = static_cast<InstanceLightmapData *>(p_instance->base_data);
  890. //erase dependencies, since no longer a lightmap
  891. for (List<InstanceLightmapData::PairInfo>::Element *E = lightmap_data->geometries.front(); E; E = E->next()) {
  892. Instance *geom = E->get().geometry;
  893. _instance_queue_update(geom, true, false);
  894. }
  895. }
  896. p_instance->mirror = p_instance->transform.basis.determinant() < 0.0;
  897. AABB new_aabb;
  898. new_aabb = p_instance->transform.xform(p_instance->aabb);
  899. p_instance->transformed_aabb = new_aabb;
  900. if (!p_instance->scenario) {
  901. return;
  902. }
  903. if (p_instance->octree_id == 0) {
  904. uint32_t base_type = 1 << p_instance->base_type;
  905. uint32_t pairable_mask = 0;
  906. bool pairable = false;
  907. if (p_instance->base_type == RS::INSTANCE_LIGHT || p_instance->base_type == RS::INSTANCE_REFLECTION_PROBE || p_instance->base_type == RS::INSTANCE_DECAL || p_instance->base_type == RS::INSTANCE_LIGHTMAP) {
  908. pairable_mask = p_instance->visible ? RS::INSTANCE_GEOMETRY_MASK : 0;
  909. pairable = true;
  910. }
  911. if (p_instance->base_type == RS::INSTANCE_PARTICLES_COLLISION) {
  912. pairable_mask = p_instance->visible ? (1 << RS::INSTANCE_PARTICLES) : 0;
  913. pairable = true;
  914. }
  915. if (p_instance->base_type == RS::INSTANCE_GI_PROBE) {
  916. //lights and geometries
  917. pairable_mask = p_instance->visible ? RS::INSTANCE_GEOMETRY_MASK | (1 << RS::INSTANCE_LIGHT) : 0;
  918. pairable = true;
  919. }
  920. // not inside octree
  921. p_instance->octree_id = p_instance->scenario->octree.create(p_instance, new_aabb, 0, pairable, base_type, pairable_mask);
  922. } else {
  923. /*
  924. if (new_aabb==p_instance->data.transformed_aabb)
  925. return;
  926. */
  927. p_instance->scenario->octree.move(p_instance->octree_id, new_aabb);
  928. }
  929. }
  930. void RendererSceneCull::_update_instance_aabb(Instance *p_instance) {
  931. AABB new_aabb;
  932. ERR_FAIL_COND(p_instance->base_type != RS::INSTANCE_NONE && !p_instance->base.is_valid());
  933. switch (p_instance->base_type) {
  934. case RenderingServer::INSTANCE_NONE: {
  935. // do nothing
  936. } break;
  937. case RenderingServer::INSTANCE_MESH: {
  938. if (p_instance->custom_aabb) {
  939. new_aabb = *p_instance->custom_aabb;
  940. } else {
  941. new_aabb = RSG::storage->mesh_get_aabb(p_instance->base, p_instance->skeleton);
  942. }
  943. } break;
  944. case RenderingServer::INSTANCE_MULTIMESH: {
  945. if (p_instance->custom_aabb) {
  946. new_aabb = *p_instance->custom_aabb;
  947. } else {
  948. new_aabb = RSG::storage->multimesh_get_aabb(p_instance->base);
  949. }
  950. } break;
  951. case RenderingServer::INSTANCE_IMMEDIATE: {
  952. if (p_instance->custom_aabb) {
  953. new_aabb = *p_instance->custom_aabb;
  954. } else {
  955. new_aabb = RSG::storage->immediate_get_aabb(p_instance->base);
  956. }
  957. } break;
  958. case RenderingServer::INSTANCE_PARTICLES: {
  959. if (p_instance->custom_aabb) {
  960. new_aabb = *p_instance->custom_aabb;
  961. } else {
  962. new_aabb = RSG::storage->particles_get_aabb(p_instance->base);
  963. }
  964. } break;
  965. case RenderingServer::INSTANCE_PARTICLES_COLLISION: {
  966. new_aabb = RSG::storage->particles_collision_get_aabb(p_instance->base);
  967. } break;
  968. case RenderingServer::INSTANCE_LIGHT: {
  969. new_aabb = RSG::storage->light_get_aabb(p_instance->base);
  970. } break;
  971. case RenderingServer::INSTANCE_REFLECTION_PROBE: {
  972. new_aabb = RSG::storage->reflection_probe_get_aabb(p_instance->base);
  973. } break;
  974. case RenderingServer::INSTANCE_DECAL: {
  975. new_aabb = RSG::storage->decal_get_aabb(p_instance->base);
  976. } break;
  977. case RenderingServer::INSTANCE_GI_PROBE: {
  978. new_aabb = RSG::storage->gi_probe_get_bounds(p_instance->base);
  979. } break;
  980. case RenderingServer::INSTANCE_LIGHTMAP: {
  981. new_aabb = RSG::storage->lightmap_get_aabb(p_instance->base);
  982. } break;
  983. default: {
  984. }
  985. }
  986. // <Zylann> This is why I didn't re-use Instance::aabb to implement custom AABBs
  987. if (p_instance->extra_margin) {
  988. new_aabb.grow_by(p_instance->extra_margin);
  989. }
  990. p_instance->aabb = new_aabb;
  991. }
  992. void RendererSceneCull::_update_instance_lightmap_captures(Instance *p_instance) {
  993. bool first_set = p_instance->lightmap_sh.size() == 0;
  994. p_instance->lightmap_sh.resize(9); //using SH
  995. p_instance->lightmap_target_sh.resize(9); //using SH
  996. Color *instance_sh = p_instance->lightmap_target_sh.ptrw();
  997. bool inside = false;
  998. Color accum_sh[9];
  999. float accum_blend = 0.0;
  1000. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  1001. for (List<Instance *>::Element *E = geom->lightmap_captures.front(); E; E = E->next()) {
  1002. Instance *lightmap = E->get();
  1003. bool interior = RSG::storage->lightmap_is_interior(lightmap->base);
  1004. if (inside && !interior) {
  1005. continue; //we are inside, ignore exteriors
  1006. }
  1007. Transform to_bounds = lightmap->transform.affine_inverse();
  1008. Vector3 center = p_instance->transform.xform(p_instance->aabb.position + p_instance->aabb.size * 0.5); //use aabb center
  1009. Vector3 lm_pos = to_bounds.xform(center);
  1010. AABB bounds = RSG::storage->lightmap_get_aabb(lightmap->base);
  1011. if (!bounds.has_point(lm_pos)) {
  1012. continue; //not in this lightmap
  1013. }
  1014. Color sh[9];
  1015. RSG::storage->lightmap_tap_sh_light(lightmap->base, lm_pos, sh);
  1016. //rotate it
  1017. Basis rot = lightmap->transform.basis.orthonormalized();
  1018. for (int i = 0; i < 3; i++) {
  1019. float csh[9];
  1020. for (int j = 0; j < 9; j++) {
  1021. csh[j] = sh[j][i];
  1022. }
  1023. rot.rotate_sh(csh);
  1024. for (int j = 0; j < 9; j++) {
  1025. sh[j][i] = csh[j];
  1026. }
  1027. }
  1028. Vector3 inner_pos = ((lm_pos - bounds.position) / bounds.size) * 2.0 - Vector3(1.0, 1.0, 1.0);
  1029. float blend = MAX(inner_pos.x, MAX(inner_pos.y, inner_pos.z));
  1030. //make blend more rounded
  1031. blend = Math::lerp(inner_pos.length(), blend, blend);
  1032. blend *= blend;
  1033. blend = MAX(0.0, 1.0 - blend);
  1034. if (interior && !inside) {
  1035. //do not blend, just replace
  1036. for (int j = 0; j < 9; j++) {
  1037. accum_sh[j] = sh[j] * blend;
  1038. }
  1039. accum_blend = blend;
  1040. inside = true;
  1041. } else {
  1042. for (int j = 0; j < 9; j++) {
  1043. accum_sh[j] += sh[j] * blend;
  1044. }
  1045. accum_blend += blend;
  1046. }
  1047. }
  1048. if (accum_blend > 0.0) {
  1049. for (int j = 0; j < 9; j++) {
  1050. instance_sh[j] = accum_sh[j] / accum_blend;
  1051. if (first_set) {
  1052. p_instance->lightmap_sh.write[j] = instance_sh[j];
  1053. }
  1054. }
  1055. }
  1056. }
  1057. bool RendererSceneCull::_light_instance_update_shadow(Instance *p_instance, const Transform p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, bool p_cam_vaspect, RID p_shadow_atlas, Scenario *p_scenario) {
  1058. InstanceLightData *light = static_cast<InstanceLightData *>(p_instance->base_data);
  1059. Transform light_transform = p_instance->transform;
  1060. light_transform.orthonormalize(); //scale does not count on lights
  1061. bool animated_material_found = false;
  1062. switch (RSG::storage->light_get_type(p_instance->base)) {
  1063. case RS::LIGHT_DIRECTIONAL: {
  1064. real_t max_distance = p_cam_projection.get_z_far();
  1065. real_t shadow_max = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE);
  1066. if (shadow_max > 0 && !p_cam_orthogonal) { //its impractical (and leads to unwanted behaviors) to set max distance in orthogonal camera
  1067. max_distance = MIN(shadow_max, max_distance);
  1068. }
  1069. max_distance = MAX(max_distance, p_cam_projection.get_z_near() + 0.001);
  1070. real_t min_distance = MIN(p_cam_projection.get_z_near(), max_distance);
  1071. RS::LightDirectionalShadowDepthRangeMode depth_range_mode = RSG::storage->light_directional_get_shadow_depth_range_mode(p_instance->base);
  1072. real_t pancake_size = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE);
  1073. if (depth_range_mode == RS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_OPTIMIZED) {
  1074. //optimize min/max
  1075. Vector<Plane> planes = p_cam_projection.get_projection_planes(p_cam_transform);
  1076. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, RS::INSTANCE_GEOMETRY_MASK);
  1077. Plane base(p_cam_transform.origin, -p_cam_transform.basis.get_axis(2));
  1078. //check distance max and min
  1079. bool found_items = false;
  1080. real_t z_max = -1e20;
  1081. real_t z_min = 1e20;
  1082. for (int i = 0; i < cull_count; i++) {
  1083. Instance *instance = instance_shadow_cull_result[i];
  1084. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1085. continue;
  1086. }
  1087. if (static_cast<InstanceGeometryData *>(instance->base_data)->material_is_animated) {
  1088. animated_material_found = true;
  1089. }
  1090. real_t max, min;
  1091. instance->transformed_aabb.project_range_in_plane(base, min, max);
  1092. if (max > z_max) {
  1093. z_max = max;
  1094. }
  1095. if (min < z_min) {
  1096. z_min = min;
  1097. }
  1098. found_items = true;
  1099. }
  1100. if (found_items) {
  1101. min_distance = MAX(min_distance, z_min);
  1102. max_distance = MIN(max_distance, z_max);
  1103. }
  1104. }
  1105. real_t range = max_distance - min_distance;
  1106. int splits = 0;
  1107. switch (RSG::storage->light_directional_get_shadow_mode(p_instance->base)) {
  1108. case RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL:
  1109. splits = 1;
  1110. break;
  1111. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS:
  1112. splits = 2;
  1113. break;
  1114. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS:
  1115. splits = 4;
  1116. break;
  1117. }
  1118. real_t distances[5];
  1119. distances[0] = min_distance;
  1120. for (int i = 0; i < splits; i++) {
  1121. distances[i + 1] = min_distance + RSG::storage->light_get_param(p_instance->base, RS::LightParam(RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET + i)) * range;
  1122. };
  1123. distances[splits] = max_distance;
  1124. real_t texture_size = scene_render->get_directional_light_shadow_size(light->instance);
  1125. bool overlap = RSG::storage->light_directional_get_blend_splits(p_instance->base);
  1126. real_t first_radius = 0.0;
  1127. real_t min_distance_bias_scale = pancake_size > 0 ? distances[1] / 10.0 : 0;
  1128. for (int i = 0; i < splits; i++) {
  1129. RENDER_TIMESTAMP("Culling Directional Light split" + itos(i));
  1130. // setup a camera matrix for that range!
  1131. CameraMatrix camera_matrix;
  1132. real_t aspect = p_cam_projection.get_aspect();
  1133. if (p_cam_orthogonal) {
  1134. Vector2 vp_he = p_cam_projection.get_viewport_half_extents();
  1135. camera_matrix.set_orthogonal(vp_he.y * 2.0, aspect, distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], false);
  1136. } else {
  1137. real_t fov = p_cam_projection.get_fov(); //this is actually yfov, because set aspect tries to keep it
  1138. camera_matrix.set_perspective(fov, aspect, distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], true);
  1139. }
  1140. //obtain the frustum endpoints
  1141. Vector3 endpoints[8]; // frustum plane endpoints
  1142. bool res = camera_matrix.get_endpoints(p_cam_transform, endpoints);
  1143. ERR_CONTINUE(!res);
  1144. // obtain the light frustm ranges (given endpoints)
  1145. Transform transform = light_transform; //discard scale and stabilize light
  1146. Vector3 x_vec = transform.basis.get_axis(Vector3::AXIS_X).normalized();
  1147. Vector3 y_vec = transform.basis.get_axis(Vector3::AXIS_Y).normalized();
  1148. Vector3 z_vec = transform.basis.get_axis(Vector3::AXIS_Z).normalized();
  1149. //z_vec points agsint the camera, like in default opengl
  1150. real_t x_min = 0.f, x_max = 0.f;
  1151. real_t y_min = 0.f, y_max = 0.f;
  1152. real_t z_min = 0.f, z_max = 0.f;
  1153. // FIXME: z_max_cam is defined, computed, but not used below when setting up
  1154. // ortho_camera. Commented out for now to fix warnings but should be investigated.
  1155. real_t x_min_cam = 0.f, x_max_cam = 0.f;
  1156. real_t y_min_cam = 0.f, y_max_cam = 0.f;
  1157. real_t z_min_cam = 0.f;
  1158. //real_t z_max_cam = 0.f;
  1159. real_t bias_scale = 1.0;
  1160. real_t aspect_bias_scale = 1.0;
  1161. //used for culling
  1162. for (int j = 0; j < 8; j++) {
  1163. real_t d_x = x_vec.dot(endpoints[j]);
  1164. real_t d_y = y_vec.dot(endpoints[j]);
  1165. real_t d_z = z_vec.dot(endpoints[j]);
  1166. if (j == 0 || d_x < x_min) {
  1167. x_min = d_x;
  1168. }
  1169. if (j == 0 || d_x > x_max) {
  1170. x_max = d_x;
  1171. }
  1172. if (j == 0 || d_y < y_min) {
  1173. y_min = d_y;
  1174. }
  1175. if (j == 0 || d_y > y_max) {
  1176. y_max = d_y;
  1177. }
  1178. if (j == 0 || d_z < z_min) {
  1179. z_min = d_z;
  1180. }
  1181. if (j == 0 || d_z > z_max) {
  1182. z_max = d_z;
  1183. }
  1184. }
  1185. real_t radius = 0;
  1186. real_t soft_shadow_expand = 0;
  1187. Vector3 center;
  1188. {
  1189. //camera viewport stuff
  1190. for (int j = 0; j < 8; j++) {
  1191. center += endpoints[j];
  1192. }
  1193. center /= 8.0;
  1194. //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;
  1195. for (int j = 0; j < 8; j++) {
  1196. real_t d = center.distance_to(endpoints[j]);
  1197. if (d > radius) {
  1198. radius = d;
  1199. }
  1200. }
  1201. radius *= texture_size / (texture_size - 2.0); //add a texel by each side
  1202. if (i == 0) {
  1203. first_radius = radius;
  1204. } else {
  1205. bias_scale = radius / first_radius;
  1206. }
  1207. z_min_cam = z_vec.dot(center) - radius;
  1208. {
  1209. float soft_shadow_angle = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_SIZE);
  1210. if (soft_shadow_angle > 0.0 && pancake_size > 0.0) {
  1211. float z_range = (z_vec.dot(center) + radius + pancake_size) - z_min_cam;
  1212. soft_shadow_expand = Math::tan(Math::deg2rad(soft_shadow_angle)) * z_range;
  1213. x_max += soft_shadow_expand;
  1214. y_max += soft_shadow_expand;
  1215. x_min -= soft_shadow_expand;
  1216. y_min -= soft_shadow_expand;
  1217. }
  1218. }
  1219. x_max_cam = x_vec.dot(center) + radius + soft_shadow_expand;
  1220. x_min_cam = x_vec.dot(center) - radius - soft_shadow_expand;
  1221. y_max_cam = y_vec.dot(center) + radius + soft_shadow_expand;
  1222. y_min_cam = y_vec.dot(center) - radius - soft_shadow_expand;
  1223. if (depth_range_mode == RS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE) {
  1224. //this trick here is what stabilizes the shadow (make potential jaggies to not move)
  1225. //at the cost of some wasted resolution. Still the quality increase is very well worth it
  1226. real_t unit = radius * 2.0 / texture_size;
  1227. x_max_cam = Math::stepify(x_max_cam, unit);
  1228. x_min_cam = Math::stepify(x_min_cam, unit);
  1229. y_max_cam = Math::stepify(y_max_cam, unit);
  1230. y_min_cam = Math::stepify(y_min_cam, unit);
  1231. }
  1232. }
  1233. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  1234. Vector<Plane> light_frustum_planes;
  1235. light_frustum_planes.resize(6);
  1236. //right/left
  1237. light_frustum_planes.write[0] = Plane(x_vec, x_max);
  1238. light_frustum_planes.write[1] = Plane(-x_vec, -x_min);
  1239. //top/bottom
  1240. light_frustum_planes.write[2] = Plane(y_vec, y_max);
  1241. light_frustum_planes.write[3] = Plane(-y_vec, -y_min);
  1242. //near/far
  1243. light_frustum_planes.write[4] = Plane(z_vec, z_max + 1e6);
  1244. light_frustum_planes.write[5] = Plane(-z_vec, -z_min); // z_min is ok, since casters further than far-light plane are not needed
  1245. int cull_count = p_scenario->octree.cull_convex(light_frustum_planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, RS::INSTANCE_GEOMETRY_MASK);
  1246. // a pre pass will need to be needed to determine the actual z-near to be used
  1247. Plane near_plane(light_transform.origin, -light_transform.basis.get_axis(2));
  1248. real_t cull_max = 0;
  1249. for (int j = 0; j < cull_count; j++) {
  1250. real_t min, max;
  1251. Instance *instance = instance_shadow_cull_result[j];
  1252. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1253. cull_count--;
  1254. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1255. j--;
  1256. continue;
  1257. }
  1258. instance->transformed_aabb.project_range_in_plane(Plane(z_vec, 0), min, max);
  1259. instance->depth = near_plane.distance_to(instance->transform.origin);
  1260. instance->depth_layer = 0;
  1261. if (j == 0 || max > cull_max) {
  1262. cull_max = max;
  1263. }
  1264. if (instance->mesh_instance.is_valid()) {
  1265. RSG::storage->mesh_instance_check_for_update(instance->mesh_instance);
  1266. }
  1267. }
  1268. if (cull_max > z_max) {
  1269. z_max = cull_max;
  1270. }
  1271. if (pancake_size > 0) {
  1272. z_max = z_vec.dot(center) + radius + pancake_size;
  1273. }
  1274. if (aspect != 1.0) {
  1275. // if the aspect is different, then the radius will become larger.
  1276. // if this happens, then bias needs to be adjusted too, as depth will increase
  1277. // to do this, compare the depth of one that would have resulted from a square frustum
  1278. CameraMatrix camera_matrix_square;
  1279. if (p_cam_orthogonal) {
  1280. Vector2 vp_he = camera_matrix.get_viewport_half_extents();
  1281. if (p_cam_vaspect) {
  1282. camera_matrix_square.set_orthogonal(vp_he.x * 2.0, 1.0, distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], true);
  1283. } else {
  1284. camera_matrix_square.set_orthogonal(vp_he.y * 2.0, 1.0, distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], false);
  1285. }
  1286. } else {
  1287. Vector2 vp_he = camera_matrix.get_viewport_half_extents();
  1288. if (p_cam_vaspect) {
  1289. camera_matrix_square.set_frustum(vp_he.x * 2.0, 1.0, Vector2(), distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], true);
  1290. } else {
  1291. camera_matrix_square.set_frustum(vp_he.y * 2.0, 1.0, Vector2(), distances[(i == 0 || !overlap) ? i : i - 1], distances[i + 1], false);
  1292. }
  1293. }
  1294. Vector3 endpoints_square[8]; // frustum plane endpoints
  1295. res = camera_matrix_square.get_endpoints(p_cam_transform, endpoints_square);
  1296. ERR_CONTINUE(!res);
  1297. Vector3 center_square;
  1298. real_t z_max_square = 0;
  1299. for (int j = 0; j < 8; j++) {
  1300. center_square += endpoints_square[j];
  1301. real_t d_z = z_vec.dot(endpoints_square[j]);
  1302. if (j == 0 || d_z > z_max_square) {
  1303. z_max_square = d_z;
  1304. }
  1305. }
  1306. if (cull_max > z_max_square) {
  1307. z_max_square = cull_max;
  1308. }
  1309. center_square /= 8.0;
  1310. real_t radius_square = 0;
  1311. for (int j = 0; j < 8; j++) {
  1312. real_t d = center_square.distance_to(endpoints_square[j]);
  1313. if (d > radius_square) {
  1314. radius_square = d;
  1315. }
  1316. }
  1317. radius_square *= texture_size / (texture_size - 2.0); //add a texel by each side
  1318. if (pancake_size > 0) {
  1319. z_max_square = z_vec.dot(center_square) + radius_square + pancake_size;
  1320. }
  1321. real_t z_min_cam_square = z_vec.dot(center_square) - radius_square;
  1322. aspect_bias_scale = (z_max - z_min_cam) / (z_max_square - z_min_cam_square);
  1323. // this is not entirely perfect, because the cull-adjusted z-max may be different
  1324. // but at least it's warranted that it results in a greater bias, so no acne should be present either way.
  1325. // pancaking also helps with this.
  1326. }
  1327. {
  1328. CameraMatrix ortho_camera;
  1329. real_t half_x = (x_max_cam - x_min_cam) * 0.5;
  1330. real_t half_y = (y_max_cam - y_min_cam) * 0.5;
  1331. ortho_camera.set_orthogonal(-half_x, half_x, -half_y, half_y, 0, (z_max - z_min_cam));
  1332. Vector2 uv_scale(1.0 / (x_max_cam - x_min_cam), 1.0 / (y_max_cam - y_min_cam));
  1333. Transform ortho_transform;
  1334. ortho_transform.basis = transform.basis;
  1335. ortho_transform.origin = x_vec * (x_min_cam + half_x) + y_vec * (y_min_cam + half_y) + z_vec * z_max;
  1336. {
  1337. Vector3 max_in_view = p_cam_transform.affine_inverse().xform(z_vec * cull_max);
  1338. Vector3 dir_in_view = p_cam_transform.xform_inv(z_vec).normalized();
  1339. cull_max = dir_in_view.dot(max_in_view);
  1340. }
  1341. scene_render->light_instance_set_shadow_transform(light->instance, ortho_camera, ortho_transform, z_max - z_min_cam, distances[i + 1], i, radius * 2.0 / texture_size, bias_scale * aspect_bias_scale * min_distance_bias_scale, z_max, uv_scale);
  1342. }
  1343. RSG::storage->update_mesh_instances();
  1344. scene_render->render_shadow(light->instance, p_shadow_atlas, i, (RendererSceneRender::InstanceBase **)instance_shadow_cull_result, cull_count);
  1345. }
  1346. } break;
  1347. case RS::LIGHT_OMNI: {
  1348. RS::LightOmniShadowMode shadow_mode = RSG::storage->light_omni_get_shadow_mode(p_instance->base);
  1349. if (shadow_mode == RS::LIGHT_OMNI_SHADOW_DUAL_PARABOLOID || !scene_render->light_instances_can_render_shadow_cube()) {
  1350. for (int i = 0; i < 2; i++) {
  1351. //using this one ensures that raster deferred will have it
  1352. RENDER_TIMESTAMP("Culling Shadow Paraboloid" + itos(i));
  1353. real_t radius = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_RANGE);
  1354. real_t z = i == 0 ? -1 : 1;
  1355. Vector<Plane> planes;
  1356. planes.resize(6);
  1357. planes.write[0] = light_transform.xform(Plane(Vector3(0, 0, z), radius));
  1358. planes.write[1] = light_transform.xform(Plane(Vector3(1, 0, z).normalized(), radius));
  1359. planes.write[2] = light_transform.xform(Plane(Vector3(-1, 0, z).normalized(), radius));
  1360. planes.write[3] = light_transform.xform(Plane(Vector3(0, 1, z).normalized(), radius));
  1361. planes.write[4] = light_transform.xform(Plane(Vector3(0, -1, z).normalized(), radius));
  1362. planes.write[5] = light_transform.xform(Plane(Vector3(0, 0, -z), 0));
  1363. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, RS::INSTANCE_GEOMETRY_MASK);
  1364. Plane near_plane(light_transform.origin, light_transform.basis.get_axis(2) * z);
  1365. for (int j = 0; j < cull_count; j++) {
  1366. Instance *instance = instance_shadow_cull_result[j];
  1367. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1368. cull_count--;
  1369. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1370. j--;
  1371. } else {
  1372. if (static_cast<InstanceGeometryData *>(instance->base_data)->material_is_animated) {
  1373. animated_material_found = true;
  1374. }
  1375. instance->depth = near_plane.distance_to(instance->transform.origin);
  1376. instance->depth_layer = 0;
  1377. if (instance->mesh_instance.is_valid()) {
  1378. RSG::storage->mesh_instance_check_for_update(instance->mesh_instance);
  1379. }
  1380. }
  1381. }
  1382. RSG::storage->update_mesh_instances();
  1383. scene_render->light_instance_set_shadow_transform(light->instance, CameraMatrix(), light_transform, radius, 0, i, 0);
  1384. scene_render->render_shadow(light->instance, p_shadow_atlas, i, (RendererSceneRender::InstanceBase **)instance_shadow_cull_result, cull_count);
  1385. }
  1386. } else { //shadow cube
  1387. real_t radius = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_RANGE);
  1388. CameraMatrix cm;
  1389. cm.set_perspective(90, 1, 0.01, radius);
  1390. for (int i = 0; i < 6; i++) {
  1391. RENDER_TIMESTAMP("Culling Shadow Cube side" + itos(i));
  1392. //using this one ensures that raster deferred will have it
  1393. static const Vector3 view_normals[6] = {
  1394. Vector3(+1, 0, 0),
  1395. Vector3(-1, 0, 0),
  1396. Vector3(0, -1, 0),
  1397. Vector3(0, +1, 0),
  1398. Vector3(0, 0, +1),
  1399. Vector3(0, 0, -1)
  1400. };
  1401. static const Vector3 view_up[6] = {
  1402. Vector3(0, -1, 0),
  1403. Vector3(0, -1, 0),
  1404. Vector3(0, 0, -1),
  1405. Vector3(0, 0, +1),
  1406. Vector3(0, -1, 0),
  1407. Vector3(0, -1, 0)
  1408. };
  1409. Transform xform = light_transform * Transform().looking_at(view_normals[i], view_up[i]);
  1410. Vector<Plane> planes = cm.get_projection_planes(xform);
  1411. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, RS::INSTANCE_GEOMETRY_MASK);
  1412. Plane near_plane(xform.origin, -xform.basis.get_axis(2));
  1413. for (int j = 0; j < cull_count; j++) {
  1414. Instance *instance = instance_shadow_cull_result[j];
  1415. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1416. cull_count--;
  1417. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1418. j--;
  1419. } else {
  1420. if (static_cast<InstanceGeometryData *>(instance->base_data)->material_is_animated) {
  1421. animated_material_found = true;
  1422. }
  1423. instance->depth = near_plane.distance_to(instance->transform.origin);
  1424. instance->depth_layer = 0;
  1425. if (instance->mesh_instance.is_valid()) {
  1426. RSG::storage->mesh_instance_check_for_update(instance->mesh_instance);
  1427. }
  1428. }
  1429. }
  1430. RSG::storage->update_mesh_instances();
  1431. scene_render->light_instance_set_shadow_transform(light->instance, cm, xform, radius, 0, i, 0);
  1432. scene_render->render_shadow(light->instance, p_shadow_atlas, i, (RendererSceneRender::InstanceBase **)instance_shadow_cull_result, cull_count);
  1433. }
  1434. //restore the regular DP matrix
  1435. scene_render->light_instance_set_shadow_transform(light->instance, CameraMatrix(), light_transform, radius, 0, 0, 0);
  1436. }
  1437. } break;
  1438. case RS::LIGHT_SPOT: {
  1439. RENDER_TIMESTAMP("Culling Spot Light");
  1440. real_t radius = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_RANGE);
  1441. real_t angle = RSG::storage->light_get_param(p_instance->base, RS::LIGHT_PARAM_SPOT_ANGLE);
  1442. CameraMatrix cm;
  1443. cm.set_perspective(angle * 2.0, 1.0, 0.01, radius);
  1444. Vector<Plane> planes = cm.get_projection_planes(light_transform);
  1445. int cull_count = p_scenario->octree.cull_convex(planes, instance_shadow_cull_result, MAX_INSTANCE_CULL, RS::INSTANCE_GEOMETRY_MASK);
  1446. Plane near_plane(light_transform.origin, -light_transform.basis.get_axis(2));
  1447. for (int j = 0; j < cull_count; j++) {
  1448. Instance *instance = instance_shadow_cull_result[j];
  1449. if (!instance->visible || !((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) || !static_cast<InstanceGeometryData *>(instance->base_data)->can_cast_shadows) {
  1450. cull_count--;
  1451. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[cull_count]);
  1452. j--;
  1453. } else {
  1454. if (static_cast<InstanceGeometryData *>(instance->base_data)->material_is_animated) {
  1455. animated_material_found = true;
  1456. }
  1457. instance->depth = near_plane.distance_to(instance->transform.origin);
  1458. instance->depth_layer = 0;
  1459. if (instance->mesh_instance.is_valid()) {
  1460. RSG::storage->mesh_instance_check_for_update(instance->mesh_instance);
  1461. }
  1462. }
  1463. }
  1464. RSG::storage->update_mesh_instances();
  1465. scene_render->light_instance_set_shadow_transform(light->instance, cm, light_transform, radius, 0, 0, 0);
  1466. scene_render->render_shadow(light->instance, p_shadow_atlas, 0, (RendererSceneRender::InstanceBase **)instance_shadow_cull_result, cull_count);
  1467. } break;
  1468. }
  1469. return animated_material_found;
  1470. }
  1471. void RendererSceneCull::render_camera(RID p_render_buffers, RID p_camera, RID p_scenario, Size2 p_viewport_size, RID p_shadow_atlas) {
  1472. // render to mono camera
  1473. #ifndef _3D_DISABLED
  1474. Camera *camera = camera_owner.getornull(p_camera);
  1475. ERR_FAIL_COND(!camera);
  1476. /* STEP 1 - SETUP CAMERA */
  1477. CameraMatrix camera_matrix;
  1478. bool ortho = false;
  1479. switch (camera->type) {
  1480. case Camera::ORTHOGONAL: {
  1481. camera_matrix.set_orthogonal(
  1482. camera->size,
  1483. p_viewport_size.width / (float)p_viewport_size.height,
  1484. camera->znear,
  1485. camera->zfar,
  1486. camera->vaspect);
  1487. ortho = true;
  1488. } break;
  1489. case Camera::PERSPECTIVE: {
  1490. camera_matrix.set_perspective(
  1491. camera->fov,
  1492. p_viewport_size.width / (float)p_viewport_size.height,
  1493. camera->znear,
  1494. camera->zfar,
  1495. camera->vaspect);
  1496. ortho = false;
  1497. } break;
  1498. case Camera::FRUSTUM: {
  1499. camera_matrix.set_frustum(
  1500. camera->size,
  1501. p_viewport_size.width / (float)p_viewport_size.height,
  1502. camera->offset,
  1503. camera->znear,
  1504. camera->zfar,
  1505. camera->vaspect);
  1506. ortho = false;
  1507. } break;
  1508. }
  1509. RID environment = _render_get_environment(p_camera, p_scenario);
  1510. _prepare_scene(camera->transform, camera_matrix, ortho, camera->vaspect, p_render_buffers, environment, camera->visible_layers, p_scenario, p_shadow_atlas, RID());
  1511. _render_scene(p_render_buffers, camera->transform, camera_matrix, ortho, environment, camera->effects, p_scenario, p_shadow_atlas, RID(), -1);
  1512. #endif
  1513. }
  1514. void RendererSceneCull::render_camera(RID p_render_buffers, Ref<XRInterface> &p_interface, XRInterface::Eyes p_eye, RID p_camera, RID p_scenario, Size2 p_viewport_size, RID p_shadow_atlas) {
  1515. // render for AR/VR interface
  1516. Camera *camera = camera_owner.getornull(p_camera);
  1517. ERR_FAIL_COND(!camera);
  1518. /* SETUP CAMERA, we are ignoring type and FOV here */
  1519. float aspect = p_viewport_size.width / (float)p_viewport_size.height;
  1520. CameraMatrix camera_matrix = p_interface->get_projection_for_eye(p_eye, aspect, camera->znear, camera->zfar);
  1521. // We also ignore our camera position, it will have been positioned with a slightly old tracking position.
  1522. // Instead we take our origin point and have our ar/vr interface add fresh tracking data! Whoohoo!
  1523. Transform world_origin = XRServer::get_singleton()->get_world_origin();
  1524. Transform cam_transform = p_interface->get_transform_for_eye(p_eye, world_origin);
  1525. RID environment = _render_get_environment(p_camera, p_scenario);
  1526. // For stereo render we only prepare for our left eye and then reuse the outcome for our right eye
  1527. if (p_eye == XRInterface::EYE_LEFT) {
  1528. // Center our transform, we assume basis is equal.
  1529. Transform mono_transform = cam_transform;
  1530. Transform right_transform = p_interface->get_transform_for_eye(XRInterface::EYE_RIGHT, world_origin);
  1531. mono_transform.origin += right_transform.origin;
  1532. mono_transform.origin *= 0.5;
  1533. // We need to combine our projection frustums for culling.
  1534. // Ideally we should use our clipping planes for this and combine them,
  1535. // however our shadow map logic uses our projection matrix.
  1536. // Note: as our left and right frustums should be mirrored, we don't need our right projection matrix.
  1537. // - get some base values we need
  1538. float eye_dist = (mono_transform.origin - cam_transform.origin).length();
  1539. float z_near = camera_matrix.get_z_near(); // get our near plane
  1540. float z_far = camera_matrix.get_z_far(); // get our far plane
  1541. float width = (2.0 * z_near) / camera_matrix.matrix[0][0];
  1542. float x_shift = width * camera_matrix.matrix[2][0];
  1543. float height = (2.0 * z_near) / camera_matrix.matrix[1][1];
  1544. float y_shift = height * camera_matrix.matrix[2][1];
  1545. // printf("Eye_dist = %f, Near = %f, Far = %f, Width = %f, Shift = %f\n", eye_dist, z_near, z_far, width, x_shift);
  1546. // - calculate our near plane size (horizontal only, right_near is mirrored)
  1547. float left_near = -eye_dist - ((width - x_shift) * 0.5);
  1548. // - calculate our far plane size (horizontal only, right_far is mirrored)
  1549. float left_far = -eye_dist - (z_far * (width - x_shift) * 0.5 / z_near);
  1550. float left_far_right_eye = eye_dist - (z_far * (width + x_shift) * 0.5 / z_near);
  1551. if (left_far > left_far_right_eye) {
  1552. // on displays smaller then double our iod, the right eye far frustrum can overtake the left eyes.
  1553. left_far = left_far_right_eye;
  1554. }
  1555. // - figure out required z-shift
  1556. float slope = (left_far - left_near) / (z_far - z_near);
  1557. float z_shift = (left_near / slope) - z_near;
  1558. // - figure out new vertical near plane size (this will be slightly oversized thanks to our z-shift)
  1559. float top_near = (height - y_shift) * 0.5;
  1560. top_near += (top_near / z_near) * z_shift;
  1561. float bottom_near = -(height + y_shift) * 0.5;
  1562. bottom_near += (bottom_near / z_near) * z_shift;
  1563. // printf("Left_near = %f, Left_far = %f, Top_near = %f, Bottom_near = %f, Z_shift = %f\n", left_near, left_far, top_near, bottom_near, z_shift);
  1564. // - generate our frustum
  1565. CameraMatrix combined_matrix;
  1566. combined_matrix.set_frustum(left_near, -left_near, bottom_near, top_near, z_near + z_shift, z_far + z_shift);
  1567. // and finally move our camera back
  1568. Transform apply_z_shift;
  1569. apply_z_shift.origin = Vector3(0.0, 0.0, z_shift); // z negative is forward so this moves it backwards
  1570. mono_transform *= apply_z_shift;
  1571. // now prepare our scene with our adjusted transform projection matrix
  1572. _prepare_scene(mono_transform, combined_matrix, false, false, p_render_buffers, environment, camera->visible_layers, p_scenario, p_shadow_atlas, RID());
  1573. } else if (p_eye == XRInterface::EYE_MONO) {
  1574. // For mono render, prepare as per usual
  1575. _prepare_scene(cam_transform, camera_matrix, false, false, p_render_buffers, environment, camera->visible_layers, p_scenario, p_shadow_atlas, RID());
  1576. }
  1577. // And render our scene...
  1578. _render_scene(p_render_buffers, cam_transform, camera_matrix, false, environment, camera->effects, p_scenario, p_shadow_atlas, RID(), -1);
  1579. };
  1580. void RendererSceneCull::_prepare_scene(const Transform p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, bool p_cam_vaspect, RID p_render_buffers, RID p_environment, uint32_t p_visible_layers, RID p_scenario, RID p_shadow_atlas, RID p_reflection_probe, bool p_using_shadows) {
  1581. // Note, in stereo rendering:
  1582. // - p_cam_transform will be a transform in the middle of our two eyes
  1583. // - p_cam_projection is a wider frustrum that encompasses both eyes
  1584. Scenario *scenario = scenario_owner.getornull(p_scenario);
  1585. render_pass++;
  1586. uint32_t camera_layer_mask = p_visible_layers;
  1587. scene_render->set_scene_pass(render_pass);
  1588. if (p_render_buffers.is_valid()) {
  1589. scene_render->sdfgi_update(p_render_buffers, p_environment, p_cam_transform.origin); //update conditions for SDFGI (whether its used or not)
  1590. }
  1591. RENDER_TIMESTAMP("Frustum Culling");
  1592. //rasterizer->set_camera(camera->transform, camera_matrix,ortho);
  1593. Vector<Plane> planes = p_cam_projection.get_projection_planes(p_cam_transform);
  1594. Plane near_plane(p_cam_transform.origin, -p_cam_transform.basis.get_axis(2).normalized());
  1595. float z_far = p_cam_projection.get_z_far();
  1596. /* STEP 2 - CULL */
  1597. instance_cull_count = scenario->octree.cull_convex(planes, instance_cull_result, MAX_INSTANCE_CULL);
  1598. light_cull_count = 0;
  1599. reflection_probe_cull_count = 0;
  1600. decal_cull_count = 0;
  1601. gi_probe_cull_count = 0;
  1602. lightmap_cull_count = 0;
  1603. //light_samplers_culled=0;
  1604. /*
  1605. print_line("OT: "+rtos( (OS::get_singleton()->get_ticks_usec()-t)/1000.0));
  1606. print_line("OTO: "+itos(p_scenario->octree.get_octant_count()));
  1607. print_line("OTE: "+itos(p_scenario->octree.get_elem_count()));
  1608. print_line("OTP: "+itos(p_scenario->octree.get_pair_count()));
  1609. */
  1610. /* STEP 3 - PROCESS PORTALS, VALIDATE ROOMS */
  1611. //removed, will replace with culling
  1612. /* STEP 4 - REMOVE FURTHER CULLED OBJECTS, ADD LIGHTS */
  1613. uint64_t frame_number = RSG::rasterizer->get_frame_number();
  1614. float lightmap_probe_update_speed = RSG::storage->lightmap_get_probe_capture_update_speed() * RSG::rasterizer->get_frame_delta_time();
  1615. for (int i = 0; i < instance_cull_count; i++) {
  1616. Instance *ins = instance_cull_result[i];
  1617. bool keep = false;
  1618. if ((camera_layer_mask & ins->layer_mask) == 0) {
  1619. //failure
  1620. } else if (ins->base_type == RS::INSTANCE_LIGHT && ins->visible) {
  1621. if (light_cull_count < MAX_LIGHTS_CULLED) {
  1622. InstanceLightData *light = static_cast<InstanceLightData *>(ins->base_data);
  1623. if (!light->geometries.empty()) {
  1624. //do not add this light if no geometry is affected by it..
  1625. light_cull_result[light_cull_count] = ins;
  1626. light_instance_cull_result[light_cull_count] = light->instance;
  1627. if (p_shadow_atlas.is_valid() && RSG::storage->light_has_shadow(ins->base)) {
  1628. scene_render->light_instance_mark_visible(light->instance); //mark it visible for shadow allocation later
  1629. }
  1630. light_cull_count++;
  1631. }
  1632. }
  1633. } else if (ins->base_type == RS::INSTANCE_REFLECTION_PROBE && ins->visible) {
  1634. if (reflection_probe_cull_count < MAX_REFLECTION_PROBES_CULLED) {
  1635. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(ins->base_data);
  1636. if (p_reflection_probe != reflection_probe->instance) {
  1637. //avoid entering The Matrix
  1638. if (!reflection_probe->geometries.empty()) {
  1639. //do not add this light if no geometry is affected by it..
  1640. if (reflection_probe->reflection_dirty || scene_render->reflection_probe_instance_needs_redraw(reflection_probe->instance)) {
  1641. if (!reflection_probe->update_list.in_list()) {
  1642. reflection_probe->render_step = 0;
  1643. reflection_probe_render_list.add_last(&reflection_probe->update_list);
  1644. }
  1645. reflection_probe->reflection_dirty = false;
  1646. }
  1647. if (scene_render->reflection_probe_instance_has_reflection(reflection_probe->instance)) {
  1648. reflection_probe_instance_cull_result[reflection_probe_cull_count] = reflection_probe->instance;
  1649. reflection_probe_cull_count++;
  1650. }
  1651. }
  1652. }
  1653. }
  1654. } else if (ins->base_type == RS::INSTANCE_DECAL && ins->visible) {
  1655. if (decal_cull_count < MAX_DECALS_CULLED) {
  1656. InstanceDecalData *decal = static_cast<InstanceDecalData *>(ins->base_data);
  1657. if (!decal->geometries.empty()) {
  1658. //do not add this decal if no geometry is affected by it..
  1659. decal_instance_cull_result[decal_cull_count] = decal->instance;
  1660. decal_cull_count++;
  1661. }
  1662. }
  1663. } else if (ins->base_type == RS::INSTANCE_GI_PROBE && ins->visible) {
  1664. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(ins->base_data);
  1665. if (!gi_probe->update_element.in_list()) {
  1666. gi_probe_update_list.add(&gi_probe->update_element);
  1667. }
  1668. if (gi_probe_cull_count < MAX_GI_PROBES_CULLED) {
  1669. gi_probe_instance_cull_result[gi_probe_cull_count] = gi_probe->probe_instance;
  1670. gi_probe_cull_count++;
  1671. }
  1672. } else if (ins->base_type == RS::INSTANCE_LIGHTMAP && ins->visible) {
  1673. if (lightmap_cull_count < MAX_LIGHTMAPS_CULLED) {
  1674. lightmap_cull_result[lightmap_cull_count] = ins;
  1675. lightmap_cull_count++;
  1676. }
  1677. } else if (((1 << ins->base_type) & RS::INSTANCE_GEOMETRY_MASK) && ins->visible && ins->cast_shadows != RS::SHADOW_CASTING_SETTING_SHADOWS_ONLY) {
  1678. keep = true;
  1679. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(ins->base_data);
  1680. if (ins->redraw_if_visible) {
  1681. RenderingServerDefault::redraw_request();
  1682. }
  1683. if (ins->base_type == RS::INSTANCE_PARTICLES) {
  1684. //particles visible? process them
  1685. if (RSG::storage->particles_is_inactive(ins->base)) {
  1686. //but if nothing is going on, don't do it.
  1687. keep = false;
  1688. } else {
  1689. RSG::storage->particles_request_process(ins->base);
  1690. RSG::storage->particles_set_view_axis(ins->base, -p_cam_transform.basis.get_axis(2).normalized());
  1691. //particles visible? request redraw
  1692. RenderingServerDefault::redraw_request();
  1693. }
  1694. }
  1695. if (geom->lighting_dirty) {
  1696. int l = 0;
  1697. //only called when lights AABB enter/exit this geometry
  1698. ins->light_instances.resize(geom->lighting.size());
  1699. for (List<Instance *>::Element *E = geom->lighting.front(); E; E = E->next()) {
  1700. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  1701. ins->light_instances.write[l++] = light->instance;
  1702. }
  1703. geom->lighting_dirty = false;
  1704. }
  1705. if (geom->reflection_dirty) {
  1706. int l = 0;
  1707. //only called when reflection probe AABB enter/exit this geometry
  1708. ins->reflection_probe_instances.resize(geom->reflection_probes.size());
  1709. for (List<Instance *>::Element *E = geom->reflection_probes.front(); E; E = E->next()) {
  1710. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(E->get()->base_data);
  1711. ins->reflection_probe_instances.write[l++] = reflection_probe->instance;
  1712. }
  1713. geom->reflection_dirty = false;
  1714. }
  1715. if (geom->gi_probes_dirty) {
  1716. int l = 0;
  1717. //only called when reflection probe AABB enter/exit this geometry
  1718. ins->gi_probe_instances.resize(geom->gi_probes.size());
  1719. for (List<Instance *>::Element *E = geom->gi_probes.front(); E; E = E->next()) {
  1720. InstanceGIProbeData *gi_probe = static_cast<InstanceGIProbeData *>(E->get()->base_data);
  1721. ins->gi_probe_instances.write[l++] = gi_probe->probe_instance;
  1722. }
  1723. geom->gi_probes_dirty = false;
  1724. }
  1725. if (ins->last_frame_pass != frame_number && !ins->lightmap_target_sh.empty() && !ins->lightmap_sh.empty()) {
  1726. Color *sh = ins->lightmap_sh.ptrw();
  1727. const Color *target_sh = ins->lightmap_target_sh.ptr();
  1728. for (uint32_t j = 0; j < 9; j++) {
  1729. sh[j] = sh[j].lerp(target_sh[j], MIN(1.0, lightmap_probe_update_speed));
  1730. }
  1731. }
  1732. if (ins->mesh_instance.is_valid()) {
  1733. RSG::storage->mesh_instance_check_for_update(ins->mesh_instance);
  1734. }
  1735. ins->depth = near_plane.distance_to(ins->transform.origin);
  1736. ins->depth_layer = CLAMP(int(ins->depth * 16 / z_far), 0, 15);
  1737. }
  1738. if (!keep) {
  1739. // remove, no reason to keep
  1740. instance_cull_count--;
  1741. SWAP(instance_cull_result[i], instance_cull_result[instance_cull_count]);
  1742. i--;
  1743. ins->last_render_pass = 0; // make invalid
  1744. } else {
  1745. ins->last_render_pass = render_pass;
  1746. }
  1747. ins->last_frame_pass = frame_number;
  1748. }
  1749. RSG::storage->update_mesh_instances();
  1750. /* STEP 5 - PROCESS LIGHTS */
  1751. RID *directional_light_ptr = &light_instance_cull_result[light_cull_count];
  1752. directional_light_count = 0;
  1753. // directional lights
  1754. {
  1755. Instance **lights_with_shadow = (Instance **)alloca(sizeof(Instance *) * scenario->directional_lights.size());
  1756. int directional_shadow_count = 0;
  1757. for (List<Instance *>::Element *E = scenario->directional_lights.front(); E; E = E->next()) {
  1758. if (light_cull_count + directional_light_count >= MAX_LIGHTS_CULLED) {
  1759. break;
  1760. }
  1761. if (!E->get()->visible) {
  1762. continue;
  1763. }
  1764. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  1765. //check shadow..
  1766. if (light) {
  1767. if (p_using_shadows && p_shadow_atlas.is_valid() && RSG::storage->light_has_shadow(E->get()->base) && !(RSG::storage->light_get_type(E->get()->base) == RS::LIGHT_DIRECTIONAL && RSG::storage->light_directional_is_sky_only(E->get()->base))) {
  1768. lights_with_shadow[directional_shadow_count++] = E->get();
  1769. }
  1770. //add to list
  1771. directional_light_ptr[directional_light_count++] = light->instance;
  1772. }
  1773. }
  1774. scene_render->set_directional_shadow_count(directional_shadow_count);
  1775. for (int i = 0; i < directional_shadow_count; i++) {
  1776. RENDER_TIMESTAMP(">Rendering Directional Light " + itos(i));
  1777. _light_instance_update_shadow(lights_with_shadow[i], p_cam_transform, p_cam_projection, p_cam_orthogonal, p_cam_vaspect, p_shadow_atlas, scenario);
  1778. RENDER_TIMESTAMP("<Rendering Directional Light " + itos(i));
  1779. }
  1780. }
  1781. if (p_using_shadows) { //setup shadow maps
  1782. //SortArray<Instance*,_InstanceLightsort> sorter;
  1783. //sorter.sort(light_cull_result,light_cull_count);
  1784. for (int i = 0; i < light_cull_count; i++) {
  1785. Instance *ins = light_cull_result[i];
  1786. if (!p_shadow_atlas.is_valid() || !RSG::storage->light_has_shadow(ins->base)) {
  1787. continue;
  1788. }
  1789. InstanceLightData *light = static_cast<InstanceLightData *>(ins->base_data);
  1790. float coverage = 0.f;
  1791. { //compute coverage
  1792. Transform cam_xf = p_cam_transform;
  1793. float zn = p_cam_projection.get_z_near();
  1794. Plane p(cam_xf.origin + cam_xf.basis.get_axis(2) * -zn, -cam_xf.basis.get_axis(2)); //camera near plane
  1795. // near plane half width and height
  1796. Vector2 vp_half_extents = p_cam_projection.get_viewport_half_extents();
  1797. switch (RSG::storage->light_get_type(ins->base)) {
  1798. case RS::LIGHT_OMNI: {
  1799. float radius = RSG::storage->light_get_param(ins->base, RS::LIGHT_PARAM_RANGE);
  1800. //get two points parallel to near plane
  1801. Vector3 points[2] = {
  1802. ins->transform.origin,
  1803. ins->transform.origin + cam_xf.basis.get_axis(0) * radius
  1804. };
  1805. if (!p_cam_orthogonal) {
  1806. //if using perspetive, map them to near plane
  1807. for (int j = 0; j < 2; j++) {
  1808. if (p.distance_to(points[j]) < 0) {
  1809. points[j].z = -zn; //small hack to keep size constant when hitting the screen
  1810. }
  1811. p.intersects_segment(cam_xf.origin, points[j], &points[j]); //map to plane
  1812. }
  1813. }
  1814. float screen_diameter = points[0].distance_to(points[1]) * 2;
  1815. coverage = screen_diameter / (vp_half_extents.x + vp_half_extents.y);
  1816. } break;
  1817. case RS::LIGHT_SPOT: {
  1818. float radius = RSG::storage->light_get_param(ins->base, RS::LIGHT_PARAM_RANGE);
  1819. float angle = RSG::storage->light_get_param(ins->base, RS::LIGHT_PARAM_SPOT_ANGLE);
  1820. float w = radius * Math::sin(Math::deg2rad(angle));
  1821. float d = radius * Math::cos(Math::deg2rad(angle));
  1822. Vector3 base = ins->transform.origin - ins->transform.basis.get_axis(2).normalized() * d;
  1823. Vector3 points[2] = {
  1824. base,
  1825. base + cam_xf.basis.get_axis(0) * w
  1826. };
  1827. if (!p_cam_orthogonal) {
  1828. //if using perspetive, map them to near plane
  1829. for (int j = 0; j < 2; j++) {
  1830. if (p.distance_to(points[j]) < 0) {
  1831. points[j].z = -zn; //small hack to keep size constant when hitting the screen
  1832. }
  1833. p.intersects_segment(cam_xf.origin, points[j], &points[j]); //map to plane
  1834. }
  1835. }
  1836. float screen_diameter = points[0].distance_to(points[1]) * 2;
  1837. coverage = screen_diameter / (vp_half_extents.x + vp_half_extents.y);
  1838. } break;
  1839. default: {
  1840. ERR_PRINT("Invalid Light Type");
  1841. }
  1842. }
  1843. }
  1844. if (light->shadow_dirty) {
  1845. light->last_version++;
  1846. light->shadow_dirty = false;
  1847. }
  1848. bool redraw = scene_render->shadow_atlas_update_light(p_shadow_atlas, light->instance, coverage, light->last_version);
  1849. if (redraw) {
  1850. //must redraw!
  1851. RENDER_TIMESTAMP(">Rendering Light " + itos(i));
  1852. light->shadow_dirty = _light_instance_update_shadow(ins, p_cam_transform, p_cam_projection, p_cam_orthogonal, p_cam_vaspect, p_shadow_atlas, scenario);
  1853. RENDER_TIMESTAMP("<Rendering Light " + itos(i));
  1854. }
  1855. }
  1856. }
  1857. /* UPDATE SDFGI */
  1858. if (p_render_buffers.is_valid()) {
  1859. uint32_t cascade_index[8];
  1860. uint32_t cascade_sizes[8];
  1861. const RID *cascade_ptrs[8];
  1862. uint32_t cascade_count = 0;
  1863. uint32_t sdfgi_light_cull_count = 0;
  1864. uint32_t prev_cascade = 0xFFFFFFFF;
  1865. for (int i = 0; i < scene_render->sdfgi_get_pending_region_count(p_render_buffers); i++) {
  1866. AABB region = scene_render->sdfgi_get_pending_region_bounds(p_render_buffers, i);
  1867. uint32_t region_cascade = scene_render->sdfgi_get_pending_region_cascade(p_render_buffers, i);
  1868. if (region_cascade != prev_cascade) {
  1869. cascade_sizes[cascade_count] = 0;
  1870. cascade_index[cascade_count] = region_cascade;
  1871. cascade_ptrs[cascade_count] = &sdfgi_light_cull_result[sdfgi_light_cull_count];
  1872. cascade_count++;
  1873. sdfgi_light_cull_pass++;
  1874. prev_cascade = region_cascade;
  1875. }
  1876. uint32_t sdfgi_cull_count = scenario->octree.cull_aabb(region, instance_shadow_cull_result, MAX_INSTANCE_CULL);
  1877. for (uint32_t j = 0; j < sdfgi_cull_count; j++) {
  1878. Instance *ins = instance_shadow_cull_result[j];
  1879. bool keep = false;
  1880. if (ins->base_type == RS::INSTANCE_LIGHT && ins->visible) {
  1881. InstanceLightData *instance_light = (InstanceLightData *)ins->base_data;
  1882. if (instance_light->bake_mode != RS::LIGHT_BAKE_STATIC || region_cascade > instance_light->max_sdfgi_cascade) {
  1883. continue;
  1884. }
  1885. if (sdfgi_light_cull_pass != instance_light->sdfgi_cascade_light_pass && sdfgi_light_cull_count < MAX_LIGHTS_CULLED) {
  1886. instance_light->sdfgi_cascade_light_pass = sdfgi_light_cull_pass;
  1887. sdfgi_light_cull_result[sdfgi_light_cull_count++] = instance_light->instance;
  1888. cascade_sizes[cascade_count - 1]++;
  1889. }
  1890. } else if ((1 << ins->base_type) & RS::INSTANCE_GEOMETRY_MASK) {
  1891. if (ins->baked_light) {
  1892. keep = true;
  1893. if (ins->mesh_instance.is_valid()) {
  1894. RSG::storage->mesh_instance_check_for_update(ins->mesh_instance);
  1895. }
  1896. }
  1897. }
  1898. if (!keep) {
  1899. // remove, no reason to keep
  1900. sdfgi_cull_count--;
  1901. SWAP(instance_shadow_cull_result[j], instance_shadow_cull_result[sdfgi_cull_count]);
  1902. j--;
  1903. }
  1904. }
  1905. RSG::storage->update_mesh_instances();
  1906. scene_render->render_sdfgi(p_render_buffers, i, (RendererSceneRender::InstanceBase **)instance_shadow_cull_result, sdfgi_cull_count);
  1907. //have to save updated cascades, then update static lights.
  1908. }
  1909. if (sdfgi_light_cull_count) {
  1910. scene_render->render_sdfgi_static_lights(p_render_buffers, cascade_count, cascade_index, cascade_ptrs, cascade_sizes);
  1911. }
  1912. scene_render->sdfgi_update_probes(p_render_buffers, p_environment, directional_light_ptr, directional_light_count, scenario->dynamic_lights.ptr(), scenario->dynamic_lights.size());
  1913. }
  1914. }
  1915. RID RendererSceneCull::_render_get_environment(RID p_camera, RID p_scenario) {
  1916. Camera *camera = camera_owner.getornull(p_camera);
  1917. if (camera && scene_render->is_environment(camera->env)) {
  1918. return camera->env;
  1919. }
  1920. Scenario *scenario = scenario_owner.getornull(p_scenario);
  1921. if (!scenario) {
  1922. return RID();
  1923. }
  1924. if (scene_render->is_environment(scenario->environment)) {
  1925. return scenario->environment;
  1926. }
  1927. if (scene_render->is_environment(scenario->fallback_environment)) {
  1928. return scenario->fallback_environment;
  1929. }
  1930. return RID();
  1931. }
  1932. void RendererSceneCull::_render_scene(RID p_render_buffers, const Transform p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, RID p_environment, RID p_force_camera_effects, RID p_scenario, RID p_shadow_atlas, RID p_reflection_probe, int p_reflection_probe_pass) {
  1933. Scenario *scenario = scenario_owner.getornull(p_scenario);
  1934. RID camera_effects;
  1935. if (p_force_camera_effects.is_valid()) {
  1936. camera_effects = p_force_camera_effects;
  1937. } else {
  1938. camera_effects = scenario->camera_effects;
  1939. }
  1940. /* PROCESS GEOMETRY AND DRAW SCENE */
  1941. RENDER_TIMESTAMP("Render Scene ");
  1942. scene_render->render_scene(p_render_buffers, p_cam_transform, p_cam_projection, p_cam_orthogonal, (RendererSceneRender::InstanceBase **)instance_cull_result, instance_cull_count, light_instance_cull_result, light_cull_count + directional_light_count, reflection_probe_instance_cull_result, reflection_probe_cull_count, gi_probe_instance_cull_result, gi_probe_cull_count, decal_instance_cull_result, decal_cull_count, (RendererSceneRender::InstanceBase **)lightmap_cull_result, lightmap_cull_count, p_environment, camera_effects, p_shadow_atlas, p_reflection_probe.is_valid() ? RID() : scenario->reflection_atlas, p_reflection_probe, p_reflection_probe_pass);
  1943. }
  1944. void RendererSceneCull::render_empty_scene(RID p_render_buffers, RID p_scenario, RID p_shadow_atlas) {
  1945. #ifndef _3D_DISABLED
  1946. Scenario *scenario = scenario_owner.getornull(p_scenario);
  1947. RID environment;
  1948. if (scenario->environment.is_valid()) {
  1949. environment = scenario->environment;
  1950. } else {
  1951. environment = scenario->fallback_environment;
  1952. }
  1953. RENDER_TIMESTAMP("Render Empty Scene ");
  1954. scene_render->render_scene(p_render_buffers, Transform(), CameraMatrix(), true, nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0, environment, RID(), p_shadow_atlas, scenario->reflection_atlas, RID(), 0);
  1955. #endif
  1956. }
  1957. bool RendererSceneCull::_render_reflection_probe_step(Instance *p_instance, int p_step) {
  1958. InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(p_instance->base_data);
  1959. Scenario *scenario = p_instance->scenario;
  1960. ERR_FAIL_COND_V(!scenario, true);
  1961. RenderingServerDefault::redraw_request(); //update, so it updates in editor
  1962. if (p_step == 0) {
  1963. if (!scene_render->reflection_probe_instance_begin_render(reflection_probe->instance, scenario->reflection_atlas)) {
  1964. return true; //all full
  1965. }
  1966. }
  1967. if (p_step >= 0 && p_step < 6) {
  1968. static const Vector3 view_normals[6] = {
  1969. Vector3(+1, 0, 0),
  1970. Vector3(-1, 0, 0),
  1971. Vector3(0, +1, 0),
  1972. Vector3(0, -1, 0),
  1973. Vector3(0, 0, +1),
  1974. Vector3(0, 0, -1)
  1975. };
  1976. static const Vector3 view_up[6] = {
  1977. Vector3(0, -1, 0),
  1978. Vector3(0, -1, 0),
  1979. Vector3(0, 0, +1),
  1980. Vector3(0, 0, -1),
  1981. Vector3(0, -1, 0),
  1982. Vector3(0, -1, 0)
  1983. };
  1984. Vector3 extents = RSG::storage->reflection_probe_get_extents(p_instance->base);
  1985. Vector3 origin_offset = RSG::storage->reflection_probe_get_origin_offset(p_instance->base);
  1986. float max_distance = RSG::storage->reflection_probe_get_origin_max_distance(p_instance->base);
  1987. Vector3 edge = view_normals[p_step] * extents;
  1988. 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
  1989. max_distance = MAX(max_distance, distance);
  1990. //render cubemap side
  1991. CameraMatrix cm;
  1992. cm.set_perspective(90, 1, 0.01, max_distance);
  1993. Transform local_view;
  1994. local_view.set_look_at(origin_offset, origin_offset + view_normals[p_step], view_up[p_step]);
  1995. Transform xform = p_instance->transform * local_view;
  1996. RID shadow_atlas;
  1997. bool use_shadows = RSG::storage->reflection_probe_renders_shadows(p_instance->base);
  1998. if (use_shadows) {
  1999. shadow_atlas = scenario->reflection_probe_shadow_atlas;
  2000. }
  2001. RENDER_TIMESTAMP("Render Reflection Probe, Step " + itos(p_step));
  2002. _prepare_scene(xform, cm, false, false, RID(), RID(), RSG::storage->reflection_probe_get_cull_mask(p_instance->base), p_instance->scenario->self, shadow_atlas, reflection_probe->instance, use_shadows);
  2003. _render_scene(RID(), xform, cm, false, RID(), RID(), p_instance->scenario->self, shadow_atlas, reflection_probe->instance, p_step);
  2004. } else {
  2005. //do roughness postprocess step until it believes it's done
  2006. RENDER_TIMESTAMP("Post-Process Reflection Probe, Step " + itos(p_step));
  2007. return scene_render->reflection_probe_instance_postprocess_step(reflection_probe->instance);
  2008. }
  2009. return false;
  2010. }
  2011. void RendererSceneCull::render_probes() {
  2012. /* REFLECTION PROBES */
  2013. SelfList<InstanceReflectionProbeData> *ref_probe = reflection_probe_render_list.first();
  2014. bool busy = false;
  2015. while (ref_probe) {
  2016. SelfList<InstanceReflectionProbeData> *next = ref_probe->next();
  2017. RID base = ref_probe->self()->owner->base;
  2018. switch (RSG::storage->reflection_probe_get_update_mode(base)) {
  2019. case RS::REFLECTION_PROBE_UPDATE_ONCE: {
  2020. if (busy) { //already rendering something
  2021. break;
  2022. }
  2023. bool done = _render_reflection_probe_step(ref_probe->self()->owner, ref_probe->self()->render_step);
  2024. if (done) {
  2025. reflection_probe_render_list.remove(ref_probe);
  2026. } else {
  2027. ref_probe->self()->render_step++;
  2028. }
  2029. busy = true; //do not render another one of this kind
  2030. } break;
  2031. case RS::REFLECTION_PROBE_UPDATE_ALWAYS: {
  2032. int step = 0;
  2033. bool done = false;
  2034. while (!done) {
  2035. done = _render_reflection_probe_step(ref_probe->self()->owner, step);
  2036. step++;
  2037. }
  2038. reflection_probe_render_list.remove(ref_probe);
  2039. } break;
  2040. }
  2041. ref_probe = next;
  2042. }
  2043. /* GI PROBES */
  2044. SelfList<InstanceGIProbeData> *gi_probe = gi_probe_update_list.first();
  2045. if (gi_probe) {
  2046. RENDER_TIMESTAMP("Render GI Probes");
  2047. }
  2048. while (gi_probe) {
  2049. SelfList<InstanceGIProbeData> *next = gi_probe->next();
  2050. InstanceGIProbeData *probe = gi_probe->self();
  2051. //Instance *instance_probe = probe->owner;
  2052. //check if probe must be setup, but don't do if on the lighting thread
  2053. bool cache_dirty = false;
  2054. int cache_count = 0;
  2055. {
  2056. int light_cache_size = probe->light_cache.size();
  2057. const InstanceGIProbeData::LightCache *caches = probe->light_cache.ptr();
  2058. const RID *instance_caches = probe->light_instances.ptr();
  2059. int idx = 0; //must count visible lights
  2060. for (Set<Instance *>::Element *E = probe->lights.front(); E; E = E->next()) {
  2061. Instance *instance = E->get();
  2062. InstanceLightData *instance_light = (InstanceLightData *)instance->base_data;
  2063. if (!instance->visible) {
  2064. continue;
  2065. }
  2066. if (cache_dirty) {
  2067. //do nothing, since idx must count all visible lights anyway
  2068. } else if (idx >= light_cache_size) {
  2069. cache_dirty = true;
  2070. } else {
  2071. const InstanceGIProbeData::LightCache *cache = &caches[idx];
  2072. if (
  2073. instance_caches[idx] != instance_light->instance ||
  2074. cache->has_shadow != RSG::storage->light_has_shadow(instance->base) ||
  2075. cache->type != RSG::storage->light_get_type(instance->base) ||
  2076. cache->transform != instance->transform ||
  2077. cache->color != RSG::storage->light_get_color(instance->base) ||
  2078. cache->energy != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ENERGY) ||
  2079. cache->bake_energy != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_INDIRECT_ENERGY) ||
  2080. cache->radius != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_RANGE) ||
  2081. cache->attenuation != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ATTENUATION) ||
  2082. cache->spot_angle != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ANGLE) ||
  2083. cache->spot_attenuation != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ATTENUATION)) {
  2084. cache_dirty = true;
  2085. }
  2086. }
  2087. idx++;
  2088. }
  2089. for (List<Instance *>::Element *E = probe->owner->scenario->directional_lights.front(); E; E = E->next()) {
  2090. Instance *instance = E->get();
  2091. InstanceLightData *instance_light = (InstanceLightData *)instance->base_data;
  2092. if (!instance->visible) {
  2093. continue;
  2094. }
  2095. if (cache_dirty) {
  2096. //do nothing, since idx must count all visible lights anyway
  2097. } else if (idx >= light_cache_size) {
  2098. cache_dirty = true;
  2099. } else {
  2100. const InstanceGIProbeData::LightCache *cache = &caches[idx];
  2101. if (
  2102. instance_caches[idx] != instance_light->instance ||
  2103. cache->has_shadow != RSG::storage->light_has_shadow(instance->base) ||
  2104. cache->type != RSG::storage->light_get_type(instance->base) ||
  2105. cache->transform != instance->transform ||
  2106. cache->color != RSG::storage->light_get_color(instance->base) ||
  2107. cache->energy != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ENERGY) ||
  2108. cache->bake_energy != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_INDIRECT_ENERGY) ||
  2109. cache->radius != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_RANGE) ||
  2110. cache->attenuation != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ATTENUATION) ||
  2111. cache->spot_angle != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ANGLE) ||
  2112. cache->spot_attenuation != RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ATTENUATION) ||
  2113. cache->sky_only != RSG::storage->light_directional_is_sky_only(instance->base)) {
  2114. cache_dirty = true;
  2115. }
  2116. }
  2117. idx++;
  2118. }
  2119. if (idx != light_cache_size) {
  2120. cache_dirty = true;
  2121. }
  2122. cache_count = idx;
  2123. }
  2124. bool update_lights = scene_render->gi_probe_needs_update(probe->probe_instance);
  2125. if (cache_dirty) {
  2126. probe->light_cache.resize(cache_count);
  2127. probe->light_instances.resize(cache_count);
  2128. if (cache_count) {
  2129. InstanceGIProbeData::LightCache *caches = probe->light_cache.ptrw();
  2130. RID *instance_caches = probe->light_instances.ptrw();
  2131. int idx = 0; //must count visible lights
  2132. for (Set<Instance *>::Element *E = probe->lights.front(); E; E = E->next()) {
  2133. Instance *instance = E->get();
  2134. InstanceLightData *instance_light = (InstanceLightData *)instance->base_data;
  2135. if (!instance->visible) {
  2136. continue;
  2137. }
  2138. InstanceGIProbeData::LightCache *cache = &caches[idx];
  2139. instance_caches[idx] = instance_light->instance;
  2140. cache->has_shadow = RSG::storage->light_has_shadow(instance->base);
  2141. cache->type = RSG::storage->light_get_type(instance->base);
  2142. cache->transform = instance->transform;
  2143. cache->color = RSG::storage->light_get_color(instance->base);
  2144. cache->energy = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ENERGY);
  2145. cache->bake_energy = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_INDIRECT_ENERGY);
  2146. cache->radius = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_RANGE);
  2147. cache->attenuation = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ATTENUATION);
  2148. cache->spot_angle = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ANGLE);
  2149. cache->spot_attenuation = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  2150. idx++;
  2151. }
  2152. for (List<Instance *>::Element *E = probe->owner->scenario->directional_lights.front(); E; E = E->next()) {
  2153. Instance *instance = E->get();
  2154. InstanceLightData *instance_light = (InstanceLightData *)instance->base_data;
  2155. if (!instance->visible) {
  2156. continue;
  2157. }
  2158. InstanceGIProbeData::LightCache *cache = &caches[idx];
  2159. instance_caches[idx] = instance_light->instance;
  2160. cache->has_shadow = RSG::storage->light_has_shadow(instance->base);
  2161. cache->type = RSG::storage->light_get_type(instance->base);
  2162. cache->transform = instance->transform;
  2163. cache->color = RSG::storage->light_get_color(instance->base);
  2164. cache->energy = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ENERGY);
  2165. cache->bake_energy = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_INDIRECT_ENERGY);
  2166. cache->radius = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_RANGE);
  2167. cache->attenuation = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_ATTENUATION);
  2168. cache->spot_angle = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ANGLE);
  2169. cache->spot_attenuation = RSG::storage->light_get_param(instance->base, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  2170. cache->sky_only = RSG::storage->light_directional_is_sky_only(instance->base);
  2171. idx++;
  2172. }
  2173. }
  2174. update_lights = true;
  2175. }
  2176. instance_cull_count = 0;
  2177. for (List<InstanceGIProbeData::PairInfo>::Element *E = probe->dynamic_geometries.front(); E; E = E->next()) {
  2178. if (instance_cull_count < MAX_INSTANCE_CULL) {
  2179. Instance *ins = E->get().geometry;
  2180. if (!ins->visible) {
  2181. continue;
  2182. }
  2183. InstanceGeometryData *geom = (InstanceGeometryData *)ins->base_data;
  2184. if (geom->gi_probes_dirty) {
  2185. //giprobes may be dirty, so update
  2186. int l = 0;
  2187. //only called when reflection probe AABB enter/exit this geometry
  2188. ins->gi_probe_instances.resize(geom->gi_probes.size());
  2189. for (List<Instance *>::Element *F = geom->gi_probes.front(); F; F = F->next()) {
  2190. InstanceGIProbeData *gi_probe2 = static_cast<InstanceGIProbeData *>(F->get()->base_data);
  2191. ins->gi_probe_instances.write[l++] = gi_probe2->probe_instance;
  2192. }
  2193. geom->gi_probes_dirty = false;
  2194. }
  2195. instance_cull_result[instance_cull_count++] = E->get().geometry;
  2196. }
  2197. }
  2198. scene_render->gi_probe_update(probe->probe_instance, update_lights, probe->light_instances, instance_cull_count, (RendererSceneRender::InstanceBase **)instance_cull_result);
  2199. gi_probe_update_list.remove(gi_probe);
  2200. gi_probe = next;
  2201. }
  2202. }
  2203. void RendererSceneCull::render_particle_colliders() {
  2204. while (heightfield_particle_colliders_update_list.front()) {
  2205. Instance *hfpc = heightfield_particle_colliders_update_list.front()->get();
  2206. if (hfpc->scenario && hfpc->base_type == RS::INSTANCE_PARTICLES_COLLISION && RSG::storage->particles_collision_is_heightfield(hfpc->base)) {
  2207. //update heightfield
  2208. int cull_count = hfpc->scenario->octree.cull_aabb(hfpc->transformed_aabb, instance_cull_result, MAX_INSTANCE_CULL); //@TODO: cull mask missing
  2209. for (int i = 0; i < cull_count; i++) {
  2210. Instance *instance = instance_cull_result[i];
  2211. if (!instance->visible || !((1 << instance->base_type) & (RS::INSTANCE_GEOMETRY_MASK & (~(1 << RS::INSTANCE_PARTICLES))))) { //all but particles to avoid self collision
  2212. cull_count--;
  2213. SWAP(instance_cull_result[i], instance_cull_result[cull_count]);
  2214. }
  2215. }
  2216. scene_render->render_particle_collider_heightfield(hfpc->base, hfpc->transform, (RendererSceneRender::InstanceBase **)instance_cull_result, cull_count);
  2217. }
  2218. heightfield_particle_colliders_update_list.erase(heightfield_particle_colliders_update_list.front());
  2219. }
  2220. }
  2221. void RendererSceneCull::_update_instance_shader_parameters_from_material(Map<StringName, RendererSceneRender::InstanceBase::InstanceShaderParameter> &isparams, const Map<StringName, RendererSceneRender::InstanceBase::InstanceShaderParameter> &existing_isparams, RID p_material) {
  2222. List<RendererStorage::InstanceShaderParam> plist;
  2223. RSG::storage->material_get_instance_shader_parameters(p_material, &plist);
  2224. for (List<RendererStorage::InstanceShaderParam>::Element *E = plist.front(); E; E = E->next()) {
  2225. StringName name = E->get().info.name;
  2226. if (isparams.has(name)) {
  2227. if (isparams[name].info.type != E->get().info.type) {
  2228. WARN_PRINT("More than one material in instance export the same instance shader uniform '" + E->get().info.name + "', but they do it with different data types. Only the first one (in order) will display correctly.");
  2229. }
  2230. if (isparams[name].index != E->get().index) {
  2231. WARN_PRINT("More than one material in instance export the same instance shader uniform '" + E->get().info.name + "', but they do it with different indices. Only the first one (in order) will display correctly.");
  2232. }
  2233. continue; //first one found always has priority
  2234. }
  2235. RendererSceneRender::InstanceBase::InstanceShaderParameter isp;
  2236. isp.index = E->get().index;
  2237. isp.info = E->get().info;
  2238. isp.default_value = E->get().default_value;
  2239. if (existing_isparams.has(name)) {
  2240. isp.value = existing_isparams[name].value;
  2241. } else {
  2242. isp.value = E->get().default_value;
  2243. }
  2244. isparams[name] = isp;
  2245. }
  2246. }
  2247. void RendererSceneCull::_update_dirty_instance(Instance *p_instance) {
  2248. if (p_instance->update_aabb) {
  2249. _update_instance_aabb(p_instance);
  2250. }
  2251. if (p_instance->update_dependencies) {
  2252. p_instance->instance_increase_version();
  2253. if (p_instance->base.is_valid()) {
  2254. RSG::storage->base_update_dependency(p_instance->base, p_instance);
  2255. }
  2256. if (p_instance->material_override.is_valid()) {
  2257. RSG::storage->material_update_dependency(p_instance->material_override, p_instance);
  2258. }
  2259. if (p_instance->base_type == RS::INSTANCE_MESH) {
  2260. //remove materials no longer used and un-own them
  2261. int new_mat_count = RSG::storage->mesh_get_surface_count(p_instance->base);
  2262. p_instance->materials.resize(new_mat_count);
  2263. _instance_update_mesh_instance(p_instance);
  2264. }
  2265. if ((1 << p_instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) {
  2266. InstanceGeometryData *geom = static_cast<InstanceGeometryData *>(p_instance->base_data);
  2267. bool can_cast_shadows = true;
  2268. bool is_animated = false;
  2269. Map<StringName, RendererSceneRender::InstanceBase::InstanceShaderParameter> isparams;
  2270. if (p_instance->cast_shadows == RS::SHADOW_CASTING_SETTING_OFF) {
  2271. can_cast_shadows = false;
  2272. }
  2273. if (p_instance->material_override.is_valid()) {
  2274. if (!RSG::storage->material_casts_shadows(p_instance->material_override)) {
  2275. can_cast_shadows = false;
  2276. }
  2277. is_animated = RSG::storage->material_is_animated(p_instance->material_override);
  2278. _update_instance_shader_parameters_from_material(isparams, p_instance->instance_shader_parameters, p_instance->material_override);
  2279. } else {
  2280. if (p_instance->base_type == RS::INSTANCE_MESH) {
  2281. RID mesh = p_instance->base;
  2282. if (mesh.is_valid()) {
  2283. bool cast_shadows = false;
  2284. for (int i = 0; i < p_instance->materials.size(); i++) {
  2285. RID mat = p_instance->materials[i].is_valid() ? p_instance->materials[i] : RSG::storage->mesh_surface_get_material(mesh, i);
  2286. if (!mat.is_valid()) {
  2287. cast_shadows = true;
  2288. } else {
  2289. if (RSG::storage->material_casts_shadows(mat)) {
  2290. cast_shadows = true;
  2291. }
  2292. if (RSG::storage->material_is_animated(mat)) {
  2293. is_animated = true;
  2294. }
  2295. _update_instance_shader_parameters_from_material(isparams, p_instance->instance_shader_parameters, mat);
  2296. RSG::storage->material_update_dependency(mat, p_instance);
  2297. }
  2298. }
  2299. if (!cast_shadows) {
  2300. can_cast_shadows = false;
  2301. }
  2302. }
  2303. } else if (p_instance->base_type == RS::INSTANCE_MULTIMESH) {
  2304. RID mesh = RSG::storage->multimesh_get_mesh(p_instance->base);
  2305. if (mesh.is_valid()) {
  2306. bool cast_shadows = false;
  2307. int sc = RSG::storage->mesh_get_surface_count(mesh);
  2308. for (int i = 0; i < sc; i++) {
  2309. RID mat = RSG::storage->mesh_surface_get_material(mesh, i);
  2310. if (!mat.is_valid()) {
  2311. cast_shadows = true;
  2312. } else {
  2313. if (RSG::storage->material_casts_shadows(mat)) {
  2314. cast_shadows = true;
  2315. }
  2316. if (RSG::storage->material_is_animated(mat)) {
  2317. is_animated = true;
  2318. }
  2319. _update_instance_shader_parameters_from_material(isparams, p_instance->instance_shader_parameters, mat);
  2320. RSG::storage->material_update_dependency(mat, p_instance);
  2321. }
  2322. }
  2323. if (!cast_shadows) {
  2324. can_cast_shadows = false;
  2325. }
  2326. RSG::storage->base_update_dependency(mesh, p_instance);
  2327. }
  2328. } else if (p_instance->base_type == RS::INSTANCE_IMMEDIATE) {
  2329. RID mat = RSG::storage->immediate_get_material(p_instance->base);
  2330. if (!(!mat.is_valid() || RSG::storage->material_casts_shadows(mat))) {
  2331. can_cast_shadows = false;
  2332. }
  2333. if (mat.is_valid() && RSG::storage->material_is_animated(mat)) {
  2334. is_animated = true;
  2335. }
  2336. if (mat.is_valid()) {
  2337. _update_instance_shader_parameters_from_material(isparams, p_instance->instance_shader_parameters, mat);
  2338. }
  2339. if (mat.is_valid()) {
  2340. RSG::storage->material_update_dependency(mat, p_instance);
  2341. }
  2342. } else if (p_instance->base_type == RS::INSTANCE_PARTICLES) {
  2343. bool cast_shadows = false;
  2344. int dp = RSG::storage->particles_get_draw_passes(p_instance->base);
  2345. for (int i = 0; i < dp; i++) {
  2346. RID mesh = RSG::storage->particles_get_draw_pass_mesh(p_instance->base, i);
  2347. if (!mesh.is_valid()) {
  2348. continue;
  2349. }
  2350. int sc = RSG::storage->mesh_get_surface_count(mesh);
  2351. for (int j = 0; j < sc; j++) {
  2352. RID mat = RSG::storage->mesh_surface_get_material(mesh, j);
  2353. if (!mat.is_valid()) {
  2354. cast_shadows = true;
  2355. } else {
  2356. if (RSG::storage->material_casts_shadows(mat)) {
  2357. cast_shadows = true;
  2358. }
  2359. if (RSG::storage->material_is_animated(mat)) {
  2360. is_animated = true;
  2361. }
  2362. _update_instance_shader_parameters_from_material(isparams, p_instance->instance_shader_parameters, mat);
  2363. RSG::storage->material_update_dependency(mat, p_instance);
  2364. }
  2365. }
  2366. }
  2367. if (!cast_shadows) {
  2368. can_cast_shadows = false;
  2369. }
  2370. }
  2371. }
  2372. if (can_cast_shadows != geom->can_cast_shadows) {
  2373. //ability to cast shadows change, let lights now
  2374. for (List<Instance *>::Element *E = geom->lighting.front(); E; E = E->next()) {
  2375. InstanceLightData *light = static_cast<InstanceLightData *>(E->get()->base_data);
  2376. light->shadow_dirty = true;
  2377. }
  2378. geom->can_cast_shadows = can_cast_shadows;
  2379. }
  2380. geom->material_is_animated = is_animated;
  2381. p_instance->instance_shader_parameters = isparams;
  2382. if (p_instance->instance_allocated_shader_parameters != (p_instance->instance_shader_parameters.size() > 0)) {
  2383. p_instance->instance_allocated_shader_parameters = (p_instance->instance_shader_parameters.size() > 0);
  2384. if (p_instance->instance_allocated_shader_parameters) {
  2385. p_instance->instance_allocated_shader_parameters_offset = RSG::storage->global_variables_instance_allocate(p_instance->self);
  2386. for (Map<StringName, RendererSceneRender::InstanceBase::InstanceShaderParameter>::Element *E = p_instance->instance_shader_parameters.front(); E; E = E->next()) {
  2387. if (E->get().value.get_type() != Variant::NIL) {
  2388. RSG::storage->global_variables_instance_update(p_instance->self, E->get().index, E->get().value);
  2389. }
  2390. }
  2391. } else {
  2392. RSG::storage->global_variables_instance_free(p_instance->self);
  2393. p_instance->instance_allocated_shader_parameters_offset = -1;
  2394. }
  2395. }
  2396. }
  2397. if (p_instance->skeleton.is_valid()) {
  2398. RSG::storage->skeleton_update_dependency(p_instance->skeleton, p_instance);
  2399. }
  2400. p_instance->clean_up_dependencies();
  2401. }
  2402. _instance_update_list.remove(&p_instance->update_item);
  2403. _update_instance(p_instance);
  2404. p_instance->update_aabb = false;
  2405. p_instance->update_dependencies = false;
  2406. }
  2407. void RendererSceneCull::update_dirty_instances() {
  2408. RSG::storage->update_dirty_resources();
  2409. while (_instance_update_list.first()) {
  2410. _update_dirty_instance(_instance_update_list.first()->self());
  2411. }
  2412. }
  2413. void RendererSceneCull::update() {
  2414. scene_render->update();
  2415. update_dirty_instances();
  2416. render_particle_colliders();
  2417. }
  2418. bool RendererSceneCull::free(RID p_rid) {
  2419. if (scene_render->free(p_rid)) {
  2420. return true;
  2421. }
  2422. if (camera_owner.owns(p_rid)) {
  2423. Camera *camera = camera_owner.getornull(p_rid);
  2424. camera_owner.free(p_rid);
  2425. memdelete(camera);
  2426. } else if (scenario_owner.owns(p_rid)) {
  2427. Scenario *scenario = scenario_owner.getornull(p_rid);
  2428. while (scenario->instances.first()) {
  2429. instance_set_scenario(scenario->instances.first()->self()->self, RID());
  2430. }
  2431. scene_render->free(scenario->reflection_probe_shadow_atlas);
  2432. scene_render->free(scenario->reflection_atlas);
  2433. scenario_owner.free(p_rid);
  2434. memdelete(scenario);
  2435. } else if (instance_owner.owns(p_rid)) {
  2436. // delete the instance
  2437. update_dirty_instances();
  2438. Instance *instance = instance_owner.getornull(p_rid);
  2439. instance_geometry_set_lightmap(p_rid, RID(), Rect2(), 0);
  2440. instance_set_scenario(p_rid, RID());
  2441. instance_set_base(p_rid, RID());
  2442. instance_geometry_set_material_override(p_rid, RID());
  2443. instance_attach_skeleton(p_rid, RID());
  2444. if (instance->instance_allocated_shader_parameters) {
  2445. //free the used shader parameters
  2446. RSG::storage->global_variables_instance_free(instance->self);
  2447. }
  2448. update_dirty_instances(); //in case something changed this
  2449. instance_owner.free(p_rid);
  2450. memdelete(instance);
  2451. } else {
  2452. return false;
  2453. }
  2454. return true;
  2455. }
  2456. TypedArray<Image> RendererSceneCull::bake_render_uv2(RID p_base, const Vector<RID> &p_material_overrides, const Size2i &p_image_size) {
  2457. return scene_render->bake_render_uv2(p_base, p_material_overrides, p_image_size);
  2458. }
  2459. /*******************************/
  2460. /* Passthrough to Scene Render */
  2461. /*******************************/
  2462. /* ENVIRONMENT API */
  2463. RendererSceneCull *RendererSceneCull::singleton = nullptr;
  2464. RendererSceneCull::RendererSceneCull() {
  2465. render_pass = 1;
  2466. singleton = this;
  2467. }
  2468. RendererSceneCull::~RendererSceneCull() {
  2469. }