[Bf-blender-cvs] [a444e9ce2f2] soc-2022-many-lights-sampling: Fix division by zero in reservoir sampling, add some asserts

Brecht Van Lommel noreply at git.blender.org
Mon Nov 21 19:10:34 CET 2022


Commit: a444e9ce2f214dccf6c4724f130ddbb9c2897b96
Author: Brecht Van Lommel
Date:   Mon Nov 21 17:00:22 2022 +0100
Branches: soc-2022-many-lights-sampling
https://developer.blender.org/rBa444e9ce2f214dccf6c4724f130ddbb9c2897b96

Fix division by zero in reservoir sampling, add some asserts

===================================================================

M	intern/cycles/kernel/light/tree.h

===================================================================

diff --git a/intern/cycles/kernel/light/tree.h b/intern/cycles/kernel/light/tree.h
index d7b8ebad2cf..560c25e9197 100644
--- a/intern/cycles/kernel/light/tree.h
+++ b/intern/cycles/kernel/light/tree.h
@@ -354,7 +354,7 @@ ccl_device_inline void sample_resevoir(const int current_index,
   }
   total_weight += current_weight;
   float thresh = current_weight / total_weight;
-  if (*rand < thresh) {
+  if (*rand <= thresh) {
     selected_index = current_index;
     selected_weight = current_weight;
     *rand = *rand / thresh;
@@ -362,6 +362,7 @@ ccl_device_inline void sample_resevoir(const int current_index,
   else {
     *rand = (*rand - thresh) / (1.0f - thresh);
   }
+  kernel_assert(*rand >= 0.0f && *rand <= 1.0f);
   return;
 }
 
@@ -525,11 +526,15 @@ ccl_device bool light_tree_sample(KernelGlobals kg,
     }
 
     if (*randu < left_probability) { /* go left */
+      kernel_assert(left_probability > 0.0f);
+
       node_index = left_index;
       *randu /= left_probability;
       *pdf_factor *= left_probability;
     }
     else {
+      kernel_assert((1.0f - left_probability) > 0.0f);
+
       node_index = right_index;
       *randu = (*randu - left_probability) / (1.0f - left_probability);
       *pdf_factor *= (1.0f - left_probability);
@@ -822,6 +827,8 @@ ccl_device bool light_tree_sample(KernelGlobals kg,
   float pdf_factor = 1.0f;
   bool ret;
   if (randu < light_tree_probability) {
+    kernel_assert(light_tree_probability > 0.0f);
+
     randu = randu / light_tree_probability;
     pdf_factor *= light_tree_probability;
     ret = light_tree_sample<in_volume_segment>(kg,
@@ -838,6 +845,8 @@ ccl_device bool light_tree_sample(KernelGlobals kg,
                                                &pdf_factor);
   }
   else {
+    kernel_assert((1.0f - light_tree_probability) > 0.0f);
+
     randu = (randu - light_tree_probability) / (1.0f - light_tree_probability);
     pdf_factor *= (1.0f - light_tree_probability);
     ret = light_tree_sample_distant_lights<in_volume_segment>(kg,



More information about the Bf-blender-cvs mailing list