Merge pull request #89729 from jitspoe/master.shadow_distance_fade_optimization

Shadow fade for omni lights actually stops the shadow from updating while faded out to improve performance.
This commit is contained in:
Rémi Verschelde 2024-04-10 14:21:56 +02:00
commit c59f493620
No known key found for this signature in database
GPG Key ID: C3336907360768E1
5 changed files with 57 additions and 4 deletions

View File

@ -441,6 +441,29 @@ public:
virtual void light_instance_set_shadow_transform(RID p_light_instance, const Projection &p_projection, const Transform3D &p_transform, float p_far, float p_split, int p_pass, float p_shadow_texel_size, float p_bias_scale = 1.0, float p_range_begin = 0, const Vector2 &p_uv_scale = Vector2()) override;
virtual void light_instance_mark_visible(RID p_light_instance) override;
virtual bool light_instance_is_shadow_visible_at_position(RID p_light_instance, const Vector3 &p_position) const override {
const LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
ERR_FAIL_NULL_V(light_instance, false);
const Light *light = light_owner.get_or_null(light_instance->light);
ERR_FAIL_NULL_V(light, false);
if (!light->shadow) {
return false;
}
if (!light->distance_fade) {
return true;
}
real_t distance = p_position.distance_to(light_instance->transform.origin);
if (distance > light->distance_fade_shadow + light->distance_fade_length) {
return false;
}
return true;
}
_FORCE_INLINE_ RID light_instance_get_base_light(RID p_light_instance) {
LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
return li->light;

View File

@ -91,6 +91,7 @@ public:
void light_instance_set_aabb(RID p_light_instance, const AABB &p_aabb) override {}
void light_instance_set_shadow_transform(RID p_light_instance, const Projection &p_projection, const Transform3D &p_transform, float p_far, float p_split, int p_pass, float p_shadow_texel_size, float p_bias_scale = 1.0, float p_range_begin = 0, const Vector2 &p_uv_scale = Vector2()) override {}
void light_instance_mark_visible(RID p_light_instance) override {}
virtual bool light_instance_is_shadow_visible_at_position(RID p_light_instance, const Vector3 &p_position) const override { return false; }
/* PROBE API */
virtual RID reflection_probe_allocate() override { return RID(); }

View File

@ -590,6 +590,29 @@ public:
virtual void light_instance_set_shadow_transform(RID p_light_instance, const Projection &p_projection, const Transform3D &p_transform, float p_far, float p_split, int p_pass, float p_shadow_texel_size, float p_bias_scale = 1.0, float p_range_begin = 0, const Vector2 &p_uv_scale = Vector2()) override;
virtual void light_instance_mark_visible(RID p_light_instance) override;
virtual bool light_instance_is_shadow_visible_at_position(RID p_light_instance, const Vector3 &p_position) const override {
const LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
ERR_FAIL_NULL_V(light_instance, false);
const Light *light = light_owner.get_or_null(light_instance->light);
ERR_FAIL_NULL_V(light, false);
if (!light->shadow) {
return false;
}
if (!light->distance_fade) {
return true;
}
real_t distance = p_position.distance_to(light_instance->transform.origin);
if (distance > light->distance_fade_shadow + light->distance_fade_length) {
return false;
}
return true;
}
_FORCE_INLINE_ RID light_instance_get_base_light(RID p_light_instance) {
LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
return li->light;

View File

@ -3029,6 +3029,7 @@ void RendererSceneCull::_render_scene(const RendererSceneRender::CameraData *p_c
light_culler->prepare_camera(p_camera_data->main_transform, p_camera_data->main_projection);
Scenario *scenario = scenario_owner.get_or_null(p_scenario);
Vector3 camera_position = p_camera_data->main_transform.origin;
ERR_FAIL_COND(p_render_buffers.is_null());
@ -3038,7 +3039,7 @@ void RendererSceneCull::_render_scene(const RendererSceneRender::CameraData *p_c
if (p_reflection_probe.is_null()) {
//no rendering code here, this is only to set up what needs to be done, request regions, etc.
scene_render->sdfgi_update(p_render_buffers, p_environment, p_camera_data->main_transform.origin); //update conditions for SDFGI (whether its used or not)
scene_render->sdfgi_update(p_render_buffers, p_environment, camera_position); //update conditions for SDFGI (whether its used or not)
}
RENDER_TIMESTAMP("Update Visibility Dependencies");
@ -3051,7 +3052,7 @@ void RendererSceneCull::_render_scene(const RendererSceneRender::CameraData *p_c
VisibilityCullData visibility_cull_data;
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;
visibility_cull_data.camera_position = camera_position;
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);
@ -3220,16 +3221,20 @@ void RendererSceneCull::_render_scene(const RendererSceneRender::CameraData *p_c
}
}
// Positional Shadowss
// Positional Shadows
for (uint32_t i = 0; i < (uint32_t)scene_cull_result.lights.size(); i++) {
Instance *ins = scene_cull_result.lights[i];
if (!p_shadow_atlas.is_valid() || !RSG::light_storage->light_has_shadow(ins->base)) {
if (!p_shadow_atlas.is_valid()) {
continue;
}
InstanceLightData *light = static_cast<InstanceLightData *>(ins->base_data);
if (!RSG::light_storage->light_instance_is_shadow_visible_at_position(light->instance, camera_position)) {
continue;
}
float coverage = 0.f;
{ //compute coverage

View File

@ -98,6 +98,7 @@ public:
virtual bool light_instances_can_render_shadow_cube() const {
return true;
}
virtual bool light_instance_is_shadow_visible_at_position(RID p_light, const Vector3 &p_position) const = 0;
/* PROBE API */