Merge pull request #70476 from BastiaanOlij/move_sdfgi_init

Move SDFGI update logic into clustered renderer
This commit is contained in:
Rémi Verschelde 2022-12-23 23:43:56 +01:00
commit d8a0382e3f
No known key found for this signature in database
GPG Key ID: C3336907360768E1
5 changed files with 112 additions and 101 deletions

View File

@ -3302,6 +3302,99 @@ void RenderForwardClustered::sub_surface_scattering_set_scale(float p_scale, flo
RenderForwardClustered *RenderForwardClustered::singleton = nullptr;
void RenderForwardClustered::sdfgi_update(const Ref<RenderSceneBuffers> &p_render_buffers, RID p_environment, const Vector3 &p_world_position) {
Ref<RenderSceneBuffersRD> rb = p_render_buffers;
ERR_FAIL_COND(rb.is_null());
Ref<RendererRD::GI::SDFGI> sdfgi;
if (rb->has_custom_data(RB_SCOPE_SDFGI)) {
sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
}
bool needs_sdfgi = p_environment.is_valid() && environment_get_sdfgi_enabled(p_environment);
if (!needs_sdfgi) {
if (sdfgi.is_valid()) {
// delete it
sdfgi.unref();
rb->set_custom_data(RB_SCOPE_SDFGI, sdfgi);
}
return;
}
static const uint32_t history_frames_to_converge[RS::ENV_SDFGI_CONVERGE_MAX] = { 5, 10, 15, 20, 25, 30 };
uint32_t requested_history_size = history_frames_to_converge[gi.sdfgi_frames_to_converge];
if (sdfgi.is_valid() && (sdfgi->num_cascades != environment_get_sdfgi_cascades(p_environment) || sdfgi->min_cell_size != environment_get_sdfgi_min_cell_size(p_environment) || requested_history_size != sdfgi->history_size || sdfgi->uses_occlusion != environment_get_sdfgi_use_occlusion(p_environment) || sdfgi->y_scale_mode != environment_get_sdfgi_y_scale(p_environment))) {
//configuration changed, erase
sdfgi.unref();
rb->set_custom_data(RB_SCOPE_SDFGI, sdfgi);
}
if (sdfgi.is_null()) {
// re-create
sdfgi = gi.create_sdfgi(p_environment, p_world_position, requested_history_size);
rb->set_custom_data(RB_SCOPE_SDFGI, sdfgi);
} else {
//check for updates
sdfgi->update(p_environment, p_world_position);
}
}
int RenderForwardClustered::sdfgi_get_pending_region_count(const Ref<RenderSceneBuffers> &p_render_buffers) const {
Ref<RenderSceneBuffersRD> rb = p_render_buffers;
ERR_FAIL_COND_V(rb.is_null(), 0);
if (!rb->has_custom_data(RB_SCOPE_SDFGI)) {
return 0;
}
Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
int dirty_count = 0;
for (uint32_t i = 0; i < sdfgi->cascades.size(); i++) {
const RendererRD::GI::SDFGI::Cascade &c = sdfgi->cascades[i];
if (c.dirty_regions == RendererRD::GI::SDFGI::Cascade::DIRTY_ALL) {
dirty_count++;
} else {
for (int j = 0; j < 3; j++) {
if (c.dirty_regions[j] != 0) {
dirty_count++;
}
}
}
}
return dirty_count;
}
AABB RenderForwardClustered::sdfgi_get_pending_region_bounds(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const {
AABB bounds;
Vector3i from;
Vector3i size;
Ref<RenderSceneBuffersRD> rb = p_render_buffers;
ERR_FAIL_COND_V(rb.is_null(), AABB());
Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
ERR_FAIL_COND_V(sdfgi.is_null(), AABB());
int c = sdfgi->get_pending_region_data(p_region, from, size, bounds);
ERR_FAIL_COND_V(c == -1, AABB());
return bounds;
}
uint32_t RenderForwardClustered::sdfgi_get_pending_region_cascade(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const {
AABB bounds;
Vector3i from;
Vector3i size;
Ref<RenderSceneBuffersRD> rb = p_render_buffers;
ERR_FAIL_COND_V(rb.is_null(), -1);
Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
ERR_FAIL_COND_V(sdfgi.is_null(), -1);
return sdfgi->get_pending_region_data(p_region, from, size, bounds);
}
void RenderForwardClustered::GeometryInstanceForwardClustered::_mark_dirty() {
if (dirty_list_element.in_list()) {
return;

View File

@ -650,6 +650,16 @@ public:
_update_render_base_uniform_set();
}
/* SDFGI UPDATE */
virtual void sdfgi_update(const Ref<RenderSceneBuffers> &p_render_buffers, RID p_environment, const Vector3 &p_world_position) override;
virtual int sdfgi_get_pending_region_count(const Ref<RenderSceneBuffers> &p_render_buffers) const override;
virtual AABB sdfgi_get_pending_region_bounds(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const override;
virtual uint32_t sdfgi_get_pending_region_cascade(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const override;
RID sdfgi_get_ubo() const { return gi.sdfgi_ubo; }
/* GEOMETRY INSTANCE */
virtual RenderGeometryInstance *geometry_instance_create(RID p_base) override;
virtual void geometry_instance_free(RenderGeometryInstance *p_geometry_instance) override;

View File

@ -555,6 +555,15 @@ public:
virtual RID reflection_probe_create_framebuffer(RID p_color, RID p_depth) override;
/* SDFGI UPDATE */
virtual void sdfgi_update(const Ref<RenderSceneBuffers> &p_render_buffers, RID p_environment, const Vector3 &p_world_position) override {}
virtual int sdfgi_get_pending_region_count(const Ref<RenderSceneBuffers> &p_render_buffers) const override { return 0; }
virtual AABB sdfgi_get_pending_region_bounds(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const override { return AABB(); }
virtual uint32_t sdfgi_get_pending_region_cascade(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const override { return 0; }
/* GEOMETRY INSTANCE */
static void _geometry_instance_dependency_changed(Dependency::DependencyChangedNotification p_notification, DependencyTracker *p_tracker);
static void _geometry_instance_dependency_deleted(const RID &p_dependency, DependencyTracker *p_tracker);

View File

@ -51,99 +51,6 @@ void get_vogel_disk(float *r_kernel, int p_sample_count) {
}
}
void RendererSceneRenderRD::sdfgi_update(const Ref<RenderSceneBuffers> &p_render_buffers, RID p_environment, const Vector3 &p_world_position) {
Ref<RenderSceneBuffersRD> rb = p_render_buffers;
ERR_FAIL_COND(rb.is_null());
Ref<RendererRD::GI::SDFGI> sdfgi;
if (rb->has_custom_data(RB_SCOPE_SDFGI)) {
sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
}
bool needs_sdfgi = p_environment.is_valid() && environment_get_sdfgi_enabled(p_environment);
if (!needs_sdfgi) {
if (sdfgi.is_valid()) {
// delete it
sdfgi.unref();
rb->set_custom_data(RB_SCOPE_SDFGI, sdfgi);
}
return;
}
static const uint32_t history_frames_to_converge[RS::ENV_SDFGI_CONVERGE_MAX] = { 5, 10, 15, 20, 25, 30 };
uint32_t requested_history_size = history_frames_to_converge[gi.sdfgi_frames_to_converge];
if (sdfgi.is_valid() && (sdfgi->num_cascades != environment_get_sdfgi_cascades(p_environment) || sdfgi->min_cell_size != environment_get_sdfgi_min_cell_size(p_environment) || requested_history_size != sdfgi->history_size || sdfgi->uses_occlusion != environment_get_sdfgi_use_occlusion(p_environment) || sdfgi->y_scale_mode != environment_get_sdfgi_y_scale(p_environment))) {
//configuration changed, erase
sdfgi.unref();
rb->set_custom_data(RB_SCOPE_SDFGI, sdfgi);
}
if (sdfgi.is_null()) {
// re-create
sdfgi = gi.create_sdfgi(p_environment, p_world_position, requested_history_size);
rb->set_custom_data(RB_SCOPE_SDFGI, sdfgi);
} else {
//check for updates
sdfgi->update(p_environment, p_world_position);
}
}
int RendererSceneRenderRD::sdfgi_get_pending_region_count(const Ref<RenderSceneBuffers> &p_render_buffers) const {
Ref<RenderSceneBuffersRD> rb = p_render_buffers;
ERR_FAIL_COND_V(rb.is_null(), 0);
if (!rb->has_custom_data(RB_SCOPE_SDFGI)) {
return 0;
}
Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
int dirty_count = 0;
for (uint32_t i = 0; i < sdfgi->cascades.size(); i++) {
const RendererRD::GI::SDFGI::Cascade &c = sdfgi->cascades[i];
if (c.dirty_regions == RendererRD::GI::SDFGI::Cascade::DIRTY_ALL) {
dirty_count++;
} else {
for (int j = 0; j < 3; j++) {
if (c.dirty_regions[j] != 0) {
dirty_count++;
}
}
}
}
return dirty_count;
}
AABB RendererSceneRenderRD::sdfgi_get_pending_region_bounds(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const {
AABB bounds;
Vector3i from;
Vector3i size;
Ref<RenderSceneBuffersRD> rb = p_render_buffers;
ERR_FAIL_COND_V(rb.is_null(), AABB());
Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
ERR_FAIL_COND_V(sdfgi.is_null(), AABB());
int c = sdfgi->get_pending_region_data(p_region, from, size, bounds);
ERR_FAIL_COND_V(c == -1, AABB());
return bounds;
}
uint32_t RendererSceneRenderRD::sdfgi_get_pending_region_cascade(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const {
AABB bounds;
Vector3i from;
Vector3i size;
Ref<RenderSceneBuffersRD> rb = p_render_buffers;
ERR_FAIL_COND_V(rb.is_null(), -1);
Ref<RendererRD::GI::SDFGI> sdfgi = rb->get_custom_data(RB_SCOPE_SDFGI);
ERR_FAIL_COND_V(sdfgi.is_null(), -1);
return sdfgi->get_pending_region_data(p_region, from, size, bounds);
}
RID RendererSceneRenderRD::sky_allocate() {
return sky.allocate_sky_rid();
}

View File

@ -217,14 +217,6 @@ public:
RendererRD::SkyRD *get_sky() { return &sky; }
/* SDFGI UPDATE */
virtual void sdfgi_update(const Ref<RenderSceneBuffers> &p_render_buffers, RID p_environment, const Vector3 &p_world_position) override;
virtual int sdfgi_get_pending_region_count(const Ref<RenderSceneBuffers> &p_render_buffers) const override;
virtual AABB sdfgi_get_pending_region_bounds(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const override;
virtual uint32_t sdfgi_get_pending_region_cascade(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const override;
RID sdfgi_get_ubo() const { return gi.sdfgi_ubo; }
/* SKY API */
virtual RID sky_allocate() override;