[Bf-blender-cvs] [34eb8571601] soc-2022-many-lights-sampling: Fix: light tree triangle calculations

Jeffrey Liu noreply at git.blender.org
Wed Jul 13 07:26:36 CEST 2022


Commit: 34eb8571601e2971c91a0dd50ce30a3eda4366e7
Author: Jeffrey Liu
Date:   Wed Jul 13 01:25:25 2022 -0400
Branches: soc-2022-many-lights-sampling
https://developer.blender.org/rB34eb8571601e2971c91a0dd50ce30a3eda4366e7

Fix: light tree triangle calculations

This fixes a variety of issues with triangles in the sampling. This
includes incorrect normal calculations and incorrect PDF calculations.
`

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

M	intern/cycles/kernel/light/light.h
M	intern/cycles/kernel/light/light_tree.h
M	intern/cycles/scene/light_tree.cpp

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

diff --git a/intern/cycles/kernel/light/light.h b/intern/cycles/kernel/light/light.h
index c81b6db42fd..40454ae6e63 100644
--- a/intern/cycles/kernel/light/light.h
+++ b/intern/cycles/kernel/light/light.h
@@ -658,17 +658,25 @@ ccl_device_forceinline float triangle_light_pdf(KernelGlobals kg,
   }
   else {
     float pdf = triangle_light_pdf_area(kg, sd->Ng, sd->I, t);
-    if (has_motion) {
-      const float area = 0.5f * len(N);
-      if (UNLIKELY(area == 0.0f)) {
-        return 0.0f;
-      }
+    const float area = 0.5f * len(N);
+    if (UNLIKELY(area == 0.0f)) {
+      return 0.0f;
+    }
+
+    if (area != 0.0f) {
       /* scale the PDF.
        * area = the area the sample was taken from
        * area_pre = the are from which pdf_triangles was calculated from */
-      triangle_world_space_vertices(kg, sd->object, sd->prim, -1.0f, V);
-      const float area_pre = (kernel_data.integrator.use_light_tree) ? 1.0 : triangle_area(V[0], V[1], V[2]);
-      pdf = pdf * area_pre / area;
+      if (has_motion) {
+        triangle_world_space_vertices(kg, sd->object, sd->prim, sd->time, V);
+        const float area_pre = (kernel_data.integrator.use_light_tree) ?
+                                   1.0 :
+                                   triangle_area(V[0], V[1], V[2]);
+        pdf *= area_pre / area;
+      }
+      else if (kernel_data.integrator.use_light_tree) {
+        pdf /= area;
+      }
     }
 
     return pdf;
@@ -823,15 +831,20 @@ ccl_device_forceinline void triangle_light_sample(KernelGlobals kg,
     /* compute incoming direction, distance and pdf */
     ls->D = normalize_len(ls->P - P, &ls->t);
     ls->pdf = triangle_light_pdf_area(kg, ls->Ng, -ls->D, ls->t);
-    if (has_motion && area != 0.0f) {
+    if (area != 0.0f) {
       /* scale the PDF.
        * area = the area the sample was taken from
        * area_pre = the are from which pdf_triangles was calculated from */
-      triangle_world_space_vertices(kg, object, prim, -1.0f, V);
-      const float area_pre = (kernel_data.integrator.use_light_tree) ?
-                                 1.0 :
-                                 triangle_area(V[0], V[1], V[2]);
-      ls->pdf = ls->pdf * area_pre / area;
+      if (has_motion) {
+        triangle_world_space_vertices(kg, object, prim, -1.0f, V);
+        const float area_pre = (kernel_data.integrator.use_light_tree) ?
+                                   1.0 :
+                                   triangle_area(V[0], V[1], V[2]);
+        ls->pdf = ls->pdf * area_pre / area;
+      }
+      else if (kernel_data.integrator.use_light_tree) {
+        ls->pdf = ls->pdf / area;
+      }
     }
     ls->u = u;
     ls->v = v;
diff --git a/intern/cycles/kernel/light/light_tree.h b/intern/cycles/kernel/light/light_tree.h
index 0ba28982e91..6ba2cec6804 100644
--- a/intern/cycles/kernel/light/light_tree.h
+++ b/intern/cycles/kernel/light/light_tree.h
@@ -151,7 +151,7 @@ ccl_device int light_tree_sample(KernelGlobals kg,
     if (tree_u < left_probability) {
       index = index + 1;
       knode = left;
-      tree_u = tree_u * left_probability;
+      tree_u = tree_u / left_probability;
       *pdf_factor *= left_probability;
     }
     else {
diff --git a/intern/cycles/scene/light_tree.cpp b/intern/cycles/scene/light_tree.cpp
index fd73114f969..c0676f508e3 100644
--- a/intern/cycles/scene/light_tree.cpp
+++ b/intern/cycles/scene/light_tree.cpp
@@ -149,7 +149,12 @@ OrientationBounds LightTreePrimitive::calculate_bcone(Scene *scene) const
       }
     }
 
-    float3 normal = triangle.compute_normal(p);
+    float3 normal = cross(p[1] - p[0], p[2] - p[0]);
+    const float normlen = len(normal);
+    if (normlen == 0.0f) {
+      normal = make_float3(1.0f, 0.0f, 0.0f);
+    }
+    normal = normal / normlen;
 
     bcone.axis = normal;



More information about the Bf-blender-cvs mailing list