[Bf-blender-cvs] [198fd56a5e6] blender-v3.3-release: Fix T100883: crash with particle instancing and clumping

Brecht Van Lommel noreply at git.blender.org
Mon Nov 28 14:38:00 CET 2022


Commit: 198fd56a5e689eb668ccae106262c08badf23d9a
Author: Brecht Van Lommel
Date:   Wed Nov 9 20:30:41 2022 +0100
Branches: blender-v3.3-release
https://developer.blender.org/rB198fd56a5e689eb668ccae106262c08badf23d9a

Fix T100883: crash with particle instancing and clumping

Properly initialize clump curve mapping tables for duplis and other cases
where this was missed by making a generic init/free function instead of
duplicating the same logic in multiple places. Also fold lattice deform
init into this.

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

M	source/blender/blenkernel/BKE_particle.h
M	source/blender/blenkernel/intern/fluid.c
M	source/blender/blenkernel/intern/object_dupli.cc
M	source/blender/blenkernel/intern/particle.c
M	source/blender/blenkernel/intern/particle_system.c
M	source/blender/draw/intern/draw_cache_impl_particles.c
M	source/blender/io/alembic/exporter/abc_writer_points.cc
M	source/blender/modifiers/intern/MOD_explode.c
M	source/blender/modifiers/intern/MOD_particleinstance.c
M	source/blender/render/intern/texture_pointdensity.c

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

diff --git a/source/blender/blenkernel/BKE_particle.h b/source/blender/blenkernel/BKE_particle.h
index 5f8b2fafdd3..cc75a47f098 100644
--- a/source/blender/blenkernel/BKE_particle.h
+++ b/source/blender/blenkernel/BKE_particle.h
@@ -291,7 +291,11 @@ void psys_set_current_num(struct Object *ob, int index);
 /* UNUSED */
 // struct Object *psys_find_object(struct Scene *scene, struct ParticleSystem *psys);
 
-struct LatticeDeformData *psys_create_lattice_deform_data(struct ParticleSimulationData *sim);
+/**
+ * Initialize/free data for particle simulation evaluation.
+ */
+void psys_sim_data_init(struct ParticleSimulationData *sim);
+void psys_sim_data_free(struct ParticleSimulationData *sim);
 
 /**
  * For a given evaluated particle system get its original.
@@ -416,7 +420,7 @@ void psys_get_particle_on_path(struct ParticleSimulationData *sim,
                                struct ParticleKey *state,
                                bool vel);
 /**
- * Gets particle's state at a time.
+ * Gets particle's state at a time. Must call psys_sim_data_init before this.
  * \return true if particle exists and can be seen and false if not.
  */
 bool psys_get_particle_state(struct ParticleSimulationData *sim,
diff --git a/source/blender/blenkernel/intern/fluid.c b/source/blender/blenkernel/intern/fluid.c
index 0fc09803088..d45aa480ec8 100644
--- a/source/blender/blenkernel/intern/fluid.c
+++ b/source/blender/blenkernel/intern/fluid.c
@@ -1541,18 +1541,8 @@ static void emit_from_particles(Object *flow_ob,
     sim.scene = scene;
     sim.ob = flow_ob;
     sim.psys = psys;
-    sim.psys->lattice_deform_data = psys_create_lattice_deform_data(&sim);
 
-    /* prepare curvemapping tables */
-    if ((psys->part->child_flag & PART_CHILD_USE_CLUMP_CURVE) && psys->part->clumpcurve) {
-      BKE_curvemapping_changed_all(psys->part->clumpcurve);
-    }
-    if ((psys->part->child_flag & PART_CHILD_USE_ROUGH_CURVE) && psys->part->roughcurve) {
-      BKE_curvemapping_changed_all(psys->part->roughcurve);
-    }
-    if ((psys->part->child_flag & PART_CHILD_USE_TWIST_CURVE) && psys->part->twistcurve) {
-      BKE_curvemapping_changed_all(psys->part->twistcurve);
-    }
+    psys_sim_data_init(&sim);
 
     /* initialize particle cache */
     if (psys->part->type == PART_HAIR) {
@@ -1693,6 +1683,8 @@ static void emit_from_particles(Object *flow_ob,
     if (particle_vel) {
       MEM_freeN(particle_vel);
     }
+
+    psys_sim_data_free(&sim);
   }
 }
 
diff --git a/source/blender/blenkernel/intern/object_dupli.cc b/source/blender/blenkernel/intern/object_dupli.cc
index 407a2c8955c..7954dc1340a 100644
--- a/source/blender/blenkernel/intern/object_dupli.cc
+++ b/source/blender/blenkernel/intern/object_dupli.cc
@@ -1292,7 +1292,7 @@ static void make_duplis_particle_system(const DupliContext *ctx, ParticleSystem
 
     RNG *rng = BLI_rng_new_srandom(31415926u + (unsigned int)psys->seed);
 
-    psys->lattice_deform_data = psys_create_lattice_deform_data(&sim);
+    psys_sim_data_init(&sim);
 
     /* Gather list of objects or single object. */
     int totcollection = 0;
@@ -1514,17 +1514,13 @@ static void make_duplis_particle_system(const DupliContext *ctx, ParticleSystem
     }
 
     BLI_rng_free(rng);
+    psys_sim_data_free(&sim);
   }
 
   /* Clean up. */
   if (oblist) {
     MEM_freeN(oblist);
   }
-
-  if (psys->lattice_deform_data) {
-    BKE_lattice_deform_data_destroy(psys->lattice_deform_data);
-    psys->lattice_deform_data = nullptr;
-  }
 }
 
 static void make_duplis_particles(const DupliContext *ctx)
diff --git a/source/blender/blenkernel/intern/particle.c b/source/blender/blenkernel/intern/particle.c
index c5344997733..4d9187d7911 100644
--- a/source/blender/blenkernel/intern/particle.c
+++ b/source/blender/blenkernel/intern/particle.c
@@ -682,10 +682,13 @@ void psys_set_current_num(Object *ob, int index)
   }
 }
 
-struct LatticeDeformData *psys_create_lattice_deform_data(ParticleSimulationData *sim)
+void psys_sim_data_init(ParticleSimulationData *sim)
 {
-  struct LatticeDeformData *lattice_deform_data = NULL;
+  ParticleSystem *psys = sim->psys;
+  ParticleSettings *part = psys->part;
 
+  /* Prepare lattice deform. */
+  psys->lattice_deform_data = NULL;
   if (psys_in_edit_mode(sim->depsgraph, sim->psys) == 0) {
     Object *lattice = NULL;
     ModifierData *md = (ModifierData *)psys_get_modifier(sim->ob, sim->psys);
@@ -697,19 +700,39 @@ struct LatticeDeformData *psys_create_lattice_deform_data(ParticleSimulationData
         if (md->mode & mode) {
           LatticeModifierData *lmd = (LatticeModifierData *)md;
           lattice = lmd->object;
-          sim->psys->lattice_strength = lmd->strength;
+          psys->lattice_strength = lmd->strength;
         }
 
         break;
       }
     }
     if (lattice) {
-      lattice_deform_data = BKE_lattice_deform_data_create(lattice, NULL);
+      psys->lattice_deform_data = BKE_lattice_deform_data_create(lattice, NULL);
     }
   }
 
-  return lattice_deform_data;
+  /* Prepare curvemapping tables. */
+  if ((part->child_flag & PART_CHILD_USE_CLUMP_CURVE) && part->clumpcurve) {
+    BKE_curvemapping_init(part->clumpcurve);
+  }
+  if ((part->child_flag & PART_CHILD_USE_ROUGH_CURVE) && part->roughcurve) {
+    BKE_curvemapping_init(part->roughcurve);
+  }
+  if ((part->child_flag & PART_CHILD_USE_TWIST_CURVE) && part->twistcurve) {
+    BKE_curvemapping_init(part->twistcurve);
+  }
 }
+
+void psys_sim_data_free(ParticleSimulationData *sim)
+{
+  ParticleSystem *psys = sim->psys;
+
+  if (psys->lattice_deform_data) {
+    BKE_lattice_deform_data_destroy(psys->lattice_deform_data);
+    psys->lattice_deform_data = NULL;
+  }
+}
+
 void psys_disable_all(Object *ob)
 {
   ParticleSystem *psys = ob->particlesystem.first;
@@ -2775,7 +2798,7 @@ static bool psys_thread_context_init_path(ParticleThreadContext *ctx,
   ctx->cfra = cfra;
   ctx->editupdate = editupdate;
 
-  psys->lattice_deform_data = psys_create_lattice_deform_data(&ctx->sim);
+  psys_sim_data_init(&ctx->sim);
 
   /* cache all relevant vertex groups if they exist */
   ctx->vg_length = psys_cache_vgroup(ctx->mesh, psys, PSYS_VG_LENGTH);
@@ -3332,7 +3355,7 @@ void psys_cache_paths(ParticleSimulationData *sim, float cfra, const bool use_re
   cache = psys->pathcache = psys_alloc_path_cache_buffers(
       &psys->pathcachebufs, totpart, segments + 1);
 
-  psys->lattice_deform_data = psys_create_lattice_deform_data(sim);
+  psys_sim_data_init(sim);
   ma = BKE_object_material_get(sim->ob, psys->part->omat);
   if (ma && (psys->part->draw_col == PART_DRAW_COL_MAT)) {
     copy_v3_v3(col, &ma->r);
@@ -3499,10 +3522,7 @@ void psys_cache_paths(ParticleSimulationData *sim, float cfra, const bool use_re
 
   psys->totcached = totpart;
 
-  if (psys->lattice_deform_data) {
-    BKE_lattice_deform_data_destroy(psys->lattice_deform_data);
-    psys->lattice_deform_data = NULL;
-  }
+  psys_sim_data_free(sim);
 
   if (vg_effector) {
     MEM_freeN(vg_effector);
@@ -4860,6 +4880,7 @@ void psys_get_particle_on_path(ParticleSimulationData *sim,
     }
   }
 }
+
 bool psys_get_particle_state(ParticleSimulationData *sim,
                              int p,
                              ParticleKey *state,
@@ -5218,7 +5239,7 @@ void psys_apply_hair_lattice(Depsgraph *depsgraph, Scene *scene, Object *ob, Par
   sim.psys = psys;
   sim.psmd = psys_get_modifier(ob, psys);
 
-  psys->lattice_deform_data = psys_create_lattice_deform_data(&sim);
+  psys_sim_data_init(&sim);
 
   if (psys->lattice_deform_data) {
     ParticleData *pa = psys->particles;
@@ -5239,12 +5260,11 @@ void psys_apply_hair_lattice(Depsgraph *depsgraph, Scene *scene, Object *ob, Par
       }
     }
 
-    BKE_lattice_deform_data_destroy(psys->lattice_deform_data);
-    psys->lattice_deform_data = NULL;
-
     /* protect the applied shape */
     psys->flag |= PSYS_EDITED;
   }
+
+  psys_sim_data_free(&sim);
 }
 
 /* Draw Engine */
diff --git a/source/blender/blenkernel/intern/particle_system.c b/source/blender/blenkernel/intern/particle_system.c
index 4a8f029beee..548c182c3e4 100644
--- a/source/blender/blenkernel/intern/particle_system.c
+++ b/source/blender/blenkernel/intern/particle_system.c
@@ -515,10 +515,7 @@ void psys_thread_context_free(ParticleThreadContext *ctx)
     MEM_freeN(ctx->vg_twist);
   }
 
-  if (ctx->sim.psys->lattice_deform_data) {
-    BKE_lattice_deform_data_destroy(ctx->sim.psys->lattice_deform_data);
-    ctx->sim.psys->lattice_deform_data = NULL;
-  }
+  psys_sim_data_free(&ctx->sim);
 
   /* distribution */
   if (ctx->jit) {
@@ -3558,12 +3555,12 @@ static void save_hair(ParticleSimulationData *sim, float UNUSED(cfra))
 
   invert_m4_m4(ob->imat, ob->obmat);
 
-  psys->lattice_deform_data = psys_create_lattice_deform_data(sim);
-
   if (psys->totpart == 0) {
     return;
   }
 
+  psys_sim_data_init(sim);
+
   /* save new keys for elements if needed */
   LOOP_PARTICLES
   {
@@ -3597,6 +3594,8 @@ static void save_hair(ParticleSimulationData *sim, float UNUSED(cfra))
       zero_v3(root->co);
     }
   }
+
+  psys_sim_data_free(sim);
 }
 
 /* Code for an adaptive time step based on the Courant-Friedrichs-Lewy
@@ -4100,6 +4099,8 @@ static void cached_step(ParticleSimulationData *sim, float cfra, const bool use_
 
   disp = psys_get_current_display_percentage(psys, use_render_params);
 
+  psys_sim_data_init(sim);
+
   LOOP_PARTICLES
   {
     psys_get_texture(sim, pa, &ptex, PAMAP_SIZE, cfra);
@@ -4108,8 +4109,6 @@ static void cached_step(ParticleSimulationData *sim, float cfra, const bool use_
       pa->size *= 1.0f - part->randsize * psys_frand(psys, p + 1);
     }
 
-    psys->lattice_deform_data = psys_create_lattice_deform_data(sim);
-
     dietime = pa->dietime;
 
     /* update alive status and push events */
@@ -4126,11 +4125,6 @@ static void cached_step(ParticleSimulationData *sim, float cfra, const bool use_
       pa->alive = PARS_ALIVE;
     }
 
-    if (psys->lattice_deform_data) {
-      BKE_lattice_deform_data_destroy(psys->lattice_deform_data);
-      psys->lattice_deform_data = NULL;
-    }
-
     if (psys_frand(psys, p) > disp) {
       pa->flag |= PARS_NO_DISP;
     }
@@ -4138,6 +4132,8 @@ static void cached_step(ParticleSimulationData *sim, 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list