Skip to content

Commit

Permalink
Merge pull request godotengine#76550 from RandomShaper/vk_gi_take_2
Browse files Browse the repository at this point in the history
Fix voxel GI issues (2)
  • Loading branch information
mhilbrunner authored May 3, 2023
2 parents c14f870 + c58e50a commit ba32279
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 47 deletions.
50 changes: 33 additions & 17 deletions servers/rendering/renderer_rd/environment/fog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,37 @@ Fog::FogShaderData::~FogShaderData() {
////////////////////////////////////////////////////////////////////////////////
// Volumetric Fog

bool Fog::VolumetricFog::sync_gi_dependent_sets_validity(bool p_ensure_freed) {
bool null = gi_dependent_sets.copy_uniform_set.is_null();
bool valid = !null && RD::get_singleton()->uniform_set_is_valid(gi_dependent_sets.copy_uniform_set);

#ifdef DEV_ENABLED
// It's all-or-nothing, or something else has changed that requires dev attention.
DEV_ASSERT(null == gi_dependent_sets.process_uniform_set_density.is_null());
DEV_ASSERT(null == gi_dependent_sets.process_uniform_set.is_null());
DEV_ASSERT(null == gi_dependent_sets.process_uniform_set2.is_null());
DEV_ASSERT(valid == RD::get_singleton()->uniform_set_is_valid(gi_dependent_sets.process_uniform_set_density));
DEV_ASSERT(valid == RD::get_singleton()->uniform_set_is_valid(gi_dependent_sets.process_uniform_set));
DEV_ASSERT(valid == RD::get_singleton()->uniform_set_is_valid(gi_dependent_sets.process_uniform_set2));
#endif

if (valid) {
if (p_ensure_freed) {
RD::get_singleton()->free(gi_dependent_sets.copy_uniform_set);
RD::get_singleton()->free(gi_dependent_sets.process_uniform_set_density);
RD::get_singleton()->free(gi_dependent_sets.process_uniform_set);
RD::get_singleton()->free(gi_dependent_sets.process_uniform_set2);
valid = false;
}
}

if (!valid && !null) {
gi_dependent_sets = {};
}

return valid;
}

void Fog::VolumetricFog::init(const Vector3i &fog_size, RID p_sky_shader) {
width = fog_size.x;
height = fog_size.y;
Expand Down Expand Up @@ -464,17 +495,7 @@ Fog::VolumetricFog::~VolumetricFog() {
RD::get_singleton()->free(fog_uniform_set);
}

// At this point, due to cascade deletions, the sets may no longer be valid, but still they must work as a group.
gi_dependent_sets.valid = RD::get_singleton()->uniform_set_is_valid(gi_dependent_sets.process_uniform_set_density);
#ifdef DEV_ENABLED
gi_dependent_sets.assert_actual_validity();
#endif
if (gi_dependent_sets.valid) {
RD::get_singleton()->free(gi_dependent_sets.copy_uniform_set);
RD::get_singleton()->free(gi_dependent_sets.process_uniform_set_density);
RD::get_singleton()->free(gi_dependent_sets.process_uniform_set);
RD::get_singleton()->free(gi_dependent_sets.process_uniform_set2);
}
sync_gi_dependent_sets_validity(true);

if (sdfgi_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(sdfgi_uniform_set)) {
RD::get_singleton()->free(sdfgi_uniform_set);
Expand Down Expand Up @@ -717,10 +738,7 @@ void Fog::volumetric_fog_update(const VolumetricFogSettings &p_settings, const P
RD::get_singleton()->compute_list_end();
}

#ifdef DEV_ENABLED
fog->gi_dependent_sets.assert_actual_validity();
#endif
if (!fog->gi_dependent_sets.valid) {
if (!fog->sync_gi_dependent_sets_validity()) {
//re create uniform set if needed
Vector<RD::Uniform> uniforms;
Vector<RD::Uniform> copy_uniforms;
Expand Down Expand Up @@ -932,8 +950,6 @@ void Fog::volumetric_fog_update(const VolumetricFogSettings &p_settings, const P
uniforms.remove_at(8);
uniforms.write[7].set_id(0, aux7);
fog->gi_dependent_sets.process_uniform_set_density = RD::get_singleton()->uniform_set_create(uniforms, volumetric_fog.process_shader.version_get_shader(volumetric_fog.process_shader_version, VolumetricFogShader::VOLUMETRIC_FOG_PROCESS_SHADER_DENSITY), 0);

fog->gi_dependent_sets.valid = true;
}

bool using_sdfgi = RendererSceneRenderRD::get_singleton()->environment_get_volumetric_fog_gi_inject(p_settings.env) > 0.0001 && RendererSceneRenderRD::get_singleton()->environment_get_sdfgi_enabled(p_settings.env) && (p_settings.sdfgi.is_valid());
Expand Down
13 changes: 2 additions & 11 deletions servers/rendering/renderer_rd/environment/fog.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,21 +303,10 @@ class Fog : public RendererFog {
RID fog_uniform_set;

struct {
bool valid = false;
RID copy_uniform_set;
RID process_uniform_set_density;
RID process_uniform_set;
RID process_uniform_set2;

#ifdef DEV_ENABLED
void assert_actual_validity() {
// It's all-or-nothing, or something else has changed that requires dev attention.
DEV_ASSERT(valid == RD::get_singleton()->uniform_set_is_valid(copy_uniform_set));
DEV_ASSERT(valid == RD::get_singleton()->uniform_set_is_valid(process_uniform_set_density));
DEV_ASSERT(valid == RD::get_singleton()->uniform_set_is_valid(process_uniform_set));
DEV_ASSERT(valid == RD::get_singleton()->uniform_set_is_valid(process_uniform_set2));
}
#endif
} gi_dependent_sets;

RID sdfgi_uniform_set;
Expand All @@ -328,6 +317,8 @@ class Fog : public RendererFog {
virtual void configure(RenderSceneBuffersRD *p_render_buffers) override{};
virtual void free_data() override{};

bool sync_gi_dependent_sets_validity(bool p_ensure_freed = false);

void init(const Vector3i &fog_size, RID p_sky_shader);
~VolumetricFog();
};
Expand Down
14 changes: 0 additions & 14 deletions servers/rendering/renderer_rd/environment/gi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3695,20 +3695,6 @@ void GI::setup_voxel_gi_instances(RenderDataRD *p_render_data, Ref<RenderSceneBu
}
rbgi->uniform_set[v] = RID();
}
if (p_render_buffers->has_custom_data(RB_SCOPE_FOG)) {
Ref<Fog::VolumetricFog> fog = p_render_buffers->get_custom_data(RB_SCOPE_FOG);

#ifdef DEV_ENABLED
fog->gi_dependent_sets.assert_actual_validity();
#endif
if (fog->gi_dependent_sets.valid) {
RD::get_singleton()->free(fog->gi_dependent_sets.copy_uniform_set);
RD::get_singleton()->free(fog->gi_dependent_sets.process_uniform_set_density);
RD::get_singleton()->free(fog->gi_dependent_sets.process_uniform_set);
RD::get_singleton()->free(fog->gi_dependent_sets.process_uniform_set2);
fog->gi_dependent_sets.valid = false;
}
}
}

if (p_voxel_gi_instances.size() > 0) {
Expand Down
10 changes: 5 additions & 5 deletions servers/rendering/renderer_rd/storage_rd/light_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ class LightStorage : public RendererLightStorage {
struct ShadowTransform {
Projection camera;
Transform3D transform;
float farplane;
float split;
float bias_scale;
float shadow_texel_size;
float range_begin;
float farplane = 0.0;
float split = 0.0;
float bias_scale = 0.0;
float shadow_texel_size = 0.0;
float range_begin = 0.0;
Rect2 atlas_rect;
Vector2 uv_scale;
};
Expand Down

0 comments on commit ba32279

Please sign in to comment.