Files
test2/source/blender/blenkernel/BKE_simulation_state_serialize.hh
Jacques Lucke 0de54b84c6 Geometry Nodes: add simulation support
This adds support for building simulations with geometry nodes. A new
`Simulation Input` and `Simulation Output` node allow maintaining a
simulation state across multiple frames. Together these two nodes form
a `simulation zone` which contains all the nodes that update the simulation
state from one frame to the next.

A new simulation zone can be added via the menu
(`Simulation > Simulation Zone`) or with the node add search.

The simulation state contains a geometry by default. However, it is possible
to add multiple geometry sockets as well as other socket types. Currently,
field inputs are evaluated and stored for the preceding geometry socket in
the order that the sockets are shown. Simulation state items can be added
by linking one of the empty sockets to something else. In the sidebar, there
is a new panel that allows adding, removing and reordering these sockets.

The simulation nodes behave as follows:
* On the first frame, the inputs of the `Simulation Input` node are evaluated
  to initialize the simulation state. In later frames these sockets are not
  evaluated anymore. The `Delta Time` at the first frame is zero, but the
  simulation zone is still evaluated.
* On every next frame, the `Simulation Input` node outputs the simulation
  state of the previous frame. Nodes in the simulation zone can edit that
  data in arbitrary ways, also taking into account the `Delta Time`. The new
  simulation state has to be passed to the `Simulation Output` node where it
  is cached and forwarded.
* On a frame that is already cached or baked, the nodes in the simulation
  zone are not evaluated, because the `Simulation Output` node can return
  the previously cached data directly.

It is not allowed to connect sockets from inside the simulation zone to the
outside without going through the `Simulation Output` node. This is a necessary
restriction to make caching and sub-frame interpolation work. Links can go into
the simulation zone without problems though.

Anonymous attributes are not propagated by the simulation nodes unless they
are explicitly stored in the simulation state. This is unfortunate, but
currently there is no practical and reliable alternative. The core problem
is detecting which anonymous attributes will be required for the simulation
and afterwards. While we can detect this for the current evaluation, we can't
look into the future in time to see what data will be necessary. We intend to
make it easier to explicitly pass data through a simulation in the future,
even if the simulation is in a nested node group.

There is a new `Simulation Nodes` panel in the physics tab in the properties
editor. It allows baking all simulation zones on the selected objects. The
baking options are intentially kept at a minimum for this MVP. More features
for simulation baking as well as baking in general can be expected to be added
separately.

All baked data is stored on disk in a folder next to the .blend file. #106937
describes how baking is implemented in more detail. Volumes can not be baked
yet and materials are lost during baking for now. Packing the baked data into
the .blend file is not yet supported.

The timeline indicates which frames are currently cached, baked or cached but
invalidated by user-changes.

Simulation input and output nodes are internally linked together by their
`bNode.identifier` which stays the same even if the node name changes. They
are generally added and removed together. However, there are still cases where
"dangling" simulation nodes can be created currently. Those generally don't
cause harm, but would be nice to avoid this in more cases in the future.

Co-authored-by: Hans Goudey <h.goudey@me.com>
Co-authored-by: Lukas Tönne <lukas@blender.org>

Pull Request: https://projects.blender.org/blender/blender/pulls/104924
2023-05-03 13:18:59 +02:00

171 lines
5.5 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
#include "BKE_simulation_state.hh"
#include "BLI_serialize.hh"
struct Main;
struct ModifierData;
namespace blender {
class fstream;
}
namespace blender::bke::sim {
using DictionaryValue = io::serialize::DictionaryValue;
using DictionaryValuePtr = std::shared_ptr<DictionaryValue>;
/**
* Reference to a slice of memory typically stored on disk.
*/
struct BDataSlice {
std::string name;
IndexRange range;
DictionaryValuePtr serialize() const;
static std::optional<BDataSlice> deserialize(const io::serialize::DictionaryValue &io_slice);
};
/**
* Abstract base class for loading binary data.
*/
class BDataReader {
public:
/**
* Read the data from the given slice into the provided memory buffer.
* \return True on success, otherwise false.
*/
[[nodiscard]] virtual bool read(const BDataSlice &slice, void *r_data) const = 0;
};
/**
* Abstract base class for writing binary data.
*/
class BDataWriter {
public:
/**
* Write the provided binary data.
* \return Slice where the data has been written to.
*/
virtual BDataSlice write(const void *data, int64_t size) = 0;
};
/**
* Allows for simple data deduplication when writing or reading data by making use of implicit
* sharing.
*/
class BDataSharing {
private:
struct StoredByRuntimeValue {
/**
* Version of the shared data that was written before. This is needed because the data might
* be changed later without changing the #ImplicitSharingInfo pointer.
*/
int64_t sharing_info_version;
/**
* Identifier of the stored data. This includes information for where the data is stored (a
* #BDataSlice) and optionally information for how it is loaded (e.g. endian information).
*/
DictionaryValuePtr io_data;
};
/**
* Map used to detect when some data has already been written. It keeps a weak reference to
* #ImplicitSharingInfo, allowing it to check for equality of two arrays just by comparing the
* sharing info's pointer and version.
*/
Map<const ImplicitSharingInfo *, StoredByRuntimeValue> stored_by_runtime_;
/**
* Use a mutex so that #read_shared can be implemented in a thread-safe way.
*/
mutable std::mutex mutex_;
/**
* Map used to detect when some data has been previously loaded. This keeps strong
* references to #ImplicitSharingInfo.
*/
mutable Map<std::string, ImplicitSharingInfoAndData> runtime_by_stored_;
public:
~BDataSharing();
/**
* Check if the data referenced by `sharing_info` has been written before. If yes, return the
* identifier for the previously written data. Otherwise, write the data now and store the
* identifier for later use.
* \return Identifier that indicates from where the data has been written.
*/
[[nodiscard]] DictionaryValuePtr write_shared(const ImplicitSharingInfo *sharing_info,
FunctionRef<DictionaryValuePtr()> write_fn);
/**
* Check if the data identified by `io_data` has been read before or load it now.
* \return Shared ownership to the read data, or none if there was an error.
*/
[[nodiscard]] std::optional<ImplicitSharingInfoAndData> read_shared(
const DictionaryValue &io_data,
FunctionRef<std::optional<ImplicitSharingInfoAndData>()> read_fn) const;
};
/**
* A specific #BDataReader that reads from disk.
*/
class DiskBDataReader : public BDataReader {
private:
const std::string bdata_dir_;
mutable std::mutex mutex_;
mutable Map<std::string, std::unique_ptr<fstream>> open_input_streams_;
public:
DiskBDataReader(std::string bdata_dir);
[[nodiscard]] bool read(const BDataSlice &slice, void *r_data) const override;
};
/**
* A specific #BDataWriter that writes to a file on disk.
*/
class DiskBDataWriter : public BDataWriter {
private:
/** Name of the file that data is written to. */
std::string bdata_name_;
/** File handle. */
std::ostream &bdata_file_;
/** Current position in the file. */
int64_t current_offset_;
public:
DiskBDataWriter(std::string bdata_name, std::ostream &bdata_file, int64_t current_offset);
BDataSlice write(const void *data, int64_t size) override;
};
/**
* Get the directory that contains all baked simulation data for the given modifier. This is a
* parent directory of the two directories below.
*/
std::string get_bake_directory(const Main &bmain, const Object &object, const ModifierData &md);
std::string get_bdata_directory(const Main &bmain, const Object &object, const ModifierData &md);
std::string get_meta_directory(const Main &bmain, const Object &object, const ModifierData &md);
/**
* Encode the simulation state in a #DictionaryValue which also contains references to external
* binary data that has been written using #bdata_writer.
*/
void serialize_modifier_simulation_state(const ModifierSimulationState &state,
BDataWriter &bdata_writer,
BDataSharing &bdata_sharing,
DictionaryValue &r_io_root);
/**
* Fill the simulation state by parsing the provided #DictionaryValue which also contains
* references to external binary data that is read using #bdata_reader.
*/
void deserialize_modifier_simulation_state(const DictionaryValue &io_root,
const BDataReader &bdata_reader,
const BDataSharing &bdata_sharing,
ModifierSimulationState &r_state);
} // namespace blender::bke::sim