Blender V5.3
volume_grid_file_cache.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Foundation
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#ifdef WITH_OPENVDB
10
12# include "BKE_volume_openvdb.hh"
13
14# include "BLI_map.hh"
15# include "BLI_memory_cache.hh"
16# include "BLI_memory_counter.hh"
17
18# include <openvdb/openvdb.h>
19
20namespace blender::bke::volume_grid::file_cache {
21
25struct GridCache {
30 openvdb::GridBase::Ptr meta_data_grid;
34 Map<int, GVolumeGrid> grid_by_simplify_level;
35};
36
40struct FileCache {
44 std::string error_message;
48 openvdb::MetaMap meta_data;
52 Vector<GridCache> grids;
53
54 GridCache *grid_cache_by_name(const StringRef name)
55 {
56 for (GridCache &grid_cache : this->grids) {
57 if (grid_cache.meta_data_grid->getName() == name) {
58 return &grid_cache;
59 }
60 }
61 return nullptr;
62 }
63};
64
68struct GlobalCache {
69 Mutex mutex;
70 Map<std::string, FileCache> file_map;
71};
72
76static GlobalCache &get_global_cache()
77{
78 static GlobalCache global_cache;
79 return global_cache;
80}
81
86static FileCache create_file_cache(const StringRef file_path)
87{
88 FileCache file_cache;
89
90 openvdb::io::File file(file_path);
91 openvdb::GridPtrVec vdb_grids;
92 try {
93 /* Disable delay loading and file copying, this has poor performance
94 * on network drives. */
95 const bool delay_load = false;
96# ifdef OPENVDB_USE_DELAYED_LOADING
97 file.setCopyMaxBytes(0);
98# endif
99 file.open(delay_load);
100 vdb_grids = *(file.readAllGridMetadata());
101 file_cache.meta_data = *file.getMetadata();
102 }
103 catch (const openvdb::IoError &e) {
104 file_cache.error_message = e.what();
105 }
106 catch (...) {
107 file_cache.error_message = "Unknown error reading VDB file";
108 }
109 if (!file_cache.error_message.empty()) {
110 return file_cache;
111 }
112
113 for (openvdb::GridBase::Ptr &vdb_grid : vdb_grids) {
114 if (!vdb_grid) {
115 continue;
116 }
117 GridCache grid_cache;
118 grid_cache.meta_data_grid = vdb_grid;
119 file_cache.grids.append(std::move(grid_cache));
120 }
121
122 return file_cache;
123}
124
125static FileCache &get_file_cache(const StringRef file_path)
126{
127 GlobalCache &global_cache = get_global_cache();
128 /* Assumes that the cache is locked already. */
129 BLI_assert(!global_cache.mutex.try_lock());
130 return global_cache.file_map.lookup_or_add_cb_as(file_path,
131 [&]() { return create_file_cache(file_path); });
132}
133
137class GridReadKey : public GenericKey {
138 public:
139 std::string file_path;
140 std::string grid_name;
141 int simplify_level;
142
143 uint64_t hash() const override
144 {
145 return get_default_hash(this->file_path, this->grid_name, this->simplify_level);
146 }
147
148 bool equal_to(const GenericKey &other) const override
149 {
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;
155 }
156 return false;
157 }
158
159 std::unique_ptr<GenericKey> to_storable() const override
160 {
161 return std::make_unique<GridReadKey>(*this);
162 }
163};
164
165class GridReadValue : public memory_cache::CachedValue {
166 private:
167 mutable std::atomic<int64_t> bytes_ = 0;
168
169 public:
170 ImplicitSharingPtr<> tree_sharing_info;
171 openvdb::GridBase::Ptr grid;
172
173 void count_memory(MemoryCounter &memory) const override
174 {
175 /* Avoid computing the amount of memory from scratch every time. */
176 if (bytes_ == 0) {
177 this->bytes_ = grid->baseTree().memUsage();
178 }
179 memory.add(bytes_);
180 }
181};
182
187static openvdb::GridBase::Ptr load_single_grid_from_disk(const StringRef file_path,
188 const StringRef grid_name)
189{
190 /* Disable delay loading and file copying, this has poor performance
191 * on network drivers. */
192 const bool delay_load = false;
193
194 openvdb::io::File file(file_path);
195# ifdef OPENVDB_USE_DELAYED_LOADING
196 file.setCopyMaxBytes(0);
197# endif
198 file.open(delay_load);
199 return file.readGrid(grid_name);
200}
201
206static LazyLoadedGrid load_single_grid_from_disk_cached(const StringRef file_path,
207 const StringRef grid_name,
208 const int simplify_level)
209{
210 GridReadKey key;
211 key.file_path = file_path;
212 key.grid_name = grid_name;
213 key.simplify_level = simplify_level;
214
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);
219 }
220 else {
221 /* Build the simplified grid from the main grid. */
222 const GVolumeGrid main_grid = get_grid_from_file(key.file_path, key.grid_name, 0);
223 const VolumeGridType grid_type = main_grid->grid_type();
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);
228 }
229 auto value = std::make_unique<GridReadValue>();
230 value->grid = std::move(grid);
231 value->tree_sharing_info = OpenvdbTreeSharingInfo::make(value->grid->baseTreePtr());
232 return value;
233 });
234 if (!value) {
235 return {};
236 }
237
238 /* Copy the grid so that it has a single owner. Note that the tree is still shared. */
239 openvdb::GridBase::Ptr grid = value->grid->copyGrid();
240 grid->setTransform(grid->transform().copy());
241 return {grid, value->tree_sharing_info};
242}
243
248static GVolumeGrid get_cached_grid(const StringRef file_path,
249 GridCache &grid_cache,
250 const int simplify_level)
251{
252 if (GVolumeGrid *grid = grid_cache.grid_by_simplify_level.lookup_ptr(simplify_level)) {
253 return *grid;
254 }
255 /* A callback that actually loads the full grid including the tree when it's accessed. */
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);
260 };
261 /* This allows the returned grid to already contain meta-data and transforms, even if the tree is
262 * not loaded yet. */
263 openvdb::GridBase::Ptr meta_data_and_transform_grid;
264 if (simplify_level == 0) {
265 /* Only pass the meta-data grid when there is no simplification for now. For simplified grids,
266 * the transform would have to be updated here already. */
267 meta_data_and_transform_grid = grid_cache.meta_data_grid->copyGrid();
268 }
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);
273 return grid;
274}
275
276GVolumeGrid get_grid_from_file(const StringRef file_path,
277 const StringRef grid_name,
278 const int simplify_level)
279{
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);
285 }
286 return {};
287}
288
289GridsFromFile get_all_grids_from_file(const StringRef file_path, const int simplify_level)
290{
291 GridsFromFile result;
292 GlobalCache &global_cache = get_global_cache();
293 std::lock_guard lock{global_cache.mutex};
294 FileCache &file_cache = get_file_cache(file_path);
295
296 if (!file_cache.error_message.empty()) {
297 result.error_message = file_cache.error_message;
298 return result;
299 }
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));
303 }
304 return result;
305}
306
307void unload_unused()
308{
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(); });
315 }
316 }
317}
318
319} // namespace blender::bke::volume_grid::file_cache
320
321#endif /* WITH_OPENVDB */
volatile int lock
#define BLI_assert(a)
unsigned long long int uint64_t
void count_memory(const VolumeGridData &grid, MemoryCounter &memory)
std::mutex Mutex
Definition BLI_mutex.hh:51
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
#define hash
Definition noise_c.cc:156