2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2020-10-07 18:03:07 +02:00
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
|
* \ingroup modifiers
|
|
|
|
|
*/
|
|
|
|
|
|
2021-04-26 14:42:03 -05:00
|
|
|
#include "BKE_geometry_set.hh"
|
2024-01-18 12:20:42 +01:00
|
|
|
#include "BKE_lib_query.hh"
|
2023-11-14 09:30:40 +01:00
|
|
|
#include "BKE_modifier.hh"
|
2020-10-07 18:03:07 +02:00
|
|
|
#include "BKE_texture.h"
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_volume.hh"
|
Volumes: refactor volume grid storage
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
2023-12-20 15:32:52 +01:00
|
|
|
#include "BKE_volume_grid.hh"
|
2023-06-30 08:49:53 +02:00
|
|
|
#include "BKE_volume_openvdb.hh"
|
2020-10-07 18:03:07 +02:00
|
|
|
|
2024-02-09 18:59:42 +01:00
|
|
|
#include "BLT_translation.hh"
|
2022-07-15 14:12:34 +02:00
|
|
|
|
2020-10-07 18:03:07 +02:00
|
|
|
#include "DNA_object_types.h"
|
|
|
|
|
#include "DNA_screen_types.h"
|
|
|
|
|
#include "DNA_texture_types.h"
|
|
|
|
|
|
2023-09-22 03:18:17 +02:00
|
|
|
#include "DEG_depsgraph_build.hh"
|
|
|
|
|
#include "DEG_depsgraph_query.hh"
|
2020-10-07 18:03:07 +02:00
|
|
|
|
2023-08-05 02:57:52 +02:00
|
|
|
#include "UI_interface.hh"
|
|
|
|
|
#include "UI_resources.hh"
|
2020-10-07 18:03:07 +02:00
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2023-05-04 18:35:37 +02:00
|
|
|
#include "MOD_ui_common.hh"
|
2020-10-07 18:03:07 +02:00
|
|
|
|
2020-11-09 15:42:38 +01:00
|
|
|
#include "RE_texture.h"
|
2020-10-07 18:03:07 +02:00
|
|
|
|
2022-03-14 16:54:46 +01:00
|
|
|
#include "RNA_prototypes.h"
|
2020-10-07 18:03:07 +02:00
|
|
|
|
|
|
|
|
#include "BLI_math_vector.h"
|
|
|
|
|
|
|
|
|
|
#ifdef WITH_OPENVDB
|
|
|
|
|
# include <openvdb/openvdb.h>
|
|
|
|
|
# include <openvdb/tools/Interpolation.h>
|
|
|
|
|
# include <openvdb/tools/Morphology.h>
|
|
|
|
|
# include <openvdb/tools/Prune.h>
|
|
|
|
|
# include <openvdb/tools/ValueTransformer.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static void init_data(ModifierData *md)
|
2020-10-07 18:03:07 +02:00
|
|
|
{
|
|
|
|
|
VolumeDisplaceModifierData *vdmd = reinterpret_cast<VolumeDisplaceModifierData *>(md);
|
2020-11-06 17:49:09 +01:00
|
|
|
vdmd->texture = nullptr;
|
2020-10-12 12:37:48 +02:00
|
|
|
vdmd->strength = 0.5f;
|
2020-10-07 18:03:07 +02:00
|
|
|
copy_v3_fl(vdmd->texture_mid_level, 0.5f);
|
|
|
|
|
vdmd->texture_sample_radius = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static void update_depsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
|
2020-10-07 18:03:07 +02:00
|
|
|
{
|
|
|
|
|
VolumeDisplaceModifierData *vdmd = reinterpret_cast<VolumeDisplaceModifierData *>(md);
|
2020-11-06 17:49:09 +01:00
|
|
|
if (vdmd->texture != nullptr) {
|
2020-10-07 18:03:07 +02:00
|
|
|
DEG_add_generic_id_relation(ctx->node, &vdmd->texture->id, "Volume Displace Modifier");
|
|
|
|
|
}
|
|
|
|
|
if (vdmd->texture_map_mode == MOD_VOLUME_DISPLACE_MAP_OBJECT) {
|
2020-11-06 17:49:09 +01:00
|
|
|
if (vdmd->texture_map_object != nullptr) {
|
2020-10-07 18:03:07 +02:00
|
|
|
DEG_add_object_relation(
|
|
|
|
|
ctx->node, vdmd->texture_map_object, DEG_OB_COMP_TRANSFORM, "Volume Displace Modifier");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
|
2020-10-07 18:03:07 +02:00
|
|
|
{
|
|
|
|
|
VolumeDisplaceModifierData *vdmd = reinterpret_cast<VolumeDisplaceModifierData *>(md);
|
2023-07-27 12:04:18 +10:00
|
|
|
walk(user_data, ob, (ID **)&vdmd->texture, IDWALK_CB_USER);
|
|
|
|
|
walk(user_data, ob, (ID **)&vdmd->texture_map_object, IDWALK_CB_USER);
|
2020-10-07 18:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static void foreach_tex_link(ModifierData *md, Object *ob, TexWalkFunc walk, void *user_data)
|
2020-10-07 18:03:07 +02:00
|
|
|
{
|
2023-07-27 12:04:18 +10:00
|
|
|
walk(user_data, ob, md, "texture");
|
2020-10-07 18:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static bool depends_on_time(Scene * /*scene*/, ModifierData *md)
|
2020-10-07 18:03:07 +02:00
|
|
|
{
|
|
|
|
|
VolumeDisplaceModifierData *vdmd = reinterpret_cast<VolumeDisplaceModifierData *>(md);
|
|
|
|
|
if (vdmd->texture) {
|
|
|
|
|
return BKE_texture_dependsOnTime(vdmd->texture);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void panel_draw(const bContext *C, Panel *panel)
|
|
|
|
|
{
|
|
|
|
|
uiLayout *layout = panel->layout;
|
|
|
|
|
|
|
|
|
|
PointerRNA ob_ptr;
|
|
|
|
|
PointerRNA *ptr = modifier_panel_get_property_pointers(panel, &ob_ptr);
|
|
|
|
|
VolumeDisplaceModifierData *vdmd = static_cast<VolumeDisplaceModifierData *>(ptr->data);
|
|
|
|
|
|
|
|
|
|
uiLayoutSetPropSep(layout, true);
|
|
|
|
|
|
2023-08-10 17:13:02 +02:00
|
|
|
uiTemplateID(layout, C, ptr, "texture", "texture.new", nullptr, nullptr, 0, false, nullptr);
|
2023-07-29 15:06:33 +10:00
|
|
|
uiItemR(layout, ptr, "texture_map_mode", UI_ITEM_NONE, "Texture Mapping", ICON_NONE);
|
2020-10-07 18:03:07 +02:00
|
|
|
|
|
|
|
|
if (vdmd->texture_map_mode == MOD_VOLUME_DISPLACE_MAP_OBJECT) {
|
2023-07-29 15:06:33 +10:00
|
|
|
uiItemR(layout, ptr, "texture_map_object", UI_ITEM_NONE, "Object", ICON_NONE);
|
2020-10-07 18:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-29 15:06:33 +10:00
|
|
|
uiItemR(layout, ptr, "strength", UI_ITEM_NONE, nullptr, ICON_NONE);
|
|
|
|
|
uiItemR(layout, ptr, "texture_sample_radius", UI_ITEM_NONE, "Sample Radius", ICON_NONE);
|
|
|
|
|
uiItemR(layout, ptr, "texture_mid_level", UI_ITEM_NONE, "Mid Level", ICON_NONE);
|
2020-10-07 18:03:07 +02:00
|
|
|
|
|
|
|
|
modifier_panel_end(layout, ptr);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static void panel_register(ARegionType *region_type)
|
2020-10-07 18:03:07 +02:00
|
|
|
{
|
|
|
|
|
modifier_panel_register(region_type, eModifierType_VolumeDisplace, panel_draw);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef WITH_OPENVDB
|
|
|
|
|
|
|
|
|
|
static openvdb::Mat4s matrix_to_openvdb(const float m[4][4])
|
|
|
|
|
{
|
2020-10-14 14:43:54 +11:00
|
|
|
/* OpenVDB matrices are transposed Blender matrices, i.e. the translation is in the last row
|
|
|
|
|
* instead of in the last column. However, the layout in memory is the same, because OpenVDB
|
2020-10-12 14:24:25 +02:00
|
|
|
* matrices are row major (compared to Blender's column major matrices). */
|
2020-10-07 18:03:07 +02:00
|
|
|
openvdb::Mat4s new_matrix{reinterpret_cast<const float *>(m)};
|
|
|
|
|
return new_matrix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename GridType> struct DisplaceOp {
|
|
|
|
|
/* Has to be copied for each thread. */
|
|
|
|
|
typename GridType::ConstAccessor accessor;
|
2020-10-12 14:24:25 +02:00
|
|
|
const openvdb::Mat4s index_to_texture;
|
2020-10-07 18:03:07 +02:00
|
|
|
|
|
|
|
|
Tex *texture;
|
|
|
|
|
const double strength;
|
|
|
|
|
const openvdb::Vec3d texture_mid_level;
|
|
|
|
|
|
|
|
|
|
void operator()(const typename GridType::ValueOnIter &iter) const
|
|
|
|
|
{
|
|
|
|
|
const openvdb::Coord coord = iter.getCoord();
|
|
|
|
|
const openvdb::Vec3d displace_vector = this->compute_displace_vector(coord);
|
|
|
|
|
/* Subtract vector because that makes the result more similar to advection and the mesh
|
|
|
|
|
* displace modifier. */
|
|
|
|
|
const openvdb::Vec3d sample_coord = coord.asVec3d() - displace_vector;
|
|
|
|
|
const auto new_value = openvdb::tools::BoxSampler::sample(this->accessor, sample_coord);
|
|
|
|
|
iter.setValue(new_value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
openvdb::Vec3d compute_displace_vector(const openvdb::Coord &coord) const
|
|
|
|
|
{
|
2020-11-06 17:49:09 +01:00
|
|
|
if (this->texture != nullptr) {
|
2020-10-12 14:24:25 +02:00
|
|
|
const openvdb::Vec3f texture_pos = coord.asVec3s() * this->index_to_texture;
|
|
|
|
|
const openvdb::Vec3d texture_value = this->evaluate_texture(texture_pos);
|
|
|
|
|
const openvdb::Vec3d displacement = (texture_value - this->texture_mid_level) *
|
|
|
|
|
this->strength;
|
|
|
|
|
return displacement;
|
2020-10-07 18:03:07 +02:00
|
|
|
}
|
|
|
|
|
return openvdb::Vec3d{0, 0, 0};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
openvdb::Vec3d evaluate_texture(const openvdb::Vec3f &pos) const
|
|
|
|
|
{
|
|
|
|
|
TexResult texture_result = {0};
|
2023-06-05 17:18:38 +02:00
|
|
|
BKE_texture_get_value(this->texture, const_cast<float *>(pos.asV()), &texture_result, false);
|
2022-01-28 13:28:31 +01:00
|
|
|
return {texture_result.trgba[0], texture_result.trgba[1], texture_result.trgba[2]};
|
2020-10-07 18:03:07 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static float get_max_voxel_side_length(const openvdb::GridBase &grid)
|
|
|
|
|
{
|
2020-10-12 14:24:25 +02:00
|
|
|
const openvdb::Vec3d voxel_size = grid.voxelSize();
|
|
|
|
|
const float max_voxel_side_length = std::max({voxel_size[0], voxel_size[1], voxel_size[2]});
|
2020-10-07 18:03:07 +02:00
|
|
|
return max_voxel_side_length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct DisplaceGridOp {
|
|
|
|
|
/* This is the grid that will be displaced. The output is copied back to the original grid. */
|
|
|
|
|
openvdb::GridBase &base_grid;
|
|
|
|
|
|
|
|
|
|
VolumeDisplaceModifierData &vdmd;
|
|
|
|
|
const ModifierEvalContext &ctx;
|
|
|
|
|
|
|
|
|
|
template<typename GridType> void operator()()
|
|
|
|
|
{
|
2022-02-16 19:49:58 +01:00
|
|
|
if constexpr (blender::
|
2024-01-02 18:12:54 +01:00
|
|
|
is_same_any_v<GridType, openvdb::points::PointDataGrid, openvdb::MaskGrid>)
|
|
|
|
|
{
|
2020-10-07 18:03:07 +02:00
|
|
|
/* We don't support displacing these grid types yet. */
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
this->displace_grid<GridType>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename GridType> void displace_grid()
|
|
|
|
|
{
|
|
|
|
|
GridType &grid = static_cast<GridType &>(base_grid);
|
|
|
|
|
|
|
|
|
|
/* Make a copy of the original grid to work on. This will replace the original grid. */
|
|
|
|
|
typename GridType::Ptr temp_grid = grid.deepCopy();
|
|
|
|
|
|
|
|
|
|
/* Dilate grid, because the currently inactive cells might become active during the displace
|
|
|
|
|
* operation. The quality of the approximation of the has a big impact on performance. */
|
|
|
|
|
const float max_voxel_side_length = get_max_voxel_side_length(grid);
|
|
|
|
|
const float sample_radius = vdmd.texture_sample_radius * std::abs(vdmd.strength) /
|
|
|
|
|
max_voxel_side_length / 2.0f;
|
|
|
|
|
openvdb::tools::dilateActiveValues(temp_grid->tree(),
|
2022-09-25 18:30:50 +10:00
|
|
|
int(std::ceil(sample_radius)),
|
2020-10-07 18:03:07 +02:00
|
|
|
openvdb::tools::NN_FACE_EDGE,
|
|
|
|
|
openvdb::tools::EXPAND_TILES);
|
|
|
|
|
|
|
|
|
|
const openvdb::Mat4s index_to_texture = this->get_index_to_texture_transform();
|
|
|
|
|
|
|
|
|
|
/* Construct the operator that will be executed on every cell of the dilated grid. */
|
|
|
|
|
DisplaceOp<GridType> displace_op{grid.getConstAccessor(),
|
|
|
|
|
index_to_texture,
|
|
|
|
|
vdmd.texture,
|
|
|
|
|
vdmd.strength / max_voxel_side_length,
|
|
|
|
|
openvdb::Vec3d{vdmd.texture_mid_level}};
|
|
|
|
|
|
|
|
|
|
/* Run the operator. This is multi-threaded. It is important that the operator is not shared
|
|
|
|
|
* between the threads, because it contains a non-thread-safe accessor for the old grid. */
|
|
|
|
|
openvdb::tools::foreach (temp_grid->beginValueOn(),
|
|
|
|
|
displace_op,
|
|
|
|
|
true,
|
2023-01-16 13:25:05 +11:00
|
|
|
/* Disable sharing of the operator. */
|
|
|
|
|
false);
|
2020-10-07 18:03:07 +02:00
|
|
|
|
|
|
|
|
/* It is likely that we produced too many active cells. Those are removed here, to avoid
|
|
|
|
|
* slowing down subsequent operations. */
|
|
|
|
|
typename GridType::ValueType prune_tolerance{0};
|
|
|
|
|
openvdb::tools::deactivate(*temp_grid, temp_grid->background(), prune_tolerance);
|
|
|
|
|
openvdb::tools::prune(temp_grid->tree());
|
|
|
|
|
|
|
|
|
|
/* Overwrite the old volume grid with the new grid. */
|
|
|
|
|
grid.clear();
|
|
|
|
|
grid.merge(*temp_grid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
openvdb::Mat4s get_index_to_texture_transform() const
|
|
|
|
|
{
|
|
|
|
|
const openvdb::Mat4s index_to_object{
|
|
|
|
|
base_grid.transform().baseMap()->getAffineMap()->getMat4()};
|
|
|
|
|
|
|
|
|
|
switch (vdmd.texture_map_mode) {
|
|
|
|
|
case MOD_VOLUME_DISPLACE_MAP_LOCAL: {
|
|
|
|
|
return index_to_object;
|
|
|
|
|
}
|
|
|
|
|
case MOD_VOLUME_DISPLACE_MAP_GLOBAL: {
|
2024-02-14 16:14:49 +01:00
|
|
|
const openvdb::Mat4s object_to_world = matrix_to_openvdb(
|
|
|
|
|
ctx.object->object_to_world().ptr());
|
2020-10-12 14:24:25 +02:00
|
|
|
return index_to_object * object_to_world;
|
2020-10-07 18:03:07 +02:00
|
|
|
}
|
|
|
|
|
case MOD_VOLUME_DISPLACE_MAP_OBJECT: {
|
2020-11-06 17:49:09 +01:00
|
|
|
if (vdmd.texture_map_object == nullptr) {
|
2020-10-07 18:03:07 +02:00
|
|
|
return index_to_object;
|
|
|
|
|
}
|
2024-02-14 16:14:49 +01:00
|
|
|
const openvdb::Mat4s object_to_world = matrix_to_openvdb(
|
|
|
|
|
ctx.object->object_to_world().ptr());
|
2022-11-02 14:41:49 +01:00
|
|
|
const openvdb::Mat4s world_to_texture = matrix_to_openvdb(
|
2024-02-14 16:14:49 +01:00
|
|
|
vdmd.texture_map_object->world_to_object().ptr());
|
2020-10-12 14:24:25 +02:00
|
|
|
return index_to_object * object_to_world * world_to_texture;
|
2020-10-07 18:03:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-10-12 14:24:25 +02:00
|
|
|
BLI_assert(false);
|
2020-10-07 18:03:07 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-04-26 14:42:03 -05:00
|
|
|
static void displace_volume(ModifierData *md, const ModifierEvalContext *ctx, Volume *volume)
|
2020-10-07 18:03:07 +02:00
|
|
|
{
|
|
|
|
|
#ifdef WITH_OPENVDB
|
|
|
|
|
VolumeDisplaceModifierData *vdmd = reinterpret_cast<VolumeDisplaceModifierData *>(md);
|
|
|
|
|
|
|
|
|
|
/* Iterate over all grids and displace them one by one. */
|
2020-10-20 11:00:16 +02:00
|
|
|
BKE_volume_load(volume, DEG_get_bmain(ctx->depsgraph));
|
2020-10-07 18:03:07 +02:00
|
|
|
const int grid_amount = BKE_volume_num_grids(volume);
|
|
|
|
|
for (int grid_index = 0; grid_index < grid_amount; grid_index++) {
|
Volumes: refactor volume grid storage
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
2023-12-20 15:32:52 +01:00
|
|
|
blender::bke::VolumeGridData *volume_grid = BKE_volume_grid_get_for_write(volume, grid_index);
|
|
|
|
|
BLI_assert(volume_grid);
|
2020-10-07 18:03:07 +02:00
|
|
|
|
2024-01-10 15:20:08 +01:00
|
|
|
blender::bke::VolumeTreeAccessToken tree_token;
|
|
|
|
|
openvdb::GridBase &grid = volume_grid->grid_for_write(tree_token);
|
Volumes: refactor volume grid storage
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
2023-12-20 15:32:52 +01:00
|
|
|
VolumeGridType grid_type = volume_grid->grid_type();
|
2020-10-07 18:03:07 +02:00
|
|
|
|
Volumes: refactor volume grid storage
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
2023-12-20 15:32:52 +01:00
|
|
|
DisplaceGridOp displace_grid_op{grid, *vdmd, *ctx};
|
2020-10-07 18:03:07 +02:00
|
|
|
BKE_volume_grid_type_operation(grid_type, displace_grid_op);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
2021-04-26 17:03:06 -05:00
|
|
|
UNUSED_VARS(md, volume, ctx);
|
2020-10-26 17:07:58 +11:00
|
|
|
BKE_modifier_set_error(ctx->object, md, "Compiled without OpenVDB");
|
2020-10-07 18:03:07 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static void modify_geometry_set(ModifierData *md,
|
|
|
|
|
const ModifierEvalContext *ctx,
|
|
|
|
|
blender::bke::GeometrySet *geometry_set)
|
2021-04-26 14:42:03 -05:00
|
|
|
{
|
|
|
|
|
Volume *input_volume = geometry_set->get_volume_for_write();
|
|
|
|
|
if (input_volume != nullptr) {
|
|
|
|
|
displace_volume(md, ctx, input_volume);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-07 18:03:07 +02:00
|
|
|
ModifierTypeInfo modifierType_VolumeDisplace = {
|
2023-07-26 17:08:14 +02:00
|
|
|
/*idname*/ "Volume Displace",
|
2023-01-16 12:41:11 +11:00
|
|
|
/*name*/ N_("Volume Displace"),
|
2023-07-27 12:04:18 +10:00
|
|
|
/*struct_name*/ "VolumeDisplaceModifierData",
|
|
|
|
|
/*struct_size*/ sizeof(VolumeDisplaceModifierData),
|
2023-01-16 12:41:11 +11:00
|
|
|
/*srna*/ &RNA_VolumeDisplaceModifier,
|
2023-11-14 10:03:56 +01:00
|
|
|
/*type*/ ModifierTypeType::NonGeometrical,
|
2023-01-16 12:41:11 +11:00
|
|
|
/*flags*/ static_cast<ModifierTypeFlag>(0),
|
|
|
|
|
/*icon*/ ICON_VOLUME_DATA, /* TODO: Use correct icon. */
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
/*copy_data*/ BKE_modifier_copydata_generic,
|
|
|
|
|
|
|
|
|
|
/*deform_verts*/ nullptr,
|
|
|
|
|
/*deform_matrices*/ nullptr,
|
|
|
|
|
/*deform_verts_EM*/ nullptr,
|
|
|
|
|
/*deform_matrices_EM*/ nullptr,
|
|
|
|
|
/*modify_mesh*/ nullptr,
|
|
|
|
|
/*modify_geometry_set*/ modify_geometry_set,
|
|
|
|
|
|
|
|
|
|
/*init_data*/ init_data,
|
|
|
|
|
/*required_data_mask*/ nullptr,
|
|
|
|
|
/*free_data*/ nullptr,
|
|
|
|
|
/*is_disabled*/ nullptr,
|
|
|
|
|
/*update_depsgraph*/ update_depsgraph,
|
|
|
|
|
/*depends_on_time*/ depends_on_time,
|
|
|
|
|
/*depends_on_normals*/ nullptr,
|
|
|
|
|
/*foreach_ID_link*/ foreach_ID_link,
|
|
|
|
|
/*foreach_tex_link*/ foreach_tex_link,
|
|
|
|
|
/*free_runtime_data*/ nullptr,
|
|
|
|
|
/*panel_register*/ panel_register,
|
|
|
|
|
/*blend_write*/ nullptr,
|
|
|
|
|
/*blend_read*/ nullptr,
|
2024-01-18 22:51:30 +01:00
|
|
|
/*foreach_cache*/ nullptr,
|
2020-10-07 18:03:07 +02:00
|
|
|
};
|