18# include <openvdb/openvdb.h>
20namespace blender::bke::volume_grid::file_cache {
30 openvdb::GridBase::Ptr meta_data_grid;
34 Map<int, GVolumeGrid> grid_by_simplify_level;
44 std::string error_message;
48 openvdb::MetaMap meta_data;
52 Vector<GridCache> grids;
54 GridCache *grid_cache_by_name(
const StringRef name)
56 for (GridCache &grid_cache : this->grids) {
57 if (grid_cache.meta_data_grid->getName() == name) {
70 Map<std::string, FileCache> file_map;
76static GlobalCache &get_global_cache()
78 static GlobalCache global_cache;
86static FileCache create_file_cache(
const StringRef file_path)
90 openvdb::io::File file(file_path);
91 openvdb::GridPtrVec vdb_grids;
95 const bool delay_load =
false;
96# ifdef OPENVDB_USE_DELAYED_LOADING
97 file.setCopyMaxBytes(0);
99 file.open(delay_load);
100 vdb_grids = *(file.readAllGridMetadata());
101 file_cache.meta_data = *file.getMetadata();
103 catch (
const openvdb::IoError &e) {
104 file_cache.error_message =
e.what();
107 file_cache.error_message =
"Unknown error reading VDB file";
109 if (!file_cache.error_message.empty()) {
113 for (openvdb::GridBase::Ptr &vdb_grid : vdb_grids) {
117 GridCache grid_cache;
118 grid_cache.meta_data_grid = vdb_grid;
119 file_cache.grids.append(std::move(grid_cache));
125static FileCache &get_file_cache(
const StringRef file_path)
127 GlobalCache &global_cache = get_global_cache();
130 return global_cache.file_map.lookup_or_add_cb_as(file_path,
131 [&]() {
return create_file_cache(file_path); });
137class GridReadKey :
public GenericKey {
139 std::string file_path;
140 std::string grid_name;
145 return get_default_hash(this->file_path, this->grid_name, this->simplify_level);
148 bool equal_to(
const GenericKey &other)
const override
150 if (
const auto *other_typed =
dynamic_cast<const GridReadKey *
>(&other)) {
151 const GridReadKey &a = *
this;
152 const GridReadKey &
b = *other_typed;
153 return a.file_path ==
b.file_path && a.grid_name ==
b.grid_name &&
154 a.simplify_level ==
b.simplify_level;
159 std::unique_ptr<GenericKey> to_storable()
const override
161 return std::make_unique<GridReadKey>(*
this);
165class GridReadValue :
public memory_cache::CachedValue {
167 mutable std::atomic<int64_t> bytes_ = 0;
170 ImplicitSharingPtr<> tree_sharing_info;
171 openvdb::GridBase::Ptr grid;
177 this->bytes_ = grid->baseTree().memUsage();
187static openvdb::GridBase::Ptr load_single_grid_from_disk(
const StringRef file_path,
188 const StringRef grid_name)
192 const bool delay_load =
false;
194 openvdb::io::File file(file_path);
195# ifdef OPENVDB_USE_DELAYED_LOADING
196 file.setCopyMaxBytes(0);
198 file.open(delay_load);
199 return file.readGrid(grid_name);
206static LazyLoadedGrid load_single_grid_from_disk_cached(
const StringRef file_path,
207 const StringRef grid_name,
208 const int simplify_level)
211 key.file_path = file_path;
212 key.grid_name = grid_name;
213 key.simplify_level = simplify_level;
215 std::shared_ptr<const GridReadValue> value = memory_cache::get<GridReadValue>(key, [&key]() {
216 openvdb::GridBase::Ptr grid;
217 if (key.simplify_level == 0) {
218 grid = load_single_grid_from_disk(key.file_path, key.grid_name);
222 const GVolumeGrid main_grid = get_grid_from_file(key.file_path, key.grid_name, 0);
224 const float resolution_factor = 1.0f / (1 << key.simplify_level);
225 VolumeTreeAccessToken tree_token;
226 grid = BKE_volume_grid_create_with_changed_resolution(
227 grid_type, main_grid->grid(tree_token), resolution_factor);
229 auto value = std::make_unique<GridReadValue>();
230 value->grid = std::move(grid);
231 value->tree_sharing_info = OpenvdbTreeSharingInfo::make(value->grid->baseTreePtr());
239 openvdb::GridBase::Ptr grid = value->grid->copyGrid();
240 grid->setTransform(grid->transform().copy());
241 return {grid, value->tree_sharing_info};
248static GVolumeGrid get_cached_grid(
const StringRef file_path,
249 GridCache &grid_cache,
250 const int simplify_level)
252 if (GVolumeGrid *grid = grid_cache.grid_by_simplify_level.lookup_ptr(simplify_level)) {
256 auto load_grid_fn = [file_path = std::string(file_path),
257 grid_name = std::string(grid_cache.meta_data_grid->getName()),
258 simplify_level]() -> LazyLoadedGrid {
259 return load_single_grid_from_disk_cached(file_path, grid_name, simplify_level);
263 openvdb::GridBase::Ptr meta_data_and_transform_grid;
264 if (simplify_level == 0) {
267 meta_data_and_transform_grid = grid_cache.meta_data_grid->copyGrid();
269 VolumeGridData *grid_data = MEM_new<VolumeGridData>(
270 __func__, load_grid_fn, meta_data_and_transform_grid);
271 GVolumeGrid grid{grid_data};
272 grid_cache.grid_by_simplify_level.add(simplify_level, grid);
276GVolumeGrid get_grid_from_file(
const StringRef file_path,
277 const StringRef grid_name,
278 const int simplify_level)
280 GlobalCache &global_cache = get_global_cache();
281 std::lock_guard
lock{global_cache.mutex};
282 FileCache &file_cache = get_file_cache(file_path);
283 if (GridCache *grid_cache = file_cache.grid_cache_by_name(grid_name)) {
284 return get_cached_grid(file_path, *grid_cache, simplify_level);
289GridsFromFile get_all_grids_from_file(
const StringRef file_path,
const int simplify_level)
292 GlobalCache &global_cache = get_global_cache();
293 std::lock_guard
lock{global_cache.mutex};
294 FileCache &file_cache = get_file_cache(file_path);
296 if (!file_cache.error_message.empty()) {
297 result.error_message = file_cache.error_message;
300 result.file_meta_data = std::make_shared<openvdb::MetaMap>(file_cache.meta_data);
301 for (GridCache &grid_cache : file_cache.grids) {
302 result.grids.append(get_cached_grid(file_path, grid_cache, simplify_level));
309 GlobalCache &global_cache = get_global_cache();
310 std::lock_guard
lock{global_cache.mutex};
311 for (FileCache &file_cache : global_cache.file_map.values()) {
312 for (GridCache &grid_cache : file_cache.grids) {
313 grid_cache.grid_by_simplify_level.remove_if(
314 [&](
const auto &item) {
return item.value->is_mutable(); });
unsigned long long int uint64_t
void count_memory(const VolumeGridData &grid, MemoryCounter &memory)
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e