Algorithm Search⌘K

Algorithmcore

Percentage-closer filtering

Also called PCF.

Soften shadow map edges by comparing several depth samples near the lookup point with the receiver depth and averaging the binary results.

Time
Θ(q k) worst
Space
Θ(1) extra
Where
k = the number of kernel taps; q = the number of shadow lookups
Properties
not adaptivecomparison baseddeterministicnot in placeofflineiterativenot stable
Origin
William T. Reeves, David H. Salesin, Robert L. Cook, 1987

Percentage-closer filtering changes the lookup step of shadow mapping: it compares the receiver depth with every depth in a small neighborhood of the shadow map and returns the weighted mean of the binary results. The map and its first pass do not change. The result is a lit fraction between 0 and 1, so the shadow edge is smooth, but its width is set by the kernel and not by the size of the light; percentage-closer soft shadows choose the kernel from the scene, and variance shadow maps change what the map stores so that it can be filtered before the test.

In plain words

A shadow map gives a yes or no answer for each point: lit or not lit. Where the shadow edge crosses the dots of the map, the yes and no answers make a staircase.

Ask the same question at several dots near the point, and count how many say lit. If 6 of 9 say lit, call the point two thirds lit. Points near the edge then get values between dark and bright, and the staircase becomes a soft ramp.

The order matters. You must ask the question at each dot first and then take the average. If you take the average of the stored distances first and ask once, you still get only yes or no, and the edge stays hard.

The problem

The input is a shadow map SS of W×HW \times H texels from the first pass of shadow mapping, a lookup point with the light space position (x,y,z)(x, y, z), a bias b≥0b \ge 0, and a kernel KK that gives a weight ω≥0\omega \ge 0 to each offset (u,v)(u, v). As on the shadow mapping page, xx and yy are in texel units, texel (i,j)(i, j) has its center at (i+12,j+12)(i + \tfrac12, j + \tfrac12), and zz is the light depth in the Direct3D range [0,1][0, 1], smaller nearer. A tap (i,j)(i, j) is lit when z−b≤S[i,j]z - b \le S[i, j], and a tap outside the map is lit (the border rule of that page). The output is the weighted mean of the lit taps over the kernel, placed at the texel that holds (x,y)(x, y). The shadow mapping page returns the shadowed fraction; this page returns the lit fraction, which is 1 minus it.

The algorithm

  1. LIT-TAP(S, i, j, z, b)
  2. if (i, j) is outside the map then return 1
  3. if z - b ≤ S[i, j] then return 1 else return 0
  4. PCF(S, x, y, z, b, K) ▷ K maps an offset (u, v) to a weight ω
  5. 1(i, j) ← (⌊x⌋, ⌊y⌋) ▷ the texel that holds (x, y)
  6. 2acc ← 0, wsum ← 0
  7. 3for each offset (u, v) with weight ω > 0 in K do
  8. 4t ← LIT-TAP(S, i + u, j + v, z, b) ▷ compare first
  9. 5acc ← acc + ω t ▷ then filter
  10. 6wsum ← wsum + ω
  11. 7return acc / wsum ▷ the lit fraction
  12. PCF-BILINEAR(S, x, y, z, b) ▷ the 2 x 2 form of the hardware
  13. 8(i, j) ← (⌊x - 1/2⌋, ⌊y - 1/2⌋), f ← x - 1/2 - i, g ← y - 1/2 - j
  14. 9c00, c10, c01, c11 ← LIT-TAP at (i, j), (i + 1, j), (i, j + 1), (i + 1, j + 1)
  15. 10return (1 - f)(1 - g) c00 + f (1 - g) c10 + (1 - f) g c01 + f g c11

The box kernel of radius rr gives the weight 1 to each offset in [−r,r]2[-r, r]^2, which is the filter of the shadow mapping page. The tent kernel gives the weight (r+1−∣u∣)(r+1−∣v∣)(r + 1 - |u|)(r + 1 - |v|), which makes the ramp smoother. PCF-BILINEAR places the four taps around (x,y)(x, y) and weights them by the distances to their centers. Graphics hardware does this in one lookup of a depth texture with a compare mode. Larger kernels in a renderer are often sums of several such bilinear lookups.

Example

The 8×88 \times 8 map has a floor at depth 0.8 and the top of a box at depth 0.3 over the texels with 2≤i,j≤52 \le i, j \le 5. The receiver is the floor, z=0.8z = 0.8, with b=0b = 0, and the kernel is the 3×33 \times 3 box. Each lookup is at the center of the texel in the table.

texel shadowed taps lit fraction
(6,3)(6, 3) column 5, rows 2 to 4 6/9=2/36/9 = 2/3
(6,6)(6, 6) (5,5)(5, 5) only 8/98/9
(5,5)(5, 5) columns 4 and 5, rows 4 and 5 5/95/9
(5,3)(5, 3) columns 4 and 5, rows 2 to 4 3/9=1/33/9 = 1/3

At (6,3)(6, 3) the floor is outside the box, but the box is one texel away, so a third of the kernel is shadowed. Without PCF the four lookups return 1, 1, 0 and 0.

The bilinear form at (x,y)=(6.25,3.5)(x, y) = (6.25, 3.5) has (i,j)=(5,3)(i, j) = (5, 3), f=0.75f = 0.75 and g=0g = 0. The tap (5,3)(5, 3) is shadowed and (6,3)(6, 3) is lit, so L10 returns 0.750.75. As xx moves from 5.5 to 6.5, the value goes from 0 to 1 along a straight line.

Filtering the depths first. At (6,3)(6, 3) the mean of the nine stored depths is (3⋅0.3+6⋅0.8)/9=0.6333(3 \cdot 0.3 + 6 \cdot 0.8)/9 = 0.6333, and 0.8>0.63330.8 > 0.6333, so one compare says shadowed (0). PCF says 2/32/3. At (5,3)(5, 3) the mean is (6⋅0.3+3⋅0.8)/9=0.4667(6 \cdot 0.3 + 3 \cdot 0.8)/9 = 0.4667, and the one compare also says 0. The mean of the depths is a depth that no surface has, and the compare with it is still binary.

Correctness

The proof file shows three claims, and the tests check each one against a second method.

  • The weighted mean. For any kernel with nonnegative weights and a positive sum, lines L1 to L7 return ∑ω t/∑ω\sum \omega\, t / \sum \omega over the taps, a value in [0,1][0, 1] that does not increase when zz grows. With one tap it is the binary test of shadow mapping. For the box, the count of lit taps equals the count from a prefix sum table of the binary image of lit texels, which is how the test checks it.
  • The bilinear form. Line L10 equals the mean of the nearest texel test over the unit square centered at (x,y)(x, y): the square overlaps the four texels in the areas (1−f)(1−g)(1 - f)(1 - g), f(1−g)f(1 - g), (1−f)g(1 - f) g and fgf g. At a texel center f=g=0f = g = 0, and the result is the binary test. The tests compare L10 with this integral, computed on a 16×1616 \times 16 grid of sample points, exactly.
  • The wrong order. Filtering the depths and then comparing once gives only 0 or 1. The example shows this.

What PCF does not do is give the true visibility of an area light: the mean is over the taps of a point light test, so it is an image filter of the binary shadow, not a penumbra.

Proof: Percentage-closer filtering returns the weighted fraction of lit taps, and the 2 x 2 bilinear form is the exact box integral of the binary testfull

We use the notation of the page. The map SS has WW columns and HH rows, and texel (i,j)(i, j) has its center at (i+12,j+12)(i + \tfrac12, j + \tfrac12). For a receiver depth zz and a bias bb, the binary test of texel (i,j)(i, j) is t(i,j)=1t(i, j) = 1 if (i,j)(i, j) is outside the map or z−b≤S[i,j]z - b \le S[i, j], and t(i,j)=0t(i, j) = 0 otherwise. So t=1t = 1 means lit. A kernel KK is a finite map from integer offsets (u,v)(u, v) to weights K(u,v)≥0K(u, v) \ge 0, with at least one positive weight. The arithmetic is exact.

The weighted form

Theorem 1. For a lookup at (x,y)(x, y) with (i,j)=(⌊x⌋,⌊y⌋)(i, j) = (\lfloor x \rfloor, \lfloor y \rfloor), PCF (lines L1 to L7) returns

P=∑(u,v)K(u,v) t(i+u,j+v)∑(u,v)K(u,v),P = \frac{\sum_{(u, v)} K(u, v)\, t(i + u, j + v)}{\sum_{(u, v)} K(u, v)},

and 0≤P≤10 \le P \le 1. PP does not increase when zz increases, and does not decrease when bb increases. For the kernel with the single tap K(0,0)=1K(0, 0) = 1, PP is the binary test of shadow mapping at the texel that holds (x,y)(x, y), with lit in place of shadowed.

Proof. Lines L3 to L6 visit each offset with a positive weight once; L5 adds K(u,v) t(i+u,j+v)K(u, v)\, t(i + u, j + v) to aa and L6 adds K(u,v)K(u, v) to ww. Offsets with weight 0 add 0 to both sums, so skipping them does not change aa or ww. L7 returns a/wa / w, and w>0w > 0. Each tt is 0 or 1 and each weight is nonnegative, so 0≤a≤w0 \le a \le w, which gives 0≤P≤10 \le P \le 1. The test z−b≤S[i+u,j+v]z - b \le S[i + u, j + v] can only change from true to false when zz increases, and only from false to true when bb increases. So each term of aa is monotone in the stated direction, and so is PP, because ww does not depend on zz or bb. With one tap, P=t(i,j)P = t(i, j). □\square

For the box kernel of radius rr (weight 1 on [−r,r]2[-r, r]^2), PP is the number of lit taps divided by (2r+1)2(2r + 1)^2. The number of lit taps in a rectangle of texels is also the four-term inclusion-exclusion over the table of prefix counts C[i,j]=∑i′<i,j′<jt(i′,j′)C[i, j] = \sum_{i' < i, j' < j} t(i', j'). This is the second method that the tests use.

The bilinear form

Theorem 2. Let tˉ(u,v)=t(⌊u⌋,⌊v⌋)\bar t(u, v) = t(\lfloor u \rfloor, \lfloor v \rfloor) be the binary test of the texel that holds the point (u,v)(u, v). Then PCF-BILINEAR (lines L8 to L10) returns

∫y−1/2y+1/2∫x−1/2x+1/2tˉ(u,v) du dv,\int_{y - 1/2}^{y + 1/2} \int_{x - 1/2}^{x + 1/2} \bar t(u, v)\, du\, dv,

the mean of the nearest-texel binary test over the unit square centered at (x,y)(x, y).

Proof. Let i=⌊x−12⌋i = \lfloor x - \tfrac12 \rfloor and f=x−12−if = x - \tfrac12 - i, so 0≤f<10 \le f < 1 and the interval [x−12,x+12][x - \tfrac12, x + \tfrac12] is [i+f,i+f+1][i + f, i + f + 1]. It meets the column [i,i+1)[i, i + 1) in a length 1−f1 - f and the column [i+1,i+2)[i + 1, i + 2) in a length ff, and no other column in a set of positive length. In the same way, with j=⌊y−12⌋j = \lfloor y - \tfrac12 \rfloor and g=y−12−jg = y - \tfrac12 - j, the rows jj and j+1j + 1 get the lengths 1−g1 - g and gg. The function tˉ\bar t is constant on each texel, so the integral is the sum over the four texels of the overlap area times the test: (1−f)(1−g) t(i,j)+f(1−g) t(i+1,j)+(1−f)g t(i,j+1)+fg t(i+1,j+1)(1 - f)(1 - g)\, t(i, j) + f (1 - g)\, t(i + 1, j) + (1 - f) g\, t(i, j + 1) + f g\, t(i + 1, j + 1). This is line L10. □\square

At the texel center (i′+12,j′+12)(i' + \tfrac12, j' + \tfrac12) we get i=i′i = i', j=j′j = j' and f=g=0f = g = 0, so the result is t(i′,j′)t(i', j'), the binary test. Between centers the result is continuous in (x,y)(x, y), because the weights are continuous in ff and gg and the weights of a texel go to 0 when the square leaves it.

Filtering the depths first is a different function

Proposition 3. The function that averages the depths in the box and compares once returns only 0 or 1, and it can differ from PCF by more than 12\tfrac12.

Proof. It returns the value of one comparison, so it is 0 or 1. In the example of the page (floor depth 0.80.8, box top 0.30.3, z=0.8z = 0.8, b=0b = 0, a 3×33 \times 3 box at texel (6,3)(6, 3)), three taps hold 0.30.3 and six hold 0.80.8. The mean depth is (3⋅0.3+6⋅0.8)/9=0.6333…<0.8(3 \cdot 0.3 + 6 \cdot 0.8)/9 = 0.6333\ldots < 0.8, so the single test returns 0. PCF counts the six lit taps and returns 2/32/3, so the two differ by 2/3>122/3 > \tfrac12. □\square

Complexity

One lookup with a kernel of kk taps makes kk texel reads and compares, so it costs Θ(k)\Theta(k), and qq lookups cost Θ(qk)\Theta(q k) in the worst case. The box of radius rr has k=(2r+1)2k = (2r + 1)^2. The bilinear form is k=4k = 4. The extra space is Θ(1)\Theta(1) beyond the map. The cost cannot drop to O(1)O(1) per lookup with a prefix sum table of the map, because the binary image depends on the receiver depth zz of each lookup. This is the reason for the filterable maps of the variants.

Properties

  • Compare, then filter. The filter acts on the binary results, so the output is a fraction of lit taps (a proved property).
  • Linear in the weights, not in the depths. For a fixed receiver depth, PCF is a linear filter of the binary image. It is not a linear filter of the stored depths, so the map cannot be mipmapped or blurred in advance.
  • Fixed penumbra. The width of the soft edge is the kernel width in texels, the same for a contact shadow and for a shadow far from its occluder. This is a heuristic softening, not the shadow of an area light.
  • Bias per tap. One bias serves all taps, but the receiver depth changes across the kernel, so a large kernel needs a larger bias or a bias that follows the receiver plane.

Variants and improvements

  • Kernel shapes. Box, tent and Gaussian kernels; a Poisson disk of taps with a rotation per pixel trades the regular pattern for noise, which temporal antialiasing can then average.
  • Bilinear taps. A kernel of (2r)2(2r)^2 texels can be read with r2r^2 bilinear lookups, each with its own weights.
  • Receiver plane depth bias. Compute the depth slope of the receiver in light space and compare each tap with the receiver depth moved to that tap, in place of one constant bias.
  • Soft shadows. Percentage-closer soft shadows estimate the penumbra width from a blocker search and set the kernel from it.
  • Filterable maps. Variance shadow maps and moment shadow mapping store moments of the depth, which can be filtered in advance, and bound the lit fraction from them.

When to use it

Use PCF as the default filter of a shadow map. The 2×22 \times 2 hardware form costs about one texture lookup, and a few bilinear lookups give a good edge at a low cost. Use a filterable map when the kernel must be large, when the map should be mipmapped, or when the shadow must be blurred over many texels, because the cost of PCF grows with the kernel area.

Pitfalls

  • Filtering the depths first. Replacing lines L4 and L5 by a mean of SS and one compare after L7 gives 0 or 1 at every point, as the example shows: 0 at (6,3)(6, 3) in place of 2/32/3.
  • Dividing by the kernel size. If line L7 divides by the number of taps in place of wsum, a weighted kernel returns a value outside [0,1][0, 1].
  • No half texel shift. If line L8 takes ⌊x⌋\lfloor x \rfloor in place of ⌊x−1/2⌋\lfloor x - 1/2 \rfloor, the four taps are not the four nearest centers, and the shadow moves by half a texel.
  • A constant bias with a large kernel. In line L4 the same z−bz - b is compared at taps several texels away, where a sloped receiver has a different depth, so a wide kernel on a sloped floor shadows the floor itself (acne) unless bb grows with the kernel.
  • Clamp at the border. Treating taps outside the map as the nearest edge texel in place of lit (line L4) stretches a shadow at the map border across the scene.

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.

percentage_closer_filtering.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/percentage-closer-filtering/. The authors are listed in prov/CF3C6M.jsonl.

"""Percentage-closer filtering (Reeves, Salesin and Cook 1987).

A shadow map s is a list of h rows of w depths, s[j][i], with i the column
(x to the right) and j the row (y down). Texel (i, j) has its center at
(i + 1/2, j + 1/2) in texel units. Depth is in [0, 1] (the Direct3D clip
range), smaller is nearer to the light. A tap is lit when z - b <= s[j][i];
a tap outside the map counts as lit. Every function returns the lit fraction.
With int or Fraction input the result is exact.
"""

from fractions import Fraction
from math import floor


def lit_tap(s, i, j, z, b):
    """1 if the tap (i, j) is lit for the receiver depth z and the bias b, else 0."""
    if j < 0 or j >= len(s) or i < 0 or i >= len(s[0]):
        return 1
    return 1 if z - b <= s[j][i] else 0


def pcf(s, x, y, z, b, kernel):
    """Weighted PCF. kernel maps the offset (u, v) to a weight >= 0."""
    i, j = floor(x), floor(y)  # @line L1
    acc, wsum = 0, 0  # @line L2
    for (u, v), wt in kernel.items():  # @line L3
        if wt <= 0:
            continue
        t = lit_tap(s, i + u, j + v, z, b)  # @line L4
        acc += wt * t  # @line L5
        wsum += wt  # @line L6
    return Fraction(acc) / wsum if isinstance(acc, int) else acc / wsum  # @line L7


def box_kernel(r):
    """The (2r + 1) x (2r + 1) box with weight 1 per tap."""
    return {(u, v): 1 for u in range(-r, r + 1) for v in range(-r, r + 1)}


def tent_kernel(r):
    """The separable tent with weights (r + 1 - |u|) (r + 1 - |v|)."""
    return {(u, v): (r + 1 - abs(u)) * (r + 1 - abs(v))
            for u in range(-r, r + 1) for v in range(-r, r + 1)}


def pcf_bilinear(s, x, y, z, b):
    """The 2 x 2 bilinear PCF of graphics hardware."""
    fx, fy = x - Fraction(1, 2), y - Fraction(1, 2)
    i, j = floor(fx), floor(fy)  # @line L8
    f, g = fx - i, fy - j
    c00 = lit_tap(s, i, j, z, b)  # @line L9
    c10 = lit_tap(s, i + 1, j, z, b)
    c01 = lit_tap(s, i, j + 1, z, b)
    c11 = lit_tap(s, i + 1, j + 1, z, b)
    return ((1 - f) * (1 - g) * c00 + f * (1 - g) * c10  # @line L10
            + (1 - f) * g * c01 + f * g * c11)


def filter_depths_then_compare(s, x, y, z, b, r):
    """The wrong order: average the depths in the box, then compare once.

    It returns 0 or 1, so it does not soften the edge. It is here for the
    tests and the page, not as a method to use.
    """
    i, j = floor(x), floor(y)
    taps = [s[j + v][i + u] for u in range(-r, r + 1) for v in range(-r, r + 1)
            if 0 <= j + v < len(s) and 0 <= i + u < len(s[0])]
    mean = Fraction(sum(Fraction(t) for t in taps)) / len(taps)
    return 1 if z - b <= mean else 0


def box_scene(w, h, floor_depth, box_depth, x0, y0, x1, y1):
    """A light-space depth map: a floor at floor_depth and the top of a box
    at box_depth over the texels with x0 <= i < x1 and y0 <= j < y1."""
    return [[box_depth if x0 <= i < x1 and y0 <= j < y1 else floor_depth
             for i in range(w)] for j in range(h)]
test_percentage_closer_filtering.py 143 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/percentage-closer-filtering/. The authors are listed in prov/CF3C6M.jsonl.

import random
from fractions import Fraction

from percentage_closer_filtering import (
    box_kernel,
    box_scene,
    filter_depths_then_compare,
    lit_tap,
    pcf,
    pcf_bilinear,
    tent_kernel,
)

HALF = Fraction(1, 2)


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


def summed_area(img):
    h, w = len(img), len(img[0])
    t = [[0] * (w + 1) for _ in range(h + 1)]
    for j in range(h):
        row = 0
        for i in range(w):
            row += img[j][i]
            t[j + 1][i + 1] = t[j][i + 1] + row
    return t


def test_example_on_the_page():
    s = box_scene(8, 8, Fraction(8, 10), Fraction(3, 10), 2, 2, 6, 6)
    z = Fraction(8, 10)
    # Just outside the box edge, one column of the 3 x 3 box is shadowed.
    assert pcf(s, Fraction(13, 2), Fraction(7, 2), z, 0, box_kernel(1)) == Fraction(2, 3)
    # Diagonally outside the box corner, 1 of 9 taps is shadowed.
    assert pcf(s, Fraction(13, 2), Fraction(13, 2), z, 0, box_kernel(1)) == Fraction(8, 9)
    assert pcf(s, Fraction(11, 2), Fraction(11, 2), z, 0, box_kernel(1)) == Fraction(5, 9)
    assert pcf(s, Fraction(11, 2), Fraction(7, 2), z, 0, box_kernel(1)) == Fraction(1, 3)
    # Filtering the depths first gives a hard 0 or 1.
    assert filter_depths_then_compare(s, Fraction(13, 2), Fraction(7, 2), z, 0, 1) == 0
    assert filter_depths_then_compare(s, Fraction(11, 2), Fraction(7, 2), z, 0, 1) == 0


def test_box_pcf_equals_summed_area_count():
    rng = random.Random(1)
    for _ in range(40):
        w, h = rng.randrange(4, 13), rng.randrange(4, 13)
        s = random_map(rng, w, h)
        z = Fraction(rng.randrange(0, 17), 16)
        b = Fraction(rng.randrange(0, 3), 32)
        lit = [[1 if z - b <= s[j][i] else 0 for i in range(w)] for j in range(h)]
        t = summed_area(lit)
        for r in range(3):
            for _ in range(10):
                i, j = rng.randrange(w), rng.randrange(h)
                x0, x1 = max(i - r, 0), min(i + r + 1, w)
                y0, y1 = max(j - r, 0), min(j + r + 1, h)
                inside = t[y1][x1] - t[y0][x1] - t[y1][x0] + t[y0][x0]
                outside = (2 * r + 1) ** 2 - (x1 - x0) * (y1 - y0)
                want = Fraction(inside + outside, (2 * r + 1) ** 2)
                assert pcf(s, i + HALF, j + HALF, z, b, box_kernel(r)) == want


def test_weighted_pcf_equals_brute_force_average():
    rng = random.Random(2)
    for _ in range(200):
        w, h = rng.randrange(3, 10), rng.randrange(3, 10)
        s = random_map(rng, w, h)
        z = Fraction(rng.randrange(0, 17), 16)
        x = Fraction(rng.randrange(0, 16 * w), 16)
        y = Fraction(rng.randrange(0, 16 * h), 16)
        r = rng.randrange(0, 3)
        k = tent_kernel(r)
        num = den = 0
        for (u, v), wt in k.items():
            i, j = int(x) + u, int(y) + v
            inside = 0 <= i < w and 0 <= j < h
            num += wt * (1 if not inside or z <= s[j][i] else 0)
            den += wt
        assert pcf(s, x, y, z, 0, k) == Fraction(num, den)


def test_radius_zero_is_the_binary_test():
    rng = random.Random(3)
    for _ in range(300):
        s = random_map(rng, 6, 5)
        z = Fraction(rng.randrange(0, 17), 16)
        x, y = Fraction(rng.randrange(0, 96), 16), Fraction(rng.randrange(0, 80), 16)
        want = 0 if z > s[int(y)][int(x)] else 1
        assert pcf(s, x, y, z, 0, box_kernel(0)) == want


def test_range_and_monotone_in_depth():
    rng = random.Random(4)
    for _ in range(60):
        s = random_map(rng, 8, 8)
        x, y = Fraction(rng.randrange(0, 128), 16), Fraction(rng.randrange(0, 128), 16)
        prev = None
        for zz in range(0, 18):
            z = Fraction(zz, 16)
            v = pcf(s, x, y, z, 0, box_kernel(2))
            assert 0 <= v <= 1
            if prev is not None:
                assert v <= prev
            prev = v
        assert prev == 0 or (x < 2 or y < 2 or x >= 6 or y >= 6)


def test_bilinear_pcf_is_the_box_integral_of_the_nearest_texel_test():
    rng = random.Random(5)
    n = 16
    for _ in range(80):
        s = random_map(rng, 6, 6)
        z = Fraction(rng.randrange(0, 17), 16)
        x = Fraction(rng.randrange(16, 80), 16)
        y = Fraction(rng.randrange(16, 80), 16)
        total = 0
        for a in range(n):
            for c in range(n):
                px = x - HALF + Fraction(2 * a + 1, 2 * n)
                py = y - HALF + Fraction(2 * c + 1, 2 * n)
                total += lit_tap(s, int(px), int(py), z, 0)
        assert pcf_bilinear(s, x, y, z, 0) == Fraction(total, n * n)


def test_bilinear_pcf_at_texel_centers_is_the_binary_test():
    rng = random.Random(6)
    s = random_map(rng, 7, 7)
    for j in range(7):
        for i in range(7):
            z = Fraction(rng.randrange(0, 17), 16)
            assert pcf_bilinear(s, i + HALF, j + HALF, z, 0) == lit_tap(s, i, j, z, 0)


def test_bias_moves_the_test():
    s = [[Fraction(1, 2)]]
    assert pcf(s, HALF, HALF, Fraction(9, 16), 0, box_kernel(0)) == 0
    assert pcf(s, HALF, HALF, Fraction(9, 16), Fraction(1, 16), box_kernel(0)) == 1

History

Reeves, Salesin and Cook described percentage-closer filtering in 1987, at Pixar, for shadows in film rendering. Their point was that the depth map cannot be filtered like a color texture, because the result of interest is the outcome of a compare. They filtered the binary results over a region of the map with stochastic samples. Depth textures with a compare mode and bilinear filtering of the results came to graphics hardware later and made the 2×22 \times 2 form a standard part of real-time shadows.

References

  1. William T. Reeves, David H. Salesin and Robert L. Cook, Rendering antialiased shadows with depth maps, Computer Graphics 21(4), SIGGRAPH 1987, pages 283 to 291, the sections on percentage-closer filtering (compare first, then filter) and on the bias
  2. Tomas Akenine-Möller, Eric Haines, Naty Hoffman, Angelo Pesce, Michał Iwanicki and Sébastien Hillaire, Real-Time Rendering, fourth edition, CRC Press, 2018, Section 7.5 (percentage-closer filtering and its kernels)

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
  • at least three sources
  • a scene preset for the example