Page MenuHome

Environment Importance Sampling gives too much noise
Confirmed, NormalPublicKNOWN ISSUE


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)

Event Timeline

Stefan Werner (swerner) lowered the priority of this task from 90 to 50.
Stefan Werner (swerner) created this task.
Stefan Werner (swerner) updated the task description. (Show Details)
Stefan Werner (swerner) added a subscriber: Cycles.

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).

YAFU (YAFU) added a subscriber: YAFU (YAFU).EditedSep 11 2019, 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.

Brecht Van Lommel (brecht) changed the subtype of this task from "Report" to "Known Issue".Jan 27 2020, 12:02 PM

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.

Here a screenshot that shows which tiles where evaluated by CPU

And here's the edited blend

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.

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