Files
test2/source/blender/blenkernel/intern/bake_geometry_nodes_modifier.cc
Jacques Lucke 00eaddbd51 Geometry Nodes: new Bake node
This adds a new `Bake` node which allows saving and loading intermediate geometries.
Typical use cases we want address with this currently are:
* Bake some data for use with a render engine.
* Bake parts of the node tree explicitly for better performance.

For now, the format that is written to disk is not considered to be an import/export format.
It's not guaranteed that data written with one Blender version can be read by another
Blender version. For that it's better to use proper interchange formats. Better support for
those will be added eventually as well. We also plan an `Import Bake` node that allows
reading the blender-specific baked data independent of the Bake node and at different frames.

The baking works very similar to the baking in the simulation zone (UI and implementation
wise). Major differences are:
* The Bake node has a `Bake Still` and `Bake Animation` mode.
* The Bake node doesn't do automatic caching.

Implementation details:
* Refactored how we create the Python operators for moving socket items so that it also
  makes sense for non-zones.
* The `ModifierCache` stores an independent map of `SimulationNodeCache` and
  `BakeNodeCache`, but both share a common data structure for the actually baked data.
* For baking, the `Bake` node is added as a side-effect-node in the modifier. This will make
  sure that the node is baked even if it's currently not connected to the output.
* Had to add a new `DEG_id_tag_update_for_side_effect_request` function that is used
  during baking. It's necessary because I want to evaluate the object again even though none
  of its inputs changed. The reevaluation is necessary to create the baked data. Using
  `DEG_id_tag_update` technically works as well, but has the problem that it also uses the
  `DEG_UPDATE_SOURCE_USER_EDIT` flag which (rightly) invalidates simulation caches
  which shouldn't happen here.
* Slightly refactored the timeline drawing so that it can also show the baked ranges of
  Bake nodes. It does not show anything for baked nodes with a in Still mode though.
* The bake operator is refactored to bake a list of `NodeBakeRequest` which makes the
  code easier to follow compared to the previous nested
  `ObjectBakeData > ModifierBakeData > NodeBakeData` data structure.
* The bake operators are disabled when the .blend file is not yet saved. This is technically
  only necessary when the bake path depends on the .blend file path but seems ok to force
  the user anyway (otherwise the bake path may be lost as well if it's set explicitly).
* The same operators are used to bake and delete single bakes in `Bake` nodes and
  `Simulation Zones`. On top of that, there are separate operators of baking and deleting all
  simulation bakes (those ignore bake nodes).
* The `Bake` node remembers which inputs have been fields and thus may be baked as attributes.
  For that it uses an `Is Attribute` flag on the socket item. This is needed because the baked data
  may still contain attribute data, even if the inputs to the bake node are disconnected.
* Similar to simulation zones, the behavior of `Bake` nodes is passed into the geometry nodes
  evaluation from the outside (from the modifier only currently). This is done by providing the
  new `GeoNodesBakeParams` in `GeoNodesCallData` when executing geometry nodes.

Next Steps (mostly because they also involve simulations):
* Visualize nodes that have not been evaluated in the last evaluation.
* Fix issue with seemingly loosing baked data after undo.
* Improve error handling when baked data is not found.
* Show bake node in link drag search.
* Higher level tools for managing bakes.

Pull Request: https://projects.blender.org/blender/blender/pulls/115466
2023-12-18 13:01:06 +01:00

223 lines
6.7 KiB
C++

/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#include <sstream>
#include "BKE_bake_geometry_nodes_modifier.hh"
#include "BKE_collection.h"
#include "BKE_curves.hh"
#include "BKE_main.hh"
#include "DNA_modifier_types.h"
#include "DNA_node_types.h"
#include "DNA_pointcloud_types.h"
#include "BLI_binary_search.hh"
#include "BLI_fileops.hh"
#include "BLI_hash_md5.h"
#include "BLI_path_util.h"
#include "BLI_string.h"
#include "BLI_string_utils.hh"
#include "MOD_nodes.hh"
namespace blender::bke::bake {
void SimulationNodeCache::reset()
{
std::destroy_at(this);
new (this) SimulationNodeCache();
}
void BakeNodeCache::reset()
{
std::destroy_at(this);
new (this) BakeNodeCache();
}
void NodeBakeCache::reset()
{
std::destroy_at(this);
new (this) NodeBakeCache();
}
IndexRange NodeBakeCache::frame_range() const
{
if (this->frames.is_empty()) {
return {};
}
const int start_frame = this->frames.first()->frame.frame();
const int end_frame = this->frames.last()->frame.frame();
return {start_frame, end_frame - start_frame + 1};
}
SimulationNodeCache *ModifierCache::get_simulation_node_cache(const int id)
{
std::unique_ptr<SimulationNodeCache> *ptr = this->simulation_cache_by_id.lookup_ptr(id);
return ptr ? (*ptr).get() : nullptr;
}
BakeNodeCache *ModifierCache::get_bake_node_cache(const int id)
{
std::unique_ptr<BakeNodeCache> *ptr = this->bake_cache_by_id.lookup_ptr(id);
return ptr ? (*ptr).get() : nullptr;
}
NodeBakeCache *ModifierCache::get_node_bake_cache(const int id)
{
if (SimulationNodeCache *cache = this->get_simulation_node_cache(id)) {
return &cache->bake;
}
if (BakeNodeCache *cache = this->get_bake_node_cache(id)) {
return &cache->bake;
}
return nullptr;
}
void scene_simulation_states_reset(Scene &scene)
{
FOREACH_SCENE_OBJECT_BEGIN (&scene, ob) {
LISTBASE_FOREACH (ModifierData *, md, &ob->modifiers) {
if (md->type != eModifierType_Nodes) {
continue;
}
NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
if (!nmd->runtime->cache) {
continue;
}
for (auto item : nmd->runtime->cache->simulation_cache_by_id.items()) {
item.value->reset();
}
}
}
FOREACH_SCENE_OBJECT_END;
}
std::optional<std::string> get_modifier_bake_path(const Main &bmain,
const Object &object,
const NodesModifierData &nmd)
{
const StringRefNull bmain_path = BKE_main_blendfile_path(&bmain);
if (bmain_path.is_empty()) {
return std::nullopt;
}
if (StringRef(nmd.bake_directory).is_empty()) {
return std::nullopt;
}
const char *base_path = ID_BLEND_PATH(&bmain, &object.id);
char absolute_bake_dir[FILE_MAX];
STRNCPY(absolute_bake_dir, nmd.bake_directory);
BLI_path_abs(absolute_bake_dir, base_path);
return absolute_bake_dir;
}
std::optional<bake::BakePath> get_node_bake_path(const Main &bmain,
const Object &object,
const NodesModifierData &nmd,
int node_id)
{
const NodesModifierBake *bake = nmd.find_bake(node_id);
if (bake == nullptr) {
return std::nullopt;
}
if (bake->flag & NODES_MODIFIER_BAKE_CUSTOM_PATH) {
if (StringRef(bake->directory).is_empty()) {
return std::nullopt;
}
const char *base_path = ID_BLEND_PATH(&bmain, &object.id);
char absolute_bake_dir[FILE_MAX];
STRNCPY(absolute_bake_dir, bake->directory);
BLI_path_abs(absolute_bake_dir, base_path);
return bake::BakePath::from_single_root(absolute_bake_dir);
}
const std::optional<std::string> modifier_bake_path = get_modifier_bake_path(bmain, object, nmd);
if (!modifier_bake_path) {
return std::nullopt;
}
char bake_dir[FILE_MAX];
BLI_path_join(
bake_dir, sizeof(bake_dir), modifier_bake_path->c_str(), std::to_string(node_id).c_str());
return bake::BakePath::from_single_root(bake_dir);
}
static IndexRange fix_frame_range(const int start, const int end)
{
const int num_frames = std::max(1, end - start + 1);
return IndexRange(start, num_frames);
}
std::optional<IndexRange> get_node_bake_frame_range(const Scene &scene,
const Object & /*object*/,
const NodesModifierData &nmd,
int node_id)
{
const NodesModifierBake *bake = nmd.find_bake(node_id);
if (bake == nullptr) {
return std::nullopt;
}
if (bake->flag & NODES_MODIFIER_BAKE_CUSTOM_SIMULATION_FRAME_RANGE) {
return fix_frame_range(bake->frame_start, bake->frame_end);
}
if (scene.flag & SCE_CUSTOM_SIMULATION_RANGE) {
return fix_frame_range(scene.simulation_frame_start, scene.simulation_frame_end);
}
return fix_frame_range(scene.r.sfra, scene.r.efra);
}
/**
* Turn the name into something that can be used as file name. It does not necessarily have to be
* human readable, but it can help if it is at least partially readable.
*/
static std::string escape_name(const StringRef name)
{
std::stringstream ss;
for (const char c : name) {
/* Only some letters allowed. Digits are not because they could lead to name collisions. */
if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) {
ss << c;
}
else {
ss << int(c);
}
}
return ss.str();
}
static std::string get_blend_file_name(const Main &bmain)
{
const StringRefNull blend_file_path = BKE_main_blendfile_path(&bmain);
char blend_name[FILE_MAX];
BLI_path_split_file_part(blend_file_path.c_str(), blend_name, sizeof(blend_name));
const int64_t type_start_index = StringRef(blend_name).rfind(".");
if (type_start_index == StringRef::not_found) {
return "";
}
blend_name[type_start_index] = '\0';
return "blendcache_" + StringRef(blend_name);
}
static std::string get_modifier_directory_name(const Object &object, const ModifierData &md)
{
const std::string object_name_escaped = escape_name(object.id.name + 2);
const std::string modifier_name_escaped = escape_name(md.name);
return object_name_escaped + "_" + modifier_name_escaped;
}
std::string get_default_modifier_bake_directory(const Main &bmain,
const Object &object,
const NodesModifierData &nmd)
{
char dir[FILE_MAX];
/* Make path that's relative to the .blend file. */
BLI_path_join(dir,
sizeof(dir),
"//",
get_blend_file_name(bmain).c_str(),
get_modifier_directory_name(object, nmd.modifier).c_str());
return dir;
}
} // namespace blender::bke::bake