From 1b392ef01671e11cef080ff7e0c13f13d61c82ed Mon Sep 17 00:00:00 2001 From: Omar Emara Date: Mon, 30 Sep 2024 17:29:44 +0200 Subject: [PATCH] Fix #127093: Kuwahara node produces NaNs The Kuwahara node produces NaNs if the pixels have a very high local standard deviation and sharpness is also high. This is because the weighted sum of the Kuwahara sectors can have a zero total weight, which causes zero division. To fix this, we return the original color if the total weight is zero. Pull Request: https://projects.blender.org/blender/blender/pulls/128378 --- .../operations/COM_KuwaharaAnisotropicOperation.cc | 10 +++++++++- .../shaders/compositor_kuwahara_anisotropic.glsl | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/source/blender/compositor/operations/COM_KuwaharaAnisotropicOperation.cc b/source/blender/compositor/operations/COM_KuwaharaAnisotropicOperation.cc index 9dcaafcabfa..4ce46c44687 100644 --- a/source/blender/compositor/operations/COM_KuwaharaAnisotropicOperation.cc +++ b/source/blender/compositor/operations/COM_KuwaharaAnisotropicOperation.cc @@ -266,7 +266,15 @@ void KuwaharaAnisotropicOperation::update_memory_buffer_partial(MemoryBuffer *ou sum_of_weights += weight; weighted_sum += color_mean * weight; } - weighted_sum /= sum_of_weights; + + /* Fallback to the original color if all sector weights are zero due to very high standard + * deviation and sharpness. */ + if (sum_of_weights == 0.0f) { + weighted_sum = center_color; + } + else { + weighted_sum /= sum_of_weights; + } copy_v4_v4(it.out, weighted_sum); } diff --git a/source/blender/compositor/realtime_compositor/shaders/compositor_kuwahara_anisotropic.glsl b/source/blender/compositor/realtime_compositor/shaders/compositor_kuwahara_anisotropic.glsl index 2cead5fc0df..572c44c74ac 100644 --- a/source/blender/compositor/realtime_compositor/shaders/compositor_kuwahara_anisotropic.glsl +++ b/source/blender/compositor/realtime_compositor/shaders/compositor_kuwahara_anisotropic.glsl @@ -249,7 +249,15 @@ void main() sum_of_weights += weight; weighted_sum += color_mean * weight; } - weighted_sum /= sum_of_weights; + + /* Fallback to the original color if all sector weights are zero due to very high standard + * deviation and sharpness. */ + if (sum_of_weights == 0.0) { + weighted_sum = center_color; + } + else { + weighted_sum /= sum_of_weights; + } imageStore(output_img, texel, weighted_sum); }