Game Development Reference
// On the second pass, use the already-available alpha values.
nearInput = texelFetch(nearSourceBuffer , clamp(B, int2(0),
textureSize(nearSourceBuffer , 0) - int2(1)), 0);
weight = float (abs(delta) < nearBlurRadiusPixels);
nearResult += nearInput * weight;
nearWeightSum += weight;
We empirically tuned this coverage falloff curve to provide good coverage when
the near field is extremely blurry and to fade smoothly into the focus field. For
example, in Figure 1.5, the out-of-focus fence in the near field must have sucient
coverage to smear white pixels over a large region, while we still want the transi-
tion of the ground plane into the focus field to look like gradual focusing and not
simply a lerp between separate blurry and sharp images. This is an extremely
hard case for a post-processing DoF algorithm that previous real-time methods
do not handle well.
The near-field buffer is written with premultiplied alpha values for the color
channel, and the color and alpha are both blurred during the subsequent vertical
The compositing pass (shown below) reads the original input buffer along with the
low-resolution blurry near- and far-field buffers. It interpolates pixels between the
original input and the blurred, inpainted far-field buffer based on the CoC at each
Figure 1.5. Input image with a chain-link fence very close to the camera (left), and
near field under extreme blur with inpainted details visible through the “solid” parts of
the fence and a smooth ground-plane transition between depth regions (right).