Simulation Nodes: bake simulation states to disk #106937

Merged
Jacques Lucke merged 116 commits from JacquesLucke/blender:sim-bake into geometry-nodes-simulation 2023-04-22 14:48:56 +02:00
4 changed files with 31 additions and 10 deletions
Showing only changes of commit a365f346ba - Show all commits

View File

@ -174,10 +174,12 @@ struct StatesAroundFrame {
class ModifierSimulationCache {
private:
Vector<std::unique_ptr<ModifierSimulationStateAtFrame>> states_at_frames_;
CacheState cache_state_ = CacheState::Valid;
public:
void load_baked_states(StringRefNull meta_dir, StringRefNull bdata_dir);
CacheState cache_state_ = CacheState::Valid;
bool failed_finding_bake_ = false;
void try_discover_bake(StringRefNull meta_dir, StringRefNull bdata_dir);
bool has_state_at_frame(const SubFrame &frame) const
{

View File

@ -13,15 +13,24 @@ void load_simulation_state(const StringRefNull meta_path,
namespace blender::bke::sim {
void ModifierSimulationCache::load_baked_states(const StringRefNull meta_dir,
void ModifierSimulationCache::try_discover_bake(const StringRefNull meta_dir,
const StringRefNull bdata_dir)
{
this->reset();
if (failed_finding_bake_) {
return;
}
direntry *dir_entries = nullptr;
const int dir_entries_num = BLI_filelist_dir_contents(meta_dir.c_str(), &dir_entries);
BLI_SCOPED_DEFER([&]() { BLI_filelist_free(dir_entries, dir_entries_num); });
if (dir_entries_num == 0) {
failed_finding_bake_ = true;
return;
}
this->reset();
for (const int i : IndexRange(dir_entries_num)) {
const direntry &dir_entry = dir_entries[i];
const StringRefNull dir_entry_path = dir_entry.path;
@ -47,10 +56,12 @@ void ModifierSimulationCache::load_baked_states(const StringRefNull meta_dir,
void ModifierSimulationState::ensure_bake_loaded() const
{
std::scoped_lock lock{mutex_};
if (!bake_loaded_) {
ed::object::bake_simulation::load_simulation_state(
*meta_path_, *bdata_dir_, const_cast<ModifierSimulationState &>(*this));
bake_loaded_ = true;
if (meta_path_ && bdata_dir_) {
if (!bake_loaded_) {
ed::object::bake_simulation::load_simulation_state(
*meta_path_, *bdata_dir_, const_cast<ModifierSimulationState &>(*this));
bake_loaded_ = true;
}
}
}

View File

@ -862,6 +862,12 @@ static int bake_simulation_exec(bContext *C, wmOperator * /*op*/)
}
}
for (NodesModifierData *nmd : modifiers) {
if (nmd->simulation_cache) {
nmd->simulation_cache->cache_state_ = CacheState::Baked;
}
}
scene->r.cfra = old_frame;
DEG_time_tag_update(bmain);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, nullptr);

View File

@ -1216,11 +1216,13 @@ static GeometrySet compute_geometry(const bNodeTree &btree,
if (nmd_orig->simulation_cache == nullptr) {
nmd_orig->simulation_cache = MEM_new<bke::sim::ModifierSimulationCache>(__func__);
}
if (current_frame.frame() == 150) {
nmd_orig->simulation_cache->load_baked_states(
if (nmd_orig->simulation_cache->cache_state() != bke::sim::CacheState::Baked) {
nmd_orig->simulation_cache->try_discover_bake(
ed::object::bake_simulation::get_meta_directory(*bmain, *ctx->object, nmd->modifier),
ed::object::bake_simulation::get_bdata_directory(*bmain, *ctx->object, nmd->modifier));
}
if (nmd_orig->simulation_cache->cache_state() == bke::sim::CacheState::Invalid &&
current_frame == start_frame) {
nmd_orig->simulation_cache->reset();