Skip to content

Commit

Permalink
Increase SSR performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Flarkk committed Nov 25, 2024
1 parent 3dbef70 commit b4e8bfd
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 241 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 @@ -1385,9 +1385,12 @@ void SSEffects::screen_space_reflection(Ref<RenderSceneBuffersRD> p_render_buffe
ssr.ubo = RD::get_singleton()->uniform_buffer_create(sizeof(ScreenSpaceReflectionSceneData));
}

Projection correction = Projection::create_depth_correction(false);

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]);
Projection corrected = correction * p_projections[v];
store_camera(corrected, scene_data.projection[v]);
store_camera(corrected.inverse(), 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 @@ -1424,11 +1427,12 @@ void SSEffects::screen_space_reflection(Ref<RenderSceneBuffersRD> p_render_buffe
{ //scale color and depth to half
RD::get_singleton()->draw_command_begin_label("SSR Scale");

Projection correction;
correction.set_depth_correction(false);
Projection temp = correction * p_projections[v];

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(temp.inverse(), 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
50 changes: 19 additions & 31 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

uint32_t orthogonal; // 4 - 20
float edge_tolerance; // 4 - 24
int32_t increment; // 4 - 28
uint32_t view_index; // 4 - 32
float proj_info[4];
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 b4e8bfd

Please sign in to comment.