I’m working on microfacet brdf model for my renderer these days, noticing that it is more than necessary to provide a separate sampling method for microfacet brdf instead of using the default one, which is usually used for diffuse like surfaces and highly inefficient for brdf with spiky shape, such as mirror like surfaces. The following image is one generated by the default sampling method:

The left monkey has pure reflection brdf which is mentioned in my previous blog post, the right one uses the microfacet model with zero as roughness value. I was expecting similar result for both of the monkeys while things turned out to be wrong here, we can barely see the reflection from the monkey. Actually there is nothing wrong, the fact is that the convergence rate of default sampling the microfacet model with 0 roughness is extremely low. As long as there are enough samples, it will reach the appearance similar to the left one. However the number of samples being enough can be arbitrarily high depending on how spiky your brdf is.

The right way of sampling microfacet brdf is mentioned in this paper. What I want to record in this blog is how these conclusions are derived from the original microfacet model. Using these better sampling method, we will get similar result for those two monkeys eventually.

### Why Default Sampling Method is Inefficient

So why is this default sampling method inefficient for mirror like brdf model. First of all, the default sampling method goes with the following pdf:

It achieves good result for diffuse surfaces like Lambert, OrenNayar. In fact, it is not a sampling method for diffuse brdfs, a sampling method for lambert brdf will respect a pdf with constant value, it also involves the cosine factor located in the rendering equation or LTE.

Since it is extreme difficult, if not impossible, to have any knowledge on the incident radiance, the only thing we have is the brdf and cosine factor. The sampling method mentioned above could be efficient if the product of brdf and cosine factor is the dominant one in this equation. It works pretty well in practice most of the time.

However the efficiency of default sampling method drops quickly as the brdf becomes the dominant factor with roughness going to zero. For microfacet brdf here, its extreme form is like a dirac-delta function, it is almost impossible to get a sample with brdf having a non-zero value. And for the lucky ones that do have non-zero brdf values, it could reach super-high value due to its low probability, introducing high variance to the sampling result. In other words, the convergence rate is quite low, which is exactly why we are seeing the above image. It can be even treated as a bug. Obviously we need a better way to sample these microfacet brdfs.

### Microfacet Model

Microfacet Model is quite hot in real time rendering these years, it is the basics of physically based shading algorithm. Generally its basic form is like the following:

The first component is Fresnel, the second one is a shadowing factor called G term, the last one is normal distribution function (NDF). Comparing with the old ad-hoc bxdf model, microfacet model obeys the rule of energy conservation which allows the artist to change the roughness of the material through one parameter instead of two, specular color and specular power.

Among all those factors in this equation, the NDF term is usually the dominant one. The specific shape of NDF is largely affected by the roughness of the bxdf. In order to sample the microfacet brdf model, it is usually to sample NDF first getting a random microfacet normal that respects the NDF and then reflect the exitence radiance along the normal to generate the incident direction.

There are several kinds of NDF around. The following three will be covered in this blog and all that will be mentioned are isotropic ones.

- GGX
- Beckmann
- Blinn

Before we proceed, there is one rule that all NDF should follow:

We will use this equation in our following derivation. Explaining the equation is outside the scope of this blog, however one can refer here for further detail.

#### Sampling GGX

Here is the basic form of GGX:

So the pdf respecting solid angle is like this:

Instead of sampling solid angle directly, we usually use spherical coordinate to sample it. So it is not the pdf respecting the solid angle that we are interested, it is the pdf respecting the spherical coordinates.

The following task is quite straight forward, which is basically sampling according to a specific pdf. Inversion method goes pretty well for all NDFs in this blog. Notice that is not even in this equation, that said the NDF is totally isotropic and we can sample uniformly. For detail proof, please refer here. The only thing left here is how to sample . In order to do that, we need to get the pdf for first.

Let’s calculate the CDF next:

With a canonical random number equals to this CDF, we have the following equation:

To solve this equation requires no more knowledge than mathematic in junior high school, I guess there is no need to show the whole process. The final solution of the above equation is:

or

The above two equations are exactly the same thing. Choosing which one to use is just a matter of taste.

#### Sampling Beckmann

The derivation of sampling method for Beckmann is quite similar to the above procedure. Here is the Beckmann distribution:

The pdf respecting spherical coordinate is like this:

Again, can be sampled uniformly. The pdf for should be this:

The CDF for can be calculated this way:

Solving the equation of gives the following solution:

or

#### Sampling Blinn

Here is the Blinn NDF:

The pdf respecting to spherical coordinate is:

Isolating gives the following:

This one is a lot simpler than the previous two, here is the CDF:

Here is the relation between canonical random number and

Since is a canonical random number, is also one. So we can simplify the above equation with the following one:

A little more about this sampling method. I’m not quite confident with this solution, although I can see nothing wrong in this derivation. PBRT gives a similar solution for Blinn which is:

And another open-source ray tracer, mitsuba, also adopts this way of sampling. I don’t quite understand the derivation in the book, so I’ll just stick to this one, which is also the one mentioned here. I tried the pbrt’s way of sampling, only minor difference can be noticed from the image.

#### One Extra Step

There is still one step further from being done. It is the incident direction sampling that we are interested, not the normal. The reason we are sampling the normal instead of incident direction directly is because NDF is usually the dominant factor in microfacet model. Generating the incident direction after sampling normal is a more efficient way than sampling the incident direction itself.

However the way we calculate PDF given an incident direction is different from the above ones respecting half-vector, whether it’s solid angle or spherical coordinate. What we have so far is the pdf for half-vector, a transformation is necessary.

### Conclusion

Here are the images generated after and before the new sampling method.

There are 64 samples per pixel and GGX is chosen as NDF, the three monkeys have different roughness values(0.0,0.5,1.0). As we can see from the image that the left-most monkey gets much better result than the default sampling method. And for the other two monkeys, we have similar result with the cosine-pdf sampling method.

There are already some research work improving the sampling method mentioned in this blog. I may need to try their method once I have some free cycles.

### Reference:

[1] Microfacet Models for Refraction through Rough Surfaces. https://www.cs.cornell.edu/~srm/publications/EGSR07-btdf.html

[2] Dirac delta function. https://en.wikipedia.org/wiki/Dirac_delta_function

[3] How is the NDF really defined. http://www.reedbeta.com/blog/2013/07/31/hows-the-ndf-really-defined/

[4] Notes on importance sampling. http://blog.tobias-franke.eu/2014/03/30/notes_on_importance_sampling.html

SteveNovember 2, 2015 / 6:54 pmYou have a small typo in the equation at the end of the “Sampling GGX” section: the division by 1 – \epsilon should be inside the square root.

LikeLiked by 1 person

AGraphicsGuyNovember 3, 2015 / 1:02 amAgreed, appreciated. It is already fixed.

LikeLike

RafaelJune 28, 2020 / 8:12 amHow the “One Extra Step” formula is derived? How do you calculate dwh / dwi ?

LikeLike

wallacekcyOctober 5, 2017 / 3:33 amHi,

Why does Beckmann’s pdf has a sine term instead of a cosine term like the others?

Thanks

LikeLiked by 1 person

AGraphicsGuyOctober 6, 2017 / 8:50 amIt should have a cosine term, I guess I missed it, :(. Already corrected.

Thanks for pointing it point.

LikeLike

Alessandro NardiniNovember 3, 2017 / 3:12 pmVery useful article, learning a lot from this! thank you for sharing!

LikeLike

Fei XiaDecember 5, 2017 / 5:20 pmit seems there exists small errors in the derivation of the cdf of both ggx and beckmann:

the second step of ggx and the fourth step of beckmann are missing the minus sign

LikeLike

Fei XiaDecember 5, 2017 / 5:26 pmmy bad, only the fourth step of beckmann is missing the minus sign.

LikeLike

AGraphicsGuyDecember 6, 2017 / 7:01 pmI don’t see any issue of the derivation, I think there is no problem with the fourth step.

LikeLike

Fei XiaDecember 5, 2017 / 5:41 pmin my understanding the difference of result of bling is because you are sampling the normal, but the other is sampling directly the insident direction.

the answer live in this article:

https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch20.html

LikeLike

AGraphicsGuyDecember 6, 2017 / 7:02 pmI’m afraid that I don’t agree. It is normal that I sample in the three cases, there is no difference of bling comparing with others.

LikeLike

g2-c02031016c3e5bf73dca2b8eb33f399aJune 21, 2018 / 10:18 pmHi! Great post but I have a question: could you explain how you do this step in the calculation?

https://imgur.com/HJLWAFh

Look like magic…

LikeLike

AGraphicsGuyJune 21, 2018 / 10:23 pmThanks.

I wish I can type latex here, which I don’t think so.

First of all, squared alpha in the nominator can be isolated because it is a constant. Try replace cos(t)^2 with x, you will get a new formula. And then what is left is very basic mathematics about integral calculus. I hope this helps.

LikeLiked by 1 person

g2-c02031016c3e5bf73dca2b8eb33f399aJune 21, 2018 / 11:38 pmThanks. (but it’s very basic for you maybe, I still can’t properly integrate like you did, I’m heavily relying on Mathematica do to that for me 😀 )

LikeLike

Aditya SreekarAugust 18, 2018 / 12:48 pmAwesome tutorial

LikeLike

Hao ZhangMarch 8, 2019 / 2:59 pmHi! Great post, but I have a problem. In Sampling Part, for example, sampling GGX. ‘The PDF respecting solid angle’ you wrote is p(h,n,α)=D(h,n,α)|h⋅n|, and the solid angle means |h⋅n|, right? why should there be a |h⋅n|?

LikeLike

Hao ZhangMarch 8, 2019 / 3:03 pmI figure out, after I read the link in the blog. Thanks.

LikeLike

AGraphicsGuyMarch 9, 2019 / 11:35 pmTo reply to your last question, none of the final conclusion reaches 0 when roughness goes to 0. Do notice that there is a curve from roughness to alpha, it is not a direct match. You can check out the code to get a better explanation of it.

LikeLike

AGraphicsGuyMarch 8, 2019 / 3:06 pmI’m glad it is helpful.

Please refer to this article for further explanation

http://www.reedbeta.com/blog/hows-the-ndf-really-defined/

LikeLike

Hao ZhangMarch 9, 2019 / 11:25 amThank you very much for your reply. I have one more question. When roughness equal to zero, the D(h) is always 0, doesn’t like a dirac-delta function which is 0 expect 1 point. So I can get good result at roughness close to 0, but it becomes totally black when the roughness is one. How did you deal with this 0 divided by 0 problem?

LikeLike

CasperJanuary 25, 2020 / 5:00 pmGreat blog, this is excactly the problem i’m encountering right now. I have one question though. For a faster converging image we need to sample in the direction of the reflection. In the end we have calculated a theta, that as far as I understand it, describes the angle between the normal and the outgoing ray. I’m not sure how this can be converted to an outgoing ray. Can we pick any outgoing direction which is at this angle?

LikeLike

Hulda ErxlebenOctober 15, 2020 / 5:22 pmI’d like to thank you for the efforts you have put in penning this website. I am hoping to check out the same high-grade blog posts by you in the future as well. In truth, your creative writing abilities has motivated me to get my very own blog now 😉

LikeLike

Jewel HeathOctober 17, 2020 / 4:44 amHaving read this I believed it was very enlightening. I appreciate you finding the time and energy to put this article together. I once again find myself spending a significant amount of time both reading and commenting. But so what, it was still worth it!

LikeLike