31 #ifndef OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED 32 #define OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED 37 #include <tbb/atomic.h> 38 #include <tbb/spin_mutex.h> 43 #include <type_traits> 68 struct LeafBufferFlags
71 struct Atomic {
union { T*
data;
void*
ptr; }; tbb::atomic<Index32>
i; tbb::spin_mutex
mutex; };
74 #ifndef __INTEL_COMPILER 75 static constexpr
bool IsAtomic = ((
sizeof(Atomic) ==
sizeof(NonAtomic))
77 && (offsetof(Atomic,
i) == offsetof(NonAtomic,
i)));
82 static constexpr
bool IsAtomic = (
sizeof(Atomic) ==
sizeof(NonAtomic));
84 static constexpr
size_t size =
sizeof(Atomic);
87 using type =
typename std::conditional<IsAtomic, tbb::atomic<Index32>,
Index32>::type;
95 template<
typename T, Index Log2Dim>
102 static const Index SIZE = 1 << 3 * Log2Dim;
104 #ifndef OPENVDB_2_ABI_COMPATIBLE 115 #ifdef OPENVDB_2_ABI_COMPATIBLE 119 explicit LeafBuffer(
const ValueType& val): mData(new ValueType[SIZE]) { this->fill(val); }
121 LeafBuffer(
const LeafBuffer& other): mData(new ValueType[SIZE]) { *
this = other; }
123 ~LeafBuffer() {
delete[] mData; }
126 bool isOutOfCore()
const {
return false; }
128 bool empty()
const {
return (mData ==
nullptr); }
130 inline LeafBuffer(): mData(new ValueType[SIZE]), mOutOfCore(0) {}
133 explicit inline LeafBuffer(
const ValueType&);
135 inline LeafBuffer(
const LeafBuffer&);
144 bool empty()
const {
return !mData || this->isOutOfCore(); }
146 bool allocate() {
if (mData ==
nullptr) mData =
new ValueType[SIZE];
return true; }
150 inline void fill(
const ValueType&);
157 inline void setValue(
Index i,
const ValueType&);
173 inline Index memUsage()
const;
180 const ValueType*
data()
const;
188 inline const ValueType& at(
Index i)
const;
195 ValueType& operator[](
Index i) {
return const_cast<ValueType&
>(this->at(
i)); }
199 #ifdef OPENVDB_2_ABI_COMPATIBLE 200 void setOutOfCore(
bool) {}
201 void loadValues()
const {}
202 void doLoad()
const {}
203 bool detachFromFile() {
return false; }
205 inline void setOutOfCore(
bool b) { mOutOfCore = b; }
208 inline void loadValues()
const {
if (this->isOutOfCore()) this->doLoad(); }
209 inline void doLoad()
const;
210 inline bool detachFromFile();
214 #ifdef OPENVDB_2_ABI_COMPATIBLE 217 using FlagsType =
typename LeafBufferFlags<ValueType>::type;
223 FlagsType mOutOfCore;
224 tbb::spin_mutex mMutex;
227 static const ValueType sZero;
230 friend class ::TestLeaf;
239 #ifndef OPENVDB_2_ABI_COMPATIBLE 241 template<
typename T, Index Log2Dim>
245 template<
typename T, Index Log2Dim>
255 template<
typename T, Index Log2Dim>
259 if (this->isOutOfCore()) {
260 this->detachFromFile();
267 template<
typename T, Index Log2Dim>
271 , mOutOfCore(other.mOutOfCore)
274 mFileInfo =
new FileInfo(*other.
mFileInfo);
275 }
else if (other.
mData !=
nullptr) {
280 while (n--) *target++ = *source++;
284 #endif // !OPENVDB_2_ABI_COMPATIBLE 287 template<
typename T, Index Log2Dim>
292 #ifdef OPENVDB_2_ABI_COMPATIBLE 296 if (mData) mData[
i] = val;
301 template<
typename T, Index Log2Dim>
305 if (&other !=
this) {
306 #ifdef OPENVDB_2_ABI_COMPATIBLE 307 if (other.
mData !=
nullptr) {
312 while (n--) *target++ = *source++;
314 #else // ! OPENVDB_2_ABI_COMPATIBLE 315 if (this->isOutOfCore()) {
316 this->detachFromFile();
321 mOutOfCore = other.mOutOfCore;
322 mFileInfo =
new FileInfo(*other.
mFileInfo);
323 }
else if (other.
mData !=
nullptr) {
328 while (n--) *target++ = *source++;
336 template<
typename T, Index Log2Dim>
340 this->detachFromFile();
341 if (mData !=
nullptr) {
344 while (n--) *target++ = val;
349 template<
typename T, Index Log2Dim>
356 if (!target && !source)
return true;
357 if (!target || !source)
return false;
364 template<
typename T, Index Log2Dim>
368 std::swap(mData, other.
mData);
369 #ifndef OPENVDB_2_ABI_COMPATIBLE 370 std::swap(mOutOfCore, other.mOutOfCore);
375 template<
typename T, Index Log2Dim>
379 size_t n =
sizeof(*this);
380 #ifdef OPENVDB_2_ABI_COMPATIBLE 381 if (mData) n += SIZE *
sizeof(
ValueType);
383 if (this->isOutOfCore()) n +=
sizeof(FileInfo);
384 else if (mData) n += SIZE *
sizeof(
ValueType);
386 return static_cast<Index>(n);
390 template<
typename T, Index Log2Dim>
394 #ifndef OPENVDB_2_ABI_COMPATIBLE 396 if (mData ==
nullptr) {
399 tbb::spin_mutex::scoped_lock lock(self->mMutex);
400 if (mData ==
nullptr)
self->mData =
new ValueType[SIZE];
406 template<
typename T, Index Log2Dim>
410 #ifndef OPENVDB_2_ABI_COMPATIBLE 412 if (mData ==
nullptr) {
414 tbb::spin_mutex::scoped_lock lock(mMutex);
415 if (mData ==
nullptr) mData =
new ValueType[SIZE];
422 template<
typename T, Index Log2Dim>
427 #ifdef OPENVDB_2_ABI_COMPATIBLE 433 if (mData)
return mData[
i];
else return sZero;
438 template<
typename T, Index Log2Dim>
440 LeafBuffer<T, Log2Dim>::deallocate()
442 if (mData !=
nullptr && !this->isOutOfCore()) {
451 #ifndef OPENVDB_2_ABI_COMPATIBLE 453 template<
typename T, Index Log2Dim>
455 LeafBuffer<T, Log2Dim>::doLoad()
const 457 if (!this->isOutOfCore())
return;
459 LeafBuffer<T, Log2Dim>*
self =
const_cast<LeafBuffer<T, Log2Dim>*
>(
this);
463 tbb::spin_mutex::scoped_lock lock(self->mMutex);
464 if (!this->isOutOfCore())
return;
466 std::unique_ptr<FileInfo> info(self->mFileInfo);
467 assert(info.get() !=
nullptr);
468 assert(info->mapping.get() !=
nullptr);
469 assert(info->meta.get() !=
nullptr);
472 self->mData =
nullptr;
475 SharedPtr<std::streambuf> buf = info->mapping->createBuffer();
476 std::istream is(buf.get());
481 is.seekg(info->maskpos);
484 is.seekg(info->bufpos);
487 self->setOutOfCore(
false);
491 template<
typename T, Index Log2Dim>
493 LeafBuffer<T, Log2Dim>::detachFromFile()
495 if (this->isOutOfCore()) {
498 this->setOutOfCore(
false);
504 #endif // !OPENVDB_2_ABI_COMPATIBLE 511 template<Index Log2Dim>
520 static const Index WORD_COUNT = NodeMaskType::WORD_COUNT;
521 static const Index SIZE = 1 << 3 * Log2Dim;
532 void fill(
bool val) { mData.set(val); }
540 if (mData.isOn(
i))
return sOn;
else return sOff;
580 #endif // OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED void * ptr
Definition: LeafBuffer.h:71
LeafBuffer(bool on)
Definition: LeafBuffer.h:528
std::shared_ptr< T > SharedPtr
Definition: Types.h:130
bool ValueType
Definition: LeafBuffer.h:517
void swap(LeafBuffer &other)
Definition: LeafBuffer.h:549
void setValue(Index i, bool val)
Definition: LeafBuffer.h:547
uint32_t Index32
Definition: Types.h:55
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:391
static Index size()
Definition: LeafBuffer.h:552
tbb::atomic< Index32 > i
Definition: LeafBuffer.h:71
void fill(bool val)
Definition: LeafBuffer.h:532
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...
T * data
Definition: LeafBuffer.h:71
static const bool sOn
Definition: LeafBuffer.h:524
tbb::spin_mutex mutex
Definition: LeafBuffer.h:71
static constexpr bool IsAtomic
true if LeafBuffer::mOutOfCore is atomic, false otherwise
Definition: LeafBuffer.h:76
Index32 Index
Definition: Types.h:57
Tag dispatch class that distinguishes constructors during file input.
Definition: Types.h:505
ValueType * mData
Definition: LeafBuffer.h:220
std::streamoff bufpos
Definition: LeafBuffer.h:108
bool operator==(const LeafBuffer &other) const
Definition: LeafBuffer.h:544
Templated block class to hold specific data types and a fixed number of values determined by Log2Dim...
Definition: LeafNode.h:61
WordType * data()
Return a pointer to the C-style array of words encoding the bits.
Definition: LeafBuffer.h:556
typename NodeMaskType::Word WordType
Definition: LeafBuffer.h:516
WordType StorageType
Definition: LeafBuffer.h:518
#define OPENVDB_VERSION_NAME
Definition: version.h:43
LeafBuffer & operator=(const LeafBuffer &b)
Definition: LeafBuffer.h:533
LeafBuffer(const NodeMaskType &other)
Definition: LeafBuffer.h:529
static const bool sOff
Definition: LeafBuffer.h:525
LeafBuffer()
Definition: LeafBuffer.h:527
bool empty() const
Return true if memory for this buffer has not yet been allocated.
Definition: LeafBuffer.h:144
ValueType StorageType
Definition: LeafBuffer.h:100
void readCompressedValues(std::istream &is, ValueT *destBuf, Index destCount, const MaskT &valueMask, bool fromHalf)
Definition: Compression.h:339
Definition: Exceptions.h:39
Array of fixed size that stores the voxel values of a LeafNode.
Definition: LeafBuffer.h:96
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:487
const WordType * data() const
Return a const pointer to the C-style array of words encoding the bits.
Definition: LeafBuffer.h:559
io::MappedFile::Ptr mapping
Definition: LeafBuffer.h:110
Definition: LeafBuffer.h:105
ValueType ValueType
Definition: LeafBuffer.h:99
FileInfo * mFileInfo
Definition: LeafBuffer.h:221
SharedPtr< MappedFile > Ptr
Definition: io.h:152
const bool & getValue(Index i) const
Definition: LeafBuffer.h:535
LeafBuffer(PartialCreate, const ValueType &)
Construct a buffer but don't allocate memory for the full array of values.
Definition: LeafBuffer.h:137
~LeafBuffer()
Definition: LeafBuffer.h:531
Index64 Word
Definition: NodeMasks.h:316
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...
Definition: LeafBuffer.h:167
SharedPtr< io::StreamMetadata > meta
Definition: LeafBuffer.h:111
const ValueType & operator[](Index i) const
Return a const reference to the i'th element of this buffer.
Definition: LeafBuffer.h:155
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation. ...
Definition: NodeMasks.h:307
Index memUsage() const
Definition: LeafBuffer.h:551
const ValueType & getValue(Index i) const
Return a const reference to the i'th element of this buffer.
Definition: LeafBuffer.h:153
LeafBuffer(const LeafBuffer &other)
Definition: LeafBuffer.h:530
static Index size()
Return the number of values contained in this buffer.
Definition: LeafBuffer.h:175
const bool & operator[](Index i) const
Definition: LeafBuffer.h:542
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
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 ...
bool isOutOfCore() const
Return true if this buffer's values have not yet been read from disk.
Definition: LeafBuffer.h:142
static constexpr size_t size
The size of a LeafBuffer when LeafBuffer::mOutOfCore is atomic.
Definition: LeafBuffer.h:85
bool operator!=(const LeafBuffer &other) const
Definition: LeafBuffer.h:545
FileInfo()
Definition: LeafBuffer.h:107
std::streamoff maskpos
Definition: LeafBuffer.h:109