Leveraging Slang's automatic differentiation for computing PDFs
2026-09-02
The probability density functions (PDFs) used in Monte Carlo integration (such
as path tracing), can be derived using Jacobian determinants.
For example, if we have a function
Where
One notable snag here is that we do need the Jacobian matrix to be square so that
we can compute the determinant. This can be a bit confusing if your function is
Now,
Since the Jacobian matrix
Here's an example of doing that with a function float3 octahedralMapping(float2 uv)
that maps a
// If the PDF of 'uv' is 1, the PDF of the octahedral sampling is
// PDF = 1.0f / octahedralJacobianAbsDet(uv)
float octahedralJacobianAbsDet(float2 uv)
{
DifferentialPair<float2> du_v = diffPair(uv, float2(1.0, 0.0));
DifferentialPair<float2> u_dv = diffPair(uv, float2(0.0, 1.0));
DifferentialPair<float3> df_du = fwd_diff(octahedralMapping)(du_v);
DifferentialPair<float3> df_dv = fwd_diff(octahedralMapping)(u_dv);
float3x3 jacobian = float3x3(
// The first vector here represents the jacobians from the "scale" of the
// vector: octahedralMapping(u, v) * scale. The derivative of that w.r.t.
// scale is just octahedralMapping(u, v), so we can use the primal value.
df_du.p,
df_du.d,
df_dv.d
);
return abs(determinant(jacobian));
}
Cool, right?