2023-01-05 12:25:55 +00:00
/**************************************************************************/
/* renderer_scene_cull.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
2020-12-03 21:09:47 +00:00
2020-12-04 18:26:24 +00:00
# include "renderer_scene_cull.h"
2020-12-03 21:09:47 +00:00
2020-12-23 16:52:58 +00:00
# include "core/config/project_settings.h"
2023-02-13 18:45:06 +00:00
# include "core/object/worker_thread_pool.h"
2020-12-03 21:09:47 +00:00
# include "core/os/os.h"
# include "rendering_server_default.h"
# include <new>
/* CAMERA API */
2021-02-09 16:19:03 +00:00
RID RendererSceneCull : : camera_allocate ( ) {
return camera_owner . allocate_rid ( ) ;
}
void RendererSceneCull : : camera_initialize ( RID p_rid ) {
2021-06-29 13:58:28 +00:00
camera_owner . initialize_rid ( p_rid ) ;
2020-12-03 21:09:47 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : camera_set_perspective ( RID p_camera , float p_fovy_degrees , float p_z_near , float p_z_far ) {
2021-09-29 17:08:41 +00:00
Camera * camera = camera_owner . get_or_null ( p_camera ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! camera ) ;
camera - > type = Camera : : PERSPECTIVE ;
camera - > fov = p_fovy_degrees ;
camera - > znear = p_z_near ;
camera - > zfar = p_z_far ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : camera_set_orthogonal ( RID p_camera , float p_size , float p_z_near , float p_z_far ) {
2021-09-29 17:08:41 +00:00
Camera * camera = camera_owner . get_or_null ( p_camera ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! camera ) ;
camera - > type = Camera : : ORTHOGONAL ;
camera - > size = p_size ;
camera - > znear = p_z_near ;
camera - > zfar = p_z_far ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : camera_set_frustum ( RID p_camera , float p_size , Vector2 p_offset , float p_z_near , float p_z_far ) {
2021-09-29 17:08:41 +00:00
Camera * camera = camera_owner . get_or_null ( p_camera ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! camera ) ;
camera - > type = Camera : : FRUSTUM ;
camera - > size = p_size ;
camera - > offset = p_offset ;
camera - > znear = p_z_near ;
camera - > zfar = p_z_far ;
}
2020-10-17 05:08:21 +00:00
void RendererSceneCull : : camera_set_transform ( RID p_camera , const Transform3D & p_transform ) {
2021-09-29 17:08:41 +00:00
Camera * camera = camera_owner . get_or_null ( p_camera ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! camera ) ;
camera - > transform = p_transform . orthonormalized ( ) ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : camera_set_cull_mask ( RID p_camera , uint32_t p_layers ) {
2021-09-29 17:08:41 +00:00
Camera * camera = camera_owner . get_or_null ( p_camera ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! camera ) ;
camera - > visible_layers = p_layers ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : camera_set_environment ( RID p_camera , RID p_env ) {
2021-09-29 17:08:41 +00:00
Camera * camera = camera_owner . get_or_null ( p_camera ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! camera ) ;
camera - > env = p_env ;
}
2022-07-31 23:20:24 +00:00
void RendererSceneCull : : camera_set_camera_attributes ( RID p_camera , RID p_attributes ) {
2021-09-29 17:08:41 +00:00
Camera * camera = camera_owner . get_or_null ( p_camera ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! camera ) ;
2022-07-31 23:20:24 +00:00
camera - > attributes = p_attributes ;
2020-12-03 21:09:47 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : camera_set_use_vertical_aspect ( RID p_camera , bool p_enable ) {
2021-09-29 17:08:41 +00:00
Camera * camera = camera_owner . get_or_null ( p_camera ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! camera ) ;
camera - > vaspect = p_enable ;
}
2020-12-04 18:26:24 +00:00
bool RendererSceneCull : : is_camera ( RID p_camera ) const {
2020-12-03 21:09:47 +00:00
return camera_owner . owns ( p_camera ) ;
}
2021-04-20 16:40:24 +00:00
/* OCCLUDER API */
RID RendererSceneCull : : occluder_allocate ( ) {
return RendererSceneOcclusionCull : : get_singleton ( ) - > occluder_allocate ( ) ;
}
void RendererSceneCull : : occluder_initialize ( RID p_rid ) {
RendererSceneOcclusionCull : : get_singleton ( ) - > occluder_initialize ( p_rid ) ;
}
void RendererSceneCull : : occluder_set_mesh ( RID p_occluder , const PackedVector3Array & p_vertices , const PackedInt32Array & p_indices ) {
RendererSceneOcclusionCull : : get_singleton ( ) - > occluder_set_mesh ( p_occluder , p_vertices , p_indices ) ;
}
2020-12-03 21:09:47 +00:00
/* SCENARIO API */
2020-12-23 16:52:58 +00:00
void RendererSceneCull : : _instance_pair ( Instance * p_A , Instance * p_B ) {
RendererSceneCull * self = ( RendererSceneCull * ) singleton ;
2020-12-03 21:09:47 +00:00
Instance * A = p_A ;
Instance * B = p_B ;
//instance indices are designed so greater always contains lesser
if ( A - > base_type > B - > base_type ) {
SWAP ( A , B ) ; //lesser always first
}
if ( B - > base_type = = RS : : INSTANCE_LIGHT & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
InstanceLightData * light = static_cast < InstanceLightData * > ( B - > base_data ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
2020-12-23 16:52:58 +00:00
geom - > lights . insert ( B ) ;
light - > geometries . insert ( A ) ;
2020-12-03 21:09:47 +00:00
if ( geom - > can_cast_shadows ) {
light - > shadow_dirty = true ;
}
2020-12-26 12:05:36 +00:00
if ( A - > scenario & & A - > array_index > = 0 ) {
InstanceData & idata = A - > scenario - > instance_data [ A - > array_index ] ;
idata . flags | = InstanceData : : FLAG_GEOM_LIGHTING_DIRTY ;
}
2020-12-03 21:09:47 +00:00
2021-07-12 23:32:05 +00:00
if ( light - > uses_projector ) {
geom - > projector_count + + ;
if ( geom - > projector_count = = 1 ) {
InstanceData & idata = A - > scenario - > instance_data [ A - > array_index ] ;
idata . flags | = InstanceData : : FLAG_GEOM_PROJECTOR_SOFTSHADOW_DIRTY ;
}
}
if ( light - > uses_softshadow ) {
geom - > softshadow_count + + ;
if ( geom - > softshadow_count = = 1 ) {
InstanceData & idata = A - > scenario - > instance_data [ A - > array_index ] ;
idata . flags | = InstanceData : : FLAG_GEOM_PROJECTOR_SOFTSHADOW_DIRTY ;
}
}
2020-12-31 12:42:56 +00:00
} else if ( self - > geometry_instance_pair_mask & ( 1 < < RS : : INSTANCE_REFLECTION_PROBE ) & & B - > base_type = = RS : : INSTANCE_REFLECTION_PROBE & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
2020-12-03 21:09:47 +00:00
InstanceReflectionProbeData * reflection_probe = static_cast < InstanceReflectionProbeData * > ( B - > base_data ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
2020-12-23 16:52:58 +00:00
geom - > reflection_probes . insert ( B ) ;
reflection_probe - > geometries . insert ( A ) ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
if ( A - > scenario & & A - > array_index > = 0 ) {
InstanceData & idata = A - > scenario - > instance_data [ A - > array_index ] ;
idata . flags | = InstanceData : : FLAG_GEOM_REFLECTION_DIRTY ;
}
2020-12-03 21:09:47 +00:00
2020-12-31 12:42:56 +00:00
} else if ( self - > geometry_instance_pair_mask & ( 1 < < RS : : INSTANCE_DECAL ) & & B - > base_type = = RS : : INSTANCE_DECAL & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
2020-12-03 21:09:47 +00:00
InstanceDecalData * decal = static_cast < InstanceDecalData * > ( B - > base_data ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
2020-12-23 16:52:58 +00:00
geom - > decals . insert ( B ) ;
decal - > geometries . insert ( A ) ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
if ( A - > scenario & & A - > array_index > = 0 ) {
InstanceData & idata = A - > scenario - > instance_data [ A - > array_index ] ;
idata . flags | = InstanceData : : FLAG_GEOM_DECAL_DIRTY ;
}
2020-12-03 21:09:47 +00:00
} else if ( B - > base_type = = RS : : INSTANCE_LIGHTMAP & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
InstanceLightmapData * lightmap_data = static_cast < InstanceLightmapData * > ( B - > base_data ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
if ( A - > dynamic_gi ) {
2023-05-15 05:52:57 +00:00
geom - > lightmap_captures . insert ( B ) ;
lightmap_data - > geometries . insert ( A ) ;
2020-12-26 12:05:36 +00:00
if ( A - > scenario & & A - > array_index > = 0 ) {
InstanceData & idata = A - > scenario - > instance_data [ A - > array_index ] ;
idata . flags | = InstanceData : : FLAG_LIGHTMAP_CAPTURE ;
}
2020-12-23 16:52:58 +00:00
( ( RendererSceneCull * ) self ) - > _instance_queue_update ( A , false , false ) ; //need to update capture
2020-12-03 21:09:47 +00:00
}
2021-06-04 22:47:26 +00:00
} else if ( self - > geometry_instance_pair_mask & ( 1 < < RS : : INSTANCE_VOXEL_GI ) & & B - > base_type = = RS : : INSTANCE_VOXEL_GI & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
InstanceVoxelGIData * voxel_gi = static_cast < InstanceVoxelGIData * > ( B - > base_data ) ;
2020-12-03 21:09:47 +00:00
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
2021-06-04 22:47:26 +00:00
geom - > voxel_gi_instances . insert ( B ) ;
2020-12-03 21:09:47 +00:00
if ( A - > dynamic_gi ) {
2021-06-04 22:47:26 +00:00
voxel_gi - > dynamic_geometries . insert ( A ) ;
2020-12-03 21:09:47 +00:00
} else {
2021-06-04 22:47:26 +00:00
voxel_gi - > geometries . insert ( A ) ;
2020-12-03 21:09:47 +00:00
}
2020-12-26 12:05:36 +00:00
if ( A - > scenario & & A - > array_index > = 0 ) {
InstanceData & idata = A - > scenario - > instance_data [ A - > array_index ] ;
2021-06-04 22:47:26 +00:00
idata . flags | = InstanceData : : FLAG_GEOM_VOXEL_GI_DIRTY ;
2020-12-26 12:05:36 +00:00
}
2020-12-03 21:09:47 +00:00
2021-06-04 22:47:26 +00:00
} else if ( B - > base_type = = RS : : INSTANCE_VOXEL_GI & & A - > base_type = = RS : : INSTANCE_LIGHT ) {
InstanceVoxelGIData * voxel_gi = static_cast < InstanceVoxelGIData * > ( B - > base_data ) ;
voxel_gi - > lights . insert ( A ) ;
2020-12-03 21:09:47 +00:00
} else if ( B - > base_type = = RS : : INSTANCE_PARTICLES_COLLISION & & A - > base_type = = RS : : INSTANCE_PARTICLES ) {
2020-12-31 12:42:56 +00:00
InstanceParticlesCollisionData * collision = static_cast < InstanceParticlesCollisionData * > ( B - > base_data ) ;
2022-04-12 11:41:50 +00:00
RSG : : particles_storage - > particles_add_collision ( A - > base , collision - > instance ) ;
2020-12-03 21:09:47 +00:00
}
}
2020-12-23 16:52:58 +00:00
void RendererSceneCull : : _instance_unpair ( Instance * p_A , Instance * p_B ) {
RendererSceneCull * self = ( RendererSceneCull * ) singleton ;
2020-12-03 21:09:47 +00:00
Instance * A = p_A ;
Instance * B = p_B ;
//instance indices are designed so greater always contains lesser
if ( A - > base_type > B - > base_type ) {
SWAP ( A , B ) ; //lesser always first
}
if ( B - > base_type = = RS : : INSTANCE_LIGHT & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
InstanceLightData * light = static_cast < InstanceLightData * > ( B - > base_data ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
2020-12-23 16:52:58 +00:00
geom - > lights . erase ( B ) ;
light - > geometries . erase ( A ) ;
2020-12-03 21:09:47 +00:00
if ( geom - > can_cast_shadows ) {
light - > shadow_dirty = true ;
}
2020-12-26 12:05:36 +00:00
if ( A - > scenario & & A - > array_index > = 0 ) {
InstanceData & idata = A - > scenario - > instance_data [ A - > array_index ] ;
idata . flags | = InstanceData : : FLAG_GEOM_LIGHTING_DIRTY ;
}
2020-12-03 21:09:47 +00:00
2021-07-12 23:32:05 +00:00
if ( light - > uses_projector ) {
# ifdef DEBUG_ENABLED
if ( geom - > projector_count = = 0 ) {
ERR_PRINT ( " geom->projector_count==0 - BUG! " ) ;
}
# endif
geom - > projector_count - - ;
if ( geom - > projector_count = = 0 ) {
InstanceData & idata = A - > scenario - > instance_data [ A - > array_index ] ;
idata . flags | = InstanceData : : FLAG_GEOM_PROJECTOR_SOFTSHADOW_DIRTY ;
}
}
if ( light - > uses_softshadow ) {
# ifdef DEBUG_ENABLED
if ( geom - > softshadow_count = = 0 ) {
ERR_PRINT ( " geom->softshadow_count==0 - BUG! " ) ;
}
# endif
geom - > softshadow_count - - ;
if ( geom - > softshadow_count = = 0 ) {
InstanceData & idata = A - > scenario - > instance_data [ A - > array_index ] ;
idata . flags | = InstanceData : : FLAG_GEOM_PROJECTOR_SOFTSHADOW_DIRTY ;
}
}
2020-12-31 12:42:56 +00:00
} else if ( self - > geometry_instance_pair_mask & ( 1 < < RS : : INSTANCE_REFLECTION_PROBE ) & & B - > base_type = = RS : : INSTANCE_REFLECTION_PROBE & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
2020-12-03 21:09:47 +00:00
InstanceReflectionProbeData * reflection_probe = static_cast < InstanceReflectionProbeData * > ( B - > base_data ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
2020-12-23 16:52:58 +00:00
geom - > reflection_probes . erase ( B ) ;
reflection_probe - > geometries . erase ( A ) ;
2020-12-26 12:05:36 +00:00
if ( A - > scenario & & A - > array_index > = 0 ) {
InstanceData & idata = A - > scenario - > instance_data [ A - > array_index ] ;
idata . flags | = InstanceData : : FLAG_GEOM_REFLECTION_DIRTY ;
}
2020-12-23 16:52:58 +00:00
2020-12-31 12:42:56 +00:00
} else if ( self - > geometry_instance_pair_mask & ( 1 < < RS : : INSTANCE_DECAL ) & & B - > base_type = = RS : : INSTANCE_DECAL & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
2020-12-03 21:09:47 +00:00
InstanceDecalData * decal = static_cast < InstanceDecalData * > ( B - > base_data ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
2020-12-23 16:52:58 +00:00
geom - > decals . erase ( B ) ;
decal - > geometries . erase ( A ) ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
if ( A - > scenario & & A - > array_index > = 0 ) {
InstanceData & idata = A - > scenario - > instance_data [ A - > array_index ] ;
idata . flags | = InstanceData : : FLAG_GEOM_DECAL_DIRTY ;
}
2020-12-03 21:09:47 +00:00
} else if ( B - > base_type = = RS : : INSTANCE_LIGHTMAP & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
2020-12-23 16:52:58 +00:00
InstanceLightmapData * lightmap_data = static_cast < InstanceLightmapData * > ( B - > base_data ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
if ( A - > dynamic_gi ) {
geom - > lightmap_captures . erase ( B ) ;
2020-12-26 12:05:36 +00:00
2020-12-15 12:04:21 +00:00
if ( geom - > lightmap_captures . is_empty ( ) & & A - > scenario & & A - > array_index > = 0 ) {
2020-12-26 12:05:36 +00:00
InstanceData & idata = A - > scenario - > instance_data [ A - > array_index ] ;
idata . flags & = ~ uint32_t ( InstanceData : : FLAG_LIGHTMAP_CAPTURE ) ;
}
2020-12-23 16:52:58 +00:00
lightmap_data - > geometries . erase ( A ) ;
( ( RendererSceneCull * ) self ) - > _instance_queue_update ( A , false , false ) ; //need to update capture
2020-12-03 21:09:47 +00:00
}
2021-06-04 22:47:26 +00:00
} else if ( self - > geometry_instance_pair_mask & ( 1 < < RS : : INSTANCE_VOXEL_GI ) & & B - > base_type = = RS : : INSTANCE_VOXEL_GI & & ( ( 1 < < A - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) ) {
InstanceVoxelGIData * voxel_gi = static_cast < InstanceVoxelGIData * > ( B - > base_data ) ;
2020-12-03 21:09:47 +00:00
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( A - > base_data ) ;
2021-06-04 22:47:26 +00:00
geom - > voxel_gi_instances . erase ( B ) ;
2020-12-03 21:09:47 +00:00
if ( A - > dynamic_gi ) {
2021-06-04 22:47:26 +00:00
voxel_gi - > dynamic_geometries . erase ( A ) ;
2020-12-03 21:09:47 +00:00
} else {
2021-06-04 22:47:26 +00:00
voxel_gi - > geometries . erase ( A ) ;
2020-12-03 21:09:47 +00:00
}
2020-12-26 12:05:36 +00:00
if ( A - > scenario & & A - > array_index > = 0 ) {
InstanceData & idata = A - > scenario - > instance_data [ A - > array_index ] ;
2021-06-04 22:47:26 +00:00
idata . flags | = InstanceData : : FLAG_GEOM_VOXEL_GI_DIRTY ;
2020-12-26 12:05:36 +00:00
}
2020-12-03 21:09:47 +00:00
2021-06-04 22:47:26 +00:00
} else if ( B - > base_type = = RS : : INSTANCE_VOXEL_GI & & A - > base_type = = RS : : INSTANCE_LIGHT ) {
InstanceVoxelGIData * voxel_gi = static_cast < InstanceVoxelGIData * > ( B - > base_data ) ;
voxel_gi - > lights . erase ( A ) ;
2020-12-03 21:09:47 +00:00
} else if ( B - > base_type = = RS : : INSTANCE_PARTICLES_COLLISION & & A - > base_type = = RS : : INSTANCE_PARTICLES ) {
2020-12-31 12:42:56 +00:00
InstanceParticlesCollisionData * collision = static_cast < InstanceParticlesCollisionData * > ( B - > base_data ) ;
2022-04-12 11:41:50 +00:00
RSG : : particles_storage - > particles_remove_collision ( A - > base , collision - > instance ) ;
2020-12-03 21:09:47 +00:00
}
}
2021-02-09 16:19:03 +00:00
RID RendererSceneCull : : scenario_allocate ( ) {
return scenario_owner . allocate_rid ( ) ;
}
void RendererSceneCull : : scenario_initialize ( RID p_rid ) {
2021-06-29 13:58:28 +00:00
scenario_owner . initialize_rid ( p_rid ) ;
2021-09-29 17:08:41 +00:00
Scenario * scenario = scenario_owner . get_or_null ( p_rid ) ;
2021-02-09 16:19:03 +00:00
scenario - > self = p_rid ;
2020-12-03 21:09:47 +00:00
2022-09-12 09:44:48 +00:00
scenario - > reflection_probe_shadow_atlas = RSG : : light_storage - > shadow_atlas_create ( ) ;
RSG : : light_storage - > shadow_atlas_set_size ( scenario - > reflection_probe_shadow_atlas , 1024 ) ; //make enough shadows for close distance, don't bother with rest
RSG : : light_storage - > shadow_atlas_set_quadrant_subdivision ( scenario - > reflection_probe_shadow_atlas , 0 , 4 ) ;
RSG : : light_storage - > shadow_atlas_set_quadrant_subdivision ( scenario - > reflection_probe_shadow_atlas , 1 , 4 ) ;
RSG : : light_storage - > shadow_atlas_set_quadrant_subdivision ( scenario - > reflection_probe_shadow_atlas , 2 , 4 ) ;
RSG : : light_storage - > shadow_atlas_set_quadrant_subdivision ( scenario - > reflection_probe_shadow_atlas , 3 , 8 ) ;
scenario - > reflection_atlas = RSG : : light_storage - > reflection_atlas_create ( ) ;
2020-12-26 12:05:36 +00:00
scenario - > instance_aabbs . set_page_pool ( & instance_aabb_page_pool ) ;
scenario - > instance_data . set_page_pool ( & instance_data_page_pool ) ;
2021-05-09 16:23:20 +00:00
scenario - > instance_visibility . set_page_pool ( & instance_visibility_data_page_pool ) ;
2020-12-26 12:05:36 +00:00
2021-04-20 16:40:24 +00:00
RendererSceneOcclusionCull : : get_singleton ( ) - > add_scenario ( p_rid ) ;
2020-12-03 21:09:47 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : scenario_set_environment ( RID p_scenario , RID p_environment ) {
2021-09-29 17:08:41 +00:00
Scenario * scenario = scenario_owner . get_or_null ( p_scenario ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! scenario ) ;
scenario - > environment = p_environment ;
}
2022-07-31 23:20:24 +00:00
void RendererSceneCull : : scenario_set_camera_attributes ( RID p_scenario , RID p_camera_attributes ) {
2021-09-29 17:08:41 +00:00
Scenario * scenario = scenario_owner . get_or_null ( p_scenario ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! scenario ) ;
2022-07-31 23:20:24 +00:00
scenario - > camera_attributes = p_camera_attributes ;
2020-12-03 21:09:47 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : scenario_set_fallback_environment ( RID p_scenario , RID p_environment ) {
2021-09-29 17:08:41 +00:00
Scenario * scenario = scenario_owner . get_or_null ( p_scenario ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! scenario ) ;
scenario - > fallback_environment = p_environment ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : scenario_set_reflection_atlas_size ( RID p_scenario , int p_reflection_size , int p_reflection_count ) {
2021-09-29 17:08:41 +00:00
Scenario * scenario = scenario_owner . get_or_null ( p_scenario ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! scenario ) ;
2022-09-12 09:44:48 +00:00
RSG : : light_storage - > reflection_atlas_set_size ( scenario - > reflection_atlas , p_reflection_size , p_reflection_count ) ;
2020-12-03 21:09:47 +00:00
}
2020-12-04 18:26:24 +00:00
bool RendererSceneCull : : is_scenario ( RID p_scenario ) const {
2020-12-03 21:09:47 +00:00
return scenario_owner . owns ( p_scenario ) ;
}
2020-12-04 18:26:24 +00:00
RID RendererSceneCull : : scenario_get_environment ( RID p_scenario ) {
2021-09-29 17:08:41 +00:00
Scenario * scenario = scenario_owner . get_or_null ( p_scenario ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND_V ( ! scenario , RID ( ) ) ;
return scenario - > environment ;
}
2021-05-09 16:23:20 +00:00
void RendererSceneCull : : scenario_remove_viewport_visibility_mask ( RID p_scenario , RID p_viewport ) {
2021-09-29 17:08:41 +00:00
Scenario * scenario = scenario_owner . get_or_null ( p_scenario ) ;
2021-05-09 16:23:20 +00:00
ERR_FAIL_COND ( ! scenario ) ;
if ( ! scenario - > viewport_visibility_masks . has ( p_viewport ) ) {
return ;
}
uint64_t mask = scenario - > viewport_visibility_masks [ p_viewport ] ;
scenario - > used_viewport_visibility_bits & = ~ mask ;
scenario - > viewport_visibility_masks . erase ( p_viewport ) ;
}
void RendererSceneCull : : scenario_add_viewport_visibility_mask ( RID p_scenario , RID p_viewport ) {
2021-09-29 17:08:41 +00:00
Scenario * scenario = scenario_owner . get_or_null ( p_scenario ) ;
2021-05-09 16:23:20 +00:00
ERR_FAIL_COND ( ! scenario ) ;
ERR_FAIL_COND ( scenario - > viewport_visibility_masks . has ( p_viewport ) ) ;
uint64_t new_mask = 1 ;
while ( new_mask & scenario - > used_viewport_visibility_bits ) {
new_mask < < = 1 ;
}
if ( new_mask = = 0 ) {
ERR_PRINT ( " Only 64 viewports per scenario allowed when using visibility ranges. " ) ;
new_mask = ( ( uint64_t ) 1 ) < < 63 ;
}
scenario - > viewport_visibility_masks [ p_viewport ] = new_mask ;
scenario - > used_viewport_visibility_bits | = new_mask ;
}
2020-12-03 21:09:47 +00:00
/* INSTANCING API */
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : _instance_queue_update ( Instance * p_instance , bool p_update_aabb , bool p_update_dependencies ) {
2020-12-03 21:09:47 +00:00
if ( p_update_aabb ) {
p_instance - > update_aabb = true ;
}
if ( p_update_dependencies ) {
p_instance - > update_dependencies = true ;
}
if ( p_instance - > update_item . in_list ( ) ) {
return ;
}
_instance_update_list . add ( & p_instance - > update_item ) ;
}
2021-02-09 16:19:03 +00:00
RID RendererSceneCull : : instance_allocate ( ) {
return instance_owner . allocate_rid ( ) ;
}
void RendererSceneCull : : instance_initialize ( RID p_rid ) {
2021-06-29 13:58:28 +00:00
instance_owner . initialize_rid ( p_rid ) ;
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_rid ) ;
2021-02-09 16:19:03 +00:00
instance - > self = p_rid ;
2020-12-03 21:09:47 +00:00
}
2020-12-16 14:07:08 +00:00
void RendererSceneCull : : _instance_update_mesh_instance ( Instance * p_instance ) {
2022-04-02 05:29:04 +00:00
bool needs_instance = RSG : : mesh_storage - > mesh_needs_instance ( p_instance - > base , p_instance - > skeleton . is_valid ( ) ) ;
2020-12-16 14:07:08 +00:00
if ( needs_instance ! = p_instance - > mesh_instance . is_valid ( ) ) {
if ( needs_instance ) {
2022-04-02 05:29:04 +00:00
p_instance - > mesh_instance = RSG : : mesh_storage - > mesh_instance_create ( p_instance - > base ) ;
2020-12-26 12:05:36 +00:00
2020-12-16 14:07:08 +00:00
} else {
2022-07-12 12:25:10 +00:00
RSG : : mesh_storage - > mesh_instance_free ( p_instance - > mesh_instance ) ;
2020-12-16 14:07:08 +00:00
p_instance - > mesh_instance = RID ( ) ;
}
2020-12-26 12:05:36 +00:00
2020-12-31 12:42:56 +00:00
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( p_instance - > base_data ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_mesh_instance ( p_instance - > mesh_instance ) ;
2020-12-31 12:42:56 +00:00
2020-12-26 12:05:36 +00:00
if ( p_instance - > scenario & & p_instance - > array_index > = 0 ) {
InstanceData & idata = p_instance - > scenario - > instance_data [ p_instance - > array_index ] ;
if ( p_instance - > mesh_instance . is_valid ( ) ) {
idata . flags | = InstanceData : : FLAG_USES_MESH_INSTANCE ;
} else {
idata . flags & = ~ uint32_t ( InstanceData : : FLAG_USES_MESH_INSTANCE ) ;
}
}
2020-12-16 14:07:08 +00:00
}
if ( p_instance - > mesh_instance . is_valid ( ) ) {
2022-04-02 05:29:04 +00:00
RSG : : mesh_storage - > mesh_instance_set_skeleton ( p_instance - > mesh_instance , p_instance - > skeleton ) ;
2020-12-16 14:07:08 +00:00
}
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : instance_set_base ( RID p_instance , RID p_base ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! instance ) ;
Scenario * scenario = instance - > scenario ;
if ( instance - > base_type ! = RS : : INSTANCE_NONE ) {
//free anything related to that base
2020-12-23 16:52:58 +00:00
if ( scenario & & instance - > indexer_id . is_valid ( ) ) {
_unpair_instance ( instance ) ;
2020-12-03 21:09:47 +00:00
}
2020-12-16 14:07:08 +00:00
if ( instance - > mesh_instance . is_valid ( ) ) {
2022-08-22 20:09:56 +00:00
RSG : : mesh_storage - > mesh_instance_free ( instance - > mesh_instance ) ;
2020-12-16 14:07:08 +00:00
instance - > mesh_instance = RID ( ) ;
2020-12-26 12:05:36 +00:00
// no need to set instance data flag here, as it was freed above
2020-12-16 14:07:08 +00:00
}
2020-12-03 21:09:47 +00:00
switch ( instance - > base_type ) {
2020-12-31 12:42:56 +00:00
case RS : : INSTANCE_MESH :
case RS : : INSTANCE_MULTIMESH :
case RS : : INSTANCE_PARTICLES : {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( instance - > base_data ) ;
scene_render - > geometry_instance_free ( geom - > geometry_instance ) ;
} break ;
2020-12-03 21:09:47 +00:00
case RS : : INSTANCE_LIGHT : {
InstanceLightData * light = static_cast < InstanceLightData * > ( instance - > base_data ) ;
2022-04-09 09:34:31 +00:00
if ( scenario & & instance - > visible & & RSG : : light_storage - > light_get_type ( instance - > base ) ! = RS : : LIGHT_DIRECTIONAL & & light - > bake_mode = = RS : : LIGHT_BAKE_DYNAMIC ) {
2020-12-03 21:09:47 +00:00
scenario - > dynamic_lights . erase ( light - > instance ) ;
}
# ifdef DEBUG_ENABLED
if ( light - > geometries . size ( ) ) {
ERR_PRINT ( " BUG, indexing did not unpair geometries from light. " ) ;
}
# endif
if ( scenario & & light - > D ) {
scenario - > directional_lights . erase ( light - > D ) ;
light - > D = nullptr ;
}
2022-09-12 09:44:48 +00:00
RSG : : light_storage - > light_instance_free ( light - > instance ) ;
2020-12-03 21:09:47 +00:00
} break ;
2020-12-31 12:42:56 +00:00
case RS : : INSTANCE_PARTICLES_COLLISION : {
InstanceParticlesCollisionData * collision = static_cast < InstanceParticlesCollisionData * > ( instance - > base_data ) ;
2022-06-21 00:08:33 +00:00
RSG : : utilities - > free ( collision - > instance ) ;
2020-12-31 12:42:56 +00:00
} break ;
2021-10-03 11:28:55 +00:00
case RS : : INSTANCE_FOG_VOLUME : {
InstanceFogVolumeData * volume = static_cast < InstanceFogVolumeData * > ( instance - > base_data ) ;
scene_render - > free ( volume - > instance ) ;
} break ;
2021-06-16 18:43:02 +00:00
case RS : : INSTANCE_VISIBLITY_NOTIFIER : {
//none
} break ;
2020-12-03 21:09:47 +00:00
case RS : : INSTANCE_REFLECTION_PROBE : {
InstanceReflectionProbeData * reflection_probe = static_cast < InstanceReflectionProbeData * > ( instance - > base_data ) ;
2022-09-12 09:44:48 +00:00
RSG : : light_storage - > reflection_probe_instance_free ( reflection_probe - > instance ) ;
2020-12-03 21:09:47 +00:00
if ( reflection_probe - > update_list . in_list ( ) ) {
reflection_probe_render_list . remove ( & reflection_probe - > update_list ) ;
}
} break ;
case RS : : INSTANCE_DECAL : {
InstanceDecalData * decal = static_cast < InstanceDecalData * > ( instance - > base_data ) ;
2022-09-12 09:44:48 +00:00
RSG : : texture_storage - > decal_instance_free ( decal - > instance ) ;
2020-12-03 21:09:47 +00:00
} break ;
case RS : : INSTANCE_LIGHTMAP : {
InstanceLightmapData * lightmap_data = static_cast < InstanceLightmapData * > ( instance - > base_data ) ;
//erase dependencies, since no longer a lightmap
2022-05-19 15:00:06 +00:00
while ( lightmap_data - > users . begin ( ) ) {
instance_geometry_set_lightmap ( ( * lightmap_data - > users . begin ( ) ) - > self , RID ( ) , Rect2 ( ) , 0 ) ;
2020-12-03 21:09:47 +00:00
}
2022-09-12 09:44:48 +00:00
RSG : : light_storage - > lightmap_instance_free ( lightmap_data - > instance ) ;
2020-12-03 21:09:47 +00:00
} break ;
2021-06-04 22:47:26 +00:00
case RS : : INSTANCE_VOXEL_GI : {
InstanceVoxelGIData * voxel_gi = static_cast < InstanceVoxelGIData * > ( instance - > base_data ) ;
2020-12-03 21:09:47 +00:00
# ifdef DEBUG_ENABLED
2021-06-04 22:47:26 +00:00
if ( voxel_gi - > geometries . size ( ) ) {
ERR_PRINT ( " BUG, indexing did not unpair geometries from VoxelGI. " ) ;
2020-12-03 21:09:47 +00:00
}
# endif
# ifdef DEBUG_ENABLED
2021-06-04 22:47:26 +00:00
if ( voxel_gi - > lights . size ( ) ) {
ERR_PRINT ( " BUG, indexing did not unpair lights from VoxelGI. " ) ;
2020-12-03 21:09:47 +00:00
}
# endif
2021-06-04 22:47:26 +00:00
if ( voxel_gi - > update_element . in_list ( ) ) {
voxel_gi_update_list . remove ( & voxel_gi - > update_element ) ;
2020-12-03 21:09:47 +00:00
}
2021-06-04 22:47:26 +00:00
scene_render - > free ( voxel_gi - > probe_instance ) ;
2020-12-03 21:09:47 +00:00
} break ;
2021-04-20 16:40:24 +00:00
case RS : : INSTANCE_OCCLUDER : {
if ( scenario & & instance - > visible ) {
RendererSceneOcclusionCull : : get_singleton ( ) - > scenario_remove_instance ( instance - > scenario - > self , p_instance ) ;
}
} break ;
2020-12-03 21:09:47 +00:00
default : {
}
}
if ( instance - > base_data ) {
memdelete ( instance - > base_data ) ;
instance - > base_data = nullptr ;
}
instance - > materials . clear ( ) ;
}
instance - > base_type = RS : : INSTANCE_NONE ;
instance - > base = RID ( ) ;
if ( p_base . is_valid ( ) ) {
2022-06-21 00:08:33 +00:00
instance - > base_type = RSG : : utilities - > get_base_type ( p_base ) ;
2021-04-20 16:40:24 +00:00
2022-05-08 23:25:49 +00:00
// fix up a specific malfunctioning case before the switch, so it can be handled
2021-04-20 16:40:24 +00:00
if ( instance - > base_type = = RS : : INSTANCE_NONE & & RendererSceneOcclusionCull : : get_singleton ( ) - > is_occluder ( p_base ) ) {
instance - > base_type = RS : : INSTANCE_OCCLUDER ;
}
2020-12-03 21:09:47 +00:00
switch ( instance - > base_type ) {
2022-05-08 23:25:49 +00:00
case RS : : INSTANCE_NONE : {
ERR_PRINT_ONCE ( " unimplemented base type encountered in renderer scene cull " ) ;
return ;
}
2020-12-03 21:09:47 +00:00
case RS : : INSTANCE_LIGHT : {
InstanceLightData * light = memnew ( InstanceLightData ) ;
2022-04-09 09:34:31 +00:00
if ( scenario & & RSG : : light_storage - > light_get_type ( p_base ) = = RS : : LIGHT_DIRECTIONAL ) {
2020-12-03 21:09:47 +00:00
light - > D = scenario - > directional_lights . push_back ( instance ) ;
}
2022-09-12 09:44:48 +00:00
light - > instance = RSG : : light_storage - > light_instance_create ( p_base ) ;
2020-12-03 21:09:47 +00:00
instance - > base_data = light ;
} break ;
case RS : : INSTANCE_MESH :
case RS : : INSTANCE_MULTIMESH :
case RS : : INSTANCE_PARTICLES : {
InstanceGeometryData * geom = memnew ( InstanceGeometryData ) ;
instance - > base_data = geom ;
2020-12-31 12:42:56 +00:00
geom - > geometry_instance = scene_render - > geometry_instance_create ( p_base ) ;
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_skeleton ( instance - > skeleton ) ;
geom - > geometry_instance - > set_material_override ( instance - > material_override ) ;
geom - > geometry_instance - > set_material_overlay ( instance - > material_overlay ) ;
geom - > geometry_instance - > set_surface_materials ( instance - > materials ) ;
geom - > geometry_instance - > set_transform ( instance - > transform , instance - > aabb , instance - > transformed_aabb ) ;
geom - > geometry_instance - > set_layer_mask ( instance - > layer_mask ) ;
2022-12-13 04:13:16 +00:00
geom - > geometry_instance - > set_pivot_data ( instance - > sorting_offset , instance - > use_aabb_center ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_lod_bias ( instance - > lod_bias ) ;
geom - > geometry_instance - > set_transparency ( instance - > transparency ) ;
geom - > geometry_instance - > set_use_baked_light ( instance - > baked_light ) ;
geom - > geometry_instance - > set_use_dynamic_gi ( instance - > dynamic_gi ) ;
geom - > geometry_instance - > set_use_lightmap ( RID ( ) , instance - > lightmap_uv_scale , instance - > lightmap_slice_index ) ;
2023-03-04 10:49:18 +00:00
geom - > geometry_instance - > set_instance_shader_uniforms_offset ( instance - > instance_allocated_shader_uniforms_offset ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_cast_double_sided_shadows ( instance - > cast_shadows = = RS : : SHADOW_CASTING_SETTING_DOUBLE_SIDED ) ;
2020-12-31 12:42:56 +00:00
if ( instance - > lightmap_sh . size ( ) = = 9 ) {
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_lightmap_capture ( instance - > lightmap_sh . ptr ( ) ) ;
2020-12-31 12:42:56 +00:00
}
2020-12-16 14:07:08 +00:00
2022-05-18 23:43:40 +00:00
for ( Instance * E : instance - > visibility_dependencies ) {
Instance * dep_instance = E ;
2021-10-13 14:28:14 +00:00
ERR_CONTINUE ( dep_instance - > array_index = = - 1 ) ;
ERR_CONTINUE ( dep_instance - > scenario - > instance_data [ dep_instance - > array_index ] . parent_array_index ! = - 1 ) ;
dep_instance - > scenario - > instance_data [ dep_instance - > array_index ] . parent_array_index = instance - > array_index ;
}
2020-12-03 21:09:47 +00:00
} break ;
2020-12-31 12:42:56 +00:00
case RS : : INSTANCE_PARTICLES_COLLISION : {
InstanceParticlesCollisionData * collision = memnew ( InstanceParticlesCollisionData ) ;
2022-04-12 11:41:50 +00:00
collision - > instance = RSG : : particles_storage - > particles_collision_instance_create ( p_base ) ;
RSG : : particles_storage - > particles_collision_instance_set_active ( collision - > instance , instance - > visible ) ;
2020-12-31 12:42:56 +00:00
instance - > base_data = collision ;
} break ;
2021-10-03 11:28:55 +00:00
case RS : : INSTANCE_FOG_VOLUME : {
InstanceFogVolumeData * volume = memnew ( InstanceFogVolumeData ) ;
volume - > instance = scene_render - > fog_volume_instance_create ( p_base ) ;
scene_render - > fog_volume_instance_set_active ( volume - > instance , instance - > visible ) ;
instance - > base_data = volume ;
} break ;
2021-06-16 18:43:02 +00:00
case RS : : INSTANCE_VISIBLITY_NOTIFIER : {
InstanceVisibilityNotifierData * vnd = memnew ( InstanceVisibilityNotifierData ) ;
vnd - > base = p_base ;
instance - > base_data = vnd ;
} break ;
2020-12-03 21:09:47 +00:00
case RS : : INSTANCE_REFLECTION_PROBE : {
InstanceReflectionProbeData * reflection_probe = memnew ( InstanceReflectionProbeData ) ;
reflection_probe - > owner = instance ;
instance - > base_data = reflection_probe ;
2022-09-12 09:44:48 +00:00
reflection_probe - > instance = RSG : : light_storage - > reflection_probe_instance_create ( p_base ) ;
2020-12-03 21:09:47 +00:00
} break ;
case RS : : INSTANCE_DECAL : {
InstanceDecalData * decal = memnew ( InstanceDecalData ) ;
decal - > owner = instance ;
instance - > base_data = decal ;
2022-09-12 09:44:48 +00:00
decal - > instance = RSG : : texture_storage - > decal_instance_create ( p_base ) ;
2023-01-20 03:44:20 +00:00
RSG : : texture_storage - > decal_instance_set_sorting_offset ( decal - > instance , instance - > sorting_offset ) ;
2020-12-03 21:09:47 +00:00
} break ;
case RS : : INSTANCE_LIGHTMAP : {
InstanceLightmapData * lightmap_data = memnew ( InstanceLightmapData ) ;
instance - > base_data = lightmap_data ;
2022-09-12 09:44:48 +00:00
lightmap_data - > instance = RSG : : light_storage - > lightmap_instance_create ( p_base ) ;
2020-12-03 21:09:47 +00:00
} break ;
2021-06-04 22:47:26 +00:00
case RS : : INSTANCE_VOXEL_GI : {
InstanceVoxelGIData * voxel_gi = memnew ( InstanceVoxelGIData ) ;
instance - > base_data = voxel_gi ;
voxel_gi - > owner = instance ;
2020-12-03 21:09:47 +00:00
2021-06-04 22:47:26 +00:00
if ( scenario & & ! voxel_gi - > update_element . in_list ( ) ) {
voxel_gi_update_list . add ( & voxel_gi - > update_element ) ;
2020-12-03 21:09:47 +00:00
}
2021-06-04 22:47:26 +00:00
voxel_gi - > probe_instance = scene_render - > voxel_gi_instance_create ( p_base ) ;
2020-12-03 21:09:47 +00:00
} break ;
2021-04-20 16:40:24 +00:00
case RS : : INSTANCE_OCCLUDER : {
if ( scenario ) {
RendererSceneOcclusionCull : : get_singleton ( ) - > scenario_set_instance ( scenario - > self , p_instance , p_base , instance - > transform , instance - > visible ) ;
}
} break ;
2020-12-03 21:09:47 +00:00
default : {
}
}
instance - > base = p_base ;
2020-12-16 14:07:08 +00:00
if ( instance - > base_type = = RS : : INSTANCE_MESH ) {
_instance_update_mesh_instance ( instance ) ;
}
2020-12-03 21:09:47 +00:00
//forcefully update the dependency now, so if for some reason it gets removed, we can immediately clear it
2022-06-21 00:08:33 +00:00
RSG : : utilities - > base_update_dependency ( p_base , & instance - > dependency_tracker ) ;
2020-12-03 21:09:47 +00:00
}
_instance_queue_update ( instance , true , true ) ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : instance_set_scenario ( RID p_instance , RID p_scenario ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! instance ) ;
if ( instance - > scenario ) {
instance - > scenario - > instances . remove ( & instance - > scenario_item ) ;
2020-12-23 16:52:58 +00:00
if ( instance - > indexer_id . is_valid ( ) ) {
_unpair_instance ( instance ) ;
2020-12-03 21:09:47 +00:00
}
switch ( instance - > base_type ) {
case RS : : INSTANCE_LIGHT : {
InstanceLightData * light = static_cast < InstanceLightData * > ( instance - > base_data ) ;
2023-01-17 22:33:15 +00:00
if ( instance - > visible & & RSG : : light_storage - > light_get_type ( instance - > base ) ! = RS : : LIGHT_DIRECTIONAL & & light - > bake_mode = = RS : : LIGHT_BAKE_DYNAMIC ) {
instance - > scenario - > dynamic_lights . erase ( light - > instance ) ;
}
2020-12-03 21:09:47 +00:00
# ifdef DEBUG_ENABLED
if ( light - > geometries . size ( ) ) {
ERR_PRINT ( " BUG, indexing did not unpair geometries from light. " ) ;
}
# endif
if ( light - > D ) {
instance - > scenario - > directional_lights . erase ( light - > D ) ;
light - > D = nullptr ;
}
} break ;
case RS : : INSTANCE_REFLECTION_PROBE : {
InstanceReflectionProbeData * reflection_probe = static_cast < InstanceReflectionProbeData * > ( instance - > base_data ) ;
2022-09-12 09:44:48 +00:00
RSG : : light_storage - > reflection_probe_release_atlas_index ( reflection_probe - > instance ) ;
2020-12-03 21:09:47 +00:00
} break ;
case RS : : INSTANCE_PARTICLES_COLLISION : {
heightfield_particle_colliders_update_list . erase ( instance ) ;
} break ;
2021-06-04 22:47:26 +00:00
case RS : : INSTANCE_VOXEL_GI : {
InstanceVoxelGIData * voxel_gi = static_cast < InstanceVoxelGIData * > ( instance - > base_data ) ;
2020-12-03 21:09:47 +00:00
# ifdef DEBUG_ENABLED
2021-06-04 22:47:26 +00:00
if ( voxel_gi - > geometries . size ( ) ) {
ERR_PRINT ( " BUG, indexing did not unpair geometries from VoxelGI. " ) ;
2020-12-03 21:09:47 +00:00
}
# endif
# ifdef DEBUG_ENABLED
2021-06-04 22:47:26 +00:00
if ( voxel_gi - > lights . size ( ) ) {
ERR_PRINT ( " BUG, indexing did not unpair lights from VoxelGI. " ) ;
2020-12-03 21:09:47 +00:00
}
# endif
2021-06-04 22:47:26 +00:00
if ( voxel_gi - > update_element . in_list ( ) ) {
voxel_gi_update_list . remove ( & voxel_gi - > update_element ) ;
2020-12-03 21:09:47 +00:00
}
} break ;
2021-04-20 16:40:24 +00:00
case RS : : INSTANCE_OCCLUDER : {
if ( instance - > visible ) {
RendererSceneOcclusionCull : : get_singleton ( ) - > scenario_remove_instance ( instance - > scenario - > self , p_instance ) ;
}
} break ;
2020-12-03 21:09:47 +00:00
default : {
}
}
instance - > scenario = nullptr ;
}
if ( p_scenario . is_valid ( ) ) {
2021-09-29 17:08:41 +00:00
Scenario * scenario = scenario_owner . get_or_null ( p_scenario ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! scenario ) ;
instance - > scenario = scenario ;
scenario - > instances . add ( & instance - > scenario_item ) ;
switch ( instance - > base_type ) {
case RS : : INSTANCE_LIGHT : {
InstanceLightData * light = static_cast < InstanceLightData * > ( instance - > base_data ) ;
2022-04-09 09:34:31 +00:00
if ( RSG : : light_storage - > light_get_type ( instance - > base ) = = RS : : LIGHT_DIRECTIONAL ) {
2020-12-03 21:09:47 +00:00
light - > D = scenario - > directional_lights . push_back ( instance ) ;
}
} break ;
2021-06-04 22:47:26 +00:00
case RS : : INSTANCE_VOXEL_GI : {
InstanceVoxelGIData * voxel_gi = static_cast < InstanceVoxelGIData * > ( instance - > base_data ) ;
if ( ! voxel_gi - > update_element . in_list ( ) ) {
voxel_gi_update_list . add ( & voxel_gi - > update_element ) ;
2020-12-03 21:09:47 +00:00
}
} break ;
2021-04-20 16:40:24 +00:00
case RS : : INSTANCE_OCCLUDER : {
RendererSceneOcclusionCull : : get_singleton ( ) - > scenario_set_instance ( scenario - > self , p_instance , instance - > base , instance - > transform , instance - > visible ) ;
} break ;
2020-12-03 21:09:47 +00:00
default : {
}
}
_instance_queue_update ( instance , true , true ) ;
}
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : instance_set_layer_mask ( RID p_instance , uint32_t p_mask ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! instance ) ;
2022-12-27 17:43:53 +00:00
if ( instance - > layer_mask = = p_mask ) {
return ;
}
2020-12-03 21:09:47 +00:00
instance - > layer_mask = p_mask ;
2020-12-26 12:05:36 +00:00
if ( instance - > scenario & & instance - > array_index > = 0 ) {
instance - > scenario - > instance_data [ instance - > array_index ] . layer_mask = p_mask ;
}
2020-12-31 12:42:56 +00:00
if ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK & & instance - > base_data ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( instance - > base_data ) ;
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_layer_mask ( p_mask ) ;
2022-12-27 17:43:53 +00:00
if ( geom - > can_cast_shadows ) {
for ( HashSet < RendererSceneCull : : Instance * > : : Iterator I = geom - > lights . begin ( ) ; I ! = geom - > lights . end ( ) ; + + I ) {
InstanceLightData * light = static_cast < InstanceLightData * > ( ( * I ) - > base_data ) ;
light - > shadow_dirty = true ;
}
}
2020-12-31 12:42:56 +00:00
}
2020-12-03 21:09:47 +00:00
}
2022-12-13 04:13:16 +00:00
void RendererSceneCull : : instance_set_pivot_data ( RID p_instance , float p_sorting_offset , bool p_use_aabb_center ) {
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
ERR_FAIL_COND ( ! instance ) ;
instance - > sorting_offset = p_sorting_offset ;
instance - > use_aabb_center = p_use_aabb_center ;
if ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK & & instance - > base_data ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( instance - > base_data ) ;
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
geom - > geometry_instance - > set_pivot_data ( p_sorting_offset , p_use_aabb_center ) ;
2023-01-20 03:44:20 +00:00
} else if ( instance - > base_type = = RS : : INSTANCE_DECAL & & instance - > base_data ) {
InstanceDecalData * decal = static_cast < InstanceDecalData * > ( instance - > base_data ) ;
RSG : : texture_storage - > decal_instance_set_sorting_offset ( decal - > instance , instance - > sorting_offset ) ;
2022-12-13 04:13:16 +00:00
}
}
2021-07-20 18:17:34 +00:00
void RendererSceneCull : : instance_geometry_set_transparency ( RID p_instance , float p_transparency ) {
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
ERR_FAIL_COND ( ! instance ) ;
2021-11-12 11:11:57 +00:00
instance - > transparency = p_transparency ;
2021-07-20 18:17:34 +00:00
if ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK & & instance - > base_data ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( instance - > base_data ) ;
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_transparency ( p_transparency ) ;
2021-07-20 18:17:34 +00:00
}
}
2020-10-17 05:08:21 +00:00
void RendererSceneCull : : instance_set_transform ( RID p_instance , const Transform3D & p_transform ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! instance ) ;
if ( instance - > transform = = p_transform ) {
return ; //must be checked to avoid worst evil
}
# ifdef DEBUG_ENABLED
for ( int i = 0 ; i < 4 ; i + + ) {
2022-04-24 22:07:35 +00:00
const Vector3 & v = i < 3 ? p_transform . basis . rows [ i ] : p_transform . origin ;
2022-08-11 08:12:27 +00:00
ERR_FAIL_COND ( ! v . is_finite ( ) ) ;
2020-12-03 21:09:47 +00:00
}
# endif
instance - > transform = p_transform ;
_instance_queue_update ( instance , true ) ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : instance_attach_object_instance_id ( RID p_instance , ObjectID p_id ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! instance ) ;
instance - > object_id = p_id ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : instance_set_blend_shape_weight ( RID p_instance , int p_shape , float p_weight ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! instance ) ;
if ( instance - > update_item . in_list ( ) ) {
_update_dirty_instance ( instance ) ;
}
2020-12-16 14:07:08 +00:00
if ( instance - > mesh_instance . is_valid ( ) ) {
2022-04-02 05:29:04 +00:00
RSG : : mesh_storage - > mesh_instance_set_blend_shape_weight ( instance - > mesh_instance , p_shape , p_weight ) ;
2020-12-16 14:07:08 +00:00
}
2020-12-03 21:09:47 +00:00
}
2021-04-14 03:45:16 +00:00
void RendererSceneCull : : instance_set_surface_override_material ( RID p_instance , int p_surface , RID p_material ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! instance ) ;
if ( instance - > base_type = = RS : : INSTANCE_MESH ) {
//may not have been updated yet, may also have not been set yet. When updated will be correcte, worst case
2022-04-02 05:29:04 +00:00
instance - > materials . resize ( MAX ( p_surface + 1 , RSG : : mesh_storage - > mesh_get_surface_count ( instance - > base ) ) ) ;
2020-12-03 21:09:47 +00:00
}
ERR_FAIL_INDEX ( p_surface , instance - > materials . size ( ) ) ;
instance - > materials . write [ p_surface ] = p_material ;
_instance_queue_update ( instance , false , true ) ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : instance_set_visible ( RID p_instance , bool p_visible ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! instance ) ;
if ( instance - > visible = = p_visible ) {
return ;
}
instance - > visible = p_visible ;
2020-12-23 16:52:58 +00:00
if ( p_visible ) {
if ( instance - > scenario ! = nullptr ) {
_instance_queue_update ( instance , true , false ) ;
2020-12-03 21:09:47 +00:00
}
2020-12-23 16:52:58 +00:00
} else if ( instance - > indexer_id . is_valid ( ) ) {
_unpair_instance ( instance ) ;
2020-12-03 21:09:47 +00:00
}
2020-12-31 12:42:56 +00:00
2021-01-17 16:25:38 +00:00
if ( instance - > base_type = = RS : : INSTANCE_LIGHT ) {
InstanceLightData * light = static_cast < InstanceLightData * > ( instance - > base_data ) ;
2022-04-09 09:34:31 +00:00
if ( instance - > scenario & & RSG : : light_storage - > light_get_type ( instance - > base ) ! = RS : : LIGHT_DIRECTIONAL & & light - > bake_mode = = RS : : LIGHT_BAKE_DYNAMIC ) {
2021-01-17 16:25:38 +00:00
if ( p_visible ) {
instance - > scenario - > dynamic_lights . push_back ( light - > instance ) ;
} else {
instance - > scenario - > dynamic_lights . erase ( light - > instance ) ;
}
}
}
2020-12-31 12:42:56 +00:00
if ( instance - > base_type = = RS : : INSTANCE_PARTICLES_COLLISION ) {
InstanceParticlesCollisionData * collision = static_cast < InstanceParticlesCollisionData * > ( instance - > base_data ) ;
2022-04-12 11:41:50 +00:00
RSG : : particles_storage - > particles_collision_instance_set_active ( collision - > instance , p_visible ) ;
2020-12-31 12:42:56 +00:00
}
2021-04-20 16:40:24 +00:00
2021-10-03 11:28:55 +00:00
if ( instance - > base_type = = RS : : INSTANCE_FOG_VOLUME ) {
InstanceFogVolumeData * volume = static_cast < InstanceFogVolumeData * > ( instance - > base_data ) ;
scene_render - > fog_volume_instance_set_active ( volume - > instance , p_visible ) ;
}
2021-04-20 16:40:24 +00:00
if ( instance - > base_type = = RS : : INSTANCE_OCCLUDER ) {
if ( instance - > scenario ) {
RendererSceneOcclusionCull : : get_singleton ( ) - > scenario_set_instance ( instance - > scenario - > self , p_instance , instance - > base , instance - > transform , p_visible ) ;
}
}
2020-12-03 21:09:47 +00:00
}
inline bool is_geometry_instance ( RenderingServer : : InstanceType p_type ) {
2021-06-30 01:55:11 +00:00
return p_type = = RS : : INSTANCE_MESH | | p_type = = RS : : INSTANCE_MULTIMESH | | p_type = = RS : : INSTANCE_PARTICLES ;
2020-12-03 21:09:47 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : instance_set_custom_aabb ( RID p_instance , AABB p_aabb ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! instance ) ;
ERR_FAIL_COND ( ! is_geometry_instance ( instance - > base_type ) ) ;
if ( p_aabb ! = AABB ( ) ) {
// Set custom AABB
if ( instance - > custom_aabb = = nullptr ) {
instance - > custom_aabb = memnew ( AABB ) ;
}
* instance - > custom_aabb = p_aabb ;
} else {
// Clear custom AABB
if ( instance - > custom_aabb ! = nullptr ) {
memdelete ( instance - > custom_aabb ) ;
instance - > custom_aabb = nullptr ;
}
}
if ( instance - > scenario ) {
_instance_queue_update ( instance , true , false ) ;
}
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : instance_attach_skeleton ( RID p_instance , RID p_skeleton ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! instance ) ;
if ( instance - > skeleton = = p_skeleton ) {
return ;
}
instance - > skeleton = p_skeleton ;
if ( p_skeleton . is_valid ( ) ) {
//update the dependency now, so if cleared, we remove it
2022-04-02 05:29:04 +00:00
RSG : : mesh_storage - > skeleton_update_dependency ( p_skeleton , & instance - > dependency_tracker ) ;
2020-12-03 21:09:47 +00:00
}
2020-12-16 14:07:08 +00:00
2020-12-03 21:09:47 +00:00
_instance_queue_update ( instance , true , true ) ;
2020-12-31 12:42:56 +00:00
if ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK & & instance - > base_data ) {
_instance_update_mesh_instance ( instance ) ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( instance - > base_data ) ;
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_skeleton ( p_skeleton ) ;
2020-12-31 12:42:56 +00:00
}
2020-12-03 21:09:47 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : instance_set_extra_visibility_margin ( RID p_instance , real_t p_margin ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! instance ) ;
instance - > extra_margin = p_margin ;
_instance_queue_update ( instance , true , false ) ;
}
2021-10-03 11:28:55 +00:00
void RendererSceneCull : : instance_set_ignore_culling ( RID p_instance , bool p_enabled ) {
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
ERR_FAIL_COND ( ! instance ) ;
instance - > ignore_all_culling = p_enabled ;
if ( instance - > scenario & & instance - > array_index > = 0 ) {
InstanceData & idata = instance - > scenario - > instance_data [ instance - > array_index ] ;
if ( instance - > ignore_all_culling ) {
idata . flags | = InstanceData : : FLAG_IGNORE_ALL_CULLING ;
} else {
idata . flags & = ~ uint32_t ( InstanceData : : FLAG_IGNORE_ALL_CULLING ) ;
}
}
}
2020-12-04 18:26:24 +00:00
Vector < ObjectID > RendererSceneCull : : instances_cull_aabb ( const AABB & p_aabb , RID p_scenario ) const {
2020-12-03 21:09:47 +00:00
Vector < ObjectID > instances ;
2021-09-29 17:08:41 +00:00
Scenario * scenario = scenario_owner . get_or_null ( p_scenario ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND_V ( ! scenario , instances ) ;
2020-12-04 18:26:24 +00:00
const_cast < RendererSceneCull * > ( this ) - > update_dirty_instances ( ) ; // check dirty instances before culling
2020-12-03 21:09:47 +00:00
2020-12-23 16:52:58 +00:00
struct CullAABB {
Vector < ObjectID > instances ;
_FORCE_INLINE_ bool operator ( ) ( void * p_data ) {
Instance * p_instance = ( Instance * ) p_data ;
if ( ! p_instance - > object_id . is_null ( ) ) {
instances . push_back ( p_instance - > object_id ) ;
}
return false ;
2020-12-03 21:09:47 +00:00
}
2020-12-23 16:52:58 +00:00
} ;
2020-12-03 21:09:47 +00:00
2020-12-23 16:52:58 +00:00
CullAABB cull_aabb ;
scenario - > indexers [ Scenario : : INDEXER_GEOMETRY ] . aabb_query ( p_aabb , cull_aabb ) ;
scenario - > indexers [ Scenario : : INDEXER_VOLUMES ] . aabb_query ( p_aabb , cull_aabb ) ;
return cull_aabb . instances ;
2020-12-03 21:09:47 +00:00
}
2020-12-04 18:26:24 +00:00
Vector < ObjectID > RendererSceneCull : : instances_cull_ray ( const Vector3 & p_from , const Vector3 & p_to , RID p_scenario ) const {
2020-12-03 21:09:47 +00:00
Vector < ObjectID > instances ;
2021-09-29 17:08:41 +00:00
Scenario * scenario = scenario_owner . get_or_null ( p_scenario ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND_V ( ! scenario , instances ) ;
2020-12-04 18:26:24 +00:00
const_cast < RendererSceneCull * > ( this ) - > update_dirty_instances ( ) ; // check dirty instances before culling
2020-12-03 21:09:47 +00:00
2020-12-23 16:52:58 +00:00
struct CullRay {
Vector < ObjectID > instances ;
_FORCE_INLINE_ bool operator ( ) ( void * p_data ) {
Instance * p_instance = ( Instance * ) p_data ;
if ( ! p_instance - > object_id . is_null ( ) ) {
instances . push_back ( p_instance - > object_id ) ;
}
return false ;
2020-12-03 21:09:47 +00:00
}
2020-12-23 16:52:58 +00:00
} ;
2020-12-03 21:09:47 +00:00
2020-12-23 16:52:58 +00:00
CullRay cull_ray ;
scenario - > indexers [ Scenario : : INDEXER_GEOMETRY ] . ray_query ( p_from , p_to , cull_ray ) ;
scenario - > indexers [ Scenario : : INDEXER_VOLUMES ] . ray_query ( p_from , p_to , cull_ray ) ;
return cull_ray . instances ;
2020-12-03 21:09:47 +00:00
}
2020-12-04 18:26:24 +00:00
Vector < ObjectID > RendererSceneCull : : instances_cull_convex ( const Vector < Plane > & p_convex , RID p_scenario ) const {
2020-12-03 21:09:47 +00:00
Vector < ObjectID > instances ;
2021-09-29 17:08:41 +00:00
Scenario * scenario = scenario_owner . get_or_null ( p_scenario ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND_V ( ! scenario , instances ) ;
2020-12-04 18:26:24 +00:00
const_cast < RendererSceneCull * > ( this ) - > update_dirty_instances ( ) ; // check dirty instances before culling
2020-12-03 21:09:47 +00:00
2020-12-23 16:52:58 +00:00
Vector < Vector3 > points = Geometry3D : : compute_convex_mesh_points ( & p_convex [ 0 ] , p_convex . size ( ) ) ;
2020-12-03 21:09:47 +00:00
2020-12-23 16:52:58 +00:00
struct CullConvex {
Vector < ObjectID > instances ;
_FORCE_INLINE_ bool operator ( ) ( void * p_data ) {
Instance * p_instance = ( Instance * ) p_data ;
if ( ! p_instance - > object_id . is_null ( ) ) {
instances . push_back ( p_instance - > object_id ) ;
}
return false ;
2020-12-03 21:09:47 +00:00
}
2020-12-23 16:52:58 +00:00
} ;
2020-12-03 21:09:47 +00:00
2020-12-23 16:52:58 +00:00
CullConvex cull_convex ;
scenario - > indexers [ Scenario : : INDEXER_GEOMETRY ] . convex_query ( p_convex . ptr ( ) , p_convex . size ( ) , points . ptr ( ) , points . size ( ) , cull_convex ) ;
scenario - > indexers [ Scenario : : INDEXER_VOLUMES ] . convex_query ( p_convex . ptr ( ) , p_convex . size ( ) , points . ptr ( ) , points . size ( ) , cull_convex ) ;
return cull_convex . instances ;
2020-12-03 21:09:47 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : instance_geometry_set_flag ( RID p_instance , RS : : InstanceFlags p_flags , bool p_enabled ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! instance ) ;
//ERR_FAIL_COND(((1 << instance->base_type) & RS::INSTANCE_GEOMETRY_MASK));
switch ( p_flags ) {
case RS : : INSTANCE_FLAG_USE_BAKED_LIGHT : {
instance - > baked_light = p_enabled ;
2020-12-26 12:05:36 +00:00
if ( instance - > scenario & & instance - > array_index > = 0 ) {
InstanceData & idata = instance - > scenario - > instance_data [ instance - > array_index ] ;
if ( instance - > baked_light ) {
idata . flags | = InstanceData : : FLAG_USES_BAKED_LIGHT ;
} else {
idata . flags & = ~ uint32_t ( InstanceData : : FLAG_USES_BAKED_LIGHT ) ;
}
}
2020-12-31 12:42:56 +00:00
if ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK & & instance - > base_data ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( instance - > base_data ) ;
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_use_baked_light ( p_enabled ) ;
2020-12-31 12:42:56 +00:00
}
2020-12-03 21:09:47 +00:00
} break ;
case RS : : INSTANCE_FLAG_USE_DYNAMIC_GI : {
if ( p_enabled = = instance - > dynamic_gi ) {
//bye, redundant
return ;
}
2020-12-23 16:52:58 +00:00
if ( instance - > indexer_id . is_valid ( ) ) {
_unpair_instance ( instance ) ;
2020-12-03 21:09:47 +00:00
_instance_queue_update ( instance , true , true ) ;
}
//once out of octree, can be changed
instance - > dynamic_gi = p_enabled ;
2020-12-31 12:42:56 +00:00
if ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK & & instance - > base_data ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( instance - > base_data ) ;
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_use_dynamic_gi ( p_enabled ) ;
2020-12-31 12:42:56 +00:00
}
2020-12-03 21:09:47 +00:00
} break ;
case RS : : INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE : {
instance - > redraw_if_visible = p_enabled ;
2020-12-26 12:05:36 +00:00
if ( instance - > scenario & & instance - > array_index > = 0 ) {
InstanceData & idata = instance - > scenario - > instance_data [ instance - > array_index ] ;
if ( instance - > redraw_if_visible ) {
idata . flags | = InstanceData : : FLAG_REDRAW_IF_VISIBLE ;
} else {
idata . flags & = ~ uint32_t ( InstanceData : : FLAG_REDRAW_IF_VISIBLE ) ;
}
}
2020-12-03 21:09:47 +00:00
} break ;
2021-04-20 16:40:24 +00:00
case RS : : INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING : {
instance - > ignore_occlusion_culling = p_enabled ;
if ( instance - > scenario & & instance - > array_index > = 0 ) {
InstanceData & idata = instance - > scenario - > instance_data [ instance - > array_index ] ;
if ( instance - > ignore_occlusion_culling ) {
idata . flags | = InstanceData : : FLAG_IGNORE_OCCLUSION_CULLING ;
} else {
idata . flags & = ~ uint32_t ( InstanceData : : FLAG_IGNORE_OCCLUSION_CULLING ) ;
}
}
} break ;
2020-12-03 21:09:47 +00:00
default : {
}
}
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : instance_geometry_set_cast_shadows_setting ( RID p_instance , RS : : ShadowCastingSetting p_shadow_casting_setting ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! instance ) ;
instance - > cast_shadows = p_shadow_casting_setting ;
2020-12-26 12:05:36 +00:00
if ( instance - > scenario & & instance - > array_index > = 0 ) {
InstanceData & idata = instance - > scenario - > instance_data [ instance - > array_index ] ;
2021-10-25 14:04:11 +00:00
if ( instance - > cast_shadows ! = RS : : SHADOW_CASTING_SETTING_OFF ) {
2020-12-26 12:05:36 +00:00
idata . flags | = InstanceData : : FLAG_CAST_SHADOWS ;
} else {
idata . flags & = ~ uint32_t ( InstanceData : : FLAG_CAST_SHADOWS ) ;
}
if ( instance - > cast_shadows = = RS : : SHADOW_CASTING_SETTING_SHADOWS_ONLY ) {
idata . flags | = InstanceData : : FLAG_CAST_SHADOWS_ONLY ;
} else {
idata . flags & = ~ uint32_t ( InstanceData : : FLAG_CAST_SHADOWS_ONLY ) ;
}
}
2020-12-31 12:42:56 +00:00
if ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK & & instance - > base_data ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( instance - > base_data ) ;
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_cast_double_sided_shadows ( instance - > cast_shadows = = RS : : SHADOW_CASTING_SETTING_DOUBLE_SIDED ) ;
2020-12-31 12:42:56 +00:00
}
2020-12-03 21:09:47 +00:00
_instance_queue_update ( instance , false , true ) ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : instance_geometry_set_material_override ( RID p_instance , RID p_material ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! instance ) ;
instance - > material_override = p_material ;
_instance_queue_update ( instance , false , true ) ;
2020-12-31 12:42:56 +00:00
if ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK & & instance - > base_data ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( instance - > base_data ) ;
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_material_override ( p_material ) ;
2020-12-31 12:42:56 +00:00
}
2020-12-03 21:09:47 +00:00
}
2021-09-25 18:40:26 +00:00
void RendererSceneCull : : instance_geometry_set_material_overlay ( RID p_instance , RID p_material ) {
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
ERR_FAIL_COND ( ! instance ) ;
instance - > material_overlay = p_material ;
_instance_queue_update ( instance , false , true ) ;
if ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK & & instance - > base_data ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( instance - > base_data ) ;
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_material_overlay ( p_material ) ;
2021-09-25 18:40:26 +00:00
}
}
2021-07-20 18:17:34 +00:00
void RendererSceneCull : : instance_geometry_set_visibility_range ( RID p_instance , float p_min , float p_max , float p_min_margin , float p_max_margin , RS : : VisibilityRangeFadeMode p_fade_mode ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2021-05-09 16:23:20 +00:00
ERR_FAIL_COND ( ! instance ) ;
instance - > visibility_range_begin = p_min ;
instance - > visibility_range_end = p_max ;
instance - > visibility_range_begin_margin = p_min_margin ;
instance - > visibility_range_end_margin = p_max_margin ;
2021-07-20 18:17:34 +00:00
instance - > visibility_range_fade_mode = p_fade_mode ;
2021-05-09 16:23:20 +00:00
_update_instance_visibility_dependencies ( instance ) ;
if ( instance - > scenario & & instance - > visibility_index ! = - 1 ) {
InstanceVisibilityData & vd = instance - > scenario - > instance_visibility [ instance - > visibility_index ] ;
vd . range_begin = instance - > visibility_range_begin ;
vd . range_end = instance - > visibility_range_end ;
vd . range_begin_margin = instance - > visibility_range_begin_margin ;
vd . range_end_margin = instance - > visibility_range_end_margin ;
2021-07-20 18:17:34 +00:00
vd . fade_mode = p_fade_mode ;
2021-05-09 16:23:20 +00:00
}
}
void RendererSceneCull : : instance_set_visibility_parent ( RID p_instance , RID p_parent_instance ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2021-05-09 16:23:20 +00:00
ERR_FAIL_COND ( ! instance ) ;
Instance * old_parent = instance - > visibility_parent ;
if ( old_parent ) {
2021-10-13 14:28:14 +00:00
old_parent - > visibility_dependencies . erase ( instance ) ;
2021-05-09 16:23:20 +00:00
instance - > visibility_parent = nullptr ;
2021-10-13 14:28:14 +00:00
_update_instance_visibility_depth ( old_parent ) ;
2021-05-09 16:23:20 +00:00
}
2021-09-29 17:08:41 +00:00
Instance * parent = instance_owner . get_or_null ( p_parent_instance ) ;
2021-05-09 16:23:20 +00:00
ERR_FAIL_COND ( p_parent_instance . is_valid ( ) & & ! parent ) ;
if ( parent ) {
2021-10-13 14:28:14 +00:00
parent - > visibility_dependencies . insert ( instance ) ;
2021-05-09 16:23:20 +00:00
instance - > visibility_parent = parent ;
2021-10-13 14:28:14 +00:00
bool cycle_detected = _update_instance_visibility_depth ( parent ) ;
if ( cycle_detected ) {
ERR_PRINT ( " Cycle detected in the visibility dependencies tree. The latest change to visibility_parent will have no effect. " ) ;
parent - > visibility_dependencies . erase ( instance ) ;
instance - > visibility_parent = nullptr ;
}
2021-05-09 16:23:20 +00:00
}
_update_instance_visibility_dependencies ( instance ) ;
}
2021-10-13 14:28:14 +00:00
bool RendererSceneCull : : _update_instance_visibility_depth ( Instance * p_instance ) {
2021-05-09 16:23:20 +00:00
bool cycle_detected = false ;
2022-05-19 15:00:06 +00:00
HashSet < Instance * > traversed_nodes ;
2021-05-09 16:23:20 +00:00
{
Instance * instance = p_instance ;
2021-10-13 14:28:14 +00:00
while ( instance ) {
if ( ! instance - > visibility_dependencies . is_empty ( ) ) {
2021-05-09 16:23:20 +00:00
uint32_t depth = 0 ;
2022-05-18 23:43:40 +00:00
for ( const Instance * E : instance - > visibility_dependencies ) {
depth = MAX ( depth , E - > visibility_dependencies_depth ) ;
2021-05-09 16:23:20 +00:00
}
2021-10-13 14:28:14 +00:00
instance - > visibility_dependencies_depth = depth + 1 ;
2021-05-09 16:23:20 +00:00
} else {
2021-10-13 14:28:14 +00:00
instance - > visibility_dependencies_depth = 0 ;
2021-05-09 16:23:20 +00:00
}
if ( instance - > scenario & & instance - > visibility_index ! = - 1 ) {
2021-10-13 14:28:14 +00:00
instance - > scenario - > instance_visibility . move ( instance - > visibility_index , instance - > visibility_dependencies_depth ) ;
2021-05-09 16:23:20 +00:00
}
traversed_nodes . insert ( instance ) ;
instance = instance - > visibility_parent ;
if ( traversed_nodes . has ( instance ) ) {
cycle_detected = true ;
break ;
}
}
}
2021-10-13 14:28:14 +00:00
return cycle_detected ;
2020-12-03 21:09:47 +00:00
}
2021-05-09 16:23:20 +00:00
void RendererSceneCull : : _update_instance_visibility_dependencies ( Instance * p_instance ) {
bool is_geometry_instance = ( ( 1 < < p_instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) & & p_instance - > base_data ;
bool has_visibility_range = p_instance - > visibility_range_begin > 0.0 | | p_instance - > visibility_range_end > 0.0 ;
bool needs_visibility_cull = has_visibility_range & & is_geometry_instance & & p_instance - > array_index ! = - 1 ;
if ( ! needs_visibility_cull & & p_instance - > visibility_index ! = - 1 ) {
2021-07-03 22:17:03 +00:00
p_instance - > scenario - > instance_visibility . remove_at ( p_instance - > visibility_index ) ;
2021-05-09 16:23:20 +00:00
p_instance - > visibility_index = - 1 ;
} else if ( needs_visibility_cull & & p_instance - > visibility_index = = - 1 ) {
InstanceVisibilityData vd ;
vd . instance = p_instance ;
vd . range_begin = p_instance - > visibility_range_begin ;
vd . range_end = p_instance - > visibility_range_end ;
vd . range_begin_margin = p_instance - > visibility_range_begin_margin ;
vd . range_end_margin = p_instance - > visibility_range_end_margin ;
2021-09-20 18:48:52 +00:00
vd . position = p_instance - > transformed_aabb . get_center ( ) ;
2021-05-09 16:23:20 +00:00
vd . array_index = p_instance - > array_index ;
2021-07-20 18:17:34 +00:00
vd . fade_mode = p_instance - > visibility_range_fade_mode ;
2021-05-09 16:23:20 +00:00
2021-10-13 14:28:14 +00:00
p_instance - > scenario - > instance_visibility . insert ( vd , p_instance - > visibility_dependencies_depth ) ;
2021-05-09 16:23:20 +00:00
}
if ( p_instance - > scenario & & p_instance - > array_index ! = - 1 ) {
2021-07-20 18:17:34 +00:00
InstanceData & idata = p_instance - > scenario - > instance_data [ p_instance - > array_index ] ;
idata . visibility_index = p_instance - > visibility_index ;
if ( is_geometry_instance ) {
if ( has_visibility_range & & p_instance - > visibility_range_fade_mode = = RS : : VISIBILITY_RANGE_FADE_SELF ) {
bool begin_enabled = p_instance - > visibility_range_begin > 0.0f ;
float begin_min = p_instance - > visibility_range_begin - p_instance - > visibility_range_begin_margin ;
float begin_max = p_instance - > visibility_range_begin + p_instance - > visibility_range_begin_margin ;
bool end_enabled = p_instance - > visibility_range_end > 0.0f ;
float end_min = p_instance - > visibility_range_end - p_instance - > visibility_range_end_margin ;
float end_max = p_instance - > visibility_range_end + p_instance - > visibility_range_end_margin ;
2022-07-23 14:01:53 +00:00
idata . instance_geometry - > set_fade_range ( begin_enabled , begin_min , begin_max , end_enabled , end_min , end_max ) ;
2021-07-20 18:17:34 +00:00
} else {
2022-07-23 14:01:53 +00:00
idata . instance_geometry - > set_fade_range ( false , 0.0f , 0.0f , false , 0.0f , 0.0f ) ;
2021-07-20 18:17:34 +00:00
}
}
2021-05-09 16:23:20 +00:00
2021-10-13 14:28:14 +00:00
if ( ( has_visibility_range | | p_instance - > visibility_parent ) & & ( p_instance - > visibility_index = = - 1 | | p_instance - > visibility_dependencies_depth = = 0 ) ) {
2021-07-20 18:17:34 +00:00
idata . flags | = InstanceData : : FLAG_VISIBILITY_DEPENDENCY_NEEDS_CHECK ;
2021-05-09 16:23:20 +00:00
} else {
2021-07-20 18:17:34 +00:00
idata . flags & = ~ InstanceData : : FLAG_VISIBILITY_DEPENDENCY_NEEDS_CHECK ;
2021-05-09 16:23:20 +00:00
}
if ( p_instance - > visibility_parent ) {
2021-07-20 18:17:34 +00:00
idata . parent_array_index = p_instance - > visibility_parent - > array_index ;
2021-05-09 16:23:20 +00:00
} else {
2021-07-20 18:17:34 +00:00
idata . parent_array_index = - 1 ;
if ( is_geometry_instance ) {
2022-07-23 14:01:53 +00:00
idata . instance_geometry - > set_parent_fade_alpha ( 1.0f ) ;
2021-07-20 18:17:34 +00:00
}
2021-05-09 16:23:20 +00:00
}
}
2020-12-03 21:09:47 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : instance_geometry_set_lightmap ( RID p_instance , RID p_lightmap , const Rect2 & p_lightmap_uv_scale , int p_slice_index ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! instance ) ;
if ( instance - > lightmap ) {
InstanceLightmapData * lightmap_data = static_cast < InstanceLightmapData * > ( ( ( Instance * ) instance - > lightmap ) - > base_data ) ;
lightmap_data - > users . erase ( instance ) ;
instance - > lightmap = nullptr ;
}
2021-09-29 17:08:41 +00:00
Instance * lightmap_instance = instance_owner . get_or_null ( p_lightmap ) ;
2020-12-03 21:09:47 +00:00
instance - > lightmap = lightmap_instance ;
instance - > lightmap_uv_scale = p_lightmap_uv_scale ;
instance - > lightmap_slice_index = p_slice_index ;
2020-12-31 12:42:56 +00:00
RID lightmap_instance_rid ;
2020-12-03 21:09:47 +00:00
if ( lightmap_instance ) {
InstanceLightmapData * lightmap_data = static_cast < InstanceLightmapData * > ( lightmap_instance - > base_data ) ;
lightmap_data - > users . insert ( instance ) ;
2020-12-31 12:42:56 +00:00
lightmap_instance_rid = lightmap_data - > instance ;
}
if ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK & & instance - > base_data ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( instance - > base_data ) ;
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_use_lightmap ( lightmap_instance_rid , p_lightmap_uv_scale , p_slice_index ) ;
2020-12-03 21:09:47 +00:00
}
}
2020-12-17 18:56:59 +00:00
void RendererSceneCull : : instance_geometry_set_lod_bias ( RID p_instance , float p_lod_bias ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2020-12-17 18:56:59 +00:00
ERR_FAIL_COND ( ! instance ) ;
instance - > lod_bias = p_lod_bias ;
2020-12-31 12:42:56 +00:00
if ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK & & instance - > base_data ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( instance - > base_data ) ;
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_lod_bias ( p_lod_bias ) ;
2020-12-31 12:42:56 +00:00
}
2020-12-17 18:56:59 +00:00
}
2022-08-27 09:22:43 +00:00
void RendererSceneCull : : instance_geometry_set_shader_parameter ( RID p_instance , const StringName & p_parameter , const Variant & p_value ) {
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! instance ) ;
2021-07-06 01:40:29 +00:00
ERR_FAIL_COND ( p_value . get_type ( ) = = Variant : : OBJECT ) ;
2022-04-03 17:09:09 +00:00
HashMap < StringName , Instance : : InstanceShaderParameter > : : Iterator E = instance - > instance_shader_uniforms . find ( p_parameter ) ;
2020-12-03 21:09:47 +00:00
if ( ! E ) {
2020-12-31 12:42:56 +00:00
Instance : : InstanceShaderParameter isp ;
2020-12-03 21:09:47 +00:00
isp . index = - 1 ;
isp . info = PropertyInfo ( ) ;
isp . value = p_value ;
2022-04-03 17:09:09 +00:00
instance - > instance_shader_uniforms [ p_parameter ] = isp ;
2020-12-03 21:09:47 +00:00
} else {
2022-05-13 13:04:37 +00:00
E - > value . value = p_value ;
2022-04-03 17:09:09 +00:00
if ( E - > value . index > = 0 & & instance - > instance_allocated_shader_uniforms ) {
2022-10-09 05:14:07 +00:00
int flags_count = 0 ;
if ( E - > value . info . hint = = PROPERTY_HINT_FLAGS ) {
// A small hack to detect boolean flags count and prevent overhead.
switch ( E - > value . info . hint_string . length ( ) ) {
case 3 : // "x,y"
flags_count = 1 ;
break ;
case 5 : // "x,y,z"
flags_count = 2 ;
break ;
case 7 : // "x,y,z,w"
flags_count = 3 ;
break ;
}
}
2020-12-03 21:09:47 +00:00
//update directly
2022-10-09 05:14:07 +00:00
RSG : : material_storage - > global_shader_parameters_instance_update ( p_instance , E - > value . index , p_value , flags_count ) ;
2020-12-03 21:09:47 +00:00
}
}
}
2022-08-27 09:22:43 +00:00
Variant RendererSceneCull : : instance_geometry_get_shader_parameter ( RID p_instance , const StringName & p_parameter ) const {
2021-09-29 17:08:41 +00:00
const Instance * instance = const_cast < RendererSceneCull * > ( this ) - > instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND_V ( ! instance , Variant ( ) ) ;
2022-04-03 17:09:09 +00:00
if ( instance - > instance_shader_uniforms . has ( p_parameter ) ) {
return instance - > instance_shader_uniforms [ p_parameter ] . value ;
2020-12-03 21:09:47 +00:00
}
return Variant ( ) ;
}
2022-08-27 09:22:43 +00:00
Variant RendererSceneCull : : instance_geometry_get_shader_parameter_default_value ( RID p_instance , const StringName & p_parameter ) const {
2021-09-29 17:08:41 +00:00
const Instance * instance = const_cast < RendererSceneCull * > ( this ) - > instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND_V ( ! instance , Variant ( ) ) ;
2022-04-03 17:09:09 +00:00
if ( instance - > instance_shader_uniforms . has ( p_parameter ) ) {
return instance - > instance_shader_uniforms [ p_parameter ] . default_value ;
2020-12-03 21:09:47 +00:00
}
return Variant ( ) ;
}
2022-08-27 09:22:43 +00:00
void RendererSceneCull : : instance_geometry_get_shader_parameter_list ( RID p_instance , List < PropertyInfo > * p_parameters ) const {
2021-09-29 17:08:41 +00:00
const Instance * instance = const_cast < RendererSceneCull * > ( this ) - > instance_owner . get_or_null ( p_instance ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! instance ) ;
2020-12-04 18:26:24 +00:00
const_cast < RendererSceneCull * > ( this ) - > update_dirty_instances ( ) ;
2020-12-03 21:09:47 +00:00
Vector < StringName > names ;
2022-04-03 17:09:09 +00:00
for ( const KeyValue < StringName , Instance : : InstanceShaderParameter > & E : instance - > instance_shader_uniforms ) {
2021-08-09 20:13:42 +00:00
names . push_back ( E . key ) ;
2020-12-03 21:09:47 +00:00
}
names . sort_custom < StringName : : AlphCompare > ( ) ;
for ( int i = 0 ; i < names . size ( ) ; i + + ) {
2022-04-03 17:09:09 +00:00
PropertyInfo pinfo = instance - > instance_shader_uniforms [ names [ i ] ] . info ;
2020-12-03 21:09:47 +00:00
p_parameters - > push_back ( pinfo ) ;
}
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : _update_instance ( Instance * p_instance ) {
2020-12-03 21:09:47 +00:00
p_instance - > version + + ;
if ( p_instance - > base_type = = RS : : INSTANCE_LIGHT ) {
InstanceLightData * light = static_cast < InstanceLightData * > ( p_instance - > base_data ) ;
2022-09-12 09:44:48 +00:00
RSG : : light_storage - > light_instance_set_transform ( light - > instance , p_instance - > transform ) ;
RSG : : light_storage - > light_instance_set_aabb ( light - > instance , p_instance - > transform . xform ( p_instance - > aabb ) ) ;
2020-12-03 21:09:47 +00:00
light - > shadow_dirty = true ;
2022-04-09 09:34:31 +00:00
RS : : LightBakeMode bake_mode = RSG : : light_storage - > light_get_bake_mode ( p_instance - > base ) ;
if ( RSG : : light_storage - > light_get_type ( p_instance - > base ) ! = RS : : LIGHT_DIRECTIONAL & & bake_mode ! = light - > bake_mode ) {
2021-01-17 16:25:38 +00:00
if ( p_instance - > visible & & p_instance - > scenario & & light - > bake_mode = = RS : : LIGHT_BAKE_DYNAMIC ) {
2020-12-03 21:09:47 +00:00
p_instance - > scenario - > dynamic_lights . erase ( light - > instance ) ;
}
light - > bake_mode = bake_mode ;
2021-01-17 16:25:38 +00:00
if ( p_instance - > visible & & p_instance - > scenario & & light - > bake_mode = = RS : : LIGHT_BAKE_DYNAMIC ) {
2020-12-03 21:09:47 +00:00
p_instance - > scenario - > dynamic_lights . push_back ( light - > instance ) ;
}
}
2022-04-09 09:34:31 +00:00
uint32_t max_sdfgi_cascade = RSG : : light_storage - > light_get_max_sdfgi_cascade ( p_instance - > base ) ;
2020-12-03 21:09:47 +00:00
if ( light - > max_sdfgi_cascade ! = max_sdfgi_cascade ) {
light - > max_sdfgi_cascade = max_sdfgi_cascade ; //should most likely make sdfgi dirty in scenario
}
2020-12-31 12:42:56 +00:00
} else if ( p_instance - > base_type = = RS : : INSTANCE_REFLECTION_PROBE ) {
2020-12-03 21:09:47 +00:00
InstanceReflectionProbeData * reflection_probe = static_cast < InstanceReflectionProbeData * > ( p_instance - > base_data ) ;
2022-09-12 09:44:48 +00:00
RSG : : light_storage - > reflection_probe_instance_set_transform ( reflection_probe - > instance , p_instance - > transform ) ;
2020-12-26 12:05:36 +00:00
if ( p_instance - > scenario & & p_instance - > array_index > = 0 ) {
InstanceData & idata = p_instance - > scenario - > instance_data [ p_instance - > array_index ] ;
idata . flags | = InstanceData : : FLAG_REFLECTION_PROBE_DIRTY ;
}
2020-12-31 12:42:56 +00:00
} else if ( p_instance - > base_type = = RS : : INSTANCE_DECAL ) {
2020-12-03 21:09:47 +00:00
InstanceDecalData * decal = static_cast < InstanceDecalData * > ( p_instance - > base_data ) ;
2022-09-12 09:44:48 +00:00
RSG : : texture_storage - > decal_instance_set_transform ( decal - > instance , p_instance - > transform ) ;
2020-12-31 12:42:56 +00:00
} else if ( p_instance - > base_type = = RS : : INSTANCE_LIGHTMAP ) {
InstanceLightmapData * lightmap = static_cast < InstanceLightmapData * > ( p_instance - > base_data ) ;
2020-12-03 21:09:47 +00:00
2022-09-12 09:44:48 +00:00
RSG : : light_storage - > lightmap_instance_set_transform ( lightmap - > instance , p_instance - > transform ) ;
2021-06-04 22:47:26 +00:00
} else if ( p_instance - > base_type = = RS : : INSTANCE_VOXEL_GI ) {
InstanceVoxelGIData * voxel_gi = static_cast < InstanceVoxelGIData * > ( p_instance - > base_data ) ;
2020-12-03 21:09:47 +00:00
2021-06-04 22:47:26 +00:00
scene_render - > voxel_gi_instance_set_transform_to_data ( voxel_gi - > probe_instance , p_instance - > transform ) ;
2020-12-31 12:42:56 +00:00
} else if ( p_instance - > base_type = = RS : : INSTANCE_PARTICLES ) {
2022-04-12 11:41:50 +00:00
RSG : : particles_storage - > particles_set_emission_transform ( p_instance - > base , p_instance - > transform ) ;
2020-12-31 12:42:56 +00:00
} else if ( p_instance - > base_type = = RS : : INSTANCE_PARTICLES_COLLISION ) {
InstanceParticlesCollisionData * collision = static_cast < InstanceParticlesCollisionData * > ( p_instance - > base_data ) ;
2020-12-03 21:09:47 +00:00
//remove materials no longer used and un-own them
2022-04-12 11:41:50 +00:00
if ( RSG : : particles_storage - > particles_collision_is_heightfield ( p_instance - > base ) ) {
2020-12-03 21:09:47 +00:00
heightfield_particle_colliders_update_list . insert ( p_instance ) ;
}
2022-04-12 11:41:50 +00:00
RSG : : particles_storage - > particles_collision_instance_set_transform ( collision - > instance , p_instance - > transform ) ;
2021-10-03 11:28:55 +00:00
} else if ( p_instance - > base_type = = RS : : INSTANCE_FOG_VOLUME ) {
InstanceFogVolumeData * volume = static_cast < InstanceFogVolumeData * > ( p_instance - > base_data ) ;
scene_render - > fog_volume_instance_set_transform ( volume - > instance , p_instance - > transform ) ;
2021-04-20 16:40:24 +00:00
} else if ( p_instance - > base_type = = RS : : INSTANCE_OCCLUDER ) {
if ( p_instance - > scenario ) {
RendererSceneOcclusionCull : : get_singleton ( ) - > scenario_set_instance ( p_instance - > scenario - > self , p_instance - > self , p_instance - > base , p_instance - > transform , p_instance - > visible ) ;
}
2020-12-03 21:09:47 +00:00
}
2022-08-15 02:48:13 +00:00
if ( ! p_instance - > aabb . has_surface ( ) ) {
2020-12-03 21:09:47 +00:00
return ;
}
2020-12-31 12:42:56 +00:00
if ( p_instance - > base_type = = RS : : INSTANCE_LIGHTMAP ) {
//if this moved, update the captured objects
InstanceLightmapData * lightmap_data = static_cast < InstanceLightmapData * > ( p_instance - > base_data ) ;
//erase dependencies, since no longer a lightmap
2022-05-18 23:43:40 +00:00
for ( Instance * E : lightmap_data - > geometries ) {
Instance * geom = E ;
2020-12-31 12:42:56 +00:00
_instance_queue_update ( geom , true , false ) ;
}
}
AABB new_aabb ;
new_aabb = p_instance - > transform . xform ( p_instance - > aabb ) ;
p_instance - > transformed_aabb = new_aabb ;
2020-12-03 21:09:47 +00:00
if ( ( 1 < < p_instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( p_instance - > base_data ) ;
//make sure lights are updated if it casts shadow
if ( geom - > can_cast_shadows ) {
2022-05-18 23:43:40 +00:00
for ( const Instance * E : geom - > lights ) {
InstanceLightData * light = static_cast < InstanceLightData * > ( E - > base_data ) ;
2020-12-03 21:09:47 +00:00
light - > shadow_dirty = true ;
}
}
if ( ! p_instance - > lightmap & & geom - > lightmap_captures . size ( ) ) {
//affected by lightmap captures, must update capture info!
_update_instance_lightmap_captures ( p_instance ) ;
} else {
2020-12-15 12:04:21 +00:00
if ( ! p_instance - > lightmap_sh . is_empty ( ) ) {
2020-12-03 21:09:47 +00:00
p_instance - > lightmap_sh . clear ( ) ; //don't need SH
p_instance - > lightmap_target_sh . clear ( ) ; //don't need SH
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_lightmap_capture ( nullptr ) ;
2020-12-03 21:09:47 +00:00
}
}
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_transform ( p_instance - > transform , p_instance - > aabb , p_instance - > transformed_aabb ) ;
2020-12-03 21:09:47 +00:00
}
2020-12-30 09:56:49 +00:00
// note: we had to remove is equal approx check here, it meant that det == 0.000004 won't work, which is the case for some of our scenes.
if ( p_instance - > scenario = = nullptr | | ! p_instance - > visible | | p_instance - > transform . basis . determinant ( ) = = 0 ) {
2020-12-24 15:18:28 +00:00
p_instance - > prev_transformed_aabb = p_instance - > transformed_aabb ;
2020-12-03 21:09:47 +00:00
return ;
}
2020-12-24 15:18:28 +00:00
//quantize to improve moving object performance
AABB bvh_aabb = p_instance - > transformed_aabb ;
if ( p_instance - > indexer_id . is_valid ( ) & & bvh_aabb ! = p_instance - > prev_transformed_aabb ) {
//assume motion, see if bounds need to be quantized
AABB motion_aabb = bvh_aabb . merge ( p_instance - > prev_transformed_aabb ) ;
float motion_longest_axis = motion_aabb . get_longest_axis_size ( ) ;
float longest_axis = p_instance - > transformed_aabb . get_longest_axis_size ( ) ;
if ( motion_longest_axis < longest_axis * 2 ) {
//moved but not a lot, use motion aabb quantizing
float quantize_size = Math : : pow ( 2.0 , Math : : ceil ( Math : : log ( motion_longest_axis ) / Math : : log ( 2.0 ) ) ) * 0.5 ; //one fifth
bvh_aabb . quantize ( quantize_size ) ;
}
}
2020-12-23 16:52:58 +00:00
if ( ! p_instance - > indexer_id . is_valid ( ) ) {
if ( ( 1 < < p_instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) {
2020-12-24 15:18:28 +00:00
p_instance - > indexer_id = p_instance - > scenario - > indexers [ Scenario : : INDEXER_GEOMETRY ] . insert ( bvh_aabb , p_instance ) ;
2020-12-23 16:52:58 +00:00
} else {
2020-12-24 15:18:28 +00:00
p_instance - > indexer_id = p_instance - > scenario - > indexers [ Scenario : : INDEXER_VOLUMES ] . insert ( bvh_aabb , p_instance ) ;
2020-12-24 10:59:57 +00:00
}
2020-12-26 12:05:36 +00:00
p_instance - > array_index = p_instance - > scenario - > instance_data . size ( ) ;
InstanceData idata ;
idata . instance = p_instance ;
idata . layer_mask = p_instance - > layer_mask ;
idata . flags = p_instance - > base_type ; //changing it means de-indexing, so this never needs to be changed later
idata . base_rid = p_instance - > base ;
2021-05-09 16:23:20 +00:00
idata . parent_array_index = p_instance - > visibility_parent ? p_instance - > visibility_parent - > array_index : - 1 ;
idata . visibility_index = p_instance - > visibility_index ;
2022-05-18 23:43:40 +00:00
for ( Instance * E : p_instance - > visibility_dependencies ) {
Instance * dep_instance = E ;
2021-10-13 14:28:14 +00:00
if ( dep_instance - > array_index ! = - 1 ) {
dep_instance - > scenario - > instance_data [ dep_instance - > array_index ] . parent_array_index = p_instance - > array_index ;
}
}
2020-12-26 12:05:36 +00:00
switch ( p_instance - > base_type ) {
2020-12-31 12:42:56 +00:00
case RS : : INSTANCE_MESH :
case RS : : INSTANCE_MULTIMESH :
case RS : : INSTANCE_PARTICLES : {
2021-05-09 16:23:20 +00:00
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( p_instance - > base_data ) ;
idata . instance_geometry = geom - > geometry_instance ;
2020-12-31 12:42:56 +00:00
} break ;
2020-12-26 12:05:36 +00:00
case RS : : INSTANCE_LIGHT : {
2021-07-12 23:32:05 +00:00
InstanceLightData * light_data = static_cast < InstanceLightData * > ( p_instance - > base_data ) ;
idata . instance_data_rid = light_data - > instance . get_id ( ) ;
2022-04-09 09:34:31 +00:00
light_data - > uses_projector = RSG : : light_storage - > light_has_projector ( p_instance - > base ) ;
light_data - > uses_softshadow = RSG : : light_storage - > light_get_param ( p_instance - > base , RS : : LIGHT_PARAM_SIZE ) > CMP_EPSILON ;
2021-07-12 23:32:05 +00:00
2020-12-26 12:05:36 +00:00
} break ;
case RS : : INSTANCE_REFLECTION_PROBE : {
2020-12-31 12:42:56 +00:00
idata . instance_data_rid = static_cast < InstanceReflectionProbeData * > ( p_instance - > base_data ) - > instance . get_id ( ) ;
2020-12-26 12:05:36 +00:00
} break ;
case RS : : INSTANCE_DECAL : {
2020-12-31 12:42:56 +00:00
idata . instance_data_rid = static_cast < InstanceDecalData * > ( p_instance - > base_data ) - > instance . get_id ( ) ;
} break ;
case RS : : INSTANCE_LIGHTMAP : {
idata . instance_data_rid = static_cast < InstanceLightmapData * > ( p_instance - > base_data ) - > instance . get_id ( ) ;
2020-12-26 12:05:36 +00:00
} break ;
2021-06-04 22:47:26 +00:00
case RS : : INSTANCE_VOXEL_GI : {
idata . instance_data_rid = static_cast < InstanceVoxelGIData * > ( p_instance - > base_data ) - > probe_instance . get_id ( ) ;
2020-12-26 12:05:36 +00:00
} break ;
2021-10-03 11:28:55 +00:00
case RS : : INSTANCE_FOG_VOLUME : {
idata . instance_data_rid = static_cast < InstanceFogVolumeData * > ( p_instance - > base_data ) - > instance . get_id ( ) ;
} break ;
2021-06-16 18:43:02 +00:00
case RS : : INSTANCE_VISIBLITY_NOTIFIER : {
idata . visibility_notifier = static_cast < InstanceVisibilityNotifierData * > ( p_instance - > base_data ) ;
} break ;
2020-12-26 12:05:36 +00:00
default : {
}
}
if ( p_instance - > base_type = = RS : : INSTANCE_REFLECTION_PROBE ) {
//always dirty when added
idata . flags | = InstanceData : : FLAG_REFLECTION_PROBE_DIRTY ;
}
2021-10-25 14:04:11 +00:00
if ( p_instance - > cast_shadows ! = RS : : SHADOW_CASTING_SETTING_OFF ) {
2020-12-26 12:05:36 +00:00
idata . flags | = InstanceData : : FLAG_CAST_SHADOWS ;
}
if ( p_instance - > cast_shadows = = RS : : SHADOW_CASTING_SETTING_SHADOWS_ONLY ) {
idata . flags | = InstanceData : : FLAG_CAST_SHADOWS_ONLY ;
}
if ( p_instance - > redraw_if_visible ) {
idata . flags | = InstanceData : : FLAG_REDRAW_IF_VISIBLE ;
}
// dirty flags should not be set here, since no pairing has happened
if ( p_instance - > baked_light ) {
idata . flags | = InstanceData : : FLAG_USES_BAKED_LIGHT ;
}
if ( p_instance - > mesh_instance . is_valid ( ) ) {
idata . flags | = InstanceData : : FLAG_USES_MESH_INSTANCE ;
}
2021-04-20 16:40:24 +00:00
if ( p_instance - > ignore_occlusion_culling ) {
idata . flags | = InstanceData : : FLAG_IGNORE_OCCLUSION_CULLING ;
}
2021-10-03 11:28:55 +00:00
if ( p_instance - > ignore_all_culling ) {
idata . flags | = InstanceData : : FLAG_IGNORE_ALL_CULLING ;
}
2020-12-26 12:05:36 +00:00
p_instance - > scenario - > instance_data . push_back ( idata ) ;
p_instance - > scenario - > instance_aabbs . push_back ( InstanceBounds ( p_instance - > transformed_aabb ) ) ;
2021-05-09 16:23:20 +00:00
_update_instance_visibility_dependencies ( p_instance ) ;
2020-12-24 10:59:57 +00:00
} else {
if ( ( 1 < < p_instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) {
2020-12-24 15:18:28 +00:00
p_instance - > scenario - > indexers [ Scenario : : INDEXER_GEOMETRY ] . update ( p_instance - > indexer_id , bvh_aabb ) ;
2020-12-24 10:59:57 +00:00
} else {
2020-12-24 15:18:28 +00:00
p_instance - > scenario - > indexers [ Scenario : : INDEXER_VOLUMES ] . update ( p_instance - > indexer_id , bvh_aabb ) ;
2020-12-03 21:09:47 +00:00
}
2020-12-26 12:05:36 +00:00
p_instance - > scenario - > instance_aabbs [ p_instance - > array_index ] = InstanceBounds ( p_instance - > transformed_aabb ) ;
2020-12-23 16:52:58 +00:00
}
2020-12-03 21:09:47 +00:00
2021-05-09 16:23:20 +00:00
if ( p_instance - > visibility_index ! = - 1 ) {
2021-09-20 18:48:52 +00:00
p_instance - > scenario - > instance_visibility [ p_instance - > visibility_index ] . position = p_instance - > transformed_aabb . get_center ( ) ;
2021-05-09 16:23:20 +00:00
}
2020-12-23 16:52:58 +00:00
//move instance and repair
pair_pass + + ;
PairInstances pair ;
pair . instance = p_instance ;
pair . pair_allocator = & pair_allocator ;
pair . pair_pass = pair_pass ;
pair . pair_mask = 0 ;
2023-02-06 20:12:47 +00:00
pair . cull_mask = 0xFFFFFFFF ;
2020-12-23 16:52:58 +00:00
if ( ( 1 < < p_instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) {
pair . pair_mask | = 1 < < RS : : INSTANCE_LIGHT ;
2021-06-04 22:47:26 +00:00
pair . pair_mask | = 1 < < RS : : INSTANCE_VOXEL_GI ;
2020-12-23 16:52:58 +00:00
pair . pair_mask | = 1 < < RS : : INSTANCE_LIGHTMAP ;
2021-04-27 15:43:49 +00:00
if ( p_instance - > base_type = = RS : : INSTANCE_PARTICLES ) {
pair . pair_mask | = 1 < < RS : : INSTANCE_PARTICLES_COLLISION ;
}
2020-12-03 21:09:47 +00:00
2020-12-31 12:42:56 +00:00
pair . pair_mask | = geometry_instance_pair_mask ;
2020-12-23 16:52:58 +00:00
pair . bvh2 = & p_instance - > scenario - > indexers [ Scenario : : INDEXER_VOLUMES ] ;
} else if ( p_instance - > base_type = = RS : : INSTANCE_LIGHT ) {
pair . pair_mask | = RS : : INSTANCE_GEOMETRY_MASK ;
pair . bvh = & p_instance - > scenario - > indexers [ Scenario : : INDEXER_GEOMETRY ] ;
2022-04-09 09:34:31 +00:00
if ( RSG : : light_storage - > light_get_bake_mode ( p_instance - > base ) = = RS : : LIGHT_BAKE_DYNAMIC ) {
2021-06-04 22:47:26 +00:00
pair . pair_mask | = ( 1 < < RS : : INSTANCE_VOXEL_GI ) ;
2020-12-23 16:52:58 +00:00
pair . bvh2 = & p_instance - > scenario - > indexers [ Scenario : : INDEXER_VOLUMES ] ;
2020-12-03 21:09:47 +00:00
}
2023-02-06 20:12:47 +00:00
pair . cull_mask = RSG : : light_storage - > light_get_cull_mask ( p_instance - > base ) ;
2020-12-31 12:42:56 +00:00
} else if ( geometry_instance_pair_mask & ( 1 < < RS : : INSTANCE_REFLECTION_PROBE ) & & ( p_instance - > base_type = = RS : : INSTANCE_REFLECTION_PROBE ) ) {
pair . pair_mask = RS : : INSTANCE_GEOMETRY_MASK ;
pair . bvh = & p_instance - > scenario - > indexers [ Scenario : : INDEXER_GEOMETRY ] ;
} else if ( geometry_instance_pair_mask & ( 1 < < RS : : INSTANCE_DECAL ) & & ( p_instance - > base_type = = RS : : INSTANCE_DECAL ) ) {
2020-12-23 16:52:58 +00:00
pair . pair_mask = RS : : INSTANCE_GEOMETRY_MASK ;
pair . bvh = & p_instance - > scenario - > indexers [ Scenario : : INDEXER_GEOMETRY ] ;
2023-02-06 20:12:47 +00:00
pair . cull_mask = RSG : : texture_storage - > decal_get_cull_mask ( p_instance - > base ) ;
2020-12-23 16:52:58 +00:00
} else if ( p_instance - > base_type = = RS : : INSTANCE_PARTICLES_COLLISION ) {
pair . pair_mask = ( 1 < < RS : : INSTANCE_PARTICLES ) ;
pair . bvh = & p_instance - > scenario - > indexers [ Scenario : : INDEXER_GEOMETRY ] ;
2021-06-04 22:47:26 +00:00
} else if ( p_instance - > base_type = = RS : : INSTANCE_VOXEL_GI ) {
2020-12-23 16:52:58 +00:00
//lights and geometries
pair . pair_mask = RS : : INSTANCE_GEOMETRY_MASK | ( 1 < < RS : : INSTANCE_LIGHT ) ;
pair . bvh = & p_instance - > scenario - > indexers [ Scenario : : INDEXER_GEOMETRY ] ;
pair . bvh2 = & p_instance - > scenario - > indexers [ Scenario : : INDEXER_VOLUMES ] ;
}
2020-12-03 21:09:47 +00:00
2020-12-23 16:52:58 +00:00
pair . pair ( ) ;
2020-12-24 15:18:28 +00:00
p_instance - > prev_transformed_aabb = p_instance - > transformed_aabb ;
2020-12-23 16:52:58 +00:00
}
2020-12-03 21:09:47 +00:00
2020-12-23 16:52:58 +00:00
void RendererSceneCull : : _unpair_instance ( Instance * p_instance ) {
if ( ! p_instance - > indexer_id . is_valid ( ) ) {
return ; //nothing to do
}
while ( p_instance - > pairs . first ( ) ) {
InstancePair * pair = p_instance - > pairs . first ( ) - > self ( ) ;
Instance * other_instance = p_instance = = pair - > a ? pair - > b : pair - > a ;
_instance_unpair ( p_instance , other_instance ) ;
pair_allocator . free ( pair ) ;
}
2020-12-03 21:09:47 +00:00
2020-12-23 16:52:58 +00:00
if ( ( 1 < < p_instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) {
p_instance - > scenario - > indexers [ Scenario : : INDEXER_GEOMETRY ] . remove ( p_instance - > indexer_id ) ;
} else {
p_instance - > scenario - > indexers [ Scenario : : INDEXER_VOLUMES ] . remove ( p_instance - > indexer_id ) ;
2020-12-03 21:09:47 +00:00
}
2020-12-23 16:52:58 +00:00
p_instance - > indexer_id = DynamicBVH : : ID ( ) ;
2020-12-26 12:05:36 +00:00
//replace this by last
int32_t swap_with_index = p_instance - > scenario - > instance_data . size ( ) - 1 ;
if ( swap_with_index ! = p_instance - > array_index ) {
2021-05-09 16:23:20 +00:00
Instance * swapped_instance = p_instance - > scenario - > instance_data [ swap_with_index ] . instance ;
swapped_instance - > array_index = p_instance - > array_index ; //swap
2020-12-26 12:05:36 +00:00
p_instance - > scenario - > instance_data [ p_instance - > array_index ] = p_instance - > scenario - > instance_data [ swap_with_index ] ;
p_instance - > scenario - > instance_aabbs [ p_instance - > array_index ] = p_instance - > scenario - > instance_aabbs [ swap_with_index ] ;
2021-05-09 16:23:20 +00:00
if ( swapped_instance - > visibility_index ! = - 1 ) {
swapped_instance - > scenario - > instance_visibility [ swapped_instance - > visibility_index ] . array_index = swapped_instance - > array_index ;
}
2022-05-18 23:43:40 +00:00
for ( Instance * E : swapped_instance - > visibility_dependencies ) {
Instance * dep_instance = E ;
2021-10-13 14:28:14 +00:00
if ( dep_instance ! = p_instance & & dep_instance - > array_index ! = - 1 ) {
dep_instance - > scenario - > instance_data [ dep_instance - > array_index ] . parent_array_index = swapped_instance - > array_index ;
2021-05-09 16:23:20 +00:00
}
}
2020-12-26 12:05:36 +00:00
}
// pop last
p_instance - > scenario - > instance_data . pop_back ( ) ;
p_instance - > scenario - > instance_aabbs . pop_back ( ) ;
//uninitialize
p_instance - > array_index = - 1 ;
if ( ( 1 < < p_instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) {
// Clear these now because the InstanceData containing the dirty flags is gone
2020-12-31 12:42:56 +00:00
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( p_instance - > base_data ) ;
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2020-12-31 12:42:56 +00:00
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > pair_light_instances ( nullptr , 0 ) ;
geom - > geometry_instance - > pair_reflection_probe_instances ( nullptr , 0 ) ;
geom - > geometry_instance - > pair_decal_instances ( nullptr , 0 ) ;
geom - > geometry_instance - > pair_voxel_gi_instances ( nullptr , 0 ) ;
2021-10-13 14:28:14 +00:00
}
2021-05-09 16:23:20 +00:00
2022-05-18 23:43:40 +00:00
for ( Instance * E : p_instance - > visibility_dependencies ) {
Instance * dep_instance = E ;
2021-10-13 14:28:14 +00:00
if ( dep_instance - > array_index ! = - 1 ) {
dep_instance - > scenario - > instance_data [ dep_instance - > array_index ] . parent_array_index = - 1 ;
2021-07-20 18:17:34 +00:00
if ( ( 1 < < dep_instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) {
2022-07-23 14:01:53 +00:00
dep_instance - > scenario - > instance_data [ dep_instance - > array_index ] . instance_geometry - > set_parent_fade_alpha ( 1.0f ) ;
2021-05-09 16:23:20 +00:00
}
}
2020-12-26 12:05:36 +00:00
}
2021-05-09 16:23:20 +00:00
_update_instance_visibility_dependencies ( p_instance ) ;
2020-12-03 21:09:47 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : _update_instance_aabb ( Instance * p_instance ) {
2020-12-03 21:09:47 +00:00
AABB new_aabb ;
ERR_FAIL_COND ( p_instance - > base_type ! = RS : : INSTANCE_NONE & & ! p_instance - > base . is_valid ( ) ) ;
switch ( p_instance - > base_type ) {
case RenderingServer : : INSTANCE_NONE : {
// do nothing
} break ;
case RenderingServer : : INSTANCE_MESH : {
if ( p_instance - > custom_aabb ) {
new_aabb = * p_instance - > custom_aabb ;
} else {
2022-04-02 05:29:04 +00:00
new_aabb = RSG : : mesh_storage - > mesh_get_aabb ( p_instance - > base , p_instance - > skeleton ) ;
2020-12-03 21:09:47 +00:00
}
} break ;
case RenderingServer : : INSTANCE_MULTIMESH : {
if ( p_instance - > custom_aabb ) {
new_aabb = * p_instance - > custom_aabb ;
} else {
2022-04-02 05:29:04 +00:00
new_aabb = RSG : : mesh_storage - > multimesh_get_aabb ( p_instance - > base ) ;
2020-12-03 21:09:47 +00:00
}
} break ;
case RenderingServer : : INSTANCE_PARTICLES : {
if ( p_instance - > custom_aabb ) {
new_aabb = * p_instance - > custom_aabb ;
} else {
2022-04-12 11:41:50 +00:00
new_aabb = RSG : : particles_storage - > particles_get_aabb ( p_instance - > base ) ;
2020-12-03 21:09:47 +00:00
}
} break ;
case RenderingServer : : INSTANCE_PARTICLES_COLLISION : {
2022-04-12 11:41:50 +00:00
new_aabb = RSG : : particles_storage - > particles_collision_get_aabb ( p_instance - > base ) ;
2020-12-03 21:09:47 +00:00
} break ;
2021-10-03 11:28:55 +00:00
case RenderingServer : : INSTANCE_FOG_VOLUME : {
2022-06-21 00:08:33 +00:00
new_aabb = RSG : : fog - > fog_volume_get_aabb ( p_instance - > base ) ;
2021-10-03 11:28:55 +00:00
} break ;
2021-06-16 18:43:02 +00:00
case RenderingServer : : INSTANCE_VISIBLITY_NOTIFIER : {
2022-06-21 00:08:33 +00:00
new_aabb = RSG : : utilities - > visibility_notifier_get_aabb ( p_instance - > base ) ;
2021-06-16 18:43:02 +00:00
} break ;
2020-12-03 21:09:47 +00:00
case RenderingServer : : INSTANCE_LIGHT : {
2022-04-09 09:34:31 +00:00
new_aabb = RSG : : light_storage - > light_get_aabb ( p_instance - > base ) ;
2020-12-03 21:09:47 +00:00
} break ;
case RenderingServer : : INSTANCE_REFLECTION_PROBE : {
2022-04-09 09:34:31 +00:00
new_aabb = RSG : : light_storage - > reflection_probe_get_aabb ( p_instance - > base ) ;
2020-12-03 21:09:47 +00:00
} break ;
case RenderingServer : : INSTANCE_DECAL : {
2022-04-07 14:00:51 +00:00
new_aabb = RSG : : texture_storage - > decal_get_aabb ( p_instance - > base ) ;
2020-12-03 21:09:47 +00:00
} break ;
2021-06-04 22:47:26 +00:00
case RenderingServer : : INSTANCE_VOXEL_GI : {
2022-05-20 02:52:19 +00:00
new_aabb = RSG : : gi - > voxel_gi_get_bounds ( p_instance - > base ) ;
2020-12-03 21:09:47 +00:00
} break ;
case RenderingServer : : INSTANCE_LIGHTMAP : {
2022-04-09 09:34:31 +00:00
new_aabb = RSG : : light_storage - > lightmap_get_aabb ( p_instance - > base ) ;
2020-12-03 21:09:47 +00:00
} break ;
default : {
}
}
if ( p_instance - > extra_margin ) {
new_aabb . grow_by ( p_instance - > extra_margin ) ;
}
p_instance - > aabb = new_aabb ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : _update_instance_lightmap_captures ( Instance * p_instance ) {
2020-12-03 21:09:47 +00:00
bool first_set = p_instance - > lightmap_sh . size ( ) = = 0 ;
p_instance - > lightmap_sh . resize ( 9 ) ; //using SH
p_instance - > lightmap_target_sh . resize ( 9 ) ; //using SH
Color * instance_sh = p_instance - > lightmap_target_sh . ptrw ( ) ;
bool inside = false ;
Color accum_sh [ 9 ] ;
float accum_blend = 0.0 ;
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( p_instance - > base_data ) ;
2022-05-18 23:43:40 +00:00
for ( Instance * E : geom - > lightmap_captures ) {
Instance * lightmap = E ;
2020-12-03 21:09:47 +00:00
2022-04-09 09:34:31 +00:00
bool interior = RSG : : light_storage - > lightmap_is_interior ( lightmap - > base ) ;
2020-12-03 21:09:47 +00:00
if ( inside & & ! interior ) {
continue ; //we are inside, ignore exteriors
}
2020-10-17 05:08:21 +00:00
Transform3D to_bounds = lightmap - > transform . affine_inverse ( ) ;
2021-09-20 18:48:52 +00:00
Vector3 center = p_instance - > transform . xform ( p_instance - > aabb . get_center ( ) ) ; //use aabb center
2020-12-03 21:09:47 +00:00
Vector3 lm_pos = to_bounds . xform ( center ) ;
2022-04-09 09:34:31 +00:00
AABB bounds = RSG : : light_storage - > lightmap_get_aabb ( lightmap - > base ) ;
2020-12-03 21:09:47 +00:00
if ( ! bounds . has_point ( lm_pos ) ) {
continue ; //not in this lightmap
}
Color sh [ 9 ] ;
2022-04-09 09:34:31 +00:00
RSG : : light_storage - > lightmap_tap_sh_light ( lightmap - > base , lm_pos , sh ) ;
2020-12-03 21:09:47 +00:00
//rotate it
Basis rot = lightmap - > transform . basis . orthonormalized ( ) ;
for ( int i = 0 ; i < 3 ; i + + ) {
2021-08-09 22:15:17 +00:00
real_t csh [ 9 ] ;
2020-12-03 21:09:47 +00:00
for ( int j = 0 ; j < 9 ; j + + ) {
csh [ j ] = sh [ j ] [ i ] ;
}
rot . rotate_sh ( csh ) ;
for ( int j = 0 ; j < 9 ; j + + ) {
sh [ j ] [ i ] = csh [ j ] ;
}
}
Vector3 inner_pos = ( ( lm_pos - bounds . position ) / bounds . size ) * 2.0 - Vector3 ( 1.0 , 1.0 , 1.0 ) ;
2021-08-09 22:15:17 +00:00
real_t blend = MAX ( inner_pos . x , MAX ( inner_pos . y , inner_pos . z ) ) ;
2020-12-03 21:09:47 +00:00
//make blend more rounded
blend = Math : : lerp ( inner_pos . length ( ) , blend , blend ) ;
blend * = blend ;
blend = MAX ( 0.0 , 1.0 - blend ) ;
if ( interior & & ! inside ) {
//do not blend, just replace
for ( int j = 0 ; j < 9 ; j + + ) {
accum_sh [ j ] = sh [ j ] * blend ;
}
accum_blend = blend ;
inside = true ;
} else {
for ( int j = 0 ; j < 9 ; j + + ) {
accum_sh [ j ] + = sh [ j ] * blend ;
}
accum_blend + = blend ;
}
}
if ( accum_blend > 0.0 ) {
for ( int j = 0 ; j < 9 ; j + + ) {
instance_sh [ j ] = accum_sh [ j ] / accum_blend ;
if ( first_set ) {
p_instance - > lightmap_sh . write [ j ] = instance_sh [ j ] ;
}
}
}
2020-12-31 12:42:56 +00:00
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_lightmap_capture ( p_instance - > lightmap_sh . ptr ( ) ) ;
2020-12-03 21:09:47 +00:00
}
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
void RendererSceneCull : : _light_instance_setup_directional_shadow ( int p_shadow_index , Instance * p_instance , const Transform3D p_cam_transform , const Projection & p_cam_projection , bool p_cam_orthogonal , bool p_cam_vaspect ) {
2020-12-03 21:09:47 +00:00
InstanceLightData * light = static_cast < InstanceLightData * > ( p_instance - > base_data ) ;
2020-10-17 05:08:21 +00:00
Transform3D light_transform = p_instance - > transform ;
2020-12-03 21:09:47 +00:00
light_transform . orthonormalize ( ) ; //scale does not count on lights
2020-12-26 12:05:36 +00:00
real_t max_distance = p_cam_projection . get_z_far ( ) ;
2022-04-09 09:34:31 +00:00
real_t shadow_max = RSG : : light_storage - > light_get_param ( p_instance - > base , RS : : LIGHT_PARAM_SHADOW_MAX_DISTANCE ) ;
2020-12-26 12:05:36 +00:00
if ( shadow_max > 0 & & ! p_cam_orthogonal ) { //its impractical (and leads to unwanted behaviors) to set max distance in orthogonal camera
max_distance = MIN ( shadow_max , max_distance ) ;
}
max_distance = MAX ( max_distance , p_cam_projection . get_z_near ( ) + 0.001 ) ;
real_t min_distance = MIN ( p_cam_projection . get_z_near ( ) , max_distance ) ;
2020-12-03 21:09:47 +00:00
2022-04-09 09:34:31 +00:00
real_t pancake_size = RSG : : light_storage - > light_get_param ( p_instance - > base , RS : : LIGHT_PARAM_SHADOW_PANCAKE_SIZE ) ;
2020-12-23 16:52:58 +00:00
2020-12-26 12:05:36 +00:00
real_t range = max_distance - min_distance ;
2020-12-23 16:52:58 +00:00
2020-12-26 12:05:36 +00:00
int splits = 0 ;
2022-04-09 09:34:31 +00:00
switch ( RSG : : light_storage - > light_directional_get_shadow_mode ( p_instance - > base ) ) {
2020-12-26 12:05:36 +00:00
case RS : : LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL :
splits = 1 ;
break ;
case RS : : LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS :
splits = 2 ;
break ;
case RS : : LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS :
splits = 4 ;
break ;
}
2020-12-23 16:52:58 +00:00
2020-12-26 12:05:36 +00:00
real_t distances [ 5 ] ;
2020-12-23 16:52:58 +00:00
2020-12-26 12:05:36 +00:00
distances [ 0 ] = min_distance ;
for ( int i = 0 ; i < splits ; i + + ) {
2022-04-09 09:34:31 +00:00
distances [ i + 1 ] = min_distance + RSG : : light_storage - > light_get_param ( p_instance - > base , RS : : LightParam ( RS : : LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET + i ) ) * range ;
2020-12-26 12:05:36 +00:00
} ;
2020-12-23 16:52:58 +00:00
2020-12-26 12:05:36 +00:00
distances [ splits ] = max_distance ;
2020-12-03 21:09:47 +00:00
2022-09-12 09:44:48 +00:00
real_t texture_size = RSG : : light_storage - > get_directional_light_shadow_size ( light - > instance ) ;
2020-12-03 21:09:47 +00:00
2022-04-09 09:34:31 +00:00
bool overlap = RSG : : light_storage - > light_directional_get_blend_splits ( p_instance - > base ) ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
cull . shadow_count = p_shadow_index + 1 ;
cull . shadows [ p_shadow_index ] . cascade_count = splits ;
cull . shadows [ p_shadow_index ] . light_instance = light - > instance ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
for ( int i = 0 ; i < splits ; i + + ) {
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " Cull DirectionalLight3D, Split " + itos ( i ) ) ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
// setup a camera matrix for that range!
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
Projection camera_matrix ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
real_t aspect = p_cam_projection . get_aspect ( ) ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
if ( p_cam_orthogonal ) {
Vector2 vp_he = p_cam_projection . get_viewport_half_extents ( ) ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
camera_matrix . set_orthogonal ( vp_he . y * 2.0 , aspect , distances [ ( i = = 0 | | ! overlap ) ? i : i - 1 ] , distances [ i + 1 ] , false ) ;
} else {
real_t fov = p_cam_projection . get_fov ( ) ; //this is actually yfov, because set aspect tries to keep it
camera_matrix . set_perspective ( fov , aspect , distances [ ( i = = 0 | | ! overlap ) ? i : i - 1 ] , distances [ i + 1 ] , true ) ;
}
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
//obtain the frustum endpoints
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
Vector3 endpoints [ 8 ] ; // frustum plane endpoints
bool res = camera_matrix . get_endpoints ( p_cam_transform , endpoints ) ;
ERR_CONTINUE ( ! res ) ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
// obtain the light frustum ranges (given endpoints)
2020-12-03 21:09:47 +00:00
2020-10-17 05:08:21 +00:00
Transform3D transform = light_transform ; //discard scale and stabilize light
2020-12-03 21:09:47 +00:00
2022-05-03 12:50:35 +00:00
Vector3 x_vec = transform . basis . get_column ( Vector3 : : AXIS_X ) . normalized ( ) ;
Vector3 y_vec = transform . basis . get_column ( Vector3 : : AXIS_Y ) . normalized ( ) ;
Vector3 z_vec = transform . basis . get_column ( Vector3 : : AXIS_Z ) . normalized ( ) ;
2020-12-26 12:05:36 +00:00
//z_vec points against the camera, like in default opengl
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
real_t x_min = 0.f , x_max = 0.f ;
real_t y_min = 0.f , y_max = 0.f ;
real_t z_min = 0.f , z_max = 0.f ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
// FIXME: z_max_cam is defined, computed, but not used below when setting up
// ortho_camera. Commented out for now to fix warnings but should be investigated.
real_t x_min_cam = 0.f , x_max_cam = 0.f ;
real_t y_min_cam = 0.f , y_max_cam = 0.f ;
real_t z_min_cam = 0.f ;
//real_t z_max_cam = 0.f;
2020-12-03 21:09:47 +00:00
2021-07-29 16:30:39 +00:00
//real_t bias_scale = 1.0;
//real_t aspect_bias_scale = 1.0;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
//used for culling
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
for ( int j = 0 ; j < 8 ; j + + ) {
real_t d_x = x_vec . dot ( endpoints [ j ] ) ;
real_t d_y = y_vec . dot ( endpoints [ j ] ) ;
real_t d_z = z_vec . dot ( endpoints [ j ] ) ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
if ( j = = 0 | | d_x < x_min ) {
x_min = d_x ;
}
if ( j = = 0 | | d_x > x_max ) {
x_max = d_x ;
}
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
if ( j = = 0 | | d_y < y_min ) {
y_min = d_y ;
}
if ( j = = 0 | | d_y > y_max ) {
y_max = d_y ;
}
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
if ( j = = 0 | | d_z < z_min ) {
z_min = d_z ;
}
if ( j = = 0 | | d_z > z_max ) {
z_max = d_z ;
}
}
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
real_t radius = 0 ;
real_t soft_shadow_expand = 0 ;
Vector3 center ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
{
//camera viewport stuff
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
for ( int j = 0 ; j < 8 ; j + + ) {
center + = endpoints [ j ] ;
}
center / = 8.0 ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
//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;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
for ( int j = 0 ; j < 8 ; j + + ) {
real_t d = center . distance_to ( endpoints [ j ] ) ;
if ( d > radius ) {
radius = d ;
2020-12-03 21:09:47 +00:00
}
2020-12-26 12:05:36 +00:00
}
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
radius * = texture_size / ( texture_size - 2.0 ) ; //add a texel by each side
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
z_min_cam = z_vec . dot ( center ) - radius ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
{
2022-04-09 09:34:31 +00:00
float soft_shadow_angle = RSG : : light_storage - > light_get_param ( p_instance - > base , RS : : LIGHT_PARAM_SIZE ) ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
if ( soft_shadow_angle > 0.0 ) {
float z_range = ( z_vec . dot ( center ) + radius + pancake_size ) - z_min_cam ;
2022-08-13 15:45:42 +00:00
soft_shadow_expand = Math : : tan ( Math : : deg_to_rad ( soft_shadow_angle ) ) * z_range ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
x_max + = soft_shadow_expand ;
y_max + = soft_shadow_expand ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
x_min - = soft_shadow_expand ;
y_min - = soft_shadow_expand ;
2020-12-03 21:09:47 +00:00
}
2020-12-26 12:05:36 +00:00
}
2020-12-03 21:09:47 +00:00
2021-07-02 18:32:43 +00:00
// This trick here is what stabilizes the shadow (make potential jaggies to not move)
// at the cost of some wasted resolution. Still, the quality increase is very well worth it.
2021-10-12 23:16:23 +00:00
const real_t unit = ( radius + soft_shadow_expand ) * 2.0 / texture_size ;
2021-07-02 18:32:43 +00:00
x_max_cam = Math : : snapped ( x_vec . dot ( center ) + radius + soft_shadow_expand , unit ) ;
x_min_cam = Math : : snapped ( x_vec . dot ( center ) - radius - soft_shadow_expand , unit ) ;
y_max_cam = Math : : snapped ( y_vec . dot ( center ) + radius + soft_shadow_expand , unit ) ;
y_min_cam = Math : : snapped ( y_vec . dot ( center ) - radius - soft_shadow_expand , unit ) ;
2020-12-26 12:05:36 +00:00
}
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
//now that we know all ranges, we can proceed to make the light frustum planes, for culling octree
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
Vector < Plane > light_frustum_planes ;
light_frustum_planes . resize ( 6 ) ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
//right/left
light_frustum_planes . write [ 0 ] = Plane ( x_vec , x_max ) ;
light_frustum_planes . write [ 1 ] = Plane ( - x_vec , - x_min ) ;
//top/bottom
light_frustum_planes . write [ 2 ] = Plane ( y_vec , y_max ) ;
light_frustum_planes . write [ 3 ] = Plane ( - y_vec , - y_min ) ;
//near/far
light_frustum_planes . write [ 4 ] = Plane ( z_vec , z_max + 1e6 ) ;
light_frustum_planes . write [ 5 ] = Plane ( - z_vec , - z_min ) ; // z_min is ok, since casters further than far-light plane are not needed
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
// a pre pass will need to be needed to determine the actual z-near to be used
2020-12-16 14:07:08 +00:00
2021-07-29 16:30:39 +00:00
z_max = z_vec . dot ( center ) + radius + pancake_size ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
{
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
Projection ortho_camera ;
2020-12-26 12:05:36 +00:00
real_t half_x = ( x_max_cam - x_min_cam ) * 0.5 ;
real_t half_y = ( y_max_cam - y_min_cam ) * 0.5 ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
ortho_camera . set_orthogonal ( - half_x , half_x , - half_y , half_y , 0 , ( z_max - z_min_cam ) ) ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
Vector2 uv_scale ( 1.0 / ( x_max_cam - x_min_cam ) , 1.0 / ( y_max_cam - y_min_cam ) ) ;
2020-12-03 21:09:47 +00:00
2020-10-17 05:08:21 +00:00
Transform3D ortho_transform ;
2020-12-26 12:05:36 +00:00
ortho_transform . basis = transform . basis ;
ortho_transform . origin = x_vec * ( x_min_cam + half_x ) + y_vec * ( y_min_cam + half_y ) + z_vec * z_max ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
cull . shadows [ p_shadow_index ] . cascades [ i ] . frustum = Frustum ( light_frustum_planes ) ;
cull . shadows [ p_shadow_index ] . cascades [ i ] . projection = ortho_camera ;
cull . shadows [ p_shadow_index ] . cascades [ i ] . transform = ortho_transform ;
cull . shadows [ p_shadow_index ] . cascades [ i ] . zfar = z_max - z_min_cam ;
cull . shadows [ p_shadow_index ] . cascades [ i ] . split = distances [ i + 1 ] ;
cull . shadows [ p_shadow_index ] . cascades [ i ] . shadow_texel_size = radius * 2.0 / texture_size ;
2021-07-29 16:30:39 +00:00
cull . shadows [ p_shadow_index ] . cascades [ i ] . bias_scale = ( z_max - z_min_cam ) ;
2020-12-26 12:05:36 +00:00
cull . shadows [ p_shadow_index ] . cascades [ i ] . range_begin = z_max ;
cull . shadows [ p_shadow_index ] . cascades [ i ] . uv_scale = uv_scale ;
}
}
}
2020-12-03 21:09:47 +00:00
2022-12-27 17:43:53 +00:00
bool RendererSceneCull : : _light_instance_update_shadow ( Instance * p_instance , const Transform3D p_cam_transform , const Projection & p_cam_projection , bool p_cam_orthogonal , bool p_cam_vaspect , RID p_shadow_atlas , Scenario * p_scenario , float p_screen_mesh_lod_threshold , uint32_t p_visible_layers ) {
2020-12-26 12:05:36 +00:00
InstanceLightData * light = static_cast < InstanceLightData * > ( p_instance - > base_data ) ;
2020-12-03 21:09:47 +00:00
2020-10-17 05:08:21 +00:00
Transform3D light_transform = p_instance - > transform ;
2020-12-26 12:05:36 +00:00
light_transform . orthonormalize ( ) ; //scale does not count on lights
2020-12-16 14:07:08 +00:00
2020-12-26 12:05:36 +00:00
bool animated_material_found = false ;
2020-12-03 21:09:47 +00:00
2022-04-09 09:34:31 +00:00
switch ( RSG : : light_storage - > light_get_type ( p_instance - > base ) ) {
2020-12-26 12:05:36 +00:00
case RS : : LIGHT_DIRECTIONAL : {
2020-12-03 21:09:47 +00:00
} break ;
case RS : : LIGHT_OMNI : {
2022-04-09 09:34:31 +00:00
RS : : LightOmniShadowMode shadow_mode = RSG : : light_storage - > light_omni_get_shadow_mode ( p_instance - > base ) ;
2020-12-03 21:09:47 +00:00
2022-09-12 09:44:48 +00:00
if ( shadow_mode = = RS : : LIGHT_OMNI_SHADOW_DUAL_PARABOLOID | | ! RSG : : light_storage - > light_instances_can_render_shadow_cube ( ) ) {
2021-02-02 19:51:36 +00:00
if ( max_shadows_used + 2 > MAX_UPDATE_SHADOWS ) {
return true ;
}
2020-12-03 21:09:47 +00:00
for ( int i = 0 ; i < 2 ; i + + ) {
//using this one ensures that raster deferred will have it
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " Cull OmniLight3D Shadow Paraboloid, Half " + itos ( i ) ) ;
2020-12-03 21:09:47 +00:00
2022-04-09 09:34:31 +00:00
real_t radius = RSG : : light_storage - > light_get_param ( p_instance - > base , RS : : LIGHT_PARAM_RANGE ) ;
2020-12-03 21:09:47 +00:00
real_t z = i = = 0 ? - 1 : 1 ;
Vector < Plane > planes ;
planes . resize ( 6 ) ;
planes . write [ 0 ] = light_transform . xform ( Plane ( Vector3 ( 0 , 0 , z ) , radius ) ) ;
planes . write [ 1 ] = light_transform . xform ( Plane ( Vector3 ( 1 , 0 , z ) . normalized ( ) , radius ) ) ;
planes . write [ 2 ] = light_transform . xform ( Plane ( Vector3 ( - 1 , 0 , z ) . normalized ( ) , radius ) ) ;
planes . write [ 3 ] = light_transform . xform ( Plane ( Vector3 ( 0 , 1 , z ) . normalized ( ) , radius ) ) ;
planes . write [ 4 ] = light_transform . xform ( Plane ( Vector3 ( 0 , - 1 , z ) . normalized ( ) , radius ) ) ;
planes . write [ 5 ] = light_transform . xform ( Plane ( Vector3 ( 0 , 0 , - z ) , 0 ) ) ;
2020-12-23 16:52:58 +00:00
instance_shadow_cull_result . clear ( ) ;
Vector < Vector3 > points = Geometry3D : : compute_convex_mesh_points ( & planes [ 0 ] , planes . size ( ) ) ;
struct CullConvex {
PagedArray < Instance * > * result ;
_FORCE_INLINE_ bool operator ( ) ( void * p_data ) {
Instance * p_instance = ( Instance * ) p_data ;
result - > push_back ( p_instance ) ;
return false ;
}
} ;
CullConvex cull_convex ;
cull_convex . result = & instance_shadow_cull_result ;
p_scenario - > indexers [ Scenario : : INDEXER_GEOMETRY ] . convex_query ( planes . ptr ( ) , planes . size ( ) , points . ptr ( ) , points . size ( ) , cull_convex ) ;
2021-02-02 19:51:36 +00:00
RendererSceneRender : : RenderShadowData & shadow_data = render_shadow_data [ max_shadows_used + + ] ;
2020-12-23 16:52:58 +00:00
for ( int j = 0 ; j < ( int ) instance_shadow_cull_result . size ( ) ; j + + ) {
2020-12-03 21:09:47 +00:00
Instance * instance = instance_shadow_cull_result [ j ] ;
2022-12-27 17:43:53 +00:00
if ( ! instance - > visible | | ! ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) | | ! static_cast < InstanceGeometryData * > ( instance - > base_data ) - > can_cast_shadows | | ! ( p_visible_layers & instance - > layer_mask ) ) {
2020-12-23 16:52:58 +00:00
continue ;
2020-12-03 21:09:47 +00:00
} else {
if ( static_cast < InstanceGeometryData * > ( instance - > base_data ) - > material_is_animated ) {
animated_material_found = true ;
}
2020-12-16 14:07:08 +00:00
if ( instance - > mesh_instance . is_valid ( ) ) {
2022-04-02 05:29:04 +00:00
RSG : : mesh_storage - > mesh_instance_check_for_update ( instance - > mesh_instance ) ;
2020-12-16 14:07:08 +00:00
}
2020-12-03 21:09:47 +00:00
}
2020-12-23 16:52:58 +00:00
2021-02-02 19:51:36 +00:00
shadow_data . instances . push_back ( static_cast < InstanceGeometryData * > ( instance - > base_data ) - > geometry_instance ) ;
2020-12-03 21:09:47 +00:00
}
2022-04-02 05:29:04 +00:00
RSG : : mesh_storage - > update_mesh_instances ( ) ;
2020-12-16 14:07:08 +00:00
2022-09-12 09:44:48 +00:00
RSG : : light_storage - > light_instance_set_shadow_transform ( light - > instance , Projection ( ) , light_transform , radius , 0 , i , 0 ) ;
2021-02-02 19:51:36 +00:00
shadow_data . light = light - > instance ;
shadow_data . pass = i ;
2020-12-03 21:09:47 +00:00
}
} else { //shadow cube
2021-02-02 19:51:36 +00:00
if ( max_shadows_used + 6 > MAX_UPDATE_SHADOWS ) {
return true ;
}
2022-04-09 09:34:31 +00:00
real_t radius = RSG : : light_storage - > light_get_param ( p_instance - > base , RS : : LIGHT_PARAM_RANGE ) ;
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
Projection cm ;
2021-08-04 15:18:06 +00:00
cm . set_perspective ( 90 , 1 , radius * 0.005f , radius ) ;
2020-12-03 21:09:47 +00:00
for ( int i = 0 ; i < 6 ; i + + ) {
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " Cull OmniLight3D Shadow Cube, Side " + itos ( i ) ) ;
2020-12-03 21:09:47 +00:00
//using this one ensures that raster deferred will have it
static const Vector3 view_normals [ 6 ] = {
Vector3 ( + 1 , 0 , 0 ) ,
Vector3 ( - 1 , 0 , 0 ) ,
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , + 1 , 0 ) ,
Vector3 ( 0 , 0 , + 1 ) ,
Vector3 ( 0 , 0 , - 1 )
} ;
static const Vector3 view_up [ 6 ] = {
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , 0 , - 1 ) ,
Vector3 ( 0 , 0 , + 1 ) ,
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , - 1 , 0 )
} ;
2020-10-17 05:08:21 +00:00
Transform3D xform = light_transform * Transform3D ( ) . looking_at ( view_normals [ i ] , view_up [ i ] ) ;
2020-12-03 21:09:47 +00:00
Vector < Plane > planes = cm . get_projection_planes ( xform ) ;
2020-12-23 16:52:58 +00:00
instance_shadow_cull_result . clear ( ) ;
Vector < Vector3 > points = Geometry3D : : compute_convex_mesh_points ( & planes [ 0 ] , planes . size ( ) ) ;
struct CullConvex {
PagedArray < Instance * > * result ;
_FORCE_INLINE_ bool operator ( ) ( void * p_data ) {
Instance * p_instance = ( Instance * ) p_data ;
result - > push_back ( p_instance ) ;
return false ;
}
} ;
CullConvex cull_convex ;
cull_convex . result = & instance_shadow_cull_result ;
p_scenario - > indexers [ Scenario : : INDEXER_GEOMETRY ] . convex_query ( planes . ptr ( ) , planes . size ( ) , points . ptr ( ) , points . size ( ) , cull_convex ) ;
2020-12-03 21:09:47 +00:00
2021-02-02 19:51:36 +00:00
RendererSceneRender : : RenderShadowData & shadow_data = render_shadow_data [ max_shadows_used + + ] ;
2020-12-23 16:52:58 +00:00
for ( int j = 0 ; j < ( int ) instance_shadow_cull_result . size ( ) ; j + + ) {
2020-12-03 21:09:47 +00:00
Instance * instance = instance_shadow_cull_result [ j ] ;
2022-12-27 17:43:53 +00:00
if ( ! instance - > visible | | ! ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) | | ! static_cast < InstanceGeometryData * > ( instance - > base_data ) - > can_cast_shadows | | ! ( p_visible_layers & instance - > layer_mask ) ) {
2020-12-23 16:52:58 +00:00
continue ;
2020-12-03 21:09:47 +00:00
} else {
if ( static_cast < InstanceGeometryData * > ( instance - > base_data ) - > material_is_animated ) {
animated_material_found = true ;
}
2020-12-16 14:07:08 +00:00
if ( instance - > mesh_instance . is_valid ( ) ) {
2022-04-02 05:29:04 +00:00
RSG : : mesh_storage - > mesh_instance_check_for_update ( instance - > mesh_instance ) ;
2020-12-16 14:07:08 +00:00
}
2020-12-03 21:09:47 +00:00
}
2020-12-23 16:52:58 +00:00
2021-02-02 19:51:36 +00:00
shadow_data . instances . push_back ( static_cast < InstanceGeometryData * > ( instance - > base_data ) - > geometry_instance ) ;
2020-12-03 21:09:47 +00:00
}
2022-04-02 05:29:04 +00:00
RSG : : mesh_storage - > update_mesh_instances ( ) ;
2022-09-12 09:44:48 +00:00
RSG : : light_storage - > light_instance_set_shadow_transform ( light - > instance , cm , xform , radius , 0 , i , 0 ) ;
2021-02-02 19:51:36 +00:00
shadow_data . light = light - > instance ;
shadow_data . pass = i ;
2020-12-03 21:09:47 +00:00
}
//restore the regular DP matrix
2022-09-12 09:44:48 +00:00
//RSG::light_storage->light_instance_set_shadow_transform(light->instance, Projection(), light_transform, radius, 0, 0, 0);
2020-12-03 21:09:47 +00:00
}
} break ;
case RS : : LIGHT_SPOT : {
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " Cull SpotLight3D Shadow " ) ;
2020-12-03 21:09:47 +00:00
2021-02-02 19:51:36 +00:00
if ( max_shadows_used + 1 > MAX_UPDATE_SHADOWS ) {
return true ;
}
2022-04-09 09:34:31 +00:00
real_t radius = RSG : : light_storage - > light_get_param ( p_instance - > base , RS : : LIGHT_PARAM_RANGE ) ;
real_t angle = RSG : : light_storage - > light_get_param ( p_instance - > base , RS : : LIGHT_PARAM_SPOT_ANGLE ) ;
2020-12-03 21:09:47 +00:00
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
Projection cm ;
2021-08-04 15:18:06 +00:00
cm . set_perspective ( angle * 2.0 , 1.0 , 0.005f * radius , radius ) ;
2020-12-03 21:09:47 +00:00
Vector < Plane > planes = cm . get_projection_planes ( light_transform ) ;
2020-12-23 16:52:58 +00:00
instance_shadow_cull_result . clear ( ) ;
Vector < Vector3 > points = Geometry3D : : compute_convex_mesh_points ( & planes [ 0 ] , planes . size ( ) ) ;
struct CullConvex {
PagedArray < Instance * > * result ;
_FORCE_INLINE_ bool operator ( ) ( void * p_data ) {
Instance * p_instance = ( Instance * ) p_data ;
result - > push_back ( p_instance ) ;
return false ;
}
} ;
CullConvex cull_convex ;
cull_convex . result = & instance_shadow_cull_result ;
p_scenario - > indexers [ Scenario : : INDEXER_GEOMETRY ] . convex_query ( planes . ptr ( ) , planes . size ( ) , points . ptr ( ) , points . size ( ) , cull_convex ) ;
2020-12-03 21:09:47 +00:00
2021-02-02 19:51:36 +00:00
RendererSceneRender : : RenderShadowData & shadow_data = render_shadow_data [ max_shadows_used + + ] ;
2020-12-23 16:52:58 +00:00
for ( int j = 0 ; j < ( int ) instance_shadow_cull_result . size ( ) ; j + + ) {
2020-12-03 21:09:47 +00:00
Instance * instance = instance_shadow_cull_result [ j ] ;
2022-12-27 17:43:53 +00:00
if ( ! instance - > visible | | ! ( ( 1 < < instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) | | ! static_cast < InstanceGeometryData * > ( instance - > base_data ) - > can_cast_shadows | | ! ( p_visible_layers & instance - > layer_mask ) ) {
2020-12-23 16:52:58 +00:00
continue ;
2020-12-03 21:09:47 +00:00
} else {
if ( static_cast < InstanceGeometryData * > ( instance - > base_data ) - > material_is_animated ) {
animated_material_found = true ;
}
2020-12-16 14:07:08 +00:00
if ( instance - > mesh_instance . is_valid ( ) ) {
2022-04-02 05:29:04 +00:00
RSG : : mesh_storage - > mesh_instance_check_for_update ( instance - > mesh_instance ) ;
2020-12-16 14:07:08 +00:00
}
2020-12-03 21:09:47 +00:00
}
2021-02-02 19:51:36 +00:00
shadow_data . instances . push_back ( static_cast < InstanceGeometryData * > ( instance - > base_data ) - > geometry_instance ) ;
2020-12-03 21:09:47 +00:00
}
2022-04-02 05:29:04 +00:00
RSG : : mesh_storage - > update_mesh_instances ( ) ;
2020-12-16 14:07:08 +00:00
2022-09-12 09:44:48 +00:00
RSG : : light_storage - > light_instance_set_shadow_transform ( light - > instance , cm , light_transform , radius , 0 , 0 , 0 ) ;
2021-02-02 19:51:36 +00:00
shadow_data . light = light - > instance ;
shadow_data . pass = 0 ;
2020-12-03 21:09:47 +00:00
} break ;
}
return animated_material_found ;
}
2022-08-04 08:40:39 +00:00
void RendererSceneCull : : render_camera ( const Ref < RenderSceneBuffers > & p_render_buffers , RID p_camera , RID p_scenario , RID p_viewport , Size2 p_viewport_size , bool p_use_taa , float p_screen_mesh_lod_threshold , RID p_shadow_atlas , Ref < XRInterface > & p_xr_interface , RenderInfo * r_render_info ) {
2020-12-03 21:09:47 +00:00
# ifndef _3D_DISABLED
2021-09-29 17:08:41 +00:00
Camera * camera = camera_owner . get_or_null ( p_camera ) ;
2020-12-03 21:09:47 +00:00
ERR_FAIL_COND ( ! camera ) ;
2022-04-04 14:10:22 +00:00
Vector2 jitter ;
if ( p_use_taa ) {
jitter = taa_jitter_array [ RSG : : rasterizer - > get_frame_number ( ) % TAA_JITTER_COUNT ] / p_viewport_size ;
}
2021-05-07 13:19:04 +00:00
RendererSceneRender : : CameraData camera_data ;
// Setup Camera(s)
if ( p_xr_interface . is_null ( ) ) {
// Normal camera
Transform3D transform = camera - > transform ;
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
Projection projection ;
2021-05-07 13:19:04 +00:00
bool vaspect = camera - > vaspect ;
2022-04-26 19:49:44 +00:00
bool is_orthogonal = false ;
2021-05-07 13:19:04 +00:00
switch ( camera - > type ) {
case Camera : : ORTHOGONAL : {
projection . set_orthogonal (
camera - > size ,
p_viewport_size . width / ( float ) p_viewport_size . height ,
camera - > znear ,
camera - > zfar ,
camera - > vaspect ) ;
2022-04-26 19:49:44 +00:00
is_orthogonal = true ;
2021-05-07 13:19:04 +00:00
} break ;
case Camera : : PERSPECTIVE : {
projection . set_perspective (
camera - > fov ,
p_viewport_size . width / ( float ) p_viewport_size . height ,
camera - > znear ,
camera - > zfar ,
camera - > vaspect ) ;
2020-12-03 21:09:47 +00:00
2021-05-07 13:19:04 +00:00
} break ;
case Camera : : FRUSTUM : {
projection . set_frustum (
camera - > size ,
p_viewport_size . width / ( float ) p_viewport_size . height ,
camera - > offset ,
camera - > znear ,
camera - > zfar ,
camera - > vaspect ) ;
} break ;
}
2020-12-03 21:09:47 +00:00
2022-10-13 02:33:06 +00:00
camera_data . set_camera ( transform , projection , is_orthogonal , vaspect , jitter , camera - > visible_layers ) ;
2021-05-07 13:19:04 +00:00
} else {
// Setup our camera for our XR interface.
// We can support multiple views here each with their own camera
Transform3D transforms [ RendererSceneRender : : MAX_RENDER_VIEWS ] ;
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
Projection projections [ RendererSceneRender : : MAX_RENDER_VIEWS ] ;
2020-12-03 21:09:47 +00:00
2021-05-07 13:19:04 +00:00
uint32_t view_count = p_xr_interface - > get_view_count ( ) ;
2022-11-19 02:43:11 +00:00
ERR_FAIL_COND_MSG ( view_count = = 0 | | view_count > RendererSceneRender : : MAX_RENDER_VIEWS , " Requested view count is not supported " ) ;
2021-04-20 16:40:24 +00:00
2021-05-07 13:19:04 +00:00
float aspect = p_viewport_size . width / ( float ) p_viewport_size . height ;
2020-12-03 21:09:47 +00:00
2021-05-07 13:19:04 +00:00
Transform3D world_origin = XRServer : : get_singleton ( ) - > get_world_origin ( ) ;
2020-12-03 21:09:47 +00:00
2021-05-07 13:19:04 +00:00
// We ignore our camera position, it will have been positioned with a slightly old tracking position.
// Instead we take our origin point and have our XR interface add fresh tracking data! Whoohoo!
for ( uint32_t v = 0 ; v < view_count ; v + + ) {
transforms [ v ] = p_xr_interface - > get_transform_for_view ( v , world_origin ) ;
projections [ v ] = p_xr_interface - > get_projection_for_view ( v , aspect , camera - > znear , camera - > zfar ) ;
}
2020-12-03 21:09:47 +00:00
2021-05-07 13:19:04 +00:00
if ( view_count = = 1 ) {
2022-10-13 02:33:06 +00:00
camera_data . set_camera ( transforms [ 0 ] , projections [ 0 ] , false , camera - > vaspect , jitter , camera - > visible_layers ) ;
2021-05-07 13:19:04 +00:00
} else if ( view_count = = 2 ) {
camera_data . set_multiview_camera ( view_count , transforms , projections , false , camera - > vaspect ) ;
} else {
// this won't be called (see fail check above) but keeping this comment to indicate we may support more then 2 views in the future...
}
}
2020-12-03 21:09:47 +00:00
RID environment = _render_get_environment ( p_camera , p_scenario ) ;
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " Update Occlusion Buffer " )
2021-05-07 13:19:04 +00:00
// For now just cull on the first camera
2022-07-23 17:12:41 +00:00
RendererSceneOcclusionCull : : get_singleton ( ) - > buffer_update ( p_viewport , camera_data . main_transform , camera_data . main_projection , camera_data . is_orthogonal ) ;
2021-05-07 13:19:04 +00:00
2022-07-31 23:20:24 +00:00
_render_scene ( & camera_data , p_render_buffers , environment , camera - > attributes , camera - > visible_layers , p_scenario , p_viewport , p_shadow_atlas , RID ( ) , - 1 , p_screen_mesh_lod_threshold , true , r_render_info ) ;
2021-02-02 19:51:36 +00:00
# endif
2021-05-07 13:19:04 +00:00
}
2020-12-03 21:09:47 +00:00
2021-05-09 16:23:20 +00:00
void RendererSceneCull : : _visibility_cull_threaded ( uint32_t p_thread , VisibilityCullData * cull_data ) {
2022-07-23 17:12:41 +00:00
uint32_t total_threads = WorkerThreadPool : : get_singleton ( ) - > get_thread_count ( ) ;
2021-05-09 16:23:20 +00:00
uint32_t bin_from = p_thread * cull_data - > cull_count / total_threads ;
uint32_t bin_to = ( p_thread + 1 = = total_threads ) ? cull_data - > cull_count : ( ( p_thread + 1 ) * cull_data - > cull_count / total_threads ) ;
_visibility_cull ( * cull_data , cull_data - > cull_offset + bin_from , cull_data - > cull_offset + bin_to ) ;
}
void RendererSceneCull : : _visibility_cull ( const VisibilityCullData & cull_data , uint64_t p_from , uint64_t p_to ) {
Scenario * scenario = cull_data . scenario ;
for ( unsigned int i = p_from ; i < p_to ; i + + ) {
InstanceVisibilityData & vd = scenario - > instance_visibility [ i ] ;
InstanceData & idata = scenario - > instance_data [ vd . array_index ] ;
if ( idata . parent_array_index > = 0 ) {
uint32_t parent_flags = scenario - > instance_data [ idata . parent_array_index ] . flags ;
2021-07-20 18:17:34 +00:00
if ( ( parent_flags & InstanceData : : FLAG_VISIBILITY_DEPENDENCY_HIDDEN ) | | ! ( parent_flags & ( InstanceData : : FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE | InstanceData : : FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN ) ) ) {
2021-05-09 16:23:20 +00:00
idata . flags | = InstanceData : : FLAG_VISIBILITY_DEPENDENCY_HIDDEN ;
idata . flags & = ~ InstanceData : : FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE ;
2021-07-20 18:17:34 +00:00
idata . flags & = ~ InstanceData : : FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN ;
2021-05-09 16:23:20 +00:00
continue ;
}
}
2021-07-20 18:17:34 +00:00
int range_check = _visibility_range_check < true > ( vd , cull_data . camera_position , cull_data . viewport_mask ) ;
2021-05-09 16:23:20 +00:00
if ( range_check = = - 1 ) {
idata . flags | = InstanceData : : FLAG_VISIBILITY_DEPENDENCY_HIDDEN ;
idata . flags & = ~ InstanceData : : FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE ;
2021-07-20 18:17:34 +00:00
idata . flags & = ~ InstanceData : : FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN ;
2021-05-09 16:23:20 +00:00
} else if ( range_check = = 1 ) {
idata . flags & = ~ InstanceData : : FLAG_VISIBILITY_DEPENDENCY_HIDDEN ;
idata . flags | = InstanceData : : FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE ;
2021-07-20 18:17:34 +00:00
idata . flags & = ~ InstanceData : : FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN ;
2021-05-09 16:23:20 +00:00
} else {
idata . flags & = ~ InstanceData : : FLAG_VISIBILITY_DEPENDENCY_HIDDEN ;
idata . flags & = ~ InstanceData : : FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE ;
2021-07-20 18:17:34 +00:00
if ( range_check = = 2 ) {
idata . flags | = InstanceData : : FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN ;
} else {
idata . flags & = ~ InstanceData : : FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN ;
}
2021-05-09 16:23:20 +00:00
}
}
}
2021-07-20 18:17:34 +00:00
template < bool p_fade_check >
2021-05-09 16:23:20 +00:00
int RendererSceneCull : : _visibility_range_check ( InstanceVisibilityData & r_vis_data , const Vector3 & p_camera_pos , uint64_t p_viewport_mask ) {
float dist = p_camera_pos . distance_to ( r_vis_data . position ) ;
2021-07-20 18:17:34 +00:00
const RS : : VisibilityRangeFadeMode & fade_mode = r_vis_data . fade_mode ;
2021-05-09 16:23:20 +00:00
2021-07-20 18:17:34 +00:00
float begin_offset = - r_vis_data . range_begin_margin ;
float end_offset = r_vis_data . range_end_margin ;
if ( fade_mode = = RS : : VISIBILITY_RANGE_FADE_DISABLED & & ! ( p_viewport_mask & r_vis_data . viewport_state ) ) {
begin_offset = - begin_offset ;
end_offset = - end_offset ;
}
2021-05-09 16:23:20 +00:00
if ( r_vis_data . range_end > 0.0f & & dist > r_vis_data . range_end + end_offset ) {
r_vis_data . viewport_state & = ~ p_viewport_mask ;
return - 1 ;
} else if ( r_vis_data . range_begin > 0.0f & & dist < r_vis_data . range_begin + begin_offset ) {
r_vis_data . viewport_state & = ~ p_viewport_mask ;
return 1 ;
} else {
r_vis_data . viewport_state | = p_viewport_mask ;
2021-07-20 18:17:34 +00:00
if ( p_fade_check ) {
if ( fade_mode ! = RS : : VISIBILITY_RANGE_FADE_DISABLED ) {
r_vis_data . children_fade_alpha = 1.0f ;
if ( r_vis_data . range_end > 0.0f & & dist > r_vis_data . range_end - end_offset ) {
if ( fade_mode = = RS : : VISIBILITY_RANGE_FADE_DEPENDENCIES ) {
r_vis_data . children_fade_alpha = MIN ( 1.0f , ( dist - ( r_vis_data . range_end - end_offset ) ) / ( 2.0f * r_vis_data . range_end_margin ) ) ;
}
return 2 ;
} else if ( r_vis_data . range_begin > 0.0f & & dist < r_vis_data . range_begin - begin_offset ) {
if ( fade_mode = = RS : : VISIBILITY_RANGE_FADE_DEPENDENCIES ) {
r_vis_data . children_fade_alpha = MIN ( 1.0f , 1.0 - ( dist - ( r_vis_data . range_begin + begin_offset ) ) / ( 2.0f * r_vis_data . range_begin_margin ) ) ;
}
return 2 ;
}
}
}
2021-05-09 16:23:20 +00:00
return 0 ;
}
}
2021-07-20 18:17:34 +00:00
bool RendererSceneCull : : _visibility_parent_check ( const CullData & p_cull_data , const InstanceData & p_instance_data ) {
if ( p_instance_data . parent_array_index = = - 1 ) {
return true ;
}
const uint32_t & parent_flags = p_cull_data . scenario - > instance_data [ p_instance_data . parent_array_index ] . flags ;
return ( ( parent_flags & InstanceData : : FLAG_VISIBILITY_DEPENDENCY_NEEDS_CHECK ) = = InstanceData : : FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE ) | | ( parent_flags & InstanceData : : FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN ) ;
}
2021-05-09 16:23:20 +00:00
void RendererSceneCull : : _scene_cull_threaded ( uint32_t p_thread , CullData * cull_data ) {
2021-01-04 20:00:44 +00:00
uint32_t cull_total = cull_data - > scenario - > instance_data . size ( ) ;
2022-07-23 17:12:41 +00:00
uint32_t total_threads = WorkerThreadPool : : get_singleton ( ) - > get_thread_count ( ) ;
2021-01-04 20:00:44 +00:00
uint32_t cull_from = p_thread * cull_total / total_threads ;
uint32_t cull_to = ( p_thread + 1 = = total_threads ) ? cull_total : ( ( p_thread + 1 ) * cull_total / total_threads ) ;
2021-05-09 16:23:20 +00:00
_scene_cull ( * cull_data , scene_cull_result_threads [ p_thread ] , cull_from , cull_to ) ;
2021-01-04 20:00:44 +00:00
}
2021-05-09 16:23:20 +00:00
void RendererSceneCull : : _scene_cull ( CullData & cull_data , InstanceCullResult & cull_result , uint64_t p_from , uint64_t p_to ) {
2021-01-04 20:00:44 +00:00
uint64_t frame_number = RSG : : rasterizer - > get_frame_number ( ) ;
2022-04-09 09:34:31 +00:00
float lightmap_probe_update_speed = RSG : : light_storage - > lightmap_get_probe_capture_update_speed ( ) * RSG : : rasterizer - > get_frame_delta_time ( ) ;
2021-01-04 20:00:44 +00:00
uint32_t sdfgi_last_light_index = 0xFFFFFFFF ;
uint32_t sdfgi_last_light_cascade = 0xFFFFFFFF ;
RID instance_pair_buffer [ MAX_INSTANCE_PAIRS ] ;
2020-10-17 05:08:21 +00:00
Transform3D inv_cam_transform = cull_data . cam_transform . inverse ( ) ;
2021-04-20 16:40:24 +00:00
float z_near = cull_data . camera_matrix - > get_z_near ( ) ;
2021-01-04 20:00:44 +00:00
for ( uint64_t i = p_from ; i < p_to ; i + + ) {
bool mesh_visible = false ;
2021-05-09 16:23:20 +00:00
InstanceData & idata = cull_data . scenario - > instance_data [ i ] ;
2021-07-20 18:17:34 +00:00
uint32_t visibility_flags = idata . flags & ( InstanceData : : FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE | InstanceData : : FLAG_VISIBILITY_DEPENDENCY_HIDDEN | InstanceData : : FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN ) ;
2021-05-09 16:23:20 +00:00
int32_t visibility_check = - 1 ;
2021-01-04 20:00:44 +00:00
2021-05-09 16:23:20 +00:00
# define HIDDEN_BY_VISIBILITY_CHECKS (visibility_flags == InstanceData::FLAG_VISIBILITY_DEPENDENCY_HIDDEN_CLOSE_RANGE || visibility_flags == InstanceData::FLAG_VISIBILITY_DEPENDENCY_HIDDEN)
# define LAYER_CHECK (cull_data.visible_layers & idata.layer_mask)
# define IN_FRUSTUM(f) (cull_data.scenario->instance_aabbs[i].in_frustum(f))
2021-07-20 18:17:34 +00:00
# define VIS_RANGE_CHECK ((idata.visibility_index == -1) || _visibility_range_check<false>(cull_data.scenario->instance_visibility[idata.visibility_index], cull_data.cam_transform.origin, cull_data.visibility_viewport_mask) == 0)
# define VIS_PARENT_CHECK (_visibility_parent_check(cull_data, idata))
2021-05-09 16:23:20 +00:00
# define VIS_CHECK (visibility_check < 0 ? (visibility_check = (visibility_flags != InstanceData::FLAG_VISIBILITY_DEPENDENCY_NEEDS_CHECK || (VIS_RANGE_CHECK && VIS_PARENT_CHECK))) : visibility_check)
2021-06-18 19:38:43 +00:00
# define OCCLUSION_CULLED (cull_data.occlusion_buffer != nullptr && (cull_data.scenario->instance_data[i].flags & InstanceData::FLAG_IGNORE_OCCLUSION_CULLING) == 0 && cull_data.occlusion_buffer->is_occluded(cull_data.scenario->instance_aabbs[i].bounds, cull_data.cam_transform.origin, inv_cam_transform, *cull_data.camera_matrix, z_near))
2021-01-04 20:00:44 +00:00
2021-05-09 16:23:20 +00:00
if ( ! HIDDEN_BY_VISIBILITY_CHECKS ) {
2021-10-03 11:28:55 +00:00
if ( ( LAYER_CHECK & & IN_FRUSTUM ( cull_data . cull - > frustum ) & & VIS_CHECK & & ! OCCLUSION_CULLED ) | | ( cull_data . scenario - > instance_data [ i ] . flags & InstanceData : : FLAG_IGNORE_ALL_CULLING ) ) {
2021-05-09 16:23:20 +00:00
uint32_t base_type = idata . flags & InstanceData : : FLAG_BASE_TYPE_MASK ;
if ( base_type = = RS : : INSTANCE_LIGHT ) {
cull_result . lights . push_back ( idata . instance ) ;
cull_result . light_instances . push_back ( RID : : from_uint64 ( idata . instance_data_rid ) ) ;
2022-04-09 09:34:31 +00:00
if ( cull_data . shadow_atlas . is_valid ( ) & & RSG : : light_storage - > light_has_shadow ( idata . base_rid ) ) {
2022-09-12 09:44:48 +00:00
RSG : : light_storage - > light_instance_mark_visible ( RID : : from_uint64 ( idata . instance_data_rid ) ) ; //mark it visible for shadow allocation later
2021-01-04 20:00:44 +00:00
}
2021-05-09 16:23:20 +00:00
} else if ( base_type = = RS : : INSTANCE_REFLECTION_PROBE ) {
if ( cull_data . render_reflection_probe ! = idata . instance ) {
//avoid entering The Matrix
2021-01-04 20:00:44 +00:00
2022-09-12 09:44:48 +00:00
if ( ( idata . flags & InstanceData : : FLAG_REFLECTION_PROBE_DIRTY ) | | RSG : : light_storage - > reflection_probe_instance_needs_redraw ( RID : : from_uint64 ( idata . instance_data_rid ) ) ) {
2021-05-09 16:23:20 +00:00
InstanceReflectionProbeData * reflection_probe = static_cast < InstanceReflectionProbeData * > ( idata . instance - > base_data ) ;
cull_data . cull - > lock . lock ( ) ;
if ( ! reflection_probe - > update_list . in_list ( ) ) {
reflection_probe - > render_step = 0 ;
reflection_probe_render_list . add_last ( & reflection_probe - > update_list ) ;
}
cull_data . cull - > lock . unlock ( ) ;
idata . flags & = ~ uint32_t ( InstanceData : : FLAG_REFLECTION_PROBE_DIRTY ) ;
}
2021-01-04 20:00:44 +00:00
2022-09-12 09:44:48 +00:00
if ( RSG : : light_storage - > reflection_probe_instance_has_reflection ( RID : : from_uint64 ( idata . instance_data_rid ) ) ) {
2021-05-09 16:23:20 +00:00
cull_result . reflections . push_back ( RID : : from_uint64 ( idata . instance_data_rid ) ) ;
}
}
} else if ( base_type = = RS : : INSTANCE_DECAL ) {
cull_result . decals . push_back ( RID : : from_uint64 ( idata . instance_data_rid ) ) ;
} else if ( base_type = = RS : : INSTANCE_VOXEL_GI ) {
InstanceVoxelGIData * voxel_gi = static_cast < InstanceVoxelGIData * > ( idata . instance - > base_data ) ;
cull_data . cull - > lock . lock ( ) ;
if ( ! voxel_gi - > update_element . in_list ( ) ) {
voxel_gi_update_list . add ( & voxel_gi - > update_element ) ;
}
cull_data . cull - > lock . unlock ( ) ;
cull_result . voxel_gi_instances . push_back ( RID : : from_uint64 ( idata . instance_data_rid ) ) ;
2021-01-04 20:00:44 +00:00
2021-05-09 16:23:20 +00:00
} else if ( base_type = = RS : : INSTANCE_LIGHTMAP ) {
cull_result . lightmaps . push_back ( RID : : from_uint64 ( idata . instance_data_rid ) ) ;
2021-10-03 11:28:55 +00:00
} else if ( base_type = = RS : : INSTANCE_FOG_VOLUME ) {
cull_result . fog_volumes . push_back ( RID : : from_uint64 ( idata . instance_data_rid ) ) ;
2021-06-16 18:43:02 +00:00
} else if ( base_type = = RS : : INSTANCE_VISIBLITY_NOTIFIER ) {
InstanceVisibilityNotifierData * vnd = idata . visibility_notifier ;
if ( ! vnd - > list_element . in_list ( ) ) {
visible_notifier_list_lock . lock ( ) ;
visible_notifier_list . add ( & vnd - > list_element ) ;
visible_notifier_list_lock . unlock ( ) ;
vnd - > just_visible = true ;
}
vnd - > visible_in_frame = RSG : : rasterizer - > get_frame_number ( ) ;
2021-05-09 16:23:20 +00:00
} else if ( ( ( 1 < < base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) & & ! ( idata . flags & InstanceData : : FLAG_CAST_SHADOWS_ONLY ) ) {
bool keep = true ;
2021-01-04 20:00:44 +00:00
2021-05-09 16:23:20 +00:00
if ( idata . flags & InstanceData : : FLAG_REDRAW_IF_VISIBLE ) {
2021-01-04 20:00:44 +00:00
RenderingServerDefault : : redraw_request ( ) ;
}
2021-05-09 16:23:20 +00:00
if ( base_type = = RS : : INSTANCE_MESH ) {
mesh_visible = true ;
} else if ( base_type = = RS : : INSTANCE_PARTICLES ) {
//particles visible? process them
2022-04-12 11:41:50 +00:00
if ( RSG : : particles_storage - > particles_is_inactive ( idata . base_rid ) ) {
2021-05-09 16:23:20 +00:00
//but if nothing is going on, don't do it.
keep = false ;
} else {
cull_data . cull - > lock . lock ( ) ;
2022-04-12 11:41:50 +00:00
RSG : : particles_storage - > particles_request_process ( idata . base_rid ) ;
2021-05-09 16:23:20 +00:00
cull_data . cull - > lock . unlock ( ) ;
2022-05-03 12:50:35 +00:00
RSG : : particles_storage - > particles_set_view_axis ( idata . base_rid , - cull_data . cam_transform . basis . get_column ( 2 ) . normalized ( ) , cull_data . cam_transform . basis . get_column ( 1 ) . normalized ( ) ) ;
2021-05-09 16:23:20 +00:00
//particles visible? request redraw
RenderingServerDefault : : redraw_request ( ) ;
2021-01-04 20:00:44 +00:00
}
}
2021-07-20 18:17:34 +00:00
if ( idata . parent_array_index ! = - 1 ) {
float fade = 1.0f ;
const uint32_t & parent_flags = cull_data . scenario - > instance_data [ idata . parent_array_index ] . flags ;
if ( parent_flags & InstanceData : : FLAG_VISIBILITY_DEPENDENCY_FADE_CHILDREN ) {
const int32_t & parent_idx = cull_data . scenario - > instance_data [ idata . parent_array_index ] . visibility_index ;
fade = cull_data . scenario - > instance_visibility [ parent_idx ] . children_fade_alpha ;
}
2022-07-23 14:01:53 +00:00
idata . instance_geometry - > set_parent_fade_alpha ( fade ) ;
2021-07-20 18:17:34 +00:00
}
2021-05-09 16:23:20 +00:00
if ( geometry_instance_pair_mask & ( 1 < < RS : : INSTANCE_LIGHT ) & & ( idata . flags & InstanceData : : FLAG_GEOM_LIGHTING_DIRTY ) ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( idata . instance - > base_data ) ;
uint32_t idx = 0 ;
2022-05-18 23:43:40 +00:00
for ( const Instance * E : geom - > lights ) {
InstanceLightData * light = static_cast < InstanceLightData * > ( E - > base_data ) ;
2021-05-09 16:23:20 +00:00
instance_pair_buffer [ idx + + ] = light - > instance ;
if ( idx = = MAX_INSTANCE_PAIRS ) {
break ;
}
}
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > pair_light_instances ( instance_pair_buffer , idx ) ;
2021-05-09 16:23:20 +00:00
idata . flags & = ~ uint32_t ( InstanceData : : FLAG_GEOM_LIGHTING_DIRTY ) ;
}
2021-01-04 20:00:44 +00:00
2021-07-12 23:32:05 +00:00
if ( idata . flags & InstanceData : : FLAG_GEOM_PROJECTOR_SOFTSHADOW_DIRTY ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( idata . instance - > base_data ) ;
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2023-01-19 23:13:31 +00:00
cull_data . cull - > lock . lock ( ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_softshadow_projector_pairing ( geom - > softshadow_count > 0 , geom - > projector_count > 0 ) ;
2023-01-19 23:13:31 +00:00
cull_data . cull - > lock . unlock ( ) ;
2021-07-12 23:32:05 +00:00
idata . flags & = ~ uint32_t ( InstanceData : : FLAG_GEOM_PROJECTOR_SOFTSHADOW_DIRTY ) ;
}
2021-05-09 16:23:20 +00:00
if ( geometry_instance_pair_mask & ( 1 < < RS : : INSTANCE_REFLECTION_PROBE ) & & ( idata . flags & InstanceData : : FLAG_GEOM_REFLECTION_DIRTY ) ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( idata . instance - > base_data ) ;
uint32_t idx = 0 ;
2021-01-04 20:00:44 +00:00
2022-05-18 23:43:40 +00:00
for ( const Instance * E : geom - > reflection_probes ) {
InstanceReflectionProbeData * reflection_probe = static_cast < InstanceReflectionProbeData * > ( E - > base_data ) ;
2021-01-04 20:00:44 +00:00
2021-05-09 16:23:20 +00:00
instance_pair_buffer [ idx + + ] = reflection_probe - > instance ;
if ( idx = = MAX_INSTANCE_PAIRS ) {
break ;
}
2021-01-04 20:00:44 +00:00
}
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > pair_reflection_probe_instances ( instance_pair_buffer , idx ) ;
2021-05-09 16:23:20 +00:00
idata . flags & = ~ uint32_t ( InstanceData : : FLAG_GEOM_REFLECTION_DIRTY ) ;
}
2021-01-04 20:00:44 +00:00
2021-05-09 16:23:20 +00:00
if ( geometry_instance_pair_mask & ( 1 < < RS : : INSTANCE_DECAL ) & & ( idata . flags & InstanceData : : FLAG_GEOM_DECAL_DIRTY ) ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( idata . instance - > base_data ) ;
uint32_t idx = 0 ;
2021-03-10 11:23:55 +00:00
2022-05-18 23:43:40 +00:00
for ( const Instance * E : geom - > decals ) {
InstanceDecalData * decal = static_cast < InstanceDecalData * > ( E - > base_data ) ;
2021-01-04 20:00:44 +00:00
2021-05-09 16:23:20 +00:00
instance_pair_buffer [ idx + + ] = decal - > instance ;
if ( idx = = MAX_INSTANCE_PAIRS ) {
break ;
}
2021-03-10 11:23:55 +00:00
}
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > pair_decal_instances ( instance_pair_buffer , idx ) ;
2022-08-30 05:32:13 +00:00
2021-05-09 16:23:20 +00:00
idata . flags & = ~ uint32_t ( InstanceData : : FLAG_GEOM_DECAL_DIRTY ) ;
2021-01-04 20:00:44 +00:00
}
2021-05-09 16:23:20 +00:00
if ( idata . flags & InstanceData : : FLAG_GEOM_VOXEL_GI_DIRTY ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( idata . instance - > base_data ) ;
uint32_t idx = 0 ;
2022-05-18 23:43:40 +00:00
for ( const Instance * E : geom - > voxel_gi_instances ) {
InstanceVoxelGIData * voxel_gi = static_cast < InstanceVoxelGIData * > ( E - > base_data ) ;
2021-01-04 20:00:44 +00:00
2021-05-09 16:23:20 +00:00
instance_pair_buffer [ idx + + ] = voxel_gi - > probe_instance ;
if ( idx = = MAX_INSTANCE_PAIRS ) {
break ;
}
2021-01-04 20:00:44 +00:00
}
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > pair_voxel_gi_instances ( instance_pair_buffer , idx ) ;
2022-08-30 05:32:13 +00:00
2021-05-09 16:23:20 +00:00
idata . flags & = ~ uint32_t ( InstanceData : : FLAG_GEOM_VOXEL_GI_DIRTY ) ;
}
2021-01-04 20:00:44 +00:00
2021-05-09 16:23:20 +00:00
if ( ( idata . flags & InstanceData : : FLAG_LIGHTMAP_CAPTURE ) & & idata . instance - > last_frame_pass ! = frame_number & & ! idata . instance - > lightmap_target_sh . is_empty ( ) & & ! idata . instance - > lightmap_sh . is_empty ( ) ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( idata . instance - > base_data ) ;
Color * sh = idata . instance - > lightmap_sh . ptrw ( ) ;
const Color * target_sh = idata . instance - > lightmap_target_sh . ptr ( ) ;
for ( uint32_t j = 0 ; j < 9 ; j + + ) {
sh [ j ] = sh [ j ] . lerp ( target_sh [ j ] , MIN ( 1.0 , lightmap_probe_update_speed ) ) ;
}
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2023-01-19 23:13:31 +00:00
cull_data . cull - > lock . lock ( ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_lightmap_capture ( sh ) ;
2023-01-19 23:13:31 +00:00
cull_data . cull - > lock . unlock ( ) ;
2021-05-09 16:23:20 +00:00
idata . instance - > last_frame_pass = frame_number ;
2021-01-04 20:00:44 +00:00
}
2021-05-09 16:23:20 +00:00
if ( keep ) {
cull_result . geometry_instances . push_back ( idata . instance_geometry ) ;
}
2021-01-04 20:00:44 +00:00
}
}
2021-05-09 16:23:20 +00:00
for ( uint32_t j = 0 ; j < cull_data . cull - > shadow_count ; j + + ) {
for ( uint32_t k = 0 ; k < cull_data . cull - > shadows [ j ] . cascade_count ; k + + ) {
if ( IN_FRUSTUM ( cull_data . cull - > shadows [ j ] . cascades [ k ] . frustum ) & & VIS_CHECK ) {
uint32_t base_type = idata . flags & InstanceData : : FLAG_BASE_TYPE_MASK ;
2021-01-04 20:00:44 +00:00
2022-12-27 17:43:53 +00:00
if ( ( ( 1 < < base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) & & idata . flags & InstanceData : : FLAG_CAST_SHADOWS & & LAYER_CHECK ) {
2021-05-09 16:23:20 +00:00
cull_result . directional_shadows [ j ] . cascade_geometry_instances [ k ] . push_back ( idata . instance_geometry ) ;
mesh_visible = true ;
}
2021-01-04 20:00:44 +00:00
}
}
}
}
2021-05-09 16:23:20 +00:00
# undef HIDDEN_BY_VISIBILITY_CHECKS
# undef LAYER_CHECK
# undef IN_FRUSTUM
# undef VIS_RANGE_CHECK
# undef VIS_PARENT_CHECK
# undef VIS_CHECK
# undef OCCLUSION_CULLED
2021-01-04 20:00:44 +00:00
for ( uint32_t j = 0 ; j < cull_data . cull - > sdfgi . region_count ; j + + ) {
if ( cull_data . scenario - > instance_aabbs [ i ] . in_aabb ( cull_data . cull - > sdfgi . region_aabb [ j ] ) ) {
uint32_t base_type = idata . flags & InstanceData : : FLAG_BASE_TYPE_MASK ;
if ( base_type = = RS : : INSTANCE_LIGHT ) {
InstanceLightData * instance_light = ( InstanceLightData * ) idata . instance - > base_data ;
if ( instance_light - > bake_mode = = RS : : LIGHT_BAKE_STATIC & & cull_data . cull - > sdfgi . region_cascade [ j ] < = instance_light - > max_sdfgi_cascade ) {
if ( sdfgi_last_light_index ! = i | | sdfgi_last_light_cascade ! = cull_data . cull - > sdfgi . region_cascade [ j ] ) {
sdfgi_last_light_index = i ;
sdfgi_last_light_cascade = cull_data . cull - > sdfgi . region_cascade [ j ] ;
cull_result . sdfgi_cascade_lights [ sdfgi_last_light_cascade ] . push_back ( instance_light - > instance ) ;
}
}
} else if ( ( 1 < < base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) {
if ( idata . flags & InstanceData : : FLAG_USES_BAKED_LIGHT ) {
cull_result . sdfgi_region_geometry_instances [ j ] . push_back ( idata . instance_geometry ) ;
mesh_visible = true ;
}
}
}
}
if ( mesh_visible & & cull_data . scenario - > instance_data [ i ] . flags & InstanceData : : FLAG_USES_MESH_INSTANCE ) {
cull_result . mesh_instances . push_back ( cull_data . scenario - > instance_data [ i ] . instance - > mesh_instance ) ;
}
}
}
2022-09-08 00:44:36 +00:00
void RendererSceneCull : : _render_scene ( const RendererSceneRender : : CameraData * p_camera_data , const Ref < RenderSceneBuffers > & p_render_buffers , RID p_environment , RID p_force_camera_attributes , uint32_t p_visible_layers , RID p_scenario , RID p_viewport , RID p_shadow_atlas , RID p_reflection_probe , int p_reflection_probe_pass , float p_screen_mesh_lod_threshold , bool p_using_shadows , RenderingMethod : : RenderInfo * r_render_info ) {
2021-09-29 17:08:41 +00:00
Instance * render_reflection_probe = instance_owner . get_or_null ( p_reflection_probe ) ; //if null, not rendering to it
2020-12-26 12:05:36 +00:00
2021-09-29 17:08:41 +00:00
Scenario * scenario = scenario_owner . get_or_null ( p_scenario ) ;
2020-12-03 21:09:47 +00:00
2023-01-30 04:09:02 +00:00
ERR_FAIL_COND ( p_render_buffers . is_null ( ) ) ;
2020-12-03 21:09:47 +00:00
render_pass + + ;
scene_render - > set_scene_pass ( render_pass ) ;
2023-01-30 04:09:02 +00:00
if ( p_reflection_probe . is_null ( ) ) {
2021-02-02 19:51:36 +00:00
//no rendering code here, this is only to set up what needs to be done, request regions, etc.
2021-05-07 13:19:04 +00:00
scene_render - > sdfgi_update ( p_render_buffers , p_environment , p_camera_data - > main_transform . origin ) ; //update conditions for SDFGI (whether its used or not)
2020-12-03 21:09:47 +00:00
}
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " Update Visibility Dependencies " ) ;
2021-05-09 16:23:20 +00:00
if ( scenario - > instance_visibility . get_bin_count ( ) > 0 ) {
if ( ! scenario - > viewport_visibility_masks . has ( p_viewport ) ) {
scenario_add_viewport_visibility_mask ( scenario - > self , p_viewport ) ;
}
2021-06-19 09:43:41 +00:00
VisibilityCullData visibility_cull_data ;
2021-05-09 16:23:20 +00:00
visibility_cull_data . scenario = scenario ;
visibility_cull_data . viewport_mask = scenario - > viewport_visibility_masks [ p_viewport ] ;
visibility_cull_data . camera_position = p_camera_data - > main_transform . origin ;
for ( int i = scenario - > instance_visibility . get_bin_count ( ) - 1 ; i > 0 ; i - - ) { // We skip bin 0
visibility_cull_data . cull_offset = scenario - > instance_visibility . get_bin_start ( i ) ;
visibility_cull_data . cull_count = scenario - > instance_visibility . get_bin_size ( i ) ;
if ( visibility_cull_data . cull_count = = 0 ) {
continue ;
}
if ( visibility_cull_data . cull_count > thread_cull_threshold ) {
2022-07-23 17:12:41 +00:00
WorkerThreadPool : : GroupID group_task = WorkerThreadPool : : get_singleton ( ) - > add_template_group_task ( this , & RendererSceneCull : : _visibility_cull_threaded , & visibility_cull_data , WorkerThreadPool : : get_singleton ( ) - > get_thread_count ( ) , - 1 , true , SNAME ( " VisibilityCullInstances " ) ) ;
WorkerThreadPool : : get_singleton ( ) - > wait_for_group_task_completion ( group_task ) ;
2021-05-09 16:23:20 +00:00
} else {
_visibility_cull ( visibility_cull_data , visibility_cull_data . cull_offset , visibility_cull_data . cull_offset + visibility_cull_data . cull_count ) ;
}
}
}
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " Cull 3D Scene " ) ;
2020-12-03 21:09:47 +00:00
2022-04-26 19:49:44 +00:00
//rasterizer->set_camera(p_camera_data->main_transform, p_camera_data.main_projection, p_camera_data.is_orthogonal);
2020-12-03 21:09:47 +00:00
/* STEP 2 - CULL */
2020-12-26 12:05:36 +00:00
2021-08-04 15:18:06 +00:00
Vector < Plane > planes = p_camera_data - > main_projection . get_projection_planes ( p_camera_data - > main_transform ) ;
2020-12-26 12:05:36 +00:00
cull . frustum = Frustum ( planes ) ;
Vector < RID > directional_lights ;
// directional lights
2020-12-23 16:52:58 +00:00
{
2020-12-26 12:05:36 +00:00
cull . shadow_count = 0 ;
Vector < Instance * > lights_with_shadow ;
2020-12-23 16:52:58 +00:00
2021-07-16 03:45:57 +00:00
for ( Instance * E : scenario - > directional_lights ) {
if ( ! E - > visible ) {
2020-12-26 12:05:36 +00:00
continue ;
}
if ( directional_lights . size ( ) > RendererSceneRender : : MAX_DIRECTIONAL_LIGHTS ) {
break ;
}
2020-12-03 21:09:47 +00:00
2021-07-16 03:45:57 +00:00
InstanceLightData * light = static_cast < InstanceLightData * > ( E - > base_data ) ;
2020-12-26 12:05:36 +00:00
//check shadow..
if ( light ) {
2022-04-09 09:34:31 +00:00
if ( p_using_shadows & & p_shadow_atlas . is_valid ( ) & & RSG : : light_storage - > light_has_shadow ( E - > base ) & & ! ( RSG : : light_storage - > light_get_type ( E - > base ) = = RS : : LIGHT_DIRECTIONAL & & RSG : : light_storage - > light_directional_get_sky_mode ( E - > base ) = = RS : : LIGHT_DIRECTIONAL_SKY_MODE_SKY_ONLY ) ) {
2021-07-16 03:45:57 +00:00
lights_with_shadow . push_back ( E ) ;
2020-12-26 12:05:36 +00:00
}
//add to list
directional_lights . push_back ( light - > instance ) ;
}
}
2022-09-12 09:44:48 +00:00
RSG : : light_storage - > set_directional_shadow_count ( lights_with_shadow . size ( ) ) ;
2020-12-26 12:05:36 +00:00
for ( int i = 0 ; i < lights_with_shadow . size ( ) ; i + + ) {
2022-04-26 19:49:44 +00:00
_light_instance_setup_directional_shadow ( i , lights_with_shadow [ i ] , p_camera_data - > main_transform , p_camera_data - > main_projection , p_camera_data - > is_orthogonal , p_camera_data - > vaspect ) ;
2020-12-26 12:05:36 +00:00
}
2020-12-23 16:52:58 +00:00
}
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
{ //sdfgi
cull . sdfgi . region_count = 0 ;
2020-12-03 21:09:47 +00:00
2023-01-30 04:09:02 +00:00
if ( p_reflection_probe . is_null ( ) ) {
2020-12-26 12:05:36 +00:00
cull . sdfgi . cascade_light_count = 0 ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
uint32_t prev_cascade = 0xFFFFFFFF ;
uint32_t pending_region_count = scene_render - > sdfgi_get_pending_region_count ( p_render_buffers ) ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
for ( uint32_t i = 0 ; i < pending_region_count ; i + + ) {
cull . sdfgi . region_aabb [ i ] = scene_render - > sdfgi_get_pending_region_bounds ( p_render_buffers , i ) ;
uint32_t region_cascade = scene_render - > sdfgi_get_pending_region_cascade ( p_render_buffers , i ) ;
cull . sdfgi . region_cascade [ i ] = region_cascade ;
if ( region_cascade ! = prev_cascade ) {
cull . sdfgi . cascade_light_index [ cull . sdfgi . cascade_light_count ] = region_cascade ;
cull . sdfgi . cascade_light_count + + ;
prev_cascade = region_cascade ;
}
2020-12-03 21:09:47 +00:00
}
2020-12-26 12:05:36 +00:00
cull . sdfgi . region_count = pending_region_count ;
}
}
2020-12-03 21:09:47 +00:00
2021-05-09 16:23:20 +00:00
scene_cull_result . clear ( ) ;
2020-12-03 21:09:47 +00:00
2020-12-26 12:05:36 +00:00
{
2021-01-04 20:00:44 +00:00
uint64_t cull_from = 0 ;
uint64_t cull_to = scenario - > instance_data . size ( ) ;
2021-04-20 16:40:24 +00:00
CullData cull_data ;
2021-01-04 20:00:44 +00:00
//prepare for eventual thread usage
cull_data . cull = & cull ;
cull_data . scenario = scenario ;
cull_data . shadow_atlas = p_shadow_atlas ;
2021-05-07 13:19:04 +00:00
cull_data . cam_transform = p_camera_data - > main_transform ;
2021-01-04 20:00:44 +00:00
cull_data . visible_layers = p_visible_layers ;
cull_data . render_reflection_probe = render_reflection_probe ;
2021-04-20 16:40:24 +00:00
cull_data . occlusion_buffer = RendererSceneOcclusionCull : : get_singleton ( ) - > buffer_get_ptr ( p_viewport ) ;
2021-05-07 13:19:04 +00:00
cull_data . camera_matrix = & p_camera_data - > main_projection ;
2021-06-19 09:43:41 +00:00
cull_data . visibility_viewport_mask = scenario - > viewport_visibility_masks . has ( p_viewport ) ? scenario - > viewport_visibility_masks [ p_viewport ] : 0 ;
2021-01-04 20:00:44 +00:00
//#define DEBUG_CULL_TIME
# ifdef DEBUG_CULL_TIME
uint64_t time_from = OS : : get_singleton ( ) - > get_ticks_usec ( ) ;
# endif
if ( cull_to > thread_cull_threshold ) {
//multiple threads
2022-12-29 00:24:45 +00:00
for ( InstanceCullResult & thread : scene_cull_result_threads ) {
thread . clear ( ) ;
2020-12-03 21:09:47 +00:00
}
2022-07-23 17:12:41 +00:00
WorkerThreadPool : : GroupID group_task = WorkerThreadPool : : get_singleton ( ) - > add_template_group_task ( this , & RendererSceneCull : : _scene_cull_threaded , & cull_data , scene_cull_result_threads . size ( ) , - 1 , true , SNAME ( " RenderCullInstances " ) ) ;
WorkerThreadPool : : get_singleton ( ) - > wait_for_group_task_completion ( group_task ) ;
2020-12-26 12:05:36 +00:00
2022-12-29 00:24:45 +00:00
for ( InstanceCullResult & thread : scene_cull_result_threads ) {
scene_cull_result . append_from ( thread ) ;
2020-12-16 14:07:08 +00:00
}
2021-01-04 20:00:44 +00:00
} else {
//single threaded
2021-05-09 16:23:20 +00:00
_scene_cull ( cull_data , scene_cull_result , cull_from , cull_to ) ;
2020-12-03 21:09:47 +00:00
}
2020-12-23 16:52:58 +00:00
2021-01-04 20:00:44 +00:00
# ifdef DEBUG_CULL_TIME
static float time_avg = 0 ;
static uint32_t time_count = 0 ;
time_avg + = double ( OS : : get_singleton ( ) - > get_ticks_usec ( ) - time_from ) / 1000.0 ;
time_count + + ;
print_line ( " time taken: " + rtos ( time_avg / time_count ) ) ;
# endif
2021-05-09 16:23:20 +00:00
if ( scene_cull_result . mesh_instances . size ( ) ) {
for ( uint64_t i = 0 ; i < scene_cull_result . mesh_instances . size ( ) ; i + + ) {
2022-04-02 05:29:04 +00:00
RSG : : mesh_storage - > mesh_instance_check_for_update ( scene_cull_result . mesh_instances [ i ] ) ;
2020-12-26 12:05:36 +00:00
}
2022-04-02 05:29:04 +00:00
RSG : : mesh_storage - > update_mesh_instances ( ) ;
2020-12-26 12:05:36 +00:00
}
2020-12-03 21:09:47 +00:00
}
2020-12-26 12:05:36 +00:00
//render shadows
2020-12-16 14:07:08 +00:00
2021-02-02 19:51:36 +00:00
max_shadows_used = 0 ;
2020-12-03 21:09:47 +00:00
2021-02-02 19:51:36 +00:00
if ( p_using_shadows ) { //setup shadow maps
2020-12-03 21:09:47 +00:00
2021-02-02 19:51:36 +00:00
// Directional Shadows
2020-12-23 16:52:58 +00:00
2021-02-02 19:51:36 +00:00
for ( uint32_t i = 0 ; i < cull . shadow_count ; i + + ) {
for ( uint32_t j = 0 ; j < cull . shadows [ i ] . cascade_count ; j + + ) {
const Cull : : Shadow : : Cascade & c = cull . shadows [ i ] . cascades [ j ] ;
// print_line("shadow " + itos(i) + " cascade " + itos(j) + " elements: " + itos(c.cull_result.size()));
2022-09-12 09:44:48 +00:00
RSG : : light_storage - > light_instance_set_shadow_transform ( cull . shadows [ i ] . light_instance , c . projection , c . transform , c . zfar , c . split , j , c . shadow_texel_size , c . bias_scale , c . range_begin , c . uv_scale ) ;
2021-02-02 19:51:36 +00:00
if ( max_shadows_used = = MAX_UPDATE_SHADOWS ) {
continue ;
}
render_shadow_data [ max_shadows_used ] . light = cull . shadows [ i ] . light_instance ;
render_shadow_data [ max_shadows_used ] . pass = j ;
2021-05-09 16:23:20 +00:00
render_shadow_data [ max_shadows_used ] . instances . merge_unordered ( scene_cull_result . directional_shadows [ i ] . cascade_geometry_instances [ j ] ) ;
2021-02-02 19:51:36 +00:00
max_shadows_used + + ;
2020-12-26 12:05:36 +00:00
}
2020-12-03 21:09:47 +00:00
}
2021-02-02 19:51:36 +00:00
// Positional Shadowss
2021-05-09 16:23:20 +00:00
for ( uint32_t i = 0 ; i < ( uint32_t ) scene_cull_result . lights . size ( ) ; i + + ) {
Instance * ins = scene_cull_result . lights [ i ] ;
2020-12-03 21:09:47 +00:00
2022-04-09 09:34:31 +00:00
if ( ! p_shadow_atlas . is_valid ( ) | | ! RSG : : light_storage - > light_has_shadow ( ins - > base ) ) {
2020-12-03 21:09:47 +00:00
continue ;
}
InstanceLightData * light = static_cast < InstanceLightData * > ( ins - > base_data ) ;
float coverage = 0.f ;
{ //compute coverage
2021-05-07 13:19:04 +00:00
Transform3D cam_xf = p_camera_data - > main_transform ;
float zn = p_camera_data - > main_projection . get_z_near ( ) ;
2022-05-03 12:50:35 +00:00
Plane p ( - cam_xf . basis . get_column ( 2 ) , cam_xf . origin + cam_xf . basis . get_column ( 2 ) * - zn ) ; //camera near plane
2020-12-03 21:09:47 +00:00
// near plane half width and height
2021-05-07 13:19:04 +00:00
Vector2 vp_half_extents = p_camera_data - > main_projection . get_viewport_half_extents ( ) ;
2020-12-03 21:09:47 +00:00
2022-04-09 09:34:31 +00:00
switch ( RSG : : light_storage - > light_get_type ( ins - > base ) ) {
2020-12-03 21:09:47 +00:00
case RS : : LIGHT_OMNI : {
2022-04-09 09:34:31 +00:00
float radius = RSG : : light_storage - > light_get_param ( ins - > base , RS : : LIGHT_PARAM_RANGE ) ;
2020-12-03 21:09:47 +00:00
//get two points parallel to near plane
Vector3 points [ 2 ] = {
ins - > transform . origin ,
2022-05-03 12:50:35 +00:00
ins - > transform . origin + cam_xf . basis . get_column ( 0 ) * radius
2020-12-03 21:09:47 +00:00
} ;
2022-04-26 19:49:44 +00:00
if ( ! p_camera_data - > is_orthogonal ) {
2020-12-03 21:09:47 +00:00
//if using perspetive, map them to near plane
for ( int j = 0 ; j < 2 ; j + + ) {
if ( p . distance_to ( points [ j ] ) < 0 ) {
points [ j ] . z = - zn ; //small hack to keep size constant when hitting the screen
}
p . intersects_segment ( cam_xf . origin , points [ j ] , & points [ j ] ) ; //map to plane
}
}
float screen_diameter = points [ 0 ] . distance_to ( points [ 1 ] ) * 2 ;
coverage = screen_diameter / ( vp_half_extents . x + vp_half_extents . y ) ;
} break ;
case RS : : LIGHT_SPOT : {
2022-04-09 09:34:31 +00:00
float radius = RSG : : light_storage - > light_get_param ( ins - > base , RS : : LIGHT_PARAM_RANGE ) ;
float angle = RSG : : light_storage - > light_get_param ( ins - > base , RS : : LIGHT_PARAM_SPOT_ANGLE ) ;
2020-12-03 21:09:47 +00:00
2022-08-13 15:45:42 +00:00
float w = radius * Math : : sin ( Math : : deg_to_rad ( angle ) ) ;
float d = radius * Math : : cos ( Math : : deg_to_rad ( angle ) ) ;
2020-12-03 21:09:47 +00:00
2022-05-03 12:50:35 +00:00
Vector3 base = ins - > transform . origin - ins - > transform . basis . get_column ( 2 ) . normalized ( ) * d ;
2020-12-03 21:09:47 +00:00
Vector3 points [ 2 ] = {
base ,
2022-05-03 12:50:35 +00:00
base + cam_xf . basis . get_column ( 0 ) * w
2020-12-03 21:09:47 +00:00
} ;
2022-04-26 19:49:44 +00:00
if ( ! p_camera_data - > is_orthogonal ) {
2020-12-03 21:09:47 +00:00
//if using perspetive, map them to near plane
for ( int j = 0 ; j < 2 ; j + + ) {
if ( p . distance_to ( points [ j ] ) < 0 ) {
points [ j ] . z = - zn ; //small hack to keep size constant when hitting the screen
}
p . intersects_segment ( cam_xf . origin , points [ j ] , & points [ j ] ) ; //map to plane
}
}
float screen_diameter = points [ 0 ] . distance_to ( points [ 1 ] ) * 2 ;
coverage = screen_diameter / ( vp_half_extents . x + vp_half_extents . y ) ;
} break ;
default : {
ERR_PRINT ( " Invalid Light Type " ) ;
}
}
}
if ( light - > shadow_dirty ) {
light - > last_version + + ;
light - > shadow_dirty = false ;
}
2022-09-12 09:44:48 +00:00
bool redraw = RSG : : light_storage - > shadow_atlas_update_light ( p_shadow_atlas , light - > instance , coverage , light - > last_version ) ;
2020-12-03 21:09:47 +00:00
2021-02-02 19:51:36 +00:00
if ( redraw & & max_shadows_used < MAX_UPDATE_SHADOWS ) {
2020-12-03 21:09:47 +00:00
//must redraw!
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " > Render Light3D " + itos ( i ) ) ;
2022-12-27 17:43:53 +00:00
light - > shadow_dirty = _light_instance_update_shadow ( ins , p_camera_data - > main_transform , p_camera_data - > main_projection , p_camera_data - > is_orthogonal , p_camera_data - > vaspect , p_shadow_atlas , scenario , p_screen_mesh_lod_threshold , p_visible_layers ) ;
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " < Render Light3D " + itos ( i ) ) ;
2021-02-02 19:51:36 +00:00
} else {
light - > shadow_dirty = redraw ;
}
}
}
//render SDFGI
{
2022-12-15 11:20:06 +00:00
// Q: Should this whole block be skipped if we're rendering our reflection probe?
2021-02-02 19:51:36 +00:00
sdfgi_update_data . update_static = false ;
if ( cull . sdfgi . region_count > 0 ) {
//update regions
for ( uint32_t i = 0 ; i < cull . sdfgi . region_count ; i + + ) {
2021-05-09 16:23:20 +00:00
render_sdfgi_data [ i ] . instances . merge_unordered ( scene_cull_result . sdfgi_region_geometry_instances [ i ] ) ;
2021-02-02 19:51:36 +00:00
render_sdfgi_data [ i ] . region = i ;
2020-12-03 21:09:47 +00:00
}
2021-02-02 19:51:36 +00:00
//check if static lights were culled
bool static_lights_culled = false ;
for ( uint32_t i = 0 ; i < cull . sdfgi . cascade_light_count ; i + + ) {
2021-05-09 16:23:20 +00:00
if ( scene_cull_result . sdfgi_cascade_lights [ i ] . size ( ) ) {
2021-02-02 19:51:36 +00:00
static_lights_culled = true ;
break ;
}
}
if ( static_lights_culled ) {
sdfgi_update_data . static_cascade_count = cull . sdfgi . cascade_light_count ;
sdfgi_update_data . static_cascade_indices = cull . sdfgi . cascade_light_index ;
2021-05-09 16:23:20 +00:00
sdfgi_update_data . static_positional_lights = scene_cull_result . sdfgi_cascade_lights ;
2021-02-02 19:51:36 +00:00
sdfgi_update_data . update_static = true ;
}
}
2023-01-30 04:09:02 +00:00
if ( p_reflection_probe . is_null ( ) ) {
2021-02-02 19:51:36 +00:00
sdfgi_update_data . directional_lights = & directional_lights ;
sdfgi_update_data . positional_light_instances = scenario - > dynamic_lights . ptr ( ) ;
sdfgi_update_data . positional_light_count = scenario - > dynamic_lights . size ( ) ;
2020-12-03 21:09:47 +00:00
}
}
2020-12-26 12:05:36 +00:00
//append the directional lights to the lights culled
for ( int i = 0 ; i < directional_lights . size ( ) ; i + + ) {
2021-05-09 16:23:20 +00:00
scene_cull_result . light_instances . push_back ( directional_lights [ i ] ) ;
2020-12-03 21:09:47 +00:00
}
2021-02-02 19:51:36 +00:00
2022-07-31 23:20:24 +00:00
RID camera_attributes ;
if ( p_force_camera_attributes . is_valid ( ) ) {
camera_attributes = p_force_camera_attributes ;
2021-02-02 19:51:36 +00:00
} else {
2022-07-31 23:20:24 +00:00
camera_attributes = scenario - > camera_attributes ;
2021-02-02 19:51:36 +00:00
}
/* PROCESS GEOMETRY AND DRAW SCENE */
2021-04-20 16:40:24 +00:00
RID occluders_tex ;
2022-04-04 14:10:22 +00:00
const RendererSceneRender : : CameraData * prev_camera_data = p_camera_data ;
2021-04-20 16:40:24 +00:00
if ( p_viewport . is_valid ( ) ) {
occluders_tex = RSG : : viewport - > viewport_get_occluder_debug_texture ( p_viewport ) ;
2022-04-04 14:10:22 +00:00
prev_camera_data = RSG : : viewport - > viewport_get_prev_camera_data ( p_viewport ) ;
2021-04-20 16:40:24 +00:00
}
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " Render 3D Scene " ) ;
2022-07-31 23:20:24 +00:00
scene_render - > render_scene ( p_render_buffers , p_camera_data , prev_camera_data , scene_cull_result . geometry_instances , scene_cull_result . light_instances , scene_cull_result . reflections , scene_cull_result . voxel_gi_instances , scene_cull_result . decals , scene_cull_result . lightmaps , scene_cull_result . fog_volumes , p_environment , camera_attributes , p_shadow_atlas , occluders_tex , p_reflection_probe . is_valid ( ) ? RID ( ) : scenario - > reflection_atlas , p_reflection_probe , p_reflection_probe_pass , p_screen_mesh_lod_threshold , render_shadow_data , max_shadows_used , render_sdfgi_data , cull . sdfgi . region_count , & sdfgi_update_data , r_render_info ) ;
2022-04-04 14:10:22 +00:00
if ( p_viewport . is_valid ( ) ) {
RSG : : viewport - > viewport_set_prev_camera_data ( p_viewport , p_camera_data ) ;
}
2021-02-02 19:51:36 +00:00
for ( uint32_t i = 0 ; i < max_shadows_used ; i + + ) {
render_shadow_data [ i ] . instances . clear ( ) ;
}
max_shadows_used = 0 ;
for ( uint32_t i = 0 ; i < cull . sdfgi . region_count ; i + + ) {
render_sdfgi_data [ i ] . instances . clear ( ) ;
}
2020-12-03 21:09:47 +00:00
}
2020-12-04 18:26:24 +00:00
RID RendererSceneCull : : _render_get_environment ( RID p_camera , RID p_scenario ) {
2021-09-29 17:08:41 +00:00
Camera * camera = camera_owner . get_or_null ( p_camera ) ;
2020-12-03 21:09:47 +00:00
if ( camera & & scene_render - > is_environment ( camera - > env ) ) {
return camera - > env ;
}
2021-09-29 17:08:41 +00:00
Scenario * scenario = scenario_owner . get_or_null ( p_scenario ) ;
2020-12-03 21:09:47 +00:00
if ( ! scenario ) {
return RID ( ) ;
}
if ( scene_render - > is_environment ( scenario - > environment ) ) {
return scenario - > environment ;
}
if ( scene_render - > is_environment ( scenario - > fallback_environment ) ) {
return scenario - > fallback_environment ;
}
return RID ( ) ;
}
2022-08-04 08:40:39 +00:00
void RendererSceneCull : : render_empty_scene ( const Ref < RenderSceneBuffers > & p_render_buffers , RID p_scenario , RID p_shadow_atlas ) {
2020-12-03 21:09:47 +00:00
# ifndef _3D_DISABLED
2021-09-29 17:08:41 +00:00
Scenario * scenario = scenario_owner . get_or_null ( p_scenario ) ;
2020-12-03 21:09:47 +00:00
RID environment ;
if ( scenario - > environment . is_valid ( ) ) {
environment = scenario - > environment ;
} else {
environment = scenario - > fallback_environment ;
}
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " Render Empty 3D Scene " ) ;
2021-05-07 13:19:04 +00:00
RendererSceneRender : : CameraData camera_data ;
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
camera_data . set_camera ( Transform3D ( ) , Projection ( ) , true , false ) ;
2021-05-07 13:19:04 +00:00
2022-11-17 20:36:47 +00:00
scene_render - > render_scene ( p_render_buffers , & camera_data , & camera_data , PagedArray < RenderGeometryInstance * > ( ) , PagedArray < RID > ( ) , PagedArray < RID > ( ) , PagedArray < RID > ( ) , PagedArray < RID > ( ) , PagedArray < RID > ( ) , PagedArray < RID > ( ) , environment , RID ( ) , p_shadow_atlas , RID ( ) , scenario - > reflection_atlas , RID ( ) , 0 , 0 , nullptr , 0 , nullptr , 0 , nullptr ) ;
2020-12-03 21:09:47 +00:00
# endif
}
2020-12-04 18:26:24 +00:00
bool RendererSceneCull : : _render_reflection_probe_step ( Instance * p_instance , int p_step ) {
2020-12-03 21:09:47 +00:00
InstanceReflectionProbeData * reflection_probe = static_cast < InstanceReflectionProbeData * > ( p_instance - > base_data ) ;
Scenario * scenario = p_instance - > scenario ;
ERR_FAIL_COND_V ( ! scenario , true ) ;
RenderingServerDefault : : redraw_request ( ) ; //update, so it updates in editor
if ( p_step = = 0 ) {
2022-09-12 09:44:48 +00:00
if ( ! RSG : : light_storage - > reflection_probe_instance_begin_render ( reflection_probe - > instance , scenario - > reflection_atlas ) ) {
2020-12-03 21:09:47 +00:00
return true ; //all full
}
}
if ( p_step > = 0 & & p_step < 6 ) {
static const Vector3 view_normals [ 6 ] = {
Vector3 ( + 1 , 0 , 0 ) ,
Vector3 ( - 1 , 0 , 0 ) ,
Vector3 ( 0 , + 1 , 0 ) ,
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , 0 , + 1 ) ,
Vector3 ( 0 , 0 , - 1 )
} ;
static const Vector3 view_up [ 6 ] = {
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , 0 , + 1 ) ,
Vector3 ( 0 , 0 , - 1 ) ,
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , - 1 , 0 )
} ;
2022-07-12 07:43:01 +00:00
Vector3 probe_size = RSG : : light_storage - > reflection_probe_get_size ( p_instance - > base ) ;
2022-04-09 09:34:31 +00:00
Vector3 origin_offset = RSG : : light_storage - > reflection_probe_get_origin_offset ( p_instance - > base ) ;
float max_distance = RSG : : light_storage - > reflection_probe_get_origin_max_distance ( p_instance - > base ) ;
2022-07-12 07:43:01 +00:00
float atlas_size = RSG : : light_storage - > reflection_atlas_get_size ( scenario - > reflection_atlas ) ;
float mesh_lod_threshold = RSG : : light_storage - > reflection_probe_get_mesh_lod_threshold ( p_instance - > base ) / atlas_size ;
2020-12-03 21:09:47 +00:00
2022-07-12 07:43:01 +00:00
Vector3 edge = view_normals [ p_step ] * probe_size / 2 ;
2020-12-03 21:09:47 +00:00
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
max_distance = MAX ( max_distance , distance ) ;
//render cubemap side
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
Projection cm ;
2020-12-03 21:09:47 +00:00
cm . set_perspective ( 90 , 1 , 0.01 , max_distance ) ;
2020-10-17 05:08:21 +00:00
Transform3D local_view ;
2020-12-03 21:09:47 +00:00
local_view . set_look_at ( origin_offset , origin_offset + view_normals [ p_step ] , view_up [ p_step ] ) ;
2020-10-17 05:08:21 +00:00
Transform3D xform = p_instance - > transform * local_view ;
2020-12-03 21:09:47 +00:00
RID shadow_atlas ;
2022-04-09 09:34:31 +00:00
bool use_shadows = RSG : : light_storage - > reflection_probe_renders_shadows ( p_instance - > base ) ;
2020-12-03 21:09:47 +00:00
if ( use_shadows ) {
shadow_atlas = scenario - > reflection_probe_shadow_atlas ;
}
2021-04-20 13:59:46 +00:00
RID environment ;
if ( scenario - > environment . is_valid ( ) ) {
environment = scenario - > environment ;
} else {
environment = scenario - > fallback_environment ;
}
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " Render ReflectionProbe, Step " + itos ( p_step ) ) ;
2021-05-07 13:19:04 +00:00
RendererSceneRender : : CameraData camera_data ;
camera_data . set_camera ( xform , cm , false , false ) ;
2022-12-15 11:20:06 +00:00
Ref < RenderSceneBuffers > render_buffers = RSG : : light_storage - > reflection_probe_atlas_get_render_buffers ( scenario - > reflection_atlas ) ;
2022-08-04 08:40:39 +00:00
_render_scene ( & camera_data , render_buffers , environment , RID ( ) , RSG : : light_storage - > reflection_probe_get_cull_mask ( p_instance - > base ) , p_instance - > scenario - > self , RID ( ) , shadow_atlas , reflection_probe - > instance , p_step , mesh_lod_threshold , use_shadows ) ;
2020-12-03 21:09:47 +00:00
} else {
//do roughness postprocess step until it believes it's done
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " Post-Process ReflectionProbe, Step " + itos ( p_step ) ) ;
2022-09-12 09:44:48 +00:00
return RSG : : light_storage - > reflection_probe_instance_postprocess_step ( reflection_probe - > instance ) ;
2020-12-03 21:09:47 +00:00
}
return false ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : render_probes ( ) {
2020-12-03 21:09:47 +00:00
/* REFLECTION PROBES */
SelfList < InstanceReflectionProbeData > * ref_probe = reflection_probe_render_list . first ( ) ;
bool busy = false ;
while ( ref_probe ) {
SelfList < InstanceReflectionProbeData > * next = ref_probe - > next ( ) ;
RID base = ref_probe - > self ( ) - > owner - > base ;
2022-04-09 09:34:31 +00:00
switch ( RSG : : light_storage - > reflection_probe_get_update_mode ( base ) ) {
2020-12-03 21:09:47 +00:00
case RS : : REFLECTION_PROBE_UPDATE_ONCE : {
if ( busy ) { //already rendering something
break ;
}
bool done = _render_reflection_probe_step ( ref_probe - > self ( ) - > owner , ref_probe - > self ( ) - > render_step ) ;
if ( done ) {
reflection_probe_render_list . remove ( ref_probe ) ;
} else {
ref_probe - > self ( ) - > render_step + + ;
}
busy = true ; //do not render another one of this kind
} break ;
case RS : : REFLECTION_PROBE_UPDATE_ALWAYS : {
int step = 0 ;
bool done = false ;
while ( ! done ) {
done = _render_reflection_probe_step ( ref_probe - > self ( ) - > owner , step ) ;
step + + ;
}
reflection_probe_render_list . remove ( ref_probe ) ;
} break ;
}
ref_probe = next ;
}
2021-06-04 22:47:26 +00:00
/* VOXEL GIS */
2020-12-03 21:09:47 +00:00
2021-06-04 22:47:26 +00:00
SelfList < InstanceVoxelGIData > * voxel_gi = voxel_gi_update_list . first ( ) ;
2020-12-03 21:09:47 +00:00
2021-06-04 22:47:26 +00:00
if ( voxel_gi ) {
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " Render VoxelGI " ) ;
2020-12-03 21:09:47 +00:00
}
2021-06-04 22:47:26 +00:00
while ( voxel_gi ) {
SelfList < InstanceVoxelGIData > * next = voxel_gi - > next ( ) ;
2020-12-03 21:09:47 +00:00
2021-06-04 22:47:26 +00:00
InstanceVoxelGIData * probe = voxel_gi - > self ( ) ;
2020-12-03 21:09:47 +00:00
//Instance *instance_probe = probe->owner;
//check if probe must be setup, but don't do if on the lighting thread
bool cache_dirty = false ;
int cache_count = 0 ;
{
int light_cache_size = probe - > light_cache . size ( ) ;
2021-06-04 22:47:26 +00:00
const InstanceVoxelGIData : : LightCache * caches = probe - > light_cache . ptr ( ) ;
2020-12-03 21:09:47 +00:00
const RID * instance_caches = probe - > light_instances . ptr ( ) ;
int idx = 0 ; //must count visible lights
2022-05-18 23:43:40 +00:00
for ( Instance * E : probe - > lights ) {
Instance * instance = E ;
2020-12-03 21:09:47 +00:00
InstanceLightData * instance_light = ( InstanceLightData * ) instance - > base_data ;
if ( ! instance - > visible ) {
continue ;
}
if ( cache_dirty ) {
//do nothing, since idx must count all visible lights anyway
} else if ( idx > = light_cache_size ) {
cache_dirty = true ;
} else {
2021-06-04 22:47:26 +00:00
const InstanceVoxelGIData : : LightCache * cache = & caches [ idx ] ;
2020-12-03 21:09:47 +00:00
if (
instance_caches [ idx ] ! = instance_light - > instance | |
2022-04-09 09:34:31 +00:00
cache - > has_shadow ! = RSG : : light_storage - > light_has_shadow ( instance - > base ) | |
cache - > type ! = RSG : : light_storage - > light_get_type ( instance - > base ) | |
2020-12-03 21:09:47 +00:00
cache - > transform ! = instance - > transform | |
2022-04-09 09:34:31 +00:00
cache - > color ! = RSG : : light_storage - > light_get_color ( instance - > base ) | |
cache - > energy ! = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_ENERGY ) | |
2022-07-31 23:20:24 +00:00
cache - > intensity ! = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_INTENSITY ) | |
2022-04-09 09:34:31 +00:00
cache - > bake_energy ! = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_INDIRECT_ENERGY ) | |
cache - > radius ! = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_RANGE ) | |
cache - > attenuation ! = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_ATTENUATION ) | |
cache - > spot_angle ! = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_SPOT_ANGLE ) | |
cache - > spot_attenuation ! = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_SPOT_ATTENUATION ) ) {
2020-12-03 21:09:47 +00:00
cache_dirty = true ;
}
}
idx + + ;
}
2021-07-24 13:46:25 +00:00
for ( const Instance * instance : probe - > owner - > scenario - > directional_lights ) {
2020-12-03 21:09:47 +00:00
InstanceLightData * instance_light = ( InstanceLightData * ) instance - > base_data ;
if ( ! instance - > visible ) {
continue ;
}
if ( cache_dirty ) {
//do nothing, since idx must count all visible lights anyway
} else if ( idx > = light_cache_size ) {
cache_dirty = true ;
} else {
2021-06-04 22:47:26 +00:00
const InstanceVoxelGIData : : LightCache * cache = & caches [ idx ] ;
2020-12-03 21:09:47 +00:00
if (
instance_caches [ idx ] ! = instance_light - > instance | |
2022-04-09 09:34:31 +00:00
cache - > has_shadow ! = RSG : : light_storage - > light_has_shadow ( instance - > base ) | |
cache - > type ! = RSG : : light_storage - > light_get_type ( instance - > base ) | |
2020-12-03 21:09:47 +00:00
cache - > transform ! = instance - > transform | |
2022-04-09 09:34:31 +00:00
cache - > color ! = RSG : : light_storage - > light_get_color ( instance - > base ) | |
cache - > energy ! = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_ENERGY ) | |
2022-07-31 23:20:24 +00:00
cache - > intensity ! = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_INTENSITY ) | |
2022-04-09 09:34:31 +00:00
cache - > bake_energy ! = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_INDIRECT_ENERGY ) | |
cache - > radius ! = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_RANGE ) | |
cache - > attenuation ! = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_ATTENUATION ) | |
cache - > spot_angle ! = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_SPOT_ANGLE ) | |
cache - > spot_attenuation ! = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_SPOT_ATTENUATION ) | |
cache - > sky_mode ! = RSG : : light_storage - > light_directional_get_sky_mode ( instance - > base ) ) {
2020-12-03 21:09:47 +00:00
cache_dirty = true ;
}
}
idx + + ;
}
if ( idx ! = light_cache_size ) {
cache_dirty = true ;
}
cache_count = idx ;
}
2021-06-04 22:47:26 +00:00
bool update_lights = scene_render - > voxel_gi_needs_update ( probe - > probe_instance ) ;
2020-12-03 21:09:47 +00:00
if ( cache_dirty ) {
probe - > light_cache . resize ( cache_count ) ;
probe - > light_instances . resize ( cache_count ) ;
if ( cache_count ) {
2021-06-04 22:47:26 +00:00
InstanceVoxelGIData : : LightCache * caches = probe - > light_cache . ptrw ( ) ;
2020-12-03 21:09:47 +00:00
RID * instance_caches = probe - > light_instances . ptrw ( ) ;
int idx = 0 ; //must count visible lights
2022-05-18 23:43:40 +00:00
for ( Instance * E : probe - > lights ) {
Instance * instance = E ;
2020-12-03 21:09:47 +00:00
InstanceLightData * instance_light = ( InstanceLightData * ) instance - > base_data ;
if ( ! instance - > visible ) {
continue ;
}
2021-06-04 22:47:26 +00:00
InstanceVoxelGIData : : LightCache * cache = & caches [ idx ] ;
2020-12-03 21:09:47 +00:00
instance_caches [ idx ] = instance_light - > instance ;
2022-04-09 09:34:31 +00:00
cache - > has_shadow = RSG : : light_storage - > light_has_shadow ( instance - > base ) ;
cache - > type = RSG : : light_storage - > light_get_type ( instance - > base ) ;
2020-12-03 21:09:47 +00:00
cache - > transform = instance - > transform ;
2022-04-09 09:34:31 +00:00
cache - > color = RSG : : light_storage - > light_get_color ( instance - > base ) ;
cache - > energy = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_ENERGY ) ;
2022-07-31 23:20:24 +00:00
cache - > intensity = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_INTENSITY ) ;
2022-04-09 09:34:31 +00:00
cache - > bake_energy = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_INDIRECT_ENERGY ) ;
cache - > radius = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_RANGE ) ;
cache - > attenuation = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_ATTENUATION ) ;
cache - > spot_angle = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_SPOT_ANGLE ) ;
cache - > spot_attenuation = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_SPOT_ATTENUATION ) ;
2020-12-03 21:09:47 +00:00
idx + + ;
}
2021-07-24 13:46:25 +00:00
for ( const Instance * instance : probe - > owner - > scenario - > directional_lights ) {
2020-12-03 21:09:47 +00:00
InstanceLightData * instance_light = ( InstanceLightData * ) instance - > base_data ;
if ( ! instance - > visible ) {
continue ;
}
2021-06-04 22:47:26 +00:00
InstanceVoxelGIData : : LightCache * cache = & caches [ idx ] ;
2020-12-03 21:09:47 +00:00
instance_caches [ idx ] = instance_light - > instance ;
2022-04-09 09:34:31 +00:00
cache - > has_shadow = RSG : : light_storage - > light_has_shadow ( instance - > base ) ;
cache - > type = RSG : : light_storage - > light_get_type ( instance - > base ) ;
2020-12-03 21:09:47 +00:00
cache - > transform = instance - > transform ;
2022-04-09 09:34:31 +00:00
cache - > color = RSG : : light_storage - > light_get_color ( instance - > base ) ;
cache - > energy = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_ENERGY ) ;
2022-07-31 23:20:24 +00:00
cache - > intensity = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_INTENSITY ) ;
2022-04-09 09:34:31 +00:00
cache - > bake_energy = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_INDIRECT_ENERGY ) ;
cache - > radius = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_RANGE ) ;
cache - > attenuation = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_ATTENUATION ) ;
cache - > spot_angle = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_SPOT_ANGLE ) ;
cache - > spot_attenuation = RSG : : light_storage - > light_get_param ( instance - > base , RS : : LIGHT_PARAM_SPOT_ATTENUATION ) ;
cache - > sky_mode = RSG : : light_storage - > light_directional_get_sky_mode ( instance - > base ) ;
2020-12-03 21:09:47 +00:00
idx + + ;
}
}
update_lights = true ;
}
2021-05-09 16:23:20 +00:00
scene_cull_result . geometry_instances . clear ( ) ;
2020-12-03 21:09:47 +00:00
2020-12-31 12:42:56 +00:00
RID instance_pair_buffer [ MAX_INSTANCE_PAIRS ] ;
2022-05-18 23:43:40 +00:00
for ( Instance * E : probe - > dynamic_geometries ) {
Instance * ins = E ;
2020-12-23 16:52:58 +00:00
if ( ! ins - > visible ) {
continue ;
}
InstanceGeometryData * geom = ( InstanceGeometryData * ) ins - > base_data ;
2020-12-03 21:09:47 +00:00
2021-06-04 22:47:26 +00:00
if ( ins - > scenario & & ins - > array_index > = 0 & & ( ins - > scenario - > instance_data [ ins - > array_index ] . flags & InstanceData : : FLAG_GEOM_VOXEL_GI_DIRTY ) ) {
2020-12-31 12:42:56 +00:00
uint32_t idx = 0 ;
2022-05-18 23:43:40 +00:00
for ( const Instance * F : geom - > voxel_gi_instances ) {
InstanceVoxelGIData * voxel_gi2 = static_cast < InstanceVoxelGIData * > ( F - > base_data ) ;
2020-12-03 21:09:47 +00:00
2021-06-04 22:47:26 +00:00
instance_pair_buffer [ idx + + ] = voxel_gi2 - > probe_instance ;
2020-12-31 12:42:56 +00:00
if ( idx = = MAX_INSTANCE_PAIRS ) {
break ;
}
2020-12-03 21:09:47 +00:00
}
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > pair_voxel_gi_instances ( instance_pair_buffer , idx ) ;
2020-12-31 12:42:56 +00:00
2021-06-04 22:47:26 +00:00
ins - > scenario - > instance_data [ ins - > array_index ] . flags & = ~ uint32_t ( InstanceData : : FLAG_GEOM_VOXEL_GI_DIRTY ) ;
2020-12-03 21:09:47 +00:00
}
2020-12-23 16:52:58 +00:00
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2021-05-09 16:23:20 +00:00
scene_cull_result . geometry_instances . push_back ( geom - > geometry_instance ) ;
2020-12-03 21:09:47 +00:00
}
2021-05-09 16:23:20 +00:00
scene_render - > voxel_gi_update ( probe - > probe_instance , update_lights , probe - > light_instances , scene_cull_result . geometry_instances ) ;
2020-12-03 21:09:47 +00:00
2021-06-04 22:47:26 +00:00
voxel_gi_update_list . remove ( voxel_gi ) ;
2020-12-03 21:09:47 +00:00
2021-06-04 22:47:26 +00:00
voxel_gi = next ;
2020-12-03 21:09:47 +00:00
}
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : render_particle_colliders ( ) {
2022-05-19 15:00:06 +00:00
while ( heightfield_particle_colliders_update_list . begin ( ) ) {
Instance * hfpc = * heightfield_particle_colliders_update_list . begin ( ) ;
2020-12-03 21:09:47 +00:00
2022-04-12 11:41:50 +00:00
if ( hfpc - > scenario & & hfpc - > base_type = = RS : : INSTANCE_PARTICLES_COLLISION & & RSG : : particles_storage - > particles_collision_is_heightfield ( hfpc - > base ) ) {
2020-12-03 21:09:47 +00:00
//update heightfield
2020-12-23 16:52:58 +00:00
instance_cull_result . clear ( ) ;
2021-05-09 16:23:20 +00:00
scene_cull_result . geometry_instances . clear ( ) ;
2020-12-23 16:52:58 +00:00
struct CullAABB {
PagedArray < Instance * > * result ;
_FORCE_INLINE_ bool operator ( ) ( void * p_data ) {
Instance * p_instance = ( Instance * ) p_data ;
result - > push_back ( p_instance ) ;
return false ;
}
} ;
CullAABB cull_aabb ;
cull_aabb . result = & instance_cull_result ;
hfpc - > scenario - > indexers [ Scenario : : INDEXER_GEOMETRY ] . aabb_query ( hfpc - > transformed_aabb , cull_aabb ) ;
hfpc - > scenario - > indexers [ Scenario : : INDEXER_VOLUMES ] . aabb_query ( hfpc - > transformed_aabb , cull_aabb ) ;
for ( int i = 0 ; i < ( int ) instance_cull_result . size ( ) ; i + + ) {
2020-12-03 21:09:47 +00:00
Instance * instance = instance_cull_result [ i ] ;
2020-12-23 16:52:58 +00:00
if ( ! instance | | ! ( ( 1 < < instance - > base_type ) & ( RS : : INSTANCE_GEOMETRY_MASK & ( ~ ( 1 < < RS : : INSTANCE_PARTICLES ) ) ) ) ) { //all but particles to avoid self collision
continue ;
2020-12-03 21:09:47 +00:00
}
2020-12-31 12:42:56 +00:00
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( instance - > base_data ) ;
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2021-05-09 16:23:20 +00:00
scene_cull_result . geometry_instances . push_back ( geom - > geometry_instance ) ;
2020-12-03 21:09:47 +00:00
}
2021-05-09 16:23:20 +00:00
scene_render - > render_particle_collider_heightfield ( hfpc - > base , hfpc - > transform , scene_cull_result . geometry_instances ) ;
2020-12-03 21:09:47 +00:00
}
2022-05-19 15:00:06 +00:00
heightfield_particle_colliders_update_list . remove ( heightfield_particle_colliders_update_list . begin ( ) ) ;
2020-12-03 21:09:47 +00:00
}
}
2022-04-03 17:09:09 +00:00
void RendererSceneCull : : _update_instance_shader_uniforms_from_material ( HashMap < StringName , Instance : : InstanceShaderParameter > & isparams , const HashMap < StringName , Instance : : InstanceShaderParameter > & existing_isparams , RID p_material ) {
2022-03-21 11:25:25 +00:00
List < RendererMaterialStorage : : InstanceShaderParam > plist ;
2022-08-27 09:22:43 +00:00
RSG : : material_storage - > material_get_instance_shader_parameters ( p_material , & plist ) ;
2022-03-21 11:25:25 +00:00
for ( const RendererMaterialStorage : : InstanceShaderParam & E : plist ) {
2021-07-16 03:45:57 +00:00
StringName name = E . info . name ;
2020-12-03 21:09:47 +00:00
if ( isparams . has ( name ) ) {
2021-07-16 03:45:57 +00:00
if ( isparams [ name ] . info . type ! = E . info . type ) {
WARN_PRINT ( " More than one material in instance export the same instance shader uniform ' " + E . info . name + " ', but they do it with different data types. Only the first one (in order) will display correctly. " ) ;
2020-12-03 21:09:47 +00:00
}
2021-07-16 03:45:57 +00:00
if ( isparams [ name ] . index ! = E . index ) {
WARN_PRINT ( " More than one material in instance export the same instance shader uniform ' " + E . info . name + " ', but they do it with different indices. Only the first one (in order) will display correctly. " ) ;
2020-12-03 21:09:47 +00:00
}
continue ; //first one found always has priority
}
2020-12-31 12:42:56 +00:00
Instance : : InstanceShaderParameter isp ;
2021-07-16 03:45:57 +00:00
isp . index = E . index ;
isp . info = E . info ;
isp . default_value = E . default_value ;
2020-12-03 21:09:47 +00:00
if ( existing_isparams . has ( name ) ) {
isp . value = existing_isparams [ name ] . value ;
} else {
2021-07-16 03:45:57 +00:00
isp . value = E . default_value ;
2020-12-03 21:09:47 +00:00
}
isparams [ name ] = isp ;
}
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : _update_dirty_instance ( Instance * p_instance ) {
2020-12-03 21:09:47 +00:00
if ( p_instance - > update_aabb ) {
_update_instance_aabb ( p_instance ) ;
}
if ( p_instance - > update_dependencies ) {
2021-01-04 12:33:25 +00:00
p_instance - > dependency_tracker . update_begin ( ) ;
2020-12-03 21:09:47 +00:00
if ( p_instance - > base . is_valid ( ) ) {
2022-06-21 00:08:33 +00:00
RSG : : utilities - > base_update_dependency ( p_instance - > base , & p_instance - > dependency_tracker ) ;
2020-12-03 21:09:47 +00:00
}
if ( p_instance - > material_override . is_valid ( ) ) {
2022-03-21 11:25:25 +00:00
RSG : : material_storage - > material_update_dependency ( p_instance - > material_override , & p_instance - > dependency_tracker ) ;
2020-12-03 21:09:47 +00:00
}
2021-09-25 18:40:26 +00:00
if ( p_instance - > material_overlay . is_valid ( ) ) {
2022-03-21 11:25:25 +00:00
RSG : : material_storage - > material_update_dependency ( p_instance - > material_overlay , & p_instance - > dependency_tracker ) ;
2021-09-25 18:40:26 +00:00
}
2020-12-03 21:09:47 +00:00
if ( p_instance - > base_type = = RS : : INSTANCE_MESH ) {
//remove materials no longer used and un-own them
2022-04-02 05:29:04 +00:00
int new_mat_count = RSG : : mesh_storage - > mesh_get_surface_count ( p_instance - > base ) ;
2020-12-03 21:09:47 +00:00
p_instance - > materials . resize ( new_mat_count ) ;
2020-12-16 14:07:08 +00:00
_instance_update_mesh_instance ( p_instance ) ;
2020-12-03 21:09:47 +00:00
}
2022-02-14 12:27:10 +00:00
if ( p_instance - > base_type = = RS : : INSTANCE_PARTICLES ) {
// update the process material dependency
2022-04-12 11:41:50 +00:00
RID particle_material = RSG : : particles_storage - > particles_get_process_material ( p_instance - > base ) ;
2022-02-14 12:27:10 +00:00
if ( particle_material . is_valid ( ) ) {
2022-03-21 11:25:25 +00:00
RSG : : material_storage - > material_update_dependency ( particle_material , & p_instance - > dependency_tracker ) ;
2022-02-14 12:27:10 +00:00
}
}
2020-12-03 21:09:47 +00:00
if ( ( 1 < < p_instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( p_instance - > base_data ) ;
bool can_cast_shadows = true ;
bool is_animated = false ;
2022-05-13 13:04:37 +00:00
HashMap < StringName , Instance : : InstanceShaderParameter > isparams ;
2020-12-03 21:09:47 +00:00
if ( p_instance - > cast_shadows = = RS : : SHADOW_CASTING_SETTING_OFF ) {
can_cast_shadows = false ;
}
if ( p_instance - > material_override . is_valid ( ) ) {
2022-03-21 11:25:25 +00:00
if ( ! RSG : : material_storage - > material_casts_shadows ( p_instance - > material_override ) ) {
2020-12-03 21:09:47 +00:00
can_cast_shadows = false ;
}
2022-03-21 11:25:25 +00:00
is_animated = RSG : : material_storage - > material_is_animated ( p_instance - > material_override ) ;
2022-04-03 17:09:09 +00:00
_update_instance_shader_uniforms_from_material ( isparams , p_instance - > instance_shader_uniforms , p_instance - > material_override ) ;
2020-12-03 21:09:47 +00:00
} else {
if ( p_instance - > base_type = = RS : : INSTANCE_MESH ) {
RID mesh = p_instance - > base ;
if ( mesh . is_valid ( ) ) {
bool cast_shadows = false ;
for ( int i = 0 ; i < p_instance - > materials . size ( ) ; i + + ) {
2022-04-02 05:29:04 +00:00
RID mat = p_instance - > materials [ i ] . is_valid ( ) ? p_instance - > materials [ i ] : RSG : : mesh_storage - > mesh_surface_get_material ( mesh , i ) ;
2020-12-03 21:09:47 +00:00
if ( ! mat . is_valid ( ) ) {
cast_shadows = true ;
} else {
2022-03-21 11:25:25 +00:00
if ( RSG : : material_storage - > material_casts_shadows ( mat ) ) {
2020-12-03 21:09:47 +00:00
cast_shadows = true ;
}
2022-03-21 11:25:25 +00:00
if ( RSG : : material_storage - > material_is_animated ( mat ) ) {
2020-12-03 21:09:47 +00:00
is_animated = true ;
}
2022-04-03 17:09:09 +00:00
_update_instance_shader_uniforms_from_material ( isparams , p_instance - > instance_shader_uniforms , mat ) ;
2020-12-03 21:09:47 +00:00
2022-03-21 11:25:25 +00:00
RSG : : material_storage - > material_update_dependency ( mat , & p_instance - > dependency_tracker ) ;
2020-12-03 21:09:47 +00:00
}
}
if ( ! cast_shadows ) {
can_cast_shadows = false ;
}
}
} else if ( p_instance - > base_type = = RS : : INSTANCE_MULTIMESH ) {
2022-04-02 05:29:04 +00:00
RID mesh = RSG : : mesh_storage - > multimesh_get_mesh ( p_instance - > base ) ;
2020-12-03 21:09:47 +00:00
if ( mesh . is_valid ( ) ) {
bool cast_shadows = false ;
2022-04-02 05:29:04 +00:00
int sc = RSG : : mesh_storage - > mesh_get_surface_count ( mesh ) ;
2020-12-03 21:09:47 +00:00
for ( int i = 0 ; i < sc ; i + + ) {
2022-04-02 05:29:04 +00:00
RID mat = RSG : : mesh_storage - > mesh_surface_get_material ( mesh , i ) ;
2020-12-03 21:09:47 +00:00
if ( ! mat . is_valid ( ) ) {
cast_shadows = true ;
} else {
2022-03-21 11:25:25 +00:00
if ( RSG : : material_storage - > material_casts_shadows ( mat ) ) {
2020-12-03 21:09:47 +00:00
cast_shadows = true ;
}
2022-03-21 11:25:25 +00:00
if ( RSG : : material_storage - > material_is_animated ( mat ) ) {
2020-12-03 21:09:47 +00:00
is_animated = true ;
}
2022-04-03 17:09:09 +00:00
_update_instance_shader_uniforms_from_material ( isparams , p_instance - > instance_shader_uniforms , mat ) ;
2020-12-03 21:09:47 +00:00
2022-03-21 11:25:25 +00:00
RSG : : material_storage - > material_update_dependency ( mat , & p_instance - > dependency_tracker ) ;
2020-12-03 21:09:47 +00:00
}
}
if ( ! cast_shadows ) {
can_cast_shadows = false ;
}
2022-06-21 00:08:33 +00:00
RSG : : utilities - > base_update_dependency ( mesh , & p_instance - > dependency_tracker ) ;
2020-12-03 21:09:47 +00:00
}
} else if ( p_instance - > base_type = = RS : : INSTANCE_PARTICLES ) {
bool cast_shadows = false ;
2022-04-12 11:41:50 +00:00
int dp = RSG : : particles_storage - > particles_get_draw_passes ( p_instance - > base ) ;
2020-12-03 21:09:47 +00:00
for ( int i = 0 ; i < dp ; i + + ) {
2022-04-12 11:41:50 +00:00
RID mesh = RSG : : particles_storage - > particles_get_draw_pass_mesh ( p_instance - > base , i ) ;
2020-12-03 21:09:47 +00:00
if ( ! mesh . is_valid ( ) ) {
continue ;
}
2022-04-02 05:29:04 +00:00
int sc = RSG : : mesh_storage - > mesh_get_surface_count ( mesh ) ;
2020-12-03 21:09:47 +00:00
for ( int j = 0 ; j < sc ; j + + ) {
2022-04-02 05:29:04 +00:00
RID mat = RSG : : mesh_storage - > mesh_surface_get_material ( mesh , j ) ;
2020-12-03 21:09:47 +00:00
if ( ! mat . is_valid ( ) ) {
cast_shadows = true ;
} else {
2022-03-21 11:25:25 +00:00
if ( RSG : : material_storage - > material_casts_shadows ( mat ) ) {
2020-12-03 21:09:47 +00:00
cast_shadows = true ;
}
2022-03-21 11:25:25 +00:00
if ( RSG : : material_storage - > material_is_animated ( mat ) ) {
2020-12-03 21:09:47 +00:00
is_animated = true ;
}
2022-04-03 17:09:09 +00:00
_update_instance_shader_uniforms_from_material ( isparams , p_instance - > instance_shader_uniforms , mat ) ;
2020-12-03 21:09:47 +00:00
2022-03-21 11:25:25 +00:00
RSG : : material_storage - > material_update_dependency ( mat , & p_instance - > dependency_tracker ) ;
2020-12-03 21:09:47 +00:00
}
}
}
if ( ! cast_shadows ) {
can_cast_shadows = false ;
}
}
}
2021-09-25 18:40:26 +00:00
if ( p_instance - > material_overlay . is_valid ( ) ) {
2022-10-07 17:15:39 +00:00
can_cast_shadows = can_cast_shadows & & RSG : : material_storage - > material_casts_shadows ( p_instance - > material_overlay ) ;
2022-03-21 11:25:25 +00:00
is_animated = is_animated | | RSG : : material_storage - > material_is_animated ( p_instance - > material_overlay ) ;
2022-04-03 17:09:09 +00:00
_update_instance_shader_uniforms_from_material ( isparams , p_instance - > instance_shader_uniforms , p_instance - > material_overlay ) ;
2021-09-25 18:40:26 +00:00
}
2020-12-03 21:09:47 +00:00
if ( can_cast_shadows ! = geom - > can_cast_shadows ) {
//ability to cast shadows change, let lights now
2022-05-18 23:43:40 +00:00
for ( const Instance * E : geom - > lights ) {
InstanceLightData * light = static_cast < InstanceLightData * > ( E - > base_data ) ;
2020-12-03 21:09:47 +00:00
light - > shadow_dirty = true ;
}
geom - > can_cast_shadows = can_cast_shadows ;
}
geom - > material_is_animated = is_animated ;
2022-04-03 17:09:09 +00:00
p_instance - > instance_shader_uniforms = isparams ;
2020-12-03 21:09:47 +00:00
2022-04-03 17:09:09 +00:00
if ( p_instance - > instance_allocated_shader_uniforms ! = ( p_instance - > instance_shader_uniforms . size ( ) > 0 ) ) {
p_instance - > instance_allocated_shader_uniforms = ( p_instance - > instance_shader_uniforms . size ( ) > 0 ) ;
if ( p_instance - > instance_allocated_shader_uniforms ) {
2022-08-27 09:22:43 +00:00
p_instance - > instance_allocated_shader_uniforms_offset = RSG : : material_storage - > global_shader_parameters_instance_allocate ( p_instance - > self ) ;
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-04-03 17:09:09 +00:00
geom - > geometry_instance - > set_instance_shader_uniforms_offset ( p_instance - > instance_allocated_shader_uniforms_offset ) ;
2020-12-31 12:42:56 +00:00
2022-04-03 17:09:09 +00:00
for ( const KeyValue < StringName , Instance : : InstanceShaderParameter > & E : p_instance - > instance_shader_uniforms ) {
2021-08-09 20:13:42 +00:00
if ( E . value . value . get_type ( ) ! = Variant : : NIL ) {
2022-10-09 05:14:07 +00:00
int flags_count = 0 ;
if ( E . value . info . hint = = PROPERTY_HINT_FLAGS ) {
// A small hack to detect boolean flags count and prevent overhead.
switch ( E . value . info . hint_string . length ( ) ) {
case 3 : // "x,y"
flags_count = 1 ;
break ;
case 5 : // "x,y,z"
flags_count = 2 ;
break ;
case 7 : // "x,y,z,w"
flags_count = 3 ;
break ;
}
}
RSG : : material_storage - > global_shader_parameters_instance_update ( p_instance - > self , E . value . index , E . value . value , flags_count ) ;
2020-12-03 21:09:47 +00:00
}
}
} else {
2022-08-27 09:22:43 +00:00
RSG : : material_storage - > global_shader_parameters_instance_free ( p_instance - > self ) ;
2022-04-03 17:09:09 +00:00
p_instance - > instance_allocated_shader_uniforms_offset = - 1 ;
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-04-03 17:09:09 +00:00
geom - > geometry_instance - > set_instance_shader_uniforms_offset ( - 1 ) ;
2020-12-03 21:09:47 +00:00
}
}
}
if ( p_instance - > skeleton . is_valid ( ) ) {
2022-04-02 05:29:04 +00:00
RSG : : mesh_storage - > skeleton_update_dependency ( p_instance - > skeleton , & p_instance - > dependency_tracker ) ;
2020-12-03 21:09:47 +00:00
}
2021-01-04 12:33:25 +00:00
p_instance - > dependency_tracker . update_end ( ) ;
2020-12-31 12:42:56 +00:00
if ( ( 1 < < p_instance - > base_type ) & RS : : INSTANCE_GEOMETRY_MASK ) {
InstanceGeometryData * geom = static_cast < InstanceGeometryData * > ( p_instance - > base_data ) ;
2022-08-30 05:32:13 +00:00
ERR_FAIL_NULL ( geom - > geometry_instance ) ;
2022-07-23 14:01:53 +00:00
geom - > geometry_instance - > set_surface_materials ( p_instance - > materials ) ;
2020-12-31 12:42:56 +00:00
}
2020-12-03 21:09:47 +00:00
}
_instance_update_list . remove ( & p_instance - > update_item ) ;
_update_instance ( p_instance ) ;
p_instance - > update_aabb = false ;
p_instance - > update_dependencies = false ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : update_dirty_instances ( ) {
2020-12-03 21:09:47 +00:00
while ( _instance_update_list . first ( ) ) {
_update_dirty_instance ( _instance_update_list . first ( ) - > self ( ) ) ;
}
2023-07-18 09:19:15 +00:00
// Update dirty resources after dirty instances as instance updates may affect resources.
RSG : : utilities - > update_dirty_resources ( ) ;
2020-12-03 21:09:47 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererSceneCull : : update ( ) {
2020-12-23 16:52:58 +00:00
//optimize bvhs
2022-02-05 10:59:34 +00:00
uint32_t rid_count = scenario_owner . get_rid_count ( ) ;
RID * rids = ( RID * ) alloca ( sizeof ( RID ) * rid_count ) ;
scenario_owner . fill_owned_buffer ( rids ) ;
for ( uint32_t i = 0 ; i < rid_count ; i + + ) {
Scenario * s = scenario_owner . get_or_null ( rids [ i ] ) ;
2020-12-23 16:52:58 +00:00
s - > indexers [ Scenario : : INDEXER_GEOMETRY ] . optimize_incremental ( indexer_update_iterations ) ;
s - > indexers [ Scenario : : INDEXER_VOLUMES ] . optimize_incremental ( indexer_update_iterations ) ;
}
2020-12-03 21:09:47 +00:00
scene_render - > update ( ) ;
update_dirty_instances ( ) ;
render_particle_colliders ( ) ;
}
2020-12-04 18:26:24 +00:00
bool RendererSceneCull : : free ( RID p_rid ) {
2020-11-18 18:11:30 +00:00
if ( p_rid . is_null ( ) ) {
return true ;
}
2020-12-03 21:09:47 +00:00
if ( scene_render - > free ( p_rid ) ) {
return true ;
}
if ( camera_owner . owns ( p_rid ) ) {
camera_owner . free ( p_rid ) ;
} else if ( scenario_owner . owns ( p_rid ) ) {
2021-09-29 17:08:41 +00:00
Scenario * scenario = scenario_owner . get_or_null ( p_rid ) ;
2020-12-03 21:09:47 +00:00
while ( scenario - > instances . first ( ) ) {
instance_set_scenario ( scenario - > instances . first ( ) - > self ( ) - > self , RID ( ) ) ;
}
2020-12-26 12:05:36 +00:00
scenario - > instance_aabbs . reset ( ) ;
scenario - > instance_data . reset ( ) ;
2021-05-09 16:23:20 +00:00
scenario - > instance_visibility . reset ( ) ;
2020-12-26 12:05:36 +00:00
2022-09-12 09:44:48 +00:00
RSG : : light_storage - > shadow_atlas_free ( scenario - > reflection_probe_shadow_atlas ) ;
RSG : : light_storage - > reflection_atlas_free ( scenario - > reflection_atlas ) ;
2020-12-03 21:09:47 +00:00
scenario_owner . free ( p_rid ) ;
2021-04-20 16:40:24 +00:00
RendererSceneOcclusionCull : : get_singleton ( ) - > remove_scenario ( p_rid ) ;
2020-12-03 21:09:47 +00:00
2021-04-20 16:40:24 +00:00
} else if ( RendererSceneOcclusionCull : : get_singleton ( ) - > is_occluder ( p_rid ) ) {
RendererSceneOcclusionCull : : get_singleton ( ) - > free_occluder ( p_rid ) ;
2020-12-03 21:09:47 +00:00
} else if ( instance_owner . owns ( p_rid ) ) {
// delete the instance
update_dirty_instances ( ) ;
2021-09-29 17:08:41 +00:00
Instance * instance = instance_owner . get_or_null ( p_rid ) ;
2020-12-03 21:09:47 +00:00
instance_geometry_set_lightmap ( p_rid , RID ( ) , Rect2 ( ) , 0 ) ;
instance_set_scenario ( p_rid , RID ( ) ) ;
instance_set_base ( p_rid , RID ( ) ) ;
instance_geometry_set_material_override ( p_rid , RID ( ) ) ;
2021-09-25 18:40:26 +00:00
instance_geometry_set_material_overlay ( p_rid , RID ( ) ) ;
2020-12-03 21:09:47 +00:00
instance_attach_skeleton ( p_rid , RID ( ) ) ;
2022-04-03 17:09:09 +00:00
if ( instance - > instance_allocated_shader_uniforms ) {
2020-12-03 21:09:47 +00:00
//free the used shader parameters
2022-08-27 09:22:43 +00:00
RSG : : material_storage - > global_shader_parameters_instance_free ( instance - > self ) ;
2020-12-03 21:09:47 +00:00
}
update_dirty_instances ( ) ; //in case something changed this
instance_owner . free ( p_rid ) ;
} else {
return false ;
}
return true ;
}
2022-08-31 17:24:04 +00:00
TypedArray < Image > RendererSceneCull : : bake_render_uv2 ( RID p_base , const TypedArray < RID > & p_material_overrides , const Size2i & p_image_size ) {
2020-12-03 21:09:47 +00:00
return scene_render - > bake_render_uv2 ( p_base , p_material_overrides , p_image_size ) ;
}
2021-06-16 18:43:02 +00:00
void RendererSceneCull : : update_visibility_notifiers ( ) {
SelfList < InstanceVisibilityNotifierData > * E = visible_notifier_list . first ( ) ;
while ( E ) {
SelfList < InstanceVisibilityNotifierData > * N = E - > next ( ) ;
InstanceVisibilityNotifierData * visibility_notifier = E - > self ( ) ;
if ( visibility_notifier - > just_visible ) {
visibility_notifier - > just_visible = false ;
2022-06-21 00:08:33 +00:00
RSG : : utilities - > visibility_notifier_call ( visibility_notifier - > base , true , RSG : : threaded ) ;
2021-06-16 18:43:02 +00:00
} else {
if ( visibility_notifier - > visible_in_frame ! = RSG : : rasterizer - > get_frame_number ( ) ) {
visible_notifier_list . remove ( E ) ;
2022-06-21 00:08:33 +00:00
RSG : : utilities - > visibility_notifier_call ( visibility_notifier - > base , false , RSG : : threaded ) ;
2021-06-16 18:43:02 +00:00
}
}
E = N ;
}
}
2020-12-03 21:09:47 +00:00
/*******************************/
/* Passthrough to Scene Render */
/*******************************/
/* ENVIRONMENT API */
2020-12-04 18:26:24 +00:00
RendererSceneCull * RendererSceneCull : : singleton = nullptr ;
2020-12-03 21:09:47 +00:00
2020-12-31 12:42:56 +00:00
void RendererSceneCull : : set_scene_render ( RendererSceneRender * p_scene_render ) {
scene_render = p_scene_render ;
geometry_instance_pair_mask = scene_render - > geometry_instance_get_pair_mask ( ) ;
}
2022-04-04 14:10:22 +00:00
float get_halton_value ( int index , int base ) {
float f = 1 ;
float r = 0 ;
while ( index > 0 ) {
f = f / static_cast < float > ( base ) ;
r = r + f * ( index % base ) ;
index = index / base ;
}
return r * 2.0f - 1.0f ;
} ;
2020-12-04 18:26:24 +00:00
RendererSceneCull : : RendererSceneCull ( ) {
2020-12-03 21:09:47 +00:00
render_pass = 1 ;
singleton = this ;
2020-12-23 16:52:58 +00:00
instance_cull_result . set_page_pool ( & instance_cull_page_pool ) ;
instance_shadow_cull_result . set_page_pool ( & instance_cull_page_pool ) ;
2021-02-02 19:51:36 +00:00
for ( uint32_t i = 0 ; i < MAX_UPDATE_SHADOWS ; i + + ) {
render_shadow_data [ i ] . instances . set_page_pool ( & geometry_instance_cull_page_pool ) ;
}
for ( uint32_t i = 0 ; i < SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE ; i + + ) {
render_sdfgi_data [ i ] . instances . set_page_pool ( & geometry_instance_cull_page_pool ) ;
}
2020-12-23 16:52:58 +00:00
2021-05-09 16:23:20 +00:00
scene_cull_result . init ( & rid_cull_page_pool , & geometry_instance_cull_page_pool , & instance_cull_page_pool ) ;
2022-07-23 17:12:41 +00:00
scene_cull_result_threads . resize ( WorkerThreadPool : : get_singleton ( ) - > get_thread_count ( ) ) ;
2022-12-29 00:24:45 +00:00
for ( InstanceCullResult & thread : scene_cull_result_threads ) {
thread . init ( & rid_cull_page_pool , & geometry_instance_cull_page_pool , & instance_cull_page_pool ) ;
2020-12-23 16:52:58 +00:00
}
2021-02-17 16:44:49 +00:00
indexer_update_iterations = GLOBAL_GET ( " rendering/limits/spatial_indexer/update_iterations_per_frame " ) ;
thread_cull_threshold = GLOBAL_GET ( " rendering/limits/spatial_indexer/threaded_cull_minimum_instances " ) ;
2022-07-23 17:12:41 +00:00
thread_cull_threshold = MAX ( thread_cull_threshold , ( uint32_t ) WorkerThreadPool : : get_singleton ( ) - > get_thread_count ( ) ) ; //make sure there is at least one thread per CPU
2021-04-20 16:40:24 +00:00
2022-04-04 14:10:22 +00:00
taa_jitter_array . resize ( TAA_JITTER_COUNT ) ;
for ( int i = 0 ; i < TAA_JITTER_COUNT ; i + + ) {
taa_jitter_array [ i ] . x = get_halton_value ( i , 2 ) ;
taa_jitter_array [ i ] . y = get_halton_value ( i , 3 ) ;
}
2021-04-20 16:40:24 +00:00
dummy_occlusion_culling = memnew ( RendererSceneOcclusionCull ) ;
2020-12-03 21:09:47 +00:00
}
2020-12-04 18:26:24 +00:00
RendererSceneCull : : ~ RendererSceneCull ( ) {
2020-12-23 16:52:58 +00:00
instance_cull_result . reset ( ) ;
instance_shadow_cull_result . reset ( ) ;
2021-02-02 19:51:36 +00:00
for ( uint32_t i = 0 ; i < MAX_UPDATE_SHADOWS ; i + + ) {
render_shadow_data [ i ] . instances . reset ( ) ;
}
for ( uint32_t i = 0 ; i < SDFGI_MAX_CASCADES * SDFGI_MAX_REGIONS_PER_CASCADE ; i + + ) {
render_sdfgi_data [ i ] . instances . reset ( ) ;
}
2020-12-26 12:05:36 +00:00
2021-05-09 16:23:20 +00:00
scene_cull_result . reset ( ) ;
2022-12-29 00:24:45 +00:00
for ( InstanceCullResult & thread : scene_cull_result_threads ) {
thread . reset ( ) ;
2020-12-23 16:52:58 +00:00
}
2021-05-09 16:23:20 +00:00
scene_cull_result_threads . clear ( ) ;
2021-04-20 16:40:24 +00:00
if ( dummy_occlusion_culling ) {
memdelete ( dummy_occlusion_culling ) ;
}
2020-12-03 21:09:47 +00:00
}