π± π, πβ² = π(π, πβ²) π π, πβ² + ΰΆ±
π»
π π, πβ², πβ²β² π± πβ², πβ²β² ππβ²β²
INFOMAGR β Advanced Graphics
Jacco Bikker - November 2019 - February 2020
Lecture 8 ,10- Variance Reduction Welcome! , = (, ) - - PowerPoint PPT Presentation
INFOMAGR Advanced Graphics Jacco Bikker - November 2019 - February 2020 Lecture 8 ,10- Variance Reduction Welcome! , = (, ) , + , , ,
π± π, πβ² = π(π, πβ²) π π, πβ² + ΰΆ±
π»
π π, πβ², πβ²β² π± πβ², πβ²β² ππβ²β²
Jacco Bikker - November 2019 - February 2020
βͺ Introduction βͺ Stratification βͺ Next Event Estimation βͺ Importance Sampling βͺ Resampled Importance
Advanced Graphics β Variance Reduction 3
Previously in Advanced Graphics
Advanced Graphics β Variance Reduction 4
Advanced Graphics β Variance Reduction 5
Today in Advanced Graphics:
βͺ Stratification βͺ Next Event Estimation βͺ Importance Sampling βͺ Multiple Importance Sampling βͺ Resampled Importance Sampling* Aim: βͺ to get a better image with the same number of samples βͺ to increase the efficiency of a path tracer βͺ to reduce variance in the estimate Requirement: βͺ produce the correct image
*: If time permits
βͺ Introduction βͺ Stratification βͺ Next Event Estimation βͺ Importance Sampling βͺ Resampled Importance
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: βͺ Anti-aliasing (sampling the pixel) βͺ Depth of field (sampling the lens) βͺ Motion blur (sampling time) βͺ Soft shadows (sampling area lights) βͺ Diffuse reflections (sampling the hemisphere) However, there are problems: βͺ We need to take one sample per stratum βͺ Stratum count: higher is better, but with diminishing returns βͺ Combining stratification for e.g. depth of field and soft shadows leads to 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
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.
βͺ Introduction βͺ Stratification βͺ Next Event Estimation βͺ Importance Sampling βͺ Resampled Importance
Advanced Graphics β Variance Reduction 13 Also recall that we had two ways to sample direct illumination:
integrating over the hemisphere integrating
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 solidAngle * BRDF * lightColor * cos_i;
Advanced Graphics β Variance Reduction 14 βΒ½Ο +Β½Ο Incoming direct light π¦ = ΰΆ±
π»
ππ π¦, ππ cos ππ πππ β 2π π ΰ·
π=1 π
ππ π, ππ cos ππ
Advanced Graphics β Variance Reduction 15 βΒ½Ο +Β½Ο Incoming direct light π¦ = ΰΆ±
π»
ππ π¦, ππ cos ππ πππ β 2π π ΰ·
π=1 π
ππ π, β¦π cos ππ = ΰΆ±
π΅..πΆ
ππ π¦, ππ cos ππ πππ + ΰΆ±
π·..πΈ
ππ π¦, ππ cos ππ πππ A B C D
Advanced Graphics β Variance Reduction 16 βΒ½Ο +Β½Ο Incoming direct + indirect light π¦ A B C D
Advanced Graphics β Variance Reduction 17 βΒ½Ο +Β½Ο Incoming direct + indirect light βΒ½Ο +Β½Ο
Advanced Graphics β Variance Reduction 18
Next Event Estimation
Observation: 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 19
Next Event Estimation
Per surface interaction, we trace two random rays. βͺ Ray A returns (via point π¦) the energy reflected by π§ (estimates indirect light for π¦). βͺ Ray B returns the direct illumination on point π¦ (estimates direct light on π¦). βͺ Ray C returns the direct illumination on point π§, which will reach the sensor via ray A. βͺ Ray D leaves the scene. π¦ π§ A B C D
Advanced Graphics β Variance Reduction 20
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 21
Next Event Estimation
We thus 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 22 π¦
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 23 π¦
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 24
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 25
Advanced Graphics β Variance Reduction 26
Advanced Graphics β Variance Reduction 27
Advanced Graphics β Variance Reduction 28
Next Event Estimation
Some vertices require special attention: βͺ If the first vertex after the camera is emissive, its energy canβt be reflected to the camera. βͺ For specular surfaces, the BRDF to a light is always 0. 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 29
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; }
βͺ Introduction βͺ Stratification βͺ Next Event Estimation βͺ Importance Sampling βͺ Multiple Importance βͺ Resampled Importance
Advanced Graphics β Variance Reduction 31
Importance Sampling for Monte Carlo
Monte Carlo integration: π
π΅ = ΰΆ± π΅ πΆ
π(π¦) ππ¦ = πΆ β π΅ πΉ π π β πΆ β π΅ π ΰ·
π=1 π
π π 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 3.5 * 6 + 3.5 = 24.5) Using Monte Carlo: π = 1 π ΰ·
π=1 π
π(πΈ1) + π(πΈ2) π₯βππ π: πΈ1, πΈ2β {1,2,3,4,5,6}, π π¦ = 6π¦, π π¦ = π¦
New; thanks βThomasβ
Advanced Graphics β Variance Reduction 32
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 T = IRand( 2 ); float f = (float)((5 * T + 1) * D) / 0.5f; total += f; rolls++; }
0.5: Probability of using die π.
Advanced Graphics β Variance Reduction 33
Importance Sampling for Monte Carlo
What happens when we donβt pick each die with the same probability? βͺ we get the correct answer; βͺ we get lower variance.
float D1_prob = 0.8f; for( int i = 0; i < 1000; i++ ) { int D = IRand( 6 ) + 1; float r = Rand(); // uniform 0..1 int T = (r < D1_prob) ? 0 : 1; float p = (T == 0) ? D1_prob : (1 β D1_prob); float f = (float)((5 * T + 1) * D) / p; total += f; rolls++; }
Advanced Graphics β Variance Reduction 34
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 35
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 36
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 37
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 38
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 π is 0 (just like radiance arriving at a point is 0). However, we can say something about the probability of choosing π in a part of the domain, by integrating the pdf over the subdomain. The pdf is a probability density.
Advanced Graphics β Variance Reduction 39
Importance Sampling for Monte Carlo
Example 4: sampling the hemisphere. Β½π βΒ½π
Advanced Graphics β Variance Reduction 40
Importance Sampling for Monte Carlo
Example 4: sampling the hemisphere. Β½π βΒ½π
Advanced Graphics β Variance Reduction 41
Importance Sampling for Monte Carlo
Monte Carlo without importance sampling: πΉ π π β 1 π ΰ·
π=1 π
π π With importance sampling: πΉ π π β 1 π ΰ·
π=1 π π ππ
π ππ Here, π π¦ is the probability density function (PDF).
Advanced Graphics β Variance Reduction 42
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 43
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 choose random directions using this PDF?
Advanced Graphics β Variance Reduction 44
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 45
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; }
βͺ Introduction βͺ Stratification βͺ Next Event Estimation βͺ Importance Sampling βͺ Resampled Importance
Advanced Graphics β Variance Reduction 47
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 48 sampling the lights sampling the hemisphere
(almost) specular glossy diffuse
Advanced Graphics β Variance Reduction 49
Multiple Importance Sampling
Light sampling: paths to random points on the light yield high variance. Hemisphere sampling (with importance): random rays yield low variance.
A ray to a random point on the light can be right in the βpeakβ area of the lobe, but also in a much dimmer area. We thus get very different samples.
Glossy BRDF
Here, rays sampled proportional to the BRDF have a high probability of hitting the light. Most samples yield similar energy levels.
Advanced Graphics β Variance Reduction 50
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. Glossy BRDF
A ray to a random point on the light gets scaled down significantly by the BRDF, but each value is roughly the same, because each direction is roughly the same. Most of these rays miss the light source.
Advanced Graphics β Variance Reduction 51
Multiple Importance Sampling
MIS: combining samples taken using multiple strategies.
πππ Ο =
1 π‘ππππ πππππ.
that case, the pdf would have been πππ Ο = 1
2π.
Since both methods are equivalent (they sample the same direction Ο), we can simply average the pdfs: πππ Ο = π₯ π1 + π₯ π2, π₯ = 0.5 (note that averaging two pdfs yields a valid pdf)
Advanced Graphics β Variance Reduction 52
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 53
Multiple Importance Sampling
Earlier, we separated direct and indirect illumination: βͺ Direct illumination is sampled using a PDF for a light source: πππ
πππβπ’ π¦ = 1/π‘πππππππππ, where π¦ is a direction towards the light.
βͺ Indirect illumination is sampled using a PDF proportional to the BRDF: πππ
ππ ππ π¦ = 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 54
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 55
Advanced Graphics β Variance Reduction 56
Advanced Graphics β Variance Reduction 57
βͺ Introduction βͺ Stratification βͺ Next Event Estimation βͺ Importance Sampling βͺ Resampled Importance
Advanced Graphics β Various 59
From Multiple to Many Lights
Potential contribution is proportional to: βͺ Solid angle βͺ Brightness of the light Sadly we cannot precalculate potential contribution; it depends on the location and orientation of the light source relative to the point we are shading. We can precalculate a less refined potential contribution based on: βͺ Area βͺ Brightness Advanced Graphics β Variance Reduction 60
Many Lights Array
The light array stores pointers to (or indices of) the lights in the scene. For π lights, light array size π is several times π. Each light occupies a number of consecutive slots in the light array, proportional to its coarse potential contribution. Selecting a random slot in the light array now yields (in constant time) a single light source π, with a probability of
π‘πππ’π‘ πππ π π
.
0|1|2|β¦ ..|M-1
Advanced Graphics β Variance Reduction 61
Resampled Importance Sampling
The light array allows us to pick a light source proportional to importance. However, this importance is not very accurate. We can improve our choice using resampled importance sampling.
This scheme allows for unbiased, accurate and constant time selection of a good light source.
0|1|2|β¦ ..|M-1
Advanced Graphics β Variance Reduction 62
Resampled Importance Sampling
Final probability for the chosen light π: π½ππππ π‘π β π½π ππ‘ππππππ Where π½ππππ π‘π =
π‘πππ’π‘ πππ π π
and π½π ππ‘ππππππ =
πππ’πππ’πππ ππππ’π πππ£π’πππ π π‘π£ππππ πππ’πππ’πππ ππππ’π πππ£π’ππππ‘.
0|1|2|β¦ ..|M-1
Advanced Graphics β Variance Reduction 63
Advanced Graphics β Variance Reduction 64
Jacco Bikker - November 2019 - February 2019
next lecture: βVariousβ