<p>You can sample using absolutely any distribution you want, as long as you weight the results by dividing by the pdf of the sampled distribution. It will converge to the right answer (as long as the distribution is nonzero everywhere that you want to integrate). Different distributions will give different amounts of variance though. The trick with importance sampling is to find a distribution that minimizes variance while still being cheap to compute. A distribution similar to the shape of the function you're integrating, or equal to some factor of that function (so that it cancels when you divide), works best. In other words you try to guide the sampling toward areas that are more important to the result, hence, &quot;importance&quot; sampling.</p>
<p>So what the author is saying is the image on the left was sampled with <span class="math-container">$\omega_o$</span> (or maybe <span class="math-container">$\omega_m$</span>, I'm not sure) drawn from a cosine hemisphere distribution, just as an example of a distribution that does <em>not</em> match the BRDF very well. Then they calculate the samples as <span class="math-container">$f(\omega_i, \omega_o) / p_{\cos}(\omega_o)$</span> where <span class="math-container">$p_{\cos}$</span> is the pdf for the cosine distribution. This is just to show you that it gives the &quot;correct&quot; result, but with a ton of variance. (Although it looks like there are some black areas on the left image that suggest it's not really converging to the correct answer...but let's assume that's a bug.)</p>
<p>Then the image on the right is sampled with (presumably) <span class="math-container">$\omega_m$</span> drawn from a distribution matching the <span class="math-container">$D(\omega_m)$</span> term from the BRDF. Then when you calculate the BRDF you can leave out the <span class="math-container">$D$</span> factor since it's canceled by the <span class="math-container">$D$</span> factor in the pdf. (The pdf probably still has some normalization and some conversion of probability from <span class="math-container">$\omega_m$</span> to <span class="math-container">$\omega_o$</span>, so you still have to divide by those factors.)</p>

CC BY-SA 4.0
2020-10-02T08:45:56.673Z
What is pdf for the cosine distribution? ; I managed to get that black spots by using ggxProb mentioned in here - http://cwyman.org/code/dxrTutors/tutors/Tutor14/tutorial14.md.html ; Image on the right is explained in details in blog post and I managed to get it working and I understand most of theory. I would say that I only don't understand why we have to Jacobians and how do we find them.
DirectX_Programmer
CC BY-SA 4.0
2020-10-02T17:10:40.633Z
The pdf for the cosine distribution is $\cos(\theta)/\pi$.
Nathan Reed
CC BY-SA 4.0
2020-10-05T08:38:08.850Z
So throughput for cosine distribution will be: $$\frac{NoL * ggx}{pdf} = NoL * ggx ~\frac{\pi}{cos(\theta)} = NoL * ggx ~\frac{\pi}{NoL} = ggx ~ * ~ \pi$$ ? I'm not planning to use it due to more efficient methods, but I'm interested if my understanding is correct.
DirectX_Programmer
CC BY-SA 4.0
2020-10-05T23:56:32.873Z
@DirectX_Programmer Yep that looks right. The fact that it cancels out the $N \cdot L$ is exactly why the cosine distribution is used for diffuse light.
Nathan Reed
CC BY-SA 4.0
2020-10-01T19:25:27.477Z
2020-10-01T19:25:27.477Z
Nathan Reed
<p>Let's take a look at this blog article - <a href="https://schuttejoe.github.io/post/ggximportancesamplingpart1/" rel="nofollow noreferrer">https://schuttejoe.github.io/post/ggximportancesamplingpart1/</a> and image presented as a result of implementing it:</p>
<p><a href="https://i.sstatic.net/xzrCR.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/xzrCR.jpg" alt="enter image description here" /></a></p>
<p>Inside above link, you can see description for this image saying</p>
<blockquote>
<p>In order to have something to compare to on the left I generated an image by importance sampling a cosine distribution and on the right I generated an image using the technique described above. [...]</p>
</blockquote>
<p>Based on the fact that image on the right was derived based on GGX pdf, how was image on the left generated? I assume we have our standard equation but what does it mean that it's generated with <em>importance sampling a cosine distribution</em>?
<span class="math-container">$$
f(w_i,w_o) = \frac{F(w_i, w_m) ~ G_2(w_i,wo_,w_m) D(w_m)}{4 ~ |w_i \cdot w_g| ~ |w_o \cdot w_g|} * PDF[?]
$$</span></p>

<raytracing><brdf><sampling><importance-sampling>
Importance sampling a cosine distribution
DirectX_Programmer
# Importance sampling a cosine distribution

(Asked by: username_0 on 2020-10-01)

Let's take a look at this blog article - <https://schuttejoe.github.io/post/ggximportancesamplingpart1/> and image presented as a result of implementing it:

[![enter image description here](https://i.sstatic.net/xzrCR.jpg)](https://i.sstatic.net/xzrCR.jpg)

Inside above link, you can see description for this image saying

> In order to have something to compare to on the left I generated an image by importance sampling a cosine distribution and on the right I generated an image using the technique described above. [...]

Based on the fact that image on the right was derived based on GGX pdf, how was image on the left generated? I assume we have our standard equation but what does it mean that it's generated with *importance sampling a cosine distribution*?
$$
f(w\_i,w\_o) = \frac{F(w\_i, w\_m) ~ G\_2(w\_i,wo\_,w\_m) D(w\_m)}{4 ~ |w\_i \cdot w\_g| ~ |w\_o \cdot w\_g|} \* PDF[?]
$$

**Tags:** raytracing, brdf, sampling, importance-sampling


---

## 1 answers

### Answer by username_1 on 2020-10-01. Score: 4 | Accepted answer

You can sample using absolutely any distribution you want, as long as you weight the results by dividing by the pdf of the sampled distribution. It will converge to the right answer (as long as the distribution is nonzero everywhere that you want to integrate). Different distributions will give different amounts of variance though. The trick with importance sampling is to find a distribution that minimizes variance while still being cheap to compute. A distribution similar to the shape of the function you're integrating, or equal to some factor of that function (so that it cancels when you divide), works best. In other words you try to guide the sampling toward areas that are more important to the result, hence, "importance" sampling.

So what the author is saying is the image on the left was sampled with $\omega\_o$ (or maybe $\omega\_m$, I'm not sure) drawn from a cosine hemisphere distribution, just as an example of a distribution that does *not* match the BRDF very well. Then they calculate the samples as $f(\omega\_i, \omega\_o) / p\_{\cos}(\omega\_o)$ where $p\_{\cos}$ is the pdf for the cosine distribution. This is just to show you that it gives the "correct" result, but with a ton of variance. (Although it looks like there are some black areas on the left image that suggest it's not really converging to the correct answer...but let's assume that's a bug.)

Then the image on the right is sampled with (presumably) $\omega\_m$ drawn from a distribution matching the $D(\omega\_m)$ term from the BRDF. Then when you calculate the BRDF you can leave out the $D$ factor since it's canceled by the $D$ factor in the pdf. (The pdf probably still has some normalization and some conversion of probability from $\omega\_m$ to $\omega\_o$, so you still have to divide by those factors.)

> **Comments:**
> **username_0**: What is pdf for the cosine distribution? ; I managed to get that black spots by using ggxProb mentioned in here - http://cwyman.org/code/dxrTutors/tutors/Tutor14/tutorial14.md.html ; Image on the right is explained in details in blog post and I managed to get it working and I understand most of theory. I would say that I only don't understand why we have to Jacobians and how do we find them.  
> **username_1**: The pdf for the cosine distribution is $\cos(\theta)/\pi$.  
> **username_0**: So throughput for cosine distribution will be: $$\frac{NoL * ggx}{pdf} = NoL * ggx ~\frac{\pi}{cos(\theta)} = NoL * ggx ~\frac{\pi}{NoL} = ggx ~ * ~ \pi$$ ? I'm not planning to use it due to more efficient methods, but I'm interested if my understanding is correct.  
> **username_1**: @username_0 Yep that looks right. The fact that it cancels out the $N \cdot L$ is exactly why the cosine distribution is used for diffuse light.  

---

