Environment Importance Sampling gives too much noise #69745

Open
opened 2019-09-11 12:40:20 +02:00 by Stefan Werner · 31 comments
Member

When using importance sampling for environment maps in Cycles, the default/recommended resolution gives too noisy results when texture interpolation other than "closest" is selected.

Blender version: 2.80

To reproduce:

  • Open attached file
  • Render in Cycles
  • Set the texture interpolation of the background map to "linear"
  • Render again

Actual result:
Second render has much more noise.

Expected result:
Little to no difference in noise.

closest.jpg
linear.jpg

Additional testing:

  • Set texture interpolation to "linear"
  • On the background tab, set Settings/Surface/Sampling to Manual
  • On the background tab, set Settings/Surface/Resolution to 2048
  • Render again

Result: Reduced noise again.

envmap.blend

(HDRI image by Greg Zaal, HDRI Haven, CC0 license)

When using importance sampling for environment maps in Cycles, the default/recommended resolution gives too noisy results when texture interpolation other than "closest" is selected. Blender version: 2.80 To reproduce: * Open attached file * Render in Cycles * Set the texture interpolation of the background map to "linear" * Render again Actual result: Second render has much more noise. Expected result: Little to no difference in noise. ![closest.jpg](https://archive.blender.org/developer/F7734825/closest.jpg) ![linear.jpg](https://archive.blender.org/developer/F7734828/linear.jpg) Additional testing: * Set texture interpolation to "linear" * On the background tab, set Settings/Surface/Sampling to Manual * On the background tab, set Settings/Surface/Resolution to 2048 * Render again Result: Reduced noise again. [envmap.blend](https://archive.blender.org/developer/F7734735/envmap.blend) (HDRI image by Greg Zaal, HDRI Haven, CC0 license)
Author
Member

Added subscriber: @Stefan_Werner

Added subscriber: @Stefan_Werner

#73832 was marked as duplicate of this issue

#73832 was marked as duplicate of this issue
Stefan Werner self-assigned this 2019-09-11 12:40:38 +02:00
Author
Member
No description provided.
Author
Member

Working theory:
When building the initial distribution, Cycles always sample the center of a pixel, effectively a closest or box filter. When using the distribution later during the render with a different filter, we get a mismatch between the function and its PDF. When sampled with linear texture filtering, single bright pixels become spikes that bleed into the neighboring pixels, where the distribution we sample from is piecewise linear over the pixel with no bleeding.

Note also how the noise is also reduced when using cubic interpolation.

Working theory: When building the initial distribution, Cycles always sample the center of a pixel, effectively a closest or box filter. When using the distribution later during the render with a different filter, we get a mismatch between the function and its PDF. When sampled with linear texture filtering, single bright pixels become spikes that bleed into the neighboring pixels, where the distribution we sample from is piecewise linear over the pixel with no bleeding. Note also how the noise is also reduced when using cubic interpolation.
Member

Added subscriber: @LukasStockner

Added subscriber: @LukasStockner
Member

In the past, I've considered tweaking the sampling algorithm to sample from a piecewise linear function instead of piecewise constant, but that would only help if the CDF map exactly aligns with the background image.

In practice, it might be simpler to just replace each pixel in the CDF by the maximum of it's 3x3 neighborhood. This will make the sampling a bit less accurate but should help with the fireflies due to undersampling and interpolation.

In the past, I've considered tweaking the sampling algorithm to sample from a piecewise linear function instead of piecewise constant, but that would only help if the CDF map exactly aligns with the background image. In practice, it might be simpler to just replace each pixel in the CDF by the maximum of it's 3x3 neighborhood. This will make the sampling a bit less accurate but should help with the fireflies due to undersampling and interpolation.

Added subscriber: @brecht

Added subscriber: @brecht

I think the sampling is already piecewise linear? But it's only along one axis at a time which may be the problem.

We compute a CDF per row, and then a CDF of all those rows. That means if you have a dark pixel with a bright pixel just below, within that row it will not get more importance. The row as a whole will get more samples, but not that specific pixel.

To help with that problem, we could use the average of the pixel and the adjacent pixel above/below when building the CDF per row. Which isn't really that different than what you guys are proposing, and maybe max or some other filter works better. Just a different way of approaching it conceptually.

I think the sampling is already piecewise linear? But it's only along one axis at a time which may be the problem. We compute a CDF per row, and then a CDF of all those rows. That means if you have a dark pixel with a bright pixel just below, within that row it will not get more importance. The row as a whole will get more samples, but not that specific pixel. To help with that problem, we could use the average of the pixel and the adjacent pixel above/below when building the CDF per row. Which isn't really that different than what you guys are proposing, and maybe max or some other filter works better. Just a different way of approaching it conceptually.
Member

The current code samples the CDF as a piecewise linear function, but the underlying PDF is piecewise constant since it's the derivative of the CDF.

I think the problem here is that interpolation essentially allows bright pixels to "leak" into parts if their neighbors, but the neighbors don't receive a high PDF.

Therefore the suggestion of the 3x3 filter before computing the marginal and conditional CDFs - if we only average between neighboring rows, we will still have this problem in the y axis.

A slightly more conservative approach might be to use a weighted 3x3 grid where the center pixel has higher weight - that way, massively brighter pixels will still increase the PDF of their neighbors to avoid strong fireflies, but the overall impact on quality might be reduced.

The current code samples the CDF as a piecewise linear function, but the underlying PDF is piecewise constant since it's the derivative of the CDF. I think the problem here is that interpolation essentially allows bright pixels to "leak" into parts if their neighbors, but the neighbors don't receive a high PDF. Therefore the suggestion of the 3x3 filter before computing the marginal and conditional CDFs - if we only average between neighboring rows, we will still have this problem in the y axis. A slightly more conservative approach might be to use a weighted 3x3 grid where the center pixel has higher weight - that way, massively brighter pixels will still increase the PDF of their neighbors to avoid strong fireflies, but the overall impact on quality might be reduced.
Author
Member

I just played with 2x2 supersampling when generating the distribution, thus avoiding sampling texels exactly in the middle. This allows for texture filtering to contribute more, as opposed to sampling texels in their exact center which makes the linear filter behave exactlylike the closest filter.

The result is a significant reduction in noise with linear interpolation, although still noisier as with closest interpolation.
supersampled linear.jpg

I just played with 2x2 supersampling when generating the distribution, thus avoiding sampling texels exactly in the middle. This allows for texture filtering to contribute more, as opposed to sampling texels in their exact center which makes the linear filter behave exactlylike the closest filter. The result is a significant reduction in noise with linear interpolation, although still noisier as with closest interpolation. ![supersampled linear.jpg](https://archive.blender.org/developer/F7735091/supersampled_linear.jpg)
Author
Member

PBRT filters the environment map when creating the distribution:

The second thing of note in this code is that the piecewise constant function values being stored here in img are found by slightly blurring the radiance function with the MIPMap::Lookup() method (rather than just copying the corresponding texel values).

http://www.pbr-book.org/3ed-2018/Light_Transport_I_Surface_Reflection/Sampling_Light_Sources.html#sec:mc-infinite-area-lights

PBRT filters the environment map when creating the distribution: > The second thing of note in this code is that the piecewise constant function values being stored here in img are found by slightly blurring the radiance function with the MIPMap::Lookup() method (rather than just copying the corresponding texel values). http://www.pbr-book.org/3ed-2018/Light_Transport_I_Surface_Reflection/Sampling_Light_Sources.html#sec:mc-infinite-area-lights

Added subscriber: @YAFU

Added subscriber: @YAFU

A comment perhaps related to this.

When Portals was introduced in Blender, many users clearly obtained very good results in tests regarding less noise using Portals. This is because Multiple Importance Sampling was disabled by default in Blender when creating a new scene (2.76b for example).

But then with new versions of Blender users in forums began to question the good result of Portals compared to not using Portals, and I am pretty sure that it started to happen when in Blender Multiple Importance Sampling it started to be enabled by default in scenes. In my tests with new versions of Blender, I only get clear better results using Portals when Multiple Importance Sampling is disabled (Sampling = None). If multiple importance sampling is used, the higher the Map Resolution value is, the smaller the difference in the Portals vs. No Portals comparison with respect to the amount of noise.

A comment perhaps related to this. When Portals was introduced in Blender, many users clearly obtained very good results in tests regarding less noise using Portals. This is because Multiple Importance Sampling was disabled by default in Blender when creating a new scene (2.76b for example). But then with new versions of Blender users in forums began to question the good result of Portals compared to not using Portals, and I am pretty sure that it started to happen when in Blender Multiple Importance Sampling it started to be enabled by default in scenes. In my tests with new versions of Blender, I only get clear better results using Portals when Multiple Importance Sampling is disabled (Sampling = None). If multiple importance sampling is used, the higher the Map Resolution value is, the smaller the difference in the Portals vs. No Portals comparison with respect to the amount of noise.

I'm fine with a some empirically determined prefiltering.

To me it seems possible to figure out the code to do piecewise linear sampling in a way that exactly matches the linear interpolation (at least in 1D and close enough in 2D). But we don't need to wait for that if we can do something simple already.

I'm fine with a some empirically determined prefiltering. To me it seems possible to figure out the code to do piecewise linear sampling in a way that exactly matches the linear interpolation (at least in 1D and close enough in 2D). But we don't need to wait for that if we can do something simple already.
Author
Member

This comment was removed by @Stefan_Werner

*This comment was removed by @Stefan_Werner*
Author
Member

I played a bit with simple filters for the distribution. Both 3x3 box and 3x3 Gaussian filters improve the case with linear texture filtering, but make things worse for closest texture filtering.

I played a bit with simple filters for the distribution. Both 3x3 box and 3x3 Gaussian filters improve the case with linear texture filtering, but make things worse for closest texture filtering.

You can auto detect closest filtering along with the resolution, and leave out the filtering then.

You can auto detect closest filtering along with the resolution, and leave out the filtering then.
Author
Member

2x2 supersampling inside kernel_background_evaluate() reduces noise more than filtering when generating the distribution and leaves the quality intact with other texture filters. It should also improve the situation for procedural high-frequency environment maps. My vote would be for supersampling.

2x2 supersampling inside kernel_background_evaluate() reduces noise more than filtering when generating the distribution and leaves the quality intact with other texture filters. It should also improve the situation for procedural high-frequency environment maps. My vote would be for supersampling.
Author
Member

I attached my test patches so others can test for themselves. They are not optimized, there are faster filter implementations that use less memory.

Best quality still comes with the closest texture interpolation, I assume because the distribution is a perfect match for the function. Linear texture interpolation is better with supersampling, although still not as good as when I increase the size of the distribution to 4x env map resolution.

filter_distribution.diff

supersampling.diff

I attached my test patches so others can test for themselves. They are not optimized, there are faster filter implementations that use less memory. Best quality still comes with the closest texture interpolation, I assume because the distribution is a perfect match for the function. Linear texture interpolation is better with supersampling, although still not as good as when I increase the size of the distribution to 4x env map resolution. [filter_distribution.diff](https://archive.blender.org/developer/F7738569/filter_distribution.diff) [supersampling.diff](https://archive.blender.org/developer/F7738570/supersampling.diff)
Stefan Werner was unassigned by Dalai Felinto 2019-12-23 13:51:22 +01:00

Added subscribers: @irfancelik, @mont29, @WilliamReynish

Added subscribers: @irfancelik, @mont29, @WilliamReynish

Added subscriber: @filibis

Added subscriber: @filibis

Added subscriber: @SteffenD

Added subscriber: @SteffenD

Added subscriber: @ReinhardK

Added subscriber: @ReinhardK

Added subscriber: @lsscpp

Added subscriber: @lsscpp

Hopefully here is the right place for this.
I noticed that Smart interpolation is not the same on GPU or CPU. On a simple scene derived from envmap.blend (at the top of this page, I just put Suzanne into a cube with a simple window to let the light in) CPU "Smart" interpolation looks just like "Cubic" while the GPU tiles look like "Linear" without noise. I hope jpeg codec doesn't kills too much the detail (edit: uploaded pngs ;), anyway, blue arrows point to the boundaries of CPU/GPU tiles, and also the magenta one with the difference that it's a harsh horizontal line (CPU/GPU boundary) in the middle of the shadow.
Notice that none of the other interpolations give this differences/artifacts.
smartfiltering1.png
Here a screenshot that shows which tiles where evaluated by CPU
smartfiltering2.png
And here's the edited blend
envmap.blend

Hopefully here is the right place for this. I noticed that Smart interpolation is not the same on GPU or CPU. On a simple scene derived from envmap.blend (at the top of this page, I just put Suzanne into a cube with a simple window to let the light in) CPU "Smart" interpolation looks just like "Cubic" while the GPU tiles look like "Linear" without noise. I hope jpeg codec doesn't kills too much the detail (edit: uploaded pngs ;), anyway, blue arrows point to the boundaries of CPU/GPU tiles, and also the magenta one with the difference that it's a harsh horizontal line (CPU/GPU boundary) in the middle of the shadow. Notice that none of the other interpolations give this differences/artifacts. ![smartfiltering1.png](https://archive.blender.org/developer/F8563902/smartfiltering1.png) Here a screenshot that shows which tiles where evaluated by CPU ![smartfiltering2.png](https://archive.blender.org/developer/F8563900/smartfiltering2.png) And here's the edited blend [envmap.blend](https://archive.blender.org/developer/F8563907/envmap.blend)

Added subscriber: @xdanic

Added subscriber: @xdanic
Member

Added subscriber: @Alaska

Added subscriber: @Alaska

Added subscriber: @Josephbburg

Added subscriber: @Josephbburg

I noticed that Smart interpolation is not the same on GPU or CPU.

Perhaps this is why? It says it's OSL only, and of course OSL isn't fully supported on the GPU.
image.png

> I noticed that Smart interpolation is not the same on GPU or CPU. Perhaps this is why? It says it's OSL only, and of course OSL isn't fully supported on the GPU. ![image.png](https://archive.blender.org/developer/F8632371/image.png)

Maybe that is something related, nice spot. But I would expect the opposite result, since smart interpolation looks better on GPU, and on CPU seems to work as cubic.
Now I wonder, if CPU is smart and GPU falls back to linear, why there are no fireflies? Can't we have CPU falling back to the same "linear mode” that gives no sampling issue?

Maybe that is something related, nice spot. But I would expect the opposite result, since smart interpolation looks better on GPU, and on CPU seems to work as cubic. Now I wonder, if CPU is smart and GPU falls back to linear, why there are no fireflies? Can't we have CPU falling back to the same "linear mode” that gives no sampling issue?

I have no pc at hand for a week, but the easy test would be: exclude CPU from rendering, and make two gpu-only renders, one linear and one smart. I'd expect two identical renders except for fireflies

I have no pc at hand for a week, but the easy test would be: exclude CPU from rendering, and make two gpu-only renders, one linear and one smart. I'd expect two identical renders except for fireflies
Philipp Oeser removed the
Interest
Render & Cycles
label 2023-02-09 13:59:32 +01:00
Sign in to join this conversation.
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
12 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#69745
No description provided.