Skip to content

Commit

Permalink
Improve SSR raymarching performance and quality
Browse files Browse the repository at this point in the history
  • Loading branch information
Flarkk committed Nov 26, 2024
1 parent 3dbef70 commit 8ce6f6f
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 240 deletions.
25 changes: 10 additions & 15 deletions servers/rendering/renderer_rd/effects/ss_effects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,13 @@ void SSEffects::screen_space_reflection(Ref<RenderSceneBuffersRD> p_render_buffe

RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);

Projection correction = Projection::create_depth_correction(false);
Projection corrected[RendererSceneRender::MAX_RENDER_VIEWS], corrected_inverse[RendererSceneRender::MAX_RENDER_VIEWS];
for (uint32_t v = 0; v < view_count; v++) {
corrected[v] = correction * p_projections[v];
corrected_inverse[v] = corrected[v].inverse();
}

{
// Store some scene data in a UBO, in the near future we will use a UBO shared with other shaders
ScreenSpaceReflectionSceneData scene_data;
Expand All @@ -1386,8 +1393,8 @@ void SSEffects::screen_space_reflection(Ref<RenderSceneBuffersRD> p_render_buffe
}

for (uint32_t v = 0; v < view_count; v++) {
store_camera(p_projections[v], scene_data.projection[v]);
store_camera(p_projections[v].inverse(), scene_data.inv_projection[v]);
store_camera(corrected[v], scene_data.projection[v]);
store_camera(corrected_inverse[v], scene_data.inv_projection[v]);
scene_data.eye_offset[v][0] = p_eye_offsets[v].x;
scene_data.eye_offset[v][1] = p_eye_offsets[v].y;
scene_data.eye_offset[v][2] = p_eye_offsets[v].z;
Expand Down Expand Up @@ -1425,10 +1432,7 @@ void SSEffects::screen_space_reflection(Ref<RenderSceneBuffersRD> p_render_buffe
RD::get_singleton()->draw_command_begin_label("SSR Scale");

ScreenSpaceReflectionScalePushConstant push_constant;
push_constant.view_index = v;
push_constant.camera_z_far = p_projections[v].get_z_far();
push_constant.camera_z_near = p_projections[v].get_z_near();
push_constant.orthogonal = p_projections[v].is_orthogonal();
store_camera(corrected_inverse[v], push_constant.inv_projection);
push_constant.filter = false; // Enabling causes artifacts.
push_constant.screen_size[0] = p_ssr_buffers.size.x;
push_constant.screen_size[1] = p_ssr_buffers.size.y;
Expand Down Expand Up @@ -1464,20 +1468,12 @@ void SSEffects::screen_space_reflection(Ref<RenderSceneBuffersRD> p_render_buffe

ScreenSpaceReflectionPushConstant push_constant;
push_constant.view_index = v;
push_constant.camera_z_far = p_projections[v].get_z_far();
push_constant.camera_z_near = p_projections[v].get_z_near();
push_constant.orthogonal = p_projections[v].is_orthogonal();
push_constant.screen_size[0] = p_ssr_buffers.size.x;
push_constant.screen_size[1] = p_ssr_buffers.size.y;
push_constant.curve_fade_in = p_fade_in;
push_constant.distance_fade = p_fade_out;
push_constant.num_steps = p_max_steps;
push_constant.depth_tolerance = p_tolerance;
push_constant.use_half_res = true;
push_constant.proj_info[0] = -2.0f / (p_ssr_buffers.size.width * p_projections[v].columns[0][0]);
push_constant.proj_info[1] = -2.0f / (p_ssr_buffers.size.height * p_projections[v].columns[1][1]);
push_constant.proj_info[2] = (1.0f - p_projections[v].columns[0][2]) / p_projections[v].columns[0][0];
push_constant.proj_info[3] = (1.0f + p_projections[v].columns[1][2]) / p_projections[v].columns[1][1];

ScreenSpaceReflectionMode mode = (ssr_roughness_quality != RS::ENV_SSR_ROUGHNESS_QUALITY_DISABLED) ? SCREEN_SPACE_REFLECTION_ROUGH : SCREEN_SPACE_REFLECTION_NORMAL;
RID shader = ssr.shader.version_get_shader(ssr.shader_version, mode);
Expand Down Expand Up @@ -1523,7 +1519,6 @@ void SSEffects::screen_space_reflection(Ref<RenderSceneBuffersRD> p_render_buffe

ScreenSpaceReflectionFilterPushConstant push_constant;
push_constant.view_index = v;
push_constant.orthogonal = p_projections[v].is_orthogonal();
push_constant.edge_tolerance = Math::sin(Math::deg_to_rad(15.0));
push_constant.proj_info[0] = -2.0f / (p_ssr_buffers.size.width * p_projections[v].columns[0][0]);
push_constant.proj_info[1] = -2.0f / (p_ssr_buffers.size.height * p_projections[v].columns[1][1]);
Expand Down
48 changes: 18 additions & 30 deletions servers/rendering/renderer_rd/effects/ss_effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,14 +425,11 @@ class SSEffects {
// SSR Scale

struct ScreenSpaceReflectionScalePushConstant {
int32_t screen_size[2];
float camera_z_near;
float camera_z_far;
float inv_projection[16];

uint32_t orthogonal;
int32_t screen_size[2];
uint32_t filter;
uint32_t view_index;
uint32_t pad1;
uint32_t pad;
};

struct ScreenSpaceReflectionScale {
Expand All @@ -450,23 +447,14 @@ class SSEffects {
};

struct ScreenSpaceReflectionPushConstant {
float proj_info[4]; // 16 - 16

int32_t screen_size[2]; // 8 - 24
float camera_z_near; // 4 - 28
float camera_z_far; // 4 - 32

int32_t num_steps; // 4 - 36
float depth_tolerance; // 4 - 40
float distance_fade; // 4 - 44
float curve_fade_in; // 4 - 48

uint32_t orthogonal; // 4 - 52
float filter_mipmap_levels; // 4 - 56
uint32_t use_half_res; // 4 - 60
uint32_t view_index; // 4 - 64
int32_t screen_size[2];
int32_t num_steps;
float depth_tolerance;

// float projection[16]; // this is in our ScreenSpaceReflectionSceneData now
float distance_fade;
float curve_fade_in;
uint32_t view_index;
uint32_t pad;
};

struct ScreenSpaceReflection {
Expand All @@ -480,16 +468,16 @@ class SSEffects {
// SSR Filter

struct ScreenSpaceReflectionFilterPushConstant {
float proj_info[4]; // 16 - 16
float proj_info[4];

uint32_t orthogonal; // 4 - 20
float edge_tolerance; // 4 - 24
int32_t increment; // 4 - 28
uint32_t view_index; // 4 - 32
uint32_t pad;
float edge_tolerance;
int32_t increment;
uint32_t view_index;

int32_t screen_size[2]; // 8 - 40
uint32_t vertical; // 4 - 44
uint32_t steps; // 4 - 48
int32_t screen_size[2];
uint32_t vertical;
uint32_t steps;
};

enum SSRReflectionMode {
Expand Down
Loading

0 comments on commit 8ce6f6f

Please sign in to comment.