Skip to content
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

Avoid possible 'divide by 0' in G_GGX_Smith function #8349

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/renderers/shaders/ShaderChunk/bsdfs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ float G_GGX_Smith( const in float alpha, const in float dotNL, const in float do

float gv = dotNV + pow( a2 + ( 1.0 - a2 ) * pow2( dotNV ), 0.5 );

return 1.0 / ( gl * gv );
float denominator = ( gl * gv ) + 0.00001; // avoid possible divide by 0 on the next line
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I would arbitrarily add a constant to one term of the BRDF without studying how the product of all the terms behaves in the limit.

Besides, I was under the assumption that we specifically bounded roughness away from zero.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We bounded roughness, but a2 here is pow4( roughness) which on low precision mobile devices may be able to go to zero.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have only seen this issue arise on low precision mobile devices. a2 = pow4(0.04) = 0.00000256. Mediump float range is [2^-14, 2^14] according to the spec. Many mobile devices are limited to mediump float precision. That means the smallest number is 0.00006103515, which is larger than pow4(0.04), which means pow4(0.04) will round to zero. I believe this explains the issue.


return 1.0 / denominator;

} // validated

Expand Down