Что такое важность выборки? В каждой статье, о которой я читал, упоминается «PDF», что же это такое?
From what I gather, importance sampling is a technique to only sample areas on a hemisphere that matter more than others. So, ideally, I should sample rays towards light sources to reduce noise and increase speed. Also, some BRDF's at grazing angles have little no difference in the calculation so using importance sampling to avoid that is good?
If I were to implement importance sampling for a Cook-Torrance BRDF how could I do this?
brdf
importance-sampling
Arjan Singh
источник
источник
Ответы:
Short answer:
Importance sampling is a method to reduce variance in Monte Carlo Integration by choosing an estimator close to the shape of the actual function.
Long Answer:
To start, let's review what Monte Carlo Integration is, and what it looks like mathematically.
Monte Carlo Integration is an technique to estimate the value of an integral. It's typically used when there isn't a closed form solution to the integral. It looks like this:
In english, this says that you can approximate an integral by averaging successive random samples from the function. AsN gets large, the approximation gets closer and closer to the solution. pdf(xi) represents the probability density function of each random sample.
Let's do an example: Calculate the value of the integralI .
Let's use Monte Carlo Integration:
A simple python program to calculate this is:
If we run the program we getI=0.4986941
Using separation by parts, we can get the exact solution:
You'll notice that the Monte Carlo Solution is not quite correct. This is because it is an estimate. That said, asN goes to infinity, the estimate should get closer and closer to the correct answer. Already at N=2000 some runs are almost identical to the correct answer.
A note about the PDF: In this simple example, we always take a uniform random sample. A uniform random sample means every sample has the exact same probability of being chosen. We sample in the range[0,2π] so, pdf(x)=1/(2π−0)
Importance sampling works by not uniformly sampling. Instead we try to choose more samples that contribute a lot to the result (important), and less samples that only contribute a little to the result (less important). Hence the name, importance sampling.
If you choose a sampling function whose pdf very closely matches the shape off , you can greatly reduce the variance, which means you can take less samples. However, if you choose a sampling function whose value is very different from f , you can increase the variance. See the picture below:
Image from Wojciech Jarosz's Dissertation Appendix A
One example of importance sampling in Path Tracing is how to choose the direction of a ray after it hits a surface. If the surface is not perfectly specular (ie. a mirror or glass), the outgoing ray can be anywhere in the hemisphere.
We could uniformly sample the hemisphere to generate the new ray. However, we can exploit the fact that the rendering equation has a cosine factor in it:
Specifically, we know that any rays at the horizon will be heavily attenuated (specifically,cos(x) ). So, rays generated near the horizon will not contribute very much to the final value.
To combat this, we use importance sampling. If we generate rays according to a cosine weighted hemisphere, we ensure that more rays are generated well above the horizon, and less near the horizon. This will lower variance and reduce noise.
In your case, you specified that you will be using a Cook-Torrance, microfacet-based BRDF. The common form being:
where
The blog "A Graphic's Guy's Note" has an excellent write up on how to sample Cook-Torrance BRDFs. I will refer you to his blog post. That said, I will try to create a brief overview below:
The NDF is generally the dominant portion of the Cook-Torrance BRDF, so if we are going to importance sample, the we should sample based on the NDF.
Cook-Torrance doesn't specify a specific NDF to use; we are free to choose whichever one suits our fancy. That said, there are a few popular NDFs:
Each NDF has it's own formula, thus each must be sampled differently. I am only going to show the final sampling function for each. If you would like to see how the formula is derived, see the blog post.
GGX is defined as:
To sample the spherical coordinates angleθ , we can use the formula:
whereξ is a uniform random variable.
We assume that the NDF is isotropic, so we can sampleϕ uniformly:
Beckmann is defined as:
Which can be sampled with:
Lastly, Blinn is defined as:
Which can be sampled with:
Putting it in Practice
Let's look at a basic backwards path tracer:
IE. we bounce around the scene, accumulating color and light attenuation as we go. At each bounce, we have to choose a new direction for the ray. As mentioned above, we could uniformly sample the hemisphere to generate the new ray. However, the code is smarter; it importance samples the new direction based on the BRDF. (Note: This is the input direction, because we are a backwards path tracer)
Which could be implemented as:
After we sample the inputDirection ('wi' in the code), we use that to calculate the value of the BRDF. And then we divide by the pdf as per the Monte Carlo formula:
Where Eval() is just the BRDF function itself (Lambert, Blinn-Phong, Cook-Torrance, etc.):
источник
wi
? I understand how to sample the spherical coordinates angle θ but for the actual direction vector how is that done?If you have a 1D functionf(x) and you want to integrate this function from say 0 to 1, one way to perform this integration is by taking N random samples in range [0, 1], evaluate f(x) for each sample and calculate the average of the samples. However, this "naive" Monte Carlo integration is said to "converge slowly", i.e. you need a large number of samples to get close to the ground truth, particularly if the function has high frequencies.
With importance sampling, instead of taking N random samples in [0, 1] range, you take more samples in the "important" regions off(x) that contribute most to the final result. However, because you bias sampling towards the important regions of the function, these samples must be weighted less to counter the bias, which is where the PDF (probability density function) comes along. PDF tells the probability of a sample at given position and is used to calculate weighted average of the samples by dividing the each sample with the PDF value at each sample position.
With Cook-Torrance importance sampling the common practice is to distribute samples based on the normal distribution function NDF. If NDF is already normalized, it can serve directly as PDF, which is convenient since it cancels the term out from the BRDF evaluation. Only thing you need to do then is to distribute sample positions based on PDF and evaluate BRDF without the NDF term, i.e.
For NDF you need to calculate Cumulative Distribution Function of the PDF to convert uniformly distributed sample position to PDF weighted sample position. For isotropic NDF this simplifies to 1D function because of the symmetry of the function. For more details about the CDF derivation you can check this old GPU Gems article.
источник