This refactors how volume grids are stored with the following new goals in mind:
* Get a **stand-alone volume grid** data structure that can be used by geometry nodes.
Previously, the `VolumeGrid` data structure was tightly coupled with the `Volume` data block.
* Support **implicit sharing of grids and trees**. Previously, it was possible to share data
when multiple `Volume` data blocks loaded grids from the same `.vdb` files but this was
not flexible enough.
* Get a safe API for **lazy-loading and unloading** of grids without requiring explicit calls
to some "load" function all the time.
* Get a safe API for **caching grids from files** that is not coupled to the `Volume` data block.
* Get a **tiered API** for different levels of `openvdb` involvement:
* No `OpenVDB`: Since `WITH_OPENVDB` is optional, it's helpful to have parts of the API that
still work in this case. This makes it possible to write high level code for volumes that does
not require `#ifdef WITH_OPENVDB` checks everywhere. This is in `BKE_volume_grid_fwd.hh`.
* Shallow `OpenVDB`: Code using this API requires `WITH_OPENVDB` checks. However, care
is taken to not include the expensive parts of `OpenVDB` and to use forward declarations as
much as possible. This is in `BKE_volume_grid.hh` and uses `openvdb_fwd.hh`.
* "Full" `OpenVDB`: This API requires more heavy `OpenVDB` includes. Fortunately, it turned
out to be not necessary for the common API. So this is only used for task specific APIs.
At the core of the new API is the `VolumeGridData` type. It's a wrapper around an
`openvdb::Grid` and adds some features on top like implicit sharing, lazy-loading and unloading.
Then there are `GVolumeGrid` and `VolumeGrid` which are containers for a volume grid.
Semantically, each `VolumeGrid` has its own independent grid, but this is cheap due to implicit
sharing. At highest level we currently have the `Volume` data-block which contains a list of
`VolumeGrid`.
```mermaid
flowchart LR
Volume --> VolumeGrid --> VolumeGridData --> openvdb::Grid
```
The loading of `.vdb` files is abstracted away behind the volume file cache API. This API makes
it easy to load and reuse entire files and individual grids from disk. It also supports caching
simplify levels for grids on disk.
An important new concept are the "tree access tokens". Whenever some code wants to work
with an openvdb tree, it has to retrieve an access token from the corresponding `VolumeGridData`.
This access token has to be kept alive for as long as the code works with the grid data. The same
token is valid for read and write access. The purpose of these access tokens is to make it possible
to detect when some code is currently working with the openvdb tree. This allows freeing it if it's
possible to reload it later on (e.g. from disk). It's possible to free a tree that is referenced by
multiple owners, but only no one is actively working with. In some sense, this is similar to the
existing `ImageUser` concept.
The most important new files to read are `BKE_volume_grid.hh` and `BKE_volume_grid_file_cache.hh`.
Most other changes are updates to existing code to use the new API.
Pull Request: https://projects.blender.org/blender/blender/pulls/116315
192 lines
6.4 KiB
C++
192 lines
6.4 KiB
C++
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#include "usd_writer_volume.h"
|
|
|
|
#include <pxr/base/tf/pathUtils.h>
|
|
#include <pxr/usd/usdVol/openVDBAsset.h>
|
|
#include <pxr/usd/usdVol/volume.h>
|
|
|
|
#include "DNA_volume_types.h"
|
|
#include "DNA_windowmanager_types.h"
|
|
|
|
#include "BKE_report.h"
|
|
#include "BKE_volume.hh"
|
|
|
|
#include "BLI_fileops.h"
|
|
#include "BLI_index_range.hh"
|
|
#include "BLI_math_base.h"
|
|
#include "BLI_path_util.h"
|
|
#include "BLI_string.h"
|
|
|
|
#include "WM_api.hh"
|
|
|
|
#include "usd_hierarchy_iterator.h"
|
|
|
|
namespace blender::io::usd {
|
|
|
|
USDVolumeWriter::USDVolumeWriter(const USDExporterContext &ctx) : USDAbstractWriter(ctx) {}
|
|
|
|
bool USDVolumeWriter::check_is_animated(const HierarchyContext &context) const
|
|
{
|
|
const Volume *volume = static_cast<Volume *>(context.object->data);
|
|
return volume->is_sequence;
|
|
}
|
|
|
|
void USDVolumeWriter::do_write(HierarchyContext &context)
|
|
{
|
|
Volume *volume = static_cast<Volume *>(context.object->data);
|
|
if (!BKE_volume_load(volume, usd_export_context_.bmain)) {
|
|
return;
|
|
}
|
|
|
|
const int num_grids = BKE_volume_num_grids(volume);
|
|
if (!num_grids) {
|
|
return;
|
|
}
|
|
|
|
auto vdb_file_path = resolve_vdb_file(volume);
|
|
if (!vdb_file_path.has_value()) {
|
|
BKE_reportf(reports(),
|
|
RPT_WARNING,
|
|
"USD Export: failed to resolve .vdb file for object: %s",
|
|
volume->id.name + 2);
|
|
return;
|
|
}
|
|
|
|
if (usd_export_context_.export_params.relative_paths) {
|
|
if (auto relative_vdb_file_path = construct_vdb_relative_file_path(*vdb_file_path)) {
|
|
vdb_file_path = relative_vdb_file_path;
|
|
}
|
|
else {
|
|
BKE_reportf(reports(),
|
|
RPT_WARNING,
|
|
"USD Export: couldn't construct relative file path for .vdb file, absolute path "
|
|
"will be used instead");
|
|
}
|
|
}
|
|
|
|
const pxr::UsdTimeCode timecode = get_export_time_code();
|
|
const pxr::SdfPath &volume_path = usd_export_context_.usd_path;
|
|
pxr::UsdStageRefPtr stage = usd_export_context_.stage;
|
|
pxr::UsdVolVolume usd_volume = pxr::UsdVolVolume::Define(stage, volume_path);
|
|
|
|
for (const int i : IndexRange(num_grids)) {
|
|
const bke::VolumeGridData *grid = BKE_volume_grid_get(volume, i);
|
|
const std::string grid_name = bke::volume_grid::get_name(*grid);
|
|
const std::string grid_id = pxr::TfMakeValidIdentifier(grid_name);
|
|
const pxr::SdfPath grid_path = volume_path.AppendPath(pxr::SdfPath(grid_id));
|
|
pxr::UsdVolOpenVDBAsset usd_grid = pxr::UsdVolOpenVDBAsset::Define(stage, grid_path);
|
|
usd_grid.GetFieldNameAttr().Set(pxr::TfToken(grid_name), timecode);
|
|
usd_grid.GetFilePathAttr().Set(pxr::SdfAssetPath(*vdb_file_path), timecode);
|
|
usd_volume.CreateFieldRelationship(pxr::TfToken(grid_id), grid_path);
|
|
}
|
|
|
|
if (const std::optional<Bounds<float3>> bounds = BKE_volume_min_max(volume)) {
|
|
const pxr::VtArray<pxr::GfVec3f> volume_extent = {pxr::GfVec3f(&bounds->min.x),
|
|
pxr::GfVec3f(&bounds->max.x)};
|
|
usd_volume.GetExtentAttr().Set(volume_extent, timecode);
|
|
}
|
|
|
|
BKE_volume_unload(volume);
|
|
}
|
|
|
|
std::optional<std::string> USDVolumeWriter::resolve_vdb_file(const Volume *volume) const
|
|
{
|
|
std::optional<std::string> vdb_file_path;
|
|
if (volume->filepath[0] == '\0') {
|
|
/* Entering this section should mean that Volume object contains OpenVDB data that is not
|
|
* obtained from external .vdb file but rather generated inside of Blender (i.e. by 'Mesh to
|
|
* Volume' modifier). Try to save this data to a .vdb file. */
|
|
|
|
vdb_file_path = construct_vdb_file_path(volume);
|
|
if (!BKE_volume_save(
|
|
volume, usd_export_context_.bmain, nullptr, vdb_file_path.value_or("").c_str()))
|
|
{
|
|
return std::nullopt;
|
|
}
|
|
}
|
|
|
|
if (!vdb_file_path.has_value()) {
|
|
vdb_file_path = BKE_volume_grids_frame_filepath(volume);
|
|
if (vdb_file_path->empty()) {
|
|
return std::nullopt;
|
|
}
|
|
}
|
|
|
|
return vdb_file_path;
|
|
}
|
|
|
|
std::optional<std::string> USDVolumeWriter::construct_vdb_file_path(const Volume *volume) const
|
|
{
|
|
const std::string usd_file_path = get_export_file_path();
|
|
if (usd_file_path.empty()) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
char usd_directory_path[FILE_MAX];
|
|
char usd_file_name[FILE_MAXFILE];
|
|
BLI_path_split_dir_file(usd_file_path.c_str(),
|
|
usd_directory_path,
|
|
sizeof(usd_directory_path),
|
|
usd_file_name,
|
|
sizeof(usd_file_name));
|
|
|
|
if (usd_directory_path[0] == '\0' || usd_file_name[0] == '\0') {
|
|
return std::nullopt;
|
|
}
|
|
|
|
const char *vdb_directory_name = "volumes";
|
|
|
|
char vdb_directory_path[FILE_MAX];
|
|
STRNCPY(vdb_directory_path, usd_directory_path);
|
|
BLI_strncat(vdb_directory_path, vdb_directory_name, sizeof(vdb_directory_path));
|
|
BLI_dir_create_recursive(vdb_directory_path);
|
|
|
|
char vdb_file_name[FILE_MAXFILE];
|
|
STRNCPY(vdb_file_name, volume->id.name + 2);
|
|
const pxr::UsdTimeCode timecode = get_export_time_code();
|
|
if (!timecode.IsDefault()) {
|
|
const int frame = int(timecode.GetValue());
|
|
const int num_frame_digits = frame == 0 ? 1 : integer_digits_i(abs(frame));
|
|
BLI_path_frame(vdb_file_name, sizeof(vdb_file_name), frame, num_frame_digits);
|
|
}
|
|
BLI_strncat(vdb_file_name, ".vdb", sizeof(vdb_file_name));
|
|
|
|
char vdb_file_path[FILE_MAX];
|
|
BLI_path_join(vdb_file_path, sizeof(vdb_file_path), vdb_directory_path, vdb_file_name);
|
|
|
|
return vdb_file_path;
|
|
}
|
|
|
|
std::optional<std::string> USDVolumeWriter::construct_vdb_relative_file_path(
|
|
const std::string &vdb_file_path) const
|
|
{
|
|
const std::string usd_file_path = get_export_file_path();
|
|
if (usd_file_path.empty()) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
char relative_path[FILE_MAX];
|
|
STRNCPY(relative_path, vdb_file_path.c_str());
|
|
BLI_path_rel(relative_path, usd_file_path.c_str());
|
|
if (!BLI_path_is_rel(relative_path)) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
/* Following code was written with an assumption that Blender's relative paths start with
|
|
* // characters as well as have OS dependent slashes. Inside of USD files those relative
|
|
* paths should start with either ./ or ../ characters and have always forward slashes (/)
|
|
* separating directories. This is the convention used in USD documentation (and it seems
|
|
* to be used in other DCC packages as well). */
|
|
std::string relative_path_processed = pxr::TfNormPath(relative_path + 2);
|
|
if (relative_path_processed[0] != '.') {
|
|
relative_path_processed.insert(0, "./");
|
|
}
|
|
|
|
return relative_path_processed;
|
|
}
|
|
|
|
} // namespace blender::io::usd
|