π± π, πβ² = π(π, πβ²) π π, πβ² +
π»
π π, πβ², πβ²β² π± πβ², πβ²β² ππβ²β²
INFOMAGR β Advanced Graphics
Jacco Bikker - November 2016 - February 2017
Lecture 8 - Variance Reduction Welcome! , = (, ) - - PowerPoint PPT Presentation
INFOMAGR Advanced Graphics Jacco Bikker - November 2016 - February 2017 Lecture 8 - Variance Reduction Welcome! , = (, ) , + , , ,
π± π, πβ² = π(π, πβ²) π π, πβ² +
π»
π π, πβ², πβ²β² π± πβ², πβ²β² ππβ²β²
Jacco Bikker - November 2016 - February 2017
Advanced Graphics β Variance Reduction 3
Previously in Advanced Graphics
Advanced Graphics β Variance Reduction 4
Advanced Graphics β Variance Reduction 5
Today in Advanced Graphics:
Aim:
Requirement:
*: If time permits
Advanced Graphics β Variance Reduction 7
Uniform Random Sampling
To sample a light source, we draw two random values in the range 0..1. The resulting 2D positions are not uniformly distributed
We can improve uniformity using stratification:
Advanced Graphics β Variance Reduction 8
Uniform Random Sampling
To sample a light source, we draw two random values in the range 0..1. The resulting 2D positions are not uniformly distributed
We can improve uniformity using stratification:
For 4x4 strata:
stratum_x = (idx % 4) * 0.25 // idx = 0..15 stratum_y = (idx / 4) * 0.25 r0 = Rand() * 0.25 r1 = Rand() * 0.25 P = vec2( stratum_x + r0, stratum_y + r1 )
Advanced Graphics β Variance Reduction 9
Advanced Graphics β Variance Reduction 10
Use Cases
Stratification can be applied to any Monte Carlo process:
However, there are problems:
correlation of the samples, unless we stratify the 4D space - which leads to a very large number of strata: the curse of dimensionality.
Advanced Graphics β Variance Reduction 11
Alleviating the Curse of Dimensionality
Imagine we have 2x2 strata for the lens, and 2x2 for area lights. We can sample this with 4 samples without correlation by randomly combining strata. In practice:
stratification;
from the array of lens samples;
stratum from the array of area light samples.
Advanced Graphics β Variance Reduction 12
Alleviating the Curse of Dimensionality
Imagine we have 2x2 strata for the lens, and 2x2 for area lights. We can sample this with 4 samples without correlation by randomly combining strata. Even more practical:
random[N][M][2], where π is the number of strata and π is the number of dimensions / 2.
random number.
Advanced Graphics β Variance Reduction 13
Troubleshooting Path Tracing Experiments
When experimenting with stratification and other variance reduction methods you will frequently produce incorrect images. Tip: Keep a simple reference path tracer without any tricks. Compare your output to this reference solution frequently.
Advanced Graphics β Variance Reduction 15 Also recall that we had two ways to sample direct illumination:
randomly sampling the hemisphere directly sampling the lights
Can we apply this to the full rendering equation, instead of just direct illumination?
Next Event Estimation
Recall the rendering equation: β¦and the way we sampled it using Monte Carlo:
Vector3 L = RandomPointOnLight() - I; float dist = L.Length(); L /= dist; float cos_o = Dot( -L, lightNormal ); float cos_i = Dot( L, ray.N ); if (cos_o <= 0 || cos_i <= 0) return BLACK; // trace shadow ray Ray r = new Ray(β¦); Scene.Intersect( r ); if (r.objIdx != -1) return BLACK; // V(p,pβ)=1; calculate transport Vector3 BRDF = material.diffuse * INVPI; float solidAngle = β¦; return BRDF * lightColor * solidAngle * cos_i;
Advanced Graphics β Variance Reduction 16
Next Event Estimation
Light travelling via any vertex on the path consists of indirect light and direct light for that vertex. Next Event Estimation: sampling direct and indirect separately.
Advanced Graphics β Variance Reduction 17
Next Event Estimation
Light travelling via any vertex on the path consists of indirect light and direct light for that vertex. Next Event Estimation: sampling direct and indirect separately. Mathematically: = ππ π¦, ππ +
π»
π
π π¦, ππ, ππ ππ π¦, ππ cos ππ πππ
+
π=1 πππβπ’π‘ π΅
π
π π‘ β π¦ β π¦β² ππ π π¦ β π¦β²
π΅ππ
π cos ππ cos ππ
β₯ π¦ β π¦β² β₯2 πππ Problem: we are now sampling lights twice. Solution 1: scale by 0.5. Solution 2: ignore direct light when using 1 .
1 2
Advanced Graphics β Variance Reduction 18
Next Event Estimation
Per surface interaction, we trace two rays.
π¦ π§ A B C D
Advanced Graphics β Variance Reduction 19
Next Event Estimation
When a ray for indirect illumination stumbles upon a light, the path is terminated and no energy is transported via ray D: This way, we prevent accounting for direct illumination on point π§ twice. π¦ π§ A B C D
Advanced Graphics β Variance Reduction 20
Next Event Estimation
Alternative interpretation: We split the hemisphere into two distinct areas:
source on it;
We can now safely send a ray to each of these areas and sum whatever we find there.
(or: we integrate over these non-overlapping areas and sum the energy we receive via both to determine the energy we receive over the entire hemisphere)
π¦
Area 1: Send a ray directly to a random light
than the targeted light. Area 2: Send a ray in a random direction on the
source.
Advanced Graphics β Variance Reduction 21
Next Event Estimation
Color Sample( Ray ray ) { // trace ray I, N, material = Trace( ray ); BRDF = material.albedo / PI; // terminate if ray left the scene if (ray.NOHIT) return BLACK; // terminate if we hit a light source if (material.isLight) return BLACK; // sample a random light source L, Nl, dist, A = RandomPointOnLight(); Ray lr( I, L, dist ); if (NβL > 0 && Nlβ-L > 0) if (!Trace( lr )) { solidAngle = ((Nlβ-L) * A) / dist2; Ld = lightColor * solidAngle * BRDF * NβL; } // continue random walk R = DiffuseReflection( N ); Ray r( I, R ); Ei = Sample( r ) * (NβR); return PI * 2.0f * BRDF * Ei + Ld; }
Advanced Graphics β Variance Reduction 22
Advanced Graphics β Variance Reduction 23
Advanced Graphics β Variance Reduction 24
Advanced Graphics β Variance Reduction 25
Next Event Estimation
Some vertices require special attention:
be reflected to the camera.
Since a light ray doesnβt make sense for specular vertices, we will include emission from a vertex directly following a specular vertex. The same goes for the first vertex after the camera: if this is emissive, we will also include this. This means we need to keep track of the type of the previous vertex during the random walk.
Advanced Graphics β Variance Reduction 26
Color Sample( Ray ray, bool lastSpecular ) { // trace ray I, N, material = Trace( ray ); BRDF = material.albedo / PI; // terminate if ray left the scene if (ray.NOHIT) return BLACK; // terminate if we hit a light source if (material.isLight) if (lastSpecular) return material.emissive; else return BLACK; // sample a random light source L, Nl, dist, A = RandomPointOnLight(); Ray lr( I, L, dist ); if (NβL > 0 && Nlβ-L > 0) if (!Trace( lr )) { solidAngle = ((Nlβ-L) * A) / dist2; Ld = lightColor * solidAngle * BRDF * NβL; } // continue random walk R = DiffuseReflection( N ); Ray r( I, R ); Ei = Sample( r, false ) * (NβR); return PI * 2.0f * BRDF * Ei + Ld; }
Advanced Graphics β Variance Reduction 28
Importance Sampling for Monte Carlo
Monte Carlo integration: π
π΅ = π΅ πΆ
π(π¦) ππ¦ =
π΅ πΆ
ππ¦ πΉ π π¦ β πΆ β π΅ π
π=1 π
π xπ Example 1: rolling two dice πΈ1 and πΈ2, the outcome is 6πΈ1 + πΈ2. What is the expected value of this experiment? (average die value is 3.5, so the answer is of course 3.5 * 6 + 3.5 = 24.5) Using Monte Carlo: π = 1 π
π=1 π
π( πΈ1) + π( πΈ2) π₯βππ π: πΈ1, πΈ1β {1,2,3,4,5,6}, π π¦ = 6π¦, π π¦ = π¦
Advanced Graphics β Variance Reduction 29
Importance Sampling for Monte Carlo
Changing the experiment slightly: each sample is one roll of one die. Using Monte Carlo: π = 1 π
π=1 π π(
π, πΈ) 0.5 π₯βππ π: πΈ β {1,2,3,4,5,6}, π β {0,1}, π π, π¦ = 5π + 1 π¦
for( int i = 0; i < 1000; i++ ) { int D1 = IRand( 6 ) + 1; int D2 = IRand( 6 ) + 1; float f = (float)(6 * D1 + D2); total += f; rolls++; } for( int i = 0; i < 2000; i++ ) { int D = IRand( 6 ) + 1; int n = IRand( 2 ); float f = (float)((5 * n + 1) * D) / 0.5f; total += f; rolls++; }
0.5: Probability of using die π.
Advanced Graphics β Variance Reduction 30
Importance Sampling for Monte Carlo
What happens when we donβt pick each die with the same probability?
for( int i = 0; i < 1000; i++ ) { int D = IRand( 6 ) + 1; float r = Rand(); // uniform 0..1 int n = (r < 0.8f) ? 0 : 1; float p = (n == 0) ? 0.8f : 0.2f; float f = (float)((5 * n + 1) * D) / p; total += f; rolls++; }
Advanced Graphics β Variance Reduction 31
Importance Sampling for Monte Carlo
Example 2: sampling two area lights. Sampling the large light with a greater probability yields a better estimate.
Advanced Graphics β Variance Reduction 32
Importance Sampling for Monte Carlo
Example 3: sampling an integral. Considering the previous experiments, which stratum should be sample more often? 1 2
Advanced Graphics β Variance Reduction 33
Importance Sampling for Monte Carlo
Example 3: sampling an integral. Considering the previous experiments, which stratum should be sample more often? 1 2 3 4
Advanced Graphics β Variance Reduction 34
Importance Sampling for Monte Carlo
Example 3: sampling an integral. Considering the previous experiments, which stratum should be sample more often? 1 2 3 4 5 6 7 8
When using 8 strata and a uniform random distribution, each stratum will be sampled with a 0.125 probability. When using 8 strata and a non-uniform sampling scheme, the sum of the sampling probabilities must be 1. Good sampling probabilities are obtained by simply following the function weβre
We donβt have to use these probabilities; any set of non-zero probabilities will work, but with greater variance. This includes any approximation of the function weβre sampling, whether this approximation is good or not.
Advanced Graphics β Variance Reduction 35
Importance Sampling for Monte Carlo
Example 3: sampling an integral. Considering the previous experiments, which stratum should be sample more often?
If we go from 8 to infinite strata, the probability of sampling a stratum becomes 0. This is where we introduce the PDF, or probability density function. On a continuous domain, the probability of sampling a specific x is 0. However, we can say that the probability of chosing any x in the domain is 1, and the probability of chosing x in the first half of the domain is 0.5. We get this 0.5 by integrating a function p(x) over the domain [0,0.5). Function p(x) is the probability per unit width of sampling a particular x. Function p(x) is the probability density.
Advanced Graphics β Variance Reduction 36
Importance Sampling for Monte Carlo
Example 4: sampling the hemisphere. βπ π
Advanced Graphics β Variance Reduction 37
Importance Sampling for Monte Carlo
Example 4: sampling the hemisphere. βπ π
Advanced Graphics β Variance Reduction 38
Importance Sampling for Monte Carlo
Monte Carlo without importance sampling: πΉ π π¦ β 1 π
π=1 π
π xπ With importance sampling: πΉ π π¦ β 1 π
π=1 π π
xπ π xπ Here, π xπ is the probability density function (PDF).
Advanced Graphics β Variance Reduction 39
Probability Density Function
Properties of a valid PDF π(π¦): 1. π π¦ > 0 for all π¦ β πΈ where π(π¦) β 0 2.
πΈ π π¦ ππ π¦ = 1
Note: π(π¦) is a density, not a probability; it can (and will) exceed 1 for some π¦. Applied to direct light sampling: π π¦ = π· for the part of the hemisphere covered by the light source ο¨ C = 1 / solid angle to ensure π(π¦) integrates to 1 ο¨ Since samples are divided by π(π¦), we multiply by 1/(1/solid angle)):
Advanced Graphics β Variance Reduction 40
Probability Density Function
Applied to hemisphere sampling: Light arriving over the hemisphere is cosine weighted. ο¨ Without further knowledge of the environment, the ideal PDF is the cosine function. ππΈπΊ: π π = cos π Question: how do we normalize this?
π»
πππ‘πππ = π β
π»
πππ‘π π ππ = 1 Question: how do we chose random directions using this PDF?
Advanced Graphics β Variance Reduction 41
Cosine-weighted Random Direction
Without deriving this in detail: A cosine-weighted random distribution is obtained by generating points on the unit disc, and projecting the disc on the unit hemisphere. In code:
float3 CosineWeightedDiffuseReflection() { float r0 = Rand(), r1 = Rand(); float r = sqrt( r0 ); float theta = 2 * PI * r1; float x = r * cosf( theta ); float y = r * sinf( theta ); return float3( x, y, sqrt( 1 β r0 ) ); }
Note: you still have to transform this to tangent space.
Advanced Graphics β Variance Reduction 42
Color Sample( Ray ray ) { // trace ray I, N, material = Trace( ray ); // terminate if ray left the scene if (ray.NOHIT) return BLACK; // terminate if we hit a light source if (material.isLight) return emittance; // continue in random direction R = DiffuseReflection( N ); Ray r( I, R ); // update throughput BRDF = material.albedo / PI; PDF = 1 / (2 * PI); Ei = Sample( r ) * (NβR) / PDF; return BRDF * Ei; } Color Sample( Ray ray ) { // trace ray I, N, material = Trace( ray ); // terminate if ray left the scene if (ray.NOHIT) return BLACK; // terminate if we hit a light source if (material.isLight) return emittance; // continue in random direction R = CosineWeightedDiffuseReflection( N ); Ray r( I, R ); // update throughput BRDF = material.albedo / PI; PDF = (NβR) / PI; Ei = Sample( r ) * (NβR) / PDF; return BRDF * Ei; }
Advanced Graphics β Variance Reduction 44 sampling the lights sampling the hemisphere
specular glossy diffuse
Advanced Graphics β Variance Reduction 45
Multiple Importance Sampling
Light sampling: paths to random points on the light yield high variance. Hemisphere sampling (with importance): random rays yield low variance.
Advanced Graphics β Variance Reduction 46
Multiple Importance Sampling
Light sampling: paths to random points on the light yield low variance. Hemisphere sampling (with importance): random rays yield very high variance.
Advanced Graphics β Variance Reduction 47
Multiple Importance Sampling
MIS: combining samples taken using multiple strategies.
πππ κ =
1 π‘ππππ πππππ.
that case, πππ κ = 1
2π.
Since both methods are equivalent (they sample the same direction κ), we can simply average the pdfs: πππ κ = π₯ π1 + π₯ π2, π₯ = 0.5
Advanced Graphics β Variance Reduction 48
Multiple Importance Sampling
MIS: combining samples taken using multiple strategies. Computing optimal weights for multiple importance sampling: π₯π π¦ = ππ( π¦) π1 π¦ + π2( π¦) This is known as the balance heuristic. Note that the balance heuristic simply assigns a greater weight to the pdf with the largest value at π¦.
Advanced Graphics β Variance Reduction 49
Color Sample( Ray ray ) { // trace ray I, N, material = Trace( ray ); BRDF = material.albedo / PI; // terminate if ray left the scene if (ray.NOHIT) return BLACK; // terminate if we hit a light source if (material.isLight) return BLACK; // sample a random light source L, Nl, dist, A = RandomPointOnLight(); Ray lr( I, L, dist ); if (NβL > 0 && Nlβ-L > 0) if (!Trace( lr )) { solidAngle = ((Nlβ-L) * A) / dist2; Ld = lightColor * solidAngle * BRDF * NβL; } // continue random walk R = DiffuseReflection( N ); Ray r( I, R ); Ei = Sample( r ) * (NβR); return PI * 2.0f * BRDF * Ei + Ld; } Color Sample( Ray ray ) { T = ( 1, 1, 1 ), E = ( 0, 0, 0 ); while (1) { I, N, material = Trace( ray ); BRDF = material.albedo / PI; if (ray.NOHIT) break; if (material.isLight) break; // sample a random light source L, Nl, dist, A = RandomPointOnLight(); Ray lr( I, L, dist ); if (NβL > 0 && Nlβ-L > 0) if (!Trace( lr )) { solidAngle = ((Nlβ-L) * A) / dist2; lightPDF = 1 / solidAngle; E += T * (NβL / lightPDF) * BRDF * lightColor; } // continue random walk R = DiffuseReflection( N ); hemiPDF = 1 / (PI * 2.0f); ray = Ray( I, R ); T *= ((NβR) / hemiPDF) * BRDF; } return E; }
Advanced Graphics β Variance Reduction 50
Multiple Importance Sampling
Earlier, we separated direct and indirect illumination:
πππ
πππβπ’ π¦ = 1/π‘πππππππππ, where π¦ is a direction towards the light.
πππ
ππ ππ π¦ = 1/2π, where π¦ is a uniform random direction on the hemisphere, or
πππ
ππ ππ π¦ = (π β π)/π, where π¦ is a random direction using a cosine weighted distribution.
When sampling the light, we can calculate πππ
ππ ππ π¦ for that same direction.
Likewise, when we encounter a light via the BRDF pdf, we can calculate πππ
πππβπ’(π¦).
Using these quantities, we can now balance the pdfs.
Advanced Graphics β Variance Reduction 51
Multiple Importance Sampling
When sampling the light, we can calculate πππ
ππ ππ π¦ for that same direction.
Likewise, when we encounter a light via the BRDF pdf, we can calculate πππ
πππβπ’(π¦).
Example: next event estimation:
L, Nl, dist, A = RandomPointOnLight(); Ray lr( I, L, dist ); if (NβL > 0 && Nlβ-L > 0) if (!Trace( lr )) { solidAngle = ((Nlβ-L) * A) / dist2; lightPDF = 1 / solidAngle; brdfPDF = 1 / (2 * PI); // or: (NβL) / PI misPDF = lightPDF + brdfPDF; E += T * (NβL / misPDF) * BRDF * lightColor; }
Note: PBR (and many others) uses a different formulation, using weights. This is the same as what we are doing here. PBR says: we sample function π using 2 random directions π¦ and π§, each picked from a pdf. Then: πΉ =
π π¦ π₯ππ ππ πππ ππ(π¦) + π π§ π₯πππβπ’ ππππβπ’(π§) , where π₯ππ ππ + π₯πππβπ’ = 1 to compensate for
the fact that we are now taking two samples. Using the balance heuristic, π₯ππ ππ =
πππ ππ(π¦) πππ ππ(π¦)+ππππβπ’(π§) and π₯πππβπ’ = ππππβπ’(π§) πππ ππ(π¦)+ππππβπ’(π§).
Then, πΉ =
π π¦
πππ ππ(π¦) πππ ππ(π¦)+ππππβπ’(π§)
πππ ππ(π¦)
+
π π§
ππππβπ’(π§) πππ ππ(π¦)+ππππβπ’(π§)
ππππβπ’(π§)
; this can be reduced to πΉ = π π¦
1 πππ ππ(π¦)+ππππβπ’(π§) + π π§ 1 πππ ππ(π¦)+ππππβπ’(π§).
Sampling π(π¦) alone (when stumbling upon a light) thus yields π π¦ /(πππ ππ(π¦) + ππππβπ’(π§)); sampling π(π§) alone (next event estimation) yields π(π¦)/(πππ ππ(π¦) + ππππβπ’(π§)).
Advanced Graphics β Variance Reduction 52
Advanced Graphics β Variance Reduction 53
Advanced Graphics β Variance Reduction 54
Jacco Bikker - November 2016 - February 2017
next lecture: βVariousβ