Branched path unification
Open, NormalPublic

Description

I'd like to unify path and branched path tracing, first to deduplicate code and improve performance on GPUs, and then to try to combine the best sampling strategies from both. The big difference between the two is that branched path tracing samples all lights and closures, instead of just 1.

The branched path code is not particularly efficient on either CUDA or OpenCL, and I think the best way to solve that is to use the same mechanism as we use for SSS in the path tracing megakernel, storing the state for each branch and restarting the path from that. This should also significantly simplify and deduplicate code.

The immediate problem is stack memory usage. Users can specify and arbitrarily high number of diffuse/glossy/... samples, and each sample would require extra state to be stored. We can address that in two ways:

  • Reduce size of the state that must be stored. I believe it can be reduced to about 200 bytes per branch, and possibly 150-100 bytes with more tricky optimizations.
  • With more branches there are quickly diminishing returns, if you have e.g. 16 branches that means you can cut the cost of the camera ray trace + shader evaluation to 1/16th, and at that point the cost of direct and indirect lighting are likely to be much bigger, and the branched path GPU code is already significantly slower. Extra AA samples to reduce aliasing, DoF and motion blur noise seems more useful at that point. So we could cap the number of branches, and if there are more closures sample only a subset of them.

Assuming we use 16 x 200 bytes = 3.2kb, that's the same memory usage as the current SSS stack, and not that big compared to the total 21k of the CUDA path tracing kernel. In fact I'm guessing the optimal branch factor for most scenes with GI is much lower than 16, but this needs to be tested with equal time renders of production scenes, which I plan to do in this task.

Details

Type
Design

Here's a few initial equal time renders from benchmark scenes, rendered on the CPU. The branched path settings have been tweaked to try to reduce noise, but there may exist better settings. The difficulty of tuning the settings is part of the reason we want to unify this though.

PathBranched PathPath + Defensive Sampling
bmw27
fishy_cat
pabellon

For bmw27, branched path has significantly less noise for the windows, but more noise for the headlights. Fishy cat seems to have some bug where we get different result, but ignoring that, it's difficult to determine a clear winner. The extra samples from path tracing help clean up the specular on the nose for example, but the grass DoF noise isn't reduced. For pabellon the clear winner seems to be path.

The noise on the windows in the bmw27 scene happens because the reflection closure has a very small weight and so very low probability of being sampled, but the area light it reflects is still quite bright due to low roughness. To mitigate this problem we can use defensive sampling (last column), giving the reflection closure a minimum weight. In branched path tracing both the reflection and refraction are always sampled so this problem does not happen (if samples are set to 1, then it's like assigning probability 0.5 to both closures for camera rays).

We can't do defensive sampling through all indirect bounces, since the closure weights compound and the sample weights start deviating too much from the actual contribution. Still there are cases where it makes sense to do for non-camera rays, like looking through a perfect mirror. So the test above is using a heuristic to stop defensive sampling at some point, but this needs further tweaking.

Note this will increase noise in some scenes, it is a matter of finding a balance. I expect this will be helpful also for the principled BSDF with diffuse + specular + coat layers.

Here's a comparison using a shot from Agent, where branched path is the clear winner:

PathBranched Path

This scene has about 20 lights, and it seems to be branched path all light sampling that gives the main improvement. Since path tracing picks one light at random and every shading point is only significantly influenced by a subset of all lights, a lot of samples are wasted. So adding support for all light sampling to path tracing could help (and it's rather simple to implement). Or perhaps better a user specified N light samples. A light tree for importance sampling of many lights would also help (example).

Branching for SSS is likely helpful as well as those areas are quite noisy. The problem there that we can't share direct lighting computations between BSDFs and BSSRDFs (much), so those pixels really need more samples and branching does that. Adaptive sampling could do this as well.