diff options
Diffstat (limited to 'source/blender/simulation/intern/simulation_solver.cc')
-rw-r--r-- | source/blender/simulation/intern/simulation_solver.cc | 67 |
1 files changed, 62 insertions, 5 deletions
diff --git a/source/blender/simulation/intern/simulation_solver.cc b/source/blender/simulation/intern/simulation_solver.cc index f542d6ab2c8..158c50957df 100644 --- a/source/blender/simulation/intern/simulation_solver.cc +++ b/source/blender/simulation/intern/simulation_solver.cc @@ -17,8 +17,11 @@ #include "simulation_solver.hh" #include "BKE_customdata.h" +#include "BKE_lib_id.h" +#include "BKE_persistent_data_handle.hh" #include "BLI_rand.hh" +#include "BLI_set.hh" namespace blender::sim { @@ -243,6 +246,53 @@ BLI_NOINLINE static void remove_dead_and_add_new_particles(ParticleSimulationSta state.next_particle_id += allocator.total_allocated(); } +static void update_persistent_data_handles(Simulation &simulation, + const VectorSet<ID *> &used_data_blocks) +{ + Set<ID *> contained_ids; + Set<int> used_handles; + + /* Remove handles that have been invalidated. */ + LISTBASE_FOREACH_MUTABLE ( + PersistentDataHandleItem *, handle_item, &simulation.persistent_data_handles) { + if (handle_item->id == nullptr) { + BLI_remlink(&simulation.persistent_data_handles, handle_item); + continue; + } + if (!used_data_blocks.contains(handle_item->id)) { + id_us_min(handle_item->id); + BLI_remlink(&simulation.persistent_data_handles, handle_item); + MEM_freeN(handle_item); + continue; + } + contained_ids.add_new(handle_item->id); + used_handles.add_new(handle_item->handle); + } + + /* Add new handles that are not in the list yet. */ + int next_handle = 0; + for (ID *id : used_data_blocks) { + if (contained_ids.contains(id)) { + continue; + } + + /* Find the next available handle. */ + while (used_handles.contains(next_handle)) { + next_handle++; + } + used_handles.add_new(next_handle); + + PersistentDataHandleItem *handle_item = (PersistentDataHandleItem *)MEM_callocN( + sizeof(*handle_item), AT); + /* Cannot store const pointers in DNA. */ + id_us_plus(id); + handle_item->id = id; + handle_item->handle = next_handle; + + BLI_addtail(&simulation.persistent_data_handles, handle_item); + } +} + void initialize_simulation_states(Simulation &simulation, Depsgraph &UNUSED(depsgraph), const SimulationInfluences &UNUSED(influences)) @@ -255,11 +305,18 @@ void solve_simulation_time_step(Simulation &simulation, const SimulationInfluences &influences, float time_step) { - SimulationSolveContext solve_context{ - simulation, - depsgraph, - influences, - TimeInterval(simulation.current_simulation_time, time_step)}; + update_persistent_data_handles(simulation, influences.used_data_blocks); + + bke::PersistentDataHandleMap handle_map; + LISTBASE_FOREACH (PersistentDataHandleItem *, handle, &simulation.persistent_data_handles) { + handle_map.add(handle->handle, *handle->id); + } + + SimulationSolveContext solve_context{simulation, + depsgraph, + influences, + TimeInterval(simulation.current_simulation_time, time_step), + handle_map}; TimeInterval simulation_time_interval{simulation.current_simulation_time, time_step}; Vector<SimulationState *> simulation_states{simulation.states}; |