From b6f72f2b4adbd1c98925656d0bcd8f97629cceac Mon Sep 17 00:00:00 2001 From: snowapril Date: Mon, 14 Mar 2022 09:32:06 +0900 Subject: [PATCH] Fix D_GGX code which can cause divide-by-zero val When given roughness is lower than 0.01, d value in original code will be zero. This can make last return value as NAN because of divide-by-zero. This is well addressed in issue #56373. Modified code is referenced on D_GGX function of google/filament (https://github.com/google/filament/blob/main/shaders/src/brdf.fs#L54-L79) Signed-off-by: snowapril --- .../renderer_rd/shaders/scene_forward_lights_inc.glsl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/servers/rendering/renderer_rd/shaders/scene_forward_lights_inc.glsl b/servers/rendering/renderer_rd/shaders/scene_forward_lights_inc.glsl index 1c9b08b6d35..bd1c2b57583 100644 --- a/servers/rendering/renderer_rd/shaders/scene_forward_lights_inc.glsl +++ b/servers/rendering/renderer_rd/shaders/scene_forward_lights_inc.glsl @@ -1,9 +1,9 @@ // Functions related to lighting float D_GGX(float cos_theta_m, float alpha) { - float alpha2 = alpha * alpha; - float d = 1.0 + (alpha2 - 1.0) * cos_theta_m * cos_theta_m; - return alpha2 / (M_PI * d * d); + float a = cos_theta_m * alpha; + float k = alpha / (1.0 - cos_theta_m * cos_theta_m + a * a); + return k * k * (1.0 / M_PI); } // From Earl Hammon, Jr. "PBR Diffuse Lighting for GGX+Smith Microsurfaces" https://www.gdcvault.com/play/1024478/PBR-Diffuse-Lighting-for-GGX