4 #ifndef OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
5 #define OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
11 #include <tbb/spin_mutex.h>
17 #include <type_traits>
30 template<
typename T, Index Log2Dim>
39 #ifdef OPENVDB_USE_DELAYED_LOADING
42 FileInfo(): bufpos(0) , maskpos(0) {}
43 std::streamoff bufpos;
44 std::streamoff maskpos;
45 io::MappedFile::Ptr mapping;
53 #ifdef OPENVDB_USE_DELAYED_LOADING
64 #ifdef OPENVDB_USE_DELAYED_LOADING
74 #ifdef OPENVDB_USE_DELAYED_LOADING
75 return bool(mOutOfCore);
85 #ifdef OPENVDB_USE_DELAYED_LOADING
88 io::MappedFile::Ptr& mappedFile,
const std::streamoff& maskpos);
142 inline void setOutOfCore(
bool b)
145 #ifdef OPENVDB_USE_DELAYED_LOADING
151 inline void loadValues()
const
153 #ifdef OPENVDB_USE_DELAYED_LOADING
157 inline void doLoad()
const;
158 inline bool detachFromFile();
160 using FlagsType = std::atomic<Index32>;
162 #ifdef OPENVDB_USE_DELAYED_LOADING
170 FlagsType mOutOfCore;
171 tbb::spin_mutex mMutex;
174 friend class ::TestLeaf;
183 template<
typename T, Index Log2Dim>
188 #ifdef OPENVDB_USE_DELAYED_LOADING
195 template<
typename T, Index Log2Dim>
199 #ifdef OPENVDB_USE_DELAYED_LOADING
200 if (this->isOutOfCore()) {
201 this->detachFromFile();
211 template<
typename T, Index Log2Dim>
216 , mOutOfCore(other.mOutOfCore.load())
219 #ifdef OPENVDB_USE_DELAYED_LOADING
221 mFileInfo =
new FileInfo(*other.mFileInfo);
224 if (other.mData !=
nullptr) {
229 while (n--) *target++ = *source++;
231 #ifdef OPENVDB_USE_DELAYED_LOADING
237 template<
typename T, Index Log2Dim>
243 if (mData) mData[i] =
val;
247 template<
typename T, Index Log2Dim>
251 if (&other !=
this) {
252 #ifdef OPENVDB_USE_DELAYED_LOADING
253 if (this->isOutOfCore()) {
254 this->detachFromFile();
259 mOutOfCore.store(other.mOutOfCore.load(std::memory_order_acquire),
260 std::memory_order_release);
261 mFileInfo =
new FileInfo(*other.mFileInfo);
264 if (other.mData !=
nullptr) {
269 while (n--) *target++ = *source++;
271 #ifdef OPENVDB_USE_DELAYED_LOADING
278 #ifdef OPENVDB_USE_DELAYED_LOADING
279 template<
typename T, Index Log2Dim>
282 io::MappedFile::Ptr& mappedFile,
const std::streamoff& maskpos)
284 this->setOutOfCore(
true);
285 mFileInfo =
new FileInfo;
286 mFileInfo->meta = meta;
287 mFileInfo->bufpos = bufpos;
288 mFileInfo->mapping = mappedFile;
289 mFileInfo->maskpos = maskpos;
293 template<
typename T, Index Log2Dim>
297 this->detachFromFile();
298 if (mData !=
nullptr) {
301 while (n--) *target++ =
val;
306 template<
typename T, Index Log2Dim>
313 if (!target && !source)
return true;
314 if (!target || !source)
return false;
321 template<
typename T, Index Log2Dim>
326 #ifdef OPENVDB_USE_DELAYED_LOADING
331 auto tmp = other.mOutOfCore.load(std::memory_order_acquire);
332 tmp = mOutOfCore.exchange(std::move(tmp));
333 other.mOutOfCore.store(std::move(tmp), std::memory_order_release);
338 template<
typename T, Index Log2Dim>
342 size_t n =
sizeof(*this);
343 #ifdef OPENVDB_USE_DELAYED_LOADING
344 if (this->isOutOfCore()) n +=
sizeof(FileInfo);
348 #ifdef OPENVDB_USE_DELAYED_LOADING
351 return static_cast<Index>(
n);
355 template<
typename T, Index Log2Dim>
359 size_t n =
sizeof(*this);
361 return static_cast<Index>(
n);
365 template<
typename T, Index Log2Dim>
370 if (mData ==
nullptr) {
372 #ifdef OPENVDB_USE_DELAYED_LOADING
374 tbb::spin_mutex::scoped_lock lock(self->mMutex);
381 template<
typename T, Index Log2Dim>
386 if (mData ==
nullptr) {
387 #ifdef OPENVDB_USE_DELAYED_LOADING
389 tbb::spin_mutex::scoped_lock lock(mMutex);
397 template<
typename T, Index Log2Dim>
401 static const ValueType sZero = zeroVal<T>();
406 if (mData)
return mData[i];
else return sZero;
410 template<
typename T, Index Log2Dim>
412 LeafBuffer<T, Log2Dim>::deallocate()
415 if (mData !=
nullptr) {
416 #ifdef OPENVDB_USE_DELAYED_LOADING
417 if (this->isOutOfCore())
return false;
427 template<
typename T, Index Log2Dim>
429 LeafBuffer<T, Log2Dim>::doLoad()
const
431 #ifdef OPENVDB_USE_DELAYED_LOADING
432 if (!this->isOutOfCore())
return;
434 LeafBuffer<T, Log2Dim>*
self =
const_cast<LeafBuffer<T, Log2Dim>*
>(
this);
438 tbb::spin_mutex::scoped_lock lock(self->mMutex);
439 if (!this->isOutOfCore())
return;
441 std::unique_ptr<FileInfo> info(self->mFileInfo);
447 self->mData =
nullptr;
450 SharedPtr<std::streambuf>
buf = info->mapping->createBuffer();
451 std::istream is(buf.get());
456 is.seekg(info->maskpos);
459 is.seekg(info->bufpos);
462 self->setOutOfCore(
false);
467 template<
typename T, Index Log2Dim>
469 LeafBuffer<T, Log2Dim>::detachFromFile()
471 #ifdef OPENVDB_USE_DELAYED_LOADING
472 if (this->isOutOfCore()) {
475 this->setOutOfCore(
false);
487 template<Index Log2Dim>
499 static inline const bool sOn =
true;
500 static inline const bool sOff =
false;
515 if (mData.isOn(i))
return sOn;
else return sOff;
551 #endif // OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
Index memUsageIfLoaded() const
Index memUsageIfLoaded() const
GLenum GLuint GLenum GLsizei const GLchar * buf
LeafBuffer & operator=(const LeafBuffer &)
Copy the other buffer's values into this buffer.
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
void swap(UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &a, UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &b)
const NodeMaskType & storage() const
Return raw LeafBuffer data.
LeafBuffer(PartialCreate, const ValueType &)
Construct a buffer but don't allocate memory for the full array of values.
OPENVDB_API bool getHalfFloat(std::ios_base &)
Return true if floating-point values should be quantized to 16 bits when writing to the given stream ...
#define OPENVDB_USE_VERSION_NAMESPACE
const bool & getValue(Index i) const
bool operator==(const LeafBuffer &) const
Return true if the contents of the other buffer exactly equal the contents of this buffer...
void swap(LeafBuffer &other)
bool empty() const
Return true if memory for this buffer has not yet been allocated.
Tag dispatch class that distinguishes constructors during file input.
#define OPENVDB_ASSERT(X)
void readCompressedValues(std::istream &is, ValueT *destBuf, Index destCount, const MaskT &valueMask, bool fromHalf)
std::shared_ptr< T > SharedPtr
LeafBuffer()
Default constructor.
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation. ...
void swap(LeafBuffer &)
Exchange this buffer's values with the other buffer's values.
Templated block class to hold specific data types and a fixed number of values determined by Log2Dim...
#define OPENVDB_USE_DELAYED_LOADING
bool allocate()
Allocate memory for this buffer if it has not already been allocated.
static Index size()
Return the number of values contained in this buffer.
GLsizei GLsizei GLchar * source
static const Index32 WORD_COUNT
const ValueType & getValue(Index i) const
Return a const reference to the i'th element of this buffer.
LeafBuffer(const LeafBuffer &other)
Index memUsage() const
Return the memory footprint of this buffer in bytes.
WordType * data()
Return a pointer to the C-style array of words encoding the bits.
GLboolean GLboolean GLboolean b
typename NodeMaskType::Word WordType
OPENVDB_API void setStreamMetadataPtr(std::ios_base &, SharedPtr< StreamMetadata > &, bool transfer=true)
Associate the given stream with (a shared pointer to) an object that stores metadata (file format...
const ValueType * data() const
Return a const pointer to the array of voxel values.
const ValueType & operator[](Index i) const
Return a const reference to the i'th element of this buffer.
void fill(const ValueType &)
Populate this buffer with a constant value.
LeafBuffer(const NodeMaskType &other)
bool operator==(const LeafBuffer &other) const
bool isOutOfCore() const
Return true if this buffer's values have not yet been read from disk.
Array of fixed size 23Log2Dim that stores the voxel values of a LeafNode.
LeafBuffer & operator=(const LeafBuffer &b)
void setValue(Index i, bool val)
void setValue(Index i, const ValueType &)
Set the i'th value of this buffer to the specified value.
bool operator!=(const LeafBuffer &other) const
Return true if the contents of the other buffer are not exactly equal to the contents of this buffer...
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
const WordType * data() const
Return a const pointer to the C-style array of words encoding the bits.
const bool & operator[](Index i) const
bool operator!=(const LeafBuffer &other) const