New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong pseudocode in BRDF reference implementation #2386
Comments
This is indeed a bug in the pseudocode which has been there for a very long time, thanks for your detailed report and derivations. Your proposal looks good, it's also more readable than the old version. However, it will change the look of the material, which might have to be discussed @emackey. This particular code snippet in the "new" Appendix B is identical to the one in the "old" Appendix B (before the rewrite), I copied this bug without noticing. Because it's only in the non-normative part, I suppose the change is fine. Might be a good idea to also check the sample viewer, it might also be affected.
Since you change the naming of some terms, it's required to adapt the extension too. I think KHR_materials_specular, _ior, _clearcoat, and maybe also _iridescence have to be updated. |
* Fix reference BRDF implementation (#2386) * Fix clearcoat emisison and other improvements * Revert removal of trailing space in unrelated section * Use mix function to blend between values * Revert usage of mix for Fresnel terms * Fix inconsistent usage of snake case.
I believe that the pseudocode for a reference BRDF implementation in Appendix B, in the end of section 3.5, is wrong.
The BRDF described prior to that in Appendix B can be formulated as
However, the final pseudocode is doing something different. First, it notes that we can pretend that metallic brdf also has a fake diffuse component equal to zero, and rewrites
which is a correct thing to do. Then, the code rewrites the BRDF computation by first computing a "mixed" Fresnel term and then using it to interpolate between the "mixed" diffuse term and the common specular term:
Which is not equal to the original formulation. If we combine all the mixes together, the problem boils down to
being not equal to
One simple way to see that these two expressions are not equivalent is to note that the latter contains terms proportional to
metallic^2
after fully expanding themix
es (becausemetallic
is in both the first and the third arguments of the outermix
), while the former only contains terms linear inmetallic
.The difference is non-negligible: if the BRDF is implemented using the reference pseudocode, the scene appears darker (top image) compared to an implementation based directly on the material description in section 2.4 (bottom image), as demonstrated by this Cornell Box rendering using a Monte-Carlo path tracer:
(Please, ignore the occasional fireflies, they are irrelevant to the discussion).
Curiously, the difference disappears if one only uses materials with
metallic = 0
ormetallic = 1
, suggesting thatmetallic^2
terms are indeed the root of the problem.I propose to rewrite the pseudocode in the following way, which also more directly corresponds to the material description that precedes the code:
I'd be happy to make a pull request myself (in case I'm not misunderstanding things, of course!).
The text was updated successfully, but these errors were encountered: