Tobias Alexander Franke

Notes on importance sampling

Some tutorials on importance sampling specular terms that are out in the wild have what I found to be an information gap: the step from the PDF to the actual sampling function is missing. Hopefully this step-by-step guide can help out one or two other confused readers. I checked all integrals with Maxima and Wolfram Alpha. If you just have a free account at Wolfram Alpha, you might run into issues with exceeding the computation time, which is why I will also write down the commands to do everything in Maxima. If your result doesn’t look like the one noted here, try simplifying it first (in the Maxima menu Simplify -> Simplifiy Expression) or put it into Wolfram Alpha and check the Alternate form section!


The PDF for a normalized Phong BRDF with the specular power notation is:

\begin{equation} p(\theta, \phi) = \frac{\left(n+1\right)}{2 \pi} \cos^n \theta \sin \theta \end{equation}

We generate two functions to sample theta and phi independently. To do this, we first integrate p(\theta, \phi) along the domain of \phi :

integrate((n+1)/(2 * %pi) * cos(t)^n * sin(t), p, 0, 2 * %pi)

\begin{equation} p(\theta) = \int^{2 \pi}_0 p(\theta, \phi) d\phi = \left(n+1\right) \cos^n \theta \sin \theta \end{equation}

This is the marginal density function of \theta . Note that the extra \sin \theta at the end is there because we are dealing with differential solid angles in spherical coordinates. We can retreive the PDF for \phi with the conditional probability p(\phi | \theta) , the conditional density function:

\begin{equation} p(\phi \| \theta) = \frac{p(\theta, \phi)}{p(\theta)} = \frac{1}{2 \pi} \end{equation}

For isotropic NDFs this is always the case, no matter which PDF you will integrate over the domain of \phi , so all you need to calculate for the next PDF is the function for \theta .

Now that we have two functions for each variable, we can integrate both to generate a CDF each. The case of \phi is trivial:

integrate(1/(2 * %pi), p, 0, s)

\begin{equation} P(s_\phi) = \int_0^{s_\phi} p(\phi)d\phi = \int_0^{s_\phi} \frac{1}{2 \pi} d\phi = \frac{s_\phi}{2 \pi} \end{equation}

If we set P(s_\phi) to a random variable \xi_\phi and solve for s_\phi , we get:

\begin{eqnarray} P(s_\phi) & = & \frac{s_\phi}{2 \pi} = \xi_\phi \\ s_\phi & = & 2 \pi \xi_\phi \\ \end{eqnarray}

Again, sampling around the azimuthal angle of an isotropic NDF is always the same because the material is rotationally invariant along this angle, so you don’t need to derive this formula again.

We now repeat the process and create a CDF for p(\theta) :

integrate((n+1) * cos(t)^n * sin(t), t, 0, s)

\begin{equation} P(s_\theta) = \int_0^{s_\theta} p(\theta) d\theta = 1 - cos^{n+1} s_\theta \end{equation}

Just like in the case of \phi , we set P(s_\theta) to a random variable again and solve for s :

solve(1 - cos(s)^(n+1) = x, s)

\begin{eqnarray} P(s_\theta) & = & 1 - cos^{n+1} s_\theta = \xi_\theta \\ s_\theta & = & cos^{-1}\left( \left( 1 - \xi_\theta \right)^\frac{1}{n+1} \right) \\ \end{eqnarray}

Therefore, a GLSL shader which generates important directions for a Phong NDF from random, uniform values looks like this:

vec2 importance_sample_phong(vec2 xi)
  float phi = 2.0f * PI * xi.x;
  float theta = acos(pow(1.0f - xi.y, 1.0f/(n+1.0f)));
  return vec2(phi, theta);

Note that since \xi_\theta \in [0,1) the expression 1 - \xi_\theta is also a random variable in the same range.

You can for instance use a low-discrepancy sequence as a source for uniformly distributed random values xi and generate important samples which aren’t clumped.

Trowbridge-Reitz aka GGX

The GGX normal distribution function is part of a microfacet model, which gives you the probability of micro-surface normals oriented along a certain direction. The term has to be normalized though before integrating over the hemisphere to account for the projected micro-surface area:

\begin{equation} \int_\Omega D(\mathbf{m})(\mathbf{n} \cdot \mathbf{m}) d\mathbf{m} = 1 \end{equation}

The NDF itself is defined as:

\begin{equation} D(\mathbf{m}) = \frac{\alpha^2}{\pi \left( \cos^2 (\mathbf{n} \cdot \mathbf{m}) \left(\alpha^2 - 1 \right) +1 \right)^2} \end{equation}

Just like above, we start out with the PDF for GGX:

\begin{equation} p(\theta, \phi) = \frac{\alpha^2}{\pi \left( \cos^2 \theta \left(\alpha^2 - 1 \right) +1 \right)^2} \cos\theta \sin\theta \end{equation}

As in the case of Phong, we create two functions for \theta and \phi . First let’s create p(\theta) :

integrate((a^2 * cos(t) * sin(t))/(%pi * ((a^2−1) * cos(t)^2+1)^2), p, 0, 2 * %pi)

\begin{equation} p(\theta) = \int^{2 \pi}_0 p(\theta, \phi) d\phi = \frac{2 \alpha^2}{\left( \cos^2 \theta \left( \alpha^2 -1 \right) + 1 \right)^2} \cos \theta \sin \theta \end{equation}

The integration for \phi is the same as above, so we skip it and instead now create the CDF for p(\theta) :

integrate((2 * a^2 * cos(t) * sin(t))/((a^2−1) * cos(t)^2+1)^2, t, 0, s)

\begin{eqnarray} P(s_\theta) & = & \int_0^{s_\theta} p(\theta) d\theta \\ & = & 2 \alpha^2 \left( \frac{1}{ \left( 2 \alpha^4 - 4 \alpha^2 + 2 \right) \cos^2 s_\theta + 2 \alpha^2 - 2} - \frac{1}{2 \alpha^4 - 2 \alpha^2 } \right) \\ \end{eqnarray}

Setting the CDF to a random variable and solving for s yields:

solve(2 * a^2 * (1/((2 * a^4−4 * a^2+2) * cos(s)^2+2 * a^2−2)−1/(2 * a^4−2 * a^2)) = x, s)

\begin{eqnarray} P(s_\theta) & = & \xi_\theta \\ s_\theta & = & cos^{-1} \left( \sqrt{ \frac{1 - \xi_\theta}{\left( \alpha^2 -1 \right) \xi_\theta + 1}} \right) \\ \end{eqnarray}

A simple GLSL function to generate important directions looks like this:

vec2 importance_sample_ggx(vec2 xi)
  float phi = 2.0f * PI * xi.x;
  float theta = acos(sqrt((1.0f - xi.y)/
                          ((a*a - 1.0f) * xi.y + 1.0f)
  return vec2(phi, theta);


The ultimate goal of mathematics is to eliminate any need for intelligent thought.

Other NDFs can be used to create important samples with the exact same procedure: given a hemispherical PDF (i.e. the specular part of the BRDF), create two independent PDFs for \theta and \phi , integrate both from 0 to s (i.e. create a CDF for each), set the result to \xi and solve for s .


  1. Walter et al., Microfacet Models for Refraction through Rough Surfaces
  2. Wikipedia, Constructions of low-discrepancy sequences
 PBS ImportanceSampling Notes