4 #ifndef OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
5 #define OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
10 #include <tbb/spin_mutex.h>
16 #include <type_traits>
29 template<
typename T, Index Log2Dim>
38 #ifdef OPENVDB_USE_DELAYED_LOADING
41 FileInfo(): bufpos(0) , maskpos(0) {}
42 std::streamoff bufpos;
43 std::streamoff maskpos;
44 io::MappedFile::Ptr mapping;
52 #ifdef OPENVDB_USE_DELAYED_LOADING
63 #ifdef OPENVDB_USE_DELAYED_LOADING
73 #ifdef OPENVDB_USE_DELAYED_LOADING
74 return bool(mOutOfCore);
135 inline void setOutOfCore(
bool b)
138 #ifdef OPENVDB_USE_DELAYED_LOADING
144 inline void loadValues()
const
146 #ifdef OPENVDB_USE_DELAYED_LOADING
150 inline void doLoad()
const;
151 inline bool detachFromFile();
153 using FlagsType = std::atomic<Index32>;
155 #ifdef OPENVDB_USE_DELAYED_LOADING
163 FlagsType mOutOfCore;
164 tbb::spin_mutex mMutex;
167 friend class ::TestLeaf;
176 template<
typename T, Index Log2Dim>
181 #ifdef OPENVDB_USE_DELAYED_LOADING
188 template<
typename T, Index Log2Dim>
192 #ifdef OPENVDB_USE_DELAYED_LOADING
193 if (this->isOutOfCore()) {
194 this->detachFromFile();
204 template<
typename T, Index Log2Dim>
209 , mOutOfCore(other.mOutOfCore.load())
212 #ifdef OPENVDB_USE_DELAYED_LOADING
214 mFileInfo =
new FileInfo(*other.mFileInfo);
217 if (other.mData !=
nullptr) {
222 while (n--) *target++ = *source++;
224 #ifdef OPENVDB_USE_DELAYED_LOADING
230 template<
typename T, Index Log2Dim>
236 if (mData) mData[i] =
val;
240 template<
typename T, Index Log2Dim>
244 if (&other !=
this) {
245 #ifdef OPENVDB_USE_DELAYED_LOADING
246 if (this->isOutOfCore()) {
247 this->detachFromFile();
252 mOutOfCore.store(other.mOutOfCore.load(std::memory_order_acquire),
253 std::memory_order_release);
254 mFileInfo =
new FileInfo(*other.mFileInfo);
257 if (other.mData !=
nullptr) {
262 while (n--) *target++ = *source++;
264 #ifdef OPENVDB_USE_DELAYED_LOADING
272 template<
typename T, Index Log2Dim>
276 this->detachFromFile();
277 if (mData !=
nullptr) {
280 while (n--) *target++ =
val;
285 template<
typename T, Index Log2Dim>
292 if (!target && !source)
return true;
293 if (!target || !source)
return false;
300 template<
typename T, Index Log2Dim>
305 #ifdef OPENVDB_USE_DELAYED_LOADING
310 auto tmp = other.mOutOfCore.load(std::memory_order_acquire);
311 tmp = mOutOfCore.exchange(std::move(tmp));
312 other.mOutOfCore.store(std::move(tmp), std::memory_order_release);
317 template<
typename T, Index Log2Dim>
321 size_t n =
sizeof(*this);
322 #ifdef OPENVDB_USE_DELAYED_LOADING
323 if (this->isOutOfCore()) n +=
sizeof(FileInfo);
327 #ifdef OPENVDB_USE_DELAYED_LOADING
330 return static_cast<Index>(
n);
334 template<
typename T, Index Log2Dim>
338 size_t n =
sizeof(*this);
340 return static_cast<Index>(
n);
344 template<
typename T, Index Log2Dim>
349 if (mData ==
nullptr) {
351 #ifdef OPENVDB_USE_DELAYED_LOADING
353 tbb::spin_mutex::scoped_lock lock(self->mMutex);
360 template<
typename T, Index Log2Dim>
365 if (mData ==
nullptr) {
366 #ifdef OPENVDB_USE_DELAYED_LOADING
368 tbb::spin_mutex::scoped_lock lock(mMutex);
376 template<
typename T, Index Log2Dim>
380 static const ValueType sZero = zeroVal<T>();
385 if (mData)
return mData[i];
else return sZero;
389 template<
typename T, Index Log2Dim>
391 LeafBuffer<T, Log2Dim>::deallocate()
394 if (mData !=
nullptr) {
395 #ifdef OPENVDB_USE_DELAYED_LOADING
396 if (this->isOutOfCore())
return false;
406 template<
typename T, Index Log2Dim>
408 LeafBuffer<T, Log2Dim>::doLoad()
const
410 #ifdef OPENVDB_USE_DELAYED_LOADING
411 if (!this->isOutOfCore())
return;
413 LeafBuffer<T, Log2Dim>*
self =
const_cast<LeafBuffer<T, Log2Dim>*
>(
this);
417 tbb::spin_mutex::scoped_lock lock(self->mMutex);
418 if (!this->isOutOfCore())
return;
420 std::unique_ptr<FileInfo> info(self->mFileInfo);
421 assert(info.get() !=
nullptr);
422 assert(info->mapping.get() !=
nullptr);
423 assert(info->meta.get() !=
nullptr);
426 self->mData =
nullptr;
429 SharedPtr<std::streambuf>
buf = info->mapping->createBuffer();
430 std::istream is(buf.get());
435 is.seekg(info->maskpos);
438 is.seekg(info->bufpos);
441 self->setOutOfCore(
false);
446 template<
typename T, Index Log2Dim>
448 LeafBuffer<T, Log2Dim>::detachFromFile()
450 #ifdef OPENVDB_USE_DELAYED_LOADING
451 if (this->isOutOfCore()) {
454 this->setOutOfCore(
false);
466 template<Index Log2Dim>
478 static inline const bool sOn =
true;
479 static inline const bool sOff =
false;
494 if (mData.isOn(i))
return sOn;
else return sOff;
527 #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)
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.
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