Skip to content

Commit

Permalink
Vulkan Volumetric fog redraw convergence
Browse files Browse the repository at this point in the history
Issue: godotengine#62794

This fix calculates the number of frames needed, based on the temporal reprojection amount, to redraw until the volumetric fog fully converges.
  • Loading branch information
Mantle-Core committed Jul 5, 2024
1 parent 20ba2f0 commit a15795c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
17 changes: 15 additions & 2 deletions servers/rendering/renderer_viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ void RendererViewport::_draw_viewport(Viewport *p_viewport) {
}
}

bool can_draw_3d = RSG::scene->is_camera(p_viewport->camera) && !p_viewport->disable_3d;

if (RSG::scene->is_scenario(p_viewport->scenario)) {
RID environment = RSG::scene->scenario_get_environment(p_viewport->scenario);
if (RSG::scene->is_environment(environment)) {
Expand All @@ -286,11 +288,16 @@ void RendererViewport::_draw_viewport(Viewport *p_viewport) {
// The scene renderer will still copy over the last frame, so we need to clear the render target.
force_clear_render_target = true;
}

// Calculate the frames needed for volumetric fog to converge.
if (p_viewport->size != p_viewport->old_size && can_draw_3d) {
float temporal_amount = RSG::scene->environment_get_volumetric_fog_temporal_reprojection_amount(environment);
float convergence_threshold = 0.01f;
p_viewport->frames_needed = Math::ceil(Math::log(convergence_threshold) / Math::log(temporal_amount));
}
}
}

bool can_draw_3d = RSG::scene->is_camera(p_viewport->camera) && !p_viewport->disable_3d;

if ((scenario_draw_canvas_bg || can_draw_3d) && !p_viewport->render_buffers.is_valid()) {
//wants to draw 3D but there is no render buffer, create
p_viewport->render_buffers = RSG::scene->render_buffers_create();
Expand All @@ -307,6 +314,12 @@ void RendererViewport::_draw_viewport(Viewport *p_viewport) {
}
}

// Redraw until volumetric fog converges.
if (p_viewport->frames_needed > 0 && can_draw_3d) {
_draw_3d(p_viewport);
p_viewport->frames_needed--;
}

if (!scenario_draw_canvas_bg && can_draw_3d) {
if (force_clear_render_target) {
RSG::texture_storage->render_target_do_clear_request(p_viewport->render_target);
Expand Down
3 changes: 3 additions & 0 deletions servers/rendering/renderer_viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ class RendererViewport {
// use xr interface to override camera positioning and projection matrices and control output
bool use_xr = false;

int frames_needed;

Size2i internal_size;
Size2i size;
Size2i old_size;
uint32_t view_count;
RID camera;
RID scenario;
Expand Down

0 comments on commit a15795c

Please sign in to comment.