Cleanup: move bake items code to separate namespace

This commit is contained in:
Jacques Lucke
2023-09-03 13:37:24 +02:00
parent 0c11542708
commit 7f00cfeab4
16 changed files with 104 additions and 101 deletions

View File

@@ -6,7 +6,7 @@
#include "BKE_geometry_set.hh"
namespace blender::bke {
namespace blender::bke::bake {
/**
* A "bake item" contains the baked data of e.g. one node socket at one frame. Typically, multiple
@@ -103,4 +103,4 @@ class StringBakeItem : public BakeItem {
}
};
} // namespace blender::bke
} // namespace blender::bke::bake

View File

@@ -11,7 +11,7 @@
#include "BLI_sub_frame.hh"
#include "BLI_vector.hh"
namespace blender::bke::bake_paths {
namespace blender::bke::bake {
struct MetaFile {
SubFrame frame;
@@ -40,4 +40,4 @@ std::optional<SubFrame> file_name_to_frame(const StringRefNull file_name);
Vector<MetaFile> find_sorted_meta_files(const StringRefNull meta_dir);
} // namespace blender::bke::bake_paths
} // namespace blender::bke::bake

View File

@@ -9,7 +9,7 @@
#include "BKE_bake_items.hh"
namespace blender::bke {
namespace blender::bke::bake {
/**
* Reference to a slice of memory typically stored on disk.
@@ -146,4 +146,4 @@ std::optional<BakeState> deserialize_bake(std::istream &stream,
const BlobReader &blob_reader,
const BlobSharing &blob_sharing);
} // namespace blender::bke
} // namespace blender::bke::bake

View File

@@ -11,7 +11,7 @@
#include "BKE_bake_items.hh"
#include "BKE_geometry_fields.hh"
namespace blender::bke {
namespace blender::bke::bake {
/**
* Describes how bake items should be mapped to sockets.
@@ -67,4 +67,4 @@ void copy_bake_items_to_socket_values(
make_attribute_field,
Span<void *> r_socket_values);
} // namespace blender::bke
} // namespace blender::bke::bake

View File

@@ -32,13 +32,13 @@ enum class CacheState {
struct SimulationZoneFrameCache {
SubFrame frame;
BakeState state;
bake::BakeState state;
/** Used when the baked data is loaded lazily. */
std::optional<std::string> meta_path;
};
struct SimulationZonePrevState {
BakeState state;
bake::BakeState state;
SubFrame frame;
};
@@ -47,7 +47,7 @@ struct SimulationZoneCache {
std::optional<SimulationZonePrevState> prev_state;
std::optional<std::string> blobs_dir;
std::unique_ptr<BlobSharing> blob_sharing;
std::unique_ptr<bake::BlobSharing> blob_sharing;
bool failed_finding_bake = false;
CacheState cache_state = CacheState::Valid;
@@ -66,10 +66,10 @@ class ModifierSimulationCache {
*/
void scene_simulation_states_reset(Scene &scene);
std::optional<bake_paths::BakePath> get_simulation_zone_bake_path(const Main &bmain,
const Object &object,
const NodesModifierData &nmd,
int zone_id);
std::optional<bake::BakePath> get_simulation_zone_bake_path(const Main &bmain,
const Object &object,
const NodesModifierData &nmd,
int zone_id);
std::optional<std::string> get_modifier_simulation_bake_path(const Main &bmain,
const Object &object,
const NodesModifierData &nmd);

View File

@@ -20,7 +20,7 @@
#include "RNA_access.hh"
#include "RNA_enum_types.hh"
namespace blender::bke {
namespace blender::bke::bake {
using namespace io::serialize;
using DictionaryValuePtr = std::shared_ptr<DictionaryValue>;
@@ -81,4 +81,4 @@ BakeStateRef::BakeStateRef(const BakeState &bake_state)
}
}
} // namespace blender::bke
} // namespace blender::bke::bake

View File

@@ -9,7 +9,7 @@
#include "BLI_string.h"
#include "BLI_string_utils.h"
namespace blender::bke::bake_paths {
namespace blender::bke::bake {
std::string frame_to_file_name(const SubFrame &frame)
{
@@ -73,4 +73,4 @@ BakePath BakePath::from_single_root(StringRefNull root_dir)
return bake_path;
}
} // namespace blender::bke::bake_paths
} // namespace blender::bke::bake

View File

@@ -22,7 +22,7 @@
#include <sstream>
namespace blender::bke {
namespace blender::bke::bake {
using namespace io::serialize;
using DictionaryValuePtr = std::shared_ptr<DictionaryValue>;
@@ -1074,7 +1074,7 @@ void serialize_bake(const BakeState &bake_state,
io::serialize::DictionaryValue &io_items = *io_root.append_dict("items");
for (auto item : bake_state.items_by_id.items()) {
io::serialize::DictionaryValue &io_item = *io_items.append_dict(std::to_string(item.key));
bke::serialize_bake_item(*item.value, blob_writer, blob_sharing, io_item);
serialize_bake_item(*item.value, blob_writer, blob_sharing, io_item);
}
io::serialize::JsonFormatter formatter;
@@ -1128,4 +1128,4 @@ std::optional<BakeState> deserialize_bake(std::istream &stream,
return bake_state;
}
} // namespace blender::bke
} // namespace blender::bke::bake

View File

@@ -10,7 +10,7 @@
#include "FN_field_cpp_type.hh"
namespace blender::bke {
namespace blender::bke::bake {
static const CPPType &get_socket_cpp_type(const eNodeSocketDatatype socket_type)
{
@@ -288,4 +288,4 @@ void copy_bake_items_to_socket_values(
rename_attributes(geometries, attribute_map);
}
} // namespace blender::bke
} // namespace blender::bke::bake

View File

@@ -71,10 +71,10 @@ std::optional<std::string> get_modifier_simulation_bake_path(const Main &bmain,
return absolute_bake_dir;
}
std::optional<bake_paths::BakePath> get_simulation_zone_bake_path(const Main &bmain,
const Object &object,
const NodesModifierData &nmd,
int zone_id)
std::optional<bake::BakePath> get_simulation_zone_bake_path(const Main &bmain,
const Object &object,
const NodesModifierData &nmd,
int zone_id)
{
const std::optional<std::string> modifier_bake_path = get_modifier_simulation_bake_path(
bmain, object, nmd);
@@ -87,7 +87,7 @@ std::optional<bake_paths::BakePath> get_simulation_zone_bake_path(const Main &bm
sizeof(zone_bake_dir),
modifier_bake_path->c_str(),
std::to_string(zone_id).c_str());
return bake_paths::BakePath::from_single_root(zone_bake_dir);
return bake::BakePath::from_single_root(zone_bake_dir);
}
/**

View File

@@ -224,8 +224,8 @@ static bool bake_simulation_poll(bContext *C)
struct ZoneBakeData {
int zone_id;
bke::bake_paths::BakePath path;
std::unique_ptr<bke::BlobSharing> blob_sharing;
bke::bake::BakePath path;
std::unique_ptr<bke::bake::BlobSharing> blob_sharing;
};
struct ModifierBakeData {
@@ -285,10 +285,9 @@ static void bake_simulation_job_startjob(void *customdata,
for (const bNestedNodeRef &nested_node_ref : nmd->node_group->nested_node_refs_span()) {
ZoneBakeData zone_bake_data;
zone_bake_data.zone_id = nested_node_ref.id;
zone_bake_data.blob_sharing = std::make_unique<bke::BlobSharing>();
if (std::optional<bke::bake_paths::BakePath> path =
bke::sim::get_simulation_zone_bake_path(
*job.bmain, *object, *nmd, nested_node_ref.id))
zone_bake_data.blob_sharing = std::make_unique<bke::bake::BlobSharing>();
if (std::optional<bke::bake::BakePath> path = bke::sim::get_simulation_zone_bake_path(
*job.bmain, *object, *nmd, nested_node_ref.id))
{
zone_bake_data.path = std::move(*path);
modifier_bake_data.zones.append(std::move(zone_bake_data));
@@ -322,7 +321,7 @@ static void bake_simulation_job_startjob(void *customdata,
BKE_scene_graph_update_for_newframe(job.depsgraph);
const std::string frame_file_name = bke::bake_paths::frame_to_file_name(frame);
const std::string frame_file_name = bke::bake::frame_to_file_name(frame);
for (ObjectBakeData &object_bake_data : objects_to_bake) {
for (ModifierBakeData &modifier_bake_data : object_bake_data.modifiers) {
@@ -342,7 +341,7 @@ static void bake_simulation_job_startjob(void *customdata,
continue;
}
const bke::bake_paths::BakePath path = zone_bake_data.path;
const bke::bake::BakePath path = zone_bake_data.path;
const std::string blob_file_name = frame_file_name + ".blob";
@@ -357,9 +356,9 @@ static void bake_simulation_job_startjob(void *customdata,
BLI_file_ensure_parent_dir_exists(meta_path);
BLI_file_ensure_parent_dir_exists(blob_path);
fstream blob_file{blob_path, std::ios::out | std::ios::binary};
bke::DiskBlobWriter blob_writer{blob_file_name, blob_file, 0};
bke::bake::DiskBlobWriter blob_writer{blob_file_name, blob_file, 0};
fstream meta_file{meta_path, std::ios::out};
bke::serialize_bake(
bke::bake::serialize_bake(
frame_cache.state, blob_writer, *zone_bake_data.blob_sharing, meta_file);
}
}
@@ -635,7 +634,7 @@ static int delete_baked_simulation_exec(bContext *C, wmOperator *op)
for (auto item : nmd->runtime->simulation_cache->cache_by_zone_id.items()) {
item.value->reset();
const std::optional<bke::bake_paths::BakePath> bake_path =
const std::optional<bke::bake::BakePath> bake_path =
bke::sim::get_simulation_zone_bake_path(*bmain, *object, *nmd, item.key);
if (!bake_path) {
continue;

View File

@@ -753,23 +753,23 @@ class NodesModifierSimulationParams : public nodes::GeoNodesSimulationParams {
/* Try load baked data. */
if (!zone_cache.failed_finding_bake) {
if (zone_cache.cache_state != CacheState::Baked) {
if (std::optional<bke::bake_paths::BakePath> zone_bake_path =
get_simulation_zone_bake_path(*bmain_, *ctx_.object, nmd_, zone_id))
if (std::optional<bke::bake::BakePath> zone_bake_path = get_simulation_zone_bake_path(
*bmain_, *ctx_.object, nmd_, zone_id))
{
Vector<bke::bake_paths::MetaFile> meta_files = bke::bake_paths::find_sorted_meta_files(
Vector<bke::bake::MetaFile> meta_files = bke::bake::find_sorted_meta_files(
zone_bake_path->meta_dir);
if (!meta_files.is_empty()) {
zone_cache.reset();
for (const bke::bake_paths::MetaFile &meta_file : meta_files) {
for (const bke::bake::MetaFile &meta_file : meta_files) {
auto frame_cache = std::make_unique<SimulationZoneFrameCache>();
frame_cache->frame = meta_file.frame;
frame_cache->meta_path = meta_file.path;
zone_cache.frame_caches.append(std::move(frame_cache));
}
zone_cache.blobs_dir = zone_bake_path->blobs_dir;
zone_cache.blob_sharing = std::make_unique<bke::BlobSharing>();
zone_cache.blob_sharing = std::make_unique<bke::bake::BlobSharing>();
zone_cache.cache_state = CacheState::Baked;
}
}
@@ -899,15 +899,16 @@ class NodesModifierSimulationParams : public nodes::GeoNodesSimulationParams {
{
auto &store_and_pass_through_info =
zone_behavior.output.emplace<sim_output::StoreAndPassThrough>();
store_and_pass_through_info.store_fn = [simulation_cache = simulation_cache_,
zone_cache = &zone_cache,
current_frame = current_frame_](bke::BakeState state) {
std::lock_guard lock{simulation_cache->mutex};
auto frame_cache = std::make_unique<bke::sim::SimulationZoneFrameCache>();
frame_cache->frame = current_frame;
frame_cache->state = std::move(state);
zone_cache->frame_caches.append(std::move(frame_cache));
};
store_and_pass_through_info.store_fn =
[simulation_cache = simulation_cache_,
zone_cache = &zone_cache,
current_frame = current_frame_](bke::bake::BakeState state) {
std::lock_guard lock{simulation_cache->mutex};
auto frame_cache = std::make_unique<bke::sim::SimulationZoneFrameCache>();
frame_cache->frame = current_frame;
frame_cache->state = std::move(state);
zone_cache->frame_caches.append(std::move(frame_cache));
};
}
void store_as_prev_items(bke::sim::SimulationZoneCache &zone_cache,
@@ -915,16 +916,17 @@ class NodesModifierSimulationParams : public nodes::GeoNodesSimulationParams {
{
auto &store_and_pass_through_info =
zone_behavior.output.emplace<sim_output::StoreAndPassThrough>();
store_and_pass_through_info.store_fn = [simulation_cache = simulation_cache_,
zone_cache = &zone_cache,
current_frame = current_frame_](bke::BakeState state) {
std::lock_guard lock{simulation_cache->mutex};
if (!zone_cache->prev_state) {
zone_cache->prev_state.emplace();
}
zone_cache->prev_state->state = std::move(state);
zone_cache->prev_state->frame = current_frame;
};
store_and_pass_through_info.store_fn =
[simulation_cache = simulation_cache_,
zone_cache = &zone_cache,
current_frame = current_frame_](bke::bake::BakeState state) {
std::lock_guard lock{simulation_cache->mutex};
if (!zone_cache->prev_state) {
zone_cache->prev_state.emplace();
}
zone_cache->prev_state->state = std::move(state);
zone_cache->prev_state->frame = current_frame;
};
}
void read_from_cache(const FrameIndices &frame_indices,
@@ -1009,9 +1011,9 @@ class NodesModifierSimulationParams : public nodes::GeoNodesSimulationParams {
if (!frame_cache.meta_path) {
return;
}
bke::DiskBlobReader blob_reader{*zone_cache.blobs_dir};
bke::bake::DiskBlobReader blob_reader{*zone_cache.blobs_dir};
fstream meta_file{*frame_cache.meta_path};
std::optional<bke::BakeState> bake_state = bke::deserialize_bake(
std::optional<bke::bake::BakeState> bake_state = bke::bake::deserialize_bake(
meta_file, blob_reader, *zone_cache.blob_sharing);
if (!bake_state.has_value()) {
return;

View File

@@ -57,7 +57,7 @@ struct PassThrough {
*/
struct OutputCopy {
float delta_time;
bke::BakeStateRef state;
bke::bake::BakeStateRef state;
};
/**
@@ -66,7 +66,7 @@ struct OutputCopy {
*/
struct OutputMove {
float delta_time;
bke::BakeState state;
bke::bake::BakeState state;
};
using Behavior = std::variant<PassThrough, OutputCopy, OutputMove>;
@@ -88,14 +88,14 @@ struct PassThrough {
* This allows the caller of geometry nodes (e.g. the modifier), to cache the new simulation state.
*/
struct StoreAndPassThrough {
std::function<void(bke::BakeState state)> store_fn;
std::function<void(bke::bake::BakeState state)> store_fn;
};
/**
* The inputs are not evaluated, instead the given cached items are output directly.
*/
struct ReadSingle {
bke::BakeStateRef state;
bke::bake::BakeStateRef state;
};
/**
@@ -104,8 +104,8 @@ struct ReadSingle {
struct ReadInterpolated {
/** Factor between 0 and 1 that determines the influence of the two simulation states. */
float mix_factor;
bke::BakeStateRef prev_state;
bke::BakeStateRef next_state;
bke::bake::BakeStateRef prev_state;
bke::bake::BakeStateRef next_state;
};
using Behavior = std::variant<PassThrough, StoreAndPassThrough, ReadSingle, ReadInterpolated>;

View File

@@ -137,16 +137,16 @@ void socket_declarations_for_simulation_items(Span<NodeSimulationItem> items,
const CPPType &get_simulation_item_cpp_type(eNodeSocketDatatype socket_type);
const CPPType &get_simulation_item_cpp_type(const NodeSimulationItem &item);
bke::BakeState move_values_to_simulation_state(
bke::bake::BakeState move_values_to_simulation_state(
const Span<NodeSimulationItem> node_simulation_items, const Span<void *> input_values);
void move_simulation_state_to_values(const Span<NodeSimulationItem> node_simulation_items,
bke::BakeState zone_state,
bke::bake::BakeState zone_state,
const Object &self_object,
const ComputeContext &compute_context,
const bNode &sim_output_node,
Span<void *> r_output_values);
void copy_simulation_state_to_values(const Span<NodeSimulationItem> node_simulation_items,
const bke::BakeStateRef &zone_state,
const bke::bake::BakeStateRef &zone_state,
const Object &self_object,
const ComputeContext &compute_context,
const bNode &sim_output_node,

View File

@@ -106,7 +106,7 @@ class LazyFunctionForSimulationInputNode final : public LazyFunction {
void output_simulation_state_copy(lf::Params &params,
const GeoNodesLFUserData &user_data,
const bke::BakeStateRef &zone_state) const
const bke::bake::BakeStateRef &zone_state) const
{
Array<void *> outputs(simulation_items_.size());
for (const int i : simulation_items_.index_range()) {
@@ -125,7 +125,7 @@ class LazyFunctionForSimulationInputNode final : public LazyFunction {
void output_simulation_state_move(lf::Params &params,
const GeoNodesLFUserData &user_data,
bke::BakeState zone_state) const
bke::bake::BakeState zone_state) const
{
Array<void *> outputs(simulation_items_.size());
for (const int i : simulation_items_.index_range()) {
@@ -155,7 +155,8 @@ class LazyFunctionForSimulationInputNode final : public LazyFunction {
/* Instead of outputting the initial values directly, convert them to a simulation state and
* then back. This ensures that some geometry processing happens on the data consistently (e.g.
* removing anonymous attributes). */
bke::BakeState bake_state = move_values_to_simulation_state(simulation_items_, input_values);
bke::bake::BakeState bake_state = move_values_to_simulation_state(simulation_items_,
input_values);
this->output_simulation_state_move(params, user_data, std::move(bake_state));
}
};

View File

@@ -149,10 +149,10 @@ const CPPType &get_simulation_item_cpp_type(const NodeSimulationItem &item)
return get_simulation_item_cpp_type(eNodeSocketDatatype(item.socket_type));
}
static bke::BakeSocketConfig make_bake_socket_config(
static bke::bake::BakeSocketConfig make_bake_socket_config(
const Span<NodeSimulationItem> node_simulation_items)
{
bke::BakeSocketConfig config;
bke::bake::BakeSocketConfig config;
const int items_num = node_simulation_items.size();
config.domains.resize(items_num);
config.types.resize(items_num);
@@ -186,20 +186,20 @@ static std::shared_ptr<AnonymousAttributeFieldInput> make_attribute_field(
}
void move_simulation_state_to_values(const Span<NodeSimulationItem> node_simulation_items,
bke::BakeState zone_state,
bke::bake::BakeState zone_state,
const Object &self_object,
const ComputeContext &compute_context,
const bNode &node,
Span<void *> r_output_values)
{
const bke::BakeSocketConfig config = make_bake_socket_config(node_simulation_items);
Vector<bke::BakeItem *> bake_items;
const bke::bake::BakeSocketConfig config = make_bake_socket_config(node_simulation_items);
Vector<bke::bake::BakeItem *> bake_items;
for (const NodeSimulationItem &item : node_simulation_items) {
auto *bake_item = zone_state.items_by_id.lookup_ptr(item.identifier);
bake_items.append(bake_item ? bake_item->get() : nullptr);
}
bke::move_bake_items_to_socket_values(
bke::bake::move_bake_items_to_socket_values(
bake_items,
config,
[&](const int i, const CPPType &type) {
@@ -210,20 +210,21 @@ void move_simulation_state_to_values(const Span<NodeSimulationItem> node_simulat
}
void copy_simulation_state_to_values(const Span<NodeSimulationItem> node_simulation_items,
const bke::BakeStateRef &zone_state,
const bke::bake::BakeStateRef &zone_state,
const Object &self_object,
const ComputeContext &compute_context,
const bNode &node,
Span<void *> r_output_values)
{
const bke::BakeSocketConfig config = make_bake_socket_config(node_simulation_items);
Vector<const bke::BakeItem *> bake_items;
const bke::bake::BakeSocketConfig config = make_bake_socket_config(node_simulation_items);
Vector<const bke::bake::BakeItem *> bake_items;
for (const NodeSimulationItem &item : node_simulation_items) {
const bke::BakeItem *const *bake_item = zone_state.items_by_id.lookup_ptr(item.identifier);
const bke::bake::BakeItem *const *bake_item = zone_state.items_by_id.lookup_ptr(
item.identifier);
bake_items.append(bake_item ? *bake_item : nullptr);
}
bke::copy_bake_items_to_socket_values(
bke::bake::copy_bake_items_to_socket_values(
bake_items,
config,
[&](const int i, const CPPType &type) {
@@ -233,18 +234,18 @@ void copy_simulation_state_to_values(const Span<NodeSimulationItem> node_simulat
r_output_values);
}
bke::BakeState move_values_to_simulation_state(
bke::bake::BakeState move_values_to_simulation_state(
const Span<NodeSimulationItem> node_simulation_items, const Span<void *> input_values)
{
const bke::BakeSocketConfig config = make_bake_socket_config(node_simulation_items);
const bke::bake::BakeSocketConfig config = make_bake_socket_config(node_simulation_items);
Array<std::unique_ptr<bke::BakeItem>> bake_items = bke::move_socket_values_to_bake_items(
input_values, config);
Array<std::unique_ptr<bke::bake::BakeItem>> bake_items =
bke::bake::move_socket_values_to_bake_items(input_values, config);
bke::BakeState bake_state;
bke::bake::BakeState bake_state;
for (const int i : node_simulation_items.index_range()) {
const NodeSimulationItem &item = node_simulation_items[i];
std::unique_ptr<bke::BakeItem> &bake_item = bake_items[i];
std::unique_ptr<bke::bake::BakeItem> &bake_item = bake_items[i];
if (bake_item) {
bake_state.items_by_id.add_new(item.identifier, std::move(bake_item));
}
@@ -591,7 +592,7 @@ class LazyFunctionForSimulationOutputNode final : public LazyFunction {
void output_cached_state(lf::Params &params,
GeoNodesLFUserData &user_data,
const bke::BakeStateRef &state) const
const bke::bake::BakeStateRef &state) const
{
Array<void *> output_values(simulation_items_.size());
for (const int i : simulation_items_.index_range()) {
@@ -611,8 +612,8 @@ class LazyFunctionForSimulationOutputNode final : public LazyFunction {
void output_mixed_cached_state(lf::Params &params,
const Object &self_object,
const ComputeContext &compute_context,
const bke::BakeStateRef &prev_state,
const bke::BakeStateRef &next_state,
const bke::bake::BakeStateRef &prev_state,
const bke::bake::BakeStateRef &next_state,
const float mix_factor) const
{
Array<void *> output_values(simulation_items_.size());
@@ -650,7 +651,7 @@ class LazyFunctionForSimulationOutputNode final : public LazyFunction {
/* Instead of outputting the initial values directly, convert them to a simulation state and
* then back. This ensures that some geometry processing happens on the data consistently (e.g.
* removing anonymous attributes). */
std::optional<bke::BakeState> bake_state = this->get_bake_state_from_inputs(params);
std::optional<bke::bake::BakeState> bake_state = this->get_bake_state_from_inputs(params);
if (!bake_state) {
/* Wait for inputs to be computed. */
return;
@@ -675,7 +676,7 @@ class LazyFunctionForSimulationOutputNode final : public LazyFunction {
GeoNodesLFUserData &user_data,
const sim_output::StoreAndPassThrough &info) const
{
std::optional<bke::BakeState> bake_state = this->get_bake_state_from_inputs(params);
std::optional<bke::bake::BakeState> bake_state = this->get_bake_state_from_inputs(params);
if (!bake_state) {
/* Wait for inputs to be computed. */
return;
@@ -684,7 +685,7 @@ class LazyFunctionForSimulationOutputNode final : public LazyFunction {
info.store_fn(std::move(*bake_state));
}
std::optional<bke::BakeState> get_bake_state_from_inputs(lf::Params &params) const
std::optional<bke::bake::BakeState> get_bake_state_from_inputs(lf::Params &params) const
{
Array<void *> input_values(inputs_.size());
for (const int i : inputs_.index_range()) {