Cube Map PreIntegration & Sampling
Since the papers "Realtime Shading with Filtered Importance Sampling" ^{1} and "Real Shading in Unreal Engine 4" ^{2}, most people have been using the mips of cube maps to preintegrate the specular illuminance to feed their BRDF for different values of roughness.
You often read something like "a different mip corresponds to some roughness value" but it's generally not very clear how exactly do you tie the mip level and the roughness value of the BRDF?
There are various possible schemes, some of them are explored by S. Lagarde in "Moving Frostbite to PBR" ^{3} in section 4.9.2 eq. 63 where they chose the simple expression:
Where:
 m is the mip level
 N is the maximum mip level (e.g for a 256x256 cube map, N would be log_2(256)=8)
 \alpha is the specular roughness (note that Lagarde uses the "linear roughness" or "perceptual roughness" \alpha_{lin} = \alpha^2 so \sqrt{\alpha_{lin}} = \alpha)
This mapping has the enormous advantage of being very simple, but it may not be optimal.
Stating the problems
Basically, we have 2 main problems when dealing with cube maps:
 an illuminance preintegration problem where, for each mip level, we want a pixel of the cube map to cover the "largest footprint of the BRDF".
 a luminance estimation problem where we want to minimize the amount of samples to take from the BRDF and the cube map
Preintegration problem
For this problem, we want a pixel of the cube map to contain the most significant illuminance information for a given roughness value that the BRDF will use.
In other words, we want the integral of the BRDF to be maximal when summed over the solid angle covered by the pixel and we want to find the roughness value to make it so.
Maximizing the BRDF's footprint
Note that we're dealing with the CookTorrance microfacet specular BRDF model here:
Where:
 \boldsymbol{ n } is the unit surface normal vector
 \boldsymbol{ \omega_i } is the unit incoming light vector
 \boldsymbol{ \omega_o } is the unit outgoing view vector
 \boldsymbol{ \omega_h } is the unit half vector
 \alpha is the surface's roughness
 F_0 is the surface's specular reflectance at normal incidence
Ignoring the Fresnel coefficient F that is factored out of the preintegration anyway, and the shadowing/masking terms that are either viewdependent (not in the integral) or lightdependent (not relevant to the problem at hand), we can focus on the normal distribution function D( \cos(\theta), \alpha ) for which we know that, by definition:
Where:
 \Omega^+ is the upperhemisphere domain
 \boldsymbol{\omega} is a unit direction vector parametrized by the spherical coordinates \theta and \phi
Or in terms of \mu = \cos(\theta):
More generally, we get the cumulative distribution function (CDF):
The case of GGX
In the case of the wellknown GGX model, the NDF is:
The antiderivative of the PDF is given by:
The CDF is then given by:
Maximizing the CDF
As explained by McGuire ^{4}, we know the average solid angle covered by a pixel of the cube map at a given mip level is given by:
We can see that at mip N, when there only remains a single pixel, we cover a full face of the cube, that is d\Omega_p = \frac{2\pi}{3}.
Equivalent solid angle over the sphere
The portion of the unit sphere domain covering the same solid angle as a texel from the cube map is determined by the cosine of the elevation angle:
Where \mu = \cos(\theta) and \theta is the elevation angle from the pole.
The area of the spherical cap C over the unit hemisphere is 2\pi (1  \cos(\theta))
Posing d\Omega_p = d\Omega_c we get:
which gives us the cosine of the spherical cap angle covering the same solid angle as a single texel of the cube map at mip m.
We immediately see that at the maximum mip level m = N we get the maximum aperture angle and:
\mu_{max} = 1  \frac{1}{3} = \frac{2}{3} (this will be important below)
NOTE: It's very interesting to notice that this value is independent of the resolution of the cube map and only depends on the solid angle of a single face of the cube.
Finding the roughness
So for any given mip we have the solid angle d\Omega_p(m) covered by a pixel and incidently, the cosine of the equivalent spherical cap \mu = 1  \frac{1}{3} 2^{2(mN)}.
We're looking after the roughness value \alpha that best maximizes the expression of the CDF for the given pixel footprint/solid angle.
CDF as a function of spherical cap angle, for various values of roughness \alpha.
Obviously, a roughness value of \alpha = 0 always satisfies our criterium since the CDF is always 1, for any solid angle.
Instead, we need to cover a certain ratio C \in [0,1] of the CDF so cdf( \mu, \alpha ) = C.
The ideal solution would be that the CDF covers the largest solid angle \frac{2\pi}{3} for the largest roughness value \alpha_{max} = 1 at the largest mip N so we simply need to solve:
(remember earlier when we found out that at the largest mip, the spherical cap would have an angle corresponding to \mu_{max} = \frac{2}{3}) (I told you it would become significant later!)
This results means that, for every mip level, we're looking for the CDF to cover ~55% of its [0,1] range, as shown in this animation below:
CDF as a function of spherical cap angle, for various roughness \alpha.
The orange part is the ratio of CDF we need to cover to maximize the footprint of the CDF for a given mip level.
So now, we know the constant C that can help us find a link between \alpha, our surface's roughness and \mu, a measure of the solid angle of a pixel of a given mip level:
Mapping mip level to roughness
We know from \eqref{(1)} earlier that:
And since it's easy to solve equation \eqref{(2)} for \alpha:
We now have the procedure to map mip level to roughness.
Mapping roughness to mip level
Again, from \eqref{(2)} we can easily solve for \mu:
The resulting mip level is then obtained by:
Result
Luminance sampling problem
For our second problem,
Conclusion
Mapping is very ugly! :D
References

Krivanek, J. Colbert, M "Realtime Shading with Filtered Importance Sampling" ↩

Karis, B. 2013 "Real Shading in Unreal Engine 4" ↩

Lagarde, S. de Rousiers, C. 2014 "Moving Frostbite to PBR" ↩

McGuire, M. 2011 "Plausible Environment Lighting in Two Lines of Code" ↩