0% found this document useful (0 votes)
10 views

07+ +Path+Tracing

Uploaded by

Benoni Tang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

07+ +Path+Tracing

Uploaded by

Benoni Tang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 113

Path Tracing

Christoph Garth
Scientific Visualization Lab
Motivation
Reminder: Rendering Equation

Z
L(x, ωr ) = Le (x, ωr ) + fr (x, ωi , ωr ) · Li (x, ωi ) · cos(θi ) dωi

Computer Graphics – Path Tracing– Motivation 7–1


Reminder: Rendering Equation

Z
L(x, ωr ) = Le (x, ωr ) + fr (x, ωi , ωr ) · Li (x, ωi ) · cos(θi ) dωi

Whitted ray tracing has avoided or very roughly approximated the actual
integration by simplifying assumptions:

• point light sources, local illumination model, shadow rays

Computer Graphics – Path Tracing– Motivation 7–1


Reminder: Rendering Equation

Z
L(x, ωr ) = Le (x, ωr ) + fr (x, ωi , ωr ) · Li (x, ωi ) · cos(θi ) dωi

Whitted ray tracing has avoided or very roughly approximated the actual
integration by simplifying assumptions:

• point light sources, local illumination model, shadow rays

This chapter: direct approximation of the integral using Monte Carlo integration.

Computer Graphics – Path Tracing– Motivation 7–1


Monte Carlo Integration
Math Recap: Expected Value

The arithmetic mean of a series of measurements corresponds to the expected


value of a random variable.

If X is a discrete random variable taking on values x1 , x2 , . . . then


X
E[X] = xi · Pr(X = xi )
i

is the expected value of X.

Example: number of heads in triple coin toss


1 3 3 1 12 3
E[X] = 0 · +1· +2· +3· = =
8 8 8 8 8 2

Computer Graphics – Path Tracing– Monte Carlo Integration 7–2


Math Recap: Expected Value

If X is a continuous random variable with density p, then


Z ∞
E[X] = x · p(x)dx
−∞

is the expected value of X.

Example: x ∈ (−1, 1), p(x) = 32 x2

3 1 3 1
1   1  1
3 2 3 3 4
Z Z
x dx = x =1 and E[X] = x · x2 dx = x =0
−1 2 2 3 −1 −1 2 8 −1

Computer Graphics – Path Tracing– Monte Carlo Integration 7–3


Math Recap: Law of Large Numbers

If x1 , x2 , . . . is a sequence of indepentently identically distributed (iid) random


variables, then for every ε > 0
N→∞
!
1 X
Pr xi − E[X] ≤ ε → 1 as N→∞
N
i=1

Interpretation: If enough samples are taken, the arithmetic mean of the random
variables converges to the expected value.

Computer Graphics – Path Tracing– Monte Carlo Integration 7–4


Math Recap: Variance and Standard Deviation

The variance provides the average squared distance to the expected value
n
1 X
Var(X) = · (xi − E[X]))2
n
i=1

It describes how strongly the individual samples vary around the expected value.
(The squares are used such that positive and negative variation does not cancel.)

While variance is an average squared distance, the standard deviation is an


average distance (in the units of the original variable):
p
σ(X) = Var(X)

Computer Graphics – Path Tracing– Monte Carlo Integration 7–5


Example: Dart Board (P. Shirley)

Throwing darts is a random experiment, where the


player throws N darts at the board B.
20
5 1
12 18
The PDF p(x, y) is given for a specific player.
9 4

A score function s(x, y) assigns a score to every hit 14 13

point (x, y).


11 6

8 10

16 16

7 2
19 17
3

blind player average player world champion

Computer Graphics – Path Tracing– Monte Carlo Integration 7–6


Average Score

The expected average score (i.e. the expected value of the score function) for a player can
be computed as
Z
scoreavg = E[s(x, y)] = s(x, y) · p(x, y)dA.
(x,y)∈B

In other words, the scores are weighted with the probability of the corresponding
position and integrated.

Alternatively, we could perform an experiment with N throws to estimate the average


score as
N
1 X
hscoreavg i = E[s(x, y)] ≈ s(xi , yi ).
N
i=1

The angle braces denote that hscoreavg i is an estimator of scoreavg .


Computer Graphics – Path Tracing– Monte Carlo Integration 7–7
Monte Carlo Integration

The evaluation of definite integrals using random numbers is called Monte Carlo
integration.

Idea: Express the value of integral as expected value of a random variable, and
estimate it using samples.

b N
1X
Z
E[g(x)] = g(x)p(x) dx ≈ g(xi )
a N
i=1

The law of large numbers tells us that this estimate of the expected value is
unbiased, i.e. converges to the expected value.

Computer Graphics – Path Tracing– Monte Carlo Integration 7–8


Monte Carlo Integration (cont.)

By rewriting Approach:
• generate random numbers xi in
f (x) = g(x) · p(x)
[a, b] with arbitrary density p
we obtain the actually interesting
• estimate the integral by evaluating
expression
f (xi ) and averaging f (xi )/p(xi )
b N
1 X f (xi )
Z
f (x)dx ≈ • condition: p must be a probability
a N p(xi )
i=1 density function (PDF)
as an estimator of the integral of
f over [a, b].

Computer Graphics – Path Tracing– Monte Carlo Integration 7–9


Illustration: Monte Carlo Integration

0.7

0.6

0.5 f (x)

0.4

0.3

0.2

0.1

0.0
0.0 0.2 0.4 0.6 0.8 1.0

Uniform case (p = const): The area under the integral is approximated using a sum of
rectangles under the function, each of width 1/N and height corresponding to the
function value.
Computer Graphics – Path Tracing– Monte Carlo Integration 7–10
Monte Carlo Integration: Interpretation

The definite integral of f is the


area under the graph of f in the 0.7

interval. 0.6

0.5 f (xi )
Dividing the area into rectangles 0.4
1
of width N·p(x)
1
and height f (xi ) 0.3
N · p(xi )
gives an idea of how the density 0.2

affects the weighting of the 0.1

samples. 0.0
0.0 0.2 0.4 0.6 0.8 1.0

Computer Graphics – Path Tracing– Monte Carlo Integration 7–11


Illustration: Monte Carlo Integration with non-uniform PDF on [0, 1]

0.7 p(x) = 1 0.7 p(x) = 2x 0.7 p(x) ≈ f (x)


0.6 0.6 0.6

0.5 0.5 0.5

0.4 0.4 0.4

0.3 0.3 0.3

0.2 0.2 0.2

0.1 0.1 0.1

0.0 0.0 0.0


0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0

Computer Graphics – Path Tracing– Monte Carlo Integration 7–12


Monte Carlo Integration: Approximation Error

If hIi is a Monte Carlo estimator for the integral I, then hIi is itself a random variable. One
can show that 2
1 b f (x)
Z 
2
σhIi = Var(hIi) = − I p(x) dx.
N a p(x)

Hence, this standard deviation of hIi provides a statistical estimate of the approximation
error in terms of the number of samples.
1
σhIi ∼ √
N

To halve the error, the number of samples must be quadrupled.

Note: hIi is called unbiased since its expected value is the true value of I.

Computer Graphics – Path Tracing– Monte Carlo Integration 7–13


Monte Carlo Integration in the Rendering Equation

How can we apply this to the integral · Li · cos θi dωi ?


R
f
Ω r

A fundamental advantage of MC integration over other quadrature schemes is that it is


easy to adapt to integration domains of arbitrary dimension. All we needed are samples
of the domain.

Samples correspond to directions of incoming light, i.e. points on the hemisphere Ω


above the surface point p where the integral is evaluated.

N

How can we generate such directions?

Computer Graphics – Path Tracing– Monte Carlo Integration 7–14


Hemisphere Sampling: Uniform Case

Since = 2π, uniformly distributed samples have a PDF of p(ω) = 2π .


1
R
Ω dω

Let r1 and r2 be two uniformly distributed random numbers over [0, 1]. One can
show that with polar coordinates φ = 2πr1 and θ = arccos r2 ,
q
cx = cos φ sin θ = cos(2πr1 ) · 1 − (1 − r2 )2
q
cy = sin φ sin θ = sin(2πr1 ) · 1 − (1 − r2 )2
cz = cos θ = 1 − r2

is a uniformly distributed point (cx , cy , cz ) on the unit hemisphere above the


xy-plane (“local coordinates”).

Computer Graphics – Path Tracing– Monte Carlo Integration 7–15


Hemisphere Sampling: Uniform Case

The hemisphere must be rotated such that the z-axis is aligned with the surface
normal N.

Idea: determine a suitable coordinate frame / local basis with z = N.

Let z = N and choose an arbitrary normalized vector h such that h ∦ N, and


compute
x = h × N, y = N × x

Then
ω = cx x + cy y + cz N

is a uniformly distributed random point on Ω.

Computer Graphics – Path Tracing– Monte Carlo Integration 7–16


Pseudocode (→ homework): slightly simpler approach

vec3 uniformHemisphereDirection( vec3 normal )


{
float u = random();
float v = random();
float z = 1.0 - 2.0 * u;
float r = sqrt(1.0 - z * z);
float angle = TWO_PI * v;

vec3 d = vec3(r * cos(angle), r * sin(angle), z);

return dot( d, normal ) < 0.0 ? -d : d;


}

Generate uniform direction on sphere, reverse if below surface.

Computer Graphics – Path Tracing– Monte Carlo Integration 7–17


———————

Monte Carlo Ray Tracing

Computer Graphics – Path Tracing– Monte Carlo Ray Tracing


Reminder: Whitted (classical) Ray Tracing

Diffuse hits termine the ray, local


light model is evaluated after
shadow ray test.

Reflected / refracted rays are

mirror
traced recursively. glass

Limited global illumination (only


shadows) and only point light diffuse surface
sources.

Computer Graphics – Path Tracing– Monte Carlo Ray Tracing 7–18


Monte Carlo Ray Tracing (or distribution ray tracing):

At diffuse hits, approximate RE emitter

integral by sampling, i.e. recursively


trace random secondary rays and
average contributions.

mirror
glass

diffuse surface

Computer Graphics – Path Tracing– Monte Carlo Ray Tracing 7–19


Monte Carlo Ray Tracing (or distribution ray tracing):

At diffuse hits, approximate RE emitter

integral by sampling, i.e. recursively


trace random secondary rays and
average contributions.

mirror
glass

diffuse surface

Computer Graphics – Path Tracing– Monte Carlo Ray Tracing 7–19


Monte Carlo Ray Tracing (or distribution ray tracing):

At diffuse hits, approximate RE emitter

integral by sampling, i.e. recursively


trace random secondary rays and
average contributions.

mirror
Reflected / refracted hits are traced
glass
recursively.

diffuse surface

Computer Graphics – Path Tracing– Monte Carlo Ray Tracing 7–19


Monte Carlo Ray Tracing (or distribution ray tracing):

At diffuse hits, approximate RE emitter

integral by sampling, i.e. recursively


trace random secondary rays and
average contributions.

mirror
Reflected / refracted hits are traced
glass
recursively.

Paths terminate when emitting


objects (emitters) are hit. diffuse surface

Computer Graphics – Path Tracing– Monte Carlo Ray Tracing 7–19


Monte Carlo Ray Tracing (or distribution ray tracing):

At diffuse hits, approximate RE emitter

integral by sampling, i.e. recursively


trace random secondary rays and
average contributions.

mirror
Reflected / refracted hits are traced
glass
recursively.

Paths terminate when emitting


objects (emitters) are hit. diffuse surface

Computer Graphics – Path Tracing– Monte Carlo Ray Tracing 7–19


Monte Carlo Ray Tracing (or distribution ray tracing):

At diffuse hits, approximate RE emitter

integral by sampling, i.e. recursively


trace random secondary rays and
average contributions.

mirror
Reflected / refracted hits are traced
glass
recursively.

Paths terminate when emitting


objects (emitters) are hit. diffuse surface

This allows all light paths L(D|S)∗ E and all global illumination effects.

Computer Graphics – Path Tracing– Monte Carlo Ray Tracing 7–19


Monte Carlo Ray Tracing (cont’d.)

Problem:
Assuming N samples at each diffuse hit, we
have
emitter

• N rays for the first indirection,


• N2 rays for the second indirection,

mirror
• N3 rays for the third indirection,
glass
• ...
The number of rays grows exponentially with
diffuse surface
recursion depth.
Throughput is reduced at each indirection, but
more rays are used per recursion level.

Computer Graphics – Path Tracing– Monte Carlo Ray Tracing 7–20


Path Tracing – Idea
Path Tracing (Kajiya 1986) – Brute Force Approach

Idea: Sampling in Path Space

Trace only one secondary ray per hit, emitter

but sample and average N paths


from the camera per pixel.
At each hit, the path is randomly
continued along a sampled direction.

mirror
glass
At emitters, either emission of
reflection is randomly sampled.
Paths end at maximum recursion
depth, or when leaving the scene. diffuse surface

All indirections require the same effort.

Computer Graphics – Path Tracing– Path Tracing – Idea 7–21


Path Tracing: Emitters

In case the path reaches an object, there are two possibilities:

• the emission term Le is sampled and the path terminates


• the reflectance term Lr is sampled and the path is continued

The algorithm randomly decides which term to use.

Assume emission is sampled with probability pemission . Due to the Monte-Carlo principle
(average f (xi )/p(xi )), to remain unbiased the returned radiance must be multiplied by
pemission . Conversely, if the reflectance is sampled, it must be weighted by 1−pemission .
1 1

The estimator remains unbiased. Light sources are not handled specially, all objects can
be emitters and reflect at the same time.

Computer Graphics – Path Tracing– Path Tracing – Idea 7–22


Path Tracing: Recursion Depth

The principle of local random sampling is very powerful. For example, it provides a much
better way to determine recursion depth than simply setting it to a fixed number.

Insight: All path lengths have equal probability in path space. Terminating paths at a
fixed recursion depth introduces bias, since all longer paths are not sampled at all.

Instead: decide randomly at each interaction whether to terminate the path


Russian Roulette.

Assume the path is terminated with probability pterminate . Then its contribution to the
overall average must be weighted by pterminate
1
. Otherwise, if the path is continued, it is
weighted by 1−pterminate .
1

Computer Graphics – Path Tracing– Path Tracing – Idea 7–23


Pseudocode: Brute Force Path Tracing (diffuse case only)

The algorithm is very straightforward to implement.

computeImage() {
// set all pixels to (0,0,0)
clear(pixels);

for (int sample = 0; sample < numSamples; ++sample) {


for ((x, y) in image) {
// mean value over numSamples samples
pixel(x, y) += pathtrace(x, y) / numSamples;
}
}
}

Computer Graphics – Path Tracing– Path Tracing – Idea 7–24


vec3 pathtrace(int x, int y) {
Ray r = getRayForPixel(x, y);
vec3 weight = vec3(1.0); // path weight

for (int bounce = 0; bounce < maxBounces; ++bounce) {


Hit hit = traceRay(r);

// choose randomly between sampling emitted and reflected light


if (random() < 0.5) {
// sample emission
return weight * emission(hit) / 0.5;
} else {
// sample reflectance
rNext = uniformHemisphereRay(hit.n);
weight *= brdf(hit, r, rNext) * dot(r, rNext) / 0.5 * TWO_PI;
r = rNext;
}
}
return vec3(0,0,0);
}

Computer Graphics – Path Tracing– Path Tracing – Idea 7–25


Example: Brute Force Path Tracing / Cornell Box (spp = samples per pixel)

1 spp 10 spp 100 spp

500 spp 1000 spp 5000 spp


Computer Graphics – Path Tracing– Path Tracing – Idea 7–26
Area Lights and Soft Shadows

Emitters can be arbitrarily large; this enables accurate reproduction of soft shadows.

Computer Graphics – Path Tracing– Path Tracing – Idea 7–27


Anti-Aliasing

Camera sensors (pixels) are not points,


they should not be approximated by a
single sample (ray through pixel center).
Instead, incoming radiance should be
integrated over the pixel area.

Use MC approximation: sample camera


rays not only through pixel center, but
entire pixel area; average results.

Effectively, this provides anti-aliasing.

The additional integral can be directly incorporated in the MC formalism.


Computer Graphics – Path Tracing– Path Tracing – Idea 7–28
Illustration: Anti-Aliasing

sampling only pixel center sampling pixel area

The same approach can be used to incorporate other phenomena (integrals).


Computer Graphics – Path Tracing– Path Tracing – Idea 7–29
Depth of Field

In the ray tracing camera model, we assumed a point-sized aperture. Real-world


apertures are NOT point-sized, creating a depth of field effect: only objects at the focal
distance are in focus.

Large apertures create shallow depth-of-field / smaller range of objects in focus.

The underlying reason is that light reaches the sensor from a small cone of directions
(instead of a single one). Larger aperture larger cone.
Computer Graphics – Path Tracing– Path Tracing – Idea 7–30
Depth of Field

This can be incorporated by integrating over the virtual aperture (cone of directions)
sample directions and average.

image plane focal plane

camera ray
pfocus
optical axis

focal distance

Practically: Sample ray origin in image plane in a circle of radius raperture around pixel
center. All rays must intersect the focal plane at the same point pfocus .
Computer Graphics – Path Tracing– Path Tracing – Idea 7–31
Motion Blur

Typical sensors do not expose instantaenously, but over a short time interval
Integrate radiance contributions over time interval by sampling in time.

Computer Graphics – Path Tracing– Path Tracing – Idea 7–32


Discussion: Brute Force Path Tracing

• simple and very general (scene, reflection model, etc.)


• reproduces full global illumnation (in contrast to ray tracing)
• unbiased, but in some cases extremely inefficient
• most random paths may never hit the light (small light sources)
• reflections with minimal contribution are sampled (glossy BRDF)
• unusable in practice due to extremely slow convergence

1
We cannot change the convergence rate of Monte Carlo integration (O(N− 2 )),
however, the variance of the estimator hIi can be reduced.

Computer Graphics – Path Tracing– Path Tracing – Idea 7–33


Illustration: Brute Force Path Tracing

sky light (50 spp) point light (50 spp)

In the left image, many rays hit the sky light, hence, variance is low. In the right image, the
point light is smaller and hit by fewer rays, hence variance is high.

Computer Graphics – Path Tracing– Path Tracing – Idea 7–34


Variance Reduction
Illustration: Brute Force Path Tracing

r = 1.0

specular

random path

glossy BRDF Lambertian BRDF


overall throughput: r = 0.03
r = 0.3 r = 0.1

Naïve selection of path directions (uniform hemisphere samples) leads to many paths
with minimal throughput that never hit an emitter.

Computer Graphics – Path Tracing– Variance Reduction 7–35


How can we systematically sample path with high contribution to the final image, while
remaining unbiased?

Options:
• Variance Reduction
• Importance Sampling
• Stratified Sampling
• ...
• Quasi Monte Carlo: uses deterministic sequences of samples (so-called low
discrepancy sequences)

Remember, only the variance of the estimator is improved, but not the convergence rate.

Let’s revisit sampling in general.

Computer Graphics – Path Tracing– Variance Reduction 7–36


Importance Sampling

N samples are taken from the domain of f with probability p(x), which gives the estimator
N  
1 X f (xi ) 1 f (x)
hIi = with variance Var(hIi) = Var
N p(xi ) N p(x)
i=1

Computer Graphics – Path Tracing– Variance Reduction 7–37


Importance Sampling

N samples are taken from the domain of f with probability p(x), which gives the estimator
N  
1 X f (xi ) 1 f (x)
hIi = with variance Var(hIi) = Var
N p(xi ) N p(x)
i=1

By clever choice of p(x) the variance of hIi can be reduced. (If we could choose
p(x) = f (x)/ f (x) dx, the variance would vanish completely!)
R

Computer Graphics – Path Tracing– Variance Reduction 7–37


Importance Sampling

N samples are taken from the domain of f with probability p(x), which gives the estimator
N  
1 X f (xi ) 1 f (x)
hIi = with variance Var(hIi) = Var
N p(xi ) N p(x)
i=1

By clever choice of p(x) the variance of hIi can be reduced. (If we could choose
p(x) = f (x)/ f (x) dx, the variance would vanish completely!)
R

Principle: Choose p(x) similar to f (x), such that samples can still be generated easily.

Computer Graphics – Path Tracing– Variance Reduction 7–37


Importance Sampling

N samples are taken from the domain of f with probability p(x), which gives the estimator
N  
1 X f (xi ) 1 f (x)
hIi = with variance Var(hIi) = Var
N p(xi ) N p(x)
i=1

By clever choice of p(x) the variance of hIi can be reduced. (If we could choose
p(x) = f (x)/ f (x) dx, the variance would vanish completely!)
R

Principle: Choose p(x) similar to f (x), such that samples can still be generated easily.

Path tracing: locally (at each hit), extend path along high-contribution directions more
often than along ones with low contribution.

Computer Graphics – Path Tracing– Variance Reduction 7–37


Application: Importance Sampling of Reflective Surfaces

Integral over hemisphere from


R
Ω fr (x, ωr , ωi ) · Li (x, ωi ) · cos θi dωi

Sample directions where BRDF is large.

Computer Graphics – Path Tracing– Variance Reduction 7–38


Illustration: If p matches the BRDF, the variance decreases drastically.

uniform
5 spp

importance
5 spp

Computer Graphics – Path Tracing– Variance Reduction 7–39


Illustration: If p matches the BRDF, the variance decreases drastically.

uniform
75 spp

importance
75 spp

Computer Graphics – Path Tracing– Variance Reduction 7–40


How can points or directions be stochastically generated?

• e.g. in non-rectangular areas


• according to any distribution (importance sampling)

Typical distributions:

• Uniform (single or multidimensional)


• Disc or circle
• Hemisphere
• Light source
• Complex lighting (→ environment map)
• Complex BRDFs

Computer Graphics – Path Tracing– Variance Reduction 7–41


Basis: Uniformly distributed random numbers in [0, 1]
Using the Random Number Generator (RNG) of the system:

• random() or drand48() in C and C++


• Math.random() in JavaScript

Important: Different samples must be independent of each other! Otherwise


visual artifacts or bias are to be expected.

Homework (GLSL): unfortunately, there is no RNG in GLSL makeshift


construction "hack" works sufficiently well

Computer Graphics – Path Tracing– Variance Reduction 7–42


Generating random samples from distribution p(x):

• Function Inversion / Inverse Transform Sampling


• Rejection Sampling
• others, e.g. Metropolis Sampling

Probability

Computer Graphics – Path Tracing– Variance Reduction 7–43


Inverse Transform Sampling

Cumulative Distribution Function (CDF)

P(X) = Pr(x < X) 0 P(X)

U
Construction of samples: solve X = P−1 (U),
where U is uniformly distributed over [0, 1].

Needed:
• Integral of p(x) 1
X
• Inverse of P−1 (x)

Computer Graphics – Path Tracing– Variance Reduction 7–44


Illustration: Inverse Transform Sampling

P(X)

p(x)
X

Computer Graphics – Path Tracing– Variance Reduction 7–45


Illustration: Inverse Transform Sampling

P(X)

p(x)
X

Computer Graphics – Path Tracing– Variance Reduction 7–45


Illustration: Inverse Transform Sampling

P(X)

p(x)
X

Computer Graphics – Path Tracing– Variance Reduction 7–45


Illustration: Inverse Transform Sampling

P(X)

p(x)
X

Computer Graphics – Path Tracing– Variance Reduction 7–45


Example: Power Function

Given
p(x) = (n + 1) xn , P(x) = xn+1

Computer Graphics – Path Tracing– Variance Reduction 7–46


Example: Power Function

Given
p(x) = (n + 1) xn , P(x) = xn+1
Compute
1 1
xn+1 1
Z
xn dx = =
0 n+1 0 n+1

Computer Graphics – Path Tracing– Variance Reduction 7–46


Example: Power Function

Given
p(x) = (n + 1) xn , P(x) = xn+1
Compute
1 1
xn+1 1
Z
xn dx = =
0 n+1 0 n+1
Then √
X = P−1 (U) =
n+1
X ∼ p(x) ⇒ U

Computer Graphics – Path Tracing– Variance Reduction 7–46


Example: Power Function

Given
p(x) = (n + 1) xn , P(x) = xn+1
Compute
1 1
xn+1 1
Z
xn dx = =
0 n+1 0 n+1
Then √
X = P−1 (U) =
n+1
X ∼ p(x) ⇒ U


Sample generation: if ξ is uniformly distributed on [0, 1], then n+1
ξ is distributed
according to p(x) = (n + 1)xn .

Computer Graphics – Path Tracing– Variance Reduction 7–46


Example: Power Function

Given
p(x) = (n + 1) xn , P(x) = xn+1
Compute
1 1
xn+1 1
Z
xn dx = =
0 n+1 0 n+1
Then √
X = P−1 (U) =
n+1
X ∼ p(x) ⇒ U


Sample generation: if ξ is uniformly distributed on [0, 1], then n+1
ξ is distributed
according to p(x) = (n + 1)xn .

In the following, ξ1 , ξ2 are independent, uniformly distributed random numbers on [0, 1].

Computer Graphics – Path Tracing– Variance Reduction 7–46


Example: Uniform Point in a Unit Disk

Circle area:
Z 2π Z 1
A = r dr dθ = π
0 0

Computer Graphics – Path Tracing– Variance Reduction 7–47


Example: Uniform Point in a Unit Disk

Circle area:
Z 2π Z 1
A = r dr dθ = π
0 0

Since the PDF is constant w.r.t. area:


r
p(r, θ) = r · p(x, y) ⇒
π

Computer Graphics – Path Tracing– Variance Reduction 7–47


Example: Uniform Point in a Unit Disk

Circle area:
Z 2π Z 1
A = r dr dθ = π
0 0

Since the PDF is constant w.r.t. area:


r
p(r, θ) = r · p(x, y) ⇒
π
Marginal density
(density on circle of radius r):
Z 2π
pr (r) = p(r, θ)dθ = 2r
0

Computer Graphics – Path Tracing– Variance Reduction 7–47


Example: Uniform Point in a Unit Disk

Circle area: Conditional density:


Z 2π Z 1
p(r, θ) 1
A = r dr dθ = π pθ (θ|r) = =
0 0 pr (r) 2π

Since the PDF is constant w.r.t. area:


r
p(r, θ) = r · p(x, y) ⇒
π
Marginal density
(density on circle of radius r):
Z 2π
pr (r) = p(r, θ)dθ = 2r
0

Computer Graphics – Path Tracing– Variance Reduction 7–47


Example: Uniform Point in a Unit Disk

Circle area: Conditional density:


Z 2π Z 1
p(r, θ) 1
A = r dr dθ = π pθ (θ|r) = =
0 0 pr (r) 2π

Since the PDF is constant w.r.t. area: Inversion method:


r Z r
p(r, θ) = r · p(x, y) ⇒
p
π Pr (r) = 2r0 dr0 = r2 ⇒ r = ξ1
0
Marginal density 2π
1
Z
θ
(density on circle of radius r): Pθ (θ|r) = dθ0 = ⇒ θ = 2πξ2
0 2π 2π
Z 2π
pr (r) = p(r, θ)dθ = 2r
0

Computer Graphics – Path Tracing– Variance Reduction 7–47


Example: Uniform Point in a Unit Disk

Circle area: Conditional density:


Z 2π Z 1
p(r, θ) 1
A = r dr dθ = π pθ (θ|r) = =
0 0 pr (r) 2π

Since the PDF is constant w.r.t. area: Inversion method:


r Z r
p(r, θ) = r · p(x, y) ⇒
p
π Pr (r) = 2r0 dr0 = r2 ⇒ r = ξ1
0
Marginal density 2π
1
Z
θ
(density on circle of radius r): Pθ (θ|r) = dθ0 = ⇒ θ = 2πξ2
0 2π 2π
Z 2π
pr (r) = p(r, θ)dθ = 2r
0 Sample generation: generate ξ1 , ξ2 , compute
r, θ from inverse CDFs, transform to
Cartesian coordinates.

Computer Graphics – Path Tracing– Variance Reduction 7–47


p
r = ξ1 r= ξ2

θ = 2πξ2 θ = 2πξ1

Wrong: areas are not equal Correct: equal areas

Computer Graphics – Path Tracing– Variance Reduction 7–48


Rejection Sampling – “dart throwing”

Rewrite a d-dimensionsl integral as a


(d + 1)-dimensional integral
Z 1 ZZ
I= f (x)dx = dxdy
0 y≤f (x)

Algorithm: f (x)
y
• choose u1 and u2 uniformly distributed
• accept sample if u1 < f (u2 )
Accepted samples are distributed as f (x).
x

Efficiency depends on area under f relative to area


of rectangle.

Computer Graphics – Path Tracing– Variance Reduction 7–49


Example: Uniform points in the Unit Disk

do {
x = 1 - 2 * random()
y = 1 - 2 * random()
} while (x*x + y*y > 1)

This approach also works for spheres.

Computer Graphics – Path Tracing– Variance Reduction 7–50


Example: Uniform directions on a hemisphere with normal n:

Option 1: Rejection Sampling

• Create uniform random numbers x, y, z ∈ [−1, 1]


• Reject if x2 + y2 + z2 > 1
• Normalize r = (x, y, z) if n − r < 0, then reverse (r = −r)

Option 2: Function Inversion

• Create ϕ ∈ [0, 2π]


• Create z ∈ [0, 1]
• Let θ = cos−1 z
• Then (x, y, z) = (sin θ cos ϕ, sin θ sin ϕ, cos θ)

Computer Graphics – Path Tracing– Variance Reduction 7–51


Pseudo code for Option 3:

vec3 uniformlyRandomDirection() {
float u = random();
float v = random();
float z = 1.0 - 2.0 * u;
float r = sqrt(1.0 - z * z);
float angle = 2.0 * PI * v;
return vec3(r * cos(angle), r * sin(angle), z);
}

vec3 uniformlyRandomHemisphereDirection(vec3 normal) {


vec3 d = uniformlyRandomDirection();
return dot(d, normal) < 0.0 : -d : d;
}

Computer Graphics – Path Tracing– Variance Reduction 7–52


Importance Sampling of Lambertian BRDF

k k
Z
fr (x, ωi , ωr ) = d ⇒ Lr (x, ωr ) = d Li (x, ωi )cos θi dωi
π π Ω

Computer Graphics – Path Tracing– Variance Reduction 7–53


Importance Sampling of Lambertian BRDF

k k
Z
fr (x, ωi , ωr ) = d ⇒ Lr (x, ωr ) = d Li (x, ωi )cos θi dωi
π π Ω

Importance sampling: choose p ∝ fr · cos θi ⇒ p(θ, φ) = π .


cos θ

Computer Graphics – Path Tracing– Variance Reduction 7–53


Importance Sampling of Lambertian BRDF

k k
Z
fr (x, ωi , ωr ) = d ⇒ Lr (x, ωr ) = d Li (x, ωi )cos θi dωi
π π Ω

Importance sampling: choose p ∝ fr · cos θi ⇒ p(θ, φ) = π .


cos θ

Inverse CDF gives: p


θ = arcsin ξ1 , φ = 2πξ2

Computer Graphics – Path Tracing– Variance Reduction 7–53


Importance Sampling of Lambertian BRDF

k k
Z
fr (x, ωi , ωr ) = d ⇒ Lr (x, ωr ) = d Li (x, ωi )cos θi dωi
π π Ω

Importance sampling: choose p ∝ fr · cos θi ⇒ p(θ, φ) = π .


cos θ

Inverse CDF gives: p


θ = arcsin ξ1 , φ = 2πξ2

Convert (θ, φ) to local Cartesian coordinates and then to local orientation (cf. slide 16).

If samples are chosen in this way, the local estimator sample becomes

Lr (x, ωr ) = kd Li (x, ωi ).

Computer Graphics – Path Tracing– Variance Reduction 7–53


Importance Sampling of Lambertian BRDF

k k
Z
fr (x, ωi , ωr ) = d ⇒ Lr (x, ωr ) = d Li (x, ωi )cos θi dωi
π π Ω

Importance sampling: choose p ∝ fr · cos θi ⇒ p(θ, φ) = π .


cos θ

Inverse CDF gives: p


θ = arcsin ξ1 , φ = 2πξ2

Convert (θ, φ) to local Cartesian coordinates and then to local orientation (cf. slide 16).

If samples are chosen in this way, the local estimator sample becomes

Lr (x, ωr ) = kd Li (x, ωi ).

This is called cosine-weighted hemisphere sampling.

Computer Graphics – Path Tracing– Variance Reduction 7–53


Illustration: Cosine-weighted hemisphere sampling.

circumference


θ = arcsin ξ1 cosine-weighted cosine-weighted uniform
(top view) (side view) (top view)

Projected to the unit disk, the cosine-weighted hemisphere points are uniformly
distributed.

Computer Graphics – Path Tracing– Variance Reduction 7–54


Importance Sampling of Phong BRDF
n+2
fr (x, ωi , ωr ) = ks cosn α

Choose p ∝ fr ⇒ p(α, φ) = n+1


2π cosn α

Inverse CDF gives:


1
α = arccos(1 − r1 ) n+1 , φ = 2πr2
Convert (α, φ) to local Cartesian coordinates and then to local orientation (cf. slide 16).

With the chosen density, the local estimator becomes


n+2
Lr (x, ωi , ωr ) = ks L (x, ωi ) cos θi
n+1 i

More details: Philip Dutré’s Global Illumination Compendium and the Sampling Transformation Zoo.

Computer Graphics – Path Tracing– Variance Reduction 7–55


Combined materials:

Materials can combine diffuse reflection, glossy reflection, and transmission.

Computer Graphics – Path Tracing– Variance Reduction 7–56


Combined materials:

Materials can combine diffuse reflection, glossy reflection, and transmission.

Importance sampling combined PDF is too complex.


Only one direction can be selected to continue path.
Randomly sample one of the three terms.

Computer Graphics – Path Tracing– Variance Reduction 7–56


Combined materials:

Materials can combine diffuse reflection, glossy reflection, and transmission.

Importance sampling combined PDF is too complex. kd =70%


ks =20%
Only one direction can be selected to continue path.
Randomly sample one of the three terms.

Example: kd = 0.7, ks = 0.2, kt = 0.1. Choose random r. kt =10%

• r ∈ [0, kd ] → sample diffuse BRDF

• r ∈ [kd , kd + ks ] → sample glossy BRDF

• r ∈ [kd + ks , kd + ks + kt ] → sample transmission


r
0 1

Computer Graphics – Path Tracing– Variance Reduction 7–56


Combined materials:

Materials can combine diffuse reflection, glossy reflection, and transmission.

Importance sampling combined PDF is too complex. kd =70%


ks =20%
Only one direction can be selected to continue path.
Randomly sample one of the three terms.

Example: kd = 0.7, ks = 0.2, kt = 0.1. Choose random r. kt =10%

• r ∈ [0, kd ] → sample diffuse BRDF

• r ∈ [kd , kd + ks ] → sample glossy BRDF

• r ∈ [kd + ks , kd + ks + kt ] → sample transmission


r
Works as long as kd + ks + kt < 1 (energy conservation). 0 1

Normalize by inverse probability of case.

Computer Graphics – Path Tracing– Variance Reduction 7–56


Example: Importance Sampling of Emission

Instead of sampling Lr and Le with equal probability, decide probability of sampling


emission by amount of emission.

(
0.9 Le (x) > 0
pemission =
0.0 Le (x) == 0

Computer Graphics – Path Tracing– Variance Reduction 7–57


Example: Importance Sampling of Emission

Instead of sampling Lr and Le with equal probability, decide probability of sampling


emission by amount of emission.

(
0.9 Le (x) > 0
pemission =
0.0 Le (x) == 0

Example: Importance Sampling Russian Roulette

Terminate path with probability based on throughput w, e.g.


(
w < wthreshold P
pterminate =
0 else

Computer Graphics – Path Tracing– Variance Reduction 7–57


Stratified Sampling

The integration domain is subdivided into strata (sg. stratum); the integral is
estimated as the sum of per-stratum estimators.

Example: Jittered sampling or Jittering


(anti-aliasing)

Instead of sampling the pixel area uniformly, the


pixel is subdivided by a grid. The same number of
samples are performed for each grid cell. pixel

Benefit: One can show that variance can be reduced. (It does not increase.)

Computer Graphics – Path Tracing– Variance Reduction 7–58


Example: Next Event Estimation

Divide the hemisphere into


• direct illumination

sample point on light source shadow ray


• indirect illumination

sample point on hemisphere, e.g. BRDF


importance sampling
The respective solid angles are sampled
separaterly (randomly decided, normalized by
solid angle).

Note: if the indirect ray hits a light, it has to be discarded.

Computer Graphics – Path Tracing– Variance Reduction 7–59


Illustration: Variance Reduction

BRDF importance sampling

BRDF importance sampling


+ next event estimation

1 spp 10 spp 100 spp

Computer Graphics – Path Tracing– Variance Reduction 7–60


Summary: Paths are selected according to the transported energy (approximately
contribution to the result)

• Many samples for paths with high energy; few paths with low energy

Macro Level
• Selection between emission / reflection
• Direct illumination

Micro Level
• Importance sampling of the BRDF to select beam directions

In principle, paths can be selected arbitrarily, but samples must cover the integration
domain and contributions must then be weighted with 1/probability.

Computer Graphics – Path Tracing– Variance Reduction 7–61


Advanced Path Tracing
Further possible improvments:

• Unbiased
• Bidirectional path tracing
• Metropolis light transport
• Biased
• Noise filtering
• Adaptive sampling
• Irradiance caching

Computer Graphics – Path Tracing– Advanced Path Tracing 7–62


Idea: Bidirectional Path Tracing

Trace eye path and light path and connect path vertices if they can see each other.

l2

l1
e1

l0
e0

Computer Graphics – Path Tracing– Advanced Path Tracing 7–63


Idea: Bidirectional Path Tracing

Trace eye path and light path and connect path vertices if they can see each other.

l2

l1
e1

l0
e0

Computer Graphics – Path Tracing– Advanced Path Tracing 7–63


Idea: Bidirectional Path Tracing

Trace eye path and light path and connect path vertices if they can see each other.

l2

l1
e1

l0
e0

Computer Graphics – Path Tracing– Advanced Path Tracing 7–63


Idea: Bidirectional Path Tracing

Trace eye path and light path and connect path vertices if they can see each other.

l2

l1
e1

l0
e0

Computer Graphics – Path Tracing– Advanced Path Tracing 7–63


Idea: Bidirectional Path Tracing

Trace eye path and light path and connect path vertices if they can see each other.

l2

l1
e1

l0
e0

Computer Graphics – Path Tracing– Advanced Path Tracing 7–63


Idea: Bidirectional Path Tracing

Trace eye path and light path and connect path vertices if they can see each other.

l2

l1
e1

l0
e0

Computer Graphics – Path Tracing– Advanced Path Tracing 7–63


Bidirectional Path Tracing — very briefly:

• Compute path starting from the eye (eye path) with vertices e0 , e1 , · · · , et
• Compute path starting from the light (light paths) with vertices l0 , l1 , · · · , ls
• If path vertices ei and lj can be connected (i.e. can see each other), then a
new path e0 , · · · , ei , lj , · · · , lm is found.
• Altogether there are t + s possible paths, over which intelligent averaging is
done.

better efficiency for caustics, small lights, etc.

Computer Graphics – Path Tracing– Advanced Path Tracing 7–64


Illustration: Bidirection Path Tracing

Computer Graphics – Path Tracing– Advanced Path Tracing 7–65


Illustration: Metropolis Light Transport

Sampling strategy, based on the


Metropolis-Hastings algorithm (a
Markov Chain Monte Carlo method)
for approximating (unknown)
distributions based on samples.

Core Idea: once an important path is


found, explore nearby paths.

Unbiased algorithm.

Computer Graphics – Path Tracing– Advanced Path Tracing 7–67


Illustration: Image Denoising

Use image processing operations to remove


variance noise, e.g. Gaussian smoothing.

Recent (2017): AI Denoising


• ML models for Denoising
• OpenImageDenoise (Intel), OptiX Denoiser unfiltered

(NVIDIA)

Biased.

filtered
Computer Graphics – Path Tracing– Advanced Path Tracing 7–68
Illustration: Adaptive Sampling

Estimate variance and use more samples where variance is large. Biased.

unfiltered filtered

Computer Graphics – Path Tracing– Advanced Path Tracing 7–69


Idea: Irradiance Caching

Pre-compute illumination (light paths) at sparse locations in the scene and interpolate
irradiance on diffuse hits.

Direct illumination (far left), indirect illumination (left), reconstructed image (right), indirect samples (far right).

Biased.

Computer Graphics – Path Tracing– Advanced Path Tracing 7–70


This marks the end of our discussion of global illumination and photorealistic
rendering.

From here on out, we will pivot and start working to lay the foundations for
real-time rendering.

Computer Graphics – Path Tracing– Advanced Path Tracing 7–71

You might also like