Cleanup: make name more specific for simulations

This is necessary for the bake node (#115466).
This commit is contained in:
Jacques Lucke
2023-12-01 17:08:00 +01:00
parent b7a476ef03
commit 5de86fc4f8
6 changed files with 47 additions and 42 deletions

View File

@@ -51,7 +51,7 @@ struct PrevCache {
/**
* Stores the cached/baked data for simulation nodes in geometry nodes.
*/
struct NodeCache {
struct SimulationNodeCache {
CacheStatus cache_status = CacheStatus::Valid;
/** All cached frames. */
@@ -71,7 +71,7 @@ struct NodeCache {
struct ModifierCache {
mutable std::mutex mutex;
Map<int, std::unique_ptr<NodeCache>> cache_by_id;
Map<int, std::unique_ptr<SimulationNodeCache>> simulation_cache_by_id;
};
/**

View File

@@ -24,10 +24,10 @@
namespace blender::bke::bake {
void NodeCache::reset()
void SimulationNodeCache::reset()
{
std::destroy_at(this);
new (this) NodeCache();
new (this) SimulationNodeCache();
}
void scene_simulation_states_reset(Scene &scene)
{
@@ -40,7 +40,7 @@ void scene_simulation_states_reset(Scene &scene)
if (!nmd->runtime->cache) {
continue;
}
for (auto item : nmd->runtime->cache->cache_by_id.items()) {
for (auto item : nmd->runtime->cache->simulation_cache_by_id.items()) {
item.value->reset();
}
}

View File

@@ -102,7 +102,7 @@ static void calculate_simulation_job_startjob(void *customdata, wmJobWorkerStatu
if (!nmd->runtime->cache) {
continue;
}
for (auto item : nmd->runtime->cache->cache_by_id.items()) {
for (auto item : nmd->runtime->cache->simulation_cache_by_id.items()) {
if (item.value->cache_status != bake::CacheStatus::Baked) {
item.value->reset();
}
@@ -296,11 +296,11 @@ static void bake_simulation_job_startjob(void *customdata, wmJobWorkerStatus *wo
NodesModifierData &nmd = *modifier_bake_data.nmd;
const bake::ModifierCache &modifier_cache = *nmd.runtime->cache;
for (NodeBakeData &node_bake_data : modifier_bake_data.nodes) {
if (!modifier_cache.cache_by_id.contains(node_bake_data.id)) {
if (!modifier_cache.simulation_cache_by_id.contains(node_bake_data.id)) {
continue;
}
const bake::NodeCache &node_cache = *modifier_cache.cache_by_id.lookup(
node_bake_data.id);
const bake::SimulationNodeCache &node_cache =
*modifier_cache.simulation_cache_by_id.lookup(node_bake_data.id);
if (node_cache.frame_caches.is_empty()) {
continue;
}
@@ -340,10 +340,10 @@ static void bake_simulation_job_startjob(void *customdata, wmJobWorkerStatus *wo
for (ModifierBakeData &modifier_bake_data : object_bake_data.modifiers) {
NodesModifierData &nmd = *modifier_bake_data.nmd;
for (NodeBakeData &node_bake_data : modifier_bake_data.nodes) {
if (std::unique_ptr<bake::NodeCache> *node_cache_ptr =
nmd.runtime->cache->cache_by_id.lookup_ptr(node_bake_data.id))
if (std::unique_ptr<bake::SimulationNodeCache> *node_cache_ptr =
nmd.runtime->cache->simulation_cache_by_id.lookup_ptr(node_bake_data.id))
{
bake::NodeCache &node_cache = **node_cache_ptr;
bake::SimulationNodeCache &node_cache = **node_cache_ptr;
if (!node_cache.frame_caches.is_empty()) {
/* Tag the caches as being baked so that they are not changed anymore. */
node_cache.cache_status = bake::CacheStatus::Baked;
@@ -422,7 +422,7 @@ static Vector<ObjectBakeData> collect_nodes_to_bake(Main &bmain,
ModifierBakeData modifier_bake_data;
modifier_bake_data.nmd = nmd;
for (auto item : nmd->runtime->cache->cache_by_id.items()) {
for (auto item : nmd->runtime->cache->simulation_cache_by_id.items()) {
item.value->reset();
}
@@ -652,10 +652,10 @@ static void try_delete_bake(
}
bake::ModifierCache &modifier_cache = *nmd.runtime->cache;
std::lock_guard lock{modifier_cache.mutex};
if (!modifier_cache.cache_by_id.contains(bake_id)) {
if (!modifier_cache.simulation_cache_by_id.contains(bake_id)) {
return;
}
bake::NodeCache &node_cache = *modifier_cache.cache_by_id.lookup(bake_id);
bake::SimulationNodeCache &node_cache = *modifier_cache.simulation_cache_by_id.lookup(bake_id);
node_cache.reset();
const std::optional<bake::BakePath> bake_path = bake::get_node_bake_path(
*bmain, object, nmd, bake_id);

View File

@@ -886,10 +886,10 @@ void timeline_draw_cache(const SpaceAction *saction, const Object *ob, const Sce
const blender::bke::bake::ModifierCache &modifier_cache = *nmd->runtime->cache;
{
std::lock_guard lock{modifier_cache.mutex};
for (const std::unique_ptr<blender::bke::bake::NodeCache> &node_cache_ptr :
modifier_cache.cache_by_id.values())
for (const std::unique_ptr<blender::bke::bake::SimulationNodeCache> &node_cache_ptr :
modifier_cache.simulation_cache_by_id.values())
{
const blender::bke::bake::NodeCache &node_cache = *node_cache_ptr;
const blender::bke::bake::SimulationNodeCache &node_cache = *node_cache_ptr;
if (node_cache.frame_caches.is_empty()) {
all_simulations_baked = false;
continue;

View File

@@ -325,20 +325,22 @@ static void update_existing_bake_caches(NodesModifierData &nmd)
bake::ModifierCache &modifier_cache = *nmd.runtime->cache;
std::lock_guard lock{modifier_cache.mutex};
Map<int, std::unique_ptr<bake::NodeCache>> &old_cache_by_id = modifier_cache.cache_by_id;
Map<int, std::unique_ptr<bake::NodeCache>> new_cache_by_id;
Map<int, std::unique_ptr<bake::SimulationNodeCache>> &old_cache_by_id =
modifier_cache.simulation_cache_by_id;
Map<int, std::unique_ptr<bake::SimulationNodeCache>> new_cache_by_id;
for (const NodesModifierBake &bake : Span{nmd.bakes, nmd.bakes_num}) {
std::unique_ptr<bake::NodeCache> node_cache;
std::unique_ptr<bake::NodeCache> *old_node_cache_ptr = old_cache_by_id.lookup_ptr(bake.id);
std::unique_ptr<bake::SimulationNodeCache> node_cache;
std::unique_ptr<bake::SimulationNodeCache> *old_node_cache_ptr = old_cache_by_id.lookup_ptr(
bake.id);
if (old_node_cache_ptr == nullptr) {
node_cache = std::make_unique<bake::NodeCache>();
node_cache = std::make_unique<bake::SimulationNodeCache>();
}
else {
node_cache = std::move(*old_node_cache_ptr);
}
new_cache_by_id.add(bake.id, std::move(node_cache));
}
modifier_cache.cache_by_id = std::move(new_cache_by_id);
modifier_cache.simulation_cache_by_id = std::move(new_cache_by_id);
}
static void update_bakes_from_node_group(NodesModifierData &nmd)
@@ -752,7 +754,8 @@ class NodesModifierSimulationParams : public nodes::GeoNodesSimulationParams {
if (depsgraph_is_active_) {
/* Invalidate data on user edits. */
if (nmd.modifier.flag & eModifierFlag_UserModified) {
for (std::unique_ptr<bake::NodeCache> &node_cache : modifier_cache_->cache_by_id.values())
for (std::unique_ptr<bake::SimulationNodeCache> &node_cache :
modifier_cache_->simulation_cache_by_id.values())
{
if (node_cache->cache_status != bake::CacheStatus::Baked) {
node_cache->cache_status = bake::CacheStatus::Invalid;
@@ -761,10 +764,10 @@ class NodesModifierSimulationParams : public nodes::GeoNodesSimulationParams {
}
this->reset_invalid_node_bakes();
}
for (const std::unique_ptr<bake::NodeCache> &node_cache_ptr :
modifier_cache_->cache_by_id.values())
for (const std::unique_ptr<bake::SimulationNodeCache> &node_cache_ptr :
modifier_cache_->simulation_cache_by_id.values())
{
const bake::NodeCache &node_cache = *node_cache_ptr;
const bake::SimulationNodeCache &node_cache = *node_cache_ptr;
if (node_cache.cache_status == bake::CacheStatus::Invalid) {
has_invalid_simulation_ = true;
break;
@@ -774,9 +777,9 @@ class NodesModifierSimulationParams : public nodes::GeoNodesSimulationParams {
void reset_invalid_node_bakes()
{
for (auto item : modifier_cache_->cache_by_id.items()) {
for (auto item : modifier_cache_->simulation_cache_by_id.items()) {
const int id = item.key;
bake::NodeCache &node_cache = *item.value;
bake::SimulationNodeCache &node_cache = *item.value;
if (node_cache.cache_status != bake::CacheStatus::Invalid) {
continue;
}
@@ -820,11 +823,12 @@ class NodesModifierSimulationParams : public nodes::GeoNodesSimulationParams {
void init_simulation_info(const int zone_id, nodes::SimulationZoneBehavior &zone_behavior) const
{
if (!modifier_cache_->cache_by_id.contains(zone_id)) {
if (!modifier_cache_->simulation_cache_by_id.contains(zone_id)) {
/* Should have been created in #update_existing_bake_caches. */
return;
}
bake::NodeCache &node_cache = *modifier_cache_->cache_by_id.lookup(zone_id);
bake::SimulationNodeCache &node_cache = *modifier_cache_->simulation_cache_by_id.lookup(
zone_id);
const IndexRange sim_frame_range = *bake::get_node_bake_frame_range(
*scene_, *ctx_.object, nmd_, zone_id);
const SubFrame sim_start_frame{int(sim_frame_range.first())};
@@ -945,7 +949,7 @@ class NodesModifierSimulationParams : public nodes::GeoNodesSimulationParams {
}
}
FrameIndices get_frame_indices(const bake::NodeCache &node_cache) const
FrameIndices get_frame_indices(const bake::SimulationNodeCache &node_cache) const
{
FrameIndices frame_indices;
if (!node_cache.frame_caches.is_empty()) {
@@ -983,7 +987,7 @@ class NodesModifierSimulationParams : public nodes::GeoNodesSimulationParams {
zone_behavior.output.emplace<sim_output::PassThrough>();
}
void output_store_frame_cache(bake::NodeCache &node_cache,
void output_store_frame_cache(bake::SimulationNodeCache &node_cache,
nodes::SimulationZoneBehavior &zone_behavior) const
{
auto &store_new_state_info = zone_behavior.output.emplace<sim_output::StoreNewState>();
@@ -998,7 +1002,7 @@ class NodesModifierSimulationParams : public nodes::GeoNodesSimulationParams {
};
}
void store_as_prev_items(bake::NodeCache &node_cache,
void store_as_prev_items(bake::SimulationNodeCache &node_cache,
nodes::SimulationZoneBehavior &zone_behavior) const
{
auto &store_new_state_info = zone_behavior.output.emplace<sim_output::StoreNewState>();
@@ -1015,7 +1019,7 @@ class NodesModifierSimulationParams : public nodes::GeoNodesSimulationParams {
}
void read_from_cache(const FrameIndices &frame_indices,
bake::NodeCache &node_cache,
bake::SimulationNodeCache &node_cache,
nodes::SimulationZoneBehavior &zone_behavior) const
{
if (frame_indices.prev) {
@@ -1050,7 +1054,7 @@ class NodesModifierSimulationParams : public nodes::GeoNodesSimulationParams {
}
void read_single(const int frame_index,
bake::NodeCache &node_cache,
bake::SimulationNodeCache &node_cache,
nodes::SimulationZoneBehavior &zone_behavior) const
{
bake::FrameCache &frame_cache = *node_cache.frame_caches[frame_index];
@@ -1061,7 +1065,7 @@ class NodesModifierSimulationParams : public nodes::GeoNodesSimulationParams {
void read_interpolated(const int prev_frame_index,
const int next_frame_index,
bake::NodeCache &node_cache,
bake::SimulationNodeCache &node_cache,
nodes::SimulationZoneBehavior &zone_behavior) const
{
bake::FrameCache &prev_frame_cache = *node_cache.frame_caches[prev_frame_index];
@@ -1076,7 +1080,8 @@ class NodesModifierSimulationParams : public nodes::GeoNodesSimulationParams {
read_interpolated_info.next_state = next_frame_cache.state;
}
void ensure_bake_loaded(bake::NodeCache &node_cache, bake::FrameCache &frame_cache) const
void ensure_bake_loaded(bake::SimulationNodeCache &node_cache,
bake::FrameCache &frame_cache) const
{
if (!frame_cache.state.items_by_id.is_empty()) {
return;

View File

@@ -780,10 +780,10 @@ static void node_layout_ex(uiLayout *layout, bContext *C, PointerRNA *ptr)
if (nmd.runtime->cache) {
const bke::bake::ModifierCache &cache = *nmd.runtime->cache;
std::lock_guard lock{cache.mutex};
if (const std::unique_ptr<bke::bake::NodeCache> *node_cache_ptr = cache.cache_by_id.lookup_ptr(
*bake_id))
if (const std::unique_ptr<bke::bake::SimulationNodeCache> *node_cache_ptr =
cache.simulation_cache_by_id.lookup_ptr(*bake_id))
{
const bke::bake::NodeCache &node_cache = **node_cache_ptr;
const bke::bake::SimulationNodeCache &node_cache = **node_cache_ptr;
if (node_cache.cache_status == bke::bake::CacheStatus::Baked &&
!node_cache.frame_caches.is_empty())
{