diff options
author | Jacques Lucke <jacques@blender.org> | 2020-07-22 18:04:18 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2020-07-22 20:16:33 +0300 |
commit | e0400d0a1c76d019dc52efe96e869948464aff61 (patch) | |
tree | a5e8feeb50c5d7c13ea189c5470bff5b0804c73c /source/blender/simulation/intern/simulation_update.cc | |
parent | 0a5ef40c2cd77a5c8dfdbcc888759fb0dab9e184 (diff) |
Simulation: move responsibility of updating persistent data handles
This solver should not be responsible for updating the handles.
Diffstat (limited to 'source/blender/simulation/intern/simulation_update.cc')
-rw-r--r-- | source/blender/simulation/intern/simulation_update.cc | 66 |
1 files changed, 63 insertions, 3 deletions
diff --git a/source/blender/simulation/intern/simulation_update.cc b/source/blender/simulation/intern/simulation_update.cc index 7a748ee57d1..117a98d2285 100644 --- a/source/blender/simulation/intern/simulation_update.cc +++ b/source/blender/simulation/intern/simulation_update.cc @@ -17,6 +17,7 @@ #include "SIM_simulation_update.hh" #include "BKE_customdata.h" +#include "BKE_lib_id.h" #include "BKE_simulation.h" #include "DNA_scene_types.h" @@ -29,6 +30,7 @@ #include "BLI_listbase.h" #include "BLI_map.hh" #include "BLI_rand.h" +#include "BLI_set.hh" #include "BLI_vector.hh" #include "particle_function.hh" @@ -88,6 +90,54 @@ static void update_simulation_state_list(Simulation *simulation, add_missing_states(simulation, required_states); } +static void update_persistent_data_handles(Simulation &simulation_orig, + const UsedPersistentData &used_persistent_data) +{ + Set<ID *> contained_ids; + Set<int> used_handles; + + /* Remove handles that have been invalidated. */ + LISTBASE_FOREACH_MUTABLE ( + PersistentDataHandleItem *, handle_item, &simulation_orig.persistent_data_handles) { + if (handle_item->id == nullptr) { + BLI_remlink(&simulation_orig.persistent_data_handles, handle_item); + MEM_freeN(handle_item); + continue; + } + if (!used_persistent_data.used_ids().contains(handle_item->id)) { + id_us_min(handle_item->id); + BLI_remlink(&simulation_orig.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_persistent_data.used_ids()) { + 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_orig.persistent_data_handles, handle_item); + } +} + void update_simulation_in_depsgraph(Depsgraph *depsgraph, Scene *scene_cow, Simulation *simulation_cow) @@ -107,13 +157,23 @@ void update_simulation_in_depsgraph(Depsgraph *depsgraph, ResourceCollector resources; SimulationInfluences influences; RequiredStates required_states; + UsedPersistentData used_persistent_data; + + collect_simulation_influences( + *simulation_cow, resources, influences, required_states, used_persistent_data); + update_persistent_data_handles(*simulation_orig, used_persistent_data); - collect_simulation_influences(*simulation_cow, resources, influences, required_states); + bke::PersistentDataHandleMap handle_map; + LISTBASE_FOREACH ( + PersistentDataHandleItem *, handle_item, &simulation_orig->persistent_data_handles) { + ID *id_cow = DEG_get_evaluated_id(depsgraph, handle_item->id); + handle_map.add(handle_item->handle, *id_cow); + } if (current_frame == 1) { reinitialize_empty_simulation_states(simulation_orig, required_states); - initialize_simulation_states(*simulation_orig, *depsgraph, influences); + initialize_simulation_states(*simulation_orig, *depsgraph, influences, handle_map); simulation_orig->current_frame = 1; copy_states_to_cow(simulation_orig, simulation_cow); @@ -122,7 +182,7 @@ void update_simulation_in_depsgraph(Depsgraph *depsgraph, update_simulation_state_list(simulation_orig, required_states); float time_step = 1.0f / 24.0f; - solve_simulation_time_step(*simulation_orig, *depsgraph, influences, time_step); + solve_simulation_time_step(*simulation_orig, *depsgraph, influences, handle_map, time_step); simulation_orig->current_frame = current_frame; copy_states_to_cow(simulation_orig, simulation_cow); |