diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2021-02-13 20:43:09 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2021-02-13 20:43:09 +0300 |
commit | 7f7e6830991bf66e285fb54cdbae6c6618ed7afa (patch) | |
tree | accbf8b2f2ab68452ee8e2dfa58bf2477abe6294 /source/blender/gpu/shaders/material/gpu_shader_material_subsurface_scattering.glsl | |
parent | 40aadd894016f6ad6a1cde3d60b63ba47a72ec6c (diff) |
EEVEE: Refactor closure_lit_lib.glsl
This refactor was needed for some reasons:
- closure_lit_lib.glsl was unreadable and could not be easily extended to use new features.
- It was generating ~5K LOC for any shader. Slowing down compilation.
- Some calculations were incorrect and BSDF/Closure code had lots of workaround/hacks.
What this refactor does:
- Add some macros to define the light object loops / eval.
- Clear separation between each closures which now have separate files. Each closure implements the eval functions.
- Make principled BSDF a bit more correct in some cases (specular coloring, mix between glass and opaque).
- The BSDF term are applied outside of the eval function and on the whole lighting (was separated for lights before).
- Make light iteration last to avoid carrying more data than needed.
- Makes sure that all inputs are within correct ranges before evaluating the closures (use `safe_normalize` on normals).
- Making each BSDF isolated means that we might carry duplicated data (normals for instance) but this should be optimized by compilers.
- Makes Translucent BSDF its own closure type to avoid having to disable raytraced shadows using hacks.
- Separate transmission roughness is now working on Principled BSDF.
- Makes principled shader variations using constants. Removing a lot of duplicated code. This needed `const` keyword detection in `gpu_material_library.c`.
- SSR/SSS masking and data loading is a bit more consistent and defined outside of closure eval. The loading functions will act as accumulator if the lighting is not to be separated.
- SSR pass now do a full deferred lighting evaluation, including lights, in order to avoid interference with the closure eval code. However, it seems that the cost of having a global SSR toggle uniform is making the surface shader more expensive (which is already the case, by the way).
- Principle fully black specular tint now returns black instead of white.
- This fixed some artifact issue on my AMD computer on normal surfaces (which might have been some uninitialized variables).
- This touched the Ambient Occlusion because it needs to be evaluated for each closure. But to avoid the cost of this, we use another approach to just pass the result of the occlusion on interpolated normals and modify it using the bent normal for each Closure. This tends to reduce shadowing. I'm still looking into improving this but this is out of the scope of this patch.
- Performance might be a bit worse with this patch since it is more oriented towards code modularity. But not by a lot.
Render tests needs to be updated after this.
Reviewed By: jbakker
Differential Revision: https://developer.blender.org/D10390
# Conflicts:
# source/blender/draw/engines/eevee/eevee_shaders.c
# source/blender/draw/engines/eevee/shaders/common_utiltex_lib.glsl
# source/blender/draw/intern/shaders/common_math_lib.glsl
Diffstat (limited to 'source/blender/gpu/shaders/material/gpu_shader_material_subsurface_scattering.glsl')
-rw-r--r-- | source/blender/gpu/shaders/material/gpu_shader_material_subsurface_scattering.glsl | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_subsurface_scattering.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_subsurface_scattering.glsl index c13b55513da..5a68f802659 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_subsurface_scattering.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_subsurface_scattering.glsl @@ -1,4 +1,7 @@ #ifndef VOLUMETRICS + +CLOSURE_EVAL_FUNCTION_DECLARE_1(node_subsurface_scattering, Diffuse) + void node_subsurface_scattering(vec4 color, float scale, vec3 radius, @@ -8,20 +11,29 @@ void node_subsurface_scattering(vec4 color, float sss_id, out Closure result) { - N = normalize(N); - vec3 out_diff; - vec3 vN = mat3(ViewMatrix) * N; + CLOSURE_VARS_DECLARE_1(Diffuse); + + in_Diffuse_0.N = N; /* Normalized during eval. */ + in_Diffuse_0.albedo = color.rgb; + + CLOSURE_EVAL_FUNCTION_1(node_subsurface_scattering, Diffuse); + result = CLOSURE_DEFAULT; - closure_load_ssr_data(vec3(0.0), 0.0, N, viewCameraVec, -1, result); - eevee_closure_subsurface(N, color.rgb, 1.0, scale, true, out_diff); + /* Not perfect for texture_blur values between 0.0 and 1.0. + * Interpolate between separated color and color applied on irradiance. */ + float one_minus_texture_blur = 1.0 - texture_blur; + vec3 sss_albedo = color.rgb * texture_blur + one_minus_texture_blur; + vec3 radiance_tint = color.rgb * one_minus_texture_blur + texture_blur; + /* Consider output radiance as irradiance. */ + out_Diffuse_0.radiance *= radiance_tint; + + closure_load_sss_data(scale, out_Diffuse_0.radiance, sss_albedo, int(sss_id), result); - /* Not perfect for texture_blur not exactly equal to 0.0 or 1.0. */ - vec3 sss_albedo = mix(color.rgb, vec3(1.0), texture_blur); - out_diff *= mix(vec3(1.0), color.rgb, texture_blur); - result.radiance = render_pass_sss_mask(sss_albedo); - closure_load_sss_data(scale, out_diff, sss_albedo, int(sss_id), result); + /* TODO(fclem) Try to not use this. */ + closure_load_ssr_data(vec3(0.0), 0.0, in_Diffuse_0.N, -1.0, result); } + #else /* Stub subsurface scattering because it is not compatible with volumetrics. */ # define node_subsurface_scattering(a, b, c, d, e, f, g, h) (h = CLOSURE_DEFAULT) |