EEVEE-Next: Shadow resolution scale and adaptive filtering #119436

Merged
Miguel Pozo merged 20 commits from pragma37/blender:pull-eevee-shadow-resolution-scale into main 2024-03-20 15:54:51 +01:00
Member

Allow the user to scale shadow-map resolution per-light.
Adapt the PCF scale based on shadow-map to pixel footprint ratio, since we can no longer assume that higher LODs don't need filtering.
This allows using much lower shadow resolutions, which can yield quite significant performance improvements, with relatively little perceptual quality loss (at the cost of softening shadow edges).
The per-light resolution scale is a literal scale, so for example 0.5 means half the resolution. The Scene Simplify Shadows setting has been updated to match this behavior.

No Simplification (~4.5 fps) 0.5 Simplification (~16 fps) 0.1 Simplification (~27 fps)
imagen imagen imagen
Allow the user to scale shadow-map resolution per-light. Adapt the PCF scale based on shadow-map to pixel footprint ratio, since we can no longer assume that higher LODs don't need filtering. This allows using much lower shadow resolutions, which can yield quite significant performance improvements, with relatively little perceptual quality loss (at the cost of softening shadow edges). The per-light resolution scale is a literal scale, so for example 0.5 means half the resolution. The Scene Simplify Shadows setting has been updated to match this behavior. | No Simplification (~4.5 fps) | 0.5 Simplification (~16 fps) | 0.1 Simplification (~27 fps) | | --- | --- | --- | | ![imagen](/attachments/c4be56ad-664e-489a-8766-f23397bb8672) | ![imagen](/attachments/e131dec4-bd29-4f78-811a-edf3d6b56c48) | ![imagen](/attachments/a7de0dca-0785-4753-88c0-1df76d1554f0) |
Miguel Pozo added the
Interest
EEVEE
Module
EEVEE & Viewport
labels 2024-03-13 19:26:50 +01:00
Miguel Pozo added 11 commits 2024-03-13 19:26:59 +01:00
Miguel Pozo added this to the EEVEE & Viewport project 2024-03-13 19:27:13 +01:00
Miguel Pozo requested review from Clément Foucault 2024-03-13 19:27:21 +01:00
Author
Member

@blender-bot build +gpu

@blender-bot build +gpu
Clément Foucault requested changes 2024-03-14 08:56:34 +01:00
Dismissed
@ -131,0 +133,4 @@
int shadow_directional_level(LightData light, vec3 lP)
{
int clipmap_lod = int(ceil(shadow_directional_level_fractional(light, lP)));
return clamp(clipmap_lod, light.clipmap_lod_min, light.clipmap_lod_max);

you are clamping twice. I don't think that is necessary.

you are clamping twice. I don't think that is necessary.
Author
Member

I did it to workaround possible floating point precision issues causing troubles with the later ceil.
But maybe I was just overly paranoid.

I did it to workaround possible floating point precision issues causing troubles with the later `ceil`. But maybe I was just overly paranoid.
fclem marked this conversation as resolved
@ -131,0 +144,4 @@
*/
float shadow_punctual_footprint_ratio(LightData light,
vec3 P,
float perspective_division,

Pass float dist_to_cam and bool is_perspective for clarity.

Pass `float dist_to_cam` and `bool is_perspective` for clarity.
pragma37 marked this conversation as resolved
@ -470,0 +478,4 @@
float dist_to_cam = distance(P, drw_view_position());
float perspective_division = 1.0;
if (drw_view_is_perspective()) {
perspective_division = distance(P, drw_view_position());

you are computing dist_to_cam again

you are computing `dist_to_cam` again
pragma37 marked this conversation as resolved
Miguel Pozo added 1 commit 2024-03-14 16:34:48 +01:00
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-windows-amd64 Build done. Details
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
014108d1b8
Review feedback
Author
Member

@blender-bot build +gpu

@blender-bot build +gpu
Clément Foucault requested changes 2024-03-14 19:34:11 +01:00
Dismissed
@ -819,2 +819,3 @@
float pcf_radius;
int _pad0;
/* Shadow Map resolution bias. */
float lod_bias;

Then remove _clipmap_lod_bias since it is redundant (and would remove some dirty define code).

Then remove `_clipmap_lod_bias` since it is redundant (and would remove some dirty define code).
pragma37 marked this conversation as resolved
@ -1311,6 +1311,8 @@ void ShadowModule::set_view(View &view, GPUTexture *depth_tx)
pixel_world_radius_ = screen_pixel_radius(view, int2(target_size));
tilemap_projection_ratio_ = tilemap_pixel_radius() / pixel_world_radius_;
data_.tilemap_projection_ratio = tilemap_projection_ratio_;

Do not duplicate data. Remove the private member.

Do not duplicate data. Remove the private member.
pragma37 marked this conversation as resolved
@ -464,3 +464,3 @@
rand = sampling_rng_2D_get(SAMPLING_SHADOW_V);
#endif
vec2 pcf_offset = interlieved_gradient_noise(UTIL_TEXEL, vec2(0.0), rand);
vec2 pcf_offset = interlieved_gradient_noise(UTIL_TEXEL, vec2(5, 7), rand);

This is yielding strong flickering in the noise pattern due to correlation between the 2 random numbers.
Use this instead:

diff --git a/source/blender/draw/engines/eevee_next/shaders/eevee_shadow_tracing_lib.glsl b/source/blender/draw/engines/eevee_next/shaders/eevee_shadow_tracing_lib.glsl
index 0f922b89184..b32c8c4a258 100644
--- a/source/blender/draw/engines/eevee_next/shaders/eevee_shadow_tracing_lib.glsl
+++ b/source/blender/draw/engines/eevee_next/shaders/eevee_shadow_tracing_lib.glsl
@@ -410,7 +410,7 @@ SHADOW_MAP_TRACE_FN(ShadowRayPunctual)
 
 /* Compute the world space offset of the shading position required for
  * stochastic percentage closer filtering of shadow-maps. */
-vec3 shadow_pcf_offset(LightData light, const bool is_directional, vec3 P, vec3 Ng)
+vec3 shadow_pcf_offset(LightData light, vec2 random, const bool is_directional, vec3 P, vec3 Ng)
 {
   if (light.pcf_radius <= 0.001) {
     /* Early return. */
@@ -458,13 +458,10 @@ vec3 shadow_pcf_offset(LightData light, const bool is_directional, vec3 P, vec3
   mat3 TBN = mat3(TP - P, BP - P, Ng);
 
   /* Compute the actual offset. */
-
-  vec2 rand = vec2(0.0);
 #ifdef EEVEE_SAMPLING_DATA
-  rand = sampling_rng_2D_get(SAMPLING_SHADOW_V);
+  random = fract(random + sampling_rng_2D_get(SAMPLING_SHADOW_X));
 #endif
-  vec2 pcf_offset = interlieved_gradient_noise(UTIL_TEXEL, vec2(5, 7), rand);
-  pcf_offset = pcf_offset * 2.0 - 1.0;
+  vec2 pcf_offset = fract(random) * 2.0 - 1.0;
   pcf_offset *= light.pcf_radius;
 
   /* Scale the offset based on shadow LOD. */
@@ -541,7 +538,7 @@ ShadowEvalResult shadow_eval(LightData light,
   float normal_offset = 0.02;
 #endif
 
-  P += shadow_pcf_offset(light, is_directional, P, Ng);
+  P += shadow_pcf_offset(light, random_shadow_3d.xy, is_directional, P, Ng);
 
   /* Avoid self intersection. */
   P = offset_ray(P, Ng);
This is yielding strong flickering in the noise pattern due to correlation between the 2 random numbers. Use this instead: ```diff diff --git a/source/blender/draw/engines/eevee_next/shaders/eevee_shadow_tracing_lib.glsl b/source/blender/draw/engines/eevee_next/shaders/eevee_shadow_tracing_lib.glsl index 0f922b89184..b32c8c4a258 100644 --- a/source/blender/draw/engines/eevee_next/shaders/eevee_shadow_tracing_lib.glsl +++ b/source/blender/draw/engines/eevee_next/shaders/eevee_shadow_tracing_lib.glsl @@ -410,7 +410,7 @@ SHADOW_MAP_TRACE_FN(ShadowRayPunctual) /* Compute the world space offset of the shading position required for * stochastic percentage closer filtering of shadow-maps. */ -vec3 shadow_pcf_offset(LightData light, const bool is_directional, vec3 P, vec3 Ng) +vec3 shadow_pcf_offset(LightData light, vec2 random, const bool is_directional, vec3 P, vec3 Ng) { if (light.pcf_radius <= 0.001) { /* Early return. */ @@ -458,13 +458,10 @@ vec3 shadow_pcf_offset(LightData light, const bool is_directional, vec3 P, vec3 mat3 TBN = mat3(TP - P, BP - P, Ng); /* Compute the actual offset. */ - - vec2 rand = vec2(0.0); #ifdef EEVEE_SAMPLING_DATA - rand = sampling_rng_2D_get(SAMPLING_SHADOW_V); + random = fract(random + sampling_rng_2D_get(SAMPLING_SHADOW_X)); #endif - vec2 pcf_offset = interlieved_gradient_noise(UTIL_TEXEL, vec2(5, 7), rand); - pcf_offset = pcf_offset * 2.0 - 1.0; + vec2 pcf_offset = fract(random) * 2.0 - 1.0; pcf_offset *= light.pcf_radius; /* Scale the offset based on shadow LOD. */ @@ -541,7 +538,7 @@ ShadowEvalResult shadow_eval(LightData light, float normal_offset = 0.02; #endif - P += shadow_pcf_offset(light, is_directional, P, Ng); + P += shadow_pcf_offset(light, random_shadow_3d.xy, is_directional, P, Ng); /* Avoid self intersection. */ P = offset_ray(P, Ng);
pragma37 marked this conversation as resolved
@ -470,0 +481,4 @@
light, P, is_perspective, dist_to_cam, uniform_buf.shadow.tilemap_projection_ratio);
float lod = -log2(footprint_ratio) + light.lod_bias;
lod = clamp(lod, 0.0, float(SHADOW_TILEMAP_LOD));
float pcf_scale = pow(2.0, lod);

Never use pow(2.0, lod). Poor precision and bad perf. Use exp2(lod) instead.

Same for pow(x, 2.0) use pow2(x).

Never use `pow(2.0, lod)`. Poor precision and bad perf. Use `exp2(lod)` instead. Same for `pow(x, 2.0)` use `pow2(x)`.
pragma37 marked this conversation as resolved
@ -307,0 +307,4 @@
prop = RNA_def_property(srna, "shadow_resolution_scale", PROP_FLOAT, PROP_FACTOR);
RNA_def_property_range(prop, 0.0f, 2.0f);
RNA_def_property_ui_range(prop, 0.0f, 2.0f, 0.25f, 2);
RNA_def_property_ui_text(prop, "Shadow Resolution Scale", "Bias the Shadow Map resolution");

This needs more attention. This can be polished later with the UI team.

However, it should already be stated that:

  • 0 means the lowest possible resolution.
  • 1 tries to match screen pixel density.
  • 2 is double the resolution.

I don't know if it should be mentioned in the tooltip that this is also affected by the simplify option.

This needs more attention. This can be polished later with the UI team. However, it should already be stated that: - `0` means the lowest possible resolution. - `1` tries to match screen pixel density. - `2` is double the resolution. I don't know if it should be mentioned in the tooltip that this is also affected by the simplify option.
Author
Member

I've updated the tooltip to :

Scale the Shadow Map target resolution, where 1.0 tries to match shadow map and screen pixel density. (The scale is applied on top of the scene Simplify Shadow Resolution)

I think that kind of covers all your suggestions without being overly wordy, but feel free to suggest an alternative.

I've updated the tooltip to : > Scale the Shadow Map target resolution, where 1.0 tries to match shadow map and screen pixel density. (The scale is applied on top of the scene Simplify Shadow Resolution) I think that kind of covers all your suggestions without being overly wordy, but feel free to suggest an alternative.
pragma37 marked this conversation as resolved
Miguel Pozo added 3 commits 2024-03-14 20:21:50 +01:00
Author
Member

Not too sold on using blue noise for the filter offset:

Blue Noise IGN
imagen imagen

It may improve the per-sample flickering, but the final image looks worse IMO.

Not too sold on using blue noise for the filter offset: | Blue Noise | IGN | | --- | --- | | ![imagen](/attachments/841d07b5-3d99-41ef-9f0c-4e32125762fb) | ![imagen](/attachments/c69d2cb3-c707-42f2-82e5-2d1614bc56fb) | It may improve the per-sample flickering, but the final image looks worse IMO.

If the noise persists at higher sample count (in F12 render) then it's some correlation artifacts. You can try using another layer of the blue noise (the alpha), you can try scrambling the resulting numbers quite more using noise = fract(noise * 6.1803398875) (but that doesn't do miracle).

But to be honest, in my tests, the code I gave you in the previous comment did yield a noise free result with enough sample. I know it combines 2 RNG offsets and that looks weird but it is worth a try.

If you are only comparing a low amount of samples (<200) then yes, the result can look noisier than IGN since the random numbers are using leaped Halton sequences that has very slow convergence.

If the noise persists at higher sample count (in F12 render) then it's some correlation artifacts. You can try using another layer of the blue noise (the alpha), you can try scrambling the resulting numbers quite more using `noise = fract(noise * 6.1803398875)` (but that doesn't do miracle). But to be honest, in my tests, the code I gave you in the previous comment did yield a noise free result with enough sample. I know it combines 2 RNG offsets and that looks weird but it is worth a try. If you are only comparing a low amount of samples (<200) then yes, the result can look noisier than IGN since the random numbers are using leaped Halton sequences that has very slow convergence.
Author
Member

Yes, those images were taken at 64 samples IIRC. The blue noise eventually converges, but it needs more samples to look ok.

Take into account that this is essentially an optimization feature.
If lowering render times comes at the cost of heavily increasing the number of samples needed, then there's no point in using it.

Yes, those images were taken at 64 samples IIRC. The blue noise eventually converges, but it needs more samples to look ok. Take into account that this is essentially an optimization feature. If lowering render times comes at the cost of heavily increasing the number of samples needed, then there's no point in using it.
Miguel Pozo added 3 commits 2024-03-15 16:29:49 +01:00
Clément Foucault approved these changes 2024-03-20 09:21:58 +01:00
Clément Foucault left a comment
Member

I think this is good to go. (after fixing the conflicts obviously)

I think this is good to go. (after fixing the conflicts obviously)
Miguel Pozo added 2 commits 2024-03-20 15:52:17 +01:00
Miguel Pozo merged commit 0c8b96d1e0 into main 2024-03-20 15:54:51 +01:00
Miguel Pozo deleted branch pull-eevee-shadow-resolution-scale 2024-03-20 15:54:53 +01:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
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
Asset Browser Project
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 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#119436
No description provided.