Algorithm Search⌘K

Algorithmadvanced

Variance shadow maps

Also called variance shadow mapping.

Store depth and squared depth, filter them like a texture, and bound the fraction of light by Chebyshev's inequality on the local depth distribution.

Time
Θ(w h + q) worst
Space
Θ(w h) extra
Where
h = the map height in texels; q = the number of shadow lookups; w = the map width in texels
Properties
not adaptivenot comparison baseddeterministicnot in placeofflineiterativenot stable
Origin
William Donnelly, Andrew Lauritzen, 2006

Variance shadow maps store the depth and the squared depth at each texel, so the map can be blurred, mipmapped or filtered with a summed-area table before any compare. At lookup time the filtered mean and variance give an upper bound on the lit fraction by the one-sided form of the Chebyshev inequality. Percentage-closer filtering compares first and then filters, which costs one compare per tap; here one lookup serves a filter of any size, but the bound can be too high, and the shadow then leaks light (light bleeding). Moment shadow mapping stores four moments to make the bound tighter.

In plain words

A normal shadow map stores one distance per dot, and you cannot blur it: a blurred distance is a distance that no object has.

This method stores two numbers per dot: the distance and the distance times itself. These two numbers can be blurred like the colors of an image. After the blur, each dot knows the average distance of the objects near it and how much these distances spread.

At lookup time a classic rule from statistics says: if the point is far behind the average, and the spread is small, then few of the objects near it can be behind the point. That gives a quick estimate of how lit the point is. The estimate is never too dark, but it can be too bright where objects at very different distances overlap.

The problem

The input is a shadow map SS of w×hw \times h texels from the first pass of shadow mapping, with depths in the Direct3D range [0,1][0, 1], smaller nearer, a filter, and a set of lookups. A lookup has a texel (i,j)(i, j) and a receiver depth tt. The filter gives a weight to each texel near (i,j)(i, j), and so a probability distribution of the depths zz under it. The lit fraction is the probability that z≥tz \ge t, the weighted fraction of the texels that the receiver is not behind, which is the value of PCF with the same weights and the bias 0. The output is a value that is at least the lit fraction and is exact in the simple case of one planar occluder over one planar receiver.

The algorithm

  1. VSM-MAP(S, r) ▷ after the depth pass, once per frame
  2. 1M[i, j] ← (S[i, j], S[i, j]^2) for each texel ▷ the moments
  3. 2F ← the (2r + 1) x (2r + 1) box filter of M, with a summed-area table of each moment
  4. return F
  5. VSM-LOOKUP(F, i, j, t, v_min, a) ▷ receiver depth t
  6. 3(m1, m2) ← F[i, j] ▷ the mean and the mean square
  7. 4if t ≤ m1 then return 1
  8. 5v ← max(m2 - m1^2, v_min) ▷ the variance, with a floor
  9. 6p ← v / (v + (t - m1)^2) ▷ the one-sided Chebyshev bound
  10. 7return clamp((p - a) / (1 - a), 0, 1) ▷ light bleeding reduction; a = 0 keeps p

Any linear filter works in line L2: a Gaussian blur in two separable passes, the mip chain of the map, or anisotropic filtering of the hardware. A summed-area table, the 2D form of the prefix sums, gives the box mean of any size in four reads. In line L5, m2−m12m_2 - m_1^2 is the variance of the filtered distribution. The floor vmin⁡v_{\min} is a tuning against round-off, and the amount aa in line L7 is a tuning that trades the bound for less bleeding.

Example

Take the 8×88 \times 8 map of the PCF page: a floor at depth 0.8 and the top of a box at depth 0.3 over the texels 2≤i,j≤52 \le i, j \le 5, with a 3×33 \times 3 box filter. At texel (6,3)(6, 3) the filter holds three texels at 0.3 and six at 0.8.

  • Line L3: m1=(3⋅0.3+6⋅0.8)/9=19/30m_1 = (3 \cdot 0.3 + 6 \cdot 0.8)/9 = 19/30 and m2=(3⋅0.09+6⋅0.64)/9=137/300m_2 = (3 \cdot 0.09 + 6 \cdot 0.64)/9 = 137/300.
  • Line L5: v=137/300−(19/30)2=1/18v = 137/300 - (19/30)^2 = 1/18.
  • Line L6 for the floor, t=0.8t = 0.8: t−m1=1/6t - m_1 = 1/6, so p=(1/18)/(1/18+1/36)=2/3p = (1/18)/(1/18 + 1/36) = 2/3.

PCF gives 2/32/3 as well: with two depths, one planar occluder and one planar receiver, the bound is exact.

Light bleeding. Two occluders at depths 0.1 and 0.9, each over half the filter, cast a shadow on a receiver at 1.0. The true lit fraction is 0. The moments are m1=0.5m_1 = 0.5 and v=0.16v = 0.16, so p=0.16/(0.16+0.25)=16/41=0.390p = 0.16/(0.16 + 0.25) = 16/41 = 0.390: the receiver shows 39 percent of the light. With occluders at 0.1 and 0.2 over a receiver at 0.3, the moments are m1=0.15m_1 = 0.15 and v=0.0025v = 0.0025, and p=0.0025/(0.0025+0.0225)=1/10p = 0.0025/(0.0025 + 0.0225) = 1/10. The bleeding is large when the occluders are far apart and the receiver is close behind the far one.

The reduction. With a=1/5a = 1/5, line L7 maps 16/4116/41 to (16/41−1/5)/(4/5)=39/164=0.238(16/41 - 1/5)/(4/5) = 39/164 = 0.238 and 1/101/10 to 0. It also maps the exact 2/32/3 of the box edge to 7/127/12, below the true value, so the result is no longer an upper bound.

Correctness

The proof file shows these claims, and the tests check each against the exact lit fraction of the filtered texels.

  • The filter. Line L2 gives the box means of zz and z2z^2 (inclusion and exclusion on the summed-area table). The test compares it with a direct sum over the box.
  • The bound. For any distribution with mean m1m_1 and variance v>0v > 0 and any t>m1t > m_1, P(z≥t)≤v/(v+(t−m1)2)P(z \ge t) \le v/(v + (t - m_1)^2). This is the one-sided Chebyshev inequality (Cantelli's inequality). The proof applies the Markov inequality to (z−m1+v/(t−m1))2(z - m_1 + v/(t - m_1))^2.
  • Sharpness. For each mean and variance there is a two-point distribution that reaches the bound, so no method that sees only m1m_1 and m2m_2 can do better.
  • Two planes. If the filter holds only the depths do<td_o < t and the receiver depth tt, the bound equals the true lit fraction.
  • The tunings. The floor vmin⁡v_{\min} keeps the bound, because pp grows with vv. The reduction of line L7 does not keep it, as the example shows.

The paper of Donnelly and Lauritzen states the inequality for P(x≥t)P(x \ge t), which is the lit fraction; its abstract calls the result a bound on the fraction occluded, which is loose wording. The occluded fraction has the lower bound 1−p1 - p.

Proof: The variance shadow map value is a sharp upper bound on the filtered lit fraction, by the one-sided Chebyshev (Cantelli) inequalityfull

We use the notation of the page. A filter is a set of texels TT with weights wk≥0w_k \ge 0 that sum to 1. The filter defines a discrete distribution: the random depth ZZ takes the depth dkd_k of texel kk with probability wkw_k. A texel is lit for the receiver depth tt when dk≥td_k \ge t, so the true filtered lit fraction is Pr⁡[Z≥t]\Pr[Z \ge t]. This is the value that percentage-closer filtering computes with the same weights and the bias 0. The arithmetic is exact.

Filtering the moments

Lemma 1. After lines L1 and L2, the texel (i,j)(i, j) of the filtered map holds (μ,μ2)=(E[Z],E[Z2])(\mu, \mu_2) = (E[Z], E[Z^2]) for the box filter: the uniform weights on the texels of the (2r+1)×(2r+1)(2r + 1) \times (2r + 1) square around (i,j)(i, j) that lie inside the map.

Proof. Line L1 stores (d,d2)(d, d^2) at each texel. The summed-area table holds at (j′,i′)(j', i') the sum of a moment over the texels with row less than j′j' and column less than i′i'. The sum over the rectangle of rows [y0,y1)[y_0, y_1) and columns [x0,x1)[x_0, x_1) is then t[y1][x1]−t[y0][x1]−t[y1][x0]+t[y0][x0]t[y_1][x_1] - t[y_0][x_1] - t[y_1][x_0] + t[y_0][x_0], by inclusion and exclusion: a texel inside the rectangle is counted once, and a texel outside is counted by an equal number of plus and minus terms. Dividing by the number of texels in the rectangle gives the mean of dd and of d2d^2 over it. □\square

The same holds for any linear filter with nonnegative weights that sum to 1, such as a Gaussian or a mipmap level, because a weighted mean of dd and of d2d^2 is the first and second moment of the weighted distribution. This is the property that the binary depth test does not have and that makes the map filterable.

The bound

Theorem 2 (Cantelli). Let ZZ have mean μ\mu and variance σ2=μ2−μ2\sigma^2 = \mu_2 - \mu^2. For t>μt > \mu,

Pr⁡[Z≥t]≤pmax⁡(t)=σ2σ2+(t−μ)2.\Pr[Z \ge t] \le p_{\max}(t) = \frac{\sigma^2}{\sigma^2 + (t - \mu)^2}.

Proof. Let a=t−μ>0a = t - \mu > 0. For every u≥0u \ge 0, the event Z−μ≥aZ - \mu \ge a implies Z−μ+u≥a+u>0Z - \mu + u \ge a + u > 0, so it implies (Z−μ+u)2≥(a+u)2(Z - \mu + u)^2 \ge (a + u)^2. By Markov's inequality applied to the nonnegative variable (Z−μ+u)2(Z - \mu + u)^2,

Pr⁡[Z≥t]≤E[(Z−μ+u)2](a+u)2=σ2+u2(a+u)2.\Pr[Z \ge t] \le \frac{E[(Z - \mu + u)^2]}{(a + u)^2} = \frac{\sigma^2 + u^2}{(a + u)^2}.

Take u=σ2/au = \sigma^2 / a. The numerator is σ2(a2+σ2)/a2\sigma^2 (a^2 + \sigma^2) / a^2 and the denominator is (a2+σ2)2/a2(a^2 + \sigma^2)^2 / a^2, so the bound is σ2/(σ2+a2)\sigma^2 / (\sigma^2 + a^2). If σ2=0\sigma^2 = 0, then Z=μ<tZ = \mu < t with probability 1, and both sides are 0. □\square

Corollary 3. With the minimum variance 0 and no bleeding reduction, the lookup (lines L3 to L6) returns a value pp with Pr⁡[Z≥t]≤p≤1\Pr[Z \ge t] \le p \le 1.

Proof. If t≤μt \le \mu, line L4 returns 1, which is an upper bound on any probability. Otherwise line L5 computes σ2≥0\sigma^2 \ge 0 (the variance of a distribution is nonnegative, and in exact arithmetic μ2−μ2\mu_2 - \mu^2 is that variance), and line L6 returns pmax⁡(t)p_{\max}(t), which is at most 1 and at least Pr⁡[Z≥t]\Pr[Z \ge t] by Theorem 2. □\square

This is a proved property, and it is the claim of the method. The inequality in the paper of Donnelly and Lauritzen is on Pr⁡[x≥t]\Pr[x \ge t], the fraction that is not occluded, although their abstract calls it a bound on the fraction occluded. The occluded fraction gets the lower bound 1−pmax⁡1 - p_{\max}.

The bound is sharp

Proposition 4. For every μ\mu, every σ2>0\sigma^2 > 0 and every t>μt > \mu, the two-point distribution with Z=tZ = t with probability q=σ2/(σ2+a2)q = \sigma^2 / (\sigma^2 + a^2) and Z=μ−σ2/aZ = \mu - \sigma^2 / a otherwise, a=t−μa = t - \mu, has mean μ\mu, variance σ2\sigma^2 and Pr⁡[Z≥t]=q=pmax⁡(t)\Pr[Z \ge t] = q = p_{\max}(t). So no function of (μ,μ2)(\mu, \mu_2) alone gives a smaller upper bound.

Proof. The mean is μ+qa−(1−q)σ2/a=μ+σ2a/(σ2+a2)−σ2a/(σ2+a2)=μ\mu + q a - (1 - q) \sigma^2 / a = \mu + \sigma^2 a / (\sigma^2 + a^2) - \sigma^2 a / (\sigma^2 + a^2) = \mu. The variance is qa2+(1−q)σ4/a2=σ2a2/(σ2+a2)+σ4/(σ2+a2)=σ2q a^2 + (1 - q) \sigma^4 / a^2 = \sigma^2 a^2 / (\sigma^2 + a^2) + \sigma^4 / (\sigma^2 + a^2) = \sigma^2. The point μ−σ2/a\mu - \sigma^2 / a is less than tt, so Pr⁡[Z≥t]=q\Pr[Z \ge t] = q. □\square

Proposition 5. For one planar occluder at depth d1d_1 with weight w1w_1 and one planar receiver at depth d2>d1d_2 > d_1 with weight w2=1−w1w_2 = 1 - w_1, the lookup at t=d2t = d_2 returns the true lit fraction w2w_2.

Proof. With Δ=d2−d1\Delta = d_2 - d_1: μ=d2−w1Δ\mu = d_2 - w_1 \Delta, so t−μ=w1Δt - \mu = w_1 \Delta, and σ2=w1w2Δ2\sigma^2 = w_1 w_2 \Delta^2. Then pmax⁡=w1w2Δ2/(w1w2Δ2+w12Δ2)=w2/(w2+w1)=w2p_{\max} = w_1 w_2 \Delta^2 / (w_1 w_2 \Delta^2 + w_1^2 \Delta^2) = w_2 / (w_2 + w_1) = w_2. □\square

The gap between pmax⁡p_{\max} and the true fraction is light bleeding. It is large when a filter region holds two occluders at different depths over a receiver: the depths {1,2}\{1, 2\} with the receiver at 3 give 1/101/10, and {1,9}\{1, 9\} with the receiver at 10 give 16/4116/41, while the true lit fraction is 0 in both cases.

The two tunings

Proposition 6. (a) A minimum variance vmin⁡v_{\min} in line L5 keeps the upper bound. (b) The bleeding reduction of line L7 with an amount c∈(0,1)c \in (0, 1) can return less than the true lit fraction, so it does not keep the bound.

Proof. (a) For a fixed t>μt > \mu, the map v↦v/(v+a2)v \mapsto v / (v + a^2) increases in vv, so replacing σ2\sigma^2 by max⁡(σ2,vmin⁡)\max(\sigma^2, v_{\min}) does not decrease pp; it stays at least Pr⁡[Z≥t]\Pr[Z \ge t]. (b) Take the planar case of Proposition 5 with w2=2/3w_2 = 2/3 and c=1/5c = 1/5. The lookup gives p=2/3p = 2/3, and line L7 gives (2/3−1/5)/(4/5)=7/12<2/3(2/3 - 1/5)/(4/5) = 7/12 < 2/3. □\square

The minimum variance is there to avoid a zero or negative variance from rounding in floating point, which shows as acne. Both values are tunings, not derived quantities.

Complexity

Line L1 and the summed-area tables of line L2 take Θ(wh)\Theta(w h) time and space, and so does any fixed-size separable blur. Each lookup is O(1)O(1): four reads per moment for the box mean, and a few arithmetic operations. So qq lookups cost Θ(wh+q)\Theta(w h + q) in the worst case, whatever the filter size. The map takes two values per texel in place of one.

Properties

  • Filterable. The stored moments are linear in the texel weights, so every linear filter, mipmapping and multisample resolve applies to them before the lookup (a proved property).
  • An upper bound. The result is never darker than the lit fraction of the filtered texels, without line L7 (a proved property).
  • Light bleeding. Where the depths under the filter spread widely and the receiver is close behind the far occluder, the bound is much too high (a proved property of the bound; the example gives 39 percent of the light in place of 0).
  • Precision. The variance m2−m12m_2 - m_1^2 is a difference of two close numbers. With 16-bit storage it loses most of its digits, so the moments need 32-bit floats or a careful offset of the depth.

Variants and improvements

  • Summed-area variance shadow maps. Lauritzen (GPU Gems 3, Chapter 8) uses summed-area tables so that each pixel can have its own filter size, which also serves as the filter step of soft shadows.
  • Light bleeding reduction. Map [a,1][a, 1] to [0,1][0, 1] by a linear step, as in line L7. It removes bleeding below the level aa and darkens true penumbras.
  • Layered and exponential maps. Split the depth range into layers, or warp the depth by an exponential before the moments are taken, so that the occluders in one layer spread less.
  • Moment shadow maps. Store four moments and use the sharp bound for them; see moment shadow mapping.

When to use it

Use variance shadow maps when the shadow must be filtered over many texels, mipmapped or blurred, and the cost per lookup must stay constant: wide soft shadows, distant cascades, or shadows with anisotropic filtering at grazing angles. Use PCF when the filter is small or when scenes with deep overlapping occluders make the bleeding visible.

Pitfalls

  • No variance floor. Without vmin⁡v_{\min} in line L5, round-off can make m2−m12m_2 - m_1^2 zero or negative, and line L6 then returns 0 or a value outside [0,1][0, 1] at a lit surface.
  • A large variance floor. Line L5 with a large vmin⁡v_{\min} lights a receiver close behind a flat occluder: with m1=0.5m_1 = 0.5, no spread and t=0.51t = 0.51, the floor 10−410^{-4} gives p=1/2p = 1/2, but the receiver is in shadow.
  • Reduction as a free fix. Line L7 with a>0a > 0 turns the bound into an estimate that can be too dark, and it shrinks true penumbras; choose aa per scene.
  • Comparing with the filtered depth. Replacing lines L4 to L6 by the test t≤m1t \le m_1 returns 0 or 1, the error that PCF exists to avoid.
  • Too little precision. Storing the moments of line L1 in 16 bits destroys the variance; the shadow then shows noise and acne.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

variance_shadow_maps.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/variance-shadow-maps/. The authors are listed in prov/BNWA0B.jsonl.

"""Variance shadow maps (Donnelly and Lauritzen 2006).

A depth map s is a list of h rows of w depths, s[j][i], i the column and j the
row. Depth is in [0, 1] (the Direct3D clip range), smaller is nearer to the
light. The moment map holds (E[z], E[z^2]) per texel after a filter. The
lookup returns an upper bound on the lit fraction, the fraction of the
filtered depths d with d >= t for the receiver depth t (Cantelli's inequality).
With Fraction input and min_var = 0 every result is exact.
"""

from fractions import Fraction


def moments(s):
    """The raw moment map: (d, d^2) per texel."""
    return [[(d, d * d) for d in row] for row in s]  # @line L1


def summed_area(m):
    """Summed-area tables of both moments, with a zero first row and column."""
    h, w = len(m), len(m[0])
    t1 = [[0] * (w + 1) for _ in range(h + 1)]
    t2 = [[0] * (w + 1) for _ in range(h + 1)]
    for j in range(h):
        a1 = a2 = 0
        for i in range(w):
            a1 += m[j][i][0]
            a2 += m[j][i][1]
            t1[j + 1][i + 1] = t1[j][i + 1] + a1
            t2[j + 1][i + 1] = t2[j][i + 1] + a2
    return t1, t2


def box_filter(m, r):
    """The (2r + 1) x (2r + 1) box filter of the moments, clamped at the border,
    computed from summed-area tables in O(1) per texel."""
    h, w = len(m), len(m[0])
    t1, t2 = summed_area(m)
    out = []
    for j in range(h):
        row = []
        for i in range(w):
            x0, x1 = max(i - r, 0), min(i + r + 1, w)
            y0, y1 = max(j - r, 0), min(j + r + 1, h)
            n = (x1 - x0) * (y1 - y0)
            m1 = t1[y1][x1] - t1[y0][x1] - t1[y1][x0] + t1[y0][x0]
            m2 = t2[y1][x1] - t2[y0][x1] - t2[y1][x0] + t2[y0][x0]
            row.append((Fraction(m1) / n, Fraction(m2) / n) if isinstance(m1, (int, Fraction))
                       else (m1 / n, m2 / n))
        out.append(row)
    return out  # @line L2


def chebyshev_upper_bound(m1, m2, t, min_var=0):
    """p_max of the paper: 1 when t <= m1, else var / (var + (t - m1)^2)."""
    if t <= m1:  # @line L4
        return 1
    var = max(m2 - m1 * m1, min_var)  # @line L5
    d = t - m1
    return var / (var + d * d)  # @line L6


def reduce_bleeding(p, amount):
    """Light bleeding reduction: map [amount, 1] linearly onto [0, 1]."""
    if amount == 0:
        return p
    return min(max((p - amount) / (1 - amount), 0), 1)  # @line L7


def vsm_lookup(fm, i, j, t, min_var=0, amount=0):
    """The lit estimate at texel (i, j) of the filtered moment map fm."""
    m1, m2 = fm[j][i]  # @line L3
    return reduce_bleeding(chebyshev_upper_bound(m1, m2, t, min_var), amount)


def lit_fraction(depths, t):
    """The true lit fraction of a list of depths for the receiver depth t."""
    return Fraction(sum(1 for d in depths if d >= t), len(depths))
test_variance_shadow_maps.py 124 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/variance-shadow-maps/. The authors are listed in prov/BNWA0B.jsonl.

import random
from fractions import Fraction

from variance_shadow_maps import (
    box_filter,
    chebyshev_upper_bound,
    lit_fraction,
    moments,
    reduce_bleeding,
    vsm_lookup,
)


def random_map(rng, w, h):
    return [[Fraction(rng.randrange(0, 33), 32) for _ in range(w)] for _ in range(h)]


def box_taps(s, i, j, r):
    h, w = len(s), len(s[0])
    return [s[y][x] for y in range(max(j - r, 0), min(j + r + 1, h))
            for x in range(max(i - r, 0), min(i + r + 1, w))]


def test_light_bleeding_numbers():
    # Half the texels at depth 1 and half at depth 2, receiver at 3.
    d = [Fraction(1), Fraction(2)]
    m1 = sum(d) / 2
    m2 = sum(x * x for x in d) / 2
    assert chebyshev_upper_bound(m1, m2, 3) == Fraction(1, 10)
    assert lit_fraction(d, 3) == 0
    # Two occluders at 1 and 9 over a receiver at 10.
    d = [Fraction(1), Fraction(9)]
    m1 = sum(d) / 2
    m2 = sum(x * x for x in d) / 2
    assert chebyshev_upper_bound(m1, m2, 10) == Fraction(16, 41)
    assert lit_fraction(d, 10) == 0


def test_summed_area_filter_equals_direct_box():
    rng = random.Random(1)
    for _ in range(20):
        w, h = rng.randrange(3, 12), rng.randrange(3, 12)
        s = random_map(rng, w, h)
        for r in range(3):
            fm = box_filter(moments(s), r)
            for j in range(h):
                for i in range(w):
                    taps = box_taps(s, i, j, r)
                    assert fm[j][i][0] == sum(taps) / len(taps)
                    assert fm[j][i][1] == sum(t * t for t in taps) / len(taps)


def test_upper_bound_on_the_lit_fraction():
    rng = random.Random(2)
    for _ in range(40):
        s = random_map(rng, 9, 9)
        r = rng.randrange(0, 3)
        fm = box_filter(moments(s), r)
        for _ in range(20):
            i, j = rng.randrange(9), rng.randrange(9)
            t = Fraction(rng.randrange(0, 40), 32)
            p = vsm_lookup(fm, i, j, t)
            assert 0 <= p <= 1
            assert p >= lit_fraction(box_taps(s, i, j, r), t)


def test_exact_for_one_planar_occluder_and_one_planar_receiver():
    rng = random.Random(3)
    for _ in range(300):
        d_occ = Fraction(rng.randrange(0, 32), 64)
        d_rec = d_occ + Fraction(rng.randrange(1, 32), 64)
        k = rng.randrange(1, 9)
        n = rng.randrange(k + 1, 20)
        depths = [d_occ] * k + [d_rec] * (n - k)
        m1 = sum(depths) / n
        m2 = sum(x * x for x in depths) / n
        assert chebyshev_upper_bound(m1, m2, d_rec) == lit_fraction(depths, d_rec)


def test_sharp_for_every_mean_and_variance():
    # Cantelli is attained by the two-point law at t and m - v / (t - m).
    rng = random.Random(4)
    for _ in range(200):
        m = Fraction(rng.randrange(0, 50), 64)
        v = Fraction(rng.randrange(1, 50), 4096)
        t = m + Fraction(rng.randrange(1, 40), 64)
        q = v / (v + (t - m) ** 2)
        a = m - v / (t - m)
        assert q * t + (1 - q) * a == m
        assert q * t * t + (1 - q) * a * a - m * m == v
        assert chebyshev_upper_bound(m, v + m * m, t) == q


def test_min_variance_and_bleeding_reduction():
    assert chebyshev_upper_bound(Fraction(1, 2), Fraction(1, 4), Fraction(1, 2)) == 1
    assert chebyshev_upper_bound(Fraction(1, 2), Fraction(1, 4), Fraction(3, 4)) == 0
    p = chebyshev_upper_bound(Fraction(1, 2), Fraction(1, 4), Fraction(1, 2) + Fraction(1, 100),
                              min_var=Fraction(1, 10000))
    assert p == Fraction(1, 2)
    rng = random.Random(5)
    for _ in range(200):
        p = Fraction(rng.randrange(0, 101), 100)
        a = Fraction(rng.randrange(0, 100), 100)
        q = reduce_bleeding(p, a)
        assert 0 <= q <= p
        if p <= a:
            assert q == 0
    assert reduce_bleeding(Fraction(1, 10), Fraction(1, 5)) == 0
    assert reduce_bleeding(Fraction(16, 41), Fraction(1, 5)) == Fraction(39, 164)


def test_float_path_agrees():
    rng = random.Random(6)
    s = random_map(rng, 8, 8)
    fe = box_filter(moments(s), 1)
    sf = [[float(d) for d in row] for row in s]
    ff = box_filter(moments(sf), 1)
    for j in range(8):
        for i in range(8):
            t = 0.7
            assert abs(vsm_lookup(fe, i, j, Fraction(7, 10)) - vsm_lookup(ff, i, j, t)) < 1e-9

History

Donnelly and Lauritzen presented variance shadow maps at I3D 2006. They saw that two moments are enough for a useful bound and that the moments can be filtered with the texture hardware, which plain depth maps cannot. Lauritzen extended the method with summed-area tables and the light bleeding reduction in GPU Gems 3 in 2007. The problem of bleeding led to exponential maps and, in 2015, to moment shadow mapping by Peters and Klein.

References

  1. William Donnelly and Andrew Lauritzen, Variance shadow maps, Proceedings of the 2006 Symposium on Interactive 3D Graphics and Games (I3D 2006), 161 to 165, the moment map, the one-tailed Chebyshev bound p_max, the exactness for one planar occluder and one planar receiver, and the light bleeding example
  2. Hubert Nguyen (editor), GPU Gems 3, Addison-Wesley, 2007, Chapter 8, Andrew Lauritzen, Summed-Area Variance Shadow Maps, Section 8.4.2 (the minimum variance) and Section 8.4.3 (the light bleeding reduction with linstep), and the summed-area table filter
  3. Tomas Akenine-Möller, Eric Haines, Naty Hoffman, Angelo Pesce, Michał Iwanicki and Sébastien Hillaire, Real-Time Rendering, fourth edition, CRC Press, 2018, Chapter 7, the part on filtered shadow maps (general locator)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example