EEVEE-Next: Add Shadows PCF #118220

Merged
Miguel Pozo merged 24 commits from pragma37/blender:pull-eevee-pcf-shadows into main 2024-02-29 15:47:26 +01:00
Member

Add percentage closer filtering to shadowmap sampling.
Adds a setting to control the radius to the Light UI.

Disabled Enabled (Radius = 3)
imagen imagen

Notes:

  • This adds PCF to eevee_shadow_tracing_lib, but not to eevee_shadow_lib, which is used by volumes (not required) and thickness (required, but might be better to use tracing there too?).
  • PCF is computed based on the LOD0 size (adapting it to the tile LOD causes more harm than good). This assumes that higher LODs are only used when the shadowmap resolution is actually good enough to match the render resolution, which seems to be a correct assumption at the moment, but it's worth keeping in mind.
    imagen
Add percentage closer filtering to shadowmap sampling. Adds a setting to control the radius to the Light UI. |Disabled|Enabled (Radius = 3)| |---|---| |![imagen](/attachments/0ff005b6-7d74-4a23-a0c2-79d197fcac47)|![imagen](/attachments/f5aed85e-8207-421b-abab-308a0ce1e11c)| Notes: * This adds PCF to `eevee_shadow_tracing_lib`, but not to `eevee_shadow_lib`, which is used by volumes (not required) and thickness (required, but might be better to use tracing there too?). * PCF is computed based on the LOD0 size (adapting it to the tile LOD causes more harm than good). This assumes that higher LODs are only used when the shadowmap resolution is actually good enough to match the render resolution, which seems to be a correct assumption at the moment, but it's worth keeping in mind. ![imagen](/attachments/ab32bac1-4026-4b12-ac86-043ca1d8a4dc)
Miguel Pozo added the
Interest
EEVEE
Module
EEVEE & Viewport
labels 2024-02-13 20:52:17 +01:00
Miguel Pozo added 2 commits 2024-02-13 20:52:29 +01:00
Miguel Pozo requested review from Clément Foucault 2024-02-13 20:52:46 +01:00
Author
Member

I've made a test to add PCF for sharp (0 radius) shadows too:
https://projects.blender.org/pragma37/blender/src/branch/test-eevee-pcf
(Note that this is a separate branch, it's not included in this PR)

Disabled Enabled (Radius = 3)
imagen imagen

It currently has issues at page edges, but that can be fixed. The downside is that this needs to actually evaluate the PCF inline (sampling the shadowmap texture multiple times instead of distributing it across render samples), which is expensive.
However, to implement this I've modified the lighting evaluation to use shadow_sample instead of shadow_eval (which uses shadow tracing) for lights with 0 radius, so the final performance ends up being pretty much the same.

On a simple scene with 4 point lights, the EvalLights pass timings look like this:
shadow_sample (PCF off): ~1.38 ms
shadow_sample (PCF on): ~3.33 ms
shadow_eval: ~3.19ms

I'd say the quality improvement is worth adding it at least as an option.

I've made a test to add PCF for sharp (0 radius) shadows too: https://projects.blender.org/pragma37/blender/src/branch/test-eevee-pcf (Note that this is a separate branch, it's not included in this PR) | Disabled | Enabled (Radius = 3) | | --- | --- | |![imagen](/attachments/84f68afd-de0f-4cb7-ae87-9e476b2be743)|![imagen](/attachments/84382ade-265d-4de3-be01-11f243cd6341)| It currently has issues at page edges, but that can be fixed. The downside is that this needs to actually evaluate the PCF inline (sampling the shadowmap texture multiple times instead of distributing it across render samples), which is expensive. However, to implement this I've modified the lighting evaluation to use `shadow_sample` instead of `shadow_eval` (which uses shadow tracing) for lights with 0 radius, so the final performance ends up being pretty much the same. On a simple scene with 4 point lights, the `EvalLights` pass timings look like this: `shadow_sample` (PCF off): ~1.38 ms `shadow_sample` (PCF on): ~3.33 ms `shadow_eval`: ~3.19ms I'd say the quality improvement is worth adding it at least as an option.
Miguel Pozo added 7 commits 2024-02-19 21:32:36 +01:00
Miguel Pozo added 1 commit 2024-02-20 19:37:03 +01:00
Miguel Pozo added 1 commit 2024-02-20 19:49:04 +01:00
9a0777f2a0 Compute the PCF offset always as if LOD0
Assumes higher lods are dense enough for their pixel size.
Otherwise changes in tile lod causes visible popping.
Miguel Pozo added 3 commits 2024-02-20 23:53:52 +01:00
Miguel Pozo changed title from WIP: EEVEE-Next: Add Shadows PCF to EEVEE-Next: Add Shadows PCF 2024-02-20 23:57:50 +01:00
Miguel Pozo added 1 commit 2024-02-21 16:06:40 +01:00
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-windows-amd64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
c71166b7da
Add early return
Author
Member

@blender-bot build +gpu

@blender-bot build +gpu
Clément Foucault requested changes 2024-02-24 13:54:11 +01:00
Clément Foucault left a comment
Member

Implementation looks good (except this matrix inversion that can be simplified).

I'm unsure about the parameter being on the scene rather than per light. But that could be changed later I think.

Implementation looks good (except this matrix inversion that can be simplified). I'm unsure about the parameter being on the scene rather than per light. But that could be changed later I think.
@ -724,6 +724,7 @@ class RENDER_PT_eevee_next_shadows(RenderButtonsPanel, Panel):
col = layout.column()
col.prop(props, "shadow_normal_bias", text="Normal Bias")
col.prop(props, "shadow_pcf_radius", text="PCF Radius")

Filtering Radius is a better term. PCF is a technical term.

`Filtering Radius` is a better term. PCF is a technical term.
pragma37 marked this conversation as resolved
@ -1023,3 +1023,3 @@
/* Bias the shading point by using the normal to avoid self intersection. */
float normal_bias;
int _pad2;
float pcf_radius;

Add description.

Add description.
pragma37 marked this conversation as resolved
@ -99,0 +125,4 @@
vec3 shadow_punctual_reconstruct_position(ShadowSampleParams params, LightData light, vec3 uvw)
{
vec3 clip_P = uvw * 2.0 - 1.0;
mat4 winmat = shadow_punctual_projection_perspective(light);

Given this function is called twice, I would put the winmat or rather wininv (or both) in the ShadowSampleParams to avoid setting it up twice. The compiler should be smart enough to eliminate it when not needed.

Given this function is called twice, I would put the `winmat` or rather `wininv` (or both) in the `ShadowSampleParams` to avoid setting it up twice. The compiler should be smart enough to eliminate it when not needed.
pragma37 marked this conversation as resolved
@ -99,0 +126,4 @@
{
vec3 clip_P = uvw * 2.0 - 1.0;
mat4 winmat = shadow_punctual_projection_perspective(light);
vec3 lP = project_point(inverse(winmat), clip_P);

This inverse is very nasty. Please create a shadow_projection_perspective_inverse and populate it manually. Might as well do the TODO and simplify it for the symmetrical case.

This is the simplified function:

mat4x4 shadow_projection_perspective(float side, float near_clip, float far_clip)
{
  float z_delta = far_clip - near_clip;

  mat4x4 mat = mat4x4(1.0);
  if (side != 0.0 && z_delta != 0.0) {
    mat[0][0] = near_clip / side;
    mat[1][1] = near_clip / side;
    mat[2][0] = 0.0;
    mat[2][1] = 0.0;
    mat[2][2] = -(far_clip + near_clip) / z_delta;
    mat[2][3] = -1.0;
    mat[3][2] = (-2.0 * near_clip * far_clip) / z_delta;
    mat[3][3] = 0.0;
  }
  return mat;
}

This link is the inversion for the symmetric matrix.

This inverse is very nasty. Please create a `shadow_projection_perspective_inverse` and populate it manually. Might as well do the TODO and simplify it for the symmetrical case. This is the simplified function: ``` mat4x4 shadow_projection_perspective(float side, float near_clip, float far_clip) { float z_delta = far_clip - near_clip; mat4x4 mat = mat4x4(1.0); if (side != 0.0 && z_delta != 0.0) { mat[0][0] = near_clip / side; mat[1][1] = near_clip / side; mat[2][0] = 0.0; mat[2][1] = 0.0; mat[2][2] = -(far_clip + near_clip) / z_delta; mat[2][3] = -1.0; mat[3][2] = (-2.0 * near_clip * far_clip) / z_delta; mat[3][3] = 0.0; } return mat; } ``` [This link]( https://www.wolframalpha.com/input?i2d=true&i=+invert%7B%7BDivide%5Bc%2Ca%5D%2C0%2C0%2C0%7D%2C%7B0%2CDivide%5Bc%2Cb%5D%2C0%2C0%7D%2C%7B0%2C0%2C-Divide%5B%5C%2840%29c%2Bd%5C%2841%29%2C%5C%2840%29c-d%5C%2841%29%5D%2C-Divide%5B2%5C%2840%29c*d%5C%2841%29%2C%5C%2840%29c-d%5C%2841%29%5D%7D%2C%7B0%2C0%2C0%2C1%7D%7D) is the inversion for the symmetric matrix.
pragma37 marked this conversation as resolved
@ -406,6 +408,64 @@ SHADOW_MAP_TRACE_FN(ShadowRayPunctual)
/** \name Shadow Evaluation
* \{ */
vec3 shadow_pcf_offset(LightData light, const bool is_directional, vec3 P, vec3 Ng)

Add overall description of what this function does.

Add overall description of what this function does.
pragma37 marked this conversation as resolved
@ -409,0 +427,4 @@
return vec3(0.0);
}
/* Compute shadomap TBN matrix. */

shadow-map

shadow-map
pragma37 marked this conversation as resolved
@ -409,0 +451,4 @@
BP = line_plane_intersect(light._position, normalize(BP - light._position), P, Ng);
}
mat3 TBN = mat3(TP - P, BP - P, Ng);

You could do it with a mat2x3.

You could do it with a `mat2x3`.
pragma37 marked this conversation as resolved
@ -409,0 +460,4 @@
rand = sampling_rng_2D_get(SAMPLING_SHADOW_V);
#endif
vec2 pcf_offset = interlieved_gradient_noise(UTIL_TEXEL, vec2(0.0), rand);
pcf_offset = pcf_offset * vec2(2.0) - vec2(1.0);

Nitpick: pcf_offset * 2.0 - 1.0;

Nitpick: `pcf_offset * 2.0 - 1.0;`
pragma37 marked this conversation as resolved
@ -1917,8 +1917,8 @@ typedef struct SceneEEVEE {
int shadow_ray_count;
int shadow_step_count;
float shadow_normal_bias;
float shadow_pcf_radius;

Rename to shadow_filter_radius. No need to mention PCF anywhere but on EEVEE's code (implementation detail).

Rename to `shadow_filter_radius`. No need to mention PCF anywhere but on EEVEE's code (implementation detail).
pragma37 marked this conversation as resolved
Miguel Pozo added 4 commits 2024-02-26 22:22:06 +01:00
Clément Foucault requested changes 2024-02-27 11:04:47 +01:00
@ -54,3 +73,2 @@
/* TODO(fclem): Remove. Only here to avoid include order hell with common_math_lib. */
mat4x4 shadow_projection_perspective(
float left, float right, float bottom, float top, float near_clip, float far_clip)
mat4x4 shadow_projection_perspective(float side, float near_clip, float far_clip)

You can remove the TODO on top as this is now a valid optimization

You can remove the `TODO` on top as this is now a valid optimization
pragma37 marked this conversation as resolved
@ -76,0 +95,4 @@
float d = 2.0 * near_clip * far_clip;
mat4x4 mat = mat4x4(1.0);
if (side != 0.0 && z_delta != 0.0) {

I'm wondering if we could remove this check as we ensure both are valid on CPU side.

I'm wondering if we could remove this check as we ensure both are valid on CPU side.
pragma37 marked this conversation as resolved
Miguel Pozo added 3 commits 2024-02-27 16:40:31 +01:00
Miguel Pozo requested review from Clément Foucault 2024-02-27 18:56:44 +01:00
Clément Foucault approved these changes 2024-02-28 15:38:26 +01:00
Clément Foucault left a comment
Member

The only issue I found is that it crease some overshadowing in some areas (see image). But that's expected as the shadow depth slop bias isn't set accordingly. This can be fixed later on. image

The only issue I found is that it crease some overshadowing in some areas (see image). But that's expected as the shadow depth slop bias isn't set accordingly. This can be fixed later on. ![image](/attachments/2a60e2ce-4b03-4502-86ad-d4b08ae09e39)
200 KiB
@ -2968,1 +2968,4 @@
if (!MAIN_VERSION_FILE_ATLEAST(bmain, 402, 6)) {
LISTBASE_FOREACH (Light *, light, &bmain->lights) {
light->shadow_filter_radius = 3.0f;

I would default to value of 1.0 for old scenes. This would match the old behavior.

I would default to value of 1.0 for old scenes. This would match the old behavior.
Miguel Pozo added 2 commits 2024-02-29 15:42:34 +01:00
Miguel Pozo merged commit 4083f8004d into main 2024-02-29 15:47:26 +01:00
Miguel Pozo deleted branch pull-eevee-pcf-shadows 2024-02-29 15:47:28 +01:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#118220
No description provided.