Page MenuHome

Environment Importance Sampling gives too much noise
Open, Confirmed, MediumPublic

Description

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.


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.

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

Details

Type
Bug

Event Timeline

Stefan Werner (swerner) updated the task description. (Show Details)
Stefan Werner (swerner) added a subscriber: Cycles.
Stefan Werner (swerner) claimed this task.
Stefan Werner (swerner) triaged this task as Confirmed, Medium priority.

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.

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.

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.

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.

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.

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

YAFU (YAFU) added a subscriber: YAFU (YAFU).EditedWed, Sep 11, 3:16 PM

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.

This comment was removed by Stefan Werner (swerner).

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.

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.

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.