Skip to content

Commit

Permalink
Fixes to SSR, WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
reduz authored and amugana committed Jun 28, 2017
1 parent 5b49e52 commit d564b5a
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 99 deletions.
14 changes: 6 additions & 8 deletions drivers/gles3/rasterizer_scene_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,17 +887,16 @@ void RasterizerSceneGLES3::environment_set_glow(RID p_env, bool p_enable, int p_
void RasterizerSceneGLES3::environment_set_fog(RID p_env, bool p_enable, float p_begin, float p_end, RID p_gradient_texture) {
}

void RasterizerSceneGLES3::environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_accel, float p_fade, float p_depth_tolerance, bool p_smooth, bool p_roughness) {
void RasterizerSceneGLES3::environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_fade_int, float p_fade_out, float p_depth_tolerance, bool p_roughness) {

Environment *env = environment_owner.getornull(p_env);
ERR_FAIL_COND(!env);

env->ssr_enabled = p_enable;
env->ssr_max_steps = p_max_steps;
env->ssr_accel = p_accel;
env->ssr_fade = p_fade;
env->ssr_fade_in = p_fade_in;
env->ssr_fade_out = p_fade_out;
env->ssr_depth_tolerance = p_depth_tolerance;
env->ssr_smooth = p_smooth;
env->ssr_roughness = p_roughness;
}

Expand Down Expand Up @@ -3253,8 +3252,7 @@ void RasterizerSceneGLES3::_render_mrts(Environment *env, const CameraMatrix &p_

//perform SSR

state.ssr_shader.set_conditional(ScreenSpaceReflectionShaderGLES3::SMOOTH_ACCEL, env->ssr_accel > 0 && env->ssr_smooth);
state.ssr_shader.set_conditional(ScreenSpaceReflectionShaderGLES3::REFLECT_ROUGHNESS, env->ssr_accel > 0 && env->ssr_roughness);
state.ssr_shader.set_conditional(ScreenSpaceReflectionShaderGLES3::REFLECT_ROUGHNESS, env->ssr_roughness);

state.ssr_shader.bind();

Expand All @@ -3270,9 +3268,9 @@ void RasterizerSceneGLES3::_render_mrts(Environment *env, const CameraMatrix &p_
//state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::FRAME_INDEX,int(render_pass));
state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::FILTER_MIPMAP_LEVELS, float(storage->frame.current_rt->effects.mip_maps[0].sizes.size()));
state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::NUM_STEPS, env->ssr_max_steps);
state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::ACCELERATION, env->ssr_accel);
state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::DEPTH_TOLERANCE, env->ssr_depth_tolerance);
state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::DISTANCE_FADE, env->ssr_fade);
state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::DISTANCE_FADE, env->ssr_fade_in);
state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::CURVE_FADE_IN, env->ssr_fade_out);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
Expand Down
12 changes: 5 additions & 7 deletions drivers/gles3/rasterizer_scene_gles3.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,9 @@ class RasterizerSceneGLES3 : public RasterizerScene {

bool ssr_enabled;
int ssr_max_steps;
float ssr_accel;
float ssr_fade;
float ssr_fade_in;
float ssr_fade_out;
float ssr_depth_tolerance;
bool ssr_smooth;
bool ssr_roughness;

bool ssao_enabled;
Expand Down Expand Up @@ -439,10 +438,9 @@ class RasterizerSceneGLES3 : public RasterizerScene {

ssr_enabled = false;
ssr_max_steps = 64;
ssr_accel = 0.04;
ssr_fade = 2.0;
ssr_fade_in = 2.0;
ssr_fade_out = 2.0;
ssr_depth_tolerance = 0.2;
ssr_smooth = true;
ssr_roughness = true;

ssao_enabled = false;
Expand Down Expand Up @@ -527,7 +525,7 @@ class RasterizerSceneGLES3 : public RasterizerScene {
virtual void environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_bloom_treshold, VS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_treshold, float p_hdr_bleed_scale, bool p_bicubic_upscale);
virtual void environment_set_fog(RID p_env, bool p_enable, float p_begin, float p_end, RID p_gradient_texture);

virtual void environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_accel, float p_fade, float p_depth_tolerance, bool p_smooth, bool p_roughness);
virtual void environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_fade_int, float p_fade_out, float p_depth_tolerance, bool p_roughness);
virtual void environment_set_ssao(RID p_env, bool p_enable, float p_radius, float p_radius2, float p_intensity2, float p_intensity, float p_bias, float p_light_affect, const Color &p_color, bool p_blur);

virtual void environment_set_tonemap(RID p_env, VS::EnvironmentToneMapper p_tone_mapper, float p_exposure, float p_white, bool p_auto_exposure, float p_min_luminance, float p_max_luminance, float p_auto_exp_speed, float p_auto_exp_scale);
Expand Down
41 changes: 4 additions & 37 deletions drivers/gles3/shaders/screen_space_reflection.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ uniform mat4 projection;
uniform int num_steps;
uniform float depth_tolerance;
uniform float distance_fade;
uniform float acceleration;
uniform float curve_fade_in;


layout(location = 0) out vec4 frag_color;

Expand Down Expand Up @@ -148,8 +149,6 @@ void main() {

bool found=false;

//if acceleration > 0, distance between pixels gets larger each step. This allows covering a larger area
float accel=1.0+acceleration;
float steps_taken=0.0;

for(int i=0;i<num_steps;i++) {
Expand Down Expand Up @@ -177,9 +176,6 @@ void main() {

steps_taken+=1.0;
prev_pos=pos;
z_advance*=accel;
w_advance*=accel;
line_advance*=accel;
}


Expand Down Expand Up @@ -207,39 +203,10 @@ void main() {

vec2 final_pos;
float grad;
grad=steps_taken/float(num_steps);
final_pos=pos;

#ifdef SMOOTH_ACCEL
//if the distance between point and prev point is >1, then take some samples in the middle for smoothing out the image
vec2 blend_dir = pos - prev_pos;
float steps = min(8.0,length(blend_dir));
if (steps>2.0) {
vec2 blend_step = blend_dir/steps;
float blend_z = (z_to-z_from)/steps;
vec2 new_pos;
float subgrad=0.0;
for(float i=0.0;i<steps;i++) {

new_pos = (prev_pos+blend_step*i);
float z = z_from+blend_z*i;

depth = texture(source_depth, new_pos*pixel_size).r * 2.0 - 1.0;
depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - depth * (camera_z_far - camera_z_near));
depth=-depth;

subgrad=i/steps;
if (depth>z)
break;
}

final_pos = new_pos;
grad=(steps_taken+subgrad)/float(num_steps);

} else {
#endif
grad=steps_taken/float(num_steps);
final_pos=pos;
#ifdef SMOOTH_ACCEL
}

#endif

Expand Down
55 changes: 22 additions & 33 deletions scene/resources/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ void Environment::_validate_property(PropertyInfo &property) const {
void Environment::set_ssr_enabled(bool p_enable) {

ssr_enabled = p_enable;
VS::get_singleton()->environment_set_ssr(environment, ssr_enabled, ssr_max_steps, ssr_accel, ssr_fade, ssr_depth_tolerance, ssr_smooth, ssr_roughness);
VS::get_singleton()->environment_set_ssr(environment, ssr_enabled, ssr_max_steps, ssr_fade_in, ssr_fade_out, ssr_depth_tolerance, ssr_roughness);
_change_notify();
}

Expand All @@ -327,57 +327,47 @@ bool Environment::is_ssr_enabled() const {
void Environment::set_ssr_max_steps(int p_steps) {

ssr_max_steps = p_steps;
VS::get_singleton()->environment_set_ssr(environment, ssr_enabled, ssr_max_steps, ssr_accel, ssr_fade, ssr_depth_tolerance, ssr_smooth, ssr_roughness);
VS::get_singleton()->environment_set_ssr(environment, ssr_enabled, ssr_max_steps, ssr_fade_in, ssr_fade_out, ssr_depth_tolerance, ssr_roughness);
}
int Environment::get_ssr_max_steps() const {

return ssr_max_steps;
}

void Environment::set_ssr_accel(float p_accel) {
void Environment::set_ssr_fade_in(float p_fade_in) {

ssr_accel = p_accel;
VS::get_singleton()->environment_set_ssr(environment, ssr_enabled, ssr_max_steps, ssr_accel, ssr_fade, ssr_depth_tolerance, ssr_smooth, ssr_roughness);
ssr_fade_in = p_fade_in;
VS::get_singleton()->environment_set_ssr(environment, ssr_enabled, ssr_max_steps, ssr_fade_in, ssr_fade_out, ssr_depth_tolerance, ssr_roughness);
}
float Environment::get_ssr_accel() const {
float Environment::get_ssr_fade_in() const {

return ssr_accel;
return ssr_fade_in;
}

void Environment::set_ssr_fade(float p_fade) {
void Environment::set_ssr_fade_out(float p_fade_out) {

ssr_fade = p_fade;
VS::get_singleton()->environment_set_ssr(environment, ssr_enabled, ssr_max_steps, ssr_accel, ssr_fade, ssr_depth_tolerance, ssr_smooth, ssr_roughness);
ssr_fade_out = p_fade_out;
VS::get_singleton()->environment_set_ssr(environment, ssr_enabled, ssr_max_steps, ssr_fade_in, ssr_fade_out, ssr_depth_tolerance, ssr_roughness);
}
float Environment::get_ssr_fade() const {
float Environment::get_ssr_fade_out() const {

return ssr_fade;
return ssr_fade_out;
}

void Environment::set_ssr_depth_tolerance(float p_depth_tolerance) {

ssr_depth_tolerance = p_depth_tolerance;
VS::get_singleton()->environment_set_ssr(environment, ssr_enabled, ssr_max_steps, ssr_accel, ssr_fade, ssr_depth_tolerance, ssr_smooth, ssr_roughness);
VS::get_singleton()->environment_set_ssr(environment, ssr_enabled, ssr_max_steps, ssr_fade_in, ssr_fade_out, ssr_depth_tolerance, ssr_roughness);
}
float Environment::get_ssr_depth_tolerance() const {

return ssr_depth_tolerance;
}

void Environment::set_ssr_smooth(bool p_enable) {

ssr_smooth = p_enable;
VS::get_singleton()->environment_set_ssr(environment, ssr_enabled, ssr_max_steps, ssr_accel, ssr_fade, ssr_depth_tolerance, ssr_smooth, ssr_roughness);
}
bool Environment::is_ssr_smooth() const {

return ssr_smooth;
}

void Environment::set_ssr_rough(bool p_enable) {

ssr_roughness = p_enable;
VS::get_singleton()->environment_set_ssr(environment, ssr_enabled, ssr_max_steps, ssr_accel, ssr_fade, ssr_depth_tolerance, ssr_smooth, ssr_roughness);
VS::get_singleton()->environment_set_ssr(environment, ssr_enabled, ssr_max_steps, ssr_fade_in, ssr_fade_out, ssr_depth_tolerance, ssr_roughness);
}
bool Environment::is_ssr_rough() const {

Expand Down Expand Up @@ -954,11 +944,11 @@ void Environment::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_ssr_max_steps", "max_steps"), &Environment::set_ssr_max_steps);
ClassDB::bind_method(D_METHOD("get_ssr_max_steps"), &Environment::get_ssr_max_steps);

ClassDB::bind_method(D_METHOD("set_ssr_accel", "accel"), &Environment::set_ssr_accel);
ClassDB::bind_method(D_METHOD("get_ssr_accel"), &Environment::get_ssr_accel);
ClassDB::bind_method(D_METHOD("set_ssr_fade_in", "fade_in"), &Environment::set_ssr_fade_in);
ClassDB::bind_method(D_METHOD("get_ssr_fade_in"), &Environment::get_ssr_fade_in);

ClassDB::bind_method(D_METHOD("set_ssr_fade", "fade"), &Environment::set_ssr_fade);
ClassDB::bind_method(D_METHOD("get_ssr_fade"), &Environment::get_ssr_fade);
ClassDB::bind_method(D_METHOD("set_ssr_fade_out", "fade_out"), &Environment::set_ssr_fade_out);
ClassDB::bind_method(D_METHOD("get_ssr_fade_out"), &Environment::get_ssr_fade_out);

ClassDB::bind_method(D_METHOD("set_ssr_depth_tolerance", "depth_tolerance"), &Environment::set_ssr_depth_tolerance);
ClassDB::bind_method(D_METHOD("get_ssr_depth_tolerance"), &Environment::get_ssr_depth_tolerance);
Expand All @@ -973,9 +963,9 @@ void Environment::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ss_reflections_enabled"), "set_ssr_enabled", "is_ssr_enabled");
ADD_PROPERTY(PropertyInfo(Variant::INT, "ss_reflections_max_steps", PROPERTY_HINT_RANGE, "1,512,1"), "set_ssr_max_steps", "get_ssr_max_steps");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "ss_reflections_accel", PROPERTY_HINT_RANGE, "0,4,0.01"), "set_ssr_accel", "get_ssr_accel");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "ss_reflections_fade", PROPERTY_HINT_EXP_EASING), "set_ssr_fade", "get_ssr_fade");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "ss_reflections_fade_in", PROPERTY_HINT_EXP_EASING), "set_ssr_fade_in", "get_ssr_fade_in");
v ADD_PROPERTY(PropertyInfo(Variant::REAL, "ss_reflections_fade_out", PROPERTY_HINT_EXP_EASING), "set_ssr_fade_out", "get_ssr_fade_out");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "ss_reflections_depth_tolerance", PROPERTY_HINT_RANGE, "0.1,128,0.1"), "set_ssr_depth_tolerance", "get_ssr_depth_tolerance");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ss_reflections_accel_smooth"), "set_ssr_smooth", "is_ssr_smooth");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ss_reflections_roughness"), "set_ssr_rough", "is_ssr_rough");

ClassDB::bind_method(D_METHOD("set_ssao_enabled", "enabled"), &Environment::set_ssao_enabled);
Expand Down Expand Up @@ -1179,10 +1169,9 @@ Environment::Environment() {

ssr_enabled = false;
ssr_max_steps = 64;
ssr_accel = 0.04;
ssr_fade = 2.0;
ssr_fade_in = 2.0;
ssr_fade_out = 2.0;
ssr_depth_tolerance = 0.2;
ssr_smooth = true;
ssr_roughness = true;

ssao_enabled = false;
Expand Down
16 changes: 6 additions & 10 deletions scene/resources/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ class Environment : public Resource {

bool ssr_enabled;
int ssr_max_steps;
float ssr_accel;
float ssr_fade;
float ssr_fade_in;
float ssr_fade_out;
float ssr_depth_tolerance;
bool ssr_smooth;
bool ssr_roughness;

bool ssao_enabled;
Expand Down Expand Up @@ -225,18 +224,15 @@ class Environment : public Resource {
void set_ssr_max_steps(int p_steps);
int get_ssr_max_steps() const;

void set_ssr_accel(float p_accel);
float get_ssr_accel() const;
void set_ssr_fade_in(float p_transition);
float get_ssr_fade_in() const;

void set_ssr_fade(float p_transition);
float get_ssr_fade() const;
void set_ssr_fade_out(float p_transition);
float get_ssr_fade_out() const;

void set_ssr_depth_tolerance(float p_depth_tolerance);
float get_ssr_depth_tolerance() const;

void set_ssr_smooth(bool p_enable);
bool is_ssr_smooth() const;

void set_ssr_rough(bool p_enable);
bool is_ssr_rough() const;

Expand Down
2 changes: 1 addition & 1 deletion servers/visual/rasterizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class RasterizerScene {
virtual void environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_bloom_treshold, VS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_treshold, float p_hdr_bleed_scale, bool p_bicubic_upscale) = 0;
virtual void environment_set_fog(RID p_env, bool p_enable, float p_begin, float p_end, RID p_gradient_texture) = 0;

virtual void environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_accel, float p_fade, float p_depth_tolerance, bool p_smooth, bool p_roughness) = 0;
virtual void environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_fade_int, float p_fade_out, float p_depth_tolerance, bool p_roughness) = 0;
virtual void environment_set_ssao(RID p_env, bool p_enable, float p_radius, float p_intensity, float p_radius2, float p_intensity2, float p_bias, float p_light_affect, const Color &p_color, bool p_blur) = 0;

virtual void environment_set_tonemap(RID p_env, VS::EnvironmentToneMapper p_tone_mapper, float p_exposure, float p_white, bool p_auto_exposure, float p_min_luminance, float p_max_luminance, float p_auto_exp_speed, float p_auto_exp_scale) = 0;
Expand Down
2 changes: 1 addition & 1 deletion servers/visual/visual_server_raster.h
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ class VisualServerRaster : public VisualServer {
BIND2(environment_set_bg_energy, RID, float)
BIND2(environment_set_canvas_max_layer, RID, int)
BIND4(environment_set_ambient_light, RID, const Color &, float, float)
BIND8(environment_set_ssr, RID, bool, int, float, float, float, bool, bool)
BIND7(environment_set_ssr, RID, bool, int, float, float, float, bool)
BIND10(environment_set_ssao, RID, bool, float, float, float, float, float, float, const Color &, bool)

BIND6(environment_set_dof_blur_near, RID, bool, float, float, float, EnvironmentDOFBlurQuality)
Expand Down
2 changes: 1 addition & 1 deletion servers/visual/visual_server_wrap_mt.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ class VisualServerWrapMT : public VisualServer {
FUNC2(environment_set_bg_energy, RID, float)
FUNC2(environment_set_canvas_max_layer, RID, int)
FUNC4(environment_set_ambient_light, RID, const Color &, float, float)
FUNC8(environment_set_ssr, RID, bool, int, float, float, float, bool, bool)
FUNC7(environment_set_ssr, RID, bool, int, float, float, float, bool)
FUNC10(environment_set_ssao, RID, bool, float, float, float, float, float, float, const Color &, bool)

FUNC6(environment_set_dof_blur_near, RID, bool, float, float, float, EnvironmentDOFBlurQuality)
Expand Down
2 changes: 1 addition & 1 deletion servers/visual_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ class VisualServer : public Object {
virtual void environment_set_tonemap(RID p_env, EnvironmentToneMapper p_tone_mapper, float p_exposure, float p_white, bool p_auto_exposure, float p_min_luminance, float p_max_luminance, float p_auto_exp_speed, float p_auto_exp_grey) = 0;
virtual void environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, RID p_ramp) = 0;

virtual void environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_accel, float p_fade, float p_depth_tolerance, bool p_smooth, bool p_roughness) = 0;
virtual void environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_fade_in, float p_fade_out, float p_depth_tolerance, bool p_roughness) = 0;
virtual void environment_set_ssao(RID p_env, bool p_enable, float p_radius, float p_intensity, float p_radius2, float p_intensity2, float p_bias, float p_light_affect, const Color &p_color, bool p_blur) = 0;

virtual void environment_set_fog(RID p_env, bool p_enable, const Color &p_color, const Color &p_sun_color, float p_sun_amount) = 0;
Expand Down

0 comments on commit d564b5a

Please sign in to comment.