[Bf-blender-cvs] [63c23dff931] soc-2022-many-lights-sampling: Fix: Blender crashes with emissive triangles

Jeffrey Liu noreply at git.blender.org
Wed Jun 22 09:29:31 CEST 2022


Commit: 63c23dff931ee269e37ed6549d0b4d57ed85d909
Author: Jeffrey Liu
Date:   Wed Jun 22 03:23:06 2022 -0400
Branches: soc-2022-many-lights-sampling
https://developer.blender.org/rB63c23dff931ee269e37ed6549d0b4d57ed85d909

Fix: Blender crashes with emissive triangles

This is related to the light-tree implementation.
The issue most often happens when the emissive material is just added
and the preview material is then rendered.

The crash is caused by iterating through the wrong thing.
We are now iterating through emissive primitives, and
many of these primitives correspond to the same object.
However, the inner loop was still iterating through all triangles of the
object, so it was iterating through the same object multiple times.

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

M	intern/cycles/scene/light.cpp

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

diff --git a/intern/cycles/scene/light.cpp b/intern/cycles/scene/light.cpp
index 3035a2e3138..97f0e98633f 100644
--- a/intern/cycles/scene/light.cpp
+++ b/intern/cycles/scene/light.cpp
@@ -444,37 +444,28 @@ void LightManager::device_update_distribution(Device *,
       shader_flag |= SHADER_EXCLUDE_SHADOW_CATCHER;
     }
 
-    size_t mesh_num_triangles = mesh->num_triangles();
-    for (size_t i = 0; i < mesh_num_triangles; i++) {
-      int shader_index = mesh->get_shader()[i];
-      Shader *shader = (shader_index < mesh->get_used_shaders().size()) ?
-                           static_cast<Shader *>(mesh->get_used_shaders()[shader_index]) :
-                           scene->default_surface;
-
-      if (shader->get_use_mis() && shader->has_surface_emission) {
-        distribution[offset].totarea = totarea;
-        distribution[offset].prim = prim.prim_id;
-        distribution[offset].mesh_light.shader_flag = shader_flag;
-        distribution[offset].mesh_light.object_id = prim.object_id;
-        offset++;
-
-        Mesh::Triangle t = mesh->get_triangle(i);
-        if (!t.valid(&mesh->get_verts()[0])) {
-          continue;
-        }
-        float3 p1 = mesh->get_verts()[t.v[0]];
-        float3 p2 = mesh->get_verts()[t.v[1]];
-        float3 p3 = mesh->get_verts()[t.v[2]];
-
-        if (!transform_applied) {
-          p1 = transform_point(&tfm, p1);
-          p2 = transform_point(&tfm, p2);
-          p3 = transform_point(&tfm, p3);
-        }
-
-        totarea += triangle_area(p1, p2, p3);
-      }
+    distribution[offset].totarea = totarea;
+    distribution[offset].prim = prim.prim_id;
+    distribution[offset].mesh_light.shader_flag = shader_flag;
+    distribution[offset].mesh_light.object_id = prim.object_id;
+    offset++;
+
+    int triangle_index = prim.prim_id - mesh->prim_offset;
+    Mesh::Triangle t = mesh->get_triangle(triangle_index);
+    if (!t.valid(&mesh->get_verts()[0])) {
+      continue;
     }
+    float3 p1 = mesh->get_verts()[t.v[0]];
+    float3 p2 = mesh->get_verts()[t.v[1]];
+    float3 p3 = mesh->get_verts()[t.v[2]];
+
+    if (!transform_applied) {
+      p1 = transform_point(&tfm, p1);
+      p2 = transform_point(&tfm, p2);
+      p3 = transform_point(&tfm, p3);
+    }
+
+    totarea += triangle_area(p1, p2, p3);
   }
 
   float trianglearea = totarea;



More information about the Bf-blender-cvs mailing list